-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
68 lines (52 loc) · 1.8 KB
/
init.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
import os
import numpy as np
import sys
import tarfile
import util
import glob
import config
from shutil import copyfile, rmtree
from scipy.io import loadmat
data_path = 'data'
trained_path = './trained'
base_url = 'http://www.robots.ox.ac.uk/~vgg/data/flowers/102/'
if not os.path.exists(data_path):
os.mkdir(data_path)
if not os.path.isdir(trained_path):
os.mkdir(trained_path)
flowers_archive_path = os.path.join(data_path, '102flowers.tgz')
img_label_path = os.path.join(data_path, 'imagelabels.mat')
setid_path = os.path.join(data_path, 'setid.mat')
if not os.path.isfile(flowers_archive_path):
print ('Downloading images...')
util.download_file(base_url + '102flowers.tgz')
else:
print("Images data already existed\n")
if not os.path.isdir('./data/jpg'):
print("Unzip the images files...")
tarfile.open(flowers_archive_path).extractall(path=data_path)
if not os.path.isfile(img_label_path):
print("Downloading image labels...")
util.download_file(base_url + 'imagelabels.mat')
else:
print("Image labels already existed\n")
if not os.path.isfile(setid_path):
print("Downloading train/test/valid splits...")
util.download_file(base_url + 'setid.mat')
else:
print("Set split already existed\n")
setid = loadmat(setid_path)
# Read .mat file containing image labels.
img_labels = loadmat(img_label_path)['labels'][0]
img_labels -= 1
files = sorted(glob.glob(os.path.join(data_path, 'jpg', '*.jpg')))
labels = np.array([i for i in zip(files, img_labels)])
if os.path.exists(config.data_dir):
rmtree(config.data_dir, ignore_errors=True)
os.mkdir(config.data_dir)
idx_train = setid['trnid'][0] - 1
idx_test = setid['tstid'][0] - 1
idx_valid = setid['valid'][0] - 1
util.move_files('test', labels[idx_test, :])
util.move_files('train', labels[idx_train, :])
util.move_files('valid', labels[idx_valid, :])