質問編集履歴
3
title
CHANGED
File without changes
|
body
CHANGED
@@ -26,4 +26,177 @@
|
|
26
26
|
|
27
27
|
とすると、下の画像の集まりしか表示されないのですが、
|
28
28
|
plt.subplotは、2つ書くと最後の方が優先されるのでしょうか?
|
29
|
-
両方表示するにはどうすればいいんでしょうか。
|
29
|
+
両方表示するにはどうすればいいんでしょうか。
|
30
|
+
|
31
|
+
コード全体は以下です。
|
32
|
+
```python
|
33
|
+
from google.colab import drive
|
34
|
+
drive.mount('/content/drive')
|
35
|
+
|
36
|
+
import sys
|
37
|
+
import numpy as np
|
38
|
+
import matplotlib.pyplot as plt
|
39
|
+
|
40
|
+
sys.path.append('/content/drive/My Drive')
|
41
|
+
|
42
|
+
import ActivationFunction as AF
|
43
|
+
|
44
|
+
from PIL import Image
|
45
|
+
from IPython.display import display
|
46
|
+
|
47
|
+
img = Image.open("drive/My Drive/mnist_dataset/rei.jpeg")
|
48
|
+
img = img.resize((100, 100))
|
49
|
+
img = np.asarray(img)
|
50
|
+
|
51
|
+
plt.imshow(img)
|
52
|
+
|
53
|
+
size = 5
|
54
|
+
|
55
|
+
v_split = img.shape[0] // size
|
56
|
+
h_split = img.shape[1] // size
|
57
|
+
out_img = []
|
58
|
+
[out_img.extend(np.hsplit(h_img, h_split))
|
59
|
+
for h_img in np.vsplit(img, v_split)]
|
60
|
+
|
61
|
+
plt.figure(figsize=(100,100))
|
62
|
+
|
63
|
+
# imgの画像を表示 元は20,20,i+1...
|
64
|
+
# for i in range(len(out_img)):
|
65
|
+
# plt.subplot(20, 20, i+1).imshow(out_img[i])
|
66
|
+
|
67
|
+
def extract(x, y):
|
68
|
+
# カラー画像の時Gだけ抜き取りたい
|
69
|
+
if len(x.shape) == 3:
|
70
|
+
h, w, ch = x.shape
|
71
|
+
|
72
|
+
# RGBのGだけ抜き取りたい
|
73
|
+
return x[:,:,y]
|
74
|
+
|
75
|
+
# v_max, v_min = 300, 200から変更
|
76
|
+
v_max, v_min = 255, 250
|
77
|
+
|
78
|
+
def diff(x):
|
79
|
+
imgrows, lenrows, imgcolumns, lencolumns = [], [], [], []
|
80
|
+
for (img, imgt) in zip(x, x.T):
|
81
|
+
rows = img[(v_min<img)&(v_max>img)]
|
82
|
+
columns = imgt[(v_min<imgt)&(v_max>imgt)]
|
83
|
+
imgrows.append(rows)
|
84
|
+
lenrows.append(len(rows))
|
85
|
+
imgcolumns.append(columns)
|
86
|
+
lencolumns.append(len(columns))
|
87
|
+
return lenrows + lencolumns
|
88
|
+
|
89
|
+
np.set_printoptions(threshold=10000)
|
90
|
+
print(type(extract(out_img[i], 1)))
|
91
|
+
print(type(diff(extract(out_img[i], 1))))
|
92
|
+
print(type(out_img[0][0]))
|
93
|
+
|
94
|
+
# 見本データに対しても同様に
|
95
|
+
# exについて同様に
|
96
|
+
training_data_list = []
|
97
|
+
|
98
|
+
for i in range(len(out_img)):
|
99
|
+
#g #b #r 抽出後diffしてappend
|
100
|
+
training_data_list.append([i] + diff(extract(out_img[i], 1)) + diff(extract(out_img[i], 2)) + diff(extract(out_img[i], 0))) # 略
|
101
|
+
|
102
|
+
# 3層ニューラルネットワーク
|
103
|
+
class ThreeLayerNetwork:
|
104
|
+
# コンストラクタ
|
105
|
+
def __init__(self, inodes, hnodes, onodes, lr):
|
106
|
+
# 各レイヤーのノード数
|
107
|
+
self.inodes = inodes
|
108
|
+
self.hnodes = hnodes
|
109
|
+
self.onodes = onodes
|
110
|
+
|
111
|
+
# 学習率
|
112
|
+
self.lr = lr
|
113
|
+
|
114
|
+
# 重みの初期化
|
115
|
+
self.w_ih = np.random.normal(0.0, 1.0, (self.hnodes, self.inodes))
|
116
|
+
self.w_ho = np.random.normal(0.0, 1.0, (self.onodes, self.hnodes))
|
117
|
+
|
118
|
+
# 活性化関数
|
119
|
+
self.af = AF.sigmoid
|
120
|
+
self.daf = AF.derivative_sigmoid
|
121
|
+
|
122
|
+
# 誤差逆伝搬
|
123
|
+
def backprop(self, idata, tdata):
|
124
|
+
|
125
|
+
# 縦ベクトルに変換
|
126
|
+
o_i = np.array(idata, ndmin=2).T
|
127
|
+
t = np.array(tdata, ndmin=2).T
|
128
|
+
|
129
|
+
# 隠れ層
|
130
|
+
x_h = np.dot(self.w_ih, o_i)
|
131
|
+
o_h = self.af(x_h)
|
132
|
+
|
133
|
+
# 出力層
|
134
|
+
x_o = np.dot(self.w_ho, o_h)
|
135
|
+
o_o = self.af(x_o)
|
136
|
+
|
137
|
+
# 誤差計算
|
138
|
+
e_o = (t - o_o)
|
139
|
+
e_h = np.dot(self.w_ho.T, e_o)
|
140
|
+
|
141
|
+
# 重みの更新
|
142
|
+
self.w_ho += self.lr * np.dot((e_o * self.daf(o_o)), o_h.T)
|
143
|
+
self.w_ih += self.lr * np.dot((e_h * self.daf(o_h)), o_i.T)
|
144
|
+
|
145
|
+
|
146
|
+
# 順伝搬
|
147
|
+
def feedforward(self, idata):
|
148
|
+
# 入力のリストを縦ベクトルに変換
|
149
|
+
o_i = np.array(idata, ndmin=2).T
|
150
|
+
|
151
|
+
# 隠れ層
|
152
|
+
x_h = np.dot(self.w_ih, o_i)
|
153
|
+
o_h = self.af(x_h)
|
154
|
+
|
155
|
+
# 出力層
|
156
|
+
x_o = np.dot(self.w_ho, o_h)
|
157
|
+
o_o = self.af(x_o)
|
158
|
+
|
159
|
+
return o_o
|
160
|
+
|
161
|
+
if __name__=='__main__':
|
162
|
+
# パラメータ
|
163
|
+
#inodes=784から30に変更
|
164
|
+
inodes = 30
|
165
|
+
hnodes = 100
|
166
|
+
onodes = len(training_data_list)
|
167
|
+
lr = 0.3
|
168
|
+
|
169
|
+
# ニューラルネットワークの初期化
|
170
|
+
nn = ThreeLayerNetwork(inodes, hnodes, onodes, lr)
|
171
|
+
|
172
|
+
# 学習
|
173
|
+
epoch = 50
|
174
|
+
for e in range(epoch):
|
175
|
+
data_size = len(training_data_list)
|
176
|
+
for i in range(data_size):
|
177
|
+
idata = (np.array(training_data_list[i][1:]) / 255.0 * 0.99) + 0.01
|
178
|
+
# 変更の余地あり
|
179
|
+
tdata = np.zeros(onodes) + 0.01
|
180
|
+
tdata[training_data_list[i][0]] = 0.99
|
181
|
+
nn.backprop(idata, tdata)
|
182
|
+
pass
|
183
|
+
pass
|
184
|
+
|
185
|
+
# テスト
|
186
|
+
scoreboard = []
|
187
|
+
for i in range(len(training_data_list)):
|
188
|
+
idata = (np.array(training_data_list[i][1:]) / 255.0 * 0.99) + 0.01
|
189
|
+
predict = nn.feedforward(idata)
|
190
|
+
plabel = np.argmax(predict)
|
191
|
+
print("plabel" ,plabel)
|
192
|
+
#修正値plabelにimg値を修正する。
|
193
|
+
out_img[i] = out_img[plabel]
|
194
|
+
pass
|
195
|
+
scoreboard_array = np.asarray(scoreboard)
|
196
|
+
print('performance: ', scoreboard_array.sum() / scoreboard_array.size)
|
197
|
+
|
198
|
+
|
199
|
+
# imgの値を修正し、かつ修正した画像を表示 元は20,20,i+1...
|
200
|
+
for i in range(len(out_img)):
|
201
|
+
plt.subplot(20, 20, i+1).imshow(out_img[i])
|
202
|
+
```
|
2
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,6 +2,24 @@
|
|
2
2
|
for i in range(len(out_img)):
|
3
3
|
plt.subplot(20, 20, i+1).imshow(out_img[i])
|
4
4
|
|
5
|
+
中略
|
6
|
+
|
7
|
+
# テスト
|
8
|
+
scoreboard = []
|
9
|
+
for i in range(len(training_data_list)):
|
10
|
+
idata = (np.array(training_data_list[i][1:]) / 255.0 * 0.99) + 0.01
|
11
|
+
predict = nn.feedforward(idata)
|
12
|
+
plabel = np.argmax(predict)
|
13
|
+
print("plabel" ,plabel)
|
14
|
+
#修正値plabelにimg値を修正する。
|
15
|
+
out_img[i] = out_img[plabel]
|
16
|
+
pass
|
17
|
+
scoreboard_array = np.asarray(scoreboard)
|
18
|
+
print('performance: ', scoreboard_array.sum() / scoreboard_array.size)
|
19
|
+
|
20
|
+
中略
|
21
|
+
|
22
|
+
|
5
23
|
for i in range(len(out_img)):
|
6
24
|
plt.subplot(20, 20, i+1).imshow(out_img[i])
|
7
25
|
```
|
1
タイトルを変更しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
plt.subplotを2回使えない。
|
body
CHANGED
File without changes
|