AttributeError:“SequenceGenerator”对象没有属性“形状”自定义序列生成器 Keras 2.2.4

Posted

技术标签:

【中文标题】AttributeError:“SequenceGenerator”对象没有属性“形状”自定义序列生成器 Keras 2.2.4【英文标题】:AttributeError: 'SequenceGenerator' object has no attribute 'shape' Custom Sequence Generator Keras 2.2.4 【发布时间】:2019-08-28 16:51:05 【问题描述】:

我正在尝试将 fit_generator 与自定义 train_sequence_generator 和自定义 test_sequence_generator 一起使用,但出现以下错误。请告知我应该在哪里更改我的代码。

1.自定义序列类:

import os
import numpy as np
from keras.utils import Sequence
from batchGenerator import BatchGenerator

from settings import  batch_size, train_folder, test_folder


class SequenceGenerator(Sequence):
    def __init__(self, batches_folder):
        self.batch_generator = BatchGenerator(folder_name=batches_folder)      
        self.names = [f for f in os.listdir(batches_folder) if f.lower().endswith('.jpg')]

    def __len__(self):
        return int(np.ceil(len(self.names) / float(batch_size)))

    def __getitem__(self, idx):
        print('Getting a bacth0'.format(idx))
        [X_batch, Y_batch] = self.batch_generator.load_batch_from_disk(idx)

        return X_batch, Y_batch


def train_seq_genenrator():
    return SequenceGenerator(train_folder)


def test_seq_generator():
    return SequenceGenerator(test_folder)

2。 CoLab 中的 Fit_generator

from sequenceGenerator import train_seq_genenrator, test_seq_generator

history = new_model.fit_generator(train_seq_genenrator()
                        , steps_per_epoch=num_train_samples // batch_size                        
                        , validation_data=test_seq_generator()
                        , validation_steps=num_test_samples // batch_size
                        , epochs=epochs
                        , shuffle=False)

3。错误

-----------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-ad1d08859faf> in <module>()
      4                         , validation_steps=num_test_samples // batch_size
      5                         , epochs=epochs
----> 6                         , shuffle=False)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1424         use_multiprocessing=use_multiprocessing,
   1425         shuffle=shuffle,
-> 1426         initial_epoch=initial_epoch)
   1427 
   1428   def evaluate_generator(self,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, **kwargs)
    113       batch_size=batch_size,
    114       epochs=epochs - initial_epoch,
--> 115       shuffle=shuffle)
    116 
    117   do_validation = validation_data is not None

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_generator.py in convert_to_generator_like(data, batch_size, steps_per_epoch, epochs, shuffle)
    375 
    376   # Create generator from NumPy or EagerTensor Input.
--> 377   num_samples = int(nest.flatten(data)[0].shape[0])
    378   if batch_size is None:
    379     raise ValueError('You must specify `batch_size`')

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

【问题讨论】:

你解决了这个问题吗?我遇到了类似的错误 【参考方案1】:

在新的 anaconda 环境中安装 Keras 和 TensorFlow,然后开始训练过程为我解决了这个问题。

【讨论】:

以上是关于AttributeError:“SequenceGenerator”对象没有属性“形状”自定义序列生成器 Keras 2.2.4的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:“字节”对象没有属性“告诉”

AttributeError: 'RDD' 对象没有属性 'show'

AttributeError:“NumpyArrayIterator”对象没有属性“类”

AttributeError:模块 'dbus' 没有属性 'lowlevel'

AttributeError:模块'keras'没有属性'initializers'

AttributeError:“会话”对象没有属性“会话”