forked from tatz1101/Edge-AI-Platform-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_write_cats-vs-dogs_images.py
More file actions
337 lines (225 loc) · 9.2 KB
/
1_write_cats-vs-dogs_images.py
File metadata and controls
337 lines (225 loc) · 9.2 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# USAGE
# python 1_write_cats-vs-dogs_images.py
# (optional) -p /home/danieleb/ML/cats-vs-dogs/input/jpg
# by daniele.bagni@xilinx.com
# ##################################################################################################
# set the matplotlib backend before any other backend, so that figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
import warnings
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
import numpy as np
# import the necessary packages
#from sklearn.preprocessing import LabelBinarizer
#from sklearn.metrics import classification_report
from config import cats_vs_dogs_config as config
from datetime import datetime
import matplotlib.pyplot as plt
import cv2
import os
import argparse
import glob
import sys
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--pathname", default=config.PROJ_JPG_DIR, help="path to the dataset")
args = vars(ap.parse_args())
path_root = args["pathname"] # root path name of dataset
if (not os.path.exists(path_root)): # create "path_root" directory if it does not exist
#os.mkdir(path_root)
print 'ERROR: you need the directory with the jpg files'
sys.exit(0)
# ##################################################################################################
#Size of images
IMAGE_WIDTH = 256 #227
IMAGE_HEIGHT = 256 #227
# ##################################################################################################
def transform_img(img, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT):
#Histogram Equalization
#img[:, :, 0] = cv2.equalizeHist(img[:, :, 0])
#img[:, :, 1] = cv2.equalizeHist(img[:, :, 1])
#img[:, :, 2] = cv2.equalizeHist(img[:, :, 2])
#Image Resizing
img = cv2.resize(img, (img_width, img_height), interpolation = cv2.INTER_CUBIC)
return img
# ##################################################################################################
labelNames = ["cat", "dog", "others"]
images_path = [img for img in glob.glob(path_root + "/*/*.jpg")]
# ##################################################################################################
print 'BUILD THE VALIDATION SET with 4000 images: 2000 per each class'
wrk_dir = path_root + "/val"
if (not os.path.exists(wrk_dir)): # create "val" directory if it does not exist
os.mkdir(wrk_dir)
f_test = open(wrk_dir+"/validation.txt", "w") #open file valid.txt"
f_lab = open(wrk_dir+"/labels.txt", "w") #open file labels.txt"
for s in [0,1,2]:
string = "%s\n" % labelNames[s]
f_lab.write(string)
f_lab.close()
counter = [-1,-1, 0]
val_count = 0
for in_idx, img_path in enumerate(images_path):
#print "DBG: now processing image ", img_path
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = transform_img(img, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT)
image2 = img.astype("int")
if '/jpg/cats/' in img_path:
label = 0
filename = img_path.split("/cats/")[1]
elif '/jpg/dogs/' in img_path:
label = 1
filename = img_path.split("/dogs/")[1]
else: # other
label = 2
filename = "others.jpg"
print 'ERROR: your path name does not contain "/jpg/" '
sys.exit(0)
counter[ label ] = counter[ label ] +1;
if (counter[ label ] <= 10499) : #skip the first 10500 images of each class and take the last 2000
continue
val_count = val_count + 1
string = "%05d" % counter[ label ]
class_name = labelNames[label]
path_name = wrk_dir + "/" + class_name
if (not os.path.exists(path_name)): # create directory if it does not exist
os.mkdir(path_name) #https://github.com/BVLC/caffe/issues/3698
path_name = wrk_dir + "/" + class_name + "/" + filename
string = " %1d" % label
f_test.write(path_name + string + "\n")
cv2.imwrite(path_name, image2)
print(path_name)
f_test.close()
# ##################################################################################################
print 'BUILD THE TEST SET with 1000 images of size 227 x 277'
wrk_dir = path_root + "/test"
if (not os.path.exists(wrk_dir)): # create "test" directory if it does not exist
os.mkdir(wrk_dir)
f_test = open(wrk_dir+"/test.txt", "w") #open file test.txt"
f_lab = open(wrk_dir+"/labels.txt", "w") #open file labels.txt"
for s in [0,1,2]:
string = "%s\n" % labelNames[s]
f_lab.write(string)
f_lab.close()
counter = [-1, -1, 0]
test_count = -1
for in_idx, img_path in enumerate(images_path):
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = transform_img(img, img_width=227, img_height=227)
image2 = img.astype("int")
if '/jpg/cats/' in img_path:
label = 0
filename = img_path.split("/cats/")[1]
elif '/jpg/dogs/' in img_path:
label = 1
filename = img_path.split("/dogs/")[1]
else: # other
label = 2
filename = "others.jpg"
print 'ERROR: your path name does not contain "/jpg/" '
sys.exit(0)
counter[ label ] = counter[ label ] +1;
if (counter[ label ] <= 9999) or (counter[ label ] > 10499) : #take the images from 10000 to 10500
continue
test_count = test_count +1
string = " %04d" % test_count
class_name = labelNames[label]
path_name = wrk_dir + "/" + filename
f_test.write(path_name + string + "\n")
cv2.imwrite(path_name, image2)
#cv2.imshow(labelNames[label], image2)
#cv2.waitKey(0)
print(path_name)
f_test.close()
print "Test set contains ", test_count+1, " images"
# ##################################################################################################
print 'BUILD THE TRAIN IMAGES SET with 20000 images'
wrk_dir = path_root + "/train"
if (not os.path.exists(wrk_dir)): # create "train" directory if it does not exist
os.mkdir(wrk_dir)
f_test = open(wrk_dir + "/train.txt", "w") #open file test.txt"
f_lab = open(wrk_dir + "/labels.txt", "w") #open file labels.txt"
for s in [0,1,2]:
string = "%s\n" % labelNames[s]
f_lab.write(string)
f_lab.close()
counter = [-1,-1,0]
train_count = 0
for in_idx, img_path in enumerate(images_path):
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = transform_img(img, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT)
image2 = img.astype("int")
if '/jpg/cats/' in img_path:
label = 0
filename = img_path.split("/cats/")[1]
elif '/jpg/dogs/' in img_path:
label = 1
filename = img_path.split("/dogs/")[1]
else: # other
label = 2
filename = "others.jpg"
print 'ERROR: your path name does not contain "/jpg/" '
sys.exit(0)
counter[ label ] = counter[ label ] +1;
if (counter[ label ] > 9999) : #skip images after the first 10000
continue
train_count = train_count +1
string = "%05d" % counter[ label ]
class_name = labelNames[label]
path_name = wrk_dir + "/" + class_name
if (not os.path.exists(path_name)): # create directory if it does not exist
os.mkdir(path_name)
path_name = wrk_dir + "/" + class_name + "/" + filename
string = " %1d" % label
f_test.write(path_name + string + "\n")
cv2.imwrite(path_name, image2)
#cv2.imshow(labelNames[label], image2)
#cv2.waitKey(0)
#print(path_name)
f_test.close()
# ##################################################################################################
print 'BUILD THE CALIBRATION IMAGES SET with 200 images'
wrk_dir = path_root + "/calib"
if (not os.path.exists(wrk_dir)): # create "calibration" directory if it does not exist
os.mkdir(wrk_dir)
f_calib = open(wrk_dir + "/calibration.txt", "w") #open file calibration.txt"
for s in [0,1,2]:
string = "%s\n" % labelNames[s]
counter = [-1,-1,0]
calib_count = -1
for in_idx, img_path in enumerate(images_path):
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = transform_img(img, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT)
image2 = img.astype("int")
if '/jpg/cats/' in img_path:
label = 0
filename = img_path.split("/cats/")[1]
elif '/jpg/dogs/' in img_path:
label = 1
filename = img_path.split("/dogs/")[1]
else: # other
label = 2
filename = "others.jpg"
print 'ERROR: your path name does not contain "/jpg/" '
sys.exit(0)
counter[ label ] = counter[ label ] +1;
if (counter[ label ] > 99) : #take only the first 100 images per each class
continue
calib_count = calib_count + 1
string = "%05d" % counter[ label]
class_name = labelNames[ label ]
path_name = wrk_dir + "/" + class_name
if (not os.path.exists(path_name)): # create directory if it does not exist
os.mkdir(path_name)
path_name = wrk_dir + "/" + class_name + "/" + filename
string2 = " %1d" % int(calib_count)
f_calib.write(class_name + "/" + filename + string2 + "\n")
cv2.imwrite(path_name, image2)
#cv2.imshow(labelNames[int(testY[int(i)])], image2)
#cv2.waitKey(0)
#print(path_name)
f_calib.close()
print "Train set contains ", train_count, " images"
print "Validation set contains ", val_count, " images"
print "Calibrationset contains ", calib_count+1, " images"
print("END\n")