Skip to content

Commit 05204af

Browse files
qingqing01reyoung
authored andcommitted
Fix memory leak in image classification demo, which is caused by dataprovider (#323)
* the memory leak is inside one pass.
1 parent bd50f93 commit 05204af

File tree

6 files changed

+31
-19
lines changed

6 files changed

+31
-19
lines changed

demo/image_classification/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ plot.png
55
train.log
66
image_provider_copy_1.py
77
*pyc
8+
train.list
9+
test.list

demo/image_classification/data/download_cifar.sh

100644100755
File mode changed.

demo/image_classification/image_provider.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,29 @@ def hook(settings, img_size, mean_img_size, num_classes, color, meta, use_jpeg,
5858
settings.logger.info('DataProvider Initialization finished')
5959

6060

61-
@provider(init_hook=hook)
62-
def processData(settings, file_name):
61+
@provider(init_hook=hook, min_pool_size=0)
62+
def processData(settings, file_list):
6363
"""
6464
The main function for loading data.
6565
Load the batch, iterate all the images and labels in this batch.
66-
file_name: the batch file name.
66+
file_list: the batch file list.
6767
"""
68-
data = cPickle.load(io.open(file_name, 'rb'))
69-
indexes = list(range(len(data['images'])))
70-
if settings.is_train:
71-
random.shuffle(indexes)
72-
for i in indexes:
73-
if settings.use_jpeg == 1:
74-
img = image_util.decode_jpeg(data['images'][i])
75-
else:
76-
img = data['images'][i]
77-
img_feat = image_util.preprocess_img(img, settings.img_mean,
78-
settings.img_size, settings.is_train,
79-
settings.color)
80-
label = data['labels'][i]
81-
yield img_feat.tolist(), int(label)
68+
with open(file_list, 'r') as fdata:
69+
lines = [line.strip() for line in fdata]
70+
random.shuffle(lines)
71+
for file_name in lines:
72+
with io.open(file_name.strip(), 'rb') as file:
73+
data = cPickle.load(file)
74+
indexes = list(range(len(data['images'])))
75+
if settings.is_train:
76+
random.shuffle(indexes)
77+
for i in indexes:
78+
if settings.use_jpeg == 1:
79+
img = image_util.decode_jpeg(data['images'][i])
80+
else:
81+
img = data['images'][i]
82+
img_feat = image_util.preprocess_img(img, settings.img_mean,
83+
settings.img_size, settings.is_train,
84+
settings.color)
85+
label = data['labels'][i]
86+
yield img_feat.astype('float32'), int(label)

demo/image_classification/preprocess.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def option_parser():
3535
data_creator = ImageClassificationDatasetCreater(data_dir,
3636
processed_image_size,
3737
color)
38+
data_creator.train_list_name = "train.txt"
39+
data_creator.test_list_name = "test.txt"
3840
data_creator.num_per_batch = 1000
3941
data_creator.overwrite = True
4042
data_creator.create_batches()

demo/image_classification/preprocess.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ set -e
1717
data_dir=./data/cifar-out
1818

1919
python preprocess.py -i $data_dir -s 32 -c 1
20+
21+
echo "data/cifar-out/batches/train.txt" > train.list
22+
echo "data/cifar-out/batches/test.txt" > test.list

demo/image_classification/vgg_16_cifar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
'img_size': 32,'num_classes': 10,
2626
'use_jpeg': 1,'color': "color"}
2727

28-
define_py_data_sources2(train_list=data_dir+"train.list",
29-
test_list=data_dir+'test.list',
28+
define_py_data_sources2(train_list="train.list",
29+
test_list="train.list",
3030
module='image_provider',
3131
obj='processData',
3232
args=args)

0 commit comments

Comments
 (0)