Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Bring your own dataset and retrain a TensorFlow model with OpenVINO™ toolkit

0.00/5 (No votes)
23 Jun 2022 1  
In this article we look at how to modify the OpenVINO™ Notebooks repo on GitHub, retrain the same model but with a different dataset.

Machine learning requires us to have existing data — not the data our application will use when we run it, but data to learn from. You need a lot of real data, in fact, the more the better. The more examples you provide, the better the computer should be able to learn. It’s the most crucial aspect that makes algorithm training possible and explains why machine learning became so popular in recent years.

The OpenVINO™ Notebooks repo on GitHub is a collection of ready-to-run Jupyter Notebooks, for learning and experimenting with the OpenVINO™ toolkit. The notebooks provide an introduction to OpenVINO™ basics and teach developers how to leverage our APIs for optimized deep learning inference. One of the examples is a notebook titled 301-tensorflow-training-openvino.ipynb, where you can train a model using TensorFlow*, and then run it in both the native TensorFlow and then using the OpenVINO™ Toolkit. It demonstrates an end-to-end deep learning training tutorial which borrows the open source code from the TensorFlow image classification tutorial, demonstrating how to train the model and then convert to OpenVINO™ Intermediate Representation (IR). It leverages the tf_flowers dataset which includes about 3,700 photos of flowers.

But, how to modify this notebook, retrain the same model but with a different dataset. Let’s say, instead of flowers, you want to use fruits dataset, retrain the same model and run inference on this new dataset. What all changes would be required?

Let’s pick an interesting new dataset Fruits-360. It’s a dataset of images containing fruits and vegetables. Unlike the name suggests, it’s not another dataset of 360 fruits, instead is a collection of 131 fruits and vegetables. This dataset was created by mounting these fruits and vegetables on a stick that was rotated 360 degrees, hence the name. Checkout the research paper to understand how these dataset images were post processed and prepared for training and testing purpose.

To set the baseline, launch the existing notebook with a sample from flowers dataset and run inference.

Python
# Pre-process the image and get it ready for inference

inp_img_url = "https://upload.wikimedia.org/wikipedia/commons/4/48/A_Close_Up_Photo_of_a_Dandelion.jpg"

file_path = Path("output")/Path("A_Close_Up_Photo_of_a_Dandelion.jpg")

input_image = pre_process_image(file_path)

# Run inference on the input image

res = compiled_model([input_image])[output_layer]

score = tf.nn.softmax(res[0])

# Show the results

image = Image.open(file_path)

plt.imshow(image)

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

Here is the classification result for this sample image of a flower:

Now, to use the Fruits-360 dataset, there would be some obvious modifications in the Jupyter notebook along with few technical adaptations. This table shows the first level changes required and you will notice that one of most notable differences is about the image size. Fruits-360 dataset consists of images with size 100x100 pixels.

Apply all the necessary code changes, run inference on the newly trained model and try classification results on any fruit or vegetable, for e.g. pick Red Potato or Cauliflower images below from the dataset.

To know about all the code changes and learn the steps which would be required to accomplish this, take up a free 20-minutes tutorial titled “BYOD- Retrain a Tensorflow Model Using Your Data Set”. It’s hosted on Intel Learning portal where there are tons of courses around AI that you can take. Just sign-up and there’s a complete curriculum for newcomers to OpenVINO™ which is 10 part learning plan, along with many newly launched training courses. Happy learning!

Resources

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here