-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras_classifier.py
125 lines (103 loc) · 3.65 KB
/
keras_classifier.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
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
'''Train a simple deep CNN on the CIFAR10 small images dataset.
It gets to 75% validation accuracy in 25 epochs, and 79% after 50 epochs.
(it's still underfitting at that point, though).
https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py
'''
from __future__ import print_function
import keras
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import History
from keras import backend as K
import os
import numpy as np
import pandas as pd
from scraper.met_scraper import get_starting_id
import utils
img_width, img_height = 150, 150
batch_size = 32 # or 16?
num_classes = 10
epochs = 3 # ???
nb_train_samples = 9000
nb_validation_samples = 1000
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_artwork_classifier_trained_model.h5'
train_data_dir = "data/training"
test_data_dir = "data/testing"
LAST_SCRAPED_ID = 100888
# Resource: https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
# Model taken from example image classification code
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# initiate RMSprop optimizer
# opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# training data augmentation
train_datagen = ImageDataGenerator(
#rotation_range=40,
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True,
fill_mode='nearest')
# testing data augmnetation
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
testing_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
history = History()
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=testing_generator,
validation_steps=nb_validation_samples // batch_size,
verbose=2,
callbacks=[history])
# Save model and weights
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)
print(history.epoch)
print(history.history)
# Score trained model.
# scores = model.evaluate(testing_data, testing_labels, verbose=1)
# print('Test loss:', scores[0])
# print('Test accuracy:', scores[1])