我正在尝试对图像进行分类。我想通过预训练的 CNN 运行每个图像以应用卷积和池化,并最终得到一个较小的图片/矩阵,其中每个像素的值是一个特征。然后我想将它传递给 SVM 进行分类。
有人可以提供代码来开始使用 CNN 和 SVM 进行特征提取吗?
我正在尝试对图像进行分类。我想通过预训练的 CNN 运行每个图像以应用卷积和池化,并最终得到一个较小的图片/矩阵,其中每个像素的值是一个特征。然后我想将它传递给 SVM 进行分类。
有人可以提供代码来开始使用 CNN 和 SVM 进行特征提取吗?
这是一些入门代码:
from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
import numpy as np
# define the CNN network
# Here we are using 19 layer CNN -VGG19 and initialising it
# with pretrained imagenet weights
base_model = VGG19(weights='imagenet')
# Extract features from an arbitrary intermediate layer
# like the block4 pooling layer in VGG19
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
# load an image and preprocess it
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# get the features
block4_pool_features = model.predict(x)
然后,您可以使用这些功能传递给 SVM 分类器。这里有一些额外的链接供参考:
Understanding CNNS
Keras pretrained networks
Coding a convolutional neural network in keras
任何人都可以建议使用代码或 Python 中的 CNN 入门指南吗?
如果你是 CNN 的菜鸟,推荐的起点是 Keras。代码不言自明,易于理解。这是一个关于使用 Keras 进行面部图像教程的简单分类相关任务的链接。你也可以从MNIST 分类入手,了解 CNN 的核心概念。一旦您了解了基础知识,您就可以尝试使用其他库,如 Tensorflow、Theano、Pytorch 等。