Skip to content
Advertisement

How to add a traditional classifier(SVM) to my CNN model

here’s my model

model=Sequential()
model.add(Xception(weights='imagenet',input_shape=(224,224,3),include_top=False))
model.add(GlobalAveragePooling2D())
model.add(Dense(4096,activation='relu',name='fc1'))
model.add(Dense(4096,activation='relu',name='fc2'))
model.add(Dense(1000,activation='relu',name='fc3'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid',name='fc4'))
model.layers[0].trainable=False

i want to make svm classifier as my final classifier in this model so how can i do that? also another question i want to know the predicted class of a certain input so when i use

model.predict(x_test)

it only gives me probabilities so how can i solve that too

Advertisement

Answer

You can use neural network as feature extractor and take outputs from last layer into your SVM. Try following:

model=Sequential()
model.add(Xception(weights='imagenet',input_shape=(224,224,3),include_top=False))
model.add(GlobalAveragePooling2D())
model.add(Dense(4096,activation='relu',name='fc1'))
model.add(Dense(4096,activation='relu',name='fc2'))
model.add(Dense(1000,activation='relu',name='fc3'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid',name='fc4'))

model.compile(loss="categorical_crossentropy", optimizer="adam")
model.summary()

model.fit(X,y, epochs=10)

model.pop() # this will remove the last layer
model.summary() # check the network 

feature_mapping = model(X) 

from sklearn import svm

clf = svm.SVC()
clf.fit(feature_mapings, y)
Advertisement