提交 03a0ae1d 编写于 作者: Y Yuan Gao 提交者: qingqing01

multiprocess data reader (#849)

上级 d300e5e4
...@@ -216,7 +216,7 @@ def distort_image(img, settings): ...@@ -216,7 +216,7 @@ def distort_image(img, settings):
def expand_image(img, bbox_labels, img_width, img_height, settings): def expand_image(img, bbox_labels, img_width, img_height, settings):
prob = random.uniform(0, 1) prob = random.uniform(0, 1)
if prob < settings._expand_prob: if prob < settings._expand_prob:
if _expand_max_ratio - 1 >= 0.01: if settings._expand_max_ratio - 1 >= 0.01:
expand_ratio = random.uniform(1, settings._expand_max_ratio) expand_ratio = random.uniform(1, settings._expand_max_ratio)
height = int(img_height * expand_ratio) height = int(img_height * expand_ratio)
width = int(img_width * expand_ratio) width = int(img_width * expand_ratio)
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import paddle
import image_util import image_util
from paddle.utils.image_util import * from paddle.utils.image_util import *
import random import random
...@@ -22,6 +23,7 @@ import xml.etree.ElementTree ...@@ -22,6 +23,7 @@ import xml.etree.ElementTree
import os import os
import time import time
import copy import copy
import functools
class Settings(object): class Settings(object):
...@@ -36,6 +38,8 @@ class Settings(object): ...@@ -36,6 +38,8 @@ class Settings(object):
for line in open(label_fpath): for line in open(label_fpath):
self._label_list.append(line.strip()) self._label_list.append(line.strip())
self._thread = 2
self._buf_size = 2048
self._apply_distort = apply_distort self._apply_distort = apply_distort
self._apply_expand = apply_expand self._apply_expand = apply_expand
self._resize_height = resize_h self._resize_height = resize_h
...@@ -94,46 +98,8 @@ class Settings(object): ...@@ -94,46 +98,8 @@ class Settings(object):
return self._img_mean return self._img_mean
def _reader_creator(settings, file_list, mode, shuffle): def process_image(sample, settings, mode):
def reader(): img = Image.open(sample[0])
if settings.dataset == 'coco':
# cocoapi
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
coco = COCO(file_list)
image_ids = coco.getImgIds()
images = coco.loadImgs(image_ids)
category_ids = coco.getCatIds()
category_names = [
item['name'] for item in coco.loadCats(category_ids)
]
elif settings.dataset == 'pascalvoc':
flist = open(file_list)
images = [line.strip() for line in flist]
if not settings.toy == 0:
images = images[:settings.toy] if len(
images) > settings.toy else images
print("{} on {} with {} images".format(mode, settings.dataset,
len(images)))
if shuffle:
random.shuffle(images)
for image in images:
if settings.dataset == 'coco':
image_name = image['file_name']
image_path = os.path.join(settings.data_dir, image_name)
elif settings.dataset == 'pascalvoc':
if mode == 'train' or mode == 'test':
image_path, label_path = image.split()
image_path = os.path.join(settings.data_dir, image_path)
label_path = os.path.join(settings.data_dir, label_path)
elif mode == 'infer':
image_path = os.path.join(settings.data_dir, image)
img = Image.open(image_path)
if img.mode == 'L': if img.mode == 'L':
img = img.convert('RGB') img = img.convert('RGB')
img_width, img_height = img.size img_width, img_height = img.size
...@@ -167,24 +133,18 @@ def _reader_creator(settings, file_list, mode, shuffle): ...@@ -167,24 +133,18 @@ def _reader_creator(settings, file_list, mode, shuffle):
elif settings.dataset == 'pascalvoc': elif settings.dataset == 'pascalvoc':
# layout: label | xmin | ymin | xmax | ymax | difficult # layout: label | xmin | ymin | xmax | ymax | difficult
bbox_labels = [] bbox_labels = []
root = xml.etree.ElementTree.parse(label_path).getroot() root = xml.etree.ElementTree.parse(sample[1]).getroot()
for object in root.findall('object'): for object in root.findall('object'):
bbox_sample = [] bbox_sample = []
# start from 1 # start from 1
bbox_sample.append( bbox_sample.append(
float( float(settings.label_list.index(object.find('name').text)))
settings.label_list.index(
object.find('name').text)))
bbox = object.find('bndbox') bbox = object.find('bndbox')
difficult = float(object.find('difficult').text) difficult = float(object.find('difficult').text)
bbox_sample.append( bbox_sample.append(float(bbox.find('xmin').text) / img_width)
float(bbox.find('xmin').text) / img_width) bbox_sample.append(float(bbox.find('ymin').text) / img_height)
bbox_sample.append( bbox_sample.append(float(bbox.find('xmax').text) / img_width)
float(bbox.find('ymin').text) / img_height) bbox_sample.append(float(bbox.find('ymax').text) / img_height)
bbox_sample.append(
float(bbox.find('xmax').text) / img_width)
bbox_sample.append(
float(bbox.find('ymax').text) / img_height)
bbox_sample.append(difficult) bbox_sample.append(difficult)
bbox_labels.append(bbox_sample) bbox_labels.append(bbox_sample)
...@@ -212,19 +172,17 @@ def _reader_creator(settings, file_list, mode, shuffle): ...@@ -212,19 +172,17 @@ def _reader_creator(settings, file_list, mode, shuffle):
batch_sampler.append( batch_sampler.append(
image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.0, 1.0)) image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.0, 1.0))
""" random crop """ """ random crop """
sampled_bbox = image_util.generate_batch_samples( sampled_bbox = image_util.generate_batch_samples(batch_sampler,
batch_sampler, bbox_labels, img_width, img_height) bbox_labels)
img = np.array(img) img = np.array(img)
if len(sampled_bbox) > 0: if len(sampled_bbox) > 0:
idx = int(random.uniform(0, len(sampled_bbox))) idx = int(random.uniform(0, len(sampled_bbox)))
img, sample_labels = image_util.crop_image( img, sample_labels = image_util.crop_image(
img, bbox_labels, sampled_bbox[idx], img_width, img, bbox_labels, sampled_bbox[idx], img_width, img_height)
img_height)
img = Image.fromarray(img) img = Image.fromarray(img)
img = img.resize((settings.resize_w, settings.resize_h), img = img.resize((settings.resize_w, settings.resize_h), Image.ANTIALIAS)
Image.ANTIALIAS)
img = np.array(img) img = np.array(img)
if mode == 'train': if mode == 'train':
...@@ -249,16 +207,58 @@ def _reader_creator(settings, file_list, mode, shuffle): ...@@ -249,16 +207,58 @@ def _reader_creator(settings, file_list, mode, shuffle):
sample_labels = np.array(sample_labels) sample_labels = np.array(sample_labels)
if mode == 'train' or mode == 'test': if mode == 'train' or mode == 'test':
if mode == 'train' and len(sample_labels) == 0: continue if len(sample_labels) != 0:
if mode == 'test' and len(sample_labels) == 0: continue return img.astype(
yield img.astype( 'float32'), sample_labels[:, 1:5], sample_labels[:, 0].astype(
'float32'
), sample_labels[:, 1:5], sample_labels[:, 0].astype(
'int32'), sample_labels[:, -1].astype('int32') 'int32'), sample_labels[:, -1].astype('int32')
elif mode == 'infer': elif mode == 'infer':
yield img.astype('float32') return img.astype('float32')
def _reader_creator(settings, file_list, mode, shuffle):
def reader():
if settings.dataset == 'coco':
# cocoapi
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
coco = COCO(file_list)
image_ids = coco.getImgIds()
images = coco.loadImgs(image_ids)
category_ids = coco.getCatIds()
category_names = [
item['name'] for item in coco.loadCats(category_ids)
]
elif settings.dataset == 'pascalvoc':
flist = open(file_list)
images = [line.strip() for line in flist]
if not settings.toy == 0:
images = images[:settings.toy] if len(
images) > settings.toy else images
print("{} on {} with {} images".format(mode, settings.dataset,
len(images)))
if shuffle:
random.shuffle(images)
for image in images:
if settings.dataset == 'coco':
image_name = image['file_name']
image_path = os.path.join(settings.data_dir, image_name)
yield [image_path]
elif settings.dataset == 'pascalvoc':
if mode == 'train' or mode == 'test':
image_path, label_path = image.split()
image_path = os.path.join(settings.data_dir, image_path)
label_path = os.path.join(settings.data_dir, label_path)
yield image_path, label_path
elif mode == 'infer':
image_path = os.path.join(settings.data_dir, image)
yield [image_path]
return reader mapper = functools.partial(process_image, mode=mode, settings=settings)
return paddle.reader.xmap_readers(mapper, reader, settings._thread,
settings._buf_size)
def draw_bounding_box_on_image(image, def draw_bounding_box_on_image(image,
......
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import reader import reader
import load_model as load_model
from mobilenet_ssd import mobile_net from mobilenet_ssd import mobile_net
from utility import add_arguments, print_arguments from utility import add_arguments, print_arguments
import os import os
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册