Spaces:
Sleeping
Sleeping
Rohan Kumar Shah commited on
Commit ·
bfb2e8a
1
Parent(s): 592fbdc
ai_human_classifier_added
Browse filesFiles changed (7)
- docs/detector/ai_human_image_checker.md+132
- features/ai_human_image_classifier/controller.pyAdded+35
- features/ai_human_image_classifier/inferencer.pyAdded+48
- features/ai_human_image_classifier/main.pyAdded+27
- features/ai_human_image_classifier/model_loader.pyAdded+80
- features/ai_human_image_classifier/preprocessor.pyAdded+34
- features/ai_human_image_classifier/routes.pyAdded+44
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132Real vs. Fake Image Classification for Production Pipeline==========================================================1\. Business Problem--------------------This project addresses the critical business need to automatically identify and flag manipulated or synthetically generated images. By accurately classifying images as **"real"** or **"fake,"** we can enhance the integrity of our platform, prevent the spread of misinformation, and protect our users from fraudulent content. This solution is designed for integration into our production pipeline to process images in real-time.2\. Solution Overview---------------------This solution leverages OpenAI's CLIP (Contrastive Language-Image Pre-Training) model to differentiate between real and fake images. The system operates as follows:1. **Feature Extraction:** A pre-trained CLIP model ('ViT-L/14') converts input images into 768-dimensional feature vectors.2. **Classification:** A Support Vector Machine (SVM) model, trained on our internal dataset of real and fake images, classifies the feature vectors.3. **Deployment:** The trained model is deployed as a service that can be integrated into our production image processing pipeline.The model has achieved an accuracy of **98.29%** on our internal test set, demonstrating its effectiveness in distinguishing between real and fake images.3\. Getting Started-------------------### 3.1. DependenciesTo ensure a reproducible environment, all dependencies are listed in the requirements.txt file. Install them using pip:```bashpip install -r requirements.txt```**requirements.txt**:- numpy- Pillow- torch- clip-by-openai- scikit-learn- tqdm- seaborn- matplotlib### 3.2. Data PreparationThe model was trained on a dataset of real and fake images obtained form kaggle the dataset link is https://www.kaggle.com/datasets/tristanzhang32/ai-generated-images-vs-real-images/data$0.### 3.3. Usage#### 3.3.1. Feature ExtractionTo extract features from a new dataset, run the following command:```python extract_features.py --data_dir /path/to/your/data --output_file features.npz```#### 3.3.2. Model TrainingTo retrain the SVM model on a new set of extracted features, run:```python train_model.py --features_file features.npz --model_output_path model.joblib```#### 3.3.3. InferenceTo classify a single image using the trained model, use the provided inference script:```python classify.py --image_path /path/to/your/image.jpg --model_path model.joblib```4\. Production Deployment-------------------------The image classification model is deployed as a microservice. The service exposes an API endpoint that accepts an image and returns a classification result ("real" or "fake").### 4.1. API Specification* **Endpoint:** /classify* **Method:** POST* **Request Body:** multipart/form-data with a single field image.* **Response:*** JSON{ "classification": "real", "confidence": 0.95}* JSON{ "error": "Error message"}### 4.2. Scalability and MonitoringThe service is deployed in a containerized environment (e.g., Docker) and managed by an orchestrator (e.g., Kubernetes) to ensure scalability and high availability. Monitoring and logging are in place to track model performance, API latency, and error rates.5\. Model Versioning--------------------We use a combination of Git for code versioning and a model registry for tracking trained model artifacts. Each model is versioned and associated with the commit hash of the code that produced it. The current production model is **v1.2.0**.6\. Testing-----------The project includes a suite of tests to ensure correctness and reliability:* **Unit tests:** To verify individual functions and components.* **Integration tests:** To test the interaction between different parts of the system.* **Model evaluation tests:** To continuously monitor model performance on a golden dataset.To run the tests, execute:```pytest```7\. Future Work---------------* **Explore more advanced classifiers:** Investigate the use of neural network-based classifiers on top of CLIP features.* **Fine-tune the CLIP model:** For even better performance, we can fine-tune the CLIP model on our specific domain of images.* **Expand the training dataset:** Continuously augment the training data with new examples of real and fake images to improve the model's robustness.8\. Contact/Support-------------------For any questions or issues regarding this project, please contact the Machine Learning team at [your-team-email@yourcompany.com](mailto:your-team-email@yourcompany.com) .
1234567891011121314151617181920212223242526272829303132333435from typing import IOfrom preprocessor import preprocessorfrom inferencer import inferencer
class ClassificationController: """ Controller to handle the image classification logic. """ def classify_image(self, image_file: IO) -> dict: """ Orchestrates the classification of a single image file.
Args: image_file (IO): The image file to classify.
Returns: dict: The classification result. """ try: # Step 1: Preprocess the image image_tensor = preprocessor.process(image_file)
# Step 2: Perform inference result = inferencer.predict(image_tensor)
return result except ValueError as e: # Handle specific errors like invalid images return {"error": str(e)} except Exception as e: # Handle unexpected errors print(f"An unexpected error occurred: {e}") return {"error": "An internal error occurred during classification."}
controller = ClassificationController()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import torchimport numpy as npfrom model_loader import models
class Inferencer:
def __init__(self): self.clip_model = models.clip_model self.svm_model = models.svm_model
@torch.no_grad() def predict(self, image_tensor:torch.Tensor) -> dict: """ Takes a preprocessed image tensor and returns the classification result.
Args: image_tensor (torch.Tensor): The preprocessed image tensor.
Returns: dict: A dictionary containing the classification label and confidence score. """
image_features = self.clip_model.encode_image(image_tensor) image_features_np = image_features.cpu().numpy()
prediction = self.svm_model.predict(image_features_np)[0]
if hasattr(self.svm_model, "predict_proba"): # If yes, use predict_proba for a true confidence score confidence_scores = self.svm_model.predict_proba(image_features_np)[0] confidence = float(np.max(confidence_scores)) else: # If no, use decision_function as a fallback confidence measure. # The absolute value of the decision function score indicates confidence. # We can apply a sigmoid function to scale it to a [0, 1] range for consistency. decision_score = self.svm_model.decision_function(image_features_np)[0] confidence = 1 / (1 + np.exp(-np.abs(decision_score))) confidence = float(confidence)
label_map = {0: 'real', 1: 'fake'} classification_label = label_map.get(prediction, "unknown")
return { "classification": classification_label, "confidence": confidence }
inferencer = Inferencer()123456789101112131415161718192021222324252627from fastapi import FastAPIfrom routes import router as api_router
# Initialize the FastAPI appapp = FastAPI( title="Real vs. Fake Image Classification API", description="An API to classify images as real or fake using OpenAI's CLIP and an SVM model.", version="1.0.0")
# Include the API router# All routes defined in routes.py will be available under the /api prefixapp.include_router(api_router, prefix="/api", tags=["Classification"])
@app.get("/", tags=["Root"])async def read_root(): """ A simple root endpoint to confirm the API is running. """ return {"message": "Welcome to the Image Classification API. Go to /docs for the API documentation."}
# To run this application:# 1. Make sure you have all dependencies from requirements.txt installed.# 2. Make sure the 'svm_model.joblib' file is in the same directory.# 3. Run the following command in your terminal:# uvicorn main:app --reload1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import clipimport torchimport joblibfrom pathlib import Pathfrom huggingface_hub import hf_hub_download
class ModelLoader: """ A class to load and hold the machine learning models. This ensures that models are loaded only once. """ def __init__(self, clip_model_name: str, svm_repo_id: str, svm_filename: str): """ Initializes the ModelLoader and loads the models.
Args: clip_model_name (str): The name of the CLIP model to load (e.g., 'ViT-L/14'). svm_repo_id (str): The repository ID on Hugging Face (e.g., 'rhnsa/ai_human_image_detector'). svm_filename (str): The name of the model file in the repository (e.g., 'model.joblib'). """ self.device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using device: {self.device}")
self.clip_model, self.clip_preprocess = self._load_clip_model(clip_model_name) self.svm_model = self._load_svm_model(repo_id=svm_repo_id, filename=svm_filename) print("Models loaded successfully.")
def _load_clip_model(self, model_name: str): """ Loads the specified CLIP model and its preprocessor. Args: model_name (str): The name of the CLIP model. Returns: A tuple containing the loaded CLIP model and its preprocess function. """ try: model, preprocess = clip.load(model_name, device=self.device) return model, preprocess except Exception as e: print(f"Error loading CLIP model: {e}") raise
def _load_svm_model(self, repo_id: str, filename: str): """ Downloads and loads the SVM model from a Hugging Face Hub repository.
Args: repo_id (str): The repository ID on Hugging Face. filename (str): The name of the model file in the repository.
Returns: The loaded SVM model object. """ print(f"Downloading SVM model from Hugging Face repo: {repo_id}") try: # Download the model file from the Hub. It returns the cached path. model_path = hf_hub_download(repo_id=repo_id, filename=filename) print(f"SVM model downloaded to: {model_path}") # Load the model from the downloaded path svm_model = joblib.load(model_path) return svm_model except Exception as e: print(f"Error downloading or loading SVM model from Hugging Face: {e}") raise
# --- Global Model Instance ---# This creates a single instance of the models that can be imported by other modules.CLIP_MODEL_NAME = 'ViT-L/14'SVM_REPO_ID = 'rhnsa/ai_human_image_detector'SVM_FILENAME = 'svm_model_real.joblib' # The name of your model file in the Hugging Face repo
# This instance will be created when the application starts.models = ModelLoader( clip_model_name=CLIP_MODEL_NAME, svm_repo_id=SVM_REPO_ID, svm_filename=SVM_FILENAME)12345678910111213141516171819202122232425262728293031323334from PIL import Imageimport torchfrom typing import IOfrom model_loader import models
class ImagePreprocessor:
def __init__(self): self.preprocess = models.clip_preprocess self.device = models.device
def process(self, image_file: IO) -> torch.Tensor: """ Opens an image file, preprocesses it, and returns it as a tensor.
Args: image_file (IO): The image file object (e.g., from a file upload).
Returns: torch.Tensor: The preprocessed image as a tensor, ready for the model. """ try: # Open the image from the file-like object image = Image.open(image_file).convert("RGB") except Exception as e: print(f"Error opening image: {e}") # You might want to raise a custom exception here raise ValueError("Invalid or corrupted image file.")
# Apply the CLIP preprocessing transformations and move to the correct device image_tensor = self.preprocess(image).unsqueeze(0).to(self.device) return image_tensor preprocessor = ImagePreprocessor()1234567891011121314151617181920212223242526272829303132333435363738394041424344from fastapi import APIRouter, File, UploadFile, HTTPException, statusfrom fastapi.responses import JSONResponsefrom controller import controller
from fastapi import Request, Dependsfrom fastapi.security import HTTPBearerfrom slowapi import Limiterfrom slowapi.util import get_remote_address
router = APIRouter()limiter = Limiter(key_func=get_remote_address)security = HTTPBearer()# Create an API routerrouter = APIRouter()
@router.post("/classify", summary="Classify an image as Real or Fake")async def classify_image_endpoint(image: UploadFile = File(...)): """ Accepts an image file and classifies it as 'real' or 'fake'.
- **image**: The image file to be classified (e.g., JPEG, PNG).
Returns a JSON object with the classification and a confidence score. """ # Check for a valid image content type if not image.content_type.startswith("image/"): raise HTTPException( status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, detail="Unsupported file type. Please upload an image (e.g., JPEG, PNG)." )
# The controller expects a file-like object, which `image.file` provides result = controller.classify_image(image.file)
if "error" in result: # If the controller returned an error, forward it as an HTTP exception raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=result["error"] ) return JSONResponse(content=result, status_code=status.HTTP_200_OK)
docs/detector/ai_human_image_checker.md+132
features/ai_human_image_classifier/controller.pyAdded+35
features/ai_human_image_classifier/inferencer.pyAdded+48
features/ai_human_image_classifier/main.pyAdded+27
features/ai_human_image_classifier/model_loader.pyAdded+80
features/ai_human_image_classifier/preprocessor.pyAdded+34
features/ai_human_image_classifier/routes.pyAdded+44