Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-664] Support integer type in ImageIter #11864

Merged
merged 1 commit into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions python/mxnet/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,16 +1057,19 @@ class ImageIter(io.DataIter):
Data name for provided symbols.
label_name : str
Label name for provided symbols.
dtype : str
Label data type. Default: float32. Other options: int32, int64, float64
kwargs : ...
More arguments for creating augmenter. See mx.image.CreateAugmenter.
"""

def __init__(self, batch_size, data_shape, label_width=1,
path_imgrec=None, path_imglist=None, path_root=None, path_imgidx=None,
shuffle=False, part_index=0, num_parts=1, aug_list=None, imglist=None,
data_name='data', label_name='softmax_label', **kwargs):
data_name='data', label_name='softmax_label', dtype='float32', **kwargs):
super(ImageIter, self).__init__()
assert path_imgrec or path_imglist or (isinstance(imglist, list))
assert dtype in ['int32', 'float32', 'int64', 'float64'], dtype + ' label not supported'
num_threads = os.environ.get('MXNET_CPU_WORKER_NTHREADS', 1)
logging.info('Using %s threads for decoding...', str(num_threads))
logging.info('Set enviroment variable MXNET_CPU_WORKER_NTHREADS to a'
Expand All @@ -1091,7 +1094,7 @@ def __init__(self, batch_size, data_shape, label_width=1,
imgkeys = []
for line in iter(fin.readline, ''):
line = line.strip().split('\t')
label = nd.array([float(i) for i in line[1:-1]])
label = nd.array(line[1:-1], dtype=dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I pass dtype='int8' or something that is not supported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int8 specifically doesn't cause any issue. but yes, generally, a check is required. Adding an assert for the next iteration. Adding a test for the default case too.

key = int(line[0])
imglist[key] = (label, line[-1])
imgkeys.append(key)
Expand All @@ -1105,11 +1108,11 @@ def __init__(self, batch_size, data_shape, label_width=1,
key = str(index) # pylint: disable=redefined-variable-type
index += 1
if len(img) > 2:
label = nd.array(img[:-1])
label = nd.array(img[:-1], dtype=dtype)
elif isinstance(img[0], numeric_types):
label = nd.array([img[0]])
label = nd.array([img[0]], dtype=dtype)
else:
label = nd.array(img[0])
label = nd.array(img[0], dtype=dtype)
result[key] = (label, img[-1])
imgkeys.append(str(key))
self.imglist = result
Expand Down
39 changes: 23 additions & 16 deletions tests/python/unittest/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,33 @@ def test_color_normalize(self):


def test_imageiter(self):
im_list = [[np.random.randint(0, 5), x] for x in TestImage.IMAGES]
test_iter = mx.image.ImageIter(2, (3, 224, 224), label_width=1, imglist=im_list,
path_root='')
for _ in range(3):
def check_imageiter(dtype='float32'):
im_list = [[np.random.randint(0, 5), x] for x in TestImage.IMAGES]
test_iter = mx.image.ImageIter(2, (3, 224, 224), label_width=1, imglist=im_list,
path_root='', dtype=dtype)
for _ in range(3):
for batch in test_iter:
pass
test_iter.reset()

# test with list file
fname = './data/test_imageiter.lst'
file_list = ['\t'.join([str(k), str(np.random.randint(0, 5)), x]) \
for k, x in enumerate(TestImage.IMAGES)]
with open(fname, 'w') as f:
for line in file_list:
f.write(line + '\n')

test_iter = mx.image.ImageIter(2, (3, 224, 224), label_width=1, path_imglist=fname,
path_root='', dtype=dtype)
for batch in test_iter:
pass
test_iter.reset()

# test with list file
fname = './data/test_imageiter.lst'
file_list = ['\t'.join([str(k), str(np.random.randint(0, 5)), x]) \
for k, x in enumerate(TestImage.IMAGES)]
with open(fname, 'w') as f:
for line in file_list:
f.write(line + '\n')
for dtype in ['int32', 'float32', 'int64', 'float64']:
check_imageiter(dtype)

test_iter = mx.image.ImageIter(2, (3, 224, 224), label_width=1, path_imglist=fname,
path_root='')
for batch in test_iter:
pass
# test with default dtype
check_imageiter()

@with_seed()
def test_augmenters(self):
Expand Down