tensorflow 尝试使用训练模型预测图像

eoigrqb6  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(181)

RE-EDITTED:我用重新配置的图像训练了一个CNN模型。model.add(Conv2D(32,3,padding="same", activation="relu", input_shape=(224,224,3)))我现在尝试在图像上使用这个训练过的模型。

import tensorflow as tf
import cv2
import keras
from keras.models import load_model
size=224
model = tf.keras.models.load_model('/home/philip/QAME696/savedmodel/CNNModel')
image = cv2.imread("/home/philip/QAME696/rain.png")
resized=cv2.resize(image,(size,size)) # keras.util.load_image
prediction=model.predict([resized])
# Check its architecture
prediction

我得到这个错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (32, 224, 3)

将line更改为model.predict(resized[np.newaxis])后,得到以下内容:

2023-01-20 21:27:47.896204: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/ros/melodic/share/euslisp/jskeus/eus//Linux64/lib:/home/philip/svp2_ws/devel/lib:/home/philip/coverage_ws/devel/lib:/opt/ros/melodic/lib
2023-01-20 21:27:47.896269: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2023-01-20 21:28:02.830662: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/philip/.local/lib/python3.6/site-packages/cv2/../../lib64:/opt/ros/melodic/share/euslisp/jskeus/eus//Linux64/lib:/home/philip/svp2_ws/devel/lib:/home/philip/coverage_ws/devel/lib:/opt/ros/melodic/lib
2023-01-20 21:28:02.830730: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)
2023-01-20 21:28:02.830782: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (philip-Blade): /proc/driver/nvidia/version does not exist
2023-01-20 21:28:02.832157: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-01-20 21:28:20.927716: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)

我必须重建tensorflow才能让这个工作?并安装cudart

jchrr9hc

jchrr9hc1#

如果模型只有一个输入,则应向其传递一个四维数组,而不是一个三维数组列表:

model.predict(resized[np.newaxis])

这通过在开始处添加轴以考虑批量大小(即,批量大小在此为1)来扩展阵列。
另一方面,像您所做的那样给出输入列表仅适用于具有多个输入的模型,而不适用于批处理中的多个图像。

相关问题