Skip to content

boxes, masks = net.forward(['detection_out_final', 'detection_masks']) crashes with matrix.cpp:541: error: (-215:Assertion failed) #14530

Closed

Description

@ConsMe

Opencv 4.1.0
Ubuntu 18.04

Hello,
According to opencv tensorflow object detection api I run segmentation with trained Mask-RCNN Inception v2 model. Mainly it works well, but with about 15% images I get the error:
boxes, masks = net.forward(['detection_out_final', 'detection_masks'])
cv2.error: OpenCV(4.1.0) /io/opencv/modules/core/src/matrix.cpp:541: error: (-215:Assertion failed) r == Range::all() || (0 <= r.start && r.start < r.end && r.end <= m.size[i]) in function 'Mat'
At the same time if I try these images with original tensorflow object detection api it works well, so I conclude it's not because of corrupted images or model mistakes, but because of opencv error.
Can you help me please?
Thank you in advance.

Activity

  1. alalek commented on May 10, 2019

    @alalek
    Member

    Provide complete minimal reproducer with included input images with issues.

  2. ConsMe commented on May 10, 2019

    @ConsMe
    Author

    Hello @alalek ,
    Please check this example code with .pb and .pbtxt - https://yadi.sk/d/I8xDeLPQ4-4DEw
    To get right result please run:
    python mask_rcnn.py --input https://ayamama.ru/wp-content/uploads/2017/06/snils.jpg --model frozen_inference_graph.pb --config graphcv.pbtxt --width 500 --height 500
    To get error run:
    python mask_rcnn.py --input https://pensioved.ru/wp-content/uploads/2018/02/1-7.jpg --model frozen_inference_graph.pb --config graphcv.pbtxt --width 500 --height 500
    Thank you

  3. ConsMe commented on May 13, 2019

    @ConsMe
    Author

    @alalek , will you help me?

  4. self-assigned this
    on May 14, 2019
  5. added and removed
    needs reproducer
    on May 14, 2019
  6. dkurt commented on May 14, 2019

    @dkurt
    Member

    @ConsMe, Please try the changes from #14552

    This is result from TensorFlow:

    tf

    and with mask_rcnn.py sample:

    python mask_rcnn.py --input 1-7.jpg --model frozen_inference_graph.pb --config graphcv.pbtxt --width 500 --height 500 --thr 0.6

    Please note that I had to replace a swapRB flag to False (perhaps the training was different for your model)

    blob = cv.dnn.blobFromImage(frame, size=(args.width, args.height), swapRB=False, crop=False)

    cv

  7. ConsMe commented on May 14, 2019

    @ConsMe
    Author

    Hello @dkurt ,
    Sorry for my backwardness, but how can I apply these changes for v.3.4 to my installed v.4.1.0?

  8. dkurt commented on May 14, 2019

    @dkurt
    Member

    You need to rebuild OpenCV. After merge the changes will be ported to master branch as well.

  9. ConsMe commented on May 15, 2019

    @ConsMe
    Author

    Hello @dkurt
    I've rebuilt opencv and it works! Thank you.
    But I have another question. The mask and bounding box that I get with opencv differs from the mask and box with tensorflow:
    Original image:
    1-7_rotated
    Detected with opencv:
    1-7_rotated_opencv
    Detected with tensorflow:
    1-7_rotated
    Can it be somehow improved?
    Thank you.

  10. dkurt commented on May 16, 2019

    @dkurt
    Member

    @ConsMe, have you considered

    Please note that I had to replace a swapRB flag to False (perhaps the training was different for your model)

    ?

  11. ConsMe commented on May 16, 2019

    @ConsMe
    Author

    have you considered

    @dkurt, yes, sure, I've got that result with swapRB = False

  12. dkurt commented on May 18, 2019

    @dkurt
    Member

    Ok, let's reopen the issue so we won't lose it. Thanks!

  13. reopened this on May 18, 2019
  14. dkurt commented on May 22, 2019

    @dkurt
    Member

    This is my experiment:

    Using the following script I get similar results in TensorFlow:

    import tensorflow as tf
    import cv2 as cv
    import numpy as np
    
    img = cv.imread('test.jpg')
    inp = cv.resize(img, dsize=(500, 500))
    inp = inp.reshape(1, 500, 500, 3)
    
    
    graph = 'frozen_inference_graph.pb'
    with tf.gfile.FastGFile(graph) as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    
    with tf.Session() as sess:
        # Restore session
        sess.graph.as_default()
        tf.import_graph_def(graph_def, name='')
    
        tfOut = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
                          sess.graph.get_tensor_by_name('detection_scores:0'),
                          sess.graph.get_tensor_by_name('detection_boxes:0'),
                          sess.graph.get_tensor_by_name('detection_classes:0'),
                          sess.graph.get_tensor_by_name('detection_masks:0'),
                          sess.graph.get_tensor_by_name('Preprocessor/sub:0')
                          ],
                         feed_dict={'image_tensor:0': inp})
    
    print tfOut[-1].shape
    
    num = int(tfOut[0])
    scores = tfOut[1] # 1xN
    boxes = tfOut[2] # 1xNx4
    classes = tfOut[3] # 1xN
    masks = tfOut[4] # 1xNx15x15
    
    color = np.array([0, 121, 255], dtype=np.uint8)
    
    for i in range(num):
        score = float(scores[0, i])
        if score < 0.8:
            continue
    
        print 1
    
        box = boxes[0, i]
        ymin = int(img.shape[0] * box[0])
        xmin = int(img.shape[1] * box[1])
        ymax = int(img.shape[0] * box[2])
        xmax = int(img.shape[1] * box[3])
    
        classMask = masks[0, i]
        classMask = cv.resize(classMask, (xmax - xmin + 1, ymax - ymin + 1))
        mask = (classMask > 0.5)
    
        roi = img[ymin:ymax+1, xmin:xmax+1][mask]
        img[ymin:ymax+1, xmin:xmax+1][mask] = (0.7 * color + 0.3 * roi).astype(np.uint8)
    
    cv.imwrite('tf.jpg', img)

    tf

    So please share the code you used to get target result.

  15. ConsMe commented on May 25, 2019

    @ConsMe
    Author

    Hello @dkurt
    I used this code form tensorflow object detection repo. I got the same result with your code example. So it's not a problem with opencv. We can close this issue. Thank you.

  16. ajinkya933 commented on Jul 19, 2019

    @ajinkya933

    this code is good

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions