def test_padding(small_dataset, model): # mAP should be similar after padding to a square preds_normal = [] preds_padded = [] for (fpath, img) in small_dataset: response = get_prediction(fpath, model) result = [box for box in response.json()[0]] preds_normal.append(result) tmp_img = Image.open(fpath) w, h = tmp_img.size target = max((w, h)) dw = (target - w) / 2 dh = (target - h) / 2 tmp_img = pad(tmp_img, (target, target)) with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp: padded_path = tmp.name tmp_img.save(padded_path) response = get_prediction(padded_path, model) result = [box for box in response.json()[0]] # half every box: for box in result: box["x1"] -= dw box["y1"] -= dh box["x2"] -= dw box["y2"] -= dh preds_padded.append(result) mAP_normal = small_dataset.eval(preds_normal) mAP_padded = small_dataset.eval(preds_padded) print(f"Padding diff: {abs(mAP_normal - mAP_padded)}") assert abs(mAP_normal - mAP_padded) < 0.05 # 5% diff seems acceptable
def test_padding(small_dataset, model): # mAP should be similar after padding to a square preds_normal = [] preds_padded = [] for (fpath, img) in small_dataset: response = get_prediction(fpath, model) result = [ann for ann in response.json()[0]] preds_normal.append(result) tmp_img = Image.open(fpath) w, h = tmp_img.size target = max((w, h)) dw = (target - w) / 2 dh = (target - h) / 2 tmp_img = pad(tmp_img, (target, target)) with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp: padded_path = tmp.name tmp_img.save(padded_path) response = get_prediction(padded_path, model) result = [ann for ann in response.json()[0]] # unpad every coord: for ann in result: ann["segmentation"]["segmentation"] = [ p - dw if i % 2 == 0 else p - dh for i, p in enumerate(ann["segmentation"]["segmentation"]) ] preds_padded.append(result) mAP_normal = small_dataset.eval(preds_normal) mAP_padded = small_dataset.eval(preds_padded) print(f"Padding diff: {abs(mAP_normal - mAP_padded)}") assert abs(mAP_normal - mAP_padded) < 0.07 # 5% diff seems acceptable
def image_to_unicode(im: Image, invert: bool = False) -> str: char_for = dict_unicode_inverted if invert else dict_unicode width = im.width height = im.height + (im.height % 2) padded_im = pad(image=im, size=(width, height)) a = padded_im.load() output_rows = [] for r in range(0, height, 2): char_list = [char_for[(a[c, r], a[c, r + 1])] for c in range(width)] row = "".join(char_list) output_rows.append(row) output_str = "\n".join(output_rows) return output_str
def test_padding(small_dataset, model): # mAP should be similar after padding to a square preds_normal = [] preds_padded = [] for fpath, img in small_dataset: response = get_prediction(fpath, model) preds_normal.append(response.json()[0]) tmp_img = Image.open(fpath) w, h = tmp_img.size target = max((w, h)) dw = (target - w) / 2 dh = (target - h) / 2 tmp_img = pad(tmp_img, (target, target)) with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp: padded_path = tmp.name tmp_img.save(padded_path) response = get_prediction(padded_path, model) preds_padded.append(response.json()[0]) score_normal = small_dataset.eval(preds_normal) score_padded = small_dataset.eval(preds_padded) print(f"Padding diff: {abs(score_normal - score_padded)}") assert abs(score_normal - score_padded) < 0.1 # 10% diff seems acceptable