-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
186 lines (164 loc) · 7.89 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
import os
import argparse
import numpy as np
import tensorflow as tf
# standard resnet block
def residual_block(x, filters, projection):
x_skip = x
# layer 1
if projection:
x = tf.keras.layers.Conv2D(filters, (3, 3), (2,2), padding='same', kernel_initializer='he_normal')(x)
else:
x = tf.keras.layers.Conv2D(filters, (3, 3), (1,1), padding='same', kernel_initializer='he_normal')(x)
x = tf.keras.layers.BatchNormalization(axis=3)(x)
x = tf.keras.layers.Activation('relu')(x)
# layer 2
x = tf.keras.layers.Conv2D(filters, (3, 3), (1,1), padding='same', kernel_initializer='he_normal')(x)
x = tf.keras.layers.BatchNormalization(axis=3)(x)
# addition
if projection:
x_skip = tf.keras.layers.Conv2D(filters, (1, 1), (2,2), padding='valid', kernel_initializer='he_normal')(x_skip)
x = tf.keras.layers.Add()([x, x_skip])
x = tf.keras.layers.Activation('relu')(x)
return x
# configured for resnet-20
def resnet20(shape_in, classes):
# input and initial convolution
x_in = tf.keras.layers.Input(shape_in)
x = tf.keras.layers.Conv2D(16, (3, 3), (1,1), padding='same', kernel_initializer='he_normal')(x_in)
x = tf.keras.layers.BatchNormalization(axis=3)(x)
x = tf.keras.layers.Activation('relu')(x)
# residual blocks
filters = [16, 32, 64]
for i in range(3):
for j in range(3):
if i > 0 and j == 0:
x = residual_block(x, filters[i], projection=True)
else:
x = residual_block(x, filters[i], projection=False)
# final dense layer and model
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x_out = tf.keras.layers.Dense(classes, kernel_initializer='he_normal')(x)
model = tf.keras.models.Model(x_in, x_out, name='ResNet20')
return model
if __name__ == "__main__":
# parse args
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, help='Specify model to use', choices=['vgg16', 'resnet20', 'convnet'], required=True)
parser.add_argument('--dataset', type=str, help='Specify medmnist dataset to use', choices=['pathmnist', 'octmnist', 'tissuemnist'], required=True)
parser.add_argument('--gpu', type=int, help='Specify gpu index to use', required=False)
args = parser.parse_args()
# set gpu index if specified
if args.gpu is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = f'{args.gpu}'
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
# create model dir if not yet created
if not os.path.exists(os.path.join(os.getcwd(), 'models')):
os.makedirs(os.path.join(os.getcwd(), 'models'))
# load dataset
dataset = np.load(f'{args.dataset}.npz')
dataset = dict(dataset)
# add axis if greyscale
# define input_shape
if len(dataset['train_images'].shape) == 3:
input_shape = (28, 28, 1)
dataset['train_images'] = dataset['train_images'][..., np.newaxis]
dataset['val_images'] = dataset['val_images'][..., np.newaxis]
dataset['test_images'] = dataset['test_images'][..., np.newaxis]
else:
input_shape = (28, 28, 3)
# define classes
if args.dataset == 'pathmnist':
classes = 9
elif args.dataset == 'octmnist':
classes = 4
elif args.dataset == 'tissuemnist':
classes = 8
# compile model
if args.model == 'resnet20':
model = resnet20(input_shape, classes)
elif args.model == 'vgg16':
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(input_shape))
for idx, filter in enumerate([64, 128, 256, 512, 512]):
model.add(tf.keras.layers.Conv2D(filter, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Dropout(0.25))
if idx > 1:
model.add(tf.keras.layers.Conv2D(filter, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Conv2D(filter, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
if idx < 4:
model.add(tf.keras.layers.MaxPool2D((2,2),(2,2)))
else:
model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512, activation='relu', kernel_initializer='he_normal'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Dense(classes, activation=None, kernel_initializer='he_normal'))
elif args.model == 'convnet':
model = tf.keras.Sequential([
tf.keras.layers.Input(input_shape),
tf.keras.layers.Conv2D(32, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.Conv2D(32, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=(2,2)),
tf.keras.layers.Conv2D(64, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.Conv2D(64, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=(2,2)),
tf.keras.layers.Conv2D(128, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.Conv2D(128, (3,3), (1,1), padding='same', kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=(2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(1024, kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(512, kernel_initializer='he_normal', activation='relu'),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(classes, kernel_initializer='he_normal', activation=None),
])
optim = tf.keras.optimizers.Adam(lr=0.001)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer=optim, loss=loss, metrics=['accuracy'])
model.summary()
# train model
lr_scheduler = tf.keras.callbacks.LearningRateScheduler(
lambda epoch, lr: lr*0.1 if epoch == 50 or epoch == 75 else lr
)
tensorboard = tf.keras.callbacks.TensorBoard(
log_dir=os.path.join(os.getcwd(), 'logs', f'{args.dataset}_{args.model}'),
)
# checkpoint = tf.keras.callbacks.ModelCheckpoint(
# os.path.join(os.getcwd(), 'models', f'model_{args.dataset}_{args.model}.h5'),
# monitor='val_accuracy',
# verbose=1,
# save_best_only=True,
# save_weights_only=False,
# mode='max',
# )
model.fit(
dataset['train_images']/255.0,
dataset['train_labels'],
batch_size=128,
epochs=100,
verbose=1,
callbacks=[lr_scheduler, tensorboard],
validation_data=(dataset['val_images']/255.0, dataset['val_labels']),
shuffle=True,
)
model.save(os.path.join(os.getcwd(), 'models', f'model_{args.dataset}_{args.model}.h5'))
# load and eval best model
model = tf.keras.models.load_model(
os.path.join(os.getcwd(), 'models', f'model_{args.dataset}_{args.model}.h5')
)
score = model.evaluate(
dataset['test_images']/255.0,
dataset['test_labels'],
batch_size=128,
verbose=1,
)
print(f"Test loss: {score[0]} - Test acc: {score[1]}")
np.save(os.path.join(os.getcwd(), 'logs', f'{args.dataset}_{args.model}', 'score.npy'), score)