-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenList_AID.py
73 lines (61 loc) · 1.87 KB
/
GenList_AID.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
# coding=utf-8
import os
import cv2
import random
import numpy
import sys
# if __name__ == "__main__":
# dict = {'Built-up': 0, 'bareland': 1, 'vegetation_coverage': 2, 'water': 3}
def label_of_directory(directory):
"""
sorted for label indices
return a dict for {'classes', 'range(len(classes))'}
"""
classes = []
for subdir in sorted(os.listdir(directory)):
if os.path.isdir(os.path.join(directory, subdir)):
classes.append(subdir)
num_classes = len(classes)
class_indices = dict(zip(classes, range(len(classes))))
#print(num_classes)#,class_indices(classes))
return class_indices
rate = 0.9 #随机抽取10%的样本作为验证集
# root = '.\\GenDatasetAID\\'
root = '.\\AID-6-4\\train_data\\'
dict = label_of_directory(root)
print(dict)
Trainlist = []
Testlist = []
alllist = []
index = 0
# max_num = 80000
for folder in dict:
img_list = [f for f in os.listdir(os.path.join(root, folder)) if not f.startswith('.')]
for img in img_list:
str0 = '%d\t%s\t%d\n' % (index, os.path.join(folder, img), dict[folder])
index += 1
alllist.append(str0)
Trainlist = alllist
Trainfile = open("./data/trainAID-6-4.txt", "w")
for str1 in Trainlist:
Trainfile.write(str1)
Trainfile.close()
root = '.\\AID-6-4\\test_data\\'
# dict = label_of_directory(root)
# print(dict)
Trainlist = []
Testlist = []
alllist = []
index = 0
# max_num = 80000
for folder in dict:
img_list = [f for f in os.listdir(os.path.join(root, folder)) if not f.startswith('.')]
for img in img_list:
str0 = '%d\t%s\t%d\n' % (index, os.path.join(folder, img), dict[folder])
index += 1
alllist.append(str0)
Testlist = alllist
Testfile = open("./data/testAID-6-4.txt", "w")
for str1 in Testlist:
Testfile.write(str1)
Testfile.close()