-
Notifications
You must be signed in to change notification settings - Fork 1
Description
KNN Classifier for Place Recognition
This document describes the implementation and usage of a k-nearest neighbors (KNN) classifier tailored for the recognition of places, utilizing a pre-trained ResNet model for feature extraction and scikit-learn's KNeighborsClassifier for classification.
Introduction
The KNN algorithm is leveraged here for its simplicity and effectiveness in classifying images into a large set of categories based on similarity to known examples. It is particularly well-suited for applications where the identification of an entity (e.g., a place or a face) from a large dataset in a relatively short computation time is critical.
Prerequisites
scikit-learn: For the KNN classification algorithm.
torchvision and PIL: For image processing and feature extraction.
numpy, tqdm, cv2 (opencv-python), joblib: For data manipulation, progress tracking, image operations, and model serialization, respectively.
Algorithm Overview
The KNN classifier is trained on a dataset of labeled images, where each label corresponds to a unique entity (place). For an unknown image, the algorithm predicts its label by identifying the k-nearest labeled images in the feature space, using Euclidean distance as a measure of similarity. The prediction is made based on a majority vote among these neighbors, with an option to weight votes according to distance to give closer neighbors greater influence.
Usage Guide
Data Preparation: Organize your image dataset into a structured directory, with one sub-directory for each category (place) containing its images.
Training:
Utilize the train function to train the classifier on your dataset. Parameters include the path to the dataset, the number of neighbors (k), and optionally, a path to save the trained model for later use.
Prediction:
With the trained model, call the predict method to classify an unknown image. The method returns the predicted category based on the trained KNN model.
Implementation Details
Feature Extraction: Implements a feature extraction pipeline using a ResNet model, converting images into feature vectors that encapsulate their visual content.
Model Training and Prediction: Leverages scikit-learn's KNeighborsClassifier for training the classifier with extracted features and making predictions.
Model Evaluation: Includes functionality for assessing the model's performance using metrics like accuracy, precision, and F1 score.
Progress Monitoring: Features a progress tracking system that logs the status of training and prediction operations, useful for long-running tasks.