Python-DNN-3D-Convolution(3D卷积)

import numpy as np
import math
def convlution(image,kernel,step,padding):
    '''
    W`=((W-kernel+2*padding)/step)+1
    H`=((H-kernel+2*padding)/step)+1
    输入格式:
    image=(C,W,H)
    Kernel_size=(kernel_N,C,kernel_W,kernel_H)
    返回格式:
    result=(Kernel_N,W`,H`)
    '''
    image_shape=image.shape
    C=image_shape[0]
    W=image_shape[1]
    H=image_shape[2]
    Kernel_size=kernel.shape
    Horizon_end=((H-Kernel_size[3]+2*padding)//step)+1
    Vertical_end=((W-Kernel_size[2]+2*padding)//step)+1
    result=[Kernel_size[0],Vertical_end,Horizon_end]
    result=np.zeros((result[0],result[1],result[2]))
    temp=0
    print("result.shape=",result.shape)
    for k in range (Kernel_size[0]):
        for v_sum in range (Vertical_end):
            for h_sum in range (Horizon_end):
                for c in range (C):
#                     temp+=np.sum((image[c][v_sum:v_sum+2,h_sum:h_sum+2])*(kernel[k][c]))
                    temp+=np.sum((image[c][v_sum:v_sum+Kernel_size[2],h_sum:h_sum+Kernel_size[3]])*(kernel[k][c]))
                print("temp=",temp)
                result[k][v_sum][h_sum]=temp
                temp=0
    return result

                    
                    
            
    
    
image=np.array([[[1,2,3,4],[1,2,3,4],[1,2,3,4],[0,1,2,3]]]*3)
kernel=np.array([[[[1,1],[2,2]],[[1,1],[2,2]],[[1,1],[2,2]]]]*2)
print(image.shape,kernel.shape)
(3, 4, 4) (2, 3, 2, 2)
print(convlution(image,kernel,step=1,padding=0))
result.shape= (2, 3, 3)
temp= 27
temp= 45
temp= 63
temp= 27
temp= 45
temp= 63
temp= 15
temp= 33
temp= 51
temp= 27
temp= 45
temp= 63
temp= 27
temp= 45
temp= 63
temp= 15
temp= 33
temp= 51
[[[ 27.  45.  63.]
  [ 27.  45.  63.]
  [ 15.  33.  51.]]

 [[ 27.  45.  63.]
  [ 27.  45.  63.]
  [ 15.  33.  51.]]]
如何快速的定义一个上面的image?
a=np.array([[1,2,3,4]]*4)
print(a)
print(a[0:2,0:2])
[[1 2 3 4]
 [1 2 3 4]
 [1 2 3 4]
 [1 2 3 4]]
[[1 2]
 [1 2]]
b=np.array([[2,2],[1,1]])
print(b)
[[2 2]
 [1 1]]
print(np.sum(a*b))
8
如何快速的创建一个shape的数组?

d=np.zeros( (1,2,3) )#表示1维2行3列

from PIL import Image
import matplotlib.pyplot as plt
import cv2

垂直边缘检测

k1 = np.array([
[1,0,-1],
[1,0,-1],
[1,0,-1]
])

k1 = np.array([[[
    [1,0,-1],
    [1,0,-1],
    [1,0,-1]
]]*3]*3)
结论:

上面的函数可以实现Kernel(N,C,W,H)的格式,但是适用不了对于(N,W,H,C)的格式,以及图片格式由(C,W,H)改成了(W,H,C),因为没有时间对这块进行修改了,
如果是2D卷积,那么完全可以实现出来。


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值