-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_CNN.py
executable file
·55 lines (39 loc) · 1.79 KB
/
train_CNN.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
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
import matplotlib.pyplot as plt
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
train_dir = 'GTSRB/train_images'
img_width, img_height = 32, 32
input_shape = (img_width, img_height, 3)
epochs = 22
batch_size = 16
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(3))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = datagen.flow_from_directory(train_dir, target_size=(img_width, img_height),
batch_size=batch_size, class_mode='categorical')
validation_generator = datagen.flow_from_directory(train_dir, target_size=(img_height, img_width),
batch_size=batch_size, class_mode='categorical') # set as validation data
history = model.fit(train_generator, epochs=epochs, batch_size=batch_size, validation_data=validation_generator, validation_steps=epochs)
model.save("GTSRB_CNN.h5")
plt.plot(history.history['loss'], label='Ошибка на обучающем наборе')
plt.plot(history.history['val_loss'], label='Ошибка на проверочном наборе')
plt.xlabel('Эпоха обучения')
plt.ylabel('Ошибка')
plt.legend()
plt.show()