— All the images (plots) are generated and modified by the Author. Today we have the super-effective technique as where we can use a pre-trained model by to classify any image of classified visual objects in the world of computer vision. Transfer Learning Google AI Transfer learning is a machine learning method which utilizes a pre-trained neural network. Here, the image recognition model called Inception-v3 consists of two parts:- part with a convolutional neural network. Feature extraction part with fully-connected and softmax layers. Classification Inception-v3 is a pre-trained convolutional neural network model that is 48 layers deep. It is a version of the network already trained on more than a million images from the database. It is the third edition of Inception CNN model by Google, originally instigated during the . ImageNet ImageNet Recognition Challenge This pre-trained network can classify images into 1000 object categories, such as keyboard, mouse, pencil, and many animals. As a result, the network has learned rich feature representations for a wide range of images. The network has an image input size of 299-by-299. The model extracts general features from input images in the first part and classifies them based on those features in the second part. is a widely-used image recognition model that has been shown to attain greater than 78.1% accuracy on the ImageNet dataset and around 93.9% accuracy in top 5 results. The model is the culmination of many ideas introduced by multiple researchers over the past years. It is based on the original paper: “ ” by Szegedy, et. al. Inception v3 Rethinking the Inception Architecture for Computer Vision More information about the Inception architecture can be found . here In Transfer Learning, when you build a new model to classify your original dataset, you reuse the feature extraction part and re-train the classification part with your dataset. Since you don’t have to train the feature extraction part (which is the most complex part of the model), you can train the model with less computational resources and training time. In this article, we will just use the Inception v3 model to predict some images and fetch the top 5 predicted classes for the same. Let’s begin. We are using Tensorflow v2.x Import Data os numpy np PIL Image imageio imread tensorflow.compat.v1 tf tf.disable_v2_behavior() tf_slim slim tf_slim.nets inception tf_slim slim cv2 matplotlib.pyplot plt import import as from import from import import as import as from import import as import import as Data Loading Setup all initial variables with default file locations and respective values. ckpt_path = images_path = img_width = img_height = batch_size = batch_shape = [batch_size, img_height, img_width, ] num_classes = predict_output = [] class_names_path = open(class_names_path) f: class_names = f.readlines() "/kaggle/input/inception_v3.ckpt" "/kaggle/input/animals/*" 299 299 16 3 1001 "/kaggle/input/imagenet_class_names.txt" with as Create Inception v3 model X = tf.placeholder(tf.float32, shape=batch_shape) slim.arg_scope(inception.inception_v3_arg_scope()): logits, end_points = inception.inception_v3( X, num_classes=num_classes, is_training= ) predictions = end_points[ ] saver = tf.train.Saver(slim.get_model_variables()) with False "Predictions" Define a function for loading images and resizing for sending to model for evaluation in RGB mode. batch_shape images = np.zeros(batch_shape) filenames = [] idx = batch_size = batch_shape[ ] files = tf.gfile.Glob(input_dir)[: ] files.sort() filepath files: tf.gfile.Open(filepath, ) f: imgRaw = np.array(Image.fromarray(imread(f, as_gray= , pilmode= )).resize(( , ))).astype(np.float) / images[idx, :, :, :] = imgRaw * - filenames.append(os.path.basename(filepath)) idx += idx == batch_size: filenames, images filenames = [] images = np.zeros(batch_shape) idx = idx > : filenames, images : def load_images (input_dir) global 0 0 20 for in with "rb" as False "RGB" 299 299 255.0 2.0 1.0 1 if yield 0 if 0 yield Load Pre-Trained Model session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=ckpt_path, master= ) '' Classify Images using Model tf.train.MonitoredSession(session_creator=session_creator) sess: filenames, images load_images(images_path): labels = sess.run(predictions, feed_dict={X: images}) filename, label, image zip(filenames, labels, images): predict_output.append([filename, label, image]) with as for in for in Predictions We will use some images from the dataset from Kaggle to declare the model predictions. Animals-10 x predict_output: out_list = list(x[ ]) topPredict = sorted(range(len(out_list)), key= i: out_list[i], reverse= )[: ] plt.imshow((((x[ ]+ )/ )* ).astype(int)) plt.show() print( ,x[ ]) print( ) p topPredict: print(class_names[p ].strip()) for in 1 lambda True 5 2 1 2 255 "Filename:" 0 "Displaying the top 5 Predictions for above image:" for in -1 At length, all of the classes are classified spot on, and we can also see that the top 5 similar classes as predicted by the model are pretty good and precise. I hope this post has been useful. I appreciate feedback and constructive criticism. If you want to talk about this article or other related topics, you can drop me a text here or on . LinkedIn Also published at https://towardsdatascience.com/classify-any-object-using-pre-trained-cnn-model-77437d61e05f