Add flowers dataset for image classification model

gangliao-patch-1
wanghaoshuang@baidu.com 8 years ago committed by wanghaoshuang
parent b15b26374b
commit 2799b0ec50

File diff suppressed because it is too large Load Diff

@ -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()

@ -1,14 +1,14 @@
import numpy as np import numpy as np
try: try:
import cv2 import cv2
except: except ImportError:
print( cv2 = None
"import cv2 error, please install opencv-python: pip install opencv-python"
) from cv2 import resize
__all__ = [ __all__ = [
"load_image", "resize_short", "to_chw", "center_crop", "random_crop", "load_image_bytes", "load_image", "resize_short", "to_chw", "center_crop",
"left_right_flip", "simple_transform", "load_and_transform" "random_crop", "left_right_flip", "simple_transform", "load_and_transform"
] ]
""" """
This file contains some common interfaces for image preprocess. This file contains some common interfaces for image preprocess.
@ -28,6 +28,28 @@ the image layout as follows.
""" """
def load_image_bytes(bytes, is_color=True):
"""
Load an color or gray image from bytes array.
Example usage:
.. code-block:: python
with open('cat.jpg') as f:
im = load_image(f.read())
:param bytes: the input image bytes array.
:type file: str
:param is_color: If set is_color True, it will load and
return a color image. Otherwise, it will
load and return a gray image.
"""
flag = 1 if is_color else 0
file_bytes = np.asarray(bytearray(bytes), dtype=np.uint8)
img = cv2.imdecode(file_bytes, flag)
return img
def load_image(file, is_color=True): def load_image(file, is_color=True):
""" """
Load an color or gray image from the file path. Load an color or gray image from the file path.
@ -76,7 +98,7 @@ def resize_short(im, size):
h_new = size * h / w h_new = size * h / w
else: else:
w_new = size * w / h w_new = size * w / h
im = cv2.resize(im, (h_new, w_new), interpolation=cv2.INTER_CUBIC) im = resize(im, (h_new, w_new), interpolation=cv2.INTER_CUBIC)
return im return im

@ -14,13 +14,15 @@
__all__ = [ __all__ = [
'map_readers', 'buffered', 'compose', 'chain', 'shuffle', 'map_readers', 'buffered', 'compose', 'chain', 'shuffle',
'ComposeNotAligned', 'firstn' 'ComposeNotAligned', 'firstn', 'xmap'
] ]
import itertools import itertools
import random import random
from Queue import Queue from Queue import Queue
from threading import Thread from threading import Thread
from multiprocessing import Queue as MQueue
from multiprocessing import Process
def map_readers(func, *readers): def map_readers(func, *readers):
@ -224,3 +226,74 @@ def firstn(reader, n):
yield item yield item
return firstn_reader return firstn_reader
class XmapEndSignal():
pass
def xmap(mapper, reader, process_num, buffer_size):
"""
Use multiprocess to map samples from reader by a mapper defined by user.
And this function contains a buffered decorator.
:param mapper: a function to map sample.
:type mapper: callable
:param reader: the data reader to read from
:type reader: callable
:param process_num: process number to handle original sample
:type process_num: int
:param buffer_size: max buffer size
:type buffer_size: int
:return: the decarated reader
:rtype: callable
"""
end = XmapEndSignal()
in_queue = MQueue(buffer_size)
out_queue = MQueue(buffer_size)
# define a worker to read samples from reader to in_queue
def read_worker(reader, in_queue):
for i in reader():
in_queue.put(i)
in_queue.put(end)
# start a read worker in a thread
t = Thread(target=read_worker, args=(reader, in_queue))
t.daemon = True
t.start()
# define a worker to handle samples from in_queue by mapper
# and put mapped samples into out_queue
def handle_worker(in_queue, out_queue, mapper):
sample = in_queue.get()
while not isinstance(sample, XmapEndSignal):
r = mapper(sample)
out_queue.put(r)
sample = in_queue.get()
in_queue.put(end)
out_queue.put(end)
# start several handle_workers
workers = []
for i in xrange(process_num):
worker = Process(
target=handle_worker, args=(in_queue, out_queue, mapper))
worker.daemon = True
workers.append(worker)
for w in workers:
w.start()
def xreader():
sample = out_queue.get()
while not isinstance(sample, XmapEndSignal):
yield sample
sample = out_queue.get()
finish = 1
while finish < process_num:
sample = out_queue.get()
if isinstance(sample, XmapEndSignal):
finish += 1
else:
yield sample
return xreader

Loading…
Cancel
Save