為什么我的 TensorFlow 模型可以正確預測 JPG 和 PNG 影像,但錯誤地預測來自實時視頻流的幀?實時視頻流中的所有幀都被錯誤地歸類為 1 類。
嘗試:我從實時視頻流中保存了一個 PNG 影像。當我單獨保存PNG影像并對其進行測驗時,模型正確地對其進行了分類。當相似影像是實時視頻流中的幀時,它被錯誤地分類。PNG 影像和實時視頻流幀在視覺上具有相同的內容(背景、光照條件、攝像機角度等)。
我的模型結構:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
rescaling_2 (Rescaling) (None, 180, 180, 3) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 180, 180, 16) 448
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 90, 90, 16) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 90, 90, 32) 4640
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 45, 45, 32) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 45, 45, 64) 18496
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 22, 22, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 30976) 0
_________________________________________________________________
dense_2 (Dense) (None, 128) 3965056
_________________________________________________________________
dense_3 (Dense) (None, 3) 387
=================================================================
Total params: 3,989,027
Trainable params: 3,989,027
Non-trainable params: 0
_________________________________________________________________
Found 1068 files belonging to 3 classes.
實時預測代碼:(在Keertika的幫助下更新!)
def testModel(imageName):
import cv2
from PIL import Image
from tensorflow.keras.preprocessing import image_dataset_from_directory
batch_size = 32
img_height = 180
img_width = 180
img = keras.preprocessing.image.load_img(
imageName,
target_size=(img_height, img_width),
interpolation = "bilinear",
color_mode = 'rgb'
)
#preprocessing different here
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) #Create a batch
predictions = new_model.predict(img_array)
score = predictions[0]
classes = ['1', '2','3']
prediction = classes[np.argmax(score)]
print(
"This image {} most likely belongs to {} with a {:.2f} percent confidence."
.format(imageName, classes[np.argmax(score)], 100 * np.max(score))
)
return prediction
培訓代碼:
#image_dataset_from_directory returns a tf.data.Dataset that yields batches of images from
#the subdirectories class_a and class_b, together with labels 0 and 1.
from keras.preprocessing import image
directory_test = "/content/test"
tf.keras.utils.image_dataset_from_directory(
directory_test, labels='inferred', label_mode='int',
class_names=None, color_mode='rgb', batch_size=32, image_size=(256,
256), shuffle=True, seed=None, validation_split=None, subset=None,
interpolation='bilinear', follow_links=False,
crop_to_aspect_ratio=False
)
tf.keras.utils.image_dataset_from_directory(directory_test, labels='inferred')
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
directory_test,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
Is the accuracy being affected by the reshaping in the realtime prediction code? I do not understand why frame predictions are incorrect, but single JPG and PNG image predictions are correct. Thank you for any help!
uj5u.com熱心網友回復:
實時預測不正確的原因是因為預處理。推理代碼的預處理應始終與訓練時使用的預處理相同。在您的實時預測代碼中使用tf.keras.preprocessing.image.load_img但它需要影像路徑來加載影像。因此您可以按名稱“sample.png”保存每一幀并將此路徑傳遞給tf.keras.preprocessing.image.load_img。這應該可以解決問題。并使用調整大小方法“雙線性”,因為它用于訓練資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/346306.html
標籤:python tensorflow opencv keras video-streaming
