ValueError:形状(None,3,2)和(None,2)使用tfrecord不兼容

时间:2020-10-17 15:02:55

标签: python python-3.x tensorflow tensorflow2.0

在以下代码中,我将标签保存到tfrecord并再次读取。 (实际上,我将图像和标签都保存到tfrecord中,这是一个简单的示例,仅供说明)。

我收到一个错误ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible,该如何解决?我正在使用Tensorflow 2.3。关键部分应该在parse_examples的return语句中。

import contextlib2
import numpy as np
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout


def process_image():

    dic={
            "image/label": tf.train.Feature(int64_list=tf.train.Int64List(value=[0,1]))
    }
    return tf.train.Example(features=tf.train.Features(feature=dic))


with contextlib2.ExitStack() as tf_record_close_stack:
    output_tfrecords = [tf_record_close_stack.enter_context(tf.io.TFRecordWriter(file_name)) for file_name in
                        [f"data_train.tfrecord"]]
    output_tfrecords[0].write(process_image().SerializeToString())

def parse_examples(examples):
    parsed_examples = tf.io.parse_example(examples, features={
        "image/label": tf.io.FixedLenFeature(shape=[2], dtype=tf.int64),
    })
    res = np.random.randint(2, size=3072).reshape(32, 32, 3)
    return (res, [parsed_examples["image/label"],parsed_examples["image/label"],parsed_examples["image/label"]])


def process_dataset(dataset):
    dataset = dataset.map(parse_examples, num_parallel_calls=tf.data.experimental.AUTOTUNE)
    dataset = dataset.batch(1)
    return dataset

train_data = tf.data.TFRecordDataset(filenames="data_train.tfrecord")
train_data = process_dataset(train_data)

base_model = tf.keras.applications.EfficientNetB7(input_shape=(32,32, 3), weights='imagenet',
                                                  include_top=False)  # or weights='noisy-student'

for layer in base_model.layers[:]:
    layer.trainable = False

x = GlobalAveragePooling2D()(base_model.output)
dropout_rate = 0.3


x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)


all_target = []
loss_list = []
test_metrics = {}
for name, node in  [("task1", 2), ("task2", 2), ("task3", 2)]:
    y1 = Dense(128, activation='relu')(x)
    y1 = Dropout(dropout_rate)(y1)
    y1 = Dense(64, activation='relu')(y1)
    y1 = Dropout(dropout_rate)(y1)
    y1 = Dense(node, activation='softmax', name=name)(y1)
    all_target.append(y1)
    loss_list.append('categorical_crossentropy')
    test_metrics[name] = "accuracy"

#    model = Model(inputs=model_input, outputs=[y1, y2, y3])
model = Model(inputs=base_model.input, outputs=all_target)

model.compile(loss=loss_list, optimizer='adam', metrics=test_metrics)


history = model.fit(train_data, epochs=1, verbose=1)

1 个答案:

答案 0 :(得分:0)

事实证明,只需将return的{​​{1}}语句更改为有效:

parse_examples

return (res, {"task1":parsed_examples["image/label"],"task2":parsed_examples["image/label"],"task3":parsed_examples["image/label"]}) task1task2是我给定的softmax层的名称。