質問編集履歴
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -60,8 +60,394 @@
|
|
60
60
|
|
61
61
|
|
62
62
|
|
63
|
+
|
64
|
+
|
63
|
-
|
65
|
+
全体のコードは以下
|
66
|
+
|
64
|
-
|
67
|
+
```python
|
68
|
+
|
69
|
+
from google.colab import drive
|
70
|
+
|
65
|
-
|
71
|
+
drive.mount('/content/drive')
|
72
|
+
|
73
|
+
|
74
|
+
|
66
|
-
|
75
|
+
import sys
|
76
|
+
|
77
|
+
import numpy as np
|
78
|
+
|
79
|
+
import matplotlib.pyplot as plt
|
80
|
+
|
81
|
+
|
82
|
+
|
67
|
-
|
83
|
+
sys.path.append('/content/drive/My Drive')
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
import ActivationFunction as AF
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
from PIL import Image
|
92
|
+
|
93
|
+
from IPython.display import display
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
img = Image.open("drive/My Drive/mnist_dataset/rei.jpeg")
|
98
|
+
|
99
|
+
img = img.resize((100, 100))
|
100
|
+
|
101
|
+
img = np.asarray(img)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
size = 5
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
v_split = img.shape[0] // size
|
110
|
+
|
111
|
+
h_split = img.shape[1] // size
|
112
|
+
|
113
|
+
out_img = []
|
114
|
+
|
115
|
+
[out_img.extend(np.hsplit(h_img, h_split))
|
116
|
+
|
117
|
+
for h_img in np.vsplit(img, v_split)]
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
plt.subplot(161).imshow(out_img[0])
|
122
|
+
|
123
|
+
plt.subplot(162).imshow(out_img[1])
|
124
|
+
|
125
|
+
plt.subplot(163).imshow(out_img[2])
|
126
|
+
|
127
|
+
plt.subplot(164).imshow(out_img[3])
|
128
|
+
|
129
|
+
plt.subplot(165).imshow(out_img[4])
|
130
|
+
|
131
|
+
plt.subplot(166).imshow(out_img[5])
|
132
|
+
|
133
|
+
plt.subplot(167).imshow(out_img[6])
|
134
|
+
|
135
|
+
plt.subplot(168).imshow(out_img[7])
|
136
|
+
|
137
|
+
plt.subplot(169).imshow(out_img[8])
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
# PILで開いたうえでデータをNumpy形式にする
|
142
|
+
|
143
|
+
# (例えばJPEGは圧縮されていてNumpyな配列になっていないので、
|
144
|
+
|
145
|
+
# そこからNumpyのデータ空間(?)に持ってくる必要がある)
|
146
|
+
|
147
|
+
tefilename = "test2.png"
|
148
|
+
|
149
|
+
teimg = Image.open("drive/My Drive/mnist_dataset/" + tefilename)
|
150
|
+
|
151
|
+
teimg = teimg.resize((10, 10))
|
152
|
+
|
153
|
+
teimg = np.asarray(teimg)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
def extract(x, y):
|
158
|
+
|
159
|
+
# カラー画像の時Gだけ抜き取りたい
|
160
|
+
|
161
|
+
if len(x.shape) == 3:
|
162
|
+
|
163
|
+
h, w, ch = x.shape
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
# RGBのGだけ抜き取りたい
|
168
|
+
|
169
|
+
return x[:,:,y]
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
v_max, v_min = 300, 200
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
def diff(x):
|
178
|
+
|
179
|
+
imgrows, lenrows, imgcolumns, lencolumns = [], [], [], []
|
180
|
+
|
181
|
+
for (img, imgt) in zip(x, x.T):
|
182
|
+
|
183
|
+
rows = img[(v_min<img)&(v_max>img)]
|
184
|
+
|
185
|
+
columns = imgt[(v_min<imgt)&(v_max>imgt)]
|
186
|
+
|
187
|
+
imgrows.append(rows)
|
188
|
+
|
189
|
+
lenrows.append(len(rows))
|
190
|
+
|
191
|
+
imgcolumns.append(columns)
|
192
|
+
|
193
|
+
lencolumns.append(len(columns))
|
194
|
+
|
195
|
+
return lenrows + lencolumns
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
test_data_list = []
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
test_data_list.append([0] + diff(extract(teimg, 1)) + diff(extract(teimg, 2)) + diff(extract(teimg, 0))) # 略
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
out_data_list0 = []
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
out_data_list0.append([0] + diff(extract(out_img[0], 1)) + diff(extract(out_img[0], 2)) + diff(extract(out_img[0], 0)))
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
out_data_list1 = []
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
out_data_list1.append([0] + diff(extract(out_img[1], 1)) + diff(extract(out_img[1], 2)) + diff(extract(out_img[1], 0)))
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
# 見本データに対しても同様に
|
224
|
+
|
225
|
+
# exについて同様に
|
226
|
+
|
227
|
+
training_data_list = []
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
for i in range(10):
|
232
|
+
|
233
|
+
for e in range(1):
|
234
|
+
|
235
|
+
trad = Image.open("drive/My Drive/mnist_dataset/" + str(10*i+e) + ".png")
|
236
|
+
|
237
|
+
trad = trad.resize((10, 10))
|
238
|
+
|
239
|
+
trad = np.asarray(trad)
|
240
|
+
|
241
|
+
#g #b #r 抽出後diffしてappend
|
242
|
+
|
243
|
+
training_data_list.append([i] + diff(extract(trad, 1)) + diff(extract(trad, 2)) + diff(extract(trad, 0))) # 略
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
print("training_data_list" ,training_data_list)
|
248
|
+
|
249
|
+
print("training_data_list[1:]" ,training_data_list[1:])
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
# 3層ニューラルネットワーク
|
254
|
+
|
255
|
+
class ThreeLayerNetwork:
|
256
|
+
|
257
|
+
# コンストラクタ
|
258
|
+
|
259
|
+
def __init__(self, inodes, hnodes, onodes, lr):
|
260
|
+
|
261
|
+
# 各レイヤーのノード数
|
262
|
+
|
263
|
+
self.inodes = inodes
|
264
|
+
|
265
|
+
self.hnodes = hnodes
|
266
|
+
|
267
|
+
self.onodes = onodes
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
# 学習率
|
272
|
+
|
273
|
+
self.lr = lr
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
# 重みの初期化
|
278
|
+
|
279
|
+
self.w_ih = np.random.normal(0.0, 1.0, (self.hnodes, self.inodes))
|
280
|
+
|
281
|
+
self.w_ho = np.random.normal(0.0, 1.0, (self.onodes, self.hnodes))
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
# 活性化関数
|
286
|
+
|
287
|
+
self.af = AF.sigmoid
|
288
|
+
|
289
|
+
self.daf = AF.derivative_sigmoid
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
# 誤差逆伝搬
|
294
|
+
|
295
|
+
def backprop(self, idata, tdata):
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
# 縦ベクトルに変換
|
300
|
+
|
301
|
+
o_i = np.array(idata, ndmin=2).T
|
302
|
+
|
303
|
+
t = np.array(tdata, ndmin=2).T
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
# 隠れ層
|
308
|
+
|
309
|
+
np.set_printoptions(threshold=10000)
|
310
|
+
|
311
|
+
x_h = np.dot(self.w_ih, o_i)
|
312
|
+
|
313
|
+
o_h = self.af(x_h)
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
# 出力層
|
318
|
+
|
319
|
+
x_o = np.dot(self.w_ho, o_h)
|
320
|
+
|
321
|
+
o_o = self.af(x_o)
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
# 誤差計算
|
326
|
+
|
327
|
+
e_o = (t - o_o)
|
328
|
+
|
329
|
+
e_h = np.dot(self.w_ho.T, e_o)
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
# 重みの更新
|
334
|
+
|
335
|
+
self.w_ho += self.lr * np.dot((e_o * self.daf(o_o)), o_h.T)
|
336
|
+
|
337
|
+
self.w_ih += self.lr * np.dot((e_h * self.daf(o_h)), o_i.T)
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
# 順伝搬
|
344
|
+
|
345
|
+
def feedforward(self, idata):
|
346
|
+
|
347
|
+
# 入力のリストを縦ベクトルに変換
|
348
|
+
|
349
|
+
o_i = np.array(idata, ndmin=2).T
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
# 隠れ層
|
354
|
+
|
355
|
+
x_h = np.dot(self.w_ih, o_i)
|
356
|
+
|
357
|
+
o_h = self.af(x_h)
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
# 出力層
|
362
|
+
|
363
|
+
x_o = np.dot(self.w_ho, o_h)
|
364
|
+
|
365
|
+
o_o = self.af(x_o)
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
return o_o
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
if __name__=='__main__':
|
374
|
+
|
375
|
+
# パラメータ
|
376
|
+
|
377
|
+
#inodes=784から30に変更
|
378
|
+
|
379
|
+
inodes = 31
|
380
|
+
|
381
|
+
hnodes = 100
|
382
|
+
|
383
|
+
onodes = 10
|
384
|
+
|
385
|
+
lr = 0.3
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
# ニューラルネットワークの初期化
|
390
|
+
|
391
|
+
nn = ThreeLayerNetwork(inodes, hnodes, onodes, lr)
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
# 学習
|
396
|
+
|
397
|
+
epoch = 50
|
398
|
+
|
399
|
+
# 50000
|
400
|
+
|
401
|
+
for e in range(epoch):
|
402
|
+
|
403
|
+
print('#epoch ', e)
|
404
|
+
|
405
|
+
data_size = len(training_data_list)
|
406
|
+
|
407
|
+
for i in range(data_size):
|
408
|
+
|
409
|
+
if i % 1000 == 0:
|
410
|
+
|
411
|
+
print(' train: {0:>5d} / {1:>5d}'.format(i, data_size))
|
412
|
+
|
413
|
+
idata = (np.array(out_data_list1) / 255.0 * 0.99) + 0.01
|
414
|
+
|
415
|
+
# 変更の余地あり
|
416
|
+
|
417
|
+
tdata = np.zeros(onodes) + 0.01
|
418
|
+
|
419
|
+
tdata[out_data_list1[0]] = 0.99
|
420
|
+
|
421
|
+
nn.backprop(idata, tdata)
|
422
|
+
|
423
|
+
pass
|
424
|
+
|
425
|
+
pass
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
# テスト
|
430
|
+
|
431
|
+
scoreboard = []
|
432
|
+
|
433
|
+
for record in test_data_list:
|
434
|
+
|
435
|
+
idata = (np.array(out_data_list0) / 255.0 * 0.99) + 0.01
|
436
|
+
|
437
|
+
predict = nn.feedforward(idata)
|
438
|
+
|
439
|
+
plabel = np.argmax(predict)
|
440
|
+
|
441
|
+
print("predict" ,predict)
|
442
|
+
|
443
|
+
print("plabel" ,plabel)
|
444
|
+
|
445
|
+
pass
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
scoreboard_array = np.asarray(scoreboard)
|
450
|
+
|
451
|
+
print('performance: ', scoreboard_array.sum() / scoreboard_array.size)
|
452
|
+
|
453
|
+
```
|
1
引用元を書きました
test
CHANGED
File without changes
|
test
CHANGED
@@ -57,3 +57,11 @@
|
|
57
57
|
ならリスト変数に数が格納されるのではと思ったがそうでもない...?
|
58
58
|
|
59
59
|
また、複雑すぎて何をやっているのか分からない。。
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
このへんを引用しました
|
64
|
+
|
65
|
+
https://teratail.com/questions/338466
|
66
|
+
|
67
|
+
https://teratail.com/questions/338168
|