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