forked from holger-jodel/sc2_predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn-regression.py
278 lines (215 loc) · 9.83 KB
/
learn-regression.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import sys
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu0,floatX=float32"
# os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=cpu,floatX=float32"
import numpy as np
import pandas as pd
from time import time
import warnings
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import h5py
from keras.models import Sequential, Model
from keras.layers.core import Dense, Dropout, Activation, SpatialDropout2D
from keras.layers import Input, Activation, Dropout, Flatten, Dense, Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
from keras.callbacks import CSVLogger, ModelCheckpoint, ReduceLROnPlateau
from keras.models import load_model
from keras.utils.data_utils import get_file
from keras.utils.layer_utils import convert_all_kernels_in_model
import keras.backend as K
from Helper.HelperFunctions import path_is_file, get_files_in_dir, path_is_dir, files_to_matrix, _obtain_input_shape, csv_to_data
def get_filename():
filename = 'SC2_regr_' + str(int(time()))
return filename
def get_old_config(path, model_path):
old_config = path + model_path.split('/')[-1].split('.h5')[0] + ''
return old_config
def copy_config(path, model_path, filename):
old_config = get_old_config(path, model_path)
if path_is_file(old_config):
cmd = 'cp {old} {new}_config.txt'.format(
old=old_config, new=path + filename)
print('copying old config with cmd:\n{}'.format(cmd))
os.system(cmd)
else:
cmd = "echo 'Could not find old config for this model' >> {new}_config.txt".format(
new=path + filename)
print('could not find old config for this model')
os.system(cmd)
def save_config(path, filename):
cmd = "cp learn-regression.py {}_config.txt".format(path + filename)
os.system(cmd)
def save_model(model, path, filename):
model.save(path + 'models/interestingness/' + filename + '.h5')
# from keras.utils.visualize_util import plot
# plot(model, to_file=path + 'models/interestingness/' + filename + '.png')
def VGG16(input_shape=None, model_path='models'):
"""adapted from keras repo at
https://github.com/fchollet/keras/blob/master/keras/applications/vgg16.py
"""
# Determine proper input shape
TH_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels_notop.h5'
TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
model = Sequential()
model.add(Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1', input_shape=input_shape))
model.add(Convolution2D(64, 3, 3, activation='relu',
border_mode='same', name='block1_conv2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool'))
model.add(Convolution2D(128, 3, 3, activation='relu',
border_mode='same', name='block2_conv1'))
model.add(Convolution2D(128, 3, 3, activation='relu',
border_mode='same', name='block2_conv2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool'))
model.add(Convolution2D(256, 3, 3, activation='relu',
border_mode='same', name='block3_conv1'))
model.add(Convolution2D(256, 3, 3, activation='relu',
border_mode='same', name='block3_conv2'))
model.add(Convolution2D(256, 3, 3, activation='relu',
border_mode='same', name='block3_conv3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool'))
model.add(Convolution2D(512, 3, 3, activation='relu',
border_mode='same', name='block4_conv1'))
model.add(Convolution2D(512, 3, 3, activation='relu',
border_mode='same', name='block4_conv2'))
model.add(Convolution2D(512, 3, 3, activation='relu',
border_mode='same', name='block4_conv3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool'))
model.add(Convolution2D(512, 3, 3, activation='relu',
border_mode='same', name='block5_conv1'))
model.add(Convolution2D(512, 3, 3, activation='relu',
border_mode='same', name='block5_conv2'))
model.add(Convolution2D(512, 3, 3, activation='relu',
border_mode='same', name='block5_conv3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool'))
if K.image_dim_ordering() == 'th':
weights_path=get_file('vgg16_weights_th_dim_ordering_th_kernels_notop.h5',
TH_WEIGHTS_PATH_NO_TOP,
cache_subdir=model_path)
model.load_weights(weights_path)
else:
weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
TF_WEIGHTS_PATH_NO_TOP,
cache_subdir=model_path)
model.load_weights(weights_path)
if K.backend() == 'theano':
convert_all_kernels_in_model(model)
print('model loaded')
if K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image dimension ordering convention '
'(`image_dim_ordering="th"`). '
'For best performance, set '
'`image_dim_ordering="tf"` in '
'your Keras config '
'at ~/.keras/keras.json.')
convert_all_kernels_in_model(model)
print('before pop {}'.format(len(model.layers)))
model.pop()
model.pop()
model.pop()
model.pop()
print('after pop {}'.format(len(model.layers)))
for layer in model.layers[:25]:
layer.trainable=False
return model
def get_model(shape, dropout=0.5, path=None):
print('building neural network')
model=Sequential()
model.add(Convolution2D(512, 3, 3, border_mode='same', input_shape=shape))
model.add(Activation('relu'))
model.add(Convolution2D(512, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(SpatialDropout2D(dropout))
model.add(Flatten())#input_shape=shape))
# model.add(Dense(4096))
# model.add(Activation('relu'))
# model.add(Dropout(0.5))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
#model.add(Activation('linear'))
return model
def main(args):
img_width=160
img_height=90
img_channels=3
img_shape=(img_channels, img_width, img_height)
regression_range=list(range(0, 5))
if(len(args) < 3):
print('give storage folder (containing "data/ingame/" and "models/" folders) and csv regression file pls')
return -1
base_path=args[1]
if(not path_is_dir(base_path)):
print('data path is wrooong')
return -1
img_path=base_path + 'data/ingame/'
if(not path_is_dir(img_path)):
print('img path is wrooong')
return -1
csv_path=args[2]
log_path=base_path + 'logs/'
model_path=''
if(len(args) > 3):
model_path=args[3]
print('loading data from csv')
X, y=csv_to_data(csv_path, img_path, 'interestingness', (img_width, img_height))
X_train, X_test, y_train, y_test=train_test_split(X, y, test_size=0.3)
nb_epoch=100
batch_size=512
nb_train_samples=X_train.shape[0]
nb_validation_samples=X_test.shape[0]
print('nb_train_samples {}'.format(nb_train_samples))
print('nb_validation_samples {}'.format(nb_validation_samples))
train_datagen=ImageDataGenerator(
#shear_range=0.2,
#zoom_range=0.2,
vertical_flip=True,
fill_mode='nearest',
#rotation_range=10.,
)
train_datagen.fit(X_train)
test_datagen=ImageDataGenerator()
test_datagen.fit(X_test)
filename=get_filename()
csv_logger=CSVLogger(log_path + filename + '.log')
model_logger = ModelCheckpoint(base_path + 'models/interestingness/' + filename + '.h5',
save_best_only=True, save_weights_only=False, verbose=True, mode='auto')
reduce_lr = ReduceLROnPlateau(factor=0.2, patience=4, min_lr=0.00001, verbose=True)
if model_path:
print('loading model from {}'.format(model_path))
#model=get_model(shape=(img_channels, img_width, img_height))
model=load_model(model_path)
copy_config(log_path, model_path, filename)
else:
model=VGG16(input_shape=img_shape, model_path=base_path + 'models/')
top_model=get_model(shape=model.output_shape[1:])
model.add(top_model)
save_config(log_path, filename)
model.compile(loss='mean_squared_error',
optimizer=SGD(lr=0.1, momentum=0.9)) #'rmsprop') #
print('model compiled')
# print('fitting model')
# model.fit(X_train, y_train, nb_epoch=nb_epoch, callbacks=[csv_logger, model_logger],
# batch_size=batch_size, validation_data=(X_test, y_test))
model.fit_generator(
train_datagen.flow(X_train, y_train, batch_size=batch_size),
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch,
validation_data=test_datagen.flow(X_test, y_test),
nb_val_samples=nb_validation_samples,
callbacks=[csv_logger, model_logger, reduce_lr])
# save_model(model, base_path, filename)
y_pred=model.predict(X_test)
mse=mean_squared_error(y_test, y_pred)
print('mean squared error {}'.format(mse))
from Helper.NotificationSender import NotificationSender
NotificationSender('RegressionLearner').notify(
'training NN is done 🚀😍\nmse: {}'.format(mse))
if __name__ == "__main__":
main(sys.argv)