model.fit给出InvalidArgumentError:图形执行错误:



我的代码如下:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
#import os
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=(32,32, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Dropout(0.5))
model.add(Dense(27))
model.add(Activation('sigmoid'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
batch_size = 5
# Training Augmentation configuration
train_datagen = ImageDataGenerator(rescale = 1./255, 
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = False)
# Testing Augmentation - Only Rescaling
test_datagen = ImageDataGenerator(rescale = 1./255)
# Generates batches of Augmented Image data
train_generator = train_datagen.flow_from_directory('D:/college_project/resources/training/', 
target_size = (64, 64), 
batch_size = batch_size,
class_mode = 'categorical') 
# Generator for validation data
validation_generator = test_datagen.flow_from_directory('D:/college_project/resources/testing/', 
target_size = (64, 64),
batch_size = batch_size,
class_mode = 'categorical')
# Fit the model on Training data
model.fit(train_generator, epochs=5, validation_data=validation_generator)

# Evaluating model performance on Testing data
loss, accuracy = model.evaluate(validation_generator)
print("nModel's Evaluation Metrics: ")
print("---------------------------")
print("Accuracy: {} nLoss: {}".format(accuracy, loss))```

我正在进行图像分类,但我得到了这个错误:

Traceback (most recent call last):
File "D:college_projectmodulestraing example.py", line 56, in <module>
`model.fit(train_generator, epochs=5, validation_data=validation_generator)`
File "C:Usersshubhanaconda3libsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:Usersshubhanaconda3libsite-packagestensorflowpythoneagerexecute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
InvalidArgumentError: Graph execution error:

在将输出提供给输出层之前,您需要在最后一个MaxPooling2D层之后使输出变平(确保输出为1D(。由于您使用categorical_crossentropy作为损失函数,因此应使用softmax激活函数而不是sigmoid。此外,输出层中的27个节点意味着您有27个不同的类。检查一下是否真的是这样。下面是一个工作示例:

import tensorflow as tf
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator

flowers = tf.keras.utils.get_file(
'flower_photos',
'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
untar=True)
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255, 
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = False)

train_generator = train_datagen.flow_from_directory(directory = flowers,
batch_size = 32,
target_size = (32, 32),
seed = 42, class_mode='categorical')
model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=(32,32, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
model.fit(train_generator, epochs=5)

相关内容

  • 没有找到相关文章

最新更新