質問編集履歴
2
title
CHANGED
File without changes
|
body
CHANGED
@@ -29,6 +29,199 @@
|
|
29
29
|
ならリスト変数に数が格納されるのではと思ったがそうでもない...?
|
30
30
|
また、複雑すぎて何をやっているのか分からない。。
|
31
31
|
|
32
|
+
|
32
|
-
|
33
|
+
全体のコードは以下
|
34
|
+
```python
|
35
|
+
from google.colab import drive
|
33
|
-
|
36
|
+
drive.mount('/content/drive')
|
37
|
+
|
38
|
+
import sys
|
39
|
+
import numpy as np
|
40
|
+
import matplotlib.pyplot as plt
|
41
|
+
|
34
|
-
|
42
|
+
sys.path.append('/content/drive/My Drive')
|
43
|
+
|
44
|
+
import ActivationFunction as AF
|
45
|
+
|
46
|
+
from PIL import Image
|
47
|
+
from IPython.display import display
|
48
|
+
|
49
|
+
img = Image.open("drive/My Drive/mnist_dataset/rei.jpeg")
|
50
|
+
img = img.resize((100, 100))
|
51
|
+
img = np.asarray(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.subplot(161).imshow(out_img[0])
|
62
|
+
plt.subplot(162).imshow(out_img[1])
|
63
|
+
plt.subplot(163).imshow(out_img[2])
|
64
|
+
plt.subplot(164).imshow(out_img[3])
|
65
|
+
plt.subplot(165).imshow(out_img[4])
|
66
|
+
plt.subplot(166).imshow(out_img[5])
|
67
|
+
plt.subplot(167).imshow(out_img[6])
|
68
|
+
plt.subplot(168).imshow(out_img[7])
|
69
|
+
plt.subplot(169).imshow(out_img[8])
|
70
|
+
|
71
|
+
# PILで開いたうえでデータをNumpy形式にする
|
72
|
+
# (例えばJPEGは圧縮されていてNumpyな配列になっていないので、
|
73
|
+
# そこからNumpyのデータ空間(?)に持ってくる必要がある)
|
74
|
+
tefilename = "test2.png"
|
75
|
+
teimg = Image.open("drive/My Drive/mnist_dataset/" + tefilename)
|
76
|
+
teimg = teimg.resize((10, 10))
|
77
|
+
teimg = np.asarray(teimg)
|
78
|
+
|
79
|
+
def extract(x, y):
|
80
|
+
# カラー画像の時Gだけ抜き取りたい
|
81
|
+
if len(x.shape) == 3:
|
82
|
+
h, w, ch = x.shape
|
83
|
+
|
84
|
+
# RGBのGだけ抜き取りたい
|
85
|
+
return x[:,:,y]
|
86
|
+
|
87
|
+
v_max, v_min = 300, 200
|
88
|
+
|
89
|
+
def diff(x):
|
90
|
+
imgrows, lenrows, imgcolumns, lencolumns = [], [], [], []
|
91
|
+
for (img, imgt) in zip(x, x.T):
|
92
|
+
rows = img[(v_min<img)&(v_max>img)]
|
93
|
+
columns = imgt[(v_min<imgt)&(v_max>imgt)]
|
94
|
+
imgrows.append(rows)
|
95
|
+
lenrows.append(len(rows))
|
96
|
+
imgcolumns.append(columns)
|
97
|
+
lencolumns.append(len(columns))
|
98
|
+
return lenrows + lencolumns
|
99
|
+
|
100
|
+
test_data_list = []
|
101
|
+
|
102
|
+
test_data_list.append([0] + diff(extract(teimg, 1)) + diff(extract(teimg, 2)) + diff(extract(teimg, 0))) # 略
|
103
|
+
|
104
|
+
out_data_list0 = []
|
105
|
+
|
106
|
+
out_data_list0.append([0] + diff(extract(out_img[0], 1)) + diff(extract(out_img[0], 2)) + diff(extract(out_img[0], 0)))
|
107
|
+
|
108
|
+
out_data_list1 = []
|
109
|
+
|
110
|
+
out_data_list1.append([0] + diff(extract(out_img[1], 1)) + diff(extract(out_img[1], 2)) + diff(extract(out_img[1], 0)))
|
111
|
+
|
112
|
+
# 見本データに対しても同様に
|
113
|
+
# exについて同様に
|
114
|
+
training_data_list = []
|
115
|
+
|
116
|
+
for i in range(10):
|
117
|
+
for e in range(1):
|
118
|
+
trad = Image.open("drive/My Drive/mnist_dataset/" + str(10*i+e) + ".png")
|
119
|
+
trad = trad.resize((10, 10))
|
120
|
+
trad = np.asarray(trad)
|
121
|
+
#g #b #r 抽出後diffしてappend
|
122
|
+
training_data_list.append([i] + diff(extract(trad, 1)) + diff(extract(trad, 2)) + diff(extract(trad, 0))) # 略
|
123
|
+
|
124
|
+
print("training_data_list" ,training_data_list)
|
125
|
+
print("training_data_list[1:]" ,training_data_list[1:])
|
126
|
+
|
127
|
+
# 3層ニューラルネットワーク
|
128
|
+
class ThreeLayerNetwork:
|
129
|
+
# コンストラクタ
|
130
|
+
def __init__(self, inodes, hnodes, onodes, lr):
|
131
|
+
# 各レイヤーのノード数
|
132
|
+
self.inodes = inodes
|
133
|
+
self.hnodes = hnodes
|
134
|
+
self.onodes = onodes
|
135
|
+
|
136
|
+
# 学習率
|
137
|
+
self.lr = lr
|
138
|
+
|
139
|
+
# 重みの初期化
|
140
|
+
self.w_ih = np.random.normal(0.0, 1.0, (self.hnodes, self.inodes))
|
141
|
+
self.w_ho = np.random.normal(0.0, 1.0, (self.onodes, self.hnodes))
|
142
|
+
|
143
|
+
# 活性化関数
|
144
|
+
self.af = AF.sigmoid
|
145
|
+
self.daf = AF.derivative_sigmoid
|
146
|
+
|
147
|
+
# 誤差逆伝搬
|
148
|
+
def backprop(self, idata, tdata):
|
149
|
+
|
150
|
+
# 縦ベクトルに変換
|
151
|
+
o_i = np.array(idata, ndmin=2).T
|
152
|
+
t = np.array(tdata, ndmin=2).T
|
153
|
+
|
154
|
+
# 隠れ層
|
155
|
+
np.set_printoptions(threshold=10000)
|
156
|
+
x_h = np.dot(self.w_ih, o_i)
|
157
|
+
o_h = self.af(x_h)
|
158
|
+
|
159
|
+
# 出力層
|
160
|
+
x_o = np.dot(self.w_ho, o_h)
|
161
|
+
o_o = self.af(x_o)
|
162
|
+
|
163
|
+
# 誤差計算
|
164
|
+
e_o = (t - o_o)
|
165
|
+
e_h = np.dot(self.w_ho.T, e_o)
|
166
|
+
|
167
|
+
# 重みの更新
|
168
|
+
self.w_ho += self.lr * np.dot((e_o * self.daf(o_o)), o_h.T)
|
169
|
+
self.w_ih += self.lr * np.dot((e_h * self.daf(o_h)), o_i.T)
|
170
|
+
|
171
|
+
|
172
|
+
# 順伝搬
|
173
|
+
def feedforward(self, idata):
|
174
|
+
# 入力のリストを縦ベクトルに変換
|
175
|
+
o_i = np.array(idata, ndmin=2).T
|
176
|
+
|
177
|
+
# 隠れ層
|
178
|
+
x_h = np.dot(self.w_ih, o_i)
|
179
|
+
o_h = self.af(x_h)
|
180
|
+
|
181
|
+
# 出力層
|
182
|
+
x_o = np.dot(self.w_ho, o_h)
|
183
|
+
o_o = self.af(x_o)
|
184
|
+
|
185
|
+
return o_o
|
186
|
+
|
187
|
+
if __name__=='__main__':
|
188
|
+
# パラメータ
|
189
|
+
#inodes=784から30に変更
|
190
|
+
inodes = 31
|
191
|
+
hnodes = 100
|
192
|
+
onodes = 10
|
193
|
+
lr = 0.3
|
194
|
+
|
195
|
+
# ニューラルネットワークの初期化
|
196
|
+
nn = ThreeLayerNetwork(inodes, hnodes, onodes, lr)
|
197
|
+
|
198
|
+
# 学習
|
199
|
+
epoch = 50
|
200
|
+
# 50000
|
201
|
+
for e in range(epoch):
|
202
|
+
print('#epoch ', e)
|
203
|
+
data_size = len(training_data_list)
|
204
|
+
for i in range(data_size):
|
205
|
+
if i % 1000 == 0:
|
206
|
+
print(' train: {0:>5d} / {1:>5d}'.format(i, data_size))
|
207
|
+
idata = (np.array(out_data_list1) / 255.0 * 0.99) + 0.01
|
208
|
+
# 変更の余地あり
|
209
|
+
tdata = np.zeros(onodes) + 0.01
|
210
|
+
tdata[out_data_list1[0]] = 0.99
|
211
|
+
nn.backprop(idata, tdata)
|
212
|
+
pass
|
213
|
+
pass
|
214
|
+
|
215
|
+
# テスト
|
216
|
+
scoreboard = []
|
217
|
+
for record in test_data_list:
|
218
|
+
idata = (np.array(out_data_list0) / 255.0 * 0.99) + 0.01
|
219
|
+
predict = nn.feedforward(idata)
|
220
|
+
plabel = np.argmax(predict)
|
221
|
+
print("predict" ,predict)
|
222
|
+
print("plabel" ,plabel)
|
223
|
+
pass
|
224
|
+
|
225
|
+
scoreboard_array = np.asarray(scoreboard)
|
226
|
+
print('performance: ', scoreboard_array.sum() / scoreboard_array.size)
|
227
|
+
```
|
1
引用元を書きました
title
CHANGED
File without changes
|
body
CHANGED
@@ -27,4 +27,8 @@
|
|
27
27
|
for h_img in np.vsplit(img, v_split)]
|
28
28
|
これは、式 for リスト変数 in 変数の形??
|
29
29
|
ならリスト変数に数が格納されるのではと思ったがそうでもない...?
|
30
|
-
また、複雑すぎて何をやっているのか分からない。。
|
30
|
+
また、複雑すぎて何をやっているのか分からない。。
|
31
|
+
|
32
|
+
このへんを引用しました
|
33
|
+
https://teratail.com/questions/338466
|
34
|
+
https://teratail.com/questions/338168
|