Upload script.py
Browse files
script.py
CHANGED
|
@@ -8,8 +8,6 @@ import pandas as pd
|
|
| 8 |
import ultralytics
|
| 9 |
from torchvision import transforms
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
def run_inference(model, image_path, conf_threshold, save_path):
|
| 14 |
|
| 15 |
test_images = os.listdir(image_path)
|
|
@@ -19,52 +17,94 @@ def run_inference(model, image_path, conf_threshold, save_path):
|
|
| 19 |
category_ids = []
|
| 20 |
test_images_names = []
|
| 21 |
|
|
|
|
|
|
|
| 22 |
for image in test_images:
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
test_images_names.append(image)
|
| 25 |
bbox = []
|
| 26 |
category_id = []
|
| 27 |
|
|
|
|
| 28 |
results = model(os.path.join(image_path, image))
|
| 29 |
|
|
|
|
| 30 |
for pred in results.pred[0]:
|
| 31 |
xmin, ymin, xmax, ymax, conf, class_id = pred.tolist()
|
| 32 |
-
if conf >= conf_threshold:
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
width = xmax - xmin
|
| 35 |
height = ymax - ymin
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
bboxes.append(bbox)
|
| 41 |
-
category_ids.append(category_id)
|
| 42 |
|
|
|
|
| 43 |
df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
|
| 44 |
|
| 45 |
for i in range(len(test_images_names)):
|
| 46 |
file_name = test_images_names[i]
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
df_predictions = pd.concat([df_predictions, new_row], ignore_index=True)
|
| 52 |
|
| 53 |
df_predictions.to_csv(save_path, index=False)
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
|
| 58 |
-
|
| 59 |
current_directory = os.path.dirname(os.path.abspath(__file__))
|
| 60 |
-
|
|
|
|
| 61 |
TEST_IMAGE_PATH = "/tmp/data/test_images"
|
| 62 |
SUBMISSION_SAVE_PATH = os.path.join(current_directory, "submission.csv")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
CONF_THRESHOLD = 0.30
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)
|
|
|
|
| 8 |
import ultralytics
|
| 9 |
from torchvision import transforms
|
| 10 |
|
|
|
|
|
|
|
| 11 |
def run_inference(model, image_path, conf_threshold, save_path):
|
| 12 |
|
| 13 |
test_images = os.listdir(image_path)
|
|
|
|
| 17 |
category_ids = []
|
| 18 |
test_images_names = []
|
| 19 |
|
| 20 |
+
print(f"Starting inference on {len(test_images)} images...")
|
| 21 |
+
|
| 22 |
for image in test_images:
|
| 23 |
|
| 24 |
+
# Skip non-image files if any
|
| 25 |
+
if not (image.endswith('.jpg') or image.endswith('.png')):
|
| 26 |
+
continue
|
| 27 |
+
|
| 28 |
test_images_names.append(image)
|
| 29 |
bbox = []
|
| 30 |
category_id = []
|
| 31 |
|
| 32 |
+
# Run inference
|
| 33 |
results = model(os.path.join(image_path, image))
|
| 34 |
|
| 35 |
+
# Parse detections
|
| 36 |
for pred in results.pred[0]:
|
| 37 |
xmin, ymin, xmax, ymax, conf, class_id = pred.tolist()
|
|
|
|
| 38 |
|
| 39 |
+
if conf >= conf_threshold:
|
| 40 |
+
|
| 41 |
+
# --- CALCULATE WIDTH & HEIGHT ---
|
| 42 |
width = xmax - xmin
|
| 43 |
height = ymax - ymin
|
| 44 |
|
| 45 |
+
# --- FORMATTING FIXES FOR LEADERBOARD ---
|
| 46 |
+
# Round coordinates to avoid messy floats (e.g. 100.0 instead of 100.43242)
|
| 47 |
+
final_box = [round(xmin, 1), round(ymin, 1), round(width, 1), round(height, 1)]
|
| 48 |
+
|
| 49 |
+
# --- CRITICAL FIX: SHIFT CLASS ID ---
|
| 50 |
+
# YOLO predicts 0, 1, 2.
|
| 51 |
+
# Your JSON categories are 1 (Needle), 2 (Grasper), 3 (Scissors).
|
| 52 |
+
# We simply add +1 to align them.
|
| 53 |
+
final_id = int(class_id) + 1
|
| 54 |
+
|
| 55 |
+
bbox.append(final_box)
|
| 56 |
+
category_id.append(final_id)
|
| 57 |
|
| 58 |
bboxes.append(bbox)
|
| 59 |
+
category_ids.append(category_id)
|
| 60 |
|
| 61 |
+
# Create Submission DataFrame
|
| 62 |
df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
|
| 63 |
|
| 64 |
for i in range(len(test_images_names)):
|
| 65 |
file_name = test_images_names[i]
|
| 66 |
+
|
| 67 |
+
# Format the list exactly how standard submission files look
|
| 68 |
+
# e.g. "[[10, 10, 50, 50]]" instead of just list objects
|
| 69 |
+
str_bbox = str(bboxes[i])
|
| 70 |
+
str_cat_id = str(category_ids[i])
|
| 71 |
+
|
| 72 |
+
new_row = pd.DataFrame({
|
| 73 |
+
"file_name": file_name,
|
| 74 |
+
"bbox": str_bbox,
|
| 75 |
+
"category_id": str_cat_id
|
| 76 |
+
}, index=[0])
|
| 77 |
+
|
| 78 |
df_predictions = pd.concat([df_predictions, new_row], ignore_index=True)
|
| 79 |
|
| 80 |
df_predictions.to_csv(save_path, index=False)
|
| 81 |
+
print(f"Submission saved to: {save_path}")
|
| 82 |
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
|
|
|
|
| 86 |
current_directory = os.path.dirname(os.path.abspath(__file__))
|
| 87 |
+
|
| 88 |
+
# Default paths (These usually don't change on the server)
|
| 89 |
TEST_IMAGE_PATH = "/tmp/data/test_images"
|
| 90 |
SUBMISSION_SAVE_PATH = os.path.join(current_directory, "submission.csv")
|
| 91 |
|
| 92 |
+
# --- MODEL LOADING CONFIG ---
|
| 93 |
+
# We look for 'best.pt' in the SAME directory as this script
|
| 94 |
+
MODEL_WEIGHTS_PATH = os.path.join(current_directory, "best.pt")
|
| 95 |
+
|
| 96 |
+
# Increase threshold slightly to avoid garbage predictions
|
| 97 |
CONF_THRESHOLD = 0.30
|
| 98 |
|
| 99 |
+
print(f"Loading model from: {MODEL_WEIGHTS_PATH}")
|
| 100 |
+
|
| 101 |
+
# Load YOLOv5 from the local folder to ensure it works offline
|
| 102 |
+
# 'source=local' means it looks for the 'yolov5' folder in this directory
|
| 103 |
+
model = torch.hub.load(
|
| 104 |
+
os.path.join(current_directory, 'yolov5'),
|
| 105 |
+
'custom',
|
| 106 |
+
path=MODEL_WEIGHTS_PATH,
|
| 107 |
+
source='local'
|
| 108 |
+
)
|
| 109 |
|
| 110 |
run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)
|