-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
executable file
·127 lines (103 loc) · 3.51 KB
/
dataset.py
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
#
#Copyright (C) 2020-2021 ISTI-CNR
#Licensed under the BSD 3-Clause Clear License (see license.txt)
#
import os
import pandas as pd
import torch
from util import load_image, torchDataAugmentation
from torch.utils.data import Dataset
from sklearn.model_selection import train_test_split
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
#
#
#
def checkFile(base_dir, fn):
full_name = os.path.join(base_dir, fn)
return os.path.isfile(full_name)
#
#
#
def read_data_split(data_dir, group=None, bPrecompGroup=True):
train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
train.sort_values(by=['Distorted'], inplace=True)
val = pd.read_csv(os.path.join(data_dir, 'val.csv'))
val.sort_values(by=['Distorted'], inplace=True)
test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
test.sort_values(by=['Distorted'], inplace=True)
return train, val, test
#
#
#
def split_data(data_dir, random_state=42, group=None, groupaffine = 1):
data = os.path.join(data_dir, 'data.csv')
data = pd.read_csv(data)
if group:
print('Grouping')
if groupaffine > 1:
print('Groups transformations are online')
n = len(data)
img_fn = []
q_val = []
lmax = []
gpa = []
for i in range(0, n):
tmp0 = data.iloc[i].Distorted
tmp1 = data.iloc[i].Q
tmp2 = data.iloc[i].Lmax
for j in range(0, groupaffine):
img_fn.append(tmp0)
q_val.append(tmp1)
lmax.append(tmp2)
gpa.append(j)
d = {'Distorted': img_fn, 'Lmax': lmax, 'Q': q_val, 'I': gpa}
data = pd.DataFrame(data=d)
group = group * groupaffine
else:
print('Groups are precomputed')
data = [data[i:i + group] for i in range(0, len(data), group)]
else:
print('No grouping')
#split data into 80% train, 10% validation, and 10% test
train, valtest = train_test_split(data, test_size=0.2, random_state=random_state)
val, test = train_test_split(valtest, test_size=0.5, random_state=random_state)
train = pd.concat(train)
val = pd.concat(val)
test = pd.concat(test)
return train, val, test
class HdrVdpDataset(Dataset):
#
#
#
def __init__(self, data, base_dir, group = None, groupaffine=1, bScaling = False, colorspace = 'REC709', color = 'gray'):
self.data = data
self.base_dir = base_dir
self.group = group
self.bScaling = bScaling
self.colorspace = colorspace
self.groupaffine = groupaffine
self.bGrayscale = (color == 'gray')
if self.bScaling:
print('Scaling is active')
else:
print('Scaling is disabled')
#
#
#
def __getitem__(self, index):
sample = self.data.iloc[index]
full_name = os.path.join(self.base_dir, sample.Distorted)
#print(full_name)
stim = load_image(full_name, maxClip = sample.Lmax, grayscale = self.bGrayscale, colorspace = self.colorspace)
if self.group != None:
if self.groupaffine > 1:
stim = torchDataAugmentation(stim, sample.I)
if self.bScaling:
q = torch.FloatTensor([sample.Q / 100.0])
else:
q = torch.FloatTensor([sample.Q])
return stim, q
def __len__(self):
return len(self.data)