readme: ---
  tags:
  - clip
  - vision
  - text
  language: multilingual
  license: mit
  ---
  
  # MultiLingual CLIP
  
  Multilingual CLIP is a pre-trained model which can be used for multilingual semantic search and zero-shot image classification in 100 languages.
  
  
  # Model Architecture
  Multilingual CLIP was built using [OpenAI CLIP](https://github.com/openai/CLIP) model. I have used the same Vision encoder (ResNet 50x4), but instead I replaced their text encoder (Transformer) with a Mulilingual Text Encoder ([XLM-Roberta](https://huggingface.co/xlm-roberta-large)) and a configurable number of projection heads, as seen below:
  
  ![Model Architecture](https://challengepost-s3-challengepost.netdna-ssl.com/photos/production/software_photos/001/858/046/datas/gallery.jpg)
  
  The model was trained in a distributed fashion on 16 Habana Gaudi Accelerators and with mixed Precision in two phases (using COCO Dataset for phase 1 and Google Conceptual Captions for phase 2). The training pipeline was built using PyTorch, PyTorch Lightning, and Distributed Data Parallel.
  
  
  # Datasets
  
  Three datasets have been used for building the model. COCO captions was used for training phase 1 and Google Conceptual Captions was used for training phase 2. Unsplash dataset was used for testing and inference.
  
  ## COCO Captions
  
  COCO (Common Objects in Context) is a large-scale object detection, segmentation, and captioning dataset. The COCO captions dataset has around ~85000 images and captions pairs.
  
  Run the following to download the dataset:
  
  ```bash
  ./download_coco.sh
  ```
  
  This dataset was used for the first pre-training phase.
  
  ## Google Conceptual Captions
  
  Conceptual Captions is a dataset consisting of ~3.3 million images annotated with captions. In contrast with the curated style of other image caption annotations, Conceptual Caption images and their raw descriptions are harvested from the web, and therefore represent a wider variety of styles.
  
  Download the datasets urls/captions from [here](https://storage.cloud.google.com/gcc-data/Train/GCC-training.tsv?_ga=2.191230122.-1896153081.1529438250) as save it to `datasets/googlecc/googlecc.tsv`. The full dataset has over 3 million images, but you can select a subset by loading the `googlecc.tsv` file and saving only the number of rows you want (I have used 1 million images for training).
  
  Then run the following commands to download each image on the `googlecc.tsv` file:
  
  ```bash
  npm install
  node download_build_googlecc.js
  ```
  
  This dataset was used for the second pre-training phase.
  
  ## Unplash
  
  This dataset was used as the test set during inference.
  
  Run `python3.8 download_unsplash.py` to download the dataset.
  
  # Training
  
  ![Training phase 1](https://challengepost-s3-challengepost.netdna-ssl.com/photos/production/software_photos/001/858/047/datas/gallery.jpg)
  
  ![Training phase 2](https://challengepost-s3-challengepost.netdna-ssl.com/photos/production/software_photos/001/858/048/datas/gallery.jpg)
  
  ## Setup
  
  Create two Habana instances ([AWS EC2 DL1](https://aws.amazon.com/ec2/instance-types/dl1/)) using [Habana® Deep Learning Base AMI (Ubuntu 20.04)](https://aws.amazon.com/marketplace/pp/prodview-fw46rwuxrtfse)
  
  
  Create the PyTorch docker container running:
  
  ```bash
  docker run --name pytorch -td --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --net=host --ipc=host vault.habana.ai/gaudi-docker/1.2.0/ubuntu20.04/habanalabs/pytorch-installer-1.10.0:1.2.0-585
  ```
  
  Enter the docker image by running:
  
  ```
  docker exec -it pytorch  /bin/bash
  ```
  
  #### Setup password-less ssh between all connected servers
  
  1. Configure password-less ssh between all nodes:
  
     Do the following in all the nodes' docker sessions:
     ```bash
     mkdir ~/.ssh
     cd ~/.ssh
     ssh-keygen -t rsa -b 4096
     ```
     Copy id_rsa.pub contents from every node's docker to every other node's docker's ~/.ssh/authorized_keys (all public keys need to be in all hosts' authorized_keys):
     ```bash
     cat id_rsa.pub > authorized_keys
     vi authorized_keys
     ```
     Copy the contents from inside to other systems.
     Paste all hosts' public keys in all hosts' “authorized_keys” file.
  
  2. On each system:
     Add all hosts (including itself) to known_hosts. The IP addresses used below are just for illustration:
     ```bash
     ssh-keyscan -p 3022 -H $IP1 >> ~/.ssh/known_hosts
     ssh-keyscan -p 3022 -H $IP2 >> ~/.ssh/known_hosts
     ```
  
  3. Change Docker SSH port to 3022
      ```bash
      sed -i 's/#Port 22/Port 3022/g' /etc/ssh/sshd_config
      sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
      service ssh restart
      ```
  
  [Allow all TCP](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html) traffic between the nodes on AWS
  
  Clone the git repo:
  
  ```bash
  git clone https://github.com/gzomer/clip-multilingual
  ```
  
  Create environment:
  
  ```bash
  python3.8 -m venv .env
  ```
  
  Install requirements:
  
  ```bash
  python3.8 -r requirements.txt
  ```
  
  Activate en