def test_batch(): """Test the batch-wise to submission.""" base_path = Path(__file__).parent / "data" test_img_path = base_path / "rgb_mask_example.png" mask = read_rgb_mask(test_img_path) tensor_mask = from_numpy(mask) batch = [ ("alpha.jpg", "Pead", "Haricot", tensor_mask), ("bravo.jpg", "Bipbip", "Haricot", tensor_mask), ("charlie.jpg", "Roseau", "Mais", tensor_mask), ] submission = { filename: asdict(_sub) for filename, _sub in [sample_to_submission(a, b, c, d) for a, b, c, d in batch] } for _file, _team, _crop, _ in batch: assert _file in submission assert submission[_file]["shape"] == [1536, 2048] assert submission[_file]["team"] == _team assert submission[_file]["crop"] == _crop assert submission[_file]["segmentation"]["crop"] is not None assert submission[_file]["segmentation"]["weed"] is not None
def test_mask_to_json(): """Test that the mask can be wrapped up as specified.""" base_path = Path(__file__).parent / "data" test_img_path = base_path / "rgb_mask_example.png" mask = read_rgb_mask(test_img_path) tensor_mask = from_numpy(mask) batch = ("arr_mask_example", "Bipbip", "Haricot", tensor_mask) submission_dict = sample_to_submission(*batch) assert submission_dict.filename == "arr_mask_example" assert submission_dict.shape == [1536, 2048] assert submission_dict.team == "Bipbip" assert submission_dict.crop == "Haricot" assert submission_dict.segmentation.crop is not None assert submission_dict.segmentation.weed is not None submission_dict = asdict(submission_dict) submission_dict = {submission_dict.pop("filename"): submission_dict} sample = base_path / "sample_submission.json" with open(f"{sample}") as json_file: sample_json = json.load(json_file) assert sample_json == submission_dict
def test_to_mask_tens(): """Test tensor loading is same as provided function.""" base_path = Path(__file__).parent / "data" test_img_path = base_path / "rgb_mask_example.png" mask_img = Image.open(test_img_path) tensor_mask = IndexEncodeMask()(mask_img) np_tens_mask = torch.as_tensor(read_rgb_mask(test_img_path)).t() torch.testing.assert_allclose(tensor_mask, np_tens_mask)
def test_encoding(): """Test the encoding.""" base_path = Path(__file__).parent / "data" test_img_path = base_path / "rgb_mask_example.png" mask = read_rgb_mask(test_img_path) assert mask is not None given_mask = np.load(base_path / "arr_mask_example.npy") assert given_mask is not None np.testing.assert_array_equal(mask, given_mask)
def test_mask_to_json(): """Test that the mask can be wrapped up as specified.""" base_path = Path(__file__).parent / "data" test_img_path = base_path / "rgb_mask_example.png" mask = read_rgb_mask(test_img_path) tensor_mask = from_numpy(mask) batch = ("bob.jpg", "Bipbip", "Haricot", tensor_mask) filename, submission_dict = sample_to_submission(*batch) assert filename == "bob.jpg" assert submission_dict.shape == [1536, 2048] assert submission_dict.team == "Bipbip" assert submission_dict.crop == "Haricot" assert submission_dict.segmentation.crop is not None assert submission_dict.segmentation.weed is not None