python卷积操作

import numpy as np
import math

class Conv2D():
    def __init__(self, inputShape, outputChannel, kernelSize, stride=1, method=""):
        self.height = inputShape[2]
        self.width = inputShape[3]
        self.inputChannel = inputShape[1]
        self.outputChannel = outputChannel
        self.batchSize = inputShape[0]
        self.stride = stride
        self.kernelSize = kernelSize
        self.method = method
        # initial the parameters of the kernel, do not initial them as zero
        self.weights = np.random.random([kernelSize, kernelSize, self.inputChannel,self.outputChannel])
        self.bias = np.random.random(self.outputChannel)
        if method == "SAME":
            self.output = np.zeros(
                (self.batchSize,
                 self.outputChannel, math.floor(self.height / self.stride), math.floor(self.width / self.stride)))
        if method == "VALID":
            self.output = np.zeros([self.batchSize,
                                    self.outputChannel, np.floor((self.height - kernelSize + 1) / self.stride),
                                    math.floor((self.width - kernelSize + 1) / self.stride)])
    def forward(self, x):
        weights = self.weights.reshape([-1,self.outputChannel])  # shape: [(h*w),#]
        # Filling operation
        # Note that: x is 4-dimensional.
        convOut = np.zeros(self.output.shape)
        for i in range(self.batchSize):
            img_i = x[i]
            print("img_i:",img_i.shape)
            # img_i = x[i][np.newaxis, :, :, :]
            colImage_i = self.im2col(img_i, self.kernelSize, self.stride)
            print("colImage_i:",colImage_i.shape)
            convOut[i] = np.reshape(np.dot(colImage_i, weights) + self.bias, self.output[0].shape)
        return convOut
    # im2col function
    def im2col(self, image, kernelSize, stride):
        imageCol = []
        for i in range(0, image.shape[1] - kernelSize + 1, stride):
            for j in range(0, image.shape[2] - kernelSize + 1, stride):
                col = image[:,i:i + kernelSize, j:j + kernelSize].reshape([-1])
                imageCol.append(col)
        imageCol = np.array(imageCol)  # shape: [(h*w),(c*h*w)] kernel's height, width and channels
        return imageCol
# Test part
inputData = np.random.random((4,3, 5, 5))
kernel = list([3, 3, 32])
conv2d = Conv2D(inputShape=inputData.shape, outputChannel=kernel[2], kernelSize=kernel[0], stride=1, method='SAME')
outputData = conv2d.forward(inputData)
print("outputShape: ", outputData.shape)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值