-
Notifications
You must be signed in to change notification settings - Fork 0
/
cla.py
executable file
·151 lines (114 loc) · 4.6 KB
/
cla.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
# coding:utf-8
import keras
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from keras import backend as K
from keras import optimizers
import numpy as np
import argparse
import sys
import mulNet
import load_data
# dimensions of our images.
img_width, img_height = 224, 224
nb_train_samples = 1126
# nb_validation_samples = 60
epochs = 10
batch_size = 32
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
# model = mulNet.build_normal(img_width, img_height)
base_model, model = mulNet.build_vgg_raw(img_width, img_height)
# print(model.summary())
def train(X_train, X_test, y_train, y_test):
# opt = optimizers.RMSprop(lr=0.001 ,decay=1e-6)
# model.compile(loss='categorical_crossentropy', # 多分类
# optimizer=opt, # 'rmsprop'
# # loss_weights=[0.1, 0.9],
# metrics=['accuracy'])
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rotation_range=30,
# rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
width_shift_range = 0.1,
height_shift_range = 0.1,
fill_mode = "nearest"
)
train_generator = train_datagen.flow(X_train, y_train, batch_size=32, shuffle=True, seed=None)
val_generator = ImageDataGenerator().flow(X_test, y_test, batch_size=32, shuffle=True, seed=None)
# train_generator = train_datagen.flow_from_directory(
# train_data_dir,
# target_size=(img_width, img_height),
# batch_size=batch_size,
# class_mode='categorical')
# val_generator = val_datagen.flow_from_directory(
# val_data_dir,
# target_size=(img_width, img_height),
# batch_size=batch_size,
# class_mode='categorical')
print('训练顶层分类器')
for layer in base_model.layers:
layer.trainable = False
opt = optimizers.Adam(lr=1e-4 ,decay=1e-6, amsgrad=True)
model.compile(loss='categorical_crossentropy', # 多分类
optimizer=opt, # 'rmsprop'
# loss_weights=[0.1, 0.9],
metrics=['accuracy'])
history_t1 = model.fit_generator(
train_generator,
validation_data=val_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs)
print('对顶层分类器fine-tune')
for layer in model.layers[:11]:
layer.trainable = False
for layer in model.layers[11:]:
layer.trainable = True
opt = optimizers.SGD(lr=1e-5, momentum=0.9)
model.compile(loss='categorical_crossentropy', # 多分类
optimizer=opt, # 'rmsprop'
# loss_weights=[0.1, 0.9],
metrics=['accuracy'])
history_ft = model.fit_generator(
train_generator,
validation_data=val_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs)
model.save('first_blood.h5')
# plot_training(history_ft)
def plot_training(history):
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r.')
plt.plot(epochs, val_acc, 'r')
plt.title('Training and validation accuracy')
plt.figure()
plt.plot(epochs, loss, 'r.')
plt.plot(epochs, val_loss, 'r-')
plt.title('Training and validation loss')
plt.show()
if __name__=='__main__':
arg = argparse.ArgumentParser(description='Process the input_output path.')
arg.add_argument("-path", "--dataset_path", default='./birds/train',
help="path to input dataset_train")
# arg.add_argument("-dtrain", "--dataset_train", default='./birds/train',
# help="path to input dataset_train")
# arg.add_argument("-dval", "--dataset_val", default='./birds/val',
# help="path to input dataset_val")
args = arg.parse_args()
# train_data_dir = vars(args)['dataset_train'] # './birds/train'
# val_data_dir = vars(args)["dataset_val"] # './birds/val'
train_data_dir = vars(args)['dataset_path']
train_data, train_labels = load_data.load_data(img_width, img_height, train_data_dir)
X_train, X_test, y_train, y_test = train_test_split(train_data, train_labels, test_size = 0.3, random_state = 42)
train(X_train, X_test, y_train, y_test)
# score = model.evaluate(X_test, y_test, batch_size=32)