model
Browse files- soil_type_model.h5 +3 -0
- train_soil_classifier.py +205 -0
soil_type_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d28500d034cb138271a60a1ee91e31fc124ec7204eadc31a317f5f2e621a6e5f
|
| 3 |
+
size 116262336
|
train_soil_classifier.py
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras import layers, models
|
| 5 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# Define paths
|
| 10 |
+
dataset_path = r"C:\Users\Joel\Desktop\bin\Soil-Type-1\Soil Types"
|
| 11 |
+
output_model_path = r"C:\Users\Joel\Desktop\bin\Soil-Type-1\soil_type_model.h5"
|
| 12 |
+
|
| 13 |
+
def create_soil_type_classifier():
|
| 14 |
+
# 1. Organize and prepare the dataset
|
| 15 |
+
|
| 16 |
+
# Create train/validation directories if they don't exist
|
| 17 |
+
train_dir = os.path.join(dataset_path, 'train')
|
| 18 |
+
val_dir = os.path.join(dataset_path, 'validation')
|
| 19 |
+
|
| 20 |
+
if not os.path.exists(train_dir):
|
| 21 |
+
os.makedirs(train_dir)
|
| 22 |
+
if not os.path.exists(val_dir):
|
| 23 |
+
os.makedirs(val_dir)
|
| 24 |
+
|
| 25 |
+
# Create subdirectories for each soil type in train and validation folders
|
| 26 |
+
soil_types = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))
|
| 27 |
+
and d not in ['train', 'validation']]
|
| 28 |
+
|
| 29 |
+
for soil_type in soil_types:
|
| 30 |
+
train_soil_dir = os.path.join(train_dir, soil_type)
|
| 31 |
+
val_soil_dir = os.path.join(val_dir, soil_type)
|
| 32 |
+
|
| 33 |
+
if not os.path.exists(train_soil_dir):
|
| 34 |
+
os.makedirs(train_soil_dir)
|
| 35 |
+
if not os.path.exists(val_soil_dir):
|
| 36 |
+
os.makedirs(val_soil_dir)
|
| 37 |
+
|
| 38 |
+
# 2. Split data into training and validation sets (80/20 split)
|
| 39 |
+
for soil_type in soil_types:
|
| 40 |
+
soil_dir = os.path.join(dataset_path, soil_type)
|
| 41 |
+
image_files = [f for f in os.listdir(soil_dir) if f.endswith(('.jpg', '.jpeg', '.png'))]
|
| 42 |
+
|
| 43 |
+
# Shuffle image files
|
| 44 |
+
np.random.shuffle(image_files)
|
| 45 |
+
|
| 46 |
+
# Split the dataset (80% train, 20% validation)
|
| 47 |
+
train_files = image_files[:int(0.8 * len(image_files))]
|
| 48 |
+
val_files = image_files[int(0.8 * len(image_files)):]
|
| 49 |
+
|
| 50 |
+
# Copy files to train directory
|
| 51 |
+
import shutil
|
| 52 |
+
for file in train_files:
|
| 53 |
+
src = os.path.join(soil_dir, file)
|
| 54 |
+
dst = os.path.join(train_dir, soil_type, file)
|
| 55 |
+
shutil.copy(src, dst)
|
| 56 |
+
|
| 57 |
+
# Copy files to validation directory
|
| 58 |
+
for file in val_files:
|
| 59 |
+
src = os.path.join(soil_dir, file)
|
| 60 |
+
dst = os.path.join(val_dir, soil_type, file)
|
| 61 |
+
shutil.copy(src, dst)
|
| 62 |
+
|
| 63 |
+
print(f"Dataset organized into {len(soil_types)} categories: {', '.join(soil_types)}")
|
| 64 |
+
|
| 65 |
+
# 3. Set up data augmentation and preprocessing
|
| 66 |
+
train_datagen = ImageDataGenerator(
|
| 67 |
+
rescale=1./255,
|
| 68 |
+
rotation_range=20,
|
| 69 |
+
width_shift_range=0.2,
|
| 70 |
+
height_shift_range=0.2,
|
| 71 |
+
shear_range=0.2,
|
| 72 |
+
zoom_range=0.2,
|
| 73 |
+
horizontal_flip=True,
|
| 74 |
+
fill_mode='nearest'
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
validation_datagen = ImageDataGenerator(rescale=1./255)
|
| 78 |
+
|
| 79 |
+
# Set target image size
|
| 80 |
+
img_height, img_width = 224, 224
|
| 81 |
+
batch_size = 32
|
| 82 |
+
|
| 83 |
+
# Create data generators
|
| 84 |
+
train_generator = train_datagen.flow_from_directory(
|
| 85 |
+
train_dir,
|
| 86 |
+
target_size=(img_height, img_width),
|
| 87 |
+
batch_size=batch_size,
|
| 88 |
+
class_mode='categorical'
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
validation_generator = validation_datagen.flow_from_directory(
|
| 92 |
+
val_dir,
|
| 93 |
+
target_size=(img_height, img_width),
|
| 94 |
+
batch_size=batch_size,
|
| 95 |
+
class_mode='categorical'
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# 4. Create and compile the model
|
| 99 |
+
model = models.Sequential([
|
| 100 |
+
# Base convolutional layers
|
| 101 |
+
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)),
|
| 102 |
+
layers.MaxPooling2D((2, 2)),
|
| 103 |
+
layers.Conv2D(64, (3, 3), activation='relu'),
|
| 104 |
+
layers.MaxPooling2D((2, 2)),
|
| 105 |
+
layers.Conv2D(128, (3, 3), activation='relu'),
|
| 106 |
+
layers.MaxPooling2D((2, 2)),
|
| 107 |
+
layers.Conv2D(128, (3, 3), activation='relu'),
|
| 108 |
+
layers.MaxPooling2D((2, 2)),
|
| 109 |
+
|
| 110 |
+
# Flatten and dense layers
|
| 111 |
+
layers.Flatten(),
|
| 112 |
+
layers.Dropout(0.5), # Reduce overfitting
|
| 113 |
+
layers.Dense(512, activation='relu'),
|
| 114 |
+
layers.Dense(len(soil_types), activation='softmax') # Output neurons equal to number of soil types
|
| 115 |
+
])
|
| 116 |
+
|
| 117 |
+
# Compile the model
|
| 118 |
+
model.compile(
|
| 119 |
+
optimizer='adam',
|
| 120 |
+
loss='categorical_crossentropy',
|
| 121 |
+
metrics=['accuracy']
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# 5. Train the model
|
| 125 |
+
epochs = 15
|
| 126 |
+
|
| 127 |
+
history = model.fit(
|
| 128 |
+
train_generator,
|
| 129 |
+
steps_per_epoch=train_generator.samples // batch_size,
|
| 130 |
+
epochs=epochs,
|
| 131 |
+
validation_data=validation_generator,
|
| 132 |
+
validation_steps=validation_generator.samples // batch_size
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# 6. Save the model
|
| 136 |
+
model.save(output_model_path)
|
| 137 |
+
print(f"Model saved to {output_model_path}")
|
| 138 |
+
|
| 139 |
+
# 7. Plot training results
|
| 140 |
+
acc = history.history['accuracy']
|
| 141 |
+
val_acc = history.history['val_accuracy']
|
| 142 |
+
loss = history.history['loss']
|
| 143 |
+
val_loss = history.history['val_loss']
|
| 144 |
+
|
| 145 |
+
plt.figure(figsize=(12, 4))
|
| 146 |
+
plt.subplot(1, 2, 1)
|
| 147 |
+
plt.plot(acc, label='Training Accuracy')
|
| 148 |
+
plt.plot(val_acc, label='Validation Accuracy')
|
| 149 |
+
plt.xlabel('Epoch')
|
| 150 |
+
plt.ylabel('Accuracy')
|
| 151 |
+
plt.legend()
|
| 152 |
+
|
| 153 |
+
plt.subplot(1, 2, 2)
|
| 154 |
+
plt.plot(loss, label='Training Loss')
|
| 155 |
+
plt.plot(val_loss, label='Validation Loss')
|
| 156 |
+
plt.xlabel('Epoch')
|
| 157 |
+
plt.ylabel('Loss')
|
| 158 |
+
plt.legend()
|
| 159 |
+
|
| 160 |
+
plt.tight_layout()
|
| 161 |
+
plt.savefig(os.path.join(dataset_path, 'training_results.png'))
|
| 162 |
+
plt.show()
|
| 163 |
+
|
| 164 |
+
# Return model and class indices for later use
|
| 165 |
+
return model, train_generator.class_indices
|
| 166 |
+
|
| 167 |
+
def predict_soil_type(model, image_path, class_indices):
|
| 168 |
+
"""
|
| 169 |
+
Predict soil type for a given image
|
| 170 |
+
"""
|
| 171 |
+
img_height, img_width = 224, 224
|
| 172 |
+
|
| 173 |
+
# Load and preprocess the image
|
| 174 |
+
img = tf.keras.preprocessing.image.load_img(
|
| 175 |
+
image_path, target_size=(img_height, img_width)
|
| 176 |
+
)
|
| 177 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
| 178 |
+
img_array = tf.expand_dims(img_array, 0) / 255.0
|
| 179 |
+
|
| 180 |
+
# Make prediction
|
| 181 |
+
predictions = model.predict(img_array)
|
| 182 |
+
score = tf.nn.softmax(predictions[0])
|
| 183 |
+
|
| 184 |
+
# Get the predicted class and confidence
|
| 185 |
+
predicted_class = np.argmax(score)
|
| 186 |
+
confidence = 100 * np.max(score)
|
| 187 |
+
|
| 188 |
+
# Map class index to soil type name (reverse the class_indices dictionary)
|
| 189 |
+
class_names = {v: k for k, v in class_indices.items()}
|
| 190 |
+
predicted_soil_type = class_names[predicted_class]
|
| 191 |
+
|
| 192 |
+
print(f"This image is classified as: {predicted_soil_type} with {confidence:.2f}% confidence")
|
| 193 |
+
return predicted_soil_type, confidence
|
| 194 |
+
|
| 195 |
+
# Run the model creation and training
|
| 196 |
+
if __name__ == "__main__":
|
| 197 |
+
model, class_indices = create_soil_type_classifier()
|
| 198 |
+
|
| 199 |
+
# Example of how to use the model for prediction
|
| 200 |
+
# (you would replace this with your own test image)
|
| 201 |
+
test_image = input("Enter path to a test image: ")
|
| 202 |
+
if os.path.exists(test_image):
|
| 203 |
+
predict_soil_type(model, test_image, class_indices)
|
| 204 |
+
else:
|
| 205 |
+
print("Invalid image path. You can test the model later with a valid image.")
|