Args: box: 4 float mask: MxM floats shape: h,w Returns: A uint8 binary image of hxw.
(box, mask, shape)
| 59 | |
| 60 | |
| 61 | def _paste_mask(box, mask, shape): |
| 62 | """ |
| 63 | Args: |
| 64 | box: 4 float |
| 65 | mask: MxM floats |
| 66 | shape: h,w |
| 67 | Returns: |
| 68 | A uint8 binary image of hxw. |
| 69 | """ |
| 70 | assert mask.shape[0] == mask.shape[1], mask.shape |
| 71 | |
| 72 | if cfg.MRCNN.ACCURATE_PASTE: |
| 73 | # This method is accurate but much slower. |
| 74 | mask = np.pad(mask, [(1, 1), (1, 1)], mode='constant') |
| 75 | box = _scale_box(box, float(mask.shape[0]) / (mask.shape[0] - 2)) |
| 76 | |
| 77 | mask_pixels = np.arange(0.0, mask.shape[0]) + 0.5 |
| 78 | mask_continuous = interpolate.interp2d(mask_pixels, mask_pixels, mask, fill_value=0.0) |
| 79 | h, w = shape |
| 80 | ys = np.arange(0.0, h) + 0.5 |
| 81 | xs = np.arange(0.0, w) + 0.5 |
| 82 | ys = (ys - box[1]) / (box[3] - box[1]) * mask.shape[0] |
| 83 | xs = (xs - box[0]) / (box[2] - box[0]) * mask.shape[1] |
| 84 | # Waste a lot of compute since most indices are out-of-border |
| 85 | res = mask_continuous(xs, ys) |
| 86 | return (res >= 0.5).astype('uint8') |
| 87 | else: |
| 88 | # This method (inspired by Detectron) is less accurate but fast. |
| 89 | |
| 90 | # int() is floor |
| 91 | # box fpcoor=0.0 -> intcoor=0.0 |
| 92 | x0, y0 = list(map(int, box[:2] + 0.5)) |
| 93 | # box fpcoor=h -> intcoor=h-1, inclusive |
| 94 | x1, y1 = list(map(int, box[2:] - 0.5)) # inclusive |
| 95 | x1 = max(x0, x1) # require at least 1x1 |
| 96 | y1 = max(y0, y1) |
| 97 | |
| 98 | w = x1 + 1 - x0 |
| 99 | h = y1 + 1 - y0 |
| 100 | |
| 101 | # rounding errors could happen here, because masks were not originally computed for this shape. |
| 102 | # but it's hard to do better, because the network does not know the "original" scale |
| 103 | mask = (cv2.resize(mask, (w, h)) > 0.5).astype('uint8') |
| 104 | ret = np.zeros(shape, dtype='uint8') |
| 105 | ret[y0:y1 + 1, x0:x1 + 1] = mask |
| 106 | return ret |
| 107 | |
| 108 | |
| 109 | def predict_image(img, model_func): |
no test coverage detected