-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
343 lines (293 loc) · 11.8 KB
/
Copy pathtrain_model.py
File metadata and controls
343 lines (293 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
"""
train_model.py — Train a defect detection model using MobileNetV2.
This script:
1. Loads labeled images from dataset/train/ and dataset/test/
2. Fine-tunes a MobileNetV2 model (pre-trained on ImageNet)
3. Converts the model to TensorFlow Lite format for Raspberry Pi
4. Saves the model and a label file
Usage:
python train_model.py
python train_model.py --epochs 20 --batch-size 16
The trained model will be saved to:
models/defect_model.tflite
models/labels.txt
"""
import argparse
import json
import os
import sys
def check_dependencies():
"""Check that required packages are installed."""
missing = []
try:
import tensorflow
except ImportError:
missing.append("tensorflow")
try:
import numpy
except ImportError:
missing.append("numpy")
try:
from PIL import Image
except ImportError:
missing.append("Pillow")
if missing:
print("ERROR: Missing required packages:")
for pkg in missing:
print(f" pip install {pkg}")
print("\nInstall them and try again.")
sys.exit(1)
def count_images(directory):
"""Count images in each subdirectory."""
counts = {}
if not os.path.exists(directory):
return counts
for category in sorted(os.listdir(directory)):
cat_path = os.path.join(directory, category)
if os.path.isdir(cat_path):
n = len([f for f in os.listdir(cat_path)
if f.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp'))])
counts[category] = n
return counts
def main():
parser = argparse.ArgumentParser(description="Train defect detection model")
parser.add_argument("--train-dir", default="dataset/train",
help="Training data directory (default: dataset/train)")
parser.add_argument("--test-dir", default="dataset/test",
help="Test data directory (default: dataset/test)")
parser.add_argument("--model-dir", default="models",
help="Output model directory (default: models)")
parser.add_argument("--epochs", type=int, default=15,
help="Number of training epochs (default: 15)")
parser.add_argument("--batch-size", type=int, default=16,
help="Batch size (default: 16, use 8 on Raspberry Pi)")
parser.add_argument("--image-size", type=int, default=224,
help="Input image size (default: 224)")
parser.add_argument("--learning-rate", type=float, default=0.001,
help="Learning rate (default: 0.001)")
args = parser.parse_args()
check_dependencies()
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
import numpy as np
print("=" * 60)
print(" DEFECT DETECTION MODEL TRAINER")
print("=" * 60)
print(f" TensorFlow version: {tf.__version__}")
print(f" Training directory : {args.train_dir}")
print(f" Test directory : {args.test_dir}")
print(f" Image size : {args.image_size}x{args.image_size}")
print(f" Epochs : {args.epochs}")
print(f" Batch size : {args.batch_size}")
print()
# --- Check Dataset ---
print("Checking dataset...")
train_counts = count_images(args.train_dir)
test_counts = count_images(args.test_dir)
if not train_counts:
print(f"\nERROR: No image folders found in '{args.train_dir}'")
print("Please add images to category folders like:")
print(f" {args.train_dir}/good/")
print(f" {args.train_dir}/scratch/")
print(f" {args.train_dir}/dent/")
print("\nUse capture_training.py to collect images.")
sys.exit(1)
print("\nTraining data:")
total_train = 0
for cat, count in train_counts.items():
status = "OK" if count >= 20 else "LOW"
print(f" {cat:12s}: {count:4d} images [{status}]")
total_train += count
if test_counts:
print("\nTest data:")
total_test = 0
for cat, count in test_counts.items():
print(f" {cat:12s}: {count:4d} images")
total_test += count
else:
print("\nNote: No test data found. Will split training data 80/20.")
if total_train < 30:
print(f"\nWARNING: Only {total_train} total training images.")
print("For decent results, aim for 50+ images per category.")
proceed = input("Continue anyway? (y/n): ")
if proceed.lower() != 'y':
sys.exit(0)
# --- Data Generators ---
print("\nPreparing data...")
# Data augmentation for training (helps model generalize with small datasets)
train_datagen = ImageDataGenerator(
rescale=1.0 / 255, # Normalize pixel values to 0-1
rotation_range=20, # Random rotation up to 20 degrees
width_shift_range=0.1, # Random horizontal shift
height_shift_range=0.1, # Random vertical shift
shear_range=0.1, # Random shearing
zoom_range=0.1, # Random zoom
horizontal_flip=True, # Random horizontal flip
brightness_range=[0.8, 1.2], # Random brightness
fill_mode='nearest',
validation_split=0.2 if not test_counts else 0.0 # Split if no test data
)
test_datagen = ImageDataGenerator(rescale=1.0 / 255)
# Load training data
train_generator = train_datagen.flow_from_directory(
args.train_dir,
target_size=(args.image_size, args.image_size),
batch_size=args.batch_size,
class_mode='categorical',
subset='training' if not test_counts else None,
shuffle=True
)
# Load validation data
if test_counts:
val_generator = test_datagen.flow_from_directory(
args.test_dir,
target_size=(args.image_size, args.image_size),
batch_size=args.batch_size,
class_mode='categorical',
shuffle=False
)
else:
val_generator = train_datagen.flow_from_directory(
args.train_dir,
target_size=(args.image_size, args.image_size),
batch_size=args.batch_size,
class_mode='categorical',
subset='validation',
shuffle=False
)
num_classes = train_generator.num_classes
class_names = list(train_generator.class_indices.keys())
print(f"\nDetected {num_classes} defect categories: {class_names}")
print(f"Training samples : {train_generator.samples}")
print(f"Validation samples: {val_generator.samples}")
# --- Build Model ---
print("\nBuilding model (MobileNetV2 + custom head)...")
# Load MobileNetV2 pre-trained on ImageNet (without top classification layer)
base_model = MobileNetV2(
input_shape=(args.image_size, args.image_size, 3),
include_top=False,
weights='imagenet'
)
# Freeze the base model layers (use pre-trained features)
base_model.trainable = False
# Add custom classification layers on top
x = base_model.output
x = GlobalAveragePooling2D()(x) # Convert feature maps to single vector
x = Dense(128, activation='relu')(x) # Hidden layer
x = Dropout(0.3)(x) # Prevent overfitting
x = Dense(64, activation='relu')(x) # Another hidden layer
x = Dropout(0.2)(x)
predictions = Dense(num_classes, activation='softmax')(x) # Output layer
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(
optimizer=Adam(learning_rate=args.learning_rate),
loss='categorical_crossentropy',
metrics=['accuracy']
)
print(f"Model parameters: {model.count_params():,}")
print(f" Trainable : {sum(p.numpy().size for p in model.trainable_weights):,}")
print(f" Non-trainable : {sum(p.numpy().size for p in model.non_trainable_weights):,}")
# --- Train ---
print(f"\nTraining for {args.epochs} epochs...")
print("-" * 40)
callbacks = [
EarlyStopping(
monitor='val_loss',
patience=5,
restore_best_weights=True,
verbose=1
),
ReduceLROnPlateau(
monitor='val_loss',
factor=0.5,
patience=3,
verbose=1
)
]
history = model.fit(
train_generator,
epochs=args.epochs,
validation_data=val_generator,
callbacks=callbacks,
verbose=1
)
# --- Evaluate ---
print("\n" + "=" * 40)
val_loss, val_acc = model.evaluate(val_generator, verbose=0)
print(f"Final Validation Accuracy: {val_acc * 100:.1f}%")
print(f"Final Validation Loss : {val_loss:.4f}")
if val_acc < 0.6:
print("\nWARNING: Accuracy is below 60%. Consider:")
print(" - Adding more training images (200+ per category)")
print(" - Improving lighting consistency")
print(" - Increasing epochs (--epochs 30)")
elif val_acc < 0.8:
print("\nAccuracy is decent but could improve with more data.")
else:
print("\nGood accuracy! Model is ready for use.")
# --- Fine-tune (optional second pass) ---
print("\nFine-tuning: Unfreezing top layers of base model...")
base_model.trainable = True
# Only fine-tune the last 30 layers
for layer in base_model.layers[:-30]:
layer.trainable = False
model.compile(
optimizer=Adam(learning_rate=args.learning_rate * 0.1), # Lower learning rate
loss='categorical_crossentropy',
metrics=['accuracy']
)
history_fine = model.fit(
train_generator,
epochs=10,
validation_data=val_generator,
callbacks=callbacks,
verbose=1
)
val_loss, val_acc = model.evaluate(val_generator, verbose=0)
print(f"\nAfter fine-tuning — Accuracy: {val_acc * 100:.1f}%")
# --- Save Model ---
os.makedirs(args.model_dir, exist_ok=True)
# Save as TensorFlow Lite model (optimized for Raspberry Pi)
print("\nConverting to TensorFlow Lite format...")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT] # Quantize for speed
tflite_model = converter.convert()
tflite_path = os.path.join(args.model_dir, "defect_model.tflite")
with open(tflite_path, 'wb') as f:
f.write(tflite_model)
model_size_mb = os.path.getsize(tflite_path) / (1024 * 1024)
print(f"Model saved: {tflite_path} ({model_size_mb:.1f} MB)")
# Save labels
labels_path = os.path.join(args.model_dir, "labels.txt")
with open(labels_path, 'w') as f:
for name in class_names:
f.write(f"{name}\n")
print(f"Labels saved: {labels_path}")
# Save training config
config = {
"image_size": args.image_size,
"class_names": class_names,
"num_classes": num_classes,
"final_accuracy": float(val_acc),
"epochs_trained": len(history.history['loss']) + len(history_fine.history['loss']),
"training_samples": train_generator.samples,
}
config_path = os.path.join(args.model_dir, "model_config.json")
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
print(f"Config saved: {config_path}")
print("\n" + "=" * 60)
print(" TRAINING COMPLETE!")
print(f" Model : {tflite_path}")
print(f" Accuracy : {val_acc * 100:.1f}%")
print(f" Classes : {', '.join(class_names)}")
print("=" * 60)
print("\nNext step: Run 'python app.py' to start the detection system!")
if __name__ == "__main__":
main()