如何在模型中输入 tfrecord 文件并进行训练?

How to feed tfrecord file in a model and train?

我写了一个 tfrecord 文件并输入了我的 Unet 模型,但输入形状有问题。下面是我的代码。

关于数据:

我用了前两个例子:

test_writer = tf.io.TFRecordWriter('test.tfrecords')

for i in range(2):
  example = create_example(image_paths[i], label_paths[i])
  test_writer.write(example.SerializeToString())

test_writer.close()

serialised_example = tf.data.TFRecordDataset('test.tfrecords')
parsed_example = serialised_example.map(parse_tfrecord)

我的模型架构(我简化了):

from tensorflow.keras.layers import Conv3D, Conv3DTranspose, Input, Rescaling

num_classes = 4

my_model = tf.keras.Sequential([

Input(shape = (240, 240, 155, 4)),
Rescaling(scale = 1./255),

Conv3D(filters = 64, kernel_size = 3, strides = 2, activation = 'relu', padding = 'same'),
Conv3D(filters = 64, kernel_size = 3, activation = 'relu', padding = 'same'),
# and more layers between...
Conv3DTranspose(filters = 64, kernel_size = 3, activation = 'relu', padding = 'same'),
Conv3DTranspose(filters = 64, kernel_size = 3, strides = 2, activation = 'relu', padding = 'same'),

Conv3D(filters = num_classes, kernel_size = 3, activation = 'softmax', padding = 'same')

])

my_model.compile(optimizer = 'rmsprop', loss = 'sparse_categorical_crossentropy')

我从我的 tfrecord 文件中得到了我的数据集,如下所示:

def get_image_and_label(features):
  image, label = features['image'], features['label']
  return image, label

def get_dataset(tfrecord_names):

  dataset = (tf.data.TFRecordDataset(tfrecord_names)
             .map(parse_tfrecord)
             .map(get_image_and_label))

  return dataset

dataset = get_dataset('test.tfrecords')

我开始训练了:

my_model.fit(dataset, epochs = 1)

并出现此错误:层“sequential_2”的输入 0 与层不兼容:预期形状=(None, 240, 240 , 155, 4), 发现形状=(240, 240, 155, 4)

我该如何解决这个问题?如果您需要更多信息(数据 link 或我以前的代码),请告诉我。

您的模型需要形状 (samples, 240, 240, 155, 4),所以尝试这样的操作:

dataset = get_dataset('test.tfrecords').batch(1)

如果您希望标签与输出匹配,则必须设置 strides=1

from tensorflow.keras.layers import Conv3D, Conv3DTranspose, Input, Rescaling

num_classes = 4

my_model = tf.keras.Sequential([

Input(shape = (240, 240, 155, 4)),
Rescaling(scale = 1./255),
Conv3D(filters = 64, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),
Conv3D(filters = 64, kernel_size = 3, activation = 'relu', padding = 'same'),
# and more layers between...
Conv3DTranspose(filters = 64, kernel_size = 3, activation = 'relu', padding = 'same'),
Conv3DTranspose(filters = 64, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),

Conv3D(filters = num_classes, kernel_size = 3, activation = 'softmax', padding = 'same')
])