-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_imagenet.py
79 lines (67 loc) · 1.98 KB
/
fetch_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
import os
import glob
import time
import urllib
from requests.exceptions import ConnectionError
label_file = open('label.lst')
label_path = 'label/'
map_url = 'http://www.image-net.org/api/text/imagenet.synset.geturls.getmapping?wnid='
if not os.path.exists(label_path):
os.makedirs(label_path)
for line in label_file.readlines():
line = line[:-1]
if os.path.isfile(label_path+line+'.lst'):
print 'Already exist %s' % line
continue
try:
print 'Fetching...',map_url+line, label_path+line+'.lst'
urllib.urlretrieve(map_url+line, label_path+line+'.lst')
except ConnectionError, e:
print 'could not download %s' % line
continue
time.sleep(1.5)
num_class = 9999
len_name = len(str(num_class))
image_path = 'image/'
d_file = open('download.lst','w')
'''
download.lst example
http://server/file1.iso
dir=/iso_images
out=file1.img
http://server/file2.iso
dir=/iso_images
out=file2.img
then download images using aria2
$aria2c -c -i download.lst -j 10 -t 10
monitor
$ watch 'for D in *; do echo $D; find $D -type f| wc -l; done'
'''
label_file = open('label.lst')
for line in label_file.readlines():
line = line[:-1]
filename = label_path+line+'.lst'
flag = 1
try:
url_file = open(filename)
except:
continue
for line in url_file.readlines():
try:
line = line.split(' ')[1][:-1]
except:
print line
continue
#print line
#print ' dir=' + image_path + filename.split('/')[-1][:-4]
#print ' out=' + str(flag).zfill(len_name)+ '.jpg'
d_file.write(line + '\r\n')
d_file.write(' dir=' + image_path + filename.split('/')[-1][:-4] + '\r\n')
d_file.write(' out=' + str(flag).zfill(len_name)+ '.jpg' + '\r\n')
if flag >= num_class: break
flag = flag + 1
print filename+' done!'
#break
d_file.close()