笔试卷积实现python

#!/bin/python
# -*- coding: utf8 -*-

'''
输入
5 5, 1 1 1 1 1 1 2 2 2 1 1 2 4 2 1 1 2 2 2 1 1 1 1 1 1
5 5, 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2
输出
3 3 , 15 23 15 23 36 23 15 23 15
'''


class Solution:
    def conv2d_x(self, kernel, image, x, y):
        t = 0
        h, w = len(kernel), len(kernel[0])
        for i in range(h):
            for j in range(w):
                temp = kernel[i][j] * image[x + i][y + j]
                t += temp
        return int(min(255, t))

    def conv2d(self, kernel, image, stride):
        khkw = kernel.split(',')[0]
        khkw = list(map(int, khkw.split()))
        kk_list = kernel.split(',')[1]
        kk_list = list(map(int, kk_list.split()))
        kk2D_list = []
        for i in range(khkw[0]):
            temp = kk_list[khkw[1] * i:khkw[1] * (i + 1)]
            kk2D_list.append(temp)

        ihiw = image.split(',')[0]
        ihiw = list(map(int, ihiw.split()))
        image_list = image.split(',')[1]
        image_list = list(map(int, image_list.split()))

        image2D_list = []
        for i in range(ihiw[0]):
            temp = image_list[ihiw[1] * i:ihiw[1] * (i + 1)]
            image2D_list.append(temp)
        new_h = ihiw[0] + khkw[0] // 2 * 2
        new_w = ihiw[1] + khkw[1] // 2 * 2

        image2D_big_list = [[0] * new_w for _ in range(new_h)]
        for i in range(khkw[0] // 2, ihiw[0] + khkw[0] // 2):
            temp = image2D_list[i - khkw[0] // 2]
            image2D_big_list[i][khkw[1] // 2:ihiw[1] + khkw[1] // 2] = temp

        resw = int((new_w - khkw[1]) // stride + 1)
        resh = int((new_h - khkw[0]) // stride + 1)

        res = [[0] * resw for _ in range(resh)]
        for i in range(resh):
            for j in range(resw):
                temp = self.conv2d_x(kk2D_list, image2D_big_list, i * stride, j * stride)
                res[i][j] = temp
        result = []
        result.append(str(len(res)))
        result.append(str(len(res[0])))
        result.append(',')
        for i in range(resh):
            for j in range(resw):
                result.append(str(res[i][j]))
        result = ' '.join(result)
        return result


s = Solution()
_kernel = '5 5, 1 1 1 1 1 1 2 2 2 1 1 2 4 2 1 1 2 2 2 1 1 1 1 1 1'
_image = '5 5, 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1'

# _kernel = '3 3, 1 2 1 1 2 1 1 2 1'
# _image = '5 5, 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1'
_stride = 2
res = s.conv2d(kernel=_kernel, image=_image, stride=_stride)

print(res + "\n")
'''
[[4. 6. 6. 6. 4.]
 [6. 9. 9. 9. 6.]
 [6. 9. 9. 9. 6.]
 [6. 9. 9. 9. 6.]
 [4. 6. 6. 6. 4.]]
 
 [[ 6.  8.  8.  8.  6.]
 [ 9. 12. 12. 12.  9.]
 [ 9. 12. 12. 12.  9.]
 [ 9. 12. 12. 12.  9.]
 [ 6.  8.  8.  8.  6.]]
 '''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值