-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclassify.py
152 lines (130 loc) · 5.94 KB
/
classify.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
import numpy as np
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img
from keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Dropout
from keras.models import Sequential
from keras.models import model_from_json
from keras.preprocessing import image
root_path = '.'
labels = {0: 'cardboard', 1: 'glass', 2: 'metal', 3: 'paper', 4: 'plastic', 5: 'trash'}
class Model:
def __init__(self):
self.model = None
def build_model(self):
self.model = Sequential()
self.model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=(300, 300, 3)))
self.model.add(MaxPooling2D(pool_size=2))
self.model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
self.model.add(MaxPooling2D(pool_size=2))
self.model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
self.model.add(MaxPooling2D(pool_size=2))
self.model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
self.model.add(MaxPooling2D(pool_size=2))
self.model.add(Flatten()) # 扁平化参数
self.model.add(Dense(64, activation='relu'))
self.model.add(Dense(6, activation='softmax'))
self.model.summary()
def train_model(self):
# 优化器, 主要有Adam、sgd、rmsprop等方式。
# sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
self.model.compile(loss='categorical_crossentropy',
optimizer='adam', # sgd 'adam'
metrics=['accuracy'])
# 自动扩充训练样本
train_datagen = ImageDataGenerator(
rescale=1. / 255, # 数据缩放,把像素点的值除以255,使之在0到1之间
shear_range=0.1, # 错切变换角度
zoom_range=0.1, # 随机缩放范围
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True,
validation_split=0.1
)
# 生成验证集
val_datagen = ImageDataGenerator(
rescale=1. / 255, validation_split=0.1)
# 以文件分类名划分label
train_generator = train_datagen.flow_from_directory(
root_path + '/dataset',
# 整数元组 (height, width),默认:(300, 300)。 所有的图像将被调整到的尺寸。
target_size=(300, 300),
# 一批数据的大小
batch_size=16,
# "categorical", "binary", "sparse", "input" 或 None 之一。
# 默认:"categorical",返回one-hot 编码标签。
class_mode='categorical',
subset='training',
seed=0)
val_generator = val_datagen.flow_from_directory(
root_path + '/dataset',
target_size=(300, 300),
batch_size=16,
class_mode='categorical',
subset='validation',
seed=0)
# 编译模型
try:
history_fit = self.model.fit_generator(train_generator,
epochs=100, # 迭代总轮数
steps_per_epoch=2276 // 32, # generator 产生的总步数/(批次样本)
validation_data=val_generator, #验证数据的生成器
validation_steps=251 // 32)
with open(root_path + "/model/history_fit.json", "w") as json_file:
json_file.write(str(history_fit))
acc = history_fit.history['accuracy']
val_acc = history_fit.history['val_accuracy']
loss = history_fit.history['loss']
val_loss = history_fit.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.figure("acc")
plt.plot(epochs, acc, 'r-', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='validation acc')
plt.title('The comparision of train_acc and val_acc')
plt.legend()
plt.show()
plt.figure("loss")
plt.plot(epochs, loss, 'r-', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='validation loss')
plt.title('The comparision of train_loss and val_loss')
plt.legend()
plt.show()
except StopIteration:
pass
def save_model(self):
model_json = self.model.to_json()
with open(root_path + '/model/model_json.json', "w") as json_file:
json_file.write(model_json)
self.model.save_weights(root_path + '/model/model_weight.h5')
self.model.save(root_path + '/model/model.h5')
print('model saved')
def load_model(self):
json_file = open(root_path + '/data/weight/model_json.json') # 加载模型结构文件
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json) # 结构文件转化为模型
# 加载权重
model.load_weights(root_path + '/data/weight/path_to_my_model.h5') # h5文件保存模型的权重数据
return model
def generate_result(result):
for i in range(6):
if (result[0][i] == 1):
return labels[i]
if __name__ == '__main__':
model = Model()
model.build_model()
print('model built')
model.train_model()
print('model trained')
model.save_model()
print('model saved')
# 已经有模型,直接加载,注释掉上面三个函数
model1 = model.load_model()
print('model loaded')
img_path = "./dataset-resized/glass/glass2.jpg"
# 把图片转换成为numpy数组
img = image.load_img(img_path, target_size=(300, 300))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
result = model1.predict(img)
print(generate_result(result))