-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_imagenet.py
59 lines (32 loc) · 1.03 KB
/
prepare_imagenet.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
#-*-coding:utf-8-*-
import os
data_set_dir='./IMAGENET'
train_dir=os.path.join(data_set_dir,'train')
val_dir=os.path.join(data_set_dir,'val')
labels=os.listdir(train_dir)
##filter
labels=[x for x in labels if os.path.isdir(os.path.join(train_dir,x))]
labels.sort()
syntext_f=open('label.txt','w')
for label in labels:
message = label + ' ' +str(labels.index(label)) +'\n'
syntext_f.write(message)
train_f=open('train.txt','w')
val_f=open('val.txt','w')
for label in labels:
cur_dir=os.path.join(train_dir,label)
pic_list=os.listdir(cur_dir)
for pic in pic_list:
cur_path=os.path.join(cur_dir,pic)
message=cur_path+'|'+str(labels.index(label))+'\n'
train_f.write(message)
for label in labels:
cur_dir = os.path.join(val_dir, label)
pic_list = os.listdir(cur_dir)
for pic in pic_list:
cur_path = os.path.join(cur_dir, pic)
message = cur_path + '|' + str(labels.index(label)) + '\n'
val_f.write(message)
train_f.close()
val_f.close()
syntext_f.close()