当前位置: 首页 > 知识库问答 >
问题:

ValueError:检查输入时出错:预期conv2d_input有4个维度,但有形状的数组(无,1)

魏健柏
2023-03-14

我完成了对我的模型的培训,该模型由20个类组成,精度达到0.9993,目前正在进行测试。我正在学习本教程,但我在

prediction = model.predict(['test1.jpg'])

培训数据定义为:

for features, label in training_data:
    x.append(features)
    y.append(label)

x = np.array(x).reshape(-1, IMG_SIZE, IMG_SIZE,1)

这就是我对cnn的定义

x = pickle.load(open("x.pickle", "rb" ))
y = pickle.load(open("y.pickle", "rb"))

x = x/255.0

model = Sequential()
model.add(Conv2D(64,(3,3), input_shape = x.shape[1:IMG_SIZE]))
model.add(Activation("relu"))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Conv2D(64,(3,3), input_shape  = x.shape[1:IMG_SIZE]))
model.add(Activation("relu"))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))

model.add(Dense(20))
model.add(Activation("sigmoid"))

这里也是我对我的模型的总结

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 222, 222, 64)      640       
_________________________________________________________________
activation (Activation)      (None, 222, 222, 64)      0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 111, 111, 64)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 109, 109, 64)      36928     
_________________________________________________________________
activation_1 (Activation)    (None, 109, 109, 64)      0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 54, 54, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 186624)            0         
_________________________________________________________________
dense (Dense)                (None, 64)                11944000  
_________________________________________________________________
dense_1 (Dense)              (None, 20)                1300      
_________________________________________________________________
activation_2 (Activation)    (None, 20)                0         
=================================================================
Total params: 11,982,868
Trainable params: 11,982,868
Non-trainable params: 0
_________________________________________________________________

我得到的错误是

---------------------------------------------------------------------------中的ValueError回溯(最近一次调用)----

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\keras\引擎\training.py预测(自我,x,batch_size,冗长,步骤,回调,max_queue_size,工人,use_multiprocessing)907max_queue_size=max_queue_size,908工人=工人,-

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\keras\engine\training\u v2。py in predict(self、model、x、批量大小、详细程度、步骤、回调、**kwargs)460返回self_模型迭代(461模型,ModeKeys.PREDICT,x=x,批大小=批大小,详细=详细--

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\keras\engine\training\u v2。py in_model_迭代(self、model、mode、x、y、批量大小、详细程度、样本权重、步骤、回调、**kwargs)442 mode=mode,443 training_context=training_context--

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\keras\engine\training\u v2。py以批处理日志的形式运行(模型、迭代器、执行函数、数据集大小、批处理大小、策略、每批处理的步数、样本数、模式、训练上下文、总记录)121步=步、模式=模式、大小=当前批处理大小:122尝试:--

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\keras\engine\training\u v2\u utils。执行函数(输入函数)中的py 84#numpy以急切模式将张量转换为值。85回巢。映射结构(_非_无_常量_值---

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\渴望\def_function.py调用(自我,*args,**kwds)455 456tracing_count=自我。_get_tracing_count()-

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\eager\def\u函数。py in_调用(self,*args,**kwds)501#这是调用的第一个调用,因此我们必须初始化。502初始值设定项\映射=对象\标识。ObjectIdentityDictionary()--

--

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\渴望\function.py在_get_concrete_function_internal_garbage_collected(自我,*args,**kwargs)1846如果self.input_signature:1847 args,kwargs=无,无-

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\eager\function。py in_可能定义函数(self、args、kwargs)2148
graph_function=self_函数缓存。主要的,重要的如果graph_函数为None,则获取(缓存_键,无)
2149:-

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\eager\function。py在创建图形函数(self、args、kwargs、override\u flat\u arg\u shapes)中2039 arg\u names=arg\u names,
2040 override\u flat\u arg\u shapes=override\u flat\u arg\u shapes-

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\框架\func_graph.pyfunc_graph_from_py_func(名称,python_func,args,kwargs,签名,func_graph,签名,autograph_options,add_control_dependencies,arg_names,op_return_value,集合,capture_by_value,override_flat_arg_shapes)913converted_func)914-

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\急切\def_function.py在wrapped_fn(*args,**kwds)356#包装允许自动图形交换转换函数。我们给357#函数一个弱引用,以避免引用循环。--

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\keras\engine\training\u v2\u utils。分布函数(输入迭代器)中的py 71策略=分布策略上下文。获取策略()72个输出=策略。实验运行v2(---

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\distribute\distribute\u lib。实验运行中的py(自身、fn、args、kwargs)758 fn=自传。tf_convert(fn,ag_ctx.control_status_ctx(),759 convert_by_default=False)--

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\distribute\distribute\u lib。py-in-call_for_每个_副本(self、fn、args、kwargs)1785 kwargs={}1786 with self_容器策略()。作用域():-

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\分发\distribute_lib.py在_call_for_each_replica(自我,fn,args,kwargs)2130
自我。_container_strategy(),2131
replica_id_in_sync_group=constant_op.constant(0,dtypes.int32)):-

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\autograph\impl\api。包装中的py(*args,**kwargs)290 def包装(*args,**kwargs):带有ag_ctx的291。ControlStatusCtx(状态=ag\u ctx.status.DISABLED):--

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\keras\engine\training\u v2\u utils。批处理中的py预测(解析参数失败)批处理中的160 def预测(模型,x,y=无,样本权重=无):161 del y,样本权重--

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\keras\引擎\training_v2_utils.pypredict_on_batch(模型,x)357#验证和标准化用户数据。358输入,_,_=模型。_standardize_user_data(-

~\Anaconda3\envs\tensorflow\u cpu\lib\site packages\tensorflow\u core\python\keras\engine\training。py-in标准化用户数据(自身、x、y、样本权重、类权重、批次大小、检查步骤、步骤名称、步骤、验证、拆分、洗牌、从数据集中提取张量)2470个输入形状,
2471检查批次轴=False,不强制实施批次大小-

~\Anaconda3\envs\tensorflow_cpu\lib\site-包\tensorflow_core\python\keras\引擎\training_utils.pystandardize_input_data(数据,名称,形状,check_batch_axis,exception_prefix)

 563                            ': expected ' + names[i] + ' to have ' +
 564                            str(len(shape)) + ' dimensions, but got array '

--

ValueError:检查输入时出错:预期conv2d_input有4个维度,但有形状的数组(无,1)

共有2个答案

周伟泽
2023-03-14

首先,第一个conv层的输入形状似乎是错误的。

input\u shape=(IMG\u大小,IMG\u大小,1)

模型。添加(Conv2D(64,(3,3),input_shape=(IMG_大小,IMG_大小,1))

其次,无需为任何中间层指定input_shape

model.add(Conv2D(64,(3,3),input_shape=x.shape[1:IMG_SIZE]))

模型。添加(Conv2D(64,(3,3))

苏鸿志
2023-03-14

您试图在某个字符串上预测['test1.jpg'],为什么?您需要准备与用于培训的数据形状和分布相同的数据-例如-加载图像,将其调整/裁剪为相关大小,将其规格化为范围[0,1](如果这是您在培训期间所做的)等。。。

 类似资料: