Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

A Supervised Learning approach on Rice Variety

Classification using Convolutional Neural Networks


Louie John L. Castillo Juvy Amor M. Galindo Jamie Eduardo C. Rosal
College of Engineering and College of Engineering College of Engineering and
Technology
Cor Jesu College and Technology Technology
Cor Jesu College Cor Jesu College
Digos City, Davao del sur,
Digos City, Davao del sur, Digos City, Davao del sur,
Philippines
Philippines Philippines
louiejohnc@gmail.com Jamiewertalmighty@gmail.co
ken8juv@yahoo.com
m

ABSTRACT grade and price of rice is mainly determined by these factors,


This project is about developing a portable imaging system using however, from a rice breeder’s perspective, field trials are vital in
Raspberry Pi that can obtain rice grain visual parameters datasets determining and testing the different traits of a new rice variety,
for Image Processing. The system aims to identify and classify at either produced via traditional methods or through modern
least three (3) Specific Rice varieties; use supervised learning biotechnology their objective and rapid measurement are difficult
approach on Convolutional Neural Networks (CNN) to to achieve without the aid of precision instruments and human
automatically attain the results in real-time. CNN is a powerful expertise [1]. With the exception of some government
algorithm to classify images. CNN was used preferably, since laboratories where expertise and scientific devices are available,
other Artificial Neural Networks types needs to obtain several the manual and visual methods are still the dominant techniques
numerical parameters to be trained, and relatively require more employed by the rice industry in the country today. The manual
human efforts. The accuracy of the developed system for novel method although practical and fast, it lacks the reliability and
rice grain samples have been tested for above 90% percent. The objectivity required in a competitive rice industry. There are
device captures the rice image using a Raspberry Pi Camera, the problems prevalent in the rice grain markets. First being the
captured image are then processed. 500 individual images of rice traders mixing a particular type of rice with poor quality type and
grains per variety are trained in the CNN model and 50 epochs the second being either unintentional or deliberate mislabeling
were used to ensure better accuracy. Apart from the 3 tested certain variety in favor of a cheaper or low grade rice [2].
varieties, new varieties can still be trained and tested in the CNN. Looking at these, the researchers propose to solve the problem of
The device can assess physical and visual features of the rice. classification and identification of rice types.
Other features such as chemical and genetic traits are not
detectable in the system. Philippine local government does not a Several methods based on biological or chemical techniques such
better material to properly ensure and the authenticity of the rice as genetic markers have been proposed to determine rice varieties.
varieties sold in sold in the local market. This device can help Although these methods are very accurate, they are destructive
classify rice grains since there are no currently no easy method techniques that are costly and time-consuming, making them
and inexpensive tools for easily and conclusively classifying rice unsuitable for mass inspection. Being limited to batch sampling,
grains because of the subtle its differences. these techniques cannot truly assess the purity of the rice seeds
under inspection. [3] To circumvent these limitations, non-
CCS Concepts destructive rice inspection systems that use a combination of
• Applied computing➝Life and medical optical imaging and data analysis techniques have been proposed.
sciences➝Bioinformatics Fraudulent labeling of one variety as another is a major concern in
the food industry. The International Rice Research Institute
Keywords (IRRI) and other organizations including the British Retail
Rice classifier; Rice variety; Neural networks; Machine learning Consortium (BRC) discuss the appearance of rice as being critical
to consumers. These organizations have established standards and
1. INTRODUCTION regulations for rice grain varieties. Certain physical characteristics
Grain characteristics of texture, shape and sizes is of great such as minimum average length and aspect ratio are specified
significance or value in quality gauges of grains. To assess the explicitly in the standards. [4]
Permission to make digital or hard copies of all or part of this work for
personal or classroom use is granted without fee provided that copies Recommended retail price of the rice in the Philippines was
are not made or distributed for profit or commercial advantage and implemented across the country and implemented severe legal
that copies bear this notice and the full citation on the first page. penalties to market traders that is caught that did not follow the
Copyrights for components of this work owned by others than ACM
policy especially in Metro Manila. The Government deployed
must be honored. Abstracting with credit is permitted. To copy
otherwise, or republish, to post on servers or to redistribute to lists, policemen, and Inspectors of National Food Authority (NFA), and
requires prior specific permission and/or a fee. Request permissions Department of Trade and Industry (DTI) officials to secure that
from Permissions@acm.org. retailers followed the standard retail price of rice variety that were
ICBRA '19, December 19–21, 2019, Seoul, Republic of Korea sold costumers. A written warning was given for the first offense.
© 2019 Association for Computing Machinery. Cancellation of Business License and severe penalty was being
ACM ISBN 978-1-4503-7218-3/19/12… given to second offenders. Color-coding are given to each and
$15.00 every label of Rice. White for Regular and well-milled, Yellow
DOI: https://doi.org/10.1145/3383783.3383788 for
Premium Grade, and Blue for Special Rice. Officials was and label of the Rice that are being sold [5]. During the inspection
reprimand and penalize the sellers who violate, change the price organized by the National Food Authority in the nearby markets in the
Philippines, particularly in Trabajo Market and Dagonoy Market Processor. Computer Monitor – is used to view the display of the
in Manila, they have not found out any violators, and yet there are Raspberry Pi [13].
rice that is much cheaper than the standard retail prices. Even
though, they are not sure if any of the labels are changed because 2.1 Image Acquisition
it they have no simple method for easily and conclusively
discover these violations.
The implication of this study can potentially assist quality
assessment government organization such as DTI (Department of
Trade and Industry), Department of Agriculture (DA), Philippine Figure 2. Image acquisition and classification setup
Rice Research Institute (PhilRice), and National Food Authority
(NFA) to identify rice variety and classify it properly and more 8MP Pi Camera is be used for image acquisition, the rice grains
accurately than the customary manual inspection and analysis, can be contained and positioned in an enclosure box to ensure
although it can be also used along with these methods. [2], [6] All adequate and consistent lighting of the LED light Panel.
of these are for the benefit of the market consumers. Raspberry Pi is used to store gathered datasets in its local storage.
The key objective of this study is to develop a portable imaging The display of these processes can be viewed in external PC
system using Raspberry Pi that can classify rice grains images, monitor as shown in Figure 2.
the system can identify and classify at least three (3) Specific The Image of rice grains was acquired by an enclosure box setup
Rice varieties in real time. Use supervised learning approach on captured by Raspberry pi camera at of 640 x 480 pixels. As much
Convolutional Neural Networks (CNN) to automatically attain the as 20 individual rice grains can be scanned in the platform at the
results. Then, test the accuracy of the developed system. same time [7], [8]. Using OpenCV, real-time video will be
displayed. The image stream will be processed and is converted
2. MATERIALS AND METHODS into 8-bit grey scaled values. Using a dark background, it was
In this paper, a classification of rice grains variety using
possible to easily extract the grains from the background using
Convolutional Neural networks is presented as shown in Figure 1.
image segmentation algorithm, Image acquisition setup is shown
in Figure 3.

Figure 1. Procedure for recognition and classification of rice


grains
Figure 3. Image acquisition setup
This part of the study provides the detailed steps that were
performed and the necessary materials and equipment that were 2.2 Image Segmentation
used to design the system.The prototype consists of a Raspberry To ensure better performance of the system we only manually
Pi 3 Model B computer, a camera module that is enclosed in a selected pure quality rice grains, which are not broken and
case for imaging with LED panel installed. visually discoloured, [8] this will be the basis for the classifying
The important factors to be considered upon image acquisition new rice grain samples. The first step in image analysis is image
and testing: [4] Image Positioning – Distance of image from the segmentation, this separates and crops an image into several
camera, Image Background – Dark Background to contrast the individual parts or objects The image is subdivided until the
rice grains. Rice Grain Orientation – Rice should be positioned objects of interest and bounded in and Region of Interest (ROI)
horizontally relative to the screen. Number of Rice Grains per rectangle as shown in Figure 4 and Figure 5.
image – more number of detections can slow down the
computation and detection, maximum of twenty rice (20) should
be enough to have better performance. Focal distance from the
Lens – The image should be clear and not blurry, out-of-focus.
Camera Resolution – Pi Camera has max of 8MP resolution, but
to improve performance we limit the resolution to 640 x 480.
Camera Lens Focal Distance – Pi Camera has fixed focus.
Amount of Lighting – LED light should be bright and consistent.
Camera CCD Sensor Sensitivity – Pi camera has a better
sensitivity on bright conditions. Computational Power –
Raspberry Pi Model B model has 1 GB of Ram, with Quad-Core
Figure 4. Detection of rice grains against dark background
of datasets

The rice grain detection and segmentation algorithms is based on


the discontinuity of gray level values (0-255), brighter pixels are
closer value of 255, and darker pixels to 0 [7-8]. This method
Figure 5. Rice grain segmentation and gathering partitions an image based on abrupt changes in gray levels.
In this study, it was found that the gray pixels values between the captured individual rice grains, converted within the parameter of
background and the objects are completely different. Once the CNN into 3 RGB color channels: The Red, Green, and Blue pixel
rice grain is identified it will be enclosed in an ROI rectangle to values ranging from (0-255). [9-11] Subtle color differences of
be labeled and stored individually in folder containing all the Rice grains can be distinguished be used as a data point for
datasets to be trained as shown in Figure 6. Classification. Given this as inputs, the CNN will describe the
probability of the rice grain image for its certain variety [11].
CNN uses weight filter to scan an image pixel by pixel to detect
features as shown in Figure 7. that was defined in a certain variety
that was previously trained. The weight filter is an array of
numerical values obtained at a particular layer. The weights are
first randomly initialized and cannot detect specific features
initially. The CNN can only learn the weight values along the
training session [9-10]. The weights convolve around the input
image, pixel by pixel, it multiplies the weights values by the pixel
values on the scanned region of the image. The matrix dot product
from each region are tallied up, to obtain the feature map of rice
grain.
Figure 6. Sample of captured images The dot product is calculated in the following manner -
WTx + b (1)
2.3 Convolutional Neural Networks 1
Unlike other image classifier algorithms, CNN uses little pre- Where: W = Filter at Layer 1, x = input image, b = bias
processing comparatively. The major advantage from other prior value
types of neural networks is the independence from prior
knowledge. In previous related studies, the researchers need
obtain manually the individual rice features, save them as
numerical data and train them in a feed-forward networks. [7], [8],
[9], [10], [11], [12] CNN on the other hand, requires little human
Figure 7. Input image converted into a feature maps
effort and time in training images. [9], [10]
The Convolutional Neural Network (CNN) algorithm is different
in several ways as compared to the other neural network Subsequently after obtaining the feature map, it passed along the
algorithms. Training, backpropagation, error reduction are still the Activation Layer Rectified Linear Unit (reLu). ReLu is a non-
same. However, The CNN algorithm is designed to be explicitly linear operation that increases the non-linear properties of CNN
used for classifying images. The algorithm starts with input image model. ReLu Activation function ends up discarding values below
from the dataset. CNN operates over some input which has zeroes, i.e. thresholds the minimum at zero.
spatial dimensions. They take input as volumes of activations and ReLu Activation Function
also produce the output as volumes of activations. Thus, in CNN
the intermediates are not normal vectors as normal Neural F(x) = max(0, x) (2)
Networks but the intermediates have spatial dimensions of height,
Next, the Pooling layer decreases the computational complexity
width and depth.
of the CNN by reducing the dimensions of the feature map, but it
CNN algorithm is divided primarily into three layers – can still retain the most significant features [10]. In our study, we
Convolutional Layer, Max pooling Layer and the fully used Max Pooling Layer. Max pooling is basically just
connected layer. downsampling of the activation maps. Max pooling layers of 2x2
filter and stride 2 are used, which end up reducing the input
To implement the CNN, we used Python Programming Language. activation maps in half spatial maps
The CNN used in this project was implemented in Google’s
TensorFlow using Keras, which provides a high level of The output was feed to another Convolutional Block: Convolution,
abstraction over TensorFlow. The first layer of CNN is the Activation, and Pooling layer. In this study, we used 4 blocks, as
Convolutional Layer, the input is 80x40 pixel image of the rice grains varieties are almost similar [11] to each other with
only very subtle differences in its features.
The Fully Connected layer takes the volume as input at the very
end. It has neurons for computing class scores and they are fully
connected to the entire input volume, just like a normal Neural
Network [10]. This layer does the last matrix multiplier to
compute the output, the probability on how likely the rice grain
image belongs to each Variety.
Overfitting can occur as augmented samples are used for input,
even if they increased the dataset size, they were highly correlated.
Although, the image size is already controlled, as the rice grains
is fixed at focal distance of the pi Camera. However some
varieties can still vary in sizes and shape. Overfitting was solved
by moderating the entropic capacity or the amount of random Dropout was implemented by flattening the feature map into a
information stored in the CNN model [9]. A Dropout layer can be
used to overcome this, by setting activation value to 0, random set
of activations will be removed as the data flows along this layer.
and sorted for the pure best quality. The datasets were trained for all the variety label, then we observed and gathered the results.
for 2-2.5 hours for 50 epoch. The loss implemented using
multiclass Cross-Entrophy, we calculated a simultaneous loss The formula for Cross-Entrophy:
one (1) dimensional vector. Then initialize a fully connected layer
with the Dense function then ReLu. The output is a three (3) ∑ (3)
dimensional vector, these vector are the number of varieties (3 The training accuracy was measured from the number of correct
varieties) that the rice needs grains to be classified. After putting predictions in the new rice grain images from actual variety. The
all these layers, [9] a Sigmoid function will be used to convert the training session data was stored and graphed the results presented
output data to the probability or confidence value for the rice in Figure 8 and Figure 9.
variety of the given input image.
M - number of varieties
2.4 Training the Datasets log - the natural log
Unlike independent image classifying, training requires large y - binary indicator (0 or 1) if class label c
amount of processing power, therefore we implement the process is the correct classification for observation o
in an external computer with high specification, [9-10] the p - predicted probability observation o is of class c
obtained training data will be used obtained and be used to
classify independently and saved in raspberry pi.
In this Study, we are going to classified 3 Specific Local Rice
Varieties, from low grade to high grade variety, respectively,
NFA Variety, M3 Variety, and Double Diamond (DD) Varieties.
NFA have different classes, but we chose the cheapest of them
available in the market, it will labeled simply NFA, as well as DD
for Double Diamond for simplification of the label [5].
Regardless, any type of Rice Variety can used for this study.
However, we only use these three as a representative for each of
the grade of rice.
The CNN can learn the value of the optimum weight filter for
classifying the rice variety by minimizing the Loss function that Figure 8. Training loss graph
measures the difference [10] between the target rice grain variety
image and the expected output for the given new input image.
Backpropagation [9-11] propagates the loss backwards for each
layer, the corresponding weight values for each filter will then be
updated so these can change in the direction of the gradient that
minimizes the value of the Loss Function.
The gathered labeled datasets will be used for training the rice
grain variety. Cross-Entropy Loss or Negative Log Likelihood is
the most common setting for classification problems. Cross-
entropy loss increases as the predicted probability diverges from
the actual label [9]. The Loss increases as the predicted
probability diverges from the actual label. So predicting a
probability of .012 when the actual observation label is 1 would Figure 9. Training accuracy graph
be bad and result in a high loss value [9-10]. A perfect model
would have a log loss of 0. Root Mean Square Propagation will
be used to perform the gradient descent. Detection and Classification

The CNN was trained on a Dell Laptop with Intel Core i7-7500U  Rice Grains are aligned, and oriented horizontally,
CPU. The training data split was into 500 per variety and 50  The Rice Grains are be classified on real time.
epochs or training iteration were used, This can store a lot of  The system detects the Rice grains and Label them
information and has the potential to be more accurate up to a individually.
certain point, however, it can also end up storing irrelevant  Detected grains are emphasized in an enclosing
features [9-11]. In our case, we used a 4 convolutional blocks Region-of-Interest Rectangle (ROI Rectangle) and
alongside data increase and dropout of 0.5. Dropout also helps label its most probable variety, and display the
reduce over fitting, by preventing a layer from seeing twice the confidence value of the variety
exact same pattern. Thus, the model which stores less information  The threshold for labeling recognized rice grain
ends up storing just the most significant features [9-12] found in image probability of not less than 60%.
the data and are likely to more relevant and generalize better.
After training, we tested 300 new rice grains samples per variety.
3. RESULTS AND DISCUSSIONS 250 of the sample are the actual variety of the rice grains being
500 unique individual rice grains images per variety are tested, and 50 samples were from other trained variety, 25 for
simultaneously trained, the rice grains are manually handpicked
each. The Following are the confusion matrices for each of the rice grain variety:
Testing NFA Rice: 250 are actual NFA Rice Grains as shown in
Figure 9, the other 50 are from 25 of each from M3 presented in
Figure 10, and Double Diamond Varieties as shown in Figure 11.
Figure 9. Confusion matrix for NFA rice

Figure 10. Confusion matrix for M3 rice


Figure 12. Testing NFA rice

Testing M3 Rice Variety: 250 are actual M3 Rice Grains, the


other 50 are from 25 of each from NFA and Double Diamond
Varieties.
Testing Double Diamond Rice: 250 are actual Double Diamond
Rice Grains, the other 50 are from 25 of each from M3 and NFA
Varieties.

Figure 13. Testing new images simultaneously with 2 different


rice varieties

Figure 11. Confusion matrix for double diamond rice


3.1 System Output
These images were a screenshot from a real time video images
captured from pi Camera using OpenCV. The Percent Value
shown in Figure 12, the display is the Confidence or Probability
and Similarity of the sample to the trained datasets as shown in
Figure 13 and Figure 14.

Figure 14. Testing random broken and untrained rice


varieties. It predicted into a different rice varieties but with
low confidence level without threshold limit (Top). Same
samples but with threshold limit (Bottom)
4. CONCLUSION setup, we have concluded that, the more the datasets obtained and
The system is able to efficiently identify and classify the rice trained, then the more accurate the system for prediction new rice
varieties into 3 trained varieties NFA, M3, and Double Diamond Grain of the same variety. For further advancements, the input variety
Varieties with an accuracy of above 90%. In the experimental for training can be added for wider range of classifying rice grains, it
would be really helpful for rice inspectors to have a portable system
that can simply and accurately classify rice grain varieties. We Digital Image Analysis. Conference Record. IEEE
have attained our goal accuracy for our System of above 90%. Instrumentation and Measurement Technology Conference.
However, false detection can still occur with only more or less 1-5. 10.1109/IMTC.2007.
than 10% margin of error. This method has a good effect on the
recognition and classification of rice grain varieties. 379116.
[5] ―SRP sa bigas pinatutupad na; NFA nagbabala sa parusa‖.
5. REFERENCES ABS-CBN https://news.abs-
[1] Jose D Guzman, Engelbert K. Peralta. “Classification of
cbn.com/business/11/09/18/srp- sa-bigas-pinatutupad-na-
Philippine Rice Grains Using Machine Vision and
nfa-nagbabala-sa-parusa
Artificial Neural Networks”, (2018) Cagayan State
University, Philippines, University of the Philippines Los [6] Proceedings of the Policy Seminar: Is Rice RD Worth
Banos, Philippines. Investing (2015). Philippine Rice Research Institute [pdf] -
https://www.philrice.gov.ph/wp-
[2] Abrogena, N.O. Galut, C. Aguinaldo, A.C. Duldulao, V.A.
content/uploads/2015/12/Is-
et al. ―Palay/rice trading in the Philippines: an evaluation for
Rice-RD-Worth-Investing-In-Final.pdf
the rice R & D [research and development] agenda‖ (2000)
[7] Hua Gao ,*1 , Yaqin Wang 2,1 , Pingju Ge. ―Rice Shape
[3] Itthi Chatnuntawecha, Kittipong Tantisantisom, Paisan
Parameter Detection Based on Image Processing‖
Khanchaitit, Thitikorn Boonkoom, Berkin Bilgic, Ekapol
Chuangsuwanic. ―Rice Classification Using Spatio-Spectral [8] Papageorgiou, Oren and Poggio, International Conference on
Deep Convolutional Neural Network‖ (2015). National Computer Vision."A general framework for object
Nanotechnology Center, National Science and Technology detection"
Development Agency, Pathum Thani, Thailand Athinoula [9] LeNet-5, ―Convolutional Neural Networks‖. LeCun, Yann
A. MartinosCenter for Biomedical Imaging, Charlestown, http://yann.lecun.com/exdb/lenet/ . 16 November 2013.
MA,
USA Harvard Medical School, Boston, MA [10] ―A Comprehensive Guide to Convolutional Neural Networks 
— the ELI5 way‖. https://towardsdatascience.com/a-
[4] Hobson, D.M. & Carter, Robert & Yan, Yong. (2007). comprehensive-guide-to- convolutional-neural-networks-
Characterisation and Identification of Rice Grains the-eli5-way-3bd2b1164a53
through
[11] Jiuxiang Gu, Zhenhua Wang, Jason Kuen, Lianyang Ma,
Amir Shahroudy, Bing Shuai, TingLiu, Xingxing Wang, Li
Wang, Gang Wang, Jianfei Cai, Tsuhan Chen. ―Recent
Advances in Convolutional Neural Networks (CNN)‖.
[12] Schmidhuber, J. (2015). Neural Networks. 61: 85–117.
―Deep Learning in Neural Networks: An Overview".
[13] ―Implementation of Grain sorting algorithm by using
Raspberry Pi‖. Ms. Shraddha N. Ranaware, Prof .Dr. K. N.
Barbole.

You might also like