-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtrain.py
194 lines (161 loc) · 7.41 KB
/
train.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from keras.layers.core import Lambda
from keras.applications.mobilenet_v2 import MobileNetV2
from keras.applications.inception_v3 import InceptionV3
from keras.applications.densenet import DenseNet121
from keras.applications.vgg16 import VGG16
from keras.applications.resnet50 import ResNet50
from keras.applications.xception import Xception
from keras.applications.mobilenet import MobileNet
from keras.layers import *
from keras.models import Model
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import *
from keras.utils import multi_gpu_model
from matplotlib import pyplot as plt
from keras.models import load_model
import keras.backend as K
import os
def Global_attention_block(inputs):
shape=K.int_shape(inputs)
x=AveragePooling2D(pool_size=(shape[1],shape[2])) (inputs)
x=Conv2D(shape[3],1, padding='same') (x)
x=Activation('relu') (x)
x=Conv2D(shape[3],1, padding='same') (x)
x=Activation('sigmoid') (x)
C_A=Multiply()([x,inputs])
x=Lambda(lambda x: K.mean(x,axis=-1,keepdims=True)) (C_A)
x=Activation('sigmoid') (x)
S_A=Multiply()([x,C_A])
return S_A
def Category_attention_block(inputs,classes,k):
shape=K.int_shape(inputs)
F=Conv2D(k*classes,1, padding='same') (inputs)
F=BatchNormalization() (F)
F1=Activation('relu') (F)
F2=F1
x=GlobalMaxPool2D()(F2)
x=Reshape((classes,k)) (x)
S=Lambda(lambda x: K.mean(x,axis=-1,keepdims=False)) (x)
x=Reshape((shape[1],shape[2],classes,k)) (F1)
x=Lambda(lambda x: K.mean(x,axis=-1,keepdims=False)) (x)
x=Multiply()([S,x])
M=Lambda(lambda x: K.mean(x,axis=-1,keepdims=True)) (x)
semantic=Multiply()([inputs,M])
return semantic
def smooth_curve(points, factor=0.9):
smoothed_points = []
for point in points:
if smoothed_points:
previous = smoothed_points[-1]
smoothed_points.append(previous * factor + point * (1 - factor))
else:
smoothed_points.append(point)
return smoothed_points
def plotmodel(history,name):
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.figure(1)
plt.plot(epochs,smooth_curve(acc))
plt.plot(epochs,smooth_curve(val_acc))
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['train_acc', 'val_acc'], loc='upper left')
plt.savefig('acc_'+name+'.png')
plt.figure(2)
plt.plot(epochs,smooth_curve(loss))
plt.plot(epochs,smooth_curve(val_loss))
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train_loss', 'val_loss'], loc='upper right')
plt.savefig('loss_'+name+'.png')
def label_smooth(y_true, y_pred):
y_true=((1-0.1)*y_true+0.05)
return K.categorical_crossentropy(y_true, y_pred)
def get_base_model(model_name,image_size):
if model_name =='vgg16':
base_model=VGG16 (include_top=False,weights='imagenet',input_shape=(image_size,image_size,3))
if model_name =='resnet50':
base_model=ResNet50 (include_top=False,weights='imagenet',input_shape=(image_size,image_size,3))
if model_name =='xception':
base_model=Xception (include_top=False, weights='imagenet',input_shape=(image_size,image_size,3))
if model_name =='densenet121':
base_model=DenseNet121 (include_top=False, weights='imagenet',input_shape=(image_size,image_size,3))
if model_name =='mobilenet0.75':
base_model=MobileNet (include_top=False,weights='imagenet',alpha=0.75,input_shape=(image_size,image_size,3))
if model_name =='mobilenet1.0':
base_model=MobileNet (include_top=False,weights='imagenet',alpha=1.0,input_shape=(image_size,image_size,3))
if model_name =='mobilenetv2':
base_model=MobileNetV2 (include_top=False,weights='imagenet',alpha=1.0,input_shape=(image_size,image_size,3))
if model_name =='inceptionv3':
base_model=InceptionV3 (include_top=False,weights='imagenet',input_shape=(image_size,image_size,3))
if model_name =='inceptionv2':
base_model=InceptionResNetV2 (include_top=False, weights='imagenet',input_shape=(image_size,image_size,3))
return base_model
def train_model(model,dataset,image_size,batch_size,save_name,lr1,lr2,Epochs1,Epochs2):
dataParam={'messidor': [957,243,2,'./data/messidor/train','./data/messidor/test'],
'kaggle': [30000,5126,5,'./data/kaggle/train','./data/kaggle/valid'],
'DDR': [9851,2503,5,'./data/DDR/train','./data/DDR/valid']} #6119
train_num,valid_num,classes,train_dir,test_dir = dataParam[dataset]
train=ImageDataGenerator(horizontal_flip=True,vertical_flip=True,rotation_range=90)
valid = ImageDataGenerator()
train_data=train.flow_from_directory(train_dir,
target_size=(image_size,image_size),
shuffle = True,
batch_size=batch_size)
valid_data=valid.flow_from_directory(test_dir,
target_size=(image_size,image_size),
shuffle = False,
batch_size=batch_size)
lr_decay=ReduceLROnPlateau(monitor='val_loss', factor=0.8, patience=3, verbose=1)
save_model=ModelCheckpoint('new/'+save_name+'{epoch:02d}.h5', monitor='val_loss',period=10)
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer=Adam(lr=lr1,decay=0.00001),loss=loss_fun,metrics=['acc'])
model.fit_generator(train_data,
steps_per_epoch=train_num/batch_size,
validation_data=valid_data,
validation_steps=valid_num/batch_size,
epochs=Epochs1,
workers=2,
callbacks=[lr_decay,save_model])
for layer in base_model.layers:
layer.trainable = True
model.compile(optimizer=Adam(lr=lr2,decay=0.00001),loss=loss_fun,metrics=['acc'])
history=model.fit_generator(train_data,
steps_per_epoch=train_num/batch_size,
validation_data=valid_data,
validation_steps=valid_num/batch_size,
epochs=Epochs2,
workers=2,
callbacks=[lr_decay,save_model])
return history
os.environ["CUDA_VISIBLE_DEVICES"] = "4"
loss_fun= 'categorical_crossentropy'
gpu_num=1
k=5
lr1=0.005
lr2=0.0001
batch_size= 16
image_size=512
classes=5
base_model=get_base_model('mobilenet1.0',image_size)
base_in=base_model.input
base_out=base_model.output
x=Global_attention_block(base_out)
base_out=Category_attention_block(x,classes,k)
x=GlobalAveragePooling2D()(base_out)
out=Dense(classes,activation='softmax')(x)
if gpu_num>1:
model=Model(base_model.input,out)
#model.summary()
parallel_model = multi_gpu_model(model, gpus=gpu_num)
parallel_model.summary()
else:
parallel_model=Model(base_model.input,out)
parallel_model.summary()
history=train_model(parallel_model,'bird',image_size,batch_size,'densenet121',lr1,lr2,1,70)
plotmodel(history,'densenet121')