【问题标题】:AttributeError: 'function' object has no attribute 'predict' while using Alexnet in KerasAttributeError:在 Keras 中使用 Alexnet 时,“函数”对象没有属性“预测”
【发布时间】:2020-01-07 00:33:59
【问题描述】:

我需要使用 Alexnet 模型进行图像分类任务。我从这个source 中获取了架构实现。我想直接应用带有 imagenet 权重的模型(不需要微调)并获得 imageNet 数据集的一些预测。代码如下:

def alexnet_model(img_shape=(224, 224, 3), n_classes=1000, l2_reg=0.,
weights='/content/drive/My Drive/cbir/models_cnn/alexnet_weights.hdf'):

    # Initialize model
    alexnet = Sequential()

    # Layer 1
    alexnet.add(Conv2D(96, (11, 11), input_shape=img_shape,
    padding='same', kernel_regularizer=l2(l2_reg)))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('relu'))
    alexnet.add(MaxPooling2D(pool_size=(2, 2)))

    # Layer 2
    alexnet.add(Conv2D(256, (5, 5), padding='same'))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('relu'))
    alexnet.add(MaxPooling2D(pool_size=(2, 2)))

    # Layer 3
    alexnet.add(ZeroPadding2D((1, 1)))
    alexnet.add(Conv2D(512, (3, 3), padding='same'))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('relu'))
    alexnet.add(MaxPooling2D(pool_size=(2, 2)))

    # Layer 4
    alexnet.add(ZeroPadding2D((1, 1)))
    alexnet.add(Conv2D(1024, (3, 3), padding='same'))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('relu'))

    # Layer 5
    alexnet.add(ZeroPadding2D((1, 1)))
    alexnet.add(Conv2D(1024, (3, 3), padding='same'))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('relu'))
    alexnet.add(MaxPooling2D(pool_size=(2, 2)))

    # Layer 6
    alexnet.add(Flatten())
    alexnet.add(Dense(3072))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('relu'))
    alexnet.add(Dropout(0.5))

    # Layer 7
    alexnet.add(Dense(4096))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('relu'))
    alexnet.add(Dropout(0.5))

   # Layer 8
    alexnet.add(Dense(n_classes))
    alexnet.add(BatchNormalization())
    alexnet.add(Activation('softmax'))

    if weights is not None:
      alexnet.load_weights(weights)


    return alexnet.compile()

之后,我运行:

model =  alexnet_model
target_size =(224,224)
img = load_img(imagePath, target_size=target_size)
img = img_to_array(img)
img = img.reshape((1, img.shape[0], img.shape[1], img.shape[2]))
img = preprocess_input(img)
y=model.predict(img)[0]

我收到此错误:

AttributeError: 'function' 对象没有属性 'predict'

【问题讨论】:

    标签: python keras conv-neural-network pre-trained-model


    【解决方案1】:

    你没有打电话给alexnet_model

    model = alexnet_model()
    

    改为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多