tensorflow 深度学习CNN ValueError:未在未知TensorShape上定义as_list()

r7knjye2  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(142)

我已经加载了数据集,并使用以下代码对图像数据进行了预处理:

data = tf.keras.utils.image_dataset_from_directory('/content/drive/MyDrive/PengantarSainsData/Capstone2/dataset_revisi')
data_iterator = data.as_numpy_iterator()
batch = data_iterator.next()
def preprocess(x, y):
    x_normalized = x / 255
    y_one_hot = tf.keras.utils.to_categorical(y, num_classes=5)
    return x_normalized, y_one_hot
data = data.map(lambda x, y: tf.py_function(func=preprocess, inp=[x, y], Tout=[tf.float32, tf.float32]))
scaled_iterator = data.as_numpy_iterator()
batch = scaled_iterator.next()

字符串
然后使用以下代码将其划分为训练,验证和测试数据:

train_size = int(len(data) * .7)
val_size = int(len(data) * .2)
test_size = int(len(data) * .1)
train = data.take(train_size)
val = data.skip(train_size).take(val_size)
test = data.skip(train_size + val_size).take(test_size)


还创建了模型架构,如下所示:

model.add(Conv2D(16, (3, 3), 1, activation = 'relu', input_shape = (256, 256, 3)))
model.add(MaxPooling2D())

model.add(Conv2D(32, (3, 3), 1, activation = 'relu'))
model.add(MaxPooling2D())

model.add(Conv2D(16, (3, 3), 1, activation = 'relu'))
model.add(MaxPooling2D())

model.add(Flatten())
model.add(Dense(128, activation = 'relu')) # 256 number of units used in dense layer
model.add(Dense(5, activation = 'softmax')) # sigmoid represents 0 and 1 output

model.compile('adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

model.summary()


那么当我想进行训练的时候

logdir = 'logs'
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir = logdir)
hist = model.fit(train, epochs = 16, validation_data = val, callbacks = [tensorboard_callback])


它不断抛出错误,作为附件的图片。我该怎么办?一直在与此斗争2天

我尝试了一个不同的模型,并使用下面的代码来更具体地定义其架构model.build(input_shape=(None, 256, 256, 3)),但它仍然不起作用

vxf3dgd4

vxf3dgd41#

我认为你用as_numpy_iterator做了一些不必要的步骤。你的image_dataset_from_directory是一个tf.data.Dataset,你可以直接操作它而不用把它变成一个NumPy迭代器。你只需要使用tf.one_hot而不是tf.keras.utils.to_categorical
这里有一个完整的例子,使用MNIST的本地版本(你必须改变目录的路径和数量:

import tensorflow as tf
from tensorflow.keras.layers import *

data = tf.keras.utils.image_dataset_from_directory(r'path\to\mnist\test')

def preprocess(x, y):
    x_normalized = x / 255
    y_one_hot = tf.one_hot(tf.cast(y, tf.int32), depth=10)
    return x_normalized, y_one_hot

data = data.map(preprocess)

train_size = int(len(data) * .7)
val_size = int(len(data) * .2)
test_size = int(len(data) * .1)
train = data.take(train_size)
val = data.skip(train_size).take(val_size)
test = data.skip(train_size + val_size).take(test_size)

model = tf.keras.models.Sequential()
model.add(Conv2D(16, (3, 3), 1, activation='relu', input_shape=(256, 256, 3)))
model.add(MaxPooling2D())

model.add(Conv2D(32, (3, 3), 1, activation='relu'))
model.add(MaxPooling2D())

model.add(Conv2D(16, (3, 3), 1, activation='relu'))
model.add(MaxPooling2D())

model.add(Flatten())
model.add(Dense(128, activation='relu'))   
model.add(Dense(10, activation='softmax')) 

model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])

model.summary()

logdir = 'logs'
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)
hist = model.fit(train, epochs=1, validation_data=val, callbacks=[tensorboard_callback])

字符串

相关问题