How to connect CNN with SVM or other classifiers

3,146 views
Skip to first unread message

Xu Zhang

unread,
Mar 6, 2017, 6:18:43 PM3/6/17
to Keras-users
We can use CNN to extract features. In normal CNN, after several convolutional layers, we need to add a flatten layer and several fully connected layers and then output classes. However, If I only want to use CNN as a feature extractor, how can I connect CNN with SVM or other classifiers? What outputs of CNN can be used as inputs of SVM? Any code examples are available? Many thanks.
 

Daπid

unread,
Mar 7, 2017, 12:43:30 AM3/7/17
to Xu Zhang, Keras-users
The traditional way is to train the full network (CNN + FC), and then
replace the FC with an SVM. The reason you need the FC layers is to
give the model a chance of optimising the CNN, because you need access
to the gradients. Once the network is fully trained, you will just
ignore the FC, extract the features, and train the SVM.

Original paper: http://deeplearning.net/wp-content/uploads/2013/03/dlsvm.pdf

Example code:

model = load_model('saved_model.h5')
layer = model.layers[-9] # Adjust here to the right depth.
tensor = layer.get_output_at(0)
f = K.function(model.inputs + [K.learning_phase()], (tensor,))
X_train_svm = f(X_train)

I know there are differentiable implementations of SVM and random
forests that you can use instead of the FC, but I haven't seen them in
action, and I haven't read any paper reporting their performance. My
guess is that they have the ability to be a bit better than the FC,
but probably computationally much more intensive.
> --
> You received this message because you are subscribed to the Google Groups
> "Keras-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to keras-users...@googlegroups.com.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/keras-users/d501e31b-93f2-4447-8a90-d15bf711f4ac%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

inde...@gmail.com

unread,
Apr 8, 2018, 6:21:39 PM4/8/18
to Keras-users
Hello! Does you found solution of your problem? I need code co put together CNN and SVM too

Marcelino Borges

unread,
Mar 3, 2021, 3:02:47 PM3/3/21
to Keras-users
Hi.

Is there any tutorial or source code with an example of how to do this?
I would like to replace the last layer of a NN by an SVM as well. The ideia is to use the NN as a feature extractor and the SVM as the classifier.

Best regards.

Lance Norskog

unread,
Mar 4, 2021, 9:22:40 PM3/4/21
to Marcelino Borges, Keras-users
The technique I've seen for this is to train an auto-encoder, then drop that second half of the network and emit the most dense representation as the embedding. I've seen this used to feed embeddings to PCA for visualizing clusters.



--
Lance Norskog
lance....@gmail.com
Redwood City, CA
Reply all
Reply to author
Forward
0 new messages