Error when trying to convert CNN model input image into Numpy array and read it

Asked 2 years ago, Updated 2 years ago, 100 views

In order to visualize the CNN model with fine tuning of vgg16 as a heat map, I created a program referring to Deep Learning with Python and Keras, page 182 but when I try to convert the input image into a Numpy array, I get an error.

Below are some excerpts from the program and the error content.Please reply.

code (excerpt):

img=image.load_img(img_path, target_size=(img_width, img_height))
x=image.img_to_array(img)
x=np.expand_dims(x,axis=0)
x = preprocess_input(x)
pres = model.predict(x)

error messages:

---->1preds=model.predict(x)

~\Anaconda3\lib\site-packages\keras\engine\training.py input (self, x, batch_size, verbose, steps)
   1167 batch_size = batch_size,
   1168 verbose = verbose,
->1169 steps=steps)
   1170 
   1171 def train_on_batch(self, x, y,

~\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in predict_loop (model, f, ins, batch_size, verbose, steps)
    292 ins_batch[i] = ins_batch[i].toarray()
    293 
-->294batch_outs=f(ins_batch)
    295batch_outs=to_list(batch_outs)
    296 if batch_index == 0:

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
   2713 return self._legacy_call(inputs)
   2714 
->2715 return self._call(inputs)
   2716 else:
   2717 if py_any(is_tensor(x) for x in inputs):

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in_call(self, inputs)
   2673 fetched=self._callable_fn(*array_vals, run_metadata=self.run_metadata)
   2674 else:
->2675 fetched=self._callable_fn(*array_vals)
   2676 return fetched [:len(self.outputs)]
   2677 

~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in__call__(self, *args, **kwargs)
   1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
   1457self._handle, args,
->1458 run_metadata_ptr)
   1459 if run_metadata:
   1460 proto_data=tf_session.TF_GetBuffer(run_metadata_ptr)

UnknownError: 2 root error(s) found.
  (0) Unknown: Failed to get convolution algorithm. This is probably because cuDN failed to initialize, so looking to see if a warning log message was printed above.
     [[{{node block1_conv1_1/convolution}}]]
  (1) Unknown: Failed to get convolution algorithm. This is probably because cuDN failed to initialize, so looking to see if a warning log message was printed above.
     [[{{node block1_conv1_1/convolution}}]]
     [[sequential_2/dense_4/Softmax/_607]]
0 successful operations.
0 derived errors ignored.

python python3 keras

2022-09-30 17:56

2 Answers

There is a high possibility that you are using anaconda and the cuda, cudnn relationship is not set up well.Check the environment variable settings in the operating system after verifying that your tensorflow, cuda, and cudnn versions are compatible.

The code below worked fine in my environment.

Environment

  • ubuntu 20.04
  • tensorflow 2.8.0
  • cuda 11.2
  • cudn 8.1
from keras_preprocessing import image
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
import numpy as np

model=VGG16(weights="imagenet")
img_path="img.jpg"
img=image.load_img(img_path, target_size=(224,224))
x=image.img_to_array(img)
x=np.expand_dims(x,axis=0)
x = preprocess_input(x)
pres = model.predict(x)
print(preds)


2022-09-30 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.