EAST dnn.forward Assertion failed
I wish to use the EAST text detector using Python (on Windows 10 with 16 GB RAM) and following this tutorial. However when working with some images it fails systematically with the following error:
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:835: error: (-215:Assertion failed) ld.inputBlobs[0]->total() == total(shapes[index]) in function 'cv::dnn::dnn4_v20180917::BlobManager::allocateBlobsForLayer'
I cannot tell what characteristics make an image fail or not. Here is the code to reproduce the error (and an example of a troublesome image can be downloaded from here ...it's a pretty big image, i.e., width = 30253 and height = 4537):
# sFileName is the path to the image, previously set
oInputImage = cv.imread(sFileName)
aiShape = oInputImage.shape
(iH, iW) = aiShape[:2]
iRequiredUnit = 32
# check if the image height is enough
iHr = iH % iRequiredUnit
iBottom = 0
iHr = iH % iRequiredUnit
if 0 < iHr:
# calculate how much padding is necessary
iBottom = iRequiredUnit - iHr
# check if the image width is enough
iRight = 0
iWr = iW % iRequiredUnit
if 0 < iWr:
# calculate how much padding is necessary
iRight = iRequiredUnit - iWr
if iBottom > 0 or iRight > 0:
# add padding to make the image proportions correct
oImage = cv.copyMakeBorder(
src=oInputImage,
top=iTop,
bottom=iBottom,
left=iLeft,
right=iRight,
borderType=cv.BORDER_CONSTANT,
value=[0, 0, 0]
)
else:
# no need to add padding
oImage = oInputImage.copy()
(iH, iW) = oImage.shape[:2])
ib, ig, ir, _ = cv.mean(oImage)
oBlob = cv.dnn.blobFromImage(
oImage, 1.0, (iW, iH), (ib, ig, ir),
swapRB=True, crop=False
)
# load the EAST network
# EAST_path initialized appropriately previously
oNet = cv.dnn.readNet(sEAST_path)
oNet.setInput(oBlob)
asLayerNames = [
"feature_fusion/Conv_7/Sigmoid",
"feature_fusion/concat_3"]
(afScores, aoGeometry) = oNet.forward(asLayerNames)
The last line causes the error.
begin EDIT 0
I tried following the example kindly indicated by berak but with no success, since most of the relevant differences take place after the call of net.forward
.
I also tried providing smaller sizes in blobFromImage
to avoid the need for the copyMakeBorder
part, but if the newly specified size is too small the detection has a lower precision (tried on other images too).
end EDIT 0
I already posted a request on stack overflow (sorry for cross posting), but I received no answer and I was about to open an issue on github, but there I was invited to post here first.
begin EDIT 1
I made some experiments to see how memory occupation could affect the execution. Here are some results, to be read as follows:
- rows:
- first and second row show the height and width passed as parameters to the openCV example
- next rows contain the memory occupation at various stages of execution
- columns:
- the first column states the phase at which memory was measured
- the remaining ones show the memory occupation by the program, expressed in GB. MIssing values are for those cases where EAST crashed with the error reported above
- height 320 640 1024 2048 4096 8192 4096 4544
- width ...