Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import custom modules from a Github repository in Google Colab?

I understand how to run a single notebook in Colab. However, I am not sure how to use all files from a repository, i.e to be able to import functions inside Colab notebook? Thank you.

like image 562
mariyana. Avatar asked Oct 06 '18 17:10

mariyana.


1 Answers

Let's say we want to run the ipynb file, named as "1-fully-connected-binarized-mnist" residing in the repo "qnn-inference-examples".

https://github.com/maltanar/qnn-inference-examples

The notebook of interest uses customly created QNN library and functions inside that repo. Yes we need to import that function. To do this, we should first upload the repo folder to Google Colab, then correct/modify library and file paths.

0) Open the ipynb file "1-fully-connected-binarized-mnist" on your Colab. You can rename it if you like. Try to run it, but will probably get some errors (as I did). So let's fix these issues

1) Insert a new code cell at the top of the notebook. And clone the repo on your Colab:

!git clone https://github.com/maltanar/qnn-inference-examples.git

now the new folder "qnn-inference-examples" created under your "content" folder. you should see something like this on the left side. And remember the path "/content/qnn-inference-examples"

check folder uploaded or not, and location

2) Now add the second new cell on top:

import sys
sys.path.insert(0,'/content/qnn-inference-examples')

This will fix the issue about not able to find the library location, when trying import the QNN libraries.

3) Manually fix the file links on the existing code, according to the new path. Because the library and files now exist under the folder "/content/qnn-inference-examples":

for example replace:

img = Image.open("7.png")

with

img = Image.open("/content/qnn-inference-examples/7.png")

These steps should do the work


Please note that: This is not my own solution, mix of 2 or 3 solutions. Credit goes to Hüseyin Elçi, KDnuggets and Alexandr Haymin

https://medium.com/analytics-vidhya/importing-your-own-python-module-or-python-file-into-colab-3e365f0a35ec

https://www.kdnuggets.com/2018/02/google-colab-free-gpu-tutorial-tensorflow-keras-pytorch.html/2

like image 70
SoajanII Avatar answered Sep 18 '22 09:09

SoajanII