Retrieving information from a Mask_RCNN Tensor











up vote
0
down vote

favorite












I've succesfully trained a Mask_RCNN, and for illustration purposes, let's focus on this sample image the network generates:



enter image description here



It's all very good, no problem. What I'd like to achieve however is to have the following variables with their values per instance:



   mask:  (as an image which shows the detected object only, like a binary map)
box: (as a list)
mask_border_positions (x,y) : (as a list)
mask_center_position (x,y) : (as a tuple)


I've also the function which visualizes the above image, from the official site:



def display_instances(image, boxes, masks, class_ids, class_names,
scores=None, title="",
figsize=(16, 16), ax=None,
show_mask=True, show_bbox=True,
colors=None, captions=None):
"""
boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates.
masks: [height, width, num_instances]
class_ids: [num_instances]
class_names: list of class names of the dataset
scores: (optional) confidence scores for each box
title: (optional) Figure title
show_mask, show_bbox: To show masks and bounding boxes or not
figsize: (optional) the size of the image
colors: (optional) An array or colors to use with each object
captions: (optional) A list of strings to use as captions for each object
"""
# Number of instances
N = boxes.shape[0]
if not N:
print("n*** No instances to display *** n")
else:
assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]

# If no axis is passed, create one and automatically call show()
auto_show = False
if not ax:
_, ax = plt.subplots(1, figsize=figsize)
auto_show = True

# Generate random colors
colors = colors or random_colors(N)

# Show area outside image boundaries.
height, width = image.shape[:2]
ax.set_ylim(height + 10, -10)
ax.set_xlim(-10, width + 10)
ax.axis('off')
ax.set_title(title)

masked_image = image.astype(np.uint32).copy()
for i in range(N):
color = colors[i]

# Bounding box
if not np.any(boxes[i]):
# Skip this instance. Has no bbox. Likely lost in image cropping.
continue
y1, x1, y2, x2 = boxes[i]
if show_bbox:
p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2,
alpha=0.7, linestyle="dashed",
edgecolor=color, facecolor='none')
ax.add_patch(p)

# Label
if not captions:
class_id = class_ids[i]
score = scores[i] if scores is not None else None
label = class_names[class_id]
x = random.randint(x1, (x1 + x2) // 2)
caption = "{} {:.3f}".format(label, score) if score else label
else:
caption = captions[i]
ax.text(x1, y1 + 8, caption,
color='w', size=11, backgroundcolor="none")

# Mask
mask = masks[:, :, i]
if show_mask:
masked_image = apply_mask(masked_image, mask, color)

# Mask Polygon
# Pad to ensure proper polygons for masks that touch image edges.
padded_mask = np.zeros(
(mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
padded_mask[1:-1, 1:-1] = mask
contours = find_contours(padded_mask, 0.5)
for verts in contours:
# Subtract the padding and flip (y, x) to (x, y)
verts = np.fliplr(verts) - 1
p = Polygon(verts, facecolor="none", edgecolor=color)
ax.add_patch(p)
ax.imshow(masked_image.astype(np.uint8))
if auto_show:
plt.show()


These code snippets below are then called in the main as follows:



file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
masks_prediction = np.zeros((510, 510, len(file_names)))
for i in range(len(file_names)):
print(i)
image = skimage.io.imread(file_names[i])
predictions = model.detect([image], verbose=1)
p = predictions[0]
masks = p['masks']
merged_mask = np.zeros((masks.shape[0], masks.shape[1]))
for j in range(masks.shape[2]):
merged_mask[masks[:,:,j]==True] = True
masks_prediction[:,:,i] = merged_mask
print(masks_prediction.shape)


and:



file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
class_names = ['BG', 'car', 'traffic_light', 'person']
test_image = skimage.io.imread(file_names[random.randint(0,len(file_names)-1)])
predictions = model.detect([test_image], verbose=1) # We are replicating the same image to fill up the batch_size
p = predictions[0]
visualize.display_instances(test_image, p['rois'], p['masks'], p['class_ids'],
class_names, p['scores'])


I know it's probably a trivial question and they already exist in the code somewhere, but since I am a starter, I could not get the mask outliers or their centers. If there is a way to have these information per instance, it would be great.



Thanks in advance.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I've succesfully trained a Mask_RCNN, and for illustration purposes, let's focus on this sample image the network generates:



    enter image description here



    It's all very good, no problem. What I'd like to achieve however is to have the following variables with their values per instance:



       mask:  (as an image which shows the detected object only, like a binary map)
    box: (as a list)
    mask_border_positions (x,y) : (as a list)
    mask_center_position (x,y) : (as a tuple)


    I've also the function which visualizes the above image, from the official site:



    def display_instances(image, boxes, masks, class_ids, class_names,
    scores=None, title="",
    figsize=(16, 16), ax=None,
    show_mask=True, show_bbox=True,
    colors=None, captions=None):
    """
    boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates.
    masks: [height, width, num_instances]
    class_ids: [num_instances]
    class_names: list of class names of the dataset
    scores: (optional) confidence scores for each box
    title: (optional) Figure title
    show_mask, show_bbox: To show masks and bounding boxes or not
    figsize: (optional) the size of the image
    colors: (optional) An array or colors to use with each object
    captions: (optional) A list of strings to use as captions for each object
    """
    # Number of instances
    N = boxes.shape[0]
    if not N:
    print("n*** No instances to display *** n")
    else:
    assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]

    # If no axis is passed, create one and automatically call show()
    auto_show = False
    if not ax:
    _, ax = plt.subplots(1, figsize=figsize)
    auto_show = True

    # Generate random colors
    colors = colors or random_colors(N)

    # Show area outside image boundaries.
    height, width = image.shape[:2]
    ax.set_ylim(height + 10, -10)
    ax.set_xlim(-10, width + 10)
    ax.axis('off')
    ax.set_title(title)

    masked_image = image.astype(np.uint32).copy()
    for i in range(N):
    color = colors[i]

    # Bounding box
    if not np.any(boxes[i]):
    # Skip this instance. Has no bbox. Likely lost in image cropping.
    continue
    y1, x1, y2, x2 = boxes[i]
    if show_bbox:
    p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2,
    alpha=0.7, linestyle="dashed",
    edgecolor=color, facecolor='none')
    ax.add_patch(p)

    # Label
    if not captions:
    class_id = class_ids[i]
    score = scores[i] if scores is not None else None
    label = class_names[class_id]
    x = random.randint(x1, (x1 + x2) // 2)
    caption = "{} {:.3f}".format(label, score) if score else label
    else:
    caption = captions[i]
    ax.text(x1, y1 + 8, caption,
    color='w', size=11, backgroundcolor="none")

    # Mask
    mask = masks[:, :, i]
    if show_mask:
    masked_image = apply_mask(masked_image, mask, color)

    # Mask Polygon
    # Pad to ensure proper polygons for masks that touch image edges.
    padded_mask = np.zeros(
    (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
    padded_mask[1:-1, 1:-1] = mask
    contours = find_contours(padded_mask, 0.5)
    for verts in contours:
    # Subtract the padding and flip (y, x) to (x, y)
    verts = np.fliplr(verts) - 1
    p = Polygon(verts, facecolor="none", edgecolor=color)
    ax.add_patch(p)
    ax.imshow(masked_image.astype(np.uint8))
    if auto_show:
    plt.show()


    These code snippets below are then called in the main as follows:



    file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
    masks_prediction = np.zeros((510, 510, len(file_names)))
    for i in range(len(file_names)):
    print(i)
    image = skimage.io.imread(file_names[i])
    predictions = model.detect([image], verbose=1)
    p = predictions[0]
    masks = p['masks']
    merged_mask = np.zeros((masks.shape[0], masks.shape[1]))
    for j in range(masks.shape[2]):
    merged_mask[masks[:,:,j]==True] = True
    masks_prediction[:,:,i] = merged_mask
    print(masks_prediction.shape)


    and:



    file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
    class_names = ['BG', 'car', 'traffic_light', 'person']
    test_image = skimage.io.imread(file_names[random.randint(0,len(file_names)-1)])
    predictions = model.detect([test_image], verbose=1) # We are replicating the same image to fill up the batch_size
    p = predictions[0]
    visualize.display_instances(test_image, p['rois'], p['masks'], p['class_ids'],
    class_names, p['scores'])


    I know it's probably a trivial question and they already exist in the code somewhere, but since I am a starter, I could not get the mask outliers or their centers. If there is a way to have these information per instance, it would be great.



    Thanks in advance.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I've succesfully trained a Mask_RCNN, and for illustration purposes, let's focus on this sample image the network generates:



      enter image description here



      It's all very good, no problem. What I'd like to achieve however is to have the following variables with their values per instance:



         mask:  (as an image which shows the detected object only, like a binary map)
      box: (as a list)
      mask_border_positions (x,y) : (as a list)
      mask_center_position (x,y) : (as a tuple)


      I've also the function which visualizes the above image, from the official site:



      def display_instances(image, boxes, masks, class_ids, class_names,
      scores=None, title="",
      figsize=(16, 16), ax=None,
      show_mask=True, show_bbox=True,
      colors=None, captions=None):
      """
      boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates.
      masks: [height, width, num_instances]
      class_ids: [num_instances]
      class_names: list of class names of the dataset
      scores: (optional) confidence scores for each box
      title: (optional) Figure title
      show_mask, show_bbox: To show masks and bounding boxes or not
      figsize: (optional) the size of the image
      colors: (optional) An array or colors to use with each object
      captions: (optional) A list of strings to use as captions for each object
      """
      # Number of instances
      N = boxes.shape[0]
      if not N:
      print("n*** No instances to display *** n")
      else:
      assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]

      # If no axis is passed, create one and automatically call show()
      auto_show = False
      if not ax:
      _, ax = plt.subplots(1, figsize=figsize)
      auto_show = True

      # Generate random colors
      colors = colors or random_colors(N)

      # Show area outside image boundaries.
      height, width = image.shape[:2]
      ax.set_ylim(height + 10, -10)
      ax.set_xlim(-10, width + 10)
      ax.axis('off')
      ax.set_title(title)

      masked_image = image.astype(np.uint32).copy()
      for i in range(N):
      color = colors[i]

      # Bounding box
      if not np.any(boxes[i]):
      # Skip this instance. Has no bbox. Likely lost in image cropping.
      continue
      y1, x1, y2, x2 = boxes[i]
      if show_bbox:
      p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2,
      alpha=0.7, linestyle="dashed",
      edgecolor=color, facecolor='none')
      ax.add_patch(p)

      # Label
      if not captions:
      class_id = class_ids[i]
      score = scores[i] if scores is not None else None
      label = class_names[class_id]
      x = random.randint(x1, (x1 + x2) // 2)
      caption = "{} {:.3f}".format(label, score) if score else label
      else:
      caption = captions[i]
      ax.text(x1, y1 + 8, caption,
      color='w', size=11, backgroundcolor="none")

      # Mask
      mask = masks[:, :, i]
      if show_mask:
      masked_image = apply_mask(masked_image, mask, color)

      # Mask Polygon
      # Pad to ensure proper polygons for masks that touch image edges.
      padded_mask = np.zeros(
      (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
      padded_mask[1:-1, 1:-1] = mask
      contours = find_contours(padded_mask, 0.5)
      for verts in contours:
      # Subtract the padding and flip (y, x) to (x, y)
      verts = np.fliplr(verts) - 1
      p = Polygon(verts, facecolor="none", edgecolor=color)
      ax.add_patch(p)
      ax.imshow(masked_image.astype(np.uint8))
      if auto_show:
      plt.show()


      These code snippets below are then called in the main as follows:



      file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
      masks_prediction = np.zeros((510, 510, len(file_names)))
      for i in range(len(file_names)):
      print(i)
      image = skimage.io.imread(file_names[i])
      predictions = model.detect([image], verbose=1)
      p = predictions[0]
      masks = p['masks']
      merged_mask = np.zeros((masks.shape[0], masks.shape[1]))
      for j in range(masks.shape[2]):
      merged_mask[masks[:,:,j]==True] = True
      masks_prediction[:,:,i] = merged_mask
      print(masks_prediction.shape)


      and:



      file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
      class_names = ['BG', 'car', 'traffic_light', 'person']
      test_image = skimage.io.imread(file_names[random.randint(0,len(file_names)-1)])
      predictions = model.detect([test_image], verbose=1) # We are replicating the same image to fill up the batch_size
      p = predictions[0]
      visualize.display_instances(test_image, p['rois'], p['masks'], p['class_ids'],
      class_names, p['scores'])


      I know it's probably a trivial question and they already exist in the code somewhere, but since I am a starter, I could not get the mask outliers or their centers. If there is a way to have these information per instance, it would be great.



      Thanks in advance.










      share|improve this question













      I've succesfully trained a Mask_RCNN, and for illustration purposes, let's focus on this sample image the network generates:



      enter image description here



      It's all very good, no problem. What I'd like to achieve however is to have the following variables with their values per instance:



         mask:  (as an image which shows the detected object only, like a binary map)
      box: (as a list)
      mask_border_positions (x,y) : (as a list)
      mask_center_position (x,y) : (as a tuple)


      I've also the function which visualizes the above image, from the official site:



      def display_instances(image, boxes, masks, class_ids, class_names,
      scores=None, title="",
      figsize=(16, 16), ax=None,
      show_mask=True, show_bbox=True,
      colors=None, captions=None):
      """
      boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates.
      masks: [height, width, num_instances]
      class_ids: [num_instances]
      class_names: list of class names of the dataset
      scores: (optional) confidence scores for each box
      title: (optional) Figure title
      show_mask, show_bbox: To show masks and bounding boxes or not
      figsize: (optional) the size of the image
      colors: (optional) An array or colors to use with each object
      captions: (optional) A list of strings to use as captions for each object
      """
      # Number of instances
      N = boxes.shape[0]
      if not N:
      print("n*** No instances to display *** n")
      else:
      assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]

      # If no axis is passed, create one and automatically call show()
      auto_show = False
      if not ax:
      _, ax = plt.subplots(1, figsize=figsize)
      auto_show = True

      # Generate random colors
      colors = colors or random_colors(N)

      # Show area outside image boundaries.
      height, width = image.shape[:2]
      ax.set_ylim(height + 10, -10)
      ax.set_xlim(-10, width + 10)
      ax.axis('off')
      ax.set_title(title)

      masked_image = image.astype(np.uint32).copy()
      for i in range(N):
      color = colors[i]

      # Bounding box
      if not np.any(boxes[i]):
      # Skip this instance. Has no bbox. Likely lost in image cropping.
      continue
      y1, x1, y2, x2 = boxes[i]
      if show_bbox:
      p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2,
      alpha=0.7, linestyle="dashed",
      edgecolor=color, facecolor='none')
      ax.add_patch(p)

      # Label
      if not captions:
      class_id = class_ids[i]
      score = scores[i] if scores is not None else None
      label = class_names[class_id]
      x = random.randint(x1, (x1 + x2) // 2)
      caption = "{} {:.3f}".format(label, score) if score else label
      else:
      caption = captions[i]
      ax.text(x1, y1 + 8, caption,
      color='w', size=11, backgroundcolor="none")

      # Mask
      mask = masks[:, :, i]
      if show_mask:
      masked_image = apply_mask(masked_image, mask, color)

      # Mask Polygon
      # Pad to ensure proper polygons for masks that touch image edges.
      padded_mask = np.zeros(
      (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
      padded_mask[1:-1, 1:-1] = mask
      contours = find_contours(padded_mask, 0.5)
      for verts in contours:
      # Subtract the padding and flip (y, x) to (x, y)
      verts = np.fliplr(verts) - 1
      p = Polygon(verts, facecolor="none", edgecolor=color)
      ax.add_patch(p)
      ax.imshow(masked_image.astype(np.uint8))
      if auto_show:
      plt.show()


      These code snippets below are then called in the main as follows:



      file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
      masks_prediction = np.zeros((510, 510, len(file_names)))
      for i in range(len(file_names)):
      print(i)
      image = skimage.io.imread(file_names[i])
      predictions = model.detect([image], verbose=1)
      p = predictions[0]
      masks = p['masks']
      merged_mask = np.zeros((masks.shape[0], masks.shape[1]))
      for j in range(masks.shape[2]):
      merged_mask[masks[:,:,j]==True] = True
      masks_prediction[:,:,i] = merged_mask
      print(masks_prediction.shape)


      and:



      file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
      class_names = ['BG', 'car', 'traffic_light', 'person']
      test_image = skimage.io.imread(file_names[random.randint(0,len(file_names)-1)])
      predictions = model.detect([test_image], verbose=1) # We are replicating the same image to fill up the batch_size
      p = predictions[0]
      visualize.display_instances(test_image, p['rois'], p['masks'], p['class_ids'],
      class_names, p['scores'])


      I know it's probably a trivial question and they already exist in the code somewhere, but since I am a starter, I could not get the mask outliers or their centers. If there is a way to have these information per instance, it would be great.



      Thanks in advance.







      python tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 15:25









      Schütze

      130424




      130424





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415299%2fretrieving-information-from-a-mask-rcnn-tensor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415299%2fretrieving-information-from-a-mask-rcnn-tensor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks