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

mx.nd.Crop does not support FP16 and decpreciated but no direct alternative with central crop #13750

Open
wenyangchu opened this issue Dec 31, 2018 · 3 comments

Comments

@wenyangchu
Copy link
Contributor

wenyangchu commented Dec 31, 2018

Note: Providing complete information in the most concise form is the best way to get help. This issue template serves as the checklist for essential information to most of the technical issues and bug reports. For non-technical issues and feature requests, feel free to present the information in what you believe is the best form.

For Q & A and discussion, please start a discussion thread at https://discuss.mxnet.io

Description

I have been using mx.nd.Crop with "crop_like" behavior in Float32 training. When I switch to train network in 16 bit, it throws errors. This function is depreciated, but I do not find direct alternative. The closest one is mx.nd.slice_like but it does not have central crop option.

Environment info (Required)

----------Python Info----------
('Version :', '2.7.12')
('Compiler :', 'GCC 5.4.0 20160609')
('Build :', ('default', 'Dec 4 2017 14:50:18'))
('Arch :', ('64bit', 'ELF'))
------------Pip Info-----------
('Version :', '18.1')
('Directory :', '/data/wy/mxnet130/local/lib/python2.7/site-packages/pip')
----------MXNet Info-----------
('Version :', '1.3.1')
('Directory :', '/data/wy/mxnet130/local/lib/python2.7/site-packages/mxnet')
('Commit Hash :', '91c1f821c7816d59d666d21549ac524c863c46b2')
----------System Info----------
('Platform :', 'Linux-4.4.0-139-generic-x86_64-with-Ubuntu-16.04-xenial')
('system :', 'Linux')
('node :', 'ae')
('release :', '4.4.0-139-generic')
('version :', '#165-Ubuntu SMP Wed Oct 24 10:58:50 UTC 2018')
----------Hardware Info----------
('machine :', 'x86_64')
('processor :', 'x86_64')

Package used: Python

Error Message:

MXNetErrorTraceback (most recent call last)
in ()
1 data_cut = mx.nd.slice_like(data=data,shape_like=data_ref,axes =(2,3))
----> 2 cropped = mx.nd.Crop(data,data_ref, num_args = 2, center_crop = True, name = 'cropped')

/data/wy/mxnet130/local/lib/python2.7/site-packages/mxnet/ndarray/register.pyc in Crop(*data, **kwargs)

/data/wy/mxnet130/local/lib/python2.7/site-packages/mxnet/_ctypes/ndarray.pyc in _imperative_invoke(handle, ndargs, keys, vals, out)
90 c_str_array(keys),
91 c_str_array([str(s) for s in vals]),
---> 92 ctypes.byref(out_stypes)))
93
94 if original_output is not None:

/data/wy/mxnet130/local/lib/python2.7/site-packages/mxnet/base.pyc in check_call(ret)
250 """
251 if ret != 0:
--> 252 raise MXNetError(py_str(_LIB.MXGetLastError()))
253
254

MXNetError: [12:50:35] include/mxnet/operator.h:228: Check failed: in_type->at(i) == mshadow::default_type_flag || in_type->at(i) == -1 Unsupported data type 2

Steps to reproduce

(Paste the commands you ran that produced the error.)

import mxnet as mx
data = mx.nd.random.uniform(shape=(1,3,10,10),dtype='float16')
data_ref = mx.nd.ones((1,3,6,6),dtype='float16')
cropped = mx.nd.Crop(data,data_ref, num_args = 2, center_crop = True, name = 'cropped')

What have you tried to solve it?

  1. I tried to use slice_like to replace crop but I found out that central crop function is missing.
@mxnet-label-bot
Copy link
Contributor

Hey, this is the MXNet Label Bot.
Thank you for submitting the issue! I will try and suggest some labels so that the appropriate MXNet community members can help resolve it.
Here are my recommended labels: Performance, Bug

@vrakesh
Copy link
Contributor

vrakesh commented Dec 31, 2018

@mxnet-label-bot add [Feature request]
@wenyangchu Thank you for the feature request, we will look into this.

@wenyangchu
Copy link
Contributor Author

wenyangchu commented Dec 31, 2018

One more comment: Crop function is depreciated but I don't see the alternative slice function provides the same central crop option. And it has been used for some people including me in the unet like implementation. It is especially important when padding is not desired in the use case of applying unet in an extremely large image to avoid edge effect caused by padding.

I personally think the current Crop function is great and unique compared to tensorflow approach:

https://tf-unet.readthedocs.io/en/latest/_modules/tf_unet/layers.html#crop_and_concat

def crop_and_concat(x1,x2):
with tf.name_scope("crop_and_concat"):
x1_shape = tf.shape(x1)
x2_shape = tf.shape(x2)
# offsets for the top left corner of the crop
offsets = [0, (x1_shape[1] - x2_shape[1]) // 2, (x1_shape[2] - x2_shape[2]) // 2, 0]
size = [-1, x2_shape[1], x2_shape[2], -1]
x1_crop = tf.slice(x1, offsets, size)
return tf.concat([x1_crop, x2], 3)

Mxnet approach:
cropped = mx.nd.Crop(data,data_ref, num_args = 2, center_crop = True, name = 'cropped')

After many search and trials I failed to implement the above crop_and_concat function in mxnet using a hybridBlock. I can only do this in the imperative mode. it is not desired because I prefer to export onnx for my deployment. If someone can teach me how to implement the above function right with hybrid block, it is be very appreciated :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants