Fix Hardcoded steps_per_epoch=512 to Dynamic Calculation #391 #409
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Original Problem:
batch_sizeparameter had no effect on iteration countRoot Cause:
The
train()function inkeras_segmentation/train.pyhad hardcoded parameters:def train(..., steps_per_epoch=512, val_steps_per_epoch=512, ...):This meant regardless of dataset size, training would always attempt 512 steps per epoch.
Solution Implementation
Core Fix: Dynamic steps_per_epoch Calculation
Modified
keras_segmentation/train.py:Nonevalues (dynamic calculation):def train(..., steps_per_epoch=None, val_steps_per_epoch=None, ...):2. Added dynamic calculation logic before model.fit():
Calculate steps_per_epoch dynamically if not provided
if steps_per_epoch is None:
from .data_utils.data_loader import get_pairs_from_paths
img_seg_pairs = get_pairs_from_paths(train_images, train_annotations)
total_train_samples = len(img_seg_pairs)
steps_per_epoch = total_train_samples // batch_size
if steps_per_epoch == 0:
steps_per_epoch = 1 # Minimum 1 step per epoch
print(f"Calculated steps_per_epoch: {steps_per_epoch} (from {total_train_samples} samples, batch_size={batch_size})")
Calculate val_steps_per_epoch dynamically if validation enabled
if validate and val_steps_per_epoch is None:
from .data_utils.data_loader import get_pairs_from_paths
val_img_seg_pairs = get_pairs_from_paths(val_images, val_annotations)
total_val_samples = len(val_img_seg_pairs)
val_steps_per_epoch = total_val_samples // val_batch_size
if val_steps_per_epoch == 0:
val_steps_per_epoch = 1 # Minimum 1 step per epoch
print(f"Calculated val_steps_per_epoch: {val_steps_per_epoch} (from {total_val_samples} samples, val_batch_size={val_batch_size})")3. Updated CLI interface (
keras_segmentation/cli_interface.py):parser.add_argument("--steps_per_epoch", type=int, default=None) # Changed from 512### 🧪 Comprehensive Testing Suite
Added
test/unit/test_steps_per_epoch.pywith 5 comprehensive tests: