-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Relay][Frontend][Keras] NHWC import support. #4899
Conversation
I'm not familiar with keras, but I wonder why keras use NCHW while tensorflow uses NHWC? (I'm not familiar with tf either). I heard that since TF 2.0, keras became their official frontend or something like that. |
@masahi, Keras does use NHWC by default just like TensorFlow. However, because TVM is better optimized for NCHW, the Keras frontend previously converted all layers from NHWC to NCHW. This option allows us to keep the layers in their original format. |
@comaniac I think I've fixed all the style issues you pointed out. |
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.
LGTM. Thanks :)
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.
I would suggest to use ConvertLayout. ConvertLayout was designed for exactly this purpose.
@anijain2305, I think you're right that for most cases ConvertLayout will achieve something similar. However, there are some benefits to having layers directly parsed as NHWC. I actually did this PR because I'm working on a project that uses some funky custom Keras layers and it's very useful to parse them directly in NHWC. |
Given that the model is in native NHWC, perhaps it is better to do it in another way around(note that the old approach converts to NCHW on the fly), ingest as NHWC as in this PR and then call ConvertLayout |
@tqchen, you're right that would be a more direct way to apply ConvertLayout. If everyone else would prefer it, I could change the Keras frontend to only output in NHWC and then we would rely on ConvertLayout to get models into NCHW. |
I see. Yes, I meant that we should retain native framework format (NHWC in this case). And use ConvertLayout to change the layout to desired layout (passed on by relay.from_keras API). @jwfromm Yes, in my opinion, that is the cleanest way forward. Same thing needs to happen for TF framework. I can take care of that. Will appreciate if you can take care of Keras. |
That sounds good. |
Merging this for now as this PR is self contained. It would be great if we can have a followup PR that updates the frontend to use ConvertLayout from NHWC -> NCHW so we won't have to support two path in the frontend Thanks @anijain2305 @jwfromm @comaniac |
* Basic test working * Almost all tests working. * all tests passing. * Fixed lint. * Improved Style.
* Basic test working * Almost all tests working. * all tests passing. * Fixed lint. * Improved Style.
* Basic test working * Almost all tests working. * all tests passing. * Fixed lint. * Improved Style.
Currently the Keras importer can only output models with NCHW layout. This PR adds a layout argument to the Keras importer that allows NHWC to be used instead. This is useful for platforms that may have better NHWC than NCHW performance such as the Raspberry Pi.
A few notes: