在我之前的两篇博客中,讲解了使用纯numpy实现手写数字识别以及采用keras的多标签多分类进行验证码识别,建议阅读这篇文章之前提前看下另外两篇博客。

今天这篇博客将讲解使用纯numpy搭建多分类多标签算法来完成验证码识别任务。
多标签多分类与单标签多分类的一个显著的区别是输出层激活函数采用sigmoid函数以及损失函数采用二元交叉熵。
首先看激活函数sigmoid,sigmoid的数学公式为:

resnet50 多标签分类_resnet50 多标签分类

根据该公式采用numpy实现:

class Sigmoid:
    def __init__(self):
        self.out=None
    def forward(self,x):
        out=1/(1+np.exp(-x))
        self.out=out
        return out
    def backward(self,dout):
        dx=dout*self.out*(1.0-self.out)
        return dx

二元交叉熵的数学公式为:

resnet50 多标签分类_resnet50 多标签分类_02


用numpy实现其算法为:

def binary_cross_entropy_error(y,t):
    delta=1e-7
    batch_val=-np.sum(t*np.log(y+delta)+(1-t)*np.log(1-y+delta),axis=1)
    return np.sum(batch_val)/batch_val.size


#二元交叉熵
class Binary_crossentropy:
    def __init__(self):
        self.y=None
        self.t=None
    def forward(self,y,t):
        self.y=y
        self.t=t
        loss=binary_cross_entropy_error(y,t)
        return loss
    def backward(self,dout=1):
        delta=1e-7
        return -(self.t/(self.y+delta)-(1-self.t)/(1-self.y+delta))/self.t.shape[0]

有的上述知识铺垫,我们采用二元交叉熵首先来解决常见的任务,为了方便大家看到这个方法的正确性,先用二元交叉熵来处理手写数字识别的任务。

手写数字的识别

import numpy as np
from collections import OrderedDict
import matplotlib.pylab as plt
from dataset.mnist import load_mnist
import pickle

#标签的个数,因为是手写数字,多标签个数就为1
yzm_len=1

def im2col(input_data, filter_h, filter_w, stride=1, pad=0):
    """

    Parameters
    ----------
    input_data : 由(数据量, 通道, 高, 长)的4维数组构成的输入数据
    filter_h : 滤波器的高
    filter_w : 滤波器的长
    stride : 步幅
    pad : 填充

    Returns
    -------
    col : 2维数组
    """
    N, C, H, W = input_data.shape
    out_h = (H + 2*pad - filter_h)//stride + 1
    out_w = (W + 2*pad - filter_w)//stride + 1

    img = np.pad(input_data, [(0,0), (0,0), (pad, pad), (pad, pad)], 'constant')
    col = np.zeros((N, C, filter_h, filter_w, out_h, out_w))

    for y in range(filter_h):
        y_max = y + stride*out_h
        for x in range(filter_w):
            x_max = x + stride*out_w
            col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]

    col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N*out_h*out_w, -1)
    return col


def col2im(col, input_shape, filter_h, filter_w, stride=1, pad=0):
    """

    Parameters
    ----------
    col :
    input_shape : 输入数据的形状(例:(10, 1, 28, 28))
    filter_h :
    filter_w
    stride
    pad

    Returns
    -------

    """
    N, C, H, W = input_shape
    out_h = (H + 2*pad - filter_h)//stride + 1
    out_w = (W + 2*pad - filter_w)//stride + 1
    col = col.reshape(N, out_h, out_w, C, filter_h, filter_w).transpose(0, 3, 4, 5, 1, 2)

    img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1))
    for y in range(filter_h):
        y_max = y + stride*out_h
        for x in range(filter_w):
            x_max = x + stride*out_w
            img[:, :, y:y_max:stride, x:x_max:stride] += col[:, :, y, x, :, :]

    return img[:, :, pad:H + pad, pad:W + pad]

class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = (x <= 0)
        out = x.copy()
        out[self.mask] = 0

        return out

    def backward(self, dout):
        dout[self.mask] = 0
        dx = dout

        return dx

class Sigmoid:
    def __init__(self):
        self.out=None
    def forward(self,x):
        out=1/(1+np.exp(-x))
        self.out=out
        return out
    def backward(self,dout):
        dx=dout*self.out*(1.0-self.out)
        return dx

def binary_cross_entropy_error(y,t):
    delta=1e-7
    batch_val=-np.sum(t*np.log(y+delta)+(1-t)*np.log(1-y+delta),axis=1)
    return np.sum(batch_val)/batch_val.size


#二元交叉熵
class Binary_crossentropy:
    def __init__(self):
        self.y=None
        self.t=None
    def forward(self,y,t):
        self.y=y
        self.t=t
        loss=binary_cross_entropy_error(y,t)
        return loss
    def backward(self,dout=1):
        delta=1e-7
        return -(self.t/(self.y+delta)-(1-self.t)/(1-self.y+delta))/self.t.shape[0]

#Affine层的实现
class Affine:
    def __init__(self,W,b):
        self.W=W
        self.b=b
        self.x=None
        self.dW=None
        self.db=None
        self.original_x_shape = None
    def forward(self,x):
        #对于卷积层 需要把数据先展平
        self.original_x_shape = x.shape
        x=x.reshape(x.shape[0],-1)
        self.x=x
        out=np.dot(x,self.W)+self.b
        return out
    def backward(self,dout):
        dx=np.dot(dout,self.W.T)
        self.dW=np.dot(self.x.T,dout)
        self.db=np.sum(dout,axis=0)

        # 还原输入数据的形状(对应张量)
        dx = dx.reshape(*self.original_x_shape)
        return dx

#卷积层的实现
class Convolution:
    def __init__(self,W,b,stride=1,pad=0):
        self.W=W
        self.b=b
        self.stride=stride
        self.pad=pad

        # 中间数据(backward时使用)
        self.x = None   
        self.col = None
        self.col_W = None
        
        # 权重和偏置参数的梯度
        self.dW = None
        self.db = None

    def forward(self,x):
        #滤波器的数目、通道数、高、宽
        FN,C,FH,FW=self.W.shape
        #输入数据的数目、通道数、高、宽
        N,C,H,W=x.shape

        #输出特征图的高、宽
        out_h=int(1+(H+2*self.pad-FH)/self.stride)
        out_w=int(1+(W+2*self.pad-FW)/self.stride)

        #输入数据使用im2col展开
        col=im2col(x,FH,FW,self.stride,self.pad)
        #滤波器的展开
        col_W=self.W.reshape(FN,-1).T
        #计算
        out=np.dot(col,col_W)+self.b
        #变换输出数据的形状
        #(N,h,w,C)->(N,c,h,w)
        out=out.reshape(N,out_h,out_w,-1).transpose(0,3,1,2)

        self.x = x
        self.col = col
        self.col_W = col_W

        return out
    
    def backward(self, dout):
        FN, C, FH, FW = self.W.shape
        dout = dout.transpose(0,2,3,1).reshape(-1, FN)

        self.db = np.sum(dout, axis=0)
        self.dW = np.dot(self.col.T, dout)
        self.dW = self.dW.transpose(1, 0).reshape(FN, C, FH, FW)

        dcol = np.dot(dout, self.col_W.T)
        dx = col2im(dcol, self.x.shape, FH, FW, self.stride, self.pad)

        return dx

#池化层的实现
class Pooling:
    def __init__(self,pool_h,pool_w,stride=1,pad=0):
        self.pool_h=pool_h
        self.pool_w=pool_w
        self.stride=stride
        self.pad=pad

        self.x = None
        self.arg_max = None
    def forward(self,x):
        #输入数据的数目、通道数、高、宽
        N,C,H,W=x.shape
        #输出数据的高、宽
        out_h=int(1+(H-self.pool_h)/self.stride)
        out_w=int(1+(W-self.pool_w)/self.stride)

        #展开
        col=im2col(x,self.pool_h,self.pool_w,self.stride,self.pad)
        col=col.reshape(-1,self.pool_h*self.pool_w)

        #最大值
        arg_max = np.argmax(col, axis=1)
        out=np.max(col,axis=1)

        #转换
        out=out.reshape(N,out_h,out_w,C).transpose(0,3,1,2)

        self.x = x
        self.arg_max = arg_max

        return out
    
    def backward(self, dout):
        dout = dout.transpose(0, 2, 3, 1)
        
        pool_size = self.pool_h * self.pool_w
        dmax = np.zeros((dout.size, pool_size))
        dmax[np.arange(self.arg_max.size), self.arg_max.flatten()] = dout.flatten()
        dmax = dmax.reshape(dout.shape + (pool_size,)) 
        
        dcol = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2], -1)
        dx = col2im(dcol, self.x.shape, self.pool_h, self.pool_w, self.stride, self.pad)
        
        return dx

#SimpleNet
class SimpleConvNet:
    def __init__(self,input_dim=(1,28,28),
                      conv_param={'filter_num':30,'filter_size':5,'pad':0,'stride':1},
                      hidden_size=100,
                      output_size=10,
                      weight_init_std=0.01):
        filter_num=conv_param['filter_num']#30
        filter_size=conv_param['filter_size']#5
        filter_pad=conv_param['pad']#0
        filter_stride=conv_param['stride']#1

        input_size=input_dim[1]#28
        conv_output_size=int((1+input_size+2*filter_pad-filter_size)/filter_stride)#24
        #pool 默认的是2x2最大值池化 池化层的大小变为卷积层的一半30*12*12=4320
        pool_output_size=int(filter_num*(conv_output_size/2)*(conv_output_size/2))

        #权重参数的初始化部分 滤波器和偏置
        self.params={}
        #(30,1,5,5)
        self.params['W1']=np.random.randn(filter_num,input_dim[0],filter_size,filter_size)*weight_init_std
        #(30,)
        self.params['b1']=np.zeros(filter_num)

        #(4320,100)
        self.params['W2']=np.random.randn(pool_output_size,hidden_size)*weight_init_std
        #(100,)
        self.params['b2']=np.zeros(hidden_size)
        #(100,10)
        self.params['W3']=np.random.randn(hidden_size,output_size)*weight_init_std
        #(10,)
        self.params['b3']=np.zeros(output_size)

        #生成必要的层
        self.layers=OrderedDict()
        #(N,1,28,28)->(N,30,24,24)
        self.layers['Conv1']=Convolution(self.params['W1'],self.params['b1'],conv_param['stride'],conv_param['pad'])
        #(N,30,24,24)
        self.layers['Relu1']=Relu()
        #池化层的步幅大小和池化应用区域大小相等
        #(N,30,12,12)
        self.layers['Pool1']=Pooling(pool_h=2,pool_w=2,stride=2)
        #全连接层
        #全连接层内部有个判断 首先是把数据展平
        #(N,30,12,12)->(N,4320)->(N,100)
        self.layers['Affine1']=Affine(self.params['W2'],self.params['b2'])
        #(N,100)
        self.layers['Relu2']=Relu()
        #(N,100)->(N,10)
        self.layers['Affine2']=Affine(self.params['W3'],self.params['b3'])

        #最后两层
        #输出层激活函数用sigmoid
        self.sigmoid=Sigmoid()
        #输出层损失函数用二元交叉熵
        self.binary_cross_loss=Binary_crossentropy()
    
    def predict(self,x):
        for layer in self.layers.values():
            x=layer.forward(x)
        return x
    
    def loss(self,x,t):
        y=self.predict(x)
        sigmoid_out=self.sigmoid.forward(y)
        binary_loss=self.binary_cross_loss.forward(sigmoid_out,t)
        return binary_loss
    
    def gradient(self,x,t):
        #forward
        self.loss(x,t)

        #backward
        dout=1
        dout=self.binary_cross_loss.backward(dout)
        dout=self.sigmoid.backward(dout)
        layers=list(self.layers.values())
        layers.reverse()
        for layer in layers:
            dout=layer.backward(dout)
        
        #梯度
        grads={}
        grads['W1']=self.layers['Conv1'].dW
        grads['b1']=self.layers['Conv1'].db
        grads['W2']=self.layers['Affine1'].dW
        grads['b2']=self.layers['Affine1'].db
        grads['W3']=self.layers['Affine2'].dW
        grads['b3']=self.layers['Affine2'].db

        return grads
    
    #计算准确率
    def accuracy(self,x,t):
        y=self.predict(x)
        y=y.reshape(y.shape[0],yzm_len,-1)
        y=y.argmax(axis=2)

        t=t.reshape(t.shape[0],yzm_len,-1)
        t=t.argmax(axis=2)

        acc_equal=(y==t)
        acc_sum=acc_equal.sum(axis=1)
        acc=(acc_sum==yzm_len).mean()
        return acc
    
    #保存模型参数
    def save_params(self, file_name="params.pkl"):
        params = {}
        for key, val in self.params.items():
            params[key] = val
        with open(file_name, 'wb') as f:
            pickle.dump(params, f)
    #载入模型参数
    def load_params(self, file_name="params.pkl"):
        with open(file_name, 'rb') as f:
            params = pickle.load(f)
        for key, val in params.items():
            self.params[key] = val

        for i, key in enumerate(['Conv1', 'Affine1', 'Affine2']):
            self.layers[key].W = self.params['W' + str(i+1)]
            self.layers[key].b = self.params['b' + str(i+1)]


if __name__=='__main__':
    (x_train,t_train),(x_test,t_test)=load_mnist(flatten=False,one_hot_label=True)
    # 处理花费时间较长的情况下减少数据 
    x_train, t_train = x_train[:5000], t_train[:5000]
    x_test, t_test = x_test[:1000], t_test[:1000]
    net=SimpleConvNet(input_dim=(1,28,28), 
                        conv_param = {'filter_num': 30, 'filter_size': 5, 'pad': 0, 'stride': 1},
                        hidden_size=100, output_size=10, weight_init_std=0.01)

    train_loss_list=[]

    #超参数
    iter_nums=1000
    train_size=x_train.shape[0]
    batch_size=100
    learning_rate=0.1

    #记录准确率
    train_acc_list=[]
    test_acc_list=[]
    #平均每个epoch的重复次数
    iter_per_epoch=max(train_size/batch_size,1)

    for i in range(iter_nums):
        #小批量数据
        batch_mask=np.random.choice(train_size,batch_size)
        x_batch=x_train[batch_mask]
        t_batch=t_train[batch_mask]

        #计算梯度
        #误差反向传播法 计算很快
        grad=net.gradient(x_batch,t_batch)

        #更新参数 权重W和偏重b
        for key in ['W1','b1','W2','b2']:
            net.params[key]-=learning_rate*grad[key]
        
        #记录学习过程
        loss=net.loss(x_batch,t_batch)
        print('训练次数:'+str(i)+'    loss:'+str(loss))
        train_loss_list.append(loss)

        #计算每个epoch的识别精度
        if i%iter_per_epoch==0:
            #测试在所有训练数据和测试数据上的准确率
            train_acc=net.accuracy(x_train,t_train)
            test_acc=net.accuracy(x_test,t_test)
            train_acc_list.append(train_acc)
            test_acc_list.append(test_acc)
            print('train acc:'+str(train_acc)+'   test acc:'+str(test_acc))
    
    # 保存参数
    net.save_params("params.pkl")
    print("模型参数保存成功!")
    
    print(train_acc_list)
    print(test_acc_list)

    # 绘制图形
    markers = {'train': 'o', 'test': 's'}
    x = np.arange(len(train_acc_list))
    plt.plot(x, train_acc_list, label='train acc')
    plt.plot(x, test_acc_list, label='test acc', linestyle='--')
    plt.xlabel("epochs")
    plt.ylabel("accuracy")
    plt.ylim(0, 1.0)
    plt.legend(loc='lower right')
    plt.show()

运行上述代码,可以看到训练过程如下:

训练次数:292    loss:1.2708599887537548
训练次数:293    loss:1.3026314765299025
训练次数:294    loss:1.259228864495369
训练次数:295    loss:1.2914919940155045
训练次数:296    loss:1.0281679314030725
训练次数:297    loss:1.1964747366743176
训练次数:298    loss:1.438395371844772
训练次数:299    loss:1.0445131505050578
训练次数:300    loss:1.3871343577533637
train acc:0.843   test acc:0.805

可以看到随着训练的进行,对手写数字的识别率逐渐增大,由此可证明该算法的正确性。

实现验证码识别

训练样本下载地址

from turtle import shape
from matplotlib.pyplot import axis
import numpy as np
from collections import OrderedDict
import matplotlib.pylab as plt
from dataset.mnist import load_mnist
import pickle
import string
from PIL import Image
import os
import random
import math

'''
准备模型训练需要的验证码数据
'''
#验证码的所有字符组合,本次识别的验证码只包含数字以及小写英文字母
characters=string.digits+string.ascii_uppercase
#验证码字符长度
yzm_len=4

model=None

#训练样本和测试样本的目录
yzm_train_dir='./yzm/train/'
yzm_test_dir='./yzm/test/'
#获取目录中的验证码路径和验证码的名称
def get_allImgInfo(img_dir):
    yzmInfo=list()
    for filename in os.listdir(img_dir):
        yzm_temp=list()

        #验证码的名称
        yzm_name=filename[0:-4]
        #验证码的路径
        yzm_path=img_dir+filename

        yzm_temp.append(yzm_path)
        yzm_temp.append(yzm_name)

        yzmInfo.append(yzm_temp)
    return yzmInfo

#获取所有验证码的路径以及名称
train_info=get_allImgInfo(yzm_train_dir)
test_info=get_allImgInfo(yzm_test_dir)

#获取图像等比例变换的宽度,高度设为32
def get_width(w1,h1,h2):
    w2=(w1*h2)/h1
    return round(w2)
w1,h1=Image.open(train_info[0][0]).size
height=32
width=get_width(w1,h1,height)
print('图像变换后的宽度为:'+str(width)+' 高度为:'+str(height))

#多标签多分类标签的one-hot
def sigmoid_one_hot(name_list):
    results=np.zeros(shape=(len(characters)*yzm_len,),dtype=np.float)
    for i,lable in enumerate(name_list):
        index=lable+len(characters)*i
        results[index]=1.
    return results



#获取验证码的训练样本和标签样本
def get_data(yzmInfo,batch_size=20):
    x=np.zeros(shape=(batch_size,3,height,width),dtype=np.float)
    y=np.zeros(shape=(batch_size,len(characters)*yzm_len),dtype=np.float)

    #随机挑选batch_size个元素
    batch_yzm=random.sample(yzmInfo,batch_size)
    for i,info in enumerate(batch_yzm):
        img=Image.open(info[0])
        img=img.resize((width,height),Image.NEAREST)
        img_array=np.array(img).astype(np.float)#float
        #将通道放到前面
        img_array=img_array.transpose((2,0,1))
        #归一化
        img_array/=255
        #进一步标准化
        img_array=(img_array-0.5)/0.5
        #存储图像像素信息
        x[i]=img_array

        #验证码名称字符
        info_name=info[1][:yzm_len]
        name_list=[characters.find(chr) for chr in info_name]
        name_array=sigmoid_one_hot(name_list)
        y[i]=name_array
    
    return x,y
    
x1,y1=get_data(train_info,batch_size=10)
pass

'''
-------------------------------------------------------------------------------------------
'''

def im2col(input_data, filter_h, filter_w, stride=1, pad=0):
    """

    Parameters
    ----------
    input_data : 由(数据量, 通道, 高, 长)的4维数组构成的输入数据
    filter_h : 滤波器的高
    filter_w : 滤波器的长
    stride : 步幅
    pad : 填充

    Returns
    -------
    col : 2维数组
    """
    N, C, H, W = input_data.shape
    out_h = (H + 2*pad - filter_h)//stride + 1
    out_w = (W + 2*pad - filter_w)//stride + 1

    img = np.pad(input_data, [(0,0), (0,0), (pad, pad), (pad, pad)], 'constant')
    col = np.zeros((N, C, filter_h, filter_w, out_h, out_w))

    for y in range(filter_h):
        y_max = y + stride*out_h
        for x in range(filter_w):
            x_max = x + stride*out_w
            col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]

    col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N*out_h*out_w, -1)
    return col


def col2im(col, input_shape, filter_h, filter_w, stride=1, pad=0):
    """

    Parameters
    ----------
    col :
    input_shape : 输入数据的形状(例:(10, 1, 28, 28))
    filter_h :
    filter_w
    stride
    pad

    Returns
    -------

    """
    N, C, H, W = input_shape
    out_h = (H + 2*pad - filter_h)//stride + 1
    out_w = (W + 2*pad - filter_w)//stride + 1
    col = col.reshape(N, out_h, out_w, C, filter_h, filter_w).transpose(0, 3, 4, 5, 1, 2)

    img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1))
    for y in range(filter_h):
        y_max = y + stride*out_h
        for x in range(filter_w):
            x_max = x + stride*out_w
            img[:, :, y:y_max:stride, x:x_max:stride] += col[:, :, y, x, :, :]

    return img[:, :, pad:H + pad, pad:W + pad]

class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = (x <= 0)
        out = x.copy()
        out[self.mask] = 0

        return out

    def backward(self, dout):
        dout[self.mask] = 0
        dx = dout

        return dx

class Sigmoid:
    def __init__(self):
        self.out=None
    def forward(self,x):
        out=1/(1+np.exp(-x))
        self.out=out
        return out
    def backward(self,dout):
        dx=dout*self.out*(1.0-self.out)
        return dx

def binary_cross_entropy_error(y,t):
    delta=1e-7
    batch_val=-np.sum(t*np.log(y+delta)+(1-t)*np.log(1-y+delta),axis=1)
    return np.sum(batch_val)/batch_val.size


#二元交叉熵
class Binary_crossentropy:
    def __init__(self):
        self.y=None
        self.t=None
    def forward(self,y,t):
        self.y=y
        self.t=t
        loss=binary_cross_entropy_error(y,t)
        return loss
    def backward(self,dout=1):
        delta=1e-7
        return -(self.t/(self.y+delta)-(1-self.t)/(1-self.y+delta))/self.t.shape[0]


#Affine层的实现
class Affine:
    def __init__(self,W,b):
        self.W=W
        self.b=b
        self.x=None
        self.dW=None
        self.db=None
        self.original_x_shape = None
    def forward(self,x):
        #对于卷积层 需要把数据先展平
        self.original_x_shape = x.shape
        x=x.reshape(x.shape[0],-1)
        self.x=x
        out=np.dot(x,self.W)+self.b
        return out
    def backward(self,dout):
        dx=np.dot(dout,self.W.T)
        self.dW=np.dot(self.x.T,dout)
        self.db=np.sum(dout,axis=0)

        # 还原输入数据的形状(对应张量)
        dx = dx.reshape(*self.original_x_shape)
        return dx

#卷积层的实现
class Convolution:
    def __init__(self,W,b,stride=1,pad=0):
        self.W=W
        self.b=b
        self.stride=stride
        self.pad=pad

        # 中间数据(backward时使用)
        self.x = None   
        self.col = None
        self.col_W = None
        
        # 权重和偏置参数的梯度
        self.dW = None
        self.db = None

    def forward(self,x):
        #滤波器的数目、通道数、高、宽
        FN,C,FH,FW=self.W.shape
        #输入数据的数目、通道数、高、宽
        N,C,H,W=x.shape

        #输出特征图的高、宽
        out_h=int(1+(H+2*self.pad-FH)/self.stride)
        out_w=int(1+(W+2*self.pad-FW)/self.stride)

        #输入数据使用im2col展开
        col=im2col(x,FH,FW,self.stride,self.pad)
        #滤波器的展开
        col_W=self.W.reshape(FN,-1).T
        #计算
        out=np.dot(col,col_W)+self.b
        #变换输出数据的形状
        #(N,h,w,C)->(N,c,h,w)
        out=out.reshape(N,out_h,out_w,-1).transpose(0,3,1,2)

        self.x = x
        self.col = col
        self.col_W = col_W

        return out
    
    def backward(self, dout):
        FN, C, FH, FW = self.W.shape
        dout = dout.transpose(0,2,3,1).reshape(-1, FN)

        self.db = np.sum(dout, axis=0)
        self.dW = np.dot(self.col.T, dout)
        self.dW = self.dW.transpose(1, 0).reshape(FN, C, FH, FW)

        dcol = np.dot(dout, self.col_W.T)
        dx = col2im(dcol, self.x.shape, FH, FW, self.stride, self.pad)

        return dx

#池化层的实现
class Pooling:
    def __init__(self,pool_h,pool_w,stride=1,pad=0):
        self.pool_h=pool_h
        self.pool_w=pool_w
        self.stride=stride
        self.pad=pad

        self.x = None
        self.arg_max = None
    def forward(self,x):
        #输入数据的数目、通道数、高、宽
        N,C,H,W=x.shape
        #输出数据的高、宽
        out_h=int(1+(H-self.pool_h)/self.stride)
        out_w=int(1+(W-self.pool_w)/self.stride)

        #展开
        col=im2col(x,self.pool_h,self.pool_w,self.stride,self.pad)
        col=col.reshape(-1,self.pool_h*self.pool_w)

        #最大值
        arg_max = np.argmax(col, axis=1)
        out=np.max(col,axis=1)

        #转换
        out=out.reshape(N,out_h,out_w,C).transpose(0,3,1,2)

        self.x = x
        self.arg_max = arg_max

        return out
    
    def backward(self, dout):
        dout = dout.transpose(0, 2, 3, 1)
        
        pool_size = self.pool_h * self.pool_w
        dmax = np.zeros((dout.size, pool_size))
        dmax[np.arange(self.arg_max.size), self.arg_max.flatten()] = dout.flatten()
        dmax = dmax.reshape(dout.shape + (pool_size,)) 
        
        dcol = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2], -1)
        dx = col2im(dcol, self.x.shape, self.pool_h, self.pool_w, self.stride, self.pad)
        
        return dx

#SimpleNet
class SimpleConvNet:
    def __init__(self,input_dim=(3,32,80),weight_init_std=0.01):

        #权重参数的初始化部分 滤波器和偏置
        self.params={}
        #(32,3,3,3)
        self.params['W1']=np.random.randn(32,3,3,3)*weight_init_std
        #(32,)
        self.params['b1']=np.zeros(32)

        #(64,32,3,3)
        self.params['W2']=np.random.randn(64,32,3,3)*weight_init_std
        #(64,)
        self.params['b2']=np.zeros(64)

        #(128,64,3,3)
        self.params['W3']=np.random.randn(128,64,3,3)*weight_init_std
        #(128,)
        self.params['b3']=np.zeros(128)

        #(128*2*10,512)
        self.params['W4']=np.random.randn(128*2*8,512)*weight_init_std
        #(512,)
        self.params['b4']=np.zeros(512)
        #(512,144)
        self.params['W5']=np.random.randn(512,144)*weight_init_std
        #(144,)
        self.params['b5']=np.zeros(144)




        #生成必要的层
        self.layers=OrderedDict()
        #(N,3,32,80)->(N,32,30,78)
        self.layers['Conv1']=Convolution(self.params['W1'],self.params['b1'],1,0)
        #(N,32,30,78)
        self.layers['Relu1']=Relu()
        #(N,32,15,39)
        self.layers['Pool1']=Pooling(pool_h=2,pool_w=2,stride=2)

        #(N,32,15,39)->(N,64,13,37)
        self.layers['Conv2']=Convolution(self.params['W2'],self.params['b2'],1,0)
        #(N,64,13,37)
        self.layers['Relu2']=Relu()
        #(N,64,6,18)
        self.layers['Pool2']=Pooling(pool_h=2,pool_w=2,stride=2)

        #(N,64,6,18)->(N,128,4,16)
        self.layers['Conv3']=Convolution(self.params['W3'],self.params['b3'],1,0)
        #(N,128,4,16)
        self.layers['Relu3']=Relu()
        #(N,128,2,8)
        self.layers['Pool3']=Pooling(pool_h=2,pool_w=2,stride=2)

        #全连接层
        #全连接层内部有个判断 首先是把数据展平
        #(N,128,2,8)->(N,2048)->(N,512)
        self.layers['Affine1']=Affine(self.params['W4'],self.params['b4'])
        #(N,512)
        self.layers['Relu2']=Relu()
        #(N,512)->(N,144)
        self.layers['Affine2']=Affine(self.params['W5'],self.params['b5'])

        #最后两层
        #输出层激活函数用sigmoid
        self.sigmoid=Sigmoid()
        #输出层损失函数用二元交叉熵
        self.binary_cross_loss=Binary_crossentropy()


    
    def predict(self,x):
        for layer in self.layers.values():
            x=layer.forward(x)
        return x
    
    def loss(self,x,t):
        y=self.predict(x)
        sigmoid_out=self.sigmoid.forward(y)
        binary_loss=self.binary_cross_loss.forward(sigmoid_out,t)
        return binary_loss
    
    def gradient(self,x,t):
        #forward
        self.loss(x,t)

        #backward
        dout=1
        dout=self.binary_cross_loss.backward(dout)
        dout=self.sigmoid.backward(dout)
        layers=list(self.layers.values())
        layers.reverse()
        for layer in layers:
            dout=layer.backward(dout)
        
        #梯度
        grads={}
        grads['W1']=self.layers['Conv1'].dW
        grads['b1']=self.layers['Conv1'].db
        grads['W2']=self.layers['Conv2'].dW
        grads['b2']=self.layers['Conv2'].db
        grads['W3']=self.layers['Conv3'].dW
        grads['b3']=self.layers['Conv3'].db

        grads['W4']=self.layers['Affine1'].dW
        grads['b4']=self.layers['Affine1'].db
        grads['W5']=self.layers['Affine2'].dW
        grads['b5']=self.layers['Affine2'].db

        return grads
    
    #计算准确率
    def accuracy(self,x,t):
        y=self.predict(x)
        y=y.reshape(y.shape[0],yzm_len,-1)
        y=y.argmax(axis=2)

        t=t.reshape(t.shape[0],yzm_len,-1)
        t=t.argmax(axis=2)

        acc_equal=(y==t)
        acc_sum=acc_equal.sum(axis=1)
        acc=(acc_sum==yzm_len).mean()
        return acc
    
    #保存模型参数
    def save_params(self, file_name="params.pkl"):
        params = {}
        for key, val in self.params.items():
            params[key] = val
        with open(file_name, 'wb') as f:
            pickle.dump(params, f)
    #载入模型参数
    def load_params(self, file_name="params.pkl"):
        with open(file_name, 'rb') as f:
            params = pickle.load(f)
        for key, val in params.items():
            self.params[key] = val

        for i, key in enumerate(['Conv1','Conv2','Conv3','Affine1', 'Affine2']):
            self.layers[key].W = self.params['W' + str(i+1)]
            self.layers[key].b = self.params['b' + str(i+1)]


if __name__=='__main__':
    # net=SimpleConvNet(input_dim=(3,32,80),weight_init_std=0.01)
    # test_data=np.zeros(shape=(10,3,32,80),dtype=np.float)
    # t=np.zeros(shape=(10,144),dtype=np.float)
    # net.gradient(x=test_data,t=t)
    x_train, t_train = get_data(train_info,2000)
    x_test, t_test = get_data(test_info,200)
    net=SimpleConvNet(input_dim=(3,32,80),weight_init_std=0.01)

    train_loss_list=[]

    #超参数
    iter_nums=1000
    train_size=x_train.shape[0]
    batch_size=500
    learning_rate=0.01

    #记录准确率
    train_acc_list=[]
    test_acc_list=[]
    #平均每个epoch的重复次数
    iter_per_epoch=max(train_size/batch_size,1)

    for i in range(iter_nums):
        #小批量数据
        batch_mask=np.random.choice(train_size,batch_size)
        x_batch=x_train[batch_mask]
        t_batch=t_train[batch_mask]

        #计算梯度
        #误差反向传播法 计算很快
        grad=net.gradient(x_batch,t_batch)

        #更新参数 权重W和偏重b
        for key in ['W1','b1','W2','b2','W3','b3','W4','b4','W5','b5']:
            net.params[key]-=learning_rate*grad[key]
        
        
        #记录学习过程
        loss=net.loss(x_batch,t_batch)
        print('训练次数:'+str(i)+'    loss:'+str(loss))
        train_loss_list.append(loss)

        #计算每个epoch的识别精度
        if i%iter_per_epoch==0:
            #测试在所有训练数据和测试数据上的准确率
            train_acc=net.accuracy(x_train,t_train)
            test_acc=net.accuracy(x_test,t_test)
            train_acc_list.append(train_acc)
            test_acc_list.append(test_acc)
            print('train acc:'+str(train_acc)+'   test acc:'+str(test_acc))
    
    # 保存参数
    net.save_params("params.pkl")
    print("模型参数保存成功!")
    
    print(train_acc_list)
    print(test_acc_list)

    # 绘制图形
    markers = {'train': 'o', 'test': 's'}
    x = np.arange(len(train_acc_list))
    plt.plot(x, train_acc_list, label='train acc')
    plt.plot(x, test_acc_list, label='test acc', linestyle='--')
    plt.xlabel("epochs")
    plt.ylabel("accuracy")
    plt.ylim(0, 1.0)
    plt.legend(loc='lower right')
    plt.show()

随着训练的进行,可以看到损失函数在逐渐降低,因此学习在正常的进行中。

resnet50 多标签分类_深度学习_03