-
Notifications
You must be signed in to change notification settings - Fork 3
/
mobilenet.py
106 lines (91 loc) · 3.18 KB
/
mobilenet.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
from keras import Sequential, Model
from keras.callbacks import ModelCheckpoint
from keras.layers import Flatten, Dense, Dropout
from tensorflow.keras.applications import MobileNet
import numpy as np
import tensorflow as tf
import os
import matplotlib.pyplot as plt
from tensorflow.keras.utils import plot_model
from time import *
cla = os.listdir('./train')
train_path = './train'
val_path = './val'
test_path = './test'
train_data = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255, # 归一化
rotation_range=40, # 旋转
width_shift_range=0.2, # 水平偏移
height_shift_range=0.2, # 垂直偏移
shear_range=0.2, # 剪切强度
zoom_range=0.2, # 缩放强度
horizontal_flip=True # 水平翻转
)
val_data = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255
)
test_data = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255
)
train_generator = train_data.flow_from_directory(
train_path,
target_size=(224, 224),
batch_size=128,
class_mode='categorical',
save_to_dir='./new',
save_format='jpg'
)
val_generator = val_data.flow_from_directory(
val_path,
target_size=(224, 224),
batch_size=1,
class_mode='categorical'
)
test_generator = test_data.flow_from_directory(
test_path,
target_size=(224, 224),
batch_size=1,
class_mode='categorical'
)
def mobilenet(train_generator, val_generator, save_model_path = './model/'+'mobilenet.h5', log_dir = './logs/mobilenet'):
base_model = MobileNet(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(6, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in base_model.layers:
layer.trainable = False
model.summary()
plot_model(model, to_file='./mobilenet/mobilenet_bottleneck.png', show_shapes=True)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
tensorboard = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
checkpoint = ModelCheckpoint(filepath=save_model_path, monitor='acc', mode='auto', save_best_only='True')
model.fit_generator(
generator=train_generator,
epochs=10,
validation_data=val_generator,
callbacks=[tensorboard, checkpoint],
)
model.save(save_model_path)
return model
def evaluate_model(test_data, save_model_path):
model = tf.keras.models.load_model(save_model_path)
loss, accuracy = model.evaluate_generator(test_data)
print('\n Loss:%.2f,Accuracy:%.2f%%' % (loss, accuracy*100))
s = 0
for i in range(len(test_data)):
image, label = test_data.next()
begin = time()
predict = np.argmax(model.predict(image))
end = time()
runtime = end - begin
s = s + runtime
# plt.figure()
# plt.imshow(np.squeeze(image))
# plt.title(cla[predict])
# plt.show()
print('平均运行时间为:', s / len(test_data))
# mobilenet(train_generator, val_generator)
evaluate_model(test_generator, './model/mobilenet.h5')