File size: 34,406 Bytes
28c2184 | 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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 | import os
import torch
import functools
import numpy as np
import pandas as pd
from PIL import Image, ImageFile
from torch.utils.data import Dataset
from tqdm import tqdm
import re
IMG_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif']
ImageFile.LOAD_TRUNCATED_IMAGES = True
def has_file_allowed_extension(filename, extensions):
"""Checks if a file is an allowed extension.
Args:
filename (string): path to a file
extensions (iterable of strings): extensions to consider (lowercase)
Returns:
bool: True if the filename ends with one of given extensions
"""
filename_lower = filename.lower()
return any(filename_lower.endswith(ext) for ext in extensions)
def image_loader(image_name):
if has_file_allowed_extension(image_name, IMG_EXTENSIONS):
I = Image.open(image_name)
return I.convert('RGB')
def get_default_img_loader():
return functools.partial(image_loader)
class ImageDataset2(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
self.data = pd.read_csv(csv_file, sep='\t', header=None)
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
I = self.loader(image_name)
I = self.preprocess(I)
I = I.unsqueeze(0)
n_channels = 3
kernel_h = 224
kernel_w = 224
if (I.size(2) >= 1024) | (I.size(3) >= 1024):
step = 48
else:
step = 32
patches = I.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
assert patches.size(0) >= self.num_patch
#self.num_patch = np.minimum(patches.size(0), self.num_patch)
if self.test:
sel_step = patches.size(0) // self.num_patch
sel = torch.zeros(self.num_patch)
for i in range(self.num_patch):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(self.num_patch, ))
patches = patches[sel, ...]
mos = self.data.iloc[index, 1]
dist_type = self.data.iloc[index, 2]
scene_content1 = self.data.iloc[index, 3]
scene_content2 = self.data.iloc[index, 4]
scene_content3 = self.data.iloc[index, 5]
if scene_content2 == 'invalid':
valid = 1
elif scene_content3 == 'invalid':
valid = 2
else:
valid = 3
sample = {'I': patches, 'mos': float(mos), 'dist_type': dist_type, 'scene_content1': scene_content1,
'scene_content2':scene_content2, 'scene_content3':scene_content3, 'valid':valid}
return sample
def __len__(self):
return len(self.data.index)
class ImageDataset_qonly(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
if csv_file[-3:] == 'txt':
data = pd.read_csv(csv_file, sep='\t', header=None)
self.data = data
self.mos_col = 1
elif csv_file[-4:] == 'xlsx':
data = pd.read_excel(csv_file, header=0)
self.data = data
self.mos_col = 1
else:
data = pd.read_csv(csv_file, header=0)
if ('split' in data.columns) & (set != 3):
self.data = data[data.split==set]
else:
self.data = data
self.mos_col = 1
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
image_name = image_name.replace('\\', '/')
I = self.loader(image_name)
I = self.preprocess(I)
I = I.unsqueeze(0)
n_channels = 3
kernel_h = 224
kernel_w = 224
if (I.size(2) >= 1024) | (I.size(3) >= 1024):
step = 48
else:
step = 32
patches = I.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
assert patches.size(0) >= self.num_patch
#self.num_patch = np.minimum(patches.size(0), self.num_patch)
if self.test:
sel_step = patches.size(0) // self.num_patch
sel = torch.zeros(self.num_patch)
for i in range(self.num_patch):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(self.num_patch, ))
patches = patches[sel, ...]
mos = self.data.iloc[index, self.mos_col]
if self.data.shape[1] == 23: #llie
distortions = self.data.iloc[index, self.mos_col+1::2]
distortions = distortions.to_numpy(dtype=float)
distortions = torch.from_numpy(distortions)
else:
distortions = 0
sample = {'I': patches, 'mos': float(mos), 'dists':distortions}
return sample
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data.index)
class ImageDataset_llie(Dataset):
def __init__(self, csv_file,
img_dir,
spatialFeat,
preprocess,
num_patch,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
if csv_file[-3:] == 'txt':
data = pd.read_csv(csv_file, sep='\t', header=None)
self.data = data
self.mos_col = 1
elif csv_file[-4:] == 'xlsx':
data = pd.read_excel(csv_file, header=0)
self.data = data
self.mos_col = 1
else:
data = pd.read_csv(csv_file, header=0)
if ('split' in data.columns) & (set != 3):
self.data = data[data.split==set]
else:
self.data = data
self.mos_col = 1
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
self.spatialFeat = spatialFeat
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
image_name = image_name.replace('\\', '/')
I = self.loader(image_name)
I = self.preprocess(I)
tmp = image_name.split('/')[-1]
tmp = tmp.split('.')[0]
spatial_feat = torch.from_numpy(np.load(os.path.join(self.spatialFeat, f'{tmp}.npy'))).view(-1)
I = I.unsqueeze(0)
n_channels = 3
kernel_h = 224
kernel_w = 224
if (I.size(2) >= 1024) | (I.size(3) >= 1024):
step = 48
else:
step = 32
patches = I.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
assert patches.size(0) >= self.num_patch
self.num_patch = np.minimum(patches.size(0), self.num_patch)
if self.test:
sel_step = patches.size(0) // self.num_patch
sel = torch.zeros(self.num_patch)
for i in range(self.num_patch):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(self.num_patch, ))
patches = patches[sel, ...]
mos = self.data.iloc[index, self.mos_col]
if self.data.shape[1] == 23: #llie
distortions = self.data.iloc[index, self.mos_col+1::2]
distortions = distortions.to_numpy(dtype=float)
distortions = torch.from_numpy(distortions)
else:
distortions = 0
sample = {'I': patches, 'spatial_feat':spatial_feat, 'mos': float(mos), 'dists':distortions}
return sample
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data.index)
class ImageDataset_llie_naflex(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
if csv_file[-3:] == 'txt':
data = pd.read_csv(csv_file, sep='\t', header=None)
self.data = data
self.mos_col = 1
elif csv_file[-4:] == 'xlsx':
data = pd.read_excel(csv_file, header=0)
self.data = data
self.mos_col = 1
else:
data = pd.read_csv(csv_file, header=0)
if ('split' in data.columns) & (set != 3):
self.data = data[data.split==set]
else:
self.data = data
self.mos_col = 1
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
image_name = image_name.replace('\\', '/')
I = self.loader(image_name)
mos = self.data.iloc[index, self.mos_col]
if self.data.shape[1] == 23: #llie
distortions = self.data.iloc[index, self.mos_col+1::2]
distortions = distortions.to_numpy(dtype=float)
distortions = torch.from_numpy(distortions)
else:
distortions = 0
#sample = {'I': I, 'mos': float(mos), 'dists':distortions}
return I, float(mos), distortions
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data.index)
class ImageDataset_sr_naflex(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
data = pd.read_excel(csv_file, header=0)
self.data = data
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, 'SR', self.data.iloc[index, 0])
image_name = image_name.replace('\\', '/')
im_name = self.data.iloc[index, 0]
I = self.loader(image_name)
mos = self.data.iloc[index, 3]
return I, float(mos)
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data.index)
class ImageDataset_diqa_naflex(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
data = pd.read_csv(csv_file, header=0)
self.data = data
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, 'res', self.data.iloc[index, 0])
image_name = image_name.replace('\\', '/')
I = self.loader(image_name)
if self.data.shape[1] == 5:
image_name2 = os.path.join(self.img_dir, 'ori', self.data.iloc[index, 1])
image_name2 = image_name.replace('\\', '/')
I_ref = self.loader(image_name2)
overall_mos = 0.8*self.data.iloc[index, 2] + 1
sharp_mos = 0.8*self.data.iloc[index, 3] + 1
color_mos = 0.8*self.data.iloc[index, 4] + 1
else:
I_ref = I
overall_mos = 0.8*self.data.iloc[index, 1] + 1
sharp_mos = 0.8*self.data.iloc[index, 2] + 1
color_mos = 0.8*self.data.iloc[index, 3] + 1
return I, I_ref, float(overall_mos), float(sharp_mos), float(color_mos)
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data.index)
class ImageDataset_llie2(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
if csv_file[-3:] == 'txt':
data = pd.read_csv(csv_file, sep='\t', header=None)
self.data = data
self.mos_col = 1
elif csv_file[-4:] == 'xlsx':
data = pd.read_excel(csv_file, header=0)
self.data = data
self.mos_col = 1
else:
data = pd.read_csv(csv_file, header=0)
if ('split' in data.columns) & (set != 3):
self.data = data[data.split==set]
else:
self.data = data
self.mos_col = 1
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
image_name = image_name.replace('\\', '/')
I = self.loader(image_name)
I = self.preprocess(I)
I = I.unsqueeze(0)
n_channels = 3
kernel_h = 224
kernel_w = 224
if (I.size(2) >= 1024) | (I.size(3) >= 1024):
step = 48
else:
step = 32
patches = I.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
assert patches.size(0) >= self.num_patch
self.num_patch = np.minimum(patches.size(0), self.num_patch)
if self.test:
sel_step = patches.size(0) // self.num_patch
sel = torch.zeros(self.num_patch)
for i in range(self.num_patch):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(self.num_patch, ))
patches = patches[sel, ...]
mos = self.data.iloc[index, self.mos_col]
if self.data.shape[1] == 23: #llie
distortions = self.data.iloc[index, self.mos_col+1::2]
distortions = distortions.to_numpy(dtype=float)
distortions = torch.from_numpy(distortions)
else:
distortions = 0
sample = {'I': patches, 'mos': float(mos), 'dists':distortions}
return sample
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data.index)
class ImageDataset_pseudo_label(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
set,
test,
pseudo_label,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
"""
self.data = pd.read_csv(csv_file, header=None)
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.pseudo_label = pseudo_label
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: A list of dicts with keys 'I' and 'mos'
"""
image_name = self.data.iloc[index, 0]
labels = []
all_patches = []
methods = list(self.pseudo_label.keys())
for method in methods:
if method == 'GT':
llie_name = image_name
elif method == 'NeRCo':
llie_name = method + '_' + image_name[:-4] + '_fake_B.png'
else:
llie_name = method + '_' + image_name
image_path = os.path.join(self.img_dir, method, llie_name)
I = self.loader(image_path)
I = self.preprocess(I)
label = self.pseudo_label[method]
I = I.unsqueeze(0)
n_channels = 3
kernel_h = 224
kernel_w = 224
if (I.size(2) >= 1024) | (I.size(3) >= 1024):
step = 48
else:
step = 32
patches = I.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
assert patches.size(0) >= self.num_patch
self.num_patch = np.minimum(patches.size(0), self.num_patch)
if self.test:
sel_step = patches.size(0) // self.num_patch
sel = torch.zeros(self.num_patch)
for i in range(self.num_patch):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(self.num_patch,))
patches = patches[sel, ...]
labels.append(label)
all_patches.append(patches)
I = torch.cat(all_patches, dim=0)
labels = torch.tensor(labels)
sample = {'I': I, 'mos': labels}
return sample
def __len__(self):
return len(self.data.index)
# level = {'mild':0, 'moderate':1, 'severe': 2}
#
# tone_issues = {'global over-exposure':0, 'global under-exposure':1, 'global reverse-tone':2, 'global hazy': 3,
# 'global high-contrast': 4, 'global low-exposure':5, 'local over-exposure': 6, 'local under-exposure': 7,
# 'local hazy': 8, 'local high-contrast': 9, 'local low-contrast': 10}
#
# color_issues = {'global yellow tint':0, 'global cold tint':1, 'global green tint':2, 'global red tint': 3,
# 'global yellow-green tint': 4, 'global purple tint':5, 'global cyan tint': 6, 'global over-saturated': 7,
# 'global under-saturated': 8, 'local yellow tint':9, 'local cold tint':10, 'local green tint':11,
# 'local red tint': 12, 'local yellow-green tint': 13, 'local purple tint':14, 'local cyan tint': 15,
# 'local over-saturated': 16,'local under-saturated': 17, 'local magenta tint':18, 'local blue tint':19}
#
# local_areas = {'highlight area':0, 'bright area':1, 'mid-dark area':2, 'dark area':3, 'black area':4, 'human area':5,
# 'face area':6, 'hair area':7, 'cloth area':8, 'plant area': 9, 'sky area': 10, 'ground area': 11,
# 'water area': 12, 'lamp area':13, 'background area':13, 'background shadows':14, 'no area':15}
#
# tasks = {'tone':0, 'color':1}
#
# scene = {'food':0, 'mixed-light':1, 'outdoor':2, 'indoor':3, 'sunset':4, 'blue tone': 5, 'nighttime':6}
class ImageDataset_oppo(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
num_patch,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
self.data = pd.read_csv(csv_file, header=0)
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.num_patch = num_patch
self.test = test
def __convertnan__(self, value):
if pd.isna(value):
value = 'free'
return value
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
I = self.loader(image_name)
I = self.preprocess(I)
I = I.unsqueeze(0)
n_channels = 3
kernel_h = 224
kernel_w = 224
if (I.size(2) >= 1024) | (I.size(3) >= 1024):
step = 48
else:
step = 32
patches = I.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
assert patches.size(0) >= self.num_patch
#self.num_patch = np.minimum(patches.size(0), self.num_patch)
if self.test:
sel_step = patches.size(0) // self.num_patch
sel = torch.zeros(self.num_patch)
for i in range(self.num_patch):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(self.num_patch, ))
patches = patches[sel, ...]
scene = self.data.iloc[index, 1]
mode = self.data.iloc[index, 2]
focal_length = self.data.iloc[index, 3]
compare_x200p = self.data.iloc[index, 4]
tone_level = self.__convertnan__(self.data.iloc[index, 5])
tone_global_issue = self.__convertnan__(self.data.iloc[index, 6])
tone_local_issue = self.__convertnan__(self.data.iloc[index, 7])
tone_local_issue_region = self.__convertnan__(self.data.iloc[index, 8])
color_level = self.__convertnan__(self.data.iloc[index, 9])
color_global_issue = self.__convertnan__(self.data.iloc[index, 10])
color_local_issue = self.__convertnan__(self.data.iloc[index, 11])
color_local_issue_region = self.__convertnan__(self.data.iloc[index, 12])
sample = {'I': patches, 'scene': scene.lower(), 'mode': mode.lower(), 'focal_length':focal_length.lower(), 'compare_x200p':compare_x200p.lower(),
'tone_level':tone_level.lower(), 'tone_global_issue':tone_global_issue.lower(), 'tone_local_issue':tone_local_issue.lower(),
'tone_local_issue_region':tone_local_issue_region.lower(), 'color_level':color_level.lower(),
'color_global_issue':color_global_issue.lower(), 'color_local_issue':color_local_issue.lower(),
'color_local_issue_region':color_local_issue_region.lower()}
return sample
def __len__(self):
return len(self.data.index)
class ImageDataset_llie_general(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
if csv_file[-3:] == 'txt':
data = pd.read_csv(csv_file, sep='\t', header=None)
self.data = data
self.mos_col = 1
elif csv_file[-4:] == 'xlsx':
data = pd.read_excel(csv_file, header=0)
self.data = data
self.mos_col = 1
else:
data = pd.read_csv(csv_file, header=0)
if ('split' in data.columns) & (set != 3):
self.data = data[data.split==set]
else:
self.data = data
self.mos_col = 1
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess = preprocess
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
image_name = image_name.replace('\\', '/')
I = self.loader(image_name)
I = self.preprocess(I)
mos = self.data.iloc[index, self.mos_col]
sample = {'I': I, 'mos': float(mos)}
return sample
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data.index)
class ImageDataset_ms(Dataset):
def __init__(self, csv_file,
img_dir,
preprocess1,
preprocess2,
preprocess3,
num_patch,
set,
test,
get_loader=get_default_img_loader):
"""
Args:
csv_file (string): Path to the csv file with annotations.
img_dir (string): Directory of the images.
transform (callable, optional): transform to be applied on a sample.
"""
if csv_file[-3:] == 'txt':
data = pd.read_csv(csv_file, sep='\t', header=None)
self.data = data
self.mos_col = 1
elif csv_file[-4:] == 'xlsx':
data = pd.read_excel(csv_file, header=0)
self.data = data
self.mos_col = 1
else:
data = pd.read_csv(csv_file, header=0)
if ('split' in data.columns) & (set != 3):
self.data = data[data.split==set]
else:
self.data = data
self.mos_col = 1
print('%d csv data successfully loaded!' % self.__len__())
self.img_dir = img_dir
self.loader = get_loader()
self.preprocess1 = preprocess1
self.preprocess2 = preprocess2
self.preprocess3 = preprocess3
self.num_patch = num_patch
self.test = test
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
samples: a Tensor that represents a video segment.
"""
image_name = os.path.join(self.img_dir, self.data.iloc[index, 0])
I = self.loader(image_name)
num_patch_per_scale = (self.num_patch - 1) // 2
I1 = self.preprocess1(I)
I1 = I1.unsqueeze(0)
I2 = self.preprocess1(I)
I2 = I2.unsqueeze(0)
I3 = self.preprocess1(I)
I3 = I3.unsqueeze(0)
I_global = I1
n_channels = 3
kernel_h = 224
kernel_w = 224
all_patches = [I_global] # insert global resized image (PaQ-2-PiQ)
step = 16
patches = I2.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
if self.test:
sel_step = patches.size(0) // num_patch_per_scale
sel = torch.zeros(num_patch_per_scale)
for i in range(num_patch_per_scale):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(num_patch_per_scale,))
patches = patches[sel, ...]
all_patches.append(patches)
step = 32
patches = I3.unfold(2, kernel_h, step).unfold(3, kernel_w, step).permute(0, 2, 3, 1, 4, 5).reshape(-1,
n_channels,
kernel_h,
kernel_w)
if self.test:
sel_step = patches.size(0) // num_patch_per_scale
sel = torch.zeros(num_patch_per_scale)
for i in range(num_patch_per_scale):
sel[i] = sel_step * i
sel = sel.long()
else:
sel = torch.randint(low=0, high=patches.size(0), size=(num_patch_per_scale,))
patches = patches[sel, ...]
all_patches.append(patches)
all_patches = torch.cat(all_patches, 0)
mos = self.data.iloc[index, 1]
if self.data.shape[1] == 23: # llie
distortions = self.data.iloc[index, self.mos_col + 1::2]
distortions = distortions.to_numpy(dtype=float)
distortions = torch.from_numpy(distortions)
else:
distortions = 0
sample = {'I': all_patches, 'mos': float(mos), 'dists': distortions}
return sample
def __len__(self):
return len(self.data)
|