Spaces:
Running
on
Zero
Running
on
Zero
File size: 11,130 Bytes
2cda712 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# Importing Libraires
import numpy as np
import pandas as pd
import math
import scipy
from PIL import Image
import random
import torch
import torchvision
from torchvision import transforms
import os,sys,warnings
warnings.filterwarnings("ignore")
from tqdm import tqdm
import pathlib
import functions.utils as utils
import defaults
# Saving Images Paths for UnivFD dataset
def save_all_images_path_UnivFD(
imgs_dir:str,
status:str,
save_path:str,
replace:bool
):
if os.path.exists(save_path) == False or replace == True:
# Dataset
dataset_images_paths = {}
# For each UnivFD image-source for the given status
for _,source in tqdm(enumerate(defaults.All_UnivFD_Sources[status])):
dataset_images_paths[source] = {}
# Initializing
for label in ["fake", "real"]:
dataset_images_paths[source][label] = []
# Images Source Directory
source_images_dir = os.path.join(imgs_dir, status, source)
# For each label or category
if ("0_real" in os.listdir(source_images_dir)) and ("1_fake" in os.listdir(source_images_dir)):
# For each label
for _,label in enumerate(os.listdir(source_images_dir)):
if label == "0_real":
img_label = "real"
elif label == "1_fake":
img_label = "fake"
else:
assert False, "Unknown Label encountered."
for fname in os.listdir(os.path.join(source_images_dir, label)):
# Image Path and Label
img_path = os.path.join(source_images_dir, label, fname)
# Appending
dataset_images_paths[source][img_label].append(os.path.relpath(img_path, defaults.main_dataset_dir))
else:
# For each category
for _,category in enumerate(os.listdir(source_images_dir)):
# For each label
for _,label in enumerate(os.listdir(os.path.join(source_images_dir, category))):
if label == "0_real":
img_label = "real"
elif label == "1_fake":
img_label = "fake"
else:
assert False, "Unknown Label encountered."
for fname in os.listdir(os.path.join(source_images_dir, category, label)):
# Image Path and Label
img_path = os.path.join(source_images_dir, category, label, fname)
# Appending
dataset_images_paths[source][img_label].append(os.path.relpath(img_path, defaults.main_dataset_dir))
# Saving
np.save(save_path, dataset_images_paths)
# Saving Images Paths for GenImage dataset
def save_all_images_path_GenImage(
imgs_dir:str,
status:str,
save_path:str,
replace:bool
):
if os.path.exists(save_path) == False or replace == True:
# Dataset
dataset_images_paths = {}
# For each GenImage image-source for the given status
for _,source in tqdm(enumerate(defaults.All_GenImage_Sources[status])):
dataset_images_paths[source] = {}
# Initializing
for label in ["fake", "real"]:
dataset_images_paths[source][label] = []
# Images Source Directory
source_images_dir = os.path.join(imgs_dir, source, status)
# For each label
for _,label in enumerate(os.listdir(source_images_dir)):
if label == "nature":
img_label = "real"
elif label == "ai":
img_label = "fake"
elif (label == "ai_reconstructed_inpainting" or label == "nature_reconstructed_inpainting") and status == "train" and source == "sdv4":
print ("Encountered label:{} for status:{} and source:{}".format(label, status, source))
img_label = "fake"
else:
assert False, "Unknown Label encountered."
for fname in os.listdir(os.path.join(source_images_dir, label)):
# Image Path and Label
img_path = os.path.join(source_images_dir, label, fname)
# Appending
dataset_images_paths[source][img_label].append(os.path.relpath(img_path, defaults.main_dataset_dir))
# Saving
np.save(save_path, dataset_images_paths)
# Saving Images Paths for DRCT dataset
def save_all_images_path_DRCT(
imgs_dir:str,
status:str,
save_path:str,
replace:bool
):
if os.path.exists(save_path) == False or replace == True:
# Dataset
dataset_images_paths = {}
# For Training Dataset: Real Images, Fake Images, Real Reconstructed Images and Fake Reconstructed Imagees
if status == "train":
# For each image-source
for _,source in tqdm(enumerate(defaults.All_DRCT_Sources[status])):
dataset_images_paths[source] = {}
# Initializing
for label in ["fake", "real"]:
dataset_images_paths[source][label] = []
# Images Source Directory
real_images_dir = os.path.join(imgs_dir, "real_images", "{}2017".format(status))
fake_images_dirs = [
os.path.join(imgs_dir, "fake_images", source, "{}2017".format(status)),
os.path.join(imgs_dir, "fake_reconstructed_images", source, "{}2017".format(status)),
os.path.join(imgs_dir, "real_reconstructed_images", source, "{}2017".format(status)),
]
# Real Images Paths
img_label = "real"
for fname in os.listdir(real_images_dir):
# Image Path and Label
img_path = os.path.join(real_images_dir, fname)
# Appending
dataset_images_paths[source][img_label].append(os.path.relpath(img_path, defaults.main_dataset_dir))
# Fake Images Paths
img_label = "fake"
for i in range(len(fake_images_dirs)):
for fname in os.listdir(fake_images_dirs[i]):
# Image Path and Label
img_path = os.path.join(fake_images_dirs[i], fname)
# Appending
dataset_images_paths[source][img_label].append(os.path.relpath(img_path, defaults.main_dataset_dir))
# For Validation Dataset: Real Images, Fake Images
else:
# For each image-source
for _,source in tqdm(enumerate(defaults.All_DRCT_Sources[status])):
dataset_images_paths[source] = {}
# Initializing
for label in ["fake", "real"]:
dataset_images_paths[source][label] = []
# Images Source Directory
real_images_dir = os.path.join(imgs_dir, "real_images", "{}2017".format(status))
fake_images_dir = os.path.join(imgs_dir, "fake_images", source, "{}2017".format(status))
# Real Images Paths
img_label = "real"
for fname in os.listdir(real_images_dir):
# Image Path and Label
img_path = os.path.join(real_images_dir, fname)
# Appending
dataset_images_paths[source][img_label].append(os.path.relpath(img_path, defaults.main_dataset_dir))
# Fake Images Paths
img_label = "fake"
for fname in os.listdir(fake_images_dir):
# Image Path and Label
img_path = os.path.join(fake_images_dir, fname)
# Appending
dataset_images_paths[source][img_label].append(os.path.relpath(img_path, defaults.main_dataset_dir))
# Saving
np.save(save_path, dataset_images_paths)
# Saving all paths of image dataset
def save_all_images_paths(
imgs_dir:str,
dataset_type:str,
status:str,
save_path:str,
replace:bool
):
"""
Saves path info images of a dataset_type, status, image_sources.
Args:
imgs_dir (str): Directory of images.
dataset_type (str): Type of Dataset. Options: ["UnivFD", "GenImage", "DRCT]
status (str): ["train", "val"]
save_path (str): Path to save .npy file.
replace (bool): Replace File if True.
"""
# Assertions
assert dataset_type in ["UnivFD", "GenImage", "DRCT"], "Invalid dataset"
assert os.path.exists(imgs_dir), f"Image directory {imgs_dir} is not found."
assert status in ["train", "val"], "Invalid status"
if dataset_type == "UnivFD":
save_all_images_path_UnivFD(
imgs_dir=imgs_dir,
status=status,
save_path=save_path,
replace=replace
)
elif dataset_type == "GenImage":
save_all_images_path_GenImage(
imgs_dir=imgs_dir,
status=status,
save_path=save_path,
replace=replace
)
else:
save_all_images_path_DRCT(
imgs_dir=imgs_dir,
status=status,
save_path=save_path,
replace=replace
)
# Get Images Paths
def get_image_paths(
dataset_type:str,
status:str,
image_sources:str,
label:str,
):
"""
Get path to all images in the folder based on arguments.
Args:
dataset_type (str): Type of Dataset. Options: ["UnivFD", "GenImage", "DRCT]
status (str): ["train", "val"]
image_sources (list): Image-Sources to consider for dataset.
label (str): ["real", "fake"]
"""
# Assertions
assert dataset_type in ["UnivFD", "GenImage", "DRCT"], "Invalid dataset"
assert status in ["train", "val"], "Invalid status"
assert label in ["real", "fake"], "Invalid label"
# Loading Paths
img_dir = os.path.join(defaults.main_dataset_dir, dataset_type, "dataset")
info_path = os.path.join(defaults.main_dataset_dir, "Info", "{}_{}_image_Paths.npy".format(dataset_type, status))
# Saving Info File
if os.path.exists(info_path) == False:
print ("Saving Info File")
save_all_images_paths(
imgs_dir=img_dir,
dataset_type=dataset_type,
status=status,
save_path=info_path,
replace=False
)
# Loading Path Info
Path_Info = np.load(info_path, allow_pickle=True)[()]
# Dataset
dataset_images_paths = []
# For each image-source
for _, source in enumerate(image_sources):
for img_path in sorted(Path_Info[source][label]):
# Image-Path
dataset_images_paths.append(img_path)
return dataset_images_paths
# Dataset Paths
def dataset_img_paths(
dataset_type:str,
status:str
):
"""
Returns real_image_paths and fake_image_paths based on arguments.
Args:
dataset_type (str): Type of Dataset. Options: ["UnivFD", "GenImage", "DTCT]
status (str): ["train", "val"]
"""
# Assertions
assert dataset_type in ["UnivFD", "GenImage", "DRCT"], "Invalid dataset"
assert status in ["train", "val"], "Invalid status"
# DRCT Dataset
if dataset_type == "DRCT":
train_image_sources, test_image_sources = utils.get_DRCT_options()
if status == "train":
image_sources = train_image_sources
else:
image_sources = test_image_sources
real_images_paths = get_image_paths(
dataset_type=dataset_type,
status=status,
image_sources=image_sources,
label="real"
)
fake_images_paths = get_image_paths(
dataset_type=dataset_type,
status=status,
image_sources=image_sources,
label="fake"
)
# GenImage Dataset
elif dataset_type == "GenImage":
train_image_sources, test_image_sources = utils.get_GenImage_options()
if status == "train":
image_sources = train_image_sources
else:
image_sources = test_image_sources
real_images_paths = get_image_paths(
dataset_type=dataset_type,
status=status,
image_sources=image_sources,
label="real"
)
fake_images_paths = get_image_paths(
dataset_type=dataset_type,
status=status,
image_sources=image_sources,
label="fake"
)
# UnivFD Dataset
elif dataset_type == "UnivFD":
train_image_sources, test_image_sources = utils.get_UnivFD_options()
if status == "train":
image_sources = train_image_sources
else:
image_sources = test_image_sources
real_images_paths = get_image_paths(
dataset_type=dataset_type,
status=status,
image_sources=image_sources,
label="real"
)
fake_images_paths = get_image_paths(
dataset_type=dataset_type,
status=status,
image_sources=image_sources,
label="fake"
)
else:
assert False, "Unknown dataset_type: {}".format(dataset_type)
return real_images_paths, fake_images_paths |