Embed TensorBoard into your Google Colab window when using Google Drive with 2 lines of code
Google Colab provides an embedded Jupyter Notebook which can integrate with your Google Drive to access files. Its ace-card is the free use of GPU (graphical processing unit) which would otherwise be a very costly purchase if used on your local computer. It provides a perfect environment for all your machine learning / deep learning projects.
Tensorboard is the “eyes and ears” of your neural network model that has been trained. It graphs the accuracy and the loss of your model that has undergone training thus allowing you to visualize the results in an accuracy graph and a loss graph.
We shall use YouTuber sendex’s DogvsCat tutorial on Tensorboard model analysis as a worked example: https://pythonprogramming.net/tensorboard-analysis-deep-learning-python-tensorflow-keras/
Pre-requisites:
1. Tutorials part 1 to part 5 on Introduction to Deep Learning by sendex
2. Google Drive access (requires a Google account)
Let’s get started:
Open Google Colab and mount your Google Drive using the folder icon on the left-hand side, then click on the last folder to mount your Google Drive:
Click on ‘CONNECT TO GOOGLE DRIVE’
Create a ‘logfiles’ folder and within that create a ‘catvsdoglogs’ folder:
Click on the ‘logfiles’ directory, right-mouse click, and select ‘Copy path’:
The first code cell contains the import statements and the global variables. Paste the clipboard contents into a variable called ‘pathoflogsdir’ and equate the first parameter of TensorBoard; log_dir to pathoflogsdir + ‘catvsdoglogs/{}’
Execute the code cell to ensure the NAME variable is correctly shown:
Click on the datasets folder, right-click and select ‘Copy path’:
In the second code cell paste the clipboard contents and assign them to a variable called ‘path’. Run the code cell to ensure both variables X and y are of type numpy.ndarray.
NOTE: use the np.asarray function to convert y from a list to a numpy array:
On the final line of the third code, cell ensure the final parameter shows
callbacks=[tensorboard]
Copy the path of the ‘catvsdoglogs’ folder again from the left-hand side:
In the final code cell, the first line should be %load_ext tensorboard. The second line should show %tensorboard –logdir= {paste text from clipboard}:
Scroll back to the first code cell that was executed and highlight the name that was shown and copy the name variable to the clipboard:
Paste (Ctrl + V) the clipboard contents and insert double-quote to close off the string value:
Click on ‘Runtime’ and click on ‘Change runtime type’:
Click on the down arrow and select ‘GPU’ from the drop-down list and click on ‘SAVE’:
Click on Runtime and click on ‘Run all’:
This will display the tensorboard results in the current browser tab:
Happy Colab coding and analyzing the results in tensorboard! If this article helped you please leave a clap.