DOI PyPI

This project was inspired by Y. Tang’s Deep Learning using Linear Support Vector Machines (2013).

The full paper on this project may be read at arXiv.org, ResearchGate, and Academia.edu.

Abstract

Convolutional neural networks (CNNs) are similar to “ordinary” neural networks in the sense that they are made up of hidden layers consisting of neurons with “learnable” parameters. These neurons receive inputs, performs a dot product, and then follows it with a non-linearity. The whole network expresses the mapping between raw image pixels and their class scores. Conventionally, the Softmax function is the classifier used at the last layer of this network. However, there have been studies (Alalshekmubarak and Smith, 2013; Agarap, 2017; Tang, 2013) conducted to challenge this norm. The cited studies introduce the usage of linear support vector machine (SVM) in an artificial neural network architecture. This project is yet another take on the subject, and is inspired by (Tang, 2013). Empirical data has shown that the CNN-SVM model was able to achieve a test accuracy of ~99.04% using the MNIST dataset (LeCun, Cortes, and Burges, 2010). On the other hand, the CNN-Softmax was able to achieve a test accuracy of ~99.23% using the same dataset. Both models were also tested on the recently-published Fashion-MNIST dataset (Xiao, Rasul, and Vollgraf, 2017), which is suppose to be a more difficult image classification dataset than MNIST (Zalandoresearch, 2017). This proved to be the case as CNN-SVM reached a test accuracy of ~90.72%, while the CNN-Softmax reached a test accuracy of ~91.86%. The said results may be improved if data preprocessing techniques were employed on the datasets, and if the base CNN model was a relatively more sophisticated than the one used in this study.

Usage

First, clone the project.

git clone https://github.com/AFAgarap/cnn-svm.git/

Run the setup.sh to ensure that the pre-requisite libraries are installed in the environment.

sudo chmod +x setup.sh
./setup.sh

Program parameters.

usage: main.py [-h] -m MODEL -d DATASET [-p PENALTY_PARAMETER] -c
               CHECKPOINT_PATH -l LOG_PATH

CNN & CNN-SVM for Image Classification

optional arguments:
  -h, --help            show this help message and exit

Arguments:
  -m MODEL, --model MODEL
                        [1] CNN-Softmax, [2] CNN-SVM
  -d DATASET, --dataset DATASET
                        path of the MNIST dataset
  -p PENALTY_PARAMETER, --penalty_parameter PENALTY_PARAMETER
                        the SVM C penalty parameter
  -c CHECKPOINT_PATH, --checkpoint_path CHECKPOINT_PATH
                        path where to save the trained model
  -l LOG_PATH, --log_path LOG_PATH
                        path where to save the TensorBoard logs

Then, go to the repository’s directory, and run the main.py module as per the desired parameters.

cd cnn-svm
python3 main.py --model 2 --dataset ./MNIST_data --penalty_parameter 1 --checkpoint_path ./checkpoint --log_path ./logs

Results

The hyperparameters used in this project were manually assigned, and not through optimization.

Hyperparameters CNN-Softmax CNN-SVM
Batch size 128 128
Learning rate 1e-3 1e-3
Steps 10000 10000
SVM C N/A 1

The experiments were conducted on a laptop computer with Intel Core(TM) i5-6300HQ CPU @ 2.30GHz x 4, 16GB of DDR3 RAM, and NVIDIA GeForce GTX 960M 4GB DDR5 GPU.

Figure 1. Training accuracy (left) and loss (right) of CNN-Softmax and CNN-SVM on image classification using MNIST.

The orange plot refers to the training accuracy and loss of CNN-Softmax, with a test accuracy of 99.22999739646912%. On the other hand, the blue plot refers to the training accuracy and loss of CNN-SVM, with a test accuracy of 99.04000163078308%. The results do not corroborate the findings of Tang (2017) for MNIST handwritten digits classification. This may be attributed to the fact that no data preprocessing nor dimensionality reduction was done on the dataset for this project.

Figure 2. Training accuracy (left) and loss (right) of CNN-Softmax and CNN-SVM on image classification using Fashion-MNIST.

The red plot refers to the training accuracy and loss of CNN-Softmax, with a test accuracy of 91.86000227928162%. On the other hand, the light blue plot refers to the training accuracy and loss of CNN-SVM, with a test accuracy of 90.71999788284302%. The result on CNN-Softmax corroborates the finding by zalandoresearch on Fashion-MNIST.

Citation

To cite the paper, kindly use the following BibTex entry:

@article{agarap2017architecture,
  title={An Architecture Combining Convolutional Neural Network (CNN) and Support Vector Machine (SVM) for Image Classification},
  author={Agarap, Abien Fred},
  journal={arXiv preprint arXiv:1712.03541},
  year={2017}
}

To cite the repository/software, kindly use the following BibTex entry:

@misc{abien_fred_agarap_2017_1098369,
  author       = {Abien Fred Agarap},
  title        = {AFAgarap/cnn-svm v0.1.0-alpha},
  month        = dec,
  year         = 2017,
  doi          = {10.5281/zenodo.1098369},
  url          = {https://doi.org/10.5281/zenodo.1098369}
}

License

Copyright 2017 Abien Fred Agarap

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

PyPI

[code][paper]

Abstract

This paper presents a comparison of six machine learning (ML) algorithms: GRU-SVM[4], Linear Regression, Multilayer Perceptron (MLP), Nearest Neighbor (NN) search, Softmax Regression, and Support Vector Machine (SVM) on the Wisconsin Diagnostic Breast Cancer (WDBC) dataset [22] by measuring their classification test accuracy and their sensitivity and specificity values. The said dataset consists of features which were computed from digitized images of FNA tests on a breast mass[22]. For the implementation of the ML algorithms, the dataset was partitioned in the following fashion: 70% for training phase, and 30% for the testing phase. The hyper-parameters used for all the classifiers were manually assigned. Results show that all the presented ML algorithms performed well (all exceeded 90% test accuracy) on the classification task. The MLP algorithm stands out among the implemented algorithms with a test accuracy of ~99.04% Lastly, the results are comparable with the findings of the related studies[18 , 23].

Citation

@article{agarap2017breast,
  title={On Breast Cancer Detection: An Application of Machine Learning Algorithms on the Wisconsin Diagnostic Dataset},
  author={Agarap, Abien Fred},
  journal={arXiv preprint arXiv:1711.07831},
  year={2017}
}

Results

All experiments in this study were conducted on a laptop computer with Intel Core(TM) i5-6300HQ CPU @ 2.30GHz x 4, 16GB of DDR3 RAM, and NVIDIA GeForce GTX 960M 4GB DDR5 GPU.

Figure 1. Training accuracy of the machine learning algorithms on breast cancer detection using WDBC.

Figure 1 shows the training accuracy of the ML algorithms: (1) GRU-SVM finished its training in 2 minutes and 54 seconds with an average training accuracy of 90.6857639%, (2) Linear Regression finished its training in 35 seconds with an average training accuracy of 92.8906257%, (3) MLP finished its training in 28 seconds with an average training accuracy of 96.9286785%, (4) Softmax Regression finished its training in 25 seconds with an average training accuracy of 97.366573%, and (5) L2-SVM finished its training in 14 seconds with an average training accuracy of 97.734375%. There was no recorded training accuracy for Nearest Neighbor search since it does not require any training, as the norm equations (L1 and L2) are directly applied on the dataset to determine the “nearest neighbor” of a given data point p_{i} ∈ p.


Table 1. Summary of experiment results on the machine learning algorithms.

Parameter GRU-SVM Linear Regression MLP L1-NN L2-NN Softmax Regression L2-SVM
Accuracy 93.75% ~96.1% ~99.04% ~93.57% ~94.74% ~97.66% ~96.09%
Data points 384000 384000 512896 171 171 384000 384000
Epochs 3000 3000 3000 1 1 3000 3000
FPR ~16.67% ~10.20% ~1.27% 6.25% ~9.38% ~5.77% ~6.38%
FNR 0 0 ~0.79% ~6.54% ~2.80% 0 ~2.47%
TPR 100% 100% ~99.21% ~93.46% ~97.2% 100% ~97.53%
TNR ~83.33% ~89.8% ~98.73% 93.75% ~90.63% ~94.23% ~93.62%

Table 1 summarizes the results of the experiment on the ML algorithms. The parameters recorded were test accuracy, number of data points (epochs * dataset_size), epochs, false positive rate (FPR), false negative rate (FNR), true positive rate (FPR), and true negative rate (TNR). All code implementations of the algorithms were written using Python with TensorFlow as the machine intelligence library.

License

Copyright 2017 Abien Fred Agarap

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.