Data Collection
In this chapter, we use the following three datasets: (i) iris, (ii) soybean aphids, and (iii) weed species. The iris dataset was used for the demonstration and decoding of different ML classification methods, while the other two datasets were used for showing the application of ML classification methods on agricultural field applications.
A brief description of each dataset and the process of obtaining the features are discussed subsequently.
Iris Dataset
The iris is a multivariate dataset that is well-known in data analysis which was introduced by Fisher (1936) to demonstrate the LDA. As iris datasets have been widely used, even in recent literature, for presenting and testing new classification methods (Kamarulzalis and Abdullah, 2019; Singh and Srivastava, 2019; Bai et al., 2019), this study also employed such a dataset to evaluate the performance of the decoded LDA and kNN algorithms. The dataset comprises of 150 observations from three iris flower species (setosa, versicolor, and virginica). Each data point consists of four features (units cm), such as sepal length, sepal width, petal length, and petal width. The dataset is preinstalled in R software command, which can be imported using the data(iris) command.
Soybean Aphid Dataset
The soybean aphid dataset was obtained from the digital images of aphid infected soybean leaves from a greenhouse experiment (Sunoj et ah, 2017). The color digital images were acquired using a digital single-lens reflex camera (Canon EOS Rebel T2i). Identifying aphids on the soybean leaf is challenging due to the presence of other objects such as exoskeletons and leaf spots. All these objects exhibit only a subtle difference in color, but a clear difference in shape (Figure 2.2A). Therefore, the images were preprocessed to extract shape features of individual objects using ImageJ. The sequence of image preprocessing steps involved in shape-feature extraction is presented in Figure 2.2.
The acquired color digital image (Figure 2.2A) was denoised by applying a median filter of two-pixel radius (Figure 2.2B). The filtered image was converted into a grayscale image to facilitate the thresholding process (Figure 2.2C). ImageJ offers 17 optimized thresholding schemes out of which a Yen thresholding scheme (Yen et al., 1995) was suitable as it appropriately selected the necessary portions (Figure 2.2D). Applying the thresholded scheme produced the binary image (Figure 2.2E). The artifacts and rough object boundary in the binary image were removed by applying the "Open" morphological operation (Figure 2.2F). It is essential to segment the overlapping objects into individual objects before feature extraction. The watershed segmentation offered by ImageJ resulted in oversegmentation; therefore, the overlapping objects were manually segmented by drawing a one-pixel-thick white line using ImageJ’s "paintbrush tool” (Figure 2.2G). The segmented objects were subjected to ImageJ’s "Analyze Particles” to derive various shape features (see Section 2.3.3) used in the analysis.
Weed Species Dataset
The weed species dataset was obtained from digital images acquired in a greenhouse for identifying the potential weed species grown in the fields. The weeds were grown in soil samples that naturally had seeds, which were transferred to the greenhouse in plastic trays from organic field experiment plots, and imaged right after emergence. Visually counting and categorizing the weed species at an early stage from multiple
FIGURE 2.2 Image preprocessing steps for feature extraction from the digital images of soybean leaves: (A) Color digital image of the soybean leaf (zoomed-in) showing aphids, exoskeletons, and leaf spots; (B) Median filtered (radius = 2 pixel) image to remove noise; (C) Grayscale image; (D) Thresholded regions in dark gray or red obtained by applying ImageJ’s Yen thresholding scheme; (E) Binary image obtained after thresholding; (F) Artifacts removed from image after applying single “Open" operation; and (G) Manually segmented image by drawing one-pixel-thick line using ImageJ’s paintbrush tool.
trays is challenging. Therefore, an image-based approach could be developed from the shape features derived from the weed species images. A comprehensive review of weed identification through image processing techniques also showed a lack of literature on identifying the “just emerged” weed species from digital images (Wang et al„ 2019).
The digital images were captured using a consumer-grade color digital camera (Samsung NX3300). A user-developed JavaScript plugin was employed to process the images for shape features extraction (Figure 2.3). The input color digital image consisted of two weed species that clearly exhibited differences in shape (Figure 2.ЗА). The color digital image was split into Lab channels, out of which the “a” channel displayed a clear difference in gray intensities between the background soil and foreground plants (Figure 2.3B). Applying the ImageJ's Yen thresholding scheme on the “a” channel selected the foreground plant portions (dark gray or red) of the image (Figure 2.3C). The thresholded binary image captured even tiny plant portions, which were not distinctly visible in the input image (Figure 2.3D).
The weeds were grown in a random arrangement that sometimes resulted in an overlap between objects. In addition, since the plants were at an early growth stage, the thresholding operation disconnected a few objects. To overcome these issues, the binary image was visually inspected and the overlapping objects were manually segmented by drawing a one-pixel-thick white line (green arrows. Figure 2.3E). Similarly, to bridge the disconnected objects, a one-pixel-thick black line was manually drawn at appropriate locations (dotted-arrows, Figure 2.3E).
FIGURE 2.3 Image preprocessing steps for feature extraction from an image of weed species seedlings. (A) Input color digital image; (B) The “a” color channel in Lab color space; (C) Thresholded regions in dark gray or red obtained through ImageJ’s Yen thresholding scheme; (D) Binary image after thresholding; and (E) Final preprocessed binary image (after correction) used for shape feature extraction.
Carefully delineating the individual object before measuring shape features is essential because the performance of ML algorithms solely depends on the quality of the input data. The shape features discussed subsequently (see Section 2.3.3) were extracted from the final preprocessed image (Figure 2.3E; without colored arrows) and used as the training data for ML models.
Shape Features Extraction
Shape features are dimensionless parameters, which are not affected by image resolution, and were used as measures in classifying objects. ImageJ offers four shape features, such as aspect ratio, circularity, roundness, and solidity (Equations (2.1)-
- (2.4) ), along with various other measurements. Those measurements were used for deriving other shape features reported in the literature (Igathinathane et al., 2008; Sunoj et al., 2017; Du and Sun, 2004) and are presented as follows (Equations
- (2.5) —(2.13)):
where a is the major axis length (pixel); b is the minor axis length (pixel); A is the area (pixel2); P is the perimeter (pixel); Ac is the convex area (pixel2); #BB is the bounding box height (pixel); IVBB is the bounding box width (pixel); Dp is the Feret diameter, also called the maximum caliper diameter (pixel); and DminF is the minimum Feret diameter, also called the minimum caliper diameter (pixel).
Data Cleaning
Cleaning the dataset is essential since even robust ML methods underperform while processing the improper and uncleaned data. On average, about 60% of the time in ML is spent on cleaning and unifying the obtained data (Chu et al., 2016). Cleaning the data usually refers to filtering and editing the data to make it easier to explore, understand, and model. Data filtering is removing unnecessary, unwanted, and irrelevant data from the dataset, while editing refers to modifying the data into a desirable and accessible format. Data cleaning was performed in R using conventional techniques, functions, and commands.
The common steps followed include checking missing values, replacing or dropping missing values, and modifying the variable type. In our datasets, the presence of missing values were checked using the is.na() and is.nanO functions. There were no missing values in our dataset; however, if it exists, the user may choose to replace the missing data w'ith a specified value or omit the entry. To replace a value w'ith 0, the function data[is.na(data)] <- 0 can be used; while to omit the data, the function na.omit() can be used. Once the data is cleaned, it is desirable to convert the data type into an appropriate form (e.g., class as factor, feature values as numeric). These can be performed using R coercion functions, such as as.numeric(), as.integer(), as.character(), and as.factor().
Feature Selection
The datasets considered for ML modeling consists of many features (predictor variables) and an outcome (dependent variable). Mostly only a group of the features are relevant, i.e., it provides information to predict the outcome w'hile the remaining are irrelevant features that are not informative. Developing ML models using all the available features results in computational burden and reduced accuracy. Therefore, the relevant features have to be carefully identified and selected to build the ML models while discarding others (Das et al., 2020a).
Feature selection is often confused with dimensionality reduction, as in those conducted using methods such as principal component analysis and single value decomposition. Both feature selection and dimensionality reduction methods seek to reduce the number of features or attributes in the dataset. However, the dimensionality reduction achieves this by creating new combinations of all the features (Das et al., 2020b), while the feature selection mutes the irrelevant features which are less useful in predicting the outcome variable. In general, fewer relevant features are desirable since it produces simpler models and provides a cost-effective and computationally efficient solution. Therefore, feature selection methods are more beneficial and useful in developing ML models than the dimensionality reduction methods. Feature selection methods can be classified into three general methods, such as filter, wrapper, and embedded methods which are briefly described.
Filter Methods
The filter method of feature selection assigns a score to each feature in the dataset by applying statistical information and correlation measure. The features are ranked based on the score, which implies the order of importance to the output variable. The features are either selected or discarded based on the rank. Some examples of filter selection methods are information gain (Joachims, 1998), chi-square test (Jin et al., 2006), and Relief (Kira and Rendefi, 1992).
Wrapper Methods
The wrapper method uses an inbuilt ML classification and regression algorithm, where the combination feature subsets are created based on approaches such as forward or backward selection and heuristic selection. These selected features are given to the specific predictive model algorithm, and the scores are assigned to the feature subset based on the accuracy of the model. Similarly, each feature subset is analyzed, and the one yielding the highest model accuracy is selected. An example of a wrapper method is the forward and backward selection algorithm (Mao. 2004).
Embedded Methods
The embedded method of feature selection learns the features that contribute the most to the accuracy of the model. These methods are also known as the regularization or penalization methods, where the coefficients of the irrelevant features are penalized and removed from the model. Some of the examples of the regularization algorithms are Lasso (Li et al., 2006), elastic net (Zou and Hastie, 2005), and ridge regression (Endelman, 2011).
Among these feature selection methods, the Relief algorithm, which falls under filter methods, is mathematically straightforward, provides a measure of the influence for the features, and can deal with multi-classes in the outcome variable. Moreover, it is the only feature selection method that is capable of detecting the feature dependencies using the simple nearest neighbor approach (Urbanowicz et al., 2018). Therefore, the Relief algorithm was chosen for feature selection in the two considered agricultural applications.
Relief Algorithms
The Relief algorithm is a family of three algorithms, such as basic Relief. ReliefF, and RRelieflF. The basic Relief algorithm can only handle the classification of two classes; the ReliefF, which is an extension of basic Relief, can handle multi-classes; and RRelietF can handle continuous regression classes. All three algorithms have the same underlying working mechanism. Since this chapter deals with classification with ML for a dataset containing multi-classes that are categorical, only the ReliefF method was appropriate. However, to follow the algorithm working principle, a manual example using the basic Relief algorithm for binary class data is demonstrated in this section since the manual demonstration using the ReliefF for multi-classes is tedious. Studies detailing the ReliefF algorithm can be found elsewhere (Kononenko, 1994; Robnik-Sikonja and Kononenko, 2003).
The pseudocode (Algorithm 2.1) decodes the basic Relief algorithm. Weights are initialized to zero for all the features in the data. The Relief algorithm identifies the two nearest neighbor, nearest hit (H) and miss (M), using Manhattan distance (rectilinear distance as sum of projections as opposed to the Euclidean distance as the direct straight line distance between points) calculated between the randomly selected instance (/?,) and the other instances in the dataset. The H instance refers to the instance that belongs to the same class and at the least distance to the random instance. The M refers to the instance that belongs to a different class and at the nearest distance to the random instance. The weight revisions are divided by the number of iterations (z) to normalize the weights between -1 and +1. As a rule of thumb, z is calculated as the square root value of the total number of observations in the dataset (n).
Algorithm 2.1 The pseudocode of the basic Relief algorithm l: procedure Basic_Relief (dataset)
- 2: W[Fj] = 0 > Set all weights of the features to zero
- 3: Z = -Jh > n - number of observations in the dataset
- 4: for (l to z) do > z - number of iteration for weights revision
- 5: select a random instance (R,)
- 6: find nearest hit (H) and miss (M) instances > using Manhattan distance
- 7: for (1 to _/) do >; is the number of features; W[F)] revised
- 8: W[F;] = W[Fj] — diff(F;,Rj,H)/z + diff(F,,R„M)/z
- 9: end for
- 10: end for
- 11: end procedure while,
| valuefF (, R.) — value(Ff, H) |
diff(F,R(,H) = --Ц-Ц-, ''
max(F;) — min(Fy)
|value(F(,R.)—value(F(, M)|
difffF.Rj.M) = ---
max(Fy) - min(F;)
where, Fj are the different features in the data set; F, = F„ F2.......F„.
The idea of revising the weights of features is based on the dissimilarity of the random instance with H and M. A significant difference between the nearest hit and the random instance indicates that the features separate two instances belonging to the same class, which is not desirable; therefore, the weights are decreased. However, a significant difference between the nearest miss and the random instance implies that the features separate the two instances belonging to different classes, which is desirable; therefore, the weights are increased.
The following R program developed and based on the decoded Relief algorithm (Algorithm 2.1) estimates the weights of the features as outcomes (Figure 2.4). For comparison, the direct black-box approach was also used as an illustration. These weights are further used to determine the most significant features.
For demonstration (Figure 2.4), consider a sample dataset with 20 total observations (n = 20), three features (F,, F2, and F3), two classes (classes 1 and 2), and five instances (R,. R2, ..., R5). The relief algorithm is employed to this sample dataset to determine the most influential features, amongst F„ F2, and F3, ranked according to the weights used in classifying the dataset into classes 1 and 2.
The features producing the highest weights are considered to be the most influential. In the case of sample data, the feature that ranked the highest, based on the weights, is F2(0.93), followed by F,(0.60) and F3(-0.50), respectively. The feature ranking obtained using the in-built (black-box) function attrEvalQ, under the R package CORElearn developed and based on the basic Relief algorithm, produced different weights (Figure 2.4).