Unity project that loads pretrained tensorflow pb model files and use them to predict
- In order to run the samples, your unity must be at least version 2017.3.0f3 and must be 64 bit
- The pre-trained tensorflow *.pb graph models in keras_codes should be trained using tensorflow 1.2 as the version of "Unity TensorFlow Plugin" that I was using has tensorflow 1.2 only.
- If your graph model is named cifar10.pb, it should be changed to cifar10.bytes and put under "Resources" folder (which is under Asset folder)
- If your graph model has the full path "Asset/Resources/tf_models/cifar10.bytes", you should call Resources.load("tf_models/cifar10") to load it.
Below is the sample code on how to use the Cifar10ImageClassifier to classify images stored in the "Resources/images/cifar10" folder:
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingTensorFlow;publicclassCifar10ImageClassifierDemo:MonoBehaviour{privateCifar10ImageClassifierclassifier=newCifar10ImageClassifier();// Use this for initializationvoidStart(){Debug.Log(TensorFlow.TFCore.Version);classifier.LoadModel("tf_models/cnn_cifar10");string[]image_names=newstring[9];intindex=0;for(inti=1;i<=3;++i){image_names[index++]="cat"+i;image_names[index++]="bird"+i;image_names[index++]="automobile"+i;}foreach(stringimage_nameinimage_names){Debug.Log(image_name);Texture2Dimg=Resources.Load<Texture2D>("images/cifar10/"+image_name);Debug.Log("Predicted: "+classifier.PredictLabel(img));}}// Update is called once per framevoidUpdate(){}}- Cifar10ImageClassifier takes image having width = 32, height = 32, channels = 3
- In order for the Cifar10ImageClassifier to read the images in the "Resources/images/cifar10" folder, you make make the images readable in their Import Settings.