Skip to content

Commit b1560bc

Browse files
committed
major bug fixes
1 parent dad0337 commit b1560bc

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

model.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@
5151
BS = 600 # batch size
5252
validation_split = 0.01
5353
batch_per_epoch = np.ceil(len(df['image']) / BS).astype(int)
54-
epochs = 100
54+
epochs = 40
5555
alpha = 0.01
56-
lr = 0.000001
56+
lr = 0.00001
5757
mom = 0.000002
58+
optimizer = 'SGD'
5859

5960
# ########################################################
6061
# Create training data labels and target
@@ -67,7 +68,7 @@ def training_gen():
6768
try:
6869
if len(training_data) != BS:
6970

70-
image = cv2.imread(f"small_training_images/{img}.png", 1)
71+
image = cv2.imread(f"smaller_training_images/{img}.png", 1)
7172
#image = cv2.resize(image, (400,400))
7273
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
7374

@@ -77,12 +78,12 @@ def training_gen():
7778

7879
else:
7980
X = [image for image, angle, normal_v in training_data]
80-
other_inp = [image for image, angle, normal_v in training_data]
81-
Y = [image for image, angle, normal_v in training_data]
81+
other_inp = [normal_v for image, angle, normal_v in training_data]
82+
Y = [angle for image, angle, normal_v in training_data]
8283

8384
training_data = []
8485

85-
X = np.array(image).reshape(-1, 400,400,3)
86+
X = (np.array(image)/255.).reshape(-1, 300,250,3)
8687
Y = np.array([angle])
8788
other_inp = np.array([velocity])
8889

@@ -95,7 +96,7 @@ def training_gen():
9596
# ########################################################
9697
# Create test data labels and target
9798
test_data = []
98-
_iterator_obj = list(zip(df['image'], df['steering_angle'], df['outcome'], test_df['normal_velocity']))
99+
_iterator_obj = list(zip(df['image'], df['steering_angle'], df['outcome'], df['normal_velocity']))
99100
random.shuffle(_iterator_obj)
100101

101102
for img, angle, outcome, velocity in _iterator_obj:
@@ -104,7 +105,7 @@ def training_gen():
104105
if len(test_data) == int(len(df['image']) * validation_split):
105106
break
106107

107-
test_image = cv2.imread(f"small_training_images/{img}.png", 1)
108+
test_image = cv2.imread(f"smaller_training_images/{img}.png", 1)
108109
#test_image = cv2.resize(test_image, (400,400))
109110
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
110111

@@ -120,7 +121,7 @@ def training_gen():
120121
test_Y = [label for img, label, velocity in test_data]
121122
test_other_inp = [velocity for img, label, velocity in test_data]
122123

123-
test_X = np.array(test_X).reshape(-1, 400,400,3)
124+
test_X = np.array(test_X).reshape(-1, 300,250,3)
124125
test_Y = np.array(test_Y)
125126
test_other_inp = np.array(test_other_inp)
126127
print(test_other_inp.shape)
@@ -129,31 +130,30 @@ def training_gen():
129130
# Create Model
130131
def create_model():
131132
# model = Sequential()
132-
input1 = Input(shape=(400,400,3))
133+
input1 = Input(shape=(300,250,3))
133134

134135
# model.add(Flatten(input_shape = (940,940,3)))
135-
conv1 = Conv2D(36, (3, 3), strides=(2, 2))(input1)
136-
#conv1 = model.add(Conv2D(20, (5, 5), strides=(2, 2), input_shape = (940,940,3)))
136+
conv1 = Conv2D(36, (3, 3), strides=(2, 2), padding='same')(input1)
137137
activ1 =LeakyReLU(alpha=alpha)(conv1)
138-
pool1 =MaxPooling2D(pool_size=(2, 2))(activ1)
138+
pool1 =MaxPooling2D(pool_size=(2, 2), padding='valid')(activ1)
139139

140-
conv2 = Conv2D(25, (3, 3), strides=(2, 2))(pool1)
140+
conv2 = Conv2D(25, (3, 3), strides=(2, 2), padding='same')(pool1)
141141
activ2 =LeakyReLU(alpha=alpha)(conv2)
142-
pool2 = MaxPooling2D(pool_size=(2, 2))(activ2)
142+
pool2 = MaxPooling2D(pool_size=(2, 2), padding='valid')(activ2)
143143

144-
conv3 = Conv2D(16, (3, 3), strides=(2, 2))(pool2)
144+
conv3 = Conv2D(16, (3, 3), strides=(2, 2), padding='same')(pool2)
145145
activ3 = LeakyReLU(alpha=alpha)(conv3)
146-
pool3 = MaxPooling2D(pool_size=(2, 2))(activ3)
146+
pool3 = MaxPooling2D(pool_size=(2, 2), padding='valid')(activ3)
147147

148-
conv4 = Conv2D(9, (3, 3), strides=1)(pool3)
149-
activ4 = LeakyReLU(alpha=alpha)(conv4)
150-
pool4 = MaxPooling2D(pool_size=(1, 1))(activ4)
148+
# conv4 = Conv2D(9, (3, 3), strides=(1, 1), padding='valid')(pool3)
149+
# activ4 = LeakyReLU(alpha=alpha)(conv4)
150+
#pool4 = MaxPooling2D(pool_size=(1, 1), padding='valid')(activ4)
151151

152-
conv5 = Conv2D(4, (3, 3), strides=1)(pool4)
153-
activ5 = LeakyReLU(alpha=alpha)(conv5)
154-
pool5 = MaxPooling2D(pool_size=(1, 1))(activ5)
152+
# conv5 = Conv2D(4, (3, 3), strides=(1, 1), padding='valid')(activ4)
153+
# activ5 = LeakyReLU(alpha=alpha)(conv5)
154+
#pool5 = MaxPooling2D(pool_size=(1, 1))(activ5)
155155

156-
flat1 = Flatten()(pool5)
156+
flat1 = Flatten()(pool3)
157157

158158
input2 = Input(shape = (1, ))
159159
concat1 = Concatenate(axis=1)([flat1, input2])
@@ -167,7 +167,7 @@ def create_model():
167167
model = Model(inputs = [input1, input2], outputs = dense4)
168168

169169
model.compile(loss='mean_squared_error',
170-
metrics=['accuracy', 'mae', 'mse'], optimizer='sgd')
170+
metrics=['accuracy', 'mae', 'mse'], optimizer=optimizer)
171171

172172
optimizers.SGD(lr=lr, momentum=mom)
173173
#optimizers.RMSprop(lr=lr)
@@ -205,4 +205,8 @@ def create_model():
205205

206206

207207
loss_graph = plt.plot(loss)
208+
plt.title(f'MSE of {round(float(loss[-1]), 6)} over {epochs} epochs with {optimizer} optimizer \nand 3 hidden conv layers')
208209
plt.show()
210+
211+
with open(f'results/{optimizer}_3.txt', 'a') as f:
212+
print(loss, file = f)

0 commit comments

Comments
 (0)