为什么我的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)
实时预测代码中的整形是否会影响精度?我不明白为什么帧预测是不正确的,但单一的JPG和PNG图像预测是正确的。谢谢你的帮助!
发布于 2021-11-02 11:36:07
实时预测不正确的原因是由于预处理的原因。推理代码的预处理应始终与训练时使用的预处理相同。在您的实时预测代码中使用tf.keras.preprocessing.image.load_img,但是它需要图像路径来加载图像。这样,您就可以按名称tf.keras.preprocessing.image.load_img.“sample.png”保存每个帧,并将此路径传递给sample.png。这应该能解决这个问题。并使用调整大小的方法“双线性”,因为这是用来训练数据的。
https://stackoverflow.com/questions/69790781
复制相似问题