完成GeneratorDatase时发生Tensorflow错误

2024-05-03 09:50:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用tensorflow制作一个图像识别系统。我导入了带标签的数据:

train_batches = ImageDataGenerator().flow_from_directory(train_path, target_size=(100,100), classes=['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'suprise'], batch_size=10)
test_batches = ImageDataGenerator().flow_from_directory(test_path, target_size=(100,100),   classes=['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'suprise'], batch_size=10)

然后导入我正在重新培训的模型:

vgg16_model = tensorflow.keras.applications.vgg16.VGG16(weights='imagenet', include_top=False, 
input_tensor=Input(shape=(100,100,3)))

for layer in vgg16_model.layers[:-4]:
    layer.trainable = False

model = Sequential()
model.add(vgg16_model)
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))

然后我试着训练模型:

model.compile(loss='categorical_crossentropy',
              optimizer=tensorflow.keras.optimizers.RMSprop(lr=1e-4),
              metrics=['acc'])

history = model.fit(
      train_batches,
      steps_per_epoch=train_batches.samples/train_batches.batch_size ,
      epochs=5,)

运行此操作时,我收到一个错误:

W tensorflow/core/kernels/data/generator_dataset_op.cc:103] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter state is not initialized. The process may be terminated.  
     [[{{node PyFunc}}]]

什么可能导致此错误?我应该更改什么来修复它


Tags: pathfromaddtargetsizemodeltensorflowbatch