rayh commited on
Commit
2b50ca3
·
unverified ·
1 Parent(s): 933bb62

Uisng the model.py approach

Browse files
Files changed (3) hide show
  1. config.json +7 -1
  2. model.py +29 -0
  3. modelling_yolo.py +0 -22
config.json CHANGED
@@ -4,5 +4,11 @@
4
  ],
5
  "model_type": "yolo-segmentation",
6
  "onnx_model": "astro-yolo11m-seg.onnx",
7
- "task": "image-segmentation"
 
 
 
 
 
 
8
  }
 
4
  ],
5
  "model_type": "yolo-segmentation",
6
  "onnx_model": "astro-yolo11m-seg.onnx",
7
+ "task": "image-segmentation",
8
+ "widgets": [
9
+ {
10
+ "src": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/image-segmentation-input.jpg"
11
+ }
12
+ ],
13
+ "library_name": "onnxruntime"
14
  }
model.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import onnxruntime as ort
3
+ import torch
4
+ from huggingface_hub import hf_hub_download
5
+ from PIL import Image
6
+
7
+
8
+ class YOLOSegmentationModel:
9
+ def __init__(self):
10
+ # Download and load the ONNX model from Hugging Face Hub
11
+ model_path = hf_hub_download(repo_id="rayh/astro-seg", filename="astro-yolo11m-seg.onnx")
12
+ self.session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
13
+
14
+ def preprocess(self, image: Image.Image):
15
+ # Convert image to RGB and preprocess for ONNX model
16
+ input_array = np.array(image.convert("RGB")).astype(np.float32)
17
+ input_array = np.expand_dims(input_array, axis=0) # Add batch dimension
18
+ return input_array
19
+
20
+ def predict(self, image: Image.Image):
21
+ input_tensor = self.preprocess(image)
22
+ outputs = self.session.run(None, {"images": input_tensor})
23
+ return outputs # Modify if needed to return bounding boxes/masks
24
+
25
+ model = YOLOSegmentationModel()
26
+
27
+ # HF Inference API expects a `predict` function
28
+ def predict(image: Image.Image):
29
+ return model.predict(image)
modelling_yolo.py DELETED
@@ -1,22 +0,0 @@
1
- import torch
2
- import onnxruntime as ort
3
- from transformers import PreTrainedModel, PretrainedConfig
4
- from PIL import Image
5
- import numpy as np
6
-
7
- class YOLOConfig(PretrainedConfig):
8
- model_type = "yolo-segmentation"
9
-
10
- class YOLOTransformersModel(PreTrainedModel):
11
- config_class = YOLOConfig
12
-
13
- def __init__(self, config):
14
- super().__init__(config)
15
- self.session = ort.InferenceSession(config.onnx_model, providers=["CPUExecutionProvider"])
16
-
17
- def forward(self, images):
18
- input_array = np.array(images.convert("RGB")).astype(np.float32)
19
- input_array = np.expand_dims(input_array, axis=0) # Add batch dimension
20
-
21
- outputs = self.session.run(None, {"images": input_array})
22
- return outputs # Modify as needed to match Transformers format