sharvari0b26 commited on
Commit
631f4c8
·
verified ·
1 Parent(s): e374e3c

Update script.py

Browse files
Files changed (1) hide show
  1. script.py +26 -32
script.py CHANGED
@@ -3,6 +3,7 @@ import torch
3
  import pandas as pd
4
  from rfdetr import RFDETRBase
5
 
 
6
  def run_inference(model, image_path, conf_threshold, save_path):
7
 
8
  test_images = sorted([
@@ -10,56 +11,51 @@ def run_inference(model, image_path, conf_threshold, save_path):
10
  if f.lower().endswith((".jpg", ".jpeg", ".png"))
11
  ])
12
 
13
- print(f"Found {len(test_images)} images for inference.")
14
-
15
  bboxes = []
16
  category_ids = []
17
  test_images_names = []
18
 
19
- for idx, image_name in enumerate(test_images):
20
- print(f"\nProcessing image {idx+1}/{len(test_images)}: {image_name}")
21
  test_images_names.append(image_name)
22
 
23
  image_file = os.path.join(image_path, image_name)
24
 
25
- # Run prediction
 
 
26
  preds = model.predict(image_file)
27
 
28
- # Debug print
29
- print(f"Raw prediction output: {preds}")
30
-
31
- # Handle empty predictions
32
- if preds is None or preds.xyxy is None or len(preds.xyxy) == 0:
33
- print("No predictions returned for this image.")
34
- image_bboxes = []
35
- image_categories = []
36
- else:
37
- image_bboxes = []
38
- image_categories = []
39
- for box, score, label in zip(preds.xyxy, preds.confidence, preds.class_id):
40
  if score >= conf_threshold:
41
- xmin, ymin, xmax, ymax = box
 
42
  width = xmax - xmin
43
  height = ymax - ymin
44
- image_bboxes.append([float(xmin), float(ymin), float(width), float(height)])
45
- image_categories.append(int(label))
46
 
47
- print(f"Detected {len(image_bboxes)} objects above threshold {conf_threshold}.")
 
48
 
49
- bboxes.append(image_bboxes)
50
- category_ids.append(image_categories)
51
 
52
- # Prepare DataFrame
53
  df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
 
54
  for i in range(len(test_images_names)):
55
- df_predictions.loc[i] = [
56
- test_images_names[i],
57
- str(bboxes[i]),
58
- str(category_ids[i]),
59
- ]
 
 
60
 
61
  df_predictions.to_csv(save_path, index=False)
62
- print(f"\nInference complete. Predictions saved to {save_path}")
63
 
64
 
65
  if __name__ == "__main__":
@@ -68,11 +64,9 @@ if __name__ == "__main__":
68
  SUBMISSION_SAVE_PATH = "submission.csv"
69
  CONF_THRESHOLD = 0.30
70
 
71
- print("Loading RF-DETR model...")
72
  model = RFDETRBase(
73
  checkpoint_path="checkpoint_best_ema.pth",
74
  device="cuda" if torch.cuda.is_available() else "cpu"
75
  )
76
 
77
- print("Starting inference...")
78
  run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)
 
3
  import pandas as pd
4
  from rfdetr import RFDETRBase
5
 
6
+
7
  def run_inference(model, image_path, conf_threshold, save_path):
8
 
9
  test_images = sorted([
 
11
  if f.lower().endswith((".jpg", ".jpeg", ".png"))
12
  ])
13
 
 
 
14
  bboxes = []
15
  category_ids = []
16
  test_images_names = []
17
 
18
+ for image_name in test_images:
 
19
  test_images_names.append(image_name)
20
 
21
  image_file = os.path.join(image_path, image_name)
22
 
23
+ bbox = []
24
+ category_id = []
25
+
26
  preds = model.predict(image_file)
27
 
28
+ if preds is not None and preds.xyxy is not None and len(preds.xyxy) > 0:
29
+ for box, score, label in zip(
30
+ preds.xyxy,
31
+ preds.confidence,
32
+ preds.class_id
33
+ ):
34
+ score = float(score)
 
 
 
 
 
35
  if score >= conf_threshold:
36
+ xmin, ymin, xmax, ymax = map(float, box)
37
+
38
  width = xmax - xmin
39
  height = ymax - ymin
 
 
40
 
41
+ bbox.append([xmin, ymin, width, height])
42
+ category_id.append(int(label))
43
 
44
+ bboxes.append(bbox)
45
+ category_ids.append(category_id)
46
 
 
47
  df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
48
+
49
  for i in range(len(test_images_names)):
50
+ new_row = pd.DataFrame({
51
+ "file_name": test_images_names[i],
52
+ "bbox": str(bboxes[i]),
53
+ "category_id": str(category_ids[i])
54
+ }, index=[0])
55
+
56
+ df_predictions = pd.concat([df_predictions, new_row], ignore_index=True)
57
 
58
  df_predictions.to_csv(save_path, index=False)
 
59
 
60
 
61
  if __name__ == "__main__":
 
64
  SUBMISSION_SAVE_PATH = "submission.csv"
65
  CONF_THRESHOLD = 0.30
66
 
 
67
  model = RFDETRBase(
68
  checkpoint_path="checkpoint_best_ema.pth",
69
  device="cuda" if torch.cuda.is_available() else "cpu"
70
  )
71
 
 
72
  run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)