avatardatatec.studio

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2122

Abstract

00/1*mP-YMR0tL7umuBZY7It_Zg.png"><figcaption>Rename the Project Name</figcaption></figure><p id="a5a0"><b>Step 4: Use GPU in Colab.</b></p><p id="58ac">Click on Colab Menu “Runtime”->”change runtime type”, choose GPU as Hardware accelerator, then click on “Save”.</p><figure id="1420"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*c7UZA_lLCCC0BSoV2iWzNg.png"><figcaption>GPU</figcaption></figure><p id="fcb2"><b>Step 5: Copy python code to Colab.</b></p><p id="0480">Copy the example code for image classification from <a href="https://medium.com/@datatec.studio/prepare-for-tensorflow-developer-certificate-ep2-run-image-classification-example-on-pycharm-ab7a8eeb6389">previous post</a> to CoLab notebook. You can find my <a href="https://colab.research.google.com/drive/1WfnyOCx5FISvO_8srv87Fy-MgQSxtBKF?usp=sharing">shared Colab here</a>.</p><p id="8628">Compare to the python code from previous post, only following line is not needed to be copied into Colab notebook:</p><div id="54cc"><pre><span class="hljs-keyword">if</span> name == <span class="hljs-string">'main'</span>:</pre></div><p id="8ed4"><b>Step 6: Run the code on Colab.</b></p><p id="86ed">After run the notebook on Colab, we see that the model has been trained and improved, the training and validation accuracy is much more better after optimisation.</p><p id="eb0a">Following chart shows the Training and Validation Accuracy and Loss after the used <i>data augmentation</i> and add <i>dropout </i>to fight overfitting.</p><p id="d867">These value are improved compare to that when we not use <i>data augmentation</i> and <i>dropout, </i>which was tested in previous post and shown below.</p><figure id="6bed"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Oo0qQnp6v-rghUk4F1Dfgw.png"><figcaption>After Optimisation</figcaption></figure><figure id="9c86"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*u7uXUWti-6hTwEAgJ4OGtA.png"><figcaption>Before Optimisation</figcaption></figure><p id="1326"><b>Step 7: Predict on new data</b></p><p id="f23f">Additional to example from previous

Options

post, we will complete the example from <a href="https://www.tensorflow.org/tutorials/images/classification">tensorflow website</a> and use the trained model to predict on new data.</p><p id="2db5">Following code will be added in the notebook and we get the result that the given picture was 99.35 percent confidence like what we want.</p><div id="f8d9"><pre><span class="hljs-comment"># Predict on new data</span> sunflower_url = <span class="hljs-string">"https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg"</span> sunflower_path = tf.keras.utils.get_file(<span class="hljs-string">'Red_sunflower'</span>, origin=sunflower_url)

img = tf.keras.utils.load_img( sunflower_path, target_size=(img_height, img_width) ) img_array = tf.keras.utils.img_to_array(img) img_array = tf.expand_dims(img_array, <span class="hljs-number">0</span>) <span class="hljs-comment"># Create a batch</span>

predictions = model.predict(img_array) score = tf.nn.softmax(predictions[<span class="hljs-number">0</span>])

<span class="hljs-built_in">print</span>( <span class="hljs-string">"This image most likely belongs to {} with a {:.2f} percent confidence."</span> .<span class="hljs-built_in">format</span>(class_names[np.argmax(score)], <span class="hljs-number">100</span> * np.<span class="hljs-built_in">max</span>(score)) )</pre></div><figure id="6a4f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FkNZvtfAi07c6RYv0fE8Cw.png"><figcaption>Result of Predict</figcaption></figure><p id="1ca7"><b>Summary</b></p><p id="180b">As we just wanna get hand dirty with Colab and make sure that we know how to run model on it, the test is from this point of view successful.</p><p id="c0c4">Also the step to choose GPU as runtime type brings performance during training model.</p><p id="6336">Maybe one day i will figure out how to export the trained model in h5 format and reuse it in PyCharm. I will then update this page when i get it work.</p><p id="a940">If you find this post is helpful, please give a like or keep following. See you next time.</p></article></body>

7 Steps to predict Image with TensorFlow on Google Colab

Cloud

In the previous post, i have introduced how to run Image Classification example with TensorFlow in PyCharm. Now i am trying summarise how to run the same code on Google Colab, which is a hosted Jupyter notebook environment that is free to use.

The goal is just to get the experience about how to use Colab to run the TensorFlow Code. As in the TensorFlow Development Certification Exam, we will use Colab to train the model.

TL;DR

One can use following steps to train model with TensorFlow on Google Colab:

Step 1: Login Colab. Step 2: Create new notebook Step 3: Rename the project name Step 4: Use GPU in Colab. Step 5: Copy python code to Colab. Step 6: Run the code on Colab. Step 7: Predict on new data Summary

Step 1: Login Colab.

Open Google Colab webpage and sign in with your google account. https://colab.research.google.com/

Step 2: Create new notebook

Just click on “New notebook” on the bottom right, when the popup window is shown by loading the Colab webpage.

First page of Colab: New notebook Button

Step 3: Rename the project name

A new webpage will be created and shown under colab driver.

Please rename the project name (.ipynb) from top left of the webpage.

Rename the Project Name

Step 4: Use GPU in Colab.

Click on Colab Menu “Runtime”->”change runtime type”, choose GPU as Hardware accelerator, then click on “Save”.

GPU

Step 5: Copy python code to Colab.

Copy the example code for image classification from previous post to CoLab notebook. You can find my shared Colab here.

Compare to the python code from previous post, only following line is not needed to be copied into Colab notebook:

if __name__ == '__main__':

Step 6: Run the code on Colab.

After run the notebook on Colab, we see that the model has been trained and improved, the training and validation accuracy is much more better after optimisation.

Following chart shows the Training and Validation Accuracy and Loss after the used data augmentation and add dropout to fight overfitting.

These value are improved compare to that when we not use data augmentation and dropout, which was tested in previous post and shown below.

After Optimisation
Before Optimisation

Step 7: Predict on new data

Additional to example from previous post, we will complete the example from tensorflow website and use the trained model to predict on new data.

Following code will be added in the notebook and we get the result that the given picture was 99.35 percent confidence like what we want.

# Predict on new data
sunflower_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg"
sunflower_path = tf.keras.utils.get_file('Red_sunflower', origin=sunflower_url)

img = tf.keras.utils.load_img(
    sunflower_path, target_size=(img_height, img_width)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch

predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])

print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
)
Result of Predict

Summary

As we just wanna get hand dirty with Colab and make sure that we know how to run model on it, the test is from this point of view successful.

Also the step to choose GPU as runtime type brings performance during training model.

Maybe one day i will figure out how to export the trained model in h5 format and reuse it in PyCharm. I will then update this page when i get it work.

If you find this post is helpful, please give a like or keep following. See you next time.

Google Colab
Google Colaboratory
TensorFlow
Image Classification
Certificate
Recommended from ReadMedium