Python 3.x 转换为与生成器类似的num样本属性错误:';int';对象没有属性';形状';
我已经使用Keras序列编写了一个自定义生成器,但在第一个纪元结束时,我得到了: 属性错误:自定义生成器对象没有属性“shape” Ubuntu 18.04 Cuda 10 试过的Tensorflow 1.13和1.14 见本页: 我试着换衣服 从keras.utils导入序列 到 从tensorflow.python.keras.utils.data\u utils导入序列 但是没有运气Python 3.x 转换为与生成器类似的num样本属性错误:';int';对象没有属性';形状';,python-3.x,tensorflow,keras,generator,Python 3.x,Tensorflow,Keras,Generator,我已经使用Keras序列编写了一个自定义生成器,但在第一个纪元结束时,我得到了: 属性错误:自定义生成器对象没有属性“shape” Ubuntu 18.04 Cuda 10 试过的Tensorflow 1.13和1.14 见本页: 我试着换衣服 从keras.utils导入序列 到 从tensorflow.python.keras.utils.data\u utils导入序列 但是没有运气 class CustomGenerator(Sequence): def __init__(self,
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'
742/743[=========================>-预计到达时间:0s-损失:1.8465-conv6-cls-loss:1.1261-sub24-out-loss:1.2478-sub4-out-loss:1.3827-conv6-cls-out分类准确率:0.6705-sub24-out分类准确率:0.6250-sub4-out分类准确率:0.5963(最近一次呼叫回溯):
文件“/home/user/Desktop/Keras ICNet/train1.py”,第75行,在
使用多处理=真,随机播放=真,最大队列大小=10,初始\u epoch=opt.epoch)
文件“/home/user/.local/lib/python3.6/site packages/tensorflow/python/keras/engine/training.py”,第1433行,在fit_生成器中
步骤(名称=“每个时代的步骤”)
文件“/home/user/.local/lib/python3.6/site packages/tensorflow/python/keras/engine/training\u generator.py”,第322行,在模型迭代中
步骤(name='validation')
文件“/home/user/.local/lib/python3.6/site packages/tensorflow/python/keras/engine/training\u generator.py”,第144行,在模型迭代中
洗牌
文件“/home/user/.local/lib/python3.6/site packages/tensorflow/python/keras/engine/training\u generator.py”,第480行,类似convert\u to\u generator
num_samples=int(nest.flatte(数据)[0].shape[0])
AttributeError:“int”对象没有属性“shape”
查看堆栈跟踪
num_samples = int(nest.flatten(data)[0].shape[0])
AttributeError: 'int' object has no attribute 'shape'
数据
实际上是指在fit\u生成器
中传递的validation\u数据
参数。这应该是一个生成器或元组。我猜这是作为数组传递的,其结果是nest.flatte(data)[0]
返回int
,因此返回错误。查看堆栈跟踪
num_samples = int(nest.flatten(data)[0].shape[0])
AttributeError: 'int' object has no attribute 'shape'
数据
实际上是指在fit\u生成器
中传递的validation\u数据
参数。这应该是一个生成器或元组。我猜这是作为数组传递的,其结果是nest.flatte(data)[0]
返回int
,因此返回错误。您如何知道它引用的是验证\u data
?火车和验证数据都是从\uuu getitem\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu(self,index)
返回self.X、[self.Y1,self.Y2,self.Y3]如果传递给fit生成器的验证数据参数是生成器,则不会发生此。如果您可以将培训代码也包括在问题中,将更容易确定问题的原因。问题解决后,参数置换您如何知道它指的是验证\u数据
?火车和验证数据都是从\uuu getitem\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu(self,index)
返回self.X、[self.Y1,self.Y2,self.Y3]如果传递给fit生成器的验证数据参数是生成器,则不会发生此。如果您可以将培训代码也包括在问题中,则更容易确定问题的原因。问题解决后,参数置换能否请您更新问题,使其也包括如何调用fit_生成器方法?能否请您更新问题,使其也包括如何调用fit_生成器方法?