-
Notifications
You must be signed in to change notification settings - Fork 6
/
CNN3_1080.py
133 lines (113 loc) · 4.62 KB
/
CNN3_1080.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
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, BatchNormalization
from keras.callbacks import TensorBoard, ModelCheckpoint
from keras.layers import LeakyReLU
import keras
import os
from PythonUtils.file import unique_name, filelist_delete, recursive_list
from generator.PoseDataSequence import DataSequence
from generator.csvgen import generate_csv
from dotenv import load_dotenv
def cleanLog(input_path):
if input_path is None:
# Dynamicly generate model input_path.
project_root = os.path.realpath(__file__)
log_path = os.path.join(os.path.dirname(project_root), "logs")
else:
log_path = input_path
if os.path.exists(log_path):
files = recursive_list(log_path)
if len(files) > 0:
filelist_delete(files)
def load_data_and_run(model,input_shape, TBCallBack):
load_dotenv()
train_path = os.getenv("train_path")
train_csv_path = os.getenv("train_csv_path")
validate_path = os.getenv("validate_path")
validate_csv_path = os.getenv("validate_csv_path")
# Dynamicly generate model path.
project_root = os.path.realpath(__file__)
model_path = os.path.join(os.path.dirname(project_root), "models")
generate_csv(train_path, train_csv_path )
train_data = DataSequence(train_csv_path, 128, mode="Train")
generate_csv(validate_path,validate_csv_path)
validation_data = DataSequence(validate_csv_path, 128)
model_name = os.path.join(model_path, unique_name())
checkpoint = ModelCheckpoint(model_name, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [TBCallBack, checkpoint]
model.fit_generator(
train_data,
steps_per_epoch=256,
epochs=500,
validation_data=validation_data,
validation_steps=256,
callbacks=callbacks_list
)
model.save(os.path.join(model_path, unique_name()))
def createModel(input_shape, output_classes):
model = Sequential()
model.add(Conv2D(16, (5, 5), padding='same', strides=(2,2), input_shape=(input_shape, input_shape, 3)))
model.add(BatchNormalization(axis=-1))
model.add(LeakyReLU(alpha=0.2))
#model.add(Conv2D(16, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(16, (3, 3), padding='same', strides=(1,1)))
model.add(BatchNormalization(axis=-1))
model.add(LeakyReLU(alpha=0.2))
#model.add(Conv2D(32, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), padding='same', strides=(1,1)))
model.add(BatchNormalization(axis=-1))
model.add(LeakyReLU(alpha=0.2))
# #model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
#
model.add(Conv2D(32, (3, 3), padding='same', strides=(1,1)))
model.add(BatchNormalization(axis=-1))
model.add(LeakyReLU(alpha=0.2))
# #model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
#
model.add(Conv2D(64, (3, 3), padding='same', strides=(1,1)))
model.add(BatchNormalization(axis=-1))
model.add(LeakyReLU(alpha=0.2))
# #model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
#
model.add(Conv2D(64, (3, 3), padding='same', strides=(1, 1)))
model.add(BatchNormalization(axis=-1))
model.add(LeakyReLU(alpha=0.2))
# model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
#model.add(Dense(2048))
#model.add(LeakyReLU(alpha=0.1))
model.add(Dense(1024))
model.add(BatchNormalization())
model.add(LeakyReLU(alpha=0.1))
model.add(Dense(512))
model.add(BatchNormalization())
model.add(LeakyReLU(alpha=0.1))
model.add(Dropout(0.5))
model.add(Dense(output_classes))
return model
if __name__ =="__main__":
from time import time
cleanLog(None)
image_size = 500
model1 = createModel(image_size, 3) # downsize to 128
model1.compile(loss="mean_squared_error", optimizer="adadelta", metrics=["acc", "mae"])
# Dynamicly generate model input_path.
project_root = os.path.realpath(__file__)
log_path = os.path.join(os.path.dirname(project_root), "logs")
tensorboard = keras.callbacks.TensorBoard(
log_dir=log_path,
histogram_freq=0,
write_images=True)
load_data_and_run(model1, image_size, tensorboard)