0

I'm trying to use a pre-trained tensorflow model to classify an image.

I downloaded the efficientnet model from tensorflow hub.

  1. The python code loads the model from the .pb file.

  2. It then loads a sample image, resizes the image to 224x224, squishes the rgb values to [0,1] and adds another dimension to make it 4d (collection of images) as the model expects.

  3. Use col_x for inference. The final input shape that is given to the model is (1, 224, 224, 3).

import os

import tensorflow as tf
from tensorflow import keras
print(tf.version.VERSION)

path = os.path.join(os.getcwd(), 'efficientnet')
model = keras.models.load_model(path)

from matplotlib import pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
import numpy as np

img = Image.open("data/zebra.jpg")
img = img.resize((224, 224), Image.ANTIALIAS)
x = tf.keras.preprocessing.image.img_to_array(img)
plt.imshow(img)
plt.show()

norm_x = x / 255
col_x = norm_x[np.newaxis,...]
plt.imshow(col_x[0])
plt.show()
model(col_x)

But I get this error:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 926, in conv2d
    "dilations", dilations)
tensorflow.python.eager.core._FallbackException: Expecting int64_t value for attr strides, got numpy.int32

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 968, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 719, in call
    convert_kwargs_to_constants=base_layer_utils.call_context().saving)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 888, in _run_internal_graph
    output_tensors = layer(computed_tensors, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 968, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\layers\convolutional.py", line 207, in call
    outputs = self._convolution_op(inputs, self.kernel)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1106, in __call__
    return self.conv_op(inp, filter)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 638, in __call__
    return self.call(inp, filter)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 237, in __call__
    name=self.name)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 2014, in conv2d
    name=name)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 933, in conv2d
    data_format=data_format, dilations=dilations, name=name, ctx=_ctx)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 1022, in conv2d_eager_fallback
    ctx=ctx, name=name)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above. [Op:Conv2D]
Emoticon
  • 26
  • 1
  • 5

1 Answers1

0

I was able could reproduce your error, Please the change the last line. to either predict or evaluate or any function that you would want to infer.

model.predict(col_x)

You are using model(col_x) , which is sending the image directly to the model as an class attribute.

Also, for the other error I do not think your system is using the GPU if available, please install the correct version of Tensorflow and CUDA for that purpose.

Visit this answer Which TensorFlow and CUDA version combinations are compatible? for correcting it.

Cheers.

DragonsCanDance
  • 441
  • 4
  • 14
  • Thanks! It worked. I had a feeling there must be a predict function attached to the model object. But i was somewhat following a [tensorflow tutorial](https://www.tensorflow.org/guide/saved_model). Can you elaborate on what is the difference between model(x) and model.predict(x) is? What does it mean to send input to the model as "a class attribute"? Also for the second error. I straight up didn't have cuDNN installed as the error said. It worked after I installed it. I already had the correct CUDA version. – Emoticon Jun 23 '20 at 16:40