-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgap_predict.py
154 lines (121 loc) · 4.66 KB
/
gap_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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# coding:utf-8
import h5py
import numpy as np
from sklearn.utils import shuffle
from keras.models import *
from keras.layers import *
from keras.utils import np_utils
from keras.optimizers import *
from keras.layers import *
from keras.preprocessing.image import ImageDataGenerator
import pandas as pd
np.random.seed(2018)
csv_sample_path = './predict_csv/sample_submission.csv'
csv_out_path = './predict_csv/13_hdropori_1024_10000.csv'
# test_data_dir = '/home/fenglf/data/dog/kaggle/cropped_test_jpg'
test_data_dir = '/home/fenglf/data/dog/kaggle/test'
h5path = '/home/fenglf/PycharmProjects/keras-finetuning-master/gap_feature/'
model_best_path = './model/output/mul/model_13_hdropori_1024_best.h5'
# model_json_path = './model/output/mul/model_13_ndropcop_2048.json'
nb_classes = 120
batch_size = 32
# img_height, img_width = 299, 299
eval_switch = True
bottleneck_feature_test_list = ["gap_InceptionResNetV2_test.h5",
"gap_DenseNet121_test.h5",
"gap_DenseNet169_test.h5",
"gap_DenseNet161_test.h5",
"gap_DenseNet201_test.h5",
"gap_Xception_test.h5",
"gap_ResNet50_test.h5",
"gap_ResNet101_test.h5",
"gap_ResNet152_test.h5",
"gap_InceptionV3_test.h5",
"gap_InceptionV4_test.h5",
"gap_VGG16_test.h5",
"gap_VGG19_test.h5"]
def predict():
print("start...")
test_datagen = ImageDataGenerator(rescale=1. / 255)
print "test_generator creating..."
# 注意:使用此方法时,test_data_dir必须有子文件夹
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
shuffle=False,
class_mode=None)
# define the input_shape
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
# setup model
# load json and create model
json_file = open('model_json_3.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights("model_3.h5")
print("Loaded model from disk")
# if os.path.exists(final_weights_path):
# model.load_weights(final_weights_path)
# print ("Checkpoint " + final_weights_path + " loaded.")
print 'predicting...'
# print test_generator.filenames
test = model.predict_generator(
test_generator,
steps=(test_generator.samples // batch_size) + 1, # 25
verbose=1)
print test
# y_pred = model.predict(X_test, verbose=1)
# top1_class = y_pred.max(axis=1)
# print top1_class
def gap_predict():
X_test = []
for filename in bottleneck_feature_test_list:
with h5py.File(h5path + filename, 'r') as h:
X_test.append(np.array(h['test']))
# setup model
# load json and create model
# json_file = open(model_json_path, 'r')
# loaded_model_json = json_file.read()
# json_file.close()
# model = model_from_json(loaded_model_json)
# # load weights into new model
# model.load_weights(model_best_path)
model = load_model(model_best_path)
print("Loaded model from {}".format(model_best_path))
# 拼接特征
X_test = np.concatenate(X_test, axis=1)
res0 = model.predict(X_test)
return res0
def write_csv(y_pred):
n = 0
gen = ImageDataGenerator()
test_generator = gen.flow_from_directory(test_data_dir, shuffle=False, batch_size=batch_size, class_mode=None)
if os.path.exists(csv_out_path):
os.remove(csv_out_path)
with open(csv_sample_path, 'r') as f:
id = f.readline()
# print id
with open(csv_out_path, 'a') as f:
f.writelines(id)
for i, file_dir in enumerate(test_generator.filenames):
file_name = file_dir.split('/')[-1]
file_name, file_ext = file_name.split('.')
# print file_name, file_ext
pred_test = y_pred[i]
if file_ext == 'png' or file_ext == 'jpg':
f.write(file_name)
for str in pred_test:
f.write(',')
# f.write(str)
f.write(str.astype("str"))
f.write('\n')
n += 1
print("count_image:", n)
if __name__ == '__main__':
y_pred = gap_predict()
write_csv(y_pred)