upload files
Browse files- .gitattributes +35 -35
- README.md +3 -0
- multiclass_model.pkl +3 -0
- pca_params.pkl +3 -0
- script.py +82 -0
- train.py +156 -0
- utils/__pycache__/utils.cpython-312.pyc +0 -0
- utils/utils.py +280 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz 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
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz 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
|
README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
---
|
multiclass_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fc6e8978594153e78b387d9612558830c81d8572bdef4b6d497b25dac8909d4f
|
| 3 |
+
size 1038102
|
pca_params.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a2c54e5dff4251571ce734df567fcd5fdd6a0dbb881aa8883978cf9ea39cf8a2
|
| 3 |
+
size 76398726
|
script.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Inference script for Hugging Face competition submission
|
| 3 |
+
Fixed version combining baseline structure with enhanced features
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import pickle
|
| 8 |
+
import cv2
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import numpy as np
|
| 11 |
+
from utils.utils import extract_features_from_image, apply_pca_transform
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def run_inference(TEST_IMAGE_PATH, svm_model, pca_params, SUBMISSION_CSV_SAVE_PATH):
|
| 15 |
+
"""
|
| 16 |
+
Run inference on test images
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
TEST_IMAGE_PATH: Path to test images (/tmp/data/test_images)
|
| 20 |
+
svm_model: Trained SVM model
|
| 21 |
+
pca_params: Dictionary containing PCA transformation parameters
|
| 22 |
+
SUBMISSION_CSV_SAVE_PATH: Path to save submission.csv
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
# Load test images
|
| 26 |
+
test_images = os.listdir(TEST_IMAGE_PATH)
|
| 27 |
+
test_images.sort()
|
| 28 |
+
|
| 29 |
+
# Extract features from all test images
|
| 30 |
+
image_feature_list = []
|
| 31 |
+
|
| 32 |
+
for test_image in test_images:
|
| 33 |
+
path_to_image = os.path.join(TEST_IMAGE_PATH, test_image)
|
| 34 |
+
|
| 35 |
+
image = cv2.imread(path_to_image)
|
| 36 |
+
|
| 37 |
+
# Extract features (using enhanced features by default)
|
| 38 |
+
image_features = extract_features_from_image(image)
|
| 39 |
+
|
| 40 |
+
image_feature_list.append(image_features)
|
| 41 |
+
|
| 42 |
+
features_array = np.array(image_feature_list)
|
| 43 |
+
|
| 44 |
+
# Apply PCA transformation using saved parameters
|
| 45 |
+
features_reduced = apply_pca_transform(features_array, pca_params)
|
| 46 |
+
|
| 47 |
+
# Run predictions
|
| 48 |
+
predictions = svm_model.predict(features_reduced)
|
| 49 |
+
|
| 50 |
+
# Create submission CSV
|
| 51 |
+
df_predictions = pd.DataFrame({
|
| 52 |
+
"file_name": test_images,
|
| 53 |
+
"category_id": predictions
|
| 54 |
+
})
|
| 55 |
+
|
| 56 |
+
df_predictions.to_csv(SUBMISSION_CSV_SAVE_PATH, index=False)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
|
| 61 |
+
# Paths
|
| 62 |
+
current_directory = os.path.dirname(os.path.abspath(__file__))
|
| 63 |
+
TEST_IMAGE_PATH = "/tmp/data/test_images"
|
| 64 |
+
|
| 65 |
+
MODEL_NAME = "multiclass_model.pkl"
|
| 66 |
+
MODEL_PATH = os.path.join(current_directory, MODEL_NAME)
|
| 67 |
+
|
| 68 |
+
PCA_PARAMS_NAME = "pca_params.pkl"
|
| 69 |
+
PCA_PARAMS_PATH = os.path.join(current_directory, PCA_PARAMS_NAME)
|
| 70 |
+
|
| 71 |
+
SUBMISSION_CSV_SAVE_PATH = os.path.join(current_directory, "submission.csv")
|
| 72 |
+
|
| 73 |
+
# Load trained SVM model
|
| 74 |
+
with open(MODEL_PATH, 'rb') as file:
|
| 75 |
+
svm_model = pickle.load(file)
|
| 76 |
+
|
| 77 |
+
# Load PCA parameters
|
| 78 |
+
with open(PCA_PARAMS_PATH, 'rb') as file:
|
| 79 |
+
pca_params = pickle.load(file)
|
| 80 |
+
|
| 81 |
+
# Run inference
|
| 82 |
+
run_inference(TEST_IMAGE_PATH, svm_model, pca_params, SUBMISSION_CSV_SAVE_PATH)
|
train.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Training script for surgical instrument classification
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import pickle
|
| 7 |
+
import cv2
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import numpy as np
|
| 10 |
+
from utils.utils import extract_features_from_image, fit_pca_transformer, train_svm_model
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def train_and_save_model(base_path, images_folder, gt_csv, save_dir, n_components=100):
|
| 14 |
+
"""
|
| 15 |
+
Complete training pipeline that saves everything needed for submission
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
base_path: Base directory path
|
| 19 |
+
images_folder: Folder name containing images
|
| 20 |
+
gt_csv: Ground truth CSV filename
|
| 21 |
+
save_dir: Directory to save model artifacts
|
| 22 |
+
n_components: Number of PCA components
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
print("="*80)
|
| 26 |
+
print("TRAINING SURGICAL INSTRUMENT CLASSIFIER")
|
| 27 |
+
print("="*80)
|
| 28 |
+
|
| 29 |
+
# Setup paths
|
| 30 |
+
PATH_TO_GT = os.path.join(base_path, gt_csv)
|
| 31 |
+
PATH_TO_IMAGES = os.path.join(base_path, images_folder)
|
| 32 |
+
|
| 33 |
+
print(f"\nConfiguration:")
|
| 34 |
+
print(f" Ground Truth: {PATH_TO_GT}")
|
| 35 |
+
print(f" Images: {PATH_TO_IMAGES}")
|
| 36 |
+
print(f" PCA Components: {n_components}")
|
| 37 |
+
print(f" Save directory: {save_dir}")
|
| 38 |
+
|
| 39 |
+
# Load ground truth
|
| 40 |
+
df = pd.read_csv(PATH_TO_GT)
|
| 41 |
+
print(f"\nLoaded {len(df)} training samples")
|
| 42 |
+
print(f"\nLabel distribution:")
|
| 43 |
+
print(df['category_id'].value_counts().sort_index())
|
| 44 |
+
|
| 45 |
+
# Extract features
|
| 46 |
+
print(f"\n{'='*80}")
|
| 47 |
+
print("STEP 1: FEATURE EXTRACTION")
|
| 48 |
+
print("="*80)
|
| 49 |
+
|
| 50 |
+
features = []
|
| 51 |
+
labels = []
|
| 52 |
+
|
| 53 |
+
for i in range(len(df)):
|
| 54 |
+
if i % 500 == 0:
|
| 55 |
+
print(f" Processing {i}/{len(df)}...")
|
| 56 |
+
|
| 57 |
+
image_name = df.iloc[i]["file_name"]
|
| 58 |
+
label = df.iloc[i]["category_id"]
|
| 59 |
+
|
| 60 |
+
path_to_image = os.path.join(PATH_TO_IMAGES, image_name)
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
image = cv2.imread(path_to_image)
|
| 64 |
+
if image is None:
|
| 65 |
+
print(f" Warning: Could not read {image_name}, skipping...")
|
| 66 |
+
continue
|
| 67 |
+
|
| 68 |
+
# Extract features with enhanced configuration
|
| 69 |
+
image_features = extract_features_from_image(image)
|
| 70 |
+
|
| 71 |
+
features.append(image_features)
|
| 72 |
+
labels.append(label)
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f" Error processing {image_name}: {e}")
|
| 76 |
+
continue
|
| 77 |
+
|
| 78 |
+
features_array = np.array(features)
|
| 79 |
+
labels_array = np.array(labels)
|
| 80 |
+
|
| 81 |
+
print(f"\nFeature extraction complete!")
|
| 82 |
+
print(f" Features shape: {features_array.shape}")
|
| 83 |
+
print(f" Labels shape: {labels_array.shape}")
|
| 84 |
+
print(f" Feature dimension: {features_array.shape[1]}")
|
| 85 |
+
|
| 86 |
+
# Apply PCA
|
| 87 |
+
print(f"\n{'='*80}")
|
| 88 |
+
print("STEP 2: DIMENSIONALITY REDUCTION (PCA)")
|
| 89 |
+
print("="*80)
|
| 90 |
+
|
| 91 |
+
pca_params, features_reduced = fit_pca_transformer(features_array, n_components)
|
| 92 |
+
|
| 93 |
+
print(f" Reduced from {features_array.shape[1]} to {n_components} dimensions")
|
| 94 |
+
print(f" Explained variance: {pca_params['cumulative_variance'][-1]:.4f}")
|
| 95 |
+
|
| 96 |
+
# Train SVM
|
| 97 |
+
print(f"\n{'='*80}")
|
| 98 |
+
print("STEP 3: TRAINING SVM CLASSIFIER")
|
| 99 |
+
print("="*80)
|
| 100 |
+
|
| 101 |
+
train_results = train_svm_model(features_reduced, labels_array)
|
| 102 |
+
|
| 103 |
+
svm_model = train_results['model']
|
| 104 |
+
|
| 105 |
+
print(f"\nTraining complete!")
|
| 106 |
+
print(f" Support vectors: {len(svm_model.support_)}")
|
| 107 |
+
|
| 108 |
+
# Save model artifacts
|
| 109 |
+
print(f"\n{'='*80}")
|
| 110 |
+
print("STEP 4: SAVING MODEL ARTIFACTS")
|
| 111 |
+
print("="*80)
|
| 112 |
+
|
| 113 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 114 |
+
|
| 115 |
+
# Save SVM model
|
| 116 |
+
model_path = os.path.join(save_dir, "multiclass_model.pkl")
|
| 117 |
+
with open(model_path, "wb") as f:
|
| 118 |
+
pickle.dump(svm_model, f)
|
| 119 |
+
print(f" ✓ Saved SVM model: {model_path}")
|
| 120 |
+
|
| 121 |
+
# Save PCA parameters
|
| 122 |
+
pca_path = os.path.join(save_dir, "pca_params.pkl")
|
| 123 |
+
with open(pca_path, "wb") as f:
|
| 124 |
+
pickle.dump(pca_params, f)
|
| 125 |
+
print(f" ✓ Saved PCA params: {pca_path}")
|
| 126 |
+
|
| 127 |
+
print(f"\n{'='*80}")
|
| 128 |
+
print("TRAINING COMPLETE!")
|
| 129 |
+
print("="*80)
|
| 130 |
+
print(f"\nFinal Results:")
|
| 131 |
+
print(f" Train Accuracy: {train_results['train_accuracy']:.4f}")
|
| 132 |
+
print(f" Test Accuracy: {train_results['test_accuracy']:.4f}")
|
| 133 |
+
print(f" Test F1-score: {train_results['test_f1']:.4f}")
|
| 134 |
+
print(f"\nFiles saved to: {save_dir}")
|
| 135 |
+
print(f"\nNext steps:")
|
| 136 |
+
print(f" 1. Create a 'utils' folder in your HuggingFace repository")
|
| 137 |
+
print(f" 2. Copy utils.py into the 'utils' folder")
|
| 138 |
+
print(f" 3. Copy script.py, multiclass_model.pkl, and pca_params.pkl to the repository root")
|
| 139 |
+
print(f" 4. Create an empty __init__.py file in the 'utils' folder")
|
| 140 |
+
print(f" 5. Submit to competition!")
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
|
| 145 |
+
# CONFIGURATION - Adjust these paths to your setup
|
| 146 |
+
BASE_PATH = "C:/Users/anna2/ISM/ANNA/phase1a2"
|
| 147 |
+
IMAGES_FOLDER = "C:/Users/anna2/ISM/Images"
|
| 148 |
+
GT_CSV = "C:/Users/anna2/ISM/Baselines/phase_1a/gt_for_classification_multiclass_from_filenames_0_index.csv"
|
| 149 |
+
|
| 150 |
+
SAVE_DIR = "C:/Users/anna2/ISM/ANNA/phase1a2/submission"
|
| 151 |
+
|
| 152 |
+
# Number of PCA components
|
| 153 |
+
N_COMPONENTS = 200
|
| 154 |
+
|
| 155 |
+
# Train and save
|
| 156 |
+
train_and_save_model(BASE_PATH, IMAGES_FOLDER, GT_CSV, SAVE_DIR, N_COMPONENTS)
|
utils/__pycache__/utils.cpython-312.pyc
ADDED
|
Binary file (11.1 kB). View file
|
|
|
utils/utils.py
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Utility functions for surgical instrument classification
|
| 3 |
+
FIXED VERSION - Bug fixes for HuggingFace submission
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
from skimage.feature.texture import graycomatrix, graycoprops
|
| 9 |
+
from skimage.feature import local_binary_pattern, hog
|
| 10 |
+
from sklearn.decomposition import PCA
|
| 11 |
+
from sklearn.svm import SVC
|
| 12 |
+
from sklearn.model_selection import train_test_split
|
| 13 |
+
from sklearn.metrics import accuracy_score, f1_score
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def preprocess_image(image):
|
| 17 |
+
"""
|
| 18 |
+
Apply CLAHE preprocessing for better contrast
|
| 19 |
+
MUST be defined BEFORE extract_features_from_image
|
| 20 |
+
"""
|
| 21 |
+
# Convert to LAB color space
|
| 22 |
+
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
|
| 23 |
+
l, a, b = cv2.split(lab)
|
| 24 |
+
|
| 25 |
+
# Apply CLAHE to L channel
|
| 26 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
| 27 |
+
l = clahe.apply(l)
|
| 28 |
+
|
| 29 |
+
# Merge and convert back
|
| 30 |
+
enhanced = cv2.merge([l, a, b])
|
| 31 |
+
enhanced = cv2.cvtColor(enhanced, cv2.COLOR_LAB2BGR)
|
| 32 |
+
|
| 33 |
+
return enhanced
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def rgb_histogram(image, bins=256):
|
| 37 |
+
"""Extract RGB histogram features"""
|
| 38 |
+
hist_features = []
|
| 39 |
+
for i in range(3): # RGB Channels
|
| 40 |
+
hist, _ = np.histogram(image[:, :, i], bins=bins, range=(0, 256), density=True)
|
| 41 |
+
hist_features.append(hist)
|
| 42 |
+
return np.concatenate(hist_features)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def hu_moments(image):
|
| 46 |
+
"""Extract Hu moment features"""
|
| 47 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Fixed: BGR not RGB
|
| 48 |
+
moments = cv2.moments(gray)
|
| 49 |
+
hu_moments = cv2.HuMoments(moments).flatten()
|
| 50 |
+
return hu_moments
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def glcm_features(image, distances=[1], angles=[0], levels=256, symmetric=True, normed=True):
|
| 54 |
+
"""Extract GLCM texture features"""
|
| 55 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Already correct
|
| 56 |
+
glcm = graycomatrix(gray, distances=distances, angles=angles, levels=levels,
|
| 57 |
+
symmetric=symmetric, normed=normed)
|
| 58 |
+
contrast = graycoprops(glcm, 'contrast').flatten()
|
| 59 |
+
dissimilarity = graycoprops(glcm, 'dissimilarity').flatten()
|
| 60 |
+
homogeneity = graycoprops(glcm, 'homogeneity').flatten()
|
| 61 |
+
energy = graycoprops(glcm, 'energy').flatten()
|
| 62 |
+
correlation = graycoprops(glcm, 'correlation').flatten()
|
| 63 |
+
asm = graycoprops(glcm, 'ASM').flatten()
|
| 64 |
+
return np.concatenate([contrast, dissimilarity, homogeneity, energy, correlation, asm])
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def local_binary_pattern_features(image, P=8, R=1):
|
| 68 |
+
"""Extract Local Binary Pattern features"""
|
| 69 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Already correct
|
| 70 |
+
lbp = local_binary_pattern(gray, P, R, method='uniform')
|
| 71 |
+
(hist, _) = np.histogram(lbp.ravel(), bins=np.arange(0, P + 3),
|
| 72 |
+
range=(0, P + 2), density=True)
|
| 73 |
+
return hist
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def hog_features(image, orientations=12, pixels_per_cell=(8, 8), cells_per_block=(2, 2)):
|
| 77 |
+
"""
|
| 78 |
+
Extract HOG (Histogram of Oriented Gradients) features
|
| 79 |
+
Great for capturing shape and edge information in surgical instruments
|
| 80 |
+
"""
|
| 81 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Fixed: BGR not RGB
|
| 82 |
+
|
| 83 |
+
# Resize to standard size for consistency
|
| 84 |
+
gray_resized = cv2.resize(gray, (256, 256))
|
| 85 |
+
|
| 86 |
+
hog_features_vector = hog(
|
| 87 |
+
gray_resized,
|
| 88 |
+
orientations=orientations,
|
| 89 |
+
pixels_per_cell=pixels_per_cell,
|
| 90 |
+
cells_per_block=cells_per_block,
|
| 91 |
+
block_norm='L2-Hys',
|
| 92 |
+
feature_vector=True
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return hog_features_vector
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def luv_histogram(image, bins=32):
|
| 99 |
+
"""
|
| 100 |
+
Extract histogram in LUV color space
|
| 101 |
+
LUV is perceptually uniform and better for underwater/surgical imaging
|
| 102 |
+
"""
|
| 103 |
+
luv = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
|
| 104 |
+
hist_features = []
|
| 105 |
+
for i in range(3):
|
| 106 |
+
hist, _ = np.histogram(luv[:, :, i], bins=bins, range=(0, 256), density=True)
|
| 107 |
+
hist_features.append(hist)
|
| 108 |
+
return np.concatenate(hist_features)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def gabor_features(image, frequencies=[0.1, 0.2, 0.3],
|
| 112 |
+
orientations=[0, 45, 90, 135]):
|
| 113 |
+
"""
|
| 114 |
+
Extract Gabor filter features
|
| 115 |
+
MUST be defined BEFORE extract_features_from_image
|
| 116 |
+
"""
|
| 117 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Fixed: BGR not RGB
|
| 118 |
+
features = []
|
| 119 |
+
|
| 120 |
+
for freq in frequencies:
|
| 121 |
+
for theta in orientations:
|
| 122 |
+
theta_rad = theta * np.pi / 180
|
| 123 |
+
kernel = cv2.getGaborKernel((21, 21), 5, theta_rad,
|
| 124 |
+
10.0/freq, 0.5, 0)
|
| 125 |
+
filtered = cv2.filter2D(gray, cv2.CV_32F, kernel)
|
| 126 |
+
features.append(np.mean(filtered))
|
| 127 |
+
features.append(np.std(filtered))
|
| 128 |
+
|
| 129 |
+
return np.array(features)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def extract_features_from_image(image):
|
| 133 |
+
"""
|
| 134 |
+
Extract enhanced features from image
|
| 135 |
+
Uses baseline features + HOG + LUV histogram + Gabor for better performance
|
| 136 |
+
|
| 137 |
+
Args:
|
| 138 |
+
image: Input image (BGR format from cv2.imread)
|
| 139 |
+
|
| 140 |
+
Returns:
|
| 141 |
+
Feature vector as numpy array
|
| 142 |
+
"""
|
| 143 |
+
# Preprocess image first
|
| 144 |
+
image = preprocess_image(image)
|
| 145 |
+
|
| 146 |
+
# Baseline features
|
| 147 |
+
hist_features = rgb_histogram(image)
|
| 148 |
+
hu_features = hu_moments(image)
|
| 149 |
+
glcm_features_vector = glcm_features(image)
|
| 150 |
+
lbp_features = local_binary_pattern_features(image)
|
| 151 |
+
|
| 152 |
+
# Enhanced features
|
| 153 |
+
hog_feat = hog_features(image)
|
| 154 |
+
luv_hist = luv_histogram(image)
|
| 155 |
+
gabor_feat = gabor_features(image)
|
| 156 |
+
|
| 157 |
+
# Concatenate all features
|
| 158 |
+
image_features = np.concatenate([
|
| 159 |
+
hist_features,
|
| 160 |
+
hu_features,
|
| 161 |
+
glcm_features_vector,
|
| 162 |
+
lbp_features,
|
| 163 |
+
hog_feat,
|
| 164 |
+
luv_hist,
|
| 165 |
+
gabor_feat
|
| 166 |
+
])
|
| 167 |
+
|
| 168 |
+
return image_features
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def fit_pca_transformer(data, num_components):
|
| 172 |
+
"""
|
| 173 |
+
Fit a PCA transformer on training data
|
| 174 |
+
|
| 175 |
+
Args:
|
| 176 |
+
data: Training data (n_samples, n_features)
|
| 177 |
+
num_components: Number of PCA components to keep
|
| 178 |
+
|
| 179 |
+
Returns:
|
| 180 |
+
pca_params: Dictionary containing PCA parameters
|
| 181 |
+
data_reduced: PCA-transformed data
|
| 182 |
+
"""
|
| 183 |
+
|
| 184 |
+
# Standardize the data
|
| 185 |
+
mean = np.mean(data, axis=0)
|
| 186 |
+
std = np.std(data, axis=0)
|
| 187 |
+
|
| 188 |
+
# Avoid division by zero
|
| 189 |
+
std[std == 0] = 1.0
|
| 190 |
+
|
| 191 |
+
data_standardized = (data - mean) / std
|
| 192 |
+
|
| 193 |
+
# Fit PCA using sklearn
|
| 194 |
+
pca_model = PCA(n_components=num_components)
|
| 195 |
+
data_reduced = pca_model.fit_transform(data_standardized)
|
| 196 |
+
|
| 197 |
+
# Create params dictionary
|
| 198 |
+
pca_params = {
|
| 199 |
+
'pca_model': pca_model,
|
| 200 |
+
'mean': mean,
|
| 201 |
+
'std': std,
|
| 202 |
+
'num_components': num_components,
|
| 203 |
+
'feature_dim': data.shape[1],
|
| 204 |
+
'explained_variance_ratio': pca_model.explained_variance_ratio_,
|
| 205 |
+
'cumulative_variance': np.cumsum(pca_model.explained_variance_ratio_)
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
return pca_params, data_reduced
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def apply_pca_transform(data, pca_params):
|
| 212 |
+
"""
|
| 213 |
+
Apply saved PCA transformation to new data
|
| 214 |
+
CRITICAL: This uses the saved mean/std/PCA from training
|
| 215 |
+
|
| 216 |
+
Args:
|
| 217 |
+
data: New data to transform (n_samples, n_features)
|
| 218 |
+
pca_params: Dictionary from fit_pca_transformer
|
| 219 |
+
|
| 220 |
+
Returns:
|
| 221 |
+
Transformed data
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
# Standardize using training mean/std
|
| 225 |
+
data_standardized = (data - pca_params['mean']) / pca_params['std']
|
| 226 |
+
|
| 227 |
+
# Apply PCA transformation
|
| 228 |
+
data_reduced = pca_params['pca_model'].transform(data_standardized)
|
| 229 |
+
|
| 230 |
+
return data_reduced
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def train_svm_model(features, labels, test_size=0.2, kernel='rbf', C=1.0):
|
| 234 |
+
"""
|
| 235 |
+
Train an SVM model and return both the model and performance metrics
|
| 236 |
+
|
| 237 |
+
Args:
|
| 238 |
+
features: Feature matrix (n_samples, n_features)
|
| 239 |
+
labels: Label array (n_samples,)
|
| 240 |
+
test_size: Proportion for test split
|
| 241 |
+
kernel: SVM kernel type
|
| 242 |
+
C: SVM regularization parameter
|
| 243 |
+
|
| 244 |
+
Returns:
|
| 245 |
+
Dictionary containing model and metrics
|
| 246 |
+
"""
|
| 247 |
+
|
| 248 |
+
# Check if labels are one-hot encoded
|
| 249 |
+
if labels.ndim > 1 and labels.shape[1] > 1:
|
| 250 |
+
labels = np.argmax(labels, axis=1)
|
| 251 |
+
|
| 252 |
+
# Split data
|
| 253 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 254 |
+
features, labels, test_size=test_size, random_state=42, stratify=labels
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
# Train SVM
|
| 258 |
+
svm_model = SVC(kernel=kernel, C=C, random_state=42)
|
| 259 |
+
svm_model.fit(X_train, y_train)
|
| 260 |
+
|
| 261 |
+
# Evaluate
|
| 262 |
+
y_train_pred = svm_model.predict(X_train)
|
| 263 |
+
y_test_pred = svm_model.predict(X_test)
|
| 264 |
+
|
| 265 |
+
train_accuracy = accuracy_score(y_train, y_train_pred)
|
| 266 |
+
test_accuracy = accuracy_score(y_test, y_test_pred)
|
| 267 |
+
test_f1 = f1_score(y_test, y_test_pred, average='macro')
|
| 268 |
+
|
| 269 |
+
print(f'Train Accuracy: {train_accuracy:.4f}')
|
| 270 |
+
print(f'Test Accuracy: {test_accuracy:.4f}')
|
| 271 |
+
print(f'Test F1-score: {test_f1:.4f}')
|
| 272 |
+
|
| 273 |
+
results = {
|
| 274 |
+
'model': svm_model,
|
| 275 |
+
'train_accuracy': train_accuracy,
|
| 276 |
+
'test_accuracy': test_accuracy,
|
| 277 |
+
'test_f1': test_f1
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
return results
|