0
votes

I made following python layer and added it to the LeNet architecture. But when building model it gives an error. I am to apply my Python layer using Numpy but when I am using OpenCV it gives an error. Following I am adding my code and corresponding error from a log file.

import cv2
import caffe
import random

def doEqualizeHist(img): img = img.astype(np.uint8) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) return cv2.equalizeHist(img)

class EqualizeLayer(caffe.Layer): def setup(self, bottom, top): assert len(bottom) == 1, 'requires a single layer.bottom' assert bottom[0].data.ndim >= 3, 'requires image data' assert len(top) == 1, 'requires a single layer.top'

def reshape(self, bottom, top): # Copy shape from bottom top[0].reshape(*bottom[0].data.shape) def forward(self, bottom, top): # Copy all of the data top[0].data[...] = bottom[0].data[...] for ii in xrange(0, top[0].data.shape[0]): imin = top[0].data[ii, :, :, :].transpose(1, 2, 0) top[0].data[ii, :, :, :] = doEqualizeHist(imin).transpose(2, 0, 1) def backward(self, top, propagate_down, bottom): pass

Error Message: 0812 06:41:53.452097 14355 net.cpp:723] Ignoring source layer train-data OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp, line 3737 Traceback (most recent call last): File "/var/lib/digits/jobs/20170812-064148-f44d/digits_python_layers.py", line 27, in forward top[0].data[ii, :, :, :] = doEqualizeHist(imin).transpose(2, 0, 1) File "/var/lib/digits/jobs/20170812-064148-f44d/digits_python_layers.py", line 8, in doEqualizeHist img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) cv2.error: /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

1
Why don't you do some debugging, and perhaps inspect img before it's passed into the statement which causes the error?Dan Mašek
I tried independent Python code then it was running fine but it causing an error during a model run in Caffe. for now, I tried to use skimage instead of OpenCV then its running fine. I know it's not a solution but a workaround.Sanket Badhe

1 Answers

1
votes

For future reference, an "Assertion failed" error message in OpenCV means you passed invalid data to a function. In this case, the assertion that failed is scn == 3 || scn == 4. To know exactly what that means, you can look at the source file where the assertion failed: modules/impgproc/src/color.cpp and examine the function where it happened: cvtColor at line 3737. Look to see what the variable scn represents.

In your case, the problem is that you're converting img to a single-channel format and then attempting to convert it from RGB to grayscale. That conversion is first asserting the input is a 3- or 4-channel format. It isn't so the assertion fails.