In today’s digital age, recommendation systems have become an integral part of our lives. From suggesting movies on Netflix to recommending outfits on e-commerce platforms, these systems enhance our experiences by tailoring options to our preferences.
But have you ever wondered how fashion recommendation systems work? What powers the suggestions of those trendy dresses or accessories? In this blog, I’ll walk you through how I built a Fashion Recommendation System using Python, TensorFlow, and pre-trained CNN models.
If you’re a data scientist, a developer, or simply someone curious about AI in fashion, this guide is for you. Let’s dive in!
A fashion recommendation system leverages image features to analyze and recommend visually similar or complementary products. Instead of relying solely on metadata like size or brand, it uses computer vision to understand aspects like:
This allows the system to recommend items that match a user’s preferences visually, taking personalization to the next level.
The idea behind this project is simple:
- Extract deep features from fashion images using a pre-trained CNN model.
- Measure the similarity between feature vectors of different images.
- Recommend the top
N
visually similar items for a given query image.
Here’s how I implemented it step-by-step.
Step 1: Setting Up the Dataset
First, I needed a diverse dataset of fashion images. I used the women-fashion.zip dataset, which contains various outfits and accessories.
To start, I extracted the dataset and listed the files:
from zipfile import ZipFile
import os zip_file_path = '/content/women-fashion.zip'
extraction_directory = '/content/women_fashion/'
if not os.path.exists(extraction_directory):
os.makedirs(extraction_directory)
with ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(extraction_directory)
extracted_files = os.listdir(extraction_directory)
print(extracted_files[:10])
This gave me a list of images that I could use for feature extraction.
Step 2: Visualizing an Image
Understanding the dataset is key. I used Pillow and Matplotlib to visualize the first image and analyze its characteristics.
from PIL import Image
import matplotlib.pyplot as plt def display_image(file_path):
image = Image.open(file_path)
plt.imshow(image)
plt.axis('off')
plt.show()
# Display the first image
first_image_path = os.path.join(extraction_directory, extracted_files[0])
display_image(first_image_path)
By observing the images, I ensured they were of high quality and suitable for feature extraction.
Step 3: Feature Extraction Using VGG16
For feature extraction, I used VGG16, a pre-trained CNN model from Keras. VGG16 is known for its ability to extract powerful representations of image features.
Load the Pre-Trained Model
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.models import Model base_model = VGG16(weights='imagenet', include_top=False)
model = Model(inputs=base_model.input, outputs=base_model.output)
Preprocess Images
Each image was resized to the input size required by VGG16 (224×224), and its pixel values were preprocessed:
from tensorflow.keras.preprocessing import image
import numpy as np def preprocess_image(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array_expanded = np.expand_dims(img_array, axis=0)
return preprocess_input(img_array_expanded)
Step 4: Extracting Features
With the model and preprocessing pipeline ready, I passed each image through the network to extract its features.
def extract_features(model, preprocessed_img):
features = model.predict(preprocessed_img)
flattened_features = features.flatten()
normalized_features = flattened_features / np.linalg.norm(flattened_features)
return normalized_features
I looped through the dataset to extract and store features for all images:
all_features = []
all_image_names = [] for img_name in extracted_files:
img_path = os.path.join(extraction_directory, img_name)
preprocessed_img = preprocess_image(img_path)
features = extract_features(model, preprocessed_img)
all_features.append(features)
all_image_names.append(img_name)
Step 5: Computing Similarity
To recommend similar items, I used cosine similarity to measure the closeness of feature vectors.
from sklearn.metrics.pairwise import cosine_similarity def find_similar_items(query_features, all_features, all_image_names, top_n=5):
similarities = cosine_similarity([query_features], all_features)
similar_indices = similarities[0].argsort()[-top_n:][::-1]
return [(all_image_names[i], similarities[0][i]) for i in similar_indices]
Step 6: Making Recommendations
For any query image, I could now extract its features, compute similarities, and recommend the most visually similar items.
query_image_path = '/content/women_fashion/women fashion/anarkali suit with intricate silver embellishments on the neckline, sleeves.jpg'
query_features = extract_features(model, preprocess_image(query_image_path)) recommendations = find_similar_items(query_features, all_features, all_image_names)
for item, score in recommendations:
print(f"Recommended Item: {item}")
query_image_path = '/content/women_fashion/women fashion/fitted black dress that reaches down to mid-calf.jpg'
query_features = extract_features(model, preprocess_image(query_image_path)) recommendations = find_similar_items(query_features, all_features, all_image_names)
for item, score in recommendations:
print(f"Recommended Item: {item}")
query_image_path = '/content/women_fashion/women fashion/sparkling, sequined dress.jpg'
query_features = extract_features(model, preprocess_image(query_image_path)) recommendations = find_similar_items(query_features, all_features, all_image_names)
for item, score in recommendations:
print(f"Recommended Item: {item}")
By providing an input image, the system successfully recommended visually similar items. The results were surprisingly accurate, even with simple preprocessing and feature extraction.