Fruit recognition from images using deep learning
Horea Mureşan, Mihai Oltean
Introduction
The aim of this paper is to propose a new dataset of images containing popular fruits. The dataset was named Fruits-360 and can be downloaded from the addresses pointed by references and . Currently (as of 2020.05.18) the set contains 90483 images of 131 fruits and vegetables and it is constantly updated with images of new fruits and vegetables as soon as the authors have accesses to them. The reader is encouraged to access the latest version of the dataset from the above indicated addresses.
Having a high-quality dataset is essential for obtaining a good classifier. Most of the existing datasets with images (see for instance the popular CIFAR dataset ) contain both the object and the noisy background. This could lead to cases where changing the background will lead to the incorrect classification of the object.
As a second objective we have trained a deep neural network that is capable of identifying fruits from images. This is part of a more complex project that has the target of obtaining a classifier that can identify a much wider array of objects from images. This fits the current trend of companies working in the augmented reality field. During its annual I/O conference, Google announced that is working on an application named Google Lens which will tell the user many useful information about the object toward which the phone camera is pointing. First step in creating such application is to correctly identify the objects. The software has been released later in 2017 as a feature of Google Assistant and Google Photos apps. Currently the identification of objects is based on a deep neural network .
Such a network would have numerous applications across multiple domains like autonomous navigation, modeling objects, controlling processes or human-robot interactions. The area we are most interested in is creating an autonomous robot that can perform more complex tasks than a regular industrial robot. An example of this is a robot that can perform inspections on the aisles of stores in order to identify out of place items or under-stocked shelves. Furthermore, this robot could be enhanced to be able to interact with the products so that it can solve the problems on its own. Another area in which this research can provide benefits is autonomous fruit harvesting. While there are several papers on this topic already, from the best of our knowledge, they focus on few species of fruits or vegetables. In this paper we attempt to create a network that can classify a variety of species of fruit, thus making it useful in many more scenarios.
As the start of this project we chose the task of identifying fruits for several reasons. On one side, fruits have certain categories that are hard to differentiate, like the citrus genus, that contains oranges and grapefruits. Thus we want to see how well can an artificial intelligence complete the task of classifying them. Another reason is that fruits are very often found in stores, so they serve as a good starting point for the previously mentioned project.
The paper is structured as follows: in the first part we will shortly discuss a few outstanding achievements obtained using deep learning for fruits recognition, followed by a presentation of the concept of deep learning. In the second part we describe the Fruits-360 dataset: how it was created and what it contains. In the third part we will present the framework used in this project - TensorFlow and the reasons we chose it. Following the framework presentation, we will detail the structure of the neural network that we used. We also describe the training and testing data used as well as the obtained performance. Finally, we will conclude with a few plans on how to improve the results of this project. Source code is listed in the Appendix.
Related work
In this section we review several previous attempts to use neural networks and deep learning for fruits recognition.
A method for recognizing and counting fruits from images in cluttered greenhouses is presented in . The targeted plants are peppers with fruits of complex shapes and varying colors similar to the plant canopy. The aim of the application is to locate and count green and red pepper fruits on large, dense pepper plants growing in a greenhouse. The training and validation data used in this paper consists of 28000 images of over 1000 plants and their fruits. The used method to locate and count the peppers is two-step: in the first step, the fruits are located in a single image and in a second step multiple views are combined to increase the detection rate of the fruits. The approach to find the pepper fruits in a single image is based on a combination of (1) finding points of interest, (2) applying a complex high-dimensional feature descriptor of a patch around the point of interest and (3) using a so-called bag-of-words for classifying the patch.
Paper presents a novel approach for detecting fruits from images using deep neural networks. For this purpose the authors adapt a Faster Region-based convolutional network. The objective is to create a neural network that would be used by autonomous robots that can harvest fruits. The network is trained using RGB and NIR (near infra red) images. The combination of the RGB and NIR models is done in 2 separate cases: early and late fusion. Early fusion implies that the input layer has 4 channels: 3 for the RGB image and one for the NIR image. Late fusion uses 2 independently trained models that are merged by obtaining predictions from both models and averaging the results. The result is a multi modal network which obtains much better performance than the existing networks.
On the topic of autonomous robots used for harvesting, paper shows a network trained to recognize fruits in an orchard. This is a particularly difficult task because in order to optimize operations, images that span many fruit trees must be used. In such images, the amount of fruits can be large, in the case of almonds up to 1500 fruits per image. Also, because the images are taken outside, there is a lot of variance in luminosity, fruit size, clustering and view point. Like in paper , this project makes use of the Faster Region-based convolutional network, which is presented in a detailed view in paper . Related to the automatic harvest of fruits, article presents a method of detecting ripe strawberries and apples from orchards. The paper also highlights existing methods and their performance.
In the authors compile a list of the available state of the art methods for harvesting with the aid of robots. They also analyze the method and propose ways to improve them.
In one can see a method of generating synthetic images that are highly similar to empirical images. Specifically, this paper introduces a method for the generation of large-scale semantic segmentation datasets on a plant-part level of realistic agriculture scenes, including automated per-pixel class and depth labeling. One purpose of such synthetic dataset would be to bootstrap or pre-train computer vision models, which are fine-tuned thereafter on a smaller empirical image dataset. Similarly, in paper we can see a network trained on synthetic images that can count the number of fruits in images without actually detecting where they are in the image.
Another paper, , uses two back propagation neural networks trained on images with apple ”Gala” variety trees in order to predict the yield for the upcoming season. For this task, four features have been extracted from images: total cross-sectional area of fruits, fruit number, total cross-section area of small fruits, and cross-sectional area of foliage.
Paper presents an analysis of fruit detectability in relation to the angle of the camera when the image was taken. Based on this research, it was concluded that the fruit detectability was the highest on front views and looking with a zenith angle of upwards.
In papers we can see an approach to detecting fruits based on color, shape and texture. They highlight the difficulty of correctly classifying similar fruits of different species. They propose combining existing methods using the texture, shape and color of fruits to detect regions of interest from images. Similarly, in a method combining shape, size and color, texture of the fruits together with a k nearest neighbor algorithm is used to increase the accuracy of recognition.
One of the most recent works presents an algorithm based on the improved Chan–Vese level-set model and combined with the level-set idea and M-S mode . The proposed goal was to conduct night-time green grape detection. Combining the principle of the minimum circumscribed rectangle of fruit and the method of Hough straight-line detection, the picking point of the fruit stem was calculated.
Deep learning
In the area of image recognition and classification, the most successful results were obtained using artificial neural networks . These networks form the basis for most deep learning models.
Deep learning is a class of machine learning algorithms that use multiple layers that contain nonlinear processing units . Each level learns to transform its input data into a slightly more abstract and composite representation . Deep neural networks have managed to outperform other machine learning algorithms. They also achieved the first superhuman pattern recognition in certain domains . This is further reinforced by the fact that deep learning is considered as an important step towards obtaining Strong AI. Secondly, deep neural networks - specifically convolutional neural networks - have been proved to obtain great results in the field of image recognition.
In the rest of this section we will briefly describe some models of deep artificial neural networks along with some results for some related problems.
Convolutional neural networks (CNN) are part of the deep learning models. Such a network can be composed of convolutional layers, pooling layers, ReLU layers, fully connected layers and loss layers . In a typical CNN architecture, each convolutional layer is followed by a Rectified Linear Unit (ReLU) layer, then a Pooling layer then one or more convolutional layer and finally one or more fully connected layer. A characteristic that sets apart the CNN from a regular neural network is taking into account the structure of the images while processing them. Note that a regular neural network converts the input in a one dimensional array which makes the trained classifier less sensitive to positional changes.
Among the best results obtained on the MNIST dataset is done by using multi-column deep neural networks. As described in paper , they use multiple maps per layer with many layers of non-linear neurons. Even if the complexity of such networks makes them harder to train, by using graphical processors and special code written for them. The structure of the network uses winner-take-all neurons with max pooling that determine the winner neurons.
Another paper further reinforces the idea that convolutional networks have obtained better accuracy in the domain of computer vision. In paper an all convolutional network that gains very good performance on CIFAR-10 is described in detail. The paper proposes the replacement of pooling and fully connected layers with equivalent convolutional ones. This may increase the number of parameters and adds inter-feature dependencies however it can be mitigated by using smaller convolutional layers within the network and acts as a form of regularization.
In what follows we will describe each of the layers of a CNN network.
Convolutional layers are named after the convolution operation. In mathematics convolution is an operation on two functions that produces a third function that is the modified (convoluted) version of one of the original functions. The resulting function gives in integral of the pointwise multiplication of the two functions as a function of the amount that one of the original functions is translated .
A convolutional layer consists of groups of neurons that make up kernels. The kernels have a small size but they always have the same depth as the input. The neurons from a kernel are connected to a small region of the input, called the receptive field, because it is highly inefficient to link all neurons to all previous outputs in the case of inputs of high dimensions such as images. For example, a 100 x 100 image has 10000 pixels and if the first layer has 100 neurons, it would result in 1000000 parameters. Instead of each neuron having weights for the full dimension of the input, a neuron holds weights for the dimension of the kernel input. The kernels slide across the width and height of the input, extract high level features and produce a 2 dimensional activation map. The stride at which a kernel slides is given as a parameter. The output of a convolutional layer is made by stacking the resulted activation maps which in turned is used to define the input of the next layer.
Applying a convolutional layer over an image of size 32 X 32 results in an activation map of size 28 X 28. If we apply more convolutional layers, the size will be further reduced, and, as a result the image size is drastically reduced which produces loss of information and the vanishing gradient problem. To correct this, we use padding. Padding increases the size of a input data by filling constants around input data. In most of the cases, this constant is zero so the operation is named zero padding. ”Same” padding means that the output feature map has the same spatial dimensions as the input feature map. This tries to pad evenly left and right, but if the number of columns to be added is odd, it will add an extra column to the right. ”Valid” padding is equivalent to no padding.
The strides causes a kernel to skip over pixels in an image and not include them in the output. The strides determines how a convolution operation works with a kernel when a larger image and more complex kernel are used. As a kernel is sliding the input, it is using the strides parameter to determine how many positions to skip.
ReLU layer, or Rectified Linear Units layer, applies the activation function max(0, x). It does not reduce the size of the network, but it increases its nonlinear properties.
1.2 Pooling layers
Pooling layers are used on one hand to reduce the spatial dimensions of the representation and to reduce the amount of computation done in the network. The other use of pooling layers is to control overfitting. The most used pooling layer has filters of size 2 x 2 with a stride 2. This effectively reduces the input to a quarter of its original size.
1.3 Fully connected layers
Fully connected layers are layers from a regular neural network. Each neuron from a fully connected layer is linked to each output of the previous layer. The operations behind a convolutional layer are the same as in a fully connected layer. Thus, it is possible to convert between the two.
1.4 Loss layers
Loss layers are used to penalize the network for deviating from the expected output. This is normally the last layer of the network. Various loss function exist: softmax is used for predicting a class from multiple disjunct classes, sigmoid cross-entropy is used for predicting multiple independent probabilities (from the interval).
2 Recurrent neural network
Another deep learning algorithm is the recursive neural network . The paper proposes an improvement to the popular convolutional network in the form of a recurrent convolutional network. In this kind of architecture the same set of weights is recursively applied over some data. Traditionally, recurrent networks have been used to process sequential data, handwriting or speech recognition being the most known examples. By using recurrent convolutional layers with some max pool layers in between them and a final global max pool layer at the end several advantages are obtained. Firstly, within a layer, every unit takes into account the state of units in an increasingly larger area around it. Secondly, by having recurrent layers, the depth of the network is increased without adding more parameters. Recurrent networks have shown good results in natural language processing.
3 Deep belief network
Yet another model that is part of the deep learning algorithms is the deep belief network . A deep belief network is a probabilistic model composed by multiple layers of hidden units. The usages of a deep belief network are the same as the other presented networks but can also be used to pre-train a deep neural network in order to improve the initial values of the weights. This process is important because it can improve the quality of the network and can reduce training times. Deep belief networks can be combined with convolutional ones in order to obtain convolutional deep belief networks which exploit the advantages offered by both types of architectures.
Fruits-360 data set
In this section we describe how the data set was created and what it contains.
The images were obtained by filming the fruits while they are rotated by a motor and then extracting frames.
Fruits were planted in the shaft of a low speed motor (3 rpm) and a short movie of 20 seconds was recorded. Behind the fruits we placed a white sheet of paper as background.
However due to the variations in the lighting conditions, the background was not uniform and we wrote a dedicated algorithm which extract the fruit from the background. This algorithm is of flood fill type: we start from each edge of the image and we mark all pixels there, then we mark all pixels found in the neighborhood of the already marked pixels for which the distance between colors is less than a prescribed value. we repeat the previous step until no more pixels can be marked.
All marked pixels are considered as being background (which is then filled with white) and the rest of pixels are considered as belonging to the object. The maximum value for the distance between 2 neighbor pixels is a parameter of the algorithm and is set (by trial and error) for each movie.
Fruits were scaled to fit a 100x100 pixels image. Other datasets (like MNIST) use 28x28 images, but we feel that small size is detrimental when you have too similar objects (a red cherry looks very similar to a red apple in small images). Our future plan is to work with even larger images, but this will require much more longer training times.
To understand the complexity of background-removal process we have depicted in Figure 1 a fruit with its original background and after the background was removed and the fruit was scaled down to 100 x 100 pixels.
The resulted dataset has 90380 images of fruits and vegetables spread across 131 labels. Each image contains a single fruit or vegetable. Separately, the dataset contains another 103 images of multiple fruits. The data set is available on GitHub and Kaggle . The labels and the number of images for training are given in Table LABEL:fruits_summary_table.
TensorFlow library
For the purpose of implementing, training and testing the network described in this paper we used the TensorFlow library . This is an open source framework for machine learning created by Google for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays called tensors.
The main components in a TensorFlow system are the client, which uses the Session interface to communicate with the master, and one or more worker processes, with each worker process responsible for arbitrating access to one or more computational devices (such as CPU cores or GPU cards) and for executing graph nodes on those devices as instructed by the master.
TensorFlow offers some powerful features such as: it allows computation mapping to multiple machines, unlike most other similar frameworks; it has built in support for automatic gradient computation; it can partially execute subgraphs of the entire graph and it can add constraints to devices, like placing nodes on devices of a certain type, ensure that two or more objects are placed in the same space etc.
Starting with version 2.0, TensorFlow includes the features of the Keras framework. Keras provides wrappers over the operations implemented in TensorFlow, greatly simplifying calls, and reducing the overall amount of code required to train and test a model.
TensorFlow is used in several projects, such as the Inception Image Classification Model . This project introduced a state of the art network for classification and detection in the ImageNet Large-Scale Visual Recognition Challenge 2014. In this project the usage of the computing resources is improved by adjusting the network width and depth while keeping the computational budget constant.
Another project that employs the TensorFlow framework is DeepSpeech, developed by Mozilla. It is an open source Speech-To-Text engine based on Baidu’s Deep Speech architecture . The architecture is a state of the art recognition system developed using end-to-end deep learning. It is simpler that other architectures and does not need hand designed components for background noise, reverberation or speaker variation.
We will present the most important utilized methods and data types from TensorFlow together with a short description for each of them.
A convolutional layer is defined like this:
Computes a 2D convolution over the input of shape [batch, in_height, in_width, in_channels] and a kernel tensor of shape [filter_height, filter_width]. This op performs the following:
Flattens the filter to a 2-D matrix with shape [filter_height * filter_width * in_channels, output_channels].
Extracts image patches from the input tensor to form a virtual tensor of shape [batch, out_height, out_width, filter_height * filter_width * in_channels].
For each patch, right-multiplies the filter matrix and the image patch vector.
If padding is set to ”same”, the input is 0-padded so that the output keeps the same height and width; else, if the padding is set to ”valid”, the input is not 0-padded, thus the output may be smaller across the width and height
Performs the max pooling operation on the input. filter_size represents size of the window over which the max function is applied. strides represents the stride of the sliding window for each dimension of the input tensor. Similar to the Conv2D layer, the padding parameter can be ”valid”‘ or ”same”.
Computes the specified activation function given by the operation. We are using in this project the rectified linear operation - max(features, 0).
Randomly sets input values to 0 with probability prob. The method scales the non zero values by 1 / 1 - prob in order to preserve the sum of the elements.
The structure of the neural network used in experiments
For this project we used a convolutional neural network. As previously described this type of network makes use of convolutional layers, pooling layers, ReLU layers, fully connected layers and loss layers. In a typical CNN architecture, each convolutional layer is followed by a Rectified Linear Unit (ReLU) layer, then a Pooling layer then one or more convolutional layer and finally one or more fully connected layer.
Note again that a characteristic that sets apart the CNN from a regular neural network is taking into account the structure of the images while processing them. A regular neural network converts the input in a one dimensional array which makes the trained classifier less sensitive to positional changes.
The input that we used consists of standard RGB images of size 100 x 100 pixels.
The neural network that we used in this project has the structure given in Table LABEL:tab_settings.
A visual representation of the neural network used is given in Figure 2.
The first layer (Convolution #1) is a convolutional layer which applies 16 5 x 5 filters. On this layer we apply max pooling with a filter of shape 2 x 2 with stride 2 which specifies that the pooled regions do not overlap (Max-Pool #1). This also reduces the width and height to 50 pixels each.
The second convolutional (Convolution #2) layer applies 32 5 x 5 filters which outputs 32 activation maps. We apply on this layer the same kind of max pooling(Max-Pool #2) as on the first layer, shape 2 x 2 and stride 2.
The third convolutional (Convolution #3) layer applies 64 5 x 5 filters. Following is another max pool layer(Max-Pool #3) of shape 2 x 2 and stride 2.
The fourth convolutional (Convolution #4) layer applies 128 5 x 5 filters after which we apply a final max pool layer (Max-Pool #4).
Because of the four max pooling layers, the dimensions of the representation have each been reduced by a factor of 16, therefore the fifth layer, which is a fully connected layer(Fully Connected #1), has 7 x 7 x 16 inputs.
This layer feeds into another fully connected layer (Fully Connected #2) with 1024 inputs and 256 outputs.
The last layer is a softmax loss layer (Softmax) with 256 inputs. The number of outputs is equal to the number of classes.
We present a short scheme containing the flow of the the training process:
Numerical experiments
For the experiments we used the 90380 images split in 2 parts: training set - which consists of 67692 images of fruits and testing set - which is made of 22688 images. The other 103 images with multiple fruits were not used in the training and testing of the network.
Using an , from the package, we provide randomized input to the network. This object manages the image loading, batch generation and can perform augmentation such as vertical and horizontal flips.
We ran multiple scenarios in which the neural network was trained using different levels of data augmentation and preprocessing:
convert the input RGB images to grayscale
keep the input images in the RGB colorspace
convert the input RGB images to the HSV colorspace
convert the input RGB images to the HSV colorspace and to grayscale and merge them
apply random hue and saturation changes on the input RGB images, randomly flip them horizontally and vertically, then convert them to the HSV colorspace and to grayscale and merge them
For each scenario we used the previously described neural network which was trained for 25 epochs with batches of 50 images selected at random from the training set. Every epoch we calculated the accuracy using cross-validation. For testing we ran the trained network on the test set. The results for each case are presented in Table LABEL:tab_accuracy. All models achieved very high accuracy on the training dataset(99.98% or above). The model trained with only RGB images obtained the best performance on the test set. A potential explanation why the model trained on augmented data performed worse than the RGB one is that the training and test images were taken into identical lighting conditions and contain the same fruit. Thus, by augmenting the images, we are introducing variation in the training set that is not found in the test set. Conversely, training on the grayscale images produces a worse result because the conversion loses all features related to color. We further studied this problem by training and testing on just the apple classes of images. The results were similar, with high accuracy on the train data, but low accuracy on the test data.
In order to determine the best network configuration for classifying the images in our dataset, we took multiple configurations, used the train set to train them and then calculated their accuracy on the test and training set. In Table LABEL:tab_configuration_comparison we present the results.
From Table LABEL:tab_configuration_comparison we can see that all the tested configurations obtained perfect accuracy over the training dataset. On the test dataset, the models’ performance varied slightly, with configuration nr. 1 achieving the best accuracy. Configurations nr. 6, 7 and 8 were the only ones to achieve an accuracy below 98%.
The evolution of accuracy and loss during training is given in Figure 3. It can be seen that the training rapidly improves over the first 5 epochs (accuracy becomes greater than 90%). Afterwards, the improvements are small for the rest of the epochs.
Some of the incorrectly classified images are given in Table LABEL:tab_results.
Conclusions and further work
We described a new and complex database of images with fruits. Also we made some numerical experiments by using TensorFlow library in order to classify the images according to their content.
From our point of view one of the main objectives for the future is to improve the accuracy of the neural network. This involves further experimenting with the structure of the network. Various tweaks and changes to any layers as well as the introduction of new layers can provide completely different results. Another option is to replace all layers with convolutional layers. This has been shown to provide some improvement over the networks that have fully connected layers in their structure. A consequence of replacing all layers with convolutional ones is that there will be an increase in the number of parameters for the network . Another possibility is to replace the rectified linear units with exponential linear units. According to paper , this reduces computational complexity and add significantly better generalization performance than rectified linear units on networks with more that 5 layers. We would like to try out these practices and also to try to find new configurations that provide interesting results.
In the near future we plan to create a mobile application which takes pictures of fruits and labels them accordingly.
Another objective is to expand the data set to include more fruits. This is a more time consuming process since we want to include items that were not used in most others related papers.
Acknowledgments
A preliminary version of this dataset with 25 fruits was presented during the Students Communication Session from Babeş-Bolyai University, June 2017.
Appendix
In this section we present the source code and project structure used in the numerical experiment described in this paper. The source code can be downloaded from GitHub .
The source code is organized (on GitHub ) as follows: \dirtree.1 root_directory. .2 image_classification. .3 Fruits-360 CNN.py. .3 labels.txt. .2 Test. .2 Training.
In order to run the project from the command line, first make sure the PYTHONPATH system variable contains the path to the root_directory.
Following, we will provide explanations for the code. We will begin with the definition of the general parameters and configurations of the project.
The following are defined in the Fruits-360 CNN.py file:
base_dir - the top level folder of the project
test_dir - the folder with the Test images
train_dir - the folder with the Training images
labels_file - the path to the optional file that contains the used labels
use_label_file - boolean used to determine whether we train the network on all the classes(variable is set to false, default) or only on those specified in the labels_file(variable set to true)
All these configurations can be changed to suit the setup of anyone using the code.