簡體   English   中英

嘗試用經過訓練的 model 預測圖像

[英]Trying to predict image with trained model

重新編輯:我用重新調整過的圖像訓練了 CNN model。 model.add(Conv2D(32,3,padding="same", activation="relu", input_shape=(224,224,3)))我現在正在嘗試在圖像上使用經過訓練的 model。 我的代碼是:

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)

將行更改為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

如果 model 有一個輸入,您應該將一個 4-D 數組傳遞給它,而不是 3-D arrays 的列表:

model.predict(resized[np.newaxis])

這通過在開頭添加一個軸來擴展數組以說明批量大小(即此處的批量大小為 1)。

另一方面,像您一樣提供輸入列表僅適用於具有多個輸入的模型,而不適用於一批中的多個圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM