我正在研究Keras中的分段问题,我希望在每个训练时代结束时显示分段结果.
我想要一些类似于Tensorflow:如何在Tensorboard中显示自定义图像(例如Matplotlib Plots),但使用Keras.我知道Keras有TensorBoard回调但看起来似乎有限.
我知道这会破坏Keras的后端抽象,但无论如何我对使用TensorFlow后端感兴趣.
是否有可能通过Keras + TensorFlow实现这一目标?
所以我通过以下代码让我的keras模型与tf.Dataset一起工作:
# Initialize batch generators(returns tf.Dataset)
batch_train = build_features.get_train_batches(batch_size=batch_size)
# Create TensorFlow Iterator object
iterator = batch_train.make_one_shot_iterator()
dataset_inputs, dataset_labels = iterator.get_next()
# Create Model
logits = .....(some layers)
keras.models.Model(inputs=dataset_inputs, outputs=logits)
# Train network
model.compile(optimizer=train_opt, loss=model_loss, target_tensors=[dataset_labels])
model.fit(epochs=epochs, steps_per_epoch=num_batches, callbacks=callbacks, verbose=1)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将validation_data参数传递给模型时.适合它告诉我,我不能用它与发电机.有没有办法在使用tf.Dataset时使用验证
例如在tensorflow中,我可以执行以下操作:
# initialize batch generators
batch_train = build_features.get_train_batches(batch_size=batch_size)
batch_valid = build_features.get_valid_batches(batch_size=batch_size)
# create TensorFlow Iterator object
iterator = tf.data.Iterator.from_structure(batch_train.output_types,
batch_train.output_shapes)
# create two initialization ops to switch between the datasets
init_op_train = iterator.make_initializer(batch_train)
init_op_valid = iterator.make_initializer(batch_valid) …Run Code Online (Sandbox Code Playgroud) 我正在遵循本指南。
它显示了如何使用以下tfds.load()方法从新的TensorFlow数据集中下载数据集:
import tensorflow_datasets as tfds
SPLIT_WEIGHTS = (8, 1, 1)
splits = tfds.Split.TRAIN.subsplit(weighted=SPLIT_WEIGHTS)
(raw_train, raw_validation, raw_test), metadata = tfds.load(
'cats_vs_dogs', split=list(splits),
with_info=True, as_supervised=True)
Run Code Online (Sandbox Code Playgroud)
后续步骤显示了如何使用map方法将函数应用于数据集中的每个项目:
def format_example(image, label):
image = tf.cast(image, tf.float32)
image = image / 255.0
# Resize the image if required
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)
Run Code Online (Sandbox Code Playgroud)
然后访问元素,我们可以使用:
for features in ds_train.take(1):
image, label = features["image"], features["label"]
Run Code Online (Sandbox Code Playgroud)
要么
for example in tfds.as_numpy(train_ds):
numpy_images, …Run Code Online (Sandbox Code Playgroud) python tensorflow tensorflow-datasets data-augmentation tensorflow2.0