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|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1_wYfP0IRdb9fpc2zvbg8IqdXGx1dTo7X
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install datasets transformers torch torchvision faiss-cpu gradio
|
| 11 |
+
|
| 12 |
+
from datasets import load_dataset
|
| 13 |
+
from PIL import Image
|
| 14 |
+
|
| 15 |
+
# Load dataset from Hugging Face
|
| 16 |
+
dataset = load_dataset("lirus18/deepfashion", split="train")
|
| 17 |
+
|
| 18 |
+
# Show one image
|
| 19 |
+
from IPython.display import display
|
| 20 |
+
|
| 21 |
+
image = dataset[0]['image']
|
| 22 |
+
display(image)
|
| 23 |
+
|
| 24 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 25 |
+
import torch
|
| 26 |
+
|
| 27 |
+
# Load the CLIP model and processor
|
| 28 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 29 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 30 |
+
|
| 31 |
+
# Use GPU if available
|
| 32 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
+
model.to(device)
|
| 34 |
+
|
| 35 |
+
import numpy as np
|
| 36 |
+
|
| 37 |
+
image_vectors = []
|
| 38 |
+
image_indices = []
|
| 39 |
+
|
| 40 |
+
# Use a subset (you can increase to 1000+ later)
|
| 41 |
+
N = 500
|
| 42 |
+
|
| 43 |
+
for i in range(N):
|
| 44 |
+
image = dataset[i]['image'].convert("RGB")
|
| 45 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
embedding = model.get_image_features(**inputs)
|
| 48 |
+
image_vectors.append(embedding.cpu().numpy().squeeze())
|
| 49 |
+
image_indices.append(i) # Store index for later retrieval
|
| 50 |
+
|
| 51 |
+
image_vectors = np.array(image_vectors)
|
| 52 |
+
|
| 53 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 54 |
+
|
| 55 |
+
def find_similar(user_image, top_k=3, exclude_index=None):
|
| 56 |
+
# Embed the user image
|
| 57 |
+
inputs = processor(images=user_image.convert("RGB"), return_tensors="pt").to(device)
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
query_vec = model.get_image_features(**inputs).cpu().numpy()
|
| 60 |
+
|
| 61 |
+
# Compute cosine similarity
|
| 62 |
+
sims = cosine_similarity(query_vec, image_vectors)[0]
|
| 63 |
+
|
| 64 |
+
# Exclude the query image itself
|
| 65 |
+
if exclude_index is not None:
|
| 66 |
+
sims[exclude_index] = -1 # Force low similarity
|
| 67 |
+
|
| 68 |
+
# Get top K similar image indices
|
| 69 |
+
top_idx = sims.argsort()[-top_k:][::-1]
|
| 70 |
+
|
| 71 |
+
return [dataset[image_indices[i]]['image'] for i in top_idx]
|
| 72 |
+
|
| 73 |
+
from IPython.display import display
|
| 74 |
+
|
| 75 |
+
query_index = 10
|
| 76 |
+
query_image = dataset[query_index]['image']
|
| 77 |
+
display(query_image)
|
| 78 |
+
|
| 79 |
+
similar_images = find_similar(query_image, exclude_index=query_index)
|
| 80 |
+
|
| 81 |
+
for img in similar_images:
|
| 82 |
+
display(img)
|
| 83 |
+
|
| 84 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 85 |
+
import torch
|
| 86 |
+
|
| 87 |
+
# Load Stable Diffusion (this might take a while)
|
| 88 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 89 |
+
"runwayml/stable-diffusion-v1-5",
|
| 90 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 91 |
+
).to(device)
|
| 92 |
+
|
| 93 |
+
def generate_outfit_from_image(input_image):
|
| 94 |
+
prompt = "fashion outfit design inspired by the clothing item"
|
| 95 |
+
init_image = input_image.resize((512, 512))
|
| 96 |
+
generated = pipe(prompt=prompt, image=init_image, strength=0.7, guidance_scale=7.5)
|
| 97 |
+
return generated.images[0]
|
| 98 |
+
|
| 99 |
+
from PIL import ImageChops
|
| 100 |
+
|
| 101 |
+
def recommend_from_upload(uploaded_image):
|
| 102 |
+
# Step 1: Compare uploaded image to all dataset images and find the most identical one
|
| 103 |
+
uploaded_image = uploaded_image.convert("RGB")
|
| 104 |
+
closest_idx = None
|
| 105 |
+
for i in range(len(image_indices)):
|
| 106 |
+
dataset_image = dataset[image_indices[i]]['image'].convert("RGB")
|
| 107 |
+
if ImageChops.difference(dataset_image, uploaded_image).getbbox() is None:
|
| 108 |
+
closest_idx = i
|
| 109 |
+
break
|
| 110 |
+
|
| 111 |
+
# Step 2: Get top 3 similar, excluding the identical one if found
|
| 112 |
+
similar_imgs = find_similar(uploaded_image, top_k=3, exclude_index=closest_idx)
|
| 113 |
+
|
| 114 |
+
# Step 3: Generate 1 new outfit (placeholder for now)
|
| 115 |
+
generated_img = generate_outfit_from_image(uploaded_image)
|
| 116 |
+
|
| 117 |
+
return [uploaded_image] + similar_imgs + [generated_img]
|
| 118 |
+
|
| 119 |
+
import gradio as gr
|
| 120 |
+
import os
|
| 121 |
+
|
| 122 |
+
# Prepare example paths
|
| 123 |
+
example_images = [
|
| 124 |
+
["/content/fashion_examples/new1.jpg"],
|
| 125 |
+
["/content/fashion_examples/newnew.jpg"],
|
| 126 |
+
["/content/fashion_examples/newoutfit.jpg"],
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
+
# Build the interface
|
| 130 |
+
demo = gr.Interface(
|
| 131 |
+
fn=recommend_from_upload,
|
| 132 |
+
inputs=gr.Image(type="pil", label="Upload a clothing item"),
|
| 133 |
+
outputs=[
|
| 134 |
+
gr.Image(label="Your Input"),
|
| 135 |
+
gr.Image(label="Similar Item 1"),
|
| 136 |
+
gr.Image(label="Similar Item 2"),
|
| 137 |
+
gr.Image(label="Similar Item 3"),
|
| 138 |
+
gr.Image(label="Generated New Outfit"),
|
| 139 |
+
],
|
| 140 |
+
title="👗 Fashion Outfit Recommender",
|
| 141 |
+
description="Upload your own image *or* click an example to get 3 similar items + 1 AI-generated outfit.",
|
| 142 |
+
examples=example_images
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
demo.launch()
|