我正在尝试使用keras(tensorflow后端,版本1.14.0)运行相对简单的CNN。这是上下文的代码。
from keras.layers import Dense, Conv2D, MaxPooling2D, BatchNormalization, GlobalAveragePooling2D
from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint
from keras import models
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from keras.models import load_model
import numpy as np
# starting point
my_model = models.Sequential()
# Add first convolutional block
my_model.add(Conv2D(16, (3, 3), activation='relu', padding='same',
input_shape=(224, 224, 3)))
my_model.add(MaxPooling2D((2, 2), padding='same'))
# second block
my_model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
my_model.add(MaxPooling2D((2, 2), padding='same'))
# third block
my_model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
my_model.add(MaxPooling2D((2, 2), padding='same'))
# fourth block
my_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
my_model.add(MaxPooling2D((2, 2), padding='same'))
# global average pooling
my_model.add(GlobalAveragePooling2D())
# fully connected layer
my_model.add(Dense(64, activation='relu'))
my_model.add(BatchNormalization())
# make predictions
my_model.add(Dense(2, activation='sigmoid'))
# Show a summary of the model. Check the number of trainable parameters
my_model.summary()
# use early stopping to optimally terminate training through callbacks
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)
# save best model automatically
mc = ModelCheckpoint("C:/Users/ab123/Desktop/vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True,
save_weights_only=False, mode='auto', period=1)
cb_list = [mc, es]
# compile model
my_model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
# set up data generator
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
# get batches of training images from the directory
train_generator = data_generator.flow_from_directory(
'D:/Project2020/Step2a/train',
target_size=(224, 224),
batch_size=10,
class_mode='categorical')
# get batches of validation images from the directory
validation_generator = data_generator.flow_from_directory(
'D:/Project2020/Step2a/val',
target_size=(224, 224),
batch_size=10,
class_mode='categorical')
history = my_model.fit_generator(
train_generator,
epochs=1,
steps_per_epoch=2000,
validation_data=validation_generator,
validation_steps=1000, callbacks=cb_list)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.ylim([.5,1.1])
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.savefig("C:/Users/ab123/Desktop/11-16-19model.png", dpi=300)
# load a saved model
import os
saved_model = load_model('C:/Users/ab123/Desktop/11-16-19model.h5')
# generate data for test set of images
test_generator = data_generator.flow_from_directory(
'C:/Users/aeshon/Downloads/birds',
target_size=(224, 224),
batch_size=1,
class_mode='categorical',
shuffle=False)
# obtain predicted activation values for the last dense layer
test_generator.reset()
pred = saved_model.predict_generator(test_generator, verbose=1, steps=100)
# determine the maximum activation value for each sample
predicted_class_indices=np.argmax(pred,axis=1)
# label each predicted value to correct gender
labels = (test_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]
# format file names to simply male or female
filenames=test_generator.filenames
filenz=[0]
for i in range(0,len(filenames)):
filenz.append(filenames[i].split('\\')[0])
filenz=filenz[1:]
# determine the test set accuracy
match=[]
for i in range(0,len(filenames)):
match.append(filenz[i]==predictions[i])
match.count(True)/100
该模型像超级按钮一样工作,直到触发“提前停止”功能或训练结束为止。之后,它会引发此错误(带有追溯)。
Traceback (most recent call last):
File "D:/Invasive Species Detector/11-16-19 model.py", line 77, in <module>
validation_steps=1000, callbacks=cb_list)
File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\keras\engine\training_generator.py", line 264, in fit_generator
callbacks.on_train_end()
File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\keras\callbacks.py", line 142, in on_train_end
callback.on_train_end(logs)
File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\tensorflow\python\keras\callbacks.py", line 940, in on_train_end
if self.model._ckpt_saved_epoch is not None:
AttributeError: 'Sequential' object has no attribute '_ckpt_saved_epoch'
自然,我首先环顾四周,看看以前是否有人遇到过相同的错误。除了一个拥有KerasTPU对象的人之外,没有其他人,但是没有人回答他关于SO的问题,因此我不知道如何解决该问题。我希望有人可以解决这个问题,请在评论中问我是否提供其他信息,例如版本等。
答案 0 :(得分:0)
在导入中,您正在keras
和tf.keras
软件包之间混合导入,这不受支持,并且会产生奇怪的错误,就像您得到的错误一样。解决方案很简单,选择一个包装并从中进行所有相关的导入。