haiquanua commited on
Commit
57874a4
·
verified ·
1 Parent(s): 7449ef8

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +4 -1
  2. README.md +1 -1
  3. app.py +39 -135
  4. gitattributes +1 -0
Dockerfile CHANGED
@@ -40,9 +40,12 @@ RUN mkdir -p /tmp/huggingface/hub && mkdir -p /tmp/huggingface/transformers && c
40
 
41
  RUN python -m pip install gradio>=4.0.0
42
  RUN python -m pip install supervision
 
 
43
  # Your app
44
  WORKDIR /app
45
- COPY app.py /app/
 
46
 
47
  EXPOSE 7860
48
  ENV PORT=7860
 
40
 
41
  RUN python -m pip install gradio>=4.0.0
42
  RUN python -m pip install supervision
43
+ RUN python -m pip install timm
44
+
45
  # Your app
46
  WORKDIR /app
47
+ #COPY app.py /app/
48
+ COPY . .
49
 
50
  EXPOSE 7860
51
  ENV PORT=7860
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: BAT102
3
  emoji: 🔥
4
  colorFrom: green
5
  colorTo: green
 
1
  ---
2
+ title: Cure
3
  emoji: 🔥
4
  colorFrom: green
5
  colorTo: green
app.py CHANGED
@@ -1,7 +1,7 @@
1
- import io, os, sys
2
  from typing import List, Tuple
3
  from PIL import Image, ImageDraw, ImageFont
4
- #from transformers import pipeline
5
  from huggingface_hub import snapshot_download
6
  #import transformers
7
  import pprint
@@ -19,131 +19,37 @@ import supervision as sv
19
  #import mmcv
20
  from mmdet.apis import inference_detector
21
  import numpy as np
22
- from supervision import Detections
23
- from typing import List, Dict, Union, Optional
 
 
 
24
 
25
- CONFIDENCE_THRESHOLD = 0.5
26
  NMS_IOU_THRESHOLD = 0.5
27
 
28
- # 1) build model from config
29
- repo_dir = snapshot_download(repo_id="haiquanua/weed_swin")
30
 
31
- cfg = Config.fromfile(f"{repo_dir}/configs/mmdet_swin_config.py", lazy_import=False)
32
- register_all_modules()
33
- detector = MODELS.build(cfg.model) #this does not work in mmcv v2
34
-
35
- # 2) load safetensors weights
36
- state_dict = load_file(f"{repo_dir}/model.safetensors") # strictly tensors only
37
- missing, unexpected = detector.load_state_dict(state_dict, strict=False)
38
- print("missing:", len(missing), "unexpected:", len(unexpected))
39
- detector.eval()
40
- detector.cfg = cfg
41
-
42
- repo_dir = os.path.join(os.path.dirname(__file__), "../weed_swin")
43
- sys.path.insert(0, repo_dir)
44
- #import pipeline
45
- # Load an object-detection pipeline (pick any model you like)
46
  #detector = pipeline("object-detection", model="facebook/detr-resnet-50")
47
- #detector = pipeline("object-detection", model="haiquanua/weed_detectron2")
48
  #detector = pipeline("object-detection", model="haiquanua/weed_detr")
49
- #repo_dir = snapshot_download("haiquanua/weed_swin")
50
- pprint.pp(sorted(PIPELINE_REGISTRY.get_supported_tasks()))
51
- #detector = pipeline(task="mmdet-detection", model="weed_swin", trust_remote_code=True)
52
- class_names={0:"weed", 1:"letuce", 2:"spinach"}
53
-
54
- def _to_hf_items_from_sv(
55
- det: "Detections",
56
- class_names: List[str],
57
- score_threshold: Optional[float] = None,
58
- top_k: Optional[int] = None,
59
- clip_shape: Optional[tuple] = None, # (H, W)
60
- ) -> List[Dict]:
61
- """
62
- Convert a single `supervision.Detections` to HF object-detection items.
63
- Parameters
64
- ----------
65
- det : supervision.Detections
66
- Expected fields: det.xyxy (N, 4), det.class_id (N,), optional det.confidence (N,)
67
- class_names : list[str]
68
- Maps numeric class_id -> human-readable label.
69
- score_threshold : float, optional
70
- Keep only detections with confidence >= threshold. If det.confidence is None, ignored.
71
- top_k : int, optional
72
- Keep only the top_k by confidence (if available), else by input order.
73
- clip_shape : (H, W), optional
74
- If provided, clip boxes to [0, W] x [0, H].
75
- """
76
- xyxy = np.asarray(det.xyxy, dtype=float) if hasattr(det, "xyxy") else None
77
- class_id = np.asarray(det.class_id, dtype=int) if hasattr(det, "class_id") else None
78
- conf = getattr(det, "confidence", None)
79
- conf = np.asarray(conf, dtype=float) if conf is not None else None
80
-
81
- if xyxy is None or class_id is None:
82
- raise ValueError("Detections must have 'xyxy' and 'class_id' fields.")
83
-
84
- n = xyxy.shape[0]
85
- idx = np.arange(n)
86
-
87
- # Threshold by confidence if available
88
- if conf is not None and score_threshold is not None:
89
- idx = idx[(conf[idx] >= score_threshold)]
90
-
91
- # Top-k (sort by confidence if present)
92
- if top_k is not None:
93
- if conf is not None:
94
- order = np.argsort(-conf[idx])
95
- idx = idx[order][:top_k]
96
- else:
97
- idx = idx[:top_k]
98
-
99
- # Clip boxes if requested
100
- if clip_shape is not None:
101
- H, W = clip_shape
102
- xyxy_clipped = xyxy.copy()
103
- xyxy_clipped[:, 0] = np.clip(xyxy_clipped[:, 0], 0, W) # xmin
104
- xyxy_clipped[:, 2] = np.clip(xyxy_clipped[:, 2], 0, W) # xmax
105
- xyxy_clipped[:, 1] = np.clip(xyxy_clipped[:, 1], 0, H) # ymin
106
- xyxy_clipped[:, 3] = np.clip(xyxy_clipped[:, 3], 0, H) # ymax
107
- xyxy = xyxy_clipped
108
-
109
- out = []
110
- for i in idx:
111
- cid = int(class_id[i])
112
- lbl = class_names[cid] if 0 <= cid < len(class_names) else str(cid)
113
- score = float(conf[i]) if conf is not None else 1.0
114
- x1, y1, x2, y2 = [float(v) for v in xyxy[i]]
115
- out.append(
116
- {
117
- "score": score,
118
- "label": lbl,
119
- "box": {"xmin": x1, "ymin": y1, "xmax": x2, "ymax": y2},
120
- }
121
- )
122
- return out
123
-
124
-
125
- def supervision_to_hf(
126
- results: Union["Detections", List["Detections"]],
127
- class_names: List[str],
128
- score_threshold: Optional[float] = None,
129
- top_k: Optional[int] = None,
130
- clip_shape: Optional[tuple] = None, # (H, W)
131
- ):
132
- """
133
- Convert supervision results (single or list) to HF object-detection format.
134
- Returns
135
- -------
136
- list[dict] for a single Detections input,
137
- list[list[dict]] for a list (batch) input.
138
- """
139
- if isinstance(results, list):
140
- batch_out = [
141
- _to_hf_items_from_sv(d, class_names, score_threshold, top_k, clip_shape)
142
- for d in results
143
- ]
144
- return batch_out
145
- else:
146
- return _to_hf_items_from_sv(results, class_names, score_threshold, top_k, clip_shape)
147
 
148
  def draw_boxes(im: Image.Image, preds, threshold: float = 0.25, class_map={"LABEL_0":"Weed", "LABEL_1":"lettuce","LABEL_2":"Spinach"}) -> Image.Image:
149
  """Draw bounding boxes + labels on a PIL image."""
@@ -190,20 +96,13 @@ def detect_multiple(images: List[Image.Image], threshold: float = 0.25) -> List[
190
  suitable for gr.Gallery. Each image is annotated with boxes.
191
  """
192
  outputs = []
193
- # Batch through the HF pipeline (it accepts lists)
194
- #results = detector(images) # list of lists of predictions
195
-
196
- #print(images)
197
- results = inference_detector(detector, np.array(images)[:, :, ::-1])
198
-
199
- print("\nRaw Predictions (pred_instances):")
200
- #print(result)
201
-
202
- results = sv.Detections.from_mmdetection(results)
203
- results = results[results.confidence > CONFIDENCE_THRESHOLD].with_nms(threshold=NMS_IOU_THRESHOLD)
204
- #print(results)
205
- results = supervision_to_hf(results, class_names, score_threshold=CONFIDENCE_THRESHOLD, top_k=100, clip_shape=None)
206
  #print(results)
 
207
  if not isinstance(images, list):
208
  annotated = draw_boxes(images.copy(), results, threshold)
209
  outputs.append(annotated)
@@ -236,7 +135,12 @@ with gr.Blocks(title="Multi-Image Object Detection") as demo:
236
 
237
  gr.Markdown("Tip: You can drag-select multiple files in the picker or paste from clipboard.")
238
 
239
- print("finished blocks setting")
 
240
 
 
 
 
 
241
  demo.queue(max_size=16).launch(server_name="0.0.0.0",server_port=7860, share=False, show_error=True)
242
 
 
1
+ import io, os, sys
2
  from typing import List, Tuple
3
  from PIL import Image, ImageDraw, ImageFont
4
+ from transformers import pipeline
5
  from huggingface_hub import snapshot_download
6
  #import transformers
7
  import pprint
 
19
  #import mmcv
20
  from mmdet.apis import inference_detector
21
  import numpy as np
22
+ from supervision import Detections
23
+ from typing import List, Dict, Union, Optional
24
+ from transformers import (
25
+ AutoConfig, AutoModelForObjectDetection, AutoImageProcessor, pipeline
26
+ )
27
 
28
+ CONFIDENCE_THRESHOLD = 0.5
29
  NMS_IOU_THRESHOLD = 0.5
30
 
 
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  #detector = pipeline("object-detection", model="facebook/detr-resnet-50")
 
33
  #detector = pipeline("object-detection", model="haiquanua/weed_detr")
34
+
35
+ repo_path="haiquanua/weed_swin"
36
+
37
+ model = AutoModelForObjectDetection.from_pretrained(
38
+ repo_path, trust_remote_code=True
39
+ )
40
+ #print("Model class:", type(model).__name__) # expect: MmdetBridge
41
+
42
+ ip = AutoImageProcessor.from_pretrained(
43
+ repo_path, trust_remote_code=True
44
+ )
45
+ #print("Processor class:", type(ip).__name__) # expect: MmdetImageProcessor
46
+
47
+ #detector = pipeline(task="mmdet-detection", model=repo_path, trust_remote_code=True)
48
+ detector = pipeline(task="object-detection", model=model, image_processor=ip, trust_remote_code=True)
49
+
50
+ num_head_params = sum(p.numel() for n,p in detector.model.named_parameters() if 'roi_head' in n or 'rpn_head' in n)
51
+ print("roi/rpn params after pipeline setup:", num_head_params)
52
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  def draw_boxes(im: Image.Image, preds, threshold: float = 0.25, class_map={"LABEL_0":"Weed", "LABEL_1":"lettuce","LABEL_2":"Spinach"}) -> Image.Image:
55
  """Draw bounding boxes + labels on a PIL image."""
 
96
  suitable for gr.Gallery. Each image is annotated with boxes.
97
  """
98
  outputs = []
99
+ if detector is None:
100
+ gr.Error("detector is empty")
101
+ #else:
102
+ # gr.Info(f"dector is {type(detector).__name__}")
103
+ results = detector(images, threshold=threshold) # list of lists of predictions
 
 
 
 
 
 
 
 
104
  #print(results)
105
+ #gr.Info("get results")
106
  if not isinstance(images, list):
107
  annotated = draw_boxes(images.copy(), results, threshold)
108
  outputs.append(annotated)
 
135
 
136
  gr.Markdown("Tip: You can drag-select multiple files in the picker or paste from clipboard.")
137
 
138
+ gr.Info(detector.__dict__)
139
+ gr.Info("finished blocks setting")
140
 
141
+ #image=Image.open(Path(__file__).resolve().parent / "test.jpg")
142
+ #print(image.size)
143
+ #results = detector(image, padding=True, threshold=0.0)
144
+ #print("final results", results)
145
  demo.queue(max_size=16).launch(server_name="0.0.0.0",server_port=7860, share=False, show_error=True)
146
 
gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ test.jpg filter=lfs diff=lfs merge=lfs -text