import torch
import numpy as np
from maskrcnn_benchmark.structures.segmentation_mask import SegmentationMask
#from maskrcnn_benchmark.structures.segmentation_mask_old import SegmentationMask as OldSegmentationMask
import matplotlib.pyplot as plt
def plotDiff(M, P, title=''):
# Mask conversion for Poly is necessary for comparison and plotting
# Sum across instances dimension
M = M.instances.masks.float()
P = P.convert('mask').instances.masks.float()
diff = M - P
plt.figure(figsize=(20, 10))
plt.subplot(1, 3, 1)
plt.title('%s(Mask) > Mask'%title)
plt.imshow(M.sum(dim=0))
plt.subplot(1, 3, 2)
plt.title('%s(Poly)'%title)
plt.imshow(P.sum(dim=0))
plt.subplot(1, 3, 3)
plt.title('%s(Mask) - (%s(Poly) > Mask) = %f'%(title, title, diff.sum()))
plt.imshow(diff.sum(dim=0))
plt.colorbar(fraction=0.046, pad=0.04)
plt.show()
A simple rectangle will do, 100 pixels away from each border
width = 640
height = 480
poly = [[[100, 10, 400,10, 400, 400, 100, 400]]]
size = (width, height)
box = [50, 5, 150, 150]
P = SegmentationMask(poly, size, 'poly')
M = SegmentationMask(poly, size, 'poly').convert('mask')
plt.figure(figsize=(20, 20))
plt.title('Mask')
plt.imshow(M.instances.masks[0])
<matplotlib.image.AxesImage at 0x7f865c96b080>
M_hat = M.convert('poly').convert('mask')
P_hat = P.convert('mask').convert('poly')
plt.figure(figsize=(20,10))
plt.subplot(1, 2, 1)
diff = M.instances.masks - M_hat.instances.masks
plt.title('(Mask) - (Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
plt.imshow(diff[0])
plt.subplot(1, 2, 2)
# Mask conversion for Poly is necessary for comparison and plotting
diff = P.convert('mask').instances.masks - P_hat.convert('mask').instances.masks
plt.title('(Poly > Mask) - (Poly > Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
im = plt.imshow(diff[0])
plt.colorbar(im, fraction=0.02, pad=0.04)
<matplotlib.colorbar.Colorbar at 0x7f865c6d4cc0>
plotDiff(M.crop(box), P.crop(box), 'crop')
new_size = 1000, 1500
plotDiff(M.resize(new_size), P.resize(new_size), 'resize')
contour, hierarchy = cv2.findContours(
mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1
)
FLIP_LEFT_RIGHT = 0
FLIP_TOP_BOTTOM = 1
plotDiff(M.transpose(FLIP_LEFT_RIGHT), P.transpose(FLIP_LEFT_RIGHT))
plotDiff(M.transpose(FLIP_TOP_BOTTOM), P.transpose(FLIP_TOP_BOTTOM))
contour, hierarchy = cv2.findContours(
mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1
)
Two rectangles, one on the top-left and one on the bottom-right
width = 640
height = 480
poly = [[[100, 100, 200, 100, 200, 200, 100, 200],
[width-110, height-110, width-10, height-110,
width-10, height-10, width-110, height-10]]]
size = (width, height)
box = [110, 110, 210, 210]
P = SegmentationMask(poly, size, 'poly')
M = SegmentationMask(poly, size, 'poly').convert('mask')
plt.figure(figsize=(20, 20))
plt.title('Mask')
plt.imshow(M.instances.masks[0])
<matplotlib.image.AxesImage at 0x7f865c60da90>
M_hat = M.convert('poly').convert('mask')
P_hat = P.convert('mask').convert('poly')
plt.figure(figsize=(20,20))
plt.subplot(1, 2, 1)
diff = M.instances.masks - M_hat.instances.masks
plt.title('(Mask) - (Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
plt.imshow(diff[0])
plt.subplot(1, 2, 2)
# Mask conversion for Poly is necessary for comparison and plotting
diff = P.convert('mask').instances.masks - P_hat.convert('mask').instances.masks
plt.title('(Poly > Mask) - (Poly > Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
plt.imshow(diff[0])
<matplotlib.image.AxesImage at 0x7f865c330240>
plotDiff(M.crop(box), P.crop(box), 'crop')
new_size = 1000, 100
plotDiff(M.resize(new_size), P.resize(new_size), 'resize')
FLIP_LEFT_RIGHT = 0
FLIP_TOP_BOTTOM = 1
plotDiff(M.transpose(FLIP_LEFT_RIGHT), P.transpose(FLIP_LEFT_RIGHT))
plotDiff(M.transpose(FLIP_TOP_BOTTOM), P.transpose(FLIP_TOP_BOTTOM))
Modified shape from unit test of PR #150
''' ORIGINAL SHAPE
poly = [[423.0, 306.5, 406.5, 277.0, 400.0, 271.5, 389.5, 277.0, 387.5, 292.0,
384.5, 295.0, 374.5, 220.0, 378.5, 210.0, 391.0, 200.5, 404.0, 199.5,
414.0, 203.5, 425.5, 221.0, 438.5, 297.0, 423.0, 306.5],
[385.5, 240.0, 404.0, 234.5, 419.5, 234.0, 416.5, 219.0, 409.0, 209.5,
394.0, 207.5, 385.5, 213.0, 382.5, 221.0, 385.5, 240.0]]
'''
poly = [[[423.0, 306.5, 406.5, 277.0, 400.0, 271.5, 389.5, 277.0, 387.5, 292.0,
384.5, 295.0, 374.5, 220.0, 378.5, 210.0, 391.0, 200.5, 404.0, 199.5,
414.0, 203.5, 425.5, 221.0, 438.5, 297.0, 423.0, 306.5]]]
width = 640
height = 480
size = (width, height)
box = [350, 200, 400, 300]
P = SegmentationMask(poly, size, 'poly')
M = SegmentationMask(poly, size, 'poly').convert('mask')
plt.figure(figsize=(20, 20))
plt.title('Mask')
plt.imshow(M.instances.masks[0])
<matplotlib.image.AxesImage at 0x7f865c422668>
M_hat = M.convert('poly').convert('mask')
P_hat = P.convert('mask').convert('poly')
plt.figure(figsize=(20,20))
plt.subplot(1, 2, 1)
diff = M.instances.masks - M_hat.instances.masks
plt.title('(Mask) - (Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
plt.imshow(diff[0])
plt.subplot(1, 2, 2)
# Mask conversion for Poly is necessary for comparison and plotting
diff = P.convert('mask').instances.masks - P_hat.convert('mask').instances.masks
plt.title('(Poly > Mask) - (Poly > Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
plt.imshow(diff[0])
<matplotlib.image.AxesImage at 0x7f865c3837b8>
plotDiff(M.crop(box), P.crop(box), 'crop')
new_size = 1000, 100
plotDiff(M.resize(new_size), P.resize(new_size), 'resize')
FLIP_LEFT_RIGHT = 0
FLIP_TOP_BOTTOM = 1
plotDiff(M.transpose(FLIP_LEFT_RIGHT), P.transpose(FLIP_LEFT_RIGHT))
plotDiff(M.transpose(FLIP_TOP_BOTTOM), P.transpose(FLIP_TOP_BOTTOM))
Modified shape from unit test of PR #150
poly = [[[423.0, 306.5, 406.5, 277.0, 400.0, 271.5, 389.5, 277.0, 387.5, 292.0,
384.5, 295.0, 374.5, 220.0, 378.5, 210.0, 391.0, 200.5, 404.0, 199.5,
414.0, 203.5, 425.5, 221.0, 438.5, 297.0, 423.0, 306.5],
[100, 100, 200, 100, 200, 200, 100, 200],
[width-200, height-200, width-100, height-200, width-100, height-100, width-200, height-100]
]]
width = 640
height = 480
size = (width, height)
box = [400, 250, 500, 300]
P = SegmentationMask(poly, size, 'poly')
M = SegmentationMask(poly, size, 'poly').convert('mask')
plt.figure(figsize=(20, 20))
plt.title('Mask')
plt.imshow(M.instances.masks[0])
<matplotlib.image.AxesImage at 0x7f865c48a710>
M_hat = M.convert('poly').convert('mask')
P_hat = P.convert('mask').convert('poly')
plt.figure(figsize=(20,20))
plt.subplot(1, 2, 1)
diff = M.instances.masks - M_hat.instances.masks
plt.title('(Mask) - (Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
plt.imshow(diff[0])
plt.subplot(1, 2, 2)
# Mask conversion for Poly is necessary for comparison and plotting
diff = P.convert('mask').instances.masks - P_hat.convert('mask').instances.masks
plt.title('(Poly > Mask) - (Poly > Mask > Poly > Mask) = %4.2f'%(diff.sum().item()))
plt.imshow(diff[0])
<matplotlib.image.AxesImage at 0x7f865c343c88>
plotDiff(M.crop(box), P.crop(box), 'crop')
new_size = 1000, 100
plotDiff(M.resize(new_size), P.resize(new_size), 'resize')
FLIP_LEFT_RIGHT = 0
FLIP_TOP_BOTTOM = 1
plotDiff(M.transpose(FLIP_LEFT_RIGHT), P.transpose(FLIP_LEFT_RIGHT))
plotDiff(M.transpose(FLIP_TOP_BOTTOM), P.transpose(FLIP_TOP_BOTTOM))