-
Notifications
You must be signed in to change notification settings - Fork 83
/
create_lmdb.py
191 lines (150 loc) · 5.5 KB
/
create_lmdb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
import csv
import os
import os.path
import sys
import glob
import random
import caffe
from caffe.proto import caffe_pb2
import h5py
import lmdb
import numpy
import PIL.Image
import shutil
IMAGE_SIZE = 256
WORK_DIR = '/home/docker'
DATASET_DIR = WORK_DIR + '/data_set'
IMAGES_DIR = DATASET_DIR + '/images'
TRAIN_LMDB = DATASET_DIR +'/train_lmdb'
VAL_LMDB = DATASET_DIR + '/val_lmdb'
TRAIN_DIR = DATASET_DIR + '/train'
VAL_DIR = DATASET_DIR + '/val'
LABELS_CSV_FILE = WORK_DIR + '/data_set/labels.csv'
SPLIT_VAL_RATE = 3 #
def reset_dir(dir_path):
print "delete and make dir: {}".format(dir_path)
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
os.mkdir(dir_path)
def reset_dirs():
reset_dir(TRAIN_LMDB)
reset_dir(VAL_LMDB)
reset_dir(TRAIN_DIR)
reset_dir(VAL_DIR)
for label_name in labels():
train_dir = '{0}/{1}'.format(TRAIN_DIR, label_name)
val_dir = '{0}/{1}'.format(VAL_DIR, label_name)
reset_dir(train_dir)
reset_dir(val_dir)
def labels():
dirs = []
for item in os.listdir(IMAGES_DIR):
if os.path.isdir(os.path.join(IMAGES_DIR, item)):
dirs.append(item)
dirs.sort()
return dirs
def write_labels_csv_file():
with open(LABELS_CSV_FILE, 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=str(','), quoting=csv.QUOTE_MINIMAL)
for i, label in enumerate(labels()):
writer.writerow([i, label])
def get_label_name(path):
return os.path.basename(os.path.dirname(path))
def get_label_index(path):
return labels().index(get_label_name(path))
def split_train_val():
reset_dirs()
paths = glob.glob(IMAGES_DIR + '/*/*.jpg')
paths.sort()
print 'exec split train val. all images num: {}'.format(len(paths))
for path in paths:
rand = random.randint(1, 10)
label_name = get_label_name(path)
if rand <= SPLIT_VAL_RATE:
print 'split to val: {}'.format(path)
dir_name = '{0}/{1}'.format(VAL_DIR, label_name)
else:
print 'split to train: {}'.format(path)
dir_name = '{0}/{1}'.format(TRAIN_DIR, label_name)
shutil.copy(path, dir_name)
def make_lmdb(db_path, paths):
print 'create db: {0}'.format(db_path)
os.system('rm -rf ' + db_path)
random.shuffle(paths)
in_db = lmdb.open(db_path, map_size=int(1e12))
with in_db.begin(write=True) as in_txt:
for i, path in enumerate(paths):
label_index = get_label_index(path)
image = caffe.io.load_image(path)
image = caffe.io.resize_image(image, (IMAGE_SIZE, IMAGE_SIZE,))
# height, width, channels to channels, height, width
image = numpy.rollaxis(image, 2).astype(float)
datum = caffe.io.array_to_datum(image, label=label_index)
in_txt.put('{:0>5d}'.format(i), datum.SerializeToString())
print '{0:0>8d}:{1}'.format(i, path)
in_db.close()
print 'complete created: {0}'.format(db_path)
def make_hd5(db_path, paths):
print 'create db: {0}'.format(db_path)
os.system('rm -rf ' + db_path)
random.shuffle(paths)
paths = paths[:20]
datas = numpy.zeros([len(paths), 3, 256, 256], numpy.float64)
data_labels = numpy.zeros([len(paths), 2], numpy.float32)
def get_image(path):
image = caffe.io.load_image(path)
image = caffe.io.resize_image(image, (IMAGE_SIZE, IMAGE_SIZE,))
# height, width, channels to channels, height, width
image = numpy.rollaxis(image, 2).astype(float)
return image
for i, path in enumerate(paths):
label_index = get_label_index(path)
image = get_image(path)
print image.shape
print image.dtype
datas[i, : ,: ,:] = image
data_labels[i, :] = [label_index, label_index]
print '{0:0>8d}:{1}'.format(i, path)
f = h5py.File(db_path, "w")
f.create_dataset("data", data=datas, compression="gzip", compression_opts=4)
f.create_dataset("label", data=data_labels, compression="gzip", compression_opts=4)
f.close()
print data_labels
def make_lmdbs():
train_paths = glob.glob(TRAIN_DIR + '/*/*.jpg')
val_paths = glob.glob(VAL_DIR + '/*/*.jpg')
# make_hd5(TRAIN_LMDB, train_paths)
# make_hd5(VAL_LMDB, val_paths)
make_lmdb(TRAIN_LMDB, train_paths)
make_lmdb(VAL_LMDB, val_paths)
def augmentation(paths):
for path in paths:
dir_name = os.path.dirname(path)
base_name = os.path.basename(path)
img = PIL.Image.open(path)
for i in range(-20, 20 + 1, 5):
if i == 0:
continue
out = "{0}/{1}_{2}".format(dir_name, i, base_name)
rgba_img = img.convert('RGBA')
# rotated image
rot_img = rgba_img.rotate(i, expand=1)
# a white image same size as rotated image
white_img = PIL.Image.new('RGBA', rot_img.size, (255,)*4)
# create a composite image using the alpha layer of rot as a mask
tmp = PIL.Image.composite(rot_img, white_img, rot_img)
# save your work (converting back to mode='1' or whatever..)
tmp.convert(img.mode).save(out)
print "save rotate {0}: {1}".format(i, out)
def augmentations():
train_paths = glob.glob(TRAIN_DIR + '/*/*.jpg')
val_paths = glob.glob(VAL_DIR + '/*/*.jpg')
augmentation(train_paths)
augmentation(val_paths)
if __name__ == "__main__":
reset_dirs()
split_train_val()
# augmentations()
make_lmdbs()
write_labels_csv_file()