| import os |
| import pickle |
| import cv2 |
| import pandas as pd |
| import numpy as np |
| from utils.utils import extract_features_from_image |
|
|
|
|
| def run_inference(TEST_IMAGE_PATH, pipeline_model, SUBMISSION_CSV_SAVE_PATH): |
| test_images = sorted(os.listdir(TEST_IMAGE_PATH)) |
| |
| image_feature_list = [] |
|
|
| for test_image in test_images: |
| path_to_image = os.path.join(TEST_IMAGE_PATH, test_image) |
| image = cv2.imread(path_to_image) |
| features = extract_features_from_image(image) |
| image_feature_list.append(features) |
|
|
| features_multiclass = np.array(image_feature_list) |
|
|
| multiclass_predictions = pipeline_model.predict(features_multiclass) |
|
|
| df_predictions = pd.DataFrame({ |
| "file_name": test_images, |
| "category_id": multiclass_predictions |
| }) |
|
|
| df_predictions.to_csv(SUBMISSION_CSV_SAVE_PATH, index=False) |
| |
| |
|
|
|
|
| if __name__ == "__main__": |
|
|
| current_directory = os.path.dirname(os.path.abspath(__file__)) |
| TEST_IMAGE_PATH = "/tmp/data/test_images" |
| |
| MODEL_NAME = "multiclass_model.pkl" |
| MODEL_PATH = os.path.join(current_directory, MODEL_NAME) |
| |
| k = 100 |
| SUBMISSION_CSV_SAVE_PATH = os.path.join(current_directory, "submission.csv") |
|
|
| |
| with open(MODEL_PATH, 'rb') as file: |
| svm_model = pickle.load(file) |
| |
| |
| run_inference(TEST_IMAGE_PATH, svm_model, k, SUBMISSION_CSV_SAVE_PATH) |