-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom.py
211 lines (175 loc) · 6.16 KB
/
custom.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!python
import numpy as np
import os
from PIL import Image
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD, rmsprop
from keras.utils import np_utils
# Load training data
X_train, Y_train = [], []
for filename in os.listdir('Resized/train/idli'):
if filename.endswith(".jpg"):
im = Image.open('Resized/train/idli/'+filename)
X_train.append(np.asarray(im))
Y_train.append([1, 0, 0, 0, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/train/porotta'):
if filename.endswith(".jpg"):
im = Image.open('Resized/train/porotta/'+filename)
X_train.append(np.asarray(im))
Y_train.append([0, 1, 0, 0, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/train/sadya'):
if filename.endswith(".jpg"):
im = Image.open('Resized/train/sadya/'+filename)
X_train.append(np.asarray(im))
Y_train.append([0, 0, 1, 0, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/train/Vada'):
if filename.endswith(".jpeg"):
im = Image.open('Resized/train/Vada/'+filename)
X_train.append(np.asarray(im))
Y_train.append([0, 0, 0, 1, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/train/pizza'):
if filename.endswith(".jpg"):
im = Image.open('Resized/train/pizza/'+filename)
X_train.append(np.asarray(im))
Y_train.append([0, 0, 0, 0, 1, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/train/mussels'):
if filename.endswith(".jpg"):
im = Image.open('Resized/train/mussels/'+filename)
X_train.append(np.asarray(im))
Y_train.append([0, 0, 0, 0, 0, 1, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/train/hamburger'):
if filename.endswith(".jpg"):
im = Image.open('Resized/train/hamburger/'+filename)
X_train.append(np.asarray(im))
Y_train.append([0, 0, 0, 0, 0, 0, 1, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/train/omelette'):
if filename.endswith(".jpg"):
im = Image.open('Resized/train/omelette/'+filename)
X_train.append(np.asarray(im))
Y_train.append([0, 0, 0, 0, 0, 0, 0, 1])
im.close()
else:
continue
x_train = np.array(X_train)/255.0
y_train = np.array(Y_train)
# Load Testing data
X_test, Y_test = [], []
for filename in os.listdir('Resized/test/idli'):
if filename.endswith(".jpg"):
im = Image.open('Resized/test/idli/'+filename)
X_test.append(np.asarray(im))
Y_test.append([1, 0, 0, 0, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/test/porotta'):
if filename.endswith(".jpg"):
im = Image.open('Resized/test/porotta/'+filename)
X_test.append(np.asarray(im))
Y_test.append([0, 1, 0, 0, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/test/sadya'):
if filename.endswith(".jpg"):
im = Image.open('Resized/test/sadya/'+filename)
X_test.append(np.asarray(im))
Y_test.append([0, 0, 1, 0, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/test/Vada/'):
if filename.endswith(".jpeg"):
im = Image.open('Resized/test/Vada/'+filename)
X_test.append(np.asarray(im))
Y_test.append([0, 0, 0, 1, 0, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/test/pizza/'):
if filename.endswith(".jpg"):
im = Image.open('Resized/test/pizza/'+filename)
X_test.append(np.asarray(im))
Y_test.append([0, 0, 0, 0, 1, 0, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/test/mussels/'):
if filename.endswith(".jpg"):
im = Image.open('Resized/test/mussels/'+filename)
X_test.append(np.asarray(im))
Y_test.append([0, 0, 0, 0, 0, 1, 0, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/test/hamburger/'):
if filename.endswith(".jpg"):
im = Image.open('Resized/test/hamburger/'+filename)
X_test.append(np.asarray(im))
Y_test.append([0, 0, 0, 0, 0, 0, 1, 0])
im.close()
else:
continue
for filename in os.listdir('Resized/test/omelette/'):
if filename.endswith(".jpg"):
im = Image.open('Resized/test/omelette/'+filename)
X_test.append(np.asarray(im))
Y_test.append([0, 0, 0, 0, 0, 0, 0, 1])
im.close()
else:
continue
x_test = np.array(X_test)/255.0
y_test = np.array(Y_test)
# Define the model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(8, activation='softmax'))
# sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit(x_train, y_train, batch_size=32, epochs=30, verbose=1, validation_data=(x_test, y_test))
# Evaluate
y_class = model.predict_classes(x_test, verbose=1)
count = 0
for a,b in zip(y_test, y_class):
if (a.argmax() == b):
count += 1
print 'Test Accuracy = ' + str(count*1.0/len(y_test))
y_class = model.predict_classes(x_train, verbose=1)
count = 0
for a,b in zip(y_train, y_class):
if (a.argmax() == b):
count += 1
print 'Train Accuracy = ' + str(count*1.0/len(y_train))