def main(unused_argv): os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_to_use dconf = data_config.get_config(FLAGS.data_name) dataset = input_fn_amoabanet(False, False, FLAGS.data_dir, FLAGS.batch_size, dataset_name=FLAGS.data_name, preprocessing_type=FLAGS.preprocessing_type) iterator = dataset.make_one_shot_iterator() images, labels = iterator.get_next() module = hub.Module("https://tfhub.dev/google/imagenet/amoebanet_a_n18_f448/classification/1") logits = module(images) # Logits with shape [batch_size, num_classes]. pred = tf.nn.softmax(logits) top1 = tf.argmax(logits, axis=1) np_preds = np.zeros(dconf.num_images['validation'], dtype=np.int64) np_labels = np.zeros(dconf.num_images['validation'], dtype=np.int64) np_i = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) n_loop = dconf.num_images['validation'] // FLAGS.batch_size for _ in tqdm(range(n_loop + 1)): try: _pred, _top1, _labels = sess.run([pred, top1, labels]) np_preds[np_i:np_i + _pred.shape[0]] = _top1 np_labels[np_i:np_i + _labels.shape[0]] = _labels np_i += _pred.shape[0] except tf.errors.OutOfRangeError: break print('Accuracy:') print(np.count_nonzero(np_preds == np_labels) / dconf.num_images['validation'])
def model_fn_bfe(features, labels, mode, params): assert int(params['resnet_size']) >= 50 dataset = data_config.get_config(params['dataset_name']) learning_rate_fn = learning_rate_with_decay( learning_rate_decay_type=params['learning_rate_decay_type'], batch_size=params['batch_size'], batch_denom=params['batch_size'], num_images=dataset.num_images['train'], num_epochs_per_decay=params['num_epochs_per_decay'], learning_rate_decay_factor=params['learning_rate_decay_factor'], end_learning_rate=params['end_learning_rate'], piecewise_lr_boundary_epochs=params['piecewise_lr_boundary_epochs'], piecewise_lr_decay_rates=params['piecewise_lr_decay_rates'], base_lr=params['base_learning_rate'], train_epochs=params['train_epochs'], warmup=params['lr_warmup']) if params['use_dropblock']: starter_kp = params['dropblock_kp'][0] end_kp = params['dropblock_kp'][1] batches_per_epoch = dataset.num_images['train'] / params['batch_size'] decay_steps = int(params['train_epochs'] * batches_per_epoch) keep_prob_fn = keep_prob_decay(starter_kp, end_kp, decay_steps) else: keep_prob_fn = None return run_loop_bfe.resnet_ir_model_fn( features=features, labels=labels, mode=mode, model_class=Model, resnet_size=params['resnet_size'], weight_decay=params['weight_decay'], learning_rate_fn=learning_rate_fn, momentum=params['momentum'], data_format=params['data_format'], dim_features=params['dim_features'], num_classes=dataset.num_classes, resnet_version=params['resnet_version'], loss_scale=params['loss_scale'], loss_filter_fn=None, dtype=params['dtype'], fine_tune=params['fine_tune'], zero_gamma=params['zero_gamma'], use_resnet_d=params['use_resnet_d'], no_downsample=params['no_downsample'], weight_softmax_loss=params['weight_softmax_loss'], weight_ranking_loss=params['weight_ranking_loss'], label_smoothing=params['label_smoothing'], use_bfe=params['use_bfe'], use_global_branch=params['use_global_branch'], ranking_loss_type=params['ranking_loss_type'], off_bfe_ranking=params['off_bfe_ranking'], margin=params['margin'], use_summary_image=params['use_summary_image'], use_se_block=params['use_se_block'], rollback_period=params['rollback_period'], rollback_lr_multiplier=params['rollback_lr_multiplier'], keep_prob_fn=keep_prob_fn)
def input_fn_amoabanet(is_training, use_random_crop, data_dir, batch_size, num_epochs=1, num_gpus=None, dtype=tf.float32, with_drawing_bbox=False, autoaugment_type=None, dataset_name=None, drop_remainder=False, preprocessing_type='imagenet', return_logits=False, dct_method="", train_regex='train*', val_regex='validation*'): filenames = data_util.get_filenames(is_training, data_dir, train_regex=train_regex, val_regex=val_regex) dataset = data_config.get_config(dataset_name) return input_fn(is_training, filenames, use_random_crop, batch_size, dataset.num_train_files, dataset.num_images['train'], dataset.shuffle_buffer, dataset.num_channels, num_epochs, num_gpus, dtype, autoaugment_type=autoaugment_type, with_drawing_bbox=with_drawing_bbox, drop_remainder=drop_remainder, preprocessing_type=preprocessing_type, return_logits=return_logits, dct_method=dct_method)
def input_fn_ir_eval(is_training, data_dir, batch_size, num_epochs=1, num_gpus=0, dtype=tf.float32, preprocessing_type='imagenet', dataset_name=None, dct_method="", val_regex='validation-*'): filenames = data_util.get_filenames(is_training, data_dir, val_regex=val_regex) assert len(filenames) > 0 dataset_config = data_config.get_config(dataset_name) return input_fn(is_training, filenames, False, batch_size, dataset_config.num_train_files, dataset_config.num_images['validation'], dataset_config.shuffle_buffer, dataset_config.num_channels, num_epochs, num_gpus, dtype, preprocessing_type=preprocessing_type, dct_method=dct_method)
def model_fn_cls(features, labels, mode, params): if int(params['resnet_size']) < 50: assert not params['use_dropblock'] assert not params['use_se_block'] assert not params['use_resnet_d'] dataset = data_config.get_config(params['dataset_name']) learning_rate_fn = learning_rate_with_decay( learning_rate_decay_type=params['learning_rate_decay_type'], batch_size=params['batch_size'], batch_denom=params['batch_size'], num_images=dataset.num_images['train'], num_epochs_per_decay=params['num_epochs_per_decay'], learning_rate_decay_factor=params['learning_rate_decay_factor'], end_learning_rate=params['end_learning_rate'], piecewise_lr_boundary_epochs=params['piecewise_lr_boundary_epochs'], piecewise_lr_decay_rates=params['piecewise_lr_decay_rates'], base_lr=params['base_learning_rate'], train_epochs=params['train_epochs'], warmup=params['lr_warmup']) if params['use_dropblock']: starter_kp = params['dropblock_kp'][0] end_kp = params['dropblock_kp'][1] batches_per_epoch = dataset.num_images['train'] / params['batch_size'] decay_steps = int(params['train_epochs'] * batches_per_epoch) keep_prob_fn = keep_prob_decay(starter_kp, end_kp, decay_steps) else: keep_prob_fn = None return run_loop_classification.resnet_model_fn( features=features, labels=labels, num_classes=dataset.num_classes, mode=mode, model_class=Model, resnet_size=params['resnet_size'], weight_decay=params['weight_decay'], learning_rate_fn=learning_rate_fn, momentum=params['momentum'], zero_gamma=params['zero_gamma'], use_resnet_d=params['use_resnet_d'], label_smoothing=params['label_smoothing'], data_format=params['data_format'], resnet_version=params['resnet_version'], loss_scale=params['loss_scale'], loss_filter_fn=None, dtype=params['dtype'], fine_tune=params['fine_tune'], use_se_block=params['use_se_block'], display_raw_images_with_bbox=params['display_raw_images_with_bbox'], use_ranking_loss=params['use_ranking_loss'], mixup_type=params['mixup_type'], rollback_period=params['rollback_period'], rollback_lr_multiplier=params['rollback_lr_multiplier'], keep_prob_fn=keep_prob_fn)
def input_fn_cls(is_training, use_random_crop, data_dir, batch_size, num_epochs=1, num_gpus=None, dtype=tf.float32, with_drawing_bbox=False, autoaugment_type=None, dataset_name=None, drop_remainder=False, dct_method=""): """Input function which provides batches for train or eval. Args: is_training: A boolean denoting whether the input is for training. use_random_crop: Whether to randomly crop a training image. data_dir: The directory containing the input data. batch_size: The number of samples per batch. num_epochs: The number of epochs to repeat the dataset. num_gpus: The number of gpus used for training. dtype: Data type to use for images/features autoaugment_type: Auto augmentation type. 'imagenet', 'svhn', 'cifar', 'good' with_drawing_bbox: If True, return the dataset including raw image tensor with bbox. dct_method: An optional `string`. Defaults to `""`. string specifying a hint about the algorithm used for decompression. Defaults to "" which maps to a system-specific default. Currently valid values are ["INTEGER_FAST", "INTEGER_ACCURATE"]. The hint may be ignored (e.g., the internal jpeg library changes to a version that does not have that specific option.) Returns: A dataset that can be used for iteration. """ filenames = data_util.get_filenames(is_training, data_dir) dataset = data_config.get_config(dataset_name) return input_fn(is_training, filenames, use_random_crop, batch_size, dataset.num_train_files, dataset.num_images['train'], dataset.shuffle_buffer, dataset.num_channels, num_epochs, num_gpus, dtype, autoaugment_type=autoaugment_type, with_drawing_bbox=with_drawing_bbox, drop_remainder=drop_remainder, dct_method=dct_method)
def input_fn_cls(is_training, use_random_crop, num_epochs, flags_obj): if flags_obj.mixup_type == 1 and is_training: batch_size = flags_obj.batch_size * 2 num_epochs = num_epochs * 2 else: batch_size = flags_obj.batch_size batch_size = distribution_utils.per_device_batch_size( batch_size, flags_core.get_num_gpus(flags_obj)) filenames_sup = data_util.get_filenames(is_training, flags_obj.data_dir, train_regex=flags_obj.train_regex, val_regex=flags_obj.val_regex) tf.logging.info('The # of Supervised tfrecords: {}'.format( len(filenames_sup))) dataset_meta = data_config.get_config(flags_obj.dataset_name) datasets = [] dataset_sup = input_fn(is_training, filenames_sup, use_random_crop, batch_size, dataset_meta.num_train_files, dataset_meta.num_images['train'], dataset_meta.shuffle_buffer, dataset_meta.num_channels, num_epochs, flags_core.get_num_gpus(flags_obj), flags_core.get_tf_dtype(flags_obj), autoaugment_type=flags_obj.autoaugment_type, with_drawing_bbox=flags_obj.with_drawing_bbox, drop_remainder=False, preprocessing_type=flags_obj.preprocessing_type, return_logits=flags_obj.kd_temp > 0, dct_method=flags_obj.dct_method, parse_record_fn=data_util.parse_record_sup) datasets.append(dataset_sup) def flatten_input(*features): images_dict = {} for feature in features: for key in feature: if key == 'label': label = feature[key] else: images_dict[key] = feature[key] return images_dict, label dataset = tf.data.Dataset.zip(tuple(datasets)) dataset = dataset.map(flatten_input) tf.logging.info('dataset = dataset.map(flatten_input)') tf.logging.info(dataset) return dataset
def input_fn_bfe_eval(is_training, data_dir, batch_size, num_epochs=1, num_gpus=0, dtype=tf.float32, preprocessing_type='imagenet', dataset_name=None, dct_method=""): """Input function which provides batches for train or eval. Args: is_training: A boolean denoting whether the input is for training. data_dir: The directory containing the input data. batch_size: The number of samples per batch. num_epochs: The number of epochs to repeat the dataset. num_gpus: The number of gpus used for training. dtype: Data type to use for images/features preprocessing_type: TODO dataset_name: TODO dct_method: An optional `string`. Defaults to `""`. string specifying a hint about the algorithm used for decompression. Defaults to "" which maps to a system-specific default. Currently valid values are ["INTEGER_FAST", "INTEGER_ACCURATE"]. The hint may be ignored (e.g., the internal jpeg library changes to a version that does not have that specific option.) Returns: A dataset that can be used for iteration. """ filenames = data_util.get_filenames(is_training, data_dir, val_regex='validation-label*') dataset_config = data_config.get_config(dataset_name) return input_fn(is_training, filenames, False, batch_size, dataset_config.num_train_files, dataset_config.num_images['validation'], dataset_config.shuffle_buffer, dataset_config.num_channels, num_epochs, num_gpus, dtype, preprocessing_type=preprocessing_type, dct_method=dct_method)
def model_fn_cls(features, labels, mode, params): if int(params['resnet_size']) < 50: assert not params['use_dropblock'] assert not params['use_se_block'] assert not params['use_sk_block'] assert not params['use_resnet_d'] dataset = data_config.get_config(params['dataset_name']) learning_rate_fn = learning_rate_with_decay( learning_rate_decay_type=params['learning_rate_decay_type'], batch_size=params['batch_size'], batch_denom=params['batch_size'], num_images=dataset.num_images['train'], num_epochs_per_decay=params['num_epochs_per_decay'], learning_rate_decay_factor=params['learning_rate_decay_factor'], end_learning_rate=params['end_learning_rate'], piecewise_lr_boundary_epochs=params['piecewise_lr_boundary_epochs'], piecewise_lr_decay_rates=params['piecewise_lr_decay_rates'], base_lr=params['base_learning_rate'], train_epochs=params['train_epochs'], warmup_epochs=params['lr_warmup_epochs']) if params['use_dropblock']: starter_kp = params['dropblock_kp'][0] end_kp = params['dropblock_kp'][1] batches_per_epoch = dataset.num_images['train'] / params['batch_size'] decay_steps = int(params['train_epochs'] * batches_per_epoch) keep_prob_fn = keep_prob_decay(starter_kp, end_kp, decay_steps) else: keep_prob_fn = None return run_loop_classification.resnet_model_fn( features=features, labels=labels, num_classes=dataset.num_classes, mode=mode, model_class=Model, learning_rate_fn=learning_rate_fn, keep_prob_fn=keep_prob_fn, loss_filter_fn=None, p=params)
def run(flags_obj): """Run ResNet ImageNet training and eval loop. Args: flags_obj: An object containing parsed flag values. """ if not flags_obj.eval_only: config_utils.dump_hparam() dataset = data_config.get_config(flags_obj.dataset_name) run_loop_classification.resnet_main(flags_obj, model_fns.model_fn_cls, input_fns.input_fn_cls, dataset.dataset_name, shape=[ dataset.default_image_size, dataset.default_image_size, dataset.num_channels ], num_images=dataset.num_images, zeroshot_eval=flags_obj.zeroshot_eval)
def resnet_main(flags_obj, model_function, input_function, dataset_name, shape=None, num_images=None, zeroshot_eval=False): model_helpers.apply_clean(flags.FLAGS) # Using the Winograd non-fused algorithms provides a small performance boost. os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1' # Create session config based on values of inter_op_parallelism_threads and # intra_op_parallelism_threads. Note that we default to having # allow_soft_placement = True, which is required for multi-GPU and not # harmful for other modes. session_config = config_utils.get_session_config(flags_obj) run_config = config_utils.get_run_config(flags_obj, flags_core, session_config, num_images['train']) tf.logging.info("ERR1!!!!") def gen_estimator(period=None): resnet_size = int(flags_obj.resnet_size) data_format = flags_obj.data_format batch_size = flags_obj.batch_size resnet_version = int(flags_obj.resnet_version) loss_scale = flags_core.get_loss_scale(flags_obj) dtype_tf = flags_core.get_tf_dtype(flags_obj) num_epochs_per_decay = flags_obj.num_epochs_per_decay learning_rate_decay_factor = flags_obj.learning_rate_decay_factor end_learning_rate = flags_obj.end_learning_rate learning_rate_decay_type = flags_obj.learning_rate_decay_type weight_decay = flags_obj.weight_decay zero_gamma = flags_obj.zero_gamma lr_warmup_epochs = flags_obj.lr_warmup_epochs base_learning_rate = flags_obj.base_learning_rate use_resnet_d = flags_obj.use_resnet_d use_dropblock = flags_obj.use_dropblock dropblock_kp = [float(be) for be in flags_obj.dropblock_kp] label_smoothing = flags_obj.label_smoothing momentum = flags_obj.momentum bn_momentum = flags_obj.bn_momentum train_epochs = flags_obj.train_epochs piecewise_lr_boundary_epochs = [ int(be) for be in flags_obj.piecewise_lr_boundary_epochs ] piecewise_lr_decay_rates = [ float(dr) for dr in flags_obj.piecewise_lr_decay_rates ] use_ranking_loss = flags_obj.use_ranking_loss use_se_block = flags_obj.use_se_block use_sk_block = flags_obj.use_sk_block mixup_type = flags_obj.mixup_type dataset_name = flags_obj.dataset_name kd_temp = flags_obj.kd_temp no_downsample = flags_obj.no_downsample anti_alias_filter_size = flags_obj.anti_alias_filter_size anti_alias_type = flags_obj.anti_alias_type cls_loss_type = flags_obj.cls_loss_type logit_type = flags_obj.logit_type embedding_size = flags_obj.embedding_size pool_type = flags_obj.pool_type arc_s = flags_obj.arc_s arc_m = flags_obj.arc_m bl_alpha = flags_obj.bl_alpha bl_beta = flags_obj.bl_beta exp = None if install_hyperdash and flags_obj.use_hyperdash: exp = Experiment(flags_obj.model_dir.split("/")[-1]) resnet_size = exp.param("resnet_size", int(flags_obj.resnet_size)) batch_size = exp.param("batch_size", flags_obj.batch_size) exp.param("dtype", flags_obj.dtype) learning_rate_decay_type = exp.param( "learning_rate_decay_type", flags_obj.learning_rate_decay_type) weight_decay = exp.param("weight_decay", flags_obj.weight_decay) zero_gamma = exp.param("zero_gamma", flags_obj.zero_gamma) lr_warmup_epochs = exp.param("lr_warmup_epochs", flags_obj.lr_warmup_epochs) base_learning_rate = exp.param("base_learning_rate", flags_obj.base_learning_rate) use_dropblock = exp.param("use_dropblock", flags_obj.use_dropblock) dropblock_kp = exp.param( "dropblock_kp", [float(be) for be in flags_obj.dropblock_kp]) piecewise_lr_boundary_epochs = exp.param( "piecewise_lr_boundary_epochs", [int(be) for be in flags_obj.piecewise_lr_boundary_epochs]) piecewise_lr_decay_rates = exp.param( "piecewise_lr_decay_rates", [float(dr) for dr in flags_obj.piecewise_lr_decay_rates]) mixup_type = exp.param("mixup_type", flags_obj.mixup_type) dataset_name = exp.param("dataset_name", flags_obj.dataset_name) exp.param("autoaugment_type", flags_obj.autoaugment_type) classifier = tf.estimator.Estimator( model_fn=model_function, model_dir=flags_obj.model_dir, config=run_config, params={ 'resnet_size': resnet_size, 'data_format': data_format, 'batch_size': batch_size, 'resnet_version': resnet_version, 'loss_scale': loss_scale, 'dtype': dtype_tf, 'num_epochs_per_decay': num_epochs_per_decay, 'learning_rate_decay_factor': learning_rate_decay_factor, 'end_learning_rate': end_learning_rate, 'learning_rate_decay_type': learning_rate_decay_type, 'weight_decay': weight_decay, 'zero_gamma': zero_gamma, 'lr_warmup_epochs': lr_warmup_epochs, 'base_learning_rate': base_learning_rate, 'use_resnet_d': use_resnet_d, 'use_dropblock': use_dropblock, 'dropblock_kp': dropblock_kp, 'label_smoothing': label_smoothing, 'momentum': momentum, 'bn_momentum': bn_momentum, 'embedding_size': embedding_size, 'train_epochs': train_epochs, 'piecewise_lr_boundary_epochs': piecewise_lr_boundary_epochs, 'piecewise_lr_decay_rates': piecewise_lr_decay_rates, 'with_drawing_bbox': flags_obj.with_drawing_bbox, 'use_ranking_loss': use_ranking_loss, 'use_se_block': use_se_block, 'use_sk_block': use_sk_block, 'mixup_type': mixup_type, 'kd_temp': kd_temp, 'no_downsample': no_downsample, 'dataset_name': dataset_name, 'anti_alias_filter_size': anti_alias_filter_size, 'anti_alias_type': anti_alias_type, 'cls_loss_type': cls_loss_type, 'logit_type': logit_type, 'arc_s': arc_s, 'arc_m': arc_m, 'pool_type': pool_type, 'bl_alpha': bl_alpha, 'bl_beta': bl_beta, 'train_steps': total_train_steps, }) return classifier, exp run_params = { 'batch_size': flags_obj.batch_size, 'dtype': flags_core.get_tf_dtype(flags_obj), 'resnet_size': flags_obj.resnet_size, 'resnet_version': flags_obj.resnet_version, 'synthetic_data': flags_obj.use_synthetic_data, 'train_epochs': flags_obj.train_epochs, } if flags_obj.use_synthetic_data: dataset_name = dataset_name + '-synthetic' benchmark_logger = logger.get_benchmark_logger() benchmark_logger.log_run_info('resnet', dataset_name, run_params, test_id=flags_obj.benchmark_test_id) train_hooks = hooks_helper.get_train_hooks(flags_obj.hooks, model_dir=flags_obj.model_dir, batch_size=flags_obj.batch_size) def input_fn_train(num_epochs): return input_function(is_training=True, use_random_crop=flags_obj.training_random_crop, num_epochs=num_epochs, flags_obj=flags_obj) def input_fn_eval(): return input_function(is_training=False, use_random_crop=False, num_epochs=1, flags_obj=flags_obj) ckpt_keeper = checkpoint_utils.CheckpointKeeper( save_dir=flags_obj.model_dir, num_to_keep=flags_obj.num_best_ckpt_to_keep, keep_epoch=flags_obj.keep_ckpt_every_eval, maximize=True) if zeroshot_eval: dataset = data_config.get_config(dataset_name) model = model_fns_predict.Model( int(flags_obj.resnet_size), flags_obj.data_format, resnet_version=int(flags_obj.resnet_version), num_classes=dataset.num_classes, zero_gamma=flags_obj.zero_gamma, use_se_block=flags_obj.use_se_block, use_sk_block=flags_obj.use_sk_block, no_downsample=flags_obj.no_downsample, anti_alias_filter_size=flags_obj.anti_alias_filter_size, anti_alias_type=flags_obj.anti_alias_type, bn_momentum=flags_obj.bn_momentum, embedding_size=flags_obj.embedding_size, pool_type=flags_obj.pool_type, bl_alpha=flags_obj.bl_alpha, bl_beta=flags_obj.bl_beta, dtype=flags_core.get_tf_dtype(flags_obj), loss_type=flags_obj.cls_loss_type) def train_and_evaluate(hooks): tf.logging.info('Starting cycle: %d/%d', cycle_index, int(n_loops)) if num_train_epochs: classifier.train(input_fn=lambda: input_fn_train(num_train_epochs), hooks=hooks, steps=flags_obj.max_train_steps) tf.logging.info('Starting to evaluate.') if zeroshot_eval: tf.reset_default_graph() eval_results = recall_metric.recall_at_k( flags_obj, flags_core, input_fns.input_fn_ir_eval, model, num_images['validation'], eval_similarity=flags_obj.eval_similarity, return_embedding=True) else: eval_results = classifier.predict(input_fn=input_fn_eval) return eval_results total_train_steps = flags_obj.train_epochs * int( num_images['train'] / flags_obj.batch_size) if flags_obj.eval_only or not flags_obj.train_epochs: schedule, n_loops = [0], 1 elif flags_obj.export_only: schedule, n_loops = [], 0 else: n_loops = math.ceil(flags_obj.train_epochs / flags_obj.epochs_between_evals) schedule = [ flags_obj.epochs_between_evals for _ in range(int(n_loops)) ] schedule[-1] = flags_obj.train_epochs - sum( schedule[:-1]) # over counting. schedule = config_utils.get_epoch_schedule(flags_obj, schedule, num_images) tf.logging.info('epoch schedule:') tf.logging.info(schedule) classifier, exp = gen_estimator() if flags_obj.pretrained_model_checkpoint_path: warm_start_hook = WarmStartHook( flags_obj.pretrained_model_checkpoint_path) train_hooks.append(warm_start_hook) for cycle_index, num_train_epochs in enumerate(schedule): tf.logging.info("ERR123!!!!") eval_results = train_and_evaluate(train_hooks) return eval_results if zeroshot_eval: metric = eval_results['recall_at_1'] else: metric = eval_results['accuracy'] tf.logging.info("ERR1234!!!!") ckpt_keeper.save(metric, flags_obj.model_dir) if exp: exp.metric("accuracy", metric) benchmark_logger.log_evaluation_result(eval_results) tf.logging.info("ERR12345!!!!") if model_helpers.past_stop_threshold(flags_obj.stop_threshold, metric): break if model_helpers.past_stop_threshold(total_train_steps, eval_results['global_step']): break if exp: exp.end() if flags_obj.export_dir is not None: export_utils.export_pb(flags_core, flags_obj, shape, classifier)
def input_fn_npair_train(is_training, use_random_crop, data_dir, batch_size, train_epochs, num_gpus=0, dtype=tf.float32, with_drawing_bbox=False, autoaugment_type=None, preprocessing_type='imagenet', dct_method="", dataset_name=None): """Input function which provides batches for train or eval. See https://oss.navercorp.com/VisualSearch/food-fighters/pull/49#issue-566301. Args: is_training: A boolean denoting whether the input is for training. use_random_crop: Whether to randomly crop a training image. data_dir: The directory containing the input data. batch_size: The number of samples per batch. train_epochs: TODO num_gpus: The number of gpus used for training. dtype: Data type to use for images/features dct_method: An optional `string`. Defaults to `""`. string specifying a hint about the algorithm used for decompression. Defaults to "" which maps to a system-specific default. Currently valid values are ["INTEGER_FAST", "INTEGER_ACCURATE"]. The hint may be ignored (e.g., the internal jpeg library changes to a version that does not have that specific option.) autoaugment_type: Auto augmentation type. 'imagenet', 'svhn', 'cifar', `good` with_drawing_bbox: If True, return the dataset including raw image tensor with bbox. Returns: A dataset that can be used for iteration. """ dconf = data_config.get_config(dataset_name) all_choices_ds = [] for filename in data_util.get_filenames(True, data_dir, train_regex='train-label*'): dataset = tf.data.Dataset.from_tensors(filename) # The cycle_length isn't necessary now because there's only one tfrecord file. # Use this feature when you want to increase the number of file shards. dataset = dataset.apply( tf.contrib.data.parallel_interleave(tf.data.TFRecordDataset, cycle_length=1)) # shuffling records by class. A larger shuffle buffer's size results in better randomness, # but smaller size reduce startup time and use less memory. dataset = dataset.shuffle(buffer_size=100, seed=0) dataset = dataset.repeat() all_choices_ds.append(dataset) # A Repeat number must be mutliples of two which means anchor and positive. max_train_steps = train_epochs * int( dconf.num_images['train'] / batch_size) choice_dataset = tf.data.Dataset.range(len(all_choices_ds)).repeat( max_train_steps * 2) dataset = tf.contrib.data.choose_from_datasets(all_choices_ds, choice_dataset) return data_util.process_record_dataset_ir( dataset=dataset, is_training=is_training, batch_size=batch_size, parse_record_fn=data_util.parse_record, num_classes=dconf.num_classes, num_channels=dconf.num_channels, num_gpus=num_gpus, use_random_crop=use_random_crop, dtype=dtype, with_drawing_bbox=with_drawing_bbox, autoaugment_type=autoaugment_type, num_instances=2, preprocessing_type=preprocessing_type, is_aggregated=False, dct_method=dct_method)
def export_test(bin_export_path, flags_obj, ir_eval): ds = tf.data.Dataset.list_files(flags_obj.data_dir + '/' + flags_obj.val_regex) ds = ds.interleave(tf.data.TFRecordDataset, cycle_length=10) def parse_tfr(example_proto): feature_def = { 'image/class/label': tf.FixedLenFeature([], dtype=tf.int64, default_value=-1), 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='') } features = tf.io.parse_single_example(serialized=example_proto, features=feature_def) return features['image/encoded'], features['image/class/label'] ds = ds.map(parse_tfr) ds = ds.batch(flags_obj.val_batch_size) iterator = ds.make_one_shot_iterator() images, labels = iterator.get_next() dconf = data_config.get_config(flags_obj.dataset_name) num_val_images = dconf.num_images['validation'] if flags_obj.zeroshot_eval or ir_eval: feature_dim = flags_obj.embedding_size if flags_obj.embedding_size > 0 else flags_obj.num_features np_features = np.zeros((num_val_images, feature_dim), dtype=np.float32) np_labels = np.zeros(num_val_images, dtype=np.int64) np_i = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) tf.saved_model.load(sess=sess, export_dir=bin_export_path, tags={"serve"}) for _ in tqdm( range(int(num_val_images / flags_obj.val_batch_size) + 1)): try: np_image, np_label = sess.run([images, labels]) np_predict = sess.run( 'embedding_tensor:0', feed_dict={'input_tensor:0': np_image}) np_features[np_i:np_i + np_predict.shape[0], :] = np_predict np_labels[np_i:np_i + np_label.shape[0]] = np_label np_i += np_predict.shape[0] except tf.errors.OutOfRangeError: break assert np_i == num_val_images from sklearn.preprocessing import normalize x = normalize(np_features) np_sim = x.dot(x.T) np.fill_diagonal(np_sim, -10) # removing similarity for query. num_correct = 0 for i in range(num_val_images): cur_label = np_labels[i] rank1_label = np_labels[np.argmax(np_sim[i, :])] if rank1_label == cur_label: num_correct += 1 recall_at_1 = num_correct / num_val_images metric = recall_at_1 else: np_i = 0 correct_cnt = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) tf.saved_model.load(sess=sess, export_dir=bin_export_path, tags={"serve"}) for _ in tqdm( range(int(num_val_images / flags_obj.val_batch_size) + 1)): try: np_image, np_label = sess.run([images, labels]) np_predict = sess.run( 'ArgMax:0', feed_dict={'input_tensor:0': np_image}) np_i += np_predict.shape[0] correct_cnt += np.sum(np_predict == np_label) except tf.errors.OutOfRangeError: break assert np_i == num_val_images metric = correct_cnt / np_i return metric
def model_fn_npair(features, labels, mode, params): if int(params['resnet_size']) < 50: assert not params['use_dropblock'] assert not params['use_resnet_d'] dataset = data_config.get_config(params['dataset_name']) learning_rate_fn = learning_rate_with_decay( learning_rate_decay_type=params['learning_rate_decay_type'], batch_size=params['global_batch_size'], batch_denom=params['global_batch_size'], num_images=dataset.num_images['train'], num_epochs_per_decay=params['num_epochs_per_decay'], learning_rate_decay_factor=params['learning_rate_decay_factor'], end_learning_rate=params['end_learning_rate'], piecewise_lr_boundary_epochs=params['piecewise_lr_boundary_epochs'], piecewise_lr_decay_rates=params['piecewise_lr_decay_rates'], base_lr=params['base_learning_rate'], train_epochs=params['train_epochs'], warmup=params['lr_warmup']) if params['use_dropblock']: starter_kp = params['dropblock_kp'][0] end_kp = params['dropblock_kp'][1] batches_per_epoch = dataset.num_images['train'] / params['batch_size'] decay_steps = int(params['train_epochs'] * batches_per_epoch) keep_prob_fn = keep_prob_decay(starter_kp, end_kp, decay_steps) else: keep_prob_fn = None steps_per_epoch = int(dataset.num_images['train'] / params['global_batch_size']) return run_loop_npair.resnet_model_fn( features=features, labels=labels, mode=mode, model_class=Model, num_classes=dataset.num_classes, resnet_size=params['resnet_size'], weight_decay=params['weight_decay'], learning_rate_fn=learning_rate_fn, momentum=params['momentum'], data_format=params['data_format'], dim_features=params['dim_features'], resnet_version=params['resnet_version'], loss_scale=params['loss_scale'], loss_filter_fn=None, dtype=params['dtype'], fine_tune=params['fine_tune'], zero_gamma=params['zero_gamma'], use_resnet_d=params['use_resnet_d'], rollback_period=params['rollback_period'], rollback_lr_multiplier=params['rollback_lr_multiplier'], use_se_block=params['use_se_block'], HDML_type=params['HDML_type'], batch_size=params['batch_size'], steps_per_epoch=steps_per_epoch, optimizer_name=params['optimizer'], loss_l2_reg=params['loss_l2_reg'], Softmax_factor=params['Softmax_factor'], lr_gen=params['lr_gen'], s_lr=params['s_lr'], alpha=params['alpha'], beta=params['beta'], _lambda=params['_lambda'], keep_prob_fn=keep_prob_fn)