qiyexuxu

1 . 图像的卷积操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import torch as t
from torch import nn
from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage

to_tensor = ToTensor()
to_pil = ToPILImage()
lena = Image.open('path to your image')


input = to_tensor(lena).unsqueeze(0)

# 锐化卷积核
kernel = t.ones(3, 3)/-9
kernel[1][1] = 1
conv = nn.Conv2d(1, 1, (3, 3), 1, bias=False)
conv.weight.data = kernel.view(1, 1, 3, 3)

out = conv(input)
to_pil(out.data.squeeze(0))