Skip to content

Instancenorm onnx#14858

Merged
alalek merged 5 commits intoopencv:3.4from
dvd42:instancenorm_onnx
Jul 4, 2019
Merged

Instancenorm onnx#14858
alalek merged 5 commits intoopencv:3.4from
dvd42:instancenorm_onnx

Conversation

@dvd42
Copy link
Copy Markdown
Contributor

@dvd42 dvd42 commented Jun 21, 2019

Merge with extra: opencv/opencv_extra#627

This Pull request is an implementation of the InstaceNorm operation for the ONNX importer. I am opening this draft PR with the hopes of getting some reviewers to look at the issue I ve been having with it.

The first Issue is that I use the cv::meanStdDev function from OpenCv inside the layer, this operation messes up the Std computation, making it differ from the one computed by the one in my test model (implemented in PyTorch). Making it fail the normAssert test in my test case.

The second issue is that this implementation seems to break the MVN layer test for some Caffe models. Which is very strange given the fact that I have not touched that layer implementation.

The implementation seems to be correct except for this precision issue, so I am looking for some help regarding both these issues.

PS: The only reason I am opening the PR and not a separate issue is because this way the reviewers can easily see all the files that I have changed.

**WIP**
force_builders=Custom,Custom Win,Custom Mac
build_image:Custom=ubuntu-openvino-2019r1:16.04
build_image:Custom Win=openvino-2019r1
build_image:Custom Mac=openvino-2019r1

test_modules:Custom=dnn,python2,python3,java
test_modules:Custom Win=dnn,python2,python3,java
test_modules:Custom Mac=dnn,python2,python3,java

buildworker:Custom=linux-1
test_opencl:Custom=ON
test_bigdata:Custom=1
test_filter:Custom=*

@dvd42 dvd42 force-pushed the instancenorm_onnx branch from d6b4d0b to a4e67e1 Compare June 21, 2019 15:26
@alalek
Copy link
Copy Markdown
Member

alalek commented Jun 27, 2019

This patch should go into 3.4 branch first. We will merge changes from 3.4 into master regularly (weekly/bi-weekly).

The same note is about opencv_extra's PR.

So, please:

  • change "base" branch of this PR: master => 3.4 (use "Edit" button near PR title)
  • rebase your commits from master onto 3.4 branch. For example:
    git rebase -i --onto upstream/3.4 upstream/master
    (check list of your commits, save and quit (Esc + "wq" + Enter)
    where upstream is configured by following this GitHub guide and fetched (git fetch upstream).
  • push rebased commits into source branch of your fork (with --force option)

Note: no needs to re-open PR, apply changes "inplace".

@dvd42 dvd42 changed the base branch from master to 3.4 June 27, 2019 20:04
@dvd42 dvd42 force-pushed the instancenorm_onnx branch from d3ab90f to 6840367 Compare June 27, 2019 20:09
@dvd42
Copy link
Copy Markdown
Contributor Author

dvd42 commented Jun 27, 2019

Hey @alalek Its done, thanks for your clear instructions! :). I assume this procedure must be done every time I open a PR, just in case 3.4 branch has changed, yes?

@alalek
Copy link
Copy Markdown
Member

alalek commented Jun 27, 2019

just in case 3.4 branch has changed

Only if conflicts observed and automatic merge is not possible.

Main problem here is to select "target branch for the patch" initially.
Any patches which can be applied onto 3.4 branch should go there first (bug fixes, additions, etc) while there is regular merges into master ("Merge-3.4" PRs).

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Jun 28, 2019

@dvd42, I were able to use the following code to pass your test case:

        else if (layer_type == "InstanceNormalization")
        {
            // InstanceNormalization = MVN + BatchNorm
            {
                LayerParams mvnLP;
                mvnLP.name = layerParams.name + "/mvn";
                mvnLP.type = "MVN";
                mvnLP.set("eps", layerParams.get<float>("epsilon"));
                layerParams.erase("epsilon");

                int id = dstNet.addLayer(mvnLP.name, mvnLP.type, mvnLP);

                // Connect to input
                layerId = layer_id.find(node_proto.input(0));
                CV_Assert(layerId != layer_id.end());
                dstNet.connect(layerId->second.layerId, layerId->second.outputId, id, 0);

                // Add shape
                layer_id.insert(std::make_pair(mvnLP.name, LayerInfo(id, 0)));
                outShapes[mvnLP.name] = outShapes[node_proto.input(0)];

                // Replace Batch Norm's input to MVN
                node_proto.set_input(0, mvnLP.name);
            }

            layerParams.type = "BatchNorm";
            Mat mean(3, 1, CV_32F, Scalar(0));
            Mat std(3, 1, CV_32F, Scalar(1));
            Mat weights = getBlob(node_proto, constBlobs, 1);
            Mat bias =  getBlob(node_proto, constBlobs, 2);

            layerParams.blobs.resize(4);
            layerParams.blobs[0] = mean;
            layerParams.blobs[1] = std;
            layerParams.blobs[2] = weights;
            layerParams.blobs[3] = bias;
            layerParams.set("has_bias", true);
            layerParams.set("has_weight", true);
        }

Please check it and try to find the difference with your implementation.

@dvd42 dvd42 force-pushed the instancenorm_onnx branch from 6110948 to 32b49eb Compare July 1, 2019 08:29
@dvd42 dvd42 force-pushed the instancenorm_onnx branch from 9092ebe to 3022b19 Compare July 1, 2019 13:05

layerParams.blobs.resize(2);

if (!node_proto.input(1).empty()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If node_proto.input_size() == 3 can we have an empty input name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In my experience no. However the same thing happens for the BatchNorm layer, and I was just trying to follow the same convention, also felt like an extra sanity check wouldnt hurt. Let me know if I should remove it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's assume that they aren't empty.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@dkurt, should be fixed now

layerId = layer_id.find(node_proto.input(0));
CV_Assert(layerId != layer_id.end());
dstNet.connect(layerId->second.layerId, layerId->second.outputId, id, 0);
shapeIt = outShapes.find(node_proto.input(0));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why it's not used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

node_proto.set_input(0, mvnParams.name);

layerParams.type = "BatchNorm";

Copy link
Copy Markdown
Member

@dkurt dkurt Jul 2, 2019

Choose a reason for hiding this comment

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

please avoid extra changes here and below (one empty line added and one removed)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@dvd42 dvd42 force-pushed the instancenorm_onnx branch 3 times, most recently from b518adf to c98f1da Compare July 2, 2019 08:37
@dvd42 dvd42 force-pushed the instancenorm_onnx branch 2 times, most recently from bd43273 to 7661f80 Compare July 4, 2019 11:06
@dvd42 dvd42 force-pushed the instancenorm_onnx branch from 2ee95b5 to 3e44d41 Compare July 4, 2019 14:14
Copy link
Copy Markdown
Member

@dkurt dkurt left a comment

Choose a reason for hiding this comment

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

👍 Thank you!

@alalek alalek merged commit 57fae4a into opencv:3.4 Jul 4, 2019
@alalek alalek mentioned this pull request Jul 9, 2019
dvd42 added a commit to dvd42/opencv that referenced this pull request Aug 6, 2019
Instancenorm onnx (opencv#14858)

* Onnx unsupported operation handling

* instance norm implementation

* Revert "Onnx unsupported operation handling"

* instance norm layer test

* onnx instancenorm layer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants