-
Notifications
You must be signed in to change notification settings - Fork 113
/
predict_average_augmentation.py
69 lines (54 loc) · 2.2 KB
/
predict_average_augmentation.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from keras.models import load_model
import os
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
img_width = 299
img_height = 299
batch_size = 32
nbr_test_samples = 1000
nbr_augmentation = 5
FishNames = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT']
#root_path = '/Users/pengpai/Desktop/python/DeepLearning/Kaggle/NCFM'
root_path = '/data1/home/waynema/popeyepeng/NCFM'
weights_path = os.path.join(root_path, 'weights.h5')
test_data_dir = os.path.join(root_path, 'data/test_stg1/')
# test data generator for prediction
test_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.1,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
print('Loading model and weights from training process ...')
InceptionV3_model = load_model(weights_path)
for idx in range(nbr_augmentation):
print('{}th augmentation for testing ...'.format(idx))
random_seed = np.random.random_integers(0, 100000)
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
shuffle = False, # Important !!!
seed = random_seed,
classes = None,
class_mode = None)
test_image_list = test_generator.filenames
#print('image_list: {}'.format(test_image_list[:10]))
print('Begin to predict for testing data ...')
if idx == 0:
predictions = InceptionV3_model.predict_generator(test_generator, nbr_test_samples)
else:
predictions += InceptionV3_model.predict_generator(test_generator, nbr_test_samples)
predictions /= nbr_augmentation
print('Begin to write submission file ..')
f_submit = open(os.path.join(root_path, 'submit.csv'), 'w')
f_submit.write('image,ALB,BET,DOL,LAG,NoF,OTHER,SHARK,YFT\n')
for i, image_name in enumerate(test_image_list):
pred = ['%.6f' % p for p in predictions[i, :]]
if i % 100 == 0:
print('{} / {}'.format(i, nbr_test_samples))
f_submit.write('%s,%s\n' % (os.path.basename(image_name), ','.join(pred)))
f_submit.close()
print('Submission file successfully generated!')