Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,37 +7,25 @@ Original file is located at
|
|
| 7 |
https://colab.research.google.com/drive/1_wYfP0IRdb9fpc2zvbg8IqdXGx1dTo7X
|
| 8 |
"""
|
| 9 |
|
| 10 |
-
|
| 11 |
from datasets import load_dataset
|
| 12 |
from PIL import Image
|
| 13 |
-
|
| 14 |
-
# Load dataset from Hugging Face
|
| 15 |
-
dataset = load_dataset("lirus18/deepfashion", split="train")
|
| 16 |
-
|
| 17 |
-
from PIL import Image
|
| 18 |
-
|
| 19 |
-
# Just to verify image loaded (not for UI)
|
| 20 |
-
image = dataset[0]['image']
|
| 21 |
-
image.save("sample_image.jpg") # saves one image to disk
|
| 22 |
-
|
| 23 |
-
|
| 24 |
from transformers import CLIPProcessor, CLIPModel
|
|
|
|
| 25 |
import torch
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Load
|
|
|
|
| 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 |
-
|
| 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):
|
|
@@ -46,45 +34,25 @@ for i in range(N):
|
|
| 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)
|
| 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
|
| 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 |
-
|
| 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,
|
|
@@ -99,7 +67,6 @@ def generate_outfit_from_image(input_image):
|
|
| 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)):
|
|
@@ -108,25 +75,12 @@ def recommend_from_upload(uploaded_image):
|
|
| 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 |
-
|
| 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"),
|
|
@@ -138,8 +92,7 @@ demo = gr.Interface(
|
|
| 138 |
gr.Image(label="Generated New Outfit"),
|
| 139 |
],
|
| 140 |
title="👗 Fashion Outfit Recommender",
|
| 141 |
-
description="Upload
|
| 142 |
-
examples=example_images
|
| 143 |
)
|
| 144 |
|
| 145 |
-
demo.launch()
|
|
|
|
| 7 |
https://colab.research.google.com/drive/1_wYfP0IRdb9fpc2zvbg8IqdXGx1dTo7X
|
| 8 |
"""
|
| 9 |
|
|
|
|
| 10 |
from datasets import load_dataset
|
| 11 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from transformers import CLIPProcessor, CLIPModel
|
| 13 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 14 |
import torch
|
| 15 |
+
import numpy as np
|
| 16 |
+
import gradio as gr
|
| 17 |
|
| 18 |
+
# Load dataset and model
|
| 19 |
+
dataset = load_dataset("lirus18/deepfashion", split="train")
|
| 20 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 21 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 22 |
|
|
|
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
model.to(device)
|
| 25 |
|
| 26 |
+
# Precompute vectors
|
|
|
|
| 27 |
image_vectors = []
|
| 28 |
image_indices = []
|
|
|
|
|
|
|
| 29 |
N = 500
|
| 30 |
|
| 31 |
for i in range(N):
|
|
|
|
| 34 |
with torch.no_grad():
|
| 35 |
embedding = model.get_image_features(**inputs)
|
| 36 |
image_vectors.append(embedding.cpu().numpy().squeeze())
|
| 37 |
+
image_indices.append(i)
|
| 38 |
|
| 39 |
image_vectors = np.array(image_vectors)
|
| 40 |
|
|
|
|
|
|
|
| 41 |
def find_similar(user_image, top_k=3, exclude_index=None):
|
|
|
|
| 42 |
inputs = processor(images=user_image.convert("RGB"), return_tensors="pt").to(device)
|
| 43 |
with torch.no_grad():
|
| 44 |
query_vec = model.get_image_features(**inputs).cpu().numpy()
|
| 45 |
|
|
|
|
| 46 |
sims = cosine_similarity(query_vec, image_vectors)[0]
|
|
|
|
|
|
|
| 47 |
if exclude_index is not None:
|
| 48 |
+
sims[exclude_index] = -1
|
| 49 |
|
|
|
|
| 50 |
top_idx = sims.argsort()[-top_k:][::-1]
|
|
|
|
| 51 |
return [dataset[image_indices[i]]['image'] for i in top_idx]
|
| 52 |
|
| 53 |
+
# Placeholder for Stable Diffusion (optional)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
from diffusers import StableDiffusionImg2ImgPipeline
|
|
|
|
| 55 |
|
|
|
|
| 56 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 57 |
"runwayml/stable-diffusion-v1-5",
|
| 58 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
|
|
|
| 67 |
from PIL import ImageChops
|
| 68 |
|
| 69 |
def recommend_from_upload(uploaded_image):
|
|
|
|
| 70 |
uploaded_image = uploaded_image.convert("RGB")
|
| 71 |
closest_idx = None
|
| 72 |
for i in range(len(image_indices)):
|
|
|
|
| 75 |
closest_idx = i
|
| 76 |
break
|
| 77 |
|
|
|
|
| 78 |
similar_imgs = find_similar(uploaded_image, top_k=3, exclude_index=closest_idx)
|
|
|
|
|
|
|
| 79 |
generated_img = generate_outfit_from_image(uploaded_image)
|
| 80 |
|
| 81 |
return [uploaded_image] + similar_imgs + [generated_img]
|
| 82 |
|
| 83 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
demo = gr.Interface(
|
| 85 |
fn=recommend_from_upload,
|
| 86 |
inputs=gr.Image(type="pil", label="Upload a clothing item"),
|
|
|
|
| 92 |
gr.Image(label="Generated New Outfit"),
|
| 93 |
],
|
| 94 |
title="👗 Fashion Outfit Recommender",
|
| 95 |
+
description="Upload a clothing image to see 3 similar outfits and 1 AI-generated one!"
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
+
demo.launch()
|