diff options
| author | dpa <dobaipatakyattila@gmail.com> | 2026-11-19 16:58:07 +0200 |
|---|---|---|
| committer | dpa <dobaipatakyattila@gmail.com> | 2026-11-19 16:58:07 +0200 |
| commit | c8d109420c38fc47238a984c82eab9da3940f093 (patch) | |
| tree | 80484cef17e433ab31dc567e10cc0c38a0c6dd68 /conv-pytorch.py | |
initial commit
Diffstat (limited to 'conv-pytorch.py')
| -rw-r--r-- | conv-pytorch.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/conv-pytorch.py b/conv-pytorch.py new file mode 100644 index 0000000..b777c53 --- /dev/null +++ b/conv-pytorch.py @@ -0,0 +1,18 @@ +import torch +import torch.nn.functional as F +import matplotlib.pyplot as plt +from PIL import Image + +img_pil = Image.open("./cameraman.png").convert("L") +img_tensor = torch.tensor(list(img_pil.getdata()), dtype=torch.float32).reshape(1, 1, img_pil.height, img_pil.width) / 255.0 + +kernel = torch.tensor([[1., 0., -1.], + [2., 0., -2.], + [1., 0., -1.]]) + +weight = kernel.view(1, 1, 3, 3) + +conv_result = F.conv2d(img_tensor, weight, padding=1) + +plt.imshow(conv_result.squeeze().numpy(), cmap='gray') +plt.show() |
