Last active
July 28, 2020 20:52
-
-
Save aakashchoubey/afa4037bad0464ff66f09fda75dd0970 to your computer and use it in GitHub Desktop.
A Helper for Google Colab for doing a bunch of things. I intend to update it as I go, feel free to suggest edits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text", | |
"id": "QYtqb4-Lz4rA" | |
}, | |
"source": [ | |
"# Google Colab Helper" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text", | |
"id": "4r_Kv_3qz4Gc" | |
}, | |
"source": [ | |
"## 1. Checking Configuration" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 1275, | |
"status": "ok", | |
"timestamp": 1529978250838, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "5qaP3ZkNzUVF", | |
"outputId": "3928fb49-7dd5-4572-e4bf-5742ec576abb" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello, Colaboratory from Python 3!\n" | |
] | |
} | |
], | |
"source": [ | |
"import sys\n", | |
"print('Hello, Colaboratory from Python {}!'.format(sys.version_info[0]))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 1177, | |
"status": "ok", | |
"timestamp": 1529979108875, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "d4RBisu7z0Ee", | |
"outputId": "783d07c0-edab-4b04-ac2f-663c65c9c336" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'/device:GPU:0'" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": { | |
"tags": [] | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import tensorflow as tf\n", | |
"tf.test.gpu_device_name()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 272 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 956, | |
"status": "ok", | |
"timestamp": 1529979110040, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "xdkXVqnz2tdy", | |
"outputId": "2e7712d9-bb60-4bd8-d241-cef61bb2a482" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[name: \"/device:CPU:0\"\n", | |
" device_type: \"CPU\"\n", | |
" memory_limit: 268435456\n", | |
" locality {\n", | |
" }\n", | |
" incarnation: 13920098296201886306, name: \"/device:GPU:0\"\n", | |
" device_type: \"GPU\"\n", | |
" memory_limit: 11287966516\n", | |
" locality {\n", | |
" bus_id: 1\n", | |
" links {\n", | |
" }\n", | |
" }\n", | |
" incarnation: 4826610352247481427\n", | |
" physical_device_desc: \"device: 0, name: Tesla K80, pci bus id: 0000:00:04.0, compute capability: 3.7\"]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": { | |
"tags": [] | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from tensorflow.python.client import device_lib\n", | |
"device_lib.list_local_devices()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 748 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 2168, | |
"status": "ok", | |
"timestamp": 1529979155793, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "Iv_UdTxx3AZx", | |
"outputId": "5d32d662-544f-43fa-c334-78e740c31422" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MemTotal: 13341832 kB\r\n", | |
"MemFree: 1290532 kB\r\n", | |
"MemAvailable: 11765056 kB\r\n", | |
"Buffers: 126652 kB\r\n", | |
"Cached: 10282880 kB\r\n", | |
"SwapCached: 0 kB\r\n", | |
"Active: 3567316 kB\r\n", | |
"Inactive: 7704776 kB\r\n", | |
"Active(anon): 1032760 kB\r\n", | |
"Inactive(anon): 85800 kB\r\n", | |
"Active(file): 2534556 kB\r\n", | |
"Inactive(file): 7618976 kB\r\n", | |
"Unevictable: 0 kB\r\n", | |
"Mlocked: 0 kB\r\n", | |
"SwapTotal: 0 kB\r\n", | |
"SwapFree: 0 kB\r\n", | |
"Dirty: 396 kB\r\n", | |
"Writeback: 0 kB\r\n", | |
"AnonPages: 862636 kB\r\n", | |
"Mapped: 382044 kB\r\n", | |
"Shmem: 255968 kB\r\n", | |
"Slab: 606936 kB\r\n", | |
"SReclaimable: 574408 kB\r\n", | |
"SUnreclaim: 32528 kB\r\n", | |
"KernelStack: 3104 kB\r\n", | |
"PageTables: 6512 kB\r\n", | |
"NFS_Unstable: 0 kB\r\n", | |
"Bounce: 0 kB\r\n", | |
"WritebackTmp: 0 kB\r\n", | |
"CommitLimit: 6670916 kB\r\n", | |
"Committed_AS: 2137560 kB\r\n", | |
"VmallocTotal: 34359738367 kB\r\n", | |
"VmallocUsed: 0 kB\r\n", | |
"VmallocChunk: 0 kB\r\n", | |
"AnonHugePages: 0 kB\r\n", | |
"HugePages_Total: 0\r\n", | |
"HugePages_Free: 0\r\n", | |
"HugePages_Rsvd: 0\r\n", | |
"HugePages_Surp: 0\r\n", | |
"Hugepagesize: 2048 kB\r\n", | |
"DirectMap4k: 159692 kB\r\n", | |
"DirectMap2M: 5083136 kB\r\n", | |
"DirectMap1G: 10485760 kB\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!cat /proc/meminfo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 955 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 2234, | |
"status": "ok", | |
"timestamp": 1529979180025, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "AaxGYZc93SxL", | |
"outputId": "31b9c490-816c-4ca1-9e81-8823412f80d4" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"processor\t: 0\r\n", | |
"vendor_id\t: GenuineIntel\r\n", | |
"cpu family\t: 6\r\n", | |
"model\t\t: 63\r\n", | |
"model name\t: Intel(R) Xeon(R) CPU @ 2.30GHz\r\n", | |
"stepping\t: 0\r\n", | |
"microcode\t: 0x1\r\n", | |
"cpu MHz\t\t: 2300.000\r\n", | |
"cache size\t: 46080 KB\r\n", | |
"physical id\t: 0\r\n", | |
"siblings\t: 2\r\n", | |
"core id\t\t: 0\r\n", | |
"cpu cores\t: 1\r\n", | |
"apicid\t\t: 0\r\n", | |
"initial apicid\t: 0\r\n", | |
"fpu\t\t: yes\r\n", | |
"fpu_exception\t: yes\r\n", | |
"cpuid level\t: 13\r\n", | |
"wp\t\t: yes\r\n", | |
"flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm kaiser fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms xsaveopt\r\n", | |
"bugs\t\t:\r\n", | |
"bogomips\t: 4600.00\r\n", | |
"clflush size\t: 64\r\n", | |
"cache_alignment\t: 64\r\n", | |
"address sizes\t: 46 bits physical, 48 bits virtual\r\n", | |
"power management:\r\n", | |
"\r\n", | |
"processor\t: 1\r\n", | |
"vendor_id\t: GenuineIntel\r\n", | |
"cpu family\t: 6\r\n", | |
"model\t\t: 63\r\n", | |
"model name\t: Intel(R) Xeon(R) CPU @ 2.30GHz\r\n", | |
"stepping\t: 0\r\n", | |
"microcode\t: 0x1\r\n", | |
"cpu MHz\t\t: 2300.000\r\n", | |
"cache size\t: 46080 KB\r\n", | |
"physical id\t: 0\r\n", | |
"siblings\t: 2\r\n", | |
"core id\t\t: 0\r\n", | |
"cpu cores\t: 1\r\n", | |
"apicid\t\t: 1\r\n", | |
"initial apicid\t: 1\r\n", | |
"fpu\t\t: yes\r\n", | |
"fpu_exception\t: yes\r\n", | |
"cpuid level\t: 13\r\n", | |
"wp\t\t: yes\r\n", | |
"flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm kaiser fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms xsaveopt\r\n", | |
"bugs\t\t:\r\n", | |
"bogomips\t: 4600.00\r\n", | |
"clflush size\t: 64\r\n", | |
"cache_alignment\t: 64\r\n", | |
"address sizes\t: 46 bits physical, 48 bits virtual\r\n", | |
"power management:\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!cat /proc/cpuinfo" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text", | |
"id": "8KHtIYNf4TzI" | |
}, | |
"source": [ | |
"## 2. Connecting with Drive\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 979, | |
"status": "ok", | |
"timestamp": 1531773607559, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "joCPNPh60lG_", | |
"outputId": "d253f158-b741-4cc5-f188-f4056e3d0c95" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['datalab', '.config', '.forever', '.local', 'drive', '.ipython', '.cache']\n" | |
] | |
} | |
], | |
"source": [ | |
"import os\n", | |
"print(os.listdir(\".\"))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 445 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 49404, | |
"status": "ok", | |
"timestamp": 1531773664185, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "SO2ugZXk0A6n", | |
"outputId": "1c7e82ac-5b14-40f0-d290-82e1f026efa4" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"gpg: keybox '/tmp/tmp6803gpkp/pubring.gpg' created\n", | |
"gpg: /tmp/tmp6803gpkp/trustdb.gpg: trustdb created\n", | |
"gpg: key AD5F235DF639B041: public key \"Launchpad PPA for Alessandro Strada\" imported\n", | |
"gpg: Total number processed: 1\n", | |
"gpg: imported: 1\n", | |
"Warning: apt-key output should not be parsed (stdout is not a terminal)\n", | |
"Selecting previously unselected package libfuse2:amd64.\n", | |
"(Reading database ... 19804 files and directories currently installed.)\n", | |
"Preparing to unpack .../libfuse2_2.9.7-1ubuntu1_amd64.deb ...\n", | |
"Unpacking libfuse2:amd64 (2.9.7-1ubuntu1) ...\n", | |
"Selecting previously unselected package fuse.\n", | |
"Preparing to unpack .../fuse_2.9.7-1ubuntu1_amd64.deb ...\n", | |
"Unpacking fuse (2.9.7-1ubuntu1) ...\n", | |
"Selecting previously unselected package google-drive-ocamlfuse.\n", | |
"Preparing to unpack .../google-drive-ocamlfuse_0.6.21-0ubuntu2_amd64.deb ...\n", | |
"Unpacking google-drive-ocamlfuse (0.6.21-0ubuntu2) ...\n", | |
"Setting up libfuse2:amd64 (2.9.7-1ubuntu1) ...\n", | |
"Processing triggers for libc-bin (2.26-0ubuntu2.1) ...\n", | |
"Setting up fuse (2.9.7-1ubuntu1) ...\n", | |
"Setting up google-drive-ocamlfuse (0.6.21-0ubuntu2) ...\n", | |
"Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force\n", | |
"··········\n", | |
"Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force\n", | |
"Please enter the verification code: Access token retrieved correctly.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Authorization\n", | |
"\n", | |
"!apt-get install -y -qq software-properties-common python-software-properties module-init-tools\n", | |
"!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null\n", | |
"!apt-get update -qq 2>&1 > /dev/null\n", | |
"!apt-get -y install -qq google-drive-ocamlfuse fuse\n", | |
"from google.colab import auth\n", | |
"auth.authenticate_user()\n", | |
"from oauth2client.client import GoogleCredentials\n", | |
"creds = GoogleCredentials.get_application_default()\n", | |
"import getpass\n", | |
"!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL\n", | |
"vcode = getpass.getpass()\n", | |
"!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 0, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
} | |
}, | |
"colab_type": "code", | |
"id": "ngHChl8X0KM3" | |
}, | |
"outputs": [], | |
"source": [ | |
"# Mount drive to \"drive/\" folder\n", | |
"\n", | |
"!mkdir -p drive\n", | |
"!google-drive-ocamlfuse drive" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 51 | |
}, | |
"colab_type": "code", | |
"executionInfo": { | |
"elapsed": 1893, | |
"status": "ok", | |
"timestamp": 1531773717622, | |
"user": { | |
"displayName": "Aakash Choubey", | |
"photoUrl": "//lh5.googleusercontent.com/-4a5kMwfwRgA/AAAAAAAAAAI/AAAAAAAAHFM/lfxVXWdVJPo/s50-c-k-no/photo.jpg", | |
"userId": "101767671542991329511" | |
}, | |
"user_tz": -330 | |
}, | |
"id": "fTFwR20v0Mt0", | |
"outputId": "e2a2db80-a364-48f5-c8a4-1aa5c8755d36" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['checking config.ipynb', 'MNIST', 'TextRecognition']\n", | |
"['TextRecognition', 'MNIST', 'checking config.ipynb']\n" | |
] | |
} | |
], | |
"source": [ | |
"# Change Working directory\n", | |
"\n", | |
"print(os.listdir(\"drive/Colab/\"))\n", | |
"os.chdir(\"drive/Colab/\")\n", | |
"print(os.listdir(\".\"))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text", | |
"id": "m0inWYjd1ARX" | |
}, | |
"source": [ | |
"## 3. Downloading a file from workspace" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 0, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
} | |
}, | |
"colab_type": "code", | |
"id": "LXB1NlQv1_d1" | |
}, | |
"outputs": [], | |
"source": [ | |
"from google.colab import files\n", | |
"files.download('Google Colab Helper.ipynb')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 0, | |
"metadata": { | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
} | |
}, | |
"colab_type": "code", | |
"id": "BkCJDaZd2nyX" | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"accelerator": "GPU", | |
"colab": { | |
"default_view": {}, | |
"name": "Google Colab Helper.ipynb", | |
"provenance": [], | |
"version": "0.3.2", | |
"views": {} | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment