2023 MathorCup Mathematical Modeling Big Data Competition (Question A) | Pothole road detection and identification based on computer vision | Modeling secrets & article code ideas

Clang! Here comes the little secret!
I hope everyone can easily model with little secrets, and the mathorcup competition will continue to help everyone relax their mind~
Grab the little secrets and let’s go~
Let’s take a look at Question A of the MathorCup Mathematical Modeling Big Data Competition

Get the full version at the end of the article~

Restatement of the problem

Question 1: Pothole road detection and identification based on computer vision

Problem description: Pothole road detection and identification is a computer vision task that aims to identify roads with potholes from digital images (usually surface pothole images). This is of great significance for research and applications in fields such as geological exploration, aerospace science and natural disasters. Traditional classification algorithms have limited effectiveness in the face of the complexity and variability of pothole images. Therefore, the development of deep learning technology in recent years has provided new solutions. This question asks for building a model for identifying potholes in roads.

The specific tasks are as follows:

Question 1: Combine the given image files, extract image features, and build a model with high recognition rate, fast speed, and accurate classification, which can be used to identify whether the roads in the image are normal or potholes.

Question 2: Train the model built in Question 1 and evaluate the model from different dimensions.

Question 3: Use the trained model to identify pothole images in the test set, and put the recognition results in “test_result.csv”.

Question 2: Using other modeling methods, give specific modeling ideas for Question 1

Problem description: In addition to deep learning methods, other mathematical models and machine learning algorithms can also be considered to solve the problem of pothole road detection and identification. This question asks for specific ideas for an alternative modeling approach.

The specific tasks are as follows:

Question 2: Using other modeling methods, give specific modeling ideas for Question 1, including model selection, feature extraction, data preprocessing and other steps.

Question 3: Use the trained model to identify pothole roads on test data

Problem Description: In Problem 1, a pothole road detection and recognition model has been established. Now you need to use this trained model to identify potholes on the test data. The test data includes thousands of road images. Each image needs to be judged by the model as a normal road or a potholed road, and the recognition results are saved in a CSV file.

The specific tasks are as follows:

Question 3: Use the trained pothole road detection model to identify the test data, and put the identification results in the “test_result.csv” file, which should include the following fields:

  • fnames: file names of test images
  • label: classification label, 1 means normal road, 0 means pothole road

Question ideas

Question 1:

  1. Feature extraction:

    • First, image feature extraction is performed to convert the image into numerical features. Various feature extraction methods can be used, such as color histograms, texture features (such as gray-level co-occurrence matrix), shape features, etc.
  2. Data preparation:

    • Prepare a training dataset, including image features and corresponding labels. Labels are binary values (0 means potholes, 1 means normal roads).
  3. Feature normalization:

    • The extracted features are normalized to ensure that the scales of different features are consistent. Normalization typically uses the following formula:

X

standardized

=

X

?

μ

σ

X_{\text{standardized}} = \frac{X – \mu}{\sigma}

Xstandardized?=σX?μ?

in,

X

X

X is the original feature,

μ

\mu

μ is the mean value of the feature,

σ

\sigma

σ is the standard deviation of the feature.

  1. SVM model selection:
    • Choose SVM as classifier. The goal of SVM is to find a separating hyperplane that maximizes the separation between support vectors (sample points closest to the hyperplane). The classification formula is:

f

(

x

)

=

sign

(

w

?

x

+

b

)

f(x) = \text{sign}(\mathbf{w} \cdot \mathbf{x} + b)

f(x)=sign(w?x + b)

in,

w

\mathbf{w}

w is the normal vector,

x

\mathbf{x}

x is the eigenvector,

b

b

b is the intercept.

  1. SVM training:

    • The SVM model is trained using the training dataset to find the optimal separating hyperplane.
  2. Model evaluation:

    • Use the validation dataset to evaluate the performance of the SVM model. Commonly used evaluation indicators include accuracy, recall, precision, F1 score, etc.
  3. Hyperparameter tuning:

    • Perform hyperparameter tuning as needed, such as SVM kernel function selection, regularization parameter C selection, etc.
  4. Model deployment:

    • Once the performance requirements are met, the SVM model is deployed into practical applications for pothole road detection and identification.
import numpy as np
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Feature extraction (in the example, grayscale histogram is used as the feature, you can choose other features according to the actual situation)
def extract_features(images):
    #Here we use grayscale histogram as an example feature extraction method
    features = []
    for image in images:
        hist, _ = np.histogram(image.ravel(), bins=256, range=(0, 256))
        features.append(hist)
    return features

# data preparation
def prepare_data():
    # You need to write code to load the training data set, including images and corresponding labels
    # Sample data is used here, please replace it with your data according to the actual situation.
    images = np.random.rand(300, 64, 64) # Example of randomly generated image data
    labels = np.random.randint(2, size=300) # Example of randomly generated labels
    return images, labels

# Feature normalization
def standardize_features(features):
    # Standardize using mean and standard deviation
    mean = np.mean(features, axis=0)
    std = np.std(features, axis=0)
    standardized_features = (features - mean) / std
    return standardized_features

#SVM model training
def train_svm(features, labels):
    clf = svm.SVC(kernel='linear', C=1.0)
    clf.fit(features, labels)
    return clf

# Model evaluation
def evaluate_model(model, features, labels):
    predictions = model.predict(features)
    accuracy = accuracy_score(labels, predictions)
    precision = precision_score(labels, predictions)
    recall = recall_score(labels, predictions)
    f1 = f1_score(labels, predictions)
    return accuracy, precision, recall, f1

# Example usage
if __name__ == '__main':
#See complete code

Question 2

The following is a specific modeling idea using random forest:

Modeling ideas for question 2 (using random forest):

  1. Feature extraction:

    • Extract image features and convert images into numerical features. Various feature extraction methods can be used, such as color histograms, texture features, shape features, etc.
  2. Data preparation:

    • Prepare a training dataset, including image features and corresponding labels. Labels are binary values (0 means potholes, 1 means normal roads).
  3. Feature normalization:

    • The extracted features are normalized to ensure that the scales of different features are consistent. Normalization typically uses the following formula:

X

standardized

=

X

?

μ

σ

X_{\text{standardized}} = \frac{X – \mu}{\sigma}

Xstandardized?=σX?μ?

Among them, (X) is the original feature, (\mu) is the mean of the feature, and (\sigma) is the standard deviation of the feature.

  1. Random Forest Model Selection:
    • Random forest was chosen as the classifier. Random forest is an ensemble learning method that performs classification by building multiple decision trees and integrating their results. The classification formula is:

f

(

x

)

=

majority vote

(

tree

1

(

x

)

,

tree

2

(

x

)

,

,

tree

n

(

x

)

)

f(x) = \text{majority vote}(\text{tree}_1(x), \text{tree}_2(x), \ldots, \text{tree}_n(x))

f(x)=majority vote(tree1?(x),tree2?(x),…,treen?(x))

in,

tree

i

(

x

)

\text{tree}_i(x)

treei?(x) is the classification result of the i-th decision tree.

  1. Random Forest Training:

    • Use the training dataset to train a random forest model, which will learn how to map image features to class labels.
  2. Model evaluation:

    • Use the validation dataset to evaluate the performance of the random forest model. Commonly used evaluation indicators include accuracy, recall, precision, F1 score, etc.
  3. Hyperparameter tuning:

    • Perform hyperparameter tuning as needed to improve model performance. This can be achieved through cross-validation.
  4. Model deployment:

    • Once the performance requirements are met, the random forest model is deployed into real applications for pothole road detection and identification.
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Feature extraction (in the example, grayscale histogram is used as the feature, you can choose other features according to the actual situation)
def extract_features(images):
    #Here we use grayscale histogram as an example feature extraction method
    features = []
    for image in images:
        hist, _ = np.histogram(image.ravel(), bins=256, range=(0, 256))
        features.append(hist)
    return features

# data preparation
def prepare_data():
    # You need to write code to load the training data set, including images and corresponding labels
    # Sample data is used here, please replace it with your data according to the actual situation.
    images = np.random.rand(300, 64, 64) # Example of randomly generated image data
    labels = np.random.randint(2, size=300) # Example of randomly generated labels
    return images, labels

# Feature normalization
def standardize_features(features):
    # Standardize using mean and standard deviation
    mean = np.mean(features, axis=0)
    std = np.std(features, axis=0)
    standardized_features = (features - mean) / std
    return standardized_features

# Random forest model training
def train_random_forest(features, labels):
    clf = RandomForestClassifier(n_estimators=100, random_state=0)
    clf.fit(features, labels)
    return clf

# Model evaluation
def evaluate_model(model, features, labels):
    predictions = model.predict(features)
    accuracy = accuracy_score(labels, predictions)
    precision = precision_score(labels, predictions)
    recall = recall_score(labels, predictions)
    f1 = f1_score(labels, predictions)

Question 3

  1. Load a trained model:

    • Load the trained model from the file, which is the model trained for pothole detection and identification in question one or other tasks.
  2. Load test data:

    • Load the test data set, including images of potholes to be identified. Make sure that the format of the test data set matches the input format expected by the model.
  3. Image preprocessing:

    • The test data is subjected to the same image preprocessing steps as the training data to ensure that the data matches the input to the model. This includes operations such as image normalization, scaling, channel processing, etc.

    • Image normalization: Usually, image pixel values need to be normalized to a fixed range, such as [0, 1] or [-1, 1]. This helps reduce the range of variation in the data and makes the model easier to work with.

    • Image scaling: Models are usually trained on fixed-size images, so test images need to be scaled to the same size. This can be achieved by interpolation methods, such as bilinear interpolation.

    • Channel processing: If the training data does not match the number of input channels expected by the model, channel processing is required. For example, if the model expects the input to be an RGB image, but the test data is a grayscale image, the grayscale image needs to be expanded to RGB.

  4. Model prediction:

    • Make predictions on test data using the loaded trained model. The model will output predictions, usually probability values or class labels.
  5. Result saving:

    • Save the model’s prediction results in a CSV file, which should include the file name of the test image and the corresponding classification identifier. You can use Python’s CSV library to generate CSV files.
import numpy as np
import pandas as pd
from sklearn.externals import joblib # Used to load trained models

#Load the trained model
model = joblib.load('trained_model.pkl') # Replace with your model file path

#Load test data
test_data = load_test_data() # Replace with the code for loading test data

# Image preprocessing (only image normalization is included in the example)
def preprocess_image(image):
    # Image normalization, scaling pixel values to the [0, 1] range
    normalized_image = image / 255.0
    return normalized_image

# Loop through the test data and make predictions
predictions = []

for image in test_data:
    preprocessed_image = preprocess_image(image)
    # Model prediction, usually output probability value or category label
    prediction = model.predict([preprocessed_image])
    predictions.append(prediction[0]) # Assume that the model output is a category label

#Create a DataFrame containing test results

For the full content, click the business card below to learn more~
Let’s pay attention to Tips on Mathematical Modeling and sprint for the mathorcup award!