-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain_model_old.py
53 lines (40 loc) · 1.94 KB
/
train_model_old.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
from tensorflow import keras
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D, Dropout, SpatialDropout2D, AveragePooling2D
import numpy as np
import pickle
#import os
#os.environ["CUDA_VISIBLE_DEVICES"]="-1"
x_train=pickle.load(open('x_train.bin','rb'))
y_train=pickle.load(open('y_train.bin','rb'))
x_val=pickle.load(open('x_val.bin','rb'))
y_val=pickle.load(open('y_val.bin','rb'))
print('\n\nLoaded Prepared Data Successfully')
layerz = [Conv2D(18, kernel_size=(5,5), input_shape=(160,160,3),activation='relu'),
MaxPooling2D(pool_size=(2,2)),
Conv2D(36, kernel_size=(3,3),activation='relu'),
Conv2D(50, kernel_size=(3,3),activation='relu'),
MaxPooling2D(pool_size=(2,2)),
Conv2D(50, kernel_size=(3,3),activation='relu'),
Conv2D(50, kernel_size=(3,3),activation='relu'),
MaxPooling2D(pool_size=(2,2)),
Conv2D(50, kernel_size=(3,3),activation='relu'),
Conv2D(50, kernel_size=(3,3),activation='relu'),
Conv2D(50, kernel_size=(3,3),activation='relu'),
Conv2D(50, kernel_size=(3,3),activation='relu'),
Conv2D(36, kernel_size=(3,3),activation='relu'),
Conv2D(36, kernel_size=(3,3),activation='relu'),
MaxPooling2D(pool_size=(2,2)),
Flatten(),
Dense(72, activation='relu'),
Dense(6,activation='sigmoid')]
model=keras.Sequential(layerz)
model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(learning_rate=0.000005), metrics=['accuracy'])
print('\n \nTraining: \n \n')
checkpoint = keras.callbacks.ModelCheckpoint('ckpt.hdf5', monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=15)
# model.load_weights('ckpt.hdf5')
# USE ONLY IF CHECKPOINT EXISTS AND IS FROM THE SAME MODEL
model.fit(x_train, y_train, epochs=50, batch_size = 18, shuffle=True, callbacks=[checkpoint, stop], validation_data=(x_val, y_val))
model.load_weights('ckpt.hdf5')
model.save("AWS")
print('\n\nSaving Model Successful')