PIR_tar / scripts /2_generate_iqa_detail.py
AaronCIH's picture
Update scripts/2_generate_iqa_detail.py
6c12352 verified
"""
data format:
{
"distortion_classes": [
"saturate_strengthen",
"compression"
],
"distortion_names": [
"saturate_strengthen_HSV",
"compression_jpeg_2000"
],
"severities": [
2,
3
],
"image_ref": null,
"image_A": "KADIS700K/refA_md_detail/dist_imgs/abyssinian-ground-hornbill-560232_0.png",
"image_B": null,
"id": "abyssinian-ground-hornbill-560232_0",
"task_type": "quality_single_A_noref",
"conversations": [
{
"from": "human",
"value": "Assess the image's quality from a professional standpoint."
},
{
"from": "gpt",
"value": "The image depicts a bird with a large beak holding some material. The colors appear unnaturally saturated, reducing detail and creating an unrealistic representation of the subject. Additionally, there's significant artifacting indicative of heavy compression, which further degrades clarity and texture. Overall, the image quality is poor, with the distortions severely impacting the visual integrity of the content."
}
]
},
"""
import os
from PIL import Image
from pathlib import Path
import numpy as np
import random
# import torchvision.transforms.functional as TF
# import torchvision.transforms as transforms
# import cv2
import re
import json
# ir dataset dictionary:
base_rt = f'/home/work/shared-fi-datasets-01/users/hsiang.chen/Project/Datasets/IR'
dataset_dict = {
# Image Restoration
## Super Resolution (3)
"HR": {
"DIV2K": {'train': 'SuperResolution/DIV2K/metas/DIV2K_train_HR.list', # (800, single)
'val': 'SuperResolution/DIV2K/metas/DIV2K_valid_HR.list'}, # (100, single)
"Flickr2K": {'train': 'SuperResolution/Flickr2K/metas/Flickr2K_HR.list'}, # (2650, single)
"OST": {'train': 'SuperResolution/OST/metas/OST_HR.list'}, # (10324, single)
},
"Low Resolution": {
"DIV2K": {'train1': 'SuperResolution/DIV2K/metas/DIV2K_train_pair_SR1.list', # (800, pair)
'train2': 'SuperResolution/DIV2K/metas/DIV2K_train_pair_SR2.list', # (800, pair)
'train3': 'SuperResolution/DIV2K/metas/DIV2K_train_pair_SR3.list', # (800, pair)
'val': 'SuperResolution/DIV2K/metas/DIV2K_valid_pair_SR.list'}, # (100, pair)
"Flickr2K": {'train1': 'SuperResolution/Flickr2K/metas/Flickr2K_train_pair_SR1.list', # (2650, pair)
'train2': 'SuperResolution/Flickr2K/metas/Flickr2K_train_pair_SR2.list', # (2650, pair)
'train3': 'SuperResolution/Flickr2K/metas/Flickr2K_train_pair_SR3.list'},# (2650, pair)
"OST": {'train1': 'SuperResolution/OST/metas/OST_train_pair_SR1.list', # (10324, pair)
'train2': 'SuperResolution/OST/metas/OST_train_pair_SR2.list', # (10324, pair)
'train3': 'SuperResolution/OST/metas/OST_train_pair_SR3.list'}, # (10324, pair)
},
## Derain (5)
"Rain": {
"RainTrainL": {'train': 'Derain/RainTrainL/metas/train.list'}, # (200, pair)
"Rain100L": {'test': 'Derain/Rain100L/metas/test.list'}, # (100, pair)
"LHPRain": {'train': 'Derain/LHPRain/metas/train.list', # (2100, pair)
'val': 'Derain/LHPRain/metas/val.list', # (600, pair)
'test': 'Derain/LHPRain/metas/test.list'}, # (300, pair)
"UHDRain": {'train': 'Derain/UHD-Rain/metas/train.list', # (3000, pair)
'test': 'Derain/UHD-Rain/metas/test.list'}, # (200, pair)
"Practical": {'test': 'Derain/Practical/metas/test.list'}, # (15, real)
},
## Deraindrop (2)
"RainDrop": {
"RainDrop": {'train': 'Derain/RainDrop/metas/Raindrop_train.list', # (861, pair)
'test_a': 'Derain/RainDrop/metas/Raindrop_test_a.list', # (58, pair)
'test_b': 'Derain/RainDrop/metas/Raindrop_test_b.list'}, # (249, pair)
"RainDS_syn_rainstreak": {'train': 'Derain/RainDS/metas/RainDS_syn_train_rainstreak.list', # (1000, pair)
'test': 'Derain/RainDS/metas/RainDS_syn_test_rainstreak.list'}, # (200, pair)
"RainDS_syn_raindrop": {'train': 'Derain/RainDS/metas/RainDS_syn_train_raindrop.list', # (1000, pair)
'test': 'Derain/RainDS/metas/RainDS_syn_test_raindrop.list'}, # (200, pair)
"RainDS_syn_rainstreak_raindrop": {'train': 'Derain/RainDS/metas/RainDS_syn_train_rainstreak_raindrop.list', # (1000, pair)
'test': 'Derain/RainDS/metas/RainDS_syn_test_rainstreak_raindrop.list'}, # (200, pair)
"RainDS_real_rainstreak": {'train': 'Derain/RainDS/metas/RainDS_real_train_set_rainstreak.list', # (150, pair)
'test': 'Derain/RainDS/metas/RainDS_real_test_set_rainstreak.list'}, # (98, pair)
"RainDS_real_raindrop": {'train': 'Derain/RainDS/metas/RainDS_real_train_set_raindrop.list', # (150, pair)
'test': 'Derain/RainDS/metas/RainDS_real_test_set_raindrop.list'}, # (98, pair)
"RainDS_real_rainstreak_raindrop": {'train': 'Derain/RainDS/metas/RainDS_real_train_set_rainstreak_raindrop.list', # (150, pair)
'test': 'Derain/RainDS/metas/RainDS_real_test_set_rainstreak.list'}, # (98, pair)
},
## Dehaze (5)
"Fog":{
"SOTS": {'test': 'Dehaze/SOTS/metas/test.list'}, # (500, pair)
"OTS": {'train': 'Dehaze/OTS/metas/train.list'}, # (72135, pair)
"4kID": {'train': 'Dehaze/4kID/metas/train.list', # (15606, pair)
'test': 'Dehaze/4kID/metas/test.list'}, # (97, pair)
"Unann": {'test': 'Dehaze/UnannotatedHazyImages/metas/test.list'}, # (4809, real)
"NH-Haze": {'test': 'Dehaze/NH-Haze/metas/test.list'}, # (5, real)
},
## Denoise (9)
"Noise": {
"BSD400": {'train': 'Denoise/BSD400/metas/BSD400.list',
'train1': 'Denoise/BSD400/metas/BSD400_Noise_L1.list',
'train2': 'Denoise/BSD400/metas/BSD400_Noise_L3.list',
'train3': 'Denoise/BSD400/metas/BSD400_Noise_L5.list',}, # (400, syn)
"WED": {'train': 'Denoise/WaterlooED/metas/WaterlooED.list',
'train1': 'Denoise/WaterlooED/metas/WaterlooED_Noise_L1.list',
'train2': 'Denoise/WaterlooED/metas/WaterlooED_Noise_L3.list',
'train3': 'Denoise/WaterlooED/metas/WaterlooED_Noise_L5.list',}, # (4744, syn)
"BSD68": {'test': 'Denoise/BSD68/metas/BSD68.list',
'test1': 'Denoise/BSD68/metas/BSD68_Noise_L1.list',
'test2': 'Denoise/BSD68/metas/BSD68_Noise_L3.list',
'test3': 'Denoise/BSD68/metas/BSD68_Noise_L5.list'}, # (68, syn)
"Urban": {'test': 'Denoise/Urban100/metas/Urban100.list',
'test1': 'Denoise/Urban100/metas/Urban100_Noise_L1.list',
'test2': 'Denoise/Urban100/metas/Urban100_Noise_L3.list',
'test3': 'Denoise/Urban100/metas/Urban100_Noise_L5.list'}, # (100, syn)
"CBSD68": {'test': 'Denoise/CBSD68/metas/CBSD68.list',
'test1': 'Denoise/CBSD68/metas/CBSD68_Noise_L1.list',
'test2': 'Denoise/CBSD68/metas/CBSD68_Noise_L3.list',
'test3': 'Denoise/CBSD68/metas/CBSD68_Noise_L5.list',}, # (68, syn)
"Kodak": {'test': 'Denoise/Kodak/metas/Kodak.list',
'test1': 'Denoise/Kodak/metas/Kodak_Noise_L1.list',
'test2': 'Denoise/Kodak/metas/Kodak_Noise_L3.list',
'test3': 'Denoise/Kodak/metas/Kodak_Noise_L5.list'}, # (24, syn)
"McMaster": {'test': 'Denoise/McMaster/metas/McMaster.list',
'test1': 'Denoise/McMaster/metas/McMaster_Noise_L1.list',
'test2': 'Denoise/McMaster/metas/McMaster_Noise_L3.list',
'test3': 'Denoise/McMaster/metas/McMaster_Noise_L5.list'}, # (18, syn)
"Set12": {'test': 'Denoise/Set12/metas/Set12.list',
'test1': 'Denoise/Set12/metas/Set12_Noise_L1.list',
'test2': 'Denoise/Set12/metas/Set12_Noise_L3.list',
'test3': 'Denoise/Set12/metas/Set12_Noise_L5.list',}, # (12, syn)
"SIDD": {'train': 'Denoise/SIDD/metas/train.list', # (320, pair)
'test': 'Denoise/SIDD/metas/test.list'}, # (1280, pair)
"DIV2K": {'train1': 'SuperResolution/DIV2K/metas/DIV2K_train_pair_Noise_L1.list', # (800, pair)
'train2': 'SuperResolution/DIV2K/metas/DIV2K_train_pair_Noise_L3.list', # (800, pair)
'train3': 'SuperResolution/DIV2K/metas/DIV2K_train_pair_Noise_L5.list', # (800, pair)
'val': 'SuperResolution/DIV2K/metas/DIV2K_valid_pair_Noise.list'}, # (100, pair)
"Flickr2K": {'train1': 'SuperResolution/Flickr2K/metas/Flickr2K_train_pair_Noise_L1.list', # (2650, pair)
'train2': 'SuperResolution/Flickr2K/metas/Flickr2K_train_pair_Noise_L3.list', # (2650, pair)
'train3': 'SuperResolution/Flickr2K/metas/Flickr2K_train_pair_Noise_L5.list'},# (2650, pair)
},
## Desnow (2)
"Snow": {
"Snow100k": {'train': 'Desnow/Snow100k/metas/train.list'}, # (50000, syn)
"Snow100k-S": {'test': 'Desnow/Snow100k/metas/test_S.list'}, # (16611, syn)
"Snow100k-M": {'test': 'Desnow/Snow100k/metas/test_M.list'}, # (16588, syn)
"Snow100k-L": {'test': 'Desnow/Snow100k/metas/test_L.list'}, # (16801, syn)
"Snow100k-R": {'test': 'Desnow/Snow100k/metas/test_realistic.list'}, # (1329, real)
"UHDSnow": {'train': 'Desnow/UHD-Snow/metas/train.list', # (3000, pair)
'test': 'Desnow/UHD-Snow/metas/test.list'}, # (200, pair)
},
## Deblur (3)
"Blur": {
"GoPro": {'train': 'Deblur/GoPro/metas/train.list', # (2103, pair)
'test': 'Deblur/GoPro/metas/test.list'}, # (1111, pair)
"HIDE": {'train': 'Deblur/HIDE/metas/train.list', # (6397, pair)
'test': 'Deblur/HIDE/metas/test.list'}, # (2025, pair)
"RealBlur-J": {'test': 'Deblur/RealBlur-J_ECC_IMCORR_centroid_itensity_ref/metas/test.list'}, # (980, pair)
"RealBlur-R": {'test': 'Deblur/RealBlur-R_BM3D_ECC_IMCORR_centroid_itensity_ref/metas/test.list'},# (980, pair)
},
## Lowlight (6)
"Low-light": {
"LOL": {'train': 'LowLight/LOL/metas/train.list', # (485, pair)
'test': 'LowLight/LOL/metas/test.list'}, # (15, pair)
"DICM": {'test': 'LowLight/DICM/metas/test.list'}, # (69, real)
"MEF": {'test': 'LowLight/MEF/metas/test.list'}, # (17, real)
"NPE": {'test': 'LowLight/NPE/metas/test.list'}, # (8, real)
"LIME": {'test': 'LowLight/LIME/metas/test.list'}, # (10, real)
"VV": {'test': 'LowLight/VV/metas/test.list'}, # (24, real)
},
## Others (1)
"Unknown": {
"UDC": {'val': 'Other/UDC/metas/val.list', # (60, pair)
'test': 'Other/UDC/metas/test.list'}, # (60, pair)
},
## Composite (1)
"Composite": {
"CDD": {
'train': 'Composite/CDD11/metas/train.list', # (13013, pair)
'test_haze': 'Composite/CDD11/metas/test_haze.list', # (200, pair)
'test_haze_rain': 'Composite/CDD11/metas/test_haze_rain.list', # (200, pair)
'test_haze_snow': 'Composite/CDD11/metas/test_haze_snow.list', # (200, pair)
'test_low': 'Composite/CDD11/metas/test_low.list', # (200, pair)
'test_low_haze': 'Composite/CDD11/metas/test_low_haze.list', # (200, pair)
'test_low_haze_rain': 'Composite/CDD11/metas/test_low_haze_rain.list', # (200, pair)
'test_low_haze_snow': 'Composite/CDD11/metas/test_low_haze_snow.list', # (200, pair)
'test_low_rain': 'Composite/CDD11/metas/test_low_rain.list', # (200, pair)
'test_low_snow': 'Composite/CDD11/metas/test_low_snow.list', # (200, pair)
'test_rain': 'Composite/CDD11/metas/test_rain.list', # (200, pair)
'test_snow': 'Composite/CDD11/metas/test_snow.list', # (200, pair)
},
},
}
def IRImageData(listfile):
# [degradation, clean, label]
paths = []
with open(listfile) as fin:
for line in fin:
line = line.strip().split()
if len(line) == 3:
paths.append(line)
paths = sorted(paths)
# Dataset
LQ_list = []
HQ_list = []
for data in paths:
lq_pth, hq_pth, label = data
if os.path.isfile(lq_pth):
LQ_list.append(lq_pth)
if os.path.isfile(hq_pth):
HQ_list.append(hq_pth)
return LQ_list, HQ_list
# question dictionary:
question_dict = {
"query": [
"Analyze the image's quality, and detail your findings.",
"Analyze the image's quality, focusing on its key elements, and detail your findings.",
"Assess the image's quality from a professional standpoint.",
"Assess the image's quality with a critical eye, highlighting both strengths and areas for improvement.",
"Assess the quality of the image with detailed reasons.",
"Can you provide a detailed evaluation of the image's quality?",
"Could you assess the overall quality of the image and elaborate on your evaluation?",
"Could you please analyze the quality of the image, considering factors such as composition and clarity, and then elaborate on your assessment?",
"Evaluate the image's clarity and explain how it contributes to the overall quality.",
"Evaluate the image's quality and justify your evaluation.",
"Evaluate the overall quality of the image and provide a comprehensive explanation of your assessment.",
"Evaluate the quality of the image and provide a comprehensive explanation.",
"Examine the image for quality aspects, and explain assessment by considering different factors influencing the composition and clarity.",
"Examine the image's quality by considering factors influencing its clarity.",
"How about the overall quality of the image, and why?",
"How do you perceive the quality of the image, and what aspects influence your judgment?",
"How does the image's quality impact its overall effectiveness or appeal?",
"How would you rate the image's quality, and what factors contribute to your assessment?",
"How would you rate the overall quality of the image, and why?",
"How would you rate the quality of the image, and what specific elements contribute to your assessment?",
"In your opinion, what are the strengths and weaknesses of the image in terms of quality, and why?",
"Offer an assessment of the image's quality, highlighting any strengths or weaknesses.",
"Please appraise the image's quality and provide insights into what makes it visually appealing or otherwise.",
"Please evaluate the image's quality and provide your reasons.",
"Provide a comprehensive assessment of the image's quality, including both strengths and areas for improvement.",
"Provide a thorough evaluation of the image's quality.",
"What aspects of the image do you find most appealing or problematic from a quality perspective, and why?",
"What criteria are you using to assess the overall quality of the image, and can you explain your assessment in detail?",
"What is your opinion on the image's quality? Elaborate on your evaluation.",
"What is your opinion on the quality of the image? Explain your viewpoint."
]
}
def question_generate():
template = random.choice(question_dict["query"])
return template
if __name__ == "__main__":
for degradation, degra_dict in dataset_dict.items():
for dname, ddict in degra_dict.items():
for dset, list_path in ddict.items():
meta_refA = []
meta_A = []
meta_syn = []
# read data list
paths = []
list_path = os.path.join(base_rt, list_path)
with open(list_path) as fin:
for line in fin:
line = line.strip().split()
if len(line) == 3:
paths.append(line)
paths = sorted(paths)
# Dataset
LQ_list = []
HQ_list = []
for data in paths:
lq_pth, hq_pth, label = data
if os.path.isfile(hq_pth):
HQ_list.append(hq_pth)
image_ref = os.path.relpath(hq_pth, base_rt).replace("\\", "/")
id = os.path.basename(image_ref)
else:
image_ref = None
if os.path.isfile(lq_pth):
LQ_list.append(lq_pth)
image_A = os.path.relpath(lq_pth, base_rt).replace("\\", "/")
id = os.path.basename(image_A)
else:
image_A = None
meta_refA.append({
"distortion_class": degradation,
"distortion_name": degradation,
"severity": 3,
"id": id,
"image_ref": image_ref,
"image_A": image_A,
"image_B": None,
"task_type": "quality_single_A",
"conversations": [
{
"from": "human",
"value": question_generate(),
},
{
"from": "gpt",
"value": None
}
],
})
meta_A.append({
"distortion_class": degradation,
"distortion_name": degradation,
"severity": 3,
"id": id,
"image_ref": image_ref,
"image_A": image_A,
"image_B": None,
"task_type": "quality_single_A_noref",
"conversations": [
{
"from": "human",
"value": question_generate(),
},
{
"from": "gpt",
"value": None
}
],
})
meta_syn.append({
"distortion_class": degradation,
"distortion_name": degradation,
"severity": 3,
"id": id,
"image_ref": image_ref,
"image_A": None,
"image_B": None,
"task_type": "quality_single_A_noref",
"conversations": [
{
"from": "human",
"value": question_generate(),
},
{
"from": "gpt",
"value": None
}
],
})
if len(LQ_list) > 0 and len(HQ_list) > 0 and len(LQ_list) == len(HQ_list): # pair
meta_refA_pth = list_path.replace(".list", "_iqa_refA_detail.json")
meta_A_pth = list_path.replace(".list", "_iqa_A_detail.json")
with open(meta_refA_pth, "w") as f:
json.dump(meta_refA, f, indent=4)
with open(meta_A_pth, "w") as f:
json.dump(meta_A, f, indent=4)
print(f"[{os.path.relpath(meta_refA_pth, base_rt)}, ], # LQ[{len(LQ_list)}], HQ[{len(HQ_list)}], quality_single_A, {degradation}, {dname}-{dset}")
print(f"[{os.path.relpath(meta_A_pth, base_rt)}, ], # LQ[{len(LQ_list)}], HQ[{len(HQ_list)}], quality_single_A_noref, {degradation}, {dname}-{dset}")
elif len(LQ_list) > 0 and len(HQ_list) == 0: # real image
meta_A_pth = list_path.replace(".list", "_iqa_A_detail.json")
with open(meta_A_pth, "w") as f:
json.dump(meta_A, f, indent=4)
print(f"[{os.path.relpath(meta_A_pth, base_rt)}, ], # LQ[{len(LQ_list)}], HQ[{len(HQ_list)}], quality_single_A_noref, {degradation}, {dname}-{dset}")
elif len(LQ_list) == 0 and len(HQ_list) > 0: # syn image
meta_refA_pth = list_path.replace(".list", "_iqa_syn_refA_detail.json")
meta_syn_pth = list_path.replace(".list", "_iqa_syn_A_detail.json")
with open(meta_refA_pth, "w") as f:
json.dump(meta_refA, f, indent=4)
with open(meta_syn_pth, "w") as f:
json.dump(meta_syn, f, indent=4)
print(f"[{os.path.relpath(meta_refA_pth, base_rt)}, ], # LQ[{len(LQ_list)}], HQ[{len(HQ_list)}], quality_single_A, {degradation}, {dname}-{dset}")
print(f"[{os.path.relpath(meta_syn_pth, base_rt)}, ], # LQ[{len(LQ_list)}], HQ[{len(HQ_list)}], quality_single_A_noref, {degradation}, {dname}-{dset}")
else:
raise KeyError(f"the task is not matched, please check the dataset {list_path}")