Po-Hsun-Su / pytorch-ssim

pytorch structural similarity (SSIM) loss
Other
1.89k stars 367 forks source link

issue with basic usage example #36

Open Alok589 opened 3 years ago

Alok589 commented 3 years ago

Hi,

I am not able to run the example script. Could you please let me know what is missing?

basic usage script :

import pytorch_ssim import torch from torch.autograd import Variable

img1 = Variable(torch.rand(1, 1, 256, 256)) img2 = Variable(torch.rand(1, 1, 256, 256))

if torch.cuda.is_available(): img1 = img1.cuda() img2 = img2.cuda()

print(pytorch_ssim.ssim(img1, img2))

ssim_loss = pytorch_ssim.SSIM(window_size=4)

print(ssim_loss(img1, img2))

here is the traceback of the code:

Traceback (most recent call last): File "/home/thesis_2/ssim_test.py", line 12, in print(pytorch_ssim.ssim(img1, img2)) File "/opt/conda/envs/venv/lib/python3.6/site-packages/pytorch_ssim/init.py", line 62, in ssim return _ssim(img1, img2, window, window_size, channel, size_average) File "/opt/conda/envs/venv/lib/python3.6/site-packages/pytorch_ssim/init.py", line 18, in _ssim mu1 = F.conv2d(img1, window, padding = window_size/2, groups = channel) TypeError: conv2d(): argument 'padding' must be tuple of ints, not float

neeek2303 commented 3 years ago

@Alok589 I had the same problem because I used pip to install this repo as a package. I just have to clone this repo, then copy pytorch_ssim folder from this repo (repo and folder have the same name - maybe it is a reason why we misunderstood) to your directory as follow: YOU_FOLDER ........pytorch_ssim ................init.py ........file.py (the file where you are supposed to use ssim )

zakajd commented 3 years ago

For anyone having same issue in the future. Replace window_size/2 with window_size // 2 to get correct dtype after division.

ucalyptus2 commented 3 years ago

@Alok589 to make it work on COLAB:


!git clone https://github.com/Po-Hsun-Su/pytorch-ssim.git

import sys;sys.path.append("/content/pytorch-ssim")
import pytorch_ssim
import torch
from torch.autograd import Variable

img1 = Variable(torch.rand(1, 1, 256, 256))
img2 = Variable(torch.rand(1, 1, 256, 256))

if torch.cuda.is_available():
    img1 = img1.cuda()
    img2 = img2.cuda()

print(pytorch_ssim.ssim(img1, img2))

ssim_loss = pytorch_ssim.SSIM(window_size = 11)

print(ssim_loss(img1, img2))