0% found this document useful (0 votes)
291 views

Deep Learning in C# - Coin Recognition in Keras - NET, Part I - CodeProject

Uploaded by

Fahd Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
291 views

Deep Learning in C# - Coin Recognition in Keras - NET, Part I - CodeProject

Uploaded by

Fahd Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2021/‫‏‬6/‫‏‬26 Deep Learning in C#: Coin Recognition in Keras.

NET, Part I - CodeProject

14,938,963 members Sign in

home articles quick answers discussions features community help Search for articles, questions, tips

Articles / Artificial Intelligence / Keras


 

Deep Learning in C#: Coin Recognition in Keras.NET, Part I


Arnaldo P. Castaño Rate me: 5.00/5 (3 votes)

6 Nov 2020 CPOL 3 min read

In this article, we will examine a convolutional neural network for the problem of coin recognition, and we will implement one in Keras.NET.

Here we will introduce convolutional neural networks (CNNs) and present an architecture of a CNN that we will train to recognize coins.

Download source - 1.5 MB

What is a CNN? As we mentioned in the previous article of the series, a CNN is a class of Neural Network (NN) frequently used for image classification tasks, such as object and face recognition,
and in general for problems where the input can have a grid-like topology. In CNNs, not every node is connected to all nodes of the next layer. This partial connectivity helps prevent overfitting
issues that come up in fully connected NNs, plus speeds up the convergence of the NN.

The core concept surrounding CNNs is that of a mathematical operation known as convolution, which is very common in the field of digital signal processing. Convolution is defined as a product
of two functions, resulting in a third function that represents the amount of overlap between the first two. In the area of CNNs, convolution is achieved by sliding a filter, known as a kernel,
through the image.

In object recognition, the convolution operation allows us to detect different features in the image, such as vertical and horizontal edges, textures, and curves. This is why one of the first layers in
any CNN is a convolutional layer.

Another layer common in CNNs is the pooling layer. Pooling is used to reduce the size of the image representation, which translates to a reduction in the number of parameters and, ultimately,
the computational effort. The most common type of pooling is max pooling, which uses a sliding window similar to the one in the convolution operations to harvest, in every location, the
maximum value from the group of cells being matched. At the end, it builds a new representation of the image from the harvested maximum values.

https://www.codeproject.com/Articles/5284228/Deep-Learning-in-Csharp-Coin-Recognition-in-Keras 1/4
2021/‫‏‬6/‫‏‬26 Deep Learning in C#: Coin Recognition in Keras.NET, Part I - CodeProject

Another concept related to convolution is padding. Padding guarantees that the convolution process will evenly happen across the entire image including the border pixels. This guarantee is
backed by a zero-pixel border that is added around the downscaled image (after pooling) so that the sliding window can go to all pixels of the image the same number of times.

The most common CNN architectures typically start with a convolutional layer, followed by an activation layer, then a pooling layer, and end with a traditional fully connected network such as a
multilayer NN. This type of model, where layers are placed one after the other, is known as a sequential model. Why a fully connected network at the end? To learn a non-linear combination of
features in the transformed image (after convolution and pooling).

Here is the architecture we’ll implement in our CNN:

Conv2D layer – 32 filters, filter size of 3


Activation Layer using the ReLU function
Conv2D layer – 32 filters, filter size of 3
Activation Layer using the ReLU function
MaxPooling2D layer – Applies the (2, 2) pooling window
DropOut layer, at 25% – Prevents overfitting by randomly dropping some of the values from the previous layer (setting them to 0); a.k.a. the dilution technique
Conv2D layer – 64 filters, filter size of 3
Activation layer using the ReLU function
Conv2D layer – 64 filters, filter size of 3 and, stride of 3
Activation layer using the ReLU function
MaxPooling2D layer – Applies the (2, 2) pooling window
DropOut layer, at 25%
Flatten layer – Transforms the data to be used in the next layer
Dense layer – Represents a fully connected traditional NN with 512 nodes.
Activation layer using the ReLU function
DropOut layer, at 50%
Dense layer, with the number of nodes matching the number of classes in the problem – 60 for the coin image dataset used
Softmax layer

The architecture proposed follows a sort of pattern for object recognition CNN architectures; layer parameters had been fine-tuned experimentally.

The result of the fine tuning process of parameters that we went through was partially stored in the Settings class that we present here:

C# Copy Code

public class Settings

public const int ImgWidth = 64;

public const int ImgHeight = 64;

public const int MaxValue = 255;

public const int MinValue = 0;

public const int Channels = 3;

public const int BatchSize = 12;

public const int Epochs = 10;

public const int FullyConnectedNodes = 512;

public const string LossFunction = "categorical_crossentropy";

public const string Accuracy = "accuracy";

https://www.codeproject.com/Articles/5284228/Deep-Learning-in-Csharp-Coin-Recognition-in-Keras 2/4
2021/‫‏‬6/‫‏‬26 Deep Learning in C#: Coin Recognition in Keras.NET, Part I - CodeProject

public const string ActivationFunction = "relu";

public const string PaddingMode = "same";

public static StringOrInstance Optimizer = new RMSprop(lr: Lr, decay: Decay);

private const float Lr = 0.0001f;

private const float Decay = 1e-6f;

We now have an architecture for the CNN that we will present in the next article. In the next delivery, we will examine the CNN we implemented for coin recognition using Keras.NET.

◁ Prev Deep Learning in C# Next ▷

License
#1 Free Data Scraping Tool
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) No Coding Required
Looking for easy yet best Web Scraper

Share Extension for your browser? Download

GREPSR.
grepsr.com

OPEN

About the Author


Arnaldo P. Castaño
Software Developer
Serbia

Computer Scientist and book author living in Belgrade and working for a German IT company. Author of Practical Artificial Intelligence: Machine Learning,
Bots, and Agent Solutions Using C# (Apress, 2018) and PrestaShop Recipes (Apress, 2017). Lover of Jazz and cinema

https://www.codeproject.com/Articles/5284228/Deep-Learning-in-Csharp-Coin-Recognition-in-Keras 3/4
2021/‫‏‬6/‫‏‬26 Deep Learning in C#: Coin Recognition in Keras.NET, Part I - CodeProject

Comments and Discussions


 

You must Sign In to use this message board.

Search Comments

Spacing Relaxed   Layout Normal   Per page 25     Update

 
-- There are no messages in this forum --

Permalink
Layout: fixed
|
fluid Article Copyright 2020 by Arnaldo P. Castaño

Advertise
Everything else
Copyright © CodeProject, 1999-2021

Privacy

Cookies
Web03 2.8.20210623.1

Terms of Use

https://www.codeproject.com/Articles/5284228/Deep-Learning-in-Csharp-Coin-Recognition-in-Keras 4/4

You might also like