-
Notifications
You must be signed in to change notification settings - Fork 54
/
bootstrap.py
executable file
·74 lines (56 loc) · 2.55 KB
/
bootstrap.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
#!/usr/bin/env python
import os
import sys
import glob
import urllib
import tarfile
import numpy as np
from scipy.io import loadmat
def download_file(url, dest=None):
if not dest:
dest = 'data/' + url.split('/')[-1]
urllib.urlretrieve(url, dest)
# Download the Oxford102 dataset into the current directory
if not os.path.exists('data'):
os.mkdir('data')
print("Downloading images...")
download_file('http://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz')
tarfile.open("data/102flowers.tgz").extractall(path='data/')
print("Downloading image labels...")
download_file('http://www.robots.ox.ac.uk/~vgg/data/flowers/102/imagelabels.mat')
print("Downloading train/test/valid splits...")
download_file('http://www.robots.ox.ac.uk/~vgg/data/flowers/102/setid.mat')
# Read .mat file containing training, testing, and validation sets.
setid = loadmat('data/setid.mat')
# The .mat file is 1-indexed, so we subtract one to match Caffe's convention.
idx_train = setid['trnid'][0] - 1
idx_test = setid['tstid'][0] - 1
idx_valid = setid['valid'][0] - 1
# Read .mat file containing image labels.
image_labels = loadmat('data/imagelabels.mat')['labels'][0]
# Subtract one to get 0-based labels
image_labels -= 1
files = sorted(glob.glob('data/jpg/*.jpg'))
labels = np.array(zip(files, image_labels))
# Get current working directory for making absolute paths to images
cwd = os.path.dirname(os.path.realpath(__file__))
def write_set_file(fout, labels):
with open(fout, 'w+') as f:
for label in labels:
f.write('%s/%s %s\n' % (cwd, label[0], label[1]))
# Images are ordered by species, so shuffle them
np.random.seed(777)
idx_train = idx_train[np.random.permutation(len(idx_train))]
idx_test = idx_test[np.random.permutation(len(idx_test))]
idx_valid = idx_valid[np.random.permutation(len(idx_valid))]
write_set_file('train.txt', labels[idx_train,:])
write_set_file('test.txt', labels[idx_test,:])
write_set_file('valid.txt', labels[idx_valid,:])
if not os.path.exists('AlexNet/pretrained-weights.caffemodel'):
print('Downloading AlexNet pretrained weights...')
download_file('https://s3.amazonaws.com/jgoode/cannaid/bvlc_reference_caffenet.caffemodel',
'AlexNet/pretrained-weights.caffemodel')
if not os.path.exists('VGG_S/pretrained-weights.caffemodel'):
print('Downloading VGG_S pretrained weights...')
download_file('http://www.robots.ox.ac.uk/~vgg/software/deep_eval/releases/bvlc/VGG_CNN_S.caffemodel',
'VGG_S/pretrained-weights.caffemodel')