Deep Learning With R : Convolutional Neural Network

Nur Mutmainnah Djafar
5 min readJan 11, 2021
Neural network with many convolutional layers

What Is Convolutional Neural Network?

Convolutional Neural Network is a type of deep learning algorithm which accepts input such as images, aspects or objects in an image that can be used by machines to “learn” to distinguish between images, one image from another.

In general, CNN is not much different from an artificial neural network. CNN has a function of weight, bias and activation of neurons. The convolutional layer consists of neurons which are also arranged in a way that forms a filter with length & height (pixels).

Convolutional Neural Network is a machine learning method from the development of Multi Layer Perceptron (MLP) which is designed to process two-dimensional data. CNN is included in the type of Deep Neural Network because of its deep network level and is widely implemented in image data. CNN has two methods; classification using feedforward and the learning stage using backpropagation. The way CNN works is similar to MLP, but in CNN each neuron is presented in two dimensions, unlike MLP, where each neuron is only one dimension.

The overall scale in the object is very important so that the input does not lose its spatial information which will be extracted and classified by features. This will increase the accuracy and optimum level of the CNN algorithm. As in the cube which has a scale on the length, width, and height. If you only use a normal Neural Network, it may only load the length and height scales. But CNN can contain all the information from the entire scale which can classify objects more accurately because it can use its wide scale as well (which may not be seen by other two-dimensional Neural Networks).

Do Analyze

By moving a filter (convolutional kernel) of a specified size to an image, the device will get new representative information from the multiplication of the image using the filter. In this analysis, we will use 12 images (6 pictures of cars and 6 pictures of bicycles) that you can download on google!

To do this analysis, we need several packages that you must install, namely tensorflow, hard, and EBImage by writing the command :

install.packages(tensorflow)
install.packages(keras)
install.packages(EBImage)

To activate the package write the command :

library(tensorflow)
library(keras)
library(EBImage)

Data from 12 images that have been downloaded, are combined in one folder. In this analysis I put the image on D:/exam. Then create a new folder again to save the resized results later. I created a new folder, namely D:/result.

Let’s start practice …

First of all, call the exam folder which contains the 12 downloaded images and gives the command for a new folder to store the resize results.

setwd("D://exam/")
save_in <- ("D://result/")

Because the size of the image is different, the next step is to make the image the same size as the command :

gambar <- list.files()
w <- 100
h <- 100
for (i in 1:length(gambar))
{result <- tryCatch({
imgname<-gambar[i]
img<-readImage(imgname)
img_resized<-resize(img,w=w,h=h)
path<-paste(save_in,imgname,sep=" ")
writeImage(img_resized,path,quality=70)
print(paste("done",i,sep=" "))
},
error=function(e){print(e)}
)}

If successful it will display the output as below

After all the sizes of the images are the same, let’s call one of the images with the command

display(gambar2[[3]]) #menampilkan gambar ke-3

Next step is make train data & test data. Where Figure 1 to Figure 4 become the data train, Figures 5 and 6 become test data. And figures 7 to 10 become the data train, figures 11 and 12 become test data.

train <- gambar2[c(1:4,7:10)]
test <- gambar2[c(5:6,11:12)]

Then look at the color set of each pixel in the image with the command

train[[5]] #menampilkan data training gambar ke-5

Then the output is shown as follows

From the output above, it can be seen that the dimensions of the image are 30,000 cells, of which each cell represents the color in the image.

Next is display 8 train data & test data sequentially in 1 image with commands

par(mfrow=c(2,4))
for (i in 1:8) plot(train[[i]])

Then the resulting output is

Resize it which will be 32 x 32 pixels using the command

for (i in 1:8){train[[i]]<-resize(train[[i]],32,32)}
for (i in 1:4){test[[i]]<-resize(test[[i]],32,32)}

Then combine all the images in the train data, as well as the test data with commands

train <- combine(train)
x<-tile(train,8)
display(x,title="gambar")
dim(train)
test <- combine(test)
y <- tile(test,4)
display(y,title="gambar")
dim(test)
Display train data combination
Display set data combination

In order to match the packages used (hard and tensorflow), it is necessary to make adjustments to the image order with the command

train<-aperm(train,c(4,1,2,3))
test<-aperm(test,c(4,1,2,3))
dim(train)
dim(test)

Due to rearrangements, the order of the dimensions changes. The order of the dimensions is reversed from the back, as is the case with the test data.

Then we will create a target label where 0 will represent the car image and 1 will represent the bicycle image using the command

#klasifikasi
trainy <- c(rep(0,4), rep(1,4))
testy <- c(rep(0,2), rep(1,2))
trainy
testy
#label dari data target
trainLabels <- to_categorical(trainy)
testLabels <- to_categorical(testy)
trainLabels
testLabels
Classification output
Output labels from target data

FINISH!

The label of the target data is attached, hehehe…

May be useful…

Sources :
https://algorit.ma/blog/convolutional-neural-networks-tensorfflow/
https://medium.com/@16611012/deep-learning-with-r-convolutional-neural-network-ebc81db63668
https://medium.com/@nadhifasofia/1-convolutional-neural-network-convolutional-neural-network-merupakan-salah-satu-metode-machine-28189e17335b

--

--