首页
社区
课程
招聘
一个图片分类器_神经网络_反向传播
2022-9-18 11:14 5403

一个图片分类器_神经网络_反向传播

2022-9-18 11:14
5403

说明

1
一个练习代码,使用神经网络分类10张数字图片。

代码

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from PIL import Image
import numpy as np
import sys
from datetime import datetime
import json
 
class Dot(object):
    def forward(self, X, W):
        self.X=X
        self.W=W
        return np.dot(X, W)
 
    def backward(self, dout):
        return np.dot(dout, self.W.T), np.dot(self.X.T, dout)  # dX, dW
 
class Add(object):
    def forward(self, D, B):
        return D+B
 
    def backward(self, dout):
        return np.sum(dout, axis=0#纵向求和
 
class Affine(object):
    def __init__(self):
        self.dot=Dot()
        self.add=Add()
 
    def forward(self, X, W, B):
        T=self.dot.forward(X, W)
        return self.add.forward(T, B)
 
    def backward(self, dout):
        dB = self.add.backward(dout)
        dX, dW = self.dot.backward(dout)
 
        return dX, dW, dB
 
class Relu(object):
    def forward(self, A):
        self.mask=(A<=0)
        A[self.mask]=0     #会破坏原参数,但是这次使用之后后面就没用了,所以破坏就破坏了,还能省点空间
        return A
 
    def backward(self, dout):
        dout[self.mask]=0
        return dout
 
class SoftmaxWithLoss(object):
    def softmax(self, A):
        A=A-A.max(axis=1, keepdims=True)
        T=np.exp(A)
        return T/np.sum(T, axis=1, keepdims=True)
 
    def crossEntropyError(self, Z, Label):
        delta=0.000000001
        return -np.sum(np.log(Z+delta)*Label)/Z.shape[0]
 
    def forward(self, A, Label):
        self.Z=self.softmax(A)
        self.Label=Label
        return self.Z, self.crossEntropyError(self.Z, Label)
 
    def backward(self, dout=1):
        return (self.Z-self.Label)*dout/self.Z.shape[0]
 
 
class SimpleImageClassifier(object):
    def __init__(self):
        # 初始化一个3层网络
        # 学习率设置为 0.01
        self.N0=28*28
        self.N1=10
        self.N2=10
        self.N3=10
        self.lr=0.01
        self.MAX_NUM=100000
 
        self.affine1=Affine()
        self.relu1  =Relu()
        self.affine2=Affine()
        self.relu2  =Relu()
        self.affine3=Affine()
        self.softmax_with_loss=SoftmaxWithLoss()
 
        '''
        self.W1=np.random.randn(self.N0, self.N1)
        self.B1=np.random.randn(self.N1)
 
        self.W2=np.random.randn(self.N1, self.N2)
        self.B2=np.random.randn(self.N2)
 
        self.W3=np.random.randn(self.N2, self.N3)
        self.B3=np.random.randn(self.N3)
        '''
 
        self.loadWB()
 
        print("self.W1\n",self.W1)
        print("self.B1\n",self.B1)
        print("self.W2\n",self.W2)
        print("self.B2\n",self.B2)
        print("self.W3\n",self.W3)
        print("self.B3\n",self.B3)
        print("")
 
    def loadImg(self):
        lst_img = []
        for i in range(0, 10):
            img = Image.open("img/{num}.jpg".format(num=i))
            lst_img.append({"data":img.tobytes(), "label":i})
 
        return lst_img
 
    def predictWithLoss(self, X, Label):
        # 0  [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]  -> [0.8, 0, 0.1, 0, 0.05, 0.05, 0, 0, 0, 0]
        # 1  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
        # 2  [0, 0, 1, 0, 0, 0, 0, 0, 0, 0] ...
 
        # 输入是 n*784的向量,n张图片,每张图有784个值 (784=28*28)
 
        A1=self.affine1.forward(X, self.W1, self.B1)
        Z1=  self.relu1.forward(A1)
        A2=self.affine2.forward(Z1, self.W2, self.B2)
        Z2=  self.relu2.forward(A2)
        A3=self.affine3.forward(Z2, self.W3, self.B3)
        Z3, l = self.softmax_with_loss.forward(A3, Label)
 
        return Z3, l
 
    def getNumericalGradient(self):
        # 使用反向传播计算W与B的偏导数
        D = self.softmax_with_loss.backward(dout=1)
        dZ2, dW3, dB3 = self.affine3.backward(D)
        dA2 = self.relu2.backward(dZ2)
        dZ1, dW2, dB2 = self.affine2.backward(dA2)
        dA1 = self.relu1.backward(dZ1)
        dX, dW1, dB1 = self.affine1.backward(dA1)
 
 
        '''
        print("grad_W1\n", dW1)
        print("grad_B1\n", dB1)
        print("grad_W2\n", dW2)
        print("grad_B2\n", dB2)
        print("grad_W3\n", dW3)
        print("grad_B3\n", dB3)
        '''
        return dW1,dB1,dW2,dB2,dW3,dB3
 
    def updateWB(self, grad_W1,grad_B1,grad_W2,grad_B2,grad_W3,grad_B3):
        def updateW(W, grad_W):
            W-=self.lr*grad_W
 
        def updateB(B, grad_B):
            B-=self.lr*grad_B
 
        updateW(self.W1, grad_W1)
        updateB(self.B1, grad_B1)
        updateW(self.W2, grad_W2)
        updateB(self.B2, grad_B2)
        updateW(self.W3, grad_W3)
        updateB(self.B3, grad_B3)
 
    def train(self):
        # 输入数据初始化
        X=[]
        Label=[]
 
        lst_img=self.loadImg()
        for dic in lst_img:
            b=max(dic["data"][:self.N0])
            X.append([ a/b for a in dic["data"][:self.N0] ])  # 归一化  #
 
            t=[0]*self.N3
            t[dic["label"]]=1
            Label.append(t)
 
        X=np.array( X )
        Label=np.array( Label )
 
        self.showDetail(X, Label, i=-1)
 
        # 算偏微分,更新权重参数
        for i in range(0, self.MAX_NUM):
            self.predictWithLoss(X, Label)
 
            grad_W1,grad_B1,grad_W2,grad_B2,grad_W3,grad_B3 = self.getNumericalGradient()
            self.updateWB(grad_W1,grad_B1,grad_W2,grad_B2,grad_W3,grad_B3)
 
            '''
            if i%20==0:
                print(i)
                print("self.W1\n",self.W1)
                print("self.B1\n",self.B1)
                print("self.W2\n",self.W2)
                print("self.B2\n",self.B2)
                print("self.W3\n",self.W3)
                print("self.B3\n",self.B3)
                print("")
            '''
 
            if i%20==0:
                self.showDetail(X, Label, i)
 
        self.showDetail(X, Label, i=self.MAX_NUM)
 
    def showDetail(self, X, Label, i=0):
        msg="i={i}  time:{time}".format(i=i, time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        print(msg)
        Z, l = self.predictWithLoss(X, Label)
        print(Z)
        print(l)
        print("")
 
        self.saveWB(out_file="wb.json", additional_data={"i":msg, "Z":Z.tolist(), "loss":l})
 
    def saveWB(self, out_file="wb.json", additional_data={}):
        dic={"W1":self.W1.tolist(), "B1":self.B1.tolist(), "W2":self.W2.tolist(), "B2":self.B2.tolist(), "W3":self.W3.tolist(), "B3":self.B3.tolist(), "additional_data":additional_data}
 
        fout=open(out_file, "a+")
        fout.write(json.dumps(dic))
        fout.write("\n")
        fout.close()
 
    def loadWB(self, in_file="wb.json"):
        WB='''{"W1": [[0.7189550494057414, 0.1989326646832533,...示意..., "loss": 0.008417616242222797}}'''
        dic=json.loads(WB)
        self.W1=np.array( dic["W1"] )
        self.B1=np.array( dic["B1"] )
        self.W2=np.array( dic["W2"] )
        self.B2=np.array( dic["B2"] )
        self.W3=np.array( dic["W3"] )
        self.B3=np.array( dic["B3"] )
 
    def whichNumber(self, img_file):
        img = Image.open(img_file)
        data=img.tobytes()
 
        X=[]
        b=max(data[:self.N0])
        X.append([ a/b for a in data[:self.N0] ])
 
 
        Z, l=self.predictWithLoss(X, [0]*10)
 
        print(Z[0])
        b=max(Z[0])
        num=np.where(Z[0]==b)[0][0]
        print("num={num},  rate={rate}".format(num=num, rate=b))
        return num
 
def main():
    a=SimpleImageClassifier()
    #a.train()
 
    a.whichNumber("img/9.jpg")
 
 
if "__main__"==__name__:
    main()

运行截图
图片描述

参考

《深度学习入门:基于Python的理论与实现》


[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界

最后于 2022-10-4 16:15 被Jtian编辑 ,原因:
上传的附件:
收藏
点赞0
打赏
分享
最新回复 (1)
雪    币: 119
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
不将、就 2022-9-18 17:09
2
0
mnist
游客
登录 | 注册 方可回帖
返回