# Control of a Mobile Line-Following Robot Using Neural Networks
## 4 Implementation
### Cnn
So, if the vehicle was turning to the left on the previous reading, the robot, when losing the line, will spin to the left and vice versa to the right. For the classification task, eight different folders were created, one with the name of each category, where all images taken in the previous step were manually classified. Although this classification method is simpler, it requires considerably more time. With this method, the robot will have eight different modes of operation. However, using more categories would lead to greater difficulty in classifying the _dataset_ due to the greater ambiguity in distinguishing these categories. With the dataset obtained and classified, it can finally be used to train the CNN network, as depicted in Figure 19:  
* **Configure model**: In the first step, the CNN network is configured using the LeNet architecture, which is suitable for image classification tasks. The model is configured to receive \(36\times 36\)-pixel grayscale images and classify them into one of eight categories, each representing a different driving command;
* **Load categorized images and assign labels**: The dataset containing images is loaded from the specified directories. Each image is read and resized to \(36\times 36\) pixels to match the input dimensions of the CNN. The images are stored in a list for further processing. The labels are derived from the directory structure: each folder corresponds to a driving command (e.g., 'forward' or'soft_left'), which is then converted into numerical label; \begin{table}
\begin{tabular}{c c c} \hline \hline
**Category** & **Right Speed** & **Left Speed** \\ \hline forward & 60 & 60 \\ \hline soft\_left & 50 & 70 \\ \hline soft\_right & 70 & 50 \\ \hline hard\_left & 30 & 90 \\ \hline hard\_right & 90 & 30 \\ \hline spin\_left & \(-50\) & 70 \\ \hline spin\_right & 70 & \(-50\) \\ \hline lost\_line & - & - \\ \hline \hline \end{tabular}
\end{table} 
Table 4: Speed of the right and left motors for each category. Figure 18: Example of a cropped final image. * **Divide the dataset into training and test data**: To ensure a good generalization of the model, the dataset is split into training and test sets. The split is configured so that 75% of the data is used to train the model and the remaining 25% are reserved for testing. This split enables the model's performance to be assessed on data not observed after training;
* **Train the model**: The CNN is trained using the training data. To avoid overfitting, early stopping is implemented. This technique monitors validation loss and stops training if the model stops improving after a certain number of epochs, ensuring that the model does not overfit the training data and generalizes better;
* **Model evaluation**: After training, the model performance is evaluated by plotting the training and validation loss over iterations. These plots provide information on how well the model has learned to generalize. If the validation loss continues to decrease, this indicates that the model is improving;
* **Save the model**: Finally, after the model has been successfully trained, it is saved to disk. This enables the model to be used in the future. The CNN model used is shown in Figure 20, and its hyperparameters are provided in Table 5.  
Figure 19: Process of training the CNN. Figure 20: Structure of the CNN used. \begin{table}
\begin{tabular}{c c} \hline \hline
**Hyperparameter** & **Value** \\ \hline Input Format & (height, width, depth) \\ \hline Number of Convolutional Layers & 2 \\ \hline Filters in First Convolutional Layer & 20 \\ \hline Filters in Second Convolutional Layer & 50 \\ \hline Filter Size (Both Convolutional Layers) & (5, 5) \\ \hline Activation Function & ReLU \\ \hline Pooling Size & (2, 2) \\ \hline Pooling Stride & (2, 2) \\ \hline Dropout (After Pooling Layers) & 0.25 \\ \hline Dropout (Before Classification Layers) & 0.5 \\ \hline Number of Dense Layers & 2 \\ \hline Units in First Dense Layer & 500 \\ \hline \hline \end{tabular}
\end{table} 
Table 5: Hyperparameters of the LeNet model.