

{"id":14855,"date":"2018-05-16T06:55:08","date_gmt":"2018-05-16T06:55:08","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=14855"},"modified":"2021-05-14T11:00:24","modified_gmt":"2021-05-14T05:30:24","slug":"tensorflow-image-recognition","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/","title":{"rendered":"TensorFlow Image Recognition Using &#8211; Python &amp; C++"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In this <strong>TensorFlow tutorial<\/strong>, we will be getting to know about the TensorFlow Image Recognition. Today in this tutorial of Tensorflow image recognition we will have a deep learning of Image Recognition using TensorFlow. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Moreover, in this tutorial, we will see the classification of the image using the inception v3 model and also look at how TensorFlow recognizes image using Python API and C++ API.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">It becomes difficult for a computer to differentiate between images as it doesn\u2019t function the way our brain does. The area of <strong>machine learning<\/strong> has shown tremendous progress to deal with this situation. <\/span><\/p>\n<p><span style=\"font-weight: 400\">As one of the solutions for this is TensorFlow Image Recognition. You have seen how a convolutional<strong> neural network model<\/strong> <strong>and<\/strong> <strong>deep learning<\/strong> can achieve recognizing visual related tasks.<\/span><\/p>\n<p>So, let&#8217;s study TensorFlow Image Recognition in detail.<\/p>\n<h2><span style=\"font-weight: 400\">TensorFlow Image Recognition\u00a0<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Now, many researchers have demonstrated progress in computer vision using the ImageNet- an academic benchmark for validating computer vision. There are many models for TensorFlow image recognition, for example, QuocNet, AlexNet, Inception. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Previously TensorFlow had launched BN-Inception-v2. Now, they have taken another step in releasing the code for Inception-v3, the new Image Recognition model in TensorFlow. <strong>Inception-v3<\/strong> is trained for large ImageNet using the data from 2012.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Now, the results from AlexNet classifying data can be seen below.<\/span><\/p>\n<div id=\"attachment_14859\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/AlexClassification.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14859\" class=\"wp-image-14859 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/AlexClassification.jpg\" alt=\"TensorFlow Image Recognition- Alex Classification\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/AlexClassification.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/AlexClassification-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/AlexClassification-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/AlexClassification-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/AlexClassification-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-14859\" class=\"wp-caption-text\">TensorFlow Image Recognition- Alex Classification<\/p><\/div>\n<p><span style=\"font-weight: 400\">Hence, to compare the models you try to analyze how these models fail to classify the images into the right categories called as \u2013 \u201ctop 5 error rate\u201d. Here in this TensorFlow tutorial, you will be learning about the Inception-v3 model, how it works and how it can be reused for other visual tasks.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">TensorFlow Image Recognition Using Python API <\/span><\/h2>\n<p><span style=\"font-weight: 400\">Use <strong>classify_image.py<\/strong> to download the trained model from tensorflow.org. Here, in\u00a0<\/span><span style=\"font-weight: 400\">TensorFlow Image Recognition Using Python API\u00a0<\/span><span style=\"font-weight: 400\">you will be needing 200M of hard disk space.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Now, run the following <strong>command<\/strong> for cloning the TensorFlow model&#8217;s repo from Github:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">cd models\/tutorials\/image\/imagenet\r\npython classify_image.py<\/pre>\n<p><span style=\"font-weight: 400\">Further, running the above will generate an image of a panda.<\/span><\/p>\n<div id=\"attachment_14860\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Panda.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14860\" class=\"wp-image-14860 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Panda.jpg\" alt=\"TensorFlow Image Recognition\" width=\"500\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Panda.jpg 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Panda-150x90.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Panda-300x180.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><p id=\"caption-attachment-14860\" class=\"wp-caption-text\">Image Recognition in TensorFlow Using Python API<\/p><\/div>\n<p><span style=\"font-weight: 400\">If the model is running properly then the following output should be achieved:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.88493)\r\nindri, indris, Indri indri, Indri brevicaudatus (score = 0.00878)\r\nlesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00317)\r\ncustard apple (score = 0.00149)\r\nearthstar (score = 0.00127)<\/pre>\n<h2>TensorFlow Image Recognition Using C++ API<\/h2>\n<p><span style=\"font-weight: 400\">Now, in\u00a0<\/span>TensorFlow Image Recognition Using C++ API y<span style=\"font-weight: 400\">ou can run the same Inception-v3 using the C++ API. For that you have to download an archive having <\/span><b><i>GraphDef<\/i><\/b><span style=\"font-weight: 400\"> running it from the root directory of TensorFlow library:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">curl -L \"https:\/\/storage.googleapis.com\/download.tensorflow.org\/models\/inception_v3_2016_08_28_frozen.pb.tar.gz\" |\r\n  tar -C tensorflow\/examples\/label_image\/data -xz<\/pre>\n<p>Next, we need to compile and run the C++ binary. Use the following <strong>command<\/strong> for that<\/p>\n<pre class=\"EnlighterJSRAW\">bazel build tensorflow\/examples\/label_image\/...<\/pre>\n<p>Further, you will get the following <strong>output.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">I tensorflow\/examples\/label_image\/main.cc:206] military uniform (653): 0.834306\r\nI tensorflow\/examples\/label_image\/main.cc:206] mortarboard (668): 0.0218692 I tensorflow\/examples\/label_image\/main.cc:206] academic gown (401): 0.0103579 I tensorflow\/examples\/label_image\/main.cc:206] pickelhaube (716): 0.00800814 I tensorflow\/examples\/label_image\/main.cc:206] bulletproof vest (466): 0.00535088<\/pre>\n<p>Basically, we will be getting the default image of <em>Admiral Hopper<\/em> and network correctly identifies her with the perfection of 0.8.<\/p>\n<div id=\"attachment_14861\" style=\"width: 503px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/grace_hopper.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14861\" class=\"wp-image-14861 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/grace_hopper.jpg\" alt=\"TensorFlow Image Recognition\" width=\"493\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/grace_hopper.jpg 493w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/grace_hopper-118x150.jpg 118w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/grace_hopper-236x300.jpg 236w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/a><p id=\"caption-attachment-14861\" class=\"wp-caption-text\">TensorFlow Image Recognition using C++ API- Admiral Hopper<\/p><\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400\">Since in TensorFlow Image Recognition, if you look inside of <strong>tensorflow\/examples\/label_image\/main.cc<\/strong> file you can get to know how this works. Now, we will walk step by step through the main functions used:<\/span><\/p>\n<p><span style=\"font-weight: 400\">The <em>Inception-v3 model<\/em> expects to get square images of size 299*299, which are the <\/span><b><i>input_height<\/i><\/b><span style=\"font-weight: 400\"> and <\/span><b><i>input_width<\/i><\/b><span style=\"font-weight: 400\"> flags. You also need to scale pixel values from integers (0-255) to floating point values that graph requires. <\/span><\/p>\n<p><span style=\"font-weight: 400\">We also need to control scaling using <\/span><b><i>input_mean<\/i><\/b><span style=\"font-weight: 400\"> and <\/span><b><i>input_std<\/i><\/b><span style=\"font-weight: 400\"> flags (<\/span><b><i>input_mean<\/i><\/b><span style=\"font-weight: 400\"> from each pixel divided by <\/span><b><i>input_std<\/i><\/b><span style=\"font-weight: 400\">).<\/span><\/p>\n<p><span style=\"font-weight: 400\">Some adjustments to the values have to be made to match the one used in the training process.<\/span><br \/>\n<span style=\"font-weight: 400\">Now, you will be seeing how they are applied to an image in <\/span><b><i>ReadTensorFromImageFile()<\/i><\/b><span style=\"font-weight: 400\">.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ Given an image file name, read in the data, try to decode it as an image,\r\n\/\/ resize it to the requested size, and then scale the values as desired.\r\nStatus ReadTensorFromImageFile(string file_name, const int input_height,\r\n                               const int input_width, const float input_mean,\r\n                               const float input_std,\r\n                               std::vector&lt;Tensor&gt;* out_tensors) {\r\n  tensorflow::GraphDefBuilder b;<\/pre>\n<p><span style=\"font-weight: 400\">Now, you need to create <\/span><b><i>GraphDefBuilder<\/i><\/b><span style=\"font-weight: 400\"> an object you can use to load or run the model.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">string input_name = \"file_reader\";\r\n  string output_name = \"normalized\";\r\n  tensorflow::Node* file_reader=tensorflow::ops::ReadFile(tensorflow::ops::Const(file_name, b.opts()),\r\n                                b.opts().WithName(input_name));<\/pre>\n<p><span style=\"font-weight: 400\">Now, create nodes for the small model that you want to run. Load, resize and scale the pixel values to get the input for the main model. The first node you create is a <\/span><b><i>Const<\/i><\/b><span style=\"font-weight: 400\"> op that holds a tensor with the file name of the image. That is then passed as the first input to the <\/span><b><i>ReadFile<\/i><\/b><span style=\"font-weight: 400\"> op. <\/span><\/p>\n<p><span style=\"font-weight: 400\">The <\/span><b><i>b.opts()<\/i><\/b><span style=\"font-weight: 400\"> argument ensures that the node is added to the definition in the <\/span><b><i>GraphDefBuilder<\/i><\/b><span style=\"font-weight: 400\">. Also name the <\/span><b><i>ReadFile<\/i><\/b><span style=\"font-weight: 400\"> operator by making <\/span><b><i>WithName()<\/i><\/b><span style=\"font-weight: 400\"> call to <\/span><b><i>b.opts().<\/i><\/b><\/p>\n<p><span style=\"font-weight: 400\">This gives a name to the node, which is not that necessary as an automatic name will be assigned, but it does make debugging a bit easier.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ Now try to figure out what kind of file it is and decode it.\r\n  const int wanted_channels = 3;\r\n  tensorflow::Node* image_reader;\r\n  if (tensorflow::StringPiece(file_name).ends_with(\".png\")) {\r\n    image_reader = tensorflow::ops::DecodePng(\r\n        file_reader,\r\n        b.opts().WithAttr(\"channels\", wanted_channels).WithName(\"png_reader\"));\r\n  } else {\r\n    \/\/ Assume if it's not a PNG then it must be a JPEG.\r\n    image_reader = tensorflow::ops::DecodeJpeg(\r\n        file_reader,\r\n        b.opts().WithAttr(\"channels\", wanted_channels).WithName(\"jpeg_reader\"));\r\n  }\r\n  \/\/ Now cast the image data to float so we can do normal math on it.\r\n  tensorflow::Node* float_caster = tensorflow::ops::Cast(\r\n      image_reader, tensorflow::DT_FLOAT, b.opts().WithName(\"float_caster\"));\r\n  \/\/ The convention for image ops in TensorFlow is that all images are expected\r\n  \/\/ to be in batches, so that they're four-dimensional arrays with indices of\r\n  \/\/ [batch, height, width, channel]. Because we only have a single image, we\r\n  \/\/ have to add a batch dimension of 1 to the start with ExpandDims().\r\n  tensorflow::Node* dims_expander = tensorflow::ops::ExpandDims(\r\n      float_caster, tensorflow::ops::Const(0, b.opts()), b.opts());\r\n  \/\/ Bilinearly resize the image to fit the required dimensions.\r\n  tensorflow::Node* resized = tensorflow::ops::ResizeBilinear(\r\n      dims_expander, tensorflow::ops::Const({input_height, input_width},\r\n                                            b.opts().WithName(\"size\")),\r\n      b.opts());\r\n  \/\/ Subtract the mean and divide by the scale.\r\n  tensorflow::ops::Div(\r\n      tensorflow::ops::Sub(\r\n          resized, tensorflow::ops::Const({input_mean}, b.opts()), b.opts()),\r\n      tensorflow::ops::Const({input_std}, b.opts()),\r\n      b.opts().WithName(output_name));<\/pre>\n<p><span style=\"font-weight: 400\">After this, keep adding more nodes, to decode the file data as an image, to cast the integers into floating point values, to resize it. At last, run the subtraction and division operations on the pixel values.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ This runs the GraphDef network definition that we've just constructed, and\r\n  \/\/ returns the results in the output tensor.\r\n  tensorflow::GraphDef graph;\r\n  TF_RETURN_IF_ERROR(b.ToGraphDef(&amp;graph));<\/pre>\n<p><span style=\"font-weight: 400\">Finally, you will have a model definition stored in the b variable. Then turn the definition into a full graph definition with the <\/span><b><i>ToGraphDef().<\/i><\/b><\/p>\n<pre class=\"EnlighterJSRAW\">std::unique_ptr&lt;tensorflow::Session&gt; session(\r\n      tensorflow::NewSession(tensorflow::SessionOptions()));\r\n  TF_RETURN_IF_ERROR(session-&gt;Create(graph));\r\n  TF_RETURN_IF_ERROR(session-&gt;Run({}, {output_name}, {}, out_tensors));\r\n  return Status::OK();<\/pre>\n<p><span style=\"font-weight: 400\">So, in this, you create a <\/span><b><i>tf.Session<\/i><\/b><span style=\"font-weight: 400\"> object, which is the interface to the graph, and runs it by specifying the node you want to get the output from, and where you want to put the output data.<\/span><\/p>\n<p><span style=\"font-weight: 400\">This TensorFlow Image Recognition process will give you a vector of <em>Tensor objects<\/em>, which will only be a single object in size. <\/span><\/p>\n<p><span style=\"font-weight: 400\">You can think of a Tensor as a <em>multi-dimensional array<\/em> as it holds a 299 pixels high, 299 pixels wide, 3 channel image as its float value. if you have your image-processing framework, you should be able to use that instead. But, remember to apply the same transformations.<\/span><\/p>\n<p><span style=\"font-weight: 400\">This was a simple example of creating a small TensorFlow graph in C++. You can see how we do that in the <\/span><b><i>LoadGraph().<\/i><\/b><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ Reads a model graph definition from disk, and creates a session object you\r\n\/\/ can use to run it.\r\nStatus LoadGraph(string graph_file_name,\r\n                 std::unique_ptr&lt;tensorflow::Session&gt;* session) {\r\n  tensorflow::GraphDef graph_def;\r\n  Status load_graph_status =\r\n      ReadBinaryProto(tensorflow::Env::Default(), graph_file_name, &amp;graph_def);\r\n  if (!load_graph_status.ok()) {\r\n    return tensorflow::errors::NotFound(\"Failed to load compute graph at '\",\r\n                                        graph_file_name, \"'\");\r\n  }<\/pre>\n<p><span style=\"font-weight: 400\">If you&#8217;ve seen the image loading code, a lot of the terms are already present. Instead of using a <\/span><b><i>GraphDefBuilder<\/i><\/b><span style=\"font-weight: 400\"> to give a <\/span><b><i>GraphDef<\/i><\/b><span style=\"font-weight: 400\"> object, you can load a <\/span><b><i>protobuf<\/i><\/b><span style=\"font-weight: 400\"> file that directly has <\/span><b><i>GraphDef<\/i><\/b><span style=\"font-weight: 400\">.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">session-&gt;reset(tensorflow::NewSession(tensorflow::SessionOptions()));\r\n  Status session_create_status = (*session)-&gt;Create(graph_def);\r\n  if (!session_create_status.ok()) {\r\n    return session_create_status;\r\n  }\r\n  return Status::OK();\r\n}<\/pre>\n<p><span style=\"font-weight: 400\">Then create a Session object from <\/span><b><i>GraphDef<\/i><\/b><span style=\"font-weight: 400\"> and then pass it to the caller so as to run it later.<\/span><br \/>\n<span style=\"font-weight: 400\">The <\/span><b><i>GetTopLabels()<\/i><\/b><span style=\"font-weight: 400\"> is similar to the image loading. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Just like the image loader, it will create a <\/span><b><i>GraphDefBuilder<\/i><\/b><span style=\"font-weight: 400\">, then add a couple of nodes to it, and run the graph to get a pair of tensors as the output.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ Analyzes the output of the Inception graph to retrieve the highest scores and\r\n\/\/ their positions in the tensor, which correspond to categories.\r\nStatus GetTopLabels(const std::vector&lt;Tensor&gt;&amp; outputs, int how_many_labels,\r\n                    Tensor* indices, Tensor* scores) {\r\n  tensorflow::GraphDefBuilder b;\r\n  string output_name = \"top_k\";\r\n  tensorflow::ops::TopK(tensorflow::ops::Const(outputs[0], b.opts()),\r\n                        how_many_labels, b.opts().WithName(output_name));\r\n  \/\/ This runs the GraphDef network definition that we've just constructed, and\r\n  \/\/ returns the results in the output tensors.\r\n  tensorflow::GraphDef graph;\r\n  TF_RETURN_IF_ERROR(b.ToGraphDef(&amp;graph));\r\n  std::unique_ptr&lt;tensorflow::Session&gt; session(\r\n      tensorflow::NewSession(tensorflow::SessionOptions()));\r\n  TF_RETURN_IF_ERROR(session-&gt;Create(graph));\r\n  \/\/ The TopK node returns two outputs, the scores and their original indices,\r\n  \/\/ so we have to append :0 and :1 to specify them both.\r\n  std::vector&lt;Tensor&gt; out_tensors;\r\n  TF_RETURN_IF_ERROR(session-&gt;Run({}, {output_name + \":0\", output_name + \":1\"},\r\n                                  {}, &amp;out_tensors));\r\n  *scores = out_tensors[0];\r\n  *indices = out_tensors[1];\r\n  return Status::OK();<\/pre>\n<p><span style=\"font-weight: 400\">The <\/span><b><i>PrintTopLabels()<\/i><\/b><span style=\"font-weight: 400\"> will take all the sorted results, and will then print them.<\/span><br \/>\n<span style=\"font-weight: 400\">In the end, <\/span><b><i>main()<\/i><\/b><span style=\"font-weight: 400\"> defines together all of these calls.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">int main(int argc, char* argv[]) {\r\n  \/\/ We need to call this to set up global state for TensorFlow.\r\n  tensorflow::port::InitMain(argv[0], &amp;argc, &amp;argv);\r\n  Status s = tensorflow::ParseCommandLineFlags(&amp;argc, argv);\r\n  if (!s.ok()) {\r\n    LOG(ERROR) &lt;&lt; \"Error parsing command line flags: \" &lt;&lt; s.ToString();\r\n    return -1;\r\n  }\r\n  \/\/ First we load and initialize the model.\r\n  std::unique_ptr&lt;tensorflow::Session&gt; session;\r\n  string graph_path = tensorflow::io::JoinPath(FLAGS_root_dir, FLAGS_graph);\r\n  Status load_graph_status = LoadGraph(graph_path, &amp;session);\r\n  if (!load_graph_status.ok()) {\r\n    LOG(ERROR) &lt;&lt; load_graph_status;\r\n    return -1;\r\n  }<\/pre>\n<p><span style=\"font-weight: 400\">Now, load the main graph.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ Get the image from disk as a float array of numbers, resized and normalized\r\n  \/\/ to the specifications the main graph expects.\r\n  std::vector&lt;Tensor&gt; resized_tensors;\r\n  string image_path = tensorflow::io::JoinPath(FLAGS_root_dir, FLAGS_image);\r\n  Status read_tensor_status = ReadTensorFromImageFile(\r\n      image_path, FLAGS_input_height, FLAGS_input_width, FLAGS_input_mean,\r\n      FLAGS_input_std, &amp;resized_tensors);\r\n  if (!read_tensor_status.ok()) {\r\n    LOG(ERROR) &lt;&lt; read_tensor_status;\r\n    return -1;\r\n  }\r\n  const Tensor&amp; resized_tensor = resized_tensors[0];<\/pre>\n<p><span style=\"font-weight: 400\">Further, load, resize and process the input.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ Actually run the image through the model.\r\n  std::vector&lt;Tensor&gt; outputs;\r\n  Status run_status = session-&gt;Run({ {FLAGS_input_layer, resized_tensor}},\r\n                                   {FLAGS_output_layer}, {}, &amp;outputs);\r\n  if (!run_status.ok()) {\r\n    LOG(ERROR) &lt;&lt; \"Running model failed: \" &lt;&lt; run_status;\r\n    return -1;\r\n  }<\/pre>\n<p><span style=\"font-weight: 400\">Next, in TensorFlow Image Recognition run the loaded graph.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/\/ This is for automated testing to make sure we get the expected result with\r\n  \/\/ the default settings. We know that label 866 (military uniform) should be\r\n  \/\/ the top label for the Admiral Hopper image.\r\n  if (FLAGS_self_test) {\r\n    bool expected_matches;\r\n    Status check_status = CheckTopLabel(outputs, 866, &amp;expected_matches);\r\n    if (!check_status.ok()) {\r\n      LOG(ERROR) &lt;&lt; \"Running check failed: \" &lt;&lt; check_status;\r\n      return -1;\r\n    }\r\n    if (!expected_matches) {\r\n      LOG(ERROR) &lt;&lt; \"Self-test failed!\";\r\n      return -1;\r\n    }\r\n  }<\/pre>\n<p><span style=\"font-weight: 400\">Hence, for testing purpose, you can check to make sure you get the output expected.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">  \/\/ Do something interesting with the results we've generated.\r\nStatus print_status = PrintTopLabels(outputs, FLAGS_labels);<\/pre>\n<p><span style=\"font-weight: 400\">At last, print the label.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">if (!print_status.ok()) {\r\n   LOG(ERROR) &lt;&lt; \"Running print failed: \" &lt;&lt; print_status;\r\n   return -1;\r\n }<\/pre>\n<p><span style=\"font-weight: 400\">Since, the error handling done here is using TensorFlow&#8217;s Status object, which is very convenient as it will let you know whether an error has occurred with the <\/span><b><i>ok()<\/i><\/b><span style=\"font-weight: 400\"> checker.<\/span><\/p>\n<p>So, this was all about TensorFlow Image Recognition using Python and C++ API. Hope you like our explanation.<\/p>\n<h2><span style=\"font-weight: 400\">Conclusion<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Hence, in this Tensorflow image recognition tutorial, we learned how to classify images using Inception V3 model, which lets us train our model with a higher accuracy than its predecessor. <\/span><\/p>\n<p><span style=\"font-weight: 400\">There are many more methods with which we can implement image recognition such as Alexnet, GoogleNet, VGGNet, etc.<\/span><\/p>\n<p><span style=\"font-weight: 400\"> Moreover, here we saw Image Recognition using Python API and C++ API. In addition, we discussed TensorFlow image recognition process by example also. Next, we will discuss CNN using TensorFlow. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Furthermore, if you have any query regarding TensorFlow Image Recognition, feel free to ask in the comment section.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this TensorFlow tutorial, we will be getting to know about the TensorFlow Image Recognition. Today in this tutorial of Tensorflow image recognition we will have a deep learning of Image Recognition using TensorFlow.&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":14862,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[270,2263,2265,4521,6468,6471,6666,6667,6668,9055,10362,14519,14547,14556,14608,15319,15320],"class_list":["post-14855","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tensorflow","tag-admiral-hopper","tag-c","tag-c-api","tag-face-recognition","tag-image-recognition","tag-image-recognition-using-tensorflow","tag-inception-model","tag-inception-v2-model","tag-inception-v3-model","tag-neural-networks-and-deep-learning","tag-python-api","tag-tensorflow","tag-tensorflow-face-recognition","tag-tensorflow-image-recognition","tag-tensorflow-tutorial","tag-v2-models","tag-v3"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>TensorFlow Image Recognition Using - Python &amp; C++ - DataFlair<\/title>\n<meta name=\"description\" content=\"TensorFlow Image Recognition,image Recognition using C++ API,Using Python API,Inception v3 model,inception v2 model,examples,image recognition in tensorFlow\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TensorFlow Image Recognition Using - Python &amp; C++ - DataFlair\" \/>\n<meta property=\"og:description\" content=\"TensorFlow Image Recognition,image Recognition using C++ API,Using Python API,Inception v3 model,inception v2 model,examples,image recognition in tensorFlow\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-16T06:55:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-14T05:30:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Image-Recognition-in-TensorFlow-01-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"TensorFlow Image Recognition Using - Python &amp; C++ - DataFlair","description":"TensorFlow Image Recognition,image Recognition using C++ API,Using Python API,Inception v3 model,inception v2 model,examples,image recognition in tensorFlow","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/","og_locale":"en_US","og_type":"article","og_title":"TensorFlow Image Recognition Using - Python &amp; C++ - DataFlair","og_description":"TensorFlow Image Recognition,image Recognition using C++ API,Using Python API,Inception v3 model,inception v2 model,examples,image recognition in tensorFlow","og_url":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-16T06:55:08+00:00","article_modified_time":"2021-05-14T05:30:24+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Image-Recognition-in-TensorFlow-01-2.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"TensorFlow Image Recognition Using &#8211; Python &amp; C++","datePublished":"2018-05-16T06:55:08+00:00","dateModified":"2021-05-14T05:30:24+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/"},"wordCount":1231,"commentCount":3,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Image-Recognition-in-TensorFlow-01-2.jpg","keywords":["Admiral hopper","C++","C++ API","face recognition","Image Recognition","Image Recognition using TensorFlow","inception model","inception v2 model","Inception v3 model","neural networks and deep learning","Python API","TensorFlow","TensorFlow Face recognition","TensorFlow Image Recognition","Tensorflow Tutorial","v2 models","v3"],"articleSection":["Tensorflow Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/","url":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/","name":"TensorFlow Image Recognition Using - Python &amp; C++ - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Image-Recognition-in-TensorFlow-01-2.jpg","datePublished":"2018-05-16T06:55:08+00:00","dateModified":"2021-05-14T05:30:24+00:00","description":"TensorFlow Image Recognition,image Recognition using C++ API,Using Python API,Inception v3 model,inception v2 model,examples,image recognition in tensorFlow","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Image-Recognition-in-TensorFlow-01-2.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Image-Recognition-in-TensorFlow-01-2.jpg","width":1200,"height":628,"caption":"TensorFlow Image Recognition"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/tensorflow-image-recognition\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Tensorflow Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/tensorflow\/"},{"@type":"ListItem","position":3,"name":"TensorFlow Image Recognition Using &#8211; Python &amp; C++"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=14855"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14855\/revisions"}],"predecessor-version":[{"id":95000,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14855\/revisions\/95000"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/14862"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=14855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=14855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=14855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}