-
Notifications
You must be signed in to change notification settings - Fork 15
/
dataloader.py
149 lines (101 loc) · 3.18 KB
/
dataloader.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
137
138
139
140
141
142
143
144
145
146
147
148
import torch
import torch.utils.data as data
import numpy as np
import cv2
from PIL import Image
import glob
import os
import random
def populateTrainList(folderPath):
folderList_pre = [x[0] for x in os.walk(folderPath)]
folderList = []
trainList = []
for folder in folderList_pre:
if folder[-3:] == '240':
folderList.append(folder + "/" + folder.split("/")[-2])
for folder in folderList:
imageList = sorted(glob.glob(folder + '/' + '*.jpg'))
for i in range(0, len(imageList), 12):
tmp = imageList[i:i+12]
if len(tmp) == 12:
trainList.append(imageList[i:i+12])
return trainList
def populateTrainList2(folderPath):
folderList = [x[0] for x in os.walk(folderPath)]
trainList = []
for folder in folderList:
imageList = sorted(glob.glob(folder + '/' + '*.jpg'))
for i in range(0, len(imageList), 12):
tmp = imageList[i:i+12]
if len(tmp) == 12:
trainList.append(imageList[i:i+12])
return trainList
def randomCropOnList(image_list, output_size):
cropped_img_list = []
h,w = output_size
height, width, _ = image_list[0].shape
#print(h,w,height,width)
i = random.randint(0, height - h)
j = random.randint(0, width - w)
st_y = 0
ed_y = w
st_x = 0
ed_x = h
or_st_y = i
or_ed_y = i + w
or_st_x = j
or_ed_x = j + h
#print(st_x, ed_x, st_y, ed_y)
#print(or_st_x, or_ed_x, or_st_y, or_ed_y)
for img in image_list:
new_img = np.empty((h,w,3), dtype=np.float32)
new_img.fill(128)
new_img[st_y: ed_y, st_x: ed_x, :] = img[or_st_y: or_ed_y, or_st_x: or_ed_x, :].copy()
cropped_img_list.append(np.ascontiguousarray(new_img))
return cropped_img_list
#print(len(populateTrainList('/home/user/data/nfs/')))
class expansionLoader(data.Dataset):
def __init__(self, folderPath):
self.trainList = populateTrainList2(folderPath)
print("# of training samples:", len(self.trainList))
def __getitem__(self, index):
img_path_list = self.trainList[index]
start = random.randint(0,3)
h,w,c = cv2.imread(img_path_list[0]).shape
image = cv2.cv2.imread(img_path_list[0])
#print(h,w,c)
if h > w:
scaleX = int(360*(h/w))
scaleY = 360
elif h <= w:
scaleX = 360
scaleY = int(360*(w/h))
img_list = []
flip = random.randint(0,1)
if flip:
for img_path in img_path_list[start:start+9]:
tmp = cv2.resize(cv2.imread(img_path), (scaleX,scaleY))[:,:,(2,1,0)]
img_list.append(np.array(cv2.flip(tmp,1), dtype=np.float32))
else:
for img_path in img_path_list[start:start+9]:
tmp = cv2.resize(cv2.imread(img_path), (scaleX, scaleY))[:,:,(2,1,0)]
img_list.append(np.array(tmp,dtype=np.float32))
#cv2.imshow("j",tmp)
#cv2.waitKey(0) & 0xff
#brak
for i in range(len(img_list)):
#print(img_list[i].shape)
#brak
img_list[i] /= 255
img_list[i][:,:,0] -= 0.485#(img_list[i]/127.5) - 1
img_list[i][:,:,1] -= 0.456
img_list[i][:,:,2] -= 0.406
img_list[i][:,:,0] /= 0.229
img_list[i][:,:,1] /= 0.224
img_list[i][:,:,2] /= 0.225
cropped_img_list = randomCropOnList(img_list,(352,352))
for i in range(len(cropped_img_list)):
cropped_img_list[i] = torch.from_numpy(cropped_img_list[i].transpose((2, 0, 1)))
return cropped_img_list
def __len__(self):
return len(self.trainList)