lc-col commited on
Commit
58b19ed
1 Parent(s): 1abae0d

Uploaded create_sample_submission.py

Browse files
Files changed (1) hide show
  1. create_sample_submission.py +66 -0
create_sample_submission.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import h5py
4
+
5
+ from trimesh.voxel.runlength import dense_to_brle
6
+ from pathlib import Path
7
+ from collections import defaultdict
8
+
9
+ from typing import Any, Union, Dict, Literal
10
+ from numpy.typing import NDArray
11
+
12
+ # class RandomModel:
13
+ # def __init__(self, shape):
14
+ # self.shape = shape
15
+ # return
16
+
17
+ # def __call__(self, input):
18
+ # # input is ignored, just generate some random predictions
19
+ # return np.random.randint(0, 2, size=self.shape, dtype=bool)
20
+
21
+ class FixedModel:
22
+ def __init__(self, shape) -> None:
23
+ self.shape = shape
24
+ return
25
+
26
+ def __call__(self, input) -> Any:
27
+ # input is ignored, just generate a mask filled with zeros, with fixed pixels set to 1
28
+ mask = np.zeros(self.shape, dtype=bool)
29
+ mask[100:250, 100:250] = True
30
+ return mask
31
+
32
+ def retrieve_validation_fold(path: Union[str, Path]) -> Dict[str, NDArray]:
33
+ result = defaultdict(dict)
34
+ with h5py.File(path, 'r') as fp:
35
+ for uuid, values in fp.items():
36
+ if values.attrs['fold'] != 0:
37
+ continue
38
+
39
+ result[uuid]['post'] = values['post_fire'][...]
40
+ # result[uuid]['pre'] = values['pre_fire'][...]
41
+
42
+ return dict(result)
43
+
44
+ def compute_submission_mask(id: str, mask: NDArray):
45
+ brle = dense_to_brle(mask.astype(bool).flatten())
46
+ return {"id": id, "rle_mask": brle, "index": np.arange(len(brle))}
47
+
48
+ if __name__ == '__main__':
49
+ validation_fold = retrieve_validation_fold('train_eval.hdf5')
50
+
51
+ # use a list to accumulate results
52
+ result = []
53
+ # instantiate the model
54
+ model = FixedModel(shape=(512, 512))
55
+ for uuid in validation_fold:
56
+ input_images = validation_fold[uuid]
57
+
58
+ # perform the prediction
59
+ predicted = model(input_images)
60
+ # convert the prediction in RLE format
61
+ encoded_prediction = compute_submission_mask(uuid, predicted)
62
+ result.append(pd.DataFrame(encoded_prediction))
63
+
64
+ # concatenate all dataframes
65
+ submission_df = pd.concat(result)
66
+ submission_df.to_csv('predictions.csv', index=False)