質問編集履歴
1
事前学習のコードを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,8 +14,318 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
+
事前にmlpとcnnで学習済みです
|
18
|
+
|
19
|
+
|
20
|
+
|
17
21
|
```python
|
18
22
|
|
23
|
+
from keras.datasets import cifar10
|
24
|
+
|
25
|
+
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
import matplotlib.pyplot as plt
|
30
|
+
|
31
|
+
from PIL import Image
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
plt.figure(figsize=(10, 10))
|
36
|
+
|
37
|
+
labels = ["airplene", "automobile", "bord", "cat", "deer", "dog", "frog", "hprse", "ship", "truck"]
|
38
|
+
|
39
|
+
for i in range(0, 40):
|
40
|
+
|
41
|
+
im = Image.fromarray(x_train[i])
|
42
|
+
|
43
|
+
plt.subplot(5, 8, i + 1)
|
44
|
+
|
45
|
+
plt.title(labels[y_train[i][0]])
|
46
|
+
|
47
|
+
plt.tick_params(labelbottom="off",bottom="off")
|
48
|
+
|
49
|
+
plt.tick_params(labelleft="off",left="off")
|
50
|
+
|
51
|
+
plt.imshow(im)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
plt.show()
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
import matplotlib.pyplot as plt
|
60
|
+
|
61
|
+
import keras
|
62
|
+
|
63
|
+
from keras.datasets import cifar10
|
64
|
+
|
65
|
+
from keras.models import Sequential
|
66
|
+
|
67
|
+
from keras.layers import Dense, Dropout
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
num_classes = 10
|
72
|
+
|
73
|
+
im_rows = 32
|
74
|
+
|
75
|
+
im_cols = 32
|
76
|
+
|
77
|
+
im_size = im_rows * im_cols * 3
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
# データを読み込む --- (*1)
|
82
|
+
|
83
|
+
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
# データを一次元配列に変換 --- (*2)
|
88
|
+
|
89
|
+
X_train = X_train.reshape(-1, im_size).astype('float32') / 255
|
90
|
+
|
91
|
+
X_test = X_test.reshape(-1, im_size).astype('float32') / 255
|
92
|
+
|
93
|
+
# ラベルデータをOne-Hot形式に変換
|
94
|
+
|
95
|
+
y_train = keras.utils.to_categorical(y_train, num_classes)
|
96
|
+
|
97
|
+
y_test = keras.utils.to_categorical(y_test, num_classes)
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
# モデルを定義 --- (*3)
|
102
|
+
|
103
|
+
model = Sequential()
|
104
|
+
|
105
|
+
model.add(Dense(512, activation='relu', input_shape=(im_size,)))
|
106
|
+
|
107
|
+
model.add(Dense(num_classes, activation='softmax'))
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# モデルをコンパイル --- (*4)
|
112
|
+
|
113
|
+
model.compile(
|
114
|
+
|
115
|
+
loss='categorical_crossentropy',
|
116
|
+
|
117
|
+
optimizer='adam',
|
118
|
+
|
119
|
+
metrics=['accuracy'])
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
# 学習を実行 --- (*5)
|
124
|
+
|
125
|
+
hist = model.fit(X_train, y_train,
|
126
|
+
|
127
|
+
batch_size=32, epochs=50,
|
128
|
+
|
129
|
+
verbose=1,
|
130
|
+
|
131
|
+
validation_data=(X_test, y_test))
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
# モデルを評価 --- (*6)
|
136
|
+
|
137
|
+
score = model.evaluate(X_test, y_test, verbose=1)
|
138
|
+
|
139
|
+
print('正解率=', score[1], 'loss=', score[0])
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
# 学習の様子をグラフへ描画 --- (*7)
|
144
|
+
|
145
|
+
plt.plot(hist.history['acc'])
|
146
|
+
|
147
|
+
plt.plot(hist.history['val_acc'])
|
148
|
+
|
149
|
+
plt.title('Accuracy')
|
150
|
+
|
151
|
+
plt.legend(['train', 'test'], loc='upper left')
|
152
|
+
|
153
|
+
plt.show()
|
154
|
+
|
155
|
+
plt.plot(hist.history['loss'])
|
156
|
+
|
157
|
+
plt.plot(hist.history['val_loss'])
|
158
|
+
|
159
|
+
plt.title('Loss')
|
160
|
+
|
161
|
+
plt.legend(['train', 'test'], loc='upper left')
|
162
|
+
|
163
|
+
plt.show()
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
import matplotlib.pyplot as plt
|
168
|
+
|
169
|
+
import keras
|
170
|
+
|
171
|
+
from keras.datasets import cifar10
|
172
|
+
|
173
|
+
from keras.models import Sequential
|
174
|
+
|
175
|
+
from keras.layers import Dense, Dropout, Activation, Flatten
|
176
|
+
|
177
|
+
from keras.layers import Conv2D, MaxPooling2D
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
num_classes = 10
|
182
|
+
|
183
|
+
im_rows = 32
|
184
|
+
|
185
|
+
im_cols = 32
|
186
|
+
|
187
|
+
in_shape = (im_rows, im_cols, 3)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
# データを読み込む --- (*1)
|
192
|
+
|
193
|
+
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
# データを正規化 --- (*2)
|
198
|
+
|
199
|
+
X_train = X_train.astype('float32') / 255
|
200
|
+
|
201
|
+
X_test = X_test.astype('float32') / 255
|
202
|
+
|
203
|
+
# ラベルデータをOne-Hot形式に変換
|
204
|
+
|
205
|
+
y_train = keras.utils.to_categorical(y_train, num_classes)
|
206
|
+
|
207
|
+
y_test = keras.utils.to_categorical(y_test, num_classes)
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
# モデルを定義 --- (*3)
|
212
|
+
|
213
|
+
model = Sequential()
|
214
|
+
|
215
|
+
model.add(Conv2D(32, (3, 3), padding='same',
|
216
|
+
|
217
|
+
input_shape=in_shape))
|
218
|
+
|
219
|
+
model.add(Activation('relu'))
|
220
|
+
|
221
|
+
model.add(Conv2D(32, (3, 3)))
|
222
|
+
|
223
|
+
model.add(Activation('relu'))
|
224
|
+
|
225
|
+
model.add(MaxPooling2D(pool_size=(2, 2)))
|
226
|
+
|
227
|
+
model.add(Dropout(0.25))
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
model.add(Conv2D(64, (3, 3), padding='same'))
|
232
|
+
|
233
|
+
model.add(Activation('relu'))
|
234
|
+
|
235
|
+
model.add(Conv2D(64, (3, 3)))
|
236
|
+
|
237
|
+
model.add(Activation('relu'))
|
238
|
+
|
239
|
+
model.add(MaxPooling2D(pool_size=(2, 2)))
|
240
|
+
|
241
|
+
model.add(Dropout(0.25))
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
model.add(Flatten())
|
246
|
+
|
247
|
+
model.add(Dense(512))
|
248
|
+
|
249
|
+
model.add(Activation('relu'))
|
250
|
+
|
251
|
+
model.add(Dropout(0.5))
|
252
|
+
|
253
|
+
model.add(Dense(num_classes))
|
254
|
+
|
255
|
+
model.add(Activation('softmax'))
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
# モデルをコンパイル --- (*4)
|
260
|
+
|
261
|
+
model.compile(
|
262
|
+
|
263
|
+
loss='categorical_crossentropy',
|
264
|
+
|
265
|
+
optimizer='adam',
|
266
|
+
|
267
|
+
metrics=['accuracy'])
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
# 学習を実行 --- (*5)
|
272
|
+
|
273
|
+
hist = model.fit(X_train, y_train,
|
274
|
+
|
275
|
+
batch_size=32, epochs=50,
|
276
|
+
|
277
|
+
verbose=1,
|
278
|
+
|
279
|
+
validation_data=(X_test, y_test))
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
# モデルを評価 --- (*6)
|
284
|
+
|
285
|
+
score = model.evaluate(X_test, y_test, verbose=1)
|
286
|
+
|
287
|
+
print('正解率=', score[1], 'loss=', score[0])
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
# 学習の様子をグラフへ描画 --- (*7)
|
292
|
+
|
293
|
+
plt.plot(hist.history['acc'])
|
294
|
+
|
295
|
+
plt.plot(hist.history['val_acc'])
|
296
|
+
|
297
|
+
plt.title('Accuracy')
|
298
|
+
|
299
|
+
plt.legend(['train', 'test'], loc='upper left')
|
300
|
+
|
301
|
+
plt.show()
|
302
|
+
|
303
|
+
plt.plot(hist.history['loss'])
|
304
|
+
|
305
|
+
plt.plot(hist.history['val_loss'])
|
306
|
+
|
307
|
+
plt.title('Loss')
|
308
|
+
|
309
|
+
plt.legend(['train', 'test'], loc='upper left')
|
310
|
+
|
311
|
+
plt.show()
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
model.save_weights("cifar10-weight.h5")
|
316
|
+
|
317
|
+
model.load_weights("cifar10-weight.h5")
|
318
|
+
|
319
|
+
model.save_weights("cifar10-mlp-weight.h5")
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
|
328
|
+
|
19
329
|
import cv2
|
20
330
|
|
21
331
|
import numpy as np
|