KUCognitiveInformaticsLab / zhu_project_real_and_fake

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

check monotonicity #7

Open conchincradle opened 1 year ago

conchincradle commented 1 year ago

need to revise the IQA model

import pyiqa
import cv2
import torch
import pandas as pd
from pyiqa import imread2tensor
import numpy as np
import os
from wand.image import Image
def print_hi(name):
    # 在下面的代码行中使用断点来调试脚本。
    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。
def mse(A,B):
  return np.sqrt(np.sum(np.power((np.float32(A) - np.float32(B))/255, 2))/(256*256))

# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':

    os.environ["CUDA_VISIBLE_DEVICES"] = "1"

    torch.device('cuda', 1)
    print_hi('PyCharm')
    paths = ["animal", "flower", "foliage", "fruit", "landscape", "manmade", "shadow", "texture", "winter"]
    upaths = list(map(lambda x: "u" + x, paths))
    ahiq_metric = pyiqa.create_metric('ahiq').cuda()
    # ahiq_metric = pyiqa.create_metric('ahiq')
    column_names = ['natural','unnatural']
    data = []
    pre = "spatial/"
    for path in paths+upaths:
        n_values = []
        print(path)

        for i in range(100) :
            # print(i)
            filename1 = pre+  path+".png"

            img11 = cv2.imread(filename1, 0)
            n_tensor1 = imread2tensor(filename1).unsqueeze(0).cuda()
            sigma = 1.3 * (10 ** (3 * (0.01*i)))
            filename512 = pre+"512_"+path+".png"
            filename2 = pre + "tmp_siwrl.png"
            with Image(filename=filename512) as img1:
                img1.swirl(degree=sigma)
                img1.resize(256, 256)
                img1.save(filename=filename2)
            n_tensor2 = imread2tensor(filename2).unsqueeze(0).cuda()
            # img2 = cv2.imread(filename2, 0)
            # n_values.append(mse(img11,img2))

            n_values.append(1-ahiq_metric(n_tensor1,n_tensor2).item())
        data.append(n_values)

    df = pd.DataFrame(np.transpose(np.array(data)), columns=paths+upaths)
    df.to_csv('swirl_ahiq_all.csv', index=False)
conchincradle commented 1 year ago

import pyiqa
import cv2
import torch
import pandas as pd
from pyiqa import imread2tensor
import numpy as np
import os

def print_hi(name):
    # 在下面的代码行中使用断点来调试脚本。
    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。
def mse(A,B):
  return np.sqrt(np.sum(np.power((np.float32(A) - np.float32(B))/255, 2))/(256*256))

# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':

    os.environ["CUDA_VISIBLE_DEVICES"] = "1"

    torch.device('cuda', 1)
    print_hi('PyCharm')
    paths = ["animal", "flower", "foliage", "fruit", "landscape", "manmade", "shadow", "texture", "winter"]
    upaths = list(map(lambda x: "u" + x, paths))
    ahiq_metric = pyiqa.create_metric('ahiq').cuda()
    column_names = ['natural','unnatural']
    data = []
    pre = "spatial/"
    for path in paths+upaths:

        jnd_means = []
        print(path)
        for i in range(8):
            # print(i)
            filename1 = pre+  path+".png"
            img = cv2.imread(pre+  path+".png",0)
            n_tensor1 = imread2tensor(filename1).unsqueeze(0).cuda()
            sigma = 0.001 * (10 ** (3 * (i*0.1)))
            n_values = []
            for seed in range(1, 10):
                np.random.seed(seed)
                noise = np.random.normal(0, sigma, img.shape)
                imgTmp = img / 255
                gauss = np.clip(imgTmp + noise, 0, 1)
                gauss = np.uint8(gauss * 255)
                # name1 = "tmp.png"
                name1 = pre+ "tmp_noise.png"
                cv2.imwrite(name1, gauss)
                n_tensor2 = imread2tensor(name1).unsqueeze(0).cuda()
                n_jnd_value  = 1-ahiq_metric(n_tensor1,n_tensor2).item()
                # if(n_jnd_value>1):
                #     print(n_jnd_value)
                n_values.append(n_jnd_value)
                # n_values.append(mse(img,gauss))

            jnd_means.append(np.mean(np.array(n_values)))
            print(np.std(np.array(n_values)))
            # jnd_means.append(np.array(n_values))

        data.append(jnd_means)

    df = pd.DataFrame(np.transpose(np.array(data)), columns=paths+upaths)
    # df = pd.DataFrame(np.transpose(np.array(data)), columns=[)
    df.to_csv('noise_ahiq_all.csv', index=False)
conchincradle commented 1 year ago

import pyiqa
import cv2
import torch
import pandas as pd
from pyiqa import imread2tensor
import numpy as np
import os

def print_hi(name):
    # 在下面的代码行中使用断点来调试脚本。
    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。
def mse(A,B):
  return np.sqrt(np.sum(np.power((np.float32(A) - np.float32(B))/255, 2))/(256*256))

# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':

    os.environ["CUDA_VISIBLE_DEVICES"] = "1"

    torch.device('cuda', 1)
    print_hi('PyCharm')
    paths = ["animal", "flower", "foliage", "fruit", "landscape", "manmade", "shadow", "texture", "winter"]
    upaths = list(map(lambda x: "u" + x, paths))
    ahiq_metric = pyiqa.create_metric('ahiq').cuda()
    column_names = ['natural','unnatural']
    data = []
    pre = "spatial/"
    for path in paths+upaths:
    # for path in ["fruit"]:
        n_values = []
        print(path)
        for i in range(20):
             # print(i)
            filename1 = pre+  path+".png"
            img = cv2.imread(pre+  path+".png",0)
            n_tensor1 = imread2tensor(filename1).unsqueeze(0).cuda()

            sigma = 0.2 * (10 **(0.04*i))
            blur = cv2.GaussianBlur(img, (0, 0), sigmaX=sigma, sigmaY=0)
            filename2 = pre + "tmp_blur.png"

            cv2.imwrite( filename2,blur)

            n_tensor2 = imread2tensor(filename2).unsqueeze(0).cuda()
            n_values.append(1-ahiq_metric(n_tensor1,n_tensor2).item())
            # n_values.append(mse(img,blur))
        data.append(n_values)

    df = pd.DataFrame(np.transpose(np.array(data)), columns=paths+upaths)
    # df = pd.DataFrame(np.transpose(np.array(data)), columns=["fruit"])
    df.to_csv('blur_ahiq_all.csv', index=False)