yusufbardolia commited on
Commit
87f1d97
Β·
verified Β·
1 Parent(s): aeab8ee

Update script.py

Browse files
Files changed (1) hide show
  1. script.py +14 -40
script.py CHANGED
@@ -4,51 +4,41 @@ from ultralytics import YOLO
4
  import glob
5
 
6
  # --- CONFIGURATION ---
7
- # The evaluation server usually puts test images here
8
  TEST_IMAGE_PATH = "/tmp/data/test_images"
9
  SUBMISSION_SAVE_PATH = "submission.csv"
10
  MODEL_WEIGHTS = "best.pt"
11
 
12
- # You can tune this. 0.25 - 0.30 is usually a safe balance for mAP.
13
- CONF_THRESHOLD = 0.001
 
14
 
15
  def get_category_id(cls_id):
16
- # ❌ OLD: return int(cls_id)
17
- # βœ… NEW: Add 1 to match competition format (1, 2, 3)
18
- return int(cls_id) + 1
19
 
20
  def run_inference(model, image_path, conf_threshold, save_path):
21
  print(f"πŸš€ Checking for images in {image_path}...")
22
 
23
- # 1. Get all images (support multiple extensions)
24
  test_images = []
25
  if os.path.exists(image_path):
26
- # Recursive search just in case, though usually they are flat in the folder
27
  extensions = ['*.png', '*.jpg', '*.jpeg', '*.bmp']
28
  for ext in extensions:
29
  test_images.extend(glob.glob(os.path.join(image_path, ext)))
30
-
31
- # Sort to ensure consistent order if needed
32
  test_images.sort()
33
- # Just keep filenames for the loop to match your specific structure requirements
34
  test_images = [os.path.basename(x) for x in test_images]
35
  else:
36
- print(f"⚠️ Warning: {image_path} not found. Testing with empty list (or verify path).")
37
- # If running locally for debug, you might want to point to a local folder here
38
- # test_images = os.listdir(".")
39
 
40
  print(f"πŸ” Found {len(test_images)} images.")
41
 
42
- # Prepare lists for the dataframe
43
  df_rows = []
44
 
45
- # 2. Run Inference
46
  if len(test_images) > 0:
47
  for image_name in test_images:
48
  full_path = os.path.join(image_path, image_name)
49
 
50
- # Run YOLOv8 inference
51
- # verbose=False keeps the logs clean
52
  results = model.predict(full_path, conf=conf_threshold, verbose=False)
53
 
54
  bbox_list = []
@@ -56,50 +46,34 @@ def run_inference(model, image_path, conf_threshold, save_path):
56
 
57
  for result in results:
58
  for box in result.boxes:
59
- # YOLO format is Center-X, Center-Y, Width, Height
60
  x_c, y_c, w, h = box.xywh[0].tolist()
61
-
62
- # Convert to COCO format: Top-Left-X, Top-Left-Y, Width, Height
63
  x_min = x_c - (w / 2)
64
  y_min = y_c - (h / 2)
65
 
66
- # Store as list [x, y, w, h]
67
  bbox_list.append([x_min, y_min, w, h])
68
 
69
- # Get predicted class ID (0, 1, or 2)
70
  cls_id = int(box.cls[0])
71
  category_list.append(get_category_id(cls_id))
72
 
73
- # 3. Format Prediction String
74
- # The baseline requires the bbox and category_id columns to be Strings of Lists
75
  df_rows.append({
76
  "file_name": image_name,
77
- "bbox": str(bbox_list), # e.g. "[[10.5, 20.1, 50.0, 50.0]]"
78
- "category_id": str(category_list) # e.g. "[0]" or "[0, 2]"
79
  })
80
 
81
- # 4. Create DataFrame and Save
82
  df_predictions = pd.DataFrame(df_rows, columns=["file_name", "bbox", "category_id"])
83
-
84
- # Safety check: if no images found, create an empty CSV with headers to prevent crash
85
  if df_predictions.empty:
86
- print("⚠️ No predictions made (no images found?). Creating empty CSV.")
87
  df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
88
 
89
- print(f"πŸ’Ύ Saving {len(df_predictions)} rows to {save_path}...")
90
  df_predictions.to_csv(save_path, index=False)
91
  print("βœ… Done!")
92
 
93
  if __name__ == "__main__":
94
- # Ensure ultralytics is installed via requirements.txt before this runs
95
- print(f"πŸ”₯ Loading YOLOv8 model: {MODEL_WEIGHTS}...")
96
-
97
  try:
98
- # Load the trained model
99
  model = YOLO(MODEL_WEIGHTS)
100
-
101
- # Execute inference
102
  run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)
103
-
104
  except Exception as e:
105
- print(f"❌ Critical Error in main execution: {e}")
 
4
  import glob
5
 
6
  # --- CONFIGURATION ---
 
7
  TEST_IMAGE_PATH = "/tmp/data/test_images"
8
  SUBMISSION_SAVE_PATH = "submission.csv"
9
  MODEL_WEIGHTS = "best.pt"
10
 
11
+ # βœ… FIX 1: Set a balanced threshold.
12
+ # 0.20 removes the noise but keeps the valid weak detections.
13
+ CONF_THRESHOLD = 0.20
14
 
15
  def get_category_id(cls_id):
16
+ # βœ… FIX 2: Revert to original IDs.
17
+ # The drop in score proved that adding +1 was wrong.
18
+ return int(cls_id)
19
 
20
  def run_inference(model, image_path, conf_threshold, save_path):
21
  print(f"πŸš€ Checking for images in {image_path}...")
22
 
 
23
  test_images = []
24
  if os.path.exists(image_path):
 
25
  extensions = ['*.png', '*.jpg', '*.jpeg', '*.bmp']
26
  for ext in extensions:
27
  test_images.extend(glob.glob(os.path.join(image_path, ext)))
 
 
28
  test_images.sort()
 
29
  test_images = [os.path.basename(x) for x in test_images]
30
  else:
31
+ print(f"⚠️ Warning: {image_path} not found.")
 
 
32
 
33
  print(f"πŸ” Found {len(test_images)} images.")
34
 
 
35
  df_rows = []
36
 
 
37
  if len(test_images) > 0:
38
  for image_name in test_images:
39
  full_path = os.path.join(image_path, image_name)
40
 
41
+ # Run inference
 
42
  results = model.predict(full_path, conf=conf_threshold, verbose=False)
43
 
44
  bbox_list = []
 
46
 
47
  for result in results:
48
  for box in result.boxes:
49
+ # YOLO (Center) -> COCO (Top-Left)
50
  x_c, y_c, w, h = box.xywh[0].tolist()
 
 
51
  x_min = x_c - (w / 2)
52
  y_min = y_c - (h / 2)
53
 
 
54
  bbox_list.append([x_min, y_min, w, h])
55
 
 
56
  cls_id = int(box.cls[0])
57
  category_list.append(get_category_id(cls_id))
58
 
 
 
59
  df_rows.append({
60
  "file_name": image_name,
61
+ "bbox": str(bbox_list),
62
+ "category_id": str(category_list)
63
  })
64
 
65
+ # Save
66
  df_predictions = pd.DataFrame(df_rows, columns=["file_name", "bbox", "category_id"])
 
 
67
  if df_predictions.empty:
 
68
  df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
69
 
 
70
  df_predictions.to_csv(save_path, index=False)
71
  print("βœ… Done!")
72
 
73
  if __name__ == "__main__":
 
 
 
74
  try:
75
+ print(f"πŸ”₯ Loading Model...")
76
  model = YOLO(MODEL_WEIGHTS)
 
 
77
  run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)
 
78
  except Exception as e:
79
+ print(f"❌ Error: {e}")