Tensorflow TF 2.0 Keras拟合_生成器:数据_生成器输出错误形状
我正在尝试使用TensorFlow2.0和tf.kerasAPI来训练图像字幕模型。我使用的数据集是Flickr 8K数据集,虽然我的计算机可以在RAM中保存整个数据集,但我希望使用fit_generator和data_generator来批量加载和准备数据(因为一旦我可以使用此数据集,我将尝试使用更大的数据集来训练此模型) 我对数据和模型定义进行预处理的方式还可以。我可以对手动生成的批处理执行model.predict(),模型输出预期的数据形状,并且没有错误。我还可以手动使用data_generator准备完整的数据集,并使用model.fit()使用整个数据。它工作正常,模型训练没有错误 当我尝试使用fit_generator进行训练时出现问题,该生成器将输出此错误(文章末尾的全长错误输出): 如果我单独调用generator函数来检查生成批次的类型和形状,在我看来一切正常:Tensorflow TF 2.0 Keras拟合_生成器:数据_生成器输出错误形状,tensorflow,keras,neural-network,tensorflow2.0,tf.keras,Tensorflow,Keras,Neural Network,Tensorflow2.0,Tf.keras,我正在尝试使用TensorFlow2.0和tf.kerasAPI来训练图像字幕模型。我使用的数据集是Flickr 8K数据集,虽然我的计算机可以在RAM中保存整个数据集,但我希望使用fit_generator和data_generator来批量加载和准备数据(因为一旦我可以使用此数据集,我将尝试使用更大的数据集来训练此模型) 我对数据和模型定义进行预处理的方式还可以。我可以对手动生成的批处理执行model.predict(),模型输出预期的数据形状,并且没有错误。我还可以手动使用data_gen
generator = data_generator(train_descriptions, train_features, wordtoix, max_length, number_pics_per_bath)
data = next(generator)
print("Total items in data: ", len(data))
# Data[1] is the encoded Y
print("Encodded Y shape: ", data[1].shape)
print("Example Y: ", data[1][0])
# Data[0] is a list of [image_feature, encoded_caption]
print("X1 shape (image feature): ", data[0][0].shape)
print("X2 shape (image caption): ", data[0][1].shape)
Outputs:
-----------------------------------
Total items in data: 2
Encodded Y shape: (168, 1652)
Example Y: [0. 0. 1. ... 0. 0. 0.]
X1 shape (image feature): (168, 2048)
X2 shape (image caption): (168, 34)
这是数据发生器功能的代码:
# data generator, intended to be used in a call to model.fit_generator()
# $descriptions: a dictionary containing <image_id> -> [ text_captions_list ]
# photos: list of numpy arrays representing image features
# wordtoix: a dictionary to convert words to word_codes (integers)
# max_length: maximum word count for a caption
def data_generator(descriptions, photos, wordtoix, max_length, num_photos_per_batch):
X1, X2, y = list(), list(), list()
n=0
# loop for ever over images
while 1:
for key, desc_list in descriptions.items():
n+=1
# retrieve the photo feature
photo = photos[key+'.jpg']
for desc in desc_list:
# encode the sequence
seq = [wordtoix[word] for word in desc.split(' ') if word in wordtoix]
# split one sequence into multiple X, y pairs
for i in range(1, len(seq)):
# split into input and output pair
in_seq, out_seq = seq[:i], seq[i]
# pad input sequence
in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
# encode output sequence
out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
# store
X1.append(photo)
X2.append(in_seq)
y.append(out_seq)
# yield the batch data
if n==num_photos_per_batch:
yield [[array(X1), array(X2)], array(y)]
X1, X2, y = list(), list(), list()
n=0
我使用带有imagenet预训练权重的inceptionv3模型从图像生成特征(然后保存到磁盘)
然后我使用这个需要“两个输入”的模型组件:图像特征数组和编码图像标题:
fit_generator的全长误差如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-73-10ea3905954d> in <module>
1 for i in range(epochs):
2 generator = data_generator(train_descriptions, train_features, wordtoix, max_length, number_pics_per_bath)
----> 3 model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
4 model.save('./saved/model_' + str(i) + '.h5')
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1295 shuffle=shuffle,
1296 initial_epoch=initial_epoch,
-> 1297 steps_name='steps_per_epoch')
1298
1299 def evaluate_generator(self,
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
263
264 is_deferred = not model._is_compiled
--> 265 batch_outs = batch_function(*batch_data)
266 if not isinstance(batch_outs, list):
267 batch_outs = [batch_outs]
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
971 outputs = training_v2_utils.train_on_batch(
972 self, x, y=y, sample_weight=sample_weight,
--> 973 class_weight=class_weight, reset_metrics=reset_metrics)
974 outputs = (outputs['total_loss'] + outputs['output_losses'] +
975 outputs['metrics'])
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in train_on_batch(model, x, y, sample_weight, class_weight, reset_metrics)
251 x, y, sample_weights = model._standardize_user_data(
252 x, y, sample_weight=sample_weight, class_weight=class_weight,
--> 253 extract_tensors_from_dataset=True)
254 batch_size = array_ops.shape(nest.flatten(x, expand_composites=True)[0])[0]
255 # If `model._distribution_strategy` is True, then we are in a replica context
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
2470 feed_input_shapes,
2471 check_batch_axis=False, # Don't enforce the batch size.
-> 2472 exception_prefix='input')
2473
2474 # Get typespecs for the input data and sanitize it if necessary.
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
504 elif isinstance(data, (list, tuple)):
505 if isinstance(data[0], (list, tuple)):
--> 506 data = [np.asarray(d) for d in data]
507 elif len(names) == 1 and isinstance(data[0], (float, int)):
508 data = [np.asarray(data)]
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py in <listcomp>(.0)
504 elif isinstance(data, (list, tuple)):
505 if isinstance(data[0], (list, tuple)):
--> 506 data = [np.asarray(d) for d in data]
507 elif len(names) == 1 and isinstance(data[0], (float, int)):
508 data = [np.asarray(data)]
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
83
84 """
---> 85 return array(a, dtype, copy=False, order=order)
86
87
ValueError: could not broadcast input array from shape (168,2048) into shape (168)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
1表示范围内的i(历元):
2发生器=数据发生器(序列描述、序列特征、文字、最大长度、每个浴池的图片数量)
---->3.模型拟合生成器(生成器,历元=1,每历元步长=1,详细度=1)
4型号保存('./已保存/型号_'+str(i)+'.h5')
~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/training.py in-fit\u生成器(self、生成器、每个历元的步骤、历元、冗余、回调、验证数据、验证步骤、验证频率、类权重、最大队列大小、工作者、使用多处理、无序、初始历元)
1295洗牌=洗牌,
1296初始纪元=初始纪元,
->1297个步骤(每个时代的步骤)
1298
1299 def评估_发生器(自,
在模型迭代中~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/training\u generator.py(模型、数据、每个历元的步骤、历元、冗余、回调、验证数据、验证步骤、验证频率、类权重、最大队列大小、工人、使用多处理、随机、初始历元、模式、批大小、步骤名称、**kwargs)
263
264是延迟的=不是模型。\是编译的
-->265批处理输出=批处理功能(*批处理数据)
266如果不存在(批次,列表):
267批次输出=[批次输出]
~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/training.py in-train\u-on\u批次(self,x,y,sample\u-weight,class\u-weight,reset\u-metrics)
971输出=批次上的培训(
972自身,x,y=y,样本重量=样本重量,
-->973类权重=类权重,重置度量=重置度量)
974输出=(输出['总损耗]+输出['输出损耗']+
975个输出[“指标])
~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/training\u v2\u utils.py in train\u on\u批次(型号、x、y、样本权重、类权重、重置度量)
251 x,y,样本权重=模型。标准化用户数据(
252 x,y,样本重量=样本重量,类别重量=类别重量,
-->253从_数据集中提取_张量_=True)
254批次大小=数组操作形状(嵌套.展平(x,展开组合=真)[0])[0]
255#如果“model._distribution_strategy”为真,则我们处于复制上下文中
~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/training.py in\u-standard\u-user\u数据(self、x、y、sample\u-weight、class\u-weight、batch\u-size、check\u-steps、steps\u-name、steps、validation\u-split、shuffle\u、shufflue、extract\u-tensors\u-from\u-data
2470个进纸输入形状,
2471检查_batch_axis=False,#不强制执行批大小。
->2472异常(前缀为“输入”)
2473
2474#获取输入数据的类型规范,必要时对其进行清理。
标准化输入数据中的~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/training\u utils.py(数据、名称、形状、检查批处理轴、异常前缀)
504 elif isinstance(数据,(列表,元组)):
505如果isinstance(数据[0],(列表,元组)):
-->506数据=[np.asarray(d)表示数据中的d]
507 elif len(名称)==1和isinstance(数据[0],(浮点,int)):
508数据=[np.asarray(数据)]
~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/training\u utils.py in(.0)
504 elif isinstance(数据,(列表,元组)):
505如果isinstance(数据[0],(列表,元组)):
-->506数据=[np.asarray(d)表示数据中的d]
507 elif len(名称)==1和isinstance(数据[0],(浮点,int)):
508数据=[np.asarray(数据)]
asarray中的~/anaconda3/envs/tf2 gpu/lib/python3.6/site-packages/numpy/core//u asarray.py(a,数据类型,顺序)
83
84 """
--->85返回数组(a,数据类型,副本=False,顺序=order)
86
87
ValueError:无法将输入数组从形状(1682048)广播到形状(168)
提前感谢您的帮助!def data_generator应该有一个返回语句。有同样的问题,您最终解决了吗?还没有,我从Keras方法转向了更纯粹的TF 2.0方法(使用TF.data.Dataset)
epochs = 20
steps = len(train_descriptions)
for i in range(epochs):
generator = data_generator(train_descriptions, train_features, wordtoix, max_length)
model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
model.save('./saved/model_' + str(i) + '.h5')
for i in range(epochs):
generator = data_generator(train_descriptions, train_features, wordtoix, max_length, number_pics_per_bath)
model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
model.save('./saved/model_' + str(i) + '.h5')
inputs1 = tf.keras.Input(shape=(2048,))
fe1 = tf.keras.layers.Dropout(0.5)(inputs1)
fe2 = tf.keras.layers.Dense(256, activation='relu')(fe1)
inputs2 = tf.keras.Input(shape=(max_length,))
se1 = tf.keras.layers.Embedding(vocab_size, embedding_dim, mask_zero=True)(inputs2)
se2 = tf.keras.layers.Dropout(0.5)(se1)
se3 = tf.keras.layers.LSTM(256)(se2)
decoder1 = tf.keras.layers.concatenate([fe2, se3])
decoder2 = tf.keras.layers.Dense(256, activation='relu')(decoder1)
outputs = tf.keras.layers.Dense(vocab_size, activation='softmax')(decoder2)
model = Model(inputs=[inputs1, inputs2], outputs=outputs)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-73-10ea3905954d> in <module>
1 for i in range(epochs):
2 generator = data_generator(train_descriptions, train_features, wordtoix, max_length, number_pics_per_bath)
----> 3 model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
4 model.save('./saved/model_' + str(i) + '.h5')
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1295 shuffle=shuffle,
1296 initial_epoch=initial_epoch,
-> 1297 steps_name='steps_per_epoch')
1298
1299 def evaluate_generator(self,
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
263
264 is_deferred = not model._is_compiled
--> 265 batch_outs = batch_function(*batch_data)
266 if not isinstance(batch_outs, list):
267 batch_outs = [batch_outs]
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
971 outputs = training_v2_utils.train_on_batch(
972 self, x, y=y, sample_weight=sample_weight,
--> 973 class_weight=class_weight, reset_metrics=reset_metrics)
974 outputs = (outputs['total_loss'] + outputs['output_losses'] +
975 outputs['metrics'])
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in train_on_batch(model, x, y, sample_weight, class_weight, reset_metrics)
251 x, y, sample_weights = model._standardize_user_data(
252 x, y, sample_weight=sample_weight, class_weight=class_weight,
--> 253 extract_tensors_from_dataset=True)
254 batch_size = array_ops.shape(nest.flatten(x, expand_composites=True)[0])[0]
255 # If `model._distribution_strategy` is True, then we are in a replica context
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
2470 feed_input_shapes,
2471 check_batch_axis=False, # Don't enforce the batch size.
-> 2472 exception_prefix='input')
2473
2474 # Get typespecs for the input data and sanitize it if necessary.
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
504 elif isinstance(data, (list, tuple)):
505 if isinstance(data[0], (list, tuple)):
--> 506 data = [np.asarray(d) for d in data]
507 elif len(names) == 1 and isinstance(data[0], (float, int)):
508 data = [np.asarray(data)]
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py in <listcomp>(.0)
504 elif isinstance(data, (list, tuple)):
505 if isinstance(data[0], (list, tuple)):
--> 506 data = [np.asarray(d) for d in data]
507 elif len(names) == 1 and isinstance(data[0], (float, int)):
508 data = [np.asarray(data)]
~/anaconda3/envs/tf2-gpu/lib/python3.6/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
83
84 """
---> 85 return array(a, dtype, copy=False, order=order)
86
87
ValueError: could not broadcast input array from shape (168,2048) into shape (168)