|
import numpy as np |
|
import pandas as pd |
|
import h5py |
|
|
|
from trimesh.voxel.runlength import dense_to_brle |
|
from pathlib import Path |
|
from collections import defaultdict |
|
|
|
from typing import Any, Union, Dict, Literal |
|
from numpy.typing import NDArray |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FixedModel: |
|
def __init__(self, shape) -> None: |
|
self.shape = shape |
|
return |
|
|
|
def __call__(self, input) -> Any: |
|
|
|
mask = np.zeros(self.shape, dtype=bool) |
|
mask[100:250, 100:250] = True |
|
return mask |
|
|
|
def retrieve_validation_fold(path: Union[str, Path]) -> Dict[str, NDArray]: |
|
result = defaultdict(dict) |
|
with h5py.File(path, 'r') as fp: |
|
for uuid, values in fp.items(): |
|
if values.attrs['fold'] != 0: |
|
continue |
|
|
|
result[uuid]['post'] = values['post_fire'][...] |
|
|
|
|
|
return dict(result) |
|
|
|
def compute_submission_mask(id: str, mask: NDArray): |
|
brle = dense_to_brle(mask.astype(bool).flatten()) |
|
return {"id": id, "rle_mask": brle, "index": np.arange(len(brle))} |
|
|
|
if __name__ == '__main__': |
|
validation_fold = retrieve_validation_fold('train_eval.hdf5') |
|
|
|
|
|
result = [] |
|
|
|
model = FixedModel(shape=(512, 512)) |
|
for uuid in validation_fold: |
|
input_images = validation_fold[uuid] |
|
|
|
|
|
predicted = model(input_images) |
|
|
|
encoded_prediction = compute_submission_mask(uuid, predicted) |
|
result.append(pd.DataFrame(encoded_prediction)) |
|
|
|
|
|
submission_df = pd.concat(result) |
|
submission_df.to_csv('predictions.csv', index=False) |