From 57fae4a6a1e5c06799f549db43922684ba028af4 Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 4 Jul 2019 20:15:04 +0200 Subject: [PATCH 1/6] Merge pull request #14858 from dvd42:instancenorm_onnx Instancenorm onnx (#14858) * Onnx unsupported operation handling * instance norm implementation * Revert "Onnx unsupported operation handling" * instance norm layer test * onnx instancenorm layer --- modules/dnn/src/onnx/onnx_importer.cpp | 37 +++++++++++++++++++++++++ modules/dnn/test/test_onnx_importer.cpp | 8 ++++++ 2 files changed, 45 insertions(+) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index b9fb0be624..5164fbeccb 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -546,6 +546,43 @@ void ONNXImporter::populateNet(Net dstNet) { replaceLayerParam(layerParams, "size", "local_size"); } + else if (layer_type == "InstanceNormalization") + { + if (node_proto.input_size() != 3) + CV_Error(Error::StsNotImplemented, + "Expected input, scale, bias"); + + layerParams.blobs.resize(4); + layerParams.blobs[2] = getBlob(node_proto, constBlobs, 1); // weightData + layerParams.blobs[3] = getBlob(node_proto, constBlobs, 2); // biasData + layerParams.set("has_bias", true); + layerParams.set("has_weight", true); + + // Get number of channels in input + int size = layerParams.blobs[2].total(); + layerParams.blobs[0] = Mat::zeros(size, 1, CV_32F); // mean + layerParams.blobs[1] = Mat::ones(size, 1, CV_32F); // std + + LayerParams mvnParams; + mvnParams.name = layerParams.name + "/MVN"; + mvnParams.type = "MVN"; + mvnParams.set("eps", layerParams.get("epsilon")); + layerParams.erase("epsilon"); + + //Create MVN layer + int id = dstNet.addLayer(mvnParams.name, mvnParams.type, mvnParams); + //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(mvnParams.name, LayerInfo(id, 0))); + outShapes[mvnParams.name] = outShapes[node_proto.input(0)]; + + //Replace Batch Norm's input to MVN + node_proto.set_input(0, mvnParams.name); + layerParams.type = "BatchNorm"; + } else if (layer_type == "BatchNormalization") { if (node_proto.input_size() != 5) diff --git a/modules/dnn/test/test_onnx_importer.cpp b/modules/dnn/test/test_onnx_importer.cpp index 186239494f..fc38a2378c 100644 --- a/modules/dnn/test/test_onnx_importer.cpp +++ b/modules/dnn/test/test_onnx_importer.cpp @@ -76,6 +76,14 @@ public: } }; +TEST_P(Test_ONNX_layers, InstanceNorm) +{ + if (target == DNN_TARGET_MYRIAD) + testONNXModels("instancenorm", npy, 0, 0, false, false); + else + testONNXModels("instancenorm", npy); +} + TEST_P(Test_ONNX_layers, MaxPooling) { testONNXModels("maxpooling"); From 3998b41d6843217e639a3ba53389222ec0620351 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 4 Jul 2019 21:41:14 +0000 Subject: [PATCH 2/6] android: JavaCamera2View use calculateCameraFrameSize() method from CameraBridgeViewBase (common base with JavaCameraView) --- .../org/opencv/android/JavaCamera2View.java | 39 +++++++++++-------- .../opencv/android/CameraBridgeViewBase.java | 10 ++++- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/modules/java/generator/android-21/java/org/opencv/android/JavaCamera2View.java b/modules/java/generator/android-21/java/org/opencv/android/JavaCamera2View.java index c22eaa90d5..e4a58539b5 100644 --- a/modules/java/generator/android-21/java/org/opencv/android/JavaCamera2View.java +++ b/modules/java/generator/android-21/java/org/opencv/android/JavaCamera2View.java @@ -2,6 +2,7 @@ package org.opencv.android; import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.List; import android.annotation.TargetApi; import android.content.Context; @@ -24,6 +25,7 @@ import android.view.ViewGroup.LayoutParams; import org.opencv.core.CvType; import org.opencv.core.Mat; +import org.opencv.core.Size; import org.opencv.imgproc.Imgproc; /** @@ -248,6 +250,20 @@ public class JavaCamera2View extends CameraBridgeViewBase { } } + public static class JavaCameraSizeAccessor implements ListItemAccessor { + @Override + public int getWidth(Object obj) { + android.util.Size size = (android.util.Size)obj; + return size.getWidth(); + } + + @Override + public int getHeight(Object obj) { + android.util.Size size = (android.util.Size)obj; + return size.getHeight(); + } + } + boolean calcPreviewSize(final int width, final int height) { Log.i(LOGTAG, "calcPreviewSize: " + width + "x" + height); if (mCameraID == null) { @@ -258,26 +274,15 @@ public class JavaCamera2View extends CameraBridgeViewBase { try { CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraID); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); - int bestWidth = 0, bestHeight = 0; - float aspect = (float) width / height; android.util.Size[] sizes = map.getOutputSizes(ImageReader.class); - bestWidth = sizes[0].getWidth(); - bestHeight = sizes[0].getHeight(); - for (android.util.Size sz : sizes) { - int w = sz.getWidth(), h = sz.getHeight(); - Log.d(LOGTAG, "trying size: " + w + "x" + h); - if (width >= w && height >= h && bestWidth <= w && bestHeight <= h - && Math.abs(aspect - (float) w / h) < 0.2) { - bestWidth = w; - bestHeight = h; - } - } - Log.i(LOGTAG, "best size: " + bestWidth + "x" + bestHeight); - assert(!(bestWidth == 0 || bestHeight == 0)); - if (mPreviewSize.getWidth() == bestWidth && mPreviewSize.getHeight() == bestHeight) + List sizes_list = Arrays.asList(sizes); + Size frameSize = calculateCameraFrameSize(sizes_list, new JavaCameraSizeAccessor(), width, height); + Log.i(LOGTAG, "Selected preview size to " + Integer.valueOf((int)frameSize.width) + "x" + Integer.valueOf((int)frameSize.height)); + assert(!(frameSize.width == 0 || frameSize.height == 0)); + if (mPreviewSize.getWidth() == frameSize.width && mPreviewSize.getHeight() == frameSize.height) return false; else { - mPreviewSize = new android.util.Size(bestWidth, bestHeight); + mPreviewSize = new android.util.Size((int)frameSize.width, (int)frameSize.height); return true; } } catch (CameraAccessException e) { diff --git a/modules/java/generator/android/java/org/opencv/android/CameraBridgeViewBase.java b/modules/java/generator/android/java/org/opencv/android/CameraBridgeViewBase.java index 54871057e2..681b7ab974 100644 --- a/modules/java/generator/android/java/org/opencv/android/CameraBridgeViewBase.java +++ b/modules/java/generator/android/java/org/opencv/android/CameraBridgeViewBase.java @@ -30,7 +30,7 @@ import android.view.SurfaceView; public abstract class CameraBridgeViewBase extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = "CameraBridge"; - private static final int MAX_UNSPECIFIED = -1; + protected static final int MAX_UNSPECIFIED = -1; private static final int STOPPED = 0; private static final int STARTED = 1; @@ -481,6 +481,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac for (Object size : supportedSizes) { int width = accessor.getWidth(size); int height = accessor.getHeight(size); + Log.d(TAG, "trying size: " + width + "x" + height); if (width <= maxAllowedWidth && height <= maxAllowedHeight) { if (width >= calcWidth && height >= calcHeight) { @@ -489,6 +490,13 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac } } } + if ((calcWidth == 0 || calcHeight == 0) && supportedSizes.size() > 0) + { + Log.i(TAG, "fallback to the first frame size"); + Object size = supportedSizes.get(0); + calcWidth = accessor.getWidth(size); + calcHeight = accessor.getHeight(size); + } return new Size(calcWidth, calcHeight); } From 594a95839cfd3be4d675741fec6a40025e03511d Mon Sep 17 00:00:00 2001 From: Tomoaki Teshima Date: Fri, 5 Jul 2019 11:22:22 +0900 Subject: [PATCH 3/6] fix test failure of OCL_ImgProc/CvtColor8u.mRGBA2RGBA --- modules/imgproc/test/ocl/test_color.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/imgproc/test/ocl/test_color.cpp b/modules/imgproc/test/ocl/test_color.cpp index 0d08de0753..bdbc6a90d0 100644 --- a/modules/imgproc/test/ocl/test_color.cpp +++ b/modules/imgproc/test/ocl/test_color.cpp @@ -294,14 +294,14 @@ OCL_TEST_P(CvtColor8u, GRAY2BGR555) { performTest(1, 2, CVTCODE(GRAY2BGR555)); } // RGBA <-> mRGBA -#ifdef HAVE_IPP +#if defined(HAVE_IPP) || defined(__arm__) #define IPP_EPS depth <= CV_32S ? 1 : 1e-3 #else #define IPP_EPS 1e-3 #endif OCL_TEST_P(CvtColor8u, RGBA2mRGBA) { performTest(4, 4, CVTCODE(RGBA2mRGBA), IPP_EPS); } -OCL_TEST_P(CvtColor8u, mRGBA2RGBA) { performTest(4, 4, CVTCODE(mRGBA2RGBA)); } +OCL_TEST_P(CvtColor8u, mRGBA2RGBA) { performTest(4, 4, CVTCODE(mRGBA2RGBA), IPP_EPS); } // RGB <-> Lab From eedbd1ad598dc9b0a1702ea65c26406cc8f6adac Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 6 Jul 2019 10:06:03 +0000 Subject: [PATCH 4/6] imgcodecs: force reshaping of imdecode() input into a single row OpenCV upstream stuff may reinterpret vector as column. --- modules/imgcodecs/src/loadsave.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/imgcodecs/src/loadsave.cpp b/modules/imgcodecs/src/loadsave.cpp index 7a610af614..6dd412fe6f 100644 --- a/modules/imgcodecs/src/loadsave.cpp +++ b/modules/imgcodecs/src/loadsave.cpp @@ -748,24 +748,28 @@ bool imwrite( const String& filename, InputArray _img, static void* imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 ) { - CV_Assert(!buf.empty() && buf.isContinuous()); + CV_Assert(!buf.empty()); + CV_Assert(buf.isContinuous()); + CV_Assert(buf.checkVector(1, CV_8U) > 0); + Mat buf_row = buf.reshape(1, 1); // decoders expects single row, avoid issues with vector columns + IplImage* image = 0; CvMat *matrix = 0; Mat temp, *data = &temp; String filename; - ImageDecoder decoder = findDecoder(buf); + ImageDecoder decoder = findDecoder(buf_row); if( !decoder ) return 0; - if( !decoder->setSource(buf) ) + if( !decoder->setSource(buf_row) ) { filename = tempfile(); FILE* f = fopen( filename.c_str(), "wb" ); if( !f ) return 0; - size_t bufSize = buf.cols*buf.rows*buf.elemSize(); - if( fwrite( buf.ptr(), 1, bufSize, f ) != bufSize ) + size_t bufSize = buf_row.total()*buf.elemSize(); + if (fwrite(buf_row.ptr(), 1, bufSize, f) != bufSize) { fclose( f ); CV_Error( CV_StsError, "failed to write image data to temporary file" ); From b9399a5df851ef15f903b51068be6958820c0da2 Mon Sep 17 00:00:00 2001 From: jxu <7989982+jxu@users.noreply.github.com> Date: Fri, 14 Jun 2019 17:57:28 -0400 Subject: [PATCH 5/6] Fix python setup in ubuntu dependencies --- .../py_setup/py_setup_in_ubuntu/py_setup_in_ubuntu.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/py_tutorials/py_setup/py_setup_in_ubuntu/py_setup_in_ubuntu.markdown b/doc/py_tutorials/py_setup/py_setup_in_ubuntu/py_setup_in_ubuntu.markdown index 56a2e6bb47..722517273b 100644 --- a/doc/py_tutorials/py_setup/py_setup_in_ubuntu/py_setup_in_ubuntu.markdown +++ b/doc/py_tutorials/py_setup/py_setup_in_ubuntu/py_setup_in_ubuntu.markdown @@ -62,8 +62,8 @@ We need **CMake** to configure the installation, **GCC** for compilation, **Pyth ``` sudo apt-get install cmake -sudo apt-get install python-devel numpy -sudo apt-get install gcc gcc-c++ +sudo apt-get install python-dev python-numpy +sudo apt-get install gcc g++ ``` Next we need **GTK** support for GUI features, Camera support (libv4l), Media Support From 4997a6bf06ce008ed750da8f199f27aedccf0139 Mon Sep 17 00:00:00 2001 From: Tomasz Gorochowik Date: Tue, 9 Jul 2019 14:00:17 +0200 Subject: [PATCH 6/6] V4L2: Add V4L2_PIX_FMT_Y10 (10 bit grey) support --- modules/videoio/src/cap_v4l.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/videoio/src/cap_v4l.cpp b/modules/videoio/src/cap_v4l.cpp index f89ec92896..2e279464f8 100644 --- a/modules/videoio/src/cap_v4l.cpp +++ b/modules/videoio/src/cap_v4l.cpp @@ -496,7 +496,8 @@ bool CvCaptureCAM_V4L::autosetup_capture_mode_v4l2() V4L2_PIX_FMT_JPEG, #endif V4L2_PIX_FMT_Y16, - V4L2_PIX_FMT_GREY + V4L2_PIX_FMT_Y10, + V4L2_PIX_FMT_GREY, }; for (size_t i = 0; i < sizeof(try_order) / sizeof(__u32); i++) { @@ -543,6 +544,7 @@ bool CvCaptureCAM_V4L::convertableToRgb() const case V4L2_PIX_FMT_SGBRG8: case V4L2_PIX_FMT_RGB24: case V4L2_PIX_FMT_Y16: + case V4L2_PIX_FMT_Y10: case V4L2_PIX_FMT_GREY: case V4L2_PIX_FMT_BGR24: return true; @@ -577,6 +579,7 @@ void CvCaptureCAM_V4L::v4l2_create_frame() size.height = size.height * 3 / 2; // "1.5" channels break; case V4L2_PIX_FMT_Y16: + case V4L2_PIX_FMT_Y10: depth = IPL_DEPTH_16U; /* fallthru */ case V4L2_PIX_FMT_GREY: @@ -1451,6 +1454,13 @@ void CvCaptureCAM_V4L::convertToRgb(const Buffer ¤tBuffer) cv::cvtColor(temp, destination, COLOR_GRAY2BGR); return; } + case V4L2_PIX_FMT_Y10: + { + cv::Mat temp(imageSize, CV_8UC1, buffers[MAX_V4L_BUFFERS].start); + cv::Mat(imageSize, CV_16UC1, currentBuffer.start).convertTo(temp, CV_8U, 1.0 / 4); + cv::cvtColor(temp, destination, COLOR_GRAY2BGR); + return; + } case V4L2_PIX_FMT_GREY: cv::cvtColor(cv::Mat(imageSize, CV_8UC1, currentBuffer.start), destination, COLOR_GRAY2BGR); break;