-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
185 lines (151 loc) Β· 5.18 KB
/
Copy pathtrain.py
File metadata and controls
185 lines (151 loc) Β· 5.18 KB
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
import keras
from keras import backend as K
from keras.utils import to_categorical
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten, Add, Input, BatchNormalization, Layer
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
import h5py, sys, os
import cv2
from tqdm import tqdm
from glob import glob
from random import random
import polygon
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
class randomize(Layer):
def __init__(self, Brightrange, **kwargs):
# self.output_dim = self.
self.Brightrange = Brightrange
super(randomize, self).__init__(**kwargs)
def build(self, input_shape):
super(randomize, self).build(input_shape)
def call(self, x):
if K.learning_phase() == 1:
#return x
#return x*(random()*self.Brightrange[1]+self.Brightrange[0])
#return polygon.shadow(x,2,2)
return polygon.shadow(x*(random()*self.Brightrange[1]+self.Brightrange[0]), 4, 1, 0.95) # img ; random factor ; number of polygons ; alpha
else:
return x
def compute_output_shape(self, input_shape):
return input_shape
x_train = []
x_train1 = []
x_train2 = []
x_train3 = []
x_trainLast = []
y_train = []
y_trainLast = []
x_test = []
x_test1 = []
x_test2 = []
x_test3 = []
y_test = []
directInv = 0
offset = 15
data = sorted(glob("whitePics\\*.png"),key=os.path.getmtime)
for image in tqdm(data):
img = cv2.imread(image,cv2.IMREAD_COLOR)#, cv2.IMREAD_GRAYSCALE)
# Prepare Data
img = cv2.resize(img,(80,64))
img = img[offset:, :]
img = img.astype("float32")
img /= 255
img = img.reshape((64-offset,80 ,3))
a = image.split("_")[0].split("\\")[1]
direct = int(a)
x_train.append(img)
y_train.append(direct)
#x_train,y_train = shuffle(x_train,y_train)
x_train,x_test,y_train,y_test = train_test_split(x_train,y_train, test_size = 0.20)
#train
x_train1 = x_train.copy()
x_train2 = x_train.copy()
x_train3 = x_train.copy()
#test
x_test1 = x_test.copy()
x_test2 = x_test.copy()
x_test3 = x_test.copy()
#train
del x_train1[0], x_train1[0] #Image shift
del x_train2[0], x_train2[-1]
del x_train3[-1], x_train3[-1]
del y_train[-1], y_train[-1] #label shift
#test
del x_test1[0], x_test1[0] #Image shift
del x_test2[0], x_test2[-1]
del x_test3[-1], x_test3[-1]
del y_test[-1], y_test[-1] #label shift
#train
x_train1 = np.array(x_train1)
x_train2 = np.array(x_train2)
x_train3 = np.array(x_train3)
y_train = np.array(y_train)
#test
x_test1 = np.array(x_test1)
x_test2 = np.array(x_test2)
x_test3 = np.array(x_test3)
y_test = np.array(y_test)
# concatenate (merge the 3 one channel images into a 3 channel image)
x_train = np.concatenate((x_train1,x_train2,x_train3),3)
x_test = np.concatenate((x_test1,x_test2,x_test3),3)
# data augmentation
# Model
#conv = Dropout(0.2)(conv)
regularisationParam = 1e-9
inp = Input(shape=(64-offset,80 ,9,))
dataGen = randomize([0.8,1.2])(inp)
conv = Conv2D(32, (2,2), strides=2, activation="relu", padding="same", kernel_regularizer=keras.regularizers.l2(l=regularisationParam))(dataGen)
conv = BatchNormalization()(conv)
conv = MaxPooling2D(pool_size=(2,2), strides=2)(conv)
conv = Dropout(0.2)(conv)
conv = Conv2D(64, (2,2), strides=2, activation="relu", padding="same", kernel_regularizer=keras.regularizers.l2(l=regularisationParam))(conv)
conv = BatchNormalization()(conv)
conv = MaxPooling2D(pool_size=(2,2), strides=2)(conv)
conv = Dropout(0.2)(conv)
conv = Flatten()(conv)
dens = Dense(32, use_bias=False, activation="relu", kernel_regularizer=keras.regularizers.l2(l=regularisationParam))(conv)
dens = BatchNormalization()(dens)
dens = Dense(16 , use_bias=False, activation="relu", kernel_regularizer=keras.regularizers.l2(l=regularisationParam))(dens)
dens = BatchNormalization()(dens)
output = Dense(1, use_bias=False, activation="tanh")(dens)
model = Model(inputs=inp, outputs=output)
# Entrainer Model
model.summary()
model.compile(loss="mean_squared_error",
optimizer=Adam(lr=0.0001),
metrics=["accuracy"])
history = model.fit(x_train,y_train,
batch_size = 32,
epochs = 80,
validation_data = (x_test,y_test))
def graphValues(history,Acc):
plt.figure()
if Acc == 1:
plt.plot(history.history['accuracy'],color="green")
plt.plot(history.history['val_accuracy'],color="blue")
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.ylim([0,1])
plt.legend(['train', 'test'], loc='upper left')
elif Acc == 0:
plt.plot(history.history['loss'],color="orange")
plt.plot(history.history['val_loss'],color="red")
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.ylim([0,1])
plt.legend(['train', 'test'], loc='upper left')
graphValues(history,0)
graphValues(history,1)
plt.show()
if input("wanna save (yes/no) ? ") == "yes":
model.save("IronCarHistory.h5")