How Google Colab helped me to speed up my video processing tasks while working in remote and had no GPU.

Masoud Masoumi Moghadam
Analytics Vidhya
Published in
5 min readMar 28, 2020

In today post, I want to show you how we can simply run a face detection program on the google colab.

Motivation

Few days ago, In my country many companies (including mine) decided to use remote working policy for employees to diminish their concern for CoronaVirus infection.
Well, remote work is really awesome, but has some drawbacks in my case:

  • Many people stay at home and of course use their phone internet (mostly Instagram). I share my phone data through hotspot on my laptop (you already guessed what happens next!). So I often tolerate lags and disconnections during work. I really miss free and fast internet in office now!
  • My job deals with videos and pretrained networks which are quite heavy and more than 300 MB each. I often need to download or upload from my company server to check my results on videos. Transferring files through VPN connection to office (which often have low bandwidth) puts a lot of pressure on servers (that also enforces lags on my colleagues connection).
  • The last and the most importantly, I had a laptop with no GPU computation support (that’s the most painful case!). I had to wait ages (literally!) for my models in pytorch/tensorflow/keras/… to train or prepare the output video.

So I had to find a way to figure out for these issues. Suddenly after I asked a question on stackoverflow in here, I thought of using google colab. It provided these advantages for me:

  • No need to download heavy files on local machine. can do it on virtual machine too. It also provides utilities to display online video results.
  • No limit of internet and fast download/upload (I could use wget command to get any file from any link).
  • Storing multiple datasets in multiple drives is also available. I could mount any drive whenever I wanted to work with specific dataset.
  • Of course GPU or TPU provided by google is the most important reason to use this service. (love ya google!)

This is the github repository for the ones in hurry. I divided the tutorials to 7 parts and provided tutorial gif for each one.

1) Step 1: Create Colab file

In your google drive create a folder and name it as `face_detection`. Then after installing google colaboratory in your google drive, right click in google drive and choose the create new google colaboratory file which is the same file extension as jupyter notebook files on system. Then rename the file as face_detection.

Creating google colaboratory file

Step 2: Set the runtime type

Now you have to click the runtime tab and choose the change runtime type. Select the GPU or TPU for accelerating the process.

Change the runtime type to GPU

Step 3: Mount the Google Drive

In order to be able to mount the google drive we use this code (Reference):

Then google handles the authentication steps and mounts the drive. Now you can change the directory to the face_recognition directory.

Mount the google drive

Step 4: Clone the face_detection repository

It’s time to clone your project to drive. In my case I use my RetinaFace repository which is a fast and extremely accurate face detection program. You can find it in this link.

Use this command to clone the repository. As you know terminal commands can be used in jupyter notebook by using ! at beginning of the command:

Then I used this command for a little bit of folder management. I just moved every file in the Pytorch_Retinaface directory to its parent directory and removed it afterward.

Cloning the retinanet face detection repository

Step 5: Get pretrained weights from another google drive.

Now as mentioned in the github repository readme file, we have to get the pretrained models to run the program and get reasonable results. We open the link, and right click on the file named as Resnet_50_Final.pth and add the file to My Drive/face_detection/weight directory.
The cool thing here for me is that There is no need to do download, upload and extra copy pasting with terminal and it can be done with just few clicks. this is exactly the opposite of my routine in office and I had to weight a long time for downloading huge files.
Check the gif file below for visual guide.

Get pretrained model weights from link and add it to our project folder weight.

Step 6: Get the video for running the algorithm (if necessary)

You can use wget method to get your test file. I found video file in Coverr.com and copy pasted the link in the notebook. Then used the code below to download the video:

I couldn’t fit the link of course. Now I used the code below for preparing the input video in upload folder and removing the extra files:

Set the video file for face recognition process

Step 7: Run face_detection_server.py file

The last part is to run this command to start the video processing for face detection:

There are other arguments that can be adjusted for project purposes, but all we needed here is the input folder. The output file would be saved in public directory after process.

Check the output results

In the end, I really hope the tips and tricks come in handy for you especially if you are another victim of doing machine learning projects in remote.
While I was enjoying to code remotely, I realized there is another drawback for remote work too and that is how you find a way to cook (no more office meals!). Unfortunately there is no Google API for cooking online food!

--

--