AttributeError:“列表”对象在转换为数组时没有属性“形状”

Posted

技术标签:

【中文标题】AttributeError:“列表”对象在转换为数组时没有属性“形状”【英文标题】:AttributeError: 'list' object has no attribute 'shape' while converting to array 【发布时间】:2020-08-03 23:52:45 【问题描述】:

我试图预测课程,但它给了我这个错误。

----------------------------------- ---------------------------- AttributeError Traceback(最近调用 最后)在 30 im=ImageGrab.grab(bbox=(205,45,585,555)) 31 im.save('img.jpg') ---> 32 个预测 = new_model.predict([prepare('img.jpg')]) 33 y=类别[np.argmax(预测[0][0])] 34

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py 在预测(自我,x,batch_size,详细,步骤,回调, 最大队列大小,工人,使用多处理)1011 max_queue_size=max_queue_size,1012 个工人=工人, -> 1013 use_multiprocessing=use_multiprocessing) 1014 1015 def reset_metrics(self):

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py 在预测(自我,模型,x,batch_size,详细,步骤,回调, max_queue_size, 工人, use_multiprocessing, **kwargs) 496 模型,ModeKeys.PREDICT,x=x,batch_size=batch_size,verbose=verbose, 497 步=步,回调=回调,max_queue_size=max_queue_size, --> 498 个工人=工人,use_multiprocessing=use_multiprocessing,**kwargs) 499 500

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py 在_model_iteration(自我,模型,模式,x,y,batch_size,详细, sample_weight、steps、callbacks、max_queue_size、workers、 use_multiprocessing, **kwargs) 第424章 425名工人=工人, --> 426 使用_多处理=使用_多处理) 427 总样本 = _get_total_number_of_samples(适配器) 428 use_sample = total_samples 不是 None

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py 在_process_inputs(模型,模式,x,y,batch_size,epochs, sample_weights,class_weights,shuffle,steps,distribution_strategy, max_queue_size、workers、use_multiprocessing) 第644章 645 x, y, sample_weights = 标准化( --> 646 x, y, sample_weight=sample_weights) 647 elif adapter_cls 是 data_adapter.ListsOfScalarsDataAdapter: 第648章

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py 在 _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size、check_steps、steps_name、steps、validation_split、shuffle、 extract_tensors_from_dataset) 2344 # 首先,我们建立模型 如有必要,可即时进行。 2345 如果不是 self.inputs: -> 2346 all_inputs,y_input,dict_inputs = self._build_model_with_inputs(x,y)2347 is_build_call = 真 2348 其他:

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py 在 _build_model_with_inputs(自我、输入、目标)中 2570 其他: 第2571章 -> 2572 self._set_inputs(cast_inputs) 2573 返回处理后的输入,目标,is_dict_inputs 2574

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py 在_set_inputs(自我,输入,输出,训练)2645第一 layer 不是 FeatureLayer。第2646章 -> 2647 个输入 = self._set_input_attrs(inputs) 2648 2649 如果输出为无:

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\training\tracking\base.py 在 _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False #pylint:禁用=保护访问 456 尝试: --> 457 结果 = 方法(自我,*args,**kwargs) 最后458: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py 在_set_input_attrs(自我,输入)2684 input_shape = (无,)2685 其他: -> 2686 input_shape = (None,) + tuple(inputs.shape[1:]) 2687 self._build_input_shape = input_shape 2688

AttributeError: 'list' 对象没有属性 'shape'

import cv2
import tensorflow as tf
import numpy as np

CATEGORIES = ["gas","back","both"]


def prepare(filepath):
    IMG_SIZE = 256
    img_array = cv2.imread(filepath)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    img = np.reshape(new_array,[1,256,256,3])
    return img


model = tf.keras.models.load_model("trained.model")

prediction = model.predict_classes([prepare('img.jpg')])
print(CATEGORIES[int(prediction[0])])

【问题讨论】:

哪一行引发了异常? 请发布错误和产生错误的行。必须有一个最小的可重现示例。 【参考方案1】:

你将一个列表传递给 model.predict_classes 尝试给它一个 numpy 数组:

prediction = model.predict_classes(prepare('img.jpg'))

编辑:显然,您的准备函数已经具有批量大小的第一个暗淡,所以我删除了 [np.newaxis, :]

【讨论】:

以上是关于AttributeError:“列表”对象在转换为数组时没有属性“形状”的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:“列表”对象没有属性“numpy”

>AttributeError:“list”对象没有属性“lower”(在小写数据框中)

AttributeError 列表对象没有属性添加

AttributeError:“列表”对象没有属性“create_png”

AttributeError: 'property' 对象没有属性 'copy' - 尝试在 Django Rest 中获取对象列表时

AttributeError:“列表”对象没有属性“排序值”