convert_to_generator_like num_samples属性错误:"int"对象没有属性"shape" [英] convert_to_generator_like num_samples Attribute Error: 'int' object has no attribute 'shape'

查看:135
本文介绍了convert_to_generator_like num_samples属性错误:"int"对象没有属性"shape"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Keras序列编写了一个自定义生成器,但是在第一个纪元结束时,我得到了: 属性错误:自定义生成器对象没有属性形状"

I've written a custom generator using Keras sequence, but at the end of first epoch i got: Attribute Error: Custom Generator object has no attribute 'shape'

Ubuntu 18.04 CUDA 10 尝试过Tensorflow 1.13& 1.14 看到此页面: https://github.com/keras-team/keras/issues/12586 我试图改变 从keras.utils导入序列 到 从tensorflow.python.keras.utils.data_utils导入序列 但没有运气!

Ubuntu 18.04 Cuda 10 Tried Tensorflow 1.13 & 1.14 seeing this page: https://github.com/keras-team/keras/issues/12586 i tried changing from keras.utils import Sequence to from tensorflow.python.keras.utils.data_utils import Sequence but no luck!

class CustomGenerator(Sequence):

def __init__(self, ....):
    ...
    # Preallocate memory
    if mode == 'train' and self.crop_shape:
        self.X = np.zeros((batch_size, crop_shape[0], crop_shape[1], 4), dtype='float32')
        # edge
        # self.X2 = np.zeros((batch_size, crop_shape[1], crop_shape[0], 3), dtype='float32')

        self.Y1 = np.zeros((batch_size, crop_shape[0] // 4, crop_shape[1] // 4, self.n_classes), dtype='float32')

def on_epoch_end(self):
    # Shuffle dataset for next epoch
    c = list(zip(self.image_path_list, self.label_path_list, self.edge_path_list))
    random.shuffle(c)
    self.image_path_list, self.label_path_list, self.edge_path_list = zip(*c)

    # Fix memory leak (tensorflow.python.keras bug)
    gc.collect()


def __getitem__(self, index):
    for n, (image_path, label_path,edge_path) in enumerate(
            zip(self.image_path_list[index * self.batch_size:(index + 1) * self.batch_size],
                self.label_path_list[index * self.batch_size:(index + 1) * self.batch_size],
                self.edge_path_list[index * self.batch_size:(index + 1) * self.batch_size])):

        image = cv2.imread(image_path, 1)
        label = cv2.imread(label_path, 0)

        edge = cv2.imread(edge_path, 0)

        ....

        self.X[n] = image
        self.Y1[n] = to_categorical(cv2.resize(label, (label.shape[1] // 4, label.shape[0] // 4)),
                                    self.n_classes).reshape((label.shape[0] // 4, label.shape[1] // 4, -1))
        self.Y2[n] = to_categorical(cv2.resize(label, (label.shape[1] // 8, label.shape[0] // 8)),
                                    self.n_classes).reshape((label.shape[0] // 8, label.shape[1] // 8, -1))
        self.Y3[n] = to_categorical(cv2.resize(label, (label.shape[1] // 16, label.shape[0] // 16)),
                                    self.n_classes).reshape((label.shape[0] // 16, label.shape[1] // 16, -1))

    return self.X, [self.Y1, self.Y2, self.Y3]

def __len__(self):
    return math.floor(len(self.image_path_list) / self.batch_size)

def random_crop(image, edge, label, random_crop_size=(800, 1600)):
    ....
    return image, label

错误是:

742/743 [============================>.] - ETA: 0s - loss: 1.8465 - conv6_cls_loss: 1.1261 - sub24_out_loss: 1.2478 - sub4_out_loss: 1.3827 - conv6_cls_categorical_accuracy: 0.6705 - sub24_out_categorical_accuracy: 0.6250 - sub4_out_categorical_accuracy: 0.5963Traceback (most recent call last):
  File "/home/user/Desktop/Keras-ICNet/train1.py", line 75, in <module>
    use_multiprocessing=True, shuffle=True, max_queue_size=10, initial_epoch=opt.epoch)
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1433, in fit_generator
    steps_name='steps_per_epoch')
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py", line 322, in model_iteration
    steps_name='validation_steps')
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py", line 144, in model_iteration
    shuffle=shuffle)
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py", line 480, in convert_to_generator_like
    num_samples = int(nest.flatten(data)[0].shape[0])
AttributeError: 'int' object has no attribute 'shape'

推荐答案

查看堆栈跟踪,

num_samples = int(nest.flatten(data)[0].shape[0])
AttributeError: 'int' object has no attribute 'shape'

data实际上是指fit_generator中传递的validation_data参数.应该是生成器元组.我猜这是作为数组传递的,结果nest.flatten(data)[0]返回一个int并因此返回错误.

The data actually refers to the validation_data parameter passed in fit_generator. This is supposed to be a generator or tuple. My guess is this is passed as an array as a result of which nest.flatten(data)[0] returns an int and hence the error.

这篇关于convert_to_generator_like num_samples属性错误:"int"对象没有属性"shape"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆