def main(_): tf_filenames = dataset_factory.get_tf_filenames(FLAGS.data_dir, FLAGS.split_name, shuffle=True) for i in range(FLAGS.num_epoch): print("-" * 30) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) print("Training epoch {}/{}".format(i + 1, FLAGS.num_epoch)) for tf_filename in tqdm(tf_filenames): # read tensor from dataset dataset = dataset_factory.get_dataset(tf_filename) # bbox: [ymin,xmin,ymax,xmax] image, bbox, _ = dataset_factory.data_provider(dataset) # TODO: receive tensor, return tensor. # image,bbox = preprocessing(image,bbox) # TODO: run tensor to get a training pair (exemplar,instance) # exemplar_img,instance_img,regression_target,conf_target = \ # dataset_factory.get_pair(image,bbox) pdb.set_trace() return
def get_weights(model_name, dataset_name, dataset_dir, dataset_split_name, checkpoint_path, conv_scope=None, batch_size=1, is_training=False): with tf.Graph().as_default(): dataset = dataset_factory.get_dataset(dataset_name, dataset_split_name, dataset_dir) preprocessing_fn = preprocessing_factory.get_preprocessing( model_name, is_training=is_training) network_fn = network_factory.get_network_fn(model_name, FLAGS.num_classes) images, _, labels = train_test_utils.load_batch( dataset, preprocessing_fn, is_training=is_training, batch_size=batch_size, shuffle=False) logits, end_points = network_fn(images) model_vars = slim.get_model_variables() variables_to_restore = slim.get_variables_to_restore() restorer = tf.train.Saver(variables_to_restore) if os.path.isdir(checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(checkpoint_path) with tf.Session() as sess: restorer.restore(sess, checkpoint_path) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) vals = sess.run(model_vars) val_map = {} for i, var in enumerate(model_vars): if 'conv' in var.op.name: val_map[var.op.name] = vals[i] coord.request_stop() coord.join() return val_map
def main(): import dataset_factory tf_filenames = dataset_factory.get_tf_filenames("./tf_records", "train", shuffle=True) dataset = dataset_factory.get_dataset(tf_filenames[0]) # bbox: [ymin,xmin,ymax,xmax] image, bbox, _ = dataset_factory.data_provider(dataset) # bbox coordinates range (0,1) for next processing bbox = utils.bbox_coord_range(image, bbox) # synthetic processing image, bbox = preprocess_for_train(image, bbox, out_shape=312) # show the processing result img, box = visualize_result(image, bbox) show_img_and_bbox(img, box)
def main(_): if not FLAGS.output_file: raise ValueError( 'You must supply the path to save to with --output_file') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default() as graph: dataset = dataset_factory.get_dataset(FLAGS.dataset_name, 'train', FLAGS.dataset_dir) network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=(dataset.num_classes - FLAGS.labels_offset), is_training=FLAGS.is_training) image_size = FLAGS.image_size or network_fn.default_image_size placeholder = tf.placeholder( name='input', dtype=tf.float32, shape=[FLAGS.batch_size, image_size, image_size, 3]) network_fn(placeholder) graph_def = graph.as_graph_def() with gfile.GFile(FLAGS.output_file, 'wb') as f: f.write(graph_def.SerializeToString())
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): ####################### # Config model_deploy # ####################### deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=FLAGS.task, num_replicas=FLAGS.worker_replicas, num_ps_tasks=FLAGS.num_ps_tasks) # Create global_step with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() ###################### # Select the dataset # ###################### dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) ###################### # Select the network # ###################### network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=(dataset.num_classes - FLAGS.labels_offset), weight_decay=FLAGS.weight_decay, is_training=True) ##################################### # Select the preprocessing function # ##################################### preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) ############################################################## # Create a dataset provider that loads data from the dataset # ############################################################## with tf.device(deploy_config.inputs_device()): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size) [image, label] = provider.get(['image', 'label']) label -= FLAGS.labels_offset train_image_size = FLAGS.train_image_size or network_fn.default_image_size image = image_preprocessing_fn(image, train_image_size, train_image_size) images, labels = tf.train.batch( [image, label], batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) labels = slim.one_hot_encoding( labels, dataset.num_classes - FLAGS.labels_offset) batch_queue = slim.prefetch_queue.prefetch_queue( [images, labels], capacity=2 * deploy_config.num_clones) #################### # Define the model # #################### def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" images, labels = batch_queue.dequeue() logits, end_points = network_fn(images) ############################# # Specify the loss function # ############################# if 'AuxLogits' in end_points: tf.losses.softmax_cross_entropy( logits=end_points['AuxLogits'], onehot_labels=labels, label_smoothing=FLAGS.label_smoothing, weights=0.4, scope='aux_loss') tf.losses.softmax_cross_entropy( logits=logits, onehot_labels=labels, label_smoothing=FLAGS.label_smoothing, weights=1.0) return end_points # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) first_clone_scope = deploy_config.clone_scope(0) # Gather update_ops from the first clone. These contain, for example, # the updates for the batch_norm variables created by network_fn. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) # Add summaries for end_points. end_points = clones[0].outputs for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram('activations/' + end_point, x)) summaries.add( tf.summary.scalar('sparsity/' + end_point, tf.nn.zero_fraction(x))) # Add summaries for losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope): summaries.add(tf.summary.scalar('losses/%s' % loss.op.name, loss)) # Add summaries for variables. for variable in slim.get_model_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) ################################# # Configure the moving averages # ################################# if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None ######################################### # Configure the optimization procedure. # ######################################### with tf.device(deploy_config.optimizer_device()): learning_rate = _configure_learning_rate(dataset.num_samples, global_step) optimizer = _configure_optimizer(learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) if FLAGS.sync_replicas: # If sync_replicas is enabled, the averaging will be done in the chief # queue runner. optimizer = tf.train.SyncReplicasOptimizer( opt=optimizer, replicas_to_aggregate=FLAGS.replicas_to_aggregate, variable_averages=variable_averages, variables_to_average=moving_average_variables, replica_id=tf.constant(FLAGS.task, tf.int32, shape=()), total_num_replicas=FLAGS.worker_replicas) elif FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append( variable_averages.apply(moving_average_variables)) # Variables to train. variables_to_train = _get_variables_to_train() # and returns a train_tensor and summary_op total_loss, clones_gradients = model_deploy.optimize_clones( clones, optimizer, var_list=variables_to_train) # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # Create gradient updates. grad_updates = optimizer.apply_gradients(clones_gradients, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') # Add the summaries from the first clone. These contain the summaries # created by model_fn and either optimize_clones() or _gather_clone_loss(). summaries |= set( tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') ########################### # Kicks off the training. # ########################### slim.learning.train( train_tensor, logdir=FLAGS.train_dir, master=FLAGS.master, is_chief=(FLAGS.task == 0), init_fn=_get_init_fn(), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, save_interval_secs=FLAGS.save_interval_secs, sync_optimizer=optimizer if FLAGS.sync_replicas else None)
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.INFO) #os.environ["OMP_NUM_THREADS"] = "54" with tf.Graph().as_default(): tf_global_step = slim.get_or_create_global_step() ###################### # Select the dataset # ###################### dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) #################### # Select the model # #################### network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=(dataset.num_classes - FLAGS.labels_offset), is_training=False) ############################################################## # Create a dataset provider that loads data from the dataset # ############################################################## provider = slim.dataset_data_provider.DatasetDataProvider( dataset, shuffle=False, common_queue_capacity=2 * FLAGS.batch_size, common_queue_min=FLAGS.batch_size) [image, label] = provider.get(['image', 'label']) label -= FLAGS.labels_offset ##################################### # Select the preprocessing function # ##################################### preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=False) eval_image_size = \ FLAGS.eval_image_size or network_fn.default_image_size image = image_preprocessing_fn(image, eval_image_size, eval_image_size) images, labels = tf.train.batch( [image, label], batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) #################### # Define the model # #################### logits, _ = network_fn(images) if FLAGS.moving_average_decay: variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, tf_global_step) variables_to_restore = variable_averages.variables_to_restore( slim.get_model_variables()) variables_to_restore[tf_global_step.op.name] = tf_global_step else: variables_to_restore = slim.get_variables_to_restore() predictions = tf.argmax(logits, 1) #labels = tf.squeeze(labels) # Define the metrics: names_to_values, names_to_updates = \ slim.metrics.aggregate_metric_map({ 'Accuracy': slim.metrics.streaming_accuracy( predictions, labels), 'Recall_5': slim.metrics.streaming_recall_at_k( logits, labels, 5), }) # Print the summaries to screen. for name, value in names_to_values.items(): summary_name = 'eval/%s' % name op = tf.summary.scalar(summary_name, value, collections=[]) op = tf.Print(op, [value], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # TODO(sguada) use num_epochs=1 if FLAGS.max_num_batches: num_batches = FLAGS.max_num_batches else: # This ensures that we make a single pass over all of the data. num_batches = math.ceil(dataset.num_samples / float(FLAGS.batch_size)) num_batches = 100 config = tf.ConfigProto( inter_op_parallelism_threads=FLAGS.inter_op_parallelism_threads, intra_op_parallelism_threads=FLAGS.intra_op_parallelism_threads) if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path tf.logging.info('Evaluating %s' % checkpoint_path) slim.evaluation.evaluate_once( master=FLAGS.master, checkpoint_path=checkpoint_path, logdir=FLAGS.eval_dir, num_evals=num_batches, eval_op=list(names_to_updates.values()), variables_to_restore=variables_to_restore, hooks=[_LoggerHook()], session_config=config)
import debiasing_networks import visualize_data import dataset_factory import matplotlib.pyplot as plt desired_number_of_samples = 5000 debias_type = 'regression_cond_y' #'lda' #'regression_cond_x' #''lda' # 'regression_cond_y' use_continuous_y = False # number of y intervals if a continous variable is used as output number_of_continuous_y_intervals = None dataset_nr = 5 sim_dataset_names = ['SJL', 'ZafarWWW2017CaseII', 'ZafarAISTATS2017', 'JLcontinuousY','GMcontinuousY','gaussian_test'] sim_dataset_name = sim_dataset_names[dataset_nr] current_dataset, x, y, z, xl, yl, zl = dataset_factory.get_dataset(simulator_dataset_name=sim_dataset_name,nr_of_samples=desired_number_of_samples, debias_type=debias_type,use_continuous_y=use_continuous_y,number_of_continuous_y_intervals=number_of_continuous_y_intervals) y_val_ranges = [0.0,1.0] group_indices_dict = dataset_factory.compute_group_indices(y=y,y_val_ranges=y_val_ranges) group_indices = [] for k in group_indices_dict: group_indices.append(group_indices_dict[k]) # create a list of group indices (this is what the data loader would typically do) #debias_layer = debiasing_networks.DebiasLayer(nr_inputs=2,nr_outputs=2,debias_type=debias_type,use_batch_normalization=False, # use_protected_projection=False,use_only_projection_penalty=True, sigma_reg=0.0) proj_layer = debiasing_networks.ProtectedRegressionLayerCondY() h_projected,projection_loss,w = proj_layer(h=x,z=z,y=y,turn_protected_projection_on=True, use_only_projection_penalty=False,current_epoch=None, group_indices=group_indices)
def get_train_dataset_num_samples(dataset_name, dataset_dir): with tf.Graph().as_default(): dataset = dataset_factory.get_dataset( dataset_name, 'train', dataset_dir) return dataset.num_samples
def eval_model(model_name, dataset_name, dataset_dir, train_dir, batch_size=32, use_mask=False, num_groups=2, init_from_pre_trained=False, init_pointwise_from_pre_trained=False, weights_map=None, **kwargs): """ Evaluate the performance of a model. """ with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) tf_global_step = tf.train.get_or_create_global_step() dataset = dataset_factory.get_dataset( dataset_name, 'test', dataset_dir) preprocessing_fn = preprocessing_factory.get_preprocessing( model_name, is_training=False) network_fn = network_factory.get_network_fn( model_name, dataset.num_classes, use_mask=use_mask) images, images_raw, labels = load_batch(dataset, preprocessing_fn, shuffle=False, batch_size=batch_size, is_training=False) logits, _ = network_fn(images, **kwargs) predictions = tf.argmax(logits, 1) # Evaluation names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'eval/accuracy': slim.metrics.streaming_accuracy(predictions, labels), 'eval/recall_5': slim.metrics.streaming_sparse_recall_at_k(logits, labels, 5), }) summary_op = create_eval_summary_op(names_to_values) num_evals = int(math.ceil(dataset.num_samples / float(batch_size))) variables_to_restore = slim.get_variables_to_restore() # We don't want to mess up with the train dir eval_dir = train_dir + '_eval' metric_values = slim.evaluation.evaluate_once( '', tf.train.latest_checkpoint(train_dir), eval_dir, num_evals=num_evals, eval_op=list(names_to_updates.values()), final_op=list(names_to_updates.values()), variables_to_restore=variables_to_restore) names_to_values = dict(zip(names_to_values.keys(), metric_values)) for name in names_to_values: print('%s: %f' % (name, names_to_values[name])) return names_to_values['eval/accuracy'], names_to_values['eval/recall_5']
def train_model(model_name, dataset_name, dataset_dir, train_dir, batch_size=32, checkpoint_path=None, max_number_of_epochs=10, trainable_scopes=None, checkpoint_exclude_scopes=None, use_mask=False, weight_decay=4e-5, learning_rate=1e-3, num_groups=2, init_from_pre_trained=False, init_pointwise_from_pre_trained=False, weights_map=None, bipartite_connections_map=None, **kwargs): """ Train the given model. Returns: A final loss of the training process. """ tf.logging.set_verbosity(tf.logging.INFO) layer_replacement = kwargs.get('layer_replacement') with tf.Graph().as_default(): dataset = dataset_factory.get_dataset(dataset_name, 'train', dataset_dir) preprocessing_fn = preprocessing_factory.get_preprocessing( model_name, is_training=True) network_fn = network_factory.get_network_fn( model_name, dataset.num_classes, use_mask=use_mask, weight_decay=weight_decay, is_training=True) max_number_of_steps = int(math.ceil(max_number_of_epochs * dataset.num_samples / batch_size)) tf.logging.info('Training on %s' % train_dir) tf.logging.info('Number of samples: %d' % dataset.num_samples) tf.logging.info('Max number of steps: %d' % max_number_of_steps) """ Load data from the dataset and pre-process. """ images, images_raw, labels = load_batch( dataset, preprocessing_fn, is_training=True) # create arg_scope logits, end_points = network_fn(images, bipartite_connections_map=bipartite_connections_map, **kwargs) # compute losses one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) slim.losses.softmax_cross_entropy(logits, one_hot_labels) total_loss = slim.losses.get_total_loss() # configure learning rate global_step = slim.create_global_step() learning_rate = configure_learning_rate(learning_rate, dataset.num_samples, global_step) # create summary op summary_op = create_train_summary_op(end_points, learning_rate, total_loss, images_raw, images, model_name=model_name) """ Configure optimizer and training. if we do fine-tuning, just set trainable_scopes. """ variables_to_train = get_variables_to_train(trainable_scopes) optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate, decay=0.9, momentum=0.9, epsilon=1.0) # training operator train_op = slim.learning.create_train_op(total_loss, optimizer, variables_to_train=variables_to_train) init_fn = get_init_fn(checkpoint_path, checkpoint_exclude_scopes, layer_replacement, model_name, weights_map=weights_map, bipartite_connections_map=bipartite_connections_map, init_from_pre_trained=init_from_pre_trained, init_pointwise_from_pre_trained=init_pointwise_from_pre_trained, num_groups=num_groups) # final loss value final_loss = slim.learning.train(train_op, logdir=train_dir, init_fn=init_fn, number_of_steps=max_number_of_steps, log_every_n_steps=10, save_summaries_secs=60) print('Finished training. Final batch loss %f' % final_loss) return final_loss
def evaluate(model_name, dataset_name, dataset_dir, dataset_split_name, eval_dir, checkpoint_path, conv_scope, batch_size=32, in_channel=0, out_channel=0, is_training=False): """ Evaluate a single conv_scope """ with tf.Graph().as_default(): g = tf.get_default_graph() dataset = dataset_factory.get_dataset(dataset_name, dataset_split_name, dataset_dir) num_batches = math.ceil(dataset.num_samples / batch_size) preprocessing_fn = preprocessing_factory.get_preprocessing( model_name, is_training=is_training) network_fn = network_factory.get_network_fn(model_name, dataset.num_classes, use_mask=True, is_training=is_training) images, _, labels = train_test_utils.load_batch( dataset, preprocessing_fn, is_training=is_training, batch_size=batch_size, shuffle=False) logits, end_points = network_fn(images) predictions = tf.argmax(logits, 1) mask_assign_op = get_mask_assign_op(conv_scope, in_channel, out_channel, g) one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) loss = tf.nn.softmax_cross_entropy_with_logits_v2( logits=logits, labels=one_hot_labels) # Evaluation names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'eval/accuracy': tf.metrics.accuracy(predictions, labels), 'eval/recall_5': slim.metrics.streaming_sparse_recall_at_k(logits, labels, 5), 'eval/mean_loss': tf.metrics.mean(loss), }) mask_variables = [ var for var in slim.get_model_variables() if 'mask' in var.op.name ] variables_to_restore = slim.get_variables_to_restore() variables_to_restore = [ x for x in variables_to_restore if 'mask' not in x.op.name ] restorer = tf.train.Saver(variables_to_restore) with tf.Session() as sess: tf.summary.FileWriter(eval_dir, sess.graph) restorer.restore(sess, tf.train.latest_checkpoint(checkpoint_path)) sess.run(tf.local_variables_initializer()) sess.run(tf.variables_initializer(mask_variables)) sess.run(mask_assign_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for batch_id in range(num_batches): if batch_id != 0 and batch_id % 10 == 0: tf.logging.info('Evaluated [%5d/%5d]' % (batch_id, num_batches)) # run accuracy evaluation sess.run(list(names_to_updates.values())) metric_values = sess.run(list(names_to_values.values())) for metric, value in zip(names_to_values.keys(), metric_values): print('Metric %s has value: %f' % (metric, value)) coord.request_stop() coord.join()
def main(argv): seed = None if os.path.exists(FLAGS.config): with open(FLAGS.config) as ifp: cfg = yaml.safe_load(ifp) augmentations = cfg['dataset']['train'][ 'augmentations'] if 'augmentations' in cfg['dataset'][ 'train'] else [] train_dataset = dataset_factory.get_dataset( cfg['dataset']['dataset_dir'], cfg['dataset']['train']['pattern'], cfg['training']['batch_size'], cfg['dataset']['height'], cfg['dataset']['width'], cfg['dataset']['channel'], augmentations, seed, cfg['dataset']['mul'], cfg['dataset']['add']) augmentations = cfg['dataset']['validation'][ 'augmentations'] if 'augmentations' in cfg['dataset'][ 'validation'] else [] val_dataset = dataset_factory.get_dataset( cfg['dataset']['dataset_dir'], cfg['dataset']['validation']['pattern'], cfg['validation']['batch_size'], cfg['dataset']['height'], cfg['dataset']['width'], cfg['dataset']['channel'], augmentations, seed, cfg['dataset']['mul'], cfg['dataset']['add']) # for data in train_dataset.take(1): # print(data[0].shape) # print(data[0][0].shape) # img = data[0][0].numpy() # print(np.min(img), np.max(img)) # plt.imsave('tmp.png', img) tf.keras.backend.set_image_data_format('channels_last') num_class = cfg['dataset']['num_class'] height = cfg['dataset']['height'] width = cfg['dataset']['width'] channel = cfg['dataset']['channel'] if cfg['model']['name'] == 'mobilenet': alpha = cfg['model']['alpha'] depth_multiplier = cfg['model']['depth_multiplier'] dropout = cfg['model']['dropout'] model = tf.keras.applications.MobileNet( input_shape=(height, width, channel), alpha=alpha, depth_multiplier=depth_multiplier, include_top=False, pooling='avg', weights='imagenet', classes=num_class) inputs = model.get_layer('input_1').input outputs = model.get_layer('global_average_pooling2d').output outputs = tf.keras.layers.Reshape((1, 1, int(1024 * alpha)), name='reshape_1')(outputs) outputs = tf.keras.layers.Dropout(dropout, name='dropout')(outputs) outputs = tf.keras.layers.Conv2D(num_class, (1, 1), padding='same', name='conv_preds')(outputs) outputs = tf.keras.layers.Reshape((num_class, ), name='reshape_2')(outputs) outputs = tf.keras.layers.Activation( 'softmax', name='act_softmax')(outputs) model = tf.keras.Model(inputs, outputs, name='mobilenet_%0.2f_%s_%d' % (alpha, height, num_class)) else: print('Error: not supported model', file=sys.stderr) return 1 model.summary() if cfg['optimizer']['name'] == 'adam': learning_rate = cfg['optimizer']['learning_rate'] beta_1 = cfg['optimizer']['beta_1'] beta_2 = cfg['optimizer']['beta_2'] epsilon = cfg['optimizer']['epsilon'] amsgrad = cfg['optimizer']['amsgrad'] optimizer = tf.keras.optimizers.Adam(learning_rate, beta_1, beta_2, epsilon, amsgrad) else: print('Error: not supported optimizer', file=sys.stderr) return 1 if cfg['loss']['name'] == 'sparse_categorical_cross_entropy': loss = tf.keras.losses.SparseCategoricalCrossentropy() else: print('Error: not supported optimizer', file=sys.stderr) return 1 metrics = [] for metric_config in cfg['metrics']: if metric_config['name'] == 'sparse_categorical_accuracy': metrics.append( tf.keras.metrics.SparseCategoricalAccuracy()) model.compile(optimizer=optimizer, loss=loss, metrics=metrics) epoch = 0 history = model.fit( train_dataset, initial_epoch=epoch, epochs=cfg['training']['epochs'], verbose=cfg['training']['verbose'], validation_data=val_dataset, steps_per_epoch=math.ceil(cfg['dataset']['train']['count'] / cfg['training']['batch_size']), validation_steps=cfg['validation']['steps'], validation_freq=cfg['validation']['freq']) epoch += cfg['training']['epochs'] print(history.history) results = model.evaluate( val_dataset, verbose=cfg['validation']['verbose'], steps=math.ceil(cfg['dataset']['validation']['count'] / cfg['validation']['batch_size'])) print("test loss, test acc:", results) if not os.path.exists(cfg['save']['dir_path']): os.makedirs(cfg['save']['dir_path']) fname = os.path.join( cfg['save']['dir_path'], f'{cfg["save"]["basename"]}-{epoch:06d}.{"h5" if cfg["save"]["format"]=="h5" else ""}' ) model.save(fname, save_format=cfg['save']['format']) return 0 print('Error: config file not found', file=sys.stderr) return 1