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 randomdef 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
img
before it's passed into the statement which causes the error? – Dan Mašek