submission.py (2396B)
1 from typing import Any, Dict 2 import argparse 3 import collections 4 import json 5 import os 6 import pathlib 7 import pickle 8 import zipfile 9 10 import mmsrl.utils 11 12 13 def make_submission_file(config: mmsrl.utils.dotdict) -> None: 14 inputs: Dict[str, Dict[str, Any]] = collections.defaultdict(dict) 15 with config.prediction.open("rb") as input_file: 16 try: 17 while True: 18 image, entities = pickle.load(input_file) 19 for entity, prediction in entities.items(): 20 inputs[image][entity] = prediction 21 except EOFError: 22 pass 23 24 data_path = pathlib.Path(os.environ['DATA_PATH']) 25 annot_file = data_path / "mmsrl" / "all" / "annotations" / f"unseen_test.jsonl" 26 with annot_file.open('r') as json_file: 27 dataset = list(map(json.loads, json_file)) 28 29 outputfile_path = config.output / pathlib.Path(config.prediction.stem).with_suffix('.json') 30 with outputfile_path.open("w") as output_file: 31 for sample in dataset: 32 image = sample["image"] 33 output: Dict[str, Any] = {"image": image} 34 for label in mmsrl.utils.LABELS: 35 output[label] = [] 36 37 for i, entity in enumerate(sample["entity_list"]): 38 if entity == "": 39 prediction = mmsrl.utils.LABELS.index("other") 40 else: 41 predictions = [(p, i) for i, p in enumerate(inputs[image][entity].tolist())] 42 predictions.sort(reverse=True) 43 if len(set(entities)) == len(entities) or entities.count(entity) == 1: 44 prediction = predictions[0][1] 45 else: 46 prediction = predictions[entities[:i].count(entity)][1] 47 output[mmsrl.utils.LABELS[prediction]].append(entity) 48 49 print(json.dumps(output), file=output_file) 50 51 with zipfile.ZipFile(str(outputfile_path.with_suffix('.zip')), mode="w") as archive: 52 archive.write(str(outputfile_path), arcname="answer.jsonl") 53 54 if __name__ == "__main__": 55 parser = argparse.ArgumentParser() 56 parser.add_argument("output", type=pathlib.Path, help="Path were we write the submission json file.") 57 parser.add_argument("prediction", type=pathlib.Path, help="Prediction file to use.") 58 args = mmsrl.utils.dotdict(vars(parser.parse_args())) 59 make_submission_file(args)