Skip to content Skip to sidebar Skip to footer

Tensorflow: Error While Loading Pre-trained Resnet Model

I want to use a pre-trained ResNet model from Tensorflow. I downloaded the code (resnet_v1.py) for the model and the checkpoint (resnet_v1_50.ckpt) file here. I already could resol

Solution 1:

Maybe you could use ResNet50 from tf.keras.applications?

According to the error, if you haven't altered the graph in any way, and this is your whole source code, it might be really, really hard to debug.

If you choose the sane tf.keras.applications.resnet50 way you could do it simply like this:

import tensorflow

in_width, in_height, in_channels = 224, 224, 3

pretrained_resnet = tensorflow.keras.applications.ResNet50(
    weights="imagenet",
    include_top=False,
    input_shape=(in_width, in_height, in_channels),
)

# You can freeze some layers if you want, depends on your task# Make "top" (last 3 layers below) whatever fits your task as well

model = tensorflow.keras.models.Sequential(
    [
        pretrained_resnet,
        tensorflow.keras.layers.Flatten(),
        tensorflow.keras.layers.Dense(1024, activation="relu"),
        tensorflow.keras.layers.Dense(10, activation="softmax"),
    ]
)

print(model.summary())

This approach would be the recommended now, especially in the light of upcoming Tensorflow 2.0, sanity and readability. BTW. This model is the same as the one provided by Tensorflow, it's transferred from it IIRC.

You can read more about tf.keras.applications in the linked documentation and in various blog posts like this one or other web resources.

How do I in Keras

Answer to questions from comments

  • How do I pass images to the network?: use model.predict(image) if you want to make a prediction, where image is np.array. Simple as that.

  • How do I access weights?: well, this one is more complicated... Just kidding, each layer has .get_weights() method which returns it's weights and biases, you can iterate over layers with for layer in model.layers(). You can get all weights at once using model.get_weights() as well.

All in all, you will learn Keras and be more productive in it than in Tensorflow in a shorter time than you can debug this issue. They have 30 seconds guide for a reason.

BTW. Tensorflow has Keras shipped by default and as such, Tensorflow's Keras flavor is part of Tensorflow (no matter how confusing this sounds). That's why I have used tensorflow in my example.

Solution using Tensorflow's Hub

Is seems you could load and fine-tune Resnet50 using Hub as described in this link.

Post a Comment for "Tensorflow: Error While Loading Pre-trained Resnet Model"