From c8d109420c38fc47238a984c82eab9da3940f093 Mon Sep 17 00:00:00 2001 From: dpa Date: Thu, 19 Nov 2026 16:58:07 +0200 Subject: initial commit --- conv-pytorch.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 conv-pytorch.py (limited to 'conv-pytorch.py') 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() -- cgit v1.2.3