# Pre-trained CNN for feature extraction [closed]

I'm trying to classify images. I want to run each image through a pretrained CNN to apply convolution and pooling and end up with a smaller picture/matrix where the value of each pixel is a feature. Then I want to pass that to an SVM for classification.

Can someone provide code to get started with feature extraction with CNN and SVM ?

Here's some starter code:


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'
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)



You can then use these features for passing to a SVM classfier. Here are some additional links for reference:
Understanding CNNS
Keras pretrained networks
Coding a convolutional neural network in keras

Can anyone suggest code to use or a guide for getting started with CNNs in Python?

If you are a noob to CNN, Keras is the recommended starting point. The code is self-explanatory and easy to understand. Here is a link to a simple classification related task on facial images tutorial using Keras. You can also start with MNIST classification and understand the core concepts of CNN. Once you understand the fundamentals, you can try and experiment with other libraries like Tensorflow, Theano, Pytorch...etc.