Image Classification: Soil Type

Objective: Train and test a Convolutional Neural Network to detect soil types from images.

Deep Learning (DL) is a subset of Machine Learning that uses Neural Network inspired architecture to make predictions. Convolutional Neural Networks (CNN) are a type of DL model that is effective in learning patterns in 2-dimensional data such as images. Images of soils are used to train a classifier to identify common soil types and fractions of soil type.

This exercise demonstrates the use of image classification to distinguish between soil types. Although applied to soil, the same methods and code can be used for any type or number of items. This example can be modified by including train and test photos in folders that are named with the item type. The code automatically takes the name of the folder as the photo label for training the classifier. Additional examples of image-based CNNs are found in the Bit and Crack Classification Case Study.

Getting Started

There are a few required packages for these exercises and can be installed with pip. These include OpenCV and TensorFlow from Google.

pip install opencv-python
pip install tensorflow

This exercise uses photos of soils in a compressed archive.

The archive contains two folders, a test folder and train folder with subdirectories corresponding to the possible soil types (Gravel, Sand, and Silt). The images are found within each subdirectory. The tree structure of the folders is:

  │   ├───Gravel
  │   ├───Sand
  │   └───Silt

The photos are imported into the Python session. The first step is to process the images into a format that 1) makes the data readable to the model, and 2) provides more training material for the model to learn. For example, the train_processor variable scales the data so that it can be a feature (input) for the model, but also takes each images and augments it so that the model can learn from multiple variations of the same picture. It flips it horizontally, rotates it, and shifts it, and more to make sure the model learns from the shape of the bit rather than the orientation or size.

The next step is to build the Convolutional Neural Network (CNN) model. Options are number of convolutional layers, fully connected dense layers, the number of nodes in each layer, and the number of training epochs. For more information on these parameters and CNNs in general, see Computer Vision with Deep Learning.

The image is divided into pieces and classification occurs on the individual tiles. An example result is:

 Percent Gravel: 25%
 Percent Sand: 0%
 Percent Silt: 75%
 Time to Classify: 0.86 seconds


  • Soleymani, M., Bonyani, M., Mahami, H. and Nasirzadeh, F., 2021. Construction material classification on imbalanced datasets for construction monitoring automation using Vision Transformer (ViT) architecture. arXiv preprint arXiv:2108.09527.
  • Bit and Crack Classification: Case study on image classification to identify drilling bit types and cracks in concrete. Bit and Crack Case Study
  • Sand, Seed, Stone: Local Binary Patterns (LBTs) to detect textures. Texture Classification

Thanks to Peter Van Katwyk for generating the soil classification case study.