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: https://github.com/robustsam/RobustSAM/blob/main/data/augment.py
|
| 3 |
+
Source Dataset:
|
| 4 |
+
BAPPS: (one to one, 150K)
|
| 5 |
+
/group-volume/Human-Action-Analysis/datasets/IQA_datasets/DataDepictQA/BAPPS/images/twoafc_train/train/cnn/ref # 38120
|
| 6 |
+
/group-volume/Human-Action-Analysis/datasets/IQA_datasets/DataDepictQA/BAPPS/images/twoafc_train/train/mix/ref # 56640
|
| 7 |
+
/group-volume/Human-Action-Analysis/datasets/IQA_datasets/DataDepictQA/BAPPS/images/twoafc_train/train/traditional/ref # 56640
|
| 8 |
+
|
| 9 |
+
PIPAL: (one to one, 50)
|
| 10 |
+
/home/CORP/hsiang.chen/Project/Datasets/DataDepictQA/PIPAL/metas/train_refAB_mix_pipal_70k.json
|
| 11 |
+
|
| 12 |
+
KADID10K: (one to one, need to find clear sample from json, estimated 70)
|
| 13 |
+
/home/CORP/hsiang.chen/Project/Datasets/DataDepictQA/KADID10K/metas/train_refAB_mix_kadid_30k.json
|
| 14 |
+
|
| 15 |
+
KADID700K: (one to one, 140k)
|
| 16 |
+
|
| 17 |
+
DetailDescriptionLAMM/metas/detailed_description_49k.json (one to one, 57895 images, 58k)
|
| 18 |
+
|
| 19 |
+
Annotation Format:
|
| 20 |
+
data = [
|
| 21 |
+
{
|
| 22 |
+
"distortion_class": "saturate_strengthen",
|
| 23 |
+
"distortion_name": "saturate_strengthen_YCrCb",
|
| 24 |
+
"severity": 5,
|
| 25 |
+
"id": "121-cc-774921_0",
|
| 26 |
+
"image_ref": "KADIS700K/ref_imgs_s224/121-cc-774921.png",
|
| 27 |
+
"image_A": "KADIS700K/refA_sd_brief/dist_imgs/121-cc-774921_0.png",
|
| 28 |
+
"image_B": None,
|
| 29 |
+
"task_type": "quality_single_A",
|
| 30 |
+
"conversations": [
|
| 31 |
+
{
|
| 32 |
+
"from": "human",
|
| 33 |
+
"value": "What critical ONE quality degradation is present in the evaluated image versus the reference?",
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"from": "gpt",
|
| 37 |
+
"value": "The critical ONE quality degradation presented is overly high saturation."
|
| 38 |
+
}
|
| 39 |
+
]
|
| 40 |
+
}
|
| 41 |
+
]
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
import json
|
| 45 |
+
import cv2
|
| 46 |
+
from glob import glob
|
| 47 |
+
import os
|
| 48 |
+
import numpy as np
|
| 49 |
+
from PIL import Image, ImageDraw
|
| 50 |
+
import matplotlib.pyplot as plt
|
| 51 |
+
import albumentations as A
|
| 52 |
+
import imgaug.augmenters as iaa
|
| 53 |
+
import random
|
| 54 |
+
import argparse
|
| 55 |
+
from tqdm import tqdm
|
| 56 |
+
from datetime import datetime
|
| 57 |
+
from pathlib import Path
|
| 58 |
+
|
| 59 |
+
import torchvision.transforms as T
|
| 60 |
+
import torchvision.transforms.functional as TF
|
| 61 |
+
import torchvision.transforms as transforms
|
| 62 |
+
|
| 63 |
+
# question dictionary:
|
| 64 |
+
question_dict = {
|
| 65 |
+
"Full-Reference": {
|
| 66 |
+
"ONE": [
|
| 67 |
+
"Compared to the reference, what ONE distortion stands out most in the evaluated image?",
|
| 68 |
+
"Determine the leading ONE degradation when comparing the evaluated image to the reference.",
|
| 69 |
+
"Determine the most impactful ONE distortion in the evaluated image compared to the reference.",
|
| 70 |
+
"Highlight the most significant ONE distortion in the evaluated image in comparison to the reference.",
|
| 71 |
+
"Identify the chief ONE degradation in the evaluated image when compared to the reference.",
|
| 72 |
+
"Identify the most notable ONE distortion in the evaluated image's quality when compared to the reference.",
|
| 73 |
+
"In comparison to the reference, what ONE distortion is most prominent in the evaluated image?",
|
| 74 |
+
"What ONE distortion is most apparent in the evaluated image relative to the reference?",
|
| 75 |
+
"What ONE distortion most significantly affects the evaluated image compared to the reference?",
|
| 76 |
+
"What ONE distortion stands out in the evaluated image against the reference?",
|
| 77 |
+
"What critical ONE quality degradation is present in the evaluated image versus the reference?",
|
| 78 |
+
],
|
| 79 |
+
"TWO": [
|
| 80 |
+
"Compared to the reference, what TWO distortions stand out most in the evaluated image?",
|
| 81 |
+
"Determine the leading TWO degradations when comparing the evaluated image to the reference.",
|
| 82 |
+
"Determine the most impactful TWO distortions in the evaluated image compared to the reference.",
|
| 83 |
+
"Highlight the most significant TWO distortions in the evaluated image in comparison to the reference.",
|
| 84 |
+
"Identify the chief TWO degradations in the evaluated image when compared to the reference.",
|
| 85 |
+
"Identify the most notable TWO distortions in the evaluated image's quality when compared to the reference.",
|
| 86 |
+
"In comparison to the reference, what TWO distortions are most prominent in the evaluated image?",
|
| 87 |
+
"What TWO distortions are most apparent in the evaluated image relative to the reference?",
|
| 88 |
+
"What TWO distortions most significantly affect the evaluated image compared to the reference?",
|
| 89 |
+
"What TWO distortions stand out in the evaluated image against the reference?",
|
| 90 |
+
"What critical TWO quality degradations are present in the evaluated image versus the reference?",
|
| 91 |
+
],
|
| 92 |
+
"Common": [
|
| 93 |
+
"Compared to the reference, what distortion(s) stand out most in the evaluated image?",
|
| 94 |
+
"Determine the leading degradation(s) when comparing the evaluated image to the reference.",
|
| 95 |
+
"Determine the most impactful distortion(s) in the evaluated image compared to the reference.",
|
| 96 |
+
"Highlight the most significant distortion(s) in the evaluated image in comparison to the reference.",
|
| 97 |
+
"Identify the chief degradation(s) in the evaluated image when compared to the reference.",
|
| 98 |
+
"Identify the most notable distortion(s) in the evaluated image's quality when compared to the reference.",
|
| 99 |
+
"In comparison to the reference, what distortion(s) are most prominent in the evaluated image?",
|
| 100 |
+
"What critical quality degradation(s) are present in the evaluated image versus the reference?",
|
| 101 |
+
"What distortion(s) are most apparent in the evaluated image relative to the reference?",
|
| 102 |
+
"What distortion(s) most significantly affect the evaluated image compared to the reference?",
|
| 103 |
+
"What distortion(s) stand out in the evaluated image against the reference?"
|
| 104 |
+
]
|
| 105 |
+
},
|
| 106 |
+
"Non-Reference": {
|
| 107 |
+
"ONE": [
|
| 108 |
+
"Determine the leading ONE degradation in the evaluated image.",
|
| 109 |
+
"Determine the most impactful ONE distortion in the evaluated image.",
|
| 110 |
+
"Highlight the most significant ONE distortion in the evaluated image.",
|
| 111 |
+
"Identify the chief ONE degradation in the evaluated image.",
|
| 112 |
+
"Identify the most critical ONE distortion in the evaluated image.",
|
| 113 |
+
"Identify the most notable ONE distortion in the evaluated image's quality.",
|
| 114 |
+
"In terms of image quality, what is the most glaring ONE issue with the evaluated image?",
|
| 115 |
+
"In the evaluated image, what ONE distortion is most detrimental to image quality?",
|
| 116 |
+
"Pinpoint the foremost ONE image quality issue in the evaluated image.",
|
| 117 |
+
"What ONE distortion is most apparent in the evaluated image?",
|
| 118 |
+
"What ONE distortion is most evident in the evaluated image?",
|
| 119 |
+
"What ONE distortion is most prominent in the evaluated image?",
|
| 120 |
+
"What ONE distortion is most prominent when examining the evaluated image?",
|
| 121 |
+
"What ONE distortion most detrimentally affects the overall quality of the evaluated image?",
|
| 122 |
+
"What ONE distortion most notably affects the clarity of the evaluated image?",
|
| 123 |
+
"What ONE distortion most significantly affects the evaluated image?",
|
| 124 |
+
"What ONE distortion stands out in the evaluated image?",
|
| 125 |
+
"What ONE quality degradation is most apparent in the evaluated image?",
|
| 126 |
+
"What critical ONE quality degradation is present in the evaluated image?",
|
| 127 |
+
"What is the foremost ONE distortion affecting the evaluated image's quality?",
|
| 128 |
+
"What is the leading ONE distortion in the evaluated image?",
|
| 129 |
+
"What is the most critical ONE image quality issue in the evaluated image?",
|
| 130 |
+
"What is the most severe ONE degradation observed in the evaluated image?",
|
| 131 |
+
"What is the primary ONE degradation observed in the evaluated image?"
|
| 132 |
+
],
|
| 133 |
+
"TWO": [
|
| 134 |
+
"Determine the leading TWO degradations in the evaluated image.",
|
| 135 |
+
"Determine the most impactful TWO distortions in the evaluated image.",
|
| 136 |
+
"Highlight the most significant TWO distortions in the evaluated image.",
|
| 137 |
+
"Identify the chief TWO degradations in the evaluated image.",
|
| 138 |
+
"Identify the most critical TWO distortions in the evaluated image.",
|
| 139 |
+
"Identify the most notable TWO distortions in the evaluated image's quality.",
|
| 140 |
+
"In terms of image quality, what are the most glaring TWO issues with the evaluated image?",
|
| 141 |
+
"In the evaluated image, what TWO distortions are most detrimental to image quality?",
|
| 142 |
+
"Pinpoint the foremost TWO image quality issues in the evaluated image.",
|
| 143 |
+
"What TWO distortions are most apparent in the evaluated image?",
|
| 144 |
+
"What TWO distortions are most evident in the evaluated image?",
|
| 145 |
+
"What TWO distortions are most prominent in the evaluated image?",
|
| 146 |
+
"What TWO distortions are most prominent when examining the evaluated image?",
|
| 147 |
+
"What TWO distortions most detrimentally affect the overall quality of the evaluated image?",
|
| 148 |
+
"What TWO distortions most notably affect the clarity of the evaluated image?",
|
| 149 |
+
"What TWO distortions most significantly affect the evaluated image?",
|
| 150 |
+
"What TWO distortions stand out in the evaluated image?",
|
| 151 |
+
"What TWO quality degradations are most apparent in the evaluated image?",
|
| 152 |
+
"What are the foremost TWO distortions affecting the evaluated image's quality?",
|
| 153 |
+
"What are the leading TWO distortions in the evaluated image?",
|
| 154 |
+
"What are the most critical TWO image quality issues in the evaluated image?",
|
| 155 |
+
"What are the most severe TWO degradations observed in the evaluated image?",
|
| 156 |
+
"What are the primary TWO degradations observed in the evaluated image?",
|
| 157 |
+
"What critical TWO quality degradations are present in the evaluated image?",
|
| 158 |
+
],
|
| 159 |
+
"Common": [
|
| 160 |
+
"Determine the leading degradation(s) in the evaluated image.",
|
| 161 |
+
"Determine the most impactful distortion(s) in the evaluated image.",
|
| 162 |
+
"Highlight the most significant distortion(s) in the evaluated image.",
|
| 163 |
+
"Identify the chief degradation(s) in the evaluated image.",
|
| 164 |
+
"Identify the most critical distortion(s) in the evaluated image.",
|
| 165 |
+
"Identify the most notable distortion(s) in the evaluated image's quality.",
|
| 166 |
+
"In terms of image quality, what are the most glaring issue(s) with the evaluated image?",
|
| 167 |
+
"In the evaluated image, what distortion(s) are most detrimental to image quality?",
|
| 168 |
+
"Pinpoint the foremost image quality issue(s) in the evaluated image.",
|
| 169 |
+
"What are the foremost distortion(s) affecting the evaluated image's quality?",
|
| 170 |
+
"What are the leading distortion(s) in the evaluated image?",
|
| 171 |
+
"What are the most critical image quality issue(s) in the evaluated image?",
|
| 172 |
+
"What are the most severe degradation(s) observed in the evaluated image?",
|
| 173 |
+
"What are the primary degradation(s) observed in the evaluated image?",
|
| 174 |
+
"What critical quality degradation(s) are present in the evaluated image?",
|
| 175 |
+
"What distortion(s) are most apparent in the evaluated image?",
|
| 176 |
+
"What distortion(s) are most evident in the evaluated image?",
|
| 177 |
+
"What distortion(s) are most prominent in the evaluated image?",
|
| 178 |
+
"What distortion(s) are most prominent when examining the evaluated image?",
|
| 179 |
+
"What distortion(s) most detrimentally affect the overall quality of the evaluated image?",
|
| 180 |
+
"What distortion(s) most notably affect the clarity of the evaluated image?",
|
| 181 |
+
"What distortion(s) most significantly affect the evaluated image?",
|
| 182 |
+
"What distortion(s) stand out in the evaluated image?",
|
| 183 |
+
"What quality degradation(s) are most apparent in the evaluated image?"
|
| 184 |
+
]
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
def question_generate(ref="Full-Reference", degra="Common"):
|
| 189 |
+
option = f" Answer the question using a single word or phrase."
|
| 190 |
+
template = random.choice(question_dict[ref]["Common"] + question_dict[ref][degra])
|
| 191 |
+
if random.random() >= 0.4:
|
| 192 |
+
template += option
|
| 193 |
+
return template
|
| 194 |
+
|
| 195 |
+
# ----------------------------
|
| 196 |
+
# Level-aware parameter helpers
|
| 197 |
+
# ----------------------------
|
| 198 |
+
def lerp(a, b, t):
|
| 199 |
+
return a + (b - a) * t
|
| 200 |
+
|
| 201 |
+
def choice_with_level(options_low, options_high, t):
|
| 202 |
+
# 在兩組範圍之間插值得到一組範圍,再隨機抽樣
|
| 203 |
+
low = (lerp(options_low[0], options_high[0], t),
|
| 204 |
+
lerp(options_low[1], options_high[1], t))
|
| 205 |
+
return random.uniform(low[0], low[1])
|
| 206 |
+
|
| 207 |
+
def int_range_with_level(rng_low, rng_high, t):
|
| 208 |
+
low = int(round(lerp(rng_low[0], rng_high[0], t)))
|
| 209 |
+
high = int(round(lerp(rng_low[1], rng_high[1], t)))
|
| 210 |
+
return random.randint(low, max(low, high))
|
| 211 |
+
|
| 212 |
+
def tuple_range_with_level(rng_low, rng_high, t):
|
| 213 |
+
return (lerp(rng_low[0], rng_high[0], t), lerp(rng_low[1], rng_high[1], t))
|
| 214 |
+
|
| 215 |
+
# level ∈ {1,2,3,4,5} → t ∈ [0,1], 1,2,3,4,5 -> 0, 0.25, 0.5, 0.75, 1
|
| 216 |
+
def level_to_t(level):
|
| 217 |
+
level = max(1, min(5, int(level)))
|
| 218 |
+
return (level - 1) / 4.0
|
| 219 |
+
|
| 220 |
+
# ----------------------------
|
| 221 |
+
# Degradations (level-aware)
|
| 222 |
+
# ----------------------------
|
| 223 |
+
def snow(image, level):
|
| 224 |
+
"""
|
| 225 |
+
original: A.RandomSnow(brightness_coeff=1.0, snow_point_lower=0.3, snow_point_upper=0.7, p=1),
|
| 226 |
+
iaa.Snowflakes(density=0.35, flake_size=(0.6, 0.8), speed=(0.01, 0.015), angle=0)
|
| 227 |
+
|
| 228 |
+
snow_point_lower, snow_point_upper: the cover rate of the snow
|
| 229 |
+
density = (0.005, 0.075)
|
| 230 |
+
density_uniformity = (0.3, 0.9)
|
| 231 |
+
flake_size = (0.2, 0.7) # ratio to image
|
| 232 |
+
flake_size_unifromity = (0.4, 0.8)
|
| 233 |
+
speed = (0.007, 0.03)
|
| 234 |
+
|
| 235 |
+
# t = level_to_t(level) # 1-5 to 0-1
|
| 236 |
+
# 亮度係數固定 1.0,雪點密度/大小/速度隨 level 增加
|
| 237 |
+
# snow_point_lower = lerp(0.1, 0.6, t) # 0.1, 0.225, 0.35, 0.475, 0.6
|
| 238 |
+
# snow_point_upper = lerp(0.3, 0.9, t) # 0.3, 0.45, 0.6, 0.75, 0.9
|
| 239 |
+
# flake_size = tuple_range_with_level((0.2, 0.4), (0.7, 1.0), t)
|
| 240 |
+
# density = lerp(0.05, 0.6, t)
|
| 241 |
+
# speed = tuple_range_with_level((0.005, 0.01), (0.02, 0.04), t)
|
| 242 |
+
"""
|
| 243 |
+
brightness_coeff_level = {1: (1.1, 1.15), 2: (1.15, 1.2), 3:(1.2, 1.25), 4:(1.3, 1.35), 5:(1.35, 1.4)}
|
| 244 |
+
lightness_threshold_level = {1: (60, 80), 2: (80, 100), 3:(100, 115), 4:(115, 130), 5:(130, 140)}
|
| 245 |
+
brightness = brightness_coeff_level[level]
|
| 246 |
+
lightness_threshold = lightness_threshold_level[level]
|
| 247 |
+
|
| 248 |
+
aug1 = iaa.FastSnowyLandscape(
|
| 249 |
+
lightness_threshold=lightness_threshold,
|
| 250 |
+
lightness_multiplier=brightness
|
| 251 |
+
)
|
| 252 |
+
img = aug1.augment_image(image)
|
| 253 |
+
|
| 254 |
+
density_level = {1: (0.005, 0.015), 2: (0.015, 0.030), 3: (0.030, 0.045), 4: (0.045, 0.060), 5: (0.060, 0.075)}
|
| 255 |
+
density = density_level[level]
|
| 256 |
+
flake_size = (0.2, 0.5)
|
| 257 |
+
speed = (0.007, 0.03)
|
| 258 |
+
aug = iaa.Snowflakes(density=density, density_uniformity=0.95,
|
| 259 |
+
flake_size=flake_size, speed=speed, angle=0)
|
| 260 |
+
img = aug.augment_image(img)
|
| 261 |
+
|
| 262 |
+
params = {
|
| 263 |
+
"lightness_threshold": lightness_threshold,
|
| 264 |
+
"brightness": brightness,
|
| 265 |
+
"flake_size": flake_size,
|
| 266 |
+
"density": density,
|
| 267 |
+
"speed": speed
|
| 268 |
+
}
|
| 269 |
+
return img, params
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def fog(image, level):
|
| 273 |
+
"""
|
| 274 |
+
original: fog_coef_lower = 0.1, fog_coef_upper = 0.2, alpha_coef = 0.08
|
| 275 |
+
t = level_to_t(level)
|
| 276 |
+
fog_coef_lower = lerp(0.05, 0.4, t)
|
| 277 |
+
fog_coef_upper = lerp(0.12, 0.7, t)
|
| 278 |
+
alpha_coef = lerp(0.02, 0.15, t)
|
| 279 |
+
"""
|
| 280 |
+
ratio = {1: (0.05, 0.2), 2: (0.2, 0.4), 3:(0.4, 0.6), 4:(0.6, 0.8), 5:(0.8, 1.0)}
|
| 281 |
+
aug = iaa.Fog()
|
| 282 |
+
fog_map = aug.augment_image(image)
|
| 283 |
+
img = image * (1-ratio[level][1]) + fog_map * ratio[level][1]
|
| 284 |
+
img = img.astype(np.uint8)
|
| 285 |
+
|
| 286 |
+
params = {
|
| 287 |
+
"alpha": ratio[level][1]
|
| 288 |
+
}
|
| 289 |
+
return img, params
|
| 290 |
+
|
| 291 |
+
def rain(image, level):
|
| 292 |
+
"""
|
| 293 |
+
original: iaa.Rain(drop_size=(0.40, 0.50), speed=(0.05, 0.1))
|
| 294 |
+
official: drop_size = (0.01, 0.02), speed = (0.04, 0.2)
|
| 295 |
+
|
| 296 |
+
t = level_to_t(level)
|
| 297 |
+
drop_size = tuple_range_with_level((0.20, 0.35), (0.45, 0.65), t)
|
| 298 |
+
speed = tuple_range_with_level((0.02, 0.06), (0.08, 0.15), t)
|
| 299 |
+
|
| 300 |
+
nb_iterations: 1-3 DENSITY (1,1), (1,2), (2,2), (2,3), (3,3)
|
| 301 |
+
drop_size: coarse of raindrop (corresponding to image scale), (0.01, 0.01)
|
| 302 |
+
speed: length of the raindrop (more large more length but more thin)
|
| 303 |
+
"""
|
| 304 |
+
H,W,C = image.shape
|
| 305 |
+
|
| 306 |
+
nb_iterations_level = {1: (1,1), 2: (1,2), 3: (2,2), 4: (2, 3), 5: (3, 3)}
|
| 307 |
+
drop_size_level = {1: (0.01, 0.01), 2: (0.01, 0.02), 3: (0.01, 0.02), 4: (0.01, 0.02), 5: (0.02, 0.03)}
|
| 308 |
+
|
| 309 |
+
drop_rate = W / 256
|
| 310 |
+
drop_size = list(np.array(drop_size_level[level]) * drop_rate)
|
| 311 |
+
|
| 312 |
+
speed_level = (0.04, 0.2)
|
| 313 |
+
speed_rate = H / 192
|
| 314 |
+
speed = list(np.array(speed_level) / speed_rate)
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
aug = iaa.Rain(nb_iterations=nb_iterations_level[level],
|
| 318 |
+
drop_size=drop_size,
|
| 319 |
+
speed=speed)
|
| 320 |
+
img = aug.augment_image(image)
|
| 321 |
+
|
| 322 |
+
params = {"nb_iterations": nb_iterations_level[level], "drop_size": drop_size, "speed": speed}
|
| 323 |
+
return img, params
|
| 324 |
+
|
| 325 |
+
DEG_FUNCS = {'Snow': snow, 'Fog': fog, 'Rain': rain}
|
| 326 |
+
|
| 327 |
+
# ----------------------------
|
| 328 |
+
# Main
|
| 329 |
+
# ----------------------------
|
| 330 |
+
def apply_degradation(case, image, level, seed=None):
|
| 331 |
+
if seed is not None:
|
| 332 |
+
random.seed(seed)
|
| 333 |
+
np.random.seed(seed)
|
| 334 |
+
img_out, params = DEG_FUNCS[case](image, level)
|
| 335 |
+
return img_out, params
|
| 336 |
+
|
| 337 |
+
def read_rgb(path):
|
| 338 |
+
bgr = cv2.imread(path, cv2.IMREAD_COLOR)
|
| 339 |
+
if bgr is None:
|
| 340 |
+
raise FileNotFoundError(path)
|
| 341 |
+
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
|
| 342 |
+
return rgb
|
| 343 |
+
|
| 344 |
+
def save_bgr(path, rgb):
|
| 345 |
+
bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)
|
| 346 |
+
cv2.imwrite(path, bgr)
|
| 347 |
+
|
| 348 |
+
def main():
|
| 349 |
+
parser = argparse.ArgumentParser()
|
| 350 |
+
parser.add_argument("--rt", type=str, default="/home/CORP/hsiang.chen/Project/Datasets/DataDepictQA")
|
| 351 |
+
parser.add_argument("--save_rt", type=str, default="/home/CORP/hsiang.chen/Project/Datasets/DataDepictQA")
|
| 352 |
+
parser.add_argument("--image_folder", type=str, default=None)
|
| 353 |
+
parser.add_argument("--meta_folder", type=str, default=None)
|
| 354 |
+
parser.add_argument("--case", type=str, default="Random", help="'Snow', 'Fog', 'Rain' or 'Random'")
|
| 355 |
+
parser.add_argument("--n_per_image", type=int, default=1, help="how many degraded samples per clear image")
|
| 356 |
+
parser.add_argument("--levels", type=str, default="1-5", help="level range, e.g. '3-5' or '2,3,5'")
|
| 357 |
+
parser.add_argument("--seed", type=int, default=42)
|
| 358 |
+
args = parser.parse_args()
|
| 359 |
+
|
| 360 |
+
# 構建 level 候選
|
| 361 |
+
level_tokens = [s.strip() for s in args.levels.split(",")]
|
| 362 |
+
levels = []
|
| 363 |
+
for tok in level_tokens:
|
| 364 |
+
if "-" in tok:
|
| 365 |
+
a, b = tok.split("-")
|
| 366 |
+
a, b = int(a), int(b)
|
| 367 |
+
levels.extend(list(range(min(a, b), max(a, b) + 1)))
|
| 368 |
+
else:
|
| 369 |
+
levels.append(int(tok))
|
| 370 |
+
levels = sorted(set([L for L in levels if 1 <= L <= 5]))
|
| 371 |
+
|
| 372 |
+
# 濾清晰圖
|
| 373 |
+
clear_dir = args.image_folder
|
| 374 |
+
jpg_list = glob("%s/*.jpg"%clear_dir)
|
| 375 |
+
png_list = glob("%s/*.png"%clear_dir)
|
| 376 |
+
bmp_list = glob("%s/*.bmp"%clear_dir)
|
| 377 |
+
print(len(jpg_list), len(png_list), len(bmp_list))
|
| 378 |
+
image_list = sorted(jpg_list + png_list + bmp_list)
|
| 379 |
+
|
| 380 |
+
if len(image_list) == 0:
|
| 381 |
+
print(f"No images found under {clear_dir}")
|
| 382 |
+
return
|
| 383 |
+
|
| 384 |
+
all_cases = list(DEG_FUNCS.keys())
|
| 385 |
+
|
| 386 |
+
# save folder
|
| 387 |
+
clear_folder = Path(clear_dir)
|
| 388 |
+
distortion_dir = clear_folder.with_name(clear_folder.name + "_weather")
|
| 389 |
+
meta_refA_path = os.path.join(args.meta_folder, "train_refA_weather_brief.json")
|
| 390 |
+
meta_A_path = os.path.join(args.meta_folder, "train_A_weather_brief.json")
|
| 391 |
+
|
| 392 |
+
# create to other dst folder (since the auth issues)
|
| 393 |
+
distortion_dir = os.path.join(args.save_rt, os.path.relpath(distortion_dir, args.rt))
|
| 394 |
+
os.makedirs(distortion_dir, exist_ok=True)
|
| 395 |
+
meta_refA_path = os.path.join(args.save_rt, os.path.relpath(meta_refA_path, args.rt))
|
| 396 |
+
os.makedirs(os.path.dirname(meta_refA_path), exist_ok=True)
|
| 397 |
+
meta_A_path = os.path.join(args.save_rt, os.path.relpath(meta_A_path, args.rt))
|
| 398 |
+
os.makedirs(os.path.dirname(meta_A_path), exist_ok=True)
|
| 399 |
+
|
| 400 |
+
print("="*100)
|
| 401 |
+
print(f"Found {len(image_list)} clear images from {args.image_folder}")
|
| 402 |
+
print(f"Save images in {distortion_dir}, and save annotation in {meta_A_path}, {meta_refA_path}")
|
| 403 |
+
print(f"Cases: {'ALL (random)' if args.case=='random' else args.case}, Levels: {levels}, n_per_image={args.n_per_image}, n_per_image={args.n_per_image}, seed={args.seed}")
|
| 404 |
+
print("="*100)
|
| 405 |
+
|
| 406 |
+
g_seed = args.seed
|
| 407 |
+
random.seed(g_seed)
|
| 408 |
+
np.random.seed(g_seed)
|
| 409 |
+
|
| 410 |
+
meta_refA = []
|
| 411 |
+
meta_A = []
|
| 412 |
+
|
| 413 |
+
for path in tqdm(image_list):
|
| 414 |
+
base = os.path.basename(path)
|
| 415 |
+
rgb = read_rgb(path)
|
| 416 |
+
|
| 417 |
+
save_folder = os.path.join(distortion_dir, os.path.relpath(os.path.dirname(path), clear_dir))
|
| 418 |
+
os.makedirs(save_folder, exist_ok=True)
|
| 419 |
+
|
| 420 |
+
for k in range(args.n_per_image):
|
| 421 |
+
# 隨機選 type & level
|
| 422 |
+
if args.case == "Random":
|
| 423 |
+
case = random.choice(all_cases)
|
| 424 |
+
else:
|
| 425 |
+
if args.case not in DEG_FUNCS:
|
| 426 |
+
raise ValueError(f"Unknown case: {args.case}")
|
| 427 |
+
case = args.case
|
| 428 |
+
level = random.choice(levels)
|
| 429 |
+
|
| 430 |
+
# 為了可重現,每張圖/樣本組合一個 seed
|
| 431 |
+
# 這邊用全域 seed + hash(base,k,case,level)
|
| 432 |
+
local_seed = (hash((base, k, case, level)) ^ g_seed) & 0xFFFFFFFF
|
| 433 |
+
out_img, params = apply_degradation(case, rgb, level, seed=local_seed)
|
| 434 |
+
|
| 435 |
+
# save images, 檔名:原檔名 + _case_L{level}_{k}.jpg
|
| 436 |
+
stem, ext = os.path.splitext(base)
|
| 437 |
+
out_name = f"{stem}_{case}_L{level}_{k}{ext}"
|
| 438 |
+
out_path = os.path.join(save_folder, out_name)
|
| 439 |
+
save_bgr(out_path, out_img)
|
| 440 |
+
|
| 441 |
+
# 記錄 metadata
|
| 442 |
+
meta_refA.append({
|
| 443 |
+
"distortion_class": case,
|
| 444 |
+
"distortion_name": case,
|
| 445 |
+
"severity": level,
|
| 446 |
+
"id": f"{stem}_{k}",
|
| 447 |
+
"image_ref": os.path.relpath(path, args.rt).replace("\\", "/"),
|
| 448 |
+
"image_A": os.path.relpath(out_path, args.save_rt).replace("\\", "/"),
|
| 449 |
+
"image_B": None,
|
| 450 |
+
"task_type": "quality_single_A",
|
| 451 |
+
"conversations": [
|
| 452 |
+
{
|
| 453 |
+
"from": "human",
|
| 454 |
+
"value": question_generate(ref="Full-Reference", degra="ONE"),
|
| 455 |
+
},
|
| 456 |
+
{
|
| 457 |
+
"from": "gpt",
|
| 458 |
+
"value": case
|
| 459 |
+
}
|
| 460 |
+
],
|
| 461 |
+
"params": params
|
| 462 |
+
})
|
| 463 |
+
|
| 464 |
+
meta_A.append({
|
| 465 |
+
"distortion_class": case,
|
| 466 |
+
"distortion_name": case,
|
| 467 |
+
"severity": level,
|
| 468 |
+
"id": f"{stem}_{k}",
|
| 469 |
+
"image_ref": None,
|
| 470 |
+
"image_A": os.path.relpath(out_path, args.save_rt).replace("\\", "/"),
|
| 471 |
+
"image_B": None,
|
| 472 |
+
"task_type": "quality_single_A_noref",
|
| 473 |
+
"conversations": [
|
| 474 |
+
{
|
| 475 |
+
"from": "human",
|
| 476 |
+
"value": question_generate(ref="Non-Reference", degra="ONE"),
|
| 477 |
+
},
|
| 478 |
+
{
|
| 479 |
+
"from": "gpt",
|
| 480 |
+
"value": case
|
| 481 |
+
}
|
| 482 |
+
],
|
| 483 |
+
"params": params
|
| 484 |
+
})
|
| 485 |
+
|
| 486 |
+
with open(meta_refA_path, "w") as f:
|
| 487 |
+
json.dump(meta_refA, f, indent=4)
|
| 488 |
+
|
| 489 |
+
with open(meta_A_path, "w") as f:
|
| 490 |
+
json.dump(meta_A, f, indent=4)
|
| 491 |
+
|
| 492 |
+
print(f"Done. Metadata saved to: {meta_refA_path}, {meta_A_path}")
|
| 493 |
+
|
| 494 |
+
if __name__ == "__main__":
|
| 495 |
+
main()
|