Anwesh
Anwesh

Reputation: 23

How to manually load pretrained model if I can't download it using TensorFlow

I am trying to download the VGG19 model via TensorFlow

base_model = VGG19(input_shape = [256,256,3],
                    include_top = False,
                    weights = 'imagenet')

However the download always gets stuck before it finishes downloading. I've tried with different models too like InceptionV3 and the same happens there.

Fortunately, the prompt makes the link available where the model can be downloaded manually

Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg19/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5
19546112/80134624 [======>.......................] - ETA: 11s

After downloading the model from the given link I try to import the model using

base_model = load_model('vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5')

but I get this error

ValueError: No model found in config file.

How do I load in the downloaded .h5 model manually?

Upvotes: 1

Views: 2632

Answers (2)

Jimmy Wang
Jimmy Wang

Reputation: 67

Got the same problem when learning on tensorflow tutorial, too.

Transfer learning and fine-tuning: Create the base model from the pre-trained convnets

# Create the base model from the pre-trained model MobileNet V2
IMG_SIZE = (160, 160)
IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE, include_top=False, weights=None)

# load model weights manually
weights = 'mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_160_no_top.h5'
base_model.load_weights(weights)

I tried download the model.h5, and load manually. It works.

`

Upvotes: 0

Djinn
Djinn

Reputation: 856

You're using load_model on weights, instead of a model. You need to have a defined model first, then load the weights.

weights = "path/to/weights"
model = VGG19  # the defined model
model.load_weights(weights)  # the weights

Upvotes: 1

Related Questions