Update script.py
Browse files
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 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
def get_category_id(cls_id):
|
| 16 |
-
#
|
| 17 |
-
#
|
| 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 |
-
# 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.
|
| 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
|
| 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
|
| 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),
|
| 78 |
-
"category_id": str(category_list)
|
| 79 |
})
|
| 80 |
|
| 81 |
-
#
|
| 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 |
-
|
| 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"β
|
|
|
|
| 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}")
|