Kaggle/learntools

Deep Learning EX3 Tensorflow Shape Error

tyler-wel opened this issue · 1 comments

Notebook path & Example:

notebooks/deep_learning/raw/ex3_programming_tf_and_keras.ipynb
2) Run an Example Model

Failing code

from IPython.display import Image, display
from learntools.deep_learning.decode_predictions import decode_predictions
import numpy as np
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing.image import load_img, img_to_array


image_size = 224

def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size):
    imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths]
    img_array = np.array([img_to_array(img) for img in imgs])
    output = preprocess_input(img_array)
    return(output)


my_model = ResNet50(weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
test_data = read_and_prep_images(img_paths)
preds = my_model.predict(test_data)

most_likely_labels = decode_predictions(preds, top=3)

Error

ValueError: Shapes (1, 1, 256, 512) and (512, 128, 1, 1) are incompatible

image

Supposed fix:

PR

While following along with the example locally, I got the same error. Doing a little google and trial and error, I finally got it to work with the following import while working with the dog files and weights:

from keras.applications.resnet50 import ResNet50

my_model = ResNet50(weights='./pre-trained/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')

Please note that I'm just a beginner with python and tensorflow, so if there is a better fix, please let me know!

Fixed with re-trained weights