jagennath-hari commited on
Commit
bcb3888
·
1 Parent(s): 9b73f1c

update for transformers compatible

Browse files
Files changed (7) hide show
  1. .gitignore +0 -1
  2. README.md +1 -0
  3. config.json +13 -0
  4. configuration_dfine.py +16 -0
  5. model.onnx +3 -0
  6. modeling_dfine.py +51 -0
  7. requirements.txt +1 -0
.gitignore DELETED
@@ -1 +0,0 @@
1
- *.py
 
 
README.md CHANGED
@@ -5,6 +5,7 @@ license: apache-2.0
5
  tags:
6
  - object-detection
7
  - AgTech
 
8
  library_name: pytorch
9
  inference: false
10
  datasets:
 
5
  tags:
6
  - object-detection
7
  - AgTech
8
+ - transformers
9
  library_name: pytorch
10
  inference: false
11
  datasets:
config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "dfine",
3
+ "architectures": ["DFineModel"],
4
+ "auto_map": {
5
+ "AutoConfig": "d-fine-small--configuration_dfine.DFineConfig",
6
+ "AutoModel": "d-fine-small--modeling_dfine.DFineModel"
7
+ },
8
+ "torch_dtype": "float32",
9
+ "transformers_version": "4.51.3",
10
+ "input_size": [640, 640],
11
+ "input_components": ["images", "orig_target_sizes", "ratio", "pad_w", "pad_h"],
12
+ "output_components": ["labels", "boxes", "scores"]
13
+ }
configuration_dfine.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+ class DFineConfig(PretrainedConfig):
4
+ model_type = "dfine"
5
+
6
+ def __init__(
7
+ self,
8
+ input_size=[640, 640],
9
+ input_components=["images", "orig_target_sizes"],
10
+ output_components=["labels", "boxes", "scores"],
11
+ **kwargs
12
+ ):
13
+ super().__init__(**kwargs)
14
+ self.input_size = input_size
15
+ self.input_components = input_components
16
+ self.output_components = output_components
model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea58338e21a9bfe88cb4505cbd543c55ff0a4e396d7ce18db1bd047374c1889f
3
+ size 41597960
modeling_dfine.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import onnxruntime as ort
4
+ from huggingface_hub import hf_hub_download
5
+ from transformers import PreTrainedModel
6
+ from .configuration_dfine import DFineConfig
7
+
8
+ class DFineModel(PreTrainedModel):
9
+ config_class = DFineConfig
10
+
11
+ def __init__(self, config):
12
+ super().__init__(config)
13
+ model_path = hf_hub_download(
14
+ repo_id="Laudando-Associates-LLC/d-fine-small",
15
+ filename="model.onnx"
16
+ )
17
+ self.session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
18
+
19
+ def forward(self, images, orig_target_sizes, ratio, pad_w, pad_h, conf_threshold=0.5):
20
+ output = self.session.run(
21
+ output_names=None,
22
+ input_feed={"images": images.numpy(), "orig_target_sizes": orig_target_sizes.numpy()},
23
+ )
24
+ labels, boxes, scores = output
25
+
26
+ # Convert to torch
27
+ labels = torch.tensor(labels)
28
+ boxes = torch.tensor(boxes)
29
+ scores = torch.tensor(scores)
30
+
31
+ # Filter by confidence per image
32
+ results = []
33
+ for i in range(scores.shape[0]):
34
+ keep = scores[i] > conf_threshold
35
+ labels_kept = labels[i][keep]
36
+ boxes_kept = boxes[i][keep]
37
+ scores_kept = scores[i][keep]
38
+
39
+ # Auto-scale boxes back to original image resolution
40
+ boxes_scaled = boxes_kept.clone()
41
+ boxes_scaled[:, 0::2] -= pad_w[i]
42
+ boxes_scaled[:, 1::2] -= pad_h[i]
43
+ boxes_scaled /= ratio[i]
44
+
45
+ results.append({
46
+ "labels": labels_kept,
47
+ "boxes": boxes_scaled,
48
+ "scores": scores_kept
49
+ })
50
+
51
+ return results
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ onnxruntime