KUCognitiveInformaticsLab / zhu_project_real_and_fake

The issues when we use the PsychoPy software
0 stars 0 forks source link

PyCharm--calcualte the mean model distance #6

Open conchincradle opened 1 year ago

conchincradle commented 1 year ago

For DISTS and LPIPSvgg using CUDA

from IQA_pytorch import SSIM, GMSD, NLPD,LPIPSvgg, DISTS,MS_SSIM,VSI
import torchvision.transforms as transforms
import cv2
import numpy as np
import torch
import os
def mse(img1,img2):
  t1 =transforms.ToTensor()(img1).unsqueeze(0).cuda()
  t2 =transforms.ToTensor()(img2).unsqueeze(0).cuda()
  score = D(t1,t2,as_loss=False)
  return score.item()

if __name__ == '__main__':

    img1 = cv2.imread("jnd/animal.png", 0)
    img2 = cv2.imread("jnd/uanimal.png", 0)

    tensor1 = transforms.ToTensor()(img1).unsqueeze(0).cuda()
    tensor2 = transforms.ToTensor()(img2).unsqueeze(0).cuda()
    X = tensor1
    # X = X.repeat(1, 3, 1, 1)
    Y = tensor2
    # Y = Y.repeat(1, 3, 1, 1)

    print(X.shape)
    # D = SSIM(channels=1)
    D = DISTS(channels=3).cuda()
    # Calculate score of the image X with the reference Y
    # X: (N,3,H,W)
    # Y: (N,3,H,W)
    # Tensor, data range: 0~1

    score = D(X, Y, as_loss=False)
    # set 'as_loss=True' to get a value as loss for optimizations.
    print(score.item())
    print(mse(img1,img2))
    print("--------------------------------------------")
    dists1 = []
    dists2 = []
    dists3 = []
    paths = ["animal", "flower", "foliage", "fruit", "landscape", "manmade", "shadow", "texture", "winter"]
    for path in paths:
        pre = "jnd/swirl/"
        f1 = "jnd/"+ path + ".png"
        f2 = pre + "14jnd_" + path + ".png"
        # u_filename1 = pre+"u" + path + ".png"
        # u_filename2 = pre+"14jnd_u" + path +".png"
        img1 = cv2.imread(f1, 0)
        img2 = cv2.imread(f2, 0)
        dists1.append(mse(img1, img2))

    for path in paths:
        pre = "jnd/blur/"
        f1 = "jnd/"+ path + ".png"
        f2 = pre + "14jnd_" + path + ".png"
        # u_filename1 = pre+"u" + path + ".png"
        # u_filename2 = pre+"14jnd_u" + path +".png"
        img1 = cv2.imread(f1, 0)
        img2 = cv2.imread(f2, 0)
        dists2.append(mse(img1, img2))

    for path in paths:
        pre = "jnd/noise/"
        f1 = "jnd/"+path + ".png"
        nums = []
        img1 = cv2.imread(f1, 0)
        for i in range(10):
            f2 = pre + "14jnd_" + path + "_" + str(i) + ".png"
            # u_filename1 = pre+"u" + path + ".png"
            # u_filename2 = pre+"14jnd_u" + path +".png"

            img2 = cv2.imread(f2, 0)
            nums.append(mse(img1, img2))
        dists3.append(np.mean(nums))
    print(np.mean(dists1))
    print(np.mean(dists2))
    print(np.mean(dists3))
    n = np.mean([np.mean(dists1), np.mean(dists2), np.mean(dists3)])
    print(n)
    print("------------------------------------")
    dists4 = []
    dists5 = []
    dists6 = []
    paths = ["animal", "flower", "foliage", "fruit", "landscape", "manmade", "shadow", "texture", "winter"]
    upaths = list(map(lambda x: "u" + x, paths))
    for path in upaths:
        pre = "jnd/swirl/"
        f1 = "jnd/"+path + ".png"
        f2 = pre + "14jnd_" + path + ".png"
        # u_filename1 = pre+"u" + path + ".png"
        # u_filename2 = pre+"14jnd_u" + path +".png"
        img1 = cv2.imread(f1, 0)
        img2 = cv2.imread(f2, 0)
        dists4.append(mse(img1, img2))

    for path in upaths:
        pre = "jnd/blur/"
        f1 = "jnd/"+path + ".png"
        f2 = pre + "14jnd_" + path + ".png"
        # u_filename1 = pre+"u" + path + ".png"
        # u_filename2 = pre+"14jnd_u" + path +".png"
        img1 = cv2.imread(f1, 0)
        img2 = cv2.imread(f2, 0)
        dists5.append(mse(img1, img2))

    for path in upaths:
        pre = "jnd/noise/"
        f1 = "jnd/"+path + ".png"
        nums = []
        img1 = cv2.imread(f1, 0)
        for i in range(10):
            f2 = pre + "14jnd_" + path + "_" + str(i) + ".png"
            # u_filename1 = pre+"u" + path + ".png"
            # u_filename2 = pre+"14jnd_u" + path +".png"

            img2 = cv2.imread(f2, 0)
            nums.append(mse(img1, img2))
        dists6.append(np.mean(nums))
    print(np.mean(dists4))
    print(np.mean(dists5))
    print(np.mean(dists6))
    un = np.mean([np.mean(dists4), np.mean(dists5), np.mean(dists6)])
    print(un)
    print(np.mean([n, un]))
conchincradle commented 1 year ago
!pip install IQA_pytorch
def mse(A,B):
  return np.sqrt(np.sum(np.power((np.float32(A) - np.float32(B))/255, 2))/(256*256))
from IQA_pytorch import SSIM, GMSD, NLPD,LPIPSvgg, DISTS,MS_SSIM,VSI
img1 = cv2.imread("animal.png",0)
img1_tensor =transforms.ToTensor()(img1).unsqueeze(0)

X = img1_tensor
# X = X.repeat(1, 3, 1, 1)  
Y = X

print(X.shape)
# D = SSIM(channels=1)
D = LPIPSvgg(channels=3)
# Calculate score of the image X with the reference Y
# X: (N,3,H,W) 
# Y: (N,3,H,W) 
# Tensor, data range: 0~1

score = D(X, Y, as_loss=False) 
# set 'as_loss=True' to get a value as loss for optimizations.
print(score.item())
import torchvision.transforms as transforms
conchincradle commented 1 year ago
def mse(img1,img2):
  t1 =transforms.ToTensor()(img1).unsqueeze(0)
  t2 =transforms.ToTensor()(img2).unsqueeze(0)
  score = D(t1,t2,as_loss=False)
  return 1-score.item()

import cv2
dists1 = []
dists2 = []
dists3 = []
paths =  ["animal","flower","foliage","fruit","landscape","manmade","shadow","texture","winter"]
for path in paths:
    pre = "swirl/" 
    f1 =  path + ".png"
    f2 = pre + "14jnd_" + path +".png"
    # u_filename1 = pre+"u" + path + ".png"
    # u_filename2 = pre+"14jnd_u" + path +".png"
    img1 = cv2.imread(f1,0)
    img2 = cv2.imread(f2,0)
    dists1.append(mse(img1,img2))

for path in paths:
    pre = "blur/" 
    f1 =  path + ".png"
    f2 = pre + "14jnd_" + path +".png"
    # u_filename1 = pre+"u" + path + ".png"
    # u_filename2 = pre+"14jnd_u" + path +".png"
    img1 = cv2.imread(f1,0)
    img2 = cv2.imread(f2,0)
    dists2.append(mse(img1,img2))

for path in paths:
    pre = "noise/" 
    f1 =  path + ".png"
    nums = []
    img1 = cv2.imread(f1,0)
    for i in range(10):
      f2 = pre + "14jnd_" + path +"_"+str(i)+".png"
      # u_filename1 = pre+"u" + path + ".png"
      # u_filename2 = pre+"14jnd_u" + path +".png"

      img2 = cv2.imread(f2,0)
      nums.append(mse(img1,img2))
    dists3.append(np.mean(nums))
print(np.mean(dists1))
print(np.mean(dists2))
print(np.mean(dists3))
n = np.mean([np.mean(dists1),np.mean(dists2),np.mean(dists3)])
print(n)
print("--------------------------------")
import cv2
dists4 = []
dists5 = []
dists6 = []
paths =  ["animal","flower","foliage","fruit","landscape","manmade","shadow","texture","winter"]
upaths = list(map(lambda x: "u" + x, paths))
for path in upaths:
    pre = "swirl/" 
    f1 =  path + ".png"
    f2 = pre + "14jnd_" + path +".png"
    # u_filename1 = pre+"u" + path + ".png"
    # u_filename2 = pre+"14jnd_u" + path +".png"
    img1 = cv2.imread(f1,0)
    img2 = cv2.imread(f2,0)
    dists4.append(mse(img1,img2))

for path in upaths:
    pre = "blur/" 
    f1 =  path + ".png"
    f2 = pre + "14jnd_" + path +".png"
    # u_filename1 = pre+"u" + path + ".png"
    # u_filename2 = pre+"14jnd_u" + path +".png"
    img1 = cv2.imread(f1,0)
    img2 = cv2.imread(f2,0)
    dists5.append(mse(img1,img2))

for path in upaths:
    pre = "noise/" 
    f1 =  path + ".png"
    nums = []
    img1 = cv2.imread(f1,0)
    for i in range(10):
      f2 = pre + "14jnd_" + path +"_"+str(i)+".png"
      # u_filename1 = pre+"u" + path + ".png"
      # u_filename2 = pre+"14jnd_u" + path +".png"

      img2 = cv2.imread(f2,0)
      nums.append(mse(img1,img2))
    dists6.append(np.mean(nums))
print(np.mean(dists4))
print(np.mean(dists5))
print(np.mean(dists6))
un = np.mean([np.mean(dists4),np.mean(dists5),np.mean(dists6)])
print(un)
print(np.mean([n,un]))