如何解决使用 Tfrecord 馈送多输入神经网络
我在 Tfrecord 上有一个包含图像和表格数据的数据集,我想将这些数据输入到混合神经网络数据中。我看到了如何通过使用 numpy 数组使用多输入神经网络的示例
model.fit([X_images,X_tabular],[Y_images,Y_tabular])
但是通过迭代将 Tfrecord 数据集转换为 numpy 数组会减慢该过程。 有没有办法直接在 Tfrecord 数据上训练它。到目前为止,我有一个只训练图像的模型,这是我正在使用的代码
tfrecords_schema = {
'id': tf.io.FixedLenFeature([],tf.string),'y_label': tf.io.FixedLenFeature([],tf.int64),'image': tf.io.FixedLenFeature([],'x_1': tf.io.FixedLenFeature([],tf.float32),'x_2': tf.io.FixedLenFeature([],'x_3': tf.io.FixedLenFeature([],'x_4': tf.io.FixedLenFeature([],}
def _parse_function(example_proto):
parsed_example = tf.io.parse_single_example(example_proto,tfrecords_schema)
return parsed_example['image'],parsed_example['y_label']
def _parse_error(image,label):
return image != b''
def _preprocess_function(image,label):
image = tf.image.decode_jpeg(image)
image = tf.image.resize(image,(299,299))
label = label_decoder(label)
return image,label
train_data = (
train_data
.map(_parse_function)
.filter(_parse_error)
.map(_preprocess_function)
.apply(tf.data.experimental.ignore_errors())
.batch(BATCH_SIZE)
)
validation_data = (
validation_data
.map(_parse_function)
.filter(_parse_error)
.map(_preprocess_function)
.apply(tf.data.experimental.ignore_errors())
.batch(BATCH_SIZE)
)
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_data = train_data.prefetch(buffer_size=AUTOTUNE)
validation_data = validation_data.prefetch(buffer_size=AUTOTUNE)
def make_model(image_shape,num_classes):
inputs = tf.keras.Input(shape=image_shape)
x = tf.keras.applications.inception_v3.preprocess_input(inputs)
base_model = tf.keras.applications.InceptionV3(input_shape=IMAGE_SHAPE,include_top=False,weights='imagenet')
base_model.trainable = False
x = base_model(x,training=False)
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
x = global_average_layer(x)
x = tf.keras.layers.Dropout(0.3)(x)
prediction_layer = tf.keras.layers.Dense(num_classes)
outputs = prediction_layer(x)
return tf.keras.Model(inputs,outputs)
model = make_model(IMAGE_SHAPE,NUM_CLASSES)
learning_rate = 0.0001
model.compile(
optimizer=tf.keras.optimizers.Adam(lr=learning_rate),loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=["accuracy"]
)
callbacks = [
tf.keras.callbacks.ModelCheckpoint(filepath="tf/pvc_data/tmp/weights.hdf5",verbose=1,save_best_only=True)
]
history = model.fit(
train_data,epochs=1,callbacks=callbacks,validation_data=validation_data
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。