我尝试优化我的数据输入管道。该数据集是一组 450 个 TFRecord 文件,每个文件大小约为 70MB,托管在 GCS 上。该作业使用 GCP ML Engine 执行。没有 GPU。
这是管道:
def build_dataset(file_pattern):
return tf.data.Dataset.list_files(
file_pattern
).interleave(
tf.data.TFRecordDataset,
num_parallel_calls=tf.data.experimental.AUTOTUNE
).shuffle(
buffer_size=2048
).batch(
batch_size=2048,
drop_remainder=True,
).cache(
).repeat(
).map(
map_func=_parse_example_batch,
num_parallel_calls=tf.data.experimental.AUTOTUNE
).prefetch(
buffer_size=1
)
Run Code Online (Sandbox Code Playgroud)
使用映射函数:
def _bit_to_float(string_batch: tf.Tensor):
return tf.reshape(tf.math.floormod(tf.dtypes.cast(tf.bitwise.right_shift(
tf.expand_dims(tf.io.decode_raw(string_batch, tf.uint8), 2),
tf.reshape(tf.dtypes.cast(tf.range(7, -1, -1), tf.uint8), (1, 1, 8))
), tf.float32), 2), (tf.shape(string_batch)[0], -1))
def _parse_example_batch(example_batch):
preprocessed_sample_columns = {
"features": tf.io.VarLenFeature(tf.float32),
"booleanFeatures": tf.io.FixedLenFeature((), tf.string, ""),
"label": tf.io.FixedLenFeature((), tf.float32, -1)
}
samples = tf.io.parse_example(example_batch, preprocessed_sample_columns)
dense_float = tf.sparse.to_dense(samples["features"])
bits_to_float …Run Code Online (Sandbox Code Playgroud) python python-3.x tensorflow tensorflow-datasets tensorflow2.0
目前我遇到了这个错误,有人可以帮忙解决吗?
---------------------------------------------------------------------------
OperatorNotAllowedInGraphError Traceback (most recent call last)
<ipython-input-24-0211c82920d0> in <module>
7 warnings.filterwarnings("ignore")
8 model.train(dataset_train,dataset_val, learning_rate=config.LEARNING_RATE,epochs=5,
----> 9 layers='heads')
/kaggle/working/maskrcnn/Mask_RCNN-master/mrcnn/model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation, custom_callbacks, no_augmentation_sources)
2355 log("Checkpoint Path: {}".format(self.checkpoint_path))
2356 self.set_trainable(layers)
-> 2357 self.compile(learning_rate, self.config.LEARNING_MOMENTUM)
2358
2359 # Work-around for Windows: Keras fails on Windows when using
/kaggle/working/maskrcnn/Mask_RCNN-master/mrcnn/model.py in compile(self, learning_rate, momentum)
2168 for name in loss_names:
2169 layer = self.keras_model.get_layer(name)
-> 2170 if layer.output in self.keras_model.losses:
2171 continue
2172 loss = (
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in …Run Code Online (Sandbox Code Playgroud) 我有一个自定义tf.keras.layers.Layer,它只使用 TF 运算符进行某种位解包(将整数转换为布尔值(0 或 1 浮点数))。
class CharUnpack(keras.layers.Layer):
def __init__(self, name="CharUnpack", *args, **kwargs):
super(CharUnpack, self).__init__(trainable=False, name=name, *args, **kwargs)
# Range [7, 6, ..., 0] to bit-shift integers
self._shifting_range = tf.reshape(
tf.dtypes.cast(
tf.range(7, -1, -1, name='shifter_range'),
tf.uint8,
name='shifter_cast'),
(1, 1, 8),
name='shifter_reshape')
# Constant value 0b00000001 to use as bitwise and operator
self._selection_bit = tf.constant(0x01, dtype=tf.uint8, name='and_selection_bit')
def call(self, inputs):
return tf.dtypes.cast(
tf.reshape(
tf.bitwise.bitwise_and(
tf.bitwise.right_shift(
tf.expand_dims(inputs, 2),
self._shifting_range,
),
self._selection_bit,
),
[x if x else -1 for x in …Run Code Online (Sandbox Code Playgroud)