質問編集履歴
3
test
CHANGED
File without changes
|
test
CHANGED
@@ -55,3 +55,349 @@
|
|
55
55
|
plt.subplotは、2つ書くと最後の方が優先されるのでしょうか?
|
56
56
|
|
57
57
|
両方表示するにはどうすればいいんでしょうか。
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
コード全体は以下です。
|
62
|
+
|
63
|
+
```python
|
64
|
+
|
65
|
+
from google.colab import drive
|
66
|
+
|
67
|
+
drive.mount('/content/drive')
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
import sys
|
72
|
+
|
73
|
+
import numpy as np
|
74
|
+
|
75
|
+
import matplotlib.pyplot as plt
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
sys.path.append('/content/drive/My Drive')
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
import ActivationFunction as AF
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
from PIL import Image
|
88
|
+
|
89
|
+
from IPython.display import display
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
img = Image.open("drive/My Drive/mnist_dataset/rei.jpeg")
|
94
|
+
|
95
|
+
img = img.resize((100, 100))
|
96
|
+
|
97
|
+
img = np.asarray(img)
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
plt.imshow(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.figure(figsize=(100,100))
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
# imgの画像を表示 元は20,20,i+1...
|
126
|
+
|
127
|
+
# for i in range(len(out_img)):
|
128
|
+
|
129
|
+
# plt.subplot(20, 20, i+1).imshow(out_img[i])
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
def extract(x, y):
|
134
|
+
|
135
|
+
# カラー画像の時Gだけ抜き取りたい
|
136
|
+
|
137
|
+
if len(x.shape) == 3:
|
138
|
+
|
139
|
+
h, w, ch = x.shape
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
# RGBのGだけ抜き取りたい
|
144
|
+
|
145
|
+
return x[:,:,y]
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
# v_max, v_min = 300, 200から変更
|
150
|
+
|
151
|
+
v_max, v_min = 255, 250
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def diff(x):
|
156
|
+
|
157
|
+
imgrows, lenrows, imgcolumns, lencolumns = [], [], [], []
|
158
|
+
|
159
|
+
for (img, imgt) in zip(x, x.T):
|
160
|
+
|
161
|
+
rows = img[(v_min<img)&(v_max>img)]
|
162
|
+
|
163
|
+
columns = imgt[(v_min<imgt)&(v_max>imgt)]
|
164
|
+
|
165
|
+
imgrows.append(rows)
|
166
|
+
|
167
|
+
lenrows.append(len(rows))
|
168
|
+
|
169
|
+
imgcolumns.append(columns)
|
170
|
+
|
171
|
+
lencolumns.append(len(columns))
|
172
|
+
|
173
|
+
return lenrows + lencolumns
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
np.set_printoptions(threshold=10000)
|
178
|
+
|
179
|
+
print(type(extract(out_img[i], 1)))
|
180
|
+
|
181
|
+
print(type(diff(extract(out_img[i], 1))))
|
182
|
+
|
183
|
+
print(type(out_img[0][0]))
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
# 見本データに対しても同様に
|
188
|
+
|
189
|
+
# exについて同様に
|
190
|
+
|
191
|
+
training_data_list = []
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
for i in range(len(out_img)):
|
196
|
+
|
197
|
+
#g #b #r 抽出後diffしてappend
|
198
|
+
|
199
|
+
training_data_list.append([i] + diff(extract(out_img[i], 1)) + diff(extract(out_img[i], 2)) + diff(extract(out_img[i], 0))) # 略
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
# 3層ニューラルネットワーク
|
204
|
+
|
205
|
+
class ThreeLayerNetwork:
|
206
|
+
|
207
|
+
# コンストラクタ
|
208
|
+
|
209
|
+
def __init__(self, inodes, hnodes, onodes, lr):
|
210
|
+
|
211
|
+
# 各レイヤーのノード数
|
212
|
+
|
213
|
+
self.inodes = inodes
|
214
|
+
|
215
|
+
self.hnodes = hnodes
|
216
|
+
|
217
|
+
self.onodes = onodes
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
# 学習率
|
222
|
+
|
223
|
+
self.lr = lr
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
# 重みの初期化
|
228
|
+
|
229
|
+
self.w_ih = np.random.normal(0.0, 1.0, (self.hnodes, self.inodes))
|
230
|
+
|
231
|
+
self.w_ho = np.random.normal(0.0, 1.0, (self.onodes, self.hnodes))
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
# 活性化関数
|
236
|
+
|
237
|
+
self.af = AF.sigmoid
|
238
|
+
|
239
|
+
self.daf = AF.derivative_sigmoid
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
# 誤差逆伝搬
|
244
|
+
|
245
|
+
def backprop(self, idata, tdata):
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
# 縦ベクトルに変換
|
250
|
+
|
251
|
+
o_i = np.array(idata, ndmin=2).T
|
252
|
+
|
253
|
+
t = np.array(tdata, ndmin=2).T
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
# 隠れ層
|
258
|
+
|
259
|
+
x_h = np.dot(self.w_ih, o_i)
|
260
|
+
|
261
|
+
o_h = self.af(x_h)
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
# 出力層
|
266
|
+
|
267
|
+
x_o = np.dot(self.w_ho, o_h)
|
268
|
+
|
269
|
+
o_o = self.af(x_o)
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
# 誤差計算
|
274
|
+
|
275
|
+
e_o = (t - o_o)
|
276
|
+
|
277
|
+
e_h = np.dot(self.w_ho.T, e_o)
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
# 重みの更新
|
282
|
+
|
283
|
+
self.w_ho += self.lr * np.dot((e_o * self.daf(o_o)), o_h.T)
|
284
|
+
|
285
|
+
self.w_ih += self.lr * np.dot((e_h * self.daf(o_h)), o_i.T)
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
# 順伝搬
|
292
|
+
|
293
|
+
def feedforward(self, idata):
|
294
|
+
|
295
|
+
# 入力のリストを縦ベクトルに変換
|
296
|
+
|
297
|
+
o_i = np.array(idata, ndmin=2).T
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
# 隠れ層
|
302
|
+
|
303
|
+
x_h = np.dot(self.w_ih, o_i)
|
304
|
+
|
305
|
+
o_h = self.af(x_h)
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
# 出力層
|
310
|
+
|
311
|
+
x_o = np.dot(self.w_ho, o_h)
|
312
|
+
|
313
|
+
o_o = self.af(x_o)
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
return o_o
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
if __name__=='__main__':
|
322
|
+
|
323
|
+
# パラメータ
|
324
|
+
|
325
|
+
#inodes=784から30に変更
|
326
|
+
|
327
|
+
inodes = 30
|
328
|
+
|
329
|
+
hnodes = 100
|
330
|
+
|
331
|
+
onodes = len(training_data_list)
|
332
|
+
|
333
|
+
lr = 0.3
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
# ニューラルネットワークの初期化
|
338
|
+
|
339
|
+
nn = ThreeLayerNetwork(inodes, hnodes, onodes, lr)
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
# 学習
|
344
|
+
|
345
|
+
epoch = 50
|
346
|
+
|
347
|
+
for e in range(epoch):
|
348
|
+
|
349
|
+
data_size = len(training_data_list)
|
350
|
+
|
351
|
+
for i in range(data_size):
|
352
|
+
|
353
|
+
idata = (np.array(training_data_list[i][1:]) / 255.0 * 0.99) + 0.01
|
354
|
+
|
355
|
+
# 変更の余地あり
|
356
|
+
|
357
|
+
tdata = np.zeros(onodes) + 0.01
|
358
|
+
|
359
|
+
tdata[training_data_list[i][0]] = 0.99
|
360
|
+
|
361
|
+
nn.backprop(idata, tdata)
|
362
|
+
|
363
|
+
pass
|
364
|
+
|
365
|
+
pass
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
# テスト
|
370
|
+
|
371
|
+
scoreboard = []
|
372
|
+
|
373
|
+
for i in range(len(training_data_list)):
|
374
|
+
|
375
|
+
idata = (np.array(training_data_list[i][1:]) / 255.0 * 0.99) + 0.01
|
376
|
+
|
377
|
+
predict = nn.feedforward(idata)
|
378
|
+
|
379
|
+
plabel = np.argmax(predict)
|
380
|
+
|
381
|
+
print("plabel" ,plabel)
|
382
|
+
|
383
|
+
#修正値plabelにimg値を修正する。
|
384
|
+
|
385
|
+
out_img[i] = out_img[plabel]
|
386
|
+
|
387
|
+
pass
|
388
|
+
|
389
|
+
scoreboard_array = np.asarray(scoreboard)
|
390
|
+
|
391
|
+
print('performance: ', scoreboard_array.sum() / scoreboard_array.size)
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
# imgの値を修正し、かつ修正した画像を表示 元は20,20,i+1...
|
398
|
+
|
399
|
+
for i in range(len(out_img)):
|
400
|
+
|
401
|
+
plt.subplot(20, 20, i+1).imshow(out_img[i])
|
402
|
+
|
403
|
+
```
|
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,6 +3,42 @@
|
|
3
3
|
for i in range(len(out_img)):
|
4
4
|
|
5
5
|
plt.subplot(20, 20, i+1).imshow(out_img[i])
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
中略
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
# テスト
|
14
|
+
|
15
|
+
scoreboard = []
|
16
|
+
|
17
|
+
for i in range(len(training_data_list)):
|
18
|
+
|
19
|
+
idata = (np.array(training_data_list[i][1:]) / 255.0 * 0.99) + 0.01
|
20
|
+
|
21
|
+
predict = nn.feedforward(idata)
|
22
|
+
|
23
|
+
plabel = np.argmax(predict)
|
24
|
+
|
25
|
+
print("plabel" ,plabel)
|
26
|
+
|
27
|
+
#修正値plabelにimg値を修正する。
|
28
|
+
|
29
|
+
out_img[i] = out_img[plabel]
|
30
|
+
|
31
|
+
pass
|
32
|
+
|
33
|
+
scoreboard_array = np.asarray(scoreboard)
|
34
|
+
|
35
|
+
print('performance: ', scoreboard_array.sum() / scoreboard_array.size)
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
中略
|
40
|
+
|
41
|
+
|
6
42
|
|
7
43
|
|
8
44
|
|
1
タイトルを変更しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
plt.subplotを2回使えない。
|
test
CHANGED
File without changes
|