-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdataset.py
659 lines (597 loc) · 28.9 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
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
from io import BytesIO
import math
import lmdb
from PIL import Image
from torch.utils.data import Dataset
import torch
import numpy as np
import tensor_transforms as tt
import pandas as pd
class Naip2SentinelTDataset(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution, integer_values):
"""
Args:
csv_path (string): path to csv file
img_path (string): path to the folder where images are
transform: pytorch transforms for transforms and tensor conversion
"""
self.integer_values = integer_values
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.naip2016 = np.asarray(self.data_info.iloc[:, 1])
self.naip2018 = np.asarray(self.data_info.iloc[:, 2])
self.naip = np.concatenate((self.naip2016, self.naip2018))
self.sentinel2016 = np.asarray(self.data_info.iloc[:, 3])
self.sentinel2018 = np.asarray(self.data_info.iloc[:, 4])
self.sentinel = np.concatenate((self.sentinel2016, self.sentinel2018))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.time = np.concatenate((np.array([0]*data_len),np.array([1]*data_len)))
self.data_len = len(self.sentinel)
self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
# self.crop = tt.RandomCrop(resolution)
# print(self.naip.shape, self.sentinel.shape, self.time.shape)
def __getitem__(self, index):
# Get image name from the pandas df
naip = self.naip[index]
sentinel = self.sentinel[index]
t = self.time[index]
if t == 1:
naip2 = self.naip[index-self.house_count]
else:
naip2 = self.naip[index+self.house_count]
# Open image
naip = Image.open(naip).convert('RGB')
naip2 = Image.open(naip2).convert('RGB')
sentinel = Image.open(sentinel).convert('RGB')
# im1 = naip.save("naip.jpg")
# im2 = sentinel.save("sentinel.jpg")
# Transform the image
naip = self.transform(naip)
naip2 = self.enc_transform(naip2)
sentinel = self.enc_transform(sentinel)
# print(naip.shape, self.coords[t].shape)
# naip = torch.cat([naip, self.coords[t]], 1).squeeze(0)
naip = torch.cat([naip, self.coords[t]], 0)
# naip = self.crop(sentinel.unsqueeze(0)).squeeze(0)
# sentinel = self.crop(sentinel.unsqueeze(0)).squeeze(0)
# print(naip.shape, sentinel.shape)
return (naip, sentinel, naip2)
def __len__(self):
return self.data_len
class Naip2SentinelTPath(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution, integer_values):
"""
Args:
csv_path (string): path to csv file
img_path (string): path to the folder where images are
transform: pytorch transforms for transforms and tensor conversion
"""
self.integer_values = integer_values
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.naip2016 = np.asarray(self.data_info.iloc[:, 1])
self.naip2018 = np.asarray(self.data_info.iloc[:, 2])
self.naip = np.concatenate((self.naip2016, self.naip2018))
self.sentinel2016 = np.asarray(self.data_info.iloc[:, 3])
self.sentinel2018 = np.asarray(self.data_info.iloc[:, 4])
self.sentinel = np.concatenate((self.sentinel2016, self.sentinel2018))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.time = np.concatenate((np.array([0]*data_len),np.array([1]*data_len)))
self.data_len = len(self.sentinel)
self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
# self.crop = tt.RandomCrop(resolution)
# print(self.naip.shape, self.sentinel.shape, self.time.shape)
def __getitem__(self, index):
# Get image name from the pandas df
naip = self.naip[index]
path = naip.split("/")[-1]
sentinel = self.sentinel[index]
t = self.time[index]
if t == 1:
naip2 = self.naip[index-self.house_count]
else:
naip2 = self.naip[index+self.house_count]
# Open image
naip = Image.open(naip).convert('RGB')
naip2 = Image.open(naip2).convert('RGB')
sentinel = Image.open(sentinel).convert('RGB')
# Transform the image
naip = self.transform(naip)
naip2 = self.enc_transform(naip2)
sentinel = self.enc_transform(sentinel)
naip = torch.cat([naip, self.coords[t]], 0)
return (naip, sentinel, naip2, path)
def __len__(self):
return self.data_len
class PatchNSTDataset(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution=256, crop_size=64, integer_values=False):
#crop for better fitting into the memory
self.crop_size = crop_size
# self.n = resolution // crop_size
# self.log_size = int(math.log(self.n, 2))
self.crop = tt.RandomCropDim3(crop_size)
# self.crop_resolution = tt.RandomCrop(resolution)
# self.to_crop = to_crop
self.resolution = resolution
self.integer_values = integer_values
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.naip2016 = np.asarray(self.data_info.iloc[:, 1])
self.naip2018 = np.asarray(self.data_info.iloc[:, 2])
self.naip = np.concatenate((self.naip2016, self.naip2018))
self.sentinel2016 = np.asarray(self.data_info.iloc[:, 3])
self.sentinel2018 = np.asarray(self.data_info.iloc[:, 4])
self.sentinel = np.concatenate((self.sentinel2016, self.sentinel2018))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.time = np.concatenate((np.array([0]*data_len),np.array([1]*data_len)))
self.data_len = len(self.sentinel)
self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
def __len__(self):
return self.data_len
def __getitem__(self, index):
naip = self.naip[index]
path = naip.split("/")[-1]
sentinel = self.sentinel[index]
t = self.time[index]
if t == 1:
naip2 = self.naip[index-self.house_count]
else:
naip2 = self.naip[index+self.house_count]
# Open image
naip = Image.open(naip).convert('RGB')
naip2 = Image.open(naip2).convert('RGB')
sentinel = Image.open(sentinel).convert('RGB')
# Transform the image
naip = self.transform(naip)
naip2 = self.enc_transform(naip2)
sentinel = self.enc_transform(sentinel)
naip = torch.cat([naip, self.coords[t]], 0)
# print(naip.shape)
naip, h_start, w_start = self.crop(naip)
naip2 = tt.patch_crop_dim3(naip2, h_start, w_start, self.crop_size)
sentinel = tt.patch_crop_dim3(sentinel, h_start, w_start, self.crop_size)
# print(naip.shape)
return (naip, sentinel, naip2, h_start, w_start)
class MSNSTDataset(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution=256, crop_size=64, integer_values=False):
#crop for better fitting into the memory
self.crop_size = crop_size
self.n = resolution // crop_size
self.resolution = resolution
self.integer_values = integer_values
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.naip2016 = np.asarray(self.data_info.iloc[:, 1])
self.naip2018 = np.asarray(self.data_info.iloc[:, 2])
self.naip = np.concatenate((self.naip2016, self.naip2018))
self.sentinel2016 = np.asarray(self.data_info.iloc[:, 3])
self.sentinel2018 = np.asarray(self.data_info.iloc[:, 4])
self.sentinel = np.concatenate((self.sentinel2016, self.sentinel2018))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.time = np.concatenate((np.array([0]*data_len),np.array([1]*data_len)))
self.data_len = len(self.sentinel)
self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
def __len__(self):
return self.data_len
def __getitem__(self, index):
data = {}
naip = self.naip[index]
path = naip.split("/")[-1]
sentinel = self.sentinel[index]
t = self.time[index]
if t == 1:
naip2 = self.naip[index-self.house_count]
else:
naip2 = self.naip[index+self.house_count]
# Open image
naip = Image.open(naip).convert('RGB')
naip2 = Image.open(naip2).convert('RGB')
sentinel = Image.open(sentinel).convert('RGB')
# Transform the image
naip = self.transform(naip)
naip2 = self.enc_transform(naip2)
sentinel = self.enc_transform(sentinel)
naip = torch.cat([naip, self.coords[t]], 0)
# print(naip.shape)
for i in range(self.n):
for j in range(self.n):
naip_ij = tt.patch_crop_dim3(naip, i*self.crop_size, j*self.crop_size, self.crop_size)
naip2_ij = tt.patch_crop_dim3(naip2, i*self.crop_size, j*self.crop_size, self.crop_size)
sentinel_ij = tt.patch_crop_dim3(sentinel, i*self.crop_size, j*self.crop_size, self.crop_size)
data[(i,j)] = (naip_ij, sentinel_ij, naip2_ij, i*self.crop_size, j*self.crop_size)
return data
class MSNSTPDataset(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution=256, crop_size=64, integer_values=False):
#crop for better fitting into the memory
self.crop_size = crop_size
self.n = resolution // crop_size
self.resolution = resolution
self.integer_values = integer_values
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.naip2016 = np.asarray(self.data_info.iloc[:, 1])
self.naip2018 = np.asarray(self.data_info.iloc[:, 2])
self.naip = np.concatenate((self.naip2016, self.naip2018))
self.sentinel2016 = np.asarray(self.data_info.iloc[:, 3])
self.sentinel2018 = np.asarray(self.data_info.iloc[:, 4])
self.sentinel = np.concatenate((self.sentinel2016, self.sentinel2018))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.time = np.concatenate((np.array([0]*data_len),np.array([1]*data_len)))
self.data_len = len(self.sentinel)
self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
def __len__(self):
return self.data_len
def __getitem__(self, index):
data = {}
naip = self.naip[index]
path = naip.split("/")[-1]
sentinel = self.sentinel[index]
t = self.time[index]
if t == 1:
naip2 = self.naip[index-self.house_count]
else:
naip2 = self.naip[index+self.house_count]
# Open image
naip_img = Image.open(naip).convert('RGB')
naip2_img = Image.open(naip2).convert('RGB')
sentinel_img = Image.open(sentinel).convert('RGB')
# Transform the image
naip = self.transform(naip_img)
naip2 = self.enc_transform(naip2_img)
sentinel = self.enc_transform(sentinel_img)
naip_img.close()
naip2_img.close()
sentinel_img.close()
naip = torch.cat([naip, self.coords[t]], 0)
# print(naip.shape)
for i in range(self.n):
for j in range(self.n):
naip_ij = tt.patch_crop_dim3(naip, i*self.crop_size, j*self.crop_size, self.crop_size)
naip2_ij = tt.patch_crop_dim3(naip2, i*self.crop_size, j*self.crop_size, self.crop_size)
sentinel_ij = tt.patch_crop_dim3(sentinel, i*self.crop_size, j*self.crop_size, self.crop_size)
data[(i,j)] = (naip_ij, sentinel_ij, naip2_ij, i*self.crop_size, j*self.crop_size)
half_size = self.crop_size//2
for i in range(self.n):
for j in range(self.n-1):
naip_ij = tt.patch_crop_dim3(naip, i*self.crop_size, j*self.crop_size+half_size, self.crop_size)
naip2_ij = tt.patch_crop_dim3(naip2, i*self.crop_size, j*self.crop_size+half_size, self.crop_size)
sentinel_ij = tt.patch_crop_dim3(sentinel, i*self.crop_size, j*self.crop_size+half_size, self.crop_size)
data[(i,j+0.5)] = (naip_ij, sentinel_ij, naip2_ij, i*self.crop_size, j*self.crop_size)
for j in range(self.n):
for i in range(self.n-1):
naip_ij = tt.patch_crop_dim3(naip, i*self.crop_size+half_size, j*self.crop_size, self.crop_size)
naip2_ij = tt.patch_crop_dim3(naip2, i*self.crop_size+half_size, j*self.crop_size, self.crop_size)
sentinel_ij = tt.patch_crop_dim3(sentinel, i*self.crop_size+half_size, j*self.crop_size, self.crop_size)
data[(i+0.5,j)] = (naip_ij, sentinel_ij, naip2_ij, i*self.crop_size, j*self.crop_size)
return data, path
class FMoWSentinel2(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution, integer_values):
"""
Args:
csv_path (string): path to csv file
img_path (string): path to the folder where images are
transform: pytorch transforms for transforms and tensor conversion
"""
self.integer_values = integer_values
self.resolution = resolution
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.highpast = np.asarray(self.data_info.iloc[:, 1])
self.highpresent = np.asarray(self.data_info.iloc[:, 2])
self.high = np.concatenate((self.highpast, self.highpresent))
self.lowpast = np.asarray(self.data_info.iloc[:, 3])
self.lowpresent = np.asarray(self.data_info.iloc[:, 4])
self.low = np.concatenate((self.lowpast, self.lowpresent))
#process date
self.pastdate = pd.to_datetime(self.data_info.iloc[:, 5], format="%Y%m%dT%H%M%S")
self.presentdate = pd.to_datetime(self.data_info.iloc[:, 6], format="%Y%m%dT%H%M%S")
self.pastdate = (self.pastdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.presentdate = (self.presentdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.time = np.concatenate((self.pastdate, self.presentdate))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.data_len = len(self.low)
# self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
# self.crop = tt.RandomCrop(resolution)
# print(self.naip.shape, self.sentinel.shape, self.time.shape)
# def get_sinusoid_encoding_table(positions, d_hid, T=1000):
# ''' Sinusoid position encoding table
# positions: int or list of integer, if int range(positions)'''
# if isinstance(positions, int):
# positions = list(range(positions))
# def cal_angle(position, hid_idx):
# return position / np.power(T, 2 * (hid_idx // 2) / d_hid)
# def get_posi_angle_vec(position):
# return [cal_angle(position, hid_j) for hid_j in range(d_hid)]
# sinusoid_table = np.array([get_posi_angle_vec(pos_i) for pos_i in positions])
# sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2]) # dim 2i
# sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2]) # dim 2i+1
# if torch.cuda.is_available():
# return torch.FloatTensor(sinusoid_table).cuda()
# else:
# return torch.FloatTensor(sinusoid_table)
def __getitem__(self, index):
# Get image name from the pandas df
high = self.high[index]
low = self.low[index]
t = self.time[index]
if index >= self.house_count:
high2 = self.high[index-self.house_count]
else:
high2 = self.highpresent[index]
if index == 0:
print(high, high2)
# Open image
high = Image.open(high).convert('RGB')
high2 = Image.open(high2).convert('RGB')
low = Image.open(low).convert('RGB')
# Transform the image
high = self.transform(high)
high2 = self.transform(high2)
low = self.enc_transform(low)
coords = tt.convert_to_coord_uneven_t(1, self.resolution, self.resolution, t, integer_values=self.integer_values)
# print(coords)
high = torch.cat([high, coords], 0)
return (high, low, high2)
def __len__(self):
return self.data_len
class FMoWSentinel2Path(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution, integer_values):
"""
Args:
csv_path (string): path to csv file
img_path (string): path to the folder where images are
transform: pytorch transforms for transforms and tensor conversion
"""
self.integer_values = integer_values
self.resolution = resolution
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.highpast = np.asarray(self.data_info.iloc[:, 1])
self.highpresent = np.asarray(self.data_info.iloc[:, 2])
self.high = np.concatenate((self.highpast, self.highpresent))
self.lowpast = np.asarray(self.data_info.iloc[:, 3])
self.lowpresent = np.asarray(self.data_info.iloc[:, 4])
self.low = np.concatenate((self.lowpast, self.lowpresent))
#process date
self.pastdate = pd.to_datetime(self.data_info.iloc[:, 5], format="%Y%m%dT%H%M%S")
self.presentdate = pd.to_datetime(self.data_info.iloc[:, 6], format="%Y%m%dT%H%M%S")
self.pastdate = (self.pastdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.presentdate = (self.presentdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.time = np.concatenate((self.pastdate, self.presentdate))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.data_len = len(self.low)
# self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
# self.crop = tt.RandomCrop(resolution)
# print(self.naip.shape, self.sentinel.shape, self.time.shape)
def __getitem__(self, index):
# Get image name from the pandas df
high = self.high[index]
path = high.split("/")[-1].split(".")[0]+"_"+str(index)
low = self.low[index]
t = self.time[index]
if index >= self.house_count:
high2 = self.high[index-self.house_count]
else:
high2 = self.highpresent[index]
if index == 0:
print(high, high2)
# Open image
high = Image.open(high).convert('RGB')
high2 = Image.open(high2).convert('RGB')
low = Image.open(low).convert('RGB')
# Transform the image
high = self.transform(high)
high2 = self.transform(high2)
low = self.enc_transform(low)
coords = tt.convert_to_coord_uneven_t(1, self.resolution, self.resolution, t, integer_values=self.integer_values)
# print(coords)
high = torch.cat([high, coords], 0)
return (high, low, high2, path)
def __len__(self):
return self.data_len
class FMoWSentinelPatch(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution, crop_size, integer_values):
"""
Args:
csv_path (string): path to csv file
img_path (string): path to the folder where images are
transform: pytorch transforms for transforms and tensor conversion
"""
self.integer_values = integer_values
self.resolution = resolution
self.crop_size = crop_size
self.crop = tt.RandomCropDim3(crop_size)
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.highpast = np.asarray(self.data_info.iloc[:, 1])
self.highpresent = np.asarray(self.data_info.iloc[:, 2])
self.high = np.concatenate((self.highpast, self.highpresent))
self.lowpast = np.asarray(self.data_info.iloc[:, 3])
self.lowpresent = np.asarray(self.data_info.iloc[:, 4])
self.low = np.concatenate((self.lowpast, self.lowpresent))
#process date
self.pastdate = pd.to_datetime(self.data_info.iloc[:, 5], format="%Y%m%dT%H%M%S")
self.presentdate = pd.to_datetime(self.data_info.iloc[:, 6], format="%Y%m%dT%H%M%S")
self.pastdate = (self.pastdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.presentdate = (self.presentdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.time = np.concatenate((self.pastdate, self.presentdate))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.data_len = len(self.low)
# self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
# self.crop = tt.RandomCrop(resolution)
# print(self.naip.shape, self.sentinel.shape, self.time.shape)
# def get_sinusoid_encoding_table(positions, d_hid, T=1000):
# ''' Sinusoid position encoding table
# positions: int or list of integer, if int range(positions)'''
# if isinstance(positions, int):
# positions = list(range(positions))
# def cal_angle(position, hid_idx):
# return position / np.power(T, 2 * (hid_idx // 2) / d_hid)
# def get_posi_angle_vec(position):
# return [cal_angle(position, hid_j) for hid_j in range(d_hid)]
# sinusoid_table = np.array([get_posi_angle_vec(pos_i) for pos_i in positions])
# sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2]) # dim 2i
# sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2]) # dim 2i+1
# if torch.cuda.is_available():
# return torch.FloatTensor(sinusoid_table).cuda()
# else:
# return torch.FloatTensor(sinusoid_table)
def __getitem__(self, index):
# Get image name from the pandas df
high = self.high[index]
low = self.low[index]
t = self.time[index]
if index >= self.house_count:
high2 = self.high[index-self.house_count]
else:
high2 = self.highpresent[index]
# if index == 0:
# print(high, high2)
# Open image
high = Image.open(high).convert('RGB')
high2 = Image.open(high2).convert('RGB')
low = Image.open(low).convert('RGB')
# Transform the image
high = self.transform(high)
high2 = self.transform(high2)
low = self.enc_transform(low)
coords = tt.convert_to_coord_uneven_t(1, self.resolution, self.resolution, t, integer_values=self.integer_values)
# print(coords)
high = torch.cat([high, coords], 0)
high, h_start, w_start = self.crop(high)
high2 = tt.patch_crop_dim3(high2, h_start, w_start, self.crop_size)
low = tt.patch_crop_dim3(low, h_start, w_start, self.crop_size)
return (high, low, high2, h_start, w_start)
def __len__(self):
return self.data_len
class FSAllPatch(Dataset):
def __init__(self, csv_path, transform, enc_transform, resolution, crop_size, integer_values):
"""
Args:
csv_path (string): path to csv file
img_path (string): path to the folder where images are
transform: pytorch transforms for transforms and tensor conversion
"""
self.integer_values = integer_values
self.resolution = resolution
self.crop_size = crop_size
self.n = resolution // crop_size
# Transforms
self.transform = transform
self.enc_transform = enc_transform
# Read the csv file
self.data_info = pd.read_csv(csv_path, header=0)
# column 1-4 contain the image paths
self.highpast = np.asarray(self.data_info.iloc[:, 1])
self.highpresent = np.asarray(self.data_info.iloc[:, 2])
self.high = np.concatenate((self.highpast, self.highpresent))
self.lowpast = np.asarray(self.data_info.iloc[:, 3])
self.lowpresent = np.asarray(self.data_info.iloc[:, 4])
self.low = np.concatenate((self.lowpast, self.lowpresent))
#process date
self.pastdate = pd.to_datetime(self.data_info.iloc[:, 5], format="%Y%m%dT%H%M%S")
self.presentdate = pd.to_datetime(self.data_info.iloc[:, 6], format="%Y%m%dT%H%M%S")
self.pastdate = (self.pastdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.presentdate = (self.presentdate - pd.to_datetime("20150623", format="%Y%m%d")).dt.days
self.time = np.concatenate((self.pastdate, self.presentdate))
# Calculate len
data_len = len(self.data_info.index)
self.house_count = data_len
self.data_len = len(self.low)
# self.coords = tt.convert_to_coord_with_t(1, resolution, resolution, [0,1], integer_values=self.integer_values)
# self.crop = tt.RandomCrop(resolution)
# print(self.naip.shape, self.sentinel.shape, self.time.shape)
def __getitem__(self, index):
# Get image name from the pandas df
data = {}
high = self.high[index]
path = high.split("/")[-1].split(".")[0]+"_"+str(index)
low = self.low[index]
t = self.time[index]
if index >= self.house_count:
high2 = self.high[index-self.house_count]
else:
high2 = self.highpresent[index]
# if index == 0:
# print(high, high2)
# Open image
high = Image.open(high).convert('RGB')
high2 = Image.open(high2).convert('RGB')
low = Image.open(low).convert('RGB')
# Transform the image
high = self.transform(high)
high2 = self.transform(high2)
low = self.enc_transform(low)
coords = tt.convert_to_coord_uneven_t(1, self.resolution, self.resolution, t, integer_values=self.integer_values)
# print(coords)
high = torch.cat([high, coords], 0)
for i in range(self.n):
for j in range(self.n):
high_ij = tt.patch_crop_dim3(high, i*self.crop_size, j*self.crop_size, self.crop_size)
high2_ij = tt.patch_crop_dim3(high2, i*self.crop_size, j*self.crop_size, self.crop_size)
low_ij = tt.patch_crop_dim3(low, i*self.crop_size, j*self.crop_size, self.crop_size)
data[(i,j)] = (high_ij, low_ij, high2_ij, i*self.crop_size, j*self.crop_size)
half_size = self.crop_size//2
for i in range(self.n):
for j in range(self.n-1):
high_ij = tt.patch_crop_dim3(high, i*self.crop_size, j*self.crop_size+half_size, self.crop_size)
high2_ij = tt.patch_crop_dim3(high2, i*self.crop_size, j*self.crop_size+half_size, self.crop_size)
low_ij = tt.patch_crop_dim3(low, i*self.crop_size, j*self.crop_size+half_size, self.crop_size)
data[(i,j+0.5)] = (high_ij, low_ij, high2_ij, i*self.crop_size, j*self.crop_size)
for j in range(self.n):
for i in range(self.n-1):
high_ij = tt.patch_crop_dim3(high, i*self.crop_size+half_size, j*self.crop_size, self.crop_size)
high2_ij = tt.patch_crop_dim3(high2, i*self.crop_size+half_size, j*self.crop_size, self.crop_size)
low_ij = tt.patch_crop_dim3(low, i*self.crop_size+half_size, j*self.crop_size, self.crop_size)
data[(i+0.5,j)] = (high_ij, low_ij, high2_ij, i*self.crop_size, j*self.crop_size)
return data, path
def __len__(self):
return self.data_len