我是靠谱客的博主 壮观钢铁侠,最近开发中收集的这篇文章主要介绍numpy实现卷积网络,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import numpy as np
class conv():
    def __init__(self,in_channles,out_channles,kernel=3,stride=1,padding=0):
        self.in_channles = in_channles
        self.out_channles =out_channles
        self.kernel = kernel
        self.stride = stride
        self.padding = padding
        self.filter = np.random.random((3,3,3))
        self.bias = np.zeros(out_channles)
    def numpy_conv(self,inputs,filter,out_shape):
        filter_size = filter.shape[0]
        result = np.zeros(out_shape)
        H, W = out_shape
        for r in range(0, H):
            for c in range(0, W):
                # 池化大小的输入区域
                cur_input = inputs[r*self.stride:r*self.stride + filter_size,
                            c*self.stride:c*self.stride + filter_size]
                cur_output = cur_input * filter
                conv_sum = np.sum(cur_output)
                result[r, c] = conv_sum
        return result

    def forward(self, inputs):
        N, C, H, W = inputs.shape
        assert C==self.in_channles,"wwwwww %s %s"%(C,self.in_channles)
        out_H = int((H - self.kernel + 2 * self.padding) / self.stride) + 1
        out_W = int((W - self.kernel + 2 * self.padding) / self.stride) + 1
        # print(out_H,out_W)
        result = np.zeros([N,self.out_channles, out_H, out_W], np.float32)
        inputs = np.pad(inputs, ((0, 0),(0, 0), (self.padding, self.padding), (self.padding, self.padding)), 'constant',
                        constant_values=(0, 0))

        for n in range(N):
            for i in range(self.out_channles):
                result[n, i, :, :] = self.bias[i]
                for j in range(self.in_channles):
                    channel_data = inputs[n,j]
                    # 采用上面对逻辑,单核单通道卷积,然后累计
                    result[n, i, :, :] += self.numpy_conv(channel_data, self.filter[i][j] ,(out_H,out_W))

        return result
class max_pool():
    def __init__(self,pooling=(2,2), strides=(2, 2), padding=(0, 0)):
        self.pooling = pooling
        self.strides = strides
        self.padding = padding
    def forward(self,z):

        z = np.pad(z, ((0, 0),(0, 0), (self.padding[0], self.padding[0]), (self.padding[1], self.padding[1])), 'constant',
                        constant_values=(0, 0))
        N, C, H, W = z.shape
        out_H = int(H/2)
        out_W = int(W / 2)
        pool_z = np.zeros((N, C, out_H, out_W))
        for i in range(N):
            for j in range(C):
                for oi in range(out_H):
                    for oj in range(out_W):
                        pool_z[i,j,oi,oj] = np.max(z[i,j,
                                                        oi*self.strides[0]:  oi*self.strides[0]+ self.pooling[0],
                                                        oj*self.strides[1]:  oj*self.strides[1]+ self.pooling[1]
                                                ])
        return pool_z
def relu(x):
    return np.maximum(x, 0)
class BN():
    def __init__(self,channel,weight=[],bias=[],eps=1e-05):
        self.channel = channel
        self.weight = weight
        self.bias = bias
        self.eps =eps
        self.running_mean = []
        self.running_var = []
    def forward(self,x):



        data = x.transpose((1, 0, 2, 3)).reshape(self.channel, -1)
        if len(self.running_mean)==0 and len(self.running_var)==0:
            mu1 = data.mean(axis=1).reshape(1, self.channel, 1, 1)
            std1 = data.std(axis=1).reshape(1, self.channel, 1, 1)
        else:
            mu1  = self.running_mean.reshape(1, self.channel, 1, 1)
            std1 = np.sqrt(self.running_var).reshape(1, self.channel, 1, 1)

        if not len(self.weight) and not len(self.bias):
            numpy_bn = (x - mu1) / (std1 + self.eps)
        else:
            self.weight = self.weight.reshape(1, self.channel, 1, 1)
            self.bias= self.bias.reshape(1, self.channel, 1, 1)
            numpy_bn = (x - mu1) / (std1 + self.eps)*self.weight + self.bias
        return numpy_bn
class Liner():
    def __init__(self,in_features,out_features,bias=0):
        self.in_features =in_features
        self.out_features = out_features
        self.weight = np.zeros((in_features,out_features))
        self.bias = np.zeros((in_features,out_features))
    def forward(self,x):
        return np.dot(x,self.weight.T) + self.bias
def max_pooling_forward(z, pooling, strides=(2, 2), padding=(0, 0)):
    """
    最大池化前向过程
    :param z: 卷积层矩阵,形状(N,C,H,W),N为batch_size,C为通道数
    :param pooling: 池化大小(k1,k2)
    :param strides: 步长
    :param padding: 0填充
    :return:
    """
    N, C, H, W = z.shape
    # 零填充
    padding_z = np.lib.pad(z, ((0, 0), (0, 0), (padding[0], padding[0]), (padding[1], padding[1])), 'constant', constant_values=0)

    # 输出的高度和宽度
    out_h = (H + 2 * padding[0] - pooling[0]) // strides[0] + 1
    out_w = (W + 2 * padding[1] - pooling[1]) // strides[1] + 1

    pool_z = np.zeros((N, C, out_h, out_w))

    for n in np.arange(N):
        for c in np.arange(C):
            for i in np.arange(out_h):
                for j in np.arange(out_w):
                    pool_z[n, c, i, j] = np.max(padding_z[n, c,
                                                          strides[0] * i:strides[0] * i + pooling[0],
                                                          strides[1] * j:strides[1] * j + pooling[1]])
    return pool_z
# backbone.0.weight
# backbone.0.bias
# backbone.1.weight
# backbone.1.bias
# backbone.1.running_mean
# backbone.1.running_var
# backbone.1.num_batches_tracked
# backbone.4.weight
# backbone.4.bias
# backbone.5.weight
# backbone.5.bias
# backbone.5.running_mean
# backbone.5.running_var
# backbone.5.num_batches_tracked
# backbone.8.weight
# backbone.8.bias
# backbone.9.weight
# backbone.9.bias
# backbone.9.running_mean
# backbone.9.running_var
# backbone.9.num_batches_tracked
# classifier.0.weight
# classifier.0.bias
class my_net():
    def __init__(self,weight):
        self.weight = weight
        self.relu = relu
        self.net = self.createNet()
    def createNet(self):
        self.conv1 = conv(3, 16, 3, 2, 1)
        self.bn1 = BN(16)
        self.pool1 = max_pool()
        self.conv2 = conv(16, 32, 3, 1)
        self.bn2 = BN(32)
        self.pool2 = max_pool()
        self.conv3 = conv(32, 32, 3, 2, 1)
        self.bn3 = BN(32)
        self.fc = Liner(32*4*4,2)

        self.inint_weight()
    def inint_weight(self):
        self.conv1.filter = self.weight['backbone.0.weight'].cpu().numpy()
        self.conv1.bias = self.weight['backbone.0.bias'].cpu().numpy()
        self.bn1.weight = self.weight['backbone.1.weight'].cpu().numpy()
        self.bn1.bias = self.weight['backbone.1.bias'].cpu().numpy()
        self.bn1.running_mean= self.weight['backbone.1.running_mean'].cpu().numpy()
        self.bn1.running_var= self.weight['backbone.1.running_var'].cpu().numpy()
        self.conv2.filter = self.weight['backbone.4.weight'].cpu().numpy()
        self.conv2.bias = self.weight['backbone.4.bias'].cpu().numpy()
        self.bn2.weight = self.weight['backbone.5.weight'].cpu().numpy()
        self.bn2.bias = self.weight['backbone.5.bias'].cpu().numpy()
        self.bn2.running_mean = self.weight['backbone.5.running_mean'].cpu().numpy()
        self.bn2.running_var = self.weight['backbone.5.running_var'].cpu().numpy()
        self.conv3.filter = self.weight['backbone.8.weight'].cpu().numpy()
        self.conv3.bias = self.weight['backbone.8.bias'].cpu().numpy()
        self.bn3.weight = self.weight['backbone.9.weight'].cpu().numpy()
        self.bn3.bias = self.weight['backbone.9.bias'].cpu().numpy()
        self.bn3.running_mean = self.weight['backbone.9.running_mean'].cpu().numpy()
        self.bn3.running_var = self.weight['backbone.9.running_var'].cpu().numpy()
        self.fc.weight = self.weight['classifier.0.weight'].cpu().numpy()
        self.fc.bias = self.weight['classifier.0.bias'].cpu().numpy()

    def forward(self,x):
        x = self.conv1.forward(x)
        x = self.bn1.forward(x)
        x = self.relu(x)
        x = self.pool1.forward(x)
        x = self.conv2.forward(x)
        x = self.bn2.forward(x)
        x = self.relu(x)
        x = self.pool2.forward(x)
        x = self.conv3.forward(x)
        x = self.bn3.forward(x)
        x = self.relu(x).reshape(1,-1)
        x = self.fc.forward(x)
        return x






if __name__=="__main__":


    import torch
    from PIL import Image
    from torchvision import transforms
    path = "4.jpeg"
    img = Image.open(path).convert('RGB')
    transform = transforms.Compose([
        transforms.Resize((64, 64)),
        transforms.ToTensor()
        ])
    img = transform(img)
    img = img.unsqueeze(0).numpy()
    print(img.shape)
    a = torch.load('112.pth')
    net = my_net(a)
    import time
    t =time.time()
    print(net.forward(img))
    print(time.time()-t)





使用文件连接:

https://download.csdn.net/download/weixin_38730719/20719481

最后

以上就是壮观钢铁侠为你收集整理的numpy实现卷积网络的全部内容,希望文章能够帮你解决numpy实现卷积网络所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部