def test_resize_tensor(): from ml import cv H, W, size = 720, 1280, 256 img = th.randn(3, H, W) resized = cv.resize(img, size) h, w = resized.shape[-2:] assert (h, w) == ( size, int(W / H * size) ), f"mismatched after resize: (h, w) == {(h, w)} but {(size, int(W / H * size))} expected" resized = cv.resize(img, 256, constraint='shorter') h, w = resized.shape[-2:] assert (h, w) == ( size, int(W / H * size) ), f"mismatched after resize: (h, w) == {(h, w)} but {(size, int(W / H * size))} expected" resized = cv.resize(img, 256, constraint='longer') h, w = resized.shape[-2:] assert (h, w) == ( int(H / W * size), size ), f"mismatched after resize: (h, w) == {(h, w)} but {(H / W * size, size)} expected"
def test_resize_pil(): from ml import cv from PIL import Image H, W, size = 720, 1280, 256 img = th.randn(3, H, W) img = cv.fromTorch(img) img = Image.fromarray(img) assert img.size == (W, H) assert img.mode == 'RGB' resized = cv.resize(img, size) w, h = resized.size assert (h, w) == ( size, int(W / H * size) ), f"mismatched after resize: (h, w) == {(h, w)} but {(size, int(W / H * size))} expected" resized = cv.resize(img, 256, constraint='shorter') w, h = resized.size assert (h, w) == ( size, int(W / H * size) ), f"mismatched after resize: (h, w) == {(h, w)} but {(size, int(W / H * size))} expected" resized = cv.resize(img, 256, constraint='longer') w, h = resized.size assert (h, w) == ( int(H / W * size), size ), f"mismatched after resize: (h, w) == {(h, w)} but {(H / W * size, size)} expected"
def test_rfcn(tile_img): from ml import cv path = Path(tile_img) img = cv.imread(path) img2 = cv.resize(img, scale=0.5) img = cv.imread(path) model_dir = None # "/tmp/ml/checkpoints" detector = rfcn(pooling=2, model_dir=model_dir, force_reload=True) assert detector.with_rpn rois, dets, pooled = detector.detect(img, return_rpn=True) print('dets:', [tuple(det.shape) for det in dets], dets) print('rois:', [tuple(roi.shape) for roi in rois]) print('pooled:', [tuple(feats.shape) for feats in pooled]) cv.render(img, dets[0], score_thr=0.01, classes=COCO80_CLASSES, path=f"export/{path.name[:-4]}-rfcn.jpg")
def __call__(self, img): return cv.resize(img, self.scale, self.width, self.height, self.interpolation)