-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataset.py
136 lines (109 loc) · 4.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
128
129
130
131
132
133
134
135
136
import cv2 as cv
import numpy as np
import torch
import os
import random
import albumentations as A
import torchvision
# Set random seed for reproducibility
manualSeed = 999
# manualSeed = random.randint(1, 10000) # use if you want new results
print("Random Seed: ", manualSeed)
random.seed(manualSeed)
torch.manual_seed(manualSeed)
class Dataset(torch.utils.data.Dataset):
def __init__(self, dataset='../Data/Dataset', setting='train', sim=True, original=False):
self.path = dataset
self.classes = os.listdir(self.path)
self.interferograms = []
self.interferograms_normal = []
self.interferograms_deformation = []
self.sim = sim
self.original = original
self.oversampling = True
for data_class in self.classes:
images = os.listdir(self.path + '/' + data_class)
for image in images:
if 'ipynb' in image:
continue
image_dict = {'path': self.path + '/' + data_class + '/' + image, 'label': data_class}
self.interferograms.append(image_dict)
if int(data_class) == 0:
self.interferograms_normal.append(image_dict)
else:
self.interferograms_deformation.append(image_dict)
self.num_examples = len(self.interferograms)
self.set = setting
def __len__(self):
return self.num_examples
def __getitem__(self, index):
if self.set == 'train' and self.sim == False and self.oversampling:
#print('Oversampling')
choice = random.randint(0, 10)
buffer = False
if choice % 2 != 0:
choice_normal = random.randint(0, len(self.interferograms_normal) - 1)
image_data = self.interferograms_normal[choice_normal]
else:
choice_deform = random.randint(0, len(self.interferograms_deformation) - 1)
image_data = self.interferograms_deformation[choice_deform]
else:
image_data = self.interferograms[index]
image_file = image_data['path']
image_label = image_data['label']
image = cv.imread(image_file)
zero = np.zeros_like(image)
if image is None:
print(image_file)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
original = image
original = original[:224, :224, :]
zero[:, :, 0] = gray
zero[:, :, 1] = gray
zero[:, :, 2] = gray
image = zero
image = image[:224, :224, :]
image = torch.from_numpy(image).float().permute(2, 0, 1)
original = torch.from_numpy(original).float().permute(2, 0, 1)
image = torchvision.transforms.Normalize((108.6684,108.6684, 108.6684), (109.1284, 109.1284, 109.1284))(image)
if image.shape[1] < 224 or image.shape[2] < 224:
print(image_file)
if self.original:
return (image, image, original), int(image_label), image_file
return (image, original), int(image_label)
class Unlabeled(torch.utils.data.Dataset):
def __init__(self, dataset='../Data/Dataset', setting='train', original=False):
self.path = dataset
self.images = os.listdir(self.path)
self.interferograms = []
self.original = original
for image in self.images:
image_dict = {'path': self.path + '/' + image}
self.interferograms.append(image_dict)
self.num_examples = len(self.interferograms)
self.set = setting
def __len__(self):
return self.num_examples
def __getitem__(self, index):
image_data = self.interferograms[index]
image_file = image_data['path']
image = cv.imread(image_file)
zero = np.zeros_like(image)
if image is None:
print(image_file)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
original = image
original = original[:224, :224, :]
zero[:, :, 0] = gray
zero[:, :, 1] = gray
zero[:, :, 2] = gray
image = zero
image = image[:224, :224, :]
image = torch.from_numpy(image).float().permute(2, 0, 1)
original = torch.from_numpy(original).float().permute(2, 0, 1)
image = torchvision.transforms.Normalize((108.6684,108.6684, 108.6684), (109.1284, 109.1284, 109.1284))(image)
if image.shape[1] < 224 or image.shape[2] < 224:
print(image_file)
if self.original:
return (image, image, original), 0, image_file
return (image, original), 0