-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisdac.py
97 lines (80 loc) · 3.5 KB
/
visdac.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
from __future__ import print_function
from PIL import Image
import os
import os.path
import numpy as np
import torch.utils.data as data
class VISDAC(data.Dataset):
def __init__(self, root, imbalanced, domain, train=True, transform=None, from_file=False):
self.train = train
self.transform = transform
if domain == 'source':
domain = 'train'
elif domain == 'target':
domain = 'validation'
else:
print("Unknown domain: {}".format(domain))
if not from_file:
data = []
labels = []
if imbalanced:
if domain == 'train':# and self.train:
file_path = '../SSISFDA/data/Imbalanced/VISDA-'+imbalanced+'_RSUT/train_RS.txt'
#elif domain == 'train' and not self.train:
# file_path = '../SSISFDA/data/Imbalanced/VISDA-'+imbalanced+'_RSUT/train_UT.txt'
elif domain == 'validation':
file_path = '../SSISFDA/data/Imbalanced/VISDA-'+imbalanced+'_RSUT/validation_UT.txt'
else:
print("Unknown domain: {}".format(domain))
with open(file_path,'r') as f:
lines = f.readlines()
for line in lines:
path, label = line.split(" ")
sample = os.path.join(root, "/".join(path.split("/")[-3:]))
data.append(sample)
labels.append(int(label))
else:
if domain == 'train':# and self.train:
file_path = '../SSISFDA/data/Imbalanced/VISDA-C/image_list_train.txt'
elif domain == 'validation':
file_path = '../SSISFDA/data/Imbalanced/VISDA-C/image_list_val.txt'
else:
print("Unknown domain: {}".format(domain))
with open(file_path,'r') as f:
lines = f.readlines()
for line in lines:
path, label = line.split(" ")
sample = os.path.join(root, "/".join(path.split("/")[-3:]))
data.append(sample)
labels.append(int(label))
np.random.seed(1234)
idx = np.random.permutation(len(data))
self.data = np.array(data)[idx]
self.labels = np.array(labels)[idx]
test_perc = 20
test_len = len(self.data)*test_perc//100
if self.train:
self.data = self.data[test_len:]
self.labels = self.labels[test_len:]
else:
self.data = self.data[:test_len]
self.labels = self.labels[:test_len]
else:
self.data = np.load(os.path.join(root, domain+"_imgs.npy"))
self.labels = np.load(os.path.join(root, domain+"_labels.npy"))
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[index], self.labels[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.open(img)
if self.transform is not None:
img = self.transform(img)
return img, target, index
def __len__(self):
return len(self.data)