Great question! Building a local LLM (Large Language Model) or image model to sort pictures on Windows 11 is a fun and practical project. Here’s a step-by-step guide to get you started, using open-source models and Python. This guide assumes you want to sort images by content (e.g., cats, dogs, landscapes, etc.), not just by file name or date.
1. Choose a Model
For image sorting, you want an image classification model. You don’t need a language model (LLM) unless you want to generate text descriptions or interact via chat. Good options:
- CLIP (by OpenAI): Can classify images based on text prompts.
- BLIP: Can generate captions for images.
- YOLO: For object detection.
For most sorting tasks, CLIP is a great choice.
2. Set Up Your Environment
-
Install Python (if not already):
Download from python.org.
-
Install Required Packages:
Open Command Prompt and run:
pip install torch torchvision pillow tqdm openai-clip
3. Download or Prepare Your Images
Put all your images in a folder, e.g., C:\Users\YourName\Pictures\ToSort.
4. Write the Sorting Script
Here’s a simple script using CLIP to sort images into folders based on categories you define (e.g., "cat", "dog", "car", "landscape").
import os
import shutil
from PIL import Image
import torch
import clip
from tqdm import tqdm
# Set your image folder and categories
image_folder = r"C:\Users\YourName\Pictures\ToSort"
output_folder = r"C:\Users\YourName\Pictures\Sorted"
categories = ["cat", "dog", "car", "landscape"]
# Load CLIP model
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
# Preprocess category prompts
text = clip.tokenize(categories).to(device)
# Create output folders
for category in categories:
os.makedirs(os.path.join(output_folder, category), exist_ok=True)
# Sort images
for filename in tqdm(os.listdir(image_folder)):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
image_path = os.path.join(image_folder, filename)
image = preprocess(Image.open(image_path)).unsqueeze(0).to(device)
with torch.no_grad():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
logits_per_image, _ = model(image, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()[0]
best_category = categories[probs.argmax()]
shutil.copy(image_path, os.path.join(output_folder, best_category, filename))
print(f"{filename} -> {best_category}")
print("Sorting complete!")