-
Notifications
You must be signed in to change notification settings - Fork 3
/
make_dataset.py
141 lines (126 loc) · 4.88 KB
/
make_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
import numpy as np
import os
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
from PIL import Image
import random
from torch.utils.data import Dataset, DataLoader
import torch
from pylab import *
import constants as ct
TRANSFORM = True
VISUALIZE = 0
def gen_data(path,val_percent,test_percent):
if 0:
delList = os.listdir(ct.TXT_PATH )
for f in delList:
filePath = os.path.join( ct.TXT_PATH , f )
if os.path.isfile(filePath):
print(filePath)
os.remove(filePath)
print (filePath + " was removed!")
dirs = os.listdir(path)
c = 0
for dir in dirs:
c += 1
print(c, dir,' is generating')
files = os.listdir(os.path.join(path,dir))
img1_path = os.path.join(path,dir,'t0.jpg')
img2_path = os.path.join(path,dir,'t1.jpg')
gt_path = os.path.join(path,dir,'label.jpg')
if not os.path.exists(ct.TXT_PATH):
os.makedirs(ct.TXT_PATH)
chance = np.random.randint(100)
if chance<val_percent:
with open(os.path.join(ct.TXT_PATH,'validation.txt'),'a') as f:
f.write(img1_path+','+img2_path+','+gt_path)
f.write('\n')
elif chance<val_percent+test_percent:
with open(os.path.join(ct.TXT_PATH,'test.txt'),'a') as f:
f.write(img1_path+','+img2_path+','+gt_path)
f.write('\n')
else:
with open(os.path.join(ct.TXT_PATH,'train.txt'),'a') as f:
f.write(img1_path+','+img2_path+','+gt_path)
f.write('\n')
if VISUALIZE:
img1 = Image.open(img1_path)
img2 = Image.open(img2_path)
gt = Image.open(gt_path)
gt1 = Image.open(gt1_path)
gt2 = Image.open(gt2_path)
plt.figure(figsize=(200,300))
plt.subplot(1,5,1)
plt.imshow(img1)
plt.subplot(1,5,2)
plt.imshow(img2)
plt.subplot(1,5,3)
plt.imshow(gt)
plt.subplot(1,5,4)
plt.imshow(gt1)
plt.subplot(1,5,5)
plt.imshow(gt2)
plt.show()
class OSCD_TRAIN(Dataset):
def __init__(self, dir_nm):
super(OSCD_TRAIN, self).__init__()
self.dir_nm = dir_nm
with open(os.path.join(self.dir_nm),'r') as f:
self.list = f.readlines()
self.file_size = len(self.list)
def __getitem__(self, idx):
x1 = Image.open(self.list[idx].split(',')[0])
x2 = Image.open(self.list[idx].split(',')[1])
gt = Image.open(self.list[idx].split(',')[2].strip())
t = [
transforms.RandomRotation((360,360), resample=False, expand=False, center=None),
transforms.RandomVerticalFlip(p=1),
transforms.RandomHorizontalFlip(p=1),
transforms.RandomRotation((180,180), resample=False, expand=False, center=None),
transforms.Resize((ct.ISIZE,ct.ISIZE)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5 ), (0.5, 0.5, 0.5)),
]
if TRANSFORM:
k = np.random.randint(4)
x1 = t[k](x1);x2 = t[k](x2);gt = t[k](gt);
x1 = t[4](x1);x2 = t[4](x2);gt = t[4](gt);
x1 = t[5](x1);x2 = t[5](x2);gt = t[5](gt);
x1 = t[6](x1);x2 = t[6](x2);
return x1, x2, gt
def __len__(self):
return self.file_size
class OSCD_TEST(Dataset):
def __init__(self, dir_nm):
super(OSCD_TEST, self).__init__()
self.dir_nm = dir_nm
with open(os.path.join(self.dir_nm),'r') as f:
self.list = f.readlines()
self.file_size = len(self.list)
def __getitem__(self, idx):
x1 = Image.open(self.list[idx].split(',')[0])
x2 = Image.open(self.list[idx].split(',')[1])
gt = Image.open(self.list[idx].split(',')[2].strip())
t = [
transforms.Resize((ct.ISIZE,ct.ISIZE)),
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5 ), (0.5,0.5,0.5 )),
]
if TRANSFORM:
x1 = t[0](x1);x2 = t[0](x2);gt = t[0](gt);
x1 = t[1](x1);x2 = t[1](x2);gt = t[1](gt);
x1 = t[2](x1);x2 = t[2](x2);
return x1, x2, gt
def __len__(self):
return self.file_size
if __name__=='__main__':
if ct.DATASET == 'CDD':
gen_data(os.path.join(ct.TRAIN_DATA, 'train'),0,0)
gen_data(os.path.join(ct.TRAIN_DATA, 'val'),100,0)
gen_data(os.path.join(ct.TRAIN_DATA, 'test'),0,100)
elif ct.DATASET == 'WHU-CD':
gen_data(ct.TRAIN_DATA,10,10)
else:
gen_data(os.path.join(ct.TRAIN_DATA, 'train'),0,0)
gen_data(os.path.join(ct.TRAIN_DATA, 'val'),100,0)
gen_data(os.path.join(ct.TRAIN_DATA, 'test'),0,100)