1
0

Merge branch 'master' of github.com:Dekker1/ResearchMethods

This commit is contained in:
Kelvin Davis 2018-05-25 14:52:43 +10:00
commit 144347e2a5
4 changed files with 220 additions and 80 deletions

BIN
mini_proj/Waldo.h5 Normal file

Binary file not shown.

View File

@ -1,9 +1,11 @@
@misc{openData,
title={Open Database License (ODbL) v1.0},
url={https://opendatacommons.org/licenses/odbl/1.0/},
journal={Open Data Commons},
year={2018},
month={Feb}
Classical Machine Learning
@article{MLReview,
title={Supervised machine learning: A review of classification techniques},
author={Kotsiantis, Sotiris B and Zaharakis, I and Pintelas, P},
journal={Emerging artificial intelligence applications in computer engineering},
volume={160},
pages={3--24},
year={2007}
}
@techreport{knn,
title={Discriminatory analysis-nonparametric discrimination: consistency properties},
@ -21,6 +23,14 @@
year={1995},
publisher={Springer}
}
@inproceedings{svmnonlinear,
title={A training algorithm for optimal margin classifiers},
author={Boser, Bernhard E and Guyon, Isabelle M and Vapnik, Vladimir N},
booktitle={Proceedings of the fifth annual workshop on Computational learning theory},
pages={144--152},
year={1992},
organization={ACM}
}
@article{naivebayes,
title={Idiot's Bayes—not so stupid after all?},
author={Hand, David J and Yu, Keming},
@ -40,19 +50,79 @@
pages={18--22},
year={2002}
}
@article{Kotsiantis2007,
abstract = {Supervised machine learning is the search for algorithms that reason from externally supplied instances to produce general hypotheses, which then make predictions about future instances. In other words, the goal of supervised learning is to build a concise model of the distribution of class labels in terms of predictor features. The resulting classifier is then used to assign class labels to the testing instances where the values of the predictor features are known, but the value of the class label is unknown. This paper describes various supervised machine learning classification techniques. Of course, a single article cannot be a complete review of all supervised machine learning classification algorithms (also known induction classification algorithms), yet we hope that the references cited will cover the major theoretical issues, guiding the researcher in interesting research directions and suggesting possible bias combinations that have yet to be explored.},
author = {Kotsiantis, Sotiris B.},
doi = {10.1115/1.1559160},
file = {:home/kelvin/.local/share/data/Mendeley Ltd./Mendeley Desktop/Downloaded/Kotsiantis - 2007 - Supervised machine learning A review of classification techniques.pdf:pdf},
isbn = {1586037803},
issn = {09226389},
journal = {Informatica},
keywords = {algorithms analysis classifiers computational conn,classifiers,data mining techniques,intelligent data analysis,learning algorithms},
mendeley-groups = {CS Proj/ML,CS Proj,Thesis,Thesis/ML},
pages = {249--268},
title = {{Supervised machine learning: A review of classification techniques}},
url = {http://books.google.com/books?hl=en{\&}lr={\&}id=vLiTXDHr{\_}sYC{\&}oi=fnd{\&}pg=PA3{\&}dq=survey+machine+learning{\&}ots=CVsyuwYHjo{\&}sig=A6wYWvywU8XTc7Dzp8ZdKJaW7rc{\%}5Cnpapers://5e3e5e59-48a2-47c1-b6b1-a778137d3ec1/Paper/p800{\%}5Cnhttp://www.informatica.si/PDF/31-3/11{\_}Kotsiantis - S},
volume = {31},
year = {2007}
Neural Networks
@article{lenet,
title={Gradient-based learning applied to document recognition},
author={LeCun, Yann and Bottou, L{\'e}on and Bengio, Yoshua and Haffner, Patrick},
journal={Proceedings of the IEEE},
volume={86},
number={11},
pages={2278--2324},
year={1998},
publisher={IEEE}
}
@inproceedings{alexnet,
title={Imagenet classification with deep convolutional neural networks},
author={Krizhevsky, Alex and Sutskever, Ilya and Hinton, Geoffrey E},
booktitle={Advances in neural information processing systems},
pages={1097--1105},
year={2012}
}
@inproceedings{lenetVSalexnet,
title={On the Performance of GoogLeNet and AlexNet Applied to Sketches.},
author={Ballester, Pedro and de Ara{\'u}jo, Ricardo Matsumura},
booktitle={AAAI},
pages={1124--1128},
year={2016}
}
@article{deepNN,
title = "A survey of deep neural network architectures and their applications",
journal = "Neurocomputing",
volume = "234",
pages = "11 - 26",
year = "2017",
issn = "0925-2312",
doi = "https://doi.org/10.1016/j.neucom.2016.12.038",
url = "http://www.sciencedirect.com/science/article/pii/S0925231216315533",
author = "Weibo Liu and Zidong Wang and Xiaohui Liu and Nianyin Zeng and Yurong Liu and Fuad E. Alsaadi",
keywords = "Autoencoder, Convolutional neural network, Deep learning, Deep belief network, Restricted Boltzmann machine"
}
MISC
@misc{openData,
title={Open Database License (ODbL) v1.0},
url={https://opendatacommons.org/licenses/odbl/1.0/},
journal={Open Data Commons},
year={2018},
month={Feb}
}
@incollection{NIPS2012_4824,
title = {ImageNet Classification with Deep Convolutional Neural Networks},
author = {Alex Krizhevsky and Sutskever, Ilya and Hinton, Geoffrey E},
booktitle = {Advances in Neural Information Processing Systems 25},
editor = {F. Pereira and C. J. C. Burges and L. Bottou and K. Q. Weinberger},
pages = {1097--1105},
year = {2012},
publisher = {Curran Associates, Inc.},
url = {http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf}
}
@ARTICLE{726791,
author={Y. Lecun and L. Bottou and Y. Bengio and P. Haffner},
journal={Proceedings of the IEEE},
title={Gradient-based learning applied to document recognition},
year={1998},
volume={86},
number={11},
pages={2278-2324},
keywords={backpropagation;convolution;multilayer perceptrons;optical character recognition;2D shape variability;GTN;back-propagation;cheque reading;complex decision surface synthesis;convolutional neural network character recognizers;document recognition;document recognition systems;field extraction;gradient based learning technique;gradient-based learning;graph transformer networks;handwritten character recognition;handwritten digit recognition task;high-dimensional patterns;language modeling;multilayer neural networks;multimodule systems;performance measure minimization;segmentation recognition;Character recognition;Feature extraction;Hidden Markov models;Machine learning;Multi-layer neural network;Neural networks;Optical character recognition software;Optical computing;Pattern recognition;Principal component analysis},
doi={10.1109/5.726791},
ISSN={0018-9219},
month={Nov},}
@book{numpy,
title={A guide to NumPy},
author={Oliphant, Travis E},
volume={1},
year={2006},
publisher={Trelgol Publishing USA}
}

View File

@ -99,14 +99,12 @@
architectures, as this method is currently the most used for image
classification.
\textbf{
\todo{
\\A couple of papers that may be useful (if needed):
- LeNet: http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf
- AlexNet: http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks
- General comparison of LeNet and AlexNet:
"On the Performance of GoogLeNet and AlexNet Applied to Sketches", Pedro Ballester and Ricardo Matsumura Araujo
- Deep NN Architecture:
https://www-sciencedirect-com.ezproxy.lib.monash.edu.au/science/article/pii/S0925231216315533
- LeNet: \cite{lenet}
- AlexNet: \cite{alexnet}
- General comparison of LeNet and AlexNet:\cite{lenetVSalexnet}
- Deep NN Architecture:\cite{deepNN}
}
\subsection{Classical Machine Learning Methods}
@ -114,7 +112,7 @@
The following paragraphs will give only brief descriptions of the different
classical machine learning methods used in this reports. For further reading
we recommend reading ``Supervised machine learning: A review of
classification techniques'' \cite{Kotsiantis2007}.
classification techniques'' \cite{MLReview}.
\paragraph{Naive Bayes Classifier}
@ -142,60 +140,82 @@
\paragraph{Support Vector Machine}
\cite{svm}
(SVM) \cite{svm} has been very successful in many classification tasks. The
method is based on finding boundaries between the different classes. The
boundaries are defined as functions on the features of the instances. The
boundaries are optimized to have the most amount of space between the
boundaries and the training instances on both sides. Originally the
boundaries where linear functions, but more recent development allows for
the training of non-linear boundaries~\cite{svmnonlinear}. Once the training
has defined the boundaries new instances are classified according to on
which side of the boundary they belong.
\paragraph{Random Forest}
\cite{randomforest}
\cite{randomforest} is a method that is based on classifications decision
trees. In a decision tree a new instances is classified by going down a
(binary) tree. Each non-leaf node contain a selection criteria to its
branches. Every leaf node contains the class that will be assigned to the
instance if the node is reached. In other training methods, decision trees
have the tendency to overfit, but in random forest a multitude of decision
tree is trained with a certain degree of randomness and the mean of these
trees is used which avoids this problem.
\subsection{Neural Network Architectures}
\todo{Did we only do the three in the end? (Alexnet?)}
Yeah, we implemented the LeNet architecture, then improved on it for a fairly standar convolutional neural network (CNN) that was deeper, extracted more features, and condensed that image information more. Then we implemented a more fully convolutional network (FCN) which contained only one dense layer for the final binary classification step. The FCN added an extra convolutional layer, meaning the before classifying each image, the network abstracted the data more than the other two.
\begin{itemize}
\item LeNet
\item CNN
\item FCN
\end{itemize}
\paragraph{Convolutional Neural Networks}
\paragraph{LeNet}
\paragraph{Fully Convolutional Neural Networks}
\tab There are many well established architectures for Neural Networks depending on the task being performed.
In this paper, the focus is placed on convolution neural networks, which have been proven to effectively classify images \cite{NIPS2012_4824}.
One of the pioneering works in the field, the LeNet \cite{726791}architecture, will be implemented to compare against two rudimentary networks with more depth.
These networks have been constructed to improve on the LeNet architecture by extracting more features, condensing image information, and allowing for more parameters in the network.
The difference between the two network use of convolutional and dense layers.
The convolutional neural network contains dense layers in the final stages of the network.
The Fully Convolutional Network (FCN) contains only one dense layer for the final binary classification step.
The FCN instead consists of an extra convolutional layer, resulting in an increased ability for the network to abstract the input data relative to the other two configurations.
\\
\textbf{Insert image of LeNet from slides}
\section{Method} \label{sec:method}
\tab
In order to effectively utilize the aforementioned modelling and classification techniques, a key consideration is the data they are acting on.
A dataset containing Waldo and non-Waldo images was obtained from an Open Database\footnote{``The Open Database License (ODbL) is a license agreement intended to allow users to freely share, modify, and use [a] Database while maintaining [the] same freedom for others"\cite{openData}}hosted on the predictive modelling and analytics competition framework, Kaggle.
The distinction between images containing Waldo, and those that do not, was providied by the separation of the images in different sub-directories.
In order to effectively utilize the aforementioned modelling and classification techniques, a key consideration is the data they are acting on.
A dataset containing Waldo and non-Waldo images was obtained from an Open Database\footnote{``The Open Database License (ODbL) is a license agreement intended to allow users to freely share, modify, and use [a] Database while maintaining [the] same freedom for others"\cite{openData}}hosted on the predictive modelling and analytics competition framework, Kaggle.
The distinction between images containing Waldo, and those that do not, was providied by the separation of the images in different sub-directories.
It was therefore necessary to preprocess these images before they could be utilised by the proposed machine learning algorithms.
\subsection{Image Processing}
\subsection{Image Processing} \label{imageProcessing}
\tab
The Waldo image database consists of images of size 64$\times$64, 128$\times$128, and 256$\times$256 pixels obtained by dividing complete Where's Waldo? puzzles.
Within each set of images, those containing Waldo are located in a folder called `waldo', and those not containing Waldo, in a folder called `not\_waldo'.
Since Where's Waldo? puzzles are usually densely populated and contain fine details, the 64$\times$64 pixel set of images were selected to train and evaluate the machine learning models.
The Waldo image database consists of images of size 64$\times$64, 128$\times$128, and 256$\times$256 pixels obtained by dividing complete Where's Waldo? puzzles.
Within each set of images, those containing Waldo are located in a folder called `waldo', and those not containing Waldo, in a folder called `not\_waldo'.
Since Where's Waldo? puzzles are usually densely populated and contain fine details, the 64$\times$64 pixel set of images were selected to train and evaluate the machine learning models.
These images provide the added benefit of containing the most individual images of the three size groups.
\\
\par
Each of the 64$\times$64 pixel images were inserted into a Numpy
\footnote{Numpy is a popular Python programming library for scientific computing}
array of images, and a binary value was inserted into a seperate list at the same index.
These binary values form the labels for each image (waldo or not waldo).
Each of the 64$\times$64 pixel images were inserted into a Numpy~\cite{numpy}
array of images, and a binary value was inserted into a seperate list at the same index.
These binary values form the labels for each image (waldo or not waldo).
Colour normalisation was performed on each so that artefacts in an image's colour profile correspond to meaningful features of the image (rather than photographic method).
\\
\par
Each original puzzle is broken down into many images, and only contains one Waldo. Although Waldo might span multiple 64$\times$64 pixel squares, this means that the non-Waldo data far outnumbers the Waldo data.
To combat the bias introduced by the skewed data, all Waldo images were artificially augmented by performing random rotations, reflections, and introducing random noise in the image to produce news images.
In this way, each original Waldo image was used to produce an additional 10 variations of the image, inserted into the image array.
Each original puzzle is broken down into many images, and only contains one Waldo. Although Waldo might span multiple 64$\times$64 pixel squares, this means that the non-Waldo data far outnumbers the Waldo data.
To combat the bias introduced by the skewed data, all Waldo images were artificially augmented by performing random rotations, reflections, and introducing random noise in the image to produce news images.
In this way, each original Waldo image was used to produce an additional 10 variations of the image, inserted into the image array.
This provided more variation in the true positives of the data set and assists in the development of more robust methods by exposing each technique to variations of the image during the training phase.
\\
\par
Despite the additional data, there were still over ten times as many non-Waldo images than Waldo images.
Therefore, it was necessary to cull the no-Waldo data, so that there was an even split of Waldo and non-Waldo images, improving the representation of true positives in the image data set.
Despite the additional data, there were still over ten times as many non-Waldo images than Waldo images.
Therefore, it was necessary to cull the no-Waldo data, so that there was an even split of Waldo and non-Waldo images, improving the representation of true positives in the image data set. Following preprocessing, the images (and associated labels) were divided into a training and a test set with a 3:1 split.
\\
\subsection{Neural Network Training}\label{nnTraining}
\tab The neural networks used to classify the images were supervised learning models; requiring training on a dataset of typical images.
Each network was trained using the preprocessed training dataset and labels, for 25 epochs (one forward and backward pass of all data) in batches of 150.
The number of epochs was chosen to maximise training time and prevent overfitting\footnote{Overfitting occurs when a model learns from the data too specifically, and loses its ability to generalise its predictions for new data (resulting in loss of prediction accuracy)} of the training data, given current model parameters.
The batch size is the number of images sent through each pass of the network. Using the entire dataset would train the network quickly, but decrease the network's ability to learn unique features from the data.
Passing one image at a time may allow the model to learn more about each image, however it would also increase the training time and risk of overfitting the data.
Therefore the batch size was chosen to maintain training accuracy while minimising training time.
\subsection{Neural Network Testing}\label{nnTesting}
\tab After training each network, a separate test set of images (and labels) was used to evaluate the models.
The result of this testing was expressed primarily in the form of an accuracy (percentage).
These results as well as the other methods presented in this paper are given in Figure \textbf{[insert ref to results here]} of the Results section.
\textbf{***********}
% Kelvin Start
\subsection{Benchmarking}\label{benchmarking}
@ -228,7 +248,7 @@
false negatives.
\end{itemize}
Accuracy is a common performance metric used in Machine Learning,
\emph{Accuracy} is a common performance metric used in Machine Learning,
however in classification problems where the training data is heavily
biased toward one category, sometimes a model will learn to optimize its
accuracy by classifying all instances as one category. I.e. the
@ -236,7 +256,8 @@
containing Waldo, but will also classify all images containing Waldo as
not containing Waldo. Thus we use, other metrics to measure performance
as well.
\\
\par
\emph{Precision} returns the percentage of classifications of Waldo that
are actually Waldo. \emph{Recall} returns the percentage of Waldos that
were actually predicted as Waldo. In the case of a classifier that
@ -251,9 +272,6 @@
\clearpage % Ensures that the references are on a seperate page
\pagebreak
% References
\section{References}
\renewcommand{\refname}{}
\bibliographystyle{alpha}
\bibliography{references}
\end{document}

View File

@ -1,19 +1,71 @@
import numpy as np
from keras.models import Model
from keras.models import Model, load_model
from keras.utils import to_categorical
import cv2
from skimage import color, exposure
from _cutter import image_cut
pred_y = np.load("predicted_results.npy")
test_y = np.load("Waldo_test_lbl.npy")
def man_result_check():
pred_y = np.load("predicted_results.npy")
test_y = np.load("Waldo_test_lbl.npy")
test_y = to_categorical(test_y)
test_y = to_categorical(test_y)
f = open("test_output.txt", 'w')
z = 0
for i in range(0, len(test_y)):
print(pred_y[i], test_y[i], file=f)
# Calculates correct predictions
if pred_y[i][0] == test_y[i][0]:
z+=1
f = open("test_output.txt", 'w')
z = 0
for i in range(0, len(test_y)):
print(pred_y[i], test_y[i], file=f)
# Calculates correct predictions
if pred_y[i][0] == test_y[i][0]:
z+=1
print("Accuracy: {}%".format(z/len(test_y)*100))
f.close()
print("Accuracy: {}%".format(z/len(test_y)*100))
f.close()
'''
Purpose:Loads a trained neural network model (using Keras) to classify an image
Input: path/to/trained_model
image [or] path/to/image [if from_file=True]
Returns:Boolean variable
'''
def is_Wally(trained_model_path, image, from_file=False):
if from_file:
img = cv2.imread(image) # Opens the image (in BGR format)
# Histogram normalization in v channel
hsv = color.rgb2hsv(img)
hsv[:, :, 2] = exposure.equalize_hist(hsv[:, :, 2])
img = color.hsv2rgb(hsv)
image = np.rollaxis(img, -1) # Rolls the colour axis to the front
trained_model = load_model(trained_model_path)
if trained_model.predict(image, verbose=1, batch_size=1)[0] == 1:
return 0
else:
return 1
# Load fully puzzle image
# Split image into array of images
# use is_Wally(img) to classify image
# Mark Wally image somehow (colour the border)
# Stitch original image back together
if __name__ == '__main__':
# Read image
image = cv2.imread("10.jpg")
# Split image
cuts = image_cut(image, 64, 64)
for i in len(cuts):
# Transform block
hsv = color.rgb2hsv(cuts[i])
hsv[:, :, 2] = exposure.equalize_hist(hsv[:, :, 2])
block = color.hsv2rgb(hsv)
block = np.rollaxis(block, -1)
if is_Wally("Waldo.h5", block):
# Border block
cuts[i] = cv2.copyMakeBorder(cuts[i],5,5,5,5,cv2.BORDER_CONSTANT,value=RED)
# Stitch image TODO!
# Show image
cv2.imwrite('output.png',image)