-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset_split_preprocess.py
166 lines (153 loc) · 4.29 KB
/
dataset_split_preprocess.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
# data segmentation code # resize origin code to match classifier model and split the data set into training and test set
import sys
import os
import shutil
import csv
import subprocess
import random
import time
import itertools
from PIL import Image
#UCM path
imagesPath = './ShenzhenLC56/' # '..\\SZLU'
converted_path = './ShenzhenLC56-5-5/'
#NUPW Path
#imagesPath = '/home/hpc-126/remote-host/NUPW-45/NWPU-RESISC45'
#converted_path ='/home/hpc-126/remote-host/NUPW-45/train224x224'
train_path = ''
test_path =''
imageWidth = 256
imageHeight = 256
split_ratio = 0.5 # ratio of train and test set size
datatype ='UCM'
labels = ''
if datatype == 'UCM':
labels = {
'golfcourse': 9,
'overpass': 14,
'freeway': 8,
'denseresidential': 6,
'mediumresidential': 12,
'harbor': 10,
'tenniscourt': 20,
'mobilehomepark': 13,
'parkinglot': 15,
'agricultural': 0,
'chaparral': 5,
'airplane': 1,
'river': 16,
'baseballdiamond': 2,
'intersection': 11,
'beach': 3,
'runway': 17,
'forest': 7,
'sparseresidential': 18,
'buildings': 4,
'storagetanks': 19
}
elif datatype =='NWPU':
labels = {
'airplane': 0,
'airport' : 1,
'baseball_diamond': 2,
'basketball_court': 3,
'beach':4,
'bridge':5,
'chaparral':6,
'church':7,
'circular_farmland':8,
'cloud':9,
'commercial_area':10,
'dense_residential':11,
'desert':12,
'forest':13,
'freeway':14,
'golf_course':15,
'ground_track_field':16,
'harbor':17,
'industrial_area':18,
'intersection':19,
'island':20,
'lake':21,
'meadow':22,
'medium_residential':23,
'mobile_home_park':24,
'mountain':25,
'overpass':26,
'palace':27,
'parking_lot':28,
'railway':29,
'railway_station':30,
'rectangular_farmland':31,
'river':32,
'roundabout':33,
'runway':34,
'sea_ice':35,
'ship':36,
'snowberg':37,
'sparse_residential':38,
'stadium':39,
'storage_tank':40,
'tennis_court':41,
'terrace':42,
'thermal_power_station':43,
'wetland':44
}
else :
print ('please specify the data type : UCM NUPW')
def remove_dir(path):
try:
shutil.rmtree(path)
except:
pass
'''
if e.errno == 2:
pass
else:
raise
'''
def convert_images(path):
images = []
train_path = os.path.join(converted_path, 'train_data')
test_path = os.path.join(converted_path, 'test_data')
os.mkdir(train_path)
os.mkdir(test_path)
# os.mkdir("DataSet_JPG")
for root, dirs, files in os.walk(path):
if root == path:
continue
category = os.path.basename(root)
#label = labels[category]
label = category
UCMjpgpath_train =(os.path.join(train_path, str(label)))
UCMjpgpath_test = (os.path.join(test_path, str(label)))
os.mkdir(UCMjpgpath_train)
os.mkdir(UCMjpgpath_test)
random.shuffle(files)
count =0
for name in files:
im = Image.open(os.path.join(root, name))
(width, height) = im.size
# images in the UCMerced_LandUse dataset are supposed to be 256x256, but they aren't
#resize the image size to defult setting
# if width != imageWidth or height != imageHeight:
# im = im.resize((imageWidth, imageHeight), Image.ANTIALIAS)
if name.find('.tif') ==-1:
jpeg_name=name
else :
jpeg_name = name.replace(".tif", ".jpg")
if count < int(len(files)*split_ratio):
im.save(os.path.join(UCMjpgpath_train, jpeg_name))
else:
im.save(os.path.join(UCMjpgpath_test, jpeg_name))
count+=1
return images
def main (argv):
if os.path.exists(converted_path):
remove_dir(converted_path)
os.mkdir(converted_path)
# train_path = os.path.join(converted_path,'train_data')
# test_path = os.path.join(converted_path,'train_data')
convert_images(imagesPath)
if __name__== "__main__":
main(sys.argv)