-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.py
42 lines (36 loc) · 1.19 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from keras.applications import inception_v3
from keras.models import load_model
from tqdm import tqdm
from util import get_labels, get_images
import csv
import numpy as np
# Define constants
INPUT_SIZE = 299
fname = 'model1_finetune.h5'
nr_predictions = 10357
# Get ids, label names and images
print('Load data...')
labels = get_labels().sort_values(by=['breed']).breed.unique()
ids = []
images = np.zeros((nr_predictions, INPUT_SIZE, INPUT_SIZE, 3), dtype='float16')
for i, (img, img_id) in tqdm(enumerate(get_images('test', INPUT_SIZE))):
x = inception_v3.preprocess_input(np.expand_dims(img, axis=0))
images[i] = x
ids.append(img_id)
# Load model weights
print(f'Load model from {fname}')
model = load_model(fname)
# Make predictions on input images
print('Predict...')
predictions = model.predict(images, verbose=1)
# Save to csv
print('Saving predictions...')
with open('predictions.csv', 'w') as csvfile:
fieldnames = ['id'] + [l for l in labels]
writer = csv.writer(csvfile)
writer.writerow(fieldnames)
for i, prediction in enumerate(predictions):
row = [ids[i]] + [f'{p:.10f}' for p in prediction]
writer.writerow(row)