简体   繁体   中英

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 resolve the error ImportError: No module named 'nets' by using the following post: see here the answer from tsveti_iko .

Now I get the following error and don't know what to do:

NotFoundError (see above for traceback): Restoring from checkpoint failed. 
This is most likely due to a Variable name or other graph key that is missing from the checkpoint. 
Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

    Tensor name "resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/biases" 
not found in checkpoint files /home/resnet_v1_50.ckpt
         [[node save/RestoreV2 (defined at my_resnet.py:34)  = 
RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ...,
DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost
/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2
/tensor_names, save/RestoreV2/shape_and_slices)]]

This is the code I am using where I try to load the model:

import numpy as np
import tensorflow as tf
import resnet_v1

# Restore variables of resnet model
slim = tf.contrib.slim

# Paths
network_dir = "home/resnet_v1_50.ckpt"

# Image dimensions
in_width, in_height, in_channels = 224, 224, 3

# Placeholder
X = tf.placeholder(tf.float32, [None, in_width, in_height, in_channels])

# Define network graph
logits, activations = resnet_v1.resnet_v1_50(X, is_training=False)
prediction = tf.argmax(logits, 1)

with tf.Session() as sess:
    variables_to_restore = slim.get_variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)
    saver.restore(sess, network_dir) 

    # Restore variables
    variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)

    # Feed random image into resnet
    img = np.random.randn(1, in_width, in_height, in_channels)
    pred = sess.run(prediction, feed_dict={X:img})

Can anybody tell me, why it is not working? How do I have to change my code in order to make it run?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM