Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions results/config_snapshot_20250718_001955.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Config:
# Models to train
MODELS_TO_TRAIN = ["MobileNetV2"]

# Data parameters
IMG_SIZE = (224, 224)
BATCH_SIZE = 32
TRAIN_SPLIT = 0.80
VALIDATION_SPLIT = 0.25

# Training parameters
INITIAL_EPOCHS = 20
FINE_TUNE_EPOCHS = 10
INITIAL_LEARNING_RATE = 0.0005
FINE_TUNE_LEARNING_RATE = 0.0001

# Model parameters
NUM_CLASSES = 23
DROPOUT_RATE = 0.5
NUM_FROZEN_LAYERS = 2 # Number of layers to freeze in feature extraction

# Enhanced Architecture Features (set to True to enable)
USE_ENHANCED_ARCHITECTURE = False # Multi-branch architecture with attention
USE_PROGRESSIVE_UNFREEZING = False # Progressive unfreezing strategy
USE_ADVANCED_AUGMENTATION = False # Advanced data augmentation
USE_ENSEMBLE_LEARNING = False # Ensemble learning
USE_TEST_TIME_AUGMENTATION = False # Test-time augmentation

# Progressive unfreezing parameters
PROGRESSIVE_PHASES = 3 # Number of progressive unfreezing phases

# Ensemble learning parameters
ENSEMBLE_STRATEGY = 'soft_voting' # 'soft_voting', 'hard_voting', 'weighted_voting', 'stacking'
ENSEMBLE_MODELS = ["MobileNetV2", "MobileNetV3Large", "MobileNetV3Small"]
META_LEARNER_EPOCHS = 50

# Test-time augmentation parameters
TTA_AUGMENTATIONS = 5 # Number of augmentations per test image

# Advanced training parameters
USE_MIXUP = False # Mixup data augmentation
USE_CUTMIX = False # CutMix data augmentation
USE_LABEL_SMOOTHING = False # Label smoothing
LABEL_SMOOTHING_FACTOR = 0.1

# Learning rate scheduling
USE_COSINE_ANNEALING = False # Cosine annealing scheduler
USE_WARM_RESTARTS = False # Warm restarts

# Regularization
USE_DROPOUT_SCHEDULING = False # Adaptive dropout scheduling
USE_WEIGHT_DECAY = False # L2 weight decay
WEIGHT_DECAY_FACTOR = 1e-4

# Paths
DATA_DIR = "data"
MODEL_SAVE_DIR = "saved_models"
RESULTS_DIR = "results"
2 changes: 2 additions & 0 deletions results/model_comparison_20250718_001955.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,Accuracy (%),Loss,Size (MB),Parameters
MobileNetV2,49.59,1.6897,9.3222,2287447
Binary file added results/training_comparison_20250718_001955.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions results/training_history_20250718_001955.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
model,epoch,train_accuracy,val_accuracy,train_loss,val_loss
MobileNetV2_feature_extraction,1,0.10285714268684387,0.1804979294538498,3.2679028511047363,2.7532925605773926
MobileNetV2_feature_extraction,2,0.22207792103290558,0.28215768933296204,2.6492979526519775,2.5107262134552
MobileNetV2_feature_extraction,3,0.32181817293167114,0.3381742835044861,2.3156282901763916,2.323220729827881
MobileNetV2_feature_extraction,4,0.37584415078163147,0.3838174343109131,2.1061758995056152,2.228482484817505
MobileNetV2_feature_extraction,5,0.4171428680419922,0.3921161890029907,1.9495772123336792,2.1282544136047363
MobileNetV2_feature_extraction,6,0.4527272582054138,0.4066390097141266,1.840901255607605,2.0412991046905518
MobileNetV2_feature_extraction,7,0.47220778465270996,0.4273858964443207,1.7568695545196533,1.970870852470398
MobileNetV2_feature_extraction,8,0.5070129632949829,0.4585062265396118,1.6532152891159058,1.9253556728363037
MobileNetV2_feature_extraction,9,0.5210389494895935,0.46265560388565063,1.5946297645568848,1.8834987878799438
MobileNetV2_feature_extraction,10,0.5425974130630493,0.47095435857772827,1.5383957624435425,1.8493201732635498
MobileNetV2_feature_extraction,11,0.5488311648368835,0.46265560388565063,1.49072265625,1.7981990575790405
MobileNetV2_feature_extraction,12,0.5703896284103394,0.4605809152126312,1.4195979833602905,1.7736471891403198
MobileNetV2_feature_extraction,13,0.5807791948318481,0.4564315378665924,1.4124072790145874,1.7660542726516724
MobileNetV2_feature_extraction,14,0.5774025917053223,0.46887966990470886,1.3677922487258911,1.7547590732574463
MobileNetV2_feature_extraction,15,0.6015584468841553,0.46680498123168945,1.3222650289535522,1.734666347503662
MobileNetV2_feature_extraction,16,0.6051948070526123,0.46473029255867004,1.3051408529281616,1.7301479578018188
MobileNetV2_feature_extraction,17,0.6038960814476013,0.4730290472507477,1.2859197854995728,1.7186366319656372
MobileNetV2_feature_extraction,18,0.6283116936683655,0.4792531132698059,1.2456966638565063,1.712315559387207
MobileNetV2_feature_extraction,19,0.6168830990791321,0.4792531132698059,1.2730422019958496,1.7302953004837036
MobileNetV2_feature_extraction,20,0.621558427810669,0.4792531132698059,1.2226208448410034,1.705318808555603
MobileNetV2_fine_tuned,1,0.635064959526062,0.48547717928886414,1.1813465356826782,1.7019144296646118
MobileNetV2_fine_tuned,2,0.6475324630737305,0.48755186796188354,1.172446846961975,1.6987230777740479
MobileNetV2_fine_tuned,3,0.6496104001998901,0.4834024906158447,1.1667516231536865,1.7070080041885376
MobileNetV2_fine_tuned,4,0.6407791972160339,0.4958506226539612,1.1737589836120605,1.6940451860427856
MobileNetV2_fine_tuned,5,0.6376623511314392,0.48962655663490295,1.1516849994659424,1.7008932828903198
MobileNetV2_fine_tuned,6,0.6342856884002686,0.4937759339809418,1.1716210842132568,1.6919862031936646
MobileNetV2_fine_tuned,7,0.6249350905418396,0.4958506226539612,1.1754311323165894,1.6897497177124023
MobileNetV2_fine_tuned,8,0.649350643157959,0.4958506226539612,1.1520062685012817,1.701900601387024
MobileNetV2_fine_tuned,9,0.6594805121421814,0.49170124530792236,1.1290615797042847,1.6990609169006348
MobileNetV2_fine_tuned,10,0.6503896117210388,0.4813278019428253,1.1408472061157227,1.6991528272628784
33 changes: 33 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ class Config:
DROPOUT_RATE = 0.5
NUM_FROZEN_LAYERS = 2 # Number of layers to freeze in feature extraction

# Enhanced Architecture Features (set to True to enable)
USE_ENHANCED_ARCHITECTURE = False # Multi-branch architecture with attention
USE_PROGRESSIVE_UNFREEZING = False # Progressive unfreezing strategy
USE_ADVANCED_AUGMENTATION = False # Advanced data augmentation
USE_ENSEMBLE_LEARNING = False # Ensemble learning
USE_TEST_TIME_AUGMENTATION = False # Test-time augmentation

# Progressive unfreezing parameters
PROGRESSIVE_PHASES = 3 # Number of progressive unfreezing phases

# Ensemble learning parameters
ENSEMBLE_STRATEGY = 'soft_voting' # 'soft_voting', 'hard_voting', 'weighted_voting', 'stacking'
ENSEMBLE_MODELS = ["MobileNetV2", "MobileNetV3Large", "MobileNetV3Small"]
META_LEARNER_EPOCHS = 50

# Test-time augmentation parameters
TTA_AUGMENTATIONS = 5 # Number of augmentations per test image

# Advanced training parameters
USE_MIXUP = False # Mixup data augmentation
USE_CUTMIX = False # CutMix data augmentation
USE_LABEL_SMOOTHING = False # Label smoothing
LABEL_SMOOTHING_FACTOR = 0.1

# Learning rate scheduling
USE_COSINE_ANNEALING = False # Cosine annealing scheduler
USE_WARM_RESTARTS = False # Warm restarts

# Regularization
USE_DROPOUT_SCHEDULING = False # Adaptive dropout scheduling
USE_WEIGHT_DECAY = False # L2 weight decay
WEIGHT_DECAY_FACTOR = 1e-4

# Paths
DATA_DIR = "data"
MODEL_SAVE_DIR = "saved_models"
Expand Down
67 changes: 61 additions & 6 deletions src/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ def create_data_generators(self, train_dir, validation_dir=None):
If validation_dir is not provided, split train_dir using validation_split.
"""

# Data augmentation pipeline
data_augmentation = tf.keras.Sequential([
layers.RandomFlip("horizontal"),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
])
# Choose augmentation strategy based on config
if hasattr(self.config, 'USE_ADVANCED_AUGMENTATION') and self.config.USE_ADVANCED_AUGMENTATION:
data_augmentation = self._create_advanced_augmentation()
else:
data_augmentation = self._create_basic_augmentation()

if validation_dir:
train_ds = tf.keras.utils.image_dataset_from_directory(
Expand Down Expand Up @@ -75,6 +74,62 @@ def create_data_generators(self, train_dir, validation_dir=None):

return train_ds, val_ds

def _create_basic_augmentation(self):
"""Create basic data augmentation pipeline."""
return tf.keras.Sequential([
layers.RandomFlip("horizontal"),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
])

def _create_advanced_augmentation(self):
"""Create advanced data augmentation pipeline with more sophisticated techniques."""
return tf.keras.Sequential([
layers.RandomFlip("horizontal"),
layers.RandomRotation(0.2),
layers.RandomZoom(0.15),
layers.RandomContrast(0.2),
layers.RandomBrightness(0.2),
layers.RandomTranslation(0.1, 0.1),
# Custom augmentation for better leaf/tree feature learning
layers.Lambda(lambda x: tf.image.random_hue(x, 0.1)),
layers.Lambda(lambda x: tf.image.random_saturation(x, 0.8, 1.2)),
layers.Lambda(lambda x: tf.image.random_jpeg_quality(x, 85, 100)),
])

def create_test_time_augmentation_dataset(self, test_ds, n_augmentations=5):
"""
Create test-time augmentation dataset for improved inference accuracy.
"""
augmentation = tf.keras.Sequential([
layers.RandomFlip("horizontal"),
layers.RandomRotation(0.05),
layers.RandomZoom(0.05),
layers.RandomBrightness(0.1),
])

def augment_batch(x, y):
# Create multiple augmented versions of each image
augmented_images = []
for _ in range(n_augmentations):
aug_x = augmentation(x)
augmented_images.append(aug_x)

# Stack all augmented versions
stacked = tf.stack(augmented_images, axis=1) # Shape: (batch, n_aug, height, width, channels)

# Reshape to treat each augmentation as a separate sample
batch_size = tf.shape(x)[0]
reshaped = tf.reshape(stacked, (-1, *x.shape[1:]))

# Repeat labels for each augmentation
repeated_labels = tf.repeat(y, n_augmentations, axis=0)

return reshaped, repeated_labels

tta_ds = test_ds.map(augment_batch)
return tta_ds, n_augmentations

def prepare_dataset_from_directory(self, data_dir):
"""
Prepare dataset from a directory structure with subdirectories for each class.
Expand Down
128 changes: 128 additions & 0 deletions src/enhanced_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
class EnhancedConfig:
"""
Enhanced configuration demonstrating all the new architectural improvements.
This configuration enables all advanced features for maximum performance.
"""

# Models to train
MODELS_TO_TRAIN = ["MobileNetV2"]

# Data parameters
IMG_SIZE = (224, 224)
BATCH_SIZE = 32
TRAIN_SPLIT = 0.80
VALIDATION_SPLIT = 0.25

# Training parameters
INITIAL_EPOCHS = 25
FINE_TUNE_EPOCHS = 15
INITIAL_LEARNING_RATE = 0.0005
FINE_TUNE_LEARNING_RATE = 0.0001

# Model parameters
NUM_CLASSES = 23
DROPOUT_RATE = 0.5
NUM_FROZEN_LAYERS = 2

# Enhanced Architecture Features - ALL ENABLED
USE_ENHANCED_ARCHITECTURE = True # Multi-branch architecture with attention
USE_PROGRESSIVE_UNFREEZING = True # Progressive unfreezing strategy
USE_ADVANCED_AUGMENTATION = True # Advanced data augmentation
USE_ENSEMBLE_LEARNING = True # Ensemble learning
USE_TEST_TIME_AUGMENTATION = True # Test-time augmentation

# Progressive unfreezing parameters
PROGRESSIVE_PHASES = 3

# Ensemble learning parameters
ENSEMBLE_STRATEGY = 'stacking' # Use stacking for best performance
ENSEMBLE_MODELS = ["MobileNetV2", "MobileNetV3Large", "MobileNetV3Small"]
META_LEARNER_EPOCHS = 50

# Test-time augmentation parameters
TTA_AUGMENTATIONS = 7 # More augmentations for better accuracy

# Advanced training parameters
USE_MIXUP = True
USE_CUTMIX = True
USE_LABEL_SMOOTHING = True
LABEL_SMOOTHING_FACTOR = 0.1

# Learning rate scheduling
USE_COSINE_ANNEALING = True
USE_WARM_RESTARTS = True

# Regularization
USE_DROPOUT_SCHEDULING = True
USE_WEIGHT_DECAY = True
WEIGHT_DECAY_FACTOR = 1e-4

# Paths
DATA_DIR = "data"
MODEL_SAVE_DIR = "saved_models"
RESULTS_DIR = "results"


class ConservativeConfig:
"""
Conservative configuration that enables only the most proven improvements.
Good for users who want better performance without experimental features.
"""

# Models to train
MODELS_TO_TRAIN = ["MobileNetV2"]

# Data parameters
IMG_SIZE = (224, 224)
BATCH_SIZE = 32
TRAIN_SPLIT = 0.80
VALIDATION_SPLIT = 0.25

# Training parameters
INITIAL_EPOCHS = 20
FINE_TUNE_EPOCHS = 10
INITIAL_LEARNING_RATE = 0.0005
FINE_TUNE_LEARNING_RATE = 0.0001

# Model parameters
NUM_CLASSES = 23
DROPOUT_RATE = 0.5
NUM_FROZEN_LAYERS = 2

# Enhanced Architecture Features - CONSERVATIVE SELECTION
USE_ENHANCED_ARCHITECTURE = False # Keep original architecture
USE_PROGRESSIVE_UNFREEZING = True # Proven to work well
USE_ADVANCED_AUGMENTATION = True # Almost always beneficial
USE_ENSEMBLE_LEARNING = False # Keep it simple
USE_TEST_TIME_AUGMENTATION = True # Low risk, good reward

# Progressive unfreezing parameters
PROGRESSIVE_PHASES = 3

# Ensemble learning parameters (not used)
ENSEMBLE_STRATEGY = 'soft_voting'
ENSEMBLE_MODELS = ["MobileNetV2"]
META_LEARNER_EPOCHS = 50

# Test-time augmentation parameters
TTA_AUGMENTATIONS = 5

# Advanced training parameters
USE_MIXUP = False
USE_CUTMIX = False
USE_LABEL_SMOOTHING = True # Usually beneficial
LABEL_SMOOTHING_FACTOR = 0.1

# Learning rate scheduling
USE_COSINE_ANNEALING = False
USE_WARM_RESTARTS = False

# Regularization
USE_DROPOUT_SCHEDULING = False
USE_WEIGHT_DECAY = True # Standard regularization
WEIGHT_DECAY_FACTOR = 1e-4

# Paths
DATA_DIR = "data"
MODEL_SAVE_DIR = "saved_models"
RESULTS_DIR = "results"
Loading