1+ # Copyright (c) 2025, InterDigital Communications, Inc
2+ # All rights reserved.
3+
4+ # Redistribution and use in source and binary forms, with or without
5+ # modification, are permitted (subject to the limitations in the disclaimer
6+ # below) provided that the following conditions are met:
7+
8+ # * Redistributions of source code must retain the above copyright notice,
9+ # this list of conditions and the following disclaimer.
10+ # * Redistributions in binary form must reproduce the above copyright notice,
11+ # this list of conditions and the following disclaimer in the documentation
12+ # and/or other materials provided with the distribution.
13+ # * Neither the name of InterDigital Communications, Inc nor the names of its
14+ # contributors may be used to endorse or promote products derived from this
15+ # software without specific prior written permission.
16+
17+ # NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
18+ # THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19+ # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20+ # NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21+ # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25+ # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26+ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27+ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28+ # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
130import base64
231import csv
332import os
@@ -44,8 +73,10 @@ def __repr__(self):
4473
4574
4675def mask_to_bbx (mask ):
47- mask = mask .cpu ()
48- mask = np .array (mask )
76+ if not isinstance (mask , np .ndarray ):
77+ mask = mask .cpu ()
78+ mask = np .array (mask )
79+
4980 mask = np .squeeze (mask )
5081 h , w = mask .shape [- 2 :]
5182 rows , cols = np .where (mask )
@@ -228,43 +259,31 @@ def _image_encoder_to_output(
228259 dense_prompt_embeddings = prompt_feature [1 ],
229260 multimask_output = False ,
230261 )
231- # print("len low_res_masks", len(low_res_masks))
262+
232263 # post process mask
233264 masks = F .interpolate (
234265 low_res_masks ,
235266 (self .image_encoder .img_size , self .image_encoder .img_size ),
236267 mode = "bilinear" ,
237268 align_corners = False ,
238269 )
239- masks = masks [
240- ..., : input_img_size [0 ], : input_img_size [1 ]
241- ] # [..., : 793, : 1024]
270+ masks = masks [..., : input_img_size [0 ], : input_img_size [1 ]]
242271 masks = F .interpolate (
243272 masks ,
244273 (org_img_size ["height" ], org_img_size ["width" ]),
245274 mode = "bilinear" ,
246275 align_corners = False ,
247276 )
248277
249- # masks1 = self.postprocess_masks(
250- # masks= low_res_masks,
251- # input_size=input_img_size,
252- # original_size=org_img_size,
253- # )
254278 mask_threshold = 0.0
255279 masks = masks > mask_threshold
256- # print("len masks", len(masks), masks[0].shape)
257- # name = '/t/vic/hevc_simulations/rosen/build/main-20250423-sam1/masks' + str(input_img_size[0]) + '.pt'
258- # torch.save(masks[0], name) #"/t/vic/hevc_simulations/rosen/build/main-20250423-sam1/masks.pt")
259280
260281 # post process result
261282 processed_results = []
262283 boxes = mask_to_bbx (masks [0 ])
263- # print("boxes", boxes)
264284 boxes = Boxes (torch .tensor (np .array ([boxes ])))
265285 scores = torch .tensor ([iou_pred ])
266- classes = torch .tensor (object_classes ) # 48 for sandwich,
267- # masks = torch.rand(1, 683, 1024) # Example binary mask
286+ classes = torch .tensor (object_classes )
268287
269288 from detectron2 .structures import Instances
270289
@@ -273,14 +292,9 @@ def _image_encoder_to_output(
273292 instances .set ("pred_boxes" , boxes )
274293 instances .set ("scores" , scores )
275294 instances .set ("pred_classes" , classes )
276- instances .set ("pred_masks" , masks [0 ]) # ✅ Now a real tensor
295+ instances .set ("pred_masks" , masks [0 ])
277296
278- # Wrap in result
279- # result = [f"{{'instances': {instances}}}"]
280- # print("result", result)
281- # print("instances", instances.get_fields().keys(), len(instances))
282297 processed_results .append ({"instances" : instances })
283- # print("processed_results", len(processed_results))
284298 return processed_results
285299
286300 @torch .no_grad ()
0 commit comments