-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_model.py
82 lines (70 loc) · 2.57 KB
/
make_model.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
import cv2, numpy as np, os
#parameters
working_dir = '/home/stephen/Desktop/shapes/data/'
os.chdir(working_dir)
img_size = 60 #size of image fed into model
def flatten(dimData, images):
images = np.array(images)
images = images.reshape(len(images), dimData)
images = images.astype('float32')
images /=255
return images
#-------------get train/test data-----------------
#get data
folders, labels, images = ['triangle', 'star', 'square', 'circle'], [], []
for folder in folders:
print folder
for path in os.listdir(os.getcwd()+'/'+folder):
img = cv2.imread(folder+'/'+path,0)
#cv2.imshow('img', img)
#cv2.waitKey(1)
images.append(cv2.resize(img, (img_size, img_size)))
labels.append(folders.index(folder))
#break data into training and test sets
to_train= 0
train_images, test_images, train_labels, test_labels = [],[],[],[]
for image, label in zip(images, labels):
if to_train<5:
train_images.append(image)
train_labels.append(label)
to_train+=1
else:
test_images.append(image)
test_labels.append(label)
to_train = 0
#-----------------keras time --> make the model
from keras.utils import to_categorical
#flatten data
dataDim = np.prod(images[0].shape)
train_data = flatten(dataDim, train_images)
test_data = flatten(dataDim, test_images)
#change labels to categorical
train_labels = np.array(train_labels)
test_labels = np.array(test_labels)
train_labels_one_hot = to_categorical(train_labels)
test_labels_one_hot = to_categorical(test_labels)
#determine the number of classes
classes = np.unique(train_labels)
nClasses = len(classes)
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
#three layers
#activation function: both
#neurons: 256
model = Sequential()
model.add(Dense(256, activation = 'tanh', input_shape = (dataDim,)))
model.add(Dropout(0.2))
model.add(Dense(256, activation='tanh'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(nClasses, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(train_data, train_labels_one_hot, batch_size = 256, epochs=50, verbose=1,
validation_data=(test_data, test_labels_one_hot))
#test model
[test_loss, test_acc] = model.evaluate(test_data, test_labels_one_hot)
print("Evaluation result on Test Data : Loss = {}, accuracy = {}".format(test_loss, test_acc))
#save model
model.save('/home/stephen/Desktop/shapes/shapesmodel.h5')