-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
103 lines (79 loc) · 2.74 KB
/
train.py
File metadata and controls
103 lines (79 loc) · 2.74 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
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense, Dropout
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
import h5py
import matplotlib.pyplot as plt
# Sequential layer
classifier = Sequential()
# First convolution layer followed by MaxPooling
classifier.add(Conv2D(32, (3, 3), input_shape=(3, 64, 64), activation="relu", data_format='channels_first'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
# Second convolution layer followed by MaxPooling
classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
# Third convolution layer followed by MaxPooling
classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
# Converting output of third layer to vector
classifier.add(Flatten())
classifier.add(Dense(256, activation='relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(26, activation='softmax'))
# Compiling CNN
classifier.compile(
optimizer=optimizers.SGD(lr=0.01),
loss='categorical_crossentropy',
metrics=['accuracy'])
# Generating training data
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
directory=r"./data/training_set",
target_size=(64, 64),
color_mode="grayscale",
batch_size=32,
class_mode="categorical")
# Generating validation data
valid_datagen = ImageDataGenerator(rescale=1./255)
valid_generator = valid_datagen.flow_from_directory(
directory=r"./data/test_set",
target_size=(64, 64),
color_mode="grayscale",
batch_size=32,
class_mode="categorical",
shuffle=True,
seed=42
)
STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size
STEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size
# Training the model
model = classifier.fit(train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=25)
# Saving the model
classifier.save("Trained_model.h5")
# Visualize model history
plt.plot(model.history['acc'])
plt.plot(model.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(model.history['loss'])
plt.plot(model.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()