我的机器学习项目遇到问题。我制作了一个CNN模型,我想在VGG16 VGG19 restnet模型和其他模型中对其进行测试,这是我认为将使其起作用的代码,我可以将vgg16更改为其他模型。但是,我不断收到此错误:
C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ pythonw.exe C:/Users/Acer/PycharmProjects/condas/rawr.py 使用TensorFlow后端 追溯(最近一次通话): 在第9行的文件“ C:/Users/Acer/PycharmProjects/condas/rawr.py” my_new_model.add(vgg16.VGG16(include_top = False,pooling ='avg',weights = resnet_weights_path)) VGG16中的第97行的文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras_applications \ vgg16.py” data_format = backend.image_data_format(), AttributeError:“ NoneType”对象没有属性“ image_data_format”
以退出代码1完成的过程
from keras.applications.vgg16 import vgg16
from keras.models import Sequential
from keras.layers import Dense, Flatten, GlobalAveragePooling2D
num_classes = 2
resnet_weights_path = 'C:/Users/Acer/imagerec/EDA'
my_new_model = Sequential()
my_new_model.add(vgg16.VGG16(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))
my_new_model.layers[0].trainable = False
my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
image_size = 224
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = data_generator.flow_from_directory(
'C:/Users/Acer/imagerec/EDA',
target_size=(image_size, image_size),
batch_size=20,
class_mode='categorical')
validation_generator = data_generator.flow_from_directory(
'C:/Users/Acer/imagerec/EDA',
target_size=(image_size, image_size),
class_mode='categorical')
my_new_model.fit_generator(
train_generator,
steps_per_epoch=3,
validation_data=validation_generator,
validation_steps=1)
答案 0 :(得分:2)
您需要致电vgg16.VGG16
而不是vgg16