Fix: Align model and input data types to float16 for inference
Browse files## Description
This PR fixes a runtime error in the example code caused by a mismatch between the input tensor type and the model weights during inference.
## Changes
- Converted the model to float16:
model = model.to("cuda").half()
- Converted float32 input tensors from the processor to float16:
inputs = {k: v.half() if isinstance(v, torch.Tensor) and v.dtype == torch.float32 else v for k, v in inputs.items()}
## Testing
The code has been successfully tested and runs without error.
## Note
This contribution is part of an ongoing research initiative to systematically identify and correct faulty example code in Hugging Face Model Cards.
We would appreciate a timely review and integration of this patch to support code reliability and enhance reproducibility for downstream users.
@@ -62,7 +62,9 @@ input_points = [[[450, 600]]] # 2D localization of a window
|
|
62 |
|
63 |
|
64 |
```python
|
|
|
65 |
inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
|
|
|
66 |
outputs = model(**inputs)
|
67 |
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
|
68 |
scores = outputs.iou_scores
|
|
|
62 |
|
63 |
|
64 |
```python
|
65 |
+
model = model.to("cuda").half()
|
66 |
inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
|
67 |
+
inputs = {k: v.half() if isinstance(v, torch.Tensor) and v.dtype == torch.float32 else v for k, v in inputs.items()}
|
68 |
outputs = model(**inputs)
|
69 |
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
|
70 |
scores = outputs.iou_scores
|