PyTorch 普通卷积和空洞卷积

import numpy as np
from torchvision.transforms import Compose, ToTensor
from torch import nn
import torch.nn.init as init
def transform():
    return Compose([
        ToTensor(),
        # Normalize((12,12,12),std = (1,1,1)),
    ])

arr = range(1,26)
arr = np.reshape(arr,[5,5])
arr = np.expand_dims(arr,2)
arr = arr.astype(np.float32)
# arr = arr.repeat(3,2)
print(arr.shape)
arr = transform()(arr)
arr = arr.unsqueeze(0)
print(arr)

conv1 = nn.Conv2d(1, 1, 3, stride=1, bias=False, dilation=1)  # 普通卷积
conv2 = nn.Conv2d(1, 1, 3, stride=1, bias=False, dilation=2)  # dilation就是空洞率,即间隔
init.constant_(conv1.weight, 1)
init.constant_(conv2.weight, 1)
out1 = conv1(arr)
out2 = conv2(arr)
print('standare conv:\n', out1.detach().numpy())
print('dilated conv:\n', out2.detach().numpy())

输出结果:
(5, 5, 1)
tensor([[[[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.],
[11., 12., 13., 14., 15.],
[16., 17., 18., 19., 20.],
[21., 22., 23., 24., 25.]]]])
standare conv:
[[[[ 63. 72. 81.]
[108. 117. 126.]
[153. 162. 171.]]]]
dilated conv:
[[[[117.]]]]

Process finished with exit code 0
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值