jagennath-hari commited on
Commit
4239e77
·
1 Parent(s): c04e497

update for transformers compatible

Browse files
Files changed (8) hide show
  1. .gitignore +0 -1
  2. README.md +1 -0
  3. config.json +13 -0
  4. config.yaml +0 -41
  5. configuration_dfine.py +16 -0
  6. model.onnx +3 -0
  7. modeling_dfine.py +51 -0
  8. 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-large--configuration_dfine.DFineConfig",
6
+ "AutoModel": "d-fine-large--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
+ }
config.yaml DELETED
@@ -1,41 +0,0 @@
1
- task: detection
2
-
3
- evaluator:
4
- type: CocoEvaluator
5
- iou_types: ['bbox', ]
6
-
7
- num_classes: 3 # your dataset classes
8
- remap_mscoco_category: False
9
-
10
- train_dataloader:
11
- type: DataLoader
12
- dataset:
13
- type: CocoDetection
14
- img_folder: /dataset/images/train
15
- ann_file: /dataset/annotations/instances_train.json
16
- return_masks: False
17
- transforms:
18
- type: Compose
19
- ops: ~
20
- shuffle: True
21
- num_workers: 4
22
- drop_last: True
23
- collate_fn:
24
- type: BatchImageCollateFunction
25
-
26
-
27
- val_dataloader:
28
- type: DataLoader
29
- dataset:
30
- type: CocoDetection
31
- img_folder: /dataset/images/val
32
- ann_file: /dataset/annotations/instances_val.json
33
- return_masks: False
34
- transforms:
35
- type: Compose
36
- ops: ~
37
- shuffle: False
38
- num_workers: 4
39
- drop_last: False
40
- collate_fn:
41
- type: BatchImageCollateFunction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:fa29629571161f24e347e7190ee10432dbd4a1787f01893f84beea9fc00584da
3
+ size 123992203
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-large",
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