def main(): from pprint import pprint config = B2Config() # pprint(vars(config)) # exit() batch_size = 4 num_classes = 15 epochs = 5 steps_per_epoch = 1000 parser = Parser( config=config, batch_size=batch_size, num_classes=num_classes) training_model = efficientdet(config, num_classes, weights=None) # compile model training_model.compile( optimizer=Adam(lr=1e-3), loss={ 'regression': smooth_l1(), 'classification': focal()}) # print(training_model.summary()) # # create the callbacks # callbacks = create_callbacks( # model, # prediction_model, # validation_generator, # args, # ) train_dataset_function = parser.get_dataset(filenames='./DATA/train*.tfrecord') training_model.fit( train_dataset_function, epochs=epochs, steps_per_epoch=steps_per_epoch,) # callbacks=callbacks) os.makedirs("./checkpoints", exist_ok=True) training_model.save("./checkpoints/efficientdetB2_final")
def train(): g = tf.Graph() with g.as_default(): # Load datasets. provider = data_provider.Deblurring() images, deblurred = provider.get('deblurred') # Define model graph. with tf.variable_scope('net'): with slim.arg_scope([slim.batch_norm, slim.layers.dropout], is_training=True): scales = [1, 2, 4] prediction, pyramid = resnet_model.multiscale_deblurring_net( images, scales=scales) # Add a cosine loss to every scale and the combined output. for net, level_name in zip([prediction] + pyramid, ['pred'] + scales): loss = losses.smooth_l1(net, deblurred) slim.losses.add_loss(loss) tf.scalar_summary('losses/loss at {}'.format(level_name), loss) total_loss = slim.losses.get_total_loss() tf.scalar_summary('losses/total loss', total_loss) tf.image_summary('blurred', images) tf.image_summary('deblurred', deblurred) tf.image_summary('pred', prediction) optimizer = tf.train.AdamOptimizer(FLAGS.initial_learning_rate) with tf.Session(graph=g) as sess: if FLAGS.pretrained_resnet_checkpoint_path: restore_resnet(sess, FLAGS.pretrained_resnet_checkpoint_path) if FLAGS.pretrained_model_checkpoint_path: variables_to_restore = slim.get_variables_to_restore() saver = tf.train.Saver(variables_to_restore) saver.restore(sess, FLAGS.pretrained_model_checkpoint_path) train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=True) logging.set_verbosity(1) slim.learning.train(train_op, FLAGS.train_dir, save_summaries_secs=60, save_interval_secs=600)
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # make save path if it doesn't exist if args.save_path is not None and not os.path.exists(args.save_path): os.makedirs(args.save_path) # create the generator generator = create_generator(args) # load the model print('Loading model, this may take a second...') model = keras.models.load_model(args.model, custom_objects=custom_objects) # print model summary print(model.summary()) loss = {'regression': losses.smooth_l1(), 'classification': losses.focal()} # start evaluation average_precisions = evaluate( generator, model, # loss, iou_threshold=args.iou_threshold, score_threshold=args.score_threshold, max_detections=args.max_detections, save_path=args.save_path) # print evaluation for label, average_precision in average_precisions.items(): print(generator.label_to_name(label), '{:.4f}'.format(average_precision)) print('mAP: {:.4f}'.format( sum(average_precisions.values()) / len(average_precisions)))
def __init__(self, backbone): # a dictionary mapping custom layer names to the correct classes import layers import losses import initializers self.custom_objects = { 'UpsampleLike': layers.UpsampleLike, 'PriorProbability': initializers.PriorProbability, 'RegressBoxes': layers.RegressBoxes, 'FilterDetections': layers.FilterDetections, 'Anchors': layers.Anchors, 'ClipBoxes': layers.ClipBoxes, '_smooth_l1': losses.smooth_l1(), '_focal': losses.focal(), } self.backbone = backbone self.validate()
def _create_models(self, backbone_retinanet, num_classes, weights, freeze_backbone=False, lr=1e-5): """ Creates three models (model, training_model, prediction_model). Parameters ---------- backbone_retinanet : A function to call to create a retinanet model with a given backbone. num_classes : The number of classes to train. weights : The weights to load into the model. multi_gpu : The number of GPUs to use for training. freeze_backbone : If True, disables learning for the backbone. config : Config parameters, None indicates the default configuration. Returns ------- model : The base model. training_model : The training model. If multi_gpu=0, this is identical to model. prediction_model : The model wrapped with utility functions to perform object detection (applies regression values and performs NMS). """ modifier = freeze_model if freeze_backbone else None anchor_params = None num_anchors = None model = self._model_with_weights(backbone_retinanet( num_classes, num_anchors=num_anchors, modifier=modifier), weights=weights, skip_mismatch=True) training_model = model prediction_model = retinanet_bbox(model=model, anchor_params=anchor_params) training_model.compile(loss={ 'regression': losses.smooth_l1(), 'classification': losses.focal() }, optimizer=keras.optimizers.adam(lr=lr, clipnorm=0.001)) return model, training_model, prediction_model
def __init__(self, backbone): import custom_layers as layers import losses import initializers self.custom_objects = { 'PriorProbability': initializers.PriorProbability, 'UpsampleLike': layers.UpsampleLike, 'RegressBoxes': layers.RegressBoxes, 'FilterDetections': layers.FilterDetections, 'Anchors': layers.Anchors, 'ClipBoxes': layers.ClipBoxes, # 'BatchNormalization': layers.BatchNormalization, '_smooth_l1': losses.smooth_l1(), '_focal': losses.focal(), } self.backbone = backbone self.validate()
def main(args=None): """ Train an EfficientPose model. Args: args: parseargs object containing configuration for the training procedure. """ allow_gpu_growth_memory() # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # create the generators print("\nCreating the Generators...") train_generator, validation_generator = create_generators(args) print("Done!") num_rotation_parameters = train_generator.get_num_rotation_parameters() num_classes = train_generator.num_classes() num_anchors = train_generator.num_anchors # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu print("\nBuilding the Model...") model, prediction_model, all_layers = build_EfficientPose( args.phi, num_classes=num_classes, num_anchors=num_anchors, freeze_bn=not args.no_freeze_bn, score_threshold=args.score_threshold, num_rotation_parameters=num_rotation_parameters) print("Done!") # load pretrained weights if args.weights: if args.weights == 'imagenet': model_name = 'efficientnet-b{}'.format(args.phi) file_name = '{}_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5'.format( model_name) file_hash = WEIGHTS_HASHES[model_name][1] weights_path = keras.utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path, by_name=True) else: print('Loading model, this may take a second...') custom_load_weights(filepath=args.weights, layers=all_layers, skip_mismatch=True) print("\nDone!") # freeze backbone layers if args.freeze_backbone: # 227, 329, 329, 374, 464, 566, 656 for i in range(1, [227, 329, 329, 374, 464, 566, 656][args.phi]): model.layers[i].trainable = False # compile model model.compile( optimizer=Adam(lr=args.lr, clipnorm=0.001), loss={ 'regression': smooth_l1(), 'classification': focal(), 'transformation': transformation_loss(model_3d_points_np=train_generator. get_all_3d_model_points_array_for_loss(), num_rotation_parameter=num_rotation_parameters) }, loss_weights={ 'regression': 1.0, 'classification': 1.0, 'transformation': 0.02 }) # create the callbacks callbacks = create_callbacks( model, prediction_model, validation_generator, args, ) if not args.compute_val_loss: validation_generator = None elif args.compute_val_loss and validation_generator is None: raise ValueError( 'When you have no validation data, you should not specify --compute-val-loss.' ) # start training return model.fit_generator(generator=train_generator, steps_per_epoch=args.steps, initial_epoch=0, epochs=args.epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=args.multiprocessing, max_queue_size=args.max_queue_size, validation_data=validation_generator)
def create_models(fl_gamma, fl_alpha, r_weight, c_weight, p_weight, train_type, sample_t, backbone_retinanet, num_classes, weights, class_weights, loss_weights, multi_gpu=0, freeze_backbone=False, lr=1e-5, config=None): """ Creates three models (model, training_model, prediction_model). Args backbone_retinanet : A function to call to create a retinanet model with a given backbone. num_classes : The number of classes to train. weights : The weights to load into the model. multi_gpu : The number of GPUs to use for training. freeze_backbone : If True, disables learning for the backbone. config : Config parameters, None indicates the default configuration. Returns model : The base model. This is also the model that is saved in snapshots. training_model : The training model. If multi_gpu=0, this is identical to model. prediction_model : The model wrapped with utility functions to perform object detection (applies regression values and performs NMS). """ modifier = freeze_model if freeze_backbone else None # load anchor parameters, or pass None (so that defaults will be used) anchor_params = None num_anchors = None if config and 'anchor_parameters' in config: anchor_params = parse_anchor_parameters(config) num_anchors = anchor_params.num_anchors() # Keras recommends initialising a multi-gpu model on the CPU to ease weight sharing, and to prevent OOM errors. # optionally wrap in a parallel model if multi_gpu > 1: from keras.utils import multi_gpu_model with tf.device('/cpu:0'): model = model_with_weights(backbone_retinanet( num_classes, num_anchors=num_anchors, modifier=modifier), weights=weights, skip_mismatch=True) training_model = multi_gpu_model(model, gpus=multi_gpu) else: model = model_with_weights(backbone_retinanet(num_classes, train_type, num_anchors=num_anchors, modifier=modifier), weights=weights, skip_mismatch=True) training_model = model # make prediction model prediction_model = retinanet_bbox(model=model, anchor_params=anchor_params) if train_type == "single": # compile model training_model.compile( loss={ 'regression': losses.smooth_l1(r_weight), ## HERE THE INPUT ARGUMENTS CAN BE GIVEN: default focal(alpha=0.25, gamma=2.0) ## gamma: the actual "focusing parameter" 'classification': losses.focal(c_weight, fl_alpha, fl_gamma, class_weights) }, optimizer=keras.optimizers.adam(lr=lr, clipnorm=0.001), #sample_weight_mode="temporal", #sample_weights=sample_t ) elif train_type == "multi": training_model.compile( loss={ 'regression': losses.smooth_l1(r_weight), ## HERE THE INPUT ARGUMENTS CAN BE GIVEN: default focal(alpha=0.25, gamma=2.0) ## gamma: the actual "focusing parameter" 'classification_artefact': losses.focal(c_weight, fl_alpha, fl_gamma), 'classification_polyp': losses.focal2(p_weight, fl_alpha, fl_gamma) }, optimizer=keras.optimizers.adam(lr=lr, clipnorm=0.001) #loss_weights=loss_weights ) return model, training_model, prediction_model
def efficientdet_training(config, LOCAL_ANNOTATIONS_PATH, LOCAL_ROOT_PATH, LOCAL_CLASSES_PATH, LOCAL_VALIDATIONS_PATH, LOCAL_LOGS_PATH, LOCAL_SNAPSHOTS_PATH): (model, prediction_model, train_generator, evaluation_generator, validation_generator, config) = load_efficient_det( config, LOCAL_ANNOTATIONS_PATH, LOCAL_ROOT_PATH, LOCAL_CLASSES_PATH, LOCAL_VALIDATIONS_PATH, LOCAL_LOGS_PATH, LOCAL_SNAPSHOTS_PATH) #model.summary() # create the callbacks if config['validation']: callbacks = create_callbacks(model, prediction_model, evaluation_generator, validation_generator, LOCAL_LOGS_PATH, LOCAL_SNAPSHOTS_PATH, config) else: callbacks = create_callbacks(model, prediction_model, evaluation_generator, train_generator, LOCAL_LOGS_PATH, LOCAL_SNAPSHOTS_PATH, config) model.compile(optimizer=Adam(lr=1e-3), loss={ 'regression': smooth_l1(), 'classification': focal(gamma=config['focal_gamma']) }) os.makedirs(LOCAL_LOGS_PATH, exist_ok=True) with open(os.path.join(LOCAL_LOGS_PATH, 'config.yaml'), 'w') as file: yaml.dump(config, file) with open(LOCAL_LOGS_PATH + '/stdout.txt', 'w') as f: with redirect_stdout(f): print('Path : ', LOCAL_ROOT_PATH) start = datetime.now() print(f'Started training at: {start}') if not validation_generator: results = model.fit_generator( generator=train_generator, steps_per_epoch=config['steps_per_epoch'], initial_epoch=0, epochs=config['nb_epoch'], verbose=1, callbacks=callbacks, workers=config['workers'], use_multiprocessing=config['multiprocessing'], max_queue_size=config['max_queue_size']) else: results = model.fit_generator( generator=train_generator, steps_per_epoch=config['steps_per_epoch'], initial_epoch=0, epochs=config['nb_epoch'], verbose=1, callbacks=callbacks, workers=config['workers'], use_multiprocessing=config['multiprocessing'], max_queue_size=config['max_queue_size'], validation_data=validation_generator) end = datetime.now() print(f'Completed training at: {end}') print(f'Total training time: {diff(start, end)}')
def train(): g = tf.Graph() with g.as_default(): # Load datasets. names = ['SyntheticDataset', 'Dataset300W'] provider = data_provider.DatasetMixer(names, batch_size=FLAGS.batch_size, densities=(1, 60)) images, uvs, masks = provider.get() images = tf.image.resize_images(images, (200, 200), method=0) uvs = tf.image.resize_images(uvs, (200, 200), method=1) masks = tf.image.resize_images(masks, (200, 200), method=1) # Define model graph. with slim.arg_scope([slim.batch_norm, slim.layers.dropout], is_training=True): prediction, logits = network.hourglass(images) loss = losses.smooth_l1(prediction, uvs) tf.summary.scalar('smooth l1 loss', loss) k = FLAGS.quantization_step n_classes = k + 1 for i, name in enumerate(['hor', 'ver']): gt = tf.to_int64(tf.floor(uvs[..., i] * k)) gt = tf.reshape(gt, [-1]) gt = slim.one_hot_encoding(gt, n_classes) class_loss = tf.contrib.losses.softmax_cross_entropy( tf.reshape(logits[..., i * n_classes:(i + 1) * n_classes], [-1, n_classes]), gt) tf.summary.scalar('losses/classification loss [{}]'.format(name), class_loss) total_loss = tf.losses.get_total_loss() tf.summary.scalar('losses/total loss', total_loss) tf.summary.image('images', images) tf.summary.image('uvs/h', uvs[..., :1]) tf.summary.image('uvs/v', uvs[..., 1:]) tf.summary.image('predictions/h', prediction[..., :1]) tf.summary.image('predictions/v', prediction[..., 1:]) optimizer = tf.train.AdamOptimizer(FLAGS.initial_learning_rate) config = tf.ConfigProto(inter_op_parallelism_threads=2) with tf.Session(graph=g, config=config) as sess: init_fn = None if FLAGS.pretrained_model_checkpoint_path: print('Loading pretrained model...') variables_to_restore = slim.get_model_variables() init_fn = slim.assign_from_checkpoint_fn( FLAGS.pretrained_model_checkpoint_path, variables_to_restore, ignore_missing_vars=True) train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=True) logging.set_verbosity(1) slim.learning.train(train_op, FLAGS.train_dir, save_summaries_secs=60, init_fn=init_fn, save_interval_secs=600)
import layers import losses import utils.customized_optimizer import numpy as np """ A dictionary mapping custom layer names to the correct classes. """ custom_objects = { 'UpsampleLike': layers.UpsampleLike, 'PriorProbability': initializers.PriorProbability, 'RegressBoxes': layers.RegressBoxes, 'FilterDetections': layers.FilterDetections, 'Anchors': layers.Anchors, 'ClipBoxes': layers.ClipBoxes, '_smooth_l1': losses.smooth_l1(), '_focal': losses.focal(), 'AdamWithLRMult': utils.customized_optimizer.AdamWithLRMult } def default_classification_model(num_classes, num_anchors, pyramid_feature_size=256, prior_probability=0.01, classification_feature_size=256, name='classification_submodel'): """ Creates the default regression submodel. Args num_classes : Number of classes to predict a score for at each feature level.
def main(): backbone = models.backbone('resnet50') # create the generators #train_generator, validation_generator = create_generators(args, backbone.preprocess_image) random_transform = True val_annotations = './data/processed/val.csv' annotations = './data/processed/train.csv' classes = './data/processed/classes.csv' common_args = { 'batch_size': 8, 'image_min_side': 224, 'image_max_side': 1333, 'preprocess_image': backbone.preprocess_image, } # create random transform generator for augmenting training data if random_transform: transform_generator = random_transform_generator( min_rotation=-0.05, max_rotation=0.05, min_translation=(-0.1, -0.1), max_translation=(0.1, 0.1), #min_shear=-0.1, #max_shear=0.1, min_scaling=(0.8, 0.8), max_scaling=(1.2, 1.2), flip_x_chance=0.5, #flip_y_chance=0.5, ) else: transform_generator = random_transform_generator(flip_x_chance=0.5) train_generator = CSVGenerator(annotations, classes, transform_generator=transform_generator, **common_args) if val_annotations: validation_generator = CSVGenerator(val_annotations, classes, **common_args) else: validation_generator = None #train_generator, validation_generator = create_generators(args, backbone.preprocess_image) num_classes = 1 # change model = backbone.retinanet(num_classes, backbone='resnet50') training_model = model # prediction_model = retinanet_bbox(model=model) nms = True class_specific_filter = True name = 'retinanet-bbox' anchor_params = AnchorParameters.default # compute the anchors features = [ model.get_layer(p_name).output for p_name in ['P3', 'P4', 'P5', 'P6', 'P7'] ] anchor = [ layers.Anchors(size=anchor_params.sizes[i], stride=anchor_params.strides[i], ratios=anchor_params.ratios, scales=anchor_params.scales, name='anchors_{}'.format(i))(f) for i, f in enumerate(features) ] anchors = keras.layers.Concatenate(axis=1, name='anchors')(anchor) # we expect the anchors, regression and classification values as first output regression = model.outputs[0] # check classification = model.outputs[1] # "other" can be any additional output from custom submodels, by default this will be [] other = model.outputs[2:] # apply predicted regression to anchors boxes = layers.RegressBoxes(name='boxes')([anchors, regression]) boxes = layers.ClipBoxes(name='clipped_boxes')([model.inputs[0], boxes]) # filter detections (apply NMS / score threshold / select top-k) detections = layers.FilterDetections( nms=nms, class_specific_filter=class_specific_filter, name='filtered_detections')([boxes, classification] + other) outputs = detections # construct the model prediction_model = keras.models.Model(inputs=model.inputs, outputs=outputs, name=name) # end of prediction_model = retinanet_bbox(model=model) # compile model training_model.compile(loss={ 'regression': losses.smooth_l1(), 'classification': losses.focal() }, optimizer=keras.optimizers.SGD(lr=1e-2, momentum=0.9, decay=.0001, nesterov=True, clipnorm=1) # , clipnorm=0.001) ) print(model.summary()) # start of create_callbacks #callbacks = create_callbacks(model,training_model,prediction_model,validation_generator,args,) callbacks = [] tensorboard_callback = None tensorboard_callback = keras.callbacks.TensorBoard( log_dir='', histogram_freq=0, batch_size=8, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None) callbacks.append(tensorboard_callback) evaluation = Evaluate(validation_generator, tensorboard=tensorboard_callback, weighted_average=False) evaluation = RedirectModel(evaluation, prediction_model) callbacks.append(evaluation) makedirs('./snapshots/') checkpoint = keras.callbacks.ModelCheckpoint(os.path.join( './snapshots/', '{backbone}_{dataset_type}_{{epoch:02d}}.h5'.format( backbone='resnet50', dataset_type='csv')), verbose=1, save_best_only=False, monitor="mAP", mode='max') checkpoint = RedirectModel(checkpoint, model) callbacks.append(checkpoint) callbacks.append( keras.callbacks.ReduceLROnPlateau(monitor='loss', factor=0.9, patience=4, verbose=1, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)) steps = 2500 epochs = 25 # start training history = training_model.fit( generator=train_generator, steps_per_epoch=steps, epochs=epochs, verbose=1, callbacks=callbacks, ) timestr = time.strftime("%Y-%m-%d-%H%M") history_path = os.path.join( './snapshots/', '{timestr}_{backbone}.csv'.format(timestr=timestr, backbone='resnet50', dataset_type='csv')) pd.DataFrame(history.history).to_csv(history_path)
def train(): g = tf.Graph() with g.as_default(): # Load datasets. provider = data_provider.HumanPose() images, ground_truth, keypoints = provider.get('pose', 'keypoints') # TODO: Current code assumes batch_size=1. # The states Tensor must be of dimensions # (batch_size, num_states, height, width, num_parts) # Define model graph. with tf.variable_scope('net'): with slim.arg_scope([slim.batch_norm, slim.layers.dropout], is_training=True): states, results = svs_model.svs_regression_net_light( images, num_iterations=FLAGS.num_iterations) # States currently is # (num_states, batch_size, height, width, num_parts) # Add a smooth l1 loss to every scale and the combined output. for i, state in enumerate(states): gt = ground_truth[:, i, :, :, :] # TODO: Move 100 to a flag. # Reweighting the loss by 100. If we do not do this # The loss becomes extremely small. ones = tf.ones_like(gt) weights = tf.select(gt < .1, ones, ones * 100) # The non-visible parts have a substracted value of a 100. weights = tf.select(gt < 0, tf.zeros_like(gt), weights) loss = losses.smooth_l1(state, gt, weights) tf.scalar_summary('svs_losses/iteration_{}'.format(i), loss) # Add a smooth l1 loss to every scale and the combined output. for i, state in enumerate(results): gt = keypoints # TODO: Move 100 to a flag. # Reweighting the loss by 100. If we do not do this # The loss becomes extremely small. ones = tf.ones_like(gt) weights = tf.select(gt < .1, ones * 10, ones * 100) # The non-visible parts have a substracted value of a 100. weights = tf.select(gt < 0, tf.zeros_like(gt), weights) loss = losses.smooth_l1(state, gt, weights) tf.scalar_summary('kpt_losses/iteration_{}'.format(i), loss) total_loss = slim.losses.get_total_loss() tf.scalar_summary('losses/total loss', total_loss) for i in range(len(states)): tf.image_summary('state/it_{}'.format(i), tf.reduce_sum(states[i], -1)[..., None]) tf.image_summary( 'gt/it_{}'.format(i), tf.reduce_sum(ground_truth[:, i, :, :, :], -1)[..., None]) for j in range(7): state = states[i][..., j][..., None] gt = ground_truth[:, i, ..., j][..., None] tf.image_summary('state/it_{}/part_{}'.format(i, j), tf.concat(2, (state, gt))) for j in range(16): state = results[i][..., j][..., None] gt = keypoints[..., j][..., None] tf.image_summary('result/it_{}/kpt_{}'.format(i, j), tf.concat(2, (state, gt))) tf.image_summary('image', images) optimizer = tf.train.AdamOptimizer(FLAGS.initial_learning_rate) with tf.Session(graph=g) as sess: init_fn = None if FLAGS.pretrained_resnet_checkpoint_path: init_fn = restore_resnet(sess, FLAGS.pretrained_resnet_checkpoint_path) if FLAGS.pretrained_model_checkpoint_path: print('Loading whole model...') variables_to_restore = slim.get_model_variables() init_fn = slim.assign_from_checkpoint_fn( FLAGS.pretrained_model_checkpoint_path, variables_to_restore, ignore_missing_vars=True) train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=True) logging.set_verbosity(1) slim.learning.train(train_op, FLAGS.train_dir, save_summaries_secs=60, init_fn=init_fn, save_interval_secs=600)
def main(config_file=None): cwd = os.getcwd() # parse configuration file if config_file is None: config_file = sys.argv[-1] config_file = os.path.join(cwd, config_file) config_file_name = config_file.split('/')[-1] configs = parse_config(config_file) # save config file if configs['Train']['save_configs']: # confirm save dir config_save_path = 'logs/' + configs['Name'] config_save_path = os.path.join(cwd, config_save_path) if not os.path.exists(config_save_path): os.mkdir(os.path.join(cwd, config_save_path)) # copy config file config_dst_name = configs['Name'] + '.json' config_file_dst = os.path.join(config_save_path, config_dst_name) shutil.copy(config_file, config_file_dst) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if configs['Train']['gpu']: os.environ['CUDA_VISIBLE_DEVICES'] = configs['Train']['gpu'] keras.backend.tensorflow_backend.set_session(get_session()) # create the generators train_generator, validation_generator = create_generators(configs) # create the model if configs['Train']['load_snapshot'] is not None: print('Loading model, this may take a second...') model = models.load_model(configs['Train']['load_snapshot'], backbone=configs['Train']['backbone']) training_model = prediction_model = model else: weights = configs['Train']['weights'] # default to imagenet if nothing else is specified if weights is None and configs['Train']['imagenet_weights']: weights = models.download_imagenet(configs['Train']['backbone']) print('Creating model, this may take a second...') backbone = configs['Train']['backbone'] num_classes = train_generator.num_classes() multi_gpu = configs['Train']['multi_gpu'] freeze_backbone = configs['Train']['freeze_backbone'] modifier = freeze_model if freeze_backbone else None # Keras recommends initialising a multi-gpu model on the CPU to ease weight sharing, and to prevent OOM errors. # optionally wrap in a parallel model if multi_gpu > 1: with tf.device('/cpu:0'): retinanet = models.retinanet_backbone( configs['Train']['backbone'])(num_classes, backbone=backbone, modifier=modifier) model = model_with_weights(retinanet, weights=weights, skip_mismatch=True) training_model = multi_gpu_model(model, gpus=multi_gpu) else: retinanet = models.retinanet_backbone( configs['Train']['backbone'])(num_classes, backbone=backbone, modifier=modifier) training_model = model = model_with_weights(retinanet, weights=weights, skip_mismatch=True) # make prediction model prediction_model = retinanet_bbox(model=model, anchor_param=configs['Anchors']) # compile model subnet_loss = { 'regression': losses.smooth_l1(), 'classification': losses.focal() } # run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) # run_metadata = tf.RunMetadata() if configs['Train']['lr_multiplier_layer']: optimizer = AdamWithLRMult( lr=configs['Train']['init_lr'], lr_multipliers=configs['Train']['lr_multiplier_layer'], debug_verbose=False, clipnorm=0.001) else: optimizer = keras.optimizers.adam(configs['Train']['init_lr'], clipnorm=0.001) training_model.compile(loss=subnet_loss, optimizer=optimizer) # training_model.compile(loss=subnet_loss, optimizer=keras.optimizers.adam(configs['Train']['init_lr'], clipnorm=0.001)) # this lets the generator compute backbone layer shapes using the actual backbone model if 'vgg' in configs['Train']['backbone'] or 'densenet' in configs['Train'][ 'backbone']: compute_anchor_targets = functools.partial( anchor_targets_bbox, shapes_callback=make_shapes_callback(model)) train_generator.compute_anchor_targets = compute_anchor_targets if validation_generator is not None: validation_generator.compute_anchor_targets = compute_anchor_targets # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, configs, ) # start training training_model.fit_generator( train_generator, validation_data=validation_generator, validation_steps=39, steps_per_epoch=configs['Train']['steps'], epochs=configs['Train']['epochs'], verbose=1, callbacks=callbacks, )
def convert(args): rot_parameters = {"axis_angle": 3, "rotation_matrix": 9, "quaternion": 4} model = model_clean.EfficientPose( args.phi, num_classes=args.num_classes, num_anchors=args.num_anchors, freeze_bn=False, score_threshold=args.score_threshold, num_rotation_parameters=rot_parameters[args.rotation_representation], lite=args.lite, no_se=args.no_se, use_groupnorm=args.use_groupnorm) _, _, tflite_model = model.get_models() tflite_model( [tf.random.normal((1, 512, 512, 3)), tf.random.normal((1, 6))]) # inp = tf.keras.layers.Input((512,512,3), dtype=tf.uint8) # inp2 = tf.keras.layers.Input((6,)) # tflite_raw_model = tfkeras.EfficientNetB0(input_tensor=preprocess_image(inp)) # tflite_raw_model = tf.keras.Model(inputs=[inp, inp2], outputs=tflite_raw_model) weight_loader.load_weights_rec(tflite_model, args.weights) if args.freeze_bn: weight_loader.freeze_bn(tflite_model) if args.q_aware: gen, val = create_generators(args) q_aware_model = tfmot.quantization.keras.quantize_model(tflite_model) q_aware_model.compile( optimizer="adam", loss={ 'regression': smooth_l1(), 'classification': focal(), 'transformation': transformation_loss(model_3d_points_np=gen. get_all_3d_model_points_array_for_loss(), num_rotation_parameter=3) }, loss_weights={ 'regression': 1.0, 'classification': 1.0, 'transformation': 0.02 }) q_aware_model.fit(gen, steps_per_epoch=1000, initial_epoch=0, epochs=5, verbose=1, validation_data=val) #tflite_raw_model.save("models/model.h5") tflite_model( [tf.random.normal((1, 512, 512, 3)), tf.random.normal((1, 6))]) converter = tf.lite.TFLiteConverter.from_keras_model(tflite_model) converter.experimental_new_converter = True converter.target_spec.supported_ops = [ tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops. tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops. ] #converter.representative_dataset = tf.lite.RepresentativeDataset(create_generators(args)) #converter.optimizations = [tf.lite.Optimize.DEFAULT] converter.allow_custom_ops = True tflite_model = converter.convert() outfile = os.path.join( args.models_dir, f"{args.base_name}_phi{args.phi}{'_lite' if args.lite else ''}.tflite") os.makedirs(args.models_dir, exist_ok=True) with open(outfile, 'wb') as f: f.write(tflite_model) if args.benchmark: run_benchmark.benchmark(args)
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # create the generators train_generator, validation_generator = create_generators(args) num_classes = train_generator.num_classes() num_anchors = train_generator.num_anchors # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu K.set_session(get_session()) model, prediction_model = efficientdet( args.phi, num_classes=num_classes, num_anchors=num_anchors, weighted_bifpn=args.weighted_bifpn, freeze_bn=args.freeze_bn, detect_quadrangle=args.detect_quadrangle) # load pretrained weights if args.snapshot: if args.snapshot == 'imagenet': model_name = 'efficientnet-b{}'.format(args.phi) file_name = '{}_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5'.format( model_name) file_hash = WEIGHTS_HASHES[model_name][1] weights_path = keras.utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path, by_name=True) else: print('Loading model, this may take a second...') model.load_weights(args.snapshot, by_name=True) # freeze backbone layers if args.freeze_backbone: # 227, 329, 329, 374, 464, 566, 656 for i in range(1, [227, 329, 329, 374, 464, 566, 656][args.phi]): model.layers[i].trainable = False if args.gpu and len(args.gpu.split(',')) > 1: model = keras.utils.multi_gpu_model(model, gpus=list( map(int, args.gpu.split(',')))) # compile model model.compile( optimizer=Adam(lr=1e-3), loss={ 'regression': smooth_l1_quad() if args.detect_quadrangle else smooth_l1(), 'classification': focal() }, ) # print(model.summary()) # create the callbacks callbacks = create_callbacks( model, prediction_model, validation_generator, args, ) if not args.compute_val_loss: validation_generator = None elif args.compute_val_loss and validation_generator is None: raise ValueError( 'When you have no validation data, you should not specify --compute-val-loss.' ) # start training return model.fit_generator(generator=train_generator, steps_per_epoch=args.steps, initial_epoch=0, epochs=args.epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=args.multiprocessing, max_queue_size=args.max_queue_size, validation_data=validation_generator)
def main(train_csv, val_csv, classes_csv, epoch_num, phi_num, steps_epoch, batch_num, model_path): batch_size = batch_num #def 1 phi = phi_num #def 0 is_text_detect = False is_detect_quadrangle = False rand_transf_augm = True train_ann_path = train_csv train_class_path = classes_csv val_ann_path = val_csv val_class_path = classes_csv epochs = epoch_num workers = 1 steps_p_epoch = steps_epoch use_multiproc = True max_que_size = 10 comp_loss = True gpu = 0 freeze_bn_arg = True weighted_bi = False # create the generators train_generator, validation_generator = create_generators( batch_size, phi, is_text_detect, is_detect_quadrangle, rand_transf_augm, train_ann_path, val_ann_path, train_class_path, val_class_path) num_classes = train_generator.num_classes() num_anchors = train_generator.num_anchors # optionally choose specific GPU if gpu: os.environ['CUDA_VISIBLE_DEVICES'] = gpu # K.set_session(get_session()) model, prediction_model = efficientdet( phi, num_classes=num_classes, num_anchors=num_anchors, weighted_bifpn=weighted_bi, freeze_bn=freeze_bn_arg, detect_quadrangle=is_detect_quadrangle) # freeze backbone layers if freeze_bn_arg: # 227, 329, 329, 374, 464, 566, 656 for i in range(1, [227, 329, 329, 374, 464, 566, 656][phi]): model.layers[i].trainable = False model.compile( optimizer=Adam(lr=1e-3), loss={ 'regression': smooth_l1_quad() if is_detect_quadrangle else smooth_l1(), 'classification': focal() }, ) # print(model.summary()) # create the callbacks callbacks = create_callbacks(model_path) if not comp_loss: validation_generator = None elif comp_loss and validation_generator is None: raise ValueError( 'When you have no validation data, you should not specify --compute-val-loss.' ) # start training return model.fit_generator(generator=train_generator, steps_per_epoch=steps_p_epoch, initial_epoch=0, epochs=epochs, verbose=1, callbacks=callbacks, workers=workers, use_multiprocessing=use_multiproc, max_queue_size=max_que_size, validation_data=validation_generator)