Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to seed training. #1085

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ please create an issue or pull request at https://github.com/fizyr/keras-retinan
* Eduardo Ramos
* DiegoAgher
* Alexander Pacha
* Martin Zlocha
16 changes: 15 additions & 1 deletion keras_retinanet/bin/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import sys
import warnings

import random
import keras
import keras.preprocessing.image
import tensorflow as tf
import numpy as np

# Allow relative imports when being executed as script.
if __name__ == "__main__" and __package__ is None:
Expand Down Expand Up @@ -223,6 +225,11 @@ def create_generators(args, preprocess_image):
'preprocess_image' : preprocess_image,
}

prng = None

if args.seed:
prng = np.random.RandomState(args.seed)

# create random transform generator for augmenting training data
if args.random_transform:
transform_generator = random_transform_generator(
Expand All @@ -236,6 +243,7 @@ def create_generators(args, preprocess_image):
max_scaling=(1.1, 1.1),
flip_x_chance=0.5,
flip_y_chance=0.5,
prng=prng
)
visual_effect_generator = random_visual_effect_generator(
contrast_range=(0.9, 1.1),
Expand All @@ -244,7 +252,7 @@ def create_generators(args, preprocess_image):
saturation_range=(0.95, 1.05)
)
else:
transform_generator = random_transform_generator(flip_x_chance=0.5)
transform_generator = random_transform_generator(flip_x_chance=0.5, prng=prng)
visual_effect_generator = None

if args.dataset_type == 'coco':
Expand Down Expand Up @@ -429,6 +437,7 @@ def csv_list(string):
parser.add_argument('--config', help='Path to a configuration parameters .ini file.')
parser.add_argument('--weighted-average', help='Compute the mAP using the weighted average of precisions among classes.', action='store_true')
parser.add_argument('--compute-val-loss', help='Compute validation loss during training', dest='compute_val_loss', action='store_true')
parser.add_argument('--seed', help='Seed value to use for training.')

# Fit generator arguments
parser.add_argument('--multiprocessing', help='Use multiprocessing in fit_generator.', action='store_true')
Expand All @@ -444,6 +453,11 @@ def main(args=None):
args = sys.argv[1:]
args = parse_args(args)

if args.seed:
np.random.seed(args.seed)
tf.set_random_seed(args.seed)
random.seed(args.seed)

# create object that stores backbone information
backbone = models.backbone(args.backbone)

Expand Down