-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
70 lines (58 loc) · 2.73 KB
/
training.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
from tensorflow import keras
import tensorflow as tf
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
from utils import get_cifar10,get_mnist
from model import get_SimpleNet
from evaluation import evaluate_model
import json
import argparse
from sklearn.model_selection import train_test_split
parser = argparse.ArgumentParser()
parser.add_argument('-a', "--data_aug", help="initialize data augmentation", action='store_true')
parser.add_argument('-s', "--dataset", help="choose 'mnist' or 'cifar10' ")
args = parser.parse_args()
with open('config/model.json') as f:
model_config = json.load(f)
with open('config/training.json') as f:
train_config = json.load(f)
if __name__ == "__main__":
model = get_SimpleNet(model_config['0'])
if args.dataset == 'mnist':
X_train,Y_train,X_test,Y_test = get_mnist(model_config['0'])
else:
X_train,Y_train,X_test,Y_test = get_cifar10(model_config['0'])
train_batch_size = train_config['train_batch_size']
train_validation_split = train_config['train_validation_split']
epochs = train_config['epochs']
if args.data_aug:
#Image Data Generator
datagen = ImageDataGenerator(width_shift_range=0.2,
horizontal_flip=True,
height_shift_range=0.2,
rotation_range=20)
datagen.fit(X_train)
checkpointer = ModelCheckpoint(filepath='saved_models/' + model.name + '-{epoch:02d}-{val_loss:.2f}.hdf5',
monitor='val_loss',
verbose=1,
save_best_only=False,
save_weights_only=False,
mode='auto',
period=1)
model.fit_generator(datagen.flow(X_train,Y_train,batch_size=train_batch_size),
steps_per_epoch=None,
epochs=epochs,
validation_data=(X_test,Y_test),
callbacks=[checkpointer]
)
evaluate_model(X_test,Y_test,model,train_config)
else:
checkpointer = ModelCheckpoint(filepath='saved_models/' + model.name + '-{epoch:02d}-{val_loss:.2f}.hdf5',
monitor='val_loss',
verbose=1,
save_best_only=False,
save_weights_only=False,
mode='auto',
period=1)
model.fit(X_train, Y_train, batch_size=train_batch_size,epochs=epochs,validation_split=train_validation_split,callbacks=[checkpointer])
evaluate_model(X_test,Y_test,model,train_config)