Keras 中的错误 - 'int' 对象没有属性 'shape'

Posted

技术标签:

【中文标题】Keras 中的错误 - \'int\' 对象没有属性 \'shape\'【英文标题】:Error in Keras - 'int' object has no attribute 'shape'Keras 中的错误 - 'int' 对象没有属性 'shape' 【发布时间】:2021-11-02 18:11:16 【问题描述】:

我正在使用 VGGFace 模型。我将 tensorflow.keras 与 Tensorflow 2.4.1 版一起使用。以下是生成器的功能:

def gen(list_tuples, person_to_images_map, batch_size=16):
    ppl = list(person_to_images_map.keys())
    while True:
        batch_tuples = sample(list_tuples, batch_size // 2)
        labels = [1] * len(batch_tuples)
        while len(batch_tuples) < batch_size:
            p1 = choice(ppl)
            p2 = choice(ppl)

            if p1 != p2 and (p1, p2) not in list_tuples and (p2, p1) not in list_tuples:
                batch_tuples.append((p1, p2))
                labels.append(0)

        for x in batch_tuples:
            if not len(person_to_images_map[x[0]]):
                print(x[0])

        X1 = [choice(person_to_images_map[x[0]]) for x in batch_tuples]
        X1 = np.array([read_img(x) for x in X1])

        X2 = [choice(person_to_images_map[x[1]]) for x in batch_tuples]
        X2 = np.array([read_img(x) for x in X2])

        yield [X1, X2], labels

valx=gen(val, val_person_to_images_map, batch_size=100)

for i in valx:
    valx=i
    break

在执行模型时会报错:'int' object has no attribute 'shape':

history = model.fit_generator(gen(train, train_person_to_images_map, batch_size=16), use_multiprocessing=True,
                    validation_data=(valx[0],valx[1]), epochs=50, verbose=1,
                    workers = 4, callbacks=callbacks_list, steps_per_epoch=200)
AttributeError                            Traceback (most recent call last)
<ipython-input-114-1c7e5a32eed2> in <module>()
      1 history = model.fit_generator(gen(train, train_person_to_images_map, batch_size=16), use_multiprocessing=True,
      2                     validation_data=(valx[0],valx[1]), epochs=50, verbose=1,
----> 3                     workers = 4, callbacks=callbacks_list, steps_per_epoch=200)

7 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py in _get_dynamic_shape(t)
    805 
    806     def _get_dynamic_shape(t):
--> 807       shape = t.shape
    808       # Unknown number of dimensions, `as_list` cannot be called.
    809       if shape.rank is None:

AttributeError: 'int' object has no attribute 'shape'

我查找了类似的查询,但我似乎不明白是哪一行导致了错误。 我尝试将 gen 函数中的代码更改为

for x in batch_tuples:
    x=np.array(x)
    if not len(person_to_images_map[x[0]]):
         print(x[0])

但这给了我另一个错误:

'generator' object is not subscriptable in python

【问题讨论】:

【参考方案1】:

请在person_to_images_map[x[0]]查看图片调整大小

【讨论】:

以上是关于Keras 中的错误 - 'int' 对象没有属性 'shape'的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用 Keras 上的回调保存我的模型时,Sequential' 对象没有属性 '_ckpt_saved_epoch' 错误

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

Python 属性错误:“int”对象没有属性“reshape”

出现错误:“int”对象没有属性“isnumeric”

分割层输出时,Keras 抛出“Tensor”对象没有属性“_keras_shape”

Keras - LeakyReLU 保存模型时没有属性名称错误