exAres
exAres

Reputation: 4926

How to load pre-trained tensorflow model named inception by Google?

I have downloaded a tensorflow checkpoint model named inception_resnet_v2_2016_08_30.ckpt.

Do I need to create a graph (with all the variables) that were used when this checkpoint was created?

How do I make use of this model?

Upvotes: 2

Views: 11955

Answers (3)

Pradyumna TK
Pradyumna TK

Reputation: 143

Another way of loading a pre-trained Imagenet model is

ResNet50

import tensorflow as tf
from tensorflow.keras.applications.resnet50 import ResNet50
model = ResNet50()
model.summary()

InceptionV3

iport tensorflow as tf
from tensorflow.keras.applications.inception_v3 import InceptionV3
model = InceptionV3()
model.summary()

You can check a detailed explanation related to this here

Upvotes: 2

Pramod Patil
Pramod Patil

Reputation: 827

First of you have get the network architecture in memory. You can get the network architecture from here

Once you have this program with you, use the following approach to use the model:

from inception_resnet_v2 import inception_resnet_v2, inception_resnet_v2_arg_scope

height = 299
width = 299
channels = 3

X = tf.placeholder(tf.float32, shape=[None, height, width, channels])
with slim.arg_scope(inception_resnet_v2_arg_scope()):
     logits, end_points = inception_resnet_v2(X, num_classes=1001,is_training=False)

With this you have all the network in memory, Now you can initialize the network with checkpoint file(ckpt) by using tf.train.saver:

saver = tf.train.Saver()
sess = tf.Session()
saver.restore(sess, "/home/pramod/Downloads/inception_resnet_v2_2016_08_30.ckpt")

If you want to do bottle feature extraction, its simple like lets say you want to get features from last layer, then simply you have to declare predictions = end_points["Logits"] If you want to get it for other intermediate layer, you can get those names from the above program inception_resnet_v2.py

After that you can call: output = sess.run(predictions, feed_dict={X:batch_images})

Upvotes: 4

WY Hsu
WY Hsu

Reputation: 1905

Do I need to create a graph (with all the variables) that were used when this checkpoint was created?

No, you don't.

As for how to use checkpoint file (cpkt file)

1.This article (TensorFlow-Slim image classification library) tells you how to train your model from scratch

2.The following is an example code from google blog

import numpy as np
import os
import tensorflow as tf
import urllib2

from datasets import imagenet
from nets import inception
from preprocessing import inception_preprocessing

slim = tf.contrib.slim

batch_size = 3
image_size = inception.inception_v3.default_image_size

checkpoints_dir = '/root/code/model'
checkpoints_filename = 'inception_resnet_v2_2016_08_30.ckpt'
model_name = 'InceptionResnetV2'
sess = tf.InteractiveSession()
graph = tf.Graph()
graph.as_default()

def classify_from_url(url):
    image_string = urllib2.urlopen(url).read()
    image = tf.image.decode_jpeg(image_string, channels=3)
    processed_image = inception_preprocessing.preprocess_image(image,     image_size, image_size, is_training=False)
processed_images  = tf.expand_dims(processed_image, 0)

# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
    logits, _ = inception.inception_resnet_v2(processed_images, num_classes=1001, is_training=False)
probabilities = tf.nn.softmax(logits)

init_fn = slim.assign_from_checkpoint_fn(
    os.path.join(checkpoints_dir, checkpoints_filename),
    slim.get_model_variables(model_name))

init_fn(sess)
np_image, probabilities = sess.run([image, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]

plt.figure()
plt.imshow(np_image.astype(np.uint8))
plt.axis('off')
plt.show()

names = imagenet.create_readable_names_for_imagenet_labels()
for i in range(5):
    index = sorted_inds[i]
    print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index]))

Upvotes: 2

Related Questions