Building YouTube Recommender System with Video Thumbnails and Titles

Published 2024-02-07
Building a deep learning-based multimodal recommender system for YouTube thumbnails and titles using PyTorch denoising autoencoders, sentence embeddings, FAISS, and Streamlit.

Building a deep learning-based recommender system for YouTube thumbnails and titles with Python.

Photo by Szabo Viktor on Unsplash
Photo by Szabo Viktor on Unsplash

Introduction

This project was born as a weekend project where I learned about autoencoders and the usage of autoencoders in real life. However, after seeing its significant progress, I compiled it as a blog post so everyone else interested can benefit from it. However, this is about more than auto-encoders; this includes vector search and text embeddings, too. Before diving into the implementation details, you can get an idea of what we are building here by visiting a hugging face space linked below.

YouTube Recommender - Hugging Face Space
https://huggingface.co/spaces/Ransaka/youtube-recommender

Before thinking about the final architecture, let's slice down our requirements. Essentially, we want an engine to give us suggestions by looking at video thumbnails and titles. We should have an extensive knowledge base containing thumbnails and titles to do that. Once we have that, we can search user-given inputs in our knowledge space and return the best candidates. But we have a couple of problems. First, we need to convert images into vectors and text into vectors. Two, those vector representations should be efficient and meaningful. While working on the project, I had a good grasp of how to proceed with the video titles. However, I was uncertain about how to approach the task of generating vectors for video thumbnail data. Nevertheless, I knew we could use auto-encoders to reduce dimensionality and extract features. Therefore, I decided to give it a try. After some brainstorming, I came up with the following architecture.

Architecture Diagram
Architecture Diagram

I used the pre-trained embedding model to convert text to fixed-length vectors. I also trained the auto-encoder to convert the image to a fixed-length vector from scratch. Once these models are completed, all text/image vectors are pre-calculated and stored as an FAISS index for later similarity search.

As a first step, we should have a proper dataset. I will use the dataset below (Creative Commons Attribution (CC BY)). You can follow along with it or use your dataset with the same format.

Dataset - Ransaka/youtube_recommendation_data
https://huggingface.co/datasets/Ransaka/youtube_recommendation_data

Perfect, now we have a dataset. After initial inspections, I noticed these points.

  1. The dataset contains English, Sinhala, and Tamil language text. Therefore, our embedding model should be a multilingual one.
  2. Every image is the same size (480, 360).

Video Title Processing

As a first step, let's extract vector embeddings for our video titles. We first aim to extract vector embeddings for the video title and store them as FAISS indexes. Later, we can utilize the FAISS index for similarity search. Let's do vectorization using sentence transformers.

from sentence_transformers import SentenceTransformer
from datasets import load_dataset

dataset = load_dataset("Ransaka/youtube_recommendation_data")
model = SentenceTransformer('intfloat/multilingual-e5-base')

input_texts = dataset['train']['title'] + dataset['test']['title']
embeddings = model.encode(input_texts, normalize_embeddings=True)

Now, we can save embeddings for later use.

torch.save(embeddings, "embeddings.bin")

Video Thumbnail Processing

I plan to use a modeling mechanism called denoising auto-encoders for thumbnail processing. This is a slightly different approach than previously used text embedding extraction. The idea here is to train a CNN model to reconstruct the original image through the bottleneck layer. If we think of the encoder and decoder as individual components, the input the encoder receives is the original input we provide for the model. But in the decoder, it's not the case; It received input from the encoder. Hence, the decoder's performance heavily depends on the encoder's latent layer's performance. This means that to increase model performance, the latent layer should learn an abstract representation of the original input. Hence, after the model is fully trained, we can unplug the decoder and utilize the encoder as an image-to-vector model or feature extractor. This is precisely what we will achieve in this tutorial.

denoising auto-encoder general model architecture
denoising auto-encoder general model architecture

Building Denoising Auto Encoder

Building an auto-encoder is a straightforward thing. But when it comes to rectangular images, keeping track of the result size is challenging. However, this Wikipedia link did an excellent job explaining it to me.

import torch
import torch.nn as nn

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class Encoder(nn.Module):
    def __init__(self, in_channels=1, out_channels=16, latent_dim=64, act_fn=nn.ReLU()):
        super().__init__()

        self.net = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, 3, padding=1),  # (480, 360)
            act_fn,
            nn.Conv2d(out_channels, out_channels, 3, padding=1),
            act_fn,
            nn.Conv2d(out_channels, 2 * out_channels, 3, padding=1, stride=2),  # (240, 180)
            act_fn,
            nn.Conv2d(2 * out_channels, 2 * out_channels, 3, padding=1),
            act_fn,
            nn.Conv2d(2 * out_channels, 4 * out_channels, 3, padding=1, stride=2),  # (120, 90)
            act_fn,
            nn.Conv2d(4 * out_channels, 4 * out_channels, 3, padding=1),
            act_fn,
            nn.Flatten(),
            nn.Linear(4 * out_channels * 120 * 90, latent_dim),
            act_fn
        )

    def forward(self, x):
        x = x.view(-1, 1, 480, 360)
        output = self.net(x)
        return output

class Decoder(nn.Module):
    def __init__(self, in_channels=1, out_channels=16, latent_dim=64, act_fn=nn.ReLU()):
        super().__init__()

        self.out_channels = out_channels

        self.linear = nn.Sequential(
            nn.Linear(latent_dim, 4 * out_channels * 120 * 90),
            act_fn
        )

        self.conv = nn.Sequential(
            nn.ConvTranspose2d(4 * out_channels, 4 * out_channels, 3, padding=1),  # (120, 90)
            act_fn,
            nn.ConvTranspose2d(4 * out_channels, 2 * out_channels, 3, padding=1,
                               stride=2, output_padding=1),  # (240, 180)
            act_fn,
            nn.ConvTranspose2d(2 * out_channels, 2 * out_channels, 3, padding=1),
            act_fn,
            nn.ConvTranspose2d(2 * out_channels, out_channels, 3, padding=1,
                               stride=2, output_padding=1),  # (480, 360)
            act_fn,
            nn.ConvTranspose2d(out_channels, out_channels, 3, padding=1),
            act_fn,
            nn.ConvTranspose2d(out_channels, in_channels, 3, padding=1)
        )

    def forward(self, x):
        output = self.linear(x)
        output = output.view(-1, 4 * self.out_channels, 120, 90)
        output = self.conv(output)
        return output

class Autoencoder(nn.Module):
    def __init__(self, encoder, decoder):
        super().__init__()
        self.encoder = encoder
        self.encoder.to(device)

        self.decoder = decoder
        self.decoder.to(device)

    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded

Building Thumbnail Dataset

Let's convert the Hugging Face dataset into the PyTorch dataset as below.

import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader

class ThumbnailDataset(Dataset):
    def __init__(self, data, transforms=None):
        self.data = data
        self.transforms = transforms

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        image = self.data[idx]['image']
        image = image.convert('L')

        if self.transforms is not None:
            image = self.transforms(image)
        return image

training_data = ThumbnailDataset(dataset['train'], transforms=transforms.Compose([transforms.ToTensor()]))
validation_data = ThumbnailDataset(dataset['test'], transforms=transforms.Compose([transforms.ToTensor()]))

Now, we can implement the training loop as follows.

Training auto-encoder

class ConvolutionalAutoencoder():
    def __init__(self, autoencoder):
        self.network = autoencoder
        self.optimizer = torch.optim.Adam(self.network.parameters(), lr=1e-3)

    def train(self, loss_function, epochs, batch_size,
              training_set, validation_set):

        # creating log
        log_dict = {
            'training_loss_per_batch': [],
            'validation_loss_per_batch': [],
            'visualizations': []
        }

        # defining weight initialization function
        def init_weights(module):
            if isinstance(module, nn.Conv2d):
                torch.nn.init.xavier_uniform_(module.weight)
                module.bias.data.fill_(0.01)
            elif isinstance(module, nn.Linear):
                torch.nn.init.xavier_uniform_(module.weight)
                module.bias.data.fill_(0.01)

        # initializing network weights
        self.network.apply(init_weights)

        # creating dataloaders
        train_loader = DataLoader(training_set, batch_size)
        val_loader = DataLoader(validation_set, batch_size)

        # setting convnet to training mode
        self.network.train()
        self.network.to(device)

        for epoch in range(epochs):
            print(f'Epoch {epoch+1}/{epochs}')

            print('training...')
            for images in tqdm(train_loader):
                # zeroing gradients
                self.optimizer.zero_grad()
                # sending images to device
                images = images.to(device)
                # reconstructing images
                output = self.network(images)
                # computing loss
                loss = loss_function(output, images.view(-1, 1, 480, 360))
                # calculating gradients
                loss.backward()
                # optimizing weights
                self.optimizer.step()

                log_dict['training_loss_per_batch'].append(loss.item())

            print('validating...')
            for val_images in tqdm(val_loader):
                with torch.no_grad():
                    # sending validation images to device
                    val_images = val_images.to(device)
                    # reconstructing images
                    output = self.network(val_images)
                    # computing validation loss
                    val_loss = loss_function(output, val_images.view(-1, 1, 480, 360))

                log_dict['validation_loss_per_batch'].append(val_loss.item())

            print(f'training_loss: {round(loss.item(), 4)} validation_loss: {round(val_loss.item(), 4)}')

        return log_dict

    def autoencode(self, x):
        return self.network(x)

    def encode(self, x):
        encoder = self.network.encoder
        return encoder(x)

    def decode(self, x):
        decoder = self.network.decoder
        return decoder(x)

model = ConvolutionalAutoencoder(Autoencoder(Encoder(), Decoder()))

log_dict = model.train(nn.MSELoss(), epochs=10, batch_size=64,
                       training_set=training_data, validation_set=validation_data)

You can check codes at any time by visiting the GitHub repository below.

GitHub - Ransaka/yt-recommender
https://github.com/Ransaka/yt-recommender

Once we successfully train our auto-encoder model, we can select its encoder part and use it as a feature extractor.

import torchvision.transforms as transforms

encoder = Encoder()
# torch.save(model.network.encoder,"encoder.bin")
encoder.load_state_dict(
    torch.load('encoder.bin', map_location=torch.device('cpu'))
)

latent_data = []
for data in training_data:
    data = data.to(device)
    latent_data.append(encoder(data).detach().cpu().numpy().flatten())

latent_data_test = []
for data in validation_data:
    data = data.to(device)
    latent_data_test.append(encoder(data).detach().cpu().numpy().flatten())

latent_data_final = np.concatenate([latent_data, latent_data_test])
torch.save(latent_data_final, "latent_data_final.bin")

First, we have to concatenate and normalize these vectors before building an index on top of them. However, we don't need to normalize title embeddings specifically. We did it while generating embedding from the model. After that, we can define the FAISS index instance to load our embedding in the FAISS index format. First, do the concatenation and normalization parts.

latent_data = torch.load("data/latent_data_final.bin")
embeddings = torch.load("data/embeddings.bin")

def row_wise_normalize_and_concatenate(array1, array2):
    normalized_array1 = array1 / np.linalg.norm(array1, axis=1, keepdims=True)
    normalized_array2 = array2 / np.linalg.norm(array2, axis=1, keepdims=True)

    concatenated_array = np.concatenate((normalized_array1, normalized_array2), axis=1)

    return concatenated_array

result_array = row_wise_normalize_and_concatenate(latent_data, embeddings)

Now, we can build the FAISS index as follows.

import faiss

class Indexer:
    def __init__(self, embed_vec):
        self.embeddings_vec = embed_vec
        self.build_index()

    def build_index(self):
        """
        Build the index for the embeddings.
        This function initializes the index for the embeddings. It calculates the dimension (self.d)
        of the embeddings vector and creates an IndexFlatL2 object (self.index) for the given dimension.
        It then adds the embeddings vector (self.embeddings_vec) to the index.
        Parameters:
        - None
        Return:
        - None
        """
        self.d = self.embeddings_vec.shape[1]
        self.index = faiss.IndexFlatL2(self.d)
        self.index.add(self.embeddings_vec)

    def topk(self, vector, k=4):
        """
        A function that takes in a vector and an optional parameter k and returns the indices of the k nearest neighbors in the index.
        Parameters:
            vector: A numpy array representing the input vector.
            k (optional): An integer representing the number of nearest neighbors to retrieve. Defaults to 4 if not specified.
        Returns:
            I: A numpy array containing the indices of the k nearest neighbors in the index.
        """
        _, I = self.index.search(vector, k)
        return I

# building indexer
index = Indexer(result_array)

This FAISS indexer will return the most similar k candidates upon calling the method topk. We'll use the topk method when we serve this model to end-users.

Now everything is completed for our application. Without further delay, let's jump into the Streamlit application. First, we need to select random images to show as initial images. Below those images, there should be an expander. Once the user clicks on that expander, recommended items should be visible to the user.

We need to select the image title and the image itself when generating recommendations. After that, we have to calculate image latent data and title embeddings. Before getting predictions, we have to normalize latent data and concatenate it as we did in the training step. The piece of code below will do that.

def get_recommendations(image, title, k):
    title_embeds = model.encode([title], normalize_embeddings=True)
    image = transforms.ToTensor()(image.convert("L"))
    image_embeds = encoder(image).detach().numpy()
    image_embeds = image_embeds / np.linalg.norm(image_embeds, axis=1, keepdims=True)
    final_embeds = np.concatenate((image_embeds, title_embeds), axis=1)

    candidates = index.topk(final_embeds, k=k)
    final_candidates = []
    final_candidates.append(list(candidates[0]))
    final_candidates = sum(final_candidates, [])
    results_dict = {"image": [], "title": []}
    for candidate in final_candidates:
        results_dict['image'].append(dataset['image'][candidate])
        results_dict['title'].append(dataset['title'][candidate])
    return results_dict

Let's build a Streamlit front end.

import streamlit as st
import random
import os
from datasets import load_dataset, concatenate_datasets
from utils.recommendation import get_recommendations

START = random.randint(a=0, b=1000)
END = START + 10

dataset = load_dataset("Ransaka/youtube_recommendation_data", token=os.environ.get('HF'))
dataset = concatenate_datasets([dataset['train'], dataset['test']])

pil_images = dataset['image'][START:END]

def show_image_metadata_and_related_info(image_index):
    selected_image = pil_images[image_index]
    image_title = dataset['title'][image_index]
    st.image(selected_image, caption=f"{image_title}", use_column_width=True)

    with st.expander("You May Also Like.."):
        recommendations = get_recommendations(selected_image, image_title, 8, method)

        col1_row1, col2_row1 = st.columns(2)
        with col1_row1:
            st.image(image=recommendations['image'][0], caption=recommendations['title'][0], width=200)
        with col2_row1:
            st.image(image=recommendations['image'][1], caption=recommendations['title'][1], width=200)

        # Second Row
        col1_row2, col2_row2 = st.columns(2)
        with col1_row2:
            st.image(image=recommendations['image'][2], caption=recommendations['title'][2], width=200)
        with col2_row2:
            st.image(image=recommendations['image'][3], caption=recommendations['title'][3], width=200)

        # Third row
        col1_row3, col2_row3 = st.columns(2)
        with col1_row3:
            st.image(image=recommendations['image'][4], caption=recommendations['title'][4], width=200)
        with col2_row3:
            st.image(image=recommendations['image'][5], caption=recommendations['title'][5], width=200)

        # Fourth Row
        col1_row4, col2_row4 = st.columns(2)
        with col1_row4:
            st.image(image=recommendations['image'][6], caption=recommendations['title'][6], width=200)
        with col2_row4:
            st.image(image=recommendations['image'][7], caption=recommendations['title'][7], width=200)

def main():
    st.title("Youtube Recommendation Engine")

    for i, image in enumerate(pil_images):
        show_image_metadata_and_related_info(i)

if __name__ == '__main__':
    main()
Streamlit application showing YouTube recommendation results
Streamlit application showing YouTube recommendation results

Awesome! You have successfully built a YouTube recommendation system with open-source tools and libraries.

Thanks for reading! Connect with me on LinkedIn.

Conclusion

Similar to all other AI/ML projects, this method may not suited for different projects you may be working on. The purpose of this article was to provide you with an understanding of how to build a hybrid recommendation engine (image-based and text-based engine) for the provided YouTube dataset. In this specific dataset, the result seems obvious. However, doing research before any real-world applications is highly advised.

All images, unless otherwise noted, are by the author.

This article was originally published in Towards AI on Medium.