ValueError: Input 0 of layer is incompatible with the layer:



"在将数据集转换为tfrecord文件格式后,我尝试用它来训练我创建的模型,但我无法将其转换为适合该模型的输入格式。图像形状(224,224.3)。模型已经通过了预处理层无法将图像转换为模型输入

的正确形状
import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
import keras
IMG_HEIGHT = 224
IMG_WIDTH = 224
IMG_CHANNELS = 3

def decode_image(image, shape=None):
image = tf.image.decode_jpeg(image, channels=3)
image = tf.cast(image, dtype=tf.float32)
image = tf.convert_to_tensor(image, dtype=np.float32)
return image
def parse_tfrecord(example):
feature_description = {
"image" : tf.io.FixedLenFeature([], tf.string),
"height" : tf.io.FixedLenFeature([], tf.int64),
"width" : tf.io.FixedLenFeature([], tf.int64),
"depht" : tf.io.FixedLenFeature([], tf.int64),
"shape" : tf.io.VarLenFeature(tf.int64),
"label" : tf.io.FixedLenFeature([], tf.string, default_value=""),
"label_index" : tf.io.FixedLenFeature([], tf.int64)
}
example = tf.io.parse_single_example(example, feature_description)
# tf.sparse.to_dense(example["shape"]
image = decode_image(example["image"], [224,224,3])
# image = tf.image.decode_jpeg(example["image"], [224,224,3])
label_index = tf.cast(example["label_index"], tf.int32)
image = tf.reshape(image, (224,224,3))
# label_indx = rec["label_index"]
return image, label_index

def preprocess(image, label_index):
prepro_image = tf.expand_dims(image, 0)
prepro_image = keras.layers.Resizing(224 , 224, crop_to_aspect_ratio=True, input_shape=(224,224,3))(prepro_image)
prepro_image = keras.layers.Rescaling(1./255)(prepro_image)
prepro_image = keras.layers.RandomFlip(mode="horizontal_and_vertical")(prepro_image)
prepro_image = tf.squeeze(prepro_image, 0)
return prepro_image, label_index
batch_size = 32
epochs = 1
steps_per_epoch = 50
AUTOTUNE = tf.data.AUTOTUNE
def create_preproc_dataset(pattern):
trainds = tf.data.TFRecordDataset(pattern).map(parse_tfrecord).map(preprocess).shuffle(25)
return trainds

trainds = create_preproc_dataset("valid_tfrecords")

NUM_EPOCHS = 25
IMG_HEIGHT = 224
IMG_WIDTH = 224
IMG_CHANNELS = 3
CLASS_NAMES = 'bluebel buttercup crocus daffodil daisy dandelion iris lilyvalley pansy snowdrop sunflowers tulip'.split()
def train_and_evaluate(batch_size = 32,
lrate = 0.001,
l1 = 0.,
l2 = 0.,
num_hidden = 16):
regularizer = tf.keras.regularizers.l1_l2(l1, l2)

train_dataset = create_preproc_dataset(
'train_tfrecord')
eval_dataset = create_preproc_dataset(
'valid_tfrecord')


layers = [
# input layer
# image preprocessing layers
# keras.layers.Resizing(height=224, width=224,crop_to_aspect_ratio=True, input_shape=(224,224,3)),
#keras.layers.RandomFlip(mode="horizontal_and_vertical", name='random_lr_flip/none'),
#RandomColorDistortion(name='random_contrast_brightness/none'),
# core layers
keras.layers.Conv2D(32, 3, activation="relu", input_shape=(224, 224, 3)),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Conv2D(64, 3, activation="relu"),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Conv2D(128, 3, activation="relu"),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Conv2D(256, 3, activation="relu"),
keras.layers.MaxPooling2D(pool_size=2),
# flatten layers
keras.layers.Flatten(),
keras.layers.Dense(num_hidden, activation="relu"),
keras.layers.Dropout(0.5),
# output layers
keras.layers.Dense(12, activation="softmax")
]
# checkpoint and early stopping callbacks
model_checkpoint_cb = keras.callbacks.ModelCheckpoint(
filepath="work_imageschkpts",monitor = "val_accuracy", mode="max",
save_best_only=True)
early_stopping_cb = keras.callbacks.EarlyStopping(
monitor="val_accuracy", mode="max",
patience=2)
# model training
model = tf.keras.Sequential(layers, name='flower_classification')
model.build([ 224, 224, 3])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lrate),
loss=tf.keras.losses.CategoricalCrossentropy(
from_logits=False),
metrics=['accuracy'])

print(model.summary())
history = model.fit(train_dataset, validation_data=eval_dataset, 
epochs=NUM_EPOCHS,
callbacks=[model_checkpoint_cb, early_stopping_cb])
training_plot(['loss', "accuracy"], history)
return model

model = train_and_evaluate()

替换此

def create_preproc_dataset(pattern):    
trainds = tf.data.TFRecordDataset(pattern).map(parse_tfrecord).map(preprocess).shuffle(25)
return trainds

def create_preproc_dataset(pattern):    
trainds = tf.data.TFRecordDataset(pattern).map(parse_tfrecord).map(preprocess).shuffle(25).batch(32)
return trainds

并将损失更改为SparseCategoricalCrossentropy将有所帮助。

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lrate),
loss=tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=False),
metrics=['accuracy'])

相关内容

  • 没有找到相关文章

最新更新