-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a xmap decorator into reader module for optimizing performance #2242
Merged
wanghaoshuang
merged 3 commits into
PaddlePaddle:develop
from
wanghaoshuang:flowers_reader
Jun 6, 2017
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
This module will download dataset from | ||
http://www.robots.ox.ac.uk/~vgg/data/flowers/102/index.html | ||
and parse train/test set intopaddle reader creators. | ||
|
||
This set contains images of flowers belonging to 102 different categories. | ||
The images were acquired by searching the web and taking pictures. There are a | ||
minimum of 40 images for each category. | ||
|
||
The database was used in: | ||
|
||
Nilsback, M-E. and Zisserman, A. Automated flower classification over a large | ||
number of classes.Proceedings of the Indian Conference on Computer Vision, | ||
Graphics and Image Processing (2008) | ||
http://www.robots.ox.ac.uk/~vgg/publications/papers/nilsback08.{pdf,ps.gz}. | ||
|
||
""" | ||
import cPickle | ||
import itertools | ||
from common import download | ||
import tarfile | ||
import scipy.io as scio | ||
from paddle.v2.image import * | ||
import os | ||
import numpy as np | ||
import paddle.v2 as paddle | ||
from multiprocessing import cpu_count | ||
__all__ = ['train', 'test', 'valid'] | ||
|
||
DATA_URL = 'http://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz' | ||
LABEL_URL = 'http://www.robots.ox.ac.uk/~vgg/data/flowers/102/imagelabels.mat' | ||
SETID_URL = 'http://www.robots.ox.ac.uk/~vgg/data/flowers/102/setid.mat' | ||
DATA_MD5 = '52808999861908f626f3c1f4e79d11fa' | ||
LABEL_MD5 = 'e0620be6f572b9609742df49c70aed4d' | ||
SETID_MD5 = 'a5357ecc9cb78c4bef273ce3793fc85c' | ||
|
||
|
||
def default_mapper(sample): | ||
''' | ||
map image bytes data to type needed by model input layer | ||
''' | ||
img, label = sample | ||
img = paddle.image.load_image_bytes(img) | ||
img = paddle.image.simple_transform(img, 256, 224, True) | ||
return img.flatten().astype('float32'), label | ||
|
||
|
||
def reader_creator(data_file, | ||
label_file, | ||
setid_file, | ||
dataset_name, | ||
mapper=default_mapper, | ||
buffered_size=1024): | ||
''' | ||
1. read images from tar file and | ||
merge images into batch files in 102flowers.tgz_batch/ | ||
2. get a reader to read sample from batch file | ||
|
||
:param data_file: downloaded data file | ||
:type data_file: string | ||
:param label_file: downloaded label file | ||
:type label_file: string | ||
:param setid_file: downloaded setid file containing information | ||
about how to split dataset | ||
:type setid_file: string | ||
:param dataset_name: data set name (tstid|trnid|valid) | ||
:type dataset_name: string | ||
:param mapper: a function to map image bytes data to type | ||
needed by model input layer | ||
:type mapper: callable | ||
:param buffered_size: the size of buffer used to process images | ||
:type buffered_size: int | ||
:return: data reader | ||
:rtype: callable | ||
''' | ||
labels = scio.loadmat(label_file)['labels'][0] | ||
indexes = scio.loadmat(setid_file)[dataset_name][0] | ||
img2label = {} | ||
for i in indexes: | ||
img = "jpg/image_%05d.jpg" % i | ||
img2label[img] = labels[i - 1] | ||
file_list = batch_images_from_tar(data_file, dataset_name, img2label) | ||
|
||
def reader(): | ||
for file in open(file_list): | ||
file = file.strip() | ||
batch = None | ||
with open(file, 'r') as f: | ||
batch = cPickle.load(f) | ||
data = batch['data'] | ||
labels = batch['label'] | ||
for sample, label in itertools.izip(data, batch['label']): | ||
yield sample, int(label) | ||
|
||
return paddle.reader.xmap_readers(mapper, reader, | ||
cpu_count(), buffered_size) | ||
|
||
|
||
def train(mapper=default_mapper, buffered_size=1024): | ||
''' | ||
Create flowers training set reader. | ||
It returns a reader, each sample in the reader is | ||
image pixels in [0, 1] and label in [1, 102] | ||
translated from original color image by steps: | ||
1. resize to 256*256 | ||
2. random crop to 224*224 | ||
3. flatten | ||
:param mapper: a function to map sample. | ||
:type mapper: callable | ||
:param buffered_size: the size of buffer used to process images | ||
:type buffered_size: int | ||
:return: train data reader | ||
:rtype: callable | ||
''' | ||
return reader_creator( | ||
download(DATA_URL, 'flowers', DATA_MD5), | ||
download(LABEL_URL, 'flowers', LABEL_MD5), | ||
download(SETID_URL, 'flowers', SETID_MD5), 'trnid', mapper, | ||
buffered_size) | ||
|
||
|
||
def test(mapper=default_mapper, buffered_size=1024): | ||
''' | ||
Create flowers test set reader. | ||
It returns a reader, each sample in the reader is | ||
image pixels in [0, 1] and label in [1, 102] | ||
translated from original color image by steps: | ||
1. resize to 256*256 | ||
2. random crop to 224*224 | ||
3. flatten | ||
:param mapper: a function to map sample. | ||
:type mapper: callable | ||
:param buffered_size: the size of buffer used to process images | ||
:type buffered_size: int | ||
:return: test data reader | ||
:rtype: callable | ||
''' | ||
return reader_creator( | ||
download(DATA_URL, 'flowers', DATA_MD5), | ||
download(LABEL_URL, 'flowers', LABEL_MD5), | ||
download(SETID_URL, 'flowers', SETID_MD5), 'tstid', mapper, | ||
buffered_size) | ||
|
||
|
||
def valid(mapper=default_mapper, buffered_size=1024): | ||
''' | ||
Create flowers validation set reader. | ||
It returns a reader, each sample in the reader is | ||
image pixels in [0, 1] and label in [1, 102] | ||
translated from original color image by steps: | ||
1. resize to 256*256 | ||
2. random crop to 224*224 | ||
3. flatten | ||
:param mapper: a function to map sample. | ||
:type mapper: callable | ||
:param buffered_size: the size of buffer used to process images | ||
:type buffered_size: int | ||
:return: test data reader | ||
:rtype: callable | ||
''' | ||
return reader_creator( | ||
download(DATA_URL, 'flowers', DATA_MD5), | ||
download(LABEL_URL, 'flowers', LABEL_MD5), | ||
download(SETID_URL, 'flowers', SETID_MD5), 'valid', mapper, | ||
buffered_size) | ||
|
||
|
||
def fetch(): | ||
download(DATA_URL, 'flowers', DATA_MD5) | ||
download(LABEL_URL, 'flowers', LABEL_MD5) | ||
download(SETID_URL, 'flowers', SETID_MD5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import paddle.v2.dataset.flowers | ||
import unittest | ||
|
||
|
||
class TestFlowers(unittest.TestCase): | ||
def check_reader(self, reader): | ||
sum = 0 | ||
label = 0 | ||
size = 224 * 224 * 3 | ||
for l in reader(): | ||
self.assertEqual(l[0].size, size) | ||
if l[1] > label: | ||
label = l[1] | ||
sum += 1 | ||
return sum, label | ||
|
||
def test_train(self): | ||
instances, max_label_value = self.check_reader( | ||
paddle.v2.dataset.flowers.train()) | ||
self.assertEqual(instances, 1020) | ||
self.assertEqual(max_label_value, 102) | ||
|
||
def test_test(self): | ||
instances, max_label_value = self.check_reader( | ||
paddle.v2.dataset.flowers.test()) | ||
self.assertEqual(instances, 6149) | ||
self.assertEqual(max_label_value, 102) | ||
|
||
def test_valid(self): | ||
instances, max_label_value = self.check_reader( | ||
paddle.v2.dataset.flowers.valid()) | ||
self.assertEqual(instances, 1020) | ||
self.assertEqual(max_label_value, 102) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This module seems reading many images from the tarball. If so, it might be great if we can call
tarfile.next()
, which returns a TarFile objects liketarfile.extractfile
. Buttarfile.next()
reads files in the tarball one-by-one. This reduces the amount of disk seeks which reduces the number of moves of the magnetic head of our disk.