-
Notifications
You must be signed in to change notification settings - Fork 1
/
training.py
56 lines (41 loc) · 2 KB
/
training.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
from keras.preprocessing.image import ImageDataGenerator
from model import get_model, get_model_mobilenet
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
import cv2
import numpy as np
batch_size = 32
train_datagen = ImageDataGenerator(rescale=1. / 255)
training_set = train_datagen.flow_from_directory('./Dataset/images/train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
test_datagen = ImageDataGenerator(rescale=1. / 255)
test_set = test_datagen.flow_from_directory('./Dataset/images/val',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
model = get_model(channels=3)
opt = Adam(0.001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
filepath = "./Models/saved-model-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
save_best_only=False)
print(model.summary())
# json_model = model.to_json()
# with open("model.json", "w") as outfile:
# outfile.write(json_model)
# history = model.fit_generator(training_set,
# steps_per_epoch=666 // batch_size,
# epochs=100,
# validation_data=test_set,
# validation_steps=81 // batch_size,
# callbacks=[checkpoint])
model.load_weights('./Models/saved-model-11-1.00.hdf5')
img = cv2.imread('./Dataset/images/test/left008788.png', cv2.IMREAD_UNCHANGED)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
resized_img = cv2.resize(img, (224, 224))
test_image = np.expand_dims(resized_img, axis=0)/255.
result = model.predict(test_image)
print(training_set.class_indices)
print(np.argmax(result))