gpt4 book ai didi

keras - 为什么我不能将图像数据拟合到我的 CNN 中的 model.predict 中?

转载 作者:行者123 更新时间:2023-12-04 16:25:46 27 4
gpt4 key购买 nike

我已经构建并训练了我的 CNN 模型,我想对其进行测试。我编写了一个脚本,从指定的目录路径中获取输入图像,然后对图像进行预处理并将像素值重新调整为 0 到 1 之间。我还将图像调整为正确的尺寸并使用了 model .predict() 给出预测。但是当我运行代码时:

from keras.models import Sequential
from keras_preprocessing.image import *
from keras.layers import *
import tensorflow as tf
import numpy as np
from keras.layers.experimental.preprocessing import Rescaling
import os
import cv2
from keras.models import *

img_size = 250

#Load weights into new model
filepath = os.getcwd() + "/trained_model.h5"

model = load_model(filepath)
print("Loaded model from disk")

#Scales the pixel values to between 0 to 1
#datagen = ImageDataGenerator(rescale=1.0/255.0)

#Prepares Testing Data

testing_dataset = cv2.imread(os.getcwd() + "/cats and dogs images/single test sample/505.png")
#img = datagen.flow_from_directory(testing_dataset, target_size=(img_size,img_size))

img = cv2.resize(testing_dataset, (img_size,img_size))
newimg = np.asarray(img)
pixels = newimg.astype('float32')
pixels /= 255.0
print(pixels.shape)


model.predict(x=pixels)

弹出这个错误:

Loaded model from disk
(250, 250, 3)
Traceback (most recent call last):
File "C:\Users\Jackson\Documents\Programming\Python Projects\Neural Network That Deteremines Cats and Dogs\Test Trained Model.py", line 34, in <module>
model.predict(x=pixels)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1599, in predict
tmp_batch_outputs = predict_function(iterator)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 696, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 3065, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1462 predict_function *
return step_function(self, iterator)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1452 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1445 run_step **
outputs = model.predict_step(data)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1418 predict_step
return self(x, training=False)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:975 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs,
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:191 assert_input_compatibility
raise ValueError('Input ' + str(input_index) + ' of layer ' +

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 250, 3]

我做错了什么还是我错过了什么?另外,我对 model.predict_classes()model.predict_generator() 进行了同样的尝试,但出现了同样的错误。

最佳答案

如果您在图像输入形状方面所做的一切都是正确的,它与模型所需的输入形状相匹配,那么模型很可能希望接收一批大小为 (250, 250, 3) 的图像,所以如果您有一个要在输入形状上测试的图像,其尺寸应为 (1, 250, 250, 3),这意味着您正在传递一批尺寸为 1 的图像。

您的错误消息的意思是模型需要一个 4 维的输入形状并且传递了一个 3 维的输入形状,您需要包含批处理维度,所以我认为在图像标准化后添加这一行应该让它工作。

pixels = np.expand_dims(pixels, axis=0)

在打印形状线时,像素形状应为 (1, 250, 250, 3)

关于keras - 为什么我不能将图像数据拟合到我的 CNN 中的 model.predict 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64544912/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com