@@ -50,7 +50,7 @@ def __getitem__(self, idx):
5050)
5151
5252# %%
53- # Define config
53+ # Define model config
5454config = {
5555 "model_kwargs" : {
5656 "num_classes" : 2 ,
@@ -60,11 +60,6 @@ def __getitem__(self, idx):
6060 "checkpoint" : "/home/sminano/swc/project_crabs/ml-runs/617393114420881798/f348d9d196934073bece1b877cbc4d38/checkpoints/last.ckpt" ,
6161}
6262
63- # %%
64- # Instantiate detector
65-
66- model = SingleDetector (config )
67-
6863# %%
6964# Define dataset
7065
@@ -99,8 +94,14 @@ def __getitem__(self, idx):
9994 # else None, # see https://github.com/pytorch/pytorch/issues/87688
10095)
10196# %%
102- # Run inference on dataset
97+ # Run inference using model on dataset
10398# (format as detections dataset)
99+
100+
101+ # Instantiate detector
102+ model = SingleDetector (config )
103+
104+ # Define trainer
104105trainer = Trainer (
105106 accelerator = "gpu" ,
106107 devices = 1 ,
@@ -109,30 +110,42 @@ def __getitem__(self, idx):
109110 # uses FP16 for most operations, FP32 for sensitive ones
110111 # This setting reduces memory and speeds up training
111112)
112- predictions = trainer .predict (model , dataloader )
113+
114+ # dataset attrs
115+ ds_attrs = {
116+ "images_dir" : images_dir ,
117+ "map_image_id_to_filename" : {
118+ id : filename .relative_to (
119+ "/home/sminano/swc/project_ethology/07.09.2023-frames"
120+ )
121+ for id , filename in enumerate (dataset .image_files )
122+ },
123+ "map_category_to_str" : {1 : "crab" },
124+ }
125+
126+ predictions_ds = model .run_inference (trainer , dataloader , attrs = ds_attrs )
113127
114128
115129# %%
116- # TODO: can I do this at the end of the predict epoch?
117- predictions_ds = model .format_predictions (
118- predictions ,
119- {
120- "images_dir" : images_dir ,
121- "map_image_id_to_filename" : {
122- id : filename .relative_to (
123- "/home/sminano/swc/project_ethology/07.09.2023-frames"
124- )
125- for id , filename in enumerate (dataset .image_files )
126- },
127- "map_category_to_str" : {1 : "crab" },
128- },
129- )
130+ # Alternative
131+ # # TODO: can I do this at the end of the predict epoch?
132+ # predictions = trainer.predict(model, dataloader)
133+ # predictions_ds = model.format_predictions(
134+ # predictions,
135+ # {
136+ # "images_dir": images_dir,
137+ # "map_image_id_to_filename": {
138+ # id: filename.relative_to(
139+ # "/home/sminano/swc/project_ethology/07.09.2023-frames"
140+ # )
141+ # for id, filename in enumerate(dataset.image_files)
142+ # },
143+ # "map_category_to_str": {1: "crab"},
144+ # },
145+ # )
130146
131147# %%
132148# Export as COCO annotations?
133- # TODO: require "category" array in input dataset?
134- # I think so, I dont think there are detectors that return label-less boxes?
135- # predictions_ds = predictions_ds.rename_vars({'category':'label'})
136149out_file = save_bboxes .to_COCO_file (predictions_ds , output_filepath = "out.json" )
137150# %%
138151# Load proofread annotations
0 commit comments