4

I am using google colab. I installed scikit-image. When I execute this code, I am getting error:

ModuleNotFoundError: No module named 'skimage.measure.simple_metrics'
import math 
import torch
import torch.nn as nn
import numpy as np
import cv2
from skimage.measure.simple_metrics import compare_psnr

def weights_init_kaiming(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        nn.init.kaiming_normal(m.weight.data, a=0, mode='fan_in')\
    elif classname.find('Linear') != -1:
        nn.init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
    elif classname.find('BatchNorm') != -1:
        # nn.init.uniform(m.weight.data, 1.0, 0.02)
        m.weight.data.normal_(mean=0, std=math.sqrt(2./9./64.)).clamp_(-0.025,0.025)
        nn.init.constant(m.bias.data, 0.0)

def batch_PSNR(img, imclean, data_range):
    Img = img.data.cpu().numpy().astype(np.float32)
    Iclean = imclean.data.cpu().numpy().astype(np.float32)
    PSNR = 0
    for i in range(Img.shape[0]):
        PSNR += compare_psnr(Iclean[i,:,:,:], Img[i,:,:,:], data_range=data_range)
    return (PSNR/Img.shape[0])
Aditya Dixit
  • 43
  • 1
  • 6
  • This [thread](https://stackoverflow.com/questions/38087558/import-error-no-module-named-skimage) might be of help? Have you installed all dependencies? – nathan liang Feb 19 '22 at 05:27

2 Answers2

2

Change

from skimage.measure.simple_metrics import compare_psnr

to

from skimage.metrics import peak_signal_noise_ratio

And change

PSNR += compare_psnr(Iclean[i,:,:,:], Img[i,:,:,:], data_range=data_range)

to

PSNR += peak_signal_noise_ratio(Iclean[i,:,:,:], Img[i,:,:,:], data_range=data_range)
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • Please explain why this solution works. There is another, accepted solution with explanation but no code. This solution could be better than the other with explanation. – Michael Ruth Dec 29 '22 at 07:57
  • This solution works because scikit-image changed it's API.(Structuing and naming convention) – Ayushman Kumar May 20 '23 at 04:47
0

Since scikit-image version 0.16, that function was renamed from skimage.measure.compare_psnr to skimage.metrics.peak_signal_noise_ratio.

Please check your scikit-image version to confirm it.

https://scikit-image.org/docs/0.19.x/api/skimage.metrics.html?highlight=peak_signal_noise_ratio#peak-signal-noise-ratio

taipei
  • 1,010
  • 10
  • 20