我是tensorflow的初学者。我想建立一个简单的模型,但出现此错误。 我认为这是因为标签,但我不知道如何解决。 我使用tf.data.Dataset从目录文件中构建数据集。
这是数据集:
访问:https://i.stack.imgur.com/H2EQT.jpg
数据
data_dir = os.path.join(os.path.dirname('D:/Downloads/Image data set/'), 'raw-img')
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*.*')))
list_ds = tf.data.Dataset.list_files(str(data_dir / '*/*'), shuffle=False)
list_ds = list_ds.shuffle(image_count, reshuffle_each_iteration=False)
train_size = int(image_count * 0.8)
test_size = int(image_count * 0.1)
val_size = int(image_count * 0.1)
train_ds = list_ds.take(train_size)
val_ds = list_ds.skip(train_size)
test_ds = val_ds.skip(test_size)
val_ds = val_ds.take(test_size)
def parse_image(filename):
parts = tf.strings.split(filename, os.sep)
label = tf.cast(parts[-2] == class_names, tf.float32)
label = tf.argmax(label)
image = tf.io.read_file(filename)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.reshape(image, [32, 150, 150, 3])
return image, labels
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_ds = train_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
val_ds = val_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
test_ds = test_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
model = tf.keras.Sequential([
kr.layers.Conv2D(16, 3, activation='relu', input_shape=(150, 150 ,3)),
kr.layers.MaxPooling2D(),
kr.layers.Conv2D(32, 3, activation='relu'),
kr.layers.MaxPooling2D(),
kr.layers.Conv2D(64, 3, activation='relu'),
kr.layers.MaxPooling2D(),
kr.layers.Flatten(),
kr.layers.Dense(128, activation='softmax'),
kr.layers.Dense(10)
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(
train_ds,
epochs=3
)
错误: 当我想拟合模型时,这是错误。
Train for 20943 steps
Epoch 1/3
1/20943 [..............................] - ETA: 1:14:41
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-38-c408313d0649> in <module>
1 model.fit(
2 train_ds,
----> 3 epochs=3
4 )
.
.
.
.
ValueError: Shape mismatch: The shape of labels (received (320,)) should equal the shape of logits except for the last dimension (received (32, 10)).
答案 0 :(得分:0)
使用批处理大小和step_per_epoch变量解决了问题。
train_ds = train_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
train_ds = train_ds.cache()
train_ds = train_ds.batch(BATCH_SIZE, drop_remainder=True).repeat()
train_ds = train_ds.prefetch(buffer_size=AUTOTUNE)
和火车
history = model.fit(train_ds, steps_per_epoch = train_size // BATCH_SIZE,
epochs = EPOCHS, batch_size = BATCH_SIZE,
validation_data = val_ds, validation_steps = val_size // BATCH_SIZE)
并已解决。