python_code
stringlengths
0
679k
repo_name
stringlengths
9
41
file_path
stringlengths
6
149
from __future__ import absolute_import from __future__ import division from __future__ import print_function import math import os from functools import partial from collections import Counter, OrderedDict import pickle import json import multiprocessing as mp import numpy as np from absl import flags import tensorflow as tf from vocabulary import Vocab from tensorflow.gfile import Exists as exists from tensorflow.gfile import MakeDirs as makedirs from tensorflow.gfile import Glob as glob def _preprocess(shard, train, vocab, save_dir, cutoffs, bin_sizes, bsz, tgt_len, num_core_per_host, num_shuffle): file_names = [] num_batch = 0 path = train[shard] data_shard = vocab.encode_file(path, ordered=False, add_double_eos=True) for shuffle in range(num_shuffle): basename = "train-{:03d}-{:02d}".format(shard, shuffle) print("Processing shard {} shuffle {}".format(shard, shuffle)) np.random.shuffle(data_shard) file_name, num_batch_shuffle = create_ordered_tfrecords( save_dir, basename, np.concatenate(data_shard), bsz, tgt_len, num_core_per_host, cutoffs, bin_sizes) file_names.append(file_name) num_batch += num_batch_shuffle return file_names, num_batch class Corpus(object): def __init__(self, path, dataset, *args, **kwargs): self.dataset = dataset self.vocab = Vocab(*args, **kwargs) if self.dataset in ["ptb", "wt2", "enwik8", "text8"]: self.vocab.count_file(os.path.join(path, "train.txt")) self.vocab.count_file(os.path.join(path, "valid.txt")) self.vocab.count_file(os.path.join(path, "test.txt")) elif self.dataset == "wt103": self.vocab.count_file(os.path.join(path, "train.txt")) elif self.dataset == "lm1b": train_path_pattern = os.path.join( path, "1-billion-word-language-modeling-benchmark-r13output", "training-monolingual.tokenized.shuffled", "news.en-*") train_paths = glob(train_path_pattern) # the vocab will load from file when build_vocab() is called # for train_path in sorted(train_paths): # self.vocab.count_file(train_path, verbose=True) self.vocab.build_vocab() if self.dataset in ["ptb", "wt2", "wt103"]: self.train = self.vocab.encode_file( os.path.join(path, "train.txt"), ordered=True) self.valid = self.vocab.encode_file( os.path.join(path, "valid.txt"), ordered=True) self.test = self.vocab.encode_file( os.path.join(path, "test.txt"), ordered=True) elif self.dataset in ["enwik8", "text8"]: self.train = self.vocab.encode_file( os.path.join(path, "train.txt"), ordered=True, add_eos=False) self.valid = self.vocab.encode_file( os.path.join(path, "valid.txt"), ordered=True, add_eos=False) self.test = self.vocab.encode_file( os.path.join(path, "test.txt"), ordered=True, add_eos=False) elif self.dataset == "lm1b": self.train = train_paths valid_path = os.path.join(path, "valid.txt") test_path = valid_path self.valid = self.vocab.encode_file( valid_path, ordered=True, add_double_eos=True) self.test = self.vocab.encode_file( test_path, ordered=True, add_double_eos=True) if self.dataset == "wt103": self.cutoffs = [0, 19997, 39997, 199997] + [len(self.vocab)] elif self.dataset == "lm1b": self.cutoffs = [0, 59997, 99997, 639997] + [len(self.vocab)] else: self.cutoffs = [] def convert_to_tfrecords(self, split, save_dir, bsz, tgt_len, num_core_per_host, **kwargs): FLAGS = kwargs.get('FLAGS') file_names = [] record_name = "record_info-{}.bsz-{}.tlen-{}.json".format( split, bsz, tgt_len) record_info_path = os.path.join(save_dir, record_name) if self.dataset in ["ptb", "wt2", "wt103", "enwik8", "text8"]: data = getattr(self, split) bin_sizes = get_bin_sizes( data, bsz // num_core_per_host, tgt_len, self.cutoffs) file_name, num_batch = create_ordered_tfrecords( save_dir, split, data, bsz, tgt_len, num_core_per_host, self.cutoffs, bin_sizes, num_passes=FLAGS.num_passes if split == 'train' else 1) file_names.append(file_name) elif self.dataset == "lm1b": bin_sizes = get_bin_sizes( self.valid, bsz // num_core_per_host, tgt_len, self.cutoffs) if split == "train": np.random.seed(123456) num_batch = 0 if FLAGS.num_procs > 1: _preprocess_wrapper = partial(_preprocess, train=self.train, vocab=self.vocab, save_dir=save_dir, cutoffs=self.cutoffs, bin_sizes=bin_sizes, bsz=bsz, tgt_len=tgt_len, num_core_per_host=num_core_per_host, num_shuffle=FLAGS.num_shuffle) pool = mp.Pool(processes=FLAGS.num_procs) results = pool.map(_preprocess_wrapper, range(len(self.train))) for res in results: file_names.extend(res[0]) num_batch += res[1] else: for shard, path in enumerate(self.train): data_shard = self.vocab.encode_file(path, ordered=False, add_double_eos=True) num_shuffle = FLAGS.num_shuffle for shuffle in range(num_shuffle): print("Processing shard {} shuffle {}".format(shard, shuffle)) basename = "train-{:03d}-{:02d}".format(shard, shuffle) np.random.shuffle(data_shard) file_name, num_batch_ = create_ordered_tfrecords( save_dir, basename, np.concatenate(data_shard), bsz, tgt_len, num_core_per_host, self.cutoffs, bin_sizes) file_names.append(file_name) num_batch += num_batch_ else: file_name, num_batch = create_ordered_tfrecords( save_dir, split, getattr(self, split), bsz, tgt_len, num_core_per_host, self.cutoffs, bin_sizes) file_names.append(file_name) with open(record_info_path, "w") as fp: record_info = { "filenames": file_names, "bin_sizes": bin_sizes, "num_batch": num_batch } json.dump(record_info, fp) def get_bin_sizes(data, batch_size, tgt_len, cutoffs, std_mult=[2.5, 2.5, 2.5]): """ Note: the `batch_size` here should be per-core batch size """ bin_sizes = [] def _nearest_to_eight(x): y = x - x % 8 return y + 8 if x % 8 >= 4 else max(8, y) if cutoffs: num_batch = len(data) // batch_size // tgt_len data = data[:batch_size * num_batch * tgt_len] data = data.reshape(batch_size, num_batch, tgt_len) tot = batch_size * tgt_len for b, (left, right) in enumerate(zip(cutoffs[1:-1], cutoffs[2:])): mask = (data >= left) * (data < right) percents = mask.astype(np.float64).sum(2).sum(0) / tot mean = np.mean(percents) std = np.std(percents) bin_size = int(math.ceil(tgt_len * batch_size * (mean + std_mult[b] * std))) bin_size = _nearest_to_eight(bin_size) bin_sizes.append(bin_size) return bin_sizes def _int64_feature(values): return tf.train.Feature(int64_list=tf.train.Int64List(value=values)) def _float_feature(values): return tf.train.Feature(float_list=tf.train.FloatList(value=values)) def batchify(data, batch_size, num_passes): """ if num_passes > 1 Here, we use multiple randomly shifted copies. """ if num_passes > 1: data_len = len(data) double_data = np.concatenate([data, data]) data_list = [] for i in range(num_passes): start = np.random.randint(0, data_len) data_list.append(double_data[start:start+data_len]) data = np.concatenate(data_list) num_step = len(data) // batch_size data = data[:batch_size * num_step] data = data.reshape(batch_size, num_step) return data def create_ordered_tfrecords(save_dir, basename, data, batch_size, tgt_len, num_core_per_host, cutoffs=[], bin_sizes=[], num_passes=1): file_name = "{}.bsz-{}.tlen-{}.tfrecords".format( basename, batch_size, tgt_len) save_path = os.path.join(save_dir, file_name) record_writer = tf.python_io.TFRecordWriter(save_path) batched_data = batchify(data, batch_size, num_passes) num_batch = 0 for t in range(0, batched_data.shape[1] - 1, tgt_len): cur_tgt_len = min(batched_data.shape[1] - 1 - t, tgt_len) if num_batch % 500 == 0: print(" processing batch {}".format(num_batch)) for idx in range(batch_size): inputs = batched_data[idx, t:t + cur_tgt_len] labels = batched_data[idx, t + 1:t + cur_tgt_len + 1] # features dict feature = { "inputs": _int64_feature(inputs), "labels": _int64_feature(labels), } example = tf.train.Example(features=tf.train.Features(feature=feature)) record_writer.write(example.SerializeToString()) num_batch += 1 record_writer.close() print("Done writing {}. batches: {}".format(file_name, num_batch)) return file_name, num_batch def get_lm_corpus(data_dir, dataset): fn = os.path.join(data_dir, "cache.pkl") if exists(fn): print("Loading cached dataset...") with open(fn, "rb") as fp: corpus = pickle.load(fp) else: print("Producing dataset...") kwargs = {} if dataset in ["wt103", "wt2"]: kwargs["special"] = ["<eos>"] kwargs["lower_case"] = False elif dataset == "ptb": kwargs["special"] = ["<eos>"] kwargs["lower_case"] = True elif dataset == "lm1b": kwargs["special"] = [] kwargs["lower_case"] = False kwargs["vocab_file"] = os.path.join(data_dir, "1b_word_vocab.txt") elif dataset in ["enwik8", "text8"]: pass corpus = Corpus(data_dir, dataset, **kwargs) print("Saving dataset...") with open(fn, "wb") as fp: pickle.dump(corpus, fp, protocol=2) corpus_info = { "vocab_size" : len(corpus.vocab), "cutoffs" : corpus.cutoffs, "dataset" : corpus.dataset } with open(os.path.join(data_dir, "corpus-info.json"), "w") as fp: json.dump(corpus_info, fp) return corpus def main(unused_argv): del unused_argv # Unused corpus = get_lm_corpus(FLAGS.data_dir, FLAGS.dataset) save_dir = os.path.join(FLAGS.data_dir, "tfrecords") if not exists(save_dir): makedirs(save_dir) # test mode if FLAGS.eval_batch_size > 0: corpus.convert_to_tfrecords("test", save_dir, FLAGS.eval_batch_size, FLAGS.tgt_len, FLAGS.num_core_per_host, FLAGS=FLAGS) return for split, batch_size in zip( ["train", "valid"], [FLAGS.train_batch_size // FLAGS.batch_chunk, FLAGS.valid_batch_size]): if batch_size <= 0: continue print("Converting {} set...".format(split)) corpus.convert_to_tfrecords(split, save_dir, batch_size, FLAGS.tgt_len, FLAGS.num_core_per_host, FLAGS=FLAGS) def load_record_info(record_info_dir, split, per_host_bsz, tgt_len, num_core_per_host): record_name = "record_info-{}.bsz-{}.tlen-{}.json".format( split, per_host_bsz, tgt_len) record_info_path = os.path.join(record_info_dir, record_name) with open(record_info_path, "r") as fp: record_info = json.load(fp) return record_info def get_input_fn(record_info_dir, split, per_host_bsz, tgt_len, num_core_per_host, num_hosts=1): """Creates input function.""" record_info = load_record_info(record_info_dir, split, per_host_bsz, tgt_len, num_core_per_host) file_names = record_info["filenames"] bin_sizes = record_info["bin_sizes"] num_batch = record_info["num_batch"] tf.logging.info("[{}] File names {}".format(split, file_names)) def input_fn(params): # per-core batch size per_core_bsz = params["batch_size"] // num_core_per_host # data_dir could be a remote path, e.g., a google storage url data_dir = params["data_dir"] def parser(record): # preprocess "inp_perm" and "tgt_perm" def _process_perm_feature(example, prefix): for b in range(len(bin_sizes)): cnt = example.pop("{}_cnt_{}".format(prefix, b))[0] tup = example.pop("{}_tup_{}".format(prefix, b)) tup = tf.reshape( tf.sparse_tensor_to_dense(tup), shape=[cnt, 2]) # tf.float32 perm = tf.sparse_to_dense( sparse_indices=tup, output_shape=[tgt_len, bin_sizes[b]], sparse_values=1.0, default_value=0.0) example["{}_perm_{}".format(prefix, b)] = perm # whether allow the last batch with a potentially shorter length record_spec = { "inputs": tf.VarLenFeature(tf.int64), "labels": tf.VarLenFeature(tf.int64), } # retrieve serialized example example = tf.parse_single_example( serialized=record, features=record_spec) # cast int64 into int32 # cast sparse to dense for key in list(example.keys()): val = example[key] if tf.keras.backend.is_sparse(val): val = tf.sparse.to_dense(val) if val.dtype == tf.int64: val = tf.to_int32(val) example[key] = val return example["inputs"], example["labels"] file_paths = [] for file_name in file_names: file_path = os.path.join(data_dir, file_name) file_paths.append(file_path) if split == "train": dataset = tf.data.Dataset.from_tensor_slices(file_paths) if len(file_paths) > 1: dataset = dataset.shuffle(len(file_paths)).repeat() dataset = tf.data.TFRecordDataset(dataset) elif num_hosts > 1: host_id = params["context"].current_host # drop the remaining batches num_batch_per_host = num_batch // num_hosts my_start_sample_id = (host_id * num_batch_per_host * num_core_per_host * per_core_bsz) my_sample_num = num_batch_per_host * num_core_per_host * per_core_bsz dataset = tf.data.TFRecordDataset(dataset).skip( my_start_sample_id).take(my_sample_num) else: dataset = tf.data.TFRecordDataset(dataset) if num_core_per_host > 1: import horovod.tensorflow as hvd dataset = dataset.shard(hvd.size(), hvd.rank()) dataset = dataset.map(parser).cache().repeat() dataset = dataset.batch(per_core_bsz, drop_remainder=True) dataset = dataset.prefetch(num_core_per_host * per_core_bsz) else: # do not shuffle, repeat or cache in evaluation dataset = tf.data.Dataset.from_tensor_slices(file_paths) dataset = tf.data.TFRecordDataset(dataset) dataset = dataset.map(parser) dataset = dataset.batch(per_core_bsz, drop_remainder=True) return dataset if split == "train" and num_hosts > 1: record_info["num_batch"] = num_batch // num_hosts return input_fn, record_info def get_corpus_info(corpus_info_path): with open(corpus_info_path, "r") as fp: corpus_info = json.load(fp) return corpus_info if __name__ == "__main__": FLAGS = flags.FLAGS flags.DEFINE_string("data_dir", None, help="Location of the data corpus") flags.DEFINE_enum("dataset", "wt103", ["ptb", "wt2", "wt103", "lm1b", "enwik8", "text8"], help="Dataset name.") flags.DEFINE_integer("train_batch_size", 256, help="train batch size each host") flags.DEFINE_integer("valid_batch_size", 256, help="valid batch size each host") flags.DEFINE_integer("eval_batch_size", 16, help="If > 0, enter test mode and process test set only." "Otherwise, process train and dev sets only.") flags.DEFINE_integer("tgt_len", 70, help="number of tokens to predict") flags.DEFINE_integer("max_batch", -1, help="run in debug mode") flags.DEFINE_integer("num_core_per_host", 8, help="number of GPUs per host") flags.DEFINE_bool("debug", default=False, help="Process only the first batch without shuffle for lm1b.") flags.DEFINE_integer("num_procs", 1, help="number of processes") flags.DEFINE_integer("num_passes", 10, help="number of passes") flags.DEFINE_integer("num_shuffle", 4, help="number of shuffles for lm1b") flags.DEFINE_integer("batch_chunk", 1, help="number of accumulation steps") tf.app.run(main)
DeepLearningExamples-master
TensorFlow/LanguageModeling/Transformer-XL/tf/data_utils.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import math import time from absl import flags import absl.logging as _logging # pylint: disable=unused-import import tensorflow as tf import horovod.tensorflow as hvd import model import data_utils import lamb import dllogger from exp_utils import AverageMeter, setup_dllogger import numpy as np flags.DEFINE_bool('horovod', True, 'Use Horovod ') # Experiment (data/checkpoint/directory) config flags.DEFINE_string("raport_file", default="summary.json", help="Path to dlloger json") flags.DEFINE_string("data_dir", default="", help="Path to tf-records directory.") flags.DEFINE_string("record_info_dir", default="", help="Path to local directory containing filenames.txt.") flags.DEFINE_string("corpus_info_path", default="", help="Path to corpus-info.json file.") flags.DEFINE_string("model_dir", default="LM-TFM", help="Estimator model_dir.") flags.DEFINE_bool("do_train", default=True, help="Whether to run training.") flags.DEFINE_bool("do_eval", default=False, help="Whether to run eval on the dev set.") flags.DEFINE_string("eval_ckpt_path", None, help="Checkpoint path for do_test evaluation." "If set, model_dir will be ignored." "If unset, will use the latest ckpt in model_dir.") flags.DEFINE_bool("amp", default=False, help="Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.") flags.DEFINE_bool("jit_optimizer", default=True, help="Whether to enable XLA on optimizer") # Optimization config flags.DEFINE_float("learning_rate", default=0.01, help="Maximum learning rate.") flags.DEFINE_float("clip", default=0.25, help="Gradient clipping value.") # for cosine decay flags.DEFINE_float("min_lr_ratio", default=0.1, help="Minimum ratio learning rate.") flags.DEFINE_integer("warmup_steps", default=1000, help="Number of steps for linear lr warmup.") # Training config flags.DEFINE_integer("train_batch_size", default=256, help="Size of train batch.") flags.DEFINE_integer("eval_batch_size", default=16, help="Size of valid batch.") flags.DEFINE_integer("train_steps", default=40000, help="Total number of training steps.") flags.DEFINE_integer("log_interval", default=100, help="Number of iterations per repeat loop.") flags.DEFINE_integer("save_steps", default=5000, help="number of steps for model checkpointing.") flags.DEFINE_integer("batch_chunk", default=1, help="Number of accumulation steps.") # Evaluation config flags.DEFINE_integer("max_eval_batch", default=-1, help="Set -1 to turn off. Only used in test mode.") flags.DEFINE_string("eval_split", "valid", help="Which data split to evaluate.") flags.DEFINE_list("percentiles", default=['90', '95', '99'], help="percentiles for latency confidence intervals") # Model config flags.DEFINE_integer("tgt_len", default=192, help="Number of steps to predict") flags.DEFINE_integer("mem_len", default=192, help="Number of steps to cache") flags.DEFINE_bool("same_length", default=False, help="Same length attention") flags.DEFINE_integer("clamp_len", default=-1, help="Clamp length") flags.DEFINE_integer("n_layer", default=16, help="Number of layers.") flags.DEFINE_integer("d_model", default=512, help="Dimension of the model.") flags.DEFINE_integer("d_embed", default=512, help="Dimension of the embeddings.") flags.DEFINE_integer("n_head", default=8, help="Number of attention heads.") flags.DEFINE_integer("d_head", default=64, help="Dimension of each attention head.") flags.DEFINE_integer("d_inner", default=2048, help="Dimension of inner hidden size in positionwise feed-forward.") flags.DEFINE_float("dropout", default=0.1, help="Dropout rate.") flags.DEFINE_float("dropatt", default=0.0, help="Attention dropout rate.") flags.DEFINE_bool("untie_r", default=False, help="untie r_w_bias and r_r_bias") # Adaptive Softmax / Embedding flags.DEFINE_bool("tie_weight", default=True, help="Tie embedding and softmax weight.") flags.DEFINE_integer("div_val", default=1, help="Divide the embedding size by this val for each bin") flags.DEFINE_bool("proj_share_all_but_first", default=False, help="True to share all but first projs, False not to share.") flags.DEFINE_bool("proj_same_dim", default=True, help="Project the bin with the same dimension.") # Parameter initialization flags.DEFINE_enum("init", default="normal", enum_values=["normal", "uniform"], help="Initialization method.") flags.DEFINE_float("init_std", default=0.02, help="Initialization std when init is normal.") flags.DEFINE_float("proj_init_std", default=0.01, help="Initialization std for embedding projection.") flags.DEFINE_float("init_range", default=0.1, help="Initialization std when init is uniform.") FLAGS = flags.FLAGS def get_model_fn(n_token, cutoffs): def model_fn(inp, tgt, mems, is_training): inp = tf.transpose(inp, [1, 0]) tgt = tf.transpose(tgt, [1, 0]) if FLAGS.init == "uniform": initializer = tf.initializers.random_uniform( minval=-FLAGS.init_range, maxval=FLAGS.init_range, seed=None) elif FLAGS.init == "normal": initializer = tf.initializers.random_normal( stddev=FLAGS.init_std, seed=None) proj_initializer = tf.initializers.random_normal( stddev=FLAGS.proj_init_std, seed=None) tie_projs = [False for _ in range(len(cutoffs) + 1)] if FLAGS.proj_share_all_but_first: for i in range(1, len(tie_projs)): tie_projs[i] = True loss, new_mems = model.transformer( dec_inp=inp, target=tgt, mems=mems, n_token=n_token, n_layer=FLAGS.n_layer, d_model=FLAGS.d_model, d_embed=FLAGS.d_embed, n_head=FLAGS.n_head, d_head=FLAGS.d_head, d_inner=FLAGS.d_inner, dropout=FLAGS.dropout, dropatt=FLAGS.dropatt, initializer=initializer, proj_initializer=proj_initializer, is_training=is_training, mem_len=FLAGS.mem_len, cutoffs=cutoffs, div_val=FLAGS.div_val, tie_projs=tie_projs, input_perms=None, target_perms=None, head_target=None, same_length=FLAGS.same_length, clamp_len=FLAGS.clamp_len, untie_r=FLAGS.untie_r, proj_same_dim=FLAGS.proj_same_dim) # number of parameters num_params = sum([np.prod(v.shape) for v in tf.trainable_variables()]) tf.logging.info('#params: {}'.format(num_params)) if is_training: all_vars = tf.trainable_variables() return loss, new_mems, all_vars else: return loss, new_mems return model_fn def single_core_graph(n_token, cutoffs, is_training, inp, tgt, mems): model_fn = get_model_fn( n_token=n_token, cutoffs=cutoffs) model_ret = model_fn( inp=inp, tgt=tgt, mems=mems, is_training=is_training) return model_ret def train(n_token, cutoffs, rank, local_rank, num_core_per_host): meters = {} warmup = 3 meters['train_throughput'] = AverageMeter(warmup=warmup) train_batch_size = FLAGS.train_batch_size // FLAGS.batch_chunk ##### Get input function and model function train_input_fn, train_record_info = data_utils.get_input_fn( record_info_dir=FLAGS.record_info_dir, split="train", per_host_bsz=train_batch_size, tgt_len=FLAGS.tgt_len, num_core_per_host=num_core_per_host, num_hosts=1) tf.logging.info("num of batches {}".format(train_record_info["num_batch"])) ##### Create computational graph train_set = train_input_fn({ "batch_size": train_batch_size, "data_dir": FLAGS.data_dir}) inputs, labels = train_set.make_one_shot_iterator().get_next() per_core_bsz = train_batch_size // num_core_per_host with tf.variable_scope(tf.get_variable_scope()): mems = [tf.Variable(tf.zeros([FLAGS.mem_len, per_core_bsz, FLAGS.d_model], tf.float32), trainable=False) for _ in range(FLAGS.n_layer)] loss, new_mems, all_vars = single_core_graph( n_token=n_token, cutoffs=cutoffs, is_training=True, inp=inputs, tgt=labels, mems=mems) assign_mems = [mems[i].assign(new_mems[i]) for i in range(FLAGS.n_layer)] target_tokens = tf.size(labels) ## configure the optimizer global_step = tf.train.get_or_create_global_step() # warmup stage: increase the learning rate linearly if FLAGS.warmup_steps > 0: warmup_lr = tf.to_float(global_step) / tf.to_float(FLAGS.warmup_steps) \ * FLAGS.learning_rate else: warmup_lr = 0.0 # decay stage: decay the learning rate using the cosine schedule decay_lr = tf.train.cosine_decay( FLAGS.learning_rate, global_step=global_step-FLAGS.warmup_steps, decay_steps=FLAGS.train_steps-FLAGS.warmup_steps, alpha=FLAGS.min_lr_ratio) # choose warmup or decay learning_rate = tf.where(global_step < FLAGS.warmup_steps, warmup_lr, decay_lr) # get the train op optimizer = lamb.LAMBOptimizer(learning_rate=learning_rate) if FLAGS.horovod: optimizer = hvd.DistributedOptimizer(optimizer, sparse_as_dense=True) grads_and_vars = optimizer.compute_gradients(loss/FLAGS.batch_chunk, all_vars) grads, all_vars = zip(*grads_and_vars) accum_vars = [tf.Variable(tf.zeros_like(tv.initialized_value()), trainable=False) for tv in all_vars] in_progress = tf.get_variable(name="in_progress", shape=[], dtype=tf.bool, trainable=False, initializer=tf.zeros_initializer) accum_ops = tf.cond(in_progress, lambda: [accum_vars[i].assign_add(grad) for i, grad in enumerate(grads)], lambda: [accum_vars[i].assign(grad) for i, grad in enumerate(grads)]) with tf.control_dependencies(accum_ops + assign_mems): acc_op = in_progress.assign(tf.ones_like(in_progress)) final_accum_vars = [accum_vars[i] + gv for i,gv in enumerate(grads)] acc_clipped, acc_gnorm = tf.clip_by_global_norm(final_accum_vars, FLAGS.clip) clipped, gnorm = tf.clip_by_global_norm(grads, FLAGS.clip) acc_train_op = optimizer.apply_gradients(list(zip(acc_clipped, all_vars)), global_step) grads_and_vars = list(zip(clipped, all_vars)) if FLAGS.jit_optimizer: jit_scope = tf.contrib.compiler.jit.experimental_jit_scope with jit_scope(): train_op = optimizer.apply_gradients(grads_and_vars, global_step) else: train_op = optimizer.apply_gradients(grads_and_vars, global_step) final_op = tf.group(train_op, assign_mems) acc_final_op = tf.group(acc_train_op, assign_mems, in_progress.assign(tf.zeros_like(in_progress))) ##### Training loop saver = tf.train.Saver() gpu_options = tf.GPUOptions(allow_growth = True, visible_device_list = str(local_rank)) with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, gpu_options = gpu_options)) as sess: sess.run(tf.global_variables_initializer()) if FLAGS.horovod: sess.run(hvd.broadcast_global_variables(0)) accum = [acc_op, target_tokens] fetches = [loss, global_step, target_tokens, learning_rate, final_op if FLAGS.batch_chunk == 1 else acc_final_op] total_loss, prev_step, target_tokens = 0., -1, 0 start_time = time.time() while True: for i in range(FLAGS.batch_chunk-1): _,tt = sess.run(accum) target_tokens += tt fetched = sess.run(fetches) loss_np, curr_step, tt = fetched[:3] total_loss += loss_np target_tokens += tt if curr_step > 0 and curr_step % FLAGS.log_interval == 0: curr_loss = total_loss / (curr_step - prev_step) throughput = target_tokens * num_core_per_host / (time.time()-start_time) meters['train_throughput'].update(throughput) if rank == 0: tf.logging.info("step {} | lr {:8.9f} " "| loss {:.2f} | pplx {:>7.2f}, bpc {:>7.4f}, tok/s {:>6.0f}".format( curr_step, fetched[-2], curr_loss, math.exp(curr_loss), curr_loss / math.log(2), throughput)) dllogger_data = { 'lr': fetched[-1], 'train_loss': curr_loss, 'train_perplexity': math.exp(curr_loss), 'train_throughput': throughput, } dllogger.log(step=int(curr_step), data=dllogger_data) total_loss, prev_step, target_tokens = 0., curr_step, 0 start_time = time.time() if curr_step > 0 and curr_step % FLAGS.save_steps == 0 and rank == 0: save_path = os.path.join(FLAGS.model_dir, "model.ckpt") saver.save(sess, save_path) tf.logging.info("Model saved in path: {}".format(save_path)) if curr_step == FLAGS.train_steps: break if rank == 0: tf.logging.info("Training throughput: {:>6.0f} tok/s".format(meters['train_throughput'].avg)) summary = { 'train_throughput': meters['train_throughput'].avg, } dllogger.log(step=tuple(), data=summary) def evaluate(n_token, cutoffs): ##### Get input function and model function eval_input_fn, eval_record_info = data_utils.get_input_fn( record_info_dir=FLAGS.record_info_dir, split=FLAGS.eval_split, per_host_bsz=FLAGS.eval_batch_size, tgt_len=FLAGS.tgt_len, num_core_per_host=1, #multicore inference is not supported num_hosts=1) meters = {} warmup = 2 meters['eval_throughput'] = AverageMeter(warmup=warmup) meters['eval_latency'] = AverageMeter(warmup=warmup, keep=True) num_batch = eval_record_info["num_batch"] if FLAGS.max_eval_batch > 0: num_batch = FLAGS.max_eval_batch tf.logging.info("num of batches {}".format(num_batch)) ##### Create computational graph eval_set = eval_input_fn({ "batch_size": FLAGS.eval_batch_size, "data_dir": FLAGS.data_dir}) inputs, labels = eval_set.make_one_shot_iterator().get_next() bsz = FLAGS.eval_batch_size with tf.variable_scope(tf.get_variable_scope()): mems = [tf.placeholder(tf.float32, [FLAGS.mem_len, bsz, FLAGS.d_model]) for _ in range(FLAGS.n_layer)] loss, new_mems = single_core_graph( n_token=n_token, cutoffs=cutoffs, is_training=False, inp=inputs, tgt=labels, mems=mems) target_tokens = tf.size(labels) ##### Evaluation loop mems_np = [np.zeros([FLAGS.mem_len, bsz, FLAGS.d_model], dtype=np.float32) for layer in range(FLAGS.n_layer)] saver = tf.train.Saver() with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: sess.run(tf.global_variables_initializer()) if FLAGS.eval_ckpt_path is None: eval_ckpt_path = tf.train.latest_checkpoint(FLAGS.model_dir) else: eval_ckpt_path = FLAGS.eval_ckpt_path tf.logging.info("Evaluate {}".format(eval_ckpt_path)) if FLAGS.eval_ckpt_path != "random": saver.restore(sess, eval_ckpt_path) fetches = [loss, new_mems, target_tokens] format_str = " >> processing batch {{:{0}d}}/{{:{0}d}}".format( len(str(num_batch))) total_loss, total_cnt, target_tokens = 0, 0, 0 start_time = time.time() for step in range(num_batch): feed_dict = {} for m, m_np in zip(mems, mems_np): feed_dict[m] = m_np fetched = sess.run(fetches, feed_dict=feed_dict) loss_np, mems_np, tt = fetched target_tokens += tt cnt_np = 1 total_loss += loss_np * cnt_np total_cnt += cnt_np elapsed = time.time()-start_time throughput = target_tokens / elapsed latency = elapsed*1000 meters['eval_throughput'].update(throughput) meters['eval_latency'].update(latency) target_tokens = 0 if (step+1) % (num_batch // 10) == 0: tf.logging.info(format_str.format(step+1, num_batch)) dllogger_data = { 'eval_latency': latency, 'eval_throughput': throughput, } dllogger.log(step=step+1, data=dllogger_data) start_time = time.time() avg_loss = total_loss / total_cnt latency_data = np.array(meters['eval_latency'].vals) tf.logging.info("Evaluating with: bs {}, math {} ".format(FLAGS.eval_batch_size, "amp" if FLAGS.amp else "fp32")) tf.logging.info("| loss {:.2f} | pplx {:>7.2f}, bpc {:>7.4f}, tok/s {:>6.1f}, ms/batch {:>4.2f}".format( avg_loss, math.exp(avg_loss), avg_loss / math.log(2), meters['eval_throughput'].avg, meters['eval_latency'].avg)) summary = { 'eval_loss': avg_loss, 'eval_ppl': math.exp(avg_loss), 'eval_avg_throughput': meters['eval_throughput'].avg, 'eval_avg_latency': meters['eval_latency'].avg, } for p in FLAGS.percentiles: p = int(p) tf.logging.info("Latency {}%: {:>4.2f} ms".format( p, np.percentile(latency_data, p))) summary[f'eval_{p}%_latency'] = np.percentile(latency_data, p) dllogger.log(step=tuple(), data=summary) def main(unused_argv): rank, local_rank, num_core_per_host = 0, 0, 1 if FLAGS.horovod: hvd.init() rank = hvd.rank() local_rank = hvd.local_rank() num_core_per_host = hvd.size() #singlenode support del unused_argv # Unused tf.logging.set_verbosity(tf.logging.INFO) if FLAGS.amp: os.environ["TF_ENABLE_AUTO_MIXED_PRECISION"] = "1" else: os.environ["TF_ENABLE_AUTO_MIXED_PRECISION"] = "0" # Get corpus info corpus_info = data_utils.get_corpus_info(FLAGS.corpus_info_path) n_token = corpus_info["vocab_size"] cutoffs = corpus_info["cutoffs"][1:-1] tf.logging.info("n_token {}".format(n_token)) setup_dllogger(enabled=True, filename=FLAGS.raport_file, rank=rank) if FLAGS.do_train: train(n_token, cutoffs, rank, local_rank, num_core_per_host) if FLAGS.do_eval: evaluate(n_token, cutoffs) if __name__ == "__main__": tf.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/Transformer-XL/tf/main.py
# Copyright (c) 2020 NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import dllogger import os class AverageMeter: """ Computes and stores the average and current value """ def __init__(self, warmup=0, keep=False): self.reset() self.warmup = warmup self.keep = keep def reset(self): self.val = 0 self.avg = 0 self.sum = 0 self.count = 0 self.iters = 0 self.vals = [] def update(self, val, n=1): self.iters += 1 self.val = val if self.iters > self.warmup: self.sum += val * n self.count += n self.avg = self.sum / self.count if self.keep: self.vals.append(val) def setup_dllogger(enabled=True, filename=os.devnull, rank=0): if enabled and rank == 0: backends = [ dllogger.JSONStreamBackend( dllogger.Verbosity.VERBOSE, filename, ), ] dllogger.init(backends) else: dllogger.init([]) dllogger.metadata("eval_avg_latency", {"unit": "ms"}) dllogger.metadata("eval_ppl", {"unit": None}) dllogger.metadata("eval_avg_throughput", {"unit": "tokens/s"}) dllogger.metadata("train_throughput", {"unit": "tokens/s"})
DeepLearningExamples-master
TensorFlow/LanguageModeling/Transformer-XL/tf/exp_utils.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function from collections import Counter, OrderedDict import numpy as np import tensorflow as tf from tensorflow.gfile import Open as open from tensorflow.gfile import Exists as exists class Vocab(object): def __init__(self, special=[], min_freq=0, max_size=None, lower_case=True, delimiter=None, vocab_file=None): self.counter = Counter() self.special = special self.min_freq = min_freq self.max_size = max_size self.lower_case = lower_case self.delimiter = delimiter self.vocab_file = vocab_file def tokenize(self, line, add_eos=False, add_double_eos=False): line = line.strip() # convert to lower case if self.lower_case: line = line.lower() # empty delimiter '' will evaluate False if self.delimiter == '': symbols = line else: symbols = line.split(self.delimiter) if add_double_eos: # lm1b return ['<S>'] + symbols + ['<S>'] elif add_eos: return symbols + ['<eos>'] else: return symbols def count_file(self, path, verbose=False, add_eos=False): if verbose: print('counting file {} ...'.format(path)) assert exists(path) sents = [] with open(path, 'r') as f: for idx, line in enumerate(f): if verbose and idx > 0 and idx % 500000 == 0: print(' line {}'.format(idx)) symbols = self.tokenize(line, add_eos=add_eos) self.counter.update(symbols) sents.append(symbols) return sents def count_sents(self, sents, verbose=False): """ sents : a list of sentences, each a list of tokenized symbols """ if verbose: print('counting {} sents ...'.format(len(sents))) for idx, symbols in enumerate(sents): if verbose and idx > 0 and idx % 500000 == 0: print(' line {}'.format(idx)) self.counter.update(symbols) def _build_from_file(self, vocab_file): self.idx2sym = [] self.sym2idx = OrderedDict() with open(vocab_file, 'r') as f: for line in f: symb = line.strip().split()[0] self.add_symbol(symb) self.unk_idx = self.sym2idx['<UNK>'] def build_vocab(self): if self.vocab_file: print('building vocab from {}'.format(self.vocab_file)) self._build_from_file(self.vocab_file) print('final vocab size {}'.format(len(self))) else: print('building vocab with min_freq={}, max_size={}'.format( self.min_freq, self.max_size)) self.idx2sym = [] self.sym2idx = OrderedDict() for sym in self.special: self.add_special(sym) for sym, cnt in self.counter.most_common(self.max_size): if cnt < self.min_freq: break self.add_symbol(sym) print('final vocab size {} from {} unique tokens'.format( len(self), len(self.counter))) def encode_file(self, path, ordered=False, verbose=False, add_eos=True, add_double_eos=False): if verbose: print('encoding file {} ...'.format(path)) assert exists(path) encoded = [] with open(path, 'r') as f: for idx, line in enumerate(f): if verbose and idx > 0 and idx % 500000 == 0: print(' line {}'.format(idx)) symbols = self.tokenize(line, add_eos=add_eos, add_double_eos=add_double_eos) encoded.append(self.convert_to_nparray(symbols)) if ordered: encoded = np.concatenate(encoded) return encoded def encode_sents(self, sents, ordered=False, verbose=False): if verbose: print('encoding {} sents ...'.format(len(sents))) encoded = [] for idx, symbols in enumerate(sents): if verbose and idx > 0 and idx % 500000 == 0: print(' line {}'.format(idx)) encoded.append(self.convert_to_nparray(symbols)) if ordered: encoded = np.concatenate(encoded) return encoded def add_special(self, sym): if sym not in self.sym2idx: self.idx2sym.append(sym) self.sym2idx[sym] = len(self.idx2sym) - 1 setattr(self, '{}_idx'.format(sym.strip('<>')), self.sym2idx[sym]) def add_symbol(self, sym): if sym not in self.sym2idx: self.idx2sym.append(sym) self.sym2idx[sym] = len(self.idx2sym) - 1 def get_sym(self, idx): assert 0 <= idx < len(self), 'Index {} out of range'.format(idx) return self.idx2sym[idx] def get_idx(self, sym): if sym in self.sym2idx: return self.sym2idx[sym] else: assert hasattr(self, 'unk_idx') return self.sym2idx.get(sym, self.unk_idx) def get_symbols(self, indices): return [self.get_sym(idx) for idx in indices] def get_indices(self, symbols): return [self.get_idx(sym) for sym in symbols] def convert_to_nparray(self, symbols): nparray = np.array(self.get_indices(symbols), dtype=np.int64) return nparray def convert_to_sent(self, indices, exclude=None): if exclude is None: return ' '.join([self.get_sym(idx) for idx in indices]) else: return ' '.join([self.get_sym(idx) for idx in indices if idx not in exclude]) def __len__(self): return len(self.idx2sym)
DeepLearningExamples-master
TensorFlow/LanguageModeling/Transformer-XL/tf/vocabulary.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import collections import copy import json import math import re import six import tensorflow as tf from tensorflow.python.framework import ops from tensorflow.contrib.layers.python.layers import utils from tensorflow.contrib.framework.python.ops import variables from tensorflow.python.ops import init_ops import numpy from tensorflow.python.ops import array_ops from tensorflow.python.framework import dtypes from tensorflow.python.ops import nn def fused_layer_norm(inputs, center=True, scale=True, activation_fn=None, reuse=None, variables_collections=None, outputs_collections=None, trainable=True, begin_norm_axis=1, begin_params_axis=-1, scope=None, use_fused_batch_norm=False): with tf.variable_scope( scope, 'LayerNorm', [inputs], reuse=reuse) as sc: inputs = ops.convert_to_tensor(inputs) inputs_shape = inputs.shape inputs_rank = inputs_shape.ndims if inputs_rank is None: raise ValueError('Inputs %s has undefined rank.' % inputs.name) dtype = inputs.dtype.base_dtype if begin_norm_axis < 0: begin_norm_axis = inputs_rank + begin_norm_axis if begin_params_axis >= inputs_rank or begin_norm_axis >= inputs_rank: raise ValueError('begin_params_axis (%d) and begin_norm_axis (%d) ' 'must be < rank(inputs) (%d)' % (begin_params_axis, begin_norm_axis, inputs_rank)) params_shape = inputs_shape[begin_params_axis:] if not params_shape.is_fully_defined(): raise ValueError( 'Inputs %s: shape(inputs)[%s:] is not fully defined: %s' % (inputs.name, begin_params_axis, inputs_shape)) # Allocate parameters for the beta and gamma of the normalization. beta, gamma = None, None if center: beta_collections = utils.get_variable_collections(variables_collections, 'beta') beta = variables.model_variable( 'beta', shape=params_shape, dtype=dtype, initializer=init_ops.zeros_initializer(), collections=beta_collections, trainable=trainable) if scale: gamma_collections = utils.get_variable_collections( variables_collections, 'gamma') gamma = variables.model_variable( 'gamma', shape=params_shape, dtype=dtype, initializer=init_ops.ones_initializer(), collections=gamma_collections, trainable=trainable) if use_fused_batch_norm: # get static TensorShape if fully defined, # otherwise retrieve shape tensor norm_shape = inputs.shape[begin_norm_axis:] if norm_shape.is_fully_defined(): bn_shape = [1, -1, 1, numpy.prod(norm_shape.as_list())] else: norm_shape = tf.shape(inputs)[begin_norm_axis:] bn_shape = [1, -1, 1, tf.reduce_prod(norm_shape)] if inputs.get_shape().is_fully_defined(): outputs_shape = inputs.get_shape() else: outputs_shape = tf.shape(inputs) inputs = array_ops.reshape(inputs, bn_shape) if inputs.get_shape().is_fully_defined(): # static inputs TensorShape fully defined after reshape. ones = array_ops.ones(inputs.get_shape()[1], dtype=dtypes.float32) zeros = array_ops.zeros(inputs.get_shape()[1], dtype=dtypes.float32) else: # static inputs TensorShape NOT fully defined after reshape. # must use dynamic shape, which means these input tensors # have to be created at runtime, which causes a slowdown. scale_shape = tf.shape(inputs)[1] ones = array_ops.ones(scale_shape, dtype=dtypes.float32) zeros = array_ops.zeros(scale_shape, dtype=dtypes.float32) outputs, mean, variance = nn.fused_batch_norm( inputs, ones, zeros, epsilon=1e-4, data_format="NCHW") outputs = array_ops.reshape(outputs, outputs_shape) if center and scale: outputs = outputs * gamma + beta elif center: outputs = outputs + beta elif scale: outputs = outputs * gamma else: # Calculate the moments on the last axis (layer activations). norm_axes = list(range(begin_norm_axis, inputs_rank)) mean, variance = nn.moments(inputs, norm_axes, keep_dims=True) # Compute layer normalization using the batch_normalization function. variance_epsilon = 1e-4 outputs = nn.batch_normalization( inputs, mean, variance, offset=beta, scale=gamma, variance_epsilon=variance_epsilon) outputs.set_shape(inputs_shape) if activation_fn is not None: outputs = activation_fn(outputs) return utils.collect_named_outputs(outputs_collections, sc.name, outputs)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/fused_layer_norm.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """BERT finetuning runner with TF-Hub.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import optimization import run_classifier import tokenization import tensorflow as tf import tensorflow_hub as hub flags = tf.flags FLAGS = flags.FLAGS flags.DEFINE_string( "bert_hub_module_handle", None, "Handle for the BERT TF-Hub module.") def create_model(is_training, input_ids, input_mask, segment_ids, labels, num_labels, bert_hub_module_handle): """Creates a classification model.""" tags = set() if is_training: tags.add("train") bert_module = hub.Module(bert_hub_module_handle, tags=tags, trainable=True) bert_inputs = dict( input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids) bert_outputs = bert_module( inputs=bert_inputs, signature="tokens", as_dict=True) # In the demo, we are doing a simple classification task on the entire # segment. # # If you want to use the token-level output, use # bert_outputs["sequence_output"] instead. output_layer = bert_outputs["pooled_output"] hidden_size = output_layer.shape[-1].value output_weights = tf.get_variable( "output_weights", [num_labels, hidden_size], initializer=tf.truncated_normal_initializer(stddev=0.02)) output_bias = tf.get_variable( "output_bias", [num_labels], initializer=tf.zeros_initializer()) with tf.variable_scope("loss"): if is_training: # I.e., 0.1 dropout output_layer = tf.nn.dropout(output_layer, keep_prob=0.9) logits = tf.matmul(output_layer, output_weights, transpose_b=True) logits = tf.nn.bias_add(logits, output_bias) probabilities = tf.nn.softmax(logits, axis=-1) log_probs = tf.nn.log_softmax(logits, axis=-1) one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32) per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1) loss = tf.reduce_mean(per_example_loss) return (loss, per_example_loss, logits, probabilities) def model_fn_builder(num_labels, learning_rate, num_train_steps, num_warmup_steps, use_tpu, bert_hub_module_handle): """Returns `model_fn` closure for TPUEstimator.""" def model_fn(features, labels, mode, params): # pylint: disable=unused-argument """The `model_fn` for TPUEstimator.""" tf.logging.info("*** Features ***") for name in sorted(features.keys()): tf.logging.info(" name = %s, shape = %s" % (name, features[name].shape)) input_ids = features["input_ids"] input_mask = features["input_mask"] segment_ids = features["segment_ids"] label_ids = features["label_ids"] is_training = (mode == tf.estimator.ModeKeys.TRAIN) (total_loss, per_example_loss, logits, probabilities) = create_model( is_training, input_ids, input_mask, segment_ids, label_ids, num_labels, bert_hub_module_handle) output_spec = None if mode == tf.estimator.ModeKeys.TRAIN: train_op = optimization.create_optimizer( total_loss, learning_rate, num_train_steps, num_warmup_steps, use_tpu) output_spec = tf.contrib.tpu.TPUEstimatorSpec( mode=mode, loss=total_loss, train_op=train_op) elif mode == tf.estimator.ModeKeys.EVAL: def metric_fn(per_example_loss, label_ids, logits): predictions = tf.argmax(logits, axis=-1, output_type=tf.int32) accuracy = tf.metrics.accuracy(label_ids, predictions) loss = tf.metrics.mean(per_example_loss) return { "eval_accuracy": accuracy, "eval_loss": loss, } eval_metrics = (metric_fn, [per_example_loss, label_ids, logits]) output_spec = tf.contrib.tpu.TPUEstimatorSpec( mode=mode, loss=total_loss, eval_metrics=eval_metrics) elif mode == tf.estimator.ModeKeys.PREDICT: output_spec = tf.contrib.tpu.TPUEstimatorSpec( mode=mode, predictions={"probabilities": probabilities}) else: raise ValueError( "Only TRAIN, EVAL and PREDICT modes are supported: %s" % (mode)) return output_spec return model_fn def create_tokenizer_from_hub_module(bert_hub_module_handle): """Get the vocab file and casing info from the Hub module.""" with tf.Graph().as_default(): bert_module = hub.Module(bert_hub_module_handle) tokenization_info = bert_module(signature="tokenization_info", as_dict=True) with tf.Session() as sess: vocab_file, do_lower_case = sess.run([tokenization_info["vocab_file"], tokenization_info["do_lower_case"]]) return tokenization.FullTokenizer( vocab_file=vocab_file, do_lower_case=do_lower_case) def main(_): tf.logging.set_verbosity(tf.logging.INFO) processors = { "cola": run_classifier.ColaProcessor, "mnli": run_classifier.MnliProcessor, "mrpc": run_classifier.MrpcProcessor, } if not FLAGS.do_train and not FLAGS.do_eval: raise ValueError("At least one of `do_train` or `do_eval` must be True.") tf.gfile.MakeDirs(FLAGS.output_dir) task_name = FLAGS.task_name.lower() if task_name not in processors: raise ValueError("Task not found: %s" % (task_name)) processor = processors[task_name]() label_list = processor.get_labels() tokenizer = create_tokenizer_from_hub_module(FLAGS.bert_hub_module_handle) tpu_cluster_resolver = None if FLAGS.use_tpu and FLAGS.tpu_name: tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver( FLAGS.tpu_name, zone=FLAGS.tpu_zone, project=FLAGS.gcp_project) is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2 run_config = tf.contrib.tpu.RunConfig( cluster=tpu_cluster_resolver, master=FLAGS.master, model_dir=FLAGS.output_dir, save_checkpoints_steps=FLAGS.save_checkpoints_steps, tpu_config=tf.contrib.tpu.TPUConfig( iterations_per_loop=FLAGS.iterations_per_loop, num_shards=FLAGS.num_tpu_cores, per_host_input_for_training=is_per_host)) train_examples = None num_train_steps = None num_warmup_steps = None if FLAGS.do_train: train_examples = processor.get_train_examples(FLAGS.data_dir) num_train_steps = int( len(train_examples) / FLAGS.train_batch_size * FLAGS.num_train_epochs) num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion) model_fn = model_fn_builder( num_labels=len(label_list), learning_rate=FLAGS.learning_rate, num_train_steps=num_train_steps, num_warmup_steps=num_warmup_steps, use_tpu=FLAGS.use_tpu, bert_hub_module_handle=FLAGS.bert_hub_module_handle) # If TPU is not available, this will fall back to normal Estimator on CPU # or GPU. estimator = tf.contrib.tpu.TPUEstimator( use_tpu=FLAGS.use_tpu, model_fn=model_fn, config=run_config, train_batch_size=FLAGS.train_batch_size, eval_batch_size=FLAGS.eval_batch_size, predict_batch_size=FLAGS.predict_batch_size) if FLAGS.do_train: train_features = run_classifier.convert_examples_to_features( train_examples, label_list, FLAGS.max_seq_length, tokenizer) tf.logging.info("***** Running training *****") tf.logging.info(" Num examples = %d", len(train_examples)) tf.logging.info(" Batch size = %d", FLAGS.train_batch_size) tf.logging.info(" Num steps = %d", num_train_steps) train_input_fn = run_classifier.input_fn_builder( features=train_features, seq_length=FLAGS.max_seq_length, is_training=True, drop_remainder=True) estimator.train(input_fn=train_input_fn, max_steps=num_train_steps) if FLAGS.do_eval: eval_examples = processor.get_dev_examples(FLAGS.data_dir) eval_features = run_classifier.convert_examples_to_features( eval_examples, label_list, FLAGS.max_seq_length, tokenizer) tf.logging.info("***** Running evaluation *****") tf.logging.info(" Num examples = %d", len(eval_examples)) tf.logging.info(" Batch size = %d", FLAGS.eval_batch_size) # This tells the estimator to run through the entire set. eval_steps = None # However, if running eval on the TPU, you will need to specify the # number of steps. if FLAGS.use_tpu: # Eval will be slightly WRONG on the TPU because it will truncate # the last batch. eval_steps = int(len(eval_examples) / FLAGS.eval_batch_size) eval_drop_remainder = True if FLAGS.use_tpu else False eval_input_fn = run_classifier.input_fn_builder( features=eval_features, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=eval_drop_remainder) result = estimator.evaluate(input_fn=eval_input_fn, steps=eval_steps) output_eval_file = os.path.join(FLAGS.output_dir, "eval_results.txt") with tf.gfile.GFile(output_eval_file, "w") as writer: tf.logging.info("***** Eval results *****") for key in sorted(result.keys()): tf.logging.info(" %s = %s", key, str(result[key])) writer.write("%s = %s\n" % (key, str(result[key]))) if FLAGS.do_predict: predict_examples = processor.get_test_examples(FLAGS.data_dir) if FLAGS.use_tpu: # Discard batch remainder if running on TPU n = len(predict_examples) predict_examples = predict_examples[:(n - n % FLAGS.predict_batch_size)] predict_file = os.path.join(FLAGS.output_dir, "predict.tf_record") run_classifier.file_based_convert_examples_to_features( predict_examples, label_list, FLAGS.max_seq_length, tokenizer, predict_file) tf.logging.info("***** Running prediction*****") tf.logging.info(" Num examples = %d", len(predict_examples)) tf.logging.info(" Batch size = %d", FLAGS.predict_batch_size) predict_input_fn = run_classifier.file_based_input_fn_builder( input_file=predict_file, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=FLAGS.use_tpu) result = estimator.predict(input_fn=predict_input_fn) output_predict_file = os.path.join(FLAGS.output_dir, "test_results.tsv") with tf.gfile.GFile(output_predict_file, "w") as writer: tf.logging.info("***** Predict results *****") for prediction in result: probabilities = prediction["probabilities"] output_line = "\t".join( str(class_probability) for class_probability in probabilities) + "\n" writer.write(output_line) if __name__ == "__main__": flags.mark_flag_as_required("data_dir") flags.mark_flag_as_required("task_name") flags.mark_flag_as_required("bert_hub_module_handle") flags.mark_flag_as_required("output_dir") tf.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/run_classifier_with_tfhub.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """BERT finetuning runner.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import csv import logging import os, sys import numpy as np import tensorflow as tf sys.path.append("/workspace/bert") import modeling import optimization import tokenization import time import horovod.tensorflow as hvd from utils.utils import LogEvalRunHook, LogTrainRunHook, setup_xla_flags from utils.gpu_affinity import set_affinity import utils.dllogger_class from dllogger import Verbosity flags = tf.flags FLAGS = flags.FLAGS ## Required parameters flags.DEFINE_string( "data_dir", None, "The input data dir. Should contain the .tsv files (or other data files) " "for the task.") flags.DEFINE_string( "bert_config_file", None, "The config json file corresponding to the pre-trained BERT model. " "This specifies the model architecture.") flags.DEFINE_string("task_name", None, "The name of the task to train.") flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_string( "output_dir", None, "The output directory where the model checkpoints will be written.") ## Other parameters flags.DEFINE_string( "dllog_path", "/results/bert_dllog.json", "filename where dllogger writes to") flags.DEFINE_string( "init_checkpoint", None, "Initial checkpoint (usually from a pre-trained BERT model).") flags.DEFINE_bool( "do_lower_case", True, "Whether to lower case the input text. Should be True for uncased " "models and False for cased models.") flags.DEFINE_integer( "max_seq_length", 128, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded.") flags.DEFINE_bool("do_train", False, "Whether to run training.") flags.DEFINE_bool("do_eval", False, "Whether to run eval on the dev set.") flags.DEFINE_bool( "do_predict", False, "Whether to run the model in inference mode on the test set.") flags.DEFINE_integer("train_batch_size", 16, "Total batch size for training.") flags.DEFINE_integer("eval_batch_size", 8, "Total batch size for eval.") flags.DEFINE_integer("predict_batch_size", 8, "Total batch size for predict.") flags.DEFINE_float("learning_rate", 5e-6, "The initial learning rate for Adam.") flags.DEFINE_float("num_train_epochs", 3.0, "Total number of training epochs to perform.") flags.DEFINE_float( "warmup_proportion", 0.1, "Proportion of training to perform linear learning rate warmup for. " "E.g., 0.1 = 10% of training.") flags.DEFINE_integer("save_checkpoints_steps", 1000, "How often to save the model checkpoint.") flags.DEFINE_integer("iterations_per_loop", 1000, "How many steps to make in each estimator call.") tf.flags.DEFINE_string("master", None, "[Optional] TensorFlow master URL.") flags.DEFINE_bool("horovod", False, "Whether to use Horovod for multi-gpu runs") flags.DEFINE_bool("amp", True, "Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.") flags.DEFINE_bool("use_xla", True, "Whether to enable XLA JIT compilation.") class InputExample(object): """A single training/test example for simple sequence classification.""" def __init__(self, guid, text_a, text_b=None, label=None): """Constructs a InputExample. Args: guid: Unique id for the example. text_a: string. The untokenized text of the first sequence. For single sequence tasks, only this sequence must be specified. text_b: (Optional) string. The untokenized text of the second sequence. Only must be specified for sequence pair tasks. label: (Optional) string. The label of the example. This should be specified for train and dev examples, but not for test examples. """ self.guid = guid self.text_a = text_a self.text_b = text_b self.label = label class PaddingInputExample(object): """Fake example so the num input examples is a multiple of the batch size. When running eval/predict on the TPU, we need to pad the number of examples to be a multiple of the batch size, because the TPU requires a fixed batch size. The alternative is to drop the last batch, which is bad because it means the entire output data won't be generated. We use this class instead of `None` because treating `None` as padding battches could cause silent errors. """ class InputFeatures(object): """A single set of features of data.""" def __init__(self, input_ids, input_mask, segment_ids, label_id, is_real_example=True): self.input_ids = input_ids self.input_mask = input_mask self.segment_ids = segment_ids self.label_id = label_id self.is_real_example = is_real_example class DataProcessor(object): """Base class for data converters for sequence classification data sets.""" def get_train_examples(self, data_dir): """Gets a collection of `InputExample`s for the train set.""" raise NotImplementedError() def get_dev_examples(self, data_dir): """Gets a collection of `InputExample`s for the dev set.""" raise NotImplementedError() def get_test_examples(self, data_dir): """Gets a collection of `InputExample`s for prediction.""" raise NotImplementedError() def get_labels(self): """Gets the list of labels for this data set.""" raise NotImplementedError() @classmethod def _read_tsv(cls, input_file, quotechar=None): """Reads a tab separated value file.""" with tf.io.gfile.GFile(input_file, "r") as f: reader = csv.reader(f, delimiter="\t", quotechar=quotechar) lines = [] for line in reader: lines.append(line) return lines class BioBERTChemprotProcessor(DataProcessor): """Processor for the BioBERT data set obtained from (https://github.com/arwhirang/recursive_chemprot/tree/master/Demo/tree_LSTM/data). """ def get_train_examples(self, data_dir, file_name="trainingPosit_chem"): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, file_name)), "train") def get_dev_examples(self, data_dir, file_name="developPosit_chem"): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, file_name)), "dev") def get_test_examples(self, data_dir, file_name="testPosit_chem"): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, file_name)), "test") def get_labels(self): """See base class.""" return ["CPR:3", "CPR:4", "CPR:5", "CPR:6", "CPR:9", "False"] def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): guid = "%s-%s" % (set_type, i) if set_type == "test": text_a = tokenization.convert_to_unicode(line[1]) label = "False" else: text_a = tokenization.convert_to_unicode(line[1]) label = tokenization.convert_to_unicode(line[2]) if label == "True": label = tokenization.convert_to_unicode(line[3]) examples.append( InputExample(guid=guid, text_a=text_a, text_b=None, label=label)) return examples class _ChemProtProcessor(DataProcessor): """Processor for the ChemProt data set.""" def get_train_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "train.tsv")), "train") def get_dev_examples(self, data_dir, file_name="dev.tsv"): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, file_name)), "dev") def get_test_examples(self, data_dir, file_name="test.tsv"): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, file_name)), "test") def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): # skip header if i == 0: continue guid = line[0] text_a = tokenization.convert_to_unicode(line[1]) if set_type == "test": label = self.get_labels()[-1] else: try: label = tokenization.convert_to_unicode(line[2]) except IndexError: logging.exception(line) exit(1) examples.append(InputExample(guid=guid, text_a=text_a, text_b=None, label=label)) return examples class ChemProtProcessor(_ChemProtProcessor): def get_labels(self): """See base class.""" return ["CPR:3", "CPR:4", "CPR:5", "CPR:6", "CPR:9", "false"] class MedNLIProcessor(DataProcessor): def get_train_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "train.tsv")), "train") def get_dev_examples(self, data_dir, file_name="dev.tsv"): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, file_name)), "dev") def get_test_examples(self, data_dir, file_name="test.tsv"): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, file_name)), "test") def get_labels(self): """See base class.""" return ['contradiction', 'entailment', 'neutral'] def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): if i == 0: continue guid = line[1] text_a = tokenization.convert_to_unicode(line[2]) text_b = tokenization.convert_to_unicode(line[3]) if set_type == "test": label = self.get_labels()[-1] else: label = tokenization.convert_to_unicode(line[0]) examples.append( InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)) return examples def convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer): """Converts a single `InputExample` into a single `InputFeatures`.""" if isinstance(example, PaddingInputExample): return InputFeatures( input_ids=[0] * max_seq_length, input_mask=[0] * max_seq_length, segment_ids=[0] * max_seq_length, label_id=0, is_real_example=False) label_map = {} for (i, label) in enumerate(label_list): label_map[label] = i tokens_a = tokenizer.tokenize(example.text_a) tokens_b = None if example.text_b: tokens_b = tokenizer.tokenize(example.text_b) if tokens_b: # Modifies `tokens_a` and `tokens_b` in place so that the total # length is less than the specified length. # Account for [CLS], [SEP], [SEP] with "- 3" _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3) else: # Account for [CLS] and [SEP] with "- 2" if len(tokens_a) > max_seq_length - 2: tokens_a = tokens_a[0:(max_seq_length - 2)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens = [] segment_ids = [] tokens.append("[CLS]") segment_ids.append(0) for token in tokens_a: tokens.append(token) segment_ids.append(0) tokens.append("[SEP]") segment_ids.append(0) if tokens_b: for token in tokens_b: tokens.append(token) segment_ids.append(1) tokens.append("[SEP]") segment_ids.append(1) input_ids = tokenizer.convert_tokens_to_ids(tokens) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. input_mask = [1] * len(input_ids) # Zero-pad up to the sequence length. while len(input_ids) < max_seq_length: input_ids.append(0) input_mask.append(0) segment_ids.append(0) assert len(input_ids) == max_seq_length assert len(input_mask) == max_seq_length assert len(segment_ids) == max_seq_length label_id = label_map[example.label] if ex_index < 5: tf.compat.v1.logging.info("*** Example ***") tf.compat.v1.logging.info("guid: %s" % (example.guid)) tf.compat.v1.logging.info("tokens: %s" % " ".join( [tokenization.printable_text(x) for x in tokens])) tf.compat.v1.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids])) tf.compat.v1.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask])) tf.compat.v1.logging.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids])) tf.compat.v1.logging.info("label: %s (id = %d)" % (example.label, label_id)) feature = InputFeatures( input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids, label_id=label_id, is_real_example=True) return feature def file_based_convert_examples_to_features( examples, label_list, max_seq_length, tokenizer, output_file): """Convert a set of `InputExample`s to a TFRecord file.""" writer = tf.python_io.TFRecordWriter(output_file) for (ex_index, example) in enumerate(examples): if ex_index % 10000 == 0: tf.compat.v1.logging.info("Writing example %d of %d" % (ex_index, len(examples))) feature = convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer) def create_int_feature(values): f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return f features = collections.OrderedDict() features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_int_feature(feature.input_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) features["label_ids"] = create_int_feature([feature.label_id]) features["is_real_example"] = create_int_feature( [int(feature.is_real_example)]) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) writer.write(tf_example.SerializeToString()) writer.close() def file_based_input_fn_builder(input_file, batch_size, seq_length, is_training, drop_remainder, hvd=None): """Creates an `input_fn` closure to be passed to TPUEstimator.""" name_to_features = { "input_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "input_mask": tf.io.FixedLenFeature([seq_length], tf.int64), "segment_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "label_ids": tf.io.FixedLenFeature([], tf.int64), "is_real_example": tf.io.FixedLenFeature([], tf.int64), } def _decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.to_int32(t) example[name] = t return example def input_fn(params): """The actual input function.""" #batch_size = params["batch_size"] # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. d = tf.data.TFRecordDataset(input_file) if is_training: if hvd is not None: d = d.shard(hvd.size(), hvd.rank()) d = d.repeat() d = d.shuffle(buffer_size=100) d = d.apply( tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, drop_remainder=drop_remainder)) return d return input_fn def _truncate_seq_pair(tokens_a, tokens_b, max_length): """Truncates a sequence pair in place to the maximum length.""" # This is a simple heuristic which will always truncate the longer sequence # one token at a time. This makes more sense than truncating an equal percent # of tokens from each, since if one sequence is very short then each token # that's truncated likely contains more information than a longer sequence. while True: total_length = len(tokens_a) + len(tokens_b) if total_length <= max_length: break if len(tokens_a) > len(tokens_b): tokens_a.pop() else: tokens_b.pop() def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, labels, num_labels, use_one_hot_embeddings): """Creates a classification model.""" model = modeling.BertModel( config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings) # In the demo, we are doing a simple classification task on the entire # segment. # # If you want to use the token-level output, use model.get_sequence_output() # instead. output_layer = model.get_pooled_output() hidden_size = output_layer.shape[-1].value output_weights = tf.get_variable( "output_weights", [num_labels, hidden_size], initializer=tf.truncated_normal_initializer(stddev=0.02)) output_bias = tf.get_variable( "output_bias", [num_labels], initializer=tf.zeros_initializer()) with tf.variable_scope("loss"): if is_training: # I.e., 0.1 dropout output_layer = tf.nn.dropout(output_layer, keep_prob=0.9) logits = tf.matmul(output_layer, output_weights, transpose_b=True) logits = tf.nn.bias_add(logits, output_bias) probabilities = tf.nn.softmax(logits, axis=-1) log_probs = tf.nn.log_softmax(logits, axis=-1) one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32) per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1) loss = tf.reduce_mean(per_example_loss) return (loss, per_example_loss, logits, probabilities) def model_fn_builder(bert_config, num_labels, init_checkpoint, learning_rate=None, num_train_steps=None, num_warmup_steps=None, use_one_hot_embeddings=False, hvd=None, amp=False): """Returns `model_fn` closure for TPUEstimator.""" def model_fn(features, labels, mode, params): # pylint: disable=unused-argument """The `model_fn` for TPUEstimator.""" tf.compat.v1.logging.info("*** Features ***") for name in sorted(features.keys()): tf.compat.v1.logging.info(" name = %s, shape = %s" % (name, features[name].shape)) input_ids = features["input_ids"] input_mask = features["input_mask"] segment_ids = features["segment_ids"] label_ids = features["label_ids"] is_real_example = None if "is_real_example" in features: is_real_example = tf.cast(features["is_real_example"], dtype=tf.float32) else: is_real_example = tf.ones(tf.shape(label_ids), dtype=tf.float32) is_training = (mode == tf.estimator.ModeKeys.TRAIN) (total_loss, per_example_loss, logits, probabilities) = create_model( bert_config, is_training, input_ids, input_mask, segment_ids, label_ids, num_labels, use_one_hot_embeddings) tvars = tf.trainable_variables() initialized_variable_names = {} scaffold_fn = None if init_checkpoint and (hvd is None or hvd.rank() == 0): (assignment_map, initialized_variable_names ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" tf.compat.v1.logging.info(" name = %s, shape = %s%s", var.name, var.shape, init_string) output_spec = None if mode == tf.estimator.ModeKeys.TRAIN: train_op = optimization.create_optimizer( total_loss, learning_rate, num_train_steps, num_warmup_steps, hvd, False, amp) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, train_op=train_op) elif mode == tf.estimator.ModeKeys.EVAL: dummy_op = tf.no_op() # Need to call mixed precision graph rewrite if fp16 to enable graph rewrite if amp: loss_scaler = tf.train.experimental.FixedLossScale(1) dummy_op = tf.train.experimental.enable_mixed_precision_graph_rewrite( optimization.LAMBOptimizer(learning_rate=0.0), loss_scaler) def metric_fn(per_example_loss, label_ids, logits, is_real_example): predictions = tf.argmax(logits, axis=-1, output_type=tf.int32) accuracy = tf.metrics.accuracy( labels=label_ids, predictions=predictions, weights=is_real_example) loss = tf.metrics.mean(values=per_example_loss, weights=is_real_example) return { "eval_accuracy": accuracy, "eval_loss": loss, } eval_metric_ops = metric_fn(per_example_loss, label_ids, logits, is_real_example) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, eval_metric_ops=eval_metric_ops) else: dummy_op = tf.no_op() # Need to call mixed precision graph rewrite if fp16 to enable graph rewrite if amp: dummy_op = tf.train.experimental.enable_mixed_precision_graph_rewrite( optimization.LAMBOptimizer(learning_rate=0.0)) output_spec = tf.estimator.EstimatorSpec( mode=mode, predictions={"probabilities": probabilities})#predicts)#probabilities) return output_spec return model_fn # This function is not used by this file but is still used by the Colab and # people who depend on it. def input_fn_builder(features, seq_length, is_training, drop_remainder): """Creates an `input_fn` closure to be passed to TPUEstimator.""" all_input_ids = [] all_input_mask = [] all_segment_ids = [] all_label_ids = [] for feature in features: all_input_ids.append(feature.input_ids) all_input_mask.append(feature.input_mask) all_segment_ids.append(feature.segment_ids) all_label_ids.append(feature.label_id) def input_fn(params): """The actual input function.""" batch_size = params["batch_size"] num_examples = len(features) # This is for demo purposes and does NOT scale to large data sets. We do # not use Dataset.from_generator() because that uses tf.py_func which is # not TPU compatible. The right way to load data is with TFRecordReader. d = tf.data.Dataset.from_tensor_slices({ "input_ids": tf.constant( all_input_ids, shape=[num_examples, seq_length], dtype=tf.int32), "input_mask": tf.constant( all_input_mask, shape=[num_examples, seq_length], dtype=tf.int32), "segment_ids": tf.constant( all_segment_ids, shape=[num_examples, seq_length], dtype=tf.int32), "label_ids": tf.constant(all_label_ids, shape=[num_examples], dtype=tf.int32), }) if is_training: d = d.repeat() d = d.shuffle(buffer_size=100) d = d.batch(batch_size=batch_size, drop_remainder=drop_remainder) return d return input_fn # This function is not used by this file but is still used by the Colab and # people who depend on it. def convert_examples_to_features(examples, label_list, max_seq_length, tokenizer): """Convert a set of `InputExample`s to a list of `InputFeatures`.""" features = [] for (ex_index, example) in enumerate(examples): if ex_index % 10000 == 0: tf.compat.v1.logging.info("Writing example %d of %d" % (ex_index, len(examples))) feature = convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer) features.append(feature) return features def main(_): setup_xla_flags() tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO) dllogging = utils.dllogger_class.dllogger_class(FLAGS.dllog_path) if FLAGS.horovod: hvd.init() processors = { "chemprot": BioBERTChemprotProcessor, 'mednli': MedNLIProcessor, } tokenization.validate_case_matches_checkpoint(FLAGS.do_lower_case, FLAGS.init_checkpoint) if not FLAGS.do_train and not FLAGS.do_eval and not FLAGS.do_predict: raise ValueError( "At least one of `do_train`, `do_eval` or `do_predict' must be True.") bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file) if FLAGS.max_seq_length > bert_config.max_position_embeddings: raise ValueError( "Cannot use sequence length %d because the BERT model " "was only trained up to sequence length %d" % (FLAGS.max_seq_length, bert_config.max_position_embeddings)) tf.io.gfile.makedirs(FLAGS.output_dir) task_name = FLAGS.task_name.lower() if task_name not in processors: raise ValueError("Task not found: %s" % (task_name)) processor = processors[task_name]() label_list = processor.get_labels() tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2 master_process = True training_hooks = [] global_batch_size = FLAGS.train_batch_size hvd_rank = 0 config = tf.compat.v1.ConfigProto() if FLAGS.horovod: global_batch_size = FLAGS.train_batch_size * hvd.size() master_process = (hvd.rank() == 0) hvd_rank = hvd.rank() config.gpu_options.visible_device_list = str(hvd.local_rank()) if hvd.size() > 1: training_hooks.append(hvd.BroadcastGlobalVariablesHook(0)) if FLAGS.use_xla: config.graph_options.optimizer_options.global_jit_level = tf.compat.v1.OptimizerOptions.ON_1 if FLAGS.amp: tf.enable_resource_variables() run_config = tf.estimator.RunConfig( model_dir=FLAGS.output_dir if master_process else None, session_config=config, save_checkpoints_steps=FLAGS.save_checkpoints_steps if master_process else None, keep_checkpoint_max=1) if master_process: tf.compat.v1.logging.info("***** Configuaration *****") for key in FLAGS.__flags.keys(): tf.compat.v1.logging.info(' {}: {}'.format(key, getattr(FLAGS, key))) tf.compat.v1.logging.info("**************************") train_examples = None num_train_steps = None num_warmup_steps = None training_hooks.append(LogTrainRunHook(global_batch_size, hvd_rank)) if FLAGS.do_train: train_examples = processor.get_train_examples(FLAGS.data_dir) num_train_steps = int( len(train_examples) / global_batch_size * FLAGS.num_train_epochs) num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion) start_index = 0 end_index = len(train_examples) tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record")] if FLAGS.horovod: tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record{}".format(i)) for i in range(hvd.size())] num_examples_per_rank = len(train_examples) // hvd.size() remainder = len(train_examples) % hvd.size() if hvd.rank() < remainder: start_index = hvd.rank() * (num_examples_per_rank+1) end_index = start_index + num_examples_per_rank + 1 else: start_index = hvd.rank() * num_examples_per_rank + remainder end_index = start_index + (num_examples_per_rank) model_fn = model_fn_builder( bert_config=bert_config, num_labels=len(label_list), init_checkpoint=FLAGS.init_checkpoint, learning_rate=FLAGS.learning_rate if not FLAGS.horovod else FLAGS.learning_rate * hvd.size(), num_train_steps=num_train_steps, num_warmup_steps=num_warmup_steps, use_one_hot_embeddings=False, hvd=None if not FLAGS.horovod else hvd, amp=FLAGS.amp) estimator = tf.estimator.Estimator( model_fn=model_fn, config=run_config) if FLAGS.do_train: file_based_convert_examples_to_features( train_examples[start_index:end_index], label_list, FLAGS.max_seq_length, tokenizer, tmp_filenames[hvd_rank]) tf.compat.v1.logging.info("***** Running training *****") tf.compat.v1.logging.info(" Num examples = %d", len(train_examples)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.train_batch_size) tf.compat.v1.logging.info(" Num steps = %d", num_train_steps) train_input_fn = file_based_input_fn_builder( input_file=tmp_filenames, batch_size=FLAGS.train_batch_size, seq_length=FLAGS.max_seq_length, is_training=True, drop_remainder=True, hvd=None if not FLAGS.horovod else hvd) train_start_time = time.time() estimator.train(input_fn=train_input_fn, max_steps=num_train_steps, hooks=training_hooks) train_time_elapsed = time.time() - train_start_time train_time_wo_overhead = training_hooks[-1].total_time avg_sentences_per_second = num_train_steps * global_batch_size * 1.0 / train_time_elapsed ss_sentences_per_second = (num_train_steps - training_hooks[-1].skipped) * global_batch_size * 1.0 / train_time_wo_overhead if master_process: tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Training Time = %0.2f for Sentences = %d", train_time_elapsed, num_train_steps * global_batch_size) tf.compat.v1.logging.info("Total Training Time W/O Overhead = %0.2f for Sentences = %d", train_time_wo_overhead, (num_train_steps - training_hooks[-1].skipped) * global_batch_size) tf.compat.v1.logging.info("Throughput Average (sentences/sec) with overhead = %0.2f", avg_sentences_per_second) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_train": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") if FLAGS.do_eval and master_process: eval_examples = processor.get_dev_examples(FLAGS.data_dir) num_actual_eval_examples = len(eval_examples) eval_file = os.path.join(FLAGS.output_dir, "eval.tf_record") file_based_convert_examples_to_features( eval_examples, label_list, FLAGS.max_seq_length, tokenizer, eval_file) tf.compat.v1.logging.info("***** Running evaluation *****") tf.compat.v1.logging.info(" Num examples = %d (%d actual, %d padding)", len(eval_examples), num_actual_eval_examples, len(eval_examples) - num_actual_eval_examples) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.eval_batch_size) # This tells the estimator to run through the entire set. eval_steps = None eval_drop_remainder = False eval_input_fn = file_based_input_fn_builder( input_file=eval_file, batch_size=FLAGS.eval_batch_size, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=eval_drop_remainder) result = estimator.evaluate(input_fn=eval_input_fn, steps=eval_steps) output_eval_file = os.path.join(FLAGS.output_dir, "eval_results.txt") with tf.io.gfile.GFile(output_eval_file, "w") as writer: tf.compat.v1.logging.info("***** Eval results *****") for key in sorted(result.keys()): tf.compat.v1.logging.info(" %s = %s", key, str(result[key])) writer.write("%s = %s\n" % (key, str(result[key]))) if FLAGS.do_predict and master_process: predict_examples = processor.get_test_examples(FLAGS.data_dir) num_actual_predict_examples = len(predict_examples) predict_file = os.path.join(FLAGS.output_dir, "predict.tf_record") file_based_convert_examples_to_features(predict_examples, label_list, FLAGS.max_seq_length, tokenizer, predict_file) tf.compat.v1.logging.info("***** Running prediction*****") tf.compat.v1.logging.info(" Num examples = %d (%d actual, %d padding)", len(predict_examples), num_actual_predict_examples, len(predict_examples) - num_actual_predict_examples) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.predict_batch_size) predict_drop_remainder = False predict_input_fn = file_based_input_fn_builder( input_file=predict_file, batch_size=FLAGS.predict_batch_size, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=predict_drop_remainder) eval_hooks = [LogEvalRunHook(FLAGS.predict_batch_size)] eval_start_time = time.time() output_predict_file = os.path.join(FLAGS.output_dir, "test_results.tsv") with tf.io.gfile.GFile(output_predict_file, "w") as writer: num_written_lines = 0 tf.compat.v1.logging.info("***** Predict results *****") for prediction in estimator.predict(input_fn=predict_input_fn, hooks=eval_hooks, yield_single_examples=True): probabilities = prediction["probabilities"] output_line = "\t".join( str(class_probability) for class_probability in probabilities) + "\n" writer.write(output_line) num_written_lines += 1 assert num_written_lines == num_actual_predict_examples eval_time_elapsed = time.time() - eval_start_time time_list = eval_hooks[-1].time_list time_list.sort() # Removing outliers (init/warmup) in throughput computation. eval_time_wo_overhead = sum(time_list[:int(len(time_list) * 0.99)]) num_sentences = (int(len(time_list) * 0.99)) * FLAGS.predict_batch_size avg = np.mean(time_list) cf_50 = max(time_list[:int(len(time_list) * 0.50)]) cf_90 = max(time_list[:int(len(time_list) * 0.90)]) cf_95 = max(time_list[:int(len(time_list) * 0.95)]) cf_99 = max(time_list[:int(len(time_list) * 0.99)]) cf_100 = max(time_list[:int(len(time_list) * 1)]) ss_sentences_per_second = num_sentences * 1.0 / eval_time_wo_overhead tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Inference Time = %0.2f for Sentences = %d", eval_time_elapsed, eval_hooks[-1].count * FLAGS.predict_batch_size) tf.compat.v1.logging.info("Total Inference Time W/O Overhead = %0.2f for Sentences = %d", eval_time_wo_overhead, num_sentences) tf.compat.v1.logging.info("Summary Inference Statistics") tf.compat.v1.logging.info("Batch size = %d", FLAGS.predict_batch_size) tf.compat.v1.logging.info("Sequence Length = %d", FLAGS.max_seq_length) tf.compat.v1.logging.info("Precision = %s", "fp16" if FLAGS.amp else "fp32") tf.compat.v1.logging.info("Latency Confidence Level 50 (ms) = %0.2f", cf_50 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 90 (ms) = %0.2f", cf_90 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 95 (ms) = %0.2f", cf_95 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 99 (ms) = %0.2f", cf_99 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 100 (ms) = %0.2f", cf_100 * 1000) tf.compat.v1.logging.info("Latency Average (ms) = %0.2f", avg * 1000) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_val": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") if __name__ == "__main__": flags.mark_flag_as_required("data_dir") flags.mark_flag_as_required("task_name") flags.mark_flag_as_required("vocab_file") flags.mark_flag_as_required("bert_config_file") flags.mark_flag_as_required("output_dir") tf.compat.v1.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/run_re.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import json import random import re import modeling import six import tensorflow as tf class BertModelTest(tf.test.TestCase): class BertModelTester(object): def __init__(self, parent, batch_size=13, seq_length=7, is_training=True, use_input_mask=True, use_token_type_ids=True, vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37, hidden_act="gelu", hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, type_vocab_size=16, initializer_range=0.02, scope=None): self.parent = parent self.batch_size = batch_size self.seq_length = seq_length self.is_training = is_training self.use_input_mask = use_input_mask self.use_token_type_ids = use_token_type_ids self.vocab_size = vocab_size self.hidden_size = hidden_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.intermediate_size = intermediate_size self.hidden_act = hidden_act self.hidden_dropout_prob = hidden_dropout_prob self.attention_probs_dropout_prob = attention_probs_dropout_prob self.max_position_embeddings = max_position_embeddings self.type_vocab_size = type_vocab_size self.initializer_range = initializer_range self.scope = scope def create_model(self): input_ids = BertModelTest.ids_tensor([self.batch_size, self.seq_length], self.vocab_size) input_mask = None if self.use_input_mask: input_mask = BertModelTest.ids_tensor( [self.batch_size, self.seq_length], vocab_size=2) token_type_ids = None if self.use_token_type_ids: token_type_ids = BertModelTest.ids_tensor( [self.batch_size, self.seq_length], self.type_vocab_size) config = modeling.BertConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, initializer_range=self.initializer_range) model = modeling.BertModel( config=config, is_training=self.is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=token_type_ids, scope=self.scope) outputs = { "embedding_output": model.get_embedding_output(), "sequence_output": model.get_sequence_output(), "pooled_output": model.get_pooled_output(), "all_encoder_layers": model.get_all_encoder_layers(), } return outputs def check_output(self, result): self.parent.assertAllEqual( result["embedding_output"].shape, [self.batch_size, self.seq_length, self.hidden_size]) self.parent.assertAllEqual( result["sequence_output"].shape, [self.batch_size, self.seq_length, self.hidden_size]) self.parent.assertAllEqual(result["pooled_output"].shape, [self.batch_size, self.hidden_size]) def test_default(self): self.run_tester(BertModelTest.BertModelTester(self)) def test_config_to_json_string(self): config = modeling.BertConfig(vocab_size=99, hidden_size=37) obj = json.loads(config.to_json_string()) self.assertEqual(obj["vocab_size"], 99) self.assertEqual(obj["hidden_size"], 37) def run_tester(self, tester): with self.test_session() as sess: ops = tester.create_model() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) output_result = sess.run(ops) tester.check_output(output_result) self.assert_all_tensors_reachable(sess, [init_op, ops]) @classmethod def ids_tensor(cls, shape, vocab_size, rng=None, name=None): """Creates a random int32 tensor of the shape within the vocab size.""" if rng is None: rng = random.Random() total_dims = 1 for dim in shape: total_dims *= dim values = [] for _ in range(total_dims): values.append(rng.randint(0, vocab_size - 1)) return tf.constant(value=values, dtype=tf.int32, shape=shape, name=name) def assert_all_tensors_reachable(self, sess, outputs): """Checks that all the tensors in the graph are reachable from outputs.""" graph = sess.graph ignore_strings = [ "^.*/assert_less_equal/.*$", "^.*/dilation_rate$", "^.*/Tensordot/concat$", "^.*/Tensordot/concat/axis$", "^testing/.*$", ] ignore_regexes = [re.compile(x) for x in ignore_strings] unreachable = self.get_unreachable_ops(graph, outputs) filtered_unreachable = [] for x in unreachable: do_ignore = False for r in ignore_regexes: m = r.match(x.name) if m is not None: do_ignore = True if do_ignore: continue filtered_unreachable.append(x) unreachable = filtered_unreachable self.assertEqual( len(unreachable), 0, "The following ops are unreachable: %s" % (" ".join([x.name for x in unreachable]))) @classmethod def get_unreachable_ops(cls, graph, outputs): """Finds all of the tensors in graph that are unreachable from outputs.""" outputs = cls.flatten_recursive(outputs) output_to_op = collections.defaultdict(list) op_to_all = collections.defaultdict(list) assign_out_to_in = collections.defaultdict(list) for op in graph.get_operations(): for x in op.inputs: op_to_all[op.name].append(x.name) for y in op.outputs: output_to_op[y.name].append(op.name) op_to_all[op.name].append(y.name) if str(op.type) == "Assign": for y in op.outputs: for x in op.inputs: assign_out_to_in[y.name].append(x.name) assign_groups = collections.defaultdict(list) for out_name in assign_out_to_in.keys(): name_group = assign_out_to_in[out_name] for n1 in name_group: assign_groups[n1].append(out_name) for n2 in name_group: if n1 != n2: assign_groups[n1].append(n2) seen_tensors = {} stack = [x.name for x in outputs] while stack: name = stack.pop() if name in seen_tensors: continue seen_tensors[name] = True if name in output_to_op: for op_name in output_to_op[name]: if op_name in op_to_all: for input_name in op_to_all[op_name]: if input_name not in stack: stack.append(input_name) expanded_names = [] if name in assign_groups: for assign_name in assign_groups[name]: expanded_names.append(assign_name) for expanded_name in expanded_names: if expanded_name not in stack: stack.append(expanded_name) unreachable_ops = [] for op in graph.get_operations(): is_unreachable = False all_names = [x.name for x in op.inputs] + [x.name for x in op.outputs] for name in all_names: if name not in seen_tensors: is_unreachable = True if is_unreachable: unreachable_ops.append(op) return unreachable_ops @classmethod def flatten_recursive(cls, item): """Flattens (potentially nested) a tuple/dictionary/list to a list.""" output = [] if isinstance(item, list): output.extend(item) elif isinstance(item, tuple): output.extend(list(item)) elif isinstance(item, dict): for (_, v) in six.iteritems(item): output.append(v) else: return [item] flat_output = [] for x in output: flat_output.extend(cls.flatten_recursive(x)) return flat_output if __name__ == "__main__": tf.test.main()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/modeling_test.py
#! usr/bin/env python3 # -*- coding:utf-8 -*- """ Copyright 2018 The Google AI Language Team Authors. BASED ON Google_BERT. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import os, sys import pickle import tensorflow as tf import numpy as np sys.path.append("/workspace/bert") from biobert.conlleval import evaluate, report_notprint import modeling import optimization import tokenization import tf_metrics import time import horovod.tensorflow as hvd from utils.utils import LogEvalRunHook, LogTrainRunHook, setup_xla_flags from utils.gpu_affinity import set_affinity import utils.dllogger_class from dllogger import Verbosity flags = tf.flags FLAGS = flags.FLAGS flags.DEFINE_string( "task_name", "NER", "The name of the task to train." ) flags.DEFINE_string( "data_dir", None, "The input datadir.", ) flags.DEFINE_string( "output_dir", None, "The output directory where the model checkpoints will be written." ) flags.DEFINE_string( "bert_config_file", None, "The config json file corresponding to the pre-trained BERT model." ) flags.DEFINE_string( "vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_string( "dllog_path", "/results/bert_dllog.json", "filename where dllogger writes to") flags.DEFINE_string( "init_checkpoint", None, "Initial checkpoint (usually from a pre-trained BERT model)." ) flags.DEFINE_bool( "do_lower_case", False, "Whether to lower case the input text." ) flags.DEFINE_integer( "max_seq_length", 128, "The maximum total input sequence length after WordPiece tokenization." ) flags.DEFINE_bool( "do_train", False, "Whether to run training." ) flags.DEFINE_bool( "do_eval", False, "Whether to run eval on the dev set.") flags.DEFINE_bool( "do_predict", False, "Whether to run the model in inference mode on the test set.") flags.DEFINE_integer( "train_batch_size", 64, "Total batch size for training.") flags.DEFINE_integer( "eval_batch_size", 16, "Total batch size for eval.") flags.DEFINE_integer( "predict_batch_size", 16, "Total batch size for predict.") flags.DEFINE_float( "learning_rate", 5e-6, "The initial learning rate for Adam.") flags.DEFINE_float( "num_train_epochs", 10.0, "Total number of training epochs to perform.") flags.DEFINE_float( "warmup_proportion", 0.1, "Proportion of training to perform linear learning rate warmup for. " "E.g., 0.1 = 10% of training.") flags.DEFINE_integer( "save_checkpoints_steps", 1000, "How often to save the model checkpoint.") flags.DEFINE_integer( "iterations_per_loop", 1000, "How many steps to make in each estimator call.") tf.flags.DEFINE_string("master", None, "[Optional] TensorFlow master URL.") flags.DEFINE_bool("horovod", False, "Whether to use Horovod for multi-gpu runs") flags.DEFINE_bool("amp", True, "Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.") flags.DEFINE_bool("use_xla", True, "Whether to enable XLA JIT compilation.") class InputExample(object): """A single training/test example for simple sequence classification.""" def __init__(self, guid, text, label=None): """Constructs a InputExample. Args: guid: Unique id for the example. text_a: string. The untokenized text of the first sequence. For single sequence tasks, only this sequence must be specified. label: (Optional) string. The label of the example. This should be specified for train and dev examples, but not for test examples. """ self.guid = guid self.text = text self.label = label class InputFeatures(object): """A single set of features of data.""" def __init__(self, input_ids, input_mask, segment_ids, label_ids, ): self.input_ids = input_ids self.input_mask = input_mask self.segment_ids = segment_ids self.label_ids = label_ids # self.label_mask = label_mask class DataProcessor(object): """Base class for data converters for sequence classification data sets.""" def get_train_examples(self, data_dir): """Gets a collection of `InputExample`s for the train set.""" raise NotImplementedError() def get_dev_examples(self, data_dir): """Gets a collection of `InputExample`s for the dev set.""" raise NotImplementedError() def get_labels(self): """Gets the list of labels for this data set.""" raise NotImplementedError() @classmethod def _read_data(cls, input_file): """Reads a BIO data.""" with open(input_file, "r") as f: lines = [] words = [] labels = [] for line in f: contends = line.strip() if len(contends) == 0: assert len(words) == len(labels) if len(words) > 30: # split if the sentence is longer than 30 while len(words) > 30: tmplabel = labels[:30] for iidx in range(len(tmplabel)): if tmplabel.pop() == 'O': break l = ' '.join( [label for label in labels[:len(tmplabel) + 1] if len(label) > 0]) w = ' '.join( [word for word in words[:len(tmplabel) + 1] if len(word) > 0]) lines.append([l, w]) words = words[len(tmplabel) + 1:] labels = labels[len(tmplabel) + 1:] if len(words) == 0: continue l = ' '.join([label for label in labels if len(label) > 0]) w = ' '.join([word for word in words if len(word) > 0]) lines.append([l, w]) words = [] labels = [] continue word = line.strip().split()[0] label = line.strip().split()[-1] words.append(word) labels.append(label) return lines class BC5CDRProcessor(DataProcessor): def get_train_examples(self, data_dir): l1 = self._read_data(os.path.join(data_dir, "train.tsv")) l2 = self._read_data(os.path.join(data_dir, "devel.tsv")) return self._create_example(l1 + l2, "train") def get_dev_examples(self, data_dir, file_name="devel.tsv"): return self._create_example( self._read_data(os.path.join(data_dir, file_name)), "dev" ) def get_test_examples(self, data_dir, file_name="test.tsv"): return self._create_example( self._read_data(os.path.join(data_dir, file_name)), "test") def get_labels(self): return ["B", "I", "O", "X", "[CLS]", "[SEP]"] def _create_example(self, lines, set_type): examples = [] for (i, line) in enumerate(lines): guid = "%s-%s" % (set_type, i) text = tokenization.convert_to_unicode(line[1]) label = tokenization.convert_to_unicode(line[0]) examples.append(InputExample(guid=guid, text=text, label=label)) return examples class CLEFEProcessor(DataProcessor): def get_train_examples(self, data_dir): lines1 = self._read_data2(os.path.join(data_dir, "Training.tsv")) lines2 = self._read_data2(os.path.join(data_dir, "Development.tsv")) return self._create_example( lines1 + lines2, "train" ) def get_dev_examples(self, data_dir, file_name="Development.tsv"): return self._create_example( self._read_data2(os.path.join(data_dir, file_name)), "dev" ) def get_test_examples(self, data_dir, file_name="Test.tsv"): return self._create_example( self._read_data2(os.path.join(data_dir, file_name)), "test") def get_labels(self): return ["B", "I", "O", "X", "[CLS]", "[SEP]"] def _create_example(self, lines, set_type): examples = [] for (i, line) in enumerate(lines): guid = "%s-%s" % (set_type, i) text = tokenization.convert_to_unicode(line[1]) label = tokenization.convert_to_unicode(line[0]) examples.append(InputExample(guid=guid, text=text, label=label)) return examples @classmethod def _read_data2(cls, input_file): with tf.io.gfile.GFile(input_file, "r") as f: lines = [] words = [] labels = [] for line in f: contends = line.strip() if len(contends) == 0: assert len(words) == len(labels) if len(words) == 0: continue l = ' '.join([label for label in labels if len(label) > 0]) w = ' '.join([word for word in words if len(word) > 0]) lines.append([l, w]) words = [] labels = [] continue elif contends.startswith('###'): continue word = line.strip().split()[0] label = line.strip().split()[-1] words.append(word) labels.append(label) return lines class I2b22012Processor(CLEFEProcessor): def get_labels(self): return ['B-CLINICAL_DEPT', 'B-EVIDENTIAL', 'B-OCCURRENCE', 'B-PROBLEM', 'B-TEST', 'B-TREATMENT', 'I-CLINICAL_DEPT', 'I-EVIDENTIAL', 'I-OCCURRENCE', 'I-PROBLEM', 'I-TEST', 'I-TREATMENT', "O", "X", "[CLS]", "[SEP]"] def write_tokens(tokens, labels, mode): if mode == "test": path = os.path.join(FLAGS.output_dir, "token_" + mode + ".txt") if tf.io.gfile.exists(path): wf = tf.io.gfile.GFile(path, 'a') else: wf = tf.io.gfile.GFile(path, 'w') for token, label in zip(tokens, labels): if token != "**NULL**": wf.write(token + ' ' + str(label) + '\n') wf.close() def convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer, mode): label_map = {} for (i, label) in enumerate(label_list, 1): label_map[label] = i label2id_file = os.path.join(FLAGS.output_dir, 'label2id.pkl') if not os.path.exists(label2id_file): with open(label2id_file, 'wb') as w: pickle.dump(label_map, w) textlist = example.text.split(' ') labellist = example.label.split(' ') tokens = [] labels = [] for i, word in enumerate(textlist): token = tokenizer.tokenize(word) tokens.extend(token) label_1 = labellist[i] for m in range(len(token)): if m == 0: labels.append(label_1) else: labels.append("X") # tokens = tokenizer.tokenize(example.text) if len(tokens) >= max_seq_length - 1: tokens = tokens[0:(max_seq_length - 2)] labels = labels[0:(max_seq_length - 2)] ntokens = [] segment_ids = [] label_ids = [] ntokens.append("[CLS]") segment_ids.append(0) # append("O") or append("[CLS]") not sure! label_ids.append(label_map["[CLS]"]) for i, token in enumerate(tokens): ntokens.append(token) segment_ids.append(0) label_ids.append(label_map[labels[i]]) ntokens.append("[SEP]") segment_ids.append(0) # append("O") or append("[SEP]") not sure! label_ids.append(label_map["[SEP]"]) input_ids = tokenizer.convert_tokens_to_ids(ntokens) input_mask = [1] * len(input_ids) # label_mask = [1] * len(input_ids) while len(input_ids) < max_seq_length: input_ids.append(0) input_mask.append(0) segment_ids.append(0) # we don't concerned about it! label_ids.append(0) ntokens.append("**NULL**") # label_mask.append(0) # print(len(input_ids)) assert len(input_ids) == max_seq_length assert len(input_mask) == max_seq_length assert len(segment_ids) == max_seq_length assert len(label_ids) == max_seq_length # assert len(label_mask) == max_seq_length if ex_index < 5: tf.compat.v1.logging.info("*** Example ***") tf.compat.v1.logging.info("guid: %s" % (example.guid)) tf.compat.v1.logging.info("tokens: %s" % " ".join( [tokenization.printable_text(x) for x in tokens])) tf.compat.v1.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids])) tf.compat.v1.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask])) tf.compat.v1.logging.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids])) tf.compat.v1.logging.info("label_ids: %s" % " ".join([str(x) for x in label_ids])) # tf.compat.v1.logging.info("label_mask: %s" % " ".join([str(x) for x in label_mask])) feature = InputFeatures( input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids, label_ids=label_ids, # label_mask = label_mask ) # write_tokens(ntokens, label_ids, mode) return feature def filed_based_convert_examples_to_features( examples, label_list, max_seq_length, tokenizer, output_file, mode=None): writer = tf.python_io.TFRecordWriter(output_file) for (ex_index, example) in enumerate(examples): if ex_index % 5000 == 0: tf.compat.v1.logging.info("Writing example %d of %d" % (ex_index, len(examples))) feature = convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer, mode) def create_int_feature(values): f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return f features = collections.OrderedDict() features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_int_feature(feature.input_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) features["label_ids"] = create_int_feature(feature.label_ids) # features["label_mask"] = create_int_feature(feature.label_mask) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) writer.write(tf_example.SerializeToString()) def file_based_input_fn_builder(input_file, batch_size, seq_length, is_training, drop_remainder, hvd=None): name_to_features = { "input_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "input_mask": tf.io.FixedLenFeature([seq_length], tf.int64), "segment_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "label_ids": tf.io.FixedLenFeature([seq_length], tf.int64), # "label_ids":tf.VarLenFeature(tf.int64), # "label_mask": tf.io.FixedLenFeature([seq_length], tf.int64), } def _decode_record(record, name_to_features): example = tf.parse_single_example(record, name_to_features) for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.to_int32(t) example[name] = t return example def input_fn(params): #batch_size = params["batch_size"] d = tf.data.TFRecordDataset(input_file) if is_training: if hvd is not None: d = d.shard(hvd.size(), hvd.rank()) d = d.repeat() d = d.shuffle(buffer_size=100) d = d.apply(tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, drop_remainder=drop_remainder )) return d return input_fn def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, labels, num_labels, use_one_hot_embeddings): model = modeling.BertModel( config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings ) output_layer = model.get_sequence_output() hidden_size = output_layer.shape[-1].value output_weight = tf.get_variable( "output_weights", [num_labels, hidden_size], initializer=tf.truncated_normal_initializer(stddev=0.02) ) output_bias = tf.get_variable( "output_bias", [num_labels], initializer=tf.zeros_initializer() ) with tf.variable_scope("loss"): if is_training: output_layer = tf.nn.dropout(output_layer, keep_prob=0.9) output_layer = tf.reshape(output_layer, [-1, hidden_size]) logits = tf.matmul(output_layer, output_weight, transpose_b=True) logits = tf.nn.bias_add(logits, output_bias) logits = tf.reshape(logits, [-1, FLAGS.max_seq_length, num_labels]) # mask = tf.cast(input_mask,tf.float32) # loss = tf.contrib.seq2seq.sequence_loss(logits,labels,mask) # return (loss, logits, predict) ########################################################################## log_probs = tf.nn.log_softmax(logits, axis=-1) one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32) per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1) loss = tf.reduce_mean(per_example_loss) probabilities = tf.nn.softmax(logits, axis=-1) predict = tf.argmax(probabilities, axis=-1) return (loss, per_example_loss, logits, predict) ########################################################################## def model_fn_builder(bert_config, num_labels, init_checkpoint=None, learning_rate=None, num_train_steps=None, num_warmup_steps=None, use_one_hot_embeddings=False, hvd=None, amp=False): def model_fn(features, labels, mode, params): tf.compat.v1.logging.info("*** Features ***") for name in sorted(features.keys()): tf.compat.v1.logging.info(" name = %s, shape = %s" % (name, features[name].shape)) input_ids = features["input_ids"] input_mask = features["input_mask"] segment_ids = features["segment_ids"] label_ids = features["label_ids"] # label_mask = features["label_mask"] is_training = (mode == tf.estimator.ModeKeys.TRAIN) (total_loss, per_example_loss, logits, predicts) = create_model( bert_config, is_training, input_ids, input_mask, segment_ids, label_ids, num_labels, use_one_hot_embeddings) tvars = tf.trainable_variables() initialized_variable_names = {} scaffold_fn = None if init_checkpoint and (hvd is None or hvd.rank() == 0): (assignment_map, initialized_variable_names) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" tf.compat.v1.logging.info(" name = %s, shape = %s%s", var.name, var.shape, init_string) output_spec = None if mode == tf.estimator.ModeKeys.TRAIN: train_op = optimization.create_optimizer( total_loss, learning_rate, num_train_steps, num_warmup_steps, hvd, False, amp) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, train_op=train_op) elif mode == tf.estimator.ModeKeys.EVAL: dummy_op = tf.no_op() # Need to call mixed precision graph rewrite if fp16 to enable graph rewrite if amp: loss_scaler = tf.train.experimental.FixedLossScale(1) dummy_op = tf.train.experimental.enable_mixed_precision_graph_rewrite( optimization.LAMBOptimizer(learning_rate=0.0), loss_scaler) def metric_fn(per_example_loss, label_ids, logits): # def metric_fn(label_ids, logits): predictions = tf.argmax(logits, axis=-1, output_type=tf.int32) precision = tf_metrics.precision(label_ids, predictions, num_labels, [1, 2], average="macro") recall = tf_metrics.recall(label_ids, predictions, num_labels, [1, 2], average="macro") f = tf_metrics.f1(label_ids, predictions, num_labels, [1, 2], average="macro") # return { "precision": precision, "recall": recall, "f1": f, } eval_metric_ops = metric_fn(per_example_loss, label_ids, logits) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, eval_metric_ops=eval_metric_ops) else: dummy_op = tf.no_op() # Need to call mixed precision graph rewrite if fp16 to enable graph rewrite if amp: dummy_op = tf.train.experimental.enable_mixed_precision_graph_rewrite( optimization.LAMBOptimizer(learning_rate=0.0)) output_spec = tf.estimator.EstimatorSpec( mode=mode, predictions=predicts)#probabilities) return output_spec return model_fn def result_to_pair(predict_line, pred_ids, id2label, writer, err_writer): words = str(predict_line.text).split(' ') labels = str(predict_line.label).split(' ') if len(words) != len(labels): tf.compat.v1.logging.error('Text and label not equal') tf.compat.v1.logging.error(predict_line.text) tf.compat.v1.logging.error(predict_line.label) exit(1) # get from CLS to SEP pred_labels = [] for id in pred_ids: if id == 0: continue curr_label = id2label[id] if curr_label == '[CLS]': continue elif curr_label == '[SEP]': break elif curr_label == 'X': continue pred_labels.append(curr_label) if len(pred_labels) > len(words): err_writer.write(predict_line.guid + '\n') err_writer.write(predict_line.text + '\n') err_writer.write(predict_line.label + '\n') err_writer.write(' '.join([str(i) for i in pred_ids]) + '\n') err_writer.write(' '.join([id2label.get(i, '**NULL**') for i in pred_ids]) + '\n\n') pred_labels = pred_labels[:len(words)] elif len(pred_labels) < len(words): err_writer.write(predict_line.guid + '\n') err_writer.write(predict_line.text + '\n') err_writer.write(predict_line.label + '\n') err_writer.write(' '.join([str(i) for i in pred_ids]) + '\n') err_writer.write(' '.join([id2label.get(i, '**NULL**') for i in pred_ids]) + '\n\n') pred_labels += ['O'] * (len(words) - len(pred_labels)) for tok, label, pred_label in zip(words, labels, pred_labels): writer.write(tok + ' ' + label + ' ' + pred_label + '\n') writer.write('\n') def main(_): setup_xla_flags() tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO) dllogging = utils.dllogger_class.dllogger_class(FLAGS.dllog_path) if FLAGS.horovod: hvd.init() processors = { "bc5cdr": BC5CDRProcessor, "clefe": CLEFEProcessor, 'i2b2': I2b22012Processor } if not FLAGS.do_train and not FLAGS.do_eval and not FLAGS.do_predict: raise ValueError("At least one of `do_train` or `do_eval` must be True.") bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file) if FLAGS.max_seq_length > bert_config.max_position_embeddings: raise ValueError( "Cannot use sequence length %d because the BERT model " "was only trained up to sequence length %d" % (FLAGS.max_seq_length, bert_config.max_position_embeddings)) task_name = FLAGS.task_name.lower() if task_name not in processors: raise ValueError("Task not found: %s" % (task_name)) tf.io.gfile.makedirs(FLAGS.output_dir) processor = processors[task_name]() label_list = processor.get_labels() tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2 master_process = True training_hooks = [] global_batch_size = FLAGS.train_batch_size hvd_rank = 0 config = tf.compat.v1.ConfigProto() if FLAGS.horovod: global_batch_size = FLAGS.train_batch_size * hvd.size() master_process = (hvd.rank() == 0) hvd_rank = hvd.rank() config.gpu_options.visible_device_list = str(hvd.local_rank()) set_affinity(hvd.local_rank()) if hvd.size() > 1: training_hooks.append(hvd.BroadcastGlobalVariablesHook(0)) if FLAGS.use_xla: config.graph_options.optimizer_options.global_jit_level = tf.compat.v1.OptimizerOptions.ON_1 if FLAGS.amp: tf.enable_resource_variables() run_config = tf.estimator.RunConfig( model_dir=FLAGS.output_dir if master_process else None, session_config=config, save_checkpoints_steps=FLAGS.save_checkpoints_steps if master_process else None, keep_checkpoint_max=1) if master_process: tf.compat.v1.logging.info("***** Configuaration *****") for key in FLAGS.__flags.keys(): tf.compat.v1.logging.info(' {}: {}'.format(key, getattr(FLAGS, key))) tf.compat.v1.logging.info("**************************") train_examples = None num_train_steps = None num_warmup_steps = None training_hooks.append(LogTrainRunHook(global_batch_size, hvd_rank)) if FLAGS.do_train: train_examples = processor.get_train_examples(FLAGS.data_dir) num_train_steps = int( len(train_examples) / global_batch_size * FLAGS.num_train_epochs) num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion) start_index = 0 end_index = len(train_examples) tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record")] if FLAGS.horovod: tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record{}".format(i)) for i in range(hvd.size())] num_examples_per_rank = len(train_examples) // hvd.size() remainder = len(train_examples) % hvd.size() if hvd.rank() < remainder: start_index = hvd.rank() * (num_examples_per_rank+1) end_index = start_index + num_examples_per_rank + 1 else: start_index = hvd.rank() * num_examples_per_rank + remainder end_index = start_index + (num_examples_per_rank) model_fn = model_fn_builder( bert_config=bert_config, num_labels=len(label_list) + 1, init_checkpoint=FLAGS.init_checkpoint, learning_rate=FLAGS.learning_rate if not FLAGS.horovod else FLAGS.learning_rate * hvd.size(), num_train_steps=num_train_steps, num_warmup_steps=num_warmup_steps, use_one_hot_embeddings=False, hvd=None if not FLAGS.horovod else hvd, amp=FLAGS.amp) estimator = tf.estimator.Estimator( model_fn=model_fn, config=run_config) if FLAGS.do_train: #train_file = os.path.join(FLAGS.output_dir, "train.tf_record") #filed_based_convert_examples_to_features( # train_examples, label_list, FLAGS.max_seq_length, tokenizer, train_file) filed_based_convert_examples_to_features( train_examples[start_index:end_index], label_list, FLAGS.max_seq_length, tokenizer, tmp_filenames[hvd_rank]) tf.compat.v1.logging.info("***** Running training *****") tf.compat.v1.logging.info(" Num examples = %d", len(train_examples)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.train_batch_size) tf.compat.v1.logging.info(" Num steps = %d", num_train_steps) train_input_fn = file_based_input_fn_builder( input_file=tmp_filenames, #train_file, batch_size=FLAGS.train_batch_size, seq_length=FLAGS.max_seq_length, is_training=True, drop_remainder=True, hvd=None if not FLAGS.horovod else hvd) #estimator.train(input_fn=train_input_fn, max_steps=num_train_steps) train_start_time = time.time() estimator.train(input_fn=train_input_fn, max_steps=num_train_steps, hooks=training_hooks) train_time_elapsed = time.time() - train_start_time train_time_wo_overhead = training_hooks[-1].total_time avg_sentences_per_second = num_train_steps * global_batch_size * 1.0 / train_time_elapsed ss_sentences_per_second = (num_train_steps - training_hooks[-1].skipped) * global_batch_size * 1.0 / train_time_wo_overhead if master_process: tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Training Time = %0.2f for Sentences = %d", train_time_elapsed, num_train_steps * global_batch_size) tf.compat.v1.logging.info("Total Training Time W/O Overhead = %0.2f for Sentences = %d", train_time_wo_overhead, (num_train_steps - training_hooks[-1].skipped) * global_batch_size) tf.compat.v1.logging.info("Throughput Average (sentences/sec) with overhead = %0.2f", avg_sentences_per_second) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_train": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") if FLAGS.do_eval and master_process: eval_examples = processor.get_dev_examples(FLAGS.data_dir) eval_file = os.path.join(FLAGS.output_dir, "eval.tf_record") filed_based_convert_examples_to_features( eval_examples, label_list, FLAGS.max_seq_length, tokenizer, eval_file) tf.compat.v1.logging.info("***** Running evaluation *****") tf.compat.v1.logging.info(" Num examples = %d", len(eval_examples)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.eval_batch_size) eval_steps = None eval_drop_remainder = False eval_input_fn = file_based_input_fn_builder( input_file=eval_file, batch_size=FLAGS.eval_batch_size, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=eval_drop_remainder) result = estimator.evaluate(input_fn=eval_input_fn, steps=eval_steps) output_eval_file = os.path.join(FLAGS.output_dir, "eval_results.txt") with tf.io.gfile.GFile(output_eval_file, "w") as writer: tf.compat.v1.logging.info("***** Eval results *****") for key in sorted(result.keys()): tf.compat.v1.logging.info(" %s = %s", key, str(result[key])) dllogging.logger.log(step=(), data={key: float(str(result[key]))}, verbosity=Verbosity.DEFAULT) writer.write("%s = %s\n" % (key, str(result[key]))) if FLAGS.do_predict and master_process: predict_examples = processor.get_test_examples(FLAGS.data_dir) predict_file = os.path.join(FLAGS.output_dir, "predict.tf_record") filed_based_convert_examples_to_features(predict_examples, label_list, FLAGS.max_seq_length, tokenizer, predict_file, mode="test") with tf.io.gfile.GFile(os.path.join(FLAGS.output_dir, 'label2id.pkl'), 'rb') as rf: label2id = pickle.load(rf) id2label = {value: key for key, value in label2id.items()} token_path = os.path.join(FLAGS.output_dir, "token_test.txt") if tf.io.gfile.exists(token_path): tf.io.gfile.remove(token_path) tf.compat.v1.logging.info("***** Running prediction*****") tf.compat.v1.logging.info(" Num examples = %d", len(predict_examples)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.predict_batch_size) predict_drop_remainder = False predict_input_fn = file_based_input_fn_builder( input_file=predict_file, batch_size=FLAGS.predict_batch_size, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=predict_drop_remainder) eval_hooks = [LogEvalRunHook(FLAGS.predict_batch_size)] eval_start_time = time.time() output_predict_file = os.path.join(FLAGS.output_dir, "label_test.txt") test_labels_file = os.path.join(FLAGS.output_dir, "test_labels.txt") test_labels_err_file = os.path.join(FLAGS.output_dir, "test_labels_errs.txt") with tf.io.gfile.GFile(output_predict_file, 'w') as writer, \ tf.io.gfile.GFile(test_labels_file, 'w') as tl, \ tf.io.gfile.GFile(test_labels_err_file, 'w') as tle: print(id2label) i=0 for prediction in estimator.predict(input_fn=predict_input_fn, hooks=eval_hooks, yield_single_examples=True): output_line = "\n".join(id2label[id] for id in prediction if id != 0) + "\n" writer.write(output_line) result_to_pair(predict_examples[i], prediction, id2label, tl, tle) i = i + 1 eval_time_elapsed = time.time() - eval_start_time time_list = eval_hooks[-1].time_list time_list.sort() # Removing outliers (init/warmup) in throughput computation. eval_time_wo_overhead = sum(time_list[:int(len(time_list) * 0.99)]) num_sentences = (int(len(time_list) * 0.99)) * FLAGS.predict_batch_size avg = np.mean(time_list) cf_50 = max(time_list[:int(len(time_list) * 0.50)]) cf_90 = max(time_list[:int(len(time_list) * 0.90)]) cf_95 = max(time_list[:int(len(time_list) * 0.95)]) cf_99 = max(time_list[:int(len(time_list) * 0.99)]) cf_100 = max(time_list[:int(len(time_list) * 1)]) ss_sentences_per_second = num_sentences * 1.0 / eval_time_wo_overhead tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Inference Time = %0.2f for Sentences = %d", eval_time_elapsed, eval_hooks[-1].count * FLAGS.predict_batch_size) tf.compat.v1.logging.info("Total Inference Time W/O Overhead = %0.2f for Sentences = %d", eval_time_wo_overhead, num_sentences) tf.compat.v1.logging.info("Summary Inference Statistics") tf.compat.v1.logging.info("Batch size = %d", FLAGS.predict_batch_size) tf.compat.v1.logging.info("Sequence Length = %d", FLAGS.max_seq_length) tf.compat.v1.logging.info("Precision = %s", "fp16" if FLAGS.amp else "fp32") tf.compat.v1.logging.info("Latency Confidence Level 50 (ms) = %0.2f", cf_50 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 90 (ms) = %0.2f", cf_90 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 95 (ms) = %0.2f", cf_95 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 99 (ms) = %0.2f", cf_99 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 100 (ms) = %0.2f", cf_100 * 1000) tf.compat.v1.logging.info("Latency Average (ms) = %0.2f", avg * 1000) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_val": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info('Reading: %s', test_labels_file) with tf.io.gfile.GFile(test_labels_file, "r") as f: counts = evaluate(f) eval_result = report_notprint(counts) print(''.join(eval_result)) with tf.io.gfile.GFile(os.path.join(FLAGS.output_dir, 'test_results_conlleval.txt'), 'w') as fd: fd.write(''.join(eval_result)) if __name__ == "__main__": flags.mark_flag_as_required("data_dir") flags.mark_flag_as_required("task_name") flags.mark_flag_as_required("vocab_file") flags.mark_flag_as_required("bert_config_file") flags.mark_flag_as_required("output_dir") tf.compat.v1.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/run_ner.py
""" Multiclass from: https://github.com/guillaumegenthial/tf_metrics/blob/master/tf_metrics/__init__.py """ __author__ = "Guillaume Genthial" import numpy as np import tensorflow as tf from tensorflow.python.ops.metrics_impl import _streaming_confusion_matrix def precision(labels, predictions, num_classes, pos_indices=None, weights=None, average='micro'): """Multi-class precision metric for Tensorflow Parameters ---------- labels : Tensor of tf.int32 or tf.int64 The true labels predictions : Tensor of tf.int32 or tf.int64 The predictions, same shape as labels num_classes : int The number of classes pos_indices : list of int, optional The indices of the positive classes, default is all weights : Tensor of tf.int32, optional Mask, must be of compatible shape with labels average : str, optional 'micro': counts the total number of true positives, false positives, and false negatives for the classes in `pos_indices` and infer the metric from it. 'macro': will compute the metric separately for each class in `pos_indices` and average. Will not account for class imbalance. 'weighted': will compute the metric separately for each class in `pos_indices` and perform a weighted average by the total number of true labels for each class. Returns ------- tuple of (scalar float Tensor, update_op) """ cm, op = _streaming_confusion_matrix( labels, predictions, num_classes, weights) pr, _, _ = metrics_from_confusion_matrix( cm, pos_indices, average=average) op, _, _ = metrics_from_confusion_matrix( op, pos_indices, average=average) return (pr, op) def recall(labels, predictions, num_classes, pos_indices=None, weights=None, average='micro'): """Multi-class recall metric for Tensorflow Parameters ---------- labels : Tensor of tf.int32 or tf.int64 The true labels predictions : Tensor of tf.int32 or tf.int64 The predictions, same shape as labels num_classes : int The number of classes pos_indices : list of int, optional The indices of the positive classes, default is all weights : Tensor of tf.int32, optional Mask, must be of compatible shape with labels average : str, optional 'micro': counts the total number of true positives, false positives, and false negatives for the classes in `pos_indices` and infer the metric from it. 'macro': will compute the metric separately for each class in `pos_indices` and average. Will not account for class imbalance. 'weighted': will compute the metric separately for each class in `pos_indices` and perform a weighted average by the total number of true labels for each class. Returns ------- tuple of (scalar float Tensor, update_op) """ cm, op = _streaming_confusion_matrix( labels, predictions, num_classes, weights) _, re, _ = metrics_from_confusion_matrix( cm, pos_indices, average=average) _, op, _ = metrics_from_confusion_matrix( op, pos_indices, average=average) return (re, op) def f1(labels, predictions, num_classes, pos_indices=None, weights=None, average='micro'): return fbeta(labels, predictions, num_classes, pos_indices, weights, average) def fbeta(labels, predictions, num_classes, pos_indices=None, weights=None, average='micro', beta=1): """Multi-class fbeta metric for Tensorflow Parameters ---------- labels : Tensor of tf.int32 or tf.int64 The true labels predictions : Tensor of tf.int32 or tf.int64 The predictions, same shape as labels num_classes : int The number of classes pos_indices : list of int, optional The indices of the positive classes, default is all weights : Tensor of tf.int32, optional Mask, must be of compatible shape with labels average : str, optional 'micro': counts the total number of true positives, false positives, and false negatives for the classes in `pos_indices` and infer the metric from it. 'macro': will compute the metric separately for each class in `pos_indices` and average. Will not account for class imbalance. 'weighted': will compute the metric separately for each class in `pos_indices` and perform a weighted average by the total number of true labels for each class. beta : int, optional Weight of precision in harmonic mean Returns ------- tuple of (scalar float Tensor, update_op) """ cm, op = _streaming_confusion_matrix( labels, predictions, num_classes, weights) _, _, fbeta = metrics_from_confusion_matrix( cm, pos_indices, average=average, beta=beta) _, _, op = metrics_from_confusion_matrix( op, pos_indices, average=average, beta=beta) return (fbeta, op) def safe_div(numerator, denominator): """Safe division, return 0 if denominator is 0""" numerator, denominator = tf.to_float(numerator), tf.to_float(denominator) zeros = tf.zeros_like(numerator, dtype=numerator.dtype) denominator_is_zero = tf.equal(denominator, zeros) return tf.where(denominator_is_zero, zeros, numerator / denominator) def pr_re_fbeta(cm, pos_indices, beta=1): """Uses a confusion matrix to compute precision, recall and fbeta""" num_classes = cm.shape[0] neg_indices = [i for i in range(num_classes) if i not in pos_indices] cm_mask = np.ones([num_classes, num_classes]) cm_mask[neg_indices, neg_indices] = 0 diag_sum = tf.reduce_sum(tf.diag_part(cm * cm_mask)) cm_mask = np.ones([num_classes, num_classes]) cm_mask[:, neg_indices] = 0 tot_pred = tf.reduce_sum(cm * cm_mask) cm_mask = np.ones([num_classes, num_classes]) cm_mask[neg_indices, :] = 0 tot_gold = tf.reduce_sum(cm * cm_mask) pr = safe_div(diag_sum, tot_pred) re = safe_div(diag_sum, tot_gold) fbeta = safe_div((1. + beta**2) * pr * re, beta**2 * pr + re) return pr, re, fbeta def metrics_from_confusion_matrix(cm, pos_indices=None, average='micro', beta=1): """Precision, Recall and F1 from the confusion matrix Parameters ---------- cm : tf.Tensor of type tf.int32, of shape (num_classes, num_classes) The streaming confusion matrix. pos_indices : list of int, optional The indices of the positive classes beta : int, optional Weight of precision in harmonic mean average : str, optional 'micro', 'macro' or 'weighted' """ num_classes = cm.shape[0] if pos_indices is None: pos_indices = [i for i in range(num_classes)] if average == 'micro': return pr_re_fbeta(cm, pos_indices, beta) elif average in {'macro', 'weighted'}: precisions, recalls, fbetas, n_golds = [], [], [], [] for idx in pos_indices: pr, re, fbeta = pr_re_fbeta(cm, [idx], beta) precisions.append(pr) recalls.append(re) fbetas.append(fbeta) cm_mask = np.zeros([num_classes, num_classes]) cm_mask[idx, :] = 1 n_golds.append(tf.to_float(tf.reduce_sum(cm * cm_mask))) if average == 'macro': pr = tf.reduce_mean(precisions) re = tf.reduce_mean(recalls) fbeta = tf.reduce_mean(fbetas) return pr, re, fbeta if average == 'weighted': n_gold = tf.reduce_sum(n_golds) pr_sum = sum(p * n for p, n in zip(precisions, n_golds)) pr = safe_div(pr_sum, n_gold) re_sum = sum(r * n for r, n in zip(recalls, n_golds)) re = safe_div(re_sum, n_gold) fbeta_sum = sum(f * n for f, n in zip(fbetas, n_golds)) fbeta = safe_div(fbeta_sum, n_gold) return pr, re, fbeta else: raise NotImplementedError()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/tf_metrics.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Extract pre-computed feature vectors from BERT.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import codecs import collections import json import re import modeling import tokenization import tensorflow as tf flags = tf.flags FLAGS = flags.FLAGS flags.DEFINE_string("input_file", None, "") flags.DEFINE_string("output_file", None, "") flags.DEFINE_string("layers", "-1,-2,-3,-4", "") flags.DEFINE_string( "bert_config_file", None, "The config json file corresponding to the pre-trained BERT model. " "This specifies the model architecture.") flags.DEFINE_integer( "max_seq_length", 128, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded.") flags.DEFINE_string( "init_checkpoint", None, "Initial checkpoint (usually from a pre-trained BERT model).") flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_bool( "do_lower_case", True, "Whether to lower case the input text. Should be True for uncased " "models and False for cased models.") flags.DEFINE_integer("batch_size", 32, "Batch size for predictions.") flags.DEFINE_bool("use_tpu", False, "Whether to use TPU or GPU/CPU.") flags.DEFINE_string("master", None, "If using a TPU, the address of the master.") flags.DEFINE_integer( "num_tpu_cores", 8, "Only used if `use_tpu` is True. Total number of TPU cores to use.") flags.DEFINE_bool( "use_one_hot_embeddings", False, "If True, tf.one_hot will be used for embedding lookups, otherwise " "tf.nn.embedding_lookup will be used. On TPUs, this should be True " "since it is much faster.") class InputExample(object): def __init__(self, unique_id, text_a, text_b): self.unique_id = unique_id self.text_a = text_a self.text_b = text_b class InputFeatures(object): """A single set of features of data.""" def __init__(self, unique_id, tokens, input_ids, input_mask, input_type_ids): self.unique_id = unique_id self.tokens = tokens self.input_ids = input_ids self.input_mask = input_mask self.input_type_ids = input_type_ids def input_fn_builder(features, seq_length): """Creates an `input_fn` closure to be passed to TPUEstimator.""" all_unique_ids = [] all_input_ids = [] all_input_mask = [] all_input_type_ids = [] for feature in features: all_unique_ids.append(feature.unique_id) all_input_ids.append(feature.input_ids) all_input_mask.append(feature.input_mask) all_input_type_ids.append(feature.input_type_ids) def input_fn(params): """The actual input function.""" batch_size = params["batch_size"] num_examples = len(features) # This is for demo purposes and does NOT scale to large data sets. We do # not use Dataset.from_generator() because that uses tf.py_func which is # not TPU compatible. The right way to load data is with TFRecordReader. d = tf.data.Dataset.from_tensor_slices({ "unique_ids": tf.constant(all_unique_ids, shape=[num_examples], dtype=tf.int32), "input_ids": tf.constant( all_input_ids, shape=[num_examples, seq_length], dtype=tf.int32), "input_mask": tf.constant( all_input_mask, shape=[num_examples, seq_length], dtype=tf.int32), "input_type_ids": tf.constant( all_input_type_ids, shape=[num_examples, seq_length], dtype=tf.int32), }) d = d.batch(batch_size=batch_size, drop_remainder=False) return d return input_fn def model_fn_builder(bert_config, init_checkpoint, layer_indexes, use_tpu, use_one_hot_embeddings): """Returns `model_fn` closure for TPUEstimator.""" def model_fn(features, labels, mode, params): # pylint: disable=unused-argument """The `model_fn` for TPUEstimator.""" unique_ids = features["unique_ids"] input_ids = features["input_ids"] input_mask = features["input_mask"] input_type_ids = features["input_type_ids"] model = modeling.BertModel( config=bert_config, is_training=False, input_ids=input_ids, input_mask=input_mask, token_type_ids=input_type_ids, use_one_hot_embeddings=use_one_hot_embeddings) if mode != tf.estimator.ModeKeys.PREDICT: raise ValueError("Only PREDICT modes are supported: %s" % (mode)) tvars = tf.trainable_variables() scaffold_fn = None (assignment_map, initialized_variable_names) = modeling.get_assignment_map_from_checkpoint( tvars, init_checkpoint) if use_tpu: def tpu_scaffold(): tf.train.init_from_checkpoint(init_checkpoint, assignment_map) return tf.train.Scaffold() scaffold_fn = tpu_scaffold else: tf.train.init_from_checkpoint(init_checkpoint, assignment_map) tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" tf.compat.v1.logging.info(" name = %s, shape = %s%s", var.name, var.shape, init_string) all_layers = model.get_all_encoder_layers() predictions = { "unique_id": unique_ids, } for (i, layer_index) in enumerate(layer_indexes): predictions["layer_output_%d" % i] = all_layers[layer_index] output_spec = tf.contrib.tpu.TPUEstimatorSpec( mode=mode, predictions=predictions, scaffold_fn=scaffold_fn) return output_spec return model_fn def convert_examples_to_features(examples, seq_length, tokenizer): """Loads a data file into a list of `InputBatch`s.""" features = [] for (ex_index, example) in enumerate(examples): tokens_a = tokenizer.tokenize(example.text_a) tokens_b = None if example.text_b: tokens_b = tokenizer.tokenize(example.text_b) if tokens_b: # Modifies `tokens_a` and `tokens_b` in place so that the total # length is less than the specified length. # Account for [CLS], [SEP], [SEP] with "- 3" _truncate_seq_pair(tokens_a, tokens_b, seq_length - 3) else: # Account for [CLS] and [SEP] with "- 2" if len(tokens_a) > seq_length - 2: tokens_a = tokens_a[0:(seq_length - 2)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens = [] input_type_ids = [] tokens.append("[CLS]") input_type_ids.append(0) for token in tokens_a: tokens.append(token) input_type_ids.append(0) tokens.append("[SEP]") input_type_ids.append(0) if tokens_b: for token in tokens_b: tokens.append(token) input_type_ids.append(1) tokens.append("[SEP]") input_type_ids.append(1) input_ids = tokenizer.convert_tokens_to_ids(tokens) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. input_mask = [1] * len(input_ids) # Zero-pad up to the sequence length. while len(input_ids) < seq_length: input_ids.append(0) input_mask.append(0) input_type_ids.append(0) assert len(input_ids) == seq_length assert len(input_mask) == seq_length assert len(input_type_ids) == seq_length if ex_index < 5: tf.compat.v1.logging.info("*** Example ***") tf.compat.v1.logging.info("unique_id: %s" % (example.unique_id)) tf.compat.v1.logging.info("tokens: %s" % " ".join( [tokenization.printable_text(x) for x in tokens])) tf.compat.v1.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids])) tf.compat.v1.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask])) tf.compat.v1.logging.info( "input_type_ids: %s" % " ".join([str(x) for x in input_type_ids])) features.append( InputFeatures( unique_id=example.unique_id, tokens=tokens, input_ids=input_ids, input_mask=input_mask, input_type_ids=input_type_ids)) return features def _truncate_seq_pair(tokens_a, tokens_b, max_length): """Truncates a sequence pair in place to the maximum length.""" # This is a simple heuristic which will always truncate the longer sequence # one token at a time. This makes more sense than truncating an equal percent # of tokens from each, since if one sequence is very short then each token # that's truncated likely contains more information than a longer sequence. while True: total_length = len(tokens_a) + len(tokens_b) if total_length <= max_length: break if len(tokens_a) > len(tokens_b): tokens_a.pop() else: tokens_b.pop() def read_examples(input_file): """Read a list of `InputExample`s from an input file.""" examples = [] unique_id = 0 with tf.io.gfile.GFile(input_file, "r") as reader: while True: line = tokenization.convert_to_unicode(reader.readline()) if not line: break line = line.strip() text_a = None text_b = None m = re.match(r"^(.*) \|\|\| (.*)$", line) if m is None: text_a = line else: text_a = m.group(1) text_b = m.group(2) examples.append( InputExample(unique_id=unique_id, text_a=text_a, text_b=text_b)) unique_id += 1 return examples def main(_): tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.info) layer_indexes = [int(x) for x in FLAGS.layers.split(",")] bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file) tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2 run_config = tf.contrib.tpu.RunConfig( master=FLAGS.master, tpu_config=tf.contrib.tpu.TPUConfig( num_shards=FLAGS.num_tpu_cores, per_host_input_for_training=is_per_host)) examples = read_examples(FLAGS.input_file) features = convert_examples_to_features( examples=examples, seq_length=FLAGS.max_seq_length, tokenizer=tokenizer) unique_id_to_feature = {} for feature in features: unique_id_to_feature[feature.unique_id] = feature model_fn = model_fn_builder( bert_config=bert_config, init_checkpoint=FLAGS.init_checkpoint, layer_indexes=layer_indexes, use_tpu=FLAGS.use_tpu, use_one_hot_embeddings=FLAGS.use_one_hot_embeddings) # If TPU is not available, this will fall back to normal Estimator on CPU # or GPU. estimator = tf.contrib.tpu.TPUEstimator( use_tpu=FLAGS.use_tpu, model_fn=model_fn, config=run_config, predict_batch_size=FLAGS.batch_size) input_fn = input_fn_builder( features=features, seq_length=FLAGS.max_seq_length) with codecs.getwriter("utf-8")(tf.io.gfile.Open(FLAGS.output_file, "w")) as writer: for result in estimator.predict(input_fn, yield_single_examples=True): unique_id = int(result["unique_id"]) feature = unique_id_to_feature[unique_id] output_json = collections.OrderedDict() output_json["linex_index"] = unique_id all_features = [] for (i, token) in enumerate(feature.tokens): all_layers = [] for (j, layer_index) in enumerate(layer_indexes): layer_output = result["layer_output_%d" % j] layers = collections.OrderedDict() layers["index"] = layer_index layers["values"] = [ round(float(x), 6) for x in layer_output[i:(i + 1)].flat ] all_layers.append(layers) features = collections.OrderedDict() features["token"] = token features["layers"] = all_layers all_features.append(features) output_json["features"] = all_features writer.write(json.dumps(output_json) + "\n") if __name__ == "__main__": flags.mark_flag_as_required("input_file") flags.mark_flag_as_required("vocab_file") flags.mark_flag_as_required("bert_config_file") flags.mark_flag_as_required("init_checkpoint") flags.mark_flag_as_required("output_file") tf.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/extract_features.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function import optimization import tensorflow as tf class OptimizationTest(tf.test.TestCase): def test_adam(self): with self.test_session() as sess: w = tf.get_variable( "w", shape=[3], initializer=tf.constant_initializer([0.1, -0.2, -0.1])) x = tf.constant([0.4, 0.2, -0.5]) loss = tf.reduce_mean(tf.square(x - w)) tvars = tf.trainable_variables() grads = tf.gradients(loss, tvars) global_step = tf.compat.v1.train.get_or_create_global_step() optimizer = optimization.AdamWeightDecayOptimizer(learning_rate=0.2) train_op = optimizer.apply_gradients(zip(grads, tvars), global_step) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) for _ in range(100): sess.run(train_op) w_np = sess.run(w) self.assertAllClose(w_np.flat, [0.4, 0.2, -0.5], rtol=1e-2, atol=1e-2) if __name__ == "__main__": tf.test.main()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/optimization_test.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Functions and classes related to optimization (weight updates).""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import re import tensorflow as tf from tensorflow.python.ops import array_ops from tensorflow.python.ops import linalg_ops from tensorflow.python.ops import math_ops from horovod.tensorflow.compression import Compression def create_optimizer(loss, init_lr, num_train_steps, num_warmup_steps, hvd=None, manual_fp16=False, use_fp16=False, num_accumulation_steps=1, optimizer_type="adam", allreduce_post_accumulation=False, init_loss_scale=2**32): """Creates an optimizer training op.""" global_step = tf.compat.v1.train.get_or_create_global_step() # avoid step change in learning rate at end of warmup phase if optimizer_type == "adam": power = 1.0 decayed_learning_rate_at_crossover_point = init_lr * ( (1.0 - float(num_warmup_steps) / float(num_train_steps)) ** power) else: power = 0.5 decayed_learning_rate_at_crossover_point = init_lr adjusted_init_lr = init_lr * (init_lr / decayed_learning_rate_at_crossover_point) print('decayed_learning_rate_at_crossover_point = %e, adjusted_init_lr = %e' % (decayed_learning_rate_at_crossover_point, adjusted_init_lr)) learning_rate = tf.constant(value=adjusted_init_lr, shape=[], dtype=tf.float32) # Implements linear decay of the learning rate. learning_rate = tf.compat.v1.train.polynomial_decay( learning_rate, global_step, num_train_steps, end_learning_rate=0.0, power=power, cycle=False) # Implements linear warmup. I.e., if global_step < num_warmup_steps, the # learning rate will be `global_step/num_warmup_steps * init_lr`. if num_warmup_steps: global_steps_int = tf.cast(global_step, tf.int32) warmup_steps_int = tf.constant(num_warmup_steps, dtype=tf.int32) global_steps_float = tf.cast(global_steps_int, tf.float32) warmup_steps_float = tf.cast(warmup_steps_int, tf.float32) warmup_percent_done = global_steps_float / warmup_steps_float warmup_learning_rate = init_lr * warmup_percent_done is_warmup = tf.cast(global_steps_int < warmup_steps_int, tf.float32) learning_rate = ( (1.0 - is_warmup) * learning_rate + is_warmup * warmup_learning_rate) if optimizer_type == "lamb": print("Initializing LAMB Optimizer") optimizer = LAMBOptimizer( learning_rate=learning_rate, weight_decay_rate=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-6, exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"]) else: print("Initializing ADAM Weight Decay Optimizer") # It is recommended that you use this optimizer for fine tuning, since this # is how the model was trained (note that the Adam m/v variables are NOT # loaded from init_checkpoint.) optimizer = AdamWeightDecayOptimizer( learning_rate=learning_rate, weight_decay_rate=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-6, exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"]) if hvd is not None and (num_accumulation_steps == 1 or (not allreduce_post_accumulation)): optimizer = hvd.DistributedOptimizer(optimizer, sparse_as_dense=True, compression=Compression.fp16 if use_fp16 or manual_fp16 else Compression.none) if use_fp16: loss_scaler = tf.train.experimental.DynamicLossScale(initial_loss_scale=init_loss_scale, increment_period=1000, multiplier=2.0) optimizer = tf.train.experimental.enable_mixed_precision_graph_rewrite(optimizer, loss_scaler) loss_scale_value = tf.identity(loss_scaler(), name="loss_scale") if manual_fp16: loss_scale_manager = tf.contrib.mixed_precision.ExponentialUpdateLossScaleManager(init_loss_scale=init_loss_scale, incr_every_n_steps=1000, decr_every_n_nan_or_inf=2, decr_ratio=0.5) optimizer = tf.contrib.mixed_precision.LossScaleOptimizer(optimizer, loss_scale_manager) tvars = tf.trainable_variables() grads_and_vars = optimizer.compute_gradients(loss * 1.0 / num_accumulation_steps, tvars) if num_accumulation_steps > 1: local_step = tf.get_variable(name="local_step", shape=[], dtype=tf.int32, trainable=False, initializer=tf.zeros_initializer) batch_finite = tf.get_variable(name="batch_finite", shape=[], dtype=tf.bool, trainable=False, initializer=tf.ones_initializer) accum_vars = [tf.get_variable( name=tvar.name.split(":")[0] + "/accum", shape=tvar.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer()) for tvar in tf.trainable_variables()] reset_step = tf.cast(tf.math.equal(local_step % num_accumulation_steps, 0), dtype=tf.bool) local_step = tf.cond(reset_step, lambda:local_step.assign(tf.ones_like(local_step)), lambda:local_step.assign_add(1)) grads_and_vars_and_accums = [(gv[0],gv[1],accum_vars[i]) for i, gv in enumerate(grads_and_vars) if gv[0] is not None] grads, tvars, accum_vars = list(zip(*grads_and_vars_and_accums)) all_are_finite = tf.reduce_all([tf.reduce_all(tf.is_finite(g)) for g in grads]) if manual_fp16 or use_fp16 else tf.constant(True, dtype=tf.bool) batch_finite = tf.cond(reset_step, lambda: batch_finite.assign(tf.math.logical_and(tf.constant(True, dtype=tf.bool), all_are_finite)), lambda:batch_finite.assign(tf.math.logical_and(batch_finite, all_are_finite))) # This is how the model was pre-trained. # ensure global norm is a finite number # to prevent clip_by_global_norm from having a hizzy fit. (clipped_grads, _) = tf.clip_by_global_norm( grads, clip_norm=1.0, use_norm=tf.cond( all_are_finite, lambda: tf.global_norm(grads), lambda: tf.constant(1.0))) accum_vars = tf.cond(reset_step, lambda: [accum_vars[i].assign(grad) for i, grad in enumerate(clipped_grads)], lambda: [accum_vars[i].assign_add(grad) for i, grad in enumerate(clipped_grads)]) def update(accum_vars): if allreduce_post_accumulation and hvd is not None: accum_vars = [hvd.allreduce(tf.convert_to_tensor(accum_var), compression=Compression.fp16 if use_fp16 or manual_fp16 else Compression.none) if isinstance(accum_var, tf.IndexedSlices) else hvd.allreduce(accum_var, compression=Compression.fp16 if use_fp16 or manual_fp16 else Compression.none) for accum_var in accum_vars] return optimizer.apply_gradients(list(zip(accum_vars, tvars)), global_step=global_step) update_step = tf.identity(tf.cast(tf.math.equal(local_step % num_accumulation_steps, 0), dtype=tf.bool), name="update_step") update_op = tf.cond(update_step, lambda: update(accum_vars), lambda: tf.no_op()) new_global_step = tf.cond(tf.math.logical_and(update_step, tf.cast(hvd.allreduce(tf.cast(batch_finite, tf.int32)), tf.bool) if hvd is not None else batch_finite), lambda: global_step+1, lambda: global_step) new_global_step = tf.identity(new_global_step, name='step_update') train_op = tf.group(update_op, [global_step.assign(new_global_step)]) else: grads_and_vars = [(g, v) for g, v in grads_and_vars if g is not None] grads, tvars = list(zip(*grads_and_vars)) all_are_finite = tf.reduce_all( [tf.reduce_all(tf.is_finite(g)) for g in grads]) if use_fp16 or manual_fp16 else tf.constant(True, dtype=tf.bool) # This is how the model was pre-trained. # ensure global norm is a finite number # to prevent clip_by_global_norm from having a hizzy fit. (clipped_grads, _) = tf.clip_by_global_norm( grads, clip_norm=1.0, use_norm=tf.cond( all_are_finite, lambda: tf.global_norm(grads), lambda: tf.constant(1.0))) train_op = optimizer.apply_gradients( list(zip(clipped_grads, tvars)), global_step=global_step) new_global_step = tf.cond(all_are_finite, lambda: global_step + 1, lambda: global_step) new_global_step = tf.identity(new_global_step, name='step_update') train_op = tf.group(train_op, [global_step.assign(new_global_step)]) return train_op class AdamWeightDecayOptimizer(tf.compat.v1.train.Optimizer): """A basic Adam optimizer that includes "correct" L2 weight decay.""" def __init__(self, learning_rate, weight_decay_rate=0.0, beta_1=0.9, beta_2=0.999, epsilon=1e-6, exclude_from_weight_decay=None, name="AdamWeightDecayOptimizer"): """Constructs a AdamWeightDecayOptimizer.""" super(AdamWeightDecayOptimizer, self).__init__(False, name) self.learning_rate = tf.identity(learning_rate, name='learning_rate') self.weight_decay_rate = weight_decay_rate self.beta_1 = beta_1 self.beta_2 = beta_2 self.epsilon = epsilon self.exclude_from_weight_decay = exclude_from_weight_decay def apply_gradients(self, grads_and_vars, global_step=None, name=None, manual_fp16=False): """See base class.""" assignments = [] for (grad, param) in grads_and_vars: if grad is None or param is None: continue param_name = self._get_variable_name(param.name) has_shadow = manual_fp16 and param.dtype.base_dtype != tf.float32 if has_shadow: # create shadow fp32 weights for fp16 variable param_fp32 = tf.get_variable( name=param_name + "/shadow", dtype=tf.float32, trainable=False, initializer=tf.cast(param.initialized_value(),tf.float32)) else: param_fp32 = param m = tf.get_variable( name=param_name + "/adam_m", shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer()) v = tf.get_variable( name=param_name + "/adam_v", shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer()) # Standard Adam update. next_m = ( tf.multiply(self.beta_1, m) + tf.multiply(1.0 - self.beta_1, grad)) next_v = ( tf.multiply(self.beta_2, v) + tf.multiply(1.0 - self.beta_2, tf.square(grad))) update = next_m / (tf.sqrt(next_v) + self.epsilon) # Just adding the square of the weights to the loss function is *not* # the correct way of using L2 regularization/weight decay with Adam, # since that will interact with the m and v parameters in strange ways. # # Instead we want to decay the weights in a manner that doesn't interact # with the m/v parameters. This is equivalent to adding the square # of the weights to the loss with plain (non-momentum) SGD. if self._do_use_weight_decay(param_name): update += self.weight_decay_rate * param_fp32 update_with_lr = self.learning_rate * update next_param = param_fp32 - update_with_lr if has_shadow: # cast shadow fp32 weights to fp16 and assign to trainable variable param.assign(tf.cast(next_param, param.dtype.base_dtype)) assignments.extend( [param_fp32.assign(next_param), m.assign(next_m), v.assign(next_v)]) return tf.group(*assignments, name=name) def _do_use_weight_decay(self, param_name): """Whether to use L2 weight decay for `param_name`.""" if not self.weight_decay_rate: return False if self.exclude_from_weight_decay: for r in self.exclude_from_weight_decay: if re.search(r, param_name) is not None: return False return True def _get_variable_name(self, param_name): """Get the variable name from the tensor name.""" m = re.match("^(.*):\\d+$", param_name) if m is not None: param_name = m.group(1) return param_name class LAMBOptimizer(tf.compat.v1.train.Optimizer): """A LAMB optimizer that includes "correct" L2 weight decay.""" def __init__(self, learning_rate, weight_decay_rate=0.0, beta_1=0.9, beta_2=0.999, epsilon=1e-6, exclude_from_weight_decay=None, name="LAMBOptimizer"): """Constructs a LAMBOptimizer.""" super(LAMBOptimizer, self).__init__(False, name) self.learning_rate = tf.identity(learning_rate, name='learning_rate') self.weight_decay_rate = weight_decay_rate self.beta_1 = beta_1 self.beta_2 = beta_2 self.epsilon = epsilon self.exclude_from_weight_decay = exclude_from_weight_decay def apply_gradients(self, grads_and_vars, global_step, name=None, manual_fp16=False): """See base class.""" assignments = [] steps = tf.cast(global_step, tf.float32) for (grad, param) in grads_and_vars: if grad is None or param is None: continue param_name = self._get_variable_name(param.name) has_shadow = manual_fp16 and param.dtype.base_dtype != tf.float32 if has_shadow: # create shadow fp32 weights for fp16 variable param_fp32 = tf.get_variable( name=param_name + "/shadow", dtype=tf.float32, trainable=False, initializer=tf.cast(param.initialized_value(),tf.float32)) else: param_fp32 = param m = tf.get_variable( name=param_name + "/adam_m", shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer()) v = tf.get_variable( name=param_name + "/adam_v", shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer()) # LAMB update next_m = ( tf.multiply(self.beta_1, m) + tf.multiply(1.0 - self.beta_1, grad)) next_v = ( tf.multiply(self.beta_2, v) + tf.multiply(1.0 - self.beta_2, tf.square(grad))) beta1_correction = (1 - self.beta_1 ** steps) beta2_correction = (1 - self.beta_2 ** steps) next_m_unbiased = next_m / beta1_correction next_v_unbiased = next_v / beta2_correction update = next_m_unbiased / (tf.sqrt(next_v_unbiased) + self.epsilon) # Just adding the square of the weights to the loss function is *not* # the correct way of using L2 regularization/weight decay with Adam, # since that will interact with the m and v parameters in strange ways. # # Instead we want to decay the weights in a manner that doesn't interact # with the m/v parameters. This is equivalent to adding the square # of the weights to the loss with plain (non-momentum) SGD. if self._do_use_weight_decay(param_name): update += self.weight_decay_rate * param_fp32 w_norm = linalg_ops.norm(param, ord=2) g_norm = linalg_ops.norm(update, ord=2) ratio = array_ops.where(math_ops.greater(w_norm, 0), array_ops.where( math_ops.greater(g_norm, 0), (w_norm / g_norm), 1.0), 1.0) update_with_lr = ratio * self.learning_rate * update next_param = param_fp32 - update_with_lr if has_shadow: # cast shadow fp32 weights to fp16 and assign to trainable variable param.assign(tf.cast(next_param, param.dtype.base_dtype)) assignments.extend( [param_fp32.assign(next_param), m.assign(next_m), v.assign(next_v)]) return tf.group(*assignments, name=name) def _do_use_weight_decay(self, param_name): """Whether to use L2 weight decay for `param_name`.""" if not self.weight_decay_rate: return False if self.exclude_from_weight_decay: for r in self.exclude_from_weight_decay: if re.search(r, param_name) is not None: return False return True def _get_variable_name(self, param_name): """Get the variable name from the tensor name.""" m = re.match("^(.*):\\d+$", param_name) if m is not None: param_name = m.group(1) return param_name
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/optimization.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import tempfile import tokenization import six import tensorflow as tf class TokenizationTest(tf.test.TestCase): def test_full_tokenizer(self): vocab_tokens = [ "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", "," ] with tempfile.NamedTemporaryFile(delete=False) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) vocab_file = vocab_writer.name tokenizer = tokenization.FullTokenizer(vocab_file) os.unlink(vocab_file) tokens = tokenizer.tokenize(u"UNwant\u00E9d,running") self.assertAllEqual(tokens, ["un", "##want", "##ed", ",", "runn", "##ing"]) self.assertAllEqual( tokenizer.convert_tokens_to_ids(tokens), [7, 4, 5, 10, 8, 9]) def test_chinese(self): tokenizer = tokenization.BasicTokenizer() self.assertAllEqual( tokenizer.tokenize(u"ah\u535A\u63A8zz"), [u"ah", u"\u535A", u"\u63A8", u"zz"]) def test_basic_tokenizer_lower(self): tokenizer = tokenization.BasicTokenizer(do_lower_case=True) self.assertAllEqual( tokenizer.tokenize(u" \tHeLLo!how \n Are yoU? "), ["hello", "!", "how", "are", "you", "?"]) self.assertAllEqual(tokenizer.tokenize(u"H\u00E9llo"), ["hello"]) def test_basic_tokenizer_no_lower(self): tokenizer = tokenization.BasicTokenizer(do_lower_case=False) self.assertAllEqual( tokenizer.tokenize(u" \tHeLLo!how \n Are yoU? "), ["HeLLo", "!", "how", "Are", "yoU", "?"]) def test_wordpiece_tokenizer(self): vocab_tokens = [ "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing" ] vocab = {} for (i, token) in enumerate(vocab_tokens): vocab[token] = i tokenizer = tokenization.WordpieceTokenizer(vocab=vocab) self.assertAllEqual(tokenizer.tokenize(""), []) self.assertAllEqual( tokenizer.tokenize("unwanted running"), ["un", "##want", "##ed", "runn", "##ing"]) self.assertAllEqual( tokenizer.tokenize("unwantedX running"), ["[UNK]", "runn", "##ing"]) def test_convert_tokens_to_ids(self): vocab_tokens = [ "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing" ] vocab = {} for (i, token) in enumerate(vocab_tokens): vocab[token] = i self.assertAllEqual( tokenization.convert_tokens_to_ids( vocab, ["un", "##want", "##ed", "runn", "##ing"]), [7, 4, 5, 8, 9]) def test_is_whitespace(self): self.assertTrue(tokenization._is_whitespace(u" ")) self.assertTrue(tokenization._is_whitespace(u"\t")) self.assertTrue(tokenization._is_whitespace(u"\r")) self.assertTrue(tokenization._is_whitespace(u"\n")) self.assertTrue(tokenization._is_whitespace(u"\u00A0")) self.assertFalse(tokenization._is_whitespace(u"A")) self.assertFalse(tokenization._is_whitespace(u"-")) def test_is_control(self): self.assertTrue(tokenization._is_control(u"\u0005")) self.assertFalse(tokenization._is_control(u"A")) self.assertFalse(tokenization._is_control(u" ")) self.assertFalse(tokenization._is_control(u"\t")) self.assertFalse(tokenization._is_control(u"\r")) self.assertFalse(tokenization._is_control(u"\U0001F4A9")) def test_is_punctuation(self): self.assertTrue(tokenization._is_punctuation(u"-")) self.assertTrue(tokenization._is_punctuation(u"$")) self.assertTrue(tokenization._is_punctuation(u"`")) self.assertTrue(tokenization._is_punctuation(u".")) self.assertFalse(tokenization._is_punctuation(u"A")) self.assertFalse(tokenization._is_punctuation(u" ")) if __name__ == "__main__": tf.test.main()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/tokenization_test.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Run BERT on SQuAD 1.1 and SQuAD 2.0.""" from __future__ import absolute_import, division, print_function import collections import json import math import os import random import shutil import time import horovod.tensorflow as hvd import numpy as np import six import tensorflow as tf from tensorflow.python.client import device_lib import modeling import optimization import tokenization from utils.create_squad_data import * from utils.utils import LogEvalRunHook, LogTrainRunHook, setup_xla_flags from utils.gpu_affinity import set_affinity import utils.dllogger_class from dllogger import Verbosity flags = tf.flags FLAGS = None def extract_run_squad_flags(): ## Required parameters flags.DEFINE_string( "bert_config_file", None, "The config json file corresponding to the pre-trained BERT model. " "This specifies the model architecture.") flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_string( "output_dir", None, "The output directory where the model checkpoints will be written.") ## Other parameters flags.DEFINE_string( "dllog_path", "/results/bert_dllog.json", "filename where dllogger writes to") flags.DEFINE_string("train_file", None, "SQuAD json for training. E.g., train-v1.1.json") flags.DEFINE_string( "predict_file", None, "SQuAD json for predictions. E.g., dev-v1.1.json or test-v1.1.json") flags.DEFINE_string( "eval_script", None, "SQuAD evaluate.py file to compute f1 and exact_match E.g., evaluate-v1.1.py") flags.DEFINE_string( "init_checkpoint", None, "Initial checkpoint (usually from a pre-trained BERT model).") flags.DEFINE_bool( "do_lower_case", True, "Whether to lower case the input text. Should be True for uncased " "models and False for cased models.") flags.DEFINE_integer( "max_seq_length", 384, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded.") flags.DEFINE_integer( "doc_stride", 128, "When splitting up a long document into chunks, how much stride to " "take between chunks.") flags.DEFINE_integer( "max_query_length", 64, "The maximum number of tokens for the question. Questions longer than " "this will be truncated to this length.") flags.DEFINE_bool("do_train", False, "Whether to run training.") flags.DEFINE_bool("do_predict", False, "Whether to run eval on the dev set.") flags.DEFINE_integer("train_batch_size", 8, "Total batch size for training.") flags.DEFINE_integer("predict_batch_size", 8, "Total batch size for predictions.") flags.DEFINE_float("learning_rate", 5e-6, "The initial learning rate for Adam.") flags.DEFINE_bool("use_trt", False, "Whether to use TF-TRT") flags.DEFINE_bool("horovod", False, "Whether to use Horovod for multi-gpu runs") flags.DEFINE_float("num_train_epochs", 3.0, "Total number of training epochs to perform.") flags.DEFINE_float( "warmup_proportion", 0.1, "Proportion of training to perform linear learning rate warmup for. " "E.g., 0.1 = 10% of training.") flags.DEFINE_integer("save_checkpoints_steps", 5000, "How often to save the model checkpoint.") flags.DEFINE_integer("display_loss_steps", 10, "How often to print loss from estimator") flags.DEFINE_integer("iterations_per_loop", 1000, "How many steps to make in each estimator call.") flags.DEFINE_integer("num_accumulation_steps", 1, "Number of accumulation steps before gradient update" "Global batch size = num_accumulation_steps * train_batch_size") flags.DEFINE_integer( "n_best_size", 20, "The total number of n-best predictions to generate in the " "nbest_predictions.json output file.") flags.DEFINE_integer( "max_answer_length", 30, "The maximum length of an answer that can be generated. This is needed " "because the start and end predictions are not conditioned on one another.") flags.DEFINE_bool( "verbose_logging", False, "If true, all of the warnings related to data processing will be printed. " "A number of warnings are expected for a normal SQuAD evaluation.") flags.DEFINE_bool( "version_2_with_negative", False, "If true, the SQuAD examples contain some that do not have an answer.") flags.DEFINE_float( "null_score_diff_threshold", 0.0, "If null_score - best_non_null is greater than the threshold predict null.") flags.DEFINE_bool("amp", True, "Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.") flags.DEFINE_bool("use_xla", True, "Whether to enable XLA JIT compilation.") flags.DEFINE_integer("num_eval_iterations", None, "How many eval iterations to run - performs inference on subset") # Triton Specific flags flags.DEFINE_bool("export_triton", False, "Whether to export saved model or run inference with Triton") flags.DEFINE_string("triton_model_name", "bert", "exports to appropriate directory for Triton") flags.DEFINE_integer("triton_model_version", 1, "exports to appropriate directory for Triton") flags.DEFINE_string("triton_server_url", "localhost:8001", "exports to appropriate directory for Triton") flags.DEFINE_bool("triton_model_overwrite", False, "If True, will overwrite an existing directory with the specified 'model_name' and 'version_name'") flags.DEFINE_integer("triton_max_batch_size", 8, "Specifies the 'max_batch_size' in the Triton model config. See the Triton documentation for more info.") flags.DEFINE_float("triton_dyn_batching_delay", 0, "Determines the dynamic_batching queue delay in milliseconds(ms) for the Triton model config. Use '0' or '-1' to specify static batching. See the Triton documentation for more info.") flags.DEFINE_integer("triton_engine_count", 1, "Specifies the 'instance_group' count value in the Triton model config. See the Triton documentation for more info.") flags.mark_flag_as_required("vocab_file") flags.mark_flag_as_required("bert_config_file") flags.mark_flag_as_required("output_dir") return flags.FLAGS def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, use_one_hot_embeddings): """Creates a classification model.""" model = modeling.BertModel( config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings, compute_type=tf.float32) final_hidden = model.get_sequence_output() final_hidden_shape = modeling.get_shape_list(final_hidden, expected_rank=3) batch_size = final_hidden_shape[0] seq_length = final_hidden_shape[1] hidden_size = final_hidden_shape[2] output_weights = tf.get_variable( "cls/squad/output_weights", [2, hidden_size], initializer=tf.truncated_normal_initializer(stddev=0.02)) output_bias = tf.get_variable( "cls/squad/output_bias", [2], initializer=tf.zeros_initializer()) final_hidden_matrix = tf.reshape(final_hidden, [batch_size * seq_length, hidden_size]) logits = tf.matmul(final_hidden_matrix, output_weights, transpose_b=True) logits = tf.nn.bias_add(logits, output_bias) logits = tf.reshape(logits, [batch_size, seq_length, 2]) logits = tf.transpose(logits, [2, 0, 1]) unstacked_logits = tf.unstack(logits, axis=0, name='unstack') (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1]) return (start_logits, end_logits) def get_frozen_tftrt_model(bert_config, shape, use_one_hot_embeddings, init_checkpoint): tf_config = tf.compat.v1.ConfigProto() tf_config.gpu_options.allow_growth = True output_node_names = ['unstack'] with tf.Session(config=tf_config) as tf_sess: input_ids = tf.placeholder(tf.int32, shape, 'input_ids') input_mask = tf.placeholder(tf.int32, shape, 'input_mask') segment_ids = tf.placeholder(tf.int32, shape, 'segment_ids') (start_logits, end_logits) = create_model(bert_config=bert_config, is_training=False, input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings) tvars = tf.trainable_variables() (assignment_map, initialized_variable_names) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) tf_sess.run(tf.global_variables_initializer()) print("LOADED!") tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" else: init_string = ", *NOTTTTTTTTTTTTTTTTTTTTT" tf.compat.v1.logging.info(" name = %s, shape = %s%s", var.name, var.shape, init_string) frozen_graph = tf.graph_util.convert_variables_to_constants(tf_sess, tf_sess.graph.as_graph_def(), output_node_names) num_nodes = len(frozen_graph.node) print('Converting graph using TensorFlow-TensorRT...') from tensorflow.python.compiler.tensorrt import trt_convert as trt converter = trt.TrtGraphConverter( input_graph_def=frozen_graph, nodes_blacklist=output_node_names, max_workspace_size_bytes=(4096 << 20) - 1000, precision_mode = "FP16" if FLAGS.amp else "FP32", minimum_segment_size=4, is_dynamic_op=True, maximum_cached_engines=1000 ) frozen_graph = converter.convert() print('Total node count before and after TF-TRT conversion:', num_nodes, '->', len(frozen_graph.node)) print('TRT node count:', len([1 for n in frozen_graph.node if str(n.op) == 'TRTEngineOp'])) with tf.io.gfile.GFile("frozen_modelTRT.pb", "wb") as f: f.write(frozen_graph.SerializeToString()) return frozen_graph def model_fn_builder(bert_config, init_checkpoint, learning_rate, num_train_steps, num_warmup_steps, hvd=None, amp=False, use_one_hot_embeddings=False): """Returns `model_fn` closure for Estimator.""" def model_fn(features, labels, mode, params): # pylint: disable=unused-argument """The `model_fn` for Estimator.""" if FLAGS.verbose_logging: tf.compat.v1.logging.info("*** Features ***") for name in sorted(features.keys()): tf.compat.v1.logging.info(" name = %s, shape = %s" % (name, features[name].shape)) unique_ids = features["unique_ids"] input_ids = features["input_ids"] input_mask = features["input_mask"] segment_ids = features["segment_ids"] is_training = (mode == tf.estimator.ModeKeys.TRAIN) if not is_training and FLAGS.use_trt: trt_graph = get_frozen_tftrt_model(bert_config, input_ids.shape, use_one_hot_embeddings, init_checkpoint) (start_logits, end_logits) = tf.import_graph_def(trt_graph, input_map={'input_ids':input_ids, 'input_mask':input_mask, 'segment_ids':segment_ids}, return_elements=['unstack:0', 'unstack:1'], name='') predictions = { "unique_ids": unique_ids, "start_logits": start_logits, "end_logits": end_logits, } output_spec = tf.estimator.EstimatorSpec( mode=mode, predictions=predictions) return output_spec (start_logits, end_logits) = create_model( bert_config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings) tvars = tf.trainable_variables() initialized_variable_names = {} if init_checkpoint and (hvd is None or hvd.rank() == 0): (assignment_map, initialized_variable_names ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) if FLAGS.verbose_logging: tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" tf.compat.v1.logging.info(" %d name = %s, shape = %s%s", 0 if hvd is None else hvd.rank(), var.name, var.shape, init_string) output_spec = None if mode == tf.estimator.ModeKeys.TRAIN: seq_length = modeling.get_shape_list(input_ids)[1] def compute_loss(logits, positions): one_hot_positions = tf.one_hot( positions, depth=seq_length, dtype=tf.float32) log_probs = tf.nn.log_softmax(logits, axis=-1) loss = -tf.reduce_mean( tf.reduce_sum(one_hot_positions * log_probs, axis=-1)) return loss start_positions = features["start_positions"] end_positions = features["end_positions"] start_loss = compute_loss(start_logits, start_positions) end_loss = compute_loss(end_logits, end_positions) total_loss = (start_loss + end_loss) / 2.0 train_op = optimization.create_optimizer( total_loss, learning_rate, num_train_steps, num_warmup_steps, hvd, False, amp, FLAGS.num_accumulation_steps) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, train_op=train_op) elif mode == tf.estimator.ModeKeys.PREDICT: dummy_op = tf.no_op() # Need to call mixed precision graph rewrite if fp16 to enable graph rewrite if amp: loss_scaler = tf.train.experimental.FixedLossScale(1) dummy_op = tf.train.experimental.enable_mixed_precision_graph_rewrite( optimization.LAMBOptimizer(learning_rate=0.0), loss_scaler) predictions = { "unique_ids": tf.identity(unique_ids), "start_logits": start_logits, "end_logits": end_logits, } output_spec = tf.estimator.EstimatorSpec( mode=mode, predictions=predictions) else: raise ValueError( "Only TRAIN and PREDICT modes are supported: %s" % (mode)) return output_spec return model_fn def input_fn_builder(input_file, batch_size, seq_length, is_training, drop_remainder, hvd=None): """Creates an `input_fn` closure to be passed to Estimator.""" name_to_features = { "unique_ids": tf.io.FixedLenFeature([], tf.int64), "input_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "input_mask": tf.io.FixedLenFeature([seq_length], tf.int64), "segment_ids": tf.io.FixedLenFeature([seq_length], tf.int64), } if is_training: name_to_features["start_positions"] = tf.io.FixedLenFeature([], tf.int64) name_to_features["end_positions"] = tf.io.FixedLenFeature([], tf.int64) def _decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.to_int32(t) example[name] = t return example def input_fn(): """The actual input function.""" # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. if is_training: d = tf.data.TFRecordDataset(input_file, num_parallel_reads=4) if hvd is not None: d = d.shard(hvd.size(), hvd.rank()) d = d.apply(tf.data.experimental.ignore_errors()) d = d.shuffle(buffer_size=100) d = d.repeat() else: d = tf.data.TFRecordDataset(input_file) d = d.apply( tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, drop_remainder=drop_remainder)) return d return input_fn RawResult = collections.namedtuple("RawResult", ["unique_id", "start_logits", "end_logits"]) def get_predictions(all_examples, all_features, all_results, n_best_size, max_answer_length, do_lower_case, version_2_with_negative, verbose_logging): """Get final predictions""" example_index_to_features = collections.defaultdict(list) for feature in all_features: example_index_to_features[feature.example_index].append(feature) unique_id_to_result = {} for result in all_results: unique_id_to_result[result.unique_id] = result _PrelimPrediction = collections.namedtuple( # pylint: disable=invalid-name "PrelimPrediction", ["feature_index", "start_index", "end_index", "start_logit", "end_logit"]) all_predictions = collections.OrderedDict() all_nbest_json = collections.OrderedDict() scores_diff_json = collections.OrderedDict() for (example_index, example) in enumerate(all_examples): features = example_index_to_features[example_index] prelim_predictions = [] # keep track of the minimum score of null start+end of position 0 score_null = 1000000 # large and positive min_null_feature_index = 0 # the paragraph slice with min mull score null_start_logit = 0 # the start logit at the slice with min null score null_end_logit = 0 # the end logit at the slice with min null score for (feature_index, feature) in enumerate(features): result = unique_id_to_result[feature.unique_id] start_indexes = _get_best_indexes(result.start_logits, n_best_size) end_indexes = _get_best_indexes(result.end_logits, n_best_size) # if we could have irrelevant answers, get the min score of irrelevant if version_2_with_negative: feature_null_score = result.start_logits[0] + result.end_logits[0] if feature_null_score < score_null: score_null = feature_null_score min_null_feature_index = feature_index null_start_logit = result.start_logits[0] null_end_logit = result.end_logits[0] for start_index in start_indexes: for end_index in end_indexes: # We could hypothetically create invalid predictions, e.g., predict # that the start of the span is in the question. We throw out all # invalid predictions. if start_index >= len(feature.tokens): continue if end_index >= len(feature.tokens): continue if start_index not in feature.token_to_orig_map: continue if end_index not in feature.token_to_orig_map: continue if not feature.token_is_max_context.get(start_index, False): continue if end_index < start_index: continue length = end_index - start_index + 1 if length > max_answer_length: continue prelim_predictions.append( _PrelimPrediction( feature_index=feature_index, start_index=start_index, end_index=end_index, start_logit=result.start_logits[start_index], end_logit=result.end_logits[end_index])) if version_2_with_negative: prelim_predictions.append( _PrelimPrediction( feature_index=min_null_feature_index, start_index=0, end_index=0, start_logit=null_start_logit, end_logit=null_end_logit)) prelim_predictions = sorted( prelim_predictions, key=lambda x: (x.start_logit + x.end_logit), reverse=True) _NbestPrediction = collections.namedtuple( # pylint: disable=invalid-name "NbestPrediction", ["text", "start_logit", "end_logit"]) seen_predictions = {} nbest = [] for pred in prelim_predictions: if len(nbest) >= n_best_size: break feature = features[pred.feature_index] if pred.start_index > 0: # this is a non-null prediction tok_tokens = feature.tokens[pred.start_index:(pred.end_index + 1)] orig_doc_start = feature.token_to_orig_map[pred.start_index] orig_doc_end = feature.token_to_orig_map[pred.end_index] orig_tokens = example.doc_tokens[orig_doc_start:(orig_doc_end + 1)] tok_text = " ".join(tok_tokens) # De-tokenize WordPieces that have been split off. tok_text = tok_text.replace(" ##", "") tok_text = tok_text.replace("##", "") # Clean whitespace tok_text = tok_text.strip() tok_text = " ".join(tok_text.split()) orig_text = " ".join(orig_tokens) final_text = get_final_text(tok_text, orig_text, do_lower_case, verbose_logging) if final_text in seen_predictions: continue seen_predictions[final_text] = True else: final_text = "" seen_predictions[final_text] = True nbest.append( _NbestPrediction( text=final_text, start_logit=pred.start_logit, end_logit=pred.end_logit)) # if we didn't inlude the empty option in the n-best, inlcude it if version_2_with_negative: if "" not in seen_predictions: nbest.append( _NbestPrediction( text="", start_logit=null_start_logit, end_logit=null_end_logit)) # In very rare edge cases we could have no valid predictions. So we # just create a nonce prediction in this case to avoid failure. if not nbest: nbest.append( _NbestPrediction(text="empty", start_logit=0.0, end_logit=0.0)) assert len(nbest) >= 1 total_scores = [] best_non_null_entry = None for entry in nbest: total_scores.append(entry.start_logit + entry.end_logit) if not best_non_null_entry: if entry.text: best_non_null_entry = entry probs = _compute_softmax(total_scores) nbest_json = [] for (i, entry) in enumerate(nbest): output = collections.OrderedDict() output["text"] = entry.text output["probability"] = probs[i] output["start_logit"] = entry.start_logit output["end_logit"] = entry.end_logit nbest_json.append(output) assert len(nbest_json) >= 1 if not version_2_with_negative: all_predictions[example.qas_id] = nbest_json[0]["text"] else: # predict "" iff the null score - the score of best non-null > threshold score_diff = score_null - best_non_null_entry.start_logit - ( best_non_null_entry.end_logit) scores_diff_json[example.qas_id] = score_diff try: null_score_diff_threshold = FLAGS.null_score_diff_threshold except: null_score_diff_threshold = 0.0 if score_diff > null_score_diff_threshold: all_predictions[example.qas_id] = "" else: all_predictions[example.qas_id] = best_non_null_entry.text all_nbest_json[example.qas_id] = nbest_json return all_predictions, all_nbest_json, scores_diff_json def write_predictions(all_examples, all_features, all_results, n_best_size, max_answer_length, do_lower_case, output_prediction_file, output_nbest_file, output_null_log_odds_file, version_2_with_negative, verbose_logging): """Write final predictions to the json file and log-odds of null if needed.""" tf.compat.v1.logging.info("Writing predictions to: %s" % (output_prediction_file)) tf.compat.v1.logging.info("Writing nbest to: %s" % (output_nbest_file)) all_predictions, all_nbest_json, scores_diff_json = get_predictions(all_examples, all_features, all_results, n_best_size, max_answer_length, do_lower_case, version_2_with_negative, verbose_logging) with tf.io.gfile.GFile(output_prediction_file, "w") as writer: writer.write(json.dumps(all_predictions, indent=4) + "\n") with tf.io.gfile.GFile(output_nbest_file, "w") as writer: writer.write(json.dumps(all_nbest_json, indent=4) + "\n") if version_2_with_negative: with tf.io.gfile.GFile(output_null_log_odds_file, "w") as writer: writer.write(json.dumps(scores_diff_json, indent=4) + "\n") def get_final_text(pred_text, orig_text, do_lower_case, verbose_logging): """Project the tokenized prediction back to the original text.""" # When we created the data, we kept track of the alignment between original # (whitespace tokenized) tokens and our WordPiece tokenized tokens. So # now `orig_text` contains the span of our original text corresponding to the # span that we predicted. # # However, `orig_text` may contain extra characters that we don't want in # our prediction. # # For example, let's say: # pred_text = steve smith # orig_text = Steve Smith's # # We don't want to return `orig_text` because it contains the extra "'s". # # We don't want to return `pred_text` because it's already been normalized # (the SQuAD eval script also does punctuation stripping/lower casing but # our tokenizer does additional normalization like stripping accent # characters). # # What we really want to return is "Steve Smith". # # Therefore, we have to apply a semi-complicated alignment heruistic between # `pred_text` and `orig_text` to get a character-to-charcter alignment. This # can fail in certain cases in which case we just return `orig_text`. def _strip_spaces(text): ns_chars = [] ns_to_s_map = collections.OrderedDict() for (i, c) in enumerate(text): if c == " ": continue ns_to_s_map[len(ns_chars)] = i ns_chars.append(c) ns_text = "".join(ns_chars) return (ns_text, ns_to_s_map) # We first tokenize `orig_text`, strip whitespace from the result # and `pred_text`, and check if they are the same length. If they are # NOT the same length, the heuristic has failed. If they are the same # length, we assume the characters are one-to-one aligned. tokenizer = tokenization.BasicTokenizer(do_lower_case=do_lower_case) tok_text = " ".join(tokenizer.tokenize(orig_text)) start_position = tok_text.find(pred_text) if start_position == -1: if verbose_logging: tf.compat.v1.logging.info( "Unable to find text: '%s' in '%s'" % (pred_text, orig_text)) return orig_text end_position = start_position + len(pred_text) - 1 (orig_ns_text, orig_ns_to_s_map) = _strip_spaces(orig_text) (tok_ns_text, tok_ns_to_s_map) = _strip_spaces(tok_text) if len(orig_ns_text) != len(tok_ns_text): if verbose_logging: tf.compat.v1.logging.info("Length not equal after stripping spaces: '%s' vs '%s'", orig_ns_text, tok_ns_text) return orig_text # We then project the characters in `pred_text` back to `orig_text` using # the character-to-character alignment. tok_s_to_ns_map = {} for (i, tok_index) in six.iteritems(tok_ns_to_s_map): tok_s_to_ns_map[tok_index] = i orig_start_position = None if start_position in tok_s_to_ns_map: ns_start_position = tok_s_to_ns_map[start_position] if ns_start_position in orig_ns_to_s_map: orig_start_position = orig_ns_to_s_map[ns_start_position] if orig_start_position is None: if verbose_logging: tf.compat.v1.logging.info("Couldn't map start position") return orig_text orig_end_position = None if end_position in tok_s_to_ns_map: ns_end_position = tok_s_to_ns_map[end_position] if ns_end_position in orig_ns_to_s_map: orig_end_position = orig_ns_to_s_map[ns_end_position] if orig_end_position is None: if verbose_logging: tf.compat.v1.logging.info("Couldn't map end position") return orig_text output_text = orig_text[orig_start_position:(orig_end_position + 1)] return output_text def _get_best_indexes(logits, n_best_size): """Get the n-best logits from a list.""" index_and_score = sorted(enumerate(logits), key=lambda x: x[1], reverse=True) best_indexes = [] for i in range(len(index_and_score)): if i >= n_best_size: break best_indexes.append(index_and_score[i][0]) return best_indexes def _compute_softmax(scores): """Compute softmax probability over raw logits.""" if not scores: return [] max_score = None for score in scores: if max_score is None or score > max_score: max_score = score exp_scores = [] total_sum = 0.0 for score in scores: x = math.exp(score - max_score) exp_scores.append(x) total_sum += x probs = [] for score in exp_scores: probs.append(score / total_sum) return probs def validate_flags_or_throw(bert_config): """Validate the input FLAGS or throw an exception.""" tokenization.validate_case_matches_checkpoint(FLAGS.do_lower_case, FLAGS.init_checkpoint) if not FLAGS.do_train and not FLAGS.do_predict and not FLAGS.export_triton: raise ValueError("At least one of `do_train` or `do_predict` or `export_SavedModel` must be True.") if FLAGS.do_train: if not FLAGS.train_file: raise ValueError( "If `do_train` is True, then `train_file` must be specified.") if FLAGS.do_predict: if not FLAGS.predict_file: raise ValueError( "If `do_predict` is True, then `predict_file` must be specified.") if FLAGS.max_seq_length > bert_config.max_position_embeddings: raise ValueError( "Cannot use sequence length %d because the BERT model " "was only trained up to sequence length %d" % (FLAGS.max_seq_length, bert_config.max_position_embeddings)) if FLAGS.max_seq_length <= FLAGS.max_query_length + 3: raise ValueError( "The max_seq_length (%d) must be greater than max_query_length " "(%d) + 3" % (FLAGS.max_seq_length, FLAGS.max_query_length)) def export_model(estimator, export_dir, init_checkpoint): """Exports a checkpoint in SavedModel format in a directory structure compatible with Triton.""" def serving_input_fn(): label_ids = tf.placeholder(tf.int32, [None,], name='unique_ids') input_ids = tf.placeholder(tf.int32, [None, FLAGS.max_seq_length], name='input_ids') input_mask = tf.placeholder(tf.int32, [None, FLAGS.max_seq_length], name='input_mask') segment_ids = tf.placeholder(tf.int32, [None, FLAGS.max_seq_length], name='segment_ids') input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn({ 'unique_ids': label_ids, 'input_ids': input_ids, 'input_mask': input_mask, 'segment_ids': segment_ids, })() return input_fn saved_dir = estimator.export_savedmodel( export_dir, serving_input_fn, assets_extra=None, as_text=False, checkpoint_path=init_checkpoint, strip_default_attrs=False) model_name = FLAGS.triton_model_name model_folder = export_dir + "/triton_models/" + model_name version_folder = model_folder + "/" + str(FLAGS.triton_model_version) final_model_folder = version_folder + "/model.savedmodel" if not os.path.exists(version_folder): os.makedirs(version_folder) if (not os.path.exists(final_model_folder)): os.rename(saved_dir, final_model_folder) print("Model saved to dir", final_model_folder) else: if (FLAGS.triton_model_overwrite): shutil.rmtree(final_model_folder) os.rename(saved_dir, final_model_folder) print("WARNING: Existing model was overwritten. Model dir: {}".format(final_model_folder)) else: print("ERROR: Could not save Triton model. Folder already exists. Use '--triton_model_overwrite=True' if you would like to overwrite an existing model. Model dir: {}".format(final_model_folder)) return # Now build the config for Triton. Check to make sure we can overwrite it, if it exists config_filename = os.path.join(model_folder, "config.pbtxt") optimization_str = "" if FLAGS.amp: optimization_str = r""" optimization { execution_accelerators { gpu_execution_accelerator : [ { name : "auto_mixed_precision" } ] } }""" if (os.path.exists(config_filename) and not FLAGS.triton_model_overwrite): print("ERROR: Could not save Triton model config. Config file already exists. Use '--triton_model_overwrite=True' if you would like to overwrite an existing model config. Model config: {}".format(config_filename)) return config_template = r""" name: "{model_name}" platform: "tensorflow_savedmodel" max_batch_size: {max_batch_size} {optimization_str} input [ {{ name: "unique_ids" data_type: TYPE_INT32 dims: [ 1 ] reshape: {{ shape: [ ] }} }}, {{ name: "segment_ids" data_type: TYPE_INT32 dims: {seq_length} }}, {{ name: "input_ids" data_type: TYPE_INT32 dims: {seq_length} }}, {{ name: "input_mask" data_type: TYPE_INT32 dims: {seq_length} }} ] output [ {{ name: "end_logits" data_type: TYPE_FP32 dims: {seq_length} }}, {{ name: "start_logits" data_type: TYPE_FP32 dims: {seq_length} }} ] {dynamic_batching} instance_group [ {{ count: {engine_count} }} ]""" batching_str = "" max_batch_size = FLAGS.triton_max_batch_size if (FLAGS.triton_dyn_batching_delay > 0): # Use only full and half full batches pref_batch_size = [int(max_batch_size / 2.0), max_batch_size] batching_str = r""" dynamic_batching {{ preferred_batch_size: [{0}] max_queue_delay_microseconds: {1} }}""".format(", ".join([str(x) for x in pref_batch_size]), int(FLAGS.triton_dyn_batching_delay * 1000.0)) config_values = { "model_name": model_name, "max_batch_size": max_batch_size, "seq_length": FLAGS.max_seq_length, "dynamic_batching": batching_str, "engine_count": FLAGS.triton_engine_count, "optimization_str":optimization_str, } with open(model_folder + "/config.pbtxt", "w") as file: final_config_str = config_template.format_map(config_values) file.write(final_config_str) def main(_): setup_xla_flags() tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO) dllogging = utils.dllogger_class.dllogger_class(FLAGS.dllog_path) if FLAGS.horovod: hvd.init() bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file) validate_flags_or_throw(bert_config) tf.io.gfile.makedirs(FLAGS.output_dir) tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) master_process = True training_hooks = [] global_batch_size = FLAGS.train_batch_size * FLAGS.num_accumulation_steps hvd_rank = 0 config = tf.compat.v1.ConfigProto() learning_rate = FLAGS.learning_rate if FLAGS.horovod: tf.compat.v1.logging.info("Multi-GPU training with TF Horovod") tf.compat.v1.logging.info("hvd.size() = %d hvd.rank() = %d", hvd.size(), hvd.rank()) global_batch_size = FLAGS.train_batch_size * hvd.size() * FLAGS.num_accumulation_steps learning_rate = learning_rate * hvd.size() master_process = (hvd.rank() == 0) hvd_rank = hvd.rank() config.gpu_options.visible_device_list = str(hvd.local_rank()) set_affinity(hvd.local_rank()) if hvd.size() > 1: training_hooks.append(hvd.BroadcastGlobalVariablesHook(0)) if FLAGS.use_xla: config.graph_options.optimizer_options.global_jit_level = tf.compat.v1.OptimizerOptions.ON_1 if FLAGS.amp: tf.enable_resource_variables() run_config = tf.estimator.RunConfig( model_dir=FLAGS.output_dir if master_process else None, session_config=config, save_checkpoints_steps=FLAGS.save_checkpoints_steps if master_process else None, save_summary_steps=FLAGS.save_checkpoints_steps if master_process else None, log_step_count_steps=FLAGS.display_loss_steps, keep_checkpoint_max=1) if master_process: tf.compat.v1.logging.info("***** Configuaration *****") for key in FLAGS.__flags.keys(): tf.compat.v1.logging.info(' {}: {}'.format(key, getattr(FLAGS, key))) tf.compat.v1.logging.info("**************************") train_examples = None num_train_steps = None num_warmup_steps = None training_hooks.append(LogTrainRunHook(global_batch_size, hvd_rank, FLAGS.save_checkpoints_steps)) # Prepare Training Data if FLAGS.do_train: train_examples = read_squad_examples( input_file=FLAGS.train_file, is_training=True, version_2_with_negative=FLAGS.version_2_with_negative) num_train_steps = int( len(train_examples) / global_batch_size * FLAGS.num_train_epochs) num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion) # Pre-shuffle the input to avoid having to make a very large shuffle # buffer in in the `input_fn`. rng = random.Random(12345) rng.shuffle(train_examples) start_index = 0 end_index = len(train_examples) tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record")] if FLAGS.horovod: tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record{}".format(i)) for i in range(hvd.size())] num_examples_per_rank = len(train_examples) // hvd.size() remainder = len(train_examples) % hvd.size() if hvd.rank() < remainder: start_index = hvd.rank() * (num_examples_per_rank+1) end_index = start_index + num_examples_per_rank + 1 else: start_index = hvd.rank() * num_examples_per_rank + remainder end_index = start_index + (num_examples_per_rank) model_fn = model_fn_builder( bert_config=bert_config, init_checkpoint=FLAGS.init_checkpoint, learning_rate=learning_rate, num_train_steps=num_train_steps, num_warmup_steps=num_warmup_steps, hvd=None if not FLAGS.horovod else hvd, amp=FLAGS.amp) estimator = tf.estimator.Estimator( model_fn=model_fn, config=run_config) if FLAGS.do_train: # We write to a temporary file to avoid storing very large constant tensors # in memory. train_writer = FeatureWriter( filename=tmp_filenames[hvd_rank], is_training=True) convert_examples_to_features( examples=train_examples[start_index:end_index], tokenizer=tokenizer, max_seq_length=FLAGS.max_seq_length, doc_stride=FLAGS.doc_stride, max_query_length=FLAGS.max_query_length, is_training=True, output_fn=train_writer.process_feature, verbose_logging=FLAGS.verbose_logging) train_writer.close() tf.compat.v1.logging.info("***** Running training *****") tf.compat.v1.logging.info(" Num orig examples = %d", end_index - start_index) tf.compat.v1.logging.info(" Num split examples = %d", train_writer.num_features) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.train_batch_size) tf.compat.v1.logging.info(" Num steps = %d", num_train_steps) tf.compat.v1.logging.info(" LR = %f", learning_rate) del train_examples train_input_fn = input_fn_builder( input_file=tmp_filenames, batch_size=FLAGS.train_batch_size, seq_length=FLAGS.max_seq_length, is_training=True, drop_remainder=True, hvd=None if not FLAGS.horovod else hvd) train_start_time = time.time() estimator.train(input_fn=train_input_fn, hooks=training_hooks, max_steps=num_train_steps) train_time_elapsed = time.time() - train_start_time train_time_wo_overhead = training_hooks[-1].total_time avg_sentences_per_second = num_train_steps * global_batch_size * 1.0 / train_time_elapsed ss_sentences_per_second = (num_train_steps - training_hooks[-1].skipped) * global_batch_size * 1.0 / train_time_wo_overhead if master_process: tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Training Time = %0.2f for Sentences = %d", train_time_elapsed, num_train_steps * global_batch_size) tf.compat.v1.logging.info("Total Training Time W/O Overhead = %0.2f for Sentences = %d", train_time_wo_overhead, (num_train_steps - training_hooks[-1].skipped) * global_batch_size) tf.compat.v1.logging.info("Throughput Average (sentences/sec) with overhead = %0.2f", avg_sentences_per_second) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_train": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") if FLAGS.export_triton and master_process: export_model(estimator, FLAGS.output_dir, FLAGS.init_checkpoint) if FLAGS.do_predict and master_process: eval_examples = read_squad_examples( input_file=FLAGS.predict_file, is_training=False, version_2_with_negative=FLAGS.version_2_with_negative) # Perform evaluation on subset, useful for profiling if FLAGS.num_eval_iterations is not None: eval_examples = eval_examples[:FLAGS.num_eval_iterations*FLAGS.predict_batch_size] eval_writer = FeatureWriter( filename=os.path.join(FLAGS.output_dir, "eval.tf_record"), is_training=False) eval_features = [] def append_feature(feature): eval_features.append(feature) eval_writer.process_feature(feature) convert_examples_to_features( examples=eval_examples, tokenizer=tokenizer, max_seq_length=FLAGS.max_seq_length, doc_stride=FLAGS.doc_stride, max_query_length=FLAGS.max_query_length, is_training=False, output_fn=append_feature, verbose_logging=FLAGS.verbose_logging) eval_writer.close() tf.compat.v1.logging.info("***** Running predictions *****") tf.compat.v1.logging.info(" Num orig examples = %d", len(eval_examples)) tf.compat.v1.logging.info(" Num split examples = %d", len(eval_features)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.predict_batch_size) predict_input_fn = input_fn_builder( input_file=eval_writer.filename, batch_size=FLAGS.predict_batch_size, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=False) all_results = [] eval_hooks = [LogEvalRunHook(FLAGS.predict_batch_size)] eval_start_time = time.time() for result in estimator.predict( predict_input_fn, yield_single_examples=True, hooks=eval_hooks): if len(all_results) % 1000 == 0: tf.compat.v1.logging.info("Processing example: %d" % (len(all_results))) unique_id = int(result["unique_ids"]) start_logits = [float(x) for x in result["start_logits"].flat] end_logits = [float(x) for x in result["end_logits"].flat] all_results.append( RawResult( unique_id=unique_id, start_logits=start_logits, end_logits=end_logits)) eval_time_elapsed = time.time() - eval_start_time time_list = eval_hooks[-1].time_list time_list.sort() # Removing outliers (init/warmup) in throughput computation. eval_time_wo_overhead = sum(time_list[:int(len(time_list) * 0.99)]) num_sentences = (int(len(time_list) * 0.99)) * FLAGS.predict_batch_size avg = np.mean(time_list) cf_50 = max(time_list[:int(len(time_list) * 0.50)]) cf_90 = max(time_list[:int(len(time_list) * 0.90)]) cf_95 = max(time_list[:int(len(time_list) * 0.95)]) cf_99 = max(time_list[:int(len(time_list) * 0.99)]) cf_100 = max(time_list[:int(len(time_list) * 1)]) ss_sentences_per_second = num_sentences * 1.0 / eval_time_wo_overhead tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Inference Time = %0.2f for Sentences = %d", eval_time_elapsed, eval_hooks[-1].count * FLAGS.predict_batch_size) tf.compat.v1.logging.info("Total Inference Time W/O Overhead = %0.2f for Sentences = %d", eval_time_wo_overhead, num_sentences) tf.compat.v1.logging.info("Summary Inference Statistics") tf.compat.v1.logging.info("Batch size = %d", FLAGS.predict_batch_size) tf.compat.v1.logging.info("Sequence Length = %d", FLAGS.max_seq_length) tf.compat.v1.logging.info("Precision = %s", "fp16" if FLAGS.amp else "fp32") tf.compat.v1.logging.info("Latency Confidence Level 50 (ms) = %0.2f", cf_50 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 90 (ms) = %0.2f", cf_90 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 95 (ms) = %0.2f", cf_95 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 99 (ms) = %0.2f", cf_99 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 100 (ms) = %0.2f", cf_100 * 1000) tf.compat.v1.logging.info("Latency Average (ms) = %0.2f", avg * 1000) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_val": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") output_prediction_file = os.path.join(FLAGS.output_dir, "predictions.json") output_nbest_file = os.path.join(FLAGS.output_dir, "nbest_predictions.json") output_null_log_odds_file = os.path.join(FLAGS.output_dir, "null_odds.json") write_predictions(eval_examples, eval_features, all_results, FLAGS.n_best_size, FLAGS.max_answer_length, FLAGS.do_lower_case, output_prediction_file, output_nbest_file, output_null_log_odds_file, FLAGS.version_2_with_negative, FLAGS.verbose_logging) if FLAGS.eval_script: import sys import subprocess eval_out = subprocess.check_output([sys.executable, FLAGS.eval_script, FLAGS.predict_file, output_prediction_file]) scores = str(eval_out).strip() exact_match = float(scores.split(":")[1].split(",")[0]) f1 = float(scores.split(":")[2].split("}")[0]) dllogging.logger.log(step=(), data={"f1": f1}, verbosity=Verbosity.DEFAULT) dllogging.logger.log(step=(), data={"exact_match": exact_match}, verbosity=Verbosity.DEFAULT) print(str(eval_out)) if __name__ == "__main__": FLAGS = extract_run_squad_flags() tf.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/run_squad.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/__init__.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors and The HugginFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tokenization classes.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import unicodedata import six import tensorflow as tf import re import os PRETRAINED_VOCAB_ARCHIVE_MAP = { 'bert-base-uncased': "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt", 'bert-large-uncased': "https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-uncased-vocab.txt", 'bert-base-cased': "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-cased-vocab.txt", 'bert-large-cased': "https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-cased-vocab.txt", 'bert-base-multilingual-uncased': "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-multilingual-uncased-vocab.txt", 'bert-base-multilingual-cased': "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-multilingual-cased-vocab.txt", 'bert-base-chinese': "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-chinese-vocab.txt", } def validate_case_matches_checkpoint(do_lower_case, init_checkpoint): """Checks whether the casing config is consistent with the checkpoint name.""" # The casing has to be passed in by the user and there is no explicit check # as to whether it matches the checkpoint. The casing information probably # should have been stored in the bert_config.json file, but it's not, so # we have to heuristically detect it to validate. if not init_checkpoint: return m = re.match("^.*?([A-Za-z0-9_-]+)/bert_model.ckpt", init_checkpoint) if m is None: return model_name = m.group(1) lower_models = [ "uncased_L-24_H-1024_A-16", "uncased_L-12_H-768_A-12", "multilingual_L-12_H-768_A-12", "chinese_L-12_H-768_A-12" ] cased_models = [ "cased_L-12_H-768_A-12", "cased_L-24_H-1024_A-16", "multi_cased_L-12_H-768_A-12" ] is_bad_config = False if model_name in lower_models and not do_lower_case: is_bad_config = True actual_flag = "False" case_name = "lowercased" opposite_flag = "True" if model_name in cased_models and do_lower_case: is_bad_config = True actual_flag = "True" case_name = "cased" opposite_flag = "False" if is_bad_config: raise ValueError( "You passed in `--do_lower_case=%s` with `--init_checkpoint=%s`. " "However, `%s` seems to be a %s model, so you " "should pass in `--do_lower_case=%s` so that the fine-tuning matches " "how the model was pre-training. If this error is wrong, please " "just comment out this check." % (actual_flag, init_checkpoint, model_name, case_name, opposite_flag)) def convert_to_unicode(text): """Converts `text` to Unicode (if it's not already), assuming utf-8 input.""" if isinstance(text, str): return text elif isinstance(text, bytes): return text.decode("utf-8", "ignore") else: raise ValueError("Unsupported string type: %s" % (type(text))) def printable_text(text): """Returns text encoded in a way suitable for print or `tf.logging`.""" # These functions want `str` for both Python2 and Python3, but in one case # it's a Unicode string and in the other it's a byte string. if isinstance(text, str): return text elif isinstance(text, bytes): return text.decode("utf-8", "ignore") else: raise ValueError("Unsupported string type: %s" % (type(text))) def load_vocab(vocab_file): """Loads a vocabulary file into a dictionary.""" vocab = collections.OrderedDict() index = 0 with open(vocab_file, "r") as reader: while True: token = convert_to_unicode(reader.readline()) if not token: break token = token.strip() vocab[token] = index index += 1 return vocab def convert_by_vocab(vocab, items): """Converts a sequence of [tokens|ids] using the vocab.""" output = [] for item in items: output.append(vocab[item]) return output def whitespace_tokenize(text): """Runs basic whitespace cleaning and splitting on a peice of text.""" text = text.strip() if not text: return [] tokens = text.split() return tokens class FullTokenizer(object): """Runs end-to-end tokenziation.""" def __init__(self, vocab_file, do_lower_case=True): self.vocab = load_vocab(vocab_file) self.inv_vocab = {v: k for k, v in self.vocab.items()} self.basic_tokenizer = BasicTokenizer(do_lower_case=do_lower_case) self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.vocab) def tokenize(self, text): split_tokens = [] for token in self.basic_tokenizer.tokenize(text): for sub_token in self.wordpiece_tokenizer.tokenize(token): split_tokens.append(sub_token) return split_tokens def convert_tokens_to_ids(self, tokens): return convert_by_vocab(self.vocab, tokens) def convert_ids_to_tokens(self, ids): return convert_by_vocab(self.inv_vocab, ids) class BertTokenizer(object): """Runs end-to-end tokenization: punctuation splitting + wordpiece""" def __init__(self, vocab_file, do_lower_case=True): if not os.path.isfile(vocab_file): raise ValueError( "Can't find a vocabulary file at path '{}'. To load the vocabulary from a Google pretrained " "model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`".format(vocab_file)) self.vocab = load_vocab(vocab_file) self.ids_to_tokens = collections.OrderedDict( [(ids, tok) for tok, ids in self.vocab.items()]) self.basic_tokenizer = BasicTokenizer(do_lower_case=do_lower_case) self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.vocab) def tokenize(self, text): split_tokens = [] for token in self.basic_tokenizer.tokenize(text): for sub_token in self.wordpiece_tokenizer.tokenize(token): split_tokens.append(sub_token) return split_tokens def convert_tokens_to_ids(self, tokens): """Converts a sequence of tokens into ids using the vocab.""" ids = [] for token in tokens: ids.append(self.vocab[token]) return ids def convert_ids_to_tokens(self, ids): """Converts a sequence of ids in wordpiece tokens using the vocab.""" tokens = [] for i in ids: tokens.append(self.ids_to_tokens[i]) return tokens @classmethod def from_pretrained(cls, pretrained_model_name, do_lower_case=True): """ Instantiate a PreTrainedBertModel from a pre-trained model file. Download and cache the pre-trained model file if needed. """ if pretrained_model_name in PRETRAINED_VOCAB_ARCHIVE_MAP: vocab_file = PRETRAINED_VOCAB_ARCHIVE_MAP[pretrained_model_name] else: vocab_file = pretrained_model_name # redirect to the cache, if necessary try: resolved_vocab_file = cached_path(vocab_file) if resolved_vocab_file == vocab_file: logger.info("loading vocabulary file {}".format(vocab_file)) else: logger.info("loading vocabulary file {} from cache at {}".format( vocab_file, resolved_vocab_file)) # Instantiate tokenizer. tokenizer = cls(resolved_vocab_file, do_lower_case) except FileNotFoundError: logger.error( "Model name '{}' was not found in model name list ({}). " "We assumed '{}' was a path or url but couldn't find any file " "associated to this path or url.".format( pretrained_model_name, ', '.join(PRETRAINED_VOCAB_ARCHIVE_MAP.keys()), pretrained_model_name)) tokenizer = None return tokenizer class BasicTokenizer(object): """Runs basic tokenization (punctuation splitting, lower casing, etc.).""" def __init__(self, do_lower_case=True): """Constructs a BasicTokenizer. Args: do_lower_case: Whether to lower case the input. """ self.do_lower_case = do_lower_case def tokenize(self, text): """Tokenizes a piece of text.""" text = convert_to_unicode(text) text = self._clean_text(text) # This was added on November 1st, 2018 for the multilingual and Chinese # models. This is also applied to the English models now, but it doesn't # matter since the English models were not trained on any Chinese data # and generally don't have any Chinese data in them (there are Chinese # characters in the vocabulary because Wikipedia does have some Chinese # words in the English Wikipedia.). text = self._tokenize_chinese_chars(text) orig_tokens = whitespace_tokenize(text) split_tokens = [] for token in orig_tokens: if self.do_lower_case: token = token.lower() token = self._run_strip_accents(token) split_tokens.extend(self._run_split_on_punc(token)) output_tokens = whitespace_tokenize(" ".join(split_tokens)) return output_tokens def _run_strip_accents(self, text): """Strips accents from a piece of text.""" text = unicodedata.normalize("NFD", text) output = [] for char in text: cat = unicodedata.category(char) if cat == "Mn": continue output.append(char) return "".join(output) def _run_split_on_punc(self, text): """Splits punctuation on a piece of text.""" chars = list(text) i = 0 start_new_word = True output = [] while i < len(chars): char = chars[i] if _is_punctuation(char): output.append([char]) start_new_word = True else: if start_new_word: output.append([]) start_new_word = False output[-1].append(char) i += 1 return ["".join(x) for x in output] def _tokenize_chinese_chars(self, text): """Adds whitespace around any CJK character.""" output = [] for char in text: cp = ord(char) if self._is_chinese_char(cp): output.append(" ") output.append(char) output.append(" ") else: output.append(char) return "".join(output) def _is_chinese_char(self, cp): """Checks whether CP is the codepoint of a CJK character.""" # This defines a "chinese character" as anything in the CJK Unicode block: # https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) # # Note that the CJK Unicode block is NOT all Japanese and Korean characters, # despite its name. The modern Korean Hangul alphabet is a different block, # as is Japanese Hiragana and Katakana. Those alphabets are used to write # space-separated words, so they are not treated specially and handled # like the all of the other languages. if ((cp >= 0x4E00 and cp <= 0x9FFF) or # (cp >= 0x3400 and cp <= 0x4DBF) or # (cp >= 0x20000 and cp <= 0x2A6DF) or # (cp >= 0x2A700 and cp <= 0x2B73F) or # (cp >= 0x2B740 and cp <= 0x2B81F) or # (cp >= 0x2B820 and cp <= 0x2CEAF) or (cp >= 0xF900 and cp <= 0xFAFF) or # (cp >= 0x2F800 and cp <= 0x2FA1F)): # return True return False def _clean_text(self, text): """Performs invalid character removal and whitespace cleanup on text.""" output = [] for char in text: cp = ord(char) if cp == 0 or cp == 0xfffd or _is_control(char): continue if _is_whitespace(char): output.append(" ") else: output.append(char) return "".join(output) class WordpieceTokenizer(object): """Runs WordPiece tokenization.""" def __init__(self, vocab, unk_token="[UNK]", max_input_chars_per_word=100): self.vocab = vocab self.unk_token = unk_token self.max_input_chars_per_word = max_input_chars_per_word def tokenize(self, text): """Tokenizes a piece of text into its word pieces. This uses a greedy longest-match-first algorithm to perform tokenization using the given vocabulary. For example: input = "unaffable" output = ["un", "##aff", "##able"] Args: text: A single token or whitespace separated tokens. This should have already been passed through `BasicTokenizer. Returns: A list of wordpiece tokens. """ text = convert_to_unicode(text) output_tokens = [] for token in whitespace_tokenize(text): chars = list(token) if len(chars) > self.max_input_chars_per_word: output_tokens.append(self.unk_token) continue is_bad = False start = 0 sub_tokens = [] while start < len(chars): end = len(chars) cur_substr = None while start < end: substr = "".join(chars[start:end]) if start > 0: substr = "##" + substr if substr in self.vocab: cur_substr = substr break end -= 1 if cur_substr is None: is_bad = True break sub_tokens.append(cur_substr) start = end if is_bad: output_tokens.append(self.unk_token) else: output_tokens.extend(sub_tokens) return output_tokens def _is_whitespace(char): """Checks whether `chars` is a whitespace character.""" # \t, \n, and \r are technically contorl characters but we treat them # as whitespace since they are generally considered as such. if char == " " or char == "\t" or char == "\n" or char == "\r": return True cat = unicodedata.category(char) if cat == "Zs": return True return False def _is_control(char): """Checks whether `chars` is a control character.""" # These are technically control characters but we count them as whitespace # characters. if char == "\t" or char == "\n" or char == "\r": return False cat = unicodedata.category(char) if cat.startswith("C"): return True return False def _is_punctuation(char): """Checks whether `chars` is a punctuation character.""" cp = ord(char) # We treat all non-letter/number ASCII as punctuation. # Characters such as "^", "$", and "`" are not in the Unicode # Punctuation class but we treat them as punctuation anyways, for # consistency. if ((cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126)): return True cat = unicodedata.category(char) if cat.startswith("P"): return True return False
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/tokenization.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Run masked LM/next sentence masked_lm pre-training for BERT.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import time import modeling import optimization import tensorflow as tf import glob from utils.utils import LogEvalRunHook, setup_xla_flags import utils.dllogger_class from utils.gpu_affinity import set_affinity from dllogger import Verbosity from tensorflow.core.protobuf import rewriter_config_pb2 flags = tf.flags FLAGS = flags.FLAGS ## Required parameters flags.DEFINE_string( "bert_config_file", None, "The config json file corresponding to the pre-trained BERT model. " "This specifies the model architecture.") flags.DEFINE_string( "input_files_dir", None, "Directory with input files, comma separated or single directory.") flags.DEFINE_string( "eval_files_dir", None, "Directory with eval files, comma separated or single directory. ") flags.DEFINE_string( "output_dir", None, "The output directory where the model checkpoints will be written.") ## Other parameters flags.DEFINE_string( "dllog_path", "/results/bert_dllog.json", "filename where dllogger writes to") flags.DEFINE_string( "init_checkpoint", None, "Initial checkpoint (usually from a pre-trained BERT model).") flags.DEFINE_string( "optimizer_type", "lamb", "Optimizer used for training - LAMB or ADAM") flags.DEFINE_integer( "max_seq_length", 512, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded. Must match data generation.") flags.DEFINE_integer( "max_predictions_per_seq", 80, "Maximum number of masked LM predictions per sequence. " "Must match data generation.") flags.DEFINE_bool("do_train", False, "Whether to run training.") flags.DEFINE_bool("do_eval", False, "Whether to run eval on the dev set.") flags.DEFINE_integer("train_batch_size", 32, "Total batch size for training.") flags.DEFINE_integer("eval_batch_size", 8, "Total batch size for eval.") flags.DEFINE_float("learning_rate", 5e-5, "The initial learning rate for Adam.") flags.DEFINE_integer("num_train_steps", 100000, "Number of training steps.") flags.DEFINE_integer("num_warmup_steps", 10000, "Number of warmup steps.") flags.DEFINE_integer("save_checkpoints_steps", 1000, "How often to save the model checkpoint.") flags.DEFINE_integer("display_loss_steps", 1, "How often to print loss") flags.DEFINE_integer("iterations_per_loop", 1000, "How many steps to make in each estimator call.") flags.DEFINE_integer("max_eval_steps", 100, "Maximum number of eval steps.") flags.DEFINE_integer("num_accumulation_steps", 1, "Number of accumulation steps before gradient update." "Global batch size = num_accumulation_steps * train_batch_size") flags.DEFINE_bool("allreduce_post_accumulation", False, "Whether to all reduce after accumulation of N steps or after each step") flags.DEFINE_bool( "verbose_logging", False, "If true, all of the trainable parameters are printed") flags.DEFINE_bool("horovod", False, "Whether to use Horovod for multi-gpu runs") flags.DEFINE_bool("report_loss", True, "Whether to report total loss during training.") flags.DEFINE_bool("manual_fp16", False, "Whether to use fp32 or fp16 arithmetic on GPU. " "Manual casting is done instead of using AMP") flags.DEFINE_bool("amp", True, "Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.") flags.DEFINE_bool("use_xla", True, "Whether to enable XLA JIT compilation.") flags.DEFINE_integer("init_loss_scale", 2**32, "Initial value of loss scale if mixed precision training") # report samples/sec, total loss and learning rate during training class _LogSessionRunHook(tf.estimator.SessionRunHook): def __init__(self, global_batch_size, num_accumulation_steps, dllogging, display_every=10, save_ckpt_steps=1000, report_loss=True, hvd_rank=-1): self.global_batch_size = global_batch_size self.display_every = display_every self.save_ckpt_steps = save_ckpt_steps self.hvd_rank = hvd_rank self.num_accumulation_steps = num_accumulation_steps self.dllogging = dllogging self.report_loss = report_loss def after_create_session(self, session, coord): self.elapsed_secs = 0.0 #elapsed seconds between every print self.count = 0 # number of global steps between every print self.all_count = 0 #number of steps (including accumulation) between every print self.loss = 0.0 # accumulation of loss in each step between every print self.total_time = 0.0 # total time taken to train (excluding warmup + ckpt saving steps) self.step_time = 0.0 # time taken per step self.init_global_step = session.run(tf.train.get_global_step()) # training starts at init_global_step self.skipped = 0 self.final_loss = 0 def before_run(self, run_context): self.t0 = time.time() if self.num_accumulation_steps <= 1: if FLAGS.manual_fp16 or FLAGS.amp: return tf.estimator.SessionRunArgs( fetches=['step_update:0', 'total_loss:0', 'learning_rate:0', 'nsp_loss:0', 'mlm_loss:0', 'loss_scale:0']) else: return tf.estimator.SessionRunArgs( fetches=['step_update:0', 'total_loss:0', 'learning_rate:0', 'nsp_loss:0', 'mlm_loss:0']) else: if FLAGS.manual_fp16 or FLAGS.amp: return tf.estimator.SessionRunArgs( fetches=['step_update:0', 'update_step:0', 'total_loss:0', 'learning_rate:0', 'nsp_loss:0', 'mlm_loss:0', 'loss_scale:0']) else: return tf.estimator.SessionRunArgs( fetches=['step_update:0', 'update_step:0', 'total_loss:0', 'learning_rate:0', 'nsp_loss:0', 'mlm_loss:0']) def after_run(self, run_context, run_values): run_time = time.time() - self.t0 if self.num_accumulation_steps <=1: if FLAGS.manual_fp16 or FLAGS.amp: self.global_step, total_loss, lr, nsp_loss, mlm_loss, loss_scaler = run_values.results else: self.global_step, total_loss, lr, nsp_loss, mlm_loss = run_values. \ results update_step = True else: if FLAGS.manual_fp16 or FLAGS.amp: self.global_step, update_step, total_loss, lr, nsp_loss, mlm_loss, loss_scaler = run_values.results else: self.global_step, update_step, total_loss, lr, nsp_loss, mlm_loss = run_values.\ results self.elapsed_secs += run_time self.step_time += run_time print_step = self.global_step + 1 # One-based index for printing. self.loss += total_loss self.all_count += 1 if update_step: self.count += 1 # Removing first six steps after every checkpoint save from timing if (self.global_step - self.init_global_step) % self.save_ckpt_steps < 6: print("Skipping time record for ", self.global_step, " due to checkpoint-saving/warmup overhead") self.skipped += 1 else: self.total_time += self.step_time self.step_time = 0.0 #Reset Step Time if (print_step == 1 or print_step % self.display_every == 0): dt = self.elapsed_secs / self.count sent_per_sec = self.global_batch_size / dt avg_loss_step = self.loss / self.all_count if self.hvd_rank >= 0 and FLAGS.report_loss: if FLAGS.manual_fp16 or FLAGS.amp: self.dllogging.logger.log(step=(print_step), data={"Rank": int(self.hvd_rank), "throughput_train": float(sent_per_sec), "mlm_loss":float(mlm_loss), "nsp_loss":float(nsp_loss), "total_loss":float(total_loss), "avg_loss_step":float(avg_loss_step), "learning_rate": str(lr), "loss_scaler":int(loss_scaler)}, verbosity=Verbosity.DEFAULT) else: self.dllogging.logger.log(step=int(print_step), data={"Rank": int(self.hvd_rank), "throughput_train": float(sent_per_sec), "mlm_loss":float(mlm_loss), "nsp_loss":float(nsp_loss), "total_loss":float(total_loss), "avg_loss_step":float(avg_loss_step), "learning_rate": str(lr)}, verbosity=Verbosity.DEFAULT) else: if FLAGS.manual_fp16 or FLAGS.amp: self.dllogging.logger.log(step=int(print_step), data={"throughput_train": float(sent_per_sec), "mlm_loss":float(mlm_loss), "nsp_loss":float(nsp_loss), "total_loss":float(total_loss), "avg_loss_step":float(avg_loss_step), "learning_rate": str(lr), "loss_scaler":int(loss_scaler)}, verbosity=Verbosity.DEFAULT) else: self.dllogging.logger.log(step=int(print_step), data={"throughput_train": float(sent_per_sec), "mlm_loss":float(mlm_loss), "nsp_loss":float(nsp_loss), "total_loss":float(total_loss), "avg_loss_step":float(avg_loss_step), "learning_rate": str(lr)}, verbosity=Verbosity.DEFAULT) self.elapsed_secs = 0.0 self.count = 0 self.loss = 0.0 self.all_count = 0 self.final_loss = avg_loss_step def model_fn_builder(bert_config, init_checkpoint, learning_rate, num_train_steps, num_warmup_steps, use_one_hot_embeddings, hvd=None): """Returns `model_fn` closure for TPUEstimator.""" def model_fn(features, labels, mode, params): # pylint: disable=unused-argument """The `model_fn` for TPUEstimator.""" tf.compat.v1.logging.info("*** Features ***") for name in sorted(features.keys()): tf.compat.v1.logging.info(" name = %s, shape = %s" % (name, features[name].shape)) input_ids = features["input_ids"] input_mask = features["input_mask"] segment_ids = features["segment_ids"] masked_lm_positions = features["masked_lm_positions"] masked_lm_ids = features["masked_lm_ids"] masked_lm_weights = features["masked_lm_weights"] next_sentence_labels = features["next_sentence_labels"] is_training = (mode == tf.estimator.ModeKeys.TRAIN) model = modeling.BertModel( config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings, compute_type=tf.float16 if FLAGS.manual_fp16 else tf.float32) (masked_lm_loss, masked_lm_example_loss, masked_lm_log_probs) = get_masked_lm_output( bert_config, model.get_sequence_output(), model.get_embedding_table(), masked_lm_positions, masked_lm_ids, masked_lm_weights) (next_sentence_loss, next_sentence_example_loss, next_sentence_log_probs) = get_next_sentence_output( bert_config, model.get_pooled_output(), next_sentence_labels) masked_lm_loss = tf.identity(masked_lm_loss, name="mlm_loss") next_sentence_loss = tf.identity(next_sentence_loss, name="nsp_loss") total_loss = masked_lm_loss + next_sentence_loss total_loss = tf.identity(total_loss, name='total_loss') tvars = tf.trainable_variables() initialized_variable_names = {} if init_checkpoint and (hvd is None or hvd.rank() == 0): print("Loading checkpoint", init_checkpoint) (assignment_map, initialized_variable_names ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) if FLAGS.verbose_logging: tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" tf.compat.v1.logging.info(" %d :: name = %s, shape = %s%s", 0 if hvd is None else hvd.rank(), var.name, var.shape, init_string) output_spec = None if mode == tf.estimator.ModeKeys.TRAIN: train_op = optimization.create_optimizer( total_loss, learning_rate, num_train_steps, num_warmup_steps, hvd, FLAGS.manual_fp16, FLAGS.amp, FLAGS.num_accumulation_steps, FLAGS.optimizer_type, FLAGS.allreduce_post_accumulation, FLAGS.init_loss_scale) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, train_op=train_op) elif mode == tf.estimator.ModeKeys.EVAL: def metric_fn(masked_lm_example_loss, masked_lm_log_probs, masked_lm_ids, masked_lm_weights, next_sentence_example_loss, next_sentence_log_probs, next_sentence_labels): """Computes the loss and accuracy of the model.""" masked_lm_log_probs = tf.reshape(masked_lm_log_probs, [-1, masked_lm_log_probs.shape[-1]]) masked_lm_predictions = tf.argmax( masked_lm_log_probs, axis=-1, output_type=tf.int32) masked_lm_example_loss = tf.reshape(masked_lm_example_loss, [-1]) masked_lm_ids = tf.reshape(masked_lm_ids, [-1]) masked_lm_weights = tf.reshape(masked_lm_weights, [-1]) masked_lm_accuracy = tf.metrics.accuracy( labels=masked_lm_ids, predictions=masked_lm_predictions, weights=masked_lm_weights) masked_lm_mean_loss = tf.metrics.mean( values=masked_lm_example_loss, weights=masked_lm_weights) next_sentence_log_probs = tf.reshape( next_sentence_log_probs, [-1, next_sentence_log_probs.shape[-1]]) next_sentence_predictions = tf.argmax( next_sentence_log_probs, axis=-1, output_type=tf.int32) next_sentence_labels = tf.reshape(next_sentence_labels, [-1]) next_sentence_accuracy = tf.metrics.accuracy( labels=next_sentence_labels, predictions=next_sentence_predictions) next_sentence_mean_loss = tf.metrics.mean( values=next_sentence_example_loss) return { "masked_lm_accuracy": masked_lm_accuracy, "masked_lm_loss": masked_lm_mean_loss, "next_sentence_accuracy": next_sentence_accuracy, "next_sentence_loss": next_sentence_mean_loss, } eval_metric_ops = metric_fn( masked_lm_example_loss, masked_lm_log_probs, masked_lm_ids, masked_lm_weights, next_sentence_example_loss, next_sentence_log_probs, next_sentence_labels ) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, eval_metric_ops=eval_metric_ops) else: raise ValueError("Only TRAIN and EVAL modes are supported: %s" % (mode)) return output_spec return model_fn def get_masked_lm_output(bert_config, input_tensor, output_weights, positions, label_ids, label_weights): """Get loss and log probs for the masked LM.""" input_tensor = gather_indexes(input_tensor, positions) with tf.variable_scope("cls/predictions"): # We apply one more non-linear transformation before the output layer. # This matrix is not used after pre-training. with tf.variable_scope("transform"): input_tensor = tf.layers.dense( input_tensor, units=bert_config.hidden_size, activation=modeling.get_activation(bert_config.hidden_act), kernel_initializer=modeling.create_initializer( bert_config.initializer_range)) input_tensor = modeling.layer_norm(input_tensor) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. output_bias = tf.get_variable( "output_bias", shape=[bert_config.vocab_size], initializer=tf.zeros_initializer()) logits = tf.matmul(tf.cast(input_tensor, tf.float32), output_weights, transpose_b=True) logits = tf.nn.bias_add(logits, output_bias) log_probs = tf.nn.log_softmax(logits, axis=-1) label_ids = tf.reshape(label_ids, [-1]) label_weights = tf.reshape(label_weights, [-1]) one_hot_labels = tf.one_hot( label_ids, depth=bert_config.vocab_size, dtype=tf.float32) # The `positions` tensor might be zero-padded (if the sequence is too # short to have the maximum number of predictions). The `label_weights` # tensor has a value of 1.0 for every real prediction and 0.0 for the # padding predictions. per_example_loss = -tf.reduce_sum(log_probs * one_hot_labels, axis=[-1]) numerator = tf.reduce_sum(label_weights * per_example_loss) denominator = tf.reduce_sum(label_weights) + 1e-5 loss = numerator / denominator return (loss, per_example_loss, log_probs) def get_next_sentence_output(bert_config, input_tensor, labels): """Get loss and log probs for the next sentence prediction.""" # Simple binary classification. Note that 0 is "next sentence" and 1 is # "random sentence". This weight matrix is not used after pre-training. with tf.variable_scope("cls/seq_relationship"): output_weights = tf.get_variable( "output_weights", shape=[2, bert_config.hidden_size], initializer=modeling.create_initializer(bert_config.initializer_range)) output_bias = tf.get_variable( "output_bias", shape=[2], initializer=tf.zeros_initializer()) logits = tf.matmul(tf.cast(input_tensor, tf.float32), output_weights, transpose_b=True) logits = tf.nn.bias_add(logits, output_bias) log_probs = tf.nn.log_softmax(logits, axis=-1) labels = tf.reshape(labels, [-1]) one_hot_labels = tf.one_hot(labels, depth=2, dtype=tf.float32) per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1) loss = tf.reduce_mean(per_example_loss) return (loss, per_example_loss, log_probs) def gather_indexes(sequence_tensor, positions): """Gathers the vectors at the specific positions over a minibatch.""" sequence_shape = modeling.get_shape_list(sequence_tensor, expected_rank=3) batch_size = sequence_shape[0] seq_length = sequence_shape[1] width = sequence_shape[2] flat_offsets = tf.reshape( tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1]) flat_positions = tf.reshape(positions + flat_offsets, [-1]) flat_sequence_tensor = tf.reshape(sequence_tensor, [batch_size * seq_length, width]) output_tensor = tf.gather(flat_sequence_tensor, flat_positions) return output_tensor def input_fn_builder(input_files, batch_size, max_seq_length, max_predictions_per_seq, is_training, num_cpu_threads=4, hvd=None): """Creates an `input_fn` closure to be passed to Estimator.""" def input_fn(): """The actual input function.""" name_to_features = { "input_ids": tf.io.FixedLenFeature([max_seq_length], tf.int64), "input_mask": tf.io.FixedLenFeature([max_seq_length], tf.int64), "segment_ids": tf.io.FixedLenFeature([max_seq_length], tf.int64), "masked_lm_positions": tf.io.FixedLenFeature([max_predictions_per_seq], tf.int64), "masked_lm_ids": tf.io.FixedLenFeature([max_predictions_per_seq], tf.int64), "masked_lm_weights": tf.io.FixedLenFeature([max_predictions_per_seq], tf.float32), "next_sentence_labels": tf.io.FixedLenFeature([1], tf.int64), } # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. if is_training: d = tf.data.Dataset.from_tensor_slices(tf.constant(input_files)) if hvd is not None: d = d.shard(hvd.size(), hvd.rank()) d = d.repeat() d = d.shuffle(buffer_size=len(input_files)) # `cycle_length` is the number of parallel files that get read. cycle_length = min(num_cpu_threads, len(input_files)) # `sloppy` mode means that the interleaving is not exact. This adds # even more randomness to the training pipeline. d = d.apply( tf.contrib.data.parallel_interleave( tf.data.TFRecordDataset, sloppy=is_training, cycle_length=cycle_length)) d = d.shuffle(buffer_size=100) else: d = tf.data.TFRecordDataset(input_files) # Since we evaluate for a fixed number of steps we don't want to encounter # out-of-range exceptions. d = d.repeat() # We must `drop_remainder` on training because the TPU requires fixed # size dimensions. For eval, we assume we are evaluating on the CPU or GPU # and we *don't* want to drop the remainder, otherwise we wont cover # every sample. d = d.apply( tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, num_parallel_batches=num_cpu_threads, drop_remainder=True if is_training else False)) return d return input_fn def _decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.to_int32(t) example[name] = t return example def main(_): setup_xla_flags() tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO) dllogging = utils.dllogger_class.dllogger_class(FLAGS.dllog_path) if not FLAGS.do_train and not FLAGS.do_eval: raise ValueError("At least one of `do_train` or `do_eval` must be True.") if FLAGS.horovod: import horovod.tensorflow as hvd hvd.init() bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file) tf.io.gfile.makedirs(FLAGS.output_dir) input_files = [] for input_file_dir in FLAGS.input_files_dir.split(","): input_files.extend(tf.io.gfile.glob(os.path.join(input_file_dir, "*"))) if FLAGS.horovod and len(input_files) < hvd.size(): raise ValueError("Input Files must be sharded") if FLAGS.amp and FLAGS.manual_fp16: raise ValueError("AMP and Manual Mixed Precision Training are both activated! Error") is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2 config = tf.compat.v1.ConfigProto() if FLAGS.horovod: config.gpu_options.visible_device_list = str(hvd.local_rank()) set_affinity(hvd.local_rank()) if hvd.rank() == 0: tf.compat.v1.logging.info("***** Configuaration *****") for key in FLAGS.__flags.keys(): tf.compat.v1.logging.info(' {}: {}'.format(key, getattr(FLAGS, key))) tf.compat.v1.logging.info("**************************") # config.gpu_options.per_process_gpu_memory_fraction = 0.7 if FLAGS.use_xla: config.graph_options.optimizer_options.global_jit_level = tf.compat.v1.OptimizerOptions.ON_1 config.graph_options.rewrite_options.memory_optimization = rewriter_config_pb2.RewriterConfig.NO_MEM_OPT if FLAGS.amp: tf.enable_resource_variables() run_config = tf.estimator.RunConfig( model_dir=FLAGS.output_dir, session_config=config, save_checkpoints_steps=FLAGS.save_checkpoints_steps if not FLAGS.horovod or hvd.rank() == 0 else None, save_summary_steps=FLAGS.save_checkpoints_steps if not FLAGS.horovod or hvd.rank() == 0 else None, # This variable controls how often estimator reports examples/sec. # Default value is every 100 steps. # When --report_loss is True, we set to very large value to prevent # default info reporting from estimator. # Ideally we should set it to None, but that does not work. log_step_count_steps=10000 if FLAGS.report_loss else 100) model_fn = model_fn_builder( bert_config=bert_config, init_checkpoint=FLAGS.init_checkpoint, learning_rate=FLAGS.learning_rate if not FLAGS.horovod else FLAGS.learning_rate*hvd.size(), num_train_steps=FLAGS.num_train_steps, num_warmup_steps=FLAGS.num_warmup_steps, use_one_hot_embeddings=False, hvd=None if not FLAGS.horovod else hvd) estimator = tf.estimator.Estimator( model_fn=model_fn, config=run_config) if FLAGS.do_train: training_hooks = [] if FLAGS.horovod and hvd.size() > 1: training_hooks.append(hvd.BroadcastGlobalVariablesHook(0)) if (not FLAGS.horovod or hvd.rank() == 0): global_batch_size = FLAGS.train_batch_size * FLAGS.num_accumulation_steps if not FLAGS.horovod else FLAGS.train_batch_size * FLAGS.num_accumulation_steps * hvd.size() log_hook = _LogSessionRunHook(global_batch_size, FLAGS.num_accumulation_steps, dllogging, FLAGS.display_loss_steps, FLAGS.save_checkpoints_steps, FLAGS.report_loss) training_hooks.append(log_hook) tf.compat.v1.logging.info("***** Running training *****") tf.compat.v1.logging.info(" Batch size = %d", FLAGS.train_batch_size) train_input_fn = input_fn_builder( input_files=input_files, batch_size=FLAGS.train_batch_size, max_seq_length=FLAGS.max_seq_length, max_predictions_per_seq=FLAGS.max_predictions_per_seq, is_training=True, hvd=None if not FLAGS.horovod else hvd) train_start_time = time.time() estimator.train(input_fn=train_input_fn, hooks=training_hooks, max_steps=FLAGS.num_train_steps) train_time_elapsed = time.time() - train_start_time if (not FLAGS.horovod or hvd.rank() == 0): train_time_wo_overhead = training_hooks[-1].total_time avg_sentences_per_second = FLAGS.num_train_steps * global_batch_size * 1.0 / train_time_elapsed ss_sentences_per_second = (FLAGS.num_train_steps - training_hooks[-1].skipped) * global_batch_size * 1.0 / train_time_wo_overhead tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Training Time = %0.2f for Sentences = %d", train_time_elapsed, FLAGS.num_train_steps * global_batch_size) tf.compat.v1.logging.info("Total Training Time W/O Overhead = %0.2f for Sentences = %d", train_time_wo_overhead, (FLAGS.num_train_steps - training_hooks[-1].skipped) * global_batch_size) tf.compat.v1.logging.info("Throughput Average (sentences/sec) with overhead = %0.2f", avg_sentences_per_second) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_train": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) if log_hook.final_loss != 0: dllogging.logger.log(step=(), data={"total_loss": log_hook.final_loss}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") if FLAGS.do_eval and (not FLAGS.horovod or hvd.rank() == 0): tf.compat.v1.logging.info("***** Running evaluation *****") tf.compat.v1.logging.info(" Batch size = %d", FLAGS.eval_batch_size) eval_files = [] for eval_file_dir in FLAGS.eval_files_dir.split(","): eval_files.extend(tf.io.gfile.glob(os.path.join(eval_file_dir, "*"))) eval_input_fn = input_fn_builder( input_files=eval_files, batch_size=FLAGS.eval_batch_size, max_seq_length=FLAGS.max_seq_length, max_predictions_per_seq=FLAGS.max_predictions_per_seq, is_training=False, hvd=None if not FLAGS.horovod else hvd) eval_hooks = [LogEvalRunHook(FLAGS.eval_batch_size)] eval_start_time = time.time() result = estimator.evaluate( input_fn=eval_input_fn, steps=FLAGS.max_eval_steps, hooks=eval_hooks) eval_time_elapsed = time.time() - eval_start_time time_list = eval_hooks[-1].time_list time_list.sort() # Removing outliers (init/warmup) in throughput computation. eval_time_wo_overhead = sum(time_list[:int(len(time_list) * 0.99)]) num_sentences = (int(len(time_list) * 0.99)) * FLAGS.eval_batch_size ss_sentences_per_second = num_sentences * 1.0 / eval_time_wo_overhead tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Inference Time = %0.2f for Sentences = %d", eval_time_elapsed, eval_hooks[-1].count * FLAGS.eval_batch_size) tf.compat.v1.logging.info("Total Inference Time W/O Overhead = %0.2f for Sentences = %d", eval_time_wo_overhead, num_sentences) tf.compat.v1.logging.info("Summary Inference Statistics on EVAL set") tf.compat.v1.logging.info("Batch size = %d", FLAGS.eval_batch_size) tf.compat.v1.logging.info("Sequence Length = %d", FLAGS.max_seq_length) tf.compat.v1.logging.info("Precision = %s", "fp16" if FLAGS.amp else "fp32") tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_val": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") output_eval_file = os.path.join(FLAGS.output_dir, "eval_results.txt") with tf.io.gfile.GFile(output_eval_file, "w") as writer: tf.compat.v1.logging.info("***** Eval results *****") for key in sorted(result.keys()): tf.compat.v1.logging.info(" %s = %s", key, str(result[key])) writer.write("%s = %s\n" % (key, str(result[key]))) if __name__ == "__main__": flags.mark_flag_as_required("input_files_dir") if FLAGS.do_eval: flags.mark_flag_as_required("eval_files_dir") flags.mark_flag_as_required("bert_config_file") flags.mark_flag_as_required("output_dir") if FLAGS.use_xla and FLAGS.manual_fp16: print('WARNING! Combining --use_xla with --manual_fp16 may prevent convergence.') print(' This warning message will be removed when the underlying') print(' issues have been fixed and you are running a TF version') print(' that has that fix.') tf.compat.v1.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/run_pretraining.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """BERT finetuning runner.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import csv import os import modeling import optimization import tokenization import tensorflow as tf import horovod.tensorflow as hvd import time from utils.utils import LogEvalRunHook, LogTrainRunHook, setup_xla_flags from utils.gpu_affinity import set_affinity import utils.dllogger_class from dllogger import Verbosity from utils.create_glue_data import * import numpy as np import tf_metrics flags = tf.flags FLAGS = flags.FLAGS ## Required parameters flags.DEFINE_string( "data_dir", None, "The input data dir. Should contain the .tsv files (or other data files) " "for the task.") flags.DEFINE_string( "bert_config_file", None, "The config json file corresponding to the pre-trained BERT model. " "This specifies the model architecture.") flags.DEFINE_string("task_name", None, "The name of the task to train.") flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_string( "output_dir", None, "The output directory where the model checkpoints will be written.") ## Other parameters flags.DEFINE_string( "dllog_path", "/results/bert_dllog.json", "filename where dllogger writes to") flags.DEFINE_string( "optimizer_type", "lamb", "Optimizer type : adam or lamb") flags.DEFINE_string( "init_checkpoint", None, "Initial checkpoint (usually from a pre-trained BERT model).") flags.DEFINE_bool( "do_lower_case", True, "Whether to lower case the input text. Should be True for uncased " "models and False for cased models.") flags.DEFINE_integer( "max_seq_length", 128, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded.") flags.DEFINE_bool("do_train", False, "Whether to run training.") flags.DEFINE_bool("do_eval", False, "Whether to run eval on the dev set.") flags.DEFINE_bool( "do_predict", False, "Whether to run the model in inference mode on the test set.") flags.DEFINE_integer("train_batch_size", 32, "Total batch size for training.") flags.DEFINE_integer("eval_batch_size", 8, "Total batch size for eval.") flags.DEFINE_integer("predict_batch_size", 8, "Total batch size for predict.") flags.DEFINE_float("learning_rate", 5e-5, "The initial learning rate for Adam.") flags.DEFINE_bool("use_trt", False, "Whether to use TF-TRT") flags.DEFINE_float("num_train_epochs", 3.0, "Total number of training epochs to perform.") flags.DEFINE_float( "warmup_proportion", 0.1, "Proportion of training to perform linear learning rate warmup for. " "E.g., 0.1 = 10% of training.") flags.DEFINE_integer("save_checkpoints_steps", 1000, "How often to save the model checkpoint.") flags.DEFINE_integer("display_loss_steps", 10, "How often to print loss from estimator") flags.DEFINE_integer("iterations_per_loop", 1000, "How many steps to make in each estimator call.") flags.DEFINE_integer("num_accumulation_steps", 1, "Number of accumulation steps before gradient update" "Global batch size = num_accumulation_steps * train_batch_size") flags.DEFINE_bool("amp", True, "Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.") flags.DEFINE_bool("use_xla", True, "Whether to enable XLA JIT compilation.") flags.DEFINE_bool("horovod", False, "Whether to use Horovod for multi-gpu runs") flags.DEFINE_bool( "verbose_logging", False, "If true, all of the warnings related to data processing will be printed. " "A number of warnings are expected for a normal SQuAD evaluation.") def file_based_input_fn_builder(input_file, batch_size, seq_length, is_training, drop_remainder, hvd=None): """Creates an `input_fn` closure to be passed to Estimator.""" name_to_features = { "input_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "input_mask": tf.io.FixedLenFeature([seq_length], tf.int64), "segment_ids": tf.io.FixedLenFeature([seq_length], tf.int64), "label_ids": tf.io.FixedLenFeature([], tf.int64), } def _decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.to_int32(t) example[name] = t return example def input_fn(): """The actual input function.""" # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. d = tf.data.TFRecordDataset(input_file) if is_training: if hvd is not None: d = d.shard(hvd.size(), hvd.rank()) d = d.repeat() d = d.shuffle(buffer_size=100) d = d.apply( tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, drop_remainder=drop_remainder)) return d return input_fn def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, labels, num_labels, use_one_hot_embeddings): """Creates a classification model.""" model = modeling.BertModel( config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings, compute_type=tf.float32) # In the demo, we are doing a simple classification task on the entire # segment. # # If you want to use the token-level output, use model.get_sequence_output() # instead. output_layer = model.get_pooled_output() hidden_size = output_layer.shape[-1].value output_weights = tf.get_variable( "output_weights", [num_labels, hidden_size], initializer=tf.truncated_normal_initializer(stddev=0.02)) output_bias = tf.get_variable( "output_bias", [num_labels], initializer=tf.zeros_initializer()) with tf.variable_scope("loss"): if is_training: # I.e., 0.1 dropout output_layer = tf.nn.dropout(output_layer, keep_prob=0.9) logits = tf.matmul(output_layer, output_weights, transpose_b=True) logits = tf.nn.bias_add(logits, output_bias, name='cls_logits') probabilities = tf.nn.softmax(logits, axis=-1, name='cls_probabilities') log_probs = tf.nn.log_softmax(logits, axis=-1) one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32) per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1, name='cls_per_example_loss') loss = tf.reduce_mean(per_example_loss, name='cls_loss') return (loss, per_example_loss, logits, probabilities) def get_frozen_tftrt_model(bert_config, shape, num_labels, use_one_hot_embeddings, init_checkpoint): tf_config = tf.compat.v1.ConfigProto() tf_config.gpu_options.allow_growth = True output_node_names = ['loss/cls_loss', 'loss/cls_per_example_loss', 'loss/cls_logits', 'loss/cls_probabilities'] with tf.Session(config=tf_config) as tf_sess: input_ids = tf.placeholder(tf.int32, shape, 'input_ids') input_mask = tf.placeholder(tf.int32, shape, 'input_mask') segment_ids = tf.placeholder(tf.int32, shape, 'segment_ids') label_ids = tf.placeholder(tf.int32, (None), 'label_ids') create_model(bert_config, False, input_ids, input_mask, segment_ids, label_ids, num_labels, use_one_hot_embeddings) tvars = tf.trainable_variables() (assignment_map, initialized_variable_names) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) tf_sess.run(tf.global_variables_initializer()) print("LOADED!") tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" else: init_string = ", *NOTTTTTTTTTTTTTTTTTTTTT" tf.compat.v1.logging.info(" name = %s, shape = %s%s", var.name, var.shape, init_string) frozen_graph = tf.graph_util.convert_variables_to_constants(tf_sess, tf_sess.graph.as_graph_def(), output_node_names) num_nodes = len(frozen_graph.node) print('Converting graph using TensorFlow-TensorRT...') from tensorflow.python.compiler.tensorrt import trt_convert as trt converter = trt.TrtGraphConverter( input_graph_def=frozen_graph, nodes_blacklist=output_node_names, max_workspace_size_bytes=(4096 << 20) - 1000, precision_mode = "FP16" if FLAGS.amp else "FP32", minimum_segment_size=4, is_dynamic_op=True, maximum_cached_engines=1000 ) frozen_graph = converter.convert() print('Total node count before and after TF-TRT conversion:', num_nodes, '->', len(frozen_graph.node)) print('TRT node count:', len([1 for n in frozen_graph.node if str(n.op) == 'TRTEngineOp'])) with tf.io.gfile.GFile("frozen_modelTRT.pb", "wb") as f: f.write(frozen_graph.SerializeToString()) return frozen_graph def model_fn_builder(task_name, bert_config, num_labels, init_checkpoint, learning_rate, num_train_steps, num_warmup_steps, use_one_hot_embeddings, hvd=None): """Returns `model_fn` closure for Estimator.""" def model_fn(features, labels, mode, params): # pylint: disable=unused-argument """The `model_fn` for Estimator.""" def metric_fn(per_example_loss, label_ids, logits): predictions = tf.argmax(logits, axis=-1, output_type=tf.int32) if task_name == "cola": FN, FN_op = tf.metrics.false_negatives(labels=label_ids, predictions=predictions) FP, FP_op = tf.metrics.false_positives(labels=label_ids, predictions=predictions) TP, TP_op = tf.metrics.true_positives(labels=label_ids, predictions=predictions) TN, TN_op = tf.metrics.true_negatives(labels=label_ids, predictions=predictions) MCC = (TP * TN - FP * FN) / ((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN)) ** 0.5 MCC_op = tf.group(FN_op, TN_op, TP_op, FP_op, tf.identity(MCC, name="MCC")) return {"MCC": (MCC, MCC_op)} elif task_name == "mrpc": accuracy = tf.metrics.accuracy( labels=label_ids, predictions=predictions) loss = tf.metrics.mean(values=per_example_loss) f1 = tf_metrics.f1(labels=label_ids, predictions=predictions, num_classes=2, pos_indices=[1]) return { "eval_accuracy": accuracy, "eval_f1": f1, "eval_loss": loss, } else: accuracy = tf.metrics.accuracy( labels=label_ids, predictions=predictions) loss = tf.metrics.mean(values=per_example_loss) return { "eval_accuracy": accuracy, "eval_loss": loss, } tf.compat.v1.logging.info("*** Features ***") tf.compat.v1.logging.info("*** Features ***") for name in sorted(features.keys()): tf.compat.v1.logging.info(" name = %s, shape = %s" % (name, features[name].shape)) input_ids = features["input_ids"] input_mask = features["input_mask"] segment_ids = features["segment_ids"] label_ids = features["label_ids"] is_training = (mode == tf.estimator.ModeKeys.TRAIN) if not is_training and FLAGS.use_trt: trt_graph = get_frozen_tftrt_model(bert_config, input_ids.shape, num_labels, use_one_hot_embeddings, init_checkpoint) (total_loss, per_example_loss, logits, probabilities) = tf.import_graph_def(trt_graph, input_map={'input_ids':input_ids, 'input_mask':input_mask, 'segment_ids':segment_ids, 'label_ids':label_ids}, return_elements=['loss/cls_loss:0', 'loss/cls_per_example_loss:0', 'loss/cls_logits:0', 'loss/cls_probabilities:0'], name='') if mode == tf.estimator.ModeKeys.PREDICT: predictions = {"probabilities": probabilities} output_spec = tf.estimator.EstimatorSpec( mode=mode, predictions=predictions) elif mode == tf.estimator.ModeKeys.EVAL: eval_metric_ops = metric_fn(per_example_loss, label_ids, logits) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, eval_metric_ops=eval_metric_ops) return output_spec (total_loss, per_example_loss, logits, probabilities) = create_model( bert_config, is_training, input_ids, input_mask, segment_ids, label_ids, num_labels, use_one_hot_embeddings) tvars = tf.trainable_variables() initialized_variable_names = {} if init_checkpoint and (hvd is None or hvd.rank() == 0): (assignment_map, initialized_variable_names ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) if FLAGS.verbose_logging: tf.compat.v1.logging.info("**** Trainable Variables ****") for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" tf.compat.v1.logging.info(" name = %s, shape = %s%s", var.name, var.shape, init_string) output_spec = None if mode == tf.estimator.ModeKeys.TRAIN: train_op = optimization.create_optimizer( total_loss, learning_rate, num_train_steps, num_warmup_steps, hvd, False, FLAGS.amp, FLAGS.num_accumulation_steps, FLAGS.optimizer_type) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, train_op=train_op) elif mode == tf.estimator.ModeKeys.EVAL: dummy_op = tf.no_op() # Need to call mixed precision graph rewrite if fp16 to enable graph rewrite if FLAGS.amp: loss_scaler = tf.train.experimental.FixedLossScale(1) dummy_op = tf.train.experimental.enable_mixed_precision_graph_rewrite( optimization.LAMBOptimizer(learning_rate=0.0), loss_scaler) eval_metric_ops = metric_fn(per_example_loss, label_ids, logits) output_spec = tf.estimator.EstimatorSpec( mode=mode, loss=total_loss, eval_metric_ops=eval_metric_ops) else: dummy_op = tf.no_op() # Need to call mixed precision graph rewrite if fp16 to enable graph rewrite if FLAGS.amp: dummy_op = tf.train.experimental.enable_mixed_precision_graph_rewrite( optimization.LAMBOptimizer(learning_rate=0.0)) output_spec = tf.estimator.EstimatorSpec( mode=mode, predictions=probabilities) return output_spec return model_fn # This function is not used by this file but is still used by the Colab and # people who depend on it. def input_fn_builder(features, batch_size, seq_length, is_training, drop_remainder, hvd=None): """Creates an `input_fn` closure to be passed to Estimator.""" all_input_ids = [] all_input_mask = [] all_segment_ids = [] all_label_ids = [] for feature in features: all_input_ids.append(feature.input_ids) all_input_mask.append(feature.input_mask) all_segment_ids.append(feature.segment_ids) all_label_ids.append(feature.label_id) def input_fn(): """The actual input function.""" num_examples = len(features) # This is for demo purposes and does NOT scale to large data sets. We do # not use Dataset.from_generator() because that uses tf.py_func which is # not TPU compatible. The right way to load data is with TFRecordReader. d = tf.data.Dataset.from_tensor_slices({ "input_ids": tf.constant( all_input_ids, shape=[num_examples, seq_length], dtype=tf.int32), "input_mask": tf.constant( all_input_mask, shape=[num_examples, seq_length], dtype=tf.int32), "segment_ids": tf.constant( all_segment_ids, shape=[num_examples, seq_length], dtype=tf.int32), "label_ids": tf.constant(all_label_ids, shape=[num_examples], dtype=tf.int32), }) if is_training: if hvd is not None: d = d.shard(hvd.size(), hvd.rank()) d = d.repeat() d = d.shuffle(buffer_size=100) d = d.batch(batch_size=batch_size, drop_remainder=drop_remainder) return d return input_fn def main(_): setup_xla_flags() tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO) dllogging = utils.dllogger_class.dllogger_class(FLAGS.dllog_path) if FLAGS.horovod: hvd.init() processors = { "cola": ColaProcessor, "mnli": MnliProcessor, "mrpc": MrpcProcessor, "xnli": XnliProcessor, } if not FLAGS.do_train and not FLAGS.do_eval and not FLAGS.do_predict: raise ValueError( "At least one of `do_train`, `do_eval` or `do_predict' must be True.") bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file) if FLAGS.max_seq_length > bert_config.max_position_embeddings: raise ValueError( "Cannot use sequence length %d because the BERT model " "was only trained up to sequence length %d" % (FLAGS.max_seq_length, bert_config.max_position_embeddings)) tf.io.gfile.makedirs(FLAGS.output_dir) task_name = FLAGS.task_name.lower() if task_name not in processors: raise ValueError("Task not found: %s" % (task_name)) processor = processors[task_name]() label_list = processor.get_labels() tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) master_process = True training_hooks = [] global_batch_size = FLAGS.train_batch_size * FLAGS.num_accumulation_steps hvd_rank = 0 config = tf.compat.v1.ConfigProto() if FLAGS.horovod: tf.compat.v1.logging.info("Multi-GPU training with TF Horovod") tf.compat.v1.logging.info("hvd.size() = %d hvd.rank() = %d", hvd.size(), hvd.rank()) global_batch_size = FLAGS.train_batch_size * FLAGS.num_accumulation_steps * hvd.size() master_process = (hvd.rank() == 0) hvd_rank = hvd.rank() config.gpu_options.visible_device_list = str(hvd.local_rank()) set_affinity(hvd.local_rank()) if hvd.size() > 1: training_hooks.append(hvd.BroadcastGlobalVariablesHook(0)) if FLAGS.use_xla: config.graph_options.optimizer_options.global_jit_level = tf.compat.v1.OptimizerOptions.ON_1 if FLAGS.amp: tf.enable_resource_variables() run_config = tf.estimator.RunConfig( model_dir=FLAGS.output_dir if master_process else None, session_config=config, save_checkpoints_steps=FLAGS.save_checkpoints_steps if master_process else None, save_summary_steps=FLAGS.save_checkpoints_steps if master_process else None, log_step_count_steps=FLAGS.display_loss_steps, keep_checkpoint_max=1) if master_process: tf.compat.v1.logging.info("***** Configuaration *****") for key in FLAGS.__flags.keys(): tf.compat.v1.logging.info(' {}: {}'.format(key, getattr(FLAGS, key))) tf.compat.v1.logging.info("**************************") train_examples = None num_train_steps = None num_warmup_steps = None training_hooks.append(LogTrainRunHook(global_batch_size, hvd_rank, FLAGS.save_checkpoints_steps, num_steps_ignore_xla=25)) if FLAGS.do_train: train_examples = processor.get_train_examples(FLAGS.data_dir) num_train_steps = int( len(train_examples) / global_batch_size * FLAGS.num_train_epochs) num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion) start_index = 0 end_index = len(train_examples) tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record")] if FLAGS.horovod: tmp_filenames = [os.path.join(FLAGS.output_dir, "train.tf_record{}".format(i)) for i in range(hvd.size())] num_examples_per_rank = len(train_examples) // hvd.size() remainder = len(train_examples) % hvd.size() if hvd.rank() < remainder: start_index = hvd.rank() * (num_examples_per_rank+1) end_index = start_index + num_examples_per_rank + 1 else: start_index = hvd.rank() * num_examples_per_rank + remainder end_index = start_index + (num_examples_per_rank) model_fn = model_fn_builder( task_name=task_name, bert_config=bert_config, num_labels=len(label_list), init_checkpoint=FLAGS.init_checkpoint, learning_rate=FLAGS.learning_rate if not FLAGS.horovod else FLAGS.learning_rate * hvd.size(), num_train_steps=num_train_steps, num_warmup_steps=num_warmup_steps, use_one_hot_embeddings=False, hvd=None if not FLAGS.horovod else hvd) estimator = tf.estimator.Estimator( model_fn=model_fn, config=run_config) if FLAGS.do_train: file_based_convert_examples_to_features( train_examples[start_index:end_index], label_list, FLAGS.max_seq_length, tokenizer, tmp_filenames[hvd_rank]) tf.compat.v1.logging.info("***** Running training *****") tf.compat.v1.logging.info(" Num examples = %d", len(train_examples)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.train_batch_size) tf.compat.v1.logging.info(" Num steps = %d", num_train_steps) train_input_fn = file_based_input_fn_builder( input_file=tmp_filenames, batch_size=FLAGS.train_batch_size, seq_length=FLAGS.max_seq_length, is_training=True, drop_remainder=True, hvd=None if not FLAGS.horovod else hvd) train_start_time = time.time() estimator.train(input_fn=train_input_fn, max_steps=num_train_steps, hooks=training_hooks) train_time_elapsed = time.time() - train_start_time train_time_wo_overhead = training_hooks[-1].total_time avg_sentences_per_second = num_train_steps * global_batch_size * 1.0 / train_time_elapsed ss_sentences_per_second = (training_hooks[-1].count - training_hooks[-1].skipped) * global_batch_size * 1.0 / train_time_wo_overhead if master_process: tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Training Time = %0.2f for Sentences = %d", train_time_elapsed, num_train_steps * global_batch_size) tf.compat.v1.logging.info("Total Training Time W/O Overhead = %0.2f for Sentences = %d", train_time_wo_overhead, (training_hooks[-1].count - training_hooks[-1].skipped) * global_batch_size) tf.compat.v1.logging.info("Throughput Average (sentences/sec) with overhead = %0.2f", avg_sentences_per_second) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) tf.compat.v1.logging.info("-----------------------------") if FLAGS.do_eval and master_process: eval_examples = processor.get_dev_examples(FLAGS.data_dir) eval_file = os.path.join(FLAGS.output_dir, "eval.tf_record") file_based_convert_examples_to_features( eval_examples, label_list, FLAGS.max_seq_length, tokenizer, eval_file) tf.compat.v1.logging.info("***** Running evaluation *****") tf.compat.v1.logging.info(" Num examples = %d", len(eval_examples)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.eval_batch_size) eval_drop_remainder = False eval_input_fn = file_based_input_fn_builder( input_file=eval_file, batch_size=FLAGS.eval_batch_size, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=eval_drop_remainder) eval_hooks = [LogEvalRunHook(FLAGS.eval_batch_size)] eval_start_time = time.time() result = estimator.evaluate(input_fn=eval_input_fn, hooks=eval_hooks) eval_time_elapsed = time.time() - eval_start_time time_list = eval_hooks[-1].time_list time_list.sort() # Removing outliers (init/warmup) in throughput computation. eval_time_wo_overhead = sum(time_list[:int(len(time_list) * 0.8)]) num_sentences = (int(len(time_list) * 0.8)) * FLAGS.eval_batch_size avg = np.mean(time_list) cf_50 = max(time_list[:int(len(time_list) * 0.50)]) cf_90 = max(time_list[:int(len(time_list) * 0.90)]) cf_95 = max(time_list[:int(len(time_list) * 0.95)]) cf_99 = max(time_list[:int(len(time_list) * 0.99)]) cf_100 = max(time_list[:int(len(time_list) * 1)]) ss_sentences_per_second = num_sentences * 1.0 / eval_time_wo_overhead tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Inference Time = %0.2f for Sentences = %d", eval_time_elapsed, eval_hooks[-1].count * FLAGS.eval_batch_size) tf.compat.v1.logging.info("Total Inference Time W/O Overhead = %0.2f for Sentences = %d", eval_time_wo_overhead, num_sentences) tf.compat.v1.logging.info("Summary Inference Statistics on EVAL set") tf.compat.v1.logging.info("Batch size = %d", FLAGS.eval_batch_size) tf.compat.v1.logging.info("Sequence Length = %d", FLAGS.max_seq_length) tf.compat.v1.logging.info("Precision = %s", "fp16" if FLAGS.amp else "fp32") tf.compat.v1.logging.info("Latency Confidence Level 50 (ms) = %0.2f", cf_50 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 90 (ms) = %0.2f", cf_90 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 95 (ms) = %0.2f", cf_95 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 99 (ms) = %0.2f", cf_99 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 100 (ms) = %0.2f", cf_100 * 1000) tf.compat.v1.logging.info("Latency Average (ms) = %0.2f", avg * 1000) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_val": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") output_eval_file = os.path.join(FLAGS.output_dir, "eval_results.txt") with tf.io.gfile.GFile(output_eval_file, "w") as writer: tf.compat.v1.logging.info("***** Eval results *****") for key in sorted(result.keys()): dllogging.logger.log(step=(), data={key: float(result[key])}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info(" %s = %s", key, str(result[key])) writer.write("%s = %s\n" % (key, str(result[key]))) if FLAGS.do_predict and master_process: predict_examples = processor.get_test_examples(FLAGS.data_dir) predict_file = os.path.join(FLAGS.output_dir, "predict.tf_record") file_based_convert_examples_to_features(predict_examples, label_list, FLAGS.max_seq_length, tokenizer, predict_file) tf.compat.v1.logging.info("***** Running prediction*****") tf.compat.v1.logging.info(" Num examples = %d", len(predict_examples)) tf.compat.v1.logging.info(" Batch size = %d", FLAGS.predict_batch_size) predict_drop_remainder = False predict_input_fn = file_based_input_fn_builder( input_file=predict_file, batch_size=FLAGS.predict_batch_size, seq_length=FLAGS.max_seq_length, is_training=False, drop_remainder=predict_drop_remainder) predict_hooks = [LogEvalRunHook(FLAGS.predict_batch_size)] predict_start_time = time.time() output_predict_file = os.path.join(FLAGS.output_dir, "test_results.tsv") with tf.io.gfile.GFile(output_predict_file, "w") as writer: tf.compat.v1.logging.info("***** Predict results *****") for prediction in estimator.predict(input_fn=predict_input_fn, hooks=predict_hooks, yield_single_examples=False): output_line = "\t".join( str(class_probability) for class_probability in prediction) + "\n" writer.write(output_line) predict_time_elapsed = time.time() - predict_start_time time_list = predict_hooks[-1].time_list time_list.sort() # Removing outliers (init/warmup) in throughput computation. predict_time_wo_overhead = sum(time_list[:int(len(time_list) * 0.8)]) num_sentences = (int(len(time_list) * 0.8)) * FLAGS.predict_batch_size avg = np.mean(time_list) cf_50 = max(time_list[:int(len(time_list) * 0.50)]) cf_90 = max(time_list[:int(len(time_list) * 0.90)]) cf_95 = max(time_list[:int(len(time_list) * 0.95)]) cf_99 = max(time_list[:int(len(time_list) * 0.99)]) cf_100 = max(time_list[:int(len(time_list) * 1)]) ss_sentences_per_second = num_sentences * 1.0 / predict_time_wo_overhead tf.compat.v1.logging.info("-----------------------------") tf.compat.v1.logging.info("Total Inference Time = %0.2f for Sentences = %d", predict_time_elapsed, predict_hooks[-1].count * FLAGS.predict_batch_size) tf.compat.v1.logging.info("Total Inference Time W/O Overhead = %0.2f for Sentences = %d", predict_time_wo_overhead, num_sentences) tf.compat.v1.logging.info("Summary Inference Statistics on TEST SET") tf.compat.v1.logging.info("Batch size = %d", FLAGS.predict_batch_size) tf.compat.v1.logging.info("Sequence Length = %d", FLAGS.max_seq_length) tf.compat.v1.logging.info("Precision = %s", "fp16" if FLAGS.amp else "fp32") tf.compat.v1.logging.info("Latency Confidence Level 50 (ms) = %0.2f", cf_50 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 90 (ms) = %0.2f", cf_90 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 95 (ms) = %0.2f", cf_95 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 99 (ms) = %0.2f", cf_99 * 1000) tf.compat.v1.logging.info("Latency Confidence Level 100 (ms) = %0.2f", cf_100 * 1000) tf.compat.v1.logging.info("Latency Average (ms) = %0.2f", avg * 1000) tf.compat.v1.logging.info("Throughput Average (sentences/sec) = %0.2f", ss_sentences_per_second) dllogging.logger.log(step=(), data={"throughput_val": ss_sentences_per_second}, verbosity=Verbosity.DEFAULT) tf.compat.v1.logging.info("-----------------------------") if __name__ == "__main__": flags.mark_flag_as_required("data_dir") flags.mark_flag_as_required("task_name") flags.mark_flag_as_required("vocab_file") flags.mark_flag_as_required("bert_config_file") flags.mark_flag_as_required("output_dir") tf.compat.v1.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/run_classifier.py
# coding=utf-8 # Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import tensorflow as tf import numpy as np def float32_variable_storage_getter(getter, name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, *args, **kwargs): """Custom variable getter that forces trainable variables to be stored in float32 precision and then casts them to the training precision. """ storage_dtype = tf.float32 if trainable else dtype variable = getter(name, shape, dtype=storage_dtype, initializer=initializer, regularizer=regularizer, trainable=trainable, *args, **kwargs) if trainable and dtype != tf.float32: variable = tf.cast(variable, dtype) return variable def get_custom_getter(compute_type): return float32_variable_storage_getter if compute_type == tf.float16 else None
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/gpu_environment.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The main BERT model and related functions.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import copy import json import math import re import numpy as np import six import tensorflow as tf from gpu_environment import get_custom_getter class BertConfig(object): """Configuration for `BertModel`.""" def __init__(self, vocab_size, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, hidden_act="gelu", hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, type_vocab_size=16, initializer_range=0.02): """Constructs BertConfig. Args: vocab_size: Vocabulary size of `inputs_ids` in `BertModel`. hidden_size: Size of the encoder layers and the pooler layer. num_hidden_layers: Number of hidden layers in the Transformer encoder. num_attention_heads: Number of attention heads for each attention layer in the Transformer encoder. intermediate_size: The size of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder. hidden_act: The non-linear activation function (function or string) in the encoder and pooler. hidden_dropout_prob: The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. attention_probs_dropout_prob: The dropout ratio for the attention probabilities. max_position_embeddings: The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048). type_vocab_size: The vocabulary size of the `token_type_ids` passed into `BertModel`. initializer_range: The stdev of the truncated_normal_initializer for initializing all weight matrices. """ self.vocab_size = vocab_size self.hidden_size = hidden_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.hidden_act = hidden_act self.intermediate_size = intermediate_size self.hidden_dropout_prob = hidden_dropout_prob self.attention_probs_dropout_prob = attention_probs_dropout_prob self.max_position_embeddings = max_position_embeddings self.type_vocab_size = type_vocab_size self.initializer_range = initializer_range @classmethod def from_dict(cls, json_object): """Constructs a `BertConfig` from a Python dictionary of parameters.""" config = BertConfig(vocab_size=None) for (key, value) in six.iteritems(json_object): config.__dict__[key] = value return config @classmethod def from_json_file(cls, json_file): """Constructs a `BertConfig` from a json file of parameters.""" with tf.io.gfile.GFile(json_file, "r") as reader: text = reader.read() return cls.from_dict(json.loads(text)) def to_dict(self): """Serializes this instance to a Python dictionary.""" output = copy.deepcopy(self.__dict__) return output def to_json_string(self): """Serializes this instance to a JSON string.""" return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n" class BertModel(object): """BERT model ("Bidirectional Encoder Representations from Transformers"). Example usage: ```python # Already been converted into WordPiece token ids input_ids = tf.constant([[31, 51, 99], [15, 5, 0]]) input_mask = tf.constant([[1, 1, 1], [1, 1, 0]]) token_type_ids = tf.constant([[0, 0, 1], [0, 2, 0]]) config = modeling.BertConfig(vocab_size=32000, hidden_size=512, num_hidden_layers=8, num_attention_heads=6, intermediate_size=1024) model = modeling.BertModel(config=config, is_training=True, input_ids=input_ids, input_mask=input_mask, token_type_ids=token_type_ids) label_embeddings = tf.get_variable(...) pooled_output = model.get_pooled_output() logits = tf.matmul(pooled_output, label_embeddings) ... ``` """ def __init__(self, config, is_training, input_ids, input_mask=None, token_type_ids=None, use_one_hot_embeddings=False, scope=None, compute_type=tf.float32): """Constructor for BertModel. Args: config: `BertConfig` instance. is_training: bool. true for training model, false for eval model. Controls whether dropout will be applied. input_ids: int32 Tensor of shape [batch_size, seq_length]. input_mask: (optional) int32 Tensor of shape [batch_size, seq_length]. token_type_ids: (optional) int32 Tensor of shape [batch_size, seq_length]. use_one_hot_embeddings: (optional) bool. Whether to use one-hot word embeddings or tf.embedding_lookup() for the word embeddings. On the TPU, it is much faster if this is True, on the CPU or GPU, it is faster if this is False. scope: (optional) variable scope. Defaults to "bert". compute_type: (optional) either float32 or float16. Only applies to GPUs. Raises: ValueError: The config is invalid or one of the input tensor shapes is invalid. """ config = copy.deepcopy(config) if not is_training: config.hidden_dropout_prob = 0.0 config.attention_probs_dropout_prob = 0.0 input_shape = get_shape_list(input_ids, expected_rank=2) batch_size = input_shape[0] seq_length = input_shape[1] if input_mask is None: input_mask = tf.ones(shape=[batch_size, seq_length], dtype=tf.int32) if token_type_ids is None: token_type_ids = tf.zeros(shape=[batch_size, seq_length], dtype=tf.int32) with tf.variable_scope(scope, default_name="bert", custom_getter=get_custom_getter(compute_type)): with tf.variable_scope("embeddings"): # For good convergence with mixed precision training, # it is important that the embedding codes remain fp32. # Perform embedding lookup on the word ids. (self.embedding_output, self.embedding_table) = embedding_lookup( input_ids=input_ids, vocab_size=config.vocab_size, embedding_size=config.hidden_size, initializer_range=config.initializer_range, word_embedding_name="word_embeddings", use_one_hot_embeddings=use_one_hot_embeddings) # Add positional embeddings and token type embeddings, then layer # normalize and perform dropout. self.embedding_output = embedding_postprocessor( input_tensor=self.embedding_output, use_token_type=True, token_type_ids=token_type_ids, token_type_vocab_size=config.type_vocab_size, token_type_embedding_name="token_type_embeddings", use_position_embeddings=True, position_embedding_name="position_embeddings", initializer_range=config.initializer_range, max_position_embeddings=config.max_position_embeddings, dropout_prob=config.hidden_dropout_prob, use_one_hot_embeddings=use_one_hot_embeddings) with tf.variable_scope("encoder"): # This converts a 2D mask of shape [batch_size, seq_length] to a 3D # mask of shape [batch_size, seq_length, seq_length] which is used # for the attention scores. attention_mask = create_attention_mask_from_input_mask( input_ids, input_mask) # Run the stacked transformer. # `sequence_output` shape = [batch_size, seq_length, hidden_size]. self.all_encoder_layers = transformer_model( input_tensor=tf.saturate_cast(self.embedding_output, compute_type), attention_mask=attention_mask, hidden_size=config.hidden_size, num_hidden_layers=config.num_hidden_layers, num_attention_heads=config.num_attention_heads, intermediate_size=config.intermediate_size, intermediate_act_fn=get_activation(config.hidden_act), hidden_dropout_prob=config.hidden_dropout_prob, attention_probs_dropout_prob=config.attention_probs_dropout_prob, initializer_range=config.initializer_range, do_return_all_layers=True) self.sequence_output = tf.cast(self.all_encoder_layers[-1], tf.float32) # The "pooler" converts the encoded sequence tensor of shape # [batch_size, seq_length, hidden_size] to a tensor of shape # [batch_size, hidden_size]. This is necessary for segment-level # (or segment-pair-level) classification tasks where we need a fixed # dimensional representation of the segment. with tf.variable_scope("pooler"): # We "pool" the model by simply taking the hidden state corresponding # to the first token. We assume that this has been pre-trained first_token_tensor = tf.squeeze(self.sequence_output[:, 0:1, :], axis=1) self.pooled_output = tf.layers.dense( first_token_tensor, config.hidden_size, activation=tf.tanh, kernel_initializer=create_initializer(config.initializer_range)) def get_pooled_output(self): return self.pooled_output def get_sequence_output(self): """Gets final hidden layer of encoder. Returns: float Tensor of shape [batch_size, seq_length, hidden_size] corresponding to the final hidden of the transformer encoder. """ return self.sequence_output def get_all_encoder_layers(self): return self.all_encoder_layers def get_embedding_output(self): """Gets output of the embedding lookup (i.e., input to the transformer). Returns: float Tensor of shape [batch_size, seq_length, hidden_size] corresponding to the output of the embedding layer, after summing the word embeddings with the positional embeddings and the token type embeddings, then performing layer normalization. This is the input to the transformer. """ return self.embedding_output def get_embedding_table(self): return self.embedding_table def gelu(x): """Gaussian Error Linear Unit. This is a smoother version of the RELU. Original paper: https://arxiv.org/abs/1606.08415 Args: x: float Tensor to perform activation. Returns: `x` with the GELU activation applied. """ cdf = 0.5 * (1.0 + tf.tanh( (np.sqrt(2 / np.pi) * (x + 0.044715 * tf.pow(x, 3))))) return x * cdf def get_activation(activation_string): """Maps a string to a Python function, e.g., "relu" => `tf.nn.relu`. Args: activation_string: String name of the activation function. Returns: A Python function corresponding to the activation function. If `activation_string` is None, empty, or "linear", this will return None. If `activation_string` is not a string, it will return `activation_string`. Raises: ValueError: The `activation_string` does not correspond to a known activation. """ # We assume that anything that"s not a string is already an activation # function, so we just return it. if not isinstance(activation_string, six.string_types): return activation_string if not activation_string: return None act = activation_string.lower() if act == "linear": return None elif act == "relu": return tf.nn.relu elif act == "gelu": return gelu elif act == "tanh": return tf.tanh else: raise ValueError("Unsupported activation: %s" % act) def get_assignment_map_from_checkpoint(tvars, init_checkpoint): """Compute the union of the current variables and checkpoint variables.""" assignment_map = {} initialized_variable_names = {} name_to_variable = collections.OrderedDict() for var in tvars: name = var.name m = re.match("^(.*):\\d+$", name) if m is not None: name = m.group(1) name_to_variable[name] = var init_vars = tf.train.list_variables(init_checkpoint) assignment_map = collections.OrderedDict() for x in init_vars: (name, var) = (x[0], x[1]) if name not in name_to_variable: continue assignment_map[name] = name initialized_variable_names[name] = 1 initialized_variable_names[name + ":0"] = 1 return (assignment_map, initialized_variable_names) def dropout(input_tensor, dropout_prob): """Perform dropout. Args: input_tensor: float Tensor. dropout_prob: Python float. The probability of dropping out a value (NOT of *keeping* a dimension as in `tf.nn.dropout`). Returns: A version of `input_tensor` with dropout applied. """ if dropout_prob is None or dropout_prob == 0.0: return input_tensor output = tf.nn.dropout(input_tensor, 1.0 - dropout_prob) return output def layer_norm(input_tensor, name=None): """Run layer normalization on the last dimension of the tensor.""" if input_tensor.dtype == tf.float16: try: from fused_layer_norm import fused_layer_norm return fused_layer_norm( inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name, use_fused_batch_norm=True) except ImportError: return tf.contrib.layers.layer_norm( inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name) else: return tf.contrib.layers.layer_norm( inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name) def layer_norm_and_dropout(input_tensor, dropout_prob, name=None): """Runs layer normalization followed by dropout.""" output_tensor = layer_norm(input_tensor, name) output_tensor = dropout(output_tensor, dropout_prob) return output_tensor def create_initializer(initializer_range=0.02): """Creates a `truncated_normal_initializer` with the given range.""" return tf.truncated_normal_initializer(stddev=initializer_range) def embedding_lookup(input_ids, vocab_size, embedding_size=128, initializer_range=0.02, word_embedding_name="word_embeddings", use_one_hot_embeddings=False): """Looks up words embeddings for id tensor. Args: input_ids: int32 Tensor of shape [batch_size, seq_length] containing word ids. vocab_size: int. Size of the embedding vocabulary. embedding_size: int. Width of the word embeddings. initializer_range: float. Embedding initialization range. word_embedding_name: string. Name of the embedding table. use_one_hot_embeddings: bool. If True, use one-hot method for word embeddings. If False, use `tf.gather()`. Returns: float Tensor of shape [batch_size, seq_length, embedding_size]. """ # This function assumes that the input is of shape [batch_size, seq_length, # num_inputs]. # # If the input is a 2D tensor of shape [batch_size, seq_length], we # reshape to [batch_size, seq_length, 1]. if input_ids.shape.ndims == 2: input_ids = tf.expand_dims(input_ids, axis=[-1]) embedding_table = tf.get_variable( name=word_embedding_name, shape=[vocab_size, embedding_size], initializer=create_initializer(initializer_range)) flat_input_ids = tf.reshape(input_ids, [-1]) if use_one_hot_embeddings: one_hot_input_ids = tf.one_hot(flat_input_ids, depth=vocab_size) output = tf.matmul(one_hot_input_ids, embedding_table) else: output = tf.gather(embedding_table, flat_input_ids) input_shape = get_shape_list(input_ids) output = tf.reshape(output, input_shape[0:-1] + [input_shape[-1] * embedding_size]) return (output, embedding_table) def embedding_postprocessor(input_tensor, use_token_type=False, token_type_ids=None, token_type_vocab_size=16, token_type_embedding_name="token_type_embeddings", use_position_embeddings=True, position_embedding_name="position_embeddings", initializer_range=0.02, max_position_embeddings=512, dropout_prob=0.1, use_one_hot_embeddings=False): """Performs various post-processing on a word embedding tensor. Args: input_tensor: float Tensor of shape [batch_size, seq_length, embedding_size]. use_token_type: bool. Whether to add embeddings for `token_type_ids`. token_type_ids: (optional) int32 Tensor of shape [batch_size, seq_length]. Must be specified if `use_token_type` is True. token_type_vocab_size: int. The vocabulary size of `token_type_ids`. token_type_embedding_name: string. The name of the embedding table variable for token type ids. use_position_embeddings: bool. Whether to add position embeddings for the position of each token in the sequence. position_embedding_name: string. The name of the embedding table variable for positional embeddings. initializer_range: float. Range of the weight initialization. max_position_embeddings: int. Maximum sequence length that might ever be used with this model. This can be longer than the sequence length of input_tensor, but cannot be shorter. dropout_prob: float. Dropout probability applied to the final output tensor. use_one_hot_embeddings: (optional) bool. Whether to use one-hot word embeddings or tf.embedding_lookup() for the word embeddings. Returns: float tensor with same shape as `input_tensor`. Raises: ValueError: One of the tensor shapes or input values is invalid. """ input_shape = get_shape_list(input_tensor, expected_rank=3) batch_size = input_shape[0] seq_length = input_shape[1] width = input_shape[2] output = input_tensor if use_token_type: if token_type_ids is None: raise ValueError("`token_type_ids` must be specified if" "`use_token_type` is True.") token_type_table = tf.get_variable( name=token_type_embedding_name, shape=[token_type_vocab_size, width], initializer=create_initializer(initializer_range)) flat_token_type_ids = tf.reshape(token_type_ids, [-1]) if use_one_hot_embeddings: # This vocab will be small so we always do one-hot here, since it is # always faster for a small vocabulary. one_hot_ids = tf.one_hot(flat_token_type_ids, depth=token_type_vocab_size) token_type_embeddings = tf.matmul(one_hot_ids, token_type_table) else: token_type_embeddings = tf.gather(token_type_table, flat_token_type_ids) token_type_embeddings = tf.reshape(token_type_embeddings, [batch_size, seq_length, width]) output += token_type_embeddings if use_position_embeddings: full_position_embeddings = tf.get_variable( name=position_embedding_name, shape=[max_position_embeddings, width], initializer=create_initializer(initializer_range)) # Since the position embedding table is a learned variable, we create it # using a (long) sequence length `max_position_embeddings`. The actual # sequence length might be shorter than this, for faster training of # tasks that do not have long sequences. # # So `full_position_embeddings` is effectively an embedding table # for position [0, 1, 2, ..., max_position_embeddings-1], and the current # sequence has positions [0, 1, 2, ... seq_length-1], so we can just # perform a slice. position_embeddings = tf.slice(full_position_embeddings, [0, 0], [seq_length, width]) num_dims = len(output.shape.as_list()) # Only the last two dimensions are relevant (`seq_length` and `width`), so # we broadcast among the first dimensions, which is typically just # the batch size. position_broadcast_shape = [] for _ in range(num_dims - 2): position_broadcast_shape.append(1) position_broadcast_shape.extend([seq_length, width]) position_embeddings = tf.reshape(position_embeddings, position_broadcast_shape) output += position_embeddings output = layer_norm_and_dropout(output, dropout_prob) return output def create_attention_mask_from_input_mask(from_tensor, to_mask): """Create 3D attention mask from a 2D tensor mask. Args: from_tensor: 2D or 3D Tensor of shape [batch_size, from_seq_length, ...]. to_mask: int32 Tensor of shape [batch_size, to_seq_length]. Returns: float Tensor of shape [batch_size, from_seq_length, to_seq_length]. """ to_mask = tf.cast(to_mask, dtype=tf.float32) from_shape = get_shape_list(from_tensor, expected_rank=[2, 3]) batch_size = from_shape[0] to_shape = get_shape_list(to_mask, expected_rank=2) to_seq_length = to_shape[1] to_mask = tf.reshape(to_mask, [batch_size, 1, to_seq_length]) # The mask will be automatically broadcasted to # [batch_size, from_seq_length, to_seq_length] when it is used in the # attention layer. return to_mask def attention_layer(from_tensor, to_tensor, attention_mask=None, num_attention_heads=1, size_per_head=512, query_act=None, key_act=None, value_act=None, attention_probs_dropout_prob=0.0, initializer_range=0.02, do_return_2d_tensor=False, batch_size=None, from_seq_length=None, to_seq_length=None): """Performs multi-headed attention from `from_tensor` to `to_tensor`. This is an implementation of multi-headed attention based on "Attention is all you Need". If `from_tensor` and `to_tensor` are the same, then this is self-attention. Each timestep in `from_tensor` attends to the corresponding sequence in `to_tensor`, and returns a fixed-with vector. This function first projects `from_tensor` into a "query" tensor and `to_tensor` into "key" and "value" tensors. These are (effectively) a list of tensors of length `num_attention_heads`, where each tensor is of shape [batch_size, seq_length, size_per_head]. Then, the query and key tensors are dot-producted and scaled. These are softmaxed to obtain attention probabilities. The value tensors are then interpolated by these probabilities, then concatenated back to a single tensor and returned. In practice, the multi-headed attention are done with transposes and reshapes rather than actual separate tensors. Args: from_tensor: float Tensor of shape [batch_size, from_seq_length, from_width]. to_tensor: float Tensor of shape [batch_size, to_seq_length, to_width]. attention_mask: (optional) int32 Tensor of shape [batch_size, from_seq_length, to_seq_length]. The values should be 1 or 0. The attention scores will effectively be set to -infinity for any positions in the mask that are 0, and will be unchanged for positions that are 1. num_attention_heads: int. Number of attention heads. size_per_head: int. Size of each attention head. query_act: (optional) Activation function for the query transform. key_act: (optional) Activation function for the key transform. value_act: (optional) Activation function for the value transform. attention_probs_dropout_prob: (optional) float. Dropout probability of the attention probabilities. initializer_range: float. Range of the weight initializer. do_return_2d_tensor: bool. If True, the output will be of shape [batch_size * from_seq_length, num_attention_heads * size_per_head]. If False, the output will be of shape [batch_size, from_seq_length, num_attention_heads * size_per_head]. batch_size: (Optional) int. If the input is 2D, this might be the batch size of the 3D version of the `from_tensor` and `to_tensor`. from_seq_length: (Optional) If the input is 2D, this might be the seq length of the 3D version of the `from_tensor`. to_seq_length: (Optional) If the input is 2D, this might be the seq length of the 3D version of the `to_tensor`. Returns: float Tensor of shape [batch_size, from_seq_length, num_attention_heads * size_per_head]. (If `do_return_2d_tensor` is true, this will be of shape [batch_size * from_seq_length, num_attention_heads * size_per_head]). Raises: ValueError: Any of the arguments or tensor shapes are invalid. """ def transpose_for_scores(input_tensor, batch_size, num_attention_heads, seq_length, width): output_tensor = tf.reshape( input_tensor, [batch_size, seq_length, num_attention_heads, width]) output_tensor = tf.transpose(output_tensor, [0, 2, 1, 3]) return output_tensor from_shape = get_shape_list(from_tensor, expected_rank=[2, 3]) to_shape = get_shape_list(to_tensor, expected_rank=[2, 3]) if len(from_shape) != len(to_shape): raise ValueError( "The rank of `from_tensor` must match the rank of `to_tensor`.") if len(from_shape) == 3: batch_size = from_shape[0] from_seq_length = from_shape[1] to_seq_length = to_shape[1] elif len(from_shape) == 2: if (batch_size is None or from_seq_length is None or to_seq_length is None): raise ValueError( "When passing in rank 2 tensors to attention_layer, the values " "for `batch_size`, `from_seq_length`, and `to_seq_length` " "must all be specified.") # Scalar dimensions referenced here: # B = batch size (number of sequences) # F = `from_tensor` sequence length # T = `to_tensor` sequence length # N = `num_attention_heads` # H = `size_per_head` from_tensor_2d = reshape_to_matrix(from_tensor) to_tensor_2d = reshape_to_matrix(to_tensor) # `query_layer` = [B*F, N*H] query_layer = tf.layers.dense( from_tensor_2d, num_attention_heads * size_per_head, activation=query_act, name="query", kernel_initializer=create_initializer(initializer_range)) # `key_layer` = [B*T, N*H] key_layer = tf.layers.dense( to_tensor_2d, num_attention_heads * size_per_head, activation=key_act, name="key", kernel_initializer=create_initializer(initializer_range)) # `value_layer` = [B*T, N*H] value_layer = tf.layers.dense( to_tensor_2d, num_attention_heads * size_per_head, activation=value_act, name="value", kernel_initializer=create_initializer(initializer_range)) # `query_layer` = [B, N, F, H] query_layer = transpose_for_scores(query_layer, batch_size, num_attention_heads, from_seq_length, size_per_head) # `key_layer` = [B, N, T, H] key_layer = transpose_for_scores(key_layer, batch_size, num_attention_heads, to_seq_length, size_per_head) # Take the dot product between "query" and "key" to get the raw # attention scores. # `attention_scores` = [B, N, F, T] attention_scores = tf.matmul(query_layer, key_layer, transpose_b=True) attention_scores = tf.multiply(attention_scores, 1.0 / math.sqrt(float(size_per_head))) if attention_mask is not None: # `attention_mask` = [B, 1, F, T] attention_mask = tf.expand_dims(attention_mask, axis=[1]) # Since attention_mask is 1.0 for positions we want to attend and 0.0 for # masked positions, this operation will create a tensor which is 0.0 for # positions we want to attend and -10000.0 for masked positions. adder = (1.0 - tf.cast(attention_mask, attention_scores.dtype)) * -10000.0 # Since we are adding it to the raw scores before the softmax, this is # effectively the same as removing these entirely. attention_scores += adder # Normalize the attention scores to probabilities. # `attention_probs` = [B, N, F, T] attention_probs = tf.nn.softmax(attention_scores) # This is actually dropping out entire tokens to attend to, which might # seem a bit unusual, but is taken from the original Transformer paper. attention_probs = dropout(attention_probs, attention_probs_dropout_prob) # `value_layer` = [B, T, N, H] value_layer = tf.reshape( value_layer, [batch_size, to_seq_length, num_attention_heads, size_per_head]) # `value_layer` = [B, N, T, H] value_layer = tf.transpose(value_layer, [0, 2, 1, 3]) # `context_layer` = [B, N, F, H] context_layer = tf.matmul(attention_probs, value_layer) # `context_layer` = [B, F, N, H] context_layer = tf.transpose(context_layer, [0, 2, 1, 3]) if do_return_2d_tensor: # `context_layer` = [B*F, N*H] context_layer = tf.reshape( context_layer, [batch_size * from_seq_length, num_attention_heads * size_per_head]) else: # `context_layer` = [B, F, N*H] context_layer = tf.reshape( context_layer, [batch_size, from_seq_length, num_attention_heads * size_per_head]) return context_layer def transformer_model(input_tensor, attention_mask=None, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, intermediate_act_fn=gelu, hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, initializer_range=0.02, do_return_all_layers=False): """Multi-headed, multi-layer Transformer from "Attention is All You Need". This is almost an exact implementation of the original Transformer encoder. See the original paper: https://arxiv.org/abs/1706.03762 Also see: https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/models/transformer.py Args: input_tensor: float Tensor of shape [batch_size, seq_length, hidden_size]. attention_mask: (optional) int32 Tensor of shape [batch_size, seq_length, seq_length], with 1 for positions that can be attended to and 0 in positions that should not be. hidden_size: int. Hidden size of the Transformer. num_hidden_layers: int. Number of layers (blocks) in the Transformer. num_attention_heads: int. Number of attention heads in the Transformer. intermediate_size: int. The size of the "intermediate" (a.k.a., feed forward) layer. intermediate_act_fn: function. The non-linear activation function to apply to the output of the intermediate/feed-forward layer. hidden_dropout_prob: float. Dropout probability for the hidden layers. attention_probs_dropout_prob: float. Dropout probability of the attention probabilities. initializer_range: float. Range of the initializer (stddev of truncated normal). do_return_all_layers: Whether to also return all layers or just the final layer. Returns: float Tensor of shape [batch_size, seq_length, hidden_size], the final hidden layer of the Transformer. Raises: ValueError: A Tensor shape or parameter is invalid. """ if hidden_size % num_attention_heads != 0: raise ValueError( "The hidden size (%d) is not a multiple of the number of attention " "heads (%d)" % (hidden_size, num_attention_heads)) attention_head_size = int(hidden_size / num_attention_heads) input_shape = get_shape_list(input_tensor, expected_rank=3) batch_size = input_shape[0] seq_length = input_shape[1] input_width = input_shape[2] # The Transformer performs sum residuals on all layers so the input needs # to be the same as the hidden size. if input_width != hidden_size: raise ValueError("The width of the input tensor (%d) != hidden size (%d)" % (input_width, hidden_size)) # We keep the representation as a 2D tensor to avoid re-shaping it back and # forth from a 3D tensor to a 2D tensor. Re-shapes are normally free on # the GPU/CPU but may not be free on the TPU, so we want to minimize them to # help the optimizer. prev_output = reshape_to_matrix(input_tensor) all_layer_outputs = [] for layer_idx in range(num_hidden_layers): with tf.variable_scope("layer_%d" % layer_idx): layer_input = prev_output with tf.variable_scope("attention"): attention_heads = [] with tf.variable_scope("self"): attention_head = attention_layer( from_tensor=layer_input, to_tensor=layer_input, attention_mask=attention_mask, num_attention_heads=num_attention_heads, size_per_head=attention_head_size, attention_probs_dropout_prob=attention_probs_dropout_prob, initializer_range=initializer_range, do_return_2d_tensor=True, batch_size=batch_size, from_seq_length=seq_length, to_seq_length=seq_length) attention_heads.append(attention_head) attention_output = None if len(attention_heads) == 1: attention_output = attention_heads[0] else: # In the case where we have other sequences, we just concatenate # them to the self-attention head before the projection. attention_output = tf.concat(attention_heads, axis=-1) # Run a linear projection of `hidden_size` then add a residual # with `layer_input`. with tf.variable_scope("output"): attention_output = tf.layers.dense( attention_output, hidden_size, kernel_initializer=create_initializer(initializer_range)) attention_output = dropout(attention_output, hidden_dropout_prob) attention_output = layer_norm(attention_output + layer_input) # The activation is only applied to the "intermediate" hidden layer. with tf.variable_scope("intermediate"): intermediate_output = tf.layers.dense( attention_output, intermediate_size, activation=intermediate_act_fn, kernel_initializer=create_initializer(initializer_range)) # Down-project back to `hidden_size` then add the residual. with tf.variable_scope("output"): layer_output = tf.layers.dense( intermediate_output, hidden_size, kernel_initializer=create_initializer(initializer_range)) layer_output = dropout(layer_output, hidden_dropout_prob) layer_output = layer_norm(layer_output + attention_output) prev_output = layer_output all_layer_outputs.append(layer_output) if do_return_all_layers: final_outputs = [] for layer_output in all_layer_outputs: final_output = reshape_from_matrix(layer_output, input_shape) final_outputs.append(final_output) return final_outputs else: final_output = reshape_from_matrix(prev_output, input_shape) return final_output def get_shape_list(tensor, expected_rank=None, name=None): """Returns a list of the shape of tensor, preferring static dimensions. Args: tensor: A tf.Tensor object to find the shape of. expected_rank: (optional) int. The expected rank of `tensor`. If this is specified and the `tensor` has a different rank, and exception will be thrown. name: Optional name of the tensor for the error message. Returns: A list of dimensions of the shape of tensor. All static dimensions will be returned as python integers, and dynamic dimensions will be returned as tf.Tensor scalars. """ if name is None: name = tensor.name if expected_rank is not None: assert_rank(tensor, expected_rank, name) shape = tensor.shape.as_list() non_static_indexes = [] for (index, dim) in enumerate(shape): if dim is None: non_static_indexes.append(index) if not non_static_indexes: return shape dyn_shape = tf.shape(tensor) for index in non_static_indexes: shape[index] = dyn_shape[index] return shape def reshape_to_matrix(input_tensor): """Reshapes a >= rank 2 tensor to a rank 2 tensor (i.e., a matrix).""" ndims = input_tensor.shape.ndims if ndims < 2: raise ValueError("Input tensor must have at least rank 2. Shape = %s" % (input_tensor.shape)) if ndims == 2: return input_tensor width = input_tensor.shape[-1] output_tensor = tf.reshape(input_tensor, [-1, width]) return output_tensor def reshape_from_matrix(output_tensor, orig_shape_list): """Reshapes a rank 2 tensor back to its original rank >= 2 tensor.""" if len(orig_shape_list) == 2: return output_tensor output_shape = get_shape_list(output_tensor) orig_dims = orig_shape_list[0:-1] width = output_shape[-1] return tf.reshape(output_tensor, orig_dims + [width]) def assert_rank(tensor, expected_rank, name=None): """Raises an exception if the tensor rank is not of the expected rank. Args: tensor: A tf.Tensor to check the rank of. expected_rank: Python integer or list of integers, expected rank. name: Optional name of the tensor for the error message. Raises: ValueError: If the expected shape doesn't match the actual shape. """ if name is None: name = tensor.name expected_rank_dict = {} if isinstance(expected_rank, six.integer_types): expected_rank_dict[expected_rank] = True else: for x in expected_rank: expected_rank_dict[x] = True actual_rank = tensor.shape.ndims if actual_rank not in expected_rank_dict: scope_name = tf.get_variable_scope().name raise ValueError( "For the tensor `%s` in scope `%s`, the actual rank " "`%d` (shape = %s) is not equal to the expected rank `%s`" % (name, scope_name, actual_rank, str(tensor.shape), str(expected_rank)))
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/modeling.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import tensorflow as tf import numpy as np def float32_variable_storage_getter(getter, name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, *args, **kwargs): """Custom variable getter that forces trainable variables to be stored in float32 precision and then casts them to the training precision. """ storage_dtype = tf.float32 if trainable else dtype variable = getter(name, shape, dtype=storage_dtype, initializer=initializer, regularizer=regularizer, trainable=trainable, *args, **kwargs) if trainable and dtype != tf.float32: variable = tf.cast(variable, dtype) return variable
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/fp16_utils.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import modeling import tokenization import tritongrpcclient from utils.create_squad_data import * import grpc from run_squad import write_predictions, get_predictions, RawResult import numpy as np import tqdm from functools import partial import sys if sys.version_info >= (3, 0): import queue else: import Queue as queue flags = tf.flags FLAGS = flags.FLAGS ## Required parameters flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_string( "output_dir", None, "The output directory where the model checkpoints will be written.") flags.DEFINE_bool( "do_lower_case", True, "Whether to lower case the input text. Should be True for uncased " "models and False for cased models.") flags.DEFINE_integer( "max_seq_length", 384, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded.") flags.DEFINE_integer( "doc_stride", 128, "When splitting up a long document into chunks, how much stride to " "take between chunks.") flags.DEFINE_integer( "max_query_length", 64, "The maximum number of tokens for the question. Questions longer than " "this will be truncated to this length.") flags.DEFINE_integer("predict_batch_size", 8, "Total batch size for predictions.") flags.DEFINE_integer( "n_best_size", 20, "The total number of n-best predictions to generate in the " "nbest_predictions.json output file.") flags.DEFINE_integer( "max_answer_length", 30, "The maximum length of an answer that can be generated. This is needed " "because the start and end predictions are not conditioned on one another.") flags.DEFINE_bool( "version_2_with_negative", False, "If true, the SQuAD examples contain some that do not have an answer.") flags.DEFINE_bool( "verbose_logging", False, "If true, all of the warnings related to data processing will be printed. " "A number of warnings are expected for a normal SQuAD evaluation.") flags.DEFINE_bool( "trt_engine", False, "If true, expects a trt engine defined input/output") # Triton Specific flags flags.DEFINE_string("triton_model_name", "bert", "exports to appropriate directory for Triton") flags.DEFINE_integer("triton_model_version", 1, "exports to appropriate directory for Triton") flags.DEFINE_string("triton_server_url", "localhost:8001", "exports to appropriate directory for Triton") # Input Text for Inference flags.DEFINE_string("question", None, "Question for Inference") flags.DEFINE_string("context", None, "Context for Inference") flags.DEFINE_string( "predict_file", None, "SQuAD json for predictions. E.g., dev-v1.1.json or test-v1.1.json") # Set this to either 'label_ids' for Google bert or 'unique_ids' for JoC label_id_key = "unique_ids" # User defined class to store infer_ctx and request id # from callback function and let main thread to handle them class UserData: def __init__(self): self._completed_requests = queue.Queue() # Callback function used for async_run(), it can capture # additional information using functools.partial as long as the last # two arguments are reserved for InferContext and request id def completion_callback(user_data, idx, start_time, inputs, result, error): user_data._completed_requests.put((result, error, idx, start_time, inputs)) def batch(iterable, n=1): l = len(iterable) for ndx in range(0, l, n): label_ids_data = () input_ids_data = () input_mask_data = () segment_ids_data = () for i in range(0, min(n, l-ndx)): label_ids_data = label_ids_data + (np.array([iterable[ndx + i].unique_id], dtype=np.int32),) input_ids_data = input_ids_data+ (np.array(iterable[ndx + i].input_ids, dtype=np.int32),) input_mask_data = input_mask_data+ (np.array(iterable[ndx + i].input_mask, dtype=np.int32),) segment_ids_data = segment_ids_data+ (np.array(iterable[ndx + i].segment_ids, dtype=np.int32),) if FLAGS.trt_engine and len(label_ids_data) != n: #TRT needs exact batch size. Pad as necessary pad_size = n - len(label_ids_data) label_ids_data = label_ids_data + ((np.array([0], dtype=np.int32),) * pad_size) input_ids_data = input_ids_data + ((np.zeros(FLAGS.max_seq_length, dtype=np.int32),) * pad_size) input_mask_data = input_mask_data + ((np.zeros(FLAGS.max_seq_length, dtype=np.int32),) * pad_size) segment_ids_data = segment_ids_data + ((np.zeros(FLAGS.max_seq_length, dtype=np.int32),) * pad_size) inputs_dict = {label_id_key: label_ids_data, 'input_ids': input_ids_data, 'input_mask': input_mask_data, 'segment_ids': segment_ids_data} yield inputs_dict def main(_): """ Ask a question of context on Triton. :param context: str :param question: str :param question_id: int :return: """ os.environ["TF_XLA_FLAGS"] = "--tf_xla_enable_lazy_compilation=false" #causes memory fragmentation for bert leading to OOM tf.compat.v1.logging.info("***** Configuaration *****") for key in FLAGS.__flags.keys(): tf.compat.v1.logging.info(' {}: {}'.format(key, getattr(FLAGS, key))) tf.compat.v1.logging.info("**************************") tokenizer = tokenization.FullTokenizer(vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) # Get the Data if FLAGS.question and FLAGS.context: input_data = [{"paragraphs":[{"context":FLAGS.context, "qas":[{"id":0, "question":FLAGS.question}]}]}] eval_examples = read_squad_examples(input_file=None, is_training=False, version_2_with_negative=FLAGS.version_2_with_negative, input_data=input_data) elif FLAGS.predict_file: eval_examples = read_squad_examples( input_file=FLAGS.predict_file, is_training=False, version_2_with_negative=FLAGS.version_2_with_negative) else: raise ValueError("Either predict_file or question+answer need to defined") # Get Eval Features = Preprocessing eval_features = [] def append_feature(feature): eval_features.append(feature) convert_examples_to_features( examples=eval_examples, tokenizer=tokenizer, max_seq_length=FLAGS.max_seq_length, doc_stride=FLAGS.doc_stride, max_query_length=FLAGS.max_query_length, is_training=False, output_fn=append_feature) protocol_str = 'grpc' # http or grpc url = FLAGS.triton_server_url verbose = False model_name = FLAGS.triton_model_name model_version = str(FLAGS.triton_model_version) batch_size = FLAGS.predict_batch_size triton_client = tritongrpcclient.InferenceServerClient(url, verbose) model_metadata = triton_client.get_model_metadata( model_name=model_name, model_version=model_version) model_config = triton_client.get_model_config( model_name=model_name, model_version=model_version) user_data = UserData() max_outstanding = 20 # Number of outstanding requests outstanding = 0 sent_prog = tqdm.tqdm(desc="Send Requests", total=len(eval_features)) recv_prog = tqdm.tqdm(desc="Recv Requests", total=len(eval_features)) def process_outstanding(do_wait, outstanding): if (outstanding == 0 or do_wait is False): return outstanding # Wait for deferred items from callback functions (result, error, idx, start_time, inputs) = user_data._completed_requests.get() if (result is None): return outstanding stop = time.time() if (error is not None): raise ValueError("Context returned null for async id marked as done") outstanding -= 1 time_list.append(stop - start_time) batch_count = len(inputs[label_id_key]) if FLAGS.trt_engine: cls_squad_logits = result.as_numpy("cls_squad_logits") try: #when batch size > 1 start_logits_results = np.array(cls_squad_logits.squeeze()[:, :, 0]) end_logits_results = np.array(cls_squad_logits.squeeze()[:, :, 1]) except: start_logits_results = np.expand_dims(np.array(cls_squad_logits.squeeze()[:, 0]), axis=0) end_logits_results = np.expand_dims(np.array(cls_squad_logits.squeeze()[:, 1]), axis=0) else: start_logits_results = result.as_numpy("start_logits") end_logits_results = result.as_numpy("end_logits") for i in range(batch_count): unique_id = int(inputs[label_id_key][i][0]) start_logits = [float(x) for x in start_logits_results[i].flat] end_logits = [float(x) for x in end_logits_results[i].flat] all_results.append( RawResult( unique_id=unique_id, start_logits=start_logits, end_logits=end_logits)) recv_prog.update(n=batch_count) return outstanding all_results = [] time_list = [] print("Starting Sending Requests....\n") all_results_start = time.time() idx = 0 for inputs_dict in batch(eval_features, batch_size): present_batch_size = len(inputs_dict[label_id_key]) if not FLAGS.trt_engine: label_ids_data = np.stack(inputs_dict[label_id_key]) input_ids_data = np.stack(inputs_dict['input_ids']) input_mask_data = np.stack(inputs_dict['input_mask']) segment_ids_data = np.stack(inputs_dict['segment_ids']) inputs = [] inputs.append(tritongrpcclient.InferInput('input_ids', input_ids_data.shape, "INT32")) inputs[0].set_data_from_numpy(input_ids_data) inputs.append(tritongrpcclient.InferInput('input_mask', input_mask_data.shape, "INT32")) inputs[1].set_data_from_numpy(input_mask_data) inputs.append(tritongrpcclient.InferInput('segment_ids', segment_ids_data.shape, "INT32")) inputs[2].set_data_from_numpy(segment_ids_data) if not FLAGS.trt_engine: inputs.append(tritongrpcclient.InferInput(label_id_key, label_ids_data.shape, "INT32")) inputs[3].set_data_from_numpy(label_ids_data) outputs = [] if FLAGS.trt_engine: outputs.append(tritongrpcclient.InferRequestedOutput('cls_squad_logits')) else: outputs.append(tritongrpcclient.InferRequestedOutput('start_logits')) outputs.append(tritongrpcclient.InferRequestedOutput('end_logits')) start_time = time.time() triton_client.async_infer( model_name, inputs, partial(completion_callback, user_data, idx, start_time, inputs_dict), request_id=str(idx), model_version=model_version, outputs=outputs) outstanding += 1 idx += 1 sent_prog.update(n=present_batch_size) # Try to process at least one response per request outstanding = process_outstanding(outstanding >= max_outstanding, outstanding) tqdm.tqdm.write("All Requests Sent! Waiting for responses. Outstanding: {}.\n".format(outstanding)) # Now process all outstanding requests while (outstanding > 0): outstanding = process_outstanding(True, outstanding) all_results_end = time.time() all_results_total = (all_results_end - all_results_start) * 1000.0 print("-----------------------------") print("Total Time: {} ms".format(all_results_total)) print("-----------------------------") print("-----------------------------") print("Total Inference Time = %0.2f for" "Sentences processed = %d" % (sum(time_list), len(eval_features))) print("Throughput Average (sentences/sec) = %0.2f" % (len(eval_features) / all_results_total * 1000.0)) print("-----------------------------") if FLAGS.output_dir and FLAGS.predict_file: # When inferencing on a dataset, get inference statistics and write results to json file time_list.sort() avg = np.mean(time_list) cf_95 = max(time_list[:int(len(time_list) * 0.95)]) cf_99 = max(time_list[:int(len(time_list) * 0.99)]) cf_100 = max(time_list[:int(len(time_list) * 1)]) print("-----------------------------") print("Summary Statistics") print("Batch size =", FLAGS.predict_batch_size) print("Sequence Length =", FLAGS.max_seq_length) print("Latency Confidence Level 95 (ms) =", cf_95 * 1000) print("Latency Confidence Level 99 (ms) =", cf_99 * 1000) print("Latency Confidence Level 100 (ms) =", cf_100 * 1000) print("Latency Average (ms) =", avg * 1000) print("-----------------------------") output_prediction_file = os.path.join(FLAGS.output_dir, "predictions.json") output_nbest_file = os.path.join(FLAGS.output_dir, "nbest_predictions.json") output_null_log_odds_file = os.path.join(FLAGS.output_dir, "null_odds.json") write_predictions(eval_examples, eval_features, all_results, FLAGS.n_best_size, FLAGS.max_answer_length, FLAGS.do_lower_case, output_prediction_file, output_nbest_file, output_null_log_odds_file, FLAGS.version_2_with_negative, FLAGS.verbose_logging) else: # When inferencing on a single example, write best answer to stdout all_predictions, all_nbest_json, scores_diff_json = get_predictions( eval_examples, eval_features, all_results, FLAGS.n_best_size, FLAGS.max_answer_length, FLAGS.do_lower_case, FLAGS.version_2_with_negative, FLAGS.verbose_logging) print("Context is: %s \n\nQuestion is: %s \n\nPredicted Answer is: %s" %(FLAGS.context, FLAGS.question, all_predictions[0])) if __name__ == "__main__": flags.mark_flag_as_required("vocab_file") tf.compat.v1.app.run()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/triton/run_squad_triton_client.py
# Copyright (c) 2020 NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math import os import pynvml pynvml.nvmlInit() def systemGetDriverVersion(): return pynvml.nvmlSystemGetDriverVersion() def deviceGetCount(): return pynvml.nvmlDeviceGetCount() class device: # assume nvml returns list of 64 bit ints _nvml_affinity_elements = math.ceil(os.cpu_count() / 64) def __init__(self, device_idx): super().__init__() self.handle = pynvml.nvmlDeviceGetHandleByIndex(device_idx) def getName(self): return pynvml.nvmlDeviceGetName(self.handle) def getCpuAffinity(self): affinity_string = '' for j in pynvml.nvmlDeviceGetCpuAffinity( self.handle, device._nvml_affinity_elements ): # assume nvml returns list of 64 bit ints affinity_string = '{:064b}'.format(j) + affinity_string affinity_list = [int(x) for x in affinity_string] affinity_list.reverse() # so core 0 is in 0th element of list return [i for i, e in enumerate(affinity_list) if e != 0] def set_affinity(gpu_id=None): if gpu_id is None: gpu_id = int(os.getenv('LOCAL_RANK', 0)) dev = device(gpu_id) os.sched_setaffinity(0, dev.getCpuAffinity()) # list of ints representing the logical cores this process is now affinitied with return os.sched_getaffinity(0)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/utils/gpu_affinity.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from dllogger import Logger, StdOutBackend, JSONStreamBackend, Verbosity import numpy class dllogger_class(): def format_step(self, step): if isinstance(step, str): return step elif isinstance(step, int): return "Iteration: {} ".format(step) elif len(step) > 0: return "Iteration: {} ".format(step[0]) else: return "" def __init__(self, log_path="bert_dllog.json"): self.logger = Logger([ StdOutBackend(Verbosity.DEFAULT, step_format=self.format_step), JSONStreamBackend(Verbosity.VERBOSE, log_path), ]) self.logger.metadata("mlm_loss", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "TRAIN"}) self.logger.metadata("nsp_loss", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "TRAIN"}) self.logger.metadata("avg_loss_step", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "TRAIN"}) self.logger.metadata("total_loss", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "TRAIN"}) self.logger.metadata("loss", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "TRAIN"}) self.logger.metadata("f1", {"unit": None, "format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "VAL"}) self.logger.metadata("precision", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "VAL"}) self.logger.metadata("recall", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "VAL"}) self.logger.metadata("mcc", {"format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "VAL"}) self.logger.metadata("exact_match", {"unit": None, "format": ":.4f", "GOAL": "MINIMIZE", "STAGE": "VAL"}) self.logger.metadata( "throughput_train", {"unit": "sequences/s", "format": ":.3f", "GOAL": "MAXIMIZE", "STAGE": "TRAIN"}, ) self.logger.metadata( "throughput_inf", {"unit": "sequences/s", "format": ":.3f", "GOAL": "MAXIMIZE", "STAGE": "VAL"}, )
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/utils/dllogger_class.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import tensorflow as tf import time import os def setup_xla_flags(): # causes memory fragmentation for bert leading to OOM if os.environ.get("TF_XLA_FLAGS", None) is not None: try: os.environ["TF_XLA_FLAGS"] += " --tf_xla_enable_lazy_compilation=false" except: #mpi 4.0.2 causes syntax error for = os.environ["TF_XLA_FLAGS"] += " --tf_xla_enable_lazy_compilation false" else: try: os.environ["TF_XLA_FLAGS"] = " --tf_xla_enable_lazy_compilation=false" except: os.environ["TF_XLA_FLAGS"] = " --tf_xla_enable_lazy_compilation false" # report latency and throughput during eval class LogEvalRunHook(tf.estimator.SessionRunHook): def __init__(self, global_batch_size, hvd_rank=-1): self.global_batch_size = global_batch_size self.hvd_rank = hvd_rank self.count = 0 self.time_list = [] def before_run(self, run_context): self.t0 = time.time() def after_run(self, run_context, run_values): elapsed_secs = time.time() - self.t0 self.count += 1 self.time_list.append(elapsed_secs) # report throughput during training class LogTrainRunHook(tf.estimator.SessionRunHook): def __init__(self, global_batch_size, hvd_rank=-1, save_checkpoints_steps=1000, num_steps_ignore_xla=100): self.global_batch_size = global_batch_size self.hvd_rank = hvd_rank self.save_checkpoints_steps = save_checkpoints_steps self.total_time = 0.0 self.count = 0 # Holds number of iterations, including skipped iterations for fp16 loss scaling self.skipped = 0 self.num_steps_ignore_xla = num_steps_ignore_xla #initial steps while xla is still compilingneed to be ignored from throughput computation def after_create_session(self, session, coord): self.init_global_step = session.run(tf.train.get_global_step()) def before_run(self, run_context): self.t0 = time.time() return tf.estimator.SessionRunArgs( fetches=['step_update:0']) def after_run(self, run_context, run_values): elapsed_secs = time.time() - self.t0 self.global_step = run_values.results[0] self.count += 1 # Removing first 100 step + first five steps after every checkpoint save if (self.global_step - self.init_global_step) <= self.num_steps_ignore_xla or (self.global_step - self.init_global_step) % self.save_checkpoints_steps < 5: print("Skipping time record for ", self.global_step, " due to checkpoint-saving/warmup overhead") self.skipped += 1 else: self.total_time += elapsed_secs
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/utils/utils.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Create masked LM/next sentence masked_lm TF examples for BERT.""" from __future__ import absolute_import, division, print_function, unicode_literals import argparse import logging import os import random from io import open import h5py import tensorflow as tf import numpy as np from tqdm import tqdm, trange from tokenization import BertTokenizer import tokenization as tokenization import random import collections class TrainingInstance(object): """A single training instance (sentence pair).""" def __init__(self, tokens, segment_ids, masked_lm_positions, masked_lm_labels, is_random_next): self.tokens = tokens self.segment_ids = segment_ids self.is_random_next = is_random_next self.masked_lm_positions = masked_lm_positions self.masked_lm_labels = masked_lm_labels def __str__(self): s = "" s += "tokens: %s\n" % (" ".join( [tokenization.printable_text(x) for x in self.tokens])) s += "segment_ids: %s\n" % (" ".join([str(x) for x in self.segment_ids])) s += "is_random_next: %s\n" % self.is_random_next s += "masked_lm_positions: %s\n" % (" ".join( [str(x) for x in self.masked_lm_positions])) s += "masked_lm_labels: %s\n" % (" ".join( [tokenization.printable_text(x) for x in self.masked_lm_labels])) s += "\n" return s def __repr__(self): return self.__str__() def write_instance_to_example_files(instances, tokenizer, max_seq_length, max_predictions_per_seq, output_files, output_formats="tfrecord"): """Create TF example files from `TrainingInstance`s.""" writers = [] for output_file in output_files: writers.append(tf.python_io.TFRecordWriter(output_file)) writer_index = 0 total_written = 0 if 'hdf5' in output_formats: features_hdf5 = collections.OrderedDict() num_instances = len(instances) features_hdf5["input_ids"] = np.zeros([num_instances, max_seq_length], dtype="int32") features_hdf5["input_mask"] = np.zeros([num_instances, max_seq_length], dtype="int32") features_hdf5["segment_ids"] = np.zeros([num_instances, max_seq_length], dtype="int32") features_hdf5["masked_lm_positions"] = np.zeros([num_instances, max_predictions_per_seq], dtype="int32") features_hdf5["masked_lm_ids"] = np.zeros([num_instances, max_predictions_per_seq], dtype="int32") features_hdf5["next_sentence_labels"] = np.zeros(num_instances, dtype="int32") for (inst_index, instance) in enumerate(instances): input_ids = tokenizer.convert_tokens_to_ids(instance.tokens) input_mask = [1] * len(input_ids) segment_ids = list(instance.segment_ids) assert len(input_ids) <= max_seq_length while len(input_ids) < max_seq_length: input_ids.append(0) input_mask.append(0) segment_ids.append(0) assert len(input_ids) == max_seq_length assert len(input_mask) == max_seq_length assert len(segment_ids) == max_seq_length masked_lm_positions = list(instance.masked_lm_positions) masked_lm_ids = tokenizer.convert_tokens_to_ids(instance.masked_lm_labels) masked_lm_weights = [1.0] * len(masked_lm_ids) while len(masked_lm_positions) < max_predictions_per_seq: masked_lm_positions.append(0) masked_lm_ids.append(0) masked_lm_weights.append(0.0) next_sentence_label = 1 if instance.is_random_next else 0 features = collections.OrderedDict() features["input_ids"] = create_int_feature(input_ids) features["input_mask"] = create_int_feature(input_mask) features["segment_ids"] = create_int_feature(segment_ids) features["masked_lm_positions"] = create_int_feature(masked_lm_positions) features["masked_lm_ids"] = create_int_feature(masked_lm_ids) features["masked_lm_weights"] = create_float_feature(masked_lm_weights) features["next_sentence_labels"] = create_int_feature([next_sentence_label]) if 'tfrecord' in output_formats: tf_example = tf.train.Example(features=tf.train.Features(feature=features)) writers[writer_index].write(tf_example.SerializeToString()) if 'hdf5' in output_formats: features_hdf5["input_ids"][inst_index] = input_ids features_hdf5["input_mask"][inst_index] = input_mask features_hdf5["segment_ids"][inst_index] = segment_ids features_hdf5["masked_lm_positions"][inst_index] = masked_lm_positions features_hdf5["masked_lm_ids"][inst_index] = masked_lm_ids features_hdf5["next_sentence_labels"][inst_index] = next_sentence_label if 'tfrecord' not in output_formats and 'hdf5' not in output_formats: assert False, 'Either empty output_formats list or unsupported type specified. Try: tfrecord or hdf5' writer_index = (writer_index + 1) % len(writers) total_written += 1 if inst_index < 20: tf.compat.v1.logging.info("*** Example ***") tf.compat.v1.logging.info("tokens: %s" % " ".join( [tokenization.printable_text(x) for x in instance.tokens])) for feature_name in features.keys(): feature = features[feature_name] values = [] if feature.int64_list.value: values = feature.int64_list.value elif feature.float_list.value: values = feature.float_list.value tf.compat.v1.logging.info( "%s: %s" % (feature_name, " ".join([str(x) for x in values]))) for writer in writers: writer.close() if 'hdf5' in output_formats: f = h5py.File(output_file, 'w') f.create_dataset("input_ids", data=features_hdf5["input_ids"], dtype='i4', compression='gzip') f.create_dataset("input_mask", data=features_hdf5["input_mask"], dtype='i1', compression='gzip') f.create_dataset("segment_ids", data=features_hdf5["segment_ids"], dtype='i1', compression='gzip') f.create_dataset("masked_lm_positions", data=features_hdf5["masked_lm_positions"], dtype='i4', compression='gzip') f.create_dataset("masked_lm_ids", data=features_hdf5["masked_lm_ids"], dtype='i4', compression='gzip') f.create_dataset("next_sentence_labels", data=features_hdf5["next_sentence_labels"], dtype='i1', compression='gzip') f.flush() f.close() tf.compat.v1.logging.info("Wrote %d total instances", total_written) def create_int_feature(values): feature = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return feature def create_float_feature(values): feature = tf.train.Feature(float_list=tf.train.FloatList(value=list(values))) return feature def create_training_instances(input_files, tokenizer, max_seq_length, dupe_factor, short_seq_prob, masked_lm_prob, max_predictions_per_seq, rng): """Create `TrainingInstance`s from raw text.""" all_documents = [[]] # Input file format: # (1) One sentence per line. These should ideally be actual sentences, not # entire paragraphs or arbitrary spans of text. (Because we use the # sentence boundaries for the "next sentence prediction" task). # (2) Blank lines between documents. Document boundaries are needed so # that the "next sentence prediction" task doesn't span between documents. for input_file in input_files: print("creating instance from {}".format(input_file)) with open(input_file, "r") as reader: while True: line = tokenization.convert_to_unicode(reader.readline()) if not line: break line = line.strip() # Empty lines are used as document delimiters if not line: all_documents.append([]) tokens = tokenizer.tokenize(line) if tokens: all_documents[-1].append(tokens) # Remove empty documents all_documents = [x for x in all_documents if x] rng.shuffle(all_documents) vocab_words = list(tokenizer.vocab.keys()) instances = [] for _ in range(dupe_factor): for document_index in range(len(all_documents)): instances.extend( create_instances_from_document( all_documents, document_index, max_seq_length, short_seq_prob, masked_lm_prob, max_predictions_per_seq, vocab_words, rng)) rng.shuffle(instances) return instances def create_instances_from_document( all_documents, document_index, max_seq_length, short_seq_prob, masked_lm_prob, max_predictions_per_seq, vocab_words, rng): """Creates `TrainingInstance`s for a single document.""" document = all_documents[document_index] # Account for [CLS], [SEP], [SEP] max_num_tokens = max_seq_length - 3 # We *usually* want to fill up the entire sequence since we are padding # to `max_seq_length` anyways, so short sequences are generally wasted # computation. However, we *sometimes* # (i.e., short_seq_prob == 0.1 == 10% of the time) want to use shorter # sequences to minimize the mismatch between pre-training and fine-tuning. # The `target_seq_length` is just a rough target however, whereas # `max_seq_length` is a hard limit. target_seq_length = max_num_tokens if rng.random() < short_seq_prob: target_seq_length = rng.randint(2, max_num_tokens) # We DON'T just concatenate all of the tokens from a document into a long # sequence and choose an arbitrary split point because this would make the # next sentence prediction task too easy. Instead, we split the input into # segments "A" and "B" based on the actual "sentences" provided by the user # input. instances = [] current_chunk = [] current_length = 0 i = 0 while i < len(document): segment = document[i] current_chunk.append(segment) current_length += len(segment) if i == len(document) - 1 or current_length >= target_seq_length: if current_chunk: # `a_end` is how many segments from `current_chunk` go into the `A` # (first) sentence. a_end = 1 if len(current_chunk) >= 2: a_end = rng.randint(1, len(current_chunk) - 1) tokens_a = [] for j in range(a_end): tokens_a.extend(current_chunk[j]) tokens_b = [] # Random next is_random_next = False if len(current_chunk) == 1 or rng.random() < 0.5: is_random_next = True target_b_length = target_seq_length - len(tokens_a) # This should rarely go for more than one iteration for large # corpora. However, just to be careful, we try to make sure that # the random document is not the same as the document # we're processing. for _ in range(10): random_document_index = rng.randint(0, len(all_documents) - 1) if random_document_index != document_index: break #If picked random document is the same as the current document if random_document_index == document_index: is_random_next = False random_document = all_documents[random_document_index] random_start = rng.randint(0, len(random_document) - 1) for j in range(random_start, len(random_document)): tokens_b.extend(random_document[j]) if len(tokens_b) >= target_b_length: break # We didn't actually use these segments so we "put them back" so # they don't go to waste. num_unused_segments = len(current_chunk) - a_end i -= num_unused_segments # Actual next else: is_random_next = False for j in range(a_end, len(current_chunk)): tokens_b.extend(current_chunk[j]) truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng) assert len(tokens_a) >= 1 assert len(tokens_b) >= 1 tokens = [] segment_ids = [] tokens.append("[CLS]") segment_ids.append(0) for token in tokens_a: tokens.append(token) segment_ids.append(0) tokens.append("[SEP]") segment_ids.append(0) for token in tokens_b: tokens.append(token) segment_ids.append(1) tokens.append("[SEP]") segment_ids.append(1) (tokens, masked_lm_positions, masked_lm_labels) = create_masked_lm_predictions( tokens, masked_lm_prob, max_predictions_per_seq, vocab_words, rng) instance = TrainingInstance( tokens=tokens, segment_ids=segment_ids, is_random_next=is_random_next, masked_lm_positions=masked_lm_positions, masked_lm_labels=masked_lm_labels) instances.append(instance) current_chunk = [] current_length = 0 i += 1 return instances MaskedLmInstance = collections.namedtuple("MaskedLmInstance", ["index", "label"]) def create_masked_lm_predictions(tokens, masked_lm_prob, max_predictions_per_seq, vocab_words, rng): """Creates the predictions for the masked LM objective.""" cand_indexes = [] for (i, token) in enumerate(tokens): if token == "[CLS]" or token == "[SEP]": continue cand_indexes.append(i) rng.shuffle(cand_indexes) output_tokens = list(tokens) num_to_predict = min(max_predictions_per_seq, max(1, int(round(len(tokens) * masked_lm_prob)))) masked_lms = [] covered_indexes = set() for index in cand_indexes: if len(masked_lms) >= num_to_predict: break if index in covered_indexes: continue covered_indexes.add(index) masked_token = None # 80% of the time, replace with [MASK] if rng.random() < 0.8: masked_token = "[MASK]" else: # 10% of the time, keep original if rng.random() < 0.5: masked_token = tokens[index] # 10% of the time, replace with random word else: masked_token = vocab_words[rng.randint(0, len(vocab_words) - 1)] output_tokens[index] = masked_token masked_lms.append(MaskedLmInstance(index=index, label=tokens[index])) masked_lms = sorted(masked_lms, key=lambda x: x.index) masked_lm_positions = [] masked_lm_labels = [] for p in masked_lms: masked_lm_positions.append(p.index) masked_lm_labels.append(p.label) return (output_tokens, masked_lm_positions, masked_lm_labels) def truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng): """Truncates a pair of sequences to a maximum sequence length.""" while True: total_length = len(tokens_a) + len(tokens_b) if total_length <= max_num_tokens: break trunc_tokens = tokens_a if len(tokens_a) > len(tokens_b) else tokens_b assert len(trunc_tokens) >= 1 # We want to sometimes truncate from the front and sometimes from the # back to add more randomness and avoid biases. if rng.random() < 0.5: del trunc_tokens[0] else: trunc_tokens.pop() def main(): parser = argparse.ArgumentParser() ## Required parameters parser.add_argument("--vocab_file", default=None, type=str, required=True, help="The vocabulary the BERT model will train on.") parser.add_argument("--input_file", default=None, type=str, required=True, help="The input train corpus. can be directory with .txt files or a path to a single file") parser.add_argument("--output_file", default=None, type=str, required=True, help="The output file where the model checkpoints will be written.") ## Other parameters # int parser.add_argument("--max_seq_length", default=128, type=int, help="The maximum total input sequence length after WordPiece tokenization. \n" "Sequences longer than this will be truncated, and sequences shorter \n" "than this will be padded.") parser.add_argument("--dupe_factor", default=10, type=int, help="Number of times to duplicate the input data (with different masks).") parser.add_argument("--max_predictions_per_seq", default=20, type=int, help="Maximum sequence length.") # floats parser.add_argument("--masked_lm_prob", default=0.15, type=float, help="Masked LM probability.") parser.add_argument("--short_seq_prob", default=0.1, type=float, help="Probability to create a sequence shorter than maximum sequence length") parser.add_argument("--do_lower_case", action='store_true', default=True, help="Whether to lower case the input text. True for uncased models, False for cased models.") parser.add_argument('--random_seed', type=int, default=12345, help="random seed for initialization") args = parser.parse_args() tokenizer = BertTokenizer(args.vocab_file, do_lower_case=args.do_lower_case) input_files = [] if os.path.isfile(args.input_file): input_files.append(args.input_file) elif os.path.isdir(args.input_file): input_files = [os.path.join(args.input_file, f) for f in os.listdir(args.input_file) if (os.path.isfile(os.path.join(args.input_file, f)) and f.endswith('.txt'))] else: raise ValueError("{} is not a valid path".format(args.input_file)) rng = random.Random(args.random_seed) instances = create_training_instances( input_files, tokenizer, args.max_seq_length, args.dupe_factor, args.short_seq_prob, args.masked_lm_prob, args.max_predictions_per_seq, rng) output_files = args.output_file.split(",") print("*** Writing to output files ***") for output_file in output_files: print(output_file) write_instance_to_example_files(instances, tokenizer, args.max_seq_length, args.max_predictions_per_seq, output_files) if __name__ == "__main__": main()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/utils/create_pretraining_data.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import json import math import os import random import modeling import optimization import tokenization import six import tensorflow as tf import horovod.tensorflow as hvd import time import csv flags = tf.flags FLAGS = None def extract_flags(): ## Required parameters flags.DEFINE_string( "data_dir", None, "The input data dir. Should contain the .tsv files (or other data files) " "for the task.") flags.DEFINE_string("task_name", None, "The name of the task to train.") flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_bool( "do_lower_case", True, "Whether to lower case the input text. Should be True for uncased " "models and False for cased models.") flags.DEFINE_integer( "max_seq_length", 128, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded.") flags.DEFINE_bool( "verbose_logging", False, "If true, all of the warnings related to data processing will be printed. " "A number of warnings are expected for a normal SQuAD evaluation.") flags.mark_flag_as_required("data_dir") flags.mark_flag_as_required("task_name") flags.mark_flag_as_required("vocab_file") return flags.FLAGS class InputExample(object): """A single training/test example for simple sequence classification.""" def __init__(self, guid, text_a, text_b=None, label=None): """Constructs a InputExample. Args: guid: Unique id for the example. text_a: string. The untokenized text of the first sequence. For single sequence tasks, only this sequence must be specified. text_b: (Optional) string. The untokenized text of the second sequence. Only must be specified for sequence pair tasks. label: (Optional) string. The label of the example. This should be specified for train and dev examples, but not for test examples. """ self.guid = guid self.text_a = text_a self.text_b = text_b self.label = label class PaddingInputExample(object): """Fake example so the num input examples is a multiple of the batch size. When running eval/predict on the TPU, we need to pad the number of examples to be a multiple of the batch size, because the TPU requires a fixed batch size. The alternative is to drop the last batch, which is bad because it means the entire output data won't be generated. We use this class instead of `None` because treating `None` as padding battches could cause silent errors. """ class InputFeatures(object): """A single set of features of data.""" def __init__(self, input_ids, input_mask, segment_ids, label_id, is_real_example=True): self.input_ids = input_ids self.input_mask = input_mask self.segment_ids = segment_ids self.label_id = label_id self.is_real_example = is_real_example class DataProcessor(object): """Base class for data converters for sequence classification data sets.""" def get_train_examples(self, data_dir): """Gets a collection of `InputExample`s for the train set.""" raise NotImplementedError() def get_dev_examples(self, data_dir): """Gets a collection of `InputExample`s for the dev set.""" raise NotImplementedError() def get_test_examples(self, data_dir): """Gets a collection of `InputExample`s for prediction.""" raise NotImplementedError() def get_labels(self): """Gets the list of labels for this data set.""" raise NotImplementedError() @classmethod def _read_tsv(cls, input_file, quotechar=None): """Reads a tab separated value file.""" with tf.gfile.Open(input_file, "r") as f: reader = csv.reader(f, delimiter="\t", quotechar=quotechar) lines = [] for line in reader: lines.append(line) return lines class XnliProcessor(DataProcessor): """Processor for the XNLI data set.""" def __init__(self): self.language = "zh" def get_train_examples(self, data_dir): """See base class.""" lines = self._read_tsv( os.path.join(data_dir, "multinli", "multinli.train.%s.tsv" % self.language)) examples = [] for (i, line) in enumerate(lines): if i == 0: continue guid = "train-%d" % (i) text_a = tokenization.convert_to_unicode(line[0]) text_b = tokenization.convert_to_unicode(line[1]) label = tokenization.convert_to_unicode(line[2]) if label == tokenization.convert_to_unicode("contradictory"): label = tokenization.convert_to_unicode("contradiction") examples.append( InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)) return examples def get_dev_examples(self, data_dir): """See base class.""" lines = self._read_tsv(os.path.join(data_dir, "xnli.dev.tsv")) examples = [] for (i, line) in enumerate(lines): if i == 0: continue guid = "dev-%d" % (i) language = tokenization.convert_to_unicode(line[0]) if language != tokenization.convert_to_unicode(self.language): continue text_a = tokenization.convert_to_unicode(line[6]) text_b = tokenization.convert_to_unicode(line[7]) label = tokenization.convert_to_unicode(line[1]) examples.append( InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)) return examples def get_labels(self): """See base class.""" return ["contradiction", "entailment", "neutral"] class MnliProcessor(DataProcessor): """Processor for the MultiNLI data set (GLUE version).""" def get_train_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "train.tsv")), "train") def get_dev_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "dev_matched.tsv")), "dev_matched") def get_test_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "test_matched.tsv")), "test") def get_labels(self): """See base class.""" return ["contradiction", "entailment", "neutral"] def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): if i == 0: continue guid = "%s-%s" % (set_type, tokenization.convert_to_unicode(line[0])) text_a = tokenization.convert_to_unicode(line[8]) text_b = tokenization.convert_to_unicode(line[9]) if set_type == "test": label = "contradiction" else: label = tokenization.convert_to_unicode(line[-1]) examples.append( InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)) return examples class MrpcProcessor(DataProcessor): """Processor for the MRPC data set (GLUE version).""" def get_train_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "train.tsv")), "train") def get_dev_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev") def get_test_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "test.tsv")), "test") def get_labels(self): """See base class.""" return ["0", "1"] def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): if i == 0: continue guid = "%s-%s" % (set_type, i) text_a = tokenization.convert_to_unicode(line[3]) text_b = tokenization.convert_to_unicode(line[4]) if set_type == "test": label = "0" else: label = tokenization.convert_to_unicode(line[0]) examples.append( InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)) return examples class ColaProcessor(DataProcessor): """Processor for the CoLA data set (GLUE version).""" def get_train_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "train.tsv")), "train") def get_dev_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev") def get_test_examples(self, data_dir): """See base class.""" return self._create_examples( self._read_tsv(os.path.join(data_dir, "test.tsv")), "test") def get_labels(self): """See base class.""" return ["0", "1"] def _create_examples(self, lines, set_type): """Creates examples for the training and dev sets.""" examples = [] for (i, line) in enumerate(lines): # Only the test set has a header if set_type == "test" and i == 0: continue guid = "%s-%s" % (set_type, i) if set_type == "test": text_a = tokenization.convert_to_unicode(line[1]) label = "0" else: text_a = tokenization.convert_to_unicode(line[3]) label = tokenization.convert_to_unicode(line[1]) examples.append( InputExample(guid=guid, text_a=text_a, text_b=None, label=label)) return examples def _truncate_seq_pair(tokens_a, tokens_b, max_length): """Truncates a sequence pair in place to the maximum length.""" # This is a simple heuristic which will always truncate the longer sequence # one token at a time. This makes more sense than truncating an equal percent # of tokens from each, since if one sequence is very short then each token # that's truncated likely contains more information than a longer sequence. while True: total_length = len(tokens_a) + len(tokens_b) if total_length <= max_length: break if len(tokens_a) > len(tokens_b): tokens_a.pop() else: tokens_b.pop() def convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer, verbose_logging=False): """Converts a single `InputExample` into a single `InputFeatures`.""" if isinstance(example, PaddingInputExample): return InputFeatures( input_ids=[0] * max_seq_length, input_mask=[0] * max_seq_length, segment_ids=[0] * max_seq_length, label_id=0, is_real_example=False) label_map = {} for (i, label) in enumerate(label_list): label_map[label] = i tokens_a = tokenizer.tokenize(example.text_a) tokens_b = None if example.text_b: tokens_b = tokenizer.tokenize(example.text_b) if tokens_b: # Modifies `tokens_a` and `tokens_b` in place so that the total # length is less than the specified length. # Account for [CLS], [SEP], [SEP] with "- 3" _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3) else: # Account for [CLS] and [SEP] with "- 2" if len(tokens_a) > max_seq_length - 2: tokens_a = tokens_a[0:(max_seq_length - 2)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens = [] segment_ids = [] tokens.append("[CLS]") segment_ids.append(0) for token in tokens_a: tokens.append(token) segment_ids.append(0) tokens.append("[SEP]") segment_ids.append(0) if tokens_b: for token in tokens_b: tokens.append(token) segment_ids.append(1) tokens.append("[SEP]") segment_ids.append(1) input_ids = tokenizer.convert_tokens_to_ids(tokens) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. input_mask = [1] * len(input_ids) # Zero-pad up to the sequence length. while len(input_ids) < max_seq_length: input_ids.append(0) input_mask.append(0) segment_ids.append(0) assert len(input_ids) == max_seq_length assert len(input_mask) == max_seq_length assert len(segment_ids) == max_seq_length label_id = label_map[example.label] if ex_index < 5 and verbose_logging: tf.compat.v1.logging.info("*** Example ***") tf.compat.v1.logging.info("guid: %s" % (example.guid)) tf.compat.v1.logging.info("tokens: %s" % " ".join( [tokenization.printable_text(x) for x in tokens])) tf.compat.v1.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids])) tf.compat.v1.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask])) tf.compat.v1.logging.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids])) tf.compat.v1.logging.info("label: %s (id = %d)" % (example.label, label_id)) feature = InputFeatures( input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids, label_id=label_id, is_real_example=True) return feature # This function is not used by this file but is still used by the Colab and # people who depend on it. def convert_examples_to_features(examples, label_list, max_seq_length, tokenizer): """Convert a set of `InputExample`s to a list of `InputFeatures`.""" features = [] for (ex_index, example) in enumerate(examples): if ex_index % 10000 == 0: tf.compat.v1.logging.info("Writing example %d of %d" % (ex_index, len(examples))) feature = convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer, FLAGS.verbose_logging) features.append(feature) return features def file_based_convert_examples_to_features( examples, label_list, max_seq_length, tokenizer, output_file): """Convert a set of `InputExample`s to a TFRecord file.""" writer = tf.python_io.TFRecordWriter(output_file) for (ex_index, example) in enumerate(examples): if ex_index % 10000 == 0: tf.compat.v1.logging.info("Writing example %d of %d" % (ex_index, len(examples))) feature = convert_single_example(ex_index, example, label_list, max_seq_length, tokenizer) def create_int_feature(values): f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return f features = collections.OrderedDict() features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_int_feature(feature.input_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) features["label_ids"] = create_int_feature([feature.label_id]) features["is_real_example"] = create_int_feature( [int(feature.is_real_example)]) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) writer.write(tf_example.SerializeToString()) writer.close() def main(): processors = { "cola": ColaProcessor, "mnli": MnliProcessor, "mrpc": MrpcProcessor, "xnli": XnliProcessor, } task_name = FLAGS.task_name.lower() if task_name not in processors: raise ValueError("Task not found: %s" % (task_name)) processor = processors[task_name]() label_list = processor.get_labels() tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) tf.gfile.MakeDirs(FLAGS.data_dir + "final_tfrecords_sharded") train_examples = processor.get_train_examples(FLAGS.data_dir) train_file = os.path.join(FLAGS.data_dir, "final_tfrecords_sharded/" + task_name + "train.tf_record") file_based_convert_examples_to_features( train_examples, label_list, FLAGS.max_seq_length, tokenizer, train_file) eval_examples = processor.get_dev_examples(FLAGS.data_dir) eval_file = os.path.join(FLAGS.data_dir, "final_tfrecords_sharded/" + task_name + "eval.tf_record") file_based_convert_examples_to_features( eval_examples, label_list, FLAGS.max_seq_length, tokenizer, eval_file) predict_examples = processor.get_test_examples(FLAGS.data_dir) predict_file = os.path.join(FLAGS.data_dir, "final_tfrecords_sharded/" + task_name + "predict.tf_record") file_based_convert_examples_to_features(predict_examples, label_list, FLAGS.max_seq_length, tokenizer, predict_file) if __name__ == "__main__": main()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/utils/create_glue_data.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import json import math import os import random import modeling import optimization import tokenization import six import tensorflow as tf import horovod.tensorflow as hvd import time flags = tf.flags FLAGS = None def extract_flags(): flags.DEFINE_integer( "max_seq_length", 384, "The maximum total input sequence length after WordPiece tokenization. " "Sequences longer than this will be truncated, and sequences shorter " "than this will be padded.") flags.DEFINE_integer( "doc_stride", 128, "When splitting up a long document into chunks, how much stride to " "take between chunks.") flags.DEFINE_integer( "max_query_length", 64, "The maximum number of tokens for the question. Questions longer than " "this will be truncated to this length.") flags.DEFINE_bool( "version_2_with_negative", False, "If true, the SQuAD examples contain some that do not have an answer.") flags.DEFINE_string("train_file", None, "SQuAD json for training. E.g., train-v1.1.json") flags.DEFINE_string( "predict_file", None, "SQuAD json for predictions. E.g., dev-v1.1.json or test-v1.1.json") flags.DEFINE_string( "squad_dir", None, "The output directory where the model checkpoints will be written.") flags.DEFINE_string("vocab_file", None, "The vocabulary file that the BERT model was trained on.") flags.DEFINE_bool( "do_lower_case", True, "Whether to lower case the input text. Should be True for uncased " "models and False for cased models.") flags.DEFINE_bool( "verbose_logging", False, "If true, all of the warnings related to data processing will be printed. " "A number of warnings are expected for a normal SQuAD evaluation.") flags.mark_flag_as_required("train_file") flags.mark_flag_as_required("predict_file") flags.mark_flag_as_required("squad_dir") flags.mark_flag_as_required("vocab_file") return flags.FLAGS class SquadExample(object): """A single training/test example for simple sequence classification. For examples without an answer, the start and end position are -1. """ def __init__(self, qas_id, question_text, doc_tokens, orig_answer_text=None, start_position=None, end_position=None, is_impossible=False): self.qas_id = qas_id self.question_text = question_text self.doc_tokens = doc_tokens self.orig_answer_text = orig_answer_text self.start_position = start_position self.end_position = end_position self.is_impossible = is_impossible def __str__(self): return self.__repr__() def __repr__(self): s = "" s += "qas_id: %s" % (tokenization.printable_text(self.qas_id)) s += ", question_text: %s" % ( tokenization.printable_text(self.question_text)) s += ", doc_tokens: [%s]" % (" ".join(self.doc_tokens)) if self.start_position: s += ", start_position: %d" % (self.start_position) if self.start_position: s += ", end_position: %d" % (self.end_position) if self.start_position: s += ", is_impossible: %r" % (self.is_impossible) return s class InputFeatures(object): """A single set of features of data.""" def __init__(self, unique_id, example_index, doc_span_index, tokens, token_to_orig_map, token_is_max_context, input_ids, input_mask, segment_ids, start_position=None, end_position=None, is_impossible=None): self.unique_id = unique_id self.example_index = example_index self.doc_span_index = doc_span_index self.tokens = tokens self.token_to_orig_map = token_to_orig_map self.token_is_max_context = token_is_max_context self.input_ids = input_ids self.input_mask = input_mask self.segment_ids = segment_ids self.start_position = start_position self.end_position = end_position self.is_impossible = is_impossible def read_squad_examples(input_file, is_training, version_2_with_negative=False, input_data=None): """Return list of SquadExample from input_data or input_file (SQuAD json file)""" if input_data is None: with tf.gfile.Open(input_file, "r") as reader: input_data = json.load(reader)["data"] def is_whitespace(c): if c == " " or c == "\t" or c == "\r" or c == "\n" or ord(c) == 0x202F: return True return False examples = [] for entry in input_data: for paragraph in entry["paragraphs"]: paragraph_text = paragraph["context"] doc_tokens = [] char_to_word_offset = [] prev_is_whitespace = True for c in paragraph_text: if is_whitespace(c): prev_is_whitespace = True else: if prev_is_whitespace: doc_tokens.append(c) else: doc_tokens[-1] += c prev_is_whitespace = False char_to_word_offset.append(len(doc_tokens) - 1) for qa in paragraph["qas"]: qas_id = qa["id"] question_text = qa["question"] start_position = None end_position = None orig_answer_text = None is_impossible = False if is_training: if version_2_with_negative: is_impossible = qa["is_impossible"] if (len(qa["answers"]) != 1) and (not is_impossible): raise ValueError( "For training, each question should have exactly 1 answer.") if not is_impossible: answer = qa["answers"][0] orig_answer_text = answer["text"] answer_offset = answer["answer_start"] answer_length = len(orig_answer_text) start_position = char_to_word_offset[answer_offset] end_position = char_to_word_offset[answer_offset + answer_length - 1] # Only add answers where the text can be exactly recovered from the # document. If this CAN'T happen it's likely due to weird Unicode # stuff so we will just skip the example. # # Note that this means for training mode, every example is NOT # guaranteed to be preserved. actual_text = " ".join( doc_tokens[start_position:(end_position + 1)]) cleaned_answer_text = " ".join( tokenization.whitespace_tokenize(orig_answer_text)) if actual_text.find(cleaned_answer_text) == -1: tf.logging.warning("Could not find answer: '%s' vs. '%s'", actual_text, cleaned_answer_text) continue else: start_position = -1 end_position = -1 orig_answer_text = "" example = SquadExample( qas_id=qas_id, question_text=question_text, doc_tokens=doc_tokens, orig_answer_text=orig_answer_text, start_position=start_position, end_position=end_position, is_impossible=is_impossible) examples.append(example) return examples def _check_is_max_context(doc_spans, cur_span_index, position): """Check if this is the 'max context' doc span for the token.""" # Because of the sliding window approach taken to scoring documents, a single # token can appear in multiple documents. E.g. # Doc: the man went to the store and bought a gallon of milk # Span A: the man went to the # Span B: to the store and bought # Span C: and bought a gallon of # ... # # Now the word 'bought' will have two scores from spans B and C. We only # want to consider the score with "maximum context", which we define as # the *minimum* of its left and right context (the *sum* of left and # right context will always be the same, of course). # # In the example the maximum context for 'bought' would be span C since # it has 1 left context and 3 right context, while span B has 4 left context # and 0 right context. best_score = None best_span_index = None for (span_index, doc_span) in enumerate(doc_spans): end = doc_span.start + doc_span.length - 1 if position < doc_span.start: continue if position > end: continue num_left_context = position - doc_span.start num_right_context = end - position score = min(num_left_context, num_right_context) + 0.01 * doc_span.length if best_score is None or score > best_score: best_score = score best_span_index = span_index return cur_span_index == best_span_index def _improve_answer_span(doc_tokens, input_start, input_end, tokenizer, orig_answer_text): """Returns tokenized answer spans that better match the annotated answer.""" # The SQuAD annotations are character based. We first project them to # whitespace-tokenized words. But then after WordPiece tokenization, we can # often find a "better match". For example: # # Question: What year was John Smith born? # Context: The leader was John Smith (1895-1943). # Answer: 1895 # # The original whitespace-tokenized answer will be "(1895-1943).". However # after tokenization, our tokens will be "( 1895 - 1943 ) .". So we can match # the exact answer, 1895. # # However, this is not always possible. Consider the following: # # Question: What country is the top exporter of electornics? # Context: The Japanese electronics industry is the lagest in the world. # Answer: Japan # # In this case, the annotator chose "Japan" as a character sub-span of # the word "Japanese". Since our WordPiece tokenizer does not split # "Japanese", we just use "Japanese" as the annotation. This is fairly rare # in SQuAD, but does happen. tok_answer_text = " ".join(tokenizer.tokenize(orig_answer_text)) for new_start in range(input_start, input_end + 1): for new_end in range(input_end, new_start - 1, -1): text_span = " ".join(doc_tokens[new_start:(new_end + 1)]) if text_span == tok_answer_text: return (new_start, new_end) return (input_start, input_end) def convert_examples_to_features(examples, tokenizer, max_seq_length, doc_stride, max_query_length, is_training, output_fn, verbose_logging=False): """Loads a data file into a list of `InputBatch`s.""" unique_id = 1000000000 for (example_index, example) in enumerate(examples): query_tokens = tokenizer.tokenize(example.question_text) if len(query_tokens) > max_query_length: query_tokens = query_tokens[0:max_query_length] tok_to_orig_index = [] orig_to_tok_index = [] all_doc_tokens = [] for (i, token) in enumerate(example.doc_tokens): orig_to_tok_index.append(len(all_doc_tokens)) sub_tokens = tokenizer.tokenize(token) for sub_token in sub_tokens: tok_to_orig_index.append(i) all_doc_tokens.append(sub_token) tok_start_position = None tok_end_position = None if is_training and example.is_impossible: tok_start_position = -1 tok_end_position = -1 if is_training and not example.is_impossible: tok_start_position = orig_to_tok_index[example.start_position] if example.end_position < len(example.doc_tokens) - 1: tok_end_position = orig_to_tok_index[example.end_position + 1] - 1 else: tok_end_position = len(all_doc_tokens) - 1 (tok_start_position, tok_end_position) = _improve_answer_span( all_doc_tokens, tok_start_position, tok_end_position, tokenizer, example.orig_answer_text) # The -3 accounts for [CLS], [SEP] and [SEP] max_tokens_for_doc = max_seq_length - len(query_tokens) - 3 # We can have documents that are longer than the maximum sequence length. # To deal with this we do a sliding window approach, where we take chunks # of the up to our max length with a stride of `doc_stride`. _DocSpan = collections.namedtuple( # pylint: disable=invalid-name "DocSpan", ["start", "length"]) doc_spans = [] start_offset = 0 while start_offset < len(all_doc_tokens): length = len(all_doc_tokens) - start_offset if length > max_tokens_for_doc: length = max_tokens_for_doc doc_spans.append(_DocSpan(start=start_offset, length=length)) if start_offset + length == len(all_doc_tokens): break start_offset += min(length, doc_stride) for (doc_span_index, doc_span) in enumerate(doc_spans): tokens = [] token_to_orig_map = {} token_is_max_context = {} segment_ids = [] tokens.append("[CLS]") segment_ids.append(0) for token in query_tokens: tokens.append(token) segment_ids.append(0) tokens.append("[SEP]") segment_ids.append(0) for i in range(doc_span.length): split_token_index = doc_span.start + i token_to_orig_map[len(tokens)] = tok_to_orig_index[split_token_index] is_max_context = _check_is_max_context(doc_spans, doc_span_index, split_token_index) token_is_max_context[len(tokens)] = is_max_context tokens.append(all_doc_tokens[split_token_index]) segment_ids.append(1) tokens.append("[SEP]") segment_ids.append(1) input_ids = tokenizer.convert_tokens_to_ids(tokens) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. input_mask = [1] * len(input_ids) # Zero-pad up to the sequence length. while len(input_ids) < max_seq_length: input_ids.append(0) input_mask.append(0) segment_ids.append(0) assert len(input_ids) == max_seq_length assert len(input_mask) == max_seq_length assert len(segment_ids) == max_seq_length start_position = None end_position = None if is_training and not example.is_impossible: # For training, if our document chunk does not contain an annotation # we throw it out, since there is nothing to predict. doc_start = doc_span.start doc_end = doc_span.start + doc_span.length - 1 out_of_span = False if not (tok_start_position >= doc_start and tok_end_position <= doc_end): out_of_span = True if out_of_span: start_position = 0 end_position = 0 else: doc_offset = len(query_tokens) + 2 start_position = tok_start_position - doc_start + doc_offset end_position = tok_end_position - doc_start + doc_offset if is_training and example.is_impossible: start_position = 0 end_position = 0 if verbose_logging and example_index < 20: tf.compat.v1.logging.info("*** Example ***") tf.compat.v1.logging.info("unique_id: %s" % (unique_id)) tf.compat.v1.logging.info("example_index: %s" % (example_index)) tf.compat.v1.logging.info("doc_span_index: %s" % (doc_span_index)) tf.compat.v1.logging.info("tokens: %s" % " ".join( [tokenization.printable_text(x) for x in tokens])) tf.compat.v1.logging.info("token_to_orig_map: %s" % " ".join( ["%d:%d" % (x, y) for (x, y) in six.iteritems(token_to_orig_map)])) tf.compat.v1.logging.info("token_is_max_context: %s" % " ".join([ "%d:%s" % (x, y) for (x, y) in six.iteritems(token_is_max_context) ])) tf.compat.v1.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids])) tf.compat.v1.logging.info( "input_mask: %s" % " ".join([str(x) for x in input_mask])) tf.compat.v1.logging.info( "segment_ids: %s" % " ".join([str(x) for x in segment_ids])) if is_training and example.is_impossible: tf.compat.v1.logging.info("impossible example") if is_training and not example.is_impossible: answer_text = " ".join(tokens[start_position:(end_position + 1)]) tf.compat.v1.logging.info("start_position: %d" % (start_position)) tf.compat.v1.logging.info("end_position: %d" % (end_position)) tf.compat.v1.logging.info( "answer: %s" % (tokenization.printable_text(answer_text))) feature = InputFeatures( unique_id=unique_id, example_index=example_index, doc_span_index=doc_span_index, tokens=tokens, token_to_orig_map=token_to_orig_map, token_is_max_context=token_is_max_context, input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids, start_position=start_position, end_position=end_position, is_impossible=example.is_impossible) # Run callback output_fn(feature) unique_id += 1 class FeatureWriter(object): """Writes InputFeature to TF example file.""" def __init__(self, filename, is_training): self.filename = filename self.is_training = is_training self.num_features = 0 self._writer = tf.python_io.TFRecordWriter(filename) def process_feature(self, feature): """Write a InputFeature to the TFRecordWriter as a tf.train.Example.""" self.num_features += 1 def create_int_feature(values): feature = tf.train.Feature( int64_list=tf.train.Int64List(value=list(values))) return feature features = collections.OrderedDict() features["unique_ids"] = create_int_feature([feature.unique_id]) features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_int_feature(feature.input_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) if self.is_training: features["start_positions"] = create_int_feature([feature.start_position]) features["end_positions"] = create_int_feature([feature.end_position]) impossible = 0 if feature.is_impossible: impossible = 1 features["is_impossible"] = create_int_feature([impossible]) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) self._writer.write(tf_example.SerializeToString()) def close(self): self._writer.close() def main(): FLAGS = extract_flags() tokenizer = tokenization.FullTokenizer( vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case) tf.gfile.MakeDirs(FLAGS.squad_dir + "/final_tfrecords_sharded") # We write to a temporary file to avoid storing very large constant tensors # in memory. train_examples = read_squad_examples( input_file=FLAGS.train_file, is_training=True, version_2_with_negative=FLAGS.version_2_with_negative) train_writer = FeatureWriter( filename=os.path.join(FLAGS.squad_dir, "final_tfrecords_sharded/train.tf_record"), is_training=True) convert_examples_to_features( examples=train_examples, tokenizer=tokenizer, max_seq_length=FLAGS.max_seq_length, doc_stride=FLAGS.doc_stride, max_query_length=FLAGS.max_query_length, is_training=True, output_fn=train_writer.process_feature, verbose_logging=FLAGS.verbose_logging) train_writer.close() eval_examples = read_squad_examples( input_file=FLAGS.predict_file, is_training=False, version_2_with_negative=FLAGS.version_2_with_negative) eval_writer = FeatureWriter( filename=os.path.join(FLAGS.squad_dir, "final_tfrecords_sharded/eval.tf_record"), is_training=False) eval_features = [] def append_feature(feature): eval_features.append(feature) eval_writer.process_feature(feature) convert_examples_to_features( examples=eval_examples, tokenizer=tokenizer, max_seq_length=FLAGS.max_seq_length, doc_stride=FLAGS.doc_stride, max_query_length=FLAGS.max_query_length, is_training=False, output_fn=append_feature, verbose_logging=FLAGS.verbose_logging) eval_writer.close() if __name__ == "__main__": main()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/utils/create_squad_data.py
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/biobert/__init__.py
import os import numpy as np import pandas as pd import sklearn.metrics import argparse parser = argparse.ArgumentParser(description='') parser.add_argument('--output_path', type=str, help='') parser.add_argument('--answer_path', type=str, help='') parser.add_argument('--task', type=str, default="binary", help='default:binary, possible other options:{chemprot}') args = parser.parse_args() testdf = pd.read_csv(args.answer_path, sep="\t", header=None) preddf = pd.read_csv(args.output_path, sep="\t", header=None) # binary if args.task == "binary": pred = [preddf.iloc[i].tolist() for i in preddf.index] pred_class = [np.argmax(v) for v in pred] pred_prob_one = [v[1] for v in pred] p,r,f,s = sklearn.metrics.precision_recall_fscore_support(y_pred=pred_class, y_true=testdf["label"]) results = dict() results["f1 score"] = f[1] results["recall"] = r[1] results["precision"] = p[1] results["specificity"] = r[0] # chemprot # micro-average of 5 target classes # see "Potent pairing: ensemble of long short-term memory networks and support vector machine for chemical-protein relation extraction (Mehryary, 2018)" for details if args.task == "chemprot": pred = [preddf.iloc[i].tolist() for i in preddf.index] pred_class = [np.argmax(v) for v in pred] str_to_int_mapper = dict() testdf.iloc[:,3] = testdf.iloc[:, 3].fillna("False") for i,v in enumerate(sorted(testdf.iloc[:,3].unique())): str_to_int_mapper[v] = i test_answer = [str_to_int_mapper[v] for v in testdf.iloc[:,3]] p,r,f,s = sklearn.metrics.precision_recall_fscore_support(y_pred=pred_class, y_true=test_answer, labels=[0,1,2,3,4], average="micro") results = dict() results["f1 score"] = f results["recall"] = r results["precision"] = p for k,v in results.items(): print("{:11s} : {:.2%}".format(k,v))
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/biobert/re_eval.py
# Python version of the evaluation script from CoNLL'00- # Originates from: https://github.com/spyysalo/conlleval.py # Intentional differences: # - accept any space as delimiter by default # - optional file argument (default STDIN) # - option to set boundary (-b argument) # - LaTeX output (-l argument) not supported # - raw tags (-r argument) not supported # add function :evaluate(predicted_label, ori_label): which will not read from file import sys import re import codecs from collections import defaultdict, namedtuple ANY_SPACE = '<SPACE>' class FormatError(Exception): pass Metrics = namedtuple('Metrics', 'tp fp fn prec rec fscore') class EvalCounts(object): def __init__(self): self.correct_chunk = 0 # number of correctly identified chunks self.correct_tags = 0 # number of correct chunk tags self.found_correct = 0 # number of chunks in corpus self.found_guessed = 0 # number of identified chunks self.token_counter = 0 # token counter (ignores sentence breaks) # counts by type self.t_correct_chunk = defaultdict(int) self.t_found_correct = defaultdict(int) self.t_found_guessed = defaultdict(int) def parse_args(argv): import argparse parser = argparse.ArgumentParser( description='evaluate tagging results using CoNLL criteria', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) arg = parser.add_argument arg('-b', '--boundary', metavar='STR', default='-X-', help='sentence boundary') arg('-d', '--delimiter', metavar='CHAR', default=ANY_SPACE, help='character delimiting items in input') arg('-o', '--otag', metavar='CHAR', default='O', help='alternative outside tag') arg('file', nargs='?', default=None) return parser.parse_args(argv) def parse_tag(t): m = re.match(r'^([^-]*)-(.*)$', t) return m.groups() if m else (t, '') def evaluate(iterable, options=None): if options is None: options = parse_args([]) # use defaults counts = EvalCounts() num_features = None # number of features per line in_correct = False # currently processed chunks is correct until now last_correct = 'O' # previous chunk tag in corpus last_correct_type = '' # type of previously identified chunk tag last_guessed = 'O' # previously identified chunk tag last_guessed_type = '' # type of previous chunk tag in corpus for i, line in enumerate(iterable): line = line.rstrip('\r\n') # print(line) if options.delimiter == ANY_SPACE: features = line.split() else: features = line.split(options.delimiter) if num_features is None: num_features = len(features) elif num_features != len(features) and len(features) != 0: raise FormatError('unexpected number of features: %d (%d) at line %d\n%s' % (len(features), num_features, i, line)) if len(features) == 0 or features[0] == options.boundary: features = [options.boundary, 'O', 'O'] if len(features) < 3: raise FormatError('unexpected number of features in line %s' % line) guessed, guessed_type = parse_tag(features.pop()) correct, correct_type = parse_tag(features.pop()) first_item = features.pop(0) if first_item == options.boundary: guessed = 'O' end_correct = end_of_chunk(last_correct, correct, last_correct_type, correct_type) end_guessed = end_of_chunk(last_guessed, guessed, last_guessed_type, guessed_type) start_correct = start_of_chunk(last_correct, correct, last_correct_type, correct_type) start_guessed = start_of_chunk(last_guessed, guessed, last_guessed_type, guessed_type) if in_correct: if (end_correct and end_guessed and last_guessed_type == last_correct_type): in_correct = False counts.correct_chunk += 1 counts.t_correct_chunk[last_correct_type] += 1 elif (end_correct != end_guessed or guessed_type != correct_type): in_correct = False if start_correct and start_guessed and guessed_type == correct_type: in_correct = True if start_correct: counts.found_correct += 1 counts.t_found_correct[correct_type] += 1 if start_guessed: counts.found_guessed += 1 counts.t_found_guessed[guessed_type] += 1 if first_item != options.boundary: if correct == guessed and guessed_type == correct_type: counts.correct_tags += 1 counts.token_counter += 1 last_guessed = guessed last_correct = correct last_guessed_type = guessed_type last_correct_type = correct_type if in_correct: counts.correct_chunk += 1 counts.t_correct_chunk[last_correct_type] += 1 return counts def uniq(iterable): seen = set() return [i for i in iterable if not (i in seen or seen.add(i))] def calculate_metrics(correct, guessed, total): tp, fp, fn = correct, guessed-correct, total-correct p = 0 if tp + fp == 0 else 1.*tp / (tp + fp) r = 0 if tp + fn == 0 else 1.*tp / (tp + fn) f = 0 if p + r == 0 else 2 * p * r / (p + r) return Metrics(tp, fp, fn, p, r, f) def metrics(counts): c = counts overall = calculate_metrics( c.correct_chunk, c.found_guessed, c.found_correct ) by_type = {} for t in uniq(list(c.t_found_correct) + list(c.t_found_guessed)): by_type[t] = calculate_metrics( c.t_correct_chunk[t], c.t_found_guessed[t], c.t_found_correct[t] ) return overall, by_type def report(counts, out=None): if out is None: out = sys.stdout overall, by_type = metrics(counts) c = counts out.write('processed %d tokens with %d phrases; ' % (c.token_counter, c.found_correct)) out.write('found: %d phrases; correct: %d.\n' % (c.found_guessed, c.correct_chunk)) if c.token_counter > 0: out.write('accuracy: %6.2f%%; ' % (100.*c.correct_tags/c.token_counter)) out.write('precision: %6.2f%%; ' % (100.*overall.prec)) out.write('recall: %6.2f%%; ' % (100.*overall.rec)) out.write('FB1: %6.2f\n' % (100.*overall.fscore)) for i, m in sorted(by_type.items()): out.write('%17s: ' % i) out.write('precision: %6.2f%%; ' % (100.*m.prec)) out.write('recall: %6.2f%%; ' % (100.*m.rec)) out.write('FB1: %6.2f %d\n' % (100.*m.fscore, c.t_found_guessed[i])) def report_notprint(counts): overall, by_type = metrics(counts) c = counts final_report = [] line = [] line.append('processed %d tokens with %d phrases; ' % (c.token_counter, c.found_correct)) line.append('found: %d phrases; correct: %d.\n' % (c.found_guessed, c.correct_chunk)) final_report.append("".join(line)) if c.token_counter > 0: line = [] line.append('accuracy: %6.2f%%; ' % (100.*c.correct_tags/c.token_counter)) line.append('precision: %6.2f%%; ' % (100.*overall.prec)) line.append('recall: %6.2f%%; ' % (100.*overall.rec)) line.append('FB1: %6.2f\n' % (100.*overall.fscore)) final_report.append("".join(line)) for i, m in sorted(by_type.items()): line = [] line.append('%17s: ' % i) line.append('precision: %6.2f%%; ' % (100.*m.prec)) line.append('recall: %6.2f%%; ' % (100.*m.rec)) line.append('FB1: %6.2f %d\n' % (100.*m.fscore, c.t_found_guessed[i])) final_report.append("".join(line)) return final_report def end_of_chunk(prev_tag, tag, prev_type, type_): # check if a chunk ended between the previous and current word # arguments: previous and current chunk tags, previous and current types chunk_end = False if prev_tag == 'E': chunk_end = True if prev_tag == 'S': chunk_end = True if prev_tag == 'B' and tag == 'B': chunk_end = True if prev_tag == 'B' and tag == 'S': chunk_end = True if prev_tag == 'B' and tag == 'O': chunk_end = True if prev_tag == 'I' and tag == 'B': chunk_end = True if prev_tag == 'I' and tag == 'S': chunk_end = True if prev_tag == 'I' and tag == 'O': chunk_end = True if prev_tag != 'O' and prev_tag != '.' and prev_type != type_: chunk_end = True # these chunks are assumed to have length 1 if prev_tag == ']': chunk_end = True if prev_tag == '[': chunk_end = True return chunk_end def start_of_chunk(prev_tag, tag, prev_type, type_): # check if a chunk started between the previous and current word # arguments: previous and current chunk tags, previous and current types chunk_start = False if tag == 'B': chunk_start = True if tag == 'S': chunk_start = True if prev_tag == 'E' and tag == 'E': chunk_start = True if prev_tag == 'E' and tag == 'I': chunk_start = True if prev_tag == 'S' and tag == 'E': chunk_start = True if prev_tag == 'S' and tag == 'I': chunk_start = True if prev_tag == 'O' and tag == 'E': chunk_start = True if prev_tag == 'O' and tag == 'I': chunk_start = True if tag != 'O' and tag != '.' and prev_type != type_: chunk_start = True # these chunks are assumed to have length 1 if tag == '[': chunk_start = True if tag == ']': chunk_start = True return chunk_start def main(argv): args = parse_args(argv[1:]) if args.file is None: counts = evaluate(sys.stdin, args) else: with open(args.file) as f: counts = evaluate(f, args) report(counts) def return_report(input_file): with open(input_file, "r") as f: counts = evaluate(f) return report_notprint(counts) if __name__ == '__main__': # sys.exit(main(sys.argv)) return_report('/home/pengy6/data/sentence_similarity/data/cdr/test1/wanli_result2/label_test.txt')
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/biobert/conlleval.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import bz2 import glob import gzip import os import urllib.request import shutil import sys class PubMedDownloader: def __init__(self, subset, save_path): self.subset = subset # Modifying self.save_path in two steps to handle creation of subdirectories self.save_path = save_path + '/pubmed' + '/' if not os.path.exists(self.save_path): os.makedirs(self.save_path) self.save_path = self.save_path + '/' + subset if not os.path.exists(self.save_path): os.makedirs(self.save_path) self.download_urls = { 'baseline' : 'ftp://ftp.ncbi.nlm.nih.gov/pubmed/baseline/', 'daily_update' : 'ftp://ftp.ncbi.nlm.nih.gov/pubmed/updatefiles/', 'fulltext' : 'ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/', 'open_access' : 'ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/' } def download(self): print('subset:', self.subset) url = self.download_urls[self.subset] self.download_files(url) self.extract_files() def download_files(self, url): url = self.download_urls[self.subset] output = os.popen('curl ' + url).read() if self.subset == 'fulltext' or self.subset == 'open_access': line_split = 'comm_use' if self.subset == 'fulltext' else 'non_comm_use' for line in output.splitlines(): if line[-10:] == 'xml.tar.gz' and \ line.split(' ')[-1].split('.')[0] == line_split: file = os.path.join(self.save_path, line.split(' ')[-1]) if not os.path.isfile(file): print('Downloading', file) response = urllib.request.urlopen(url + line.split(' ')[-1]) with open(file, "wb") as handle: handle.write(response.read()) elif self.subset == 'baseline' or self.subset == 'daily_update': for line in output.splitlines(): if line[-3:] == '.gz': file = os.path.join(self.save_path, line.split(' ')[-1]) if not os.path.isfile(file): print('Downloading', file) response = urllib.request.urlopen(url + line.split(' ')[-1]) with open(file, "wb") as handle: handle.write(response.read()) else: assert False, 'Invalid PubMed dataset/subset specified.' def extract_files(self): files = glob.glob(self.save_path + '/*.xml.gz') for file in files: print('file:', file) input = gzip.GzipFile(file, mode='rb') s = input.read() input.close() out = open(file[:-3], mode='wb') out.write(s) out.close()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/PubMedDownloader.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import bz2 import os import urllib.request import sys class SquadDownloader: def __init__(self, save_path): self.save_path = save_path + '/squad' if not os.path.exists(self.save_path): os.makedirs(self.save_path) if not os.path.exists(self.save_path + '/v1.1'): os.makedirs(self.save_path + '/v1.1') if not os.path.exists(self.save_path + '/v2.0'): os.makedirs(self.save_path + '/v2.0') self.download_urls = { 'https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v1.1.json' : 'v1.1/train-v1.1.json', 'https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json' : 'v1.1/dev-v1.1.json', 'https://worksheets.codalab.org/rest/bundles/0xbcd57bee090b421c982906709c8c27e1/contents/blob/' : 'v1.1/evaluate-v1.1.py', 'https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v2.0.json' : 'v2.0/train-v2.0.json', 'https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v2.0.json' : 'v2.0/dev-v2.0.json', 'https://worksheets.codalab.org/rest/bundles/0x6b567e1cf2e041ec80d7098f031c5c9e/contents/blob/' : 'v2.0/evaluate-v2.0.py', } def download(self): for item in self.download_urls: url = item file = self.download_urls[item] print('Downloading:', url) if os.path.isfile(self.save_path + '/' + file): print('** Download file already exists, skipping download') else: response = urllib.request.urlopen(url) with open(self.save_path + '/' + file, "wb") as handle: handle.write(response.read())
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/SquadDownloader.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import csv o = csv.reader(open("data/biobert/chemprot-data_treeLSTM/dev.tsv", "r"), delimiter="\t") nv = csv.reader(open("data/biobert/ChemProt_NV/dev.tsv", "r"), delimiter="\t") count = {} for l, i in enumerate(nv): if l == 0: continue if count.get(i[0].split(".")[0], None) is None: count[i[0].split(".")[0]] = 0 count[i[0].split(".")[0]] += 1 count_1 = {} for i in o: if count_1.get(i[0], None) is None: count_1[i[0]] = 0 count_1[i[0]] += 1 for k in count.keys(): if count[k] != count_1[k]: print(k, count[k], count_1[k]) # import os # import csv # import zipfile # import argparse # class ChemProtTextFormatting: # """A basic formatter to preprocess the chemprot dataset. # """ # def __init__(self, input_folder, output_folder): # chemprot_folder = input_folder # with zipfile.ZipFile(os.path.join(chemprot_folder, "ChemProt_Corpus.zip"), "r") as zip: # zip.extractall(chemprot_folder) # chemprot_folder = os.path.join(input_folder, "ChemProt_Corpus") # with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_development.zip")) as zip: # zip.extractall(chemprot_folder) # if not os.path.exists(output_folder): # os.makedirs(output_folder) # self.format(os.path.join(chemprot_folder, "chemprot_development"), # "chemprot_development_entities.tsv", "chemprot_development_relations.tsv", # "chemprot_development_abstracts.tsv", os.path.join(output_folder, "dev.tsv")) # with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_test_gs.zip")) as zip: # zip.extractall(chemprot_folder) # self.format(os.path.join(chemprot_folder, "chemprot_test_gs"), # "chemprot_test_entities_gs.tsv", "chemprot_test_relations_gs.tsv", # "chemprot_test_abstracts_gs.tsv", os.path.join(output_folder, "test.tsv")) # with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_training.zip")) as zip: # zip.extractall(chemprot_folder) # self.format(os.path.join(chemprot_folder, "chemprot_training"), # "chemprot_training_entities.tsv", "chemprot_training_relations.tsv", # "chemprot_training_abstracts.tsv", os.path.join(output_folder, "train.tsv")) # def format(self, chemprot_path, entity_filename, relations_filename, abstracts_filename, output_filename): # """ # Constructs ChemProt dataset for Relation Extraction. # Args: # chemprot_path: Path to files # entity_filename: Contains labelled mention annotations of chemical compounds and genes/proteins. # <PMID> <EntityNumber> <Type of Entity> <Start Character offset> <End Character Offset> <Text String> # relations_filename: Contains a subset of chemical-protein relations annotations for the Chemprot dataset # <PMID> <CPR Group> <EntityNumber1> <EntityNumber2> # abstracts_filename: Contains plain text CHEMPROT PubMed Data # <PMID> <Title of the Article> <Abstract of the Article> # output_filename: Path to output file that will contain preprocessed data # <PMID.EntityNumber1.EntityNumber2> <Preprocessed Sentence> <CPR Group> # """ # data = {} # train_entities = csv.reader(open(os.path.join(chemprot_path, entity_filename), # mode="r"), delimiter="\t") # for entity in train_entities: # id = entity[0] # if data.get(id, None) is None: # data[id] = {"relations":[], "entities":{}} # data[id]["entities"][entity[1]] = (int(entity[3]), int(entity[4]), entity[2]) # train_relations=csv.reader(open(os.path.join(chemprot_path, relations_filename), # mode="r"), delimiter="\t") # for relation in train_relations: # try: # id = relation[0] # data[id]["relations"].append((relation[1], relation[2], relation[4].split("Arg1:")[-1], relation[5].split("Arg2:")[-1])) # except: # print("invalid id") # raise ValueError # with open(output_filename, 'w') as ofile: # train_abstracts = csv.reader(open(os.path.join(chemprot_path, abstracts_filename), # mode="r"), delimiter="\t") # owriter = csv.writer(ofile, delimiter='\t', lineterminator=os.linesep) # owriter.writerow(["index", "sentence", "label"]) # num_sentences = 0 # rejected = 0 # for abstract in train_abstracts: # id = abstract[0] # line = abstract[1] + abstract[2] # for relation in data[id]["relations"]: # tag1 = relation[2] # tag2 = relation[3] # start = 0 # for sentence in line.split("."): # end = start + len(sentence) # if data[id]["entities"][tag1][0] >= start and data[id]["entities"][tag2][0] >= start and \ # data[id]["entities"][tag1][1] <= end and data[id]["entities"][tag2][1] <= end: # for offset_start, offset_end, word in sorted([(data[id]["entities"][tag1][0], data[id]["entities"][tag1][1], data[id]["entities"][tag1][2]), # (data[id]["entities"][tag2][0], data[id]["entities"][tag2][1], data[id]["entities"][tag2][2])], # reverse=True): # sentence = sentence[:offset_start-start-1] + "@" + word + "$" + sentence[offset_end-start-1:] # sentence = sentence.strip() # owriter.writerow([id+"."+tag1+"."+tag2, sentence, relation[0] if relation[1] == "Y " else "false"]) # num_sentences += 1 # if id == "10064839": # print(tag1, tag2, start, end, offset_start, offset_end, "yes") # break # else: # rejected += 1 # if id == "10064839": # print(tag1, tag2, start, end, data[id]["entities"][tag1][0], data[id]["entities"][tag1][1], data[id]["entities"][tag2][0], data[id]["entities"][tag2][1]) # start = end + 1 # print("Succesfully written {} samples to {}".format(num_sentences, output_filename)) # print("Rejected are", rejected) # if __name__=="__main__": # parser = argparse.ArgumentParser( # description='Preprocessing Application for ChemProt' # ) # parser.add_argument( # '--input_folder', # type=str, # help='Specify the input files in a comma-separated list (no spaces)' # ) # parser.add_argument( # '--output_folder', # type=str, # help='Specify the input files in a comma-separated list (no spaces)' # ) # args = parser.parse_args() # preprocess_chemprot = ChemProtTextFormatting(args.input_folder, args.output_folder) # # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # # you may not use this file except in compliance with the License. # # You may obtain a copy of the License at # # # # http://www.apache.org/licenses/LICENSE-2.0 # # # # Unless required by applicable law or agreed to in writing, software # # distributed under the License is distributed on an "AS IS" BASIS, # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # # See the License for the specific language governing permissions and # # limitations under the License. # import os # import csv # import zipfile # import argparse # class ChemProtTextFormatting: # """A basic formatter to preprocess the chemprot dataset. # """ # def __init__(self, input_folder, output_folder): # chemprot_folder = input_folder # with zipfile.ZipFile(os.path.join(chemprot_folder, "ChemProt_Corpus.zip"), "r") as zip: # zip.extractall(chemprot_folder) # chemprot_folder = os.path.join(input_folder, "ChemProt_Corpus") # with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_development.zip")) as zip: # zip.extractall(chemprot_folder) # if not os.path.exists(output_folder): # os.makedirs(output_folder) # self.format(os.path.join(chemprot_folder, "chemprot_development"), # "chemprot_development_entities.tsv", "chemprot_development_relations.tsv", # "chemprot_development_abstracts.tsv", os.path.join(output_folder, "dev.tsv")) # with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_test_gs.zip")) as zip: # zip.extractall(chemprot_folder) # self.format(os.path.join(chemprot_folder, "chemprot_test_gs"), # "chemprot_test_entities_gs.tsv", "chemprot_test_relations_gs.tsv", # "chemprot_test_abstracts_gs.tsv", os.path.join(output_folder, "test.tsv")) # with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_training.zip")) as zip: # zip.extractall(chemprot_folder) # self.format(os.path.join(chemprot_folder, "chemprot_training"), # "chemprot_training_entities.tsv", "chemprot_training_relations.tsv", # "chemprot_training_abstracts.tsv", os.path.join(output_folder, "train.tsv")) # def format(self, chemprot_path, entity_filename, relations_filename, abstracts_filename, output_filename): # """ # Constructs ChemProt dataset for Relation Extraction. # Args: # chemprot_path: Path to files # entity_filename: Contains labelled mention annotations of chemical compounds and genes/proteins. # <PMID> <EntityNumber> <Type of Entity> <Start Character offset> <End Character Offset> <Text String> # relations_filename: Contains a subset of chemical-protein relations annotations for the Chemprot dataset # <PMID> <CPR Group> <EntityNumber1> <EntityNumber2> # abstracts_filename: Contains plain text CHEMPROT PubMed Data # <PMID> <Title of the Article> <Abstract of the Article> # output_filename: Path to output file that will contain preprocessed data # <PMID.EntityNumber1.EntityNumber2> <Preprocessed Sentence> <CPR Group> # """ # data = {} # train_entities = csv.reader(open(os.path.join(chemprot_path, entity_filename), # mode="r"), delimiter="\t") # for entity in train_entities: # id = entity[0] # if data.get(id, None) is None: # data[id] = {"relations": {}, "entities": {"CHEMICAL": {"00": (0, 0, None)}, "GENE": {}}} # data[id]["entities"]["CHEMICAL" if entity[2] == "CHEMICAL" else "GENE"][entity[1]] = ( # int(entity[3]), int(entity[4]), entity[2]) # train_relations = csv.reader(open(os.path.join(chemprot_path, relations_filename), # mode="r"), delimiter="\t") # for relation in train_relations: # try: # id = relation[0] # data[id]["relations"][(relation[4].split("Arg1:")[-1], relation[5].split("Arg2:")[-1])] = relation[ # 1] if relation[2] == "Y " else "false" # except: # print("invalid id") # raise ValueError # # print(data[list(data.keys())[0]]) # with open(output_filename, 'w') as ofile: # train_abstracts = csv.reader(open(os.path.join(chemprot_path, abstracts_filename), # mode="r"), delimiter="\t") # owriter = csv.writer(ofile, delimiter='\t', lineterminator=os.linesep) # owriter.writerow(["index", "sentence", "label"]) # num_sentences = 0 # rejected = 0 # for abstract in train_abstracts: # id = abstract[0] # line = abstract[1] + abstract[2] # for tag1 in data[id]["entities"]["CHEMICAL"].keys(): # for tag2 in data[id]["entities"]["GENE"].keys(): # relation = data[id]["relations"].get((tag2, tag1), None) # relation = data[id]["relations"].get((tag1, tag2), None) if relation is None else relation # if relation is None: # relation = "false" # start = 0 # for sentence in line.split("."): # original_sentence = sentence # end = start + len(sentence) # tag1_details = data[id]["entities"]["CHEMICAL"][tag1] # tag2_details = data[id]["entities"]["GENE"][tag2] # if ((tag1_details[2] is None) or ( # tag1_details[0] >= start and tag1_details[1] <= end)) and \ # (tag2_details[0] >= start and tag2_details[1] <= end): # for offset_start, offset_end, value in sorted( # list(data[id]["entities"]["CHEMICAL"].values()) + list( # data[id]["entities"]["GENE"].values()), # reverse=True): # if offset_start < start or offset_end > end or value is None: # continue # word = value if (offset_start, offset_end) == ( # tag1_details[0], tag1_details[1]) or (offset_start, offset_end) == ( # tag2_details[0], tag2_details[1]) else "OTHER" # sentence = sentence[:offset_start - start - 1] + "@" + word + "$" + sentence[ # offset_end - start - 1:] # sentence = sentence.strip() # owriter.writerow([id + "." + tag1 + "." + tag2, sentence, relation]) # num_sentences += 1 # # if id == list(data.keys())[0]: # # print(original_sentence, sentence) # # break # else: # rejected += 1 # if id == "10064839": # # print(tag1, tag2, start, end, tag1_details[0], tag1_details[1], tag2_details[0], tag2_details[1]) # pass # start = end + 1 # print("Succesfully written {} samples to {}".format(num_sentences, output_filename)) # print("Rejected are", rejected) # if __name__ == "__main__": # parser = argparse.ArgumentParser( # description='Preprocessing Application for ChemProt' # ) # parser.add_argument( # '--input_folder', # type=str, # help='Specify the input files in a comma-separated list (no spaces)' # ) # parser.add_argument( # '--output_folder', # type=str, # help='Specify the input files in a comma-separated list (no spaces)' # ) # args = parser.parse_args() # preprocess_chemprot = ChemProtTextFormatting(args.input_folder, args.output_folder)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/check.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from GooglePretrainedWeightDownloader import GooglePretrainedWeightDownloader from NVIDIAPretrainedWeightDownloader import NVIDIAPretrainedWeightDownloader from WikiDownloader import WikiDownloader from BooksDownloader import BooksDownloader from GLUEDownloader import GLUEDownloader from SquadDownloader import SquadDownloader from PubMedDownloader import PubMedDownloader class Downloader: def __init__(self, dataset_name, save_path): self.dataset_name = dataset_name self.save_path = save_path def download(self): if self.dataset_name == 'bookscorpus': self.download_bookscorpus() elif self.dataset_name == 'wikicorpus_en': self.download_wikicorpus('en') elif self.dataset_name == 'wikicorpus_zh': self.download_wikicorpus('zh') elif self.dataset_name == 'pubmed_baseline': self.download_pubmed('baseline') elif self.dataset_name == 'pubmed_daily_update': self.download_pubmed('daily_update') elif self.dataset_name == 'pubmed_fulltext': self.download_pubmed('fulltext') elif self.dataset_name == 'pubmed_open_access': self.download_pubmed('open_access') elif self.dataset_name == 'google_pretrained_weights': self.download_google_pretrained_weights() elif self.dataset_name == 'nvidia_pretrained_weights': self.download_nvidia_pretrained_weights() elif self.dataset_name == 'mrpc': self.download_glue(self.dataset_name) elif self.dataset_name == 'mnli': self.download_glue(self.dataset_name) elif self.dataset_name == 'cola': self.download_glue(self.dataset_name) elif self.dataset_name == 'sst-2': self.download_glue(self.dataset_name) elif self.dataset_name == 'squad': self.download_squad() elif self.dataset_name == 'all': self.download_bookscorpus() self.download_wikicorpus('en') self.download_wikicorpus('zh') self.download_pubmed('baseline') self.download_pubmed('daily_update') self.download_pubmed('fulltext') self.download_pubmed('open_access') self.download_google_pretrained_weights() self.download_nvidia_pretrained_weights() self.download_glue("cola") self.download_glue("mnli") self.download_glue("mrpc") self.download_glue("sst-2") self.download_squad() else: print(self.dataset_name) assert False, 'Unknown dataset_name provided to downloader' def download_bookscorpus(self): downloader = BooksDownloader(self.save_path) downloader.download() def download_wikicorpus(self, language): downloader = WikiDownloader(language, self.save_path) downloader.download() def download_pubmed(self, subset): downloader = PubMedDownloader(subset, self.save_path) downloader.download() def download_google_pretrained_weights(self): downloader = GooglePretrainedWeightDownloader(self.save_path) downloader.download() def download_nvidia_pretrained_weights(self): downloader = NVIDIAPretrainedWeightDownloader(self.save_path) downloader.download() def download_glue(self, glue_task_name): downloader = GLUEDownloader(self.save_path) downloader.download(glue_task_name) def download_squad(self): downloader = SquadDownloader(self.save_path) downloader.download()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/Downloader.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import glob import os class BookscorpusTextFormatting: def __init__(self, books_path, output_filename, recursive = False): self.books_path = books_path self.recursive = recursive self.output_filename = output_filename # This puts one book per line def merge(self): with open(self.output_filename, mode='w', newline='\n') as ofile: for filename in glob.glob(self.books_path + '/' + '*.txt', recursive=True): with open(filename, mode='r', encoding='utf-8-sig', newline='\n') as file: for line in file: if line.strip() != '': ofile.write(line.strip() + ' ') ofile.write("\n\n")
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/BookscorpusTextFormatting.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os class NVIDIAPretrainedWeightDownloader: def __init__(self, save_path): self.save_path = save_path + '/nvidia_pretrained_weights' if not os.path.exists(self.save_path): os.makedirs(self.save_path) pass def download(self): assert False, 'NVIDIAPretrainedWeightDownloader not implemented yet.'
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/NVIDIAPretrainedWeightDownloader.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/__init__.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import bz2 import os import urllib.request import sys import subprocess class WikiDownloader: def __init__(self, language, save_path): self.save_path = save_path + '/wikicorpus_' + language if not os.path.exists(self.save_path): os.makedirs(self.save_path) self.language = language self.download_urls = { 'en' : 'https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2', 'zh' : 'https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2' } self.output_files = { 'en' : 'wikicorpus_en.xml.bz2', 'zh' : 'wikicorpus_zh.xml.bz2' } def download(self): if self.language in self.download_urls: url = self.download_urls[self.language] filename = self.output_files[self.language] print('Downloading:', url) if os.path.isfile(self.save_path + '/' + filename): print('** Download file already exists, skipping download') else: cmd = ['wget', url, '--output-document={}'.format(self.save_path + '/' + filename)] print('Running:', cmd) status = subprocess.run(cmd) if status.returncode != 0: raise RuntimeError('Wiki download not successful') # Always unzipping since this is relatively fast and will overwrite print('Unzipping:', self.output_files[self.language]) subprocess.run('bzip2 -dk ' + self.save_path + '/' + filename, shell=True, check=True) else: assert False, 'WikiDownloader not implemented for this language yet.'
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/WikiDownloader.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import glob import os class WikicorpusTextFormatting: def __init__(self, wiki_path, output_filename, recursive = False): self.wiki_path = wiki_path self.recursive = recursive self.output_filename = output_filename # This puts one article per line def merge(self): with open(self.output_filename, mode='w', newline='\n') as ofile: for dirname in glob.glob(self.wiki_path + '/*/', recursive=False): for filename in glob.glob(dirname + 'wiki_*', recursive=self.recursive): print(filename) article_lines = [] article_open = False with open(filename, mode='r', newline='\n') as file: for line in file: if '<doc id=' in line: article_open = True elif '</doc>' in line: article_open = False for oline in article_lines[1:]: if oline != '\n': ofile.write(oline.rstrip() + " ") ofile.write("\n\n") article_lines = [] else: if article_open: article_lines.append(line)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/WikicorpusTextFormatting.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import csv import zipfile import argparse import re class ChemProtTextFormatting: """A basic formatter to preprocess the chemprot dataset. """ def __init__(self, input_folder, output_folder): chemprot_folder = input_folder with zipfile.ZipFile(os.path.join(chemprot_folder, "ChemProt_Corpus.zip"), "r") as zip: zip.extractall(chemprot_folder) chemprot_folder = os.path.join(input_folder, "ChemProt_Corpus") with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_development.zip")) as zip: zip.extractall(chemprot_folder) if not os.path.exists(output_folder): os.makedirs(output_folder) self.format(os.path.join(chemprot_folder, "chemprot_development"), "chemprot_development_entities.tsv", "chemprot_development_relations.tsv", "chemprot_development_abstracts.tsv", os.path.join(output_folder, "dev.tsv")) with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_test_gs.zip")) as zip: zip.extractall(chemprot_folder) self.format(os.path.join(chemprot_folder, "chemprot_test_gs"), "chemprot_test_entities_gs.tsv", "chemprot_test_relations_gs.tsv", "chemprot_test_abstracts_gs.tsv", os.path.join(output_folder, "test.tsv")) with zipfile.ZipFile(os.path.join(chemprot_folder, "chemprot_training.zip")) as zip: zip.extractall(chemprot_folder) self.format(os.path.join(chemprot_folder, "chemprot_training"), "chemprot_training_entities.tsv", "chemprot_training_relations.tsv", "chemprot_training_abstracts.tsv", os.path.join(output_folder, "train.tsv")) def format(self, chemprot_path, entity_filename, relations_filename, abstracts_filename, output_filename): """ Constructs ChemProt dataset for Relation Extraction. Args: chemprot_path: Path to files entity_filename: Contains labelled mention annotations of chemical compounds and genes/proteins. <PMID> <EntityNumber> <Type of Entity> <Start Character offset> <End Character Offset> <Text String> relations_filename: Contains a subset of chemical-protein relations annotations for the Chemprot dataset <PMID> <CPR Group> <EntityNumber1> <EntityNumber2> abstracts_filename: Contains plain text CHEMPROT PubMed Data <PMID> <Title of the Article> <Abstract of the Article> output_filename: Path to output file that will contain preprocessed data <PMID.EntityNumber1.EntityNumber2> <Preprocessed Sentence> <CPR Group> """ data = {} train_entities = csv.reader(open(os.path.join(chemprot_path, entity_filename), mode="r"), delimiter="\t") for entity in train_entities: id = entity[0] if data.get(id, None) is None: data[id] = {"relations":{}, "entities":{"CHEMICAL":{}, "GENE":{}}} data[id]["entities"]["CHEMICAL" if entity[2] == "CHEMICAL" else "GENE"][entity[1]] = (int(entity[3]), int(entity[4]), entity[2]) train_relations=csv.reader(open(os.path.join(chemprot_path, relations_filename), mode="r"), delimiter="\t") for relation in train_relations: try: id = relation[0] data[id]["relations"][(relation[4].split("Arg1:")[-1], relation[5].split("Arg2:")[-1])] = relation[1] if relation[2] == "Y " else "false" except: print("invalid id") raise ValueError # print(data[list(data.keys())[0]]) with open(output_filename, 'w') as ofile: train_abstracts = csv.reader(open(os.path.join(chemprot_path, abstracts_filename), mode="r"), delimiter="\t") owriter = csv.writer(ofile, delimiter='\t', lineterminator=os.linesep) owriter.writerow(["index", "sentence", "label"]) num_sentences = 0 rejected = 0 for abstract in train_abstracts: id = abstract[0] line = abstract[1] + "\n" + abstract[2] for tag1 in data[id]["entities"]["CHEMICAL"].keys(): for tag2 in data[id]["entities"]["GENE"].keys(): tag1_details = data[id]["entities"]["CHEMICAL"][tag1] tag2_details = data[id]["entities"]["GENE"][tag2] if ((tag1_details[0] <= tag2_details[0] and tag2_details[0] <= tag1_details[1]) # x1 <= y1 <= x2 or (tag1_details[0] <= tag2_details[1] and tag2_details[0] <= tag1_details[1])): # x1 <= y2 <= x2 continue relation = data[id]["relations"].get((tag2, tag1), None) relation = data[id]["relations"].get((tag1, tag2), None) if relation is None else relation if relation is None: relation = "false" start = 0 line_protected = re.sub(r"(.)\.(?=[\d])", r"\1[PROTECTED_DOT]", line) for sentence in re.split(r'\.|\?', line_protected): sentence = sentence.replace("[PROTECTED_DOT]", ".") original_sentence = sentence end = start + len(sentence) if (tag1_details[0] >= start and tag1_details[1] <= end) and \ (tag2_details[0] >= start and tag2_details[1] <= end): for offset_start, offset_end, value in sorted(list(data[id]["entities"]["CHEMICAL"].values()) + list(data[id]["entities"]["GENE"].values()), reverse=True): if (offset_start, offset_end) == (tag1_details[0], tag1_details[1]) or (offset_start, offset_end) == (tag2_details[0], tag2_details[1]): if sentence[offset_start - start] == "@": offset_end = start + sentence.find('$',offset_start - start) + 1 word = value elif offset_start < start or offset_end > end or sentence[offset_start - start] == "@": continue else: word = "OTHER" sentence = sentence[:offset_start-start] + "@" + word + "$" + sentence[offset_end-start:] sentence = sentence.strip() owriter.writerow([id+"."+tag1+"."+tag2, sentence, relation]) num_sentences += 1 if id == "23538201" and start == 1048: print("Accepted", tag1, tag2) else: rejected += 1 start = end + 1 print("Succesfully written {} samples to {}".format(num_sentences, output_filename)) print("Rejected are", rejected) if __name__=="__main__": parser = argparse.ArgumentParser( description='Preprocessing Application for ChemProt' ) parser.add_argument( '--input_folder', type=str, help='Specify the input files in a comma-separated list (no spaces)' ) parser.add_argument( '--output_folder', type=str, help='Specify the input files in a comma-separated list (no spaces)' ) args = parser.parse_args() preprocess_chemprot = ChemProtTextFormatting(args.input_folder, args.output_folder)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/ChemProtTextFormatting.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import glob import os import pubmed_parser as pmp class PubMedTextFormatting: def __init__(self, pubmed_path, output_filename, recursive = False): self.pubmed_path = pubmed_path self.recursive = recursive self.output_filename = output_filename # This puts one article per line def merge(self): print('PubMed path:', self.pubmed_path) with open(self.output_filename, mode='w', newline='\n') as ofile: for filename in glob.glob(self.pubmed_path + '/*.xml*', recursive=self.recursive): print('file:', filename) dicts_out = pmp.parse_medline_xml(filename) for dict_out in dicts_out: if not dict_out['abstract']: continue try: for line in dict_out['abstract'].splitlines(): if len(line) < 30: continue ofile.write(line.strip() + " ") ofile.write("\n\n") except: ofile.write("\n\n") continue
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/PubMedTextFormatting.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys import wget from pathlib import Path def mkdir(path): Path(path).mkdir(parents=True, exist_ok=True) class GLUEDownloader: def __init__(self, save_path): self.save_path = save_path + '/glue' def download(self, task_name): mkdir(self.save_path) if task_name in {'mrpc', 'mnli'}: task_name = task_name.upper() elif task_name == 'cola': task_name = 'CoLA' else: # SST-2 assert task_name == 'sst-2' task_name = 'SST' wget.download( 'https://gist.githubusercontent.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e/raw/1502038877f6a88c225a34450793fbc3ea87eaba/download_glue_data.py', out=self.save_path, ) sys.path.append(self.save_path) import download_glue_data download_glue_data.main( ['--data_dir', self.save_path, '--tasks', task_name]) sys.path.pop()
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/GLUEDownloader.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import BookscorpusTextFormatting import Downloader import TextSharding import WikicorpusTextFormatting import PubMedTextFormatting import argparse import itertools import multiprocessing import os import pprint import subprocess def main(args): working_dir = os.environ['BERT_PREP_WORKING_DIR'] print('Working Directory:', working_dir) print('Action:', args.action) print('Dataset Name:', args.dataset) if args.input_files: args.input_files = args.input_files.split(',') hdf5_tfrecord_folder_prefix = "/lower_case_" + str(args.do_lower_case) + "_seq_len_" + str(args.max_seq_length) \ + "_max_pred_" + str(args.max_predictions_per_seq) + "_masked_lm_prob_" + str(args.masked_lm_prob) \ + "_random_seed_" + str(args.random_seed) + "_dupe_factor_" + str(args.dupe_factor) \ + "_shard_" + str(args.n_training_shards) + "_test_split_" + str(int(args.fraction_test_set * 100)) directory_structure = { 'download' : working_dir + '/download', # Downloaded and decompressed 'extracted' : working_dir +'/extracted', # Extracted from whatever the initial format is (e.g., wikiextractor) 'formatted' : working_dir + '/formatted_one_article_per_line', # This is the level where all sources should look the same 'sharded' : working_dir + '/sharded', 'tfrecord' : working_dir + '/tfrecord' + hdf5_tfrecord_folder_prefix, 'hdf5': working_dir + '/hdf5'+ hdf5_tfrecord_folder_prefix, } print('\nDirectory Structure:') pp = pprint.PrettyPrinter(indent=2) pp.pprint(directory_structure) print('') if args.action == 'download': if not os.path.exists(directory_structure['download']): os.makedirs(directory_structure['download']) downloader = Downloader.Downloader(args.dataset, directory_structure['download']) downloader.download() elif args.action == 'text_formatting': assert args.dataset != 'google_pretrained_weights' and args.dataset != 'nvidia_pretrained_weights' \ and args.dataset != 'squad' and args.dataset != 'mrpc' and args.dataset != 'cola' and \ args.dataset != 'mnli' and args.dataset != 'sst-2', 'Cannot perform text_formatting on pretrained weights' if not os.path.exists(directory_structure['extracted']): os.makedirs(directory_structure['extracted']) if not os.path.exists(directory_structure['formatted']): os.makedirs(directory_structure['formatted']) if args.dataset == 'bookscorpus': books_path = directory_structure['download'] + '/bookscorpus' #books_path = directory_structure['download'] output_filename = directory_structure['formatted'] + '/bookscorpus_one_book_per_line.txt' books_formatter = BookscorpusTextFormatting.BookscorpusTextFormatting(books_path, output_filename, recursive=True) books_formatter.merge() elif args.dataset == 'wikicorpus_en': if args.skip_wikiextractor == 0: path_to_wikiextractor_in_container = '/workspace/wikiextractor/WikiExtractor.py' wikiextractor_command = path_to_wikiextractor_in_container + ' ' + directory_structure['download'] + '/' + args.dataset + '/wikicorpus_en.xml ' + '-b 100M --processes ' + str(args.n_processes) + ' -o ' + directory_structure['extracted'] + '/' + args.dataset print('WikiExtractor Command:', wikiextractor_command) wikiextractor_process = subprocess.run(wikiextractor_command, shell=True, check=True) wiki_path = directory_structure['extracted'] + '/wikicorpus_en' output_filename = directory_structure['formatted'] + '/wikicorpus_en_one_article_per_line.txt' wiki_formatter = WikicorpusTextFormatting.WikicorpusTextFormatting(wiki_path, output_filename, recursive=True) wiki_formatter.merge() elif args.dataset == 'wikicorpus_zh': assert False, 'wikicorpus_zh not fully supported at this time. The simplified/tradition Chinese data needs to be translated and properly segmented still, and should work once this step is added.' if args.skip_wikiextractor == 0: path_to_wikiextractor_in_container = '/workspace/wikiextractor/WikiExtractor.py' wikiextractor_command = path_to_wikiextractor_in_container + ' ' + directory_structure['download'] + '/' + args.dataset + '/wikicorpus_zh.xml ' + '-b 100M --processes ' + str(args.n_processes) + ' -o ' + directory_structure['extracted'] + '/' + args.dataset print('WikiExtractor Command:', wikiextractor_command) wikiextractor_process = subprocess.run(wikiextractor_command, shell=True, check=True) wiki_path = directory_structure['extracted'] + '/wikicorpus_zh' output_filename = directory_structure['formatted'] + '/wikicorpus_zh_one_article_per_line.txt' wiki_formatter = WikicorpusTextFormatting.WikicorpusTextFormatting(wiki_path, output_filename, recursive=True) wiki_formatter.merge() elif args.dataset == 'pubmed_baseline': pubmed_path = directory_structure['download'] + '/pubmed' + '/baseline' output_filename = directory_structure['formatted'] + '/pubmed_baseline_one_article_per_line.txt' pubmed_formatter = PubMedTextFormatting.PubMedTextFormatting(pubmed_path, output_filename, recursive=True) pubmed_formatter.merge() elif args.action == 'sharding': # Note: books+wiki requires user to provide list of input_files (comma-separated with no spaces) if args.dataset == 'bookscorpus' or 'wikicorpus' in args.dataset or 'books_wiki' in args.dataset or 'pubmed' in args.dataset: if args.input_files is None: if args.dataset == 'bookscorpus': args.input_files = [directory_structure['formatted'] + '/bookscorpus_one_book_per_line.txt'] elif args.dataset == 'wikicorpus_en': args.input_files = [directory_structure['formatted'] + '/wikicorpus_en_one_article_per_line.txt'] elif args.dataset == 'wikicorpus_zh': args.input_files = [directory_structure['formatted'] + '/wikicorpus_zh_one_article_per_line.txt'] elif args.dataset == 'books_wiki_en_corpus': args.input_files = [directory_structure['formatted'] + '/bookscorpus_one_book_per_line.txt', directory_structure['formatted'] + '/wikicorpus_en_one_article_per_line.txt'] elif args.dataset == 'pubmed_baseline': args.input_files = [directory_structure['formatted'] + '/pubmed_baseline_one_article_per_line.txt'] output_file_prefix = directory_structure['sharded'] + '/' + args.dataset + '/' + args.dataset if not os.path.exists(directory_structure['sharded']): os.makedirs(directory_structure['sharded']) if not os.path.exists(directory_structure['sharded'] + '/' + args.dataset): os.makedirs(directory_structure['sharded'] + '/' + args.dataset) if not os.path.exists(directory_structure['sharded'] + '/' + args.dataset + '/training'): os.makedirs(directory_structure['sharded'] + '/' + args.dataset + '/training') if not os.path.exists(directory_structure['sharded'] + '/' + args.dataset + '/test'): os.makedirs(directory_structure['sharded'] + '/' + args.dataset + '/test') # Segmentation is here because all datasets look the same in one article/book/whatever per line format, and # it seemed unnecessarily complicated to add an additional preprocessing step to call just for this. # Different languages (e.g., Chinese simplified/traditional) may require translation and # other packages to be called from here -- just add a conditional branch for those extra steps segmenter = TextSharding.NLTKSegmenter() sharding = TextSharding.Sharding(args.input_files, output_file_prefix, args.n_training_shards, args.n_test_shards, args.fraction_test_set) sharding.load_articles() sharding.segment_articles_into_sentences(segmenter) sharding.distribute_articles_over_shards() sharding.write_shards_to_disk() else: assert False, 'Unsupported dataset for sharding' elif args.action == 'create_tfrecord_files': if not os.path.exists(directory_structure['tfrecord'] + "/" + args.dataset): os.makedirs(directory_structure['tfrecord'] + "/" + args.dataset) if not os.path.exists(directory_structure['tfrecord'] + "/" + args.dataset + '/training'): os.makedirs(directory_structure['tfrecord'] + "/" + args.dataset + '/training') if not os.path.exists(directory_structure['tfrecord'] + "/" + args.dataset + '/test'): os.makedirs(directory_structure['tfrecord'] + "/" + args.dataset + '/test') last_process = None def create_record_worker(filename_prefix, shard_id, output_format='tfrecord', split='training'): bert_preprocessing_command = 'python /workspace/bert/utils/create_pretraining_data.py' bert_preprocessing_command += ' --input_file=' + directory_structure['sharded'] + '/' + args.dataset + '/' + split + '/' + filename_prefix + '_' + str(shard_id) + '.txt' bert_preprocessing_command += ' --output_file=' + directory_structure['tfrecord'] + '/' + args.dataset + '/' + split + '/' + filename_prefix + '_' + str(shard_id) + '.' + output_format bert_preprocessing_command += ' --vocab_file=' + args.vocab_file bert_preprocessing_command += ' --do_lower_case' if args.do_lower_case else '' bert_preprocessing_command += ' --max_seq_length=' + str(args.max_seq_length) bert_preprocessing_command += ' --max_predictions_per_seq=' + str(args.max_predictions_per_seq) bert_preprocessing_command += ' --masked_lm_prob=' + str(args.masked_lm_prob) bert_preprocessing_command += ' --random_seed=' + str(args.random_seed) bert_preprocessing_command += ' --dupe_factor=' + str(args.dupe_factor) bert_preprocessing_process = subprocess.Popen(bert_preprocessing_command, shell=True) last_process = bert_preprocessing_process # This could be better optimized (fine if all take equal time) if shard_id % args.n_processes == 0 and shard_id > 0: bert_preprocessing_process.wait() return last_process output_file_prefix = args.dataset for i in range(args.n_training_shards): last_process = create_record_worker(output_file_prefix + '_training', i, 'tfrecord', 'training') last_process.wait() for i in range(args.n_test_shards): last_process = create_record_worker(output_file_prefix + '_test', i, 'tfrecord', 'test') last_process.wait() elif args.action == 'create_hdf5_files': assert False, 'HDF5 format not fully supported in this release.' if not os.path.exists(directory_structure['hdf5'] + "/" + args.dataset): os.makedirs(directory_structure['hdf5'] + "/" + args.dataset) last_process = None def create_record_worker(filename_prefix, shard_id, output_format='hdf5'): bert_preprocessing_command = 'python /workspace/bert/utils/create_pretraining_data.py' bert_preprocessing_command += ' --input_file=' + directory_structure['sharded'] + '/' + args.dataset + '/' + filename_prefix + '_' + str(shard_id) + '.txt' bert_preprocessing_command += ' --output_file=' + directory_structure['hdf5'] + '/' + args.dataset + '/' + filename_prefix + '_' + str(shard_id) + '.' + output_format bert_preprocessing_command += ' --vocab_file=' + args.vocab_file bert_preprocessing_command += ' --do_lower_case' if args.do_lower_case else '' bert_preprocessing_command += ' --max_seq_length=' + args.max_seq_length bert_preprocessing_command += ' --max_predictions_per_seq=' + args.max_predictions_per_seq bert_preprocessing_command += ' --masked_lm_prob=' + args.masked_lm_prob bert_preprocessing_command += ' --random_seed=' + args.random_seed bert_preprocessing_command += ' --dupe_factor=' + args.dupe_factor bert_preprocessing_process = subprocess.Popen(bert_preprocessing_command, shell=True) last_process = bert_preprocessing_process # This could be better optimized (fine if all take equal time) if shard_id % args.n_processes == 0 and shard_id > 0: bert_preprocessing_process.wait() for i in range(args.n_training_shards): create_record_worker(args.output_file_prefix + '_training', i) last_process.wait() for i in range(args.n_test_shards): create_record_worker(args.output_file_prefix + '_test', i) last_process.wait() if __name__ == "__main__": parser = argparse.ArgumentParser( description='Preprocessing Application for Everything BERT-related' ) parser.add_argument( '--action', type=str, help='Specify the action you want the app to take. e.g., generate vocab, segment, create tfrecords', choices={ 'download', # Download and verify mdf5/sha sums 'text_formatting', # Convert into a file that contains one article/book per line 'sharding', # Convert previous formatted text into shards containing one sentence per line 'create_tfrecord_files', # Turn each shard into a TFrecord with masking and next sentence prediction info 'create_hdf5_files' # Turn each shard into a HDF5 file with masking and next sentence prediction info } ) parser.add_argument( '--dataset', type=str, help='Specify the dataset to perform --action on', choices={ 'bookscorpus', 'wikicorpus_en', 'wikicorpus_zh', 'books_wiki_en_corpus', 'pubmed_baseline', 'pubmed_daily_update', 'pubmed_fulltext', 'pubmed_open_access', 'google_pretrained_weights', 'nvidia_pretrained_weights', 'squad', 'mrpc', 'sst-2', 'mnli', 'cola', 'all' } ) parser.add_argument( '--input_files', type=str, help='Specify the input files in a comma-separated list (no spaces)' ) parser.add_argument( '--n_training_shards', type=int, help='Specify the number of training shards to generate', default=1472 ) parser.add_argument( '--n_test_shards', type=int, help='Specify the number of test shards to generate', default=1472 ) parser.add_argument( '--fraction_test_set', type=float, help='Specify the fraction (0..1) of the data to withhold for the test data split (based on number of sequences)', default=0.1 ) parser.add_argument( '--segmentation_method', type=str, help='Specify your choice of sentence segmentation', choices={ 'nltk' }, default='nltk' ) parser.add_argument( '--n_processes', type=int, help='Specify the max number of processes to allow at one time', default=4 ) parser.add_argument( '--random_seed', type=int, help='Specify the base seed to use for any random number generation', default=12345 ) parser.add_argument( '--dupe_factor', type=int, help='Specify the duplication factor', default=5 ) parser.add_argument( '--masked_lm_prob', type=float, help='Specify the probability for masked lm', default=0.15 ) parser.add_argument( '--max_seq_length', type=int, help='Specify the maximum sequence length', default=512 ) parser.add_argument( '--max_predictions_per_seq', type=int, help='Specify the maximum number of masked words per sequence', default=20 ) parser.add_argument( '--do_lower_case', type=int, help='Specify whether it is cased (0) or uncased (1) (any number greater than 0 will be treated as uncased)', default=1 ) parser.add_argument( '--vocab_file', type=str, help='Specify absolute path to vocab file to use)' ) parser.add_argument( '--skip_wikiextractor', type=int, help='Specify whether to skip wikiextractor step 0=False, 1=True', default=0 ) parser.add_argument( '--interactive_json_config_generator', type=str, help='Specify the action you want the app to take. e.g., generate vocab, segment, create tfrecords' ) args = parser.parse_args() main(args)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/bertPrep.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import subprocess class BooksDownloader: def __init__(self, save_path): self.save_path = save_path pass def download(self): bookscorpus_download_command = 'python3 /workspace/bookcorpus/download_files.py --list /workspace/bookcorpus/url_list.jsonl --out' bookscorpus_download_command += ' ' + self.save_path + '/bookscorpus' bookscorpus_download_command += ' --trash-bad-count' bookscorpus_download_process = subprocess.run(bookscorpus_download_command, shell=True, check=True)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/BooksDownloader.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from collections import defaultdict from itertools import islice import multiprocessing import os import statistics class Sharding: def __init__(self, input_files, output_name_prefix, n_training_shards, n_test_shards, fraction_test_set): assert len(input_files) > 0, 'The input file list must contain at least one file.' assert n_training_shards > 0, 'There must be at least one output shard.' assert n_test_shards > 0, 'There must be at least one output shard.' self.n_training_shards = n_training_shards self.n_test_shards = n_test_shards self.fraction_test_set = fraction_test_set self.input_files = input_files self.output_name_prefix = output_name_prefix self.output_training_identifier = '_training' self.output_test_identifier = '_test' self.output_file_extension = '.txt' self.articles = {} # key: integer identifier, value: list of articles self.sentences = {} # key: integer identifier, value: list of sentences self.output_training_files = {} # key: filename, value: list of articles to go into file self.output_test_files = {} # key: filename, value: list of articles to go into file self.init_output_files() # Remember, the input files contain one article per line (the whitespace check is to skip extraneous blank lines) def load_articles(self): print('Start: Loading Articles') global_article_count = 0 for input_file in self.input_files: print('input file:', input_file) with open(input_file, mode='r', newline='\n') as f: for i, line in enumerate(f): if line.strip(): self.articles[global_article_count] = line.rstrip() global_article_count += 1 print('End: Loading Articles: There are', len(self.articles), 'articles.') def segment_articles_into_sentences(self, segmenter): print('Start: Sentence Segmentation') if len(self.articles) is 0: self.load_articles() assert len(self.articles) is not 0, 'Please check that input files are present and contain data.' # TODO: WIP: multiprocessing (create independent ranges and spawn processes) use_multiprocessing = 'serial' def chunks(data, size=len(self.articles)): it = iter(data) for i in range(0, len(data), size): yield {k: data[k] for k in islice(it, size)} if use_multiprocessing == 'manager': manager = multiprocessing.Manager() return_dict = manager.dict() jobs = [] n_processes = 7 # in addition to the main process, total = n_proc+1 def work(articles, return_dict): sentences = {} for i, article in enumerate(articles): sentences[i] = segmenter.segment_string(articles[article]) if i % 5000 == 0: print('Segmenting article', i) return_dict.update(sentences) for item in chunks(self.articles, len(self.articles)): p = multiprocessing.Process(target=work, args=(item, return_dict)) # Busy wait while len(jobs) >= n_processes: pass jobs.append(p) p.start() for proc in jobs: proc.join() elif use_multiprocessing == 'queue': work_queue = multiprocessing.Queue() jobs = [] for item in chunks(self.articles, len(self.articles)): pass else: # serial option for i, article in enumerate(self.articles): self.sentences[i] = segmenter.segment_string(self.articles[article]) if i % 5000 == 0: print('Segmenting article', i) print('End: Sentence Segmentation') def init_output_files(self): print('Start: Init Output Files') assert len(self.output_training_files) is 0, 'Internal storage self.output_files already contains data. This function is intended to be used by the constructor only.' assert len(self.output_test_files) is 0, 'Internal storage self.output_files already contains data. This function is intended to be used by the constructor only.' for i in range(self.n_training_shards): name = self.output_name_prefix + self.output_training_identifier + '_' + str(i) + self.output_file_extension self.output_training_files[name] = [] for i in range(self.n_test_shards): name = self.output_name_prefix + self.output_test_identifier + '_' + str(i) + self.output_file_extension self.output_test_files[name] = [] print('End: Init Output Files') def get_sentences_per_shard(self, shard): result = 0 for article_id in shard: result += len(self.sentences[article_id]) return result def distribute_articles_over_shards(self): print('Start: Distribute Articles Over Shards') assert len(self.articles) >= self.n_training_shards + self.n_test_shards, 'There are fewer articles than shards. Please add more data or reduce the number of shards requested.' # Create dictionary with - key: sentence count per article, value: article id number sentence_counts = defaultdict(lambda: []) max_sentences = 0 total_sentences = 0 for article_id in self.sentences: current_length = len(self.sentences[article_id]) sentence_counts[current_length].append(article_id) max_sentences = max(max_sentences, current_length) total_sentences += current_length n_sentences_assigned_to_training = int((1 - self.fraction_test_set) * total_sentences) nominal_sentences_per_training_shard = n_sentences_assigned_to_training // self.n_training_shards nominal_sentences_per_test_shard = (total_sentences - n_sentences_assigned_to_training) // self.n_test_shards consumed_article_set = set({}) unused_article_set = set(self.articles.keys()) # Make first pass and add one article worth of lines per file for file in self.output_training_files: current_article_id = sentence_counts[max_sentences][-1] sentence_counts[max_sentences].pop(-1) self.output_training_files[file].append(current_article_id) consumed_article_set.add(current_article_id) unused_article_set.remove(current_article_id) # Maintain the max sentence count while len(sentence_counts[max_sentences]) == 0 and max_sentences > 0: max_sentences -= 1 if len(self.sentences[current_article_id]) > nominal_sentences_per_training_shard: nominal_sentences_per_training_shard = len(self.sentences[current_article_id]) print('Warning: A single article contains more than the nominal number of sentences per training shard.') for file in self.output_test_files: current_article_id = sentence_counts[max_sentences][-1] sentence_counts[max_sentences].pop(-1) self.output_test_files[file].append(current_article_id) consumed_article_set.add(current_article_id) unused_article_set.remove(current_article_id) # Maintain the max sentence count while len(sentence_counts[max_sentences]) == 0 and max_sentences > 0: max_sentences -= 1 if len(self.sentences[current_article_id]) > nominal_sentences_per_test_shard: nominal_sentences_per_test_shard = len(self.sentences[current_article_id]) print('Warning: A single article contains more than the nominal number of sentences per test shard.') training_counts = [] test_counts = [] for shard in self.output_training_files: training_counts.append(self.get_sentences_per_shard(self.output_training_files[shard])) for shard in self.output_test_files: test_counts.append(self.get_sentences_per_shard(self.output_test_files[shard])) training_median = statistics.median(training_counts) test_median = statistics.median(test_counts) # Make subsequent passes over files to find articles to add without going over limit history_remaining = [] n_history_remaining = 4 while len(consumed_article_set) < len(self.articles): for fidx, file in enumerate(self.output_training_files): nominal_next_article_size = min(nominal_sentences_per_training_shard - training_counts[fidx], max_sentences) # Maintain the max sentence count while len(sentence_counts[max_sentences]) == 0 and max_sentences > 0: max_sentences -= 1 while len(sentence_counts[nominal_next_article_size]) == 0 and nominal_next_article_size > 0: nominal_next_article_size -= 1 if nominal_next_article_size not in sentence_counts or nominal_next_article_size is 0 or training_counts[fidx] > training_median: continue # skip adding to this file, will come back later if no file can accept unused articles current_article_id = sentence_counts[nominal_next_article_size][-1] sentence_counts[nominal_next_article_size].pop(-1) self.output_training_files[file].append(current_article_id) consumed_article_set.add(current_article_id) unused_article_set.remove(current_article_id) for fidx, file in enumerate(self.output_test_files): nominal_next_article_size = min(nominal_sentences_per_test_shard - test_counts[fidx], max_sentences) # Maintain the max sentence count while len(sentence_counts[max_sentences]) == 0 and max_sentences > 0: max_sentences -= 1 while len(sentence_counts[nominal_next_article_size]) == 0 and nominal_next_article_size > 0: nominal_next_article_size -= 1 if nominal_next_article_size not in sentence_counts or nominal_next_article_size is 0 or test_counts[fidx] > test_median: continue # skip adding to this file, will come back later if no file can accept unused articles current_article_id = sentence_counts[nominal_next_article_size][-1] sentence_counts[nominal_next_article_size].pop(-1) self.output_test_files[file].append(current_article_id) consumed_article_set.add(current_article_id) unused_article_set.remove(current_article_id) # If unable to place articles a few times, bump up nominal sizes by fraction until articles get placed if len(history_remaining) == n_history_remaining: history_remaining.pop(0) history_remaining.append(len(unused_article_set)) history_same = True for i in range(1, len(history_remaining)): history_same = history_same and (history_remaining[i-1] == history_remaining[i]) if history_same: nominal_sentences_per_training_shard += 1 # nominal_sentences_per_test_shard += 1 training_counts = [] test_counts = [] for shard in self.output_training_files: training_counts.append(self.get_sentences_per_shard(self.output_training_files[shard])) for shard in self.output_test_files: test_counts.append(self.get_sentences_per_shard(self.output_test_files[shard])) training_median = statistics.median(training_counts) test_median = statistics.median(test_counts) print('Distributing data over shards:', len(unused_article_set), 'articles remaining.') if len(unused_article_set) != 0: print('Warning: Some articles did not make it into output files.') for shard in self.output_training_files: print('Training shard:', self.get_sentences_per_shard(self.output_training_files[shard])) for shard in self.output_test_files: print('Test shard:', self.get_sentences_per_shard(self.output_test_files[shard])) print('End: Distribute Articles Over Shards') def write_shards_to_disk(self): print('Start: Write Shards to Disk') for shard in self.output_training_files: self.write_single_shard(shard, self.output_training_files[shard], 'training') for shard in self.output_test_files: self.write_single_shard(shard, self.output_test_files[shard], 'test') print('End: Write Shards to Disk') def write_single_shard(self, shard_name, shard, split): shard_split = os.path.split(shard_name) shard_name = shard_split[0] + '/' + split + '/' + shard_split[1] with open(shard_name, mode='w', newline='\n') as f: for article_id in shard: for line in self.sentences[article_id]: f.write(line + '\n') f.write('\n') # Line break between articles import nltk nltk.download('punkt') class NLTKSegmenter: def __init(self): pass def segment_string(self, article): return nltk.tokenize.sent_tokenize(article)
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/TextSharding.py
# Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import hashlib import os import urllib.request import zipfile class GooglePretrainedWeightDownloader: def __init__(self, save_path): self.save_path = save_path + '/google_pretrained_weights' if not os.path.exists(self.save_path): os.makedirs(self.save_path) # Download urls self.model_urls = { 'bert_base_uncased': ('https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip', 'uncased_L-12_H-768_A-12.zip'), 'bert_large_uncased': ('https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-24_H-1024_A-16.zip', 'uncased_L-24_H-1024_A-16.zip'), 'bert_base_cased': ('https://storage.googleapis.com/bert_models/2018_10_18/cased_L-12_H-768_A-12.zip', 'cased_L-12_H-768_A-12.zip'), 'bert_large_cased': ('https://storage.googleapis.com/bert_models/2018_10_18/cased_L-24_H-1024_A-16.zip', 'cased_L-24_H-1024_A-16.zip'), 'bert_base_multilingual_cased': ('https://storage.googleapis.com/bert_models/2018_11_23/multi_cased_L-12_H-768_A-12.zip', 'multi_cased_L-12_H-768_A-12.zip'), 'bert_large_multilingual_uncased': ('https://storage.googleapis.com/bert_models/2018_11_03/multilingual_L-12_H-768_A-12.zip', 'multilingual_L-12_H-768_A-12.zip'), 'bert_base_chinese': ('https://storage.googleapis.com/bert_models/2018_11_03/chinese_L-12_H-768_A-12.zip', 'chinese_L-12_H-768_A-12.zip') } # SHA256sum verification for file download integrity (and checking for changes from the download source over time) self.bert_base_uncased_sha = { 'bert_config.json': '7b4e5f53efbd058c67cda0aacfafb340113ea1b5797d9ce6ee411704ba21fcbc', 'bert_model.ckpt.data-00000-of-00001': '58580dc5e0bf0ae0d2efd51d0e8272b2f808857f0a43a88aaf7549da6d7a8a84', 'bert_model.ckpt.index': '04c1323086e2f1c5b7c0759d8d3e484afbb0ab45f51793daab9f647113a0117b', 'bert_model.ckpt.meta': 'dd5682170a10c3ea0280c2e9b9a45fee894eb62da649bbdea37b38b0ded5f60e', 'vocab.txt': '07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3', } self.bert_large_uncased_sha = { 'bert_config.json': 'bfa42236d269e2aeb3a6d30412a33d15dbe8ea597e2b01dc9518c63cc6efafcb', 'bert_model.ckpt.data-00000-of-00001': 'bc6b3363e3be458c99ecf64b7f472d2b7c67534fd8f564c0556a678f90f4eea1', 'bert_model.ckpt.index': '68b52f2205ffc64dc627d1120cf399c1ef1cbc35ea5021d1afc889ffe2ce2093', 'bert_model.ckpt.meta': '6fcce8ff7628f229a885a593625e3d5ff9687542d5ef128d9beb1b0c05edc4a1', 'vocab.txt': '07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3', } self.bert_base_cased_sha = { 'bert_config.json': 'f11dfb757bea16339a33e1bf327b0aade6e57fd9c29dc6b84f7ddb20682f48bc', 'bert_model.ckpt.data-00000-of-00001': '734d5a1b68bf98d4e9cb6b6692725d00842a1937af73902e51776905d8f760ea', 'bert_model.ckpt.index': '517d6ef5c41fc2ca1f595276d6fccf5521810d57f5a74e32616151557790f7b1', 'bert_model.ckpt.meta': '5f8a9771ff25dadd61582abb4e3a748215a10a6b55947cbb66d0f0ba1694be98', 'vocab.txt': 'eeaa9875b23b04b4c54ef759d03db9d1ba1554838f8fb26c5d96fa551df93d02', } self.bert_large_cased_sha = { 'bert_config.json': '7adb2125c8225da495656c982fd1c5f64ba8f20ad020838571a3f8a954c2df57', 'bert_model.ckpt.data-00000-of-00001': '6ff33640f40d472f7a16af0c17b1179ca9dcc0373155fb05335b6a4dd1657ef0', 'bert_model.ckpt.index': 'ef42a53f577fbe07381f4161b13c7cab4f4fc3b167cec6a9ae382c53d18049cf', 'bert_model.ckpt.meta': 'd2ddff3ed33b80091eac95171e94149736ea74eb645e575d942ec4a5e01a40a1', 'vocab.txt': 'eeaa9875b23b04b4c54ef759d03db9d1ba1554838f8fb26c5d96fa551df93d02', } self.bert_base_multilingual_cased_sha = { 'bert_config.json': 'e76c3964bc14a8bb37a5530cdc802699d2f4a6fddfab0611e153aa2528f234f0', 'bert_model.ckpt.data-00000-of-00001': '55b8a2df41f69c60c5180e50a7c31b7cdf6238909390c4ddf05fbc0d37aa1ac5', 'bert_model.ckpt.index': '7d8509c2a62b4e300feb55f8e5f1eef41638f4998dd4d887736f42d4f6a34b37', 'bert_model.ckpt.meta': '95e5f1997e8831f1c31e5cf530f1a2e99f121e9cd20887f2dce6fe9e3343e3fa', 'vocab.txt': 'fe0fda7c425b48c516fc8f160d594c8022a0808447475c1a7c6d6479763f310c', } self.bert_large_multilingual_uncased_sha = { 'bert_config.json': '49063bb061390211d2fdd108cada1ed86faa5f90b80c8f6fdddf406afa4c4624', 'bert_model.ckpt.data-00000-of-00001': '3cd83912ebeb0efe2abf35c9f1d5a515d8e80295e61c49b75c8853f756658429', 'bert_model.ckpt.index': '87c372c1a3b1dc7effaaa9103c80a81b3cbab04c7933ced224eec3b8ad2cc8e7', 'bert_model.ckpt.meta': '27f504f34f02acaa6b0f60d65195ec3e3f9505ac14601c6a32b421d0c8413a29', 'vocab.txt': '87b44292b452f6c05afa49b2e488e7eedf79ea4f4c39db6f2f4b37764228ef3f', } self.bert_base_chinese_sha = { 'bert_config.json': '7aaad0335058e2640bcb2c2e9a932b1cd9da200c46ea7b8957d54431f201c015', 'bert_model.ckpt.data-00000-of-00001': '756699356b78ad0ef1ca9ba6528297bcb3dd1aef5feadd31f4775d7c7fc989ba', 'bert_model.ckpt.index': '46315546e05ce62327b3e2cd1bed22836adcb2ff29735ec87721396edb21b82e', 'bert_model.ckpt.meta': 'c0f8d51e1ab986604bc2b25d6ec0af7fd21ff94cf67081996ec3f3bf5d823047', 'vocab.txt': '45bbac6b341c319adc98a532532882e91a9cefc0329aa57bac9ae761c27b291c', } # Relate SHA to urls for loop below self.model_sha = { 'bert_base_uncased': self.bert_base_uncased_sha, 'bert_large_uncased': self.bert_large_uncased_sha, 'bert_base_cased': self.bert_base_cased_sha, 'bert_large_cased': self.bert_large_cased_sha, 'bert_base_multilingual_cased': self.bert_base_multilingual_cased_sha, 'bert_large_multilingual_uncased': self.bert_large_multilingual_uncased_sha, 'bert_base_chinese': self.bert_base_chinese_sha } # Helper to get sha256sum of a file def sha256sum(self, filename): h = hashlib.sha256() b = bytearray(128*1024) mv = memoryview(b) with open(filename, 'rb', buffering=0) as f: for n in iter(lambda : f.readinto(mv), 0): h.update(mv[:n]) return h.hexdigest() def download(self): # Iterate over urls: download, unzip, verify sha256sum found_mismatch_sha = False for model in self.model_urls: url = self.model_urls[model][0] file = self.save_path + '/' + self.model_urls[model][1] print('Downloading', url) response = urllib.request.urlopen(url) with open(file, 'wb') as handle: handle.write(response.read()) print('Unzipping', file) zip = zipfile.ZipFile(file, 'r') zip.extractall(self.save_path) zip.close() sha_dict = self.model_sha[model] for extracted_file in sha_dict: sha = sha_dict[extracted_file] if sha != self.sha256sum(file[:-4] + '/' + extracted_file): found_mismatch_sha = True print('SHA256sum does not match on file:', extracted_file, 'from download url:', url) else: print(file[:-4] + '/' + extracted_file, '\t', 'verified') if not found_mismatch_sha: print("All downloads pass sha256sum verification.") def serialize(self): pass def deserialize(self): pass def listAvailableWeights(self): print("Available Weight Datasets") for item in self.model_urls: print(item) def listLocallyStoredWeights(self): pass
DeepLearningExamples-master
TensorFlow/LanguageModeling/BERT/data/GooglePretrainedWeightDownloader.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.export_inference_graph.""" import os import numpy as np import six import tensorflow as tf from google.protobuf import text_format from tensorflow.python.framework import dtypes from tensorflow.python.ops import array_ops from object_detection import exporter from object_detection.builders import graph_rewriter_builder from object_detection.builders import model_builder from object_detection.core import model from object_detection.protos import graph_rewriter_pb2 from object_detection.protos import pipeline_pb2 from object_detection.utils import ops if six.PY2: import mock # pylint: disable=g-import-not-at-top else: from unittest import mock # pylint: disable=g-import-not-at-top slim = tf.contrib.slim class FakeModel(model.DetectionModel): def __init__(self, add_detection_keypoints=False, add_detection_masks=False): self._add_detection_keypoints = add_detection_keypoints self._add_detection_masks = add_detection_masks def preprocess(self, inputs): true_image_shapes = [] # Doesn't matter for the fake model. return tf.identity(inputs), true_image_shapes def predict(self, preprocessed_inputs, true_image_shapes): return {'image': tf.layers.conv2d(preprocessed_inputs, 3, 1)} def postprocess(self, prediction_dict, true_image_shapes): with tf.control_dependencies(prediction_dict.values()): postprocessed_tensors = { 'detection_boxes': tf.constant([[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]], tf.float32), 'detection_scores': tf.constant([[0.7, 0.6], [0.9, 0.0]], tf.float32), 'detection_classes': tf.constant([[0, 1], [1, 0]], tf.float32), 'num_detections': tf.constant([2, 1], tf.float32) } if self._add_detection_keypoints: postprocessed_tensors['detection_keypoints'] = tf.constant( np.arange(48).reshape([2, 2, 6, 2]), tf.float32) if self._add_detection_masks: postprocessed_tensors['detection_masks'] = tf.constant( np.arange(64).reshape([2, 2, 4, 4]), tf.float32) return postprocessed_tensors def restore_map(self, checkpoint_path, fine_tune_checkpoint_type): pass def loss(self, prediction_dict, true_image_shapes): pass def regularization_losses(self): pass def updates(self): pass class ExportInferenceGraphTest(tf.test.TestCase): def _save_checkpoint_from_mock_model(self, checkpoint_path, use_moving_averages, enable_quantization=False): g = tf.Graph() with g.as_default(): mock_model = FakeModel() preprocessed_inputs, true_image_shapes = mock_model.preprocess( tf.placeholder(tf.float32, shape=[None, None, None, 3])) predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) mock_model.postprocess(predictions, true_image_shapes) if use_moving_averages: tf.train.ExponentialMovingAverage(0.0).apply() tf.train.get_or_create_global_step() if enable_quantization: graph_rewriter_config = graph_rewriter_pb2.GraphRewriter() graph_rewriter_config.quantization.delay = 500000 graph_rewriter_fn = graph_rewriter_builder.build( graph_rewriter_config, is_training=False) graph_rewriter_fn() saver = tf.train.Saver() init = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init) saver.save(sess, checkpoint_path) def _load_inference_graph(self, inference_graph_path, is_binary=True): od_graph = tf.Graph() with od_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(inference_graph_path) as fid: if is_binary: od_graph_def.ParseFromString(fid.read()) else: text_format.Parse(fid.read(), od_graph_def) tf.import_graph_def(od_graph_def, name='') return od_graph def _create_tf_example(self, image_array): with self.test_session(): encoded_image = tf.image.encode_jpeg(tf.constant(image_array)).eval() def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) example = tf.train.Example(features=tf.train.Features(feature={ 'image/encoded': _bytes_feature(encoded_image), 'image/format': _bytes_feature('jpg'), 'image/source_id': _bytes_feature('image_id') })).SerializeToString() return example def test_export_graph_with_image_tensor_input(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() output_directory = os.path.join(tmp_dir, 'output') pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) self.assertTrue(os.path.exists(os.path.join( output_directory, 'saved_model', 'saved_model.pb'))) def test_write_inference_graph(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() output_directory = os.path.join(tmp_dir, 'output') pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory, write_inference_graph=True) self.assertTrue(os.path.exists(os.path.join( output_directory, 'inference_graph.pbtxt'))) def test_export_graph_with_fixed_size_image_tensor_input(self): input_shape = [1, 320, 320, 3] tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model( trained_checkpoint_prefix, use_moving_averages=False) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() output_directory = os.path.join(tmp_dir, 'output') pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory, input_shape=input_shape) saved_model_path = os.path.join(output_directory, 'saved_model') self.assertTrue( os.path.exists(os.path.join(saved_model_path, 'saved_model.pb'))) with tf.Graph().as_default() as od_graph: with self.test_session(graph=od_graph) as sess: meta_graph = tf.saved_model.loader.load( sess, [tf.saved_model.tag_constants.SERVING], saved_model_path) signature = meta_graph.signature_def['serving_default'] input_tensor_name = signature.inputs['inputs'].name image_tensor = od_graph.get_tensor_by_name(input_tensor_name) self.assertSequenceEqual(image_tensor.get_shape().as_list(), input_shape) def test_export_graph_with_tf_example_input(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() output_directory = os.path.join(tmp_dir, 'output') pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='tf_example', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) self.assertTrue(os.path.exists(os.path.join( output_directory, 'saved_model', 'saved_model.pb'))) def test_export_graph_with_encoded_image_string_input(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() output_directory = os.path.join(tmp_dir, 'output') pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='encoded_image_string_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) self.assertTrue(os.path.exists(os.path.join( output_directory, 'saved_model', 'saved_model.pb'))) def _get_variables_in_checkpoint(self, checkpoint_file): return set([ var_name for var_name, _ in tf.train.list_variables(checkpoint_file)]) def test_replace_variable_values_with_moving_averages(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') new_checkpoint_prefix = os.path.join(tmp_dir, 'new.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) graph = tf.Graph() with graph.as_default(): fake_model = FakeModel() preprocessed_inputs, true_image_shapes = fake_model.preprocess( tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3])) predictions = fake_model.predict(preprocessed_inputs, true_image_shapes) fake_model.postprocess(predictions, true_image_shapes) exporter.replace_variable_values_with_moving_averages( graph, trained_checkpoint_prefix, new_checkpoint_prefix) expected_variables = set(['conv2d/bias', 'conv2d/kernel']) variables_in_old_ckpt = self._get_variables_in_checkpoint( trained_checkpoint_prefix) self.assertIn('conv2d/bias/ExponentialMovingAverage', variables_in_old_ckpt) self.assertIn('conv2d/kernel/ExponentialMovingAverage', variables_in_old_ckpt) variables_in_new_ckpt = self._get_variables_in_checkpoint( new_checkpoint_prefix) self.assertTrue(expected_variables.issubset(variables_in_new_ckpt)) self.assertNotIn('conv2d/bias/ExponentialMovingAverage', variables_in_new_ckpt) self.assertNotIn('conv2d/kernel/ExponentialMovingAverage', variables_in_new_ckpt) def test_export_graph_with_moving_averages(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = True exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) self.assertTrue(os.path.exists(os.path.join( output_directory, 'saved_model', 'saved_model.pb'))) expected_variables = set(['conv2d/bias', 'conv2d/kernel', 'global_step']) actual_variables = set( [var_name for var_name, _ in tf.train.list_variables(output_directory)]) self.assertTrue(expected_variables.issubset(actual_variables)) def test_export_model_with_quantization_nodes(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model( trained_checkpoint_prefix, use_moving_averages=False, enable_quantization=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'inference_graph.pbtxt') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() text_format.Merge( """graph_rewriter { quantization { delay: 50000 activation_bits: 8 weight_bits: 8 } }""", pipeline_config) exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory, write_inference_graph=True) self._load_inference_graph(inference_graph_path, is_binary=False) has_quant_nodes = False for v in tf.global_variables(): if v.op.name.endswith('act_quant/min'): has_quant_nodes = True break self.assertTrue(has_quant_nodes) def test_export_model_with_all_output_nodes(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) inference_graph = self._load_inference_graph(inference_graph_path) with self.test_session(graph=inference_graph): inference_graph.get_tensor_by_name('image_tensor:0') inference_graph.get_tensor_by_name('detection_boxes:0') inference_graph.get_tensor_by_name('detection_scores:0') inference_graph.get_tensor_by_name('detection_classes:0') inference_graph.get_tensor_by_name('detection_keypoints:0') inference_graph.get_tensor_by_name('detection_masks:0') inference_graph.get_tensor_by_name('num_detections:0') def test_export_model_with_detection_only_nodes(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel(add_detection_masks=False) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) inference_graph = self._load_inference_graph(inference_graph_path) with self.test_session(graph=inference_graph): inference_graph.get_tensor_by_name('image_tensor:0') inference_graph.get_tensor_by_name('detection_boxes:0') inference_graph.get_tensor_by_name('detection_scores:0') inference_graph.get_tensor_by_name('detection_classes:0') inference_graph.get_tensor_by_name('num_detections:0') with self.assertRaises(KeyError): inference_graph.get_tensor_by_name('detection_keypoints:0') inference_graph.get_tensor_by_name('detection_masks:0') def test_export_and_run_inference_with_image_tensor(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) inference_graph = self._load_inference_graph(inference_graph_path) with self.test_session(graph=inference_graph) as sess: image_tensor = inference_graph.get_tensor_by_name('image_tensor:0') boxes = inference_graph.get_tensor_by_name('detection_boxes:0') scores = inference_graph.get_tensor_by_name('detection_scores:0') classes = inference_graph.get_tensor_by_name('detection_classes:0') keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') masks = inference_graph.get_tensor_by_name('detection_masks:0') num_detections = inference_graph.get_tensor_by_name('num_detections:0') (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={image_tensor: np.ones((2, 4, 4, 3)).astype(np.uint8)}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def _create_encoded_image_string(self, image_array_np, encoding_format): od_graph = tf.Graph() with od_graph.as_default(): if encoding_format == 'jpg': encoded_string = tf.image.encode_jpeg(image_array_np) elif encoding_format == 'png': encoded_string = tf.image.encode_png(image_array_np) else: raise ValueError('Supports only the following formats: `jpg`, `png`') with self.test_session(graph=od_graph): return encoded_string.eval() def test_export_and_run_inference_with_encoded_image_string_tensor(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='encoded_image_string_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) inference_graph = self._load_inference_graph(inference_graph_path) jpg_image_str = self._create_encoded_image_string( np.ones((4, 4, 3)).astype(np.uint8), 'jpg') png_image_str = self._create_encoded_image_string( np.ones((4, 4, 3)).astype(np.uint8), 'png') with self.test_session(graph=inference_graph) as sess: image_str_tensor = inference_graph.get_tensor_by_name( 'encoded_image_string_tensor:0') boxes = inference_graph.get_tensor_by_name('detection_boxes:0') scores = inference_graph.get_tensor_by_name('detection_scores:0') classes = inference_graph.get_tensor_by_name('detection_classes:0') keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') masks = inference_graph.get_tensor_by_name('detection_masks:0') num_detections = inference_graph.get_tensor_by_name('num_detections:0') for image_str in [jpg_image_str, png_image_str]: image_str_batch_np = np.hstack([image_str]* 2) (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={image_str_tensor: image_str_batch_np}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def test_raise_runtime_error_on_images_with_different_sizes(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='encoded_image_string_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) inference_graph = self._load_inference_graph(inference_graph_path) large_image = self._create_encoded_image_string( np.ones((4, 4, 3)).astype(np.uint8), 'jpg') small_image = self._create_encoded_image_string( np.ones((2, 2, 3)).astype(np.uint8), 'jpg') image_str_batch_np = np.hstack([large_image, small_image]) with self.test_session(graph=inference_graph) as sess: image_str_tensor = inference_graph.get_tensor_by_name( 'encoded_image_string_tensor:0') boxes = inference_graph.get_tensor_by_name('detection_boxes:0') scores = inference_graph.get_tensor_by_name('detection_scores:0') classes = inference_graph.get_tensor_by_name('detection_classes:0') keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') masks = inference_graph.get_tensor_by_name('detection_masks:0') num_detections = inference_graph.get_tensor_by_name('num_detections:0') with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, 'TensorArray.*shape'): sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={image_str_tensor: image_str_batch_np}) def test_export_and_run_inference_with_tf_example(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='tf_example', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) inference_graph = self._load_inference_graph(inference_graph_path) tf_example_np = np.expand_dims(self._create_tf_example( np.ones((4, 4, 3)).astype(np.uint8)), axis=0) with self.test_session(graph=inference_graph) as sess: tf_example = inference_graph.get_tensor_by_name('tf_example:0') boxes = inference_graph.get_tensor_by_name('detection_boxes:0') scores = inference_graph.get_tensor_by_name('detection_scores:0') classes = inference_graph.get_tensor_by_name('detection_classes:0') keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') masks = inference_graph.get_tensor_by_name('detection_masks:0') num_detections = inference_graph.get_tensor_by_name('num_detections:0') (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={tf_example: tf_example_np}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def test_write_frozen_graph(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') inference_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') tf.gfile.MakeDirs(output_directory) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False detection_model = model_builder.build(pipeline_config.model, is_training=False) outputs, _ = exporter._build_detection_graph( input_type='tf_example', detection_model=detection_model, input_shape=None, output_collection_name='inference_op', graph_hook_fn=None) output_node_names = ','.join(outputs.keys()) saver = tf.train.Saver() input_saver_def = saver.as_saver_def() exporter.freeze_graph_with_def_protos( input_graph_def=tf.get_default_graph().as_graph_def(), input_saver_def=input_saver_def, input_checkpoint=trained_checkpoint_prefix, output_node_names=output_node_names, restore_op_name='save/restore_all', filename_tensor_name='save/Const:0', output_graph=inference_graph_path, clear_devices=True, initializer_nodes='') inference_graph = self._load_inference_graph(inference_graph_path) tf_example_np = np.expand_dims(self._create_tf_example( np.ones((4, 4, 3)).astype(np.uint8)), axis=0) with self.test_session(graph=inference_graph) as sess: tf_example = inference_graph.get_tensor_by_name('tf_example:0') boxes = inference_graph.get_tensor_by_name('detection_boxes:0') scores = inference_graph.get_tensor_by_name('detection_scores:0') classes = inference_graph.get_tensor_by_name('detection_classes:0') keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') masks = inference_graph.get_tensor_by_name('detection_masks:0') num_detections = inference_graph.get_tensor_by_name('num_detections:0') (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={tf_example: tf_example_np}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def test_export_graph_saves_pipeline_file(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=True) output_directory = os.path.join(tmp_dir, 'output') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() exporter.export_inference_graph( input_type='image_tensor', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) expected_pipeline_path = os.path.join( output_directory, 'pipeline.config') self.assertTrue(os.path.exists(expected_pipeline_path)) written_pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() with tf.gfile.GFile(expected_pipeline_path, 'r') as f: proto_str = f.read() text_format.Merge(proto_str, written_pipeline_config) self.assertProtoEquals(pipeline_config, written_pipeline_config) def test_export_saved_model_and_run_inference(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) output_directory = os.path.join(tmp_dir, 'output') saved_model_path = os.path.join(output_directory, 'saved_model') with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='tf_example', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) tf_example_np = np.hstack([self._create_tf_example( np.ones((4, 4, 3)).astype(np.uint8))] * 2) with tf.Graph().as_default() as od_graph: with self.test_session(graph=od_graph) as sess: meta_graph = tf.saved_model.loader.load( sess, [tf.saved_model.tag_constants.SERVING], saved_model_path) signature = meta_graph.signature_def['serving_default'] input_tensor_name = signature.inputs['inputs'].name tf_example = od_graph.get_tensor_by_name(input_tensor_name) boxes = od_graph.get_tensor_by_name( signature.outputs['detection_boxes'].name) scores = od_graph.get_tensor_by_name( signature.outputs['detection_scores'].name) classes = od_graph.get_tensor_by_name( signature.outputs['detection_classes'].name) keypoints = od_graph.get_tensor_by_name( signature.outputs['detection_keypoints'].name) masks = od_graph.get_tensor_by_name( signature.outputs['detection_masks'].name) num_detections = od_graph.get_tensor_by_name( signature.outputs['num_detections'].name) (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={tf_example: tf_example_np}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def test_write_saved_model(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) output_directory = os.path.join(tmp_dir, 'output') saved_model_path = os.path.join(output_directory, 'saved_model') tf.gfile.MakeDirs(output_directory) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False detection_model = model_builder.build(pipeline_config.model, is_training=False) outputs, placeholder_tensor = exporter._build_detection_graph( input_type='tf_example', detection_model=detection_model, input_shape=None, output_collection_name='inference_op', graph_hook_fn=None) output_node_names = ','.join(outputs.keys()) saver = tf.train.Saver() input_saver_def = saver.as_saver_def() frozen_graph_def = exporter.freeze_graph_with_def_protos( input_graph_def=tf.get_default_graph().as_graph_def(), input_saver_def=input_saver_def, input_checkpoint=trained_checkpoint_prefix, output_node_names=output_node_names, restore_op_name='save/restore_all', filename_tensor_name='save/Const:0', output_graph='', clear_devices=True, initializer_nodes='') exporter.write_saved_model( saved_model_path=saved_model_path, frozen_graph_def=frozen_graph_def, inputs=placeholder_tensor, outputs=outputs) tf_example_np = np.hstack([self._create_tf_example( np.ones((4, 4, 3)).astype(np.uint8))] * 2) with tf.Graph().as_default() as od_graph: with self.test_session(graph=od_graph) as sess: meta_graph = tf.saved_model.loader.load( sess, [tf.saved_model.tag_constants.SERVING], saved_model_path) signature = meta_graph.signature_def['serving_default'] input_tensor_name = signature.inputs['inputs'].name tf_example = od_graph.get_tensor_by_name(input_tensor_name) boxes = od_graph.get_tensor_by_name( signature.outputs['detection_boxes'].name) scores = od_graph.get_tensor_by_name( signature.outputs['detection_scores'].name) classes = od_graph.get_tensor_by_name( signature.outputs['detection_classes'].name) keypoints = od_graph.get_tensor_by_name( signature.outputs['detection_keypoints'].name) masks = od_graph.get_tensor_by_name( signature.outputs['detection_masks'].name) num_detections = od_graph.get_tensor_by_name( signature.outputs['num_detections'].name) (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={tf_example: tf_example_np}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def test_export_checkpoint_and_run_inference(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) output_directory = os.path.join(tmp_dir, 'output') model_path = os.path.join(output_directory, 'model.ckpt') meta_graph_path = model_path + '.meta' with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False exporter.export_inference_graph( input_type='tf_example', pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_directory=output_directory) tf_example_np = np.hstack([self._create_tf_example( np.ones((4, 4, 3)).astype(np.uint8))] * 2) with tf.Graph().as_default() as od_graph: with self.test_session(graph=od_graph) as sess: new_saver = tf.train.import_meta_graph(meta_graph_path) new_saver.restore(sess, model_path) tf_example = od_graph.get_tensor_by_name('tf_example:0') boxes = od_graph.get_tensor_by_name('detection_boxes:0') scores = od_graph.get_tensor_by_name('detection_scores:0') classes = od_graph.get_tensor_by_name('detection_classes:0') keypoints = od_graph.get_tensor_by_name('detection_keypoints:0') masks = od_graph.get_tensor_by_name('detection_masks:0') num_detections = od_graph.get_tensor_by_name('num_detections:0') (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={tf_example: tf_example_np}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def test_write_graph_and_checkpoint(self): tmp_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, use_moving_averages=False) output_directory = os.path.join(tmp_dir, 'output') model_path = os.path.join(output_directory, 'model.ckpt') meta_graph_path = model_path + '.meta' tf.gfile.MakeDirs(output_directory) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel( add_detection_keypoints=True, add_detection_masks=True) pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False detection_model = model_builder.build(pipeline_config.model, is_training=False) exporter._build_detection_graph( input_type='tf_example', detection_model=detection_model, input_shape=None, output_collection_name='inference_op', graph_hook_fn=None) saver = tf.train.Saver() input_saver_def = saver.as_saver_def() exporter.write_graph_and_checkpoint( inference_graph_def=tf.get_default_graph().as_graph_def(), model_path=model_path, input_saver_def=input_saver_def, trained_checkpoint_prefix=trained_checkpoint_prefix) tf_example_np = np.hstack([self._create_tf_example( np.ones((4, 4, 3)).astype(np.uint8))] * 2) with tf.Graph().as_default() as od_graph: with self.test_session(graph=od_graph) as sess: new_saver = tf.train.import_meta_graph(meta_graph_path) new_saver.restore(sess, model_path) tf_example = od_graph.get_tensor_by_name('tf_example:0') boxes = od_graph.get_tensor_by_name('detection_boxes:0') scores = od_graph.get_tensor_by_name('detection_scores:0') classes = od_graph.get_tensor_by_name('detection_classes:0') keypoints = od_graph.get_tensor_by_name('detection_keypoints:0') masks = od_graph.get_tensor_by_name('detection_masks:0') num_detections = od_graph.get_tensor_by_name('num_detections:0') (boxes_np, scores_np, classes_np, keypoints_np, masks_np, num_detections_np) = sess.run( [boxes, scores, classes, keypoints, masks, num_detections], feed_dict={tf_example: tf_example_np}) self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) self.assertAllClose(scores_np, [[0.7, 0.6], [0.9, 0.0]]) self.assertAllClose(classes_np, [[1, 2], [2, 1]]) self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) self.assertAllClose(num_detections_np, [2, 1]) def test_rewrite_nn_resize_op(self): g = tf.Graph() with g.as_default(): x = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) y = array_ops.placeholder(dtypes.float32, shape=(8, 20, 20, 8)) s = ops.nearest_neighbor_upsampling(x, 2) t = s + y exporter.rewrite_nn_resize_op() resize_op_found = False for op in g.get_operations(): if op.type == 'ResizeNearestNeighbor': resize_op_found = True self.assertEqual(op.inputs[0], x) self.assertEqual(op.outputs[0].consumers()[0], t.op) break self.assertTrue(resize_op_found) def test_rewrite_nn_resize_op_quantized(self): g = tf.Graph() with g.as_default(): x = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) x_conv = tf.contrib.slim.conv2d(x, 8, 1) y = array_ops.placeholder(dtypes.float32, shape=(8, 20, 20, 8)) s = ops.nearest_neighbor_upsampling(x_conv, 2) t = s + y graph_rewriter_config = graph_rewriter_pb2.GraphRewriter() graph_rewriter_config.quantization.delay = 500000 graph_rewriter_fn = graph_rewriter_builder.build( graph_rewriter_config, is_training=False) graph_rewriter_fn() exporter.rewrite_nn_resize_op(is_quantized=True) resize_op_found = False for op in g.get_operations(): if op.type == 'ResizeNearestNeighbor': resize_op_found = True self.assertEqual(op.inputs[0].op.type, 'FakeQuantWithMinMaxVars') self.assertEqual(op.outputs[0].consumers()[0], t.op) break self.assertTrue(resize_op_found) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/exporter_test.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Exports an SSD detection model to use with tf-lite. See export_tflite_ssd_graph.py for usage. """ import os import tempfile import numpy as np import tensorflow as tf from tensorflow.core.framework import attr_value_pb2 from tensorflow.core.framework import types_pb2 from tensorflow.core.protobuf import saver_pb2 from tensorflow.tools.graph_transforms import TransformGraph from object_detection import exporter from object_detection.builders import graph_rewriter_builder from object_detection.builders import model_builder from object_detection.builders import post_processing_builder from object_detection.core import box_list _DEFAULT_NUM_CHANNELS = 3 _DEFAULT_NUM_COORD_BOX = 4 def get_const_center_size_encoded_anchors(anchors): """Exports center-size encoded anchors as a constant tensor. Args: anchors: a float32 tensor of shape [num_anchors, 4] containing the anchor boxes Returns: encoded_anchors: a float32 constant tensor of shape [num_anchors, 4] containing the anchor boxes. """ anchor_boxlist = box_list.BoxList(anchors) y, x, h, w = anchor_boxlist.get_center_coordinates_and_sizes() num_anchors = y.get_shape().as_list() with tf.Session() as sess: y_out, x_out, h_out, w_out = sess.run([y, x, h, w]) encoded_anchors = tf.constant( np.transpose(np.stack((y_out, x_out, h_out, w_out))), dtype=tf.float32, shape=[num_anchors[0], _DEFAULT_NUM_COORD_BOX], name='anchors') return encoded_anchors def append_postprocessing_op(frozen_graph_def, max_detections, max_classes_per_detection, nms_score_threshold, nms_iou_threshold, num_classes, scale_values, detections_per_class=100, use_regular_nms=False): """Appends postprocessing custom op. Args: frozen_graph_def: Frozen GraphDef for SSD model after freezing the checkpoint max_detections: Maximum number of detections (boxes) to show max_classes_per_detection: Number of classes to display per detection nms_score_threshold: Score threshold used in Non-maximal suppression in post-processing nms_iou_threshold: Intersection-over-union threshold used in Non-maximal suppression in post-processing num_classes: number of classes in SSD detector scale_values: scale values is a dict with following key-value pairs {y_scale: 10, x_scale: 10, h_scale: 5, w_scale: 5} that are used in decode centersize boxes detections_per_class: In regular NonMaxSuppression, number of anchors used for NonMaxSuppression per class use_regular_nms: Flag to set postprocessing op to use Regular NMS instead of Fast NMS. Returns: transformed_graph_def: Frozen GraphDef with postprocessing custom op appended TFLite_Detection_PostProcess custom op node has four outputs: detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box locations detection_classes: a float32 tensor of shape [1, num_boxes] with class indices detection_scores: a float32 tensor of shape [1, num_boxes] with class scores num_boxes: a float32 tensor of size 1 containing the number of detected boxes """ new_output = frozen_graph_def.node.add() new_output.op = 'TFLite_Detection_PostProcess' new_output.name = 'TFLite_Detection_PostProcess' new_output.attr['_output_quantized'].CopyFrom( attr_value_pb2.AttrValue(b=True)) new_output.attr['_output_types'].list.type.extend([ types_pb2.DT_FLOAT, types_pb2.DT_FLOAT, types_pb2.DT_FLOAT, types_pb2.DT_FLOAT ]) new_output.attr['_support_output_type_float_in_quantized_op'].CopyFrom( attr_value_pb2.AttrValue(b=True)) new_output.attr['max_detections'].CopyFrom( attr_value_pb2.AttrValue(i=max_detections)) new_output.attr['max_classes_per_detection'].CopyFrom( attr_value_pb2.AttrValue(i=max_classes_per_detection)) new_output.attr['nms_score_threshold'].CopyFrom( attr_value_pb2.AttrValue(f=nms_score_threshold.pop())) new_output.attr['nms_iou_threshold'].CopyFrom( attr_value_pb2.AttrValue(f=nms_iou_threshold.pop())) new_output.attr['num_classes'].CopyFrom( attr_value_pb2.AttrValue(i=num_classes)) new_output.attr['y_scale'].CopyFrom( attr_value_pb2.AttrValue(f=scale_values['y_scale'].pop())) new_output.attr['x_scale'].CopyFrom( attr_value_pb2.AttrValue(f=scale_values['x_scale'].pop())) new_output.attr['h_scale'].CopyFrom( attr_value_pb2.AttrValue(f=scale_values['h_scale'].pop())) new_output.attr['w_scale'].CopyFrom( attr_value_pb2.AttrValue(f=scale_values['w_scale'].pop())) new_output.attr['detections_per_class'].CopyFrom( attr_value_pb2.AttrValue(i=detections_per_class)) new_output.attr['use_regular_nms'].CopyFrom( attr_value_pb2.AttrValue(b=use_regular_nms)) new_output.input.extend( ['raw_outputs/box_encodings', 'raw_outputs/class_predictions', 'anchors']) # Transform the graph to append new postprocessing op input_names = [] output_names = ['TFLite_Detection_PostProcess'] transforms = ['strip_unused_nodes'] transformed_graph_def = TransformGraph(frozen_graph_def, input_names, output_names, transforms) return transformed_graph_def def export_tflite_graph(pipeline_config, trained_checkpoint_prefix, output_dir, add_postprocessing_op, max_detections, max_classes_per_detection, detections_per_class=100, use_regular_nms=False): """Exports a tflite compatible graph and anchors for ssd detection model. Anchors are written to a tensor and tflite compatible graph is written to output_dir/tflite_graph.pb. Args: pipeline_config: a pipeline.proto object containing the configuration for SSD model to export. trained_checkpoint_prefix: a file prefix for the checkpoint containing the trained parameters of the SSD model. output_dir: A directory to write the tflite graph and anchor file to. add_postprocessing_op: If add_postprocessing_op is true: frozen graph adds a TFLite_Detection_PostProcess custom op max_detections: Maximum number of detections (boxes) to show max_classes_per_detection: Number of classes to display per detection detections_per_class: In regular NonMaxSuppression, number of anchors used for NonMaxSuppression per class use_regular_nms: Flag to set postprocessing op to use Regular NMS instead of Fast NMS. Raises: ValueError: if the pipeline config contains models other than ssd or uses an fixed_shape_resizer and provides a shape as well. """ tf.gfile.MakeDirs(output_dir) if pipeline_config.model.WhichOneof('model') != 'ssd': raise ValueError('Only ssd models are supported in tflite. ' 'Found {} in config'.format( pipeline_config.model.WhichOneof('model'))) num_classes = pipeline_config.model.ssd.num_classes nms_score_threshold = { pipeline_config.model.ssd.post_processing.batch_non_max_suppression. score_threshold } nms_iou_threshold = { pipeline_config.model.ssd.post_processing.batch_non_max_suppression. iou_threshold } scale_values = {} scale_values['y_scale'] = { pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale } scale_values['x_scale'] = { pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale } scale_values['h_scale'] = { pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale } scale_values['w_scale'] = { pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale } image_resizer_config = pipeline_config.model.ssd.image_resizer image_resizer = image_resizer_config.WhichOneof('image_resizer_oneof') num_channels = _DEFAULT_NUM_CHANNELS if image_resizer == 'fixed_shape_resizer': height = image_resizer_config.fixed_shape_resizer.height width = image_resizer_config.fixed_shape_resizer.width if image_resizer_config.fixed_shape_resizer.convert_to_grayscale: num_channels = 1 shape = [1, height, width, num_channels] else: raise ValueError( 'Only fixed_shape_resizer' 'is supported with tflite. Found {}'.format( image_resizer_config.WhichOneof('image_resizer_oneof'))) image = tf.placeholder( tf.float32, shape=shape, name='normalized_input_image_tensor') detection_model = model_builder.build( pipeline_config.model, is_training=False) predicted_tensors = detection_model.predict(image, true_image_shapes=None) # The score conversion occurs before the post-processing custom op _, score_conversion_fn = post_processing_builder.build( pipeline_config.model.ssd.post_processing) class_predictions = score_conversion_fn( predicted_tensors['class_predictions_with_background']) with tf.name_scope('raw_outputs'): # 'raw_outputs/box_encodings': a float32 tensor of shape [1, num_anchors, 4] # containing the encoded box predictions. Note that these are raw # predictions and no Non-Max suppression is applied on them and # no decode center size boxes is applied to them. tf.identity(predicted_tensors['box_encodings'], name='box_encodings') # 'raw_outputs/class_predictions': a float32 tensor of shape # [1, num_anchors, num_classes] containing the class scores for each anchor # after applying score conversion. tf.identity(class_predictions, name='class_predictions') # 'anchors': a float32 tensor of shape # [4, num_anchors] containing the anchors as a constant node. tf.identity( get_const_center_size_encoded_anchors(predicted_tensors['anchors']), name='anchors') # Add global step to the graph, so we know the training step number when we # evaluate the model. tf.train.get_or_create_global_step() # graph rewriter is_quantized = pipeline_config.HasField('graph_rewriter') if is_quantized: graph_rewriter_config = pipeline_config.graph_rewriter graph_rewriter_fn = graph_rewriter_builder.build( graph_rewriter_config, is_training=False) graph_rewriter_fn() if pipeline_config.model.ssd.feature_extractor.HasField('fpn'): exporter.rewrite_nn_resize_op(is_quantized) # freeze the graph saver_kwargs = {} if pipeline_config.eval_config.use_moving_averages: saver_kwargs['write_version'] = saver_pb2.SaverDef.V1 moving_average_checkpoint = tempfile.NamedTemporaryFile() exporter.replace_variable_values_with_moving_averages( tf.get_default_graph(), trained_checkpoint_prefix, moving_average_checkpoint.name) checkpoint_to_use = moving_average_checkpoint.name else: checkpoint_to_use = trained_checkpoint_prefix saver = tf.train.Saver(**saver_kwargs) input_saver_def = saver.as_saver_def() frozen_graph_def = exporter.freeze_graph_with_def_protos( input_graph_def=tf.get_default_graph().as_graph_def(), input_saver_def=input_saver_def, input_checkpoint=checkpoint_to_use, output_node_names=','.join([ 'raw_outputs/box_encodings', 'raw_outputs/class_predictions', 'anchors' ]), restore_op_name='save/restore_all', filename_tensor_name='save/Const:0', clear_devices=True, output_graph='', initializer_nodes='') # Add new operation to do post processing in a custom op (TF Lite only) if add_postprocessing_op: transformed_graph_def = append_postprocessing_op( frozen_graph_def, max_detections, max_classes_per_detection, nms_score_threshold, nms_iou_threshold, num_classes, scale_values, detections_per_class, use_regular_nms) else: # Return frozen without adding post-processing custom op transformed_graph_def = frozen_graph_def binary_graph = os.path.join(output_dir, 'tflite_graph.pb') with tf.gfile.GFile(binary_graph, 'wb') as f: f.write(transformed_graph_def.SerializeToString()) txt_graph = os.path.join(output_dir, 'tflite_graph.pbtxt') with tf.gfile.GFile(txt_graph, 'w') as f: f.write(str(transformed_graph_def))
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/export_tflite_ssd_graph_lib.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== # # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. r"""Constructs model, inputs, and training environment.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import copy import functools import os import tensorflow as tf import horovod.tensorflow as hvd from object_detection import eval_util from object_detection import exporter as exporter_lib from object_detection import inputs from object_detection.builders import graph_rewriter_builder from object_detection.builders import model_builder from object_detection.builders import optimizer_builder from object_detection.core import standard_fields as fields from object_detection.utils import config_util from object_detection.utils import label_map_util from object_detection.utils import shape_utils from object_detection.utils import variables_helper from object_detection.utils import visualization_utils as vis_utils # A map of names to methods that help build the model. MODEL_BUILD_UTIL_MAP = { 'get_configs_from_pipeline_file': config_util.get_configs_from_pipeline_file, 'create_pipeline_proto_from_configs': config_util.create_pipeline_proto_from_configs, 'merge_external_params_with_configs': config_util.merge_external_params_with_configs, 'create_train_input_fn': inputs.create_train_input_fn, 'create_eval_input_fn': inputs.create_eval_input_fn, 'create_predict_input_fn': inputs.create_predict_input_fn, } def _prepare_groundtruth_for_eval(detection_model, class_agnostic, max_number_of_boxes): """Extracts groundtruth data from detection_model and prepares it for eval. Args: detection_model: A `DetectionModel` object. class_agnostic: Whether the detections are class_agnostic. max_number_of_boxes: Max number of groundtruth boxes. Returns: A tuple of: groundtruth: Dictionary with the following fields: 'groundtruth_boxes': [batch_size, num_boxes, 4] float32 tensor of boxes, in normalized coordinates. 'groundtruth_classes': [batch_size, num_boxes] int64 tensor of 1-indexed classes. 'groundtruth_masks': 4D float32 tensor of instance masks (if provided in groundtruth) 'groundtruth_is_crowd': [batch_size, num_boxes] bool tensor indicating is_crowd annotations (if provided in groundtruth). 'num_groundtruth_boxes': [batch_size] tensor containing the maximum number of groundtruth boxes per image.. class_agnostic: Boolean indicating whether detections are class agnostic. """ input_data_fields = fields.InputDataFields() groundtruth_boxes = tf.stack( detection_model.groundtruth_lists(fields.BoxListFields.boxes)) groundtruth_boxes_shape = tf.shape(groundtruth_boxes) # For class-agnostic models, groundtruth one-hot encodings collapse to all # ones. if class_agnostic: groundtruth_classes_one_hot = tf.ones( [groundtruth_boxes_shape[0], groundtruth_boxes_shape[1], 1]) else: groundtruth_classes_one_hot = tf.stack( detection_model.groundtruth_lists(fields.BoxListFields.classes)) label_id_offset = 1 # Applying label id offset (b/63711816) groundtruth_classes = ( tf.argmax(groundtruth_classes_one_hot, axis=2) + label_id_offset) groundtruth = { input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes } if detection_model.groundtruth_has_field(fields.BoxListFields.masks): groundtruth[input_data_fields.groundtruth_instance_masks] = tf.stack( detection_model.groundtruth_lists(fields.BoxListFields.masks)) if detection_model.groundtruth_has_field(fields.BoxListFields.is_crowd): groundtruth[input_data_fields.groundtruth_is_crowd] = tf.stack( detection_model.groundtruth_lists(fields.BoxListFields.is_crowd)) groundtruth[input_data_fields.num_groundtruth_boxes] = ( tf.tile([max_number_of_boxes], multiples=[groundtruth_boxes_shape[0]])) return groundtruth def unstack_batch(tensor_dict, unpad_groundtruth_tensors=True): """Unstacks all tensors in `tensor_dict` along 0th dimension. Unstacks tensor from the tensor dict along 0th dimension and returns a tensor_dict containing values that are lists of unstacked, unpadded tensors. Tensors in the `tensor_dict` are expected to be of one of the three shapes: 1. [batch_size] 2. [batch_size, height, width, channels] 3. [batch_size, num_boxes, d1, d2, ... dn] When unpad_groundtruth_tensors is set to true, unstacked tensors of form 3 above are sliced along the `num_boxes` dimension using the value in tensor field.InputDataFields.num_groundtruth_boxes. Note that this function has a static list of input data fields and has to be kept in sync with the InputDataFields defined in core/standard_fields.py Args: tensor_dict: A dictionary of batched groundtruth tensors. unpad_groundtruth_tensors: Whether to remove padding along `num_boxes` dimension of the groundtruth tensors. Returns: A dictionary where the keys are from fields.InputDataFields and values are a list of unstacked (optionally unpadded) tensors. Raises: ValueError: If unpad_tensors is True and `tensor_dict` does not contain `num_groundtruth_boxes` tensor. """ unbatched_tensor_dict = { key: tf.unstack(tensor) for key, tensor in tensor_dict.items() } if unpad_groundtruth_tensors: if (fields.InputDataFields.num_groundtruth_boxes not in unbatched_tensor_dict): raise ValueError('`num_groundtruth_boxes` not found in tensor_dict. ' 'Keys available: {}'.format( unbatched_tensor_dict.keys())) unbatched_unpadded_tensor_dict = {} unpad_keys = set([ # List of input data fields that are padded along the num_boxes # dimension. This list has to be kept in sync with InputDataFields in # standard_fields.py. fields.InputDataFields.groundtruth_instance_masks, fields.InputDataFields.groundtruth_classes, fields.InputDataFields.groundtruth_boxes, fields.InputDataFields.groundtruth_keypoints, fields.InputDataFields.groundtruth_group_of, fields.InputDataFields.groundtruth_difficult, fields.InputDataFields.groundtruth_is_crowd, fields.InputDataFields.groundtruth_area, fields.InputDataFields.groundtruth_weights ]).intersection(set(unbatched_tensor_dict.keys())) for key in unpad_keys: unpadded_tensor_list = [] for num_gt, padded_tensor in zip( unbatched_tensor_dict[fields.InputDataFields.num_groundtruth_boxes], unbatched_tensor_dict[key]): tensor_shape = shape_utils.combined_static_and_dynamic_shape( padded_tensor) slice_begin = tf.zeros([len(tensor_shape)], dtype=tf.int32) slice_size = tf.stack( [num_gt] + [-1 if dim is None else dim for dim in tensor_shape[1:]]) unpadded_tensor = tf.slice(padded_tensor, slice_begin, slice_size) unpadded_tensor_list.append(unpadded_tensor) unbatched_unpadded_tensor_dict[key] = unpadded_tensor_list unbatched_tensor_dict.update(unbatched_unpadded_tensor_dict) return unbatched_tensor_dict def create_model_fn(detection_model_fn, configs, hparams, use_tpu=False): """Creates a model function for `Estimator`. Args: detection_model_fn: Function that returns a `DetectionModel` instance. configs: Dictionary of pipeline config objects. hparams: `HParams` object. use_tpu: Boolean indicating whether model should be constructed for use on TPU. Returns: `model_fn` for `Estimator`. """ train_config = configs['train_config'] eval_input_config = configs['eval_input_config'] eval_config = configs['eval_config'] def model_fn(features, labels, mode, params=None): """Constructs the object detection model. Args: features: Dictionary of feature tensors, returned from `input_fn`. labels: Dictionary of groundtruth tensors if mode is TRAIN or EVAL, otherwise None. mode: Mode key from tf.estimator.ModeKeys. params: Parameter dictionary passed from the estimator. Returns: An `EstimatorSpec` that encapsulates the model and its serving configurations. """ params = params or {} total_loss, train_op, detections, export_outputs = None, None, None, None is_training = mode == tf.estimator.ModeKeys.TRAIN # Make sure to set the Keras learning phase. True during training, # False for inference. tf.keras.backend.set_learning_phase(is_training) detection_model = detection_model_fn( is_training=is_training, add_summaries=(not use_tpu)) scaffold_fn = None if mode == tf.estimator.ModeKeys.TRAIN: labels = unstack_batch( labels, unpad_groundtruth_tensors=train_config.unpad_groundtruth_tensors) elif mode == tf.estimator.ModeKeys.EVAL: # For evaling on train data, it is necessary to check whether groundtruth # must be unpadded. boxes_shape = ( labels[fields.InputDataFields.groundtruth_boxes].get_shape() .as_list()) unpad_groundtruth_tensors = boxes_shape[1] is not None and not use_tpu labels = unstack_batch( labels, unpad_groundtruth_tensors=unpad_groundtruth_tensors) if mode in (tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL): gt_boxes_list = labels[fields.InputDataFields.groundtruth_boxes] gt_classes_list = labels[fields.InputDataFields.groundtruth_classes] gt_masks_list = None if fields.InputDataFields.groundtruth_instance_masks in labels: gt_masks_list = labels[ fields.InputDataFields.groundtruth_instance_masks] gt_keypoints_list = None if fields.InputDataFields.groundtruth_keypoints in labels: gt_keypoints_list = labels[fields.InputDataFields.groundtruth_keypoints] gt_weights_list = None if fields.InputDataFields.groundtruth_weights in labels: gt_weights_list = labels[fields.InputDataFields.groundtruth_weights] gt_confidences_list = None if fields.InputDataFields.groundtruth_confidences in labels: gt_confidences_list = labels[ fields.InputDataFields.groundtruth_confidences] gt_is_crowd_list = None if fields.InputDataFields.groundtruth_is_crowd in labels: gt_is_crowd_list = labels[fields.InputDataFields.groundtruth_is_crowd] detection_model.provide_groundtruth( groundtruth_boxes_list=gt_boxes_list, groundtruth_classes_list=gt_classes_list, groundtruth_confidences_list=gt_confidences_list, groundtruth_masks_list=gt_masks_list, groundtruth_keypoints_list=gt_keypoints_list, groundtruth_weights_list=gt_weights_list, groundtruth_is_crowd_list=gt_is_crowd_list) preprocessed_images = features[fields.InputDataFields.image] if use_tpu and train_config.use_bfloat16: with tf.contrib.tpu.bfloat16_scope(): prediction_dict = detection_model.predict( preprocessed_images, features[fields.InputDataFields.true_image_shape]) for k, v in prediction_dict.items(): if v.dtype == tf.bfloat16: prediction_dict[k] = tf.cast(v, tf.float32) else: prediction_dict = detection_model.predict( preprocessed_images, features[fields.InputDataFields.true_image_shape]) if mode in (tf.estimator.ModeKeys.EVAL, tf.estimator.ModeKeys.PREDICT): detections = detection_model.postprocess( prediction_dict, features[fields.InputDataFields.true_image_shape]) if mode == tf.estimator.ModeKeys.TRAIN: if train_config.fine_tune_checkpoint and hparams.load_pretrained: if not train_config.fine_tune_checkpoint_type: # train_config.from_detection_checkpoint field is deprecated. For # backward compatibility, set train_config.fine_tune_checkpoint_type # based on train_config.from_detection_checkpoint. if train_config.from_detection_checkpoint: train_config.fine_tune_checkpoint_type = 'detection' else: train_config.fine_tune_checkpoint_type = 'classification' asg_map = detection_model.restore_map( fine_tune_checkpoint_type=train_config.fine_tune_checkpoint_type, load_all_detection_checkpoint_vars=( train_config.load_all_detection_checkpoint_vars)) available_var_map = ( variables_helper.get_variables_available_in_checkpoint( asg_map, train_config.fine_tune_checkpoint, include_global_step=False)) if use_tpu: def tpu_scaffold(): tf.train.init_from_checkpoint(train_config.fine_tune_checkpoint, available_var_map) return tf.train.Scaffold() scaffold_fn = tpu_scaffold else: tf.train.init_from_checkpoint(train_config.fine_tune_checkpoint, available_var_map) if mode in (tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL): losses_dict = detection_model.loss( prediction_dict, features[fields.InputDataFields.true_image_shape]) losses = [loss_tensor for loss_tensor in losses_dict.values()] if train_config.add_regularization_loss: regularization_losses = detection_model.regularization_losses() if regularization_losses: regularization_loss = tf.add_n( regularization_losses, name='regularization_loss') losses.append(regularization_loss) losses_dict['Loss/regularization_loss'] = regularization_loss total_loss = tf.add_n(losses, name='total_loss') losses_dict['Loss/total_loss'] = total_loss if 'graph_rewriter_config' in configs: graph_rewriter_fn = graph_rewriter_builder.build( configs['graph_rewriter_config'], is_training=is_training) graph_rewriter_fn() # TODO(rathodv): Stop creating optimizer summary vars in EVAL mode once we # can write learning rate summaries on TPU without host calls. global_step = tf.train.get_or_create_global_step() training_optimizer, optimizer_summary_vars = optimizer_builder.build( train_config.optimizer) if mode == tf.estimator.ModeKeys.TRAIN: if use_tpu: training_optimizer = tf.contrib.tpu.CrossShardOptimizer( training_optimizer) # Optionally freeze some layers by setting their gradients to be zero. trainable_variables = None include_variables = ( train_config.update_trainable_variables if train_config.update_trainable_variables else None) exclude_variables = ( train_config.freeze_variables if train_config.freeze_variables else None) trainable_variables = tf.contrib.framework.filter_variables( tf.trainable_variables(), include_patterns=include_variables, exclude_patterns=exclude_variables) clip_gradients_value = None if train_config.gradient_clipping_by_norm > 0: clip_gradients_value = train_config.gradient_clipping_by_norm if not use_tpu: for var in optimizer_summary_vars: tf.summary.scalar(var.op.name, var) summaries = [] if use_tpu else None if train_config.summarize_gradients: summaries = ['gradients', 'gradient_norm', 'global_gradient_norm'] train_op = tf.contrib.layers.optimize_loss( loss=total_loss, global_step=global_step, learning_rate=None, clip_gradients=clip_gradients_value, optimizer=training_optimizer, update_ops=detection_model.updates(), variables=trainable_variables, summaries=summaries, name='') # Preventing scope prefix on all variables. if mode == tf.estimator.ModeKeys.PREDICT: exported_output = exporter_lib.add_output_tensor_nodes(detections) export_outputs = { tf.saved_model.signature_constants.PREDICT_METHOD_NAME: tf.estimator.export.PredictOutput(exported_output) } eval_metric_ops = None scaffold = None if mode == tf.estimator.ModeKeys.EVAL: class_agnostic = ( fields.DetectionResultFields.detection_classes not in detections) groundtruth = _prepare_groundtruth_for_eval( detection_model, class_agnostic, eval_input_config.max_number_of_boxes) use_original_images = fields.InputDataFields.original_image in features if use_original_images: eval_images = features[fields.InputDataFields.original_image] true_image_shapes = tf.slice( features[fields.InputDataFields.true_image_shape], [0, 0], [-1, 3]) original_image_spatial_shapes = features[fields.InputDataFields .original_image_spatial_shape] else: eval_images = features[fields.InputDataFields.image] true_image_shapes = None original_image_spatial_shapes = None eval_dict = eval_util.result_dict_for_batched_example( eval_images, features[inputs.HASH_KEY], detections, groundtruth, class_agnostic=class_agnostic, scale_to_absolute=True, original_image_spatial_shapes=original_image_spatial_shapes, true_image_shapes=true_image_shapes) if class_agnostic: category_index = label_map_util.create_class_agnostic_category_index() else: category_index = label_map_util.create_category_index_from_labelmap( eval_input_config.label_map_path) vis_metric_ops = None if not use_tpu and use_original_images: eval_metric_op_vis = vis_utils.VisualizeSingleFrameDetections( category_index, max_examples_to_draw=eval_config.num_visualizations, max_boxes_to_draw=eval_config.max_num_boxes_to_visualize, min_score_thresh=eval_config.min_score_threshold, use_normalized_coordinates=False) vis_metric_ops = eval_metric_op_vis.get_estimator_eval_metric_ops( eval_dict) # Eval metrics on a single example. eval_metric_ops = eval_util.get_eval_metric_ops_for_evaluators( eval_config, list(category_index.values()), eval_dict) for loss_key, loss_tensor in iter(losses_dict.items()): eval_metric_ops[loss_key] = tf.metrics.mean(loss_tensor) for var in optimizer_summary_vars: eval_metric_ops[var.op.name] = (var, tf.no_op()) if vis_metric_ops is not None: eval_metric_ops.update(vis_metric_ops) eval_metric_ops = {str(k): v for k, v in eval_metric_ops.items()} if eval_config.use_moving_averages: variable_averages = tf.train.ExponentialMovingAverage(0.0) variables_to_restore = variable_averages.variables_to_restore() keep_checkpoint_every_n_hours = ( train_config.keep_checkpoint_every_n_hours) saver = tf.train.Saver( variables_to_restore, keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours) scaffold = tf.train.Scaffold(saver=saver) # EVAL executes on CPU, so use regular non-TPU EstimatorSpec. if use_tpu and mode != tf.estimator.ModeKeys.EVAL: return tf.contrib.tpu.TPUEstimatorSpec( mode=mode, scaffold_fn=scaffold_fn, predictions=detections, loss=total_loss, train_op=train_op, eval_metrics=eval_metric_ops, export_outputs=export_outputs) else: if scaffold is None: keep_checkpoint_every_n_hours = ( train_config.keep_checkpoint_every_n_hours) saver = tf.train.Saver( sharded=True, keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours, save_relative_paths=True) tf.add_to_collection(tf.GraphKeys.SAVERS, saver) scaffold = tf.train.Scaffold(saver=saver) return tf.estimator.EstimatorSpec( mode=mode, predictions=detections, loss=total_loss, train_op=train_op, eval_metric_ops=eval_metric_ops, export_outputs=export_outputs, scaffold=scaffold) return model_fn def create_estimator_and_inputs(run_config, hparams, pipeline_config_path, eval_count=1, config_override=None, train_steps=None, sample_1_of_n_eval_examples=1, sample_1_of_n_eval_on_train_examples=1, model_fn_creator=create_model_fn, use_tpu_estimator=False, use_tpu=False, num_shards=1, params=None, override_eval_num_epochs=True, save_final_config=False, **kwargs): """Creates `Estimator`, input functions, and steps. Args: run_config: A `RunConfig`. hparams: A `HParams`. pipeline_config_path: A path to a pipeline config file. config_override: A pipeline_pb2.TrainEvalPipelineConfig text proto to override the config from `pipeline_config_path`. train_steps: Number of training steps. If None, the number of training steps is set from the `TrainConfig` proto. sample_1_of_n_eval_examples: Integer representing how often an eval example should be sampled. If 1, will sample all examples. sample_1_of_n_eval_on_train_examples: Similar to `sample_1_of_n_eval_examples`, except controls the sampling of training data for evaluation. model_fn_creator: A function that creates a `model_fn` for `Estimator`. Follows the signature: * Args: * `detection_model_fn`: Function that returns `DetectionModel` instance. * `configs`: Dictionary of pipeline config objects. * `hparams`: `HParams` object. * Returns: `model_fn` for `Estimator`. use_tpu_estimator: Whether a `TPUEstimator` should be returned. If False, an `Estimator` will be returned. use_tpu: Boolean, whether training and evaluation should run on TPU. Only used if `use_tpu_estimator` is True. num_shards: Number of shards (TPU cores). Only used if `use_tpu_estimator` is True. params: Parameter dictionary passed from the estimator. Only used if `use_tpu_estimator` is True. override_eval_num_epochs: Whether to overwrite the number of epochs to 1 for eval_input. save_final_config: Whether to save final config (obtained after applying overrides) to `estimator.model_dir`. **kwargs: Additional keyword arguments for configuration override. Returns: A dictionary with the following fields: 'estimator': An `Estimator` or `TPUEstimator`. 'train_input_fn': A training input function. 'eval_input_fns': A list of all evaluation input functions. 'eval_input_names': A list of names for each evaluation input. 'eval_on_train_input_fn': An evaluation-on-train input function. 'predict_input_fn': A prediction input function. 'train_steps': Number of training steps. Either directly from input or from configuration. 'train_batch_size': train batch size per GPU """ get_configs_from_pipeline_file = MODEL_BUILD_UTIL_MAP[ 'get_configs_from_pipeline_file'] merge_external_params_with_configs = MODEL_BUILD_UTIL_MAP[ 'merge_external_params_with_configs'] create_pipeline_proto_from_configs = MODEL_BUILD_UTIL_MAP[ 'create_pipeline_proto_from_configs'] create_train_input_fn = MODEL_BUILD_UTIL_MAP['create_train_input_fn'] create_eval_input_fn = MODEL_BUILD_UTIL_MAP['create_eval_input_fn'] create_predict_input_fn = MODEL_BUILD_UTIL_MAP['create_predict_input_fn'] configs = get_configs_from_pipeline_file(pipeline_config_path, config_override=config_override) kwargs.update({ 'train_steps': train_steps, 'sample_1_of_n_eval_examples': sample_1_of_n_eval_examples }) if override_eval_num_epochs: kwargs.update({'eval_num_epochs': 1}) tf.logging.warning( 'Forced number of epochs for all eval validations to be 1.') configs = merge_external_params_with_configs( configs, hparams, kwargs_dict=kwargs) model_config = configs['model'] train_config = configs['train_config'] train_input_config = configs['train_input_config'] eval_config = configs['eval_config'] eval_input_configs = configs['eval_input_configs'] eval_on_train_input_config = copy.deepcopy(train_input_config) eval_on_train_input_config.sample_1_of_n_examples = ( sample_1_of_n_eval_on_train_examples) if override_eval_num_epochs and eval_on_train_input_config.num_epochs != 1: tf.logging.warning('Expected number of evaluation epochs is 1, but ' 'instead encountered `eval_on_train_input_config' '.num_epochs` = ' '{}. Overwriting `num_epochs` to 1.'.format( eval_on_train_input_config.num_epochs)) eval_on_train_input_config.num_epochs = 1 # update train_steps from config but only when non-zero value is provided if train_steps is None and train_config.num_steps != 0: train_steps = train_config.num_steps detection_model_fn = functools.partial( model_builder.build, model_config=model_config) # Create the input functions for TRAIN/EVAL/PREDICT. train_input_fn = create_train_input_fn( train_config=train_config, train_input_config=train_input_config, model_config=model_config) eval_input_fns = [ create_eval_input_fn( eval_config=eval_config, eval_input_config=eval_input_config, model_config=model_config) for eval_input_config in eval_input_configs ] eval_input_names = [ eval_input_config.name for eval_input_config in eval_input_configs ] eval_on_train_input_fn = create_eval_input_fn( eval_config=eval_config, eval_input_config=eval_on_train_input_config, model_config=model_config) predict_input_fn = create_predict_input_fn( model_config=model_config, predict_input_config=eval_input_configs[0]) export_to_tpu = hparams.get('export_to_tpu', False) tf.logging.info('create_estimator_and_inputs: use_tpu %s, export_to_tpu %s', use_tpu, export_to_tpu) model_fn = model_fn_creator(detection_model_fn, configs, hparams, use_tpu) run_config = tf.estimator.RunConfig(model_dir=run_config.model_dir, session_config=run_config.session_config, save_checkpoints_steps=train_steps // eval_count) if use_tpu_estimator: estimator = tf.contrib.tpu.TPUEstimator( model_fn=model_fn, train_batch_size=train_config.batch_size, # For each core, only batch size 1 is supported for eval. eval_batch_size=num_shards * 1 if use_tpu else 1, use_tpu=use_tpu, config=run_config, # TODO(lzc): Remove conditional after CMLE moves to TF 1.9 params=params if params else {}) else: estimator = tf.estimator.Estimator(model_fn=model_fn, config=run_config) # Write the as-run pipeline config to disk. if run_config.is_chief and save_final_config: pipeline_config_final = create_pipeline_proto_from_configs(configs) config_util.save_pipeline_config(pipeline_config_final, estimator.model_dir) return dict( estimator=estimator, train_input_fn=train_input_fn, eval_input_fns=eval_input_fns, eval_input_names=eval_input_names, eval_on_train_input_fn=eval_on_train_input_fn, predict_input_fn=predict_input_fn, train_steps=train_steps, train_batch_size=train_config.batch_size) def create_train_and_eval_specs(train_input_fn, eval_input_fns, eval_on_train_input_fn, predict_input_fn, train_steps, eval_on_train_data=False, final_exporter_name='Servo', eval_spec_names=None): """Creates a `TrainSpec` and `EvalSpec`s. Args: train_input_fn: Function that produces features and labels on train data. eval_input_fns: A list of functions that produce features and labels on eval data. eval_on_train_input_fn: Function that produces features and labels for evaluation on train data. predict_input_fn: Function that produces features for inference. train_steps: Number of training steps. eval_on_train_data: Whether to evaluate model on training data. Default is False. final_exporter_name: String name given to `FinalExporter`. eval_spec_names: A list of string names for each `EvalSpec`. Returns: Tuple of `TrainSpec` and list of `EvalSpecs`. If `eval_on_train_data` is True, the last `EvalSpec` in the list will correspond to training data. The rest EvalSpecs in the list are evaluation datas. """ train_spec = tf.estimator.TrainSpec( input_fn=train_input_fn, max_steps=train_steps // hvd.size(), # no `steps' attribute; only max_steps available hooks=[hvd.BroadcastGlobalVariablesHook(0)]) if eval_spec_names is None: eval_spec_names = [str(i) for i in range(len(eval_input_fns))] eval_specs = [] for index, (eval_spec_name, eval_input_fn) in enumerate( zip(eval_spec_names, eval_input_fns)): # Uses final_exporter_name as exporter_name for the first eval spec for # backward compatibility. if index == 0: exporter_name = final_exporter_name else: exporter_name = '{}_{}'.format(final_exporter_name, eval_spec_name) exporter = tf.estimator.FinalExporter( name=exporter_name, serving_input_receiver_fn=predict_input_fn) eval_specs.append( tf.estimator.EvalSpec( name=eval_spec_name, input_fn=eval_input_fn, steps=None, exporters=exporter)) if eval_on_train_data: eval_specs.append( tf.estimator.EvalSpec( name='eval_on_train', input_fn=eval_on_train_input_fn, steps=None)) return train_spec, eval_specs def continuous_eval(estimator, model_dir, input_fn, train_steps, name): """Perform continuous evaluation on checkpoints written to a model directory. Args: estimator: Estimator object to use for evaluation. model_dir: Model directory to read checkpoints for continuous evaluation. input_fn: Input function to use for evaluation. train_steps: Number of training steps. This is used to infer the last checkpoint and stop evaluation loop. name: Namescope for eval summary. """ def terminate_eval(): tf.logging.info('Terminating eval after 180 seconds of no checkpoints') return True for ckpt in tf.contrib.training.checkpoints_iterator( model_dir, min_interval_secs=180, timeout=None, timeout_fn=terminate_eval): tf.logging.info('Starting Evaluation.') try: eval_results = estimator.evaluate( input_fn=input_fn, steps=None, checkpoint_path=ckpt, name=name) tf.logging.info('Eval results: %s' % eval_results) # Terminate eval job when final checkpoint is reached current_step = int(os.path.basename(ckpt).split('-')[1]) if current_step >= train_steps: tf.logging.info( 'Evaluation finished after training step %d' % current_step) break except tf.errors.NotFoundError: tf.logging.info( 'Checkpoint %s no longer exists, skipping checkpoint' % ckpt) def populate_experiment(run_config, hparams, pipeline_config_path, train_steps=None, eval_steps=None, model_fn_creator=create_model_fn, **kwargs): """Populates an `Experiment` object. EXPERIMENT CLASS IS DEPRECATED. Please switch to tf.estimator.train_and_evaluate. As an example, see model_main.py. Args: run_config: A `RunConfig`. hparams: A `HParams`. pipeline_config_path: A path to a pipeline config file. train_steps: Number of training steps. If None, the number of training steps is set from the `TrainConfig` proto. eval_steps: Number of evaluation steps per evaluation cycle. If None, the number of evaluation steps is set from the `EvalConfig` proto. model_fn_creator: A function that creates a `model_fn` for `Estimator`. Follows the signature: * Args: * `detection_model_fn`: Function that returns `DetectionModel` instance. * `configs`: Dictionary of pipeline config objects. * `hparams`: `HParams` object. * Returns: `model_fn` for `Estimator`. **kwargs: Additional keyword arguments for configuration override. Returns: An `Experiment` that defines all aspects of training, evaluation, and export. """ tf.logging.warning('Experiment is being deprecated. Please use ' 'tf.estimator.train_and_evaluate(). See model_main.py for ' 'an example.') train_and_eval_dict = create_estimator_and_inputs( run_config, hparams, pipeline_config_path, train_steps=train_steps, eval_steps=eval_steps, model_fn_creator=model_fn_creator, save_final_config=True, **kwargs) estimator = train_and_eval_dict['estimator'] train_input_fn = train_and_eval_dict['train_input_fn'] eval_input_fns = train_and_eval_dict['eval_input_fns'] predict_input_fn = train_and_eval_dict['predict_input_fn'] train_steps = train_and_eval_dict['train_steps'] export_strategies = [ tf.contrib.learn.utils.saved_model_export_utils.make_export_strategy( serving_input_fn=predict_input_fn) ] return tf.contrib.learn.Experiment( estimator=estimator, train_input_fn=train_input_fn, eval_input_fn=eval_input_fns[0], train_steps=train_steps, eval_steps=None, export_strategies=export_strategies, eval_delay_secs=120, )
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/model_lib.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object detection model library.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import os import numpy as np import tensorflow as tf from tensorflow.contrib.tpu.python.tpu import tpu_config from tensorflow.contrib.tpu.python.tpu import tpu_estimator from object_detection import inputs from object_detection import model_hparams from object_detection import model_lib from object_detection.builders import model_builder from object_detection.core import standard_fields as fields from object_detection.utils import config_util # Model for test. Options are: # 'ssd_inception_v2_pets', 'faster_rcnn_resnet50_pets' MODEL_NAME_FOR_TEST = 'ssd_inception_v2_pets' def _get_data_path(): """Returns an absolute path to TFRecord file.""" return os.path.join(tf.resource_loader.get_data_files_path(), 'test_data', 'pets_examples.record') def get_pipeline_config_path(model_name): """Returns path to the local pipeline config file.""" return os.path.join(tf.resource_loader.get_data_files_path(), 'samples', 'configs', model_name + '.config') def _get_labelmap_path(): """Returns an absolute path to label map file.""" return os.path.join(tf.resource_loader.get_data_files_path(), 'data', 'pet_label_map.pbtxt') def _get_configs_for_model(model_name): """Returns configurations for model.""" filename = get_pipeline_config_path(model_name) data_path = _get_data_path() label_map_path = _get_labelmap_path() configs = config_util.get_configs_from_pipeline_file(filename) override_dict = { 'train_input_path': data_path, 'eval_input_path': data_path, 'label_map_path': label_map_path } configs = config_util.merge_external_params_with_configs( configs, kwargs_dict=override_dict) return configs def _make_initializable_iterator(dataset): """Creates an iterator, and initializes tables. Args: dataset: A `tf.data.Dataset` object. Returns: A `tf.data.Iterator`. """ iterator = dataset.make_initializable_iterator() tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer) return iterator class ModelLibTest(tf.test.TestCase): @classmethod def setUpClass(cls): tf.reset_default_graph() def _assert_model_fn_for_train_eval(self, configs, mode, class_agnostic=False): model_config = configs['model'] train_config = configs['train_config'] with tf.Graph().as_default(): if mode == 'train': features, labels = _make_initializable_iterator( inputs.create_train_input_fn(configs['train_config'], configs['train_input_config'], configs['model'])()).get_next() model_mode = tf.estimator.ModeKeys.TRAIN batch_size = train_config.batch_size elif mode == 'eval': features, labels = _make_initializable_iterator( inputs.create_eval_input_fn(configs['eval_config'], configs['eval_input_config'], configs['model'])()).get_next() model_mode = tf.estimator.ModeKeys.EVAL batch_size = 1 elif mode == 'eval_on_train': features, labels = _make_initializable_iterator( inputs.create_eval_input_fn(configs['eval_config'], configs['train_input_config'], configs['model'])()).get_next() model_mode = tf.estimator.ModeKeys.EVAL batch_size = 1 detection_model_fn = functools.partial( model_builder.build, model_config=model_config, is_training=True) hparams = model_hparams.create_hparams( hparams_overrides='load_pretrained=false') model_fn = model_lib.create_model_fn(detection_model_fn, configs, hparams) estimator_spec = model_fn(features, labels, model_mode) self.assertIsNotNone(estimator_spec.loss) self.assertIsNotNone(estimator_spec.predictions) if mode == 'eval' or mode == 'eval_on_train': if class_agnostic: self.assertNotIn('detection_classes', estimator_spec.predictions) else: detection_classes = estimator_spec.predictions['detection_classes'] self.assertEqual(batch_size, detection_classes.shape.as_list()[0]) self.assertEqual(tf.float32, detection_classes.dtype) detection_boxes = estimator_spec.predictions['detection_boxes'] detection_scores = estimator_spec.predictions['detection_scores'] num_detections = estimator_spec.predictions['num_detections'] self.assertEqual(batch_size, detection_boxes.shape.as_list()[0]) self.assertEqual(tf.float32, detection_boxes.dtype) self.assertEqual(batch_size, detection_scores.shape.as_list()[0]) self.assertEqual(tf.float32, detection_scores.dtype) self.assertEqual(tf.float32, num_detections.dtype) if mode == 'eval': self.assertIn('Detections_Left_Groundtruth_Right/0', estimator_spec.eval_metric_ops) if model_mode == tf.estimator.ModeKeys.TRAIN: self.assertIsNotNone(estimator_spec.train_op) return estimator_spec def _assert_model_fn_for_predict(self, configs): model_config = configs['model'] with tf.Graph().as_default(): features, _ = _make_initializable_iterator( inputs.create_eval_input_fn(configs['eval_config'], configs['eval_input_config'], configs['model'])()).get_next() detection_model_fn = functools.partial( model_builder.build, model_config=model_config, is_training=False) hparams = model_hparams.create_hparams( hparams_overrides='load_pretrained=false') model_fn = model_lib.create_model_fn(detection_model_fn, configs, hparams) estimator_spec = model_fn(features, None, tf.estimator.ModeKeys.PREDICT) self.assertIsNone(estimator_spec.loss) self.assertIsNone(estimator_spec.train_op) self.assertIsNotNone(estimator_spec.predictions) self.assertIsNotNone(estimator_spec.export_outputs) self.assertIn(tf.saved_model.signature_constants.PREDICT_METHOD_NAME, estimator_spec.export_outputs) def test_model_fn_in_train_mode(self): """Tests the model function in TRAIN mode.""" configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) self._assert_model_fn_for_train_eval(configs, 'train') def test_model_fn_in_train_mode_freeze_all_variables(self): """Tests model_fn TRAIN mode with all variables frozen.""" configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) configs['train_config'].freeze_variables.append('.*') with self.assertRaisesRegexp(ValueError, 'No variables to optimize'): self._assert_model_fn_for_train_eval(configs, 'train') def test_model_fn_in_train_mode_freeze_all_included_variables(self): """Tests model_fn TRAIN mode with all included variables frozen.""" configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) train_config = configs['train_config'] train_config.update_trainable_variables.append('FeatureExtractor') train_config.freeze_variables.append('.*') with self.assertRaisesRegexp(ValueError, 'No variables to optimize'): self._assert_model_fn_for_train_eval(configs, 'train') def test_model_fn_in_train_mode_freeze_box_predictor(self): """Tests model_fn TRAIN mode with FeatureExtractor variables frozen.""" configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) train_config = configs['train_config'] train_config.update_trainable_variables.append('FeatureExtractor') train_config.update_trainable_variables.append('BoxPredictor') train_config.freeze_variables.append('FeatureExtractor') self._assert_model_fn_for_train_eval(configs, 'train') def test_model_fn_in_eval_mode(self): """Tests the model function in EVAL mode.""" configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) self._assert_model_fn_for_train_eval(configs, 'eval') def test_model_fn_in_eval_on_train_mode(self): """Tests the model function in EVAL mode with train data.""" configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) self._assert_model_fn_for_train_eval(configs, 'eval_on_train') def test_model_fn_in_predict_mode(self): """Tests the model function in PREDICT mode.""" configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) self._assert_model_fn_for_predict(configs) def test_create_estimator_and_inputs(self): """Tests that Estimator and input function are constructed correctly.""" run_config = tf.estimator.RunConfig() hparams = model_hparams.create_hparams( hparams_overrides='load_pretrained=false') pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) train_steps = 20 train_and_eval_dict = model_lib.create_estimator_and_inputs( run_config, hparams, pipeline_config_path, train_steps=train_steps) estimator = train_and_eval_dict['estimator'] train_steps = train_and_eval_dict['train_steps'] self.assertIsInstance(estimator, tf.estimator.Estimator) self.assertEqual(20, train_steps) self.assertIn('train_input_fn', train_and_eval_dict) self.assertIn('eval_input_fns', train_and_eval_dict) self.assertIn('eval_on_train_input_fn', train_and_eval_dict) def test_create_estimator_with_default_train_eval_steps(self): """Tests that number of train/eval defaults to config values.""" run_config = tf.estimator.RunConfig() hparams = model_hparams.create_hparams( hparams_overrides='load_pretrained=false') pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) configs = config_util.get_configs_from_pipeline_file(pipeline_config_path) config_train_steps = configs['train_config'].num_steps train_and_eval_dict = model_lib.create_estimator_and_inputs( run_config, hparams, pipeline_config_path) estimator = train_and_eval_dict['estimator'] train_steps = train_and_eval_dict['train_steps'] self.assertIsInstance(estimator, tf.estimator.Estimator) self.assertEqual(config_train_steps, train_steps) def test_create_tpu_estimator_and_inputs(self): """Tests that number of train/eval defaults to config values.""" run_config = tpu_config.RunConfig() hparams = model_hparams.create_hparams( hparams_overrides='load_pretrained=false') pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) train_steps = 20 train_and_eval_dict = model_lib.create_estimator_and_inputs( run_config, hparams, pipeline_config_path, train_steps=train_steps, use_tpu_estimator=True) estimator = train_and_eval_dict['estimator'] train_steps = train_and_eval_dict['train_steps'] self.assertIsInstance(estimator, tpu_estimator.TPUEstimator) self.assertEqual(20, train_steps) def test_create_train_and_eval_specs(self): """Tests that `TrainSpec` and `EvalSpec` is created correctly.""" run_config = tf.estimator.RunConfig() hparams = model_hparams.create_hparams( hparams_overrides='load_pretrained=false') pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) train_steps = 20 train_and_eval_dict = model_lib.create_estimator_and_inputs( run_config, hparams, pipeline_config_path, train_steps=train_steps) train_input_fn = train_and_eval_dict['train_input_fn'] eval_input_fns = train_and_eval_dict['eval_input_fns'] eval_on_train_input_fn = train_and_eval_dict['eval_on_train_input_fn'] predict_input_fn = train_and_eval_dict['predict_input_fn'] train_steps = train_and_eval_dict['train_steps'] train_spec, eval_specs = model_lib.create_train_and_eval_specs( train_input_fn, eval_input_fns, eval_on_train_input_fn, predict_input_fn, train_steps, eval_on_train_data=True, final_exporter_name='exporter', eval_spec_names=['holdout']) self.assertEqual(train_steps, train_spec.max_steps) self.assertEqual(2, len(eval_specs)) self.assertEqual(None, eval_specs[0].steps) self.assertEqual('holdout', eval_specs[0].name) self.assertEqual('exporter', eval_specs[0].exporters[0].name) self.assertEqual(None, eval_specs[1].steps) self.assertEqual('eval_on_train', eval_specs[1].name) def test_experiment(self): """Tests that the `Experiment` object is constructed correctly.""" run_config = tf.estimator.RunConfig() hparams = model_hparams.create_hparams( hparams_overrides='load_pretrained=false') pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) experiment = model_lib.populate_experiment( run_config, hparams, pipeline_config_path, train_steps=10, eval_steps=20) self.assertEqual(10, experiment.train_steps) self.assertEqual(None, experiment.eval_steps) class UnbatchTensorsTest(tf.test.TestCase): def test_unbatch_without_unpadding(self): image_placeholder = tf.placeholder(tf.float32, [2, None, None, None]) groundtruth_boxes_placeholder = tf.placeholder(tf.float32, [2, None, None]) groundtruth_classes_placeholder = tf.placeholder(tf.float32, [2, None, None]) groundtruth_weights_placeholder = tf.placeholder(tf.float32, [2, None]) tensor_dict = { fields.InputDataFields.image: image_placeholder, fields.InputDataFields.groundtruth_boxes: groundtruth_boxes_placeholder, fields.InputDataFields.groundtruth_classes: groundtruth_classes_placeholder, fields.InputDataFields.groundtruth_weights: groundtruth_weights_placeholder } unbatched_tensor_dict = model_lib.unstack_batch( tensor_dict, unpad_groundtruth_tensors=False) with self.test_session() as sess: unbatched_tensor_dict_out = sess.run( unbatched_tensor_dict, feed_dict={ image_placeholder: np.random.rand(2, 4, 4, 3).astype(np.float32), groundtruth_boxes_placeholder: np.random.rand(2, 5, 4).astype(np.float32), groundtruth_classes_placeholder: np.random.rand(2, 5, 6).astype(np.float32), groundtruth_weights_placeholder: np.random.rand(2, 5).astype(np.float32) }) for image_out in unbatched_tensor_dict_out[fields.InputDataFields.image]: self.assertAllEqual(image_out.shape, [4, 4, 3]) for groundtruth_boxes_out in unbatched_tensor_dict_out[ fields.InputDataFields.groundtruth_boxes]: self.assertAllEqual(groundtruth_boxes_out.shape, [5, 4]) for groundtruth_classes_out in unbatched_tensor_dict_out[ fields.InputDataFields.groundtruth_classes]: self.assertAllEqual(groundtruth_classes_out.shape, [5, 6]) for groundtruth_weights_out in unbatched_tensor_dict_out[ fields.InputDataFields.groundtruth_weights]: self.assertAllEqual(groundtruth_weights_out.shape, [5]) def test_unbatch_and_unpad_groundtruth_tensors(self): image_placeholder = tf.placeholder(tf.float32, [2, None, None, None]) groundtruth_boxes_placeholder = tf.placeholder(tf.float32, [2, 5, None]) groundtruth_classes_placeholder = tf.placeholder(tf.float32, [2, 5, None]) groundtruth_weights_placeholder = tf.placeholder(tf.float32, [2, 5]) num_groundtruth_placeholder = tf.placeholder(tf.int32, [2]) tensor_dict = { fields.InputDataFields.image: image_placeholder, fields.InputDataFields.groundtruth_boxes: groundtruth_boxes_placeholder, fields.InputDataFields.groundtruth_classes: groundtruth_classes_placeholder, fields.InputDataFields.groundtruth_weights: groundtruth_weights_placeholder, fields.InputDataFields.num_groundtruth_boxes: num_groundtruth_placeholder } unbatched_tensor_dict = model_lib.unstack_batch( tensor_dict, unpad_groundtruth_tensors=True) with self.test_session() as sess: unbatched_tensor_dict_out = sess.run( unbatched_tensor_dict, feed_dict={ image_placeholder: np.random.rand(2, 4, 4, 3).astype(np.float32), groundtruth_boxes_placeholder: np.random.rand(2, 5, 4).astype(np.float32), groundtruth_classes_placeholder: np.random.rand(2, 5, 6).astype(np.float32), groundtruth_weights_placeholder: np.random.rand(2, 5).astype(np.float32), num_groundtruth_placeholder: np.array([3, 3], np.int32) }) for image_out in unbatched_tensor_dict_out[fields.InputDataFields.image]: self.assertAllEqual(image_out.shape, [4, 4, 3]) for groundtruth_boxes_out in unbatched_tensor_dict_out[ fields.InputDataFields.groundtruth_boxes]: self.assertAllEqual(groundtruth_boxes_out.shape, [3, 4]) for groundtruth_classes_out in unbatched_tensor_dict_out[ fields.InputDataFields.groundtruth_classes]: self.assertAllEqual(groundtruth_classes_out.shape, [3, 6]) for groundtruth_weights_out in unbatched_tensor_dict_out[ fields.InputDataFields.groundtruth_weights]: self.assertAllEqual(groundtruth_weights_out.shape, [3]) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/model_lib_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Functions to export object detection inference graph.""" import os import tempfile import tensorflow as tf from tensorflow.contrib.quantize.python import graph_matcher from tensorflow.core.protobuf import saver_pb2 from tensorflow.python.client import session from tensorflow.python.platform import gfile from tensorflow.python.saved_model import signature_constants from tensorflow.python.tools import freeze_graph from tensorflow.python.training import saver as saver_lib from object_detection.builders import graph_rewriter_builder from object_detection.builders import model_builder from object_detection.core import standard_fields as fields from object_detection.data_decoders import tf_example_decoder from object_detection.utils import config_util from object_detection.utils import shape_utils slim = tf.contrib.slim freeze_graph_with_def_protos = freeze_graph.freeze_graph_with_def_protos def rewrite_nn_resize_op(is_quantized=False): """Replaces a custom nearest-neighbor resize op with the Tensorflow version. Some graphs use this custom version for TPU-compatibility. Args: is_quantized: True if the default graph is quantized. """ input_pattern = graph_matcher.OpTypePattern( 'FakeQuantWithMinMaxVars' if is_quantized else '*') reshape_1_pattern = graph_matcher.OpTypePattern( 'Reshape', inputs=[input_pattern, 'Const'], ordered_inputs=False) mul_pattern = graph_matcher.OpTypePattern( 'Mul', inputs=[reshape_1_pattern, 'Const'], ordered_inputs=False) # The quantization script may or may not insert a fake quant op after the # Mul. In either case, these min/max vars are not needed once replaced with # the TF version of NN resize. fake_quant_pattern = graph_matcher.OpTypePattern( 'FakeQuantWithMinMaxVars', inputs=[mul_pattern, 'Identity', 'Identity'], ordered_inputs=False) reshape_2_pattern = graph_matcher.OpTypePattern( 'Reshape', inputs=[graph_matcher.OneofPattern([fake_quant_pattern, mul_pattern]), 'Const'], ordered_inputs=False) add_pattern = graph_matcher.OpTypePattern( 'Add', inputs=[reshape_2_pattern, '*'], ordered_inputs=False) matcher = graph_matcher.GraphMatcher(add_pattern) for match in matcher.match_graph(tf.get_default_graph()): projection_op = match.get_op(input_pattern) reshape_2_op = match.get_op(reshape_2_pattern) add_op = match.get_op(add_pattern) nn_resize = tf.image.resize_nearest_neighbor( projection_op.outputs[0], add_op.outputs[0].shape.dims[1:3], align_corners=False) for index, op_input in enumerate(add_op.inputs): if op_input == reshape_2_op.outputs[0]: add_op._update_input(index, nn_resize) # pylint: disable=protected-access break def replace_variable_values_with_moving_averages(graph, current_checkpoint_file, new_checkpoint_file): """Replaces variable values in the checkpoint with their moving averages. If the current checkpoint has shadow variables maintaining moving averages of the variables defined in the graph, this function generates a new checkpoint where the variables contain the values of their moving averages. Args: graph: a tf.Graph object. current_checkpoint_file: a checkpoint containing both original variables and their moving averages. new_checkpoint_file: file path to write a new checkpoint. """ with graph.as_default(): variable_averages = tf.train.ExponentialMovingAverage(0.0) ema_variables_to_restore = variable_averages.variables_to_restore() with tf.Session() as sess: read_saver = tf.train.Saver(ema_variables_to_restore) read_saver.restore(sess, current_checkpoint_file) write_saver = tf.train.Saver() write_saver.save(sess, new_checkpoint_file) def _image_tensor_input_placeholder(input_shape=None): """Returns input placeholder and a 4-D uint8 image tensor.""" if input_shape is None: input_shape = (None, None, None, 3) input_tensor = tf.placeholder( dtype=tf.uint8, shape=input_shape, name='image_tensor') return input_tensor, input_tensor def _tf_example_input_placeholder(): """Returns input that accepts a batch of strings with tf examples. Returns: a tuple of input placeholder and the output decoded images. """ batch_tf_example_placeholder = tf.placeholder( tf.string, shape=[None], name='tf_example') def decode(tf_example_string_tensor): tensor_dict = tf_example_decoder.TfExampleDecoder().decode( tf_example_string_tensor) image_tensor = tensor_dict[fields.InputDataFields.image] return image_tensor return (batch_tf_example_placeholder, shape_utils.static_or_dynamic_map_fn( decode, elems=batch_tf_example_placeholder, dtype=tf.uint8, parallel_iterations=32, back_prop=False)) def _encoded_image_string_tensor_input_placeholder(): """Returns input that accepts a batch of PNG or JPEG strings. Returns: a tuple of input placeholder and the output decoded images. """ batch_image_str_placeholder = tf.placeholder( dtype=tf.string, shape=[None], name='encoded_image_string_tensor') def decode(encoded_image_string_tensor): image_tensor = tf.image.decode_image(encoded_image_string_tensor, channels=3) image_tensor.set_shape((None, None, 3)) return image_tensor return (batch_image_str_placeholder, tf.map_fn( decode, elems=batch_image_str_placeholder, dtype=tf.uint8, parallel_iterations=32, back_prop=False)) input_placeholder_fn_map = { 'image_tensor': _image_tensor_input_placeholder, 'encoded_image_string_tensor': _encoded_image_string_tensor_input_placeholder, 'tf_example': _tf_example_input_placeholder, } def add_output_tensor_nodes(postprocessed_tensors, output_collection_name='inference_op'): """Adds output nodes for detection boxes and scores. Adds the following nodes for output tensors - * num_detections: float32 tensor of shape [batch_size]. * detection_boxes: float32 tensor of shape [batch_size, num_boxes, 4] containing detected boxes. * detection_scores: float32 tensor of shape [batch_size, num_boxes] containing scores for the detected boxes. * detection_classes: float32 tensor of shape [batch_size, num_boxes] containing class predictions for the detected boxes. * detection_keypoints: (Optional) float32 tensor of shape [batch_size, num_boxes, num_keypoints, 2] containing keypoints for each detection box. * detection_masks: (Optional) float32 tensor of shape [batch_size, num_boxes, mask_height, mask_width] containing masks for each detection box. Args: postprocessed_tensors: a dictionary containing the following fields 'detection_boxes': [batch, max_detections, 4] 'detection_scores': [batch, max_detections] 'detection_classes': [batch, max_detections] 'detection_masks': [batch, max_detections, mask_height, mask_width] (optional). 'detection_keypoints': [batch, max_detections, num_keypoints, 2] (optional). 'num_detections': [batch] output_collection_name: Name of collection to add output tensors to. Returns: A tensor dict containing the added output tensor nodes. """ detection_fields = fields.DetectionResultFields label_id_offset = 1 boxes = postprocessed_tensors.get(detection_fields.detection_boxes) scores = postprocessed_tensors.get(detection_fields.detection_scores) classes = postprocessed_tensors.get( detection_fields.detection_classes) + label_id_offset keypoints = postprocessed_tensors.get(detection_fields.detection_keypoints) masks = postprocessed_tensors.get(detection_fields.detection_masks) num_detections = postprocessed_tensors.get(detection_fields.num_detections) outputs = {} outputs[detection_fields.detection_boxes] = tf.identity( boxes, name=detection_fields.detection_boxes) outputs[detection_fields.detection_scores] = tf.identity( scores, name=detection_fields.detection_scores) outputs[detection_fields.detection_classes] = tf.identity( classes, name=detection_fields.detection_classes) outputs[detection_fields.num_detections] = tf.identity( num_detections, name=detection_fields.num_detections) if keypoints is not None: outputs[detection_fields.detection_keypoints] = tf.identity( keypoints, name=detection_fields.detection_keypoints) if masks is not None: outputs[detection_fields.detection_masks] = tf.identity( masks, name=detection_fields.detection_masks) for output_key in outputs: tf.add_to_collection(output_collection_name, outputs[output_key]) return outputs def write_saved_model(saved_model_path, frozen_graph_def, inputs, outputs): """Writes SavedModel to disk. If checkpoint_path is not None bakes the weights into the graph thereby eliminating the need of checkpoint files during inference. If the model was trained with moving averages, setting use_moving_averages to true restores the moving averages, otherwise the original set of variables is restored. Args: saved_model_path: Path to write SavedModel. frozen_graph_def: tf.GraphDef holding frozen graph. inputs: The input placeholder tensor. outputs: A tensor dictionary containing the outputs of a DetectionModel. """ with tf.Graph().as_default(): with session.Session() as sess: tf.import_graph_def(frozen_graph_def, name='') builder = tf.saved_model.builder.SavedModelBuilder(saved_model_path) tensor_info_inputs = { 'inputs': tf.saved_model.utils.build_tensor_info(inputs)} tensor_info_outputs = {} for k, v in outputs.items(): tensor_info_outputs[k] = tf.saved_model.utils.build_tensor_info(v) detection_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs=tensor_info_inputs, outputs=tensor_info_outputs, method_name=signature_constants.PREDICT_METHOD_NAME)) builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={ signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: detection_signature, }, ) builder.save() def write_graph_and_checkpoint(inference_graph_def, model_path, input_saver_def, trained_checkpoint_prefix): """Writes the graph and the checkpoint into disk.""" for node in inference_graph_def.node: node.device = '' with tf.Graph().as_default(): tf.import_graph_def(inference_graph_def, name='') with session.Session() as sess: saver = saver_lib.Saver(saver_def=input_saver_def, save_relative_paths=True) saver.restore(sess, trained_checkpoint_prefix) saver.save(sess, model_path) def _get_outputs_from_inputs(input_tensors, detection_model, output_collection_name): inputs = tf.to_float(input_tensors) preprocessed_inputs, true_image_shapes = detection_model.preprocess(inputs) output_tensors = detection_model.predict( preprocessed_inputs, true_image_shapes) postprocessed_tensors = detection_model.postprocess( output_tensors, true_image_shapes) return add_output_tensor_nodes(postprocessed_tensors, output_collection_name) def _build_detection_graph(input_type, detection_model, input_shape, output_collection_name, graph_hook_fn): """Build the detection graph.""" if input_type not in input_placeholder_fn_map: raise ValueError('Unknown input type: {}'.format(input_type)) placeholder_args = {} if input_shape is not None: if input_type != 'image_tensor': raise ValueError('Can only specify input shape for `image_tensor` ' 'inputs.') placeholder_args['input_shape'] = input_shape placeholder_tensor, input_tensors = input_placeholder_fn_map[input_type]( **placeholder_args) outputs = _get_outputs_from_inputs( input_tensors=input_tensors, detection_model=detection_model, output_collection_name=output_collection_name) # Add global step to the graph. slim.get_or_create_global_step() if graph_hook_fn: graph_hook_fn() return outputs, placeholder_tensor def _export_inference_graph(input_type, detection_model, use_moving_averages, trained_checkpoint_prefix, output_directory, additional_output_tensor_names=None, input_shape=None, output_collection_name='inference_op', graph_hook_fn=None, write_inference_graph=False): """Export helper.""" tf.gfile.MakeDirs(output_directory) frozen_graph_path = os.path.join(output_directory, 'frozen_inference_graph.pb') saved_model_path = os.path.join(output_directory, 'saved_model') model_path = os.path.join(output_directory, 'model.ckpt') outputs, placeholder_tensor = _build_detection_graph( input_type=input_type, detection_model=detection_model, input_shape=input_shape, output_collection_name=output_collection_name, graph_hook_fn=graph_hook_fn) profile_inference_graph(tf.get_default_graph()) saver_kwargs = {} if use_moving_averages: # This check is to be compatible with both version of SaverDef. if os.path.isfile(trained_checkpoint_prefix): saver_kwargs['write_version'] = saver_pb2.SaverDef.V1 temp_checkpoint_prefix = tempfile.NamedTemporaryFile().name else: temp_checkpoint_prefix = tempfile.mkdtemp() replace_variable_values_with_moving_averages( tf.get_default_graph(), trained_checkpoint_prefix, temp_checkpoint_prefix) checkpoint_to_use = temp_checkpoint_prefix else: checkpoint_to_use = trained_checkpoint_prefix saver = tf.train.Saver(**saver_kwargs) input_saver_def = saver.as_saver_def() write_graph_and_checkpoint( inference_graph_def=tf.get_default_graph().as_graph_def(), model_path=model_path, input_saver_def=input_saver_def, trained_checkpoint_prefix=checkpoint_to_use) if write_inference_graph: inference_graph_def = tf.get_default_graph().as_graph_def() inference_graph_path = os.path.join(output_directory, 'inference_graph.pbtxt') for node in inference_graph_def.node: node.device = '' with gfile.GFile(inference_graph_path, 'wb') as f: f.write(str(inference_graph_def)) if additional_output_tensor_names is not None: output_node_names = ','.join(outputs.keys()+additional_output_tensor_names) else: output_node_names = ','.join(outputs.keys()) frozen_graph_def = freeze_graph.freeze_graph_with_def_protos( input_graph_def=tf.get_default_graph().as_graph_def(), input_saver_def=input_saver_def, input_checkpoint=checkpoint_to_use, output_node_names=output_node_names, restore_op_name='save/restore_all', filename_tensor_name='save/Const:0', output_graph=frozen_graph_path, clear_devices=True, initializer_nodes='') write_saved_model(saved_model_path, frozen_graph_def, placeholder_tensor, outputs) def export_inference_graph(input_type, pipeline_config, trained_checkpoint_prefix, output_directory, input_shape=None, output_collection_name='inference_op', additional_output_tensor_names=None, write_inference_graph=False): """Exports inference graph for the model specified in the pipeline config. Args: input_type: Type of input for the graph. Can be one of ['image_tensor', 'encoded_image_string_tensor', 'tf_example']. pipeline_config: pipeline_pb2.TrainAndEvalPipelineConfig proto. trained_checkpoint_prefix: Path to the trained checkpoint file. output_directory: Path to write outputs. input_shape: Sets a fixed shape for an `image_tensor` input. If not specified, will default to [None, None, None, 3]. output_collection_name: Name of collection to add output tensors to. If None, does not add output tensors to a collection. additional_output_tensor_names: list of additional output tensors to include in the frozen graph. write_inference_graph: If true, writes inference graph to disk. """ detection_model = model_builder.build(pipeline_config.model, is_training=False) graph_rewriter_fn = None if pipeline_config.HasField('graph_rewriter'): graph_rewriter_config = pipeline_config.graph_rewriter graph_rewriter_fn = graph_rewriter_builder.build(graph_rewriter_config, is_training=False) _export_inference_graph( input_type, detection_model, pipeline_config.eval_config.use_moving_averages, trained_checkpoint_prefix, output_directory, additional_output_tensor_names, input_shape, output_collection_name, graph_hook_fn=graph_rewriter_fn, write_inference_graph=write_inference_graph) pipeline_config.eval_config.use_moving_averages = False config_util.save_pipeline_config(pipeline_config, output_directory) def profile_inference_graph(graph): """Profiles the inference graph. Prints model parameters and computation FLOPs given an inference graph. BatchNorms are excluded from the parameter count due to the fact that BatchNorms are usually folded. BatchNorm, Initializer, Regularizer and BiasAdd are not considered in FLOP count. Args: graph: the inference graph. """ tfprof_vars_option = ( tf.contrib.tfprof.model_analyzer.TRAINABLE_VARS_PARAMS_STAT_OPTIONS) tfprof_flops_option = tf.contrib.tfprof.model_analyzer.FLOAT_OPS_OPTIONS # Batchnorm is usually folded during inference. tfprof_vars_option['trim_name_regexes'] = ['.*BatchNorm.*'] # Initializer and Regularizer are only used in training. tfprof_flops_option['trim_name_regexes'] = [ '.*BatchNorm.*', '.*Initializer.*', '.*Regularizer.*', '.*BiasAdd.*' ] tf.contrib.tfprof.model_analyzer.print_model_analysis( graph, tfprof_options=tfprof_vars_option) tf.contrib.tfprof.model_analyzer.print_model_analysis( graph, tfprof_options=tfprof_flops_option)
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/exporter.py
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/__init__.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for eval_util.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from absl.testing import parameterized import tensorflow as tf from object_detection import eval_util from object_detection.core import standard_fields as fields from object_detection.protos import eval_pb2 from object_detection.utils import test_case class EvalUtilTest(test_case.TestCase, parameterized.TestCase): def _get_categories_list(self): return [{'id': 0, 'name': 'person'}, {'id': 1, 'name': 'dog'}, {'id': 2, 'name': 'cat'}] def _make_evaluation_dict(self, resized_groundtruth_masks=False, batch_size=1, max_gt_boxes=None, scale_to_absolute=False): input_data_fields = fields.InputDataFields detection_fields = fields.DetectionResultFields image = tf.zeros(shape=[batch_size, 20, 20, 3], dtype=tf.uint8) if batch_size == 1: key = tf.constant('image1') else: key = tf.constant([str(i) for i in range(batch_size)]) detection_boxes = tf.tile(tf.constant([[[0., 0., 1., 1.]]]), multiples=[batch_size, 1, 1]) detection_scores = tf.tile(tf.constant([[0.8]]), multiples=[batch_size, 1]) detection_classes = tf.tile(tf.constant([[0]]), multiples=[batch_size, 1]) detection_masks = tf.tile(tf.ones(shape=[1, 1, 20, 20], dtype=tf.float32), multiples=[batch_size, 1, 1, 1]) num_detections = tf.ones([batch_size]) groundtruth_boxes = tf.constant([[0., 0., 1., 1.]]) groundtruth_classes = tf.constant([1]) groundtruth_instance_masks = tf.ones(shape=[1, 20, 20], dtype=tf.uint8) if resized_groundtruth_masks: groundtruth_instance_masks = tf.ones(shape=[1, 10, 10], dtype=tf.uint8) if batch_size > 1: groundtruth_boxes = tf.tile(tf.expand_dims(groundtruth_boxes, 0), multiples=[batch_size, 1, 1]) groundtruth_classes = tf.tile(tf.expand_dims(groundtruth_classes, 0), multiples=[batch_size, 1]) groundtruth_instance_masks = tf.tile( tf.expand_dims(groundtruth_instance_masks, 0), multiples=[batch_size, 1, 1, 1]) detections = { detection_fields.detection_boxes: detection_boxes, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes, detection_fields.detection_masks: detection_masks, detection_fields.num_detections: num_detections } groundtruth = { input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, input_data_fields.groundtruth_instance_masks: groundtruth_instance_masks } if batch_size > 1: return eval_util.result_dict_for_batched_example( image, key, detections, groundtruth, scale_to_absolute=scale_to_absolute, max_gt_boxes=max_gt_boxes) else: return eval_util.result_dict_for_single_example( image, key, detections, groundtruth, scale_to_absolute=scale_to_absolute) @parameterized.parameters( {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': True}, {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': True}, {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': False}, {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': False} ) def test_get_eval_metric_ops_for_coco_detections(self, batch_size=1, max_gt_boxes=None, scale_to_absolute=False): eval_config = eval_pb2.EvalConfig() eval_config.metrics_set.extend(['coco_detection_metrics']) categories = self._get_categories_list() eval_dict = self._make_evaluation_dict(batch_size=batch_size, max_gt_boxes=max_gt_boxes, scale_to_absolute=scale_to_absolute) metric_ops = eval_util.get_eval_metric_ops_for_evaluators( eval_config, categories, eval_dict) _, update_op = metric_ops['DetectionBoxes_Precision/mAP'] with self.test_session() as sess: metrics = {} for key, (value_op, _) in metric_ops.iteritems(): metrics[key] = value_op sess.run(update_op) metrics = sess.run(metrics) self.assertAlmostEqual(1.0, metrics['DetectionBoxes_Precision/mAP']) self.assertNotIn('DetectionMasks_Precision/mAP', metrics) @parameterized.parameters( {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': True}, {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': True}, {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': False}, {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': False} ) def test_get_eval_metric_ops_for_coco_detections_and_masks( self, batch_size=1, max_gt_boxes=None, scale_to_absolute=False): eval_config = eval_pb2.EvalConfig() eval_config.metrics_set.extend( ['coco_detection_metrics', 'coco_mask_metrics']) categories = self._get_categories_list() eval_dict = self._make_evaluation_dict(batch_size=batch_size, max_gt_boxes=max_gt_boxes, scale_to_absolute=scale_to_absolute) metric_ops = eval_util.get_eval_metric_ops_for_evaluators( eval_config, categories, eval_dict) _, update_op_boxes = metric_ops['DetectionBoxes_Precision/mAP'] _, update_op_masks = metric_ops['DetectionMasks_Precision/mAP'] with self.test_session() as sess: metrics = {} for key, (value_op, _) in metric_ops.iteritems(): metrics[key] = value_op sess.run(update_op_boxes) sess.run(update_op_masks) metrics = sess.run(metrics) self.assertAlmostEqual(1.0, metrics['DetectionBoxes_Precision/mAP']) self.assertAlmostEqual(1.0, metrics['DetectionMasks_Precision/mAP']) @parameterized.parameters( {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': True}, {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': True}, {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': False}, {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': False} ) def test_get_eval_metric_ops_for_coco_detections_and_resized_masks( self, batch_size=1, max_gt_boxes=None, scale_to_absolute=False): eval_config = eval_pb2.EvalConfig() eval_config.metrics_set.extend( ['coco_detection_metrics', 'coco_mask_metrics']) categories = self._get_categories_list() eval_dict = self._make_evaluation_dict(batch_size=batch_size, max_gt_boxes=max_gt_boxes, scale_to_absolute=scale_to_absolute, resized_groundtruth_masks=True) metric_ops = eval_util.get_eval_metric_ops_for_evaluators( eval_config, categories, eval_dict) _, update_op_boxes = metric_ops['DetectionBoxes_Precision/mAP'] _, update_op_masks = metric_ops['DetectionMasks_Precision/mAP'] with self.test_session() as sess: metrics = {} for key, (value_op, _) in metric_ops.iteritems(): metrics[key] = value_op sess.run(update_op_boxes) sess.run(update_op_masks) metrics = sess.run(metrics) self.assertAlmostEqual(1.0, metrics['DetectionBoxes_Precision/mAP']) self.assertAlmostEqual(1.0, metrics['DetectionMasks_Precision/mAP']) def test_get_eval_metric_ops_raises_error_with_unsupported_metric(self): eval_config = eval_pb2.EvalConfig() eval_config.metrics_set.extend(['unsupported_metric']) categories = self._get_categories_list() eval_dict = self._make_evaluation_dict() with self.assertRaises(ValueError): eval_util.get_eval_metric_ops_for_evaluators( eval_config, categories, eval_dict) def test_get_eval_metric_ops_for_evaluators(self): eval_config = eval_pb2.EvalConfig() eval_config.metrics_set.extend( ['coco_detection_metrics', 'coco_mask_metrics']) eval_config.include_metrics_per_category = True evaluator_options = eval_util.evaluator_options_from_eval_config( eval_config) self.assertTrue(evaluator_options['coco_detection_metrics'][ 'include_metrics_per_category']) self.assertTrue(evaluator_options['coco_mask_metrics'][ 'include_metrics_per_category']) def test_get_evaluator_with_evaluator_options(self): eval_config = eval_pb2.EvalConfig() eval_config.metrics_set.extend(['coco_detection_metrics']) eval_config.include_metrics_per_category = True categories = self._get_categories_list() evaluator_options = eval_util.evaluator_options_from_eval_config( eval_config) evaluator = eval_util.get_evaluators( eval_config, categories, evaluator_options) self.assertTrue(evaluator[0]._include_metrics_per_category) def test_get_evaluator_with_no_evaluator_options(self): eval_config = eval_pb2.EvalConfig() eval_config.metrics_set.extend(['coco_detection_metrics']) eval_config.include_metrics_per_category = True categories = self._get_categories_list() evaluator = eval_util.get_evaluators( eval_config, categories, evaluator_options=None) # Even though we are setting eval_config.include_metrics_per_category = True # this option is never passed into the DetectionEvaluator constructor (via # `evaluator_options`). self.assertFalse(evaluator[0]._include_metrics_per_category) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/eval_util_test.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.export_tflite_ssd_graph.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import numpy as np import six import tensorflow as tf from tensorflow.core.framework import types_pb2 from object_detection import export_tflite_ssd_graph_lib from object_detection import exporter from object_detection.builders import graph_rewriter_builder from object_detection.builders import model_builder from object_detection.core import model from object_detection.protos import graph_rewriter_pb2 from object_detection.protos import pipeline_pb2 from object_detection.protos import post_processing_pb2 if six.PY2: import mock # pylint: disable=g-import-not-at-top else: from unittest import mock # pylint: disable=g-import-not-at-top class FakeModel(model.DetectionModel): def __init__(self, add_detection_masks=False): self._add_detection_masks = add_detection_masks def preprocess(self, inputs): pass def predict(self, preprocessed_inputs, true_image_shapes): features = tf.contrib.slim.conv2d(preprocessed_inputs, 3, 1) with tf.control_dependencies([features]): prediction_tensors = { 'box_encodings': tf.constant([[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]], tf.float32), 'class_predictions_with_background': tf.constant([[[0.7, 0.6], [0.9, 0.0]]], tf.float32), } with tf.control_dependencies( [tf.convert_to_tensor(features.get_shape().as_list()[1:3])]): prediction_tensors['anchors'] = tf.constant( [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 1.0]], tf.float32) return prediction_tensors def postprocess(self, prediction_tensors, true_image_shapes): pass def restore_map(self, checkpoint_path, from_detection_checkpoint): pass def loss(self, prediction_dict, true_image_shapes): pass def regularization_losses(self): pass def updates(self): pass class ExportTfliteGraphTest(tf.test.TestCase): def _save_checkpoint_from_mock_model(self, checkpoint_path, use_moving_averages, quantize=False, num_channels=3): g = tf.Graph() with g.as_default(): mock_model = FakeModel() inputs = tf.placeholder(tf.float32, shape=[1, 10, 10, num_channels]) mock_model.predict(inputs, true_image_shapes=None) if use_moving_averages: tf.train.ExponentialMovingAverage(0.0).apply() tf.train.get_or_create_global_step() if quantize: graph_rewriter_config = graph_rewriter_pb2.GraphRewriter() graph_rewriter_config.quantization.delay = 500000 graph_rewriter_fn = graph_rewriter_builder.build( graph_rewriter_config, is_training=False) graph_rewriter_fn() saver = tf.train.Saver() init = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init) saver.save(sess, checkpoint_path) def _assert_quant_vars_exists(self, tflite_graph_file): with tf.gfile.Open(tflite_graph_file) as f: graph_string = f.read() print(graph_string) self.assertTrue('quant' in graph_string) def _import_graph_and_run_inference(self, tflite_graph_file, num_channels=3): """Imports a tflite graph, runs single inference and returns outputs.""" graph = tf.Graph() with graph.as_default(): graph_def = tf.GraphDef() with tf.gfile.Open(tflite_graph_file) as f: graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') input_tensor = graph.get_tensor_by_name('normalized_input_image_tensor:0') box_encodings = graph.get_tensor_by_name('raw_outputs/box_encodings:0') class_predictions = graph.get_tensor_by_name( 'raw_outputs/class_predictions:0') with self.test_session(graph) as sess: [box_encodings_np, class_predictions_np] = sess.run( [box_encodings, class_predictions], feed_dict={input_tensor: np.random.rand(1, 10, 10, num_channels)}) return box_encodings_np, class_predictions_np def _export_graph(self, pipeline_config, num_channels=3): """Exports a tflite graph.""" output_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(output_dir, 'model.ckpt') tflite_graph_file = os.path.join(output_dir, 'tflite_graph.pb') quantize = pipeline_config.HasField('graph_rewriter') self._save_checkpoint_from_mock_model( trained_checkpoint_prefix, use_moving_averages=pipeline_config.eval_config.use_moving_averages, quantize=quantize, num_channels=num_channels) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() with tf.Graph().as_default(): export_tflite_ssd_graph_lib.export_tflite_graph( pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_dir=output_dir, add_postprocessing_op=False, max_detections=10, max_classes_per_detection=1) return tflite_graph_file def _export_graph_with_postprocessing_op(self, pipeline_config, num_channels=3): """Exports a tflite graph with custom postprocessing op.""" output_dir = self.get_temp_dir() trained_checkpoint_prefix = os.path.join(output_dir, 'model.ckpt') tflite_graph_file = os.path.join(output_dir, 'tflite_graph.pb') quantize = pipeline_config.HasField('graph_rewriter') self._save_checkpoint_from_mock_model( trained_checkpoint_prefix, use_moving_averages=pipeline_config.eval_config.use_moving_averages, quantize=quantize, num_channels=num_channels) with mock.patch.object( model_builder, 'build', autospec=True) as mock_builder: mock_builder.return_value = FakeModel() with tf.Graph().as_default(): export_tflite_ssd_graph_lib.export_tflite_graph( pipeline_config=pipeline_config, trained_checkpoint_prefix=trained_checkpoint_prefix, output_dir=output_dir, add_postprocessing_op=True, max_detections=10, max_classes_per_detection=1) return tflite_graph_file def test_export_tflite_graph_with_moving_averages(self): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = True pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 pipeline_config.model.ssd.num_classes = 2 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 tflite_graph_file = self._export_graph(pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) (box_encodings_np, class_predictions_np ) = self._import_graph_and_run_inference(tflite_graph_file) self.assertAllClose(box_encodings_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) def test_export_tflite_graph_without_moving_averages(self): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 pipeline_config.model.ssd.num_classes = 2 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 tflite_graph_file = self._export_graph(pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) (box_encodings_np, class_predictions_np ) = self._import_graph_and_run_inference(tflite_graph_file) self.assertAllClose(box_encodings_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) def test_export_tflite_graph_grayscale(self): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 (pipeline_config.model.ssd.image_resizer.fixed_shape_resizer ).convert_to_grayscale = True pipeline_config.model.ssd.num_classes = 2 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 tflite_graph_file = self._export_graph(pipeline_config, num_channels=1) self.assertTrue(os.path.exists(tflite_graph_file)) (box_encodings_np, class_predictions_np) = self._import_graph_and_run_inference( tflite_graph_file, num_channels=1) self.assertAllClose(box_encodings_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) def test_export_tflite_graph_with_quantization(self): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 pipeline_config.graph_rewriter.quantization.delay = 500000 pipeline_config.model.ssd.num_classes = 2 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 tflite_graph_file = self._export_graph(pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) self._assert_quant_vars_exists(tflite_graph_file) (box_encodings_np, class_predictions_np ) = self._import_graph_and_run_inference(tflite_graph_file) self.assertAllClose(box_encodings_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) def test_export_tflite_graph_with_softmax_score_conversion(self): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False pipeline_config.model.ssd.post_processing.score_converter = ( post_processing_pb2.PostProcessing.SOFTMAX) pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 pipeline_config.model.ssd.num_classes = 2 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 tflite_graph_file = self._export_graph(pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) (box_encodings_np, class_predictions_np ) = self._import_graph_and_run_inference(tflite_graph_file) self.assertAllClose(box_encodings_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) self.assertAllClose(class_predictions_np, [[[0.524979, 0.475021], [0.710949, 0.28905]]]) def test_export_tflite_graph_with_sigmoid_score_conversion(self): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False pipeline_config.model.ssd.post_processing.score_converter = ( post_processing_pb2.PostProcessing.SIGMOID) pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 pipeline_config.model.ssd.num_classes = 2 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 tflite_graph_file = self._export_graph(pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) (box_encodings_np, class_predictions_np ) = self._import_graph_and_run_inference(tflite_graph_file) self.assertAllClose(box_encodings_np, [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) self.assertAllClose(class_predictions_np, [[[0.668188, 0.645656], [0.710949, 0.5]]]) def test_export_tflite_graph_with_postprocessing_op(self): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.eval_config.use_moving_averages = False pipeline_config.model.ssd.post_processing.score_converter = ( post_processing_pb2.PostProcessing.SIGMOID) pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 pipeline_config.model.ssd.num_classes = 2 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 tflite_graph_file = self._export_graph_with_postprocessing_op( pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) graph = tf.Graph() with graph.as_default(): graph_def = tf.GraphDef() with tf.gfile.Open(tflite_graph_file) as f: graph_def.ParseFromString(f.read()) all_op_names = [node.name for node in graph_def.node] self.assertTrue('TFLite_Detection_PostProcess' in all_op_names) for node in graph_def.node: if node.name == 'TFLite_Detection_PostProcess': self.assertTrue(node.attr['_output_quantized'].b is True) self.assertTrue( node.attr['_support_output_type_float_in_quantized_op'].b is True) self.assertTrue(node.attr['y_scale'].f == 10.0) self.assertTrue(node.attr['x_scale'].f == 10.0) self.assertTrue(node.attr['h_scale'].f == 5.0) self.assertTrue(node.attr['w_scale'].f == 5.0) self.assertTrue(node.attr['num_classes'].i == 2) self.assertTrue( all([ t == types_pb2.DT_FLOAT for t in node.attr['_output_types'].list.type ])) @mock.patch.object(exporter, 'rewrite_nn_resize_op') def test_export_with_nn_resize_op_not_called_without_fpn(self, mock_get): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 tflite_graph_file = self._export_graph_with_postprocessing_op( pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) mock_get.assert_not_called() @mock.patch.object(exporter, 'rewrite_nn_resize_op') def test_export_with_nn_resize_op_called_with_fpn(self, mock_get): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 pipeline_config.model.ssd.feature_extractor.fpn.min_level = 3 pipeline_config.model.ssd.feature_extractor.fpn.max_level = 7 tflite_graph_file = self._export_graph_with_postprocessing_op( pipeline_config) self.assertTrue(os.path.exists(tflite_graph_file)) mock_get.assert_called_once() if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/export_tflite_ssd_graph_lib_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Common utility functions for evaluation.""" import collections import os import time import numpy as np import tensorflow as tf from object_detection.core import box_list from object_detection.core import box_list_ops from object_detection.core import keypoint_ops from object_detection.core import standard_fields as fields from object_detection.metrics import coco_evaluation from object_detection.utils import label_map_util from object_detection.utils import object_detection_evaluation from object_detection.utils import ops from object_detection.utils import shape_utils from object_detection.utils import visualization_utils as vis_utils slim = tf.contrib.slim # A dictionary of metric names to classes that implement the metric. The classes # in the dictionary must implement # utils.object_detection_evaluation.DetectionEvaluator interface. EVAL_METRICS_CLASS_DICT = { 'coco_detection_metrics': coco_evaluation.CocoDetectionEvaluator, 'coco_mask_metrics': coco_evaluation.CocoMaskEvaluator, 'oid_challenge_detection_metrics': object_detection_evaluation.OpenImagesDetectionChallengeEvaluator, 'pascal_voc_detection_metrics': object_detection_evaluation.PascalDetectionEvaluator, 'weighted_pascal_voc_detection_metrics': object_detection_evaluation.WeightedPascalDetectionEvaluator, 'pascal_voc_instance_segmentation_metrics': object_detection_evaluation.PascalInstanceSegmentationEvaluator, 'weighted_pascal_voc_instance_segmentation_metrics': object_detection_evaluation.WeightedPascalInstanceSegmentationEvaluator, 'oid_V2_detection_metrics': object_detection_evaluation.OpenImagesDetectionEvaluator, } EVAL_DEFAULT_METRIC = 'coco_detection_metrics' def write_metrics(metrics, global_step, summary_dir): """Write metrics to a summary directory. Args: metrics: A dictionary containing metric names and values. global_step: Global step at which the metrics are computed. summary_dir: Directory to write tensorflow summaries to. """ tf.logging.info('Writing metrics to tf summary.') summary_writer = tf.summary.FileWriterCache.get(summary_dir) for key in sorted(metrics): summary = tf.Summary(value=[ tf.Summary.Value(tag=key, simple_value=metrics[key]), ]) summary_writer.add_summary(summary, global_step) tf.logging.info('%s: %f', key, metrics[key]) tf.logging.info('Metrics written to tf summary.') # TODO(rathodv): Add tests. def visualize_detection_results(result_dict, tag, global_step, categories, summary_dir='', export_dir='', agnostic_mode=False, show_groundtruth=False, groundtruth_box_visualization_color='black', min_score_thresh=.5, max_num_predictions=20, skip_scores=False, skip_labels=False, keep_image_id_for_visualization_export=False): """Visualizes detection results and writes visualizations to image summaries. This function visualizes an image with its detected bounding boxes and writes to image summaries which can be viewed on tensorboard. It optionally also writes images to a directory. In the case of missing entry in the label map, unknown class name in the visualization is shown as "N/A". Args: result_dict: a dictionary holding groundtruth and detection data corresponding to each image being evaluated. The following keys are required: 'original_image': a numpy array representing the image with shape [1, height, width, 3] or [1, height, width, 1] 'detection_boxes': a numpy array of shape [N, 4] 'detection_scores': a numpy array of shape [N] 'detection_classes': a numpy array of shape [N] The following keys are optional: 'groundtruth_boxes': a numpy array of shape [N, 4] 'groundtruth_keypoints': a numpy array of shape [N, num_keypoints, 2] Detections are assumed to be provided in decreasing order of score and for display, and we assume that scores are probabilities between 0 and 1. tag: tensorboard tag (string) to associate with image. global_step: global step at which the visualization are generated. categories: a list of dictionaries representing all possible categories. Each dict in this list has the following keys: 'id': (required) an integer id uniquely identifying this category 'name': (required) string representing category name e.g., 'cat', 'dog', 'pizza' 'supercategory': (optional) string representing the supercategory e.g., 'animal', 'vehicle', 'food', etc summary_dir: the output directory to which the image summaries are written. export_dir: the output directory to which images are written. If this is empty (default), then images are not exported. agnostic_mode: boolean (default: False) controlling whether to evaluate in class-agnostic mode or not. show_groundtruth: boolean (default: False) controlling whether to show groundtruth boxes in addition to detected boxes groundtruth_box_visualization_color: box color for visualizing groundtruth boxes min_score_thresh: minimum score threshold for a box to be visualized max_num_predictions: maximum number of detections to visualize skip_scores: whether to skip score when drawing a single detection skip_labels: whether to skip label when drawing a single detection keep_image_id_for_visualization_export: whether to keep image identifier in filename when exported to export_dir Raises: ValueError: if result_dict does not contain the expected keys (i.e., 'original_image', 'detection_boxes', 'detection_scores', 'detection_classes') """ detection_fields = fields.DetectionResultFields input_fields = fields.InputDataFields if not set([ input_fields.original_image, detection_fields.detection_boxes, detection_fields.detection_scores, detection_fields.detection_classes, ]).issubset(set(result_dict.keys())): raise ValueError('result_dict does not contain all expected keys.') if show_groundtruth and input_fields.groundtruth_boxes not in result_dict: raise ValueError('If show_groundtruth is enabled, result_dict must contain ' 'groundtruth_boxes.') tf.logging.info('Creating detection visualizations.') category_index = label_map_util.create_category_index(categories) image = np.squeeze(result_dict[input_fields.original_image], axis=0) if image.shape[2] == 1: # If one channel image, repeat in RGB. image = np.tile(image, [1, 1, 3]) detection_boxes = result_dict[detection_fields.detection_boxes] detection_scores = result_dict[detection_fields.detection_scores] detection_classes = np.int32((result_dict[ detection_fields.detection_classes])) detection_keypoints = result_dict.get(detection_fields.detection_keypoints) detection_masks = result_dict.get(detection_fields.detection_masks) detection_boundaries = result_dict.get(detection_fields.detection_boundaries) # Plot groundtruth underneath detections if show_groundtruth: groundtruth_boxes = result_dict[input_fields.groundtruth_boxes] groundtruth_keypoints = result_dict.get(input_fields.groundtruth_keypoints) vis_utils.visualize_boxes_and_labels_on_image_array( image=image, boxes=groundtruth_boxes, classes=None, scores=None, category_index=category_index, keypoints=groundtruth_keypoints, use_normalized_coordinates=False, max_boxes_to_draw=None, groundtruth_box_visualization_color=groundtruth_box_visualization_color) vis_utils.visualize_boxes_and_labels_on_image_array( image, detection_boxes, detection_classes, detection_scores, category_index, instance_masks=detection_masks, instance_boundaries=detection_boundaries, keypoints=detection_keypoints, use_normalized_coordinates=False, max_boxes_to_draw=max_num_predictions, min_score_thresh=min_score_thresh, agnostic_mode=agnostic_mode, skip_scores=skip_scores, skip_labels=skip_labels) if export_dir: if keep_image_id_for_visualization_export and result_dict[fields. InputDataFields() .key]: export_path = os.path.join(export_dir, 'export-{}-{}.png'.format( tag, result_dict[fields.InputDataFields().key])) else: export_path = os.path.join(export_dir, 'export-{}.png'.format(tag)) vis_utils.save_image_array_as_png(image, export_path) summary = tf.Summary(value=[ tf.Summary.Value( tag=tag, image=tf.Summary.Image( encoded_image_string=vis_utils.encode_image_array_as_png_str( image))) ]) summary_writer = tf.summary.FileWriterCache.get(summary_dir) summary_writer.add_summary(summary, global_step) tf.logging.info('Detection visualizations written to summary with tag %s.', tag) def _run_checkpoint_once(tensor_dict, evaluators=None, batch_processor=None, checkpoint_dirs=None, variables_to_restore=None, restore_fn=None, num_batches=1, master='', save_graph=False, save_graph_dir='', losses_dict=None, eval_export_path=None): """Evaluates metrics defined in evaluators and returns summaries. This function loads the latest checkpoint in checkpoint_dirs and evaluates all metrics defined in evaluators. The metrics are processed in batch by the batch_processor. Args: tensor_dict: a dictionary holding tensors representing a batch of detections and corresponding groundtruth annotations. evaluators: a list of object of type DetectionEvaluator to be used for evaluation. Note that the metric names produced by different evaluators must be unique. batch_processor: a function taking four arguments: 1. tensor_dict: the same tensor_dict that is passed in as the first argument to this function. 2. sess: a tensorflow session 3. batch_index: an integer representing the index of the batch amongst all batches By default, batch_processor is None, which defaults to running: return sess.run(tensor_dict) To skip an image, it suffices to return an empty dictionary in place of result_dict. checkpoint_dirs: list of directories to load into an EnsembleModel. If it has only one directory, EnsembleModel will not be used -- a DetectionModel will be instantiated directly. Not used if restore_fn is set. variables_to_restore: None, or a dictionary mapping variable names found in a checkpoint to model variables. The dictionary would normally be generated by creating a tf.train.ExponentialMovingAverage object and calling its variables_to_restore() method. Not used if restore_fn is set. restore_fn: None, or a function that takes a tf.Session object and correctly restores all necessary variables from the correct checkpoint file. If None, attempts to restore from the first directory in checkpoint_dirs. num_batches: the number of batches to use for evaluation. master: the location of the Tensorflow session. save_graph: whether or not the Tensorflow graph is stored as a pbtxt file. save_graph_dir: where to store the Tensorflow graph on disk. If save_graph is True this must be non-empty. losses_dict: optional dictionary of scalar detection losses. eval_export_path: Path for saving a json file that contains the detection results in json format. Returns: global_step: the count of global steps. all_evaluator_metrics: A dictionary containing metric names and values. Raises: ValueError: if restore_fn is None and checkpoint_dirs doesn't have at least one element. ValueError: if save_graph is True and save_graph_dir is not defined. """ if save_graph and not save_graph_dir: raise ValueError('`save_graph_dir` must be defined.') sess = tf.Session(master, graph=tf.get_default_graph()) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) sess.run(tf.tables_initializer()) if restore_fn: restore_fn(sess) else: if not checkpoint_dirs: raise ValueError('`checkpoint_dirs` must have at least one entry.') checkpoint_file = tf.train.latest_checkpoint(checkpoint_dirs[0]) saver = tf.train.Saver(variables_to_restore) saver.restore(sess, checkpoint_file) if save_graph: tf.train.write_graph(sess.graph_def, save_graph_dir, 'eval.pbtxt') counters = {'skipped': 0, 'success': 0} aggregate_result_losses_dict = collections.defaultdict(list) with tf.contrib.slim.queues.QueueRunners(sess): try: for batch in range(int(num_batches)): if (batch + 1) % 100 == 0: tf.logging.info('Running eval ops batch %d/%d', batch + 1, num_batches) if not batch_processor: try: if not losses_dict: losses_dict = {} result_dict, result_losses_dict = sess.run([tensor_dict, losses_dict]) counters['success'] += 1 except tf.errors.InvalidArgumentError: tf.logging.info('Skipping image') counters['skipped'] += 1 result_dict = {} else: result_dict, result_losses_dict = batch_processor( tensor_dict, sess, batch, counters, losses_dict=losses_dict) if not result_dict: continue for key, value in iter(result_losses_dict.items()): aggregate_result_losses_dict[key].append(value) for evaluator in evaluators: # TODO(b/65130867): Use image_id tensor once we fix the input data # decoders to return correct image_id. # TODO(akuznetsa): result_dict contains batches of images, while # add_single_ground_truth_image_info expects a single image. Fix if (isinstance(result_dict, dict) and fields.InputDataFields.key in result_dict and result_dict[fields.InputDataFields.key]): image_id = result_dict[fields.InputDataFields.key] else: image_id = batch evaluator.add_single_ground_truth_image_info( image_id=image_id, groundtruth_dict=result_dict) evaluator.add_single_detected_image_info( image_id=image_id, detections_dict=result_dict) tf.logging.info('Running eval batches done.') except tf.errors.OutOfRangeError: tf.logging.info('Done evaluating -- epoch limit reached') finally: # When done, ask the threads to stop. tf.logging.info('# success: %d', counters['success']) tf.logging.info('# skipped: %d', counters['skipped']) all_evaluator_metrics = {} if eval_export_path and eval_export_path is not None: for evaluator in evaluators: if (isinstance(evaluator, coco_evaluation.CocoDetectionEvaluator) or isinstance(evaluator, coco_evaluation.CocoMaskEvaluator)): tf.logging.info('Started dumping to json file.') evaluator.dump_detections_to_json_file( json_output_path=eval_export_path) tf.logging.info('Finished dumping to json file.') for evaluator in evaluators: metrics = evaluator.evaluate() evaluator.clear() if any(key in all_evaluator_metrics for key in metrics): raise ValueError('Metric names between evaluators must not collide.') all_evaluator_metrics.update(metrics) global_step = tf.train.global_step(sess, tf.train.get_global_step()) for key, value in iter(aggregate_result_losses_dict.items()): all_evaluator_metrics['Losses/' + key] = np.mean(value) sess.close() return (global_step, all_evaluator_metrics) # TODO(rathodv): Add tests. def repeated_checkpoint_run(tensor_dict, summary_dir, evaluators, batch_processor=None, checkpoint_dirs=None, variables_to_restore=None, restore_fn=None, num_batches=1, eval_interval_secs=120, max_number_of_evaluations=None, master='', save_graph=False, save_graph_dir='', losses_dict=None, eval_export_path=None): """Periodically evaluates desired tensors using checkpoint_dirs or restore_fn. This function repeatedly loads a checkpoint and evaluates a desired set of tensors (provided by tensor_dict) and hands the resulting numpy arrays to a function result_processor which can be used to further process/save/visualize the results. Args: tensor_dict: a dictionary holding tensors representing a batch of detections and corresponding groundtruth annotations. summary_dir: a directory to write metrics summaries. evaluators: a list of object of type DetectionEvaluator to be used for evaluation. Note that the metric names produced by different evaluators must be unique. batch_processor: a function taking three arguments: 1. tensor_dict: the same tensor_dict that is passed in as the first argument to this function. 2. sess: a tensorflow session 3. batch_index: an integer representing the index of the batch amongst all batches By default, batch_processor is None, which defaults to running: return sess.run(tensor_dict) checkpoint_dirs: list of directories to load into a DetectionModel or an EnsembleModel if restore_fn isn't set. Also used to determine when to run next evaluation. Must have at least one element. variables_to_restore: None, or a dictionary mapping variable names found in a checkpoint to model variables. The dictionary would normally be generated by creating a tf.train.ExponentialMovingAverage object and calling its variables_to_restore() method. Not used if restore_fn is set. restore_fn: a function that takes a tf.Session object and correctly restores all necessary variables from the correct checkpoint file. num_batches: the number of batches to use for evaluation. eval_interval_secs: the number of seconds between each evaluation run. max_number_of_evaluations: the max number of iterations of the evaluation. If the value is left as None the evaluation continues indefinitely. master: the location of the Tensorflow session. save_graph: whether or not the Tensorflow graph is saved as a pbtxt file. save_graph_dir: where to save on disk the Tensorflow graph. If store_graph is True this must be non-empty. losses_dict: optional dictionary of scalar detection losses. eval_export_path: Path for saving a json file that contains the detection results in json format. Returns: metrics: A dictionary containing metric names and values in the latest evaluation. Raises: ValueError: if max_num_of_evaluations is not None or a positive number. ValueError: if checkpoint_dirs doesn't have at least one element. """ if max_number_of_evaluations and max_number_of_evaluations <= 0: raise ValueError( '`number_of_steps` must be either None or a positive number.') if not checkpoint_dirs: raise ValueError('`checkpoint_dirs` must have at least one entry.') last_evaluated_model_path = None number_of_evaluations = 0 while True: start = time.time() tf.logging.info('Starting evaluation at ' + time.strftime( '%Y-%m-%d-%H:%M:%S', time.gmtime())) model_path = tf.train.latest_checkpoint(checkpoint_dirs[0]) if not model_path: tf.logging.info('No model found in %s. Will try again in %d seconds', checkpoint_dirs[0], eval_interval_secs) elif model_path == last_evaluated_model_path: tf.logging.info('Found already evaluated checkpoint. Will try again in ' '%d seconds', eval_interval_secs) else: last_evaluated_model_path = model_path global_step, metrics = _run_checkpoint_once( tensor_dict, evaluators, batch_processor, checkpoint_dirs, variables_to_restore, restore_fn, num_batches, master, save_graph, save_graph_dir, losses_dict=losses_dict, eval_export_path=eval_export_path) write_metrics(metrics, global_step, summary_dir) number_of_evaluations += 1 if (max_number_of_evaluations and number_of_evaluations >= max_number_of_evaluations): tf.logging.info('Finished evaluation!') break time_to_next_eval = start + eval_interval_secs - time.time() if time_to_next_eval > 0: time.sleep(time_to_next_eval) return metrics def _scale_box_to_absolute(args): boxes, image_shape = args return box_list_ops.to_absolute_coordinates( box_list.BoxList(boxes), image_shape[0], image_shape[1]).get() def _resize_detection_masks(args): detection_boxes, detection_masks, image_shape = args detection_masks_reframed = ops.reframe_box_masks_to_image_masks( detection_masks, detection_boxes, image_shape[0], image_shape[1]) return tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8) def _resize_groundtruth_masks(args): mask, image_shape = args mask = tf.expand_dims(mask, 3) mask = tf.image.resize_images( mask, image_shape, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, align_corners=True) return tf.cast(tf.squeeze(mask, 3), tf.uint8) def _scale_keypoint_to_absolute(args): keypoints, image_shape = args return keypoint_ops.scale(keypoints, image_shape[0], image_shape[1]) def result_dict_for_single_example(image, key, detections, groundtruth=None, class_agnostic=False, scale_to_absolute=False): """Merges all detection and groundtruth information for a single example. Note that evaluation tools require classes that are 1-indexed, and so this function performs the offset. If `class_agnostic` is True, all output classes have label 1. Args: image: A single 4D uint8 image tensor of shape [1, H, W, C]. key: A single string tensor identifying the image. detections: A dictionary of detections, returned from DetectionModel.postprocess(). groundtruth: (Optional) Dictionary of groundtruth items, with fields: 'groundtruth_boxes': [num_boxes, 4] float32 tensor of boxes, in normalized coordinates. 'groundtruth_classes': [num_boxes] int64 tensor of 1-indexed classes. 'groundtruth_area': [num_boxes] float32 tensor of bbox area. (Optional) 'groundtruth_is_crowd': [num_boxes] int64 tensor. (Optional) 'groundtruth_difficult': [num_boxes] int64 tensor. (Optional) 'groundtruth_group_of': [num_boxes] int64 tensor. (Optional) 'groundtruth_instance_masks': 3D int64 tensor of instance masks (Optional). class_agnostic: Boolean indicating whether the detections are class-agnostic (i.e. binary). Default False. scale_to_absolute: Boolean indicating whether boxes and keypoints should be scaled to absolute coordinates. Note that for IoU based evaluations, it does not matter whether boxes are expressed in absolute or relative coordinates. Default False. Returns: A dictionary with: 'original_image': A [1, H, W, C] uint8 image tensor. 'key': A string tensor with image identifier. 'detection_boxes': [max_detections, 4] float32 tensor of boxes, in normalized or absolute coordinates, depending on the value of `scale_to_absolute`. 'detection_scores': [max_detections] float32 tensor of scores. 'detection_classes': [max_detections] int64 tensor of 1-indexed classes. 'detection_masks': [max_detections, H, W] float32 tensor of binarized masks, reframed to full image masks. 'groundtruth_boxes': [num_boxes, 4] float32 tensor of boxes, in normalized or absolute coordinates, depending on the value of `scale_to_absolute`. (Optional) 'groundtruth_classes': [num_boxes] int64 tensor of 1-indexed classes. (Optional) 'groundtruth_area': [num_boxes] float32 tensor of bbox area. (Optional) 'groundtruth_is_crowd': [num_boxes] int64 tensor. (Optional) 'groundtruth_difficult': [num_boxes] int64 tensor. (Optional) 'groundtruth_group_of': [num_boxes] int64 tensor. (Optional) 'groundtruth_instance_masks': 3D int64 tensor of instance masks (Optional). """ if groundtruth: max_gt_boxes = tf.shape( groundtruth[fields.InputDataFields.groundtruth_boxes])[0] for gt_key in groundtruth: # expand groundtruth dict along the batch dimension. groundtruth[gt_key] = tf.expand_dims(groundtruth[gt_key], 0) for detection_key in detections: detections[detection_key] = tf.expand_dims( detections[detection_key][0], axis=0) batched_output_dict = result_dict_for_batched_example( image, tf.expand_dims(key, 0), detections, groundtruth, class_agnostic, scale_to_absolute, max_gt_boxes=max_gt_boxes) exclude_keys = [ fields.InputDataFields.original_image, fields.DetectionResultFields.num_detections, fields.InputDataFields.num_groundtruth_boxes ] output_dict = { fields.InputDataFields.original_image: batched_output_dict[fields.InputDataFields.original_image] } for key in batched_output_dict: # remove the batch dimension. if key not in exclude_keys: output_dict[key] = tf.squeeze(batched_output_dict[key], 0) return output_dict def result_dict_for_batched_example(images, keys, detections, groundtruth=None, class_agnostic=False, scale_to_absolute=False, original_image_spatial_shapes=None, true_image_shapes=None, max_gt_boxes=None): """Merges all detection and groundtruth information for a single example. Note that evaluation tools require classes that are 1-indexed, and so this function performs the offset. If `class_agnostic` is True, all output classes have label 1. Args: images: A single 4D uint8 image tensor of shape [batch_size, H, W, C]. keys: A [batch_size] string tensor with image identifier. detections: A dictionary of detections, returned from DetectionModel.postprocess(). groundtruth: (Optional) Dictionary of groundtruth items, with fields: 'groundtruth_boxes': [batch_size, max_number_of_boxes, 4] float32 tensor of boxes, in normalized coordinates. 'groundtruth_classes': [batch_size, max_number_of_boxes] int64 tensor of 1-indexed classes. 'groundtruth_area': [batch_size, max_number_of_boxes] float32 tensor of bbox area. (Optional) 'groundtruth_is_crowd':[batch_size, max_number_of_boxes] int64 tensor. (Optional) 'groundtruth_difficult': [batch_size, max_number_of_boxes] int64 tensor. (Optional) 'groundtruth_group_of': [batch_size, max_number_of_boxes] int64 tensor. (Optional) 'groundtruth_instance_masks': 4D int64 tensor of instance masks (Optional). class_agnostic: Boolean indicating whether the detections are class-agnostic (i.e. binary). Default False. scale_to_absolute: Boolean indicating whether boxes and keypoints should be scaled to absolute coordinates. Note that for IoU based evaluations, it does not matter whether boxes are expressed in absolute or relative coordinates. Default False. original_image_spatial_shapes: A 2D int32 tensor of shape [batch_size, 2] used to resize the image. When set to None, the image size is retained. true_image_shapes: A 2D int32 tensor of shape [batch_size, 3] containing the size of the unpadded original_image. max_gt_boxes: [batch_size] tensor representing the maximum number of groundtruth boxes to pad. Returns: A dictionary with: 'original_image': A [batch_size, H, W, C] uint8 image tensor. 'original_image_spatial_shape': A [batch_size, 2] tensor containing the original image sizes. 'true_image_shape': A [batch_size, 3] tensor containing the size of the unpadded original_image. 'key': A [batch_size] string tensor with image identifier. 'detection_boxes': [batch_size, max_detections, 4] float32 tensor of boxes, in normalized or absolute coordinates, depending on the value of `scale_to_absolute`. 'detection_scores': [batch_size, max_detections] float32 tensor of scores. 'detection_classes': [batch_size, max_detections] int64 tensor of 1-indexed classes. 'detection_masks': [batch_size, max_detections, H, W] float32 tensor of binarized masks, reframed to full image masks. 'num_detections': [batch_size] int64 tensor containing number of valid detections. 'groundtruth_boxes': [batch_size, num_boxes, 4] float32 tensor of boxes, in normalized or absolute coordinates, depending on the value of `scale_to_absolute`. (Optional) 'groundtruth_classes': [batch_size, num_boxes] int64 tensor of 1-indexed classes. (Optional) 'groundtruth_area': [batch_size, num_boxes] float32 tensor of bbox area. (Optional) 'groundtruth_is_crowd': [batch_size, num_boxes] int64 tensor. (Optional) 'groundtruth_difficult': [batch_size, num_boxes] int64 tensor. (Optional) 'groundtruth_group_of': [batch_size, num_boxes] int64 tensor. (Optional) 'groundtruth_instance_masks': 4D int64 tensor of instance masks (Optional). 'num_groundtruth_boxes': [batch_size] tensor containing the maximum number of groundtruth boxes per image. Raises: ValueError: if original_image_spatial_shape is not 2D int32 tensor of shape [2]. ValueError: if true_image_shapes is not 2D int32 tensor of shape [3]. """ label_id_offset = 1 # Applying label id offset (b/63711816) input_data_fields = fields.InputDataFields if original_image_spatial_shapes is None: original_image_spatial_shapes = tf.tile( tf.expand_dims(tf.shape(images)[1:3], axis=0), multiples=[tf.shape(images)[0], 1]) else: if (len(original_image_spatial_shapes.shape) != 2 and original_image_spatial_shapes.shape[1] != 2): raise ValueError( '`original_image_spatial_shape` should be a 2D tensor of shape ' '[batch_size, 2].') if true_image_shapes is None: true_image_shapes = tf.tile( tf.expand_dims(tf.shape(images)[1:4], axis=0), multiples=[tf.shape(images)[0], 1]) else: if (len(true_image_shapes.shape) != 2 and true_image_shapes.shape[1] != 3): raise ValueError('`true_image_shapes` should be a 2D tensor of ' 'shape [batch_size, 3].') output_dict = { input_data_fields.original_image: images, input_data_fields.key: keys, input_data_fields.original_image_spatial_shape: ( original_image_spatial_shapes), input_data_fields.true_image_shape: true_image_shapes } detection_fields = fields.DetectionResultFields detection_boxes = detections[detection_fields.detection_boxes] detection_scores = detections[detection_fields.detection_scores] num_detections = tf.to_int32(detections[detection_fields.num_detections]) if class_agnostic: detection_classes = tf.ones_like(detection_scores, dtype=tf.int64) else: detection_classes = ( tf.to_int64(detections[detection_fields.detection_classes]) + label_id_offset) if scale_to_absolute: output_dict[detection_fields.detection_boxes] = ( shape_utils.static_or_dynamic_map_fn( _scale_box_to_absolute, elems=[detection_boxes, original_image_spatial_shapes], dtype=tf.float32)) else: output_dict[detection_fields.detection_boxes] = detection_boxes output_dict[detection_fields.detection_classes] = detection_classes output_dict[detection_fields.detection_scores] = detection_scores output_dict[detection_fields.num_detections] = num_detections if detection_fields.detection_masks in detections: detection_masks = detections[detection_fields.detection_masks] # TODO(rathodv): This should be done in model's postprocess # function ideally. output_dict[detection_fields.detection_masks] = ( shape_utils.static_or_dynamic_map_fn( _resize_detection_masks, elems=[detection_boxes, detection_masks, original_image_spatial_shapes], dtype=tf.uint8)) if detection_fields.detection_keypoints in detections: detection_keypoints = detections[detection_fields.detection_keypoints] output_dict[detection_fields.detection_keypoints] = detection_keypoints if scale_to_absolute: output_dict[detection_fields.detection_keypoints] = ( shape_utils.static_or_dynamic_map_fn( _scale_keypoint_to_absolute, elems=[detection_keypoints, original_image_spatial_shapes], dtype=tf.float32)) if groundtruth: if max_gt_boxes is None: if input_data_fields.num_groundtruth_boxes in groundtruth: max_gt_boxes = groundtruth[input_data_fields.num_groundtruth_boxes] else: raise ValueError( 'max_gt_boxes must be provided when processing batched examples.') if input_data_fields.groundtruth_instance_masks in groundtruth: masks = groundtruth[input_data_fields.groundtruth_instance_masks] groundtruth[input_data_fields.groundtruth_instance_masks] = ( shape_utils.static_or_dynamic_map_fn( _resize_groundtruth_masks, elems=[masks, original_image_spatial_shapes], dtype=tf.uint8)) output_dict.update(groundtruth) if scale_to_absolute: groundtruth_boxes = groundtruth[input_data_fields.groundtruth_boxes] output_dict[input_data_fields.groundtruth_boxes] = ( shape_utils.static_or_dynamic_map_fn( _scale_box_to_absolute, elems=[groundtruth_boxes, original_image_spatial_shapes], dtype=tf.float32)) # For class-agnostic models, groundtruth classes all become 1. if class_agnostic: groundtruth_classes = groundtruth[input_data_fields.groundtruth_classes] groundtruth_classes = tf.ones_like(groundtruth_classes, dtype=tf.int64) output_dict[input_data_fields.groundtruth_classes] = groundtruth_classes output_dict[input_data_fields.num_groundtruth_boxes] = max_gt_boxes return output_dict def get_evaluators(eval_config, categories, evaluator_options=None): """Returns the evaluator class according to eval_config, valid for categories. Args: eval_config: An `eval_pb2.EvalConfig`. categories: A list of dicts, each of which has the following keys - 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog'. evaluator_options: A dictionary of metric names (see EVAL_METRICS_CLASS_DICT) to `DetectionEvaluator` initialization keyword arguments. For example: evalator_options = { 'coco_detection_metrics': {'include_metrics_per_category': True} } Returns: An list of instances of DetectionEvaluator. Raises: ValueError: if metric is not in the metric class dictionary. """ evaluator_options = evaluator_options or {} eval_metric_fn_keys = eval_config.metrics_set if not eval_metric_fn_keys: eval_metric_fn_keys = [EVAL_DEFAULT_METRIC] evaluators_list = [] for eval_metric_fn_key in eval_metric_fn_keys: if eval_metric_fn_key not in EVAL_METRICS_CLASS_DICT: raise ValueError('Metric not found: {}'.format(eval_metric_fn_key)) kwargs_dict = (evaluator_options[eval_metric_fn_key] if eval_metric_fn_key in evaluator_options else {}) evaluators_list.append(EVAL_METRICS_CLASS_DICT[eval_metric_fn_key]( categories, **kwargs_dict)) return evaluators_list def get_eval_metric_ops_for_evaluators(eval_config, categories, eval_dict): """Returns eval metrics ops to use with `tf.estimator.EstimatorSpec`. Args: eval_config: An `eval_pb2.EvalConfig`. categories: A list of dicts, each of which has the following keys - 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog'. eval_dict: An evaluation dictionary, returned from result_dict_for_single_example(). Returns: A dictionary of metric names to tuple of value_op and update_op that can be used as eval metric ops in tf.EstimatorSpec. """ eval_metric_ops = {} evaluator_options = evaluator_options_from_eval_config(eval_config) evaluators_list = get_evaluators(eval_config, categories, evaluator_options) for evaluator in evaluators_list: eval_metric_ops.update(evaluator.get_estimator_eval_metric_ops( eval_dict)) return eval_metric_ops def evaluator_options_from_eval_config(eval_config): """Produces a dictionary of evaluation options for each eval metric. Args: eval_config: An `eval_pb2.EvalConfig`. Returns: evaluator_options: A dictionary of metric names (see EVAL_METRICS_CLASS_DICT) to `DetectionEvaluator` initialization keyword arguments. For example: evalator_options = { 'coco_detection_metrics': {'include_metrics_per_category': True} } """ eval_metric_fn_keys = eval_config.metrics_set evaluator_options = {} for eval_metric_fn_key in eval_metric_fn_keys: if eval_metric_fn_key in ('coco_detection_metrics', 'coco_mask_metrics'): evaluator_options[eval_metric_fn_key] = { 'include_metrics_per_category': ( eval_config.include_metrics_per_category) } return evaluator_options
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/eval_util.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== # # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Model input function for tf-learn object detection model.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import tensorflow as tf from object_detection.builders import dataset_builder from object_detection.builders import image_resizer_builder from object_detection.builders import model_builder from object_detection.builders import preprocessor_builder from object_detection.core import preprocessor from object_detection.core import standard_fields as fields from object_detection.data_decoders import tf_example_decoder from object_detection.protos import eval_pb2 from object_detection.protos import input_reader_pb2 from object_detection.protos import model_pb2 from object_detection.protos import train_pb2 from object_detection.utils import config_util from object_detection.utils import ops as util_ops from object_detection.utils import shape_utils HASH_KEY = 'hash' HASH_BINS = 1 << 31 SERVING_FED_EXAMPLE_KEY = 'serialized_example' # A map of names to methods that help build the input pipeline. INPUT_BUILDER_UTIL_MAP = { 'dataset_build': dataset_builder.build, } def transform_input_data(tensor_dict, model_preprocess_fn, image_resizer_fn, num_classes, data_augmentation_fn=None, merge_multiple_boxes=False, retain_original_image=False, use_bfloat16=False): """A single function that is responsible for all input data transformations. Data transformation functions are applied in the following order. 1. If key fields.InputDataFields.image_additional_channels is present in tensor_dict, the additional channels will be merged into fields.InputDataFields.image. 2. data_augmentation_fn (optional): applied on tensor_dict. 3. model_preprocess_fn: applied only on image tensor in tensor_dict. 4. image_resizer_fn: applied on original image and instance mask tensor in tensor_dict. 5. one_hot_encoding: applied to classes tensor in tensor_dict. 6. merge_multiple_boxes (optional): when groundtruth boxes are exactly the same they can be merged into a single box with an associated k-hot class label. Args: tensor_dict: dictionary containing input tensors keyed by fields.InputDataFields. model_preprocess_fn: model's preprocess function to apply on image tensor. This function must take in a 4-D float tensor and return a 4-D preprocess float tensor and a tensor containing the true image shape. image_resizer_fn: image resizer function to apply on groundtruth instance `masks. This function must take a 3-D float tensor of an image and a 3-D tensor of instance masks and return a resized version of these along with the true shapes. num_classes: number of max classes to one-hot (or k-hot) encode the class labels. data_augmentation_fn: (optional) data augmentation function to apply on input `tensor_dict`. merge_multiple_boxes: (optional) whether to merge multiple groundtruth boxes and classes for a given image if the boxes are exactly the same. retain_original_image: (optional) whether to retain original image in the output dictionary. use_bfloat16: (optional) a bool, whether to use bfloat16 in training. Returns: A dictionary keyed by fields.InputDataFields containing the tensors obtained after applying all the transformations. """ if fields.InputDataFields.groundtruth_boxes in tensor_dict: tensor_dict = util_ops.filter_groundtruth_with_nan_box_coordinates( tensor_dict) if fields.InputDataFields.image_additional_channels in tensor_dict: channels = tensor_dict[fields.InputDataFields.image_additional_channels] tensor_dict[fields.InputDataFields.image] = tf.concat( [tensor_dict[fields.InputDataFields.image], channels], axis=2) if retain_original_image: tensor_dict[fields.InputDataFields.original_image] = tf.cast( image_resizer_fn(tensor_dict[fields.InputDataFields.image], None)[0], tf.uint8) # Apply data augmentation ops. if data_augmentation_fn is not None: tensor_dict = data_augmentation_fn(tensor_dict) # Apply model preprocessing ops and resize instance masks. image = tensor_dict[fields.InputDataFields.image] preprocessed_resized_image, true_image_shape = model_preprocess_fn( tf.expand_dims(tf.to_float(image), axis=0)) if use_bfloat16: preprocessed_resized_image = tf.cast( preprocessed_resized_image, tf.bfloat16) tensor_dict[fields.InputDataFields.image] = tf.squeeze( preprocessed_resized_image, axis=0) tensor_dict[fields.InputDataFields.true_image_shape] = tf.squeeze( true_image_shape, axis=0) if fields.InputDataFields.groundtruth_instance_masks in tensor_dict: masks = tensor_dict[fields.InputDataFields.groundtruth_instance_masks] _, resized_masks, _ = image_resizer_fn(image, masks) if use_bfloat16: resized_masks = tf.cast(resized_masks, tf.bfloat16) tensor_dict[fields.InputDataFields. groundtruth_instance_masks] = resized_masks # Transform groundtruth classes to one hot encodings. label_offset = 1 zero_indexed_groundtruth_classes = tensor_dict[ fields.InputDataFields.groundtruth_classes] - label_offset tensor_dict[fields.InputDataFields.groundtruth_classes] = tf.one_hot( zero_indexed_groundtruth_classes, num_classes) if fields.InputDataFields.groundtruth_confidences in tensor_dict: groundtruth_confidences = tensor_dict[ fields.InputDataFields.groundtruth_confidences] # Map the confidences to the one-hot encoding of classes tensor_dict[fields.InputDataFields.groundtruth_confidences] = ( tf.reshape(groundtruth_confidences, [-1, 1]) * tensor_dict[fields.InputDataFields.groundtruth_classes]) else: groundtruth_confidences = tf.ones_like( zero_indexed_groundtruth_classes, dtype=tf.float32) tensor_dict[fields.InputDataFields.groundtruth_confidences] = ( tensor_dict[fields.InputDataFields.groundtruth_classes]) if merge_multiple_boxes: merged_boxes, merged_classes, merged_confidences, _ = ( util_ops.merge_boxes_with_multiple_labels( tensor_dict[fields.InputDataFields.groundtruth_boxes], zero_indexed_groundtruth_classes, groundtruth_confidences, num_classes)) merged_classes = tf.cast(merged_classes, tf.float32) tensor_dict[fields.InputDataFields.groundtruth_boxes] = merged_boxes tensor_dict[fields.InputDataFields.groundtruth_classes] = merged_classes tensor_dict[fields.InputDataFields.groundtruth_confidences] = ( merged_confidences) if fields.InputDataFields.groundtruth_boxes in tensor_dict: tensor_dict[fields.InputDataFields.num_groundtruth_boxes] = tf.shape( tensor_dict[fields.InputDataFields.groundtruth_boxes])[0] return tensor_dict def pad_input_data_to_static_shapes(tensor_dict, max_num_boxes, num_classes, spatial_image_shape=None): """Pads input tensors to static shapes. Args: tensor_dict: Tensor dictionary of input data max_num_boxes: Max number of groundtruth boxes needed to compute shapes for padding. num_classes: Number of classes in the dataset needed to compute shapes for padding. spatial_image_shape: A list of two integers of the form [height, width] containing expected spatial shape of the image. Returns: A dictionary keyed by fields.InputDataFields containing padding shapes for tensors in the dataset. Raises: ValueError: If groundtruth classes is neither rank 1 nor rank 2. """ if not spatial_image_shape or spatial_image_shape == [-1, -1]: height, width = None, None else: height, width = spatial_image_shape # pylint: disable=unpacking-non-sequence num_additional_channels = 0 if fields.InputDataFields.image_additional_channels in tensor_dict: num_additional_channels = tensor_dict[ fields.InputDataFields.image_additional_channels].shape[2].value num_image_channels = 3 if fields.InputDataFields.image in tensor_dict: num_image_channels = tensor_dict[fields.InputDataFields .image].shape[2].value padding_shapes = { # Additional channels are merged before batching. fields.InputDataFields.image: [ height, width, num_image_channels + num_additional_channels ], fields.InputDataFields.original_image_spatial_shape: [2], fields.InputDataFields.image_additional_channels: [ height, width, num_additional_channels ], fields.InputDataFields.source_id: [], fields.InputDataFields.filename: [], fields.InputDataFields.key: [], fields.InputDataFields.groundtruth_difficult: [max_num_boxes], fields.InputDataFields.groundtruth_boxes: [max_num_boxes, 4], fields.InputDataFields.groundtruth_classes: [max_num_boxes, num_classes], fields.InputDataFields.groundtruth_instance_masks: [ max_num_boxes, height, width ], fields.InputDataFields.groundtruth_is_crowd: [max_num_boxes], fields.InputDataFields.groundtruth_group_of: [max_num_boxes], fields.InputDataFields.groundtruth_area: [max_num_boxes], fields.InputDataFields.groundtruth_weights: [max_num_boxes], fields.InputDataFields.groundtruth_confidences: [ max_num_boxes, num_classes ], fields.InputDataFields.num_groundtruth_boxes: [], fields.InputDataFields.groundtruth_label_types: [max_num_boxes], fields.InputDataFields.groundtruth_label_weights: [max_num_boxes], fields.InputDataFields.true_image_shape: [3], fields.InputDataFields.multiclass_scores: [ max_num_boxes, num_classes + 1 if num_classes is not None else None ], fields.InputDataFields.groundtruth_image_classes: [num_classes], fields.InputDataFields.groundtruth_image_confidences: [num_classes], } if fields.InputDataFields.original_image in tensor_dict: padding_shapes[fields.InputDataFields.original_image] = [ height, width, num_image_channels + num_additional_channels ] if fields.InputDataFields.groundtruth_keypoints in tensor_dict: tensor_shape = ( tensor_dict[fields.InputDataFields.groundtruth_keypoints].shape) padding_shape = [max_num_boxes, tensor_shape[1].value, tensor_shape[2].value] padding_shapes[fields.InputDataFields.groundtruth_keypoints] = padding_shape if fields.InputDataFields.groundtruth_keypoint_visibilities in tensor_dict: tensor_shape = tensor_dict[fields.InputDataFields. groundtruth_keypoint_visibilities].shape padding_shape = [max_num_boxes, tensor_shape[1].value] padding_shapes[fields.InputDataFields. groundtruth_keypoint_visibilities] = padding_shape padded_tensor_dict = {} for tensor_name in tensor_dict: padded_tensor_dict[tensor_name] = shape_utils.pad_or_clip_nd( tensor_dict[tensor_name], padding_shapes[tensor_name]) # Make sure that the number of groundtruth boxes now reflects the # padded/clipped tensors. if fields.InputDataFields.num_groundtruth_boxes in padded_tensor_dict: padded_tensor_dict[fields.InputDataFields.num_groundtruth_boxes] = ( tf.minimum( padded_tensor_dict[fields.InputDataFields.num_groundtruth_boxes], max_num_boxes)) return padded_tensor_dict def augment_input_data(tensor_dict, data_augmentation_options): """Applies data augmentation ops to input tensors. Args: tensor_dict: A dictionary of input tensors keyed by fields.InputDataFields. data_augmentation_options: A list of tuples, where each tuple contains a function and a dictionary that contains arguments and their values. Usually, this is the output of core/preprocessor.build. Returns: A dictionary of tensors obtained by applying data augmentation ops to the input tensor dictionary. """ tensor_dict[fields.InputDataFields.image] = tf.expand_dims( tf.to_float(tensor_dict[fields.InputDataFields.image]), 0) include_instance_masks = (fields.InputDataFields.groundtruth_instance_masks in tensor_dict) include_keypoints = (fields.InputDataFields.groundtruth_keypoints in tensor_dict) include_label_weights = (fields.InputDataFields.groundtruth_weights in tensor_dict) include_label_confidences = (fields.InputDataFields.groundtruth_confidences in tensor_dict) tensor_dict = preprocessor.preprocess( tensor_dict, data_augmentation_options, func_arg_map=preprocessor.get_default_func_arg_map( include_label_weights=include_label_weights, include_label_confidences=include_label_confidences, include_instance_masks=include_instance_masks, include_keypoints=include_keypoints)) tensor_dict[fields.InputDataFields.image] = tf.squeeze( tensor_dict[fields.InputDataFields.image], axis=0) return tensor_dict def _get_labels_dict(input_dict): """Extracts labels dict from input dict.""" required_label_keys = [ fields.InputDataFields.num_groundtruth_boxes, fields.InputDataFields.groundtruth_boxes, fields.InputDataFields.groundtruth_classes, fields.InputDataFields.groundtruth_weights, ] labels_dict = {} for key in required_label_keys: labels_dict[key] = input_dict[key] optional_label_keys = [ fields.InputDataFields.groundtruth_confidences, fields.InputDataFields.groundtruth_keypoints, fields.InputDataFields.groundtruth_instance_masks, fields.InputDataFields.groundtruth_area, fields.InputDataFields.groundtruth_is_crowd, fields.InputDataFields.groundtruth_difficult ] for key in optional_label_keys: if key in input_dict: labels_dict[key] = input_dict[key] if fields.InputDataFields.groundtruth_difficult in labels_dict: labels_dict[fields.InputDataFields.groundtruth_difficult] = tf.cast( labels_dict[fields.InputDataFields.groundtruth_difficult], tf.int32) return labels_dict def _replace_empty_string_with_random_number(string_tensor): """Returns string unchanged if non-empty, and random string tensor otherwise. The random string is an integer 0 and 2**63 - 1, casted as string. Args: string_tensor: A tf.tensor of dtype string. Returns: out_string: A tf.tensor of dtype string. If string_tensor contains the empty string, out_string will contain a random integer casted to a string. Otherwise string_tensor is returned unchanged. """ empty_string = tf.constant('', dtype=tf.string, name='EmptyString') random_source_id = tf.as_string( tf.random_uniform(shape=[], maxval=2**63 - 1, dtype=tf.int64)) out_string = tf.cond( tf.equal(string_tensor, empty_string), true_fn=lambda: random_source_id, false_fn=lambda: string_tensor) return out_string def _get_features_dict(input_dict): """Extracts features dict from input dict.""" source_id = _replace_empty_string_with_random_number( input_dict[fields.InputDataFields.source_id]) hash_from_source_id = tf.string_to_hash_bucket_fast(source_id, HASH_BINS) features = { fields.InputDataFields.image: input_dict[fields.InputDataFields.image], HASH_KEY: tf.cast(hash_from_source_id, tf.int32), fields.InputDataFields.true_image_shape: input_dict[fields.InputDataFields.true_image_shape], fields.InputDataFields.original_image_spatial_shape: input_dict[fields.InputDataFields.original_image_spatial_shape] } if fields.InputDataFields.original_image in input_dict: features[fields.InputDataFields.original_image] = input_dict[ fields.InputDataFields.original_image] return features def create_train_input_fn(train_config, train_input_config, model_config): """Creates a train `input` function for `Estimator`. Args: train_config: A train_pb2.TrainConfig. train_input_config: An input_reader_pb2.InputReader. model_config: A model_pb2.DetectionModel. Returns: `input_fn` for `Estimator` in TRAIN mode. """ def _train_input_fn(params=None): """Returns `features` and `labels` tensor dictionaries for training. Args: params: Parameter dictionary passed from the estimator. Returns: A tf.data.Dataset that holds (features, labels) tuple. features: Dictionary of feature tensors. features[fields.InputDataFields.image] is a [batch_size, H, W, C] float32 tensor with preprocessed images. features[HASH_KEY] is a [batch_size] int32 tensor representing unique identifiers for the images. features[fields.InputDataFields.true_image_shape] is a [batch_size, 3] int32 tensor representing the true image shapes, as preprocessed images could be padded. features[fields.InputDataFields.original_image] (optional) is a [batch_size, H, W, C] float32 tensor with original images. labels: Dictionary of groundtruth tensors. labels[fields.InputDataFields.num_groundtruth_boxes] is a [batch_size] int32 tensor indicating the number of groundtruth boxes. labels[fields.InputDataFields.groundtruth_boxes] is a [batch_size, num_boxes, 4] float32 tensor containing the corners of the groundtruth boxes. labels[fields.InputDataFields.groundtruth_classes] is a [batch_size, num_boxes, num_classes] float32 one-hot tensor of classes. labels[fields.InputDataFields.groundtruth_weights] is a [batch_size, num_boxes] float32 tensor containing groundtruth weights for the boxes. -- Optional -- labels[fields.InputDataFields.groundtruth_instance_masks] is a [batch_size, num_boxes, H, W] float32 tensor containing only binary values, which represent instance masks for objects. labels[fields.InputDataFields.groundtruth_keypoints] is a [batch_size, num_boxes, num_keypoints, 2] float32 tensor containing keypoints for each box. Raises: TypeError: if the `train_config`, `train_input_config` or `model_config` are not of the correct type. """ if not isinstance(train_config, train_pb2.TrainConfig): raise TypeError('For training mode, the `train_config` must be a ' 'train_pb2.TrainConfig.') if not isinstance(train_input_config, input_reader_pb2.InputReader): raise TypeError('The `train_input_config` must be a ' 'input_reader_pb2.InputReader.') if not isinstance(model_config, model_pb2.DetectionModel): raise TypeError('The `model_config` must be a ' 'model_pb2.DetectionModel.') def transform_and_pad_input_data_fn(tensor_dict): """Combines transform and pad operation.""" data_augmentation_options = [ preprocessor_builder.build(step) for step in train_config.data_augmentation_options ] data_augmentation_fn = functools.partial( augment_input_data, data_augmentation_options=data_augmentation_options) model = model_builder.build(model_config, is_training=True) image_resizer_config = config_util.get_image_resizer_config(model_config) image_resizer_fn = image_resizer_builder.build(image_resizer_config) transform_data_fn = functools.partial( transform_input_data, model_preprocess_fn=model.preprocess, image_resizer_fn=image_resizer_fn, num_classes=config_util.get_number_of_classes(model_config), data_augmentation_fn=data_augmentation_fn, merge_multiple_boxes=train_config.merge_multiple_label_boxes, retain_original_image=train_config.retain_original_images, use_bfloat16=train_config.use_bfloat16) tensor_dict = pad_input_data_to_static_shapes( tensor_dict=transform_data_fn(tensor_dict), max_num_boxes=train_input_config.max_number_of_boxes, num_classes=config_util.get_number_of_classes(model_config), spatial_image_shape=config_util.get_spatial_image_size( image_resizer_config)) return (_get_features_dict(tensor_dict), _get_labels_dict(tensor_dict)) dataset = INPUT_BUILDER_UTIL_MAP['dataset_build']( train_input_config, transform_input_data_fn=transform_and_pad_input_data_fn, batch_size=params['batch_size'] if params else train_config.batch_size, multi_gpu=True) return dataset return _train_input_fn def create_eval_input_fn(eval_config, eval_input_config, model_config): """Creates an eval `input` function for `Estimator`. Args: eval_config: An eval_pb2.EvalConfig. eval_input_config: An input_reader_pb2.InputReader. model_config: A model_pb2.DetectionModel. Returns: `input_fn` for `Estimator` in EVAL mode. """ def _eval_input_fn(params=None): """Returns `features` and `labels` tensor dictionaries for evaluation. Args: params: Parameter dictionary passed from the estimator. Returns: A tf.data.Dataset that holds (features, labels) tuple. features: Dictionary of feature tensors. features[fields.InputDataFields.image] is a [1, H, W, C] float32 tensor with preprocessed images. features[HASH_KEY] is a [1] int32 tensor representing unique identifiers for the images. features[fields.InputDataFields.true_image_shape] is a [1, 3] int32 tensor representing the true image shapes, as preprocessed images could be padded. features[fields.InputDataFields.original_image] is a [1, H', W', C] float32 tensor with the original image. labels: Dictionary of groundtruth tensors. labels[fields.InputDataFields.groundtruth_boxes] is a [1, num_boxes, 4] float32 tensor containing the corners of the groundtruth boxes. labels[fields.InputDataFields.groundtruth_classes] is a [num_boxes, num_classes] float32 one-hot tensor of classes. labels[fields.InputDataFields.groundtruth_area] is a [1, num_boxes] float32 tensor containing object areas. labels[fields.InputDataFields.groundtruth_is_crowd] is a [1, num_boxes] bool tensor indicating if the boxes enclose a crowd. labels[fields.InputDataFields.groundtruth_difficult] is a [1, num_boxes] int32 tensor indicating if the boxes represent difficult instances. -- Optional -- labels[fields.InputDataFields.groundtruth_instance_masks] is a [1, num_boxes, H, W] float32 tensor containing only binary values, which represent instance masks for objects. Raises: TypeError: if the `eval_config`, `eval_input_config` or `model_config` are not of the correct type. """ params = params or {} if not isinstance(eval_config, eval_pb2.EvalConfig): raise TypeError('For eval mode, the `eval_config` must be a ' 'train_pb2.EvalConfig.') if not isinstance(eval_input_config, input_reader_pb2.InputReader): raise TypeError('The `eval_input_config` must be a ' 'input_reader_pb2.InputReader.') if not isinstance(model_config, model_pb2.DetectionModel): raise TypeError('The `model_config` must be a ' 'model_pb2.DetectionModel.') def transform_and_pad_input_data_fn(tensor_dict): """Combines transform and pad operation.""" num_classes = config_util.get_number_of_classes(model_config) model = model_builder.build(model_config, is_training=False) image_resizer_config = config_util.get_image_resizer_config(model_config) image_resizer_fn = image_resizer_builder.build(image_resizer_config) transform_data_fn = functools.partial( transform_input_data, model_preprocess_fn=model.preprocess, image_resizer_fn=image_resizer_fn, num_classes=num_classes, data_augmentation_fn=None, retain_original_image=eval_config.retain_original_images) tensor_dict = pad_input_data_to_static_shapes( tensor_dict=transform_data_fn(tensor_dict), max_num_boxes=eval_input_config.max_number_of_boxes, num_classes=config_util.get_number_of_classes(model_config), spatial_image_shape=config_util.get_spatial_image_size( image_resizer_config)) return (_get_features_dict(tensor_dict), _get_labels_dict(tensor_dict)) dataset = INPUT_BUILDER_UTIL_MAP['dataset_build']( eval_input_config, batch_size=params['batch_size'] if params else eval_config.batch_size, transform_input_data_fn=transform_and_pad_input_data_fn, multi_gpu=False) return dataset return _eval_input_fn def create_predict_input_fn(model_config, predict_input_config): """Creates a predict `input` function for `Estimator`. Args: model_config: A model_pb2.DetectionModel. predict_input_config: An input_reader_pb2.InputReader. Returns: `input_fn` for `Estimator` in PREDICT mode. """ def _predict_input_fn(params=None): """Decodes serialized tf.Examples and returns `ServingInputReceiver`. Args: params: Parameter dictionary passed from the estimator. Returns: `ServingInputReceiver`. """ del params example = tf.placeholder(dtype=tf.string, shape=[], name='tf_example') num_classes = config_util.get_number_of_classes(model_config) model = model_builder.build(model_config, is_training=False) image_resizer_config = config_util.get_image_resizer_config(model_config) image_resizer_fn = image_resizer_builder.build(image_resizer_config) transform_fn = functools.partial( transform_input_data, model_preprocess_fn=model.preprocess, image_resizer_fn=image_resizer_fn, num_classes=num_classes, data_augmentation_fn=None) decoder = tf_example_decoder.TfExampleDecoder( load_instance_masks=False, num_additional_channels=predict_input_config.num_additional_channels) input_dict = transform_fn(decoder.decode(example)) images = tf.to_float(input_dict[fields.InputDataFields.image]) images = tf.expand_dims(images, axis=0) true_image_shape = tf.expand_dims( input_dict[fields.InputDataFields.true_image_shape], axis=0) return tf.estimator.export.ServingInputReceiver( features={ fields.InputDataFields.image: images, fields.InputDataFields.true_image_shape: true_image_shape}, receiver_tensors={SERVING_FED_EXAMPLE_KEY: example}) return _predict_input_fn
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/inputs.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Exports an SSD detection model to use with tf-lite. Outputs file: * A tflite compatible frozen graph - $output_directory/tflite_graph.pb The exported graph has the following input and output nodes. Inputs: 'normalized_input_image_tensor': a float32 tensor of shape [1, height, width, 3] containing the normalized input image. Note that the height and width must be compatible with the height and width configured in the fixed_shape_image resizer options in the pipeline config proto. In floating point Mobilenet model, 'normalized_image_tensor' has values between [-1,1). This typically means mapping each pixel (linearly) to a value between [-1, 1]. Input image values between 0 and 255 are scaled by (1/128.0) and then a value of -1 is added to them to ensure the range is [-1,1). In quantized Mobilenet model, 'normalized_image_tensor' has values between [0, 255]. In general, see the `preprocess` function defined in the feature extractor class in the object_detection/models directory. Outputs: If add_postprocessing_op is true: frozen graph adds a TFLite_Detection_PostProcess custom op node has four outputs: detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box locations detection_classes: a float32 tensor of shape [1, num_boxes] with class indices detection_scores: a float32 tensor of shape [1, num_boxes] with class scores num_boxes: a float32 tensor of size 1 containing the number of detected boxes else: the graph has two outputs: 'raw_outputs/box_encodings': a float32 tensor of shape [1, num_anchors, 4] containing the encoded box predictions. 'raw_outputs/class_predictions': a float32 tensor of shape [1, num_anchors, num_classes] containing the class scores for each anchor after applying score conversion. Example Usage: -------------- python object_detection/export_tflite_ssd_graph \ --pipeline_config_path path/to/ssd_mobilenet.config \ --trained_checkpoint_prefix path/to/model.ckpt \ --output_directory path/to/exported_model_directory The expected output would be in the directory path/to/exported_model_directory (which is created if it does not exist) with contents: - tflite_graph.pbtxt - tflite_graph.pb Config overrides (see the `config_override` flag) are text protobufs (also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override certain fields in the provided pipeline_config_path. These are useful for making small changes to the inference graph that differ from the training or eval config. Example Usage (in which we change the NMS iou_threshold to be 0.5 and NMS score_threshold to be 0.0): python object_detection/export_tflite_ssd_graph \ --pipeline_config_path path/to/ssd_mobilenet.config \ --trained_checkpoint_prefix path/to/model.ckpt \ --output_directory path/to/exported_model_directory --config_override " \ model{ \ ssd{ \ post_processing { \ batch_non_max_suppression { \ score_threshold: 0.0 \ iou_threshold: 0.5 \ } \ } \ } \ } \ " """ import tensorflow as tf from google.protobuf import text_format from object_detection import export_tflite_ssd_graph_lib from object_detection.protos import pipeline_pb2 flags = tf.app.flags flags.DEFINE_string('output_directory', None, 'Path to write outputs.') flags.DEFINE_string( 'pipeline_config_path', None, 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' 'file.') flags.DEFINE_string('trained_checkpoint_prefix', None, 'Checkpoint prefix.') flags.DEFINE_integer('max_detections', 10, 'Maximum number of detections (boxes) to show.') flags.DEFINE_integer('max_classes_per_detection', 1, 'Number of classes to display per detection box.') flags.DEFINE_integer( 'detections_per_class', 100, 'Number of anchors used per class in Regular Non-Max-Suppression.') flags.DEFINE_bool('add_postprocessing_op', True, 'Add TFLite custom op for postprocessing to the graph.') flags.DEFINE_bool( 'use_regular_nms', False, 'Flag to set postprocessing op to use Regular NMS instead of Fast NMS.') flags.DEFINE_string( 'config_override', '', 'pipeline_pb2.TrainEvalPipelineConfig ' 'text proto to override pipeline_config_path.') FLAGS = flags.FLAGS def main(argv): del argv # Unused. flags.mark_flag_as_required('output_directory') flags.mark_flag_as_required('pipeline_config_path') flags.mark_flag_as_required('trained_checkpoint_prefix') pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() with tf.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f: text_format.Merge(f.read(), pipeline_config) text_format.Merge(FLAGS.config_override, pipeline_config) export_tflite_ssd_graph_lib.export_tflite_graph( pipeline_config, FLAGS.trained_checkpoint_prefix, FLAGS.output_directory, FLAGS.add_postprocessing_op, FLAGS.max_detections, FLAGS.max_classes_per_detection, FLAGS.use_regular_nms) if __name__ == '__main__': tf.app.run(main)
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/export_tflite_ssd_graph.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Hyperparameters for the object detection model in TF.learn. This file consolidates and documents the hyperparameters used by the model. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf def create_hparams(hparams_overrides=None): """Returns hyperparameters, including any flag value overrides. Args: hparams_overrides: Optional hparams overrides, represented as a string containing comma-separated hparam_name=value pairs. Returns: The hyperparameters as a tf.HParams object. """ hparams = tf.contrib.training.HParams( # Whether a fine tuning checkpoint (provided in the pipeline config) # should be loaded for training. load_pretrained=True) # Override any of the preceding hyperparameter values. if hparams_overrides: hparams = hparams.parse(hparams_overrides) return hparams
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/model_hparams.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.tflearn.inputs.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import os from absl.testing import parameterized import numpy as np import tensorflow as tf from object_detection import inputs from object_detection.core import preprocessor from object_detection.core import standard_fields as fields from object_detection.utils import config_util from object_detection.utils import test_case FLAGS = tf.flags.FLAGS def _get_configs_for_model(model_name): """Returns configurations for model.""" fname = os.path.join(tf.resource_loader.get_data_files_path(), 'samples/configs/' + model_name + '.config') label_map_path = os.path.join(tf.resource_loader.get_data_files_path(), 'data/pet_label_map.pbtxt') data_path = os.path.join(tf.resource_loader.get_data_files_path(), 'test_data/pets_examples.record') configs = config_util.get_configs_from_pipeline_file(fname) override_dict = { 'train_input_path': data_path, 'eval_input_path': data_path, 'label_map_path': label_map_path } return config_util.merge_external_params_with_configs( configs, kwargs_dict=override_dict) def _make_initializable_iterator(dataset): """Creates an iterator, and initializes tables. Args: dataset: A `tf.data.Dataset` object. Returns: A `tf.data.Iterator`. """ iterator = dataset.make_initializable_iterator() tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer) return iterator class InputsTest(test_case.TestCase, parameterized.TestCase): def test_faster_rcnn_resnet50_train_input(self): """Tests the training input function for FasterRcnnResnet50.""" configs = _get_configs_for_model('faster_rcnn_resnet50_pets') model_config = configs['model'] model_config.faster_rcnn.num_classes = 37 train_input_fn = inputs.create_train_input_fn( configs['train_config'], configs['train_input_config'], model_config) features, labels = _make_initializable_iterator(train_input_fn()).get_next() self.assertAllEqual([1, None, None, 3], features[fields.InputDataFields.image].shape.as_list()) self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) self.assertAllEqual([1], features[inputs.HASH_KEY].shape.as_list()) self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) self.assertAllEqual( [1, 100, 4], labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_boxes].dtype) self.assertAllEqual( [1, 100, model_config.faster_rcnn.num_classes], labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_classes].dtype) self.assertAllEqual( [1, 100], labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_weights].dtype) self.assertAllEqual( [1, 100, model_config.faster_rcnn.num_classes], labels[fields.InputDataFields.groundtruth_confidences].shape.as_list()) self.assertEqual( tf.float32, labels[fields.InputDataFields.groundtruth_confidences].dtype) @parameterized.parameters( {'eval_batch_size': 1}, {'eval_batch_size': 8} ) def test_faster_rcnn_resnet50_eval_input(self, eval_batch_size=1): """Tests the eval input function for FasterRcnnResnet50.""" configs = _get_configs_for_model('faster_rcnn_resnet50_pets') model_config = configs['model'] model_config.faster_rcnn.num_classes = 37 eval_config = configs['eval_config'] eval_config.batch_size = eval_batch_size eval_input_fn = inputs.create_eval_input_fn( eval_config, configs['eval_input_configs'][0], model_config) features, labels = _make_initializable_iterator(eval_input_fn()).get_next() self.assertAllEqual([eval_batch_size, None, None, 3], features[fields.InputDataFields.image].shape.as_list()) self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) self.assertAllEqual( [eval_batch_size, None, None, 3], features[fields.InputDataFields.original_image].shape.as_list()) self.assertEqual(tf.uint8, features[fields.InputDataFields.original_image].dtype) self.assertAllEqual([eval_batch_size], features[inputs.HASH_KEY].shape.as_list()) self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) self.assertAllEqual( [eval_batch_size, 100, 4], labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_boxes].dtype) self.assertAllEqual( [eval_batch_size, 100, model_config.faster_rcnn.num_classes], labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_classes].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) self.assertEqual( tf.float32, labels[fields.InputDataFields.groundtruth_weights].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[fields.InputDataFields.groundtruth_area].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_area].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[fields.InputDataFields.groundtruth_is_crowd].shape.as_list()) self.assertEqual( tf.bool, labels[fields.InputDataFields.groundtruth_is_crowd].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[fields.InputDataFields.groundtruth_difficult].shape.as_list()) self.assertEqual( tf.int32, labels[fields.InputDataFields.groundtruth_difficult].dtype) def test_ssd_inceptionV2_train_input(self): """Tests the training input function for SSDInceptionV2.""" configs = _get_configs_for_model('ssd_inception_v2_pets') model_config = configs['model'] model_config.ssd.num_classes = 37 batch_size = configs['train_config'].batch_size train_input_fn = inputs.create_train_input_fn( configs['train_config'], configs['train_input_config'], model_config) features, labels = _make_initializable_iterator(train_input_fn()).get_next() self.assertAllEqual([batch_size, 300, 300, 3], features[fields.InputDataFields.image].shape.as_list()) self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) self.assertAllEqual([batch_size], features[inputs.HASH_KEY].shape.as_list()) self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) self.assertAllEqual( [batch_size], labels[fields.InputDataFields.num_groundtruth_boxes].shape.as_list()) self.assertEqual(tf.int32, labels[fields.InputDataFields.num_groundtruth_boxes].dtype) self.assertAllEqual( [batch_size, 100, 4], labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_boxes].dtype) self.assertAllEqual( [batch_size, 100, model_config.ssd.num_classes], labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_classes].dtype) self.assertAllEqual( [batch_size, 100], labels[ fields.InputDataFields.groundtruth_weights].shape.as_list()) self.assertEqual( tf.float32, labels[fields.InputDataFields.groundtruth_weights].dtype) @parameterized.parameters( {'eval_batch_size': 1}, {'eval_batch_size': 8} ) def test_ssd_inceptionV2_eval_input(self, eval_batch_size=1): """Tests the eval input function for SSDInceptionV2.""" configs = _get_configs_for_model('ssd_inception_v2_pets') model_config = configs['model'] model_config.ssd.num_classes = 37 eval_config = configs['eval_config'] eval_config.batch_size = eval_batch_size eval_input_fn = inputs.create_eval_input_fn( eval_config, configs['eval_input_configs'][0], model_config) features, labels = _make_initializable_iterator(eval_input_fn()).get_next() self.assertAllEqual([eval_batch_size, 300, 300, 3], features[fields.InputDataFields.image].shape.as_list()) self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) self.assertAllEqual( [eval_batch_size, 300, 300, 3], features[fields.InputDataFields.original_image].shape.as_list()) self.assertEqual(tf.uint8, features[fields.InputDataFields.original_image].dtype) self.assertAllEqual([eval_batch_size], features[inputs.HASH_KEY].shape.as_list()) self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) self.assertAllEqual( [eval_batch_size, 100, 4], labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_boxes].dtype) self.assertAllEqual( [eval_batch_size, 100, model_config.ssd.num_classes], labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_classes].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[ fields.InputDataFields.groundtruth_weights].shape.as_list()) self.assertEqual( tf.float32, labels[fields.InputDataFields.groundtruth_weights].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[fields.InputDataFields.groundtruth_area].shape.as_list()) self.assertEqual(tf.float32, labels[fields.InputDataFields.groundtruth_area].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[fields.InputDataFields.groundtruth_is_crowd].shape.as_list()) self.assertEqual( tf.bool, labels[fields.InputDataFields.groundtruth_is_crowd].dtype) self.assertAllEqual( [eval_batch_size, 100], labels[fields.InputDataFields.groundtruth_difficult].shape.as_list()) self.assertEqual( tf.int32, labels[fields.InputDataFields.groundtruth_difficult].dtype) def test_predict_input(self): """Tests the predict input function.""" configs = _get_configs_for_model('ssd_inception_v2_pets') predict_input_fn = inputs.create_predict_input_fn( model_config=configs['model'], predict_input_config=configs['eval_input_configs'][0]) serving_input_receiver = predict_input_fn() image = serving_input_receiver.features[fields.InputDataFields.image] receiver_tensors = serving_input_receiver.receiver_tensors[ inputs.SERVING_FED_EXAMPLE_KEY] self.assertEqual([1, 300, 300, 3], image.shape.as_list()) self.assertEqual(tf.float32, image.dtype) self.assertEqual(tf.string, receiver_tensors.dtype) def test_predict_input_with_additional_channels(self): """Tests the predict input function with additional channels.""" configs = _get_configs_for_model('ssd_inception_v2_pets') configs['eval_input_configs'][0].num_additional_channels = 2 predict_input_fn = inputs.create_predict_input_fn( model_config=configs['model'], predict_input_config=configs['eval_input_configs'][0]) serving_input_receiver = predict_input_fn() image = serving_input_receiver.features[fields.InputDataFields.image] receiver_tensors = serving_input_receiver.receiver_tensors[ inputs.SERVING_FED_EXAMPLE_KEY] # RGB + 2 additional channels = 5 channels. self.assertEqual([1, 300, 300, 5], image.shape.as_list()) self.assertEqual(tf.float32, image.dtype) self.assertEqual(tf.string, receiver_tensors.dtype) def test_error_with_bad_train_config(self): """Tests that a TypeError is raised with improper train config.""" configs = _get_configs_for_model('ssd_inception_v2_pets') configs['model'].ssd.num_classes = 37 train_input_fn = inputs.create_train_input_fn( train_config=configs['eval_config'], # Expecting `TrainConfig`. train_input_config=configs['train_input_config'], model_config=configs['model']) with self.assertRaises(TypeError): train_input_fn() def test_error_with_bad_train_input_config(self): """Tests that a TypeError is raised with improper train input config.""" configs = _get_configs_for_model('ssd_inception_v2_pets') configs['model'].ssd.num_classes = 37 train_input_fn = inputs.create_train_input_fn( train_config=configs['train_config'], train_input_config=configs['model'], # Expecting `InputReader`. model_config=configs['model']) with self.assertRaises(TypeError): train_input_fn() def test_error_with_bad_train_model_config(self): """Tests that a TypeError is raised with improper train model config.""" configs = _get_configs_for_model('ssd_inception_v2_pets') configs['model'].ssd.num_classes = 37 train_input_fn = inputs.create_train_input_fn( train_config=configs['train_config'], train_input_config=configs['train_input_config'], model_config=configs['train_config']) # Expecting `DetectionModel`. with self.assertRaises(TypeError): train_input_fn() def test_error_with_bad_eval_config(self): """Tests that a TypeError is raised with improper eval config.""" configs = _get_configs_for_model('ssd_inception_v2_pets') configs['model'].ssd.num_classes = 37 eval_input_fn = inputs.create_eval_input_fn( eval_config=configs['train_config'], # Expecting `EvalConfig`. eval_input_config=configs['eval_input_configs'][0], model_config=configs['model']) with self.assertRaises(TypeError): eval_input_fn() def test_error_with_bad_eval_input_config(self): """Tests that a TypeError is raised with improper eval input config.""" configs = _get_configs_for_model('ssd_inception_v2_pets') configs['model'].ssd.num_classes = 37 eval_input_fn = inputs.create_eval_input_fn( eval_config=configs['eval_config'], eval_input_config=configs['model'], # Expecting `InputReader`. model_config=configs['model']) with self.assertRaises(TypeError): eval_input_fn() def test_error_with_bad_eval_model_config(self): """Tests that a TypeError is raised with improper eval model config.""" configs = _get_configs_for_model('ssd_inception_v2_pets') configs['model'].ssd.num_classes = 37 eval_input_fn = inputs.create_eval_input_fn( eval_config=configs['eval_config'], eval_input_config=configs['eval_input_configs'][0], model_config=configs['eval_config']) # Expecting `DetectionModel`. with self.assertRaises(TypeError): eval_input_fn() def test_output_equal_in_replace_empty_string_with_random_number(self): string_placeholder = tf.placeholder(tf.string, shape=[]) replaced_string = inputs._replace_empty_string_with_random_number( string_placeholder) test_string = 'hello world' feed_dict = {string_placeholder: test_string} with self.test_session() as sess: out_string = sess.run(replaced_string, feed_dict=feed_dict) self.assertEqual(test_string, out_string) def test_output_is_integer_in_replace_empty_string_with_random_number(self): string_placeholder = tf.placeholder(tf.string, shape=[]) replaced_string = inputs._replace_empty_string_with_random_number( string_placeholder) empty_string = '' feed_dict = {string_placeholder: empty_string} tf.set_random_seed(0) with self.test_session() as sess: out_string = sess.run(replaced_string, feed_dict=feed_dict) # Test whether out_string is a string which represents an integer. int(out_string) # throws an error if out_string is not castable to int. self.assertEqual(out_string, '2798129067578209328') class DataAugmentationFnTest(test_case.TestCase): def test_apply_image_and_box_augmentation(self): data_augmentation_options = [ (preprocessor.resize_image, { 'new_height': 20, 'new_width': 20, 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR }), (preprocessor.scale_boxes_to_pixel_coordinates, {}), ] data_augmentation_fn = functools.partial( inputs.augment_input_data, data_augmentation_options=data_augmentation_options) tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), fields.InputDataFields.groundtruth_boxes: tf.constant(np.array([[.5, .5, 1., 1.]], np.float32)) } augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) with self.test_session() as sess: augmented_tensor_dict_out = sess.run(augmented_tensor_dict) self.assertAllEqual( augmented_tensor_dict_out[fields.InputDataFields.image].shape, [20, 20, 3] ) self.assertAllClose( augmented_tensor_dict_out[fields.InputDataFields.groundtruth_boxes], [[10, 10, 20, 20]] ) def test_apply_image_and_box_augmentation_with_scores(self): data_augmentation_options = [ (preprocessor.resize_image, { 'new_height': 20, 'new_width': 20, 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR }), (preprocessor.scale_boxes_to_pixel_coordinates, {}), ] data_augmentation_fn = functools.partial( inputs.augment_input_data, data_augmentation_options=data_augmentation_options) tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), fields.InputDataFields.groundtruth_boxes: tf.constant(np.array([[.5, .5, 1., 1.]], np.float32)), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([1.0], np.float32)), fields.InputDataFields.groundtruth_weights: tf.constant(np.array([0.8], np.float32)), } augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) with self.test_session() as sess: augmented_tensor_dict_out = sess.run(augmented_tensor_dict) self.assertAllEqual( augmented_tensor_dict_out[fields.InputDataFields.image].shape, [20, 20, 3] ) self.assertAllClose( augmented_tensor_dict_out[fields.InputDataFields.groundtruth_boxes], [[10, 10, 20, 20]] ) self.assertAllClose( augmented_tensor_dict_out[fields.InputDataFields.groundtruth_classes], [1.0] ) self.assertAllClose( augmented_tensor_dict_out[ fields.InputDataFields.groundtruth_weights], [0.8] ) def test_include_masks_in_data_augmentation(self): data_augmentation_options = [ (preprocessor.resize_image, { 'new_height': 20, 'new_width': 20, 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR }) ] data_augmentation_fn = functools.partial( inputs.augment_input_data, data_augmentation_options=data_augmentation_options) tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), fields.InputDataFields.groundtruth_instance_masks: tf.constant(np.zeros([2, 10, 10], np.uint8)) } augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) with self.test_session() as sess: augmented_tensor_dict_out = sess.run(augmented_tensor_dict) self.assertAllEqual( augmented_tensor_dict_out[fields.InputDataFields.image].shape, [20, 20, 3]) self.assertAllEqual(augmented_tensor_dict_out[ fields.InputDataFields.groundtruth_instance_masks].shape, [2, 20, 20]) def test_include_keypoints_in_data_augmentation(self): data_augmentation_options = [ (preprocessor.resize_image, { 'new_height': 20, 'new_width': 20, 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR }), (preprocessor.scale_boxes_to_pixel_coordinates, {}), ] data_augmentation_fn = functools.partial( inputs.augment_input_data, data_augmentation_options=data_augmentation_options) tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), fields.InputDataFields.groundtruth_boxes: tf.constant(np.array([[.5, .5, 1., 1.]], np.float32)), fields.InputDataFields.groundtruth_keypoints: tf.constant(np.array([[[0.5, 1.0], [0.5, 0.5]]], np.float32)) } augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) with self.test_session() as sess: augmented_tensor_dict_out = sess.run(augmented_tensor_dict) self.assertAllEqual( augmented_tensor_dict_out[fields.InputDataFields.image].shape, [20, 20, 3] ) self.assertAllClose( augmented_tensor_dict_out[fields.InputDataFields.groundtruth_boxes], [[10, 10, 20, 20]] ) self.assertAllClose( augmented_tensor_dict_out[fields.InputDataFields.groundtruth_keypoints], [[[10, 20], [10, 10]]] ) def _fake_model_preprocessor_fn(image): return (image, tf.expand_dims(tf.shape(image)[1:], axis=0)) def _fake_image_resizer_fn(image, mask): return (image, mask, tf.shape(image)) class DataTransformationFnTest(test_case.TestCase): def test_combine_additional_channels_if_present(self): image = np.random.rand(4, 4, 3).astype(np.float32) additional_channels = np.random.rand(4, 4, 2).astype(np.float32) tensor_dict = { fields.InputDataFields.image: tf.constant(image), fields.InputDataFields.image_additional_channels: tf.constant(additional_channels), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([1, 1], np.int32)) } input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=_fake_model_preprocessor_fn, image_resizer_fn=_fake_image_resizer_fn, num_classes=1) with self.test_session() as sess: transformed_inputs = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllEqual(transformed_inputs[fields.InputDataFields.image].dtype, tf.float32) self.assertAllEqual(transformed_inputs[fields.InputDataFields.image].shape, [4, 4, 5]) self.assertAllClose(transformed_inputs[fields.InputDataFields.image], np.concatenate((image, additional_channels), axis=2)) def test_returns_correct_class_label_encodings(self): tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), fields.InputDataFields.groundtruth_boxes: tf.constant(np.array([[0, 0, 1, 1], [.5, .5, 1, 1]], np.float32)), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([3, 1], np.int32)) } num_classes = 3 input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=_fake_model_preprocessor_fn, image_resizer_fn=_fake_image_resizer_fn, num_classes=num_classes) with self.test_session() as sess: transformed_inputs = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllClose( transformed_inputs[fields.InputDataFields.groundtruth_classes], [[0, 0, 1], [1, 0, 0]]) self.assertAllClose( transformed_inputs[fields.InputDataFields.groundtruth_confidences], [[0, 0, 1], [1, 0, 0]]) def test_returns_correct_merged_boxes(self): tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), fields.InputDataFields.groundtruth_boxes: tf.constant(np.array([[.5, .5, 1, 1], [.5, .5, 1, 1]], np.float32)), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([3, 1], np.int32)) } num_classes = 3 input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=_fake_model_preprocessor_fn, image_resizer_fn=_fake_image_resizer_fn, num_classes=num_classes, merge_multiple_boxes=True) with self.test_session() as sess: transformed_inputs = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllClose( transformed_inputs[fields.InputDataFields.groundtruth_boxes], [[.5, .5, 1., 1.]]) self.assertAllClose( transformed_inputs[fields.InputDataFields.groundtruth_classes], [[1, 0, 1]]) self.assertAllClose( transformed_inputs[fields.InputDataFields.groundtruth_confidences], [[1, 0, 1]]) self.assertAllClose( transformed_inputs[fields.InputDataFields.num_groundtruth_boxes], 1) def test_returns_correct_groundtruth_confidences_when_input_present(self): tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), fields.InputDataFields.groundtruth_boxes: tf.constant(np.array([[0, 0, 1, 1], [.5, .5, 1, 1]], np.float32)), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([3, 1], np.int32)), fields.InputDataFields.groundtruth_confidences: tf.constant(np.array([1.0, -1.0], np.float32)) } num_classes = 3 input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=_fake_model_preprocessor_fn, image_resizer_fn=_fake_image_resizer_fn, num_classes=num_classes) with self.test_session() as sess: transformed_inputs = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllClose( transformed_inputs[fields.InputDataFields.groundtruth_classes], [[0, 0, 1], [1, 0, 0]]) self.assertAllClose( transformed_inputs[fields.InputDataFields.groundtruth_confidences], [[0, 0, 1], [-1, 0, 0]]) def test_returns_resized_masks(self): tensor_dict = { fields.InputDataFields.image: tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), fields.InputDataFields.groundtruth_instance_masks: tf.constant(np.random.rand(2, 4, 4).astype(np.float32)), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([3, 1], np.int32)), fields.InputDataFields.original_image_spatial_shape: tf.constant(np.array([4, 4], np.int32)) } def fake_image_resizer_fn(image, masks=None): resized_image = tf.image.resize_images(image, [8, 8]) results = [resized_image] if masks is not None: resized_masks = tf.transpose( tf.image.resize_images(tf.transpose(masks, [1, 2, 0]), [8, 8]), [2, 0, 1]) results.append(resized_masks) results.append(tf.shape(resized_image)) return results num_classes = 3 input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=_fake_model_preprocessor_fn, image_resizer_fn=fake_image_resizer_fn, num_classes=num_classes, retain_original_image=True) with self.test_session() as sess: transformed_inputs = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllEqual(transformed_inputs[ fields.InputDataFields.original_image].dtype, tf.uint8) self.assertAllEqual(transformed_inputs[ fields.InputDataFields.original_image_spatial_shape], [4, 4]) self.assertAllEqual(transformed_inputs[ fields.InputDataFields.original_image].shape, [8, 8, 3]) self.assertAllEqual(transformed_inputs[ fields.InputDataFields.groundtruth_instance_masks].shape, [2, 8, 8]) def test_applies_model_preprocess_fn_to_image_tensor(self): np_image = np.random.randint(256, size=(4, 4, 3)) tensor_dict = { fields.InputDataFields.image: tf.constant(np_image), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([3, 1], np.int32)) } def fake_model_preprocessor_fn(image): return (image / 255., tf.expand_dims(tf.shape(image)[1:], axis=0)) num_classes = 3 input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=fake_model_preprocessor_fn, image_resizer_fn=_fake_image_resizer_fn, num_classes=num_classes) with self.test_session() as sess: transformed_inputs = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllClose(transformed_inputs[fields.InputDataFields.image], np_image / 255.) self.assertAllClose(transformed_inputs[fields.InputDataFields. true_image_shape], [4, 4, 3]) def test_applies_data_augmentation_fn_to_tensor_dict(self): np_image = np.random.randint(256, size=(4, 4, 3)) tensor_dict = { fields.InputDataFields.image: tf.constant(np_image), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([3, 1], np.int32)) } def add_one_data_augmentation_fn(tensor_dict): return {key: value + 1 for key, value in tensor_dict.items()} num_classes = 4 input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=_fake_model_preprocessor_fn, image_resizer_fn=_fake_image_resizer_fn, num_classes=num_classes, data_augmentation_fn=add_one_data_augmentation_fn) with self.test_session() as sess: augmented_tensor_dict = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllEqual(augmented_tensor_dict[fields.InputDataFields.image], np_image + 1) self.assertAllEqual( augmented_tensor_dict[fields.InputDataFields.groundtruth_classes], [[0, 0, 0, 1], [0, 1, 0, 0]]) def test_applies_data_augmentation_fn_before_model_preprocess_fn(self): np_image = np.random.randint(256, size=(4, 4, 3)) tensor_dict = { fields.InputDataFields.image: tf.constant(np_image), fields.InputDataFields.groundtruth_classes: tf.constant(np.array([3, 1], np.int32)) } def mul_two_model_preprocessor_fn(image): return (image * 2, tf.expand_dims(tf.shape(image)[1:], axis=0)) def add_five_to_image_data_augmentation_fn(tensor_dict): tensor_dict[fields.InputDataFields.image] += 5 return tensor_dict num_classes = 4 input_transformation_fn = functools.partial( inputs.transform_input_data, model_preprocess_fn=mul_two_model_preprocessor_fn, image_resizer_fn=_fake_image_resizer_fn, num_classes=num_classes, data_augmentation_fn=add_five_to_image_data_augmentation_fn) with self.test_session() as sess: augmented_tensor_dict = sess.run( input_transformation_fn(tensor_dict=tensor_dict)) self.assertAllEqual(augmented_tensor_dict[fields.InputDataFields.image], (np_image + 5) * 2) class PadInputDataToStaticShapesFnTest(test_case.TestCase): def test_pad_images_boxes_and_classes(self): input_tensor_dict = { fields.InputDataFields.image: tf.placeholder(tf.float32, [None, None, 3]), fields.InputDataFields.groundtruth_boxes: tf.placeholder(tf.float32, [None, 4]), fields.InputDataFields.groundtruth_classes: tf.placeholder(tf.int32, [None, 3]), fields.InputDataFields.true_image_shape: tf.placeholder(tf.int32, [3]), fields.InputDataFields.original_image_spatial_shape: tf.placeholder(tf.int32, [2]) } padded_tensor_dict = inputs.pad_input_data_to_static_shapes( tensor_dict=input_tensor_dict, max_num_boxes=3, num_classes=3, spatial_image_shape=[5, 6]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), [5, 6, 3]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.true_image_shape] .shape.as_list(), [3]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.original_image_spatial_shape] .shape.as_list(), [2]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.groundtruth_boxes] .shape.as_list(), [3, 4]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.groundtruth_classes] .shape.as_list(), [3, 3]) def test_clip_boxes_and_classes(self): input_tensor_dict = { fields.InputDataFields.groundtruth_boxes: tf.placeholder(tf.float32, [None, 4]), fields.InputDataFields.groundtruth_classes: tf.placeholder(tf.int32, [None, 3]), fields.InputDataFields.num_groundtruth_boxes: tf.placeholder(tf.int32, []) } padded_tensor_dict = inputs.pad_input_data_to_static_shapes( tensor_dict=input_tensor_dict, max_num_boxes=3, num_classes=3, spatial_image_shape=[5, 6]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.groundtruth_boxes] .shape.as_list(), [3, 4]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.groundtruth_classes] .shape.as_list(), [3, 3]) with self.test_session() as sess: out_tensor_dict = sess.run( padded_tensor_dict, feed_dict={ input_tensor_dict[fields.InputDataFields.groundtruth_boxes]: np.random.rand(5, 4), input_tensor_dict[fields.InputDataFields.groundtruth_classes]: np.random.rand(2, 3), input_tensor_dict[fields.InputDataFields.num_groundtruth_boxes]: 5, }) self.assertAllEqual( out_tensor_dict[fields.InputDataFields.groundtruth_boxes].shape, [3, 4]) self.assertAllEqual( out_tensor_dict[fields.InputDataFields.groundtruth_classes].shape, [3, 3]) self.assertEqual( out_tensor_dict[fields.InputDataFields.num_groundtruth_boxes], 3) def test_do_not_pad_dynamic_images(self): input_tensor_dict = { fields.InputDataFields.image: tf.placeholder(tf.float32, [None, None, 3]), } padded_tensor_dict = inputs.pad_input_data_to_static_shapes( tensor_dict=input_tensor_dict, max_num_boxes=3, num_classes=3, spatial_image_shape=[None, None]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), [None, None, 3]) def test_images_and_additional_channels(self): input_tensor_dict = { fields.InputDataFields.image: tf.placeholder(tf.float32, [None, None, 3]), fields.InputDataFields.image_additional_channels: tf.placeholder(tf.float32, [None, None, 2]), } padded_tensor_dict = inputs.pad_input_data_to_static_shapes( tensor_dict=input_tensor_dict, max_num_boxes=3, num_classes=3, spatial_image_shape=[5, 6]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), [5, 6, 5]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.image_additional_channels] .shape.as_list(), [5, 6, 2]) def test_gray_images(self): input_tensor_dict = { fields.InputDataFields.image: tf.placeholder(tf.float32, [None, None, 1]), } padded_tensor_dict = inputs.pad_input_data_to_static_shapes( tensor_dict=input_tensor_dict, max_num_boxes=3, num_classes=3, spatial_image_shape=[5, 6]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), [5, 6, 1]) def test_gray_images_and_additional_channels(self): input_tensor_dict = { fields.InputDataFields.image: tf.placeholder(tf.float32, [None, None, 1]), fields.InputDataFields.image_additional_channels: tf.placeholder(tf.float32, [None, None, 2]), } padded_tensor_dict = inputs.pad_input_data_to_static_shapes( tensor_dict=input_tensor_dict, max_num_boxes=3, num_classes=3, spatial_image_shape=[5, 6]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), [5, 6, 3]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.image_additional_channels] .shape.as_list(), [5, 6, 2]) def test_keypoints(self): input_tensor_dict = { fields.InputDataFields.groundtruth_keypoints: tf.placeholder(tf.float32, [None, 16, 4]), fields.InputDataFields.groundtruth_keypoint_visibilities: tf.placeholder(tf.bool, [None, 16]), } padded_tensor_dict = inputs.pad_input_data_to_static_shapes( tensor_dict=input_tensor_dict, max_num_boxes=3, num_classes=3, spatial_image_shape=[5, 6]) self.assertAllEqual( padded_tensor_dict[fields.InputDataFields.groundtruth_keypoints] .shape.as_list(), [3, 16, 4]) self.assertAllEqual( padded_tensor_dict[ fields.InputDataFields.groundtruth_keypoint_visibilities] .shape.as_list(), [3, 16]) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/inputs_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== # # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Binary to run train and evaluation on object detection model.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from absl import flags import tensorflow as tf import horovod.tensorflow as hvd import dllogger import time import os from object_detection import model_hparams from object_detection import model_lib from object_detection.utils.exp_utils import AverageMeter, setup_dllogger flags.DEFINE_string( 'model_dir', None, 'Path to output model directory ' 'where event and checkpoint files will be written.') flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config ' 'file.') flags.DEFINE_string("raport_file", default="summary.json", help="Path to dlloger json") flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.') flags.DEFINE_boolean('eval_training_data', False, 'If training data should be evaluated for this job. Note ' 'that one call only use this in eval-only mode, and ' '`checkpoint_dir` must be supplied.') flags.DEFINE_integer('sample_1_of_n_eval_examples', 1, 'Will sample one of ' 'every n eval input examples, where n is provided.') flags.DEFINE_integer('sample_1_of_n_eval_on_train_examples', 5, 'Will sample ' 'one of every n train input examples for evaluation, ' 'where n is provided. This is only used if ' '`eval_training_data` is True.') flags.DEFINE_integer('eval_count', 1, 'How many times the evaluation should be run') flags.DEFINE_string( 'hparams_overrides', None, 'Hyperparameter overrides, ' 'represented as a string containing comma-separated ' 'hparam_name=value pairs.') flags.DEFINE_string( 'checkpoint_dir', None, 'Path to directory holding a checkpoint. If ' '`checkpoint_dir` is provided, this binary operates in eval-only mode, ' 'writing resulting metrics to `model_dir`.') flags.DEFINE_boolean( 'allow_xla', False, 'Enable XLA compilation') flags.DEFINE_boolean( 'amp', False, 'Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.') flags.DEFINE_boolean( 'run_once', False, 'If running in eval-only mode, whether to run just ' 'one round of eval vs running continuously (default).' ) FLAGS = flags.FLAGS class DLLoggerHook(tf.estimator.SessionRunHook): def __init__(self, global_batch_size, rank=-1): self.global_batch_size = global_batch_size self.rank = rank setup_dllogger(enabled=True, filename=FLAGS.raport_file, rank=rank) def after_create_session(self, session, coord): self.meters = {} warmup = 100 self.meters['train_throughput'] = AverageMeter(warmup=warmup) def before_run(self, run_context): self.t0 = time.time() return tf.estimator.SessionRunArgs(fetches=['global_step:0', 'learning_rate:0']) def after_run(self, run_context, run_values): throughput = self.global_batch_size/(time.time() - self.t0) global_step, lr = run_values.results self.meters['train_throughput'].update(throughput) def end(self, session): summary = { 'train_throughput': self.meters['train_throughput'].avg, } dllogger.log(step=tuple(), data=summary) def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) if FLAGS.amp: os.environ["TF_ENABLE_AUTO_MIXED_PRECISION"] = "1" else: os.environ["TF_ENABLE_AUTO_MIXED_PRECISION"] = "0" hvd.init() flags.mark_flag_as_required('model_dir') flags.mark_flag_as_required('pipeline_config_path') session_config = tf.ConfigProto() session_config.gpu_options.per_process_gpu_memory_fraction=0.9 session_config.gpu_options.visible_device_list = str(hvd.local_rank()) if FLAGS.allow_xla: session_config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 model_dir = FLAGS.model_dir if hvd.rank() == 0 else None config = tf.estimator.RunConfig(model_dir=model_dir, session_config=session_config) train_and_eval_dict = model_lib.create_estimator_and_inputs( run_config=config, eval_count=FLAGS.eval_count, hparams=model_hparams.create_hparams(FLAGS.hparams_overrides), pipeline_config_path=FLAGS.pipeline_config_path, train_steps=FLAGS.num_train_steps, sample_1_of_n_eval_examples=FLAGS.sample_1_of_n_eval_examples, sample_1_of_n_eval_on_train_examples=( FLAGS.sample_1_of_n_eval_on_train_examples)) estimator = train_and_eval_dict['estimator'] train_input_fn = train_and_eval_dict['train_input_fn'] eval_input_fns = train_and_eval_dict['eval_input_fns'] eval_on_train_input_fn = train_and_eval_dict['eval_on_train_input_fn'] predict_input_fn = train_and_eval_dict['predict_input_fn'] train_steps = train_and_eval_dict['train_steps'] if FLAGS.checkpoint_dir: if FLAGS.eval_training_data: name = 'training_data' input_fn = eval_on_train_input_fn else: name = 'validation_data' # The first eval input will be evaluated. input_fn = eval_input_fns[0] if FLAGS.run_once: estimator.evaluate(input_fn, steps=None, checkpoint_path=tf.train.latest_checkpoint( FLAGS.checkpoint_dir)) else: model_lib.continuous_eval(estimator, FLAGS.checkpoint_dir, input_fn, train_steps, name) else: train_spec, eval_specs = model_lib.create_train_and_eval_specs( train_input_fn, eval_input_fns, eval_on_train_input_fn, predict_input_fn, train_steps, eval_on_train_data=False) train_hooks = [hvd.BroadcastGlobalVariablesHook(0), DLLoggerHook(hvd.size()*train_and_eval_dict['train_batch_size'], hvd.rank())] eval_hooks = [] for x in range(FLAGS.eval_count): estimator.train(train_input_fn, hooks=train_hooks, steps=train_steps // FLAGS.eval_count) if hvd.rank() == 0: eval_input_fn = eval_input_fns[0] results = estimator.evaluate(eval_input_fn, steps=None, hooks=eval_hooks) if __name__ == '__main__': tf.app.run()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/model_main.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Creates and runs `Estimator` for object detection model on TPUs. This uses the TPUEstimator API to define and run a model in TRAIN/EVAL modes. """ # pylint: enable=line-too-long from __future__ import absolute_import from __future__ import division from __future__ import print_function from absl import flags import tensorflow as tf from object_detection import model_hparams from object_detection import model_lib tf.flags.DEFINE_bool('use_tpu', True, 'Use TPUs rather than plain CPUs') # Cloud TPU Cluster Resolvers flags.DEFINE_string( 'gcp_project', default=None, help='Project name for the Cloud TPU-enabled project. If not specified, we ' 'will attempt to automatically detect the GCE project from metadata.') flags.DEFINE_string( 'tpu_zone', default=None, help='GCE zone where the Cloud TPU is located in. If not specified, we ' 'will attempt to automatically detect the GCE project from metadata.') flags.DEFINE_string( 'tpu_name', default=None, help='Name of the Cloud TPU for Cluster Resolvers.') flags.DEFINE_integer('num_shards', 8, 'Number of shards (TPU cores).') flags.DEFINE_integer('iterations_per_loop', 100, 'Number of iterations per TPU training loop.') # For mode=train_and_eval, evaluation occurs after training is finished. # Note: independently of steps_per_checkpoint, estimator will save the most # recent checkpoint every 10 minutes by default for train_and_eval flags.DEFINE_string('mode', 'train', 'Mode to run: train, eval') flags.DEFINE_integer('train_batch_size', None, 'Batch size for training. If ' 'this is not provided, batch size is read from training ' 'config.') flags.DEFINE_string( 'hparams_overrides', None, 'Comma-separated list of ' 'hyperparameters to override defaults.') flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.') flags.DEFINE_boolean('eval_training_data', False, 'If training data should be evaluated for this job.') flags.DEFINE_integer('sample_1_of_n_eval_examples', 1, 'Will sample one of ' 'every n eval input examples, where n is provided.') flags.DEFINE_integer('sample_1_of_n_eval_on_train_examples', 5, 'Will sample ' 'one of every n train input examples for evaluation, ' 'where n is provided. This is only used if ' '`eval_training_data` is True.') flags.DEFINE_string( 'model_dir', None, 'Path to output model directory ' 'where event and checkpoint files will be written.') flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config ' 'file.') FLAGS = tf.flags.FLAGS def main(unused_argv): flags.mark_flag_as_required('model_dir') flags.mark_flag_as_required('pipeline_config_path') tpu_cluster_resolver = ( tf.contrib.cluster_resolver.TPUClusterResolver( tpu=[FLAGS.tpu_name], zone=FLAGS.tpu_zone, project=FLAGS.gcp_project)) tpu_grpc_url = tpu_cluster_resolver.get_master() config = tf.contrib.tpu.RunConfig( master=tpu_grpc_url, evaluation_master=tpu_grpc_url, model_dir=FLAGS.model_dir, tpu_config=tf.contrib.tpu.TPUConfig( iterations_per_loop=FLAGS.iterations_per_loop, num_shards=FLAGS.num_shards)) kwargs = {} if FLAGS.train_batch_size: kwargs['batch_size'] = FLAGS.train_batch_size train_and_eval_dict = model_lib.create_estimator_and_inputs( run_config=config, hparams=model_hparams.create_hparams(FLAGS.hparams_overrides), pipeline_config_path=FLAGS.pipeline_config_path, train_steps=FLAGS.num_train_steps, sample_1_of_n_eval_examples=FLAGS.sample_1_of_n_eval_examples, sample_1_of_n_eval_on_train_examples=( FLAGS.sample_1_of_n_eval_on_train_examples), use_tpu_estimator=True, use_tpu=FLAGS.use_tpu, num_shards=FLAGS.num_shards, save_final_config=FLAGS.mode == 'train', **kwargs) estimator = train_and_eval_dict['estimator'] train_input_fn = train_and_eval_dict['train_input_fn'] eval_input_fns = train_and_eval_dict['eval_input_fns'] eval_on_train_input_fn = train_and_eval_dict['eval_on_train_input_fn'] train_steps = train_and_eval_dict['train_steps'] if FLAGS.mode == 'train': estimator.train(input_fn=train_input_fn, max_steps=train_steps) # Continuously evaluating. if FLAGS.mode == 'eval': if FLAGS.eval_training_data: name = 'training_data' input_fn = eval_on_train_input_fn else: name = 'validation_data' # Currently only a single eval input is allowed. input_fn = eval_input_fns[0] model_lib.continuous_eval(estimator, FLAGS.model_dir, input_fn, train_steps, name) if __name__ == '__main__': tf.app.run()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/model_tpu_main.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Tool to export an object detection model for inference. Prepares an object detection tensorflow graph for inference using model configuration and a trained checkpoint. Outputs inference graph, associated checkpoint files, a frozen inference graph and a SavedModel (https://tensorflow.github.io/serving/serving_basic.html). The inference graph contains one of three input nodes depending on the user specified option. * `image_tensor`: Accepts a uint8 4-D tensor of shape [None, None, None, 3] * `encoded_image_string_tensor`: Accepts a 1-D string tensor of shape [None] containing encoded PNG or JPEG images. Image resolutions are expected to be the same if more than 1 image is provided. * `tf_example`: Accepts a 1-D string tensor of shape [None] containing serialized TFExample protos. Image resolutions are expected to be the same if more than 1 image is provided. and the following output nodes returned by the model.postprocess(..): * `num_detections`: Outputs float32 tensors of the form [batch] that specifies the number of valid boxes per image in the batch. * `detection_boxes`: Outputs float32 tensors of the form [batch, num_boxes, 4] containing detected boxes. * `detection_scores`: Outputs float32 tensors of the form [batch, num_boxes] containing class scores for the detections. * `detection_classes`: Outputs float32 tensors of the form [batch, num_boxes] containing classes for the detections. * `detection_masks`: Outputs float32 tensors of the form [batch, num_boxes, mask_height, mask_width] containing predicted instance masks for each box if its present in the dictionary of postprocessed tensors returned by the model. Notes: * This tool uses `use_moving_averages` from eval_config to decide which weights to freeze. Example Usage: -------------- python export_inference_graph \ --input_type image_tensor \ --pipeline_config_path path/to/ssd_inception_v2.config \ --trained_checkpoint_prefix path/to/model.ckpt \ --output_directory path/to/exported_model_directory The expected output would be in the directory path/to/exported_model_directory (which is created if it does not exist) with contents: - inference_graph.pbtxt - model.ckpt.data-00000-of-00001 - model.ckpt.info - model.ckpt.meta - frozen_inference_graph.pb + saved_model (a directory) Config overrides (see the `config_override` flag) are text protobufs (also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override certain fields in the provided pipeline_config_path. These are useful for making small changes to the inference graph that differ from the training or eval config. Example Usage (in which we change the second stage post-processing score threshold to be 0.5): python export_inference_graph \ --input_type image_tensor \ --pipeline_config_path path/to/ssd_inception_v2.config \ --trained_checkpoint_prefix path/to/model.ckpt \ --output_directory path/to/exported_model_directory \ --config_override " \ model{ \ faster_rcnn { \ second_stage_post_processing { \ batch_non_max_suppression { \ score_threshold: 0.5 \ } \ } \ } \ }" """ import tensorflow as tf from google.protobuf import text_format from object_detection import exporter from object_detection.protos import pipeline_pb2 slim = tf.contrib.slim flags = tf.app.flags flags.DEFINE_string('input_type', 'image_tensor', 'Type of input node. Can be ' 'one of [`image_tensor`, `encoded_image_string_tensor`, ' '`tf_example`]') flags.DEFINE_string('input_shape', None, 'If input_type is `image_tensor`, this can explicitly set ' 'the shape of this input tensor to a fixed size. The ' 'dimensions are to be provided as a comma-separated list ' 'of integers. A value of -1 can be used for unknown ' 'dimensions. If not specified, for an `image_tensor, the ' 'default shape will be partially specified as ' '`[None, None, None, 3]`.') flags.DEFINE_string('pipeline_config_path', None, 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' 'file.') flags.DEFINE_string('trained_checkpoint_prefix', None, 'Path to trained checkpoint, typically of the form ' 'path/to/model.ckpt') flags.DEFINE_string('output_directory', None, 'Path to write outputs.') flags.DEFINE_string('config_override', '', 'pipeline_pb2.TrainEvalPipelineConfig ' 'text proto to override pipeline_config_path.') flags.DEFINE_boolean('write_inference_graph', False, 'If true, writes inference graph to disk.') tf.app.flags.mark_flag_as_required('pipeline_config_path') tf.app.flags.mark_flag_as_required('trained_checkpoint_prefix') tf.app.flags.mark_flag_as_required('output_directory') FLAGS = flags.FLAGS def main(_): pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() with tf.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f: text_format.Merge(f.read(), pipeline_config) text_format.Merge(FLAGS.config_override, pipeline_config) if FLAGS.input_shape: input_shape = [ int(dim) if dim != '-1' else None for dim in FLAGS.input_shape.split(',') ] else: input_shape = None exporter.export_inference_graph( FLAGS.input_type, pipeline_config, FLAGS.trained_checkpoint_prefix, FLAGS.output_directory, input_shape=input_shape, write_inference_graph=FLAGS.write_inference_graph) if __name__ == '__main__': tf.app.run()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/export_inference_graph.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Wrappers for third party pycocotools to be used within object_detection. Note that nothing in this file is tensorflow related and thus cannot be called directly as a slim metric, for example. TODO(jonathanhuang): wrap as a slim metric in metrics.py Usage example: given a set of images with ids in the list image_ids and corresponding lists of numpy arrays encoding groundtruth (boxes and classes) and detections (boxes, scores and classes), where elements of each list correspond to detections/annotations of a single image, then evaluation (in multi-class mode) can be invoked as follows: groundtruth_dict = coco_tools.ExportGroundtruthToCOCO( image_ids, groundtruth_boxes_list, groundtruth_classes_list, max_num_classes, output_path=None) detections_list = coco_tools.ExportDetectionsToCOCO( image_ids, detection_boxes_list, detection_scores_list, detection_classes_list, output_path=None) groundtruth = coco_tools.COCOWrapper(groundtruth_dict) detections = groundtruth.LoadAnnotations(detections_list) evaluator = coco_tools.COCOEvalWrapper(groundtruth, detections, agnostic_mode=False) metrics = evaluator.ComputeMetrics() """ from collections import OrderedDict import copy import time import dllogger import numpy as np from pycocotools import coco from pycocotools import cocoeval from pycocotools import mask import tensorflow as tf from object_detection.utils import json_utils class COCOWrapper(coco.COCO): """Wrapper for the pycocotools COCO class.""" def __init__(self, dataset, detection_type='bbox'): """COCOWrapper constructor. See http://mscoco.org/dataset/#format for a description of the format. By default, the coco.COCO class constructor reads from a JSON file. This function duplicates the same behavior but loads from a dictionary, allowing us to perform evaluation without writing to external storage. Args: dataset: a dictionary holding bounding box annotations in the COCO format. detection_type: type of detections being wrapped. Can be one of ['bbox', 'segmentation'] Raises: ValueError: if detection_type is unsupported. """ supported_detection_types = ['bbox', 'segmentation'] if detection_type not in supported_detection_types: raise ValueError('Unsupported detection type: {}. ' 'Supported values are: {}'.format( detection_type, supported_detection_types)) self._detection_type = detection_type coco.COCO.__init__(self) self.dataset = dataset self.createIndex() def LoadAnnotations(self, annotations): """Load annotations dictionary into COCO datastructure. See http://mscoco.org/dataset/#format for a description of the annotations format. As above, this function replicates the default behavior of the API but does not require writing to external storage. Args: annotations: python list holding object detection results where each detection is encoded as a dict with required keys ['image_id', 'category_id', 'score'] and one of ['bbox', 'segmentation'] based on `detection_type`. Returns: a coco.COCO datastructure holding object detection annotations results Raises: ValueError: if annotations is not a list ValueError: if annotations do not correspond to the images contained in self. """ results = coco.COCO() results.dataset['images'] = [img for img in self.dataset['images']] tf.logging.info('Loading and preparing annotation results...') tic = time.time() if not isinstance(annotations, list): raise ValueError('annotations is not a list of objects') annotation_img_ids = [ann['image_id'] for ann in annotations] if (set(annotation_img_ids) != (set(annotation_img_ids) & set(self.getImgIds()))): raise ValueError('Results do not correspond to current coco set') results.dataset['categories'] = copy.deepcopy(self.dataset['categories']) if self._detection_type == 'bbox': for idx, ann in enumerate(annotations): bb = ann['bbox'] ann['area'] = bb[2] * bb[3] ann['id'] = idx + 1 ann['iscrowd'] = 0 elif self._detection_type == 'segmentation': for idx, ann in enumerate(annotations): ann['area'] = mask.area(ann['segmentation']) ann['bbox'] = mask.toBbox(ann['segmentation']) ann['id'] = idx + 1 ann['iscrowd'] = 0 tf.logging.info('DONE (t=%0.2fs)', (time.time() - tic)) results.dataset['annotations'] = annotations results.createIndex() return results class COCOEvalWrapper(cocoeval.COCOeval): """Wrapper for the pycocotools COCOeval class. To evaluate, create two objects (groundtruth_dict and detections_list) using the conventions listed at http://mscoco.org/dataset/#format. Then call evaluation as follows: groundtruth = coco_tools.COCOWrapper(groundtruth_dict) detections = groundtruth.LoadAnnotations(detections_list) evaluator = coco_tools.COCOEvalWrapper(groundtruth, detections, agnostic_mode=False) metrics = evaluator.ComputeMetrics() """ def __init__(self, groundtruth=None, detections=None, agnostic_mode=False, iou_type='bbox'): """COCOEvalWrapper constructor. Note that for the area-based metrics to be meaningful, detection and groundtruth boxes must be in image coordinates measured in pixels. Args: groundtruth: a coco.COCO (or coco_tools.COCOWrapper) object holding groundtruth annotations detections: a coco.COCO (or coco_tools.COCOWrapper) object holding detections agnostic_mode: boolean (default: False). If True, evaluation ignores class labels, treating all detections as proposals. iou_type: IOU type to use for evaluation. Supports `bbox` or `segm`. """ cocoeval.COCOeval.__init__(self, groundtruth, detections, iouType=iou_type) if agnostic_mode: self.params.useCats = 0 def GetCategory(self, category_id): """Fetches dictionary holding category information given category id. Args: category_id: integer id Returns: dictionary holding 'id', 'name'. """ return self.cocoGt.cats[category_id] def GetAgnosticMode(self): """Returns true if COCO Eval is configured to evaluate in agnostic mode.""" return self.params.useCats == 0 def GetCategoryIdList(self): """Returns list of valid category ids.""" return self.params.catIds def ComputeMetrics(self, include_metrics_per_category=False, all_metrics_per_category=False): """Computes detection metrics. Args: include_metrics_per_category: If True, will include metrics per category. all_metrics_per_category: If true, include all the summery metrics for each category in per_category_ap. Be careful with setting it to true if you have more than handful of categories, because it will pollute your mldash. Returns: 1. summary_metrics: a dictionary holding: 'Precision/mAP': mean average precision over classes averaged over IOU thresholds ranging from .5 to .95 with .05 increments 'Precision/[email protected]': mean average precision at 50% IOU 'Precision/[email protected]': mean average precision at 75% IOU 'Precision/mAP (small)': mean average precision for small objects (area < 32^2 pixels) 'Precision/mAP (medium)': mean average precision for medium sized objects (32^2 pixels < area < 96^2 pixels) 'Precision/mAP (large)': mean average precision for large objects (96^2 pixels < area < 10000^2 pixels) 'Recall/AR@1': average recall with 1 detection 'Recall/AR@10': average recall with 10 detections 'Recall/AR@100': average recall with 100 detections 'Recall/AR@100 (small)': average recall for small objects with 100 detections 'Recall/AR@100 (medium)': average recall for medium objects with 100 detections 'Recall/AR@100 (large)': average recall for large objects with 100 detections 2. per_category_ap: a dictionary holding category specific results with keys of the form: 'Precision mAP ByCategory/category' (without the supercategory part if no supercategories exist). For backward compatibility 'PerformanceByCategory' is included in the output regardless of all_metrics_per_category. If evaluating class-agnostic mode, per_category_ap is an empty dictionary. Raises: ValueError: If category_stats does not exist. """ self.evaluate() self.accumulate() self.summarize() summary_metrics = OrderedDict([ ('Precision/mAP', self.stats[0]), ('Precision/[email protected]', self.stats[1]), ('Precision/[email protected]', self.stats[2]), ('Precision/mAP (small)', self.stats[3]), ('Precision/mAP (medium)', self.stats[4]), ('Precision/mAP (large)', self.stats[5]), ('Recall/AR@1', self.stats[6]), ('Recall/AR@10', self.stats[7]), ('Recall/AR@100', self.stats[8]), ('Recall/AR@100 (small)', self.stats[9]), ('Recall/AR@100 (medium)', self.stats[10]), ('Recall/AR@100 (large)', self.stats[11]) ]) dllogger.log(step=tuple(), data=summary_metrics) if not include_metrics_per_category: return summary_metrics, {} if not hasattr(self, 'category_stats'): raise ValueError('Category stats do not exist') per_category_ap = OrderedDict([]) if self.GetAgnosticMode(): return summary_metrics, per_category_ap for category_index, category_id in enumerate(self.GetCategoryIdList()): category = self.GetCategory(category_id)['name'] # Kept for backward compatilbility per_category_ap['PerformanceByCategory/mAP/{}'.format( category)] = self.category_stats[0][category_index] if all_metrics_per_category: per_category_ap['Precision mAP ByCategory/{}'.format( category)] = self.category_stats[0][category_index] per_category_ap['Precision [email protected] ByCategory/{}'.format( category)] = self.category_stats[1][category_index] per_category_ap['Precision [email protected] ByCategory/{}'.format( category)] = self.category_stats[2][category_index] per_category_ap['Precision mAP (small) ByCategory/{}'.format( category)] = self.category_stats[3][category_index] per_category_ap['Precision mAP (medium) ByCategory/{}'.format( category)] = self.category_stats[4][category_index] per_category_ap['Precision mAP (large) ByCategory/{}'.format( category)] = self.category_stats[5][category_index] per_category_ap['Recall AR@1 ByCategory/{}'.format( category)] = self.category_stats[6][category_index] per_category_ap['Recall AR@10 ByCategory/{}'.format( category)] = self.category_stats[7][category_index] per_category_ap['Recall AR@100 ByCategory/{}'.format( category)] = self.category_stats[8][category_index] per_category_ap['Recall AR@100 (small) ByCategory/{}'.format( category)] = self.category_stats[9][category_index] per_category_ap['Recall AR@100 (medium) ByCategory/{}'.format( category)] = self.category_stats[10][category_index] per_category_ap['Recall AR@100 (large) ByCategory/{}'.format( category)] = self.category_stats[11][category_index] return summary_metrics, per_category_ap def _ConvertBoxToCOCOFormat(box): """Converts a box in [ymin, xmin, ymax, xmax] format to COCO format. This is a utility function for converting from our internal [ymin, xmin, ymax, xmax] convention to the convention used by the COCO API i.e., [xmin, ymin, width, height]. Args: box: a [ymin, xmin, ymax, xmax] numpy array Returns: a list of floats representing [xmin, ymin, width, height] """ return [float(box[1]), float(box[0]), float(box[3] - box[1]), float(box[2] - box[0])] def _RleCompress(masks): """Compresses mask using Run-length encoding provided by pycocotools. Args: masks: uint8 numpy array of shape [mask_height, mask_width] with values in {0, 1}. Returns: A pycocotools Run-length encoding of the mask. """ return mask.encode(np.asfortranarray(masks)) def ExportSingleImageGroundtruthToCoco(image_id, next_annotation_id, category_id_set, groundtruth_boxes, groundtruth_classes, groundtruth_masks=None, groundtruth_is_crowd=None): """Export groundtruth of a single image to COCO format. This function converts groundtruth detection annotations represented as numpy arrays to dictionaries that can be ingested by the COCO evaluation API. Note that the image_ids provided here must match the ones given to ExportSingleImageDetectionsToCoco. We assume that boxes and classes are in correspondence - that is: groundtruth_boxes[i, :], and groundtruth_classes[i] are associated with the same groundtruth annotation. In the exported result, "area" fields are always set to the area of the groundtruth bounding box. Args: image_id: a unique image identifier either of type integer or string. next_annotation_id: integer specifying the first id to use for the groundtruth annotations. All annotations are assigned a continuous integer id starting from this value. category_id_set: A set of valid class ids. Groundtruth with classes not in category_id_set are dropped. groundtruth_boxes: numpy array (float32) with shape [num_gt_boxes, 4] groundtruth_classes: numpy array (int) with shape [num_gt_boxes] groundtruth_masks: optional uint8 numpy array of shape [num_detections, image_height, image_width] containing detection_masks. groundtruth_is_crowd: optional numpy array (int) with shape [num_gt_boxes] indicating whether groundtruth boxes are crowd. Returns: a list of groundtruth annotations for a single image in the COCO format. Raises: ValueError: if (1) groundtruth_boxes and groundtruth_classes do not have the right lengths or (2) if each of the elements inside these lists do not have the correct shapes or (3) if image_ids are not integers """ if len(groundtruth_classes.shape) != 1: raise ValueError('groundtruth_classes is ' 'expected to be of rank 1.') if len(groundtruth_boxes.shape) != 2: raise ValueError('groundtruth_boxes is expected to be of ' 'rank 2.') if groundtruth_boxes.shape[1] != 4: raise ValueError('groundtruth_boxes should have ' 'shape[1] == 4.') num_boxes = groundtruth_classes.shape[0] if num_boxes != groundtruth_boxes.shape[0]: raise ValueError('Corresponding entries in groundtruth_classes, ' 'and groundtruth_boxes should have ' 'compatible shapes (i.e., agree on the 0th dimension).' 'Classes shape: %d. Boxes shape: %d. Image ID: %s' % ( groundtruth_classes.shape[0], groundtruth_boxes.shape[0], image_id)) has_is_crowd = groundtruth_is_crowd is not None if has_is_crowd and len(groundtruth_is_crowd.shape) != 1: raise ValueError('groundtruth_is_crowd is expected to be of rank 1.') groundtruth_list = [] for i in range(num_boxes): if groundtruth_classes[i] in category_id_set: iscrowd = groundtruth_is_crowd[i] if has_is_crowd else 0 export_dict = { 'id': next_annotation_id + i, 'image_id': image_id, 'category_id': int(groundtruth_classes[i]), 'bbox': list(_ConvertBoxToCOCOFormat(groundtruth_boxes[i, :])), 'area': float((groundtruth_boxes[i, 2] - groundtruth_boxes[i, 0]) * (groundtruth_boxes[i, 3] - groundtruth_boxes[i, 1])), 'iscrowd': iscrowd } if groundtruth_masks is not None: export_dict['segmentation'] = _RleCompress(groundtruth_masks[i]) groundtruth_list.append(export_dict) return groundtruth_list def ExportGroundtruthToCOCO(image_ids, groundtruth_boxes, groundtruth_classes, categories, output_path=None): """Export groundtruth detection annotations in numpy arrays to COCO API. This function converts a set of groundtruth detection annotations represented as numpy arrays to dictionaries that can be ingested by the COCO API. Inputs to this function are three lists: image ids for each groundtruth image, groundtruth boxes for each image and groundtruth classes respectively. Note that the image_ids provided here must match the ones given to the ExportDetectionsToCOCO function in order for evaluation to work properly. We assume that for each image, boxes, scores and classes are in correspondence --- that is: image_id[i], groundtruth_boxes[i, :] and groundtruth_classes[i] are associated with the same groundtruth annotation. In the exported result, "area" fields are always set to the area of the groundtruth bounding box and "iscrowd" fields are always set to 0. TODO(jonathanhuang): pass in "iscrowd" array for evaluating on COCO dataset. Args: image_ids: a list of unique image identifier either of type integer or string. groundtruth_boxes: list of numpy arrays with shape [num_gt_boxes, 4] (note that num_gt_boxes can be different for each entry in the list) groundtruth_classes: list of numpy arrays (int) with shape [num_gt_boxes] (note that num_gt_boxes can be different for each entry in the list) categories: a list of dictionaries representing all possible categories. Each dict in this list has the following keys: 'id': (required) an integer id uniquely identifying this category 'name': (required) string representing category name e.g., 'cat', 'dog', 'pizza' 'supercategory': (optional) string representing the supercategory e.g., 'animal', 'vehicle', 'food', etc output_path: (optional) path for exporting result to JSON Returns: dictionary that can be read by COCO API Raises: ValueError: if (1) groundtruth_boxes and groundtruth_classes do not have the right lengths or (2) if each of the elements inside these lists do not have the correct shapes or (3) if image_ids are not integers """ category_id_set = set([cat['id'] for cat in categories]) groundtruth_export_list = [] image_export_list = [] if not len(image_ids) == len(groundtruth_boxes) == len(groundtruth_classes): raise ValueError('Input lists must have the same length') # For reasons internal to the COCO API, it is important that annotation ids # are not equal to zero; we thus start counting from 1. annotation_id = 1 for image_id, boxes, classes in zip(image_ids, groundtruth_boxes, groundtruth_classes): image_export_list.append({'id': image_id}) groundtruth_export_list.extend(ExportSingleImageGroundtruthToCoco( image_id, annotation_id, category_id_set, boxes, classes)) num_boxes = classes.shape[0] annotation_id += num_boxes groundtruth_dict = { 'annotations': groundtruth_export_list, 'images': image_export_list, 'categories': categories } if output_path: with tf.gfile.GFile(output_path, 'w') as fid: json_utils.Dump(groundtruth_dict, fid, float_digits=4, indent=2) return groundtruth_dict def ExportSingleImageDetectionBoxesToCoco(image_id, category_id_set, detection_boxes, detection_scores, detection_classes): """Export detections of a single image to COCO format. This function converts detections represented as numpy arrays to dictionaries that can be ingested by the COCO evaluation API. Note that the image_ids provided here must match the ones given to the ExporSingleImageDetectionBoxesToCoco. We assume that boxes, and classes are in correspondence - that is: boxes[i, :], and classes[i] are associated with the same groundtruth annotation. Args: image_id: unique image identifier either of type integer or string. category_id_set: A set of valid class ids. Detections with classes not in category_id_set are dropped. detection_boxes: float numpy array of shape [num_detections, 4] containing detection boxes. detection_scores: float numpy array of shape [num_detections] containing scored for the detection boxes. detection_classes: integer numpy array of shape [num_detections] containing the classes for detection boxes. Returns: a list of detection annotations for a single image in the COCO format. Raises: ValueError: if (1) detection_boxes, detection_scores and detection_classes do not have the right lengths or (2) if each of the elements inside these lists do not have the correct shapes or (3) if image_ids are not integers. """ if len(detection_classes.shape) != 1 or len(detection_scores.shape) != 1: raise ValueError('All entries in detection_classes and detection_scores' 'expected to be of rank 1.') if len(detection_boxes.shape) != 2: raise ValueError('All entries in detection_boxes expected to be of ' 'rank 2.') if detection_boxes.shape[1] != 4: raise ValueError('All entries in detection_boxes should have ' 'shape[1] == 4.') num_boxes = detection_classes.shape[0] if not num_boxes == detection_boxes.shape[0] == detection_scores.shape[0]: raise ValueError('Corresponding entries in detection_classes, ' 'detection_scores and detection_boxes should have ' 'compatible shapes (i.e., agree on the 0th dimension). ' 'Classes shape: %d. Boxes shape: %d. ' 'Scores shape: %d' % ( detection_classes.shape[0], detection_boxes.shape[0], detection_scores.shape[0] )) detections_list = [] for i in range(num_boxes): if detection_classes[i] in category_id_set: detections_list.append({ 'image_id': image_id, 'category_id': int(detection_classes[i]), 'bbox': list(_ConvertBoxToCOCOFormat(detection_boxes[i, :])), 'score': float(detection_scores[i]) }) return detections_list def ExportSingleImageDetectionMasksToCoco(image_id, category_id_set, detection_masks, detection_scores, detection_classes): """Export detection masks of a single image to COCO format. This function converts detections represented as numpy arrays to dictionaries that can be ingested by the COCO evaluation API. We assume that detection_masks, detection_scores, and detection_classes are in correspondence - that is: detection_masks[i, :], detection_classes[i] and detection_scores[i] are associated with the same annotation. Args: image_id: unique image identifier either of type integer or string. category_id_set: A set of valid class ids. Detections with classes not in category_id_set are dropped. detection_masks: uint8 numpy array of shape [num_detections, image_height, image_width] containing detection_masks. detection_scores: float numpy array of shape [num_detections] containing scores for detection masks. detection_classes: integer numpy array of shape [num_detections] containing the classes for detection masks. Returns: a list of detection mask annotations for a single image in the COCO format. Raises: ValueError: if (1) detection_masks, detection_scores and detection_classes do not have the right lengths or (2) if each of the elements inside these lists do not have the correct shapes or (3) if image_ids are not integers. """ if len(detection_classes.shape) != 1 or len(detection_scores.shape) != 1: raise ValueError('All entries in detection_classes and detection_scores' 'expected to be of rank 1.') num_boxes = detection_classes.shape[0] if not num_boxes == len(detection_masks) == detection_scores.shape[0]: raise ValueError('Corresponding entries in detection_classes, ' 'detection_scores and detection_masks should have ' 'compatible lengths and shapes ' 'Classes length: %d. Masks length: %d. ' 'Scores length: %d' % ( detection_classes.shape[0], len(detection_masks), detection_scores.shape[0] )) detections_list = [] for i in range(num_boxes): if detection_classes[i] in category_id_set: detections_list.append({ 'image_id': image_id, 'category_id': int(detection_classes[i]), 'segmentation': _RleCompress(detection_masks[i]), 'score': float(detection_scores[i]) }) return detections_list def ExportDetectionsToCOCO(image_ids, detection_boxes, detection_scores, detection_classes, categories, output_path=None): """Export detection annotations in numpy arrays to COCO API. This function converts a set of predicted detections represented as numpy arrays to dictionaries that can be ingested by the COCO API. Inputs to this function are lists, consisting of boxes, scores and classes, respectively, corresponding to each image for which detections have been produced. Note that the image_ids provided here must match the ones given to the ExportGroundtruthToCOCO function in order for evaluation to work properly. We assume that for each image, boxes, scores and classes are in correspondence --- that is: detection_boxes[i, :], detection_scores[i] and detection_classes[i] are associated with the same detection. Args: image_ids: a list of unique image identifier either of type integer or string. detection_boxes: list of numpy arrays with shape [num_detection_boxes, 4] detection_scores: list of numpy arrays (float) with shape [num_detection_boxes]. Note that num_detection_boxes can be different for each entry in the list. detection_classes: list of numpy arrays (int) with shape [num_detection_boxes]. Note that num_detection_boxes can be different for each entry in the list. categories: a list of dictionaries representing all possible categories. Each dict in this list must have an integer 'id' key uniquely identifying this category. output_path: (optional) path for exporting result to JSON Returns: list of dictionaries that can be read by COCO API, where each entry corresponds to a single detection and has keys from: ['image_id', 'category_id', 'bbox', 'score']. Raises: ValueError: if (1) detection_boxes and detection_classes do not have the right lengths or (2) if each of the elements inside these lists do not have the correct shapes or (3) if image_ids are not integers. """ category_id_set = set([cat['id'] for cat in categories]) detections_export_list = [] if not (len(image_ids) == len(detection_boxes) == len(detection_scores) == len(detection_classes)): raise ValueError('Input lists must have the same length') for image_id, boxes, scores, classes in zip(image_ids, detection_boxes, detection_scores, detection_classes): detections_export_list.extend(ExportSingleImageDetectionBoxesToCoco( image_id, category_id_set, boxes, scores, classes)) if output_path: with tf.gfile.GFile(output_path, 'w') as fid: json_utils.Dump(detections_export_list, fid, float_digits=4, indent=2) return detections_export_list def ExportSegmentsToCOCO(image_ids, detection_masks, detection_scores, detection_classes, categories, output_path=None): """Export segmentation masks in numpy arrays to COCO API. This function converts a set of predicted instance masks represented as numpy arrays to dictionaries that can be ingested by the COCO API. Inputs to this function are lists, consisting of segments, scores and classes, respectively, corresponding to each image for which detections have been produced. Note this function is recommended to use for small dataset. For large dataset, it should be used with a merge function (e.g. in map reduce), otherwise the memory consumption is large. We assume that for each image, masks, scores and classes are in correspondence --- that is: detection_masks[i, :, :, :], detection_scores[i] and detection_classes[i] are associated with the same detection. Args: image_ids: list of image ids (typically ints or strings) detection_masks: list of numpy arrays with shape [num_detection, h, w, 1] and type uint8. The height and width should match the shape of corresponding image. detection_scores: list of numpy arrays (float) with shape [num_detection]. Note that num_detection can be different for each entry in the list. detection_classes: list of numpy arrays (int) with shape [num_detection]. Note that num_detection can be different for each entry in the list. categories: a list of dictionaries representing all possible categories. Each dict in this list must have an integer 'id' key uniquely identifying this category. output_path: (optional) path for exporting result to JSON Returns: list of dictionaries that can be read by COCO API, where each entry corresponds to a single detection and has keys from: ['image_id', 'category_id', 'segmentation', 'score']. Raises: ValueError: if detection_masks and detection_classes do not have the right lengths or if each of the elements inside these lists do not have the correct shapes. """ if not (len(image_ids) == len(detection_masks) == len(detection_scores) == len(detection_classes)): raise ValueError('Input lists must have the same length') segment_export_list = [] for image_id, masks, scores, classes in zip(image_ids, detection_masks, detection_scores, detection_classes): if len(classes.shape) != 1 or len(scores.shape) != 1: raise ValueError('All entries in detection_classes and detection_scores' 'expected to be of rank 1.') if len(masks.shape) != 4: raise ValueError('All entries in masks expected to be of ' 'rank 4. Given {}'.format(masks.shape)) num_boxes = classes.shape[0] if not num_boxes == masks.shape[0] == scores.shape[0]: raise ValueError('Corresponding entries in segment_classes, ' 'detection_scores and detection_boxes should have ' 'compatible shapes (i.e., agree on the 0th dimension).') category_id_set = set([cat['id'] for cat in categories]) segment_export_list.extend(ExportSingleImageDetectionMasksToCoco( image_id, category_id_set, np.squeeze(masks, axis=3), scores, classes)) if output_path: with tf.gfile.GFile(output_path, 'w') as fid: json_utils.Dump(segment_export_list, fid, float_digits=4, indent=2) return segment_export_list def ExportKeypointsToCOCO(image_ids, detection_keypoints, detection_scores, detection_classes, categories, output_path=None): """Exports keypoints in numpy arrays to COCO API. This function converts a set of predicted keypoints represented as numpy arrays to dictionaries that can be ingested by the COCO API. Inputs to this function are lists, consisting of keypoints, scores and classes, respectively, corresponding to each image for which detections have been produced. We assume that for each image, keypoints, scores and classes are in correspondence --- that is: detection_keypoints[i, :, :, :], detection_scores[i] and detection_classes[i] are associated with the same detection. Args: image_ids: list of image ids (typically ints or strings) detection_keypoints: list of numpy arrays with shape [num_detection, num_keypoints, 2] and type float32 in absolute x-y coordinates. detection_scores: list of numpy arrays (float) with shape [num_detection]. Note that num_detection can be different for each entry in the list. detection_classes: list of numpy arrays (int) with shape [num_detection]. Note that num_detection can be different for each entry in the list. categories: a list of dictionaries representing all possible categories. Each dict in this list must have an integer 'id' key uniquely identifying this category and an integer 'num_keypoints' key specifying the number of keypoints the category has. output_path: (optional) path for exporting result to JSON Returns: list of dictionaries that can be read by COCO API, where each entry corresponds to a single detection and has keys from: ['image_id', 'category_id', 'keypoints', 'score']. Raises: ValueError: if detection_keypoints and detection_classes do not have the right lengths or if each of the elements inside these lists do not have the correct shapes. """ if not (len(image_ids) == len(detection_keypoints) == len(detection_scores) == len(detection_classes)): raise ValueError('Input lists must have the same length') keypoints_export_list = [] for image_id, keypoints, scores, classes in zip( image_ids, detection_keypoints, detection_scores, detection_classes): if len(classes.shape) != 1 or len(scores.shape) != 1: raise ValueError('All entries in detection_classes and detection_scores' 'expected to be of rank 1.') if len(keypoints.shape) != 3: raise ValueError('All entries in keypoints expected to be of ' 'rank 3. Given {}'.format(keypoints.shape)) num_boxes = classes.shape[0] if not num_boxes == keypoints.shape[0] == scores.shape[0]: raise ValueError('Corresponding entries in detection_classes, ' 'detection_keypoints, and detection_scores should have ' 'compatible shapes (i.e., agree on the 0th dimension).') category_id_set = set([cat['id'] for cat in categories]) category_id_to_num_keypoints_map = { cat['id']: cat['num_keypoints'] for cat in categories if 'num_keypoints' in cat} for i in range(num_boxes): if classes[i] not in category_id_set: raise ValueError('class id should be in category_id_set\n') if classes[i] in category_id_to_num_keypoints_map: num_keypoints = category_id_to_num_keypoints_map[classes[i]] # Adds extra ones to indicate the visibility for each keypoint as is # recommended by MSCOCO. instance_keypoints = np.concatenate( [keypoints[i, 0:num_keypoints, :], np.expand_dims(np.ones(num_keypoints), axis=1)], axis=1).astype(int) instance_keypoints = instance_keypoints.flatten().tolist() keypoints_export_list.append({ 'image_id': image_id, 'category_id': int(classes[i]), 'keypoints': instance_keypoints, 'score': float(scores[i]) }) if output_path: with tf.gfile.GFile(output_path, 'w') as fid: json_utils.Dump(keypoints_export_list, fid, float_digits=4, indent=2) return keypoints_export_list
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/coco_tools.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Converts data from CSV format to the VRDDetectionEvaluator format.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np from object_detection.core import standard_fields from object_detection.utils import vrd_evaluation def build_groundtruth_vrd_dictionary(data, class_label_map, relationship_label_map): """Builds a groundtruth dictionary from groundtruth data in CSV file. Args: data: Pandas DataFrame with the groundtruth data for a single image. class_label_map: Class labelmap from string label name to an integer. relationship_label_map: Relationship type labelmap from string name to an integer. Returns: A dictionary with keys suitable for passing to VRDDetectionEvaluator.add_single_ground_truth_image_info: standard_fields.InputDataFields.groundtruth_boxes: A numpy array of structures with the shape [M, 1], representing M tuples, each tuple containing the same number of named bounding boxes. Each box is of the format [y_min, x_min, y_max, x_max] (see datatype vrd_box_data_type, single_box_data_type above). standard_fields.InputDataFields.groundtruth_classes: A numpy array of structures shape [M, 1], representing the class labels of the corresponding bounding boxes and possibly additional classes (see datatype label_data_type above). standard_fields.InputDataFields.verified_labels: numpy array of shape [K] containing verified labels. """ data_boxes = data[data.LabelName.isnull()] data_labels = data[data.LabelName1.isnull()] boxes = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.vrd_box_data_type) boxes['subject'] = data_boxes[['YMin1', 'XMin1', 'YMax1', 'XMax1']].as_matrix() boxes['object'] = data_boxes[['YMin2', 'XMin2', 'YMax2', 'XMax2']].as_matrix() labels = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.label_data_type) labels['subject'] = data_boxes['LabelName1'].map( lambda x: class_label_map[x]).as_matrix() labels['object'] = data_boxes['LabelName2'].map( lambda x: class_label_map[x]).as_matrix() labels['relation'] = data_boxes['RelationshipLabel'].map( lambda x: relationship_label_map[x]).as_matrix() return { standard_fields.InputDataFields.groundtruth_boxes: boxes, standard_fields.InputDataFields.groundtruth_classes: labels, standard_fields.InputDataFields.groundtruth_image_classes: data_labels['LabelName'].map(lambda x: class_label_map[x]) .as_matrix(), } def build_predictions_vrd_dictionary(data, class_label_map, relationship_label_map): """Builds a predictions dictionary from predictions data in CSV file. Args: data: Pandas DataFrame with the predictions data for a single image. class_label_map: Class labelmap from string label name to an integer. relationship_label_map: Relationship type labelmap from string name to an integer. Returns: Dictionary with keys suitable for passing to VRDDetectionEvaluator.add_single_detected_image_info: standard_fields.DetectionResultFields.detection_boxes: A numpy array of structures with shape [N, 1], representing N tuples, each tuple containing the same number of named bounding boxes. Each box is of the format [y_min, x_min, y_max, x_max] (as an example see datatype vrd_box_data_type, single_box_data_type above). standard_fields.DetectionResultFields.detection_scores: float32 numpy array of shape [N] containing detection scores for the boxes. standard_fields.DetectionResultFields.detection_classes: A numpy array of structures shape [N, 1], representing the class labels of the corresponding bounding boxes and possibly additional classes (see datatype label_data_type above). """ data_boxes = data boxes = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.vrd_box_data_type) boxes['subject'] = data_boxes[['YMin1', 'XMin1', 'YMax1', 'XMax1']].as_matrix() boxes['object'] = data_boxes[['YMin2', 'XMin2', 'YMax2', 'XMax2']].as_matrix() labels = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.label_data_type) labels['subject'] = data_boxes['LabelName1'].map( lambda x: class_label_map[x]).as_matrix() labels['object'] = data_boxes['LabelName2'].map( lambda x: class_label_map[x]).as_matrix() labels['relation'] = data_boxes['RelationshipLabel'].map( lambda x: relationship_label_map[x]).as_matrix() return { standard_fields.DetectionResultFields.detection_boxes: boxes, standard_fields.DetectionResultFields.detection_classes: labels, standard_fields.DetectionResultFields.detection_scores: data_boxes['Score'].as_matrix() }
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for oid_od_challenge_evaluation_util.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np import pandas as pd import tensorflow as tf from object_detection.core import standard_fields from object_detection.metrics import oid_od_challenge_evaluation_utils as utils class OidOdChallengeEvaluationUtilTest(tf.test.TestCase): def testBuildGroundtruthDictionary(self): np_data = pd.DataFrame( [['fe58ec1b06db2bb7', '/m/04bcr3', 0.0, 0.3, 0.5, 0.6, 1, None], [ 'fe58ec1b06db2bb7', '/m/02gy9n', 0.1, 0.2, 0.3, 0.4, 0, None ], ['fe58ec1b06db2bb7', '/m/04bcr3', None, None, None, None, None, 1], [ 'fe58ec1b06db2bb7', '/m/083vt', None, None, None, None, None, 0 ], ['fe58ec1b06db2bb7', '/m/02gy9n', None, None, None, None, None, 1]], columns=[ 'ImageID', 'LabelName', 'XMin', 'XMax', 'YMin', 'YMax', 'IsGroupOf', 'ConfidenceImageLabel' ]) class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} groundtruth_dictionary = utils.build_groundtruth_boxes_dictionary( np_data, class_label_map) self.assertTrue(standard_fields.InputDataFields.groundtruth_boxes in groundtruth_dictionary) self.assertTrue(standard_fields.InputDataFields.groundtruth_classes in groundtruth_dictionary) self.assertTrue(standard_fields.InputDataFields.groundtruth_group_of in groundtruth_dictionary) self.assertTrue(standard_fields.InputDataFields.groundtruth_image_classes in groundtruth_dictionary) self.assertAllEqual( np.array([1, 3]), groundtruth_dictionary[ standard_fields.InputDataFields.groundtruth_classes]) self.assertAllEqual( np.array([1, 0]), groundtruth_dictionary[ standard_fields.InputDataFields.groundtruth_group_of]) expected_boxes_data = np.array([[0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2]]) self.assertNDArrayNear( expected_boxes_data, groundtruth_dictionary[ standard_fields.InputDataFields.groundtruth_boxes], 1e-5) self.assertAllEqual( np.array([1, 2, 3]), groundtruth_dictionary[ standard_fields.InputDataFields.groundtruth_image_classes]) def testBuildPredictionDictionary(self): np_data = pd.DataFrame( [['fe58ec1b06db2bb7', '/m/04bcr3', 0.0, 0.3, 0.5, 0.6, 0.1], [ 'fe58ec1b06db2bb7', '/m/02gy9n', 0.1, 0.2, 0.3, 0.4, 0.2 ], ['fe58ec1b06db2bb7', '/m/04bcr3', 0.0, 0.1, 0.2, 0.3, 0.3]], columns=[ 'ImageID', 'LabelName', 'XMin', 'XMax', 'YMin', 'YMax', 'Score' ]) class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} prediction_dictionary = utils.build_predictions_dictionary( np_data, class_label_map) self.assertTrue(standard_fields.DetectionResultFields.detection_boxes in prediction_dictionary) self.assertTrue(standard_fields.DetectionResultFields.detection_classes in prediction_dictionary) self.assertTrue(standard_fields.DetectionResultFields.detection_scores in prediction_dictionary) self.assertAllEqual( np.array([1, 3, 1]), prediction_dictionary[ standard_fields.DetectionResultFields.detection_classes]) expected_boxes_data = np.array([[0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2], [0.2, 0.0, 0.3, 0.1]]) self.assertNDArrayNear( expected_boxes_data, prediction_dictionary[ standard_fields.DetectionResultFields.detection_boxes], 1e-5) self.assertNDArrayNear( np.array([0.1, 0.2, 0.3]), prediction_dictionary[ standard_fields.DetectionResultFields.detection_scores], 1e-5) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/oid_od_challenge_evaluation_utils_test.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Runs evaluation using OpenImages groundtruth and predictions. Example usage: python \ models/research/object_detection/metrics/oid_vrd_challenge_evaluation.py \ --input_annotations_boxes=/path/to/input/annotations-human-bbox.csv \ --input_annotations_labels=/path/to/input/annotations-label.csv \ --input_class_labelmap=/path/to/input/class_labelmap.pbtxt \ --input_relationship_labelmap=/path/to/input/relationship_labelmap.pbtxt \ --input_predictions=/path/to/input/predictions.csv \ --output_metrics=/path/to/output/metric.csv \ CSVs with bounding box annotations and image label (including the image URLs) can be downloaded from the Open Images Challenge website: https://storage.googleapis.com/openimages/web/challenge.html The format of the input csv and the metrics itself are described on the challenge website. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import pandas as pd from google.protobuf import text_format from object_detection.metrics import io_utils from object_detection.metrics import oid_vrd_challenge_evaluation_utils as utils from object_detection.protos import string_int_label_map_pb2 from object_detection.utils import vrd_evaluation def _load_labelmap(labelmap_path): """Loads labelmap from the labelmap path. Args: labelmap_path: Path to the labelmap. Returns: A dictionary mapping class name to class numerical id. """ label_map = string_int_label_map_pb2.StringIntLabelMap() with open(labelmap_path, 'r') as fid: label_map_string = fid.read() text_format.Merge(label_map_string, label_map) labelmap_dict = {} for item in label_map.item: labelmap_dict[item.name] = item.id return labelmap_dict def _swap_labelmap_dict(labelmap_dict): """Swaps keys and labels in labelmap. Args: labelmap_dict: Input dictionary. Returns: A dictionary mapping class name to class numerical id. """ return dict((v, k) for k, v in labelmap_dict.iteritems()) def main(parsed_args): all_box_annotations = pd.read_csv(parsed_args.input_annotations_boxes) all_label_annotations = pd.read_csv(parsed_args.input_annotations_labels) all_annotations = pd.concat([all_box_annotations, all_label_annotations]) class_label_map = _load_labelmap(parsed_args.input_class_labelmap) relationship_label_map = _load_labelmap( parsed_args.input_relationship_labelmap) relation_evaluator = vrd_evaluation.VRDRelationDetectionEvaluator() phrase_evaluator = vrd_evaluation.VRDPhraseDetectionEvaluator() for _, groundtruth in enumerate(all_annotations.groupby('ImageID')): image_id, image_groundtruth = groundtruth groundtruth_dictionary = utils.build_groundtruth_vrd_dictionary( image_groundtruth, class_label_map, relationship_label_map) relation_evaluator.add_single_ground_truth_image_info( image_id, groundtruth_dictionary) phrase_evaluator.add_single_ground_truth_image_info(image_id, groundtruth_dictionary) all_predictions = pd.read_csv(parsed_args.input_predictions) for _, prediction_data in enumerate(all_predictions.groupby('ImageID')): image_id, image_predictions = prediction_data prediction_dictionary = utils.build_predictions_vrd_dictionary( image_predictions, class_label_map, relationship_label_map) relation_evaluator.add_single_detected_image_info(image_id, prediction_dictionary) phrase_evaluator.add_single_detected_image_info(image_id, prediction_dictionary) relation_metrics = relation_evaluator.evaluate( relationships=_swap_labelmap_dict(relationship_label_map)) phrase_metrics = phrase_evaluator.evaluate( relationships=_swap_labelmap_dict(relationship_label_map)) with open(parsed_args.output_metrics, 'w') as fid: io_utils.write_csv(fid, relation_metrics) io_utils.write_csv(fid, phrase_metrics) if __name__ == '__main__': parser = argparse.ArgumentParser( description= 'Evaluate Open Images Visual Relationship Detection predictions.') parser.add_argument( '--input_annotations_boxes', required=True, help='File with groundtruth vrd annotations.') parser.add_argument( '--input_annotations_labels', required=True, help='File with groundtruth labels annotations') parser.add_argument( '--input_predictions', required=True, help="""File with detection predictions; NOTE: no postprocessing is applied in the evaluation script.""") parser.add_argument( '--input_class_labelmap', required=True, help="""OpenImages Challenge labelmap; note: it is expected to include attributes.""") parser.add_argument( '--input_relationship_labelmap', required=True, help="""OpenImages Challenge relationship labelmap.""") parser.add_argument( '--output_metrics', required=True, help='Output file with csv metrics') args = parser.parse_args() main(args)
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/oid_vrd_challenge_evaluation.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Converts data from CSV to the OpenImagesDetectionChallengeEvaluator format. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from object_detection.core import standard_fields def build_groundtruth_boxes_dictionary(data, class_label_map): """Builds a groundtruth dictionary from groundtruth data in CSV file. Args: data: Pandas DataFrame with the groundtruth data for a single image. class_label_map: Class labelmap from string label name to an integer. Returns: A dictionary with keys suitable for passing to OpenImagesDetectionChallengeEvaluator.add_single_ground_truth_image_info: standard_fields.InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. standard_fields.InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. standard_fields.InputDataFields.verified_labels: integer 1D numpy array containing all classes for which labels are verified. standard_fields.InputDataFields.groundtruth_group_of: Optional length M numpy boolean array denoting whether a groundtruth box contains a group of instances. """ data_boxes = data[data.ConfidenceImageLabel.isnull()] data_labels = data[data.XMin.isnull()] return { standard_fields.InputDataFields.groundtruth_boxes: data_boxes[['YMin', 'XMin', 'YMax', 'XMax']].as_matrix(), standard_fields.InputDataFields.groundtruth_classes: data_boxes['LabelName'].map(lambda x: class_label_map[x]).as_matrix(), standard_fields.InputDataFields.groundtruth_group_of: data_boxes['IsGroupOf'].as_matrix().astype(int), standard_fields.InputDataFields.groundtruth_image_classes: data_labels['LabelName'].map(lambda x: class_label_map[x]) .as_matrix(), } def build_predictions_dictionary(data, class_label_map): """Builds a predictions dictionary from predictions data in CSV file. Args: data: Pandas DataFrame with the predictions data for a single image. class_label_map: Class labelmap from string label name to an integer. Returns: Dictionary with keys suitable for passing to OpenImagesDetectionChallengeEvaluator.add_single_detected_image_info: standard_fields.DetectionResultFields.detection_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` detection boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. standard_fields.DetectionResultFields.detection_scores: float32 numpy array of shape [num_boxes] containing detection scores for the boxes. standard_fields.DetectionResultFields.detection_classes: integer numpy array of shape [num_boxes] containing 1-indexed detection classes for the boxes. """ return { standard_fields.DetectionResultFields.detection_boxes: data[['YMin', 'XMin', 'YMax', 'XMax']].as_matrix(), standard_fields.DetectionResultFields.detection_classes: data['LabelName'].map(lambda x: class_label_map[x]).as_matrix(), standard_fields.DetectionResultFields.detection_scores: data['Score'].as_matrix() }
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/oid_od_challenge_evaluation_utils.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.data_decoders.tf_example_parser.""" import numpy as np import numpy.testing as np_testing import tensorflow as tf from object_detection.core import standard_fields as fields from object_detection.metrics import tf_example_parser class TfExampleDecoderTest(tf.test.TestCase): def _Int64Feature(self, value): return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) def _FloatFeature(self, value): return tf.train.Feature(float_list=tf.train.FloatList(value=value)) def _BytesFeature(self, value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def testParseDetectionsAndGT(self): source_id = 'abc.jpg' # y_min, x_min, y_max, x_max object_bb = np.array([[0.0, 0.5, 0.3], [0.0, 0.1, 0.6], [1.0, 0.6, 0.8], [1.0, 0.6, 0.7]]).transpose() detection_bb = np.array([[0.1, 0.2], [0.0, 0.8], [1.0, 0.6], [1.0, 0.85]]).transpose() object_class_label = [1, 1, 2] object_difficult = [1, 0, 0] object_group_of = [0, 0, 1] verified_labels = [1, 2, 3, 4] detection_class_label = [2, 1] detection_score = [0.5, 0.3] features = { fields.TfExampleFields.source_id: self._BytesFeature(source_id), fields.TfExampleFields.object_bbox_ymin: self._FloatFeature(object_bb[:, 0].tolist()), fields.TfExampleFields.object_bbox_xmin: self._FloatFeature(object_bb[:, 1].tolist()), fields.TfExampleFields.object_bbox_ymax: self._FloatFeature(object_bb[:, 2].tolist()), fields.TfExampleFields.object_bbox_xmax: self._FloatFeature(object_bb[:, 3].tolist()), fields.TfExampleFields.detection_bbox_ymin: self._FloatFeature(detection_bb[:, 0].tolist()), fields.TfExampleFields.detection_bbox_xmin: self._FloatFeature(detection_bb[:, 1].tolist()), fields.TfExampleFields.detection_bbox_ymax: self._FloatFeature(detection_bb[:, 2].tolist()), fields.TfExampleFields.detection_bbox_xmax: self._FloatFeature(detection_bb[:, 3].tolist()), fields.TfExampleFields.detection_class_label: self._Int64Feature(detection_class_label), fields.TfExampleFields.detection_score: self._FloatFeature(detection_score), } example = tf.train.Example(features=tf.train.Features(feature=features)) parser = tf_example_parser.TfExampleDetectionAndGTParser() results_dict = parser.parse(example) self.assertIsNone(results_dict) features[fields.TfExampleFields.object_class_label] = ( self._Int64Feature(object_class_label)) features[fields.TfExampleFields.object_difficult] = ( self._Int64Feature(object_difficult)) example = tf.train.Example(features=tf.train.Features(feature=features)) results_dict = parser.parse(example) self.assertIsNotNone(results_dict) self.assertEqual(source_id, results_dict[fields.DetectionResultFields.key]) np_testing.assert_almost_equal( object_bb, results_dict[fields.InputDataFields.groundtruth_boxes]) np_testing.assert_almost_equal( detection_bb, results_dict[fields.DetectionResultFields.detection_boxes]) np_testing.assert_almost_equal( detection_score, results_dict[fields.DetectionResultFields.detection_scores]) np_testing.assert_almost_equal( detection_class_label, results_dict[fields.DetectionResultFields.detection_classes]) np_testing.assert_almost_equal( object_difficult, results_dict[fields.InputDataFields.groundtruth_difficult]) np_testing.assert_almost_equal( object_class_label, results_dict[fields.InputDataFields.groundtruth_classes]) parser = tf_example_parser.TfExampleDetectionAndGTParser() features[fields.TfExampleFields.object_group_of] = ( self._Int64Feature(object_group_of)) example = tf.train.Example(features=tf.train.Features(feature=features)) results_dict = parser.parse(example) self.assertIsNotNone(results_dict) np_testing.assert_equal( object_group_of, results_dict[fields.InputDataFields.groundtruth_group_of]) features[fields.TfExampleFields.image_class_label] = ( self._Int64Feature(verified_labels)) example = tf.train.Example(features=tf.train.Features(feature=features)) results_dict = parser.parse(example) self.assertIsNotNone(results_dict) np_testing.assert_equal( verified_labels, results_dict[fields.InputDataFields.groundtruth_image_classes]) def testParseString(self): string_val = 'abc' features = {'string': self._BytesFeature(string_val)} example = tf.train.Example(features=tf.train.Features(feature=features)) parser = tf_example_parser.StringParser('string') result = parser.parse(example) self.assertIsNotNone(result) self.assertEqual(result, string_val) parser = tf_example_parser.StringParser('another_string') result = parser.parse(example) self.assertIsNone(result) def testParseFloat(self): float_array_val = [1.5, 1.4, 2.0] features = {'floats': self._FloatFeature(float_array_val)} example = tf.train.Example(features=tf.train.Features(feature=features)) parser = tf_example_parser.FloatParser('floats') result = parser.parse(example) self.assertIsNotNone(result) np_testing.assert_almost_equal(result, float_array_val) parser = tf_example_parser.StringParser('another_floats') result = parser.parse(example) self.assertIsNone(result) def testInt64Parser(self): int_val = [1, 2, 3] features = {'ints': self._Int64Feature(int_val)} example = tf.train.Example(features=tf.train.Features(feature=features)) parser = tf_example_parser.Int64Parser('ints') result = parser.parse(example) self.assertIsNotNone(result) np_testing.assert_almost_equal(result, int_val) parser = tf_example_parser.Int64Parser('another_ints') result = parser.parse(example) self.assertIsNone(result) def testBoundingBoxParser(self): bounding_boxes = np.array([[0.0, 0.5, 0.3], [0.0, 0.1, 0.6], [1.0, 0.6, 0.8], [1.0, 0.6, 0.7]]).transpose() features = { 'ymin': self._FloatFeature(bounding_boxes[:, 0]), 'xmin': self._FloatFeature(bounding_boxes[:, 1]), 'ymax': self._FloatFeature(bounding_boxes[:, 2]), 'xmax': self._FloatFeature(bounding_boxes[:, 3]) } example = tf.train.Example(features=tf.train.Features(feature=features)) parser = tf_example_parser.BoundingBoxParser('xmin', 'ymin', 'xmax', 'ymax') result = parser.parse(example) self.assertIsNotNone(result) np_testing.assert_almost_equal(result, bounding_boxes) parser = tf_example_parser.BoundingBoxParser('xmin', 'ymin', 'xmax', 'another_ymax') result = parser.parse(example) self.assertIsNone(result) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/tf_example_parser_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for utilities in offline_eval_map_corloc binary.""" import tensorflow as tf from object_detection.metrics import offline_eval_map_corloc as offline_eval class OfflineEvalMapCorlocTest(tf.test.TestCase): def test_generateShardedFilenames(self): test_filename = '/path/to/file' result = offline_eval._generate_sharded_filenames(test_filename) self.assertEqual(result, [test_filename]) test_filename = '/path/to/file-00000-of-00050' result = offline_eval._generate_sharded_filenames(test_filename) self.assertEqual(result, [test_filename]) result = offline_eval._generate_sharded_filenames('/path/to/@3.record') self.assertEqual(result, [ '/path/to/-00000-of-00003.record', '/path/to/-00001-of-00003.record', '/path/to/-00002-of-00003.record' ]) result = offline_eval._generate_sharded_filenames('/path/to/abc@3') self.assertEqual(result, [ '/path/to/abc-00000-of-00003', '/path/to/abc-00001-of-00003', '/path/to/abc-00002-of-00003' ]) result = offline_eval._generate_sharded_filenames('/path/to/@1') self.assertEqual(result, ['/path/to/-00000-of-00001']) def test_generateFilenames(self): test_filenames = ['/path/to/file', '/path/to/@3.record'] result = offline_eval._generate_filenames(test_filenames) self.assertEqual(result, [ '/path/to/file', '/path/to/-00000-of-00003.record', '/path/to/-00001-of-00003.record', '/path/to/-00002-of-00003.record' ]) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/offline_eval_map_corloc_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tensorflow Example proto parser for data loading. A parser to decode data containing serialized tensorflow.Example protos into materialized tensors (numpy arrays). """ import numpy as np from object_detection.core import data_parser from object_detection.core import standard_fields as fields class FloatParser(data_parser.DataToNumpyParser): """Tensorflow Example float parser.""" def __init__(self, field_name): self.field_name = field_name def parse(self, tf_example): return np.array( tf_example.features.feature[self.field_name].float_list.value, dtype=np.float).transpose() if tf_example.features.feature[ self.field_name].HasField("float_list") else None class StringParser(data_parser.DataToNumpyParser): """Tensorflow Example string parser.""" def __init__(self, field_name): self.field_name = field_name def parse(self, tf_example): return "".join(tf_example.features.feature[self.field_name] .bytes_list.value) if tf_example.features.feature[ self.field_name].HasField("bytes_list") else None class Int64Parser(data_parser.DataToNumpyParser): """Tensorflow Example int64 parser.""" def __init__(self, field_name): self.field_name = field_name def parse(self, tf_example): return np.array( tf_example.features.feature[self.field_name].int64_list.value, dtype=np.int64).transpose() if tf_example.features.feature[ self.field_name].HasField("int64_list") else None class BoundingBoxParser(data_parser.DataToNumpyParser): """Tensorflow Example bounding box parser.""" def __init__(self, xmin_field_name, ymin_field_name, xmax_field_name, ymax_field_name): self.field_names = [ ymin_field_name, xmin_field_name, ymax_field_name, xmax_field_name ] def parse(self, tf_example): result = [] parsed = True for field_name in self.field_names: result.append(tf_example.features.feature[field_name].float_list.value) parsed &= ( tf_example.features.feature[field_name].HasField("float_list")) return np.array(result).transpose() if parsed else None class TfExampleDetectionAndGTParser(data_parser.DataToNumpyParser): """Tensorflow Example proto parser.""" def __init__(self): self.items_to_handlers = { fields.DetectionResultFields.key: StringParser(fields.TfExampleFields.source_id), # Object ground truth boxes and classes. fields.InputDataFields.groundtruth_boxes: (BoundingBoxParser( fields.TfExampleFields.object_bbox_xmin, fields.TfExampleFields.object_bbox_ymin, fields.TfExampleFields.object_bbox_xmax, fields.TfExampleFields.object_bbox_ymax)), fields.InputDataFields.groundtruth_classes: ( Int64Parser(fields.TfExampleFields.object_class_label)), # Object detections. fields.DetectionResultFields.detection_boxes: (BoundingBoxParser( fields.TfExampleFields.detection_bbox_xmin, fields.TfExampleFields.detection_bbox_ymin, fields.TfExampleFields.detection_bbox_xmax, fields.TfExampleFields.detection_bbox_ymax)), fields.DetectionResultFields.detection_classes: ( Int64Parser(fields.TfExampleFields.detection_class_label)), fields.DetectionResultFields.detection_scores: ( FloatParser(fields.TfExampleFields.detection_score)), } self.optional_items_to_handlers = { fields.InputDataFields.groundtruth_difficult: Int64Parser(fields.TfExampleFields.object_difficult), fields.InputDataFields.groundtruth_group_of: Int64Parser(fields.TfExampleFields.object_group_of), fields.InputDataFields.groundtruth_image_classes: Int64Parser(fields.TfExampleFields.image_class_label), } def parse(self, tf_example): """Parses tensorflow example and returns a tensor dictionary. Args: tf_example: a tf.Example object. Returns: A dictionary of the following numpy arrays: fields.DetectionResultFields.source_id - string containing original image id. fields.InputDataFields.groundtruth_boxes - a numpy array containing groundtruth boxes. fields.InputDataFields.groundtruth_classes - a numpy array containing groundtruth classes. fields.InputDataFields.groundtruth_group_of - a numpy array containing groundtruth group of flag (optional, None if not specified). fields.InputDataFields.groundtruth_difficult - a numpy array containing groundtruth difficult flag (optional, None if not specified). fields.InputDataFields.groundtruth_image_classes - a numpy array containing groundtruth image-level labels. fields.DetectionResultFields.detection_boxes - a numpy array containing detection boxes. fields.DetectionResultFields.detection_classes - a numpy array containing detection class labels. fields.DetectionResultFields.detection_scores - a numpy array containing detection scores. Returns None if tf.Example was not parsed or non-optional fields were not found. """ results_dict = {} parsed = True for key, parser in self.items_to_handlers.items(): results_dict[key] = parser.parse(tf_example) parsed &= (results_dict[key] is not None) for key, parser in self.optional_items_to_handlers.items(): results_dict[key] = parser.parse(tf_example) return results_dict if parsed else None
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/tf_example_parser.py
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/__init__.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Class for evaluating object detections with COCO metrics.""" import numpy as np import tensorflow as tf from object_detection.core import standard_fields from object_detection.metrics import coco_tools from object_detection.utils import json_utils from object_detection.utils import object_detection_evaluation class CocoDetectionEvaluator(object_detection_evaluation.DetectionEvaluator): """Class to evaluate COCO detection metrics.""" def __init__(self, categories, include_metrics_per_category=False, all_metrics_per_category=False): """Constructor. Args: categories: A list of dicts, each of which has the following keys - 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog'. include_metrics_per_category: If True, include metrics for each category. all_metrics_per_category: Whether to include all the summary metrics for each category in per_category_ap. Be careful with setting it to true if you have more than handful of categories, because it will pollute your mldash. """ super(CocoDetectionEvaluator, self).__init__(categories) # _image_ids is a dictionary that maps unique image ids to Booleans which # indicate whether a corresponding detection has been added. self._image_ids = {} self._groundtruth_list = [] self._detection_boxes_list = [] self._category_id_set = set([cat['id'] for cat in self._categories]) self._annotation_id = 1 self._metrics = None self._include_metrics_per_category = include_metrics_per_category self._all_metrics_per_category = all_metrics_per_category def clear(self): """Clears the state to prepare for a fresh evaluation.""" self._image_ids.clear() self._groundtruth_list = [] self._detection_boxes_list = [] def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Adds groundtruth for a single image to be used for evaluation. If the image has already been added, a warning is logged, and groundtruth is ignored. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A dictionary containing - InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. InputDataFields.groundtruth_is_crowd (optional): integer numpy array of shape [num_boxes] containing iscrowd flag for groundtruth boxes. """ if image_id in self._image_ids: tf.logging.warning('Ignoring ground truth with image id %s since it was ' 'previously added', image_id) return groundtruth_is_crowd = groundtruth_dict.get( standard_fields.InputDataFields.groundtruth_is_crowd) # Drop groundtruth_is_crowd if empty tensor. if groundtruth_is_crowd is not None and not groundtruth_is_crowd.shape[0]: groundtruth_is_crowd = None self._groundtruth_list.extend( coco_tools.ExportSingleImageGroundtruthToCoco( image_id=image_id, next_annotation_id=self._annotation_id, category_id_set=self._category_id_set, groundtruth_boxes=groundtruth_dict[ standard_fields.InputDataFields.groundtruth_boxes], groundtruth_classes=groundtruth_dict[ standard_fields.InputDataFields.groundtruth_classes], groundtruth_is_crowd=groundtruth_is_crowd)) self._annotation_id += groundtruth_dict[standard_fields.InputDataFields. groundtruth_boxes].shape[0] # Boolean to indicate whether a detection has been added for this image. self._image_ids[image_id] = False def add_single_detected_image_info(self, image_id, detections_dict): """Adds detections for a single image to be used for evaluation. If a detection has already been added for this image id, a warning is logged, and the detection is skipped. Args: image_id: A unique string/integer identifier for the image. detections_dict: A dictionary containing - DetectionResultFields.detection_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` detection boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. DetectionResultFields.detection_scores: float32 numpy array of shape [num_boxes] containing detection scores for the boxes. DetectionResultFields.detection_classes: integer numpy array of shape [num_boxes] containing 1-indexed detection classes for the boxes. Raises: ValueError: If groundtruth for the image_id is not available. """ if image_id not in self._image_ids: raise ValueError('Missing groundtruth for image id: {}'.format(image_id)) if self._image_ids[image_id]: tf.logging.warning('Ignoring detection with image id %s since it was ' 'previously added', image_id) return self._detection_boxes_list.extend( coco_tools.ExportSingleImageDetectionBoxesToCoco( image_id=image_id, category_id_set=self._category_id_set, detection_boxes=detections_dict[standard_fields. DetectionResultFields .detection_boxes], detection_scores=detections_dict[standard_fields. DetectionResultFields. detection_scores], detection_classes=detections_dict[standard_fields. DetectionResultFields. detection_classes])) self._image_ids[image_id] = True def dump_detections_to_json_file(self, json_output_path): """Saves the detections into json_output_path in the format used by MS COCO. Args: json_output_path: String containing the output file's path. It can be also None. In that case nothing will be written to the output file. """ if json_output_path and json_output_path is not None: with tf.gfile.GFile(json_output_path, 'w') as fid: tf.logging.info('Dumping detections to output json file.') json_utils.Dump( obj=self._detection_boxes_list, fid=fid, float_digits=4, indent=2) def evaluate(self): """Evaluates the detection boxes and returns a dictionary of coco metrics. Returns: A dictionary holding - 1. summary_metrics: 'DetectionBoxes_Precision/mAP': mean average precision over classes averaged over IOU thresholds ranging from .5 to .95 with .05 increments. 'DetectionBoxes_Precision/[email protected]': mean average precision at 50% IOU 'DetectionBoxes_Precision/[email protected]': mean average precision at 75% IOU 'DetectionBoxes_Precision/mAP (small)': mean average precision for small objects (area < 32^2 pixels). 'DetectionBoxes_Precision/mAP (medium)': mean average precision for medium sized objects (32^2 pixels < area < 96^2 pixels). 'DetectionBoxes_Precision/mAP (large)': mean average precision for large objects (96^2 pixels < area < 10000^2 pixels). 'DetectionBoxes_Recall/AR@1': average recall with 1 detection. 'DetectionBoxes_Recall/AR@10': average recall with 10 detections. 'DetectionBoxes_Recall/AR@100': average recall with 100 detections. 'DetectionBoxes_Recall/AR@100 (small)': average recall for small objects with 100. 'DetectionBoxes_Recall/AR@100 (medium)': average recall for medium objects with 100. 'DetectionBoxes_Recall/AR@100 (large)': average recall for large objects with 100 detections. 2. per_category_ap: if include_metrics_per_category is True, category specific results with keys of the form: 'Precision mAP ByCategory/category' (without the supercategory part if no supercategories exist). For backward compatibility 'PerformanceByCategory' is included in the output regardless of all_metrics_per_category. """ groundtruth_dict = { 'annotations': self._groundtruth_list, 'images': [{'id': image_id} for image_id in self._image_ids], 'categories': self._categories } coco_wrapped_groundtruth = coco_tools.COCOWrapper(groundtruth_dict) coco_wrapped_detections = coco_wrapped_groundtruth.LoadAnnotations( self._detection_boxes_list) box_evaluator = coco_tools.COCOEvalWrapper( coco_wrapped_groundtruth, coco_wrapped_detections, agnostic_mode=False) box_metrics, box_per_category_ap = box_evaluator.ComputeMetrics( include_metrics_per_category=self._include_metrics_per_category, all_metrics_per_category=self._all_metrics_per_category) box_metrics.update(box_per_category_ap) box_metrics = {'DetectionBoxes_'+ key: value for key, value in iter(box_metrics.items())} return box_metrics def get_estimator_eval_metric_ops(self, eval_dict): """Returns a dictionary of eval metric ops. Note that once value_op is called, the detections and groundtruth added via update_op are cleared. This function can take in groundtruth and detections for a batch of images, or for a single image. For the latter case, the batch dimension for input tensors need not be present. Args: eval_dict: A dictionary that holds tensors for evaluating object detection performance. For single-image evaluation, this dictionary may be produced from eval_util.result_dict_for_single_example(). If multi-image evaluation, `eval_dict` should contain the fields 'num_groundtruth_boxes_per_image' and 'num_det_boxes_per_image' to properly unpad the tensors from the batch. Returns: a dictionary of metric names to tuple of value_op and update_op that can be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all update ops must be run together and similarly all value ops must be run together to guarantee correct behaviour. """ def update_op( image_id_batched, groundtruth_boxes_batched, groundtruth_classes_batched, groundtruth_is_crowd_batched, num_gt_boxes_per_image, detection_boxes_batched, detection_scores_batched, detection_classes_batched, num_det_boxes_per_image, is_annotated_batched): """Update operation for adding batch of images to Coco evaluator.""" for (image_id, gt_box, gt_class, gt_is_crowd, num_gt_box, det_box, det_score, det_class, num_det_box, is_annotated) in zip( image_id_batched, groundtruth_boxes_batched, groundtruth_classes_batched, groundtruth_is_crowd_batched, num_gt_boxes_per_image, detection_boxes_batched, detection_scores_batched, detection_classes_batched, num_det_boxes_per_image, is_annotated_batched): if is_annotated: self.add_single_ground_truth_image_info( image_id, { 'groundtruth_boxes': gt_box[:num_gt_box], 'groundtruth_classes': gt_class[:num_gt_box], 'groundtruth_is_crowd': gt_is_crowd[:num_gt_box] }) self.add_single_detected_image_info( image_id, {'detection_boxes': det_box[:num_det_box], 'detection_scores': det_score[:num_det_box], 'detection_classes': det_class[:num_det_box]}) # Unpack items from the evaluation dictionary. input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields image_id = eval_dict[input_data_fields.key] groundtruth_boxes = eval_dict[input_data_fields.groundtruth_boxes] groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] groundtruth_is_crowd = eval_dict.get( input_data_fields.groundtruth_is_crowd, None) detection_boxes = eval_dict[detection_fields.detection_boxes] detection_scores = eval_dict[detection_fields.detection_scores] detection_classes = eval_dict[detection_fields.detection_classes] num_gt_boxes_per_image = eval_dict.get( 'num_groundtruth_boxes_per_image', None) num_det_boxes_per_image = eval_dict.get('num_det_boxes_per_image', None) is_annotated = eval_dict.get('is_annotated', None) if groundtruth_is_crowd is None: groundtruth_is_crowd = tf.zeros_like(groundtruth_classes, dtype=tf.bool) if not image_id.shape.as_list(): # Apply a batch dimension to all tensors. image_id = tf.expand_dims(image_id, 0) groundtruth_boxes = tf.expand_dims(groundtruth_boxes, 0) groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) groundtruth_is_crowd = tf.expand_dims(groundtruth_is_crowd, 0) detection_boxes = tf.expand_dims(detection_boxes, 0) detection_scores = tf.expand_dims(detection_scores, 0) detection_classes = tf.expand_dims(detection_classes, 0) if num_gt_boxes_per_image is None: num_gt_boxes_per_image = tf.shape(groundtruth_boxes)[1:2] else: num_gt_boxes_per_image = tf.expand_dims(num_gt_boxes_per_image, 0) if num_det_boxes_per_image is None: num_det_boxes_per_image = tf.shape(detection_boxes)[1:2] else: num_det_boxes_per_image = tf.expand_dims(num_det_boxes_per_image, 0) if is_annotated is None: is_annotated = tf.constant([True]) else: is_annotated = tf.expand_dims(is_annotated, 0) else: if num_gt_boxes_per_image is None: num_gt_boxes_per_image = tf.tile( tf.shape(groundtruth_boxes)[1:2], multiples=tf.shape(groundtruth_boxes)[0:1]) if num_det_boxes_per_image is None: num_det_boxes_per_image = tf.tile( tf.shape(detection_boxes)[1:2], multiples=tf.shape(detection_boxes)[0:1]) if is_annotated is None: is_annotated = tf.ones_like(image_id, dtype=tf.bool) update_op = tf.py_func(update_op, [image_id, groundtruth_boxes, groundtruth_classes, groundtruth_is_crowd, num_gt_boxes_per_image, detection_boxes, detection_scores, detection_classes, num_det_boxes_per_image, is_annotated], []) metric_names = ['DetectionBoxes_Precision/mAP', 'DetectionBoxes_Precision/[email protected]', 'DetectionBoxes_Precision/[email protected]', 'DetectionBoxes_Precision/mAP (large)', 'DetectionBoxes_Precision/mAP (medium)', 'DetectionBoxes_Precision/mAP (small)', 'DetectionBoxes_Recall/AR@1', 'DetectionBoxes_Recall/AR@10', 'DetectionBoxes_Recall/AR@100', 'DetectionBoxes_Recall/AR@100 (large)', 'DetectionBoxes_Recall/AR@100 (medium)', 'DetectionBoxes_Recall/AR@100 (small)'] if self._include_metrics_per_category: for category_dict in self._categories: metric_names.append('DetectionBoxes_PerformanceByCategory/mAP/' + category_dict['name']) def first_value_func(): self._metrics = self.evaluate() self.clear() return np.float32(self._metrics[metric_names[0]]) def value_func_factory(metric_name): def value_func(): return np.float32(self._metrics[metric_name]) return value_func # Ensure that the metrics are only evaluated once. first_value_op = tf.py_func(first_value_func, [], tf.float32) eval_metric_ops = {metric_names[0]: (first_value_op, update_op)} with tf.control_dependencies([first_value_op]): for metric_name in metric_names[1:]: eval_metric_ops[metric_name] = (tf.py_func( value_func_factory(metric_name), [], np.float32), update_op) return eval_metric_ops def _check_mask_type_and_value(array_name, masks): """Checks whether mask dtype is uint8 and the values are either 0 or 1.""" if masks.dtype != np.uint8: raise ValueError('{} must be of type np.uint8. Found {}.'.format( array_name, masks.dtype)) if np.any(np.logical_and(masks != 0, masks != 1)): raise ValueError('{} elements can only be either 0 or 1.'.format( array_name)) class CocoMaskEvaluator(object_detection_evaluation.DetectionEvaluator): """Class to evaluate COCO detection metrics.""" def __init__(self, categories, include_metrics_per_category=False): """Constructor. Args: categories: A list of dicts, each of which has the following keys - 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog'. include_metrics_per_category: If True, include metrics for each category. """ super(CocoMaskEvaluator, self).__init__(categories) self._image_id_to_mask_shape_map = {} self._image_ids_with_detections = set([]) self._groundtruth_list = [] self._detection_masks_list = [] self._category_id_set = set([cat['id'] for cat in self._categories]) self._annotation_id = 1 self._include_metrics_per_category = include_metrics_per_category def clear(self): """Clears the state to prepare for a fresh evaluation.""" self._image_id_to_mask_shape_map.clear() self._image_ids_with_detections.clear() self._groundtruth_list = [] self._detection_masks_list = [] def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Adds groundtruth for a single image to be used for evaluation. If the image has already been added, a warning is logged, and groundtruth is ignored. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A dictionary containing - InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. InputDataFields.groundtruth_instance_masks: uint8 numpy array of shape [num_boxes, image_height, image_width] containing groundtruth masks corresponding to the boxes. The elements of the array must be in {0, 1}. """ if image_id in self._image_id_to_mask_shape_map: tf.logging.warning('Ignoring ground truth with image id %s since it was ' 'previously added', image_id) return groundtruth_instance_masks = groundtruth_dict[ standard_fields.InputDataFields.groundtruth_instance_masks] _check_mask_type_and_value(standard_fields.InputDataFields. groundtruth_instance_masks, groundtruth_instance_masks) self._groundtruth_list.extend( coco_tools. ExportSingleImageGroundtruthToCoco( image_id=image_id, next_annotation_id=self._annotation_id, category_id_set=self._category_id_set, groundtruth_boxes=groundtruth_dict[standard_fields.InputDataFields. groundtruth_boxes], groundtruth_classes=groundtruth_dict[standard_fields. InputDataFields. groundtruth_classes], groundtruth_masks=groundtruth_instance_masks)) self._annotation_id += groundtruth_dict[standard_fields.InputDataFields. groundtruth_boxes].shape[0] self._image_id_to_mask_shape_map[image_id] = groundtruth_dict[ standard_fields.InputDataFields.groundtruth_instance_masks].shape def add_single_detected_image_info(self, image_id, detections_dict): """Adds detections for a single image to be used for evaluation. If a detection has already been added for this image id, a warning is logged, and the detection is skipped. Args: image_id: A unique string/integer identifier for the image. detections_dict: A dictionary containing - DetectionResultFields.detection_scores: float32 numpy array of shape [num_boxes] containing detection scores for the boxes. DetectionResultFields.detection_classes: integer numpy array of shape [num_boxes] containing 1-indexed detection classes for the boxes. DetectionResultFields.detection_masks: optional uint8 numpy array of shape [num_boxes, image_height, image_width] containing instance masks corresponding to the boxes. The elements of the array must be in {0, 1}. Raises: ValueError: If groundtruth for the image_id is not available or if spatial shapes of groundtruth_instance_masks and detection_masks are incompatible. """ if image_id not in self._image_id_to_mask_shape_map: raise ValueError('Missing groundtruth for image id: {}'.format(image_id)) if image_id in self._image_ids_with_detections: tf.logging.warning('Ignoring detection with image id %s since it was ' 'previously added', image_id) return groundtruth_masks_shape = self._image_id_to_mask_shape_map[image_id] detection_masks = detections_dict[standard_fields.DetectionResultFields. detection_masks] if groundtruth_masks_shape[1:] != detection_masks.shape[1:]: raise ValueError('Spatial shape of groundtruth masks and detection masks ' 'are incompatible: {} vs {}'.format( groundtruth_masks_shape, detection_masks.shape)) _check_mask_type_and_value(standard_fields.DetectionResultFields. detection_masks, detection_masks) self._detection_masks_list.extend( coco_tools.ExportSingleImageDetectionMasksToCoco( image_id=image_id, category_id_set=self._category_id_set, detection_masks=detection_masks, detection_scores=detections_dict[standard_fields. DetectionResultFields. detection_scores], detection_classes=detections_dict[standard_fields. DetectionResultFields. detection_classes])) self._image_ids_with_detections.update([image_id]) def dump_detections_to_json_file(self, json_output_path): """Saves the detections into json_output_path in the format used by MS COCO. Args: json_output_path: String containing the output file's path. It can be also None. In that case nothing will be written to the output file. """ if json_output_path and json_output_path is not None: tf.logging.info('Dumping detections to output json file.') with tf.gfile.GFile(json_output_path, 'w') as fid: json_utils.Dump( obj=self._detection_masks_list, fid=fid, float_digits=4, indent=2) def evaluate(self): """Evaluates the detection masks and returns a dictionary of coco metrics. Returns: A dictionary holding - 1. summary_metrics: 'DetectionMasks_Precision/mAP': mean average precision over classes averaged over IOU thresholds ranging from .5 to .95 with .05 increments. 'DetectionMasks_Precision/[email protected]': mean average precision at 50% IOU. 'DetectionMasks_Precision/[email protected]': mean average precision at 75% IOU. 'DetectionMasks_Precision/mAP (small)': mean average precision for small objects (area < 32^2 pixels). 'DetectionMasks_Precision/mAP (medium)': mean average precision for medium sized objects (32^2 pixels < area < 96^2 pixels). 'DetectionMasks_Precision/mAP (large)': mean average precision for large objects (96^2 pixels < area < 10000^2 pixels). 'DetectionMasks_Recall/AR@1': average recall with 1 detection. 'DetectionMasks_Recall/AR@10': average recall with 10 detections. 'DetectionMasks_Recall/AR@100': average recall with 100 detections. 'DetectionMasks_Recall/AR@100 (small)': average recall for small objects with 100 detections. 'DetectionMasks_Recall/AR@100 (medium)': average recall for medium objects with 100 detections. 'DetectionMasks_Recall/AR@100 (large)': average recall for large objects with 100 detections. 2. per_category_ap: if include_metrics_per_category is True, category specific results with keys of the form: 'Precision mAP ByCategory/category' (without the supercategory part if no supercategories exist). For backward compatibility 'PerformanceByCategory' is included in the output regardless of all_metrics_per_category. """ groundtruth_dict = { 'annotations': self._groundtruth_list, 'images': [{'id': image_id, 'height': shape[1], 'width': shape[2]} for image_id, shape in self._image_id_to_mask_shape_map. iteritems()], 'categories': self._categories } coco_wrapped_groundtruth = coco_tools.COCOWrapper( groundtruth_dict, detection_type='segmentation') coco_wrapped_detection_masks = coco_wrapped_groundtruth.LoadAnnotations( self._detection_masks_list) mask_evaluator = coco_tools.COCOEvalWrapper( coco_wrapped_groundtruth, coco_wrapped_detection_masks, agnostic_mode=False, iou_type='segm') mask_metrics, mask_per_category_ap = mask_evaluator.ComputeMetrics( include_metrics_per_category=self._include_metrics_per_category) mask_metrics.update(mask_per_category_ap) mask_metrics = {'DetectionMasks_'+ key: value for key, value in mask_metrics.iteritems()} return mask_metrics def get_estimator_eval_metric_ops(self, eval_dict): """Returns a dictionary of eval metric ops. Note that once value_op is called, the detections and groundtruth added via update_op are cleared. Args: eval_dict: A dictionary that holds tensors for evaluating object detection performance. For single-image evaluation, this dictionary may be produced from eval_util.result_dict_for_single_example(). If multi-image evaluation, `eval_dict` should contain the fields 'num_groundtruth_boxes_per_image' and 'num_det_boxes_per_image' to properly unpad the tensors from the batch. Returns: a dictionary of metric names to tuple of value_op and update_op that can be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all update ops must be run together and similarly all value ops must be run together to guarantee correct behaviour. """ def update_op(image_id_batched, groundtruth_boxes_batched, groundtruth_classes_batched, groundtruth_instance_masks_batched, groundtruth_is_crowd_batched, num_gt_boxes_per_image, detection_scores_batched, detection_classes_batched, detection_masks_batched, num_det_boxes_per_image): """Update op for metrics.""" for (image_id, groundtruth_boxes, groundtruth_classes, groundtruth_instance_masks, groundtruth_is_crowd, num_gt_box, detection_scores, detection_classes, detection_masks, num_det_box) in zip( image_id_batched, groundtruth_boxes_batched, groundtruth_classes_batched, groundtruth_instance_masks_batched, groundtruth_is_crowd_batched, num_gt_boxes_per_image, detection_scores_batched, detection_classes_batched, detection_masks_batched, num_det_boxes_per_image): self.add_single_ground_truth_image_info( image_id, { 'groundtruth_boxes': groundtruth_boxes[:num_gt_box], 'groundtruth_classes': groundtruth_classes[:num_gt_box], 'groundtruth_instance_masks': groundtruth_instance_masks[:num_gt_box], 'groundtruth_is_crowd': groundtruth_is_crowd[:num_gt_box] }) self.add_single_detected_image_info( image_id, { 'detection_scores': detection_scores[:num_det_box], 'detection_classes': detection_classes[:num_det_box], 'detection_masks': detection_masks[:num_det_box] }) # Unpack items from the evaluation dictionary. input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields image_id = eval_dict[input_data_fields.key] groundtruth_boxes = eval_dict[input_data_fields.groundtruth_boxes] groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] groundtruth_instance_masks = eval_dict[ input_data_fields.groundtruth_instance_masks] groundtruth_is_crowd = eval_dict.get( input_data_fields.groundtruth_is_crowd, None) num_gt_boxes_per_image = eval_dict.get( input_data_fields.num_groundtruth_boxes, None) detection_scores = eval_dict[detection_fields.detection_scores] detection_classes = eval_dict[detection_fields.detection_classes] detection_masks = eval_dict[detection_fields.detection_masks] num_det_boxes_per_image = eval_dict.get(detection_fields.num_detections, None) if groundtruth_is_crowd is None: groundtruth_is_crowd = tf.zeros_like(groundtruth_classes, dtype=tf.bool) if not image_id.shape.as_list(): # Apply a batch dimension to all tensors. image_id = tf.expand_dims(image_id, 0) groundtruth_boxes = tf.expand_dims(groundtruth_boxes, 0) groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) groundtruth_instance_masks = tf.expand_dims(groundtruth_instance_masks, 0) groundtruth_is_crowd = tf.expand_dims(groundtruth_is_crowd, 0) detection_scores = tf.expand_dims(detection_scores, 0) detection_classes = tf.expand_dims(detection_classes, 0) detection_masks = tf.expand_dims(detection_masks, 0) if num_gt_boxes_per_image is None: num_gt_boxes_per_image = tf.shape(groundtruth_boxes)[1:2] else: num_gt_boxes_per_image = tf.expand_dims(num_gt_boxes_per_image, 0) if num_det_boxes_per_image is None: num_det_boxes_per_image = tf.shape(detection_scores)[1:2] else: num_det_boxes_per_image = tf.expand_dims(num_det_boxes_per_image, 0) else: if num_gt_boxes_per_image is None: num_gt_boxes_per_image = tf.tile( tf.shape(groundtruth_boxes)[1:2], multiples=tf.shape(groundtruth_boxes)[0:1]) if num_det_boxes_per_image is None: num_det_boxes_per_image = tf.tile( tf.shape(detection_scores)[1:2], multiples=tf.shape(detection_scores)[0:1]) update_op = tf.py_func(update_op, [ image_id, groundtruth_boxes, groundtruth_classes, groundtruth_instance_masks, groundtruth_is_crowd, num_gt_boxes_per_image, detection_scores, detection_classes, detection_masks, num_det_boxes_per_image ], []) metric_names = ['DetectionMasks_Precision/mAP', 'DetectionMasks_Precision/[email protected]', 'DetectionMasks_Precision/[email protected]', 'DetectionMasks_Precision/mAP (large)', 'DetectionMasks_Precision/mAP (medium)', 'DetectionMasks_Precision/mAP (small)', 'DetectionMasks_Recall/AR@1', 'DetectionMasks_Recall/AR@10', 'DetectionMasks_Recall/AR@100', 'DetectionMasks_Recall/AR@100 (large)', 'DetectionMasks_Recall/AR@100 (medium)', 'DetectionMasks_Recall/AR@100 (small)'] if self._include_metrics_per_category: for category_dict in self._categories: metric_names.append('DetectionMasks_PerformanceByCategory/mAP/' + category_dict['name']) def first_value_func(): self._metrics = self.evaluate() self.clear() return np.float32(self._metrics[metric_names[0]]) def value_func_factory(metric_name): def value_func(): return np.float32(self._metrics[metric_name]) return value_func # Ensure that the metrics are only evaluated once. first_value_op = tf.py_func(first_value_func, [], tf.float32) eval_metric_ops = {metric_names[0]: (first_value_op, update_op)} with tf.control_dependencies([first_value_op]): for metric_name in metric_names[1:]: eval_metric_ops[metric_name] = (tf.py_func( value_func_factory(metric_name), [], np.float32), update_op) return eval_metric_ops
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/coco_evaluation.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for oid_vrd_challenge_evaluation_utils.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np import pandas as pd import tensorflow as tf from object_detection.core import standard_fields from object_detection.metrics import oid_vrd_challenge_evaluation_utils as utils from object_detection.utils import vrd_evaluation class OidVrdChallengeEvaluationUtilsTest(tf.test.TestCase): def testBuildGroundtruthDictionary(self): np_data = pd.DataFrame( [[ 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.3, 0.5, 0.6, 0.0, 0.3, 0.5, 0.6, 'is', None, None ], [ 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/02gy9n', 0.0, 0.3, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 'under', None, None ], [ 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.1, 0.2, 0.3, 0.0, 0.1, 0.2, 0.3, 'is', None, None ], [ 'fe58ec1b06db2bb7', '/m/083vt', '/m/04bcr3', 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 'at', None, None ], [ 'fe58ec1b06db2bb7', None, None, None, None, None, None, None, None, None, None, None, '/m/04bcr3', 1.0 ], [ 'fe58ec1b06db2bb7', None, None, None, None, None, None, None, None, None, None, None, '/m/083vt', 0.0 ], [ 'fe58ec1b06db2bb7', None, None, None, None, None, None, None, None, None, None, None, '/m/02gy9n', 0.0 ]], columns=[ 'ImageID', 'LabelName1', 'LabelName2', 'XMin1', 'XMax1', 'YMin1', 'YMax1', 'XMin2', 'XMax2', 'YMin2', 'YMax2', 'RelationshipLabel', 'LabelName', 'Confidence' ]) class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} relationship_label_map = {'is': 1, 'under': 2, 'at': 3} groundtruth_dictionary = utils.build_groundtruth_vrd_dictionary( np_data, class_label_map, relationship_label_map) self.assertTrue(standard_fields.InputDataFields.groundtruth_boxes in groundtruth_dictionary) self.assertTrue(standard_fields.InputDataFields.groundtruth_classes in groundtruth_dictionary) self.assertTrue(standard_fields.InputDataFields.groundtruth_image_classes in groundtruth_dictionary) self.assertAllEqual( np.array( [(1, 2, 1), (1, 3, 2), (1, 2, 1), (2, 1, 3)], dtype=vrd_evaluation.label_data_type), groundtruth_dictionary[ standard_fields.InputDataFields.groundtruth_classes]) expected_vrd_data = np.array( [ ([0.5, 0.0, 0.6, 0.3], [0.5, 0.0, 0.6, 0.3]), ([0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2]), ([0.2, 0.0, 0.3, 0.1], [0.2, 0.0, 0.3, 0.1]), ([0.3, 0.1, 0.4, 0.2], [0.7, 0.5, 0.8, 0.6]), ], dtype=vrd_evaluation.vrd_box_data_type) for field in expected_vrd_data.dtype.fields: self.assertNDArrayNear( expected_vrd_data[field], groundtruth_dictionary[ standard_fields.InputDataFields.groundtruth_boxes][field], 1e-5) self.assertAllEqual( np.array([1, 2, 3]), groundtruth_dictionary[ standard_fields.InputDataFields.groundtruth_image_classes]) def testBuildPredictionDictionary(self): np_data = pd.DataFrame( [[ 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.3, 0.5, 0.6, 0.0, 0.3, 0.5, 0.6, 'is', 0.1 ], [ 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/02gy9n', 0.0, 0.3, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 'under', 0.2 ], [ 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.1, 0.2, 0.3, 0.0, 0.1, 0.2, 0.3, 'is', 0.3 ], [ 'fe58ec1b06db2bb7', '/m/083vt', '/m/04bcr3', 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 'at', 0.4 ]], columns=[ 'ImageID', 'LabelName1', 'LabelName2', 'XMin1', 'XMax1', 'YMin1', 'YMax1', 'XMin2', 'XMax2', 'YMin2', 'YMax2', 'RelationshipLabel', 'Score' ]) class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} relationship_label_map = {'is': 1, 'under': 2, 'at': 3} prediction_dictionary = utils.build_predictions_vrd_dictionary( np_data, class_label_map, relationship_label_map) self.assertTrue(standard_fields.DetectionResultFields.detection_boxes in prediction_dictionary) self.assertTrue(standard_fields.DetectionResultFields.detection_classes in prediction_dictionary) self.assertTrue(standard_fields.DetectionResultFields.detection_scores in prediction_dictionary) self.assertAllEqual( np.array( [(1, 2, 1), (1, 3, 2), (1, 2, 1), (2, 1, 3)], dtype=vrd_evaluation.label_data_type), prediction_dictionary[ standard_fields.DetectionResultFields.detection_classes]) expected_vrd_data = np.array( [ ([0.5, 0.0, 0.6, 0.3], [0.5, 0.0, 0.6, 0.3]), ([0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2]), ([0.2, 0.0, 0.3, 0.1], [0.2, 0.0, 0.3, 0.1]), ([0.3, 0.1, 0.4, 0.2], [0.7, 0.5, 0.8, 0.6]), ], dtype=vrd_evaluation.vrd_box_data_type) for field in expected_vrd_data.dtype.fields: self.assertNDArrayNear( expected_vrd_data[field], prediction_dictionary[ standard_fields.DetectionResultFields.detection_boxes][field], 1e-5) self.assertNDArrayNear( np.array([0.1, 0.2, 0.3, 0.4]), prediction_dictionary[ standard_fields.DetectionResultFields.detection_scores], 1e-5) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for tensorflow_model.object_detection.metrics.coco_tools.""" import json import os import re import numpy as np from pycocotools import mask import tensorflow as tf from object_detection.metrics import coco_tools class CocoToolsTest(tf.test.TestCase): def setUp(self): groundtruth_annotations_list = [ { 'id': 1, 'image_id': 'first', 'category_id': 1, 'bbox': [100., 100., 100., 100.], 'area': 100.**2, 'iscrowd': 0 }, { 'id': 2, 'image_id': 'second', 'category_id': 1, 'bbox': [50., 50., 50., 50.], 'area': 50.**2, 'iscrowd': 0 }, ] image_list = [{'id': 'first'}, {'id': 'second'}] category_list = [{'id': 0, 'name': 'person'}, {'id': 1, 'name': 'cat'}, {'id': 2, 'name': 'dog'}] self._groundtruth_dict = { 'annotations': groundtruth_annotations_list, 'images': image_list, 'categories': category_list } self._detections_list = [ { 'image_id': 'first', 'category_id': 1, 'bbox': [100., 100., 100., 100.], 'score': .8 }, { 'image_id': 'second', 'category_id': 1, 'bbox': [50., 50., 50., 50.], 'score': .7 }, ] def testCocoWrappers(self): groundtruth = coco_tools.COCOWrapper(self._groundtruth_dict) detections = groundtruth.LoadAnnotations(self._detections_list) evaluator = coco_tools.COCOEvalWrapper(groundtruth, detections) summary_metrics, _ = evaluator.ComputeMetrics() self.assertAlmostEqual(1.0, summary_metrics['Precision/mAP']) def testExportGroundtruthToCOCO(self): image_ids = ['first', 'second'] groundtruth_boxes = [np.array([[100, 100, 200, 200]], np.float), np.array([[50, 50, 100, 100]], np.float)] groundtruth_classes = [np.array([1], np.int32), np.array([1], np.int32)] categories = [{'id': 0, 'name': 'person'}, {'id': 1, 'name': 'cat'}, {'id': 2, 'name': 'dog'}] output_path = os.path.join(tf.test.get_temp_dir(), 'groundtruth.json') result = coco_tools.ExportGroundtruthToCOCO( image_ids, groundtruth_boxes, groundtruth_classes, categories, output_path=output_path) self.assertDictEqual(result, self._groundtruth_dict) with tf.gfile.GFile(output_path, 'r') as f: written_result = f.read() # The json output should have floats written to 4 digits of precision. matcher = re.compile(r'"bbox":\s+\[\n\s+\d+.\d\d\d\d,', re.MULTILINE) self.assertTrue(matcher.findall(written_result)) written_result = json.loads(written_result) self.assertAlmostEqual(result, written_result) def testExportDetectionsToCOCO(self): image_ids = ['first', 'second'] detections_boxes = [np.array([[100, 100, 200, 200]], np.float), np.array([[50, 50, 100, 100]], np.float)] detections_scores = [np.array([.8], np.float), np.array([.7], np.float)] detections_classes = [np.array([1], np.int32), np.array([1], np.int32)] categories = [{'id': 0, 'name': 'person'}, {'id': 1, 'name': 'cat'}, {'id': 2, 'name': 'dog'}] output_path = os.path.join(tf.test.get_temp_dir(), 'detections.json') result = coco_tools.ExportDetectionsToCOCO( image_ids, detections_boxes, detections_scores, detections_classes, categories, output_path=output_path) self.assertListEqual(result, self._detections_list) with tf.gfile.GFile(output_path, 'r') as f: written_result = f.read() # The json output should have floats written to 4 digits of precision. matcher = re.compile(r'"bbox":\s+\[\n\s+\d+.\d\d\d\d,', re.MULTILINE) self.assertTrue(matcher.findall(written_result)) written_result = json.loads(written_result) self.assertAlmostEqual(result, written_result) def testExportSegmentsToCOCO(self): image_ids = ['first', 'second'] detection_masks = [np.array( [[[0, 1, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 1, 0, 1]]], dtype=np.uint8), np.array( [[[0, 1, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 1, 0, 1]]], dtype=np.uint8)] for i, detection_mask in enumerate(detection_masks): detection_masks[i] = detection_mask[:, :, :, None] detection_scores = [np.array([.8], np.float), np.array([.7], np.float)] detection_classes = [np.array([1], np.int32), np.array([1], np.int32)] categories = [{'id': 0, 'name': 'person'}, {'id': 1, 'name': 'cat'}, {'id': 2, 'name': 'dog'}] output_path = os.path.join(tf.test.get_temp_dir(), 'segments.json') result = coco_tools.ExportSegmentsToCOCO( image_ids, detection_masks, detection_scores, detection_classes, categories, output_path=output_path) with tf.gfile.GFile(output_path, 'r') as f: written_result = f.read() written_result = json.loads(written_result) mask_load = mask.decode([written_result[0]['segmentation']]) self.assertTrue(np.allclose(mask_load, detection_masks[0])) self.assertAlmostEqual(result, written_result) def testExportKeypointsToCOCO(self): image_ids = ['first', 'second'] detection_keypoints = [ np.array( [[[100, 200], [300, 400], [500, 600]], [[50, 150], [250, 350], [450, 550]]], dtype=np.int32), np.array( [[[110, 210], [310, 410], [510, 610]], [[60, 160], [260, 360], [460, 560]]], dtype=np.int32)] detection_scores = [np.array([.8, 0.2], np.float), np.array([.7, 0.3], np.float)] detection_classes = [np.array([1, 1], np.int32), np.array([1, 1], np.int32)] categories = [{'id': 1, 'name': 'person', 'num_keypoints': 3}, {'id': 2, 'name': 'cat'}, {'id': 3, 'name': 'dog'}] output_path = os.path.join(tf.test.get_temp_dir(), 'keypoints.json') result = coco_tools.ExportKeypointsToCOCO( image_ids, detection_keypoints, detection_scores, detection_classes, categories, output_path=output_path) with tf.gfile.GFile(output_path, 'r') as f: written_result = f.read() written_result = json.loads(written_result) self.assertAlmostEqual(result, written_result) def testSingleImageDetectionBoxesExport(self): boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, 1, 1]], dtype=np.float32) classes = np.array([1, 2, 3], dtype=np.int32) scores = np.array([0.8, 0.2, 0.7], dtype=np.float32) coco_boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, .5, .5]], dtype=np.float32) coco_annotations = coco_tools.ExportSingleImageDetectionBoxesToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), detection_boxes=boxes, detection_classes=classes, detection_scores=scores) for i, annotation in enumerate(coco_annotations): self.assertEqual(annotation['image_id'], 'first_image') self.assertEqual(annotation['category_id'], classes[i]) self.assertAlmostEqual(annotation['score'], scores[i]) self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) def testSingleImageDetectionMaskExport(self): masks = np.array( [[[1, 1,], [1, 1]], [[0, 0], [0, 1]], [[0, 0], [0, 0]]], dtype=np.uint8) classes = np.array([1, 2, 3], dtype=np.int32) scores = np.array([0.8, 0.2, 0.7], dtype=np.float32) coco_annotations = coco_tools.ExportSingleImageDetectionMasksToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), detection_classes=classes, detection_scores=scores, detection_masks=masks) expected_counts = ['04', '31', '4'] for i, mask_annotation in enumerate(coco_annotations): self.assertEqual(mask_annotation['segmentation']['counts'], expected_counts[i]) self.assertTrue(np.all(np.equal(mask.decode( mask_annotation['segmentation']), masks[i]))) self.assertEqual(mask_annotation['image_id'], 'first_image') self.assertEqual(mask_annotation['category_id'], classes[i]) self.assertAlmostEqual(mask_annotation['score'], scores[i]) def testSingleImageGroundtruthExport(self): masks = np.array( [[[1, 1,], [1, 1]], [[0, 0], [0, 1]], [[0, 0], [0, 0]]], dtype=np.uint8) boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, 1, 1]], dtype=np.float32) coco_boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, .5, .5]], dtype=np.float32) classes = np.array([1, 2, 3], dtype=np.int32) is_crowd = np.array([0, 1, 0], dtype=np.int32) next_annotation_id = 1 expected_counts = ['04', '31', '4'] # Tests exporting without passing in is_crowd (for backward compatibility). coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), next_annotation_id=next_annotation_id, groundtruth_boxes=boxes, groundtruth_classes=classes, groundtruth_masks=masks) for i, annotation in enumerate(coco_annotations): self.assertEqual(annotation['segmentation']['counts'], expected_counts[i]) self.assertTrue(np.all(np.equal(mask.decode( annotation['segmentation']), masks[i]))) self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) self.assertEqual(annotation['image_id'], 'first_image') self.assertEqual(annotation['category_id'], classes[i]) self.assertEqual(annotation['id'], i + next_annotation_id) # Tests exporting with is_crowd. coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), next_annotation_id=next_annotation_id, groundtruth_boxes=boxes, groundtruth_classes=classes, groundtruth_masks=masks, groundtruth_is_crowd=is_crowd) for i, annotation in enumerate(coco_annotations): self.assertEqual(annotation['segmentation']['counts'], expected_counts[i]) self.assertTrue(np.all(np.equal(mask.decode( annotation['segmentation']), masks[i]))) self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) self.assertEqual(annotation['image_id'], 'first_image') self.assertEqual(annotation['category_id'], classes[i]) self.assertEqual(annotation['iscrowd'], is_crowd[i]) self.assertEqual(annotation['id'], i + next_annotation_id) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/coco_tools_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Evaluation executable for detection data. This executable evaluates precomputed detections produced by a detection model and writes the evaluation results into csv file metrics.csv, stored in the directory, specified by --eval_dir. The evaluation metrics set is supplied in object_detection.protos.EvalConfig in metrics_set field. Currently two set of metrics are supported: - pascal_voc_metrics: standard PASCAL VOC 2007 metric - open_images_detection_metrics: Open Image V2 metric All other field of object_detection.protos.EvalConfig are ignored. Example usage: ./compute_metrics \ --eval_dir=path/to/eval_dir \ --eval_config_path=path/to/evaluation/configuration/file \ --input_config_path=path/to/input/configuration/file """ import csv import os import re import tensorflow as tf from object_detection.core import standard_fields from object_detection.legacy import evaluator from object_detection.metrics import tf_example_parser from object_detection.utils import config_util from object_detection.utils import label_map_util flags = tf.app.flags tf.logging.set_verbosity(tf.logging.INFO) flags.DEFINE_string('eval_dir', None, 'Directory to write eval summaries to.') flags.DEFINE_string('eval_config_path', None, 'Path to an eval_pb2.EvalConfig config file.') flags.DEFINE_string('input_config_path', None, 'Path to an eval_pb2.InputConfig config file.') FLAGS = flags.FLAGS def _generate_sharded_filenames(filename): m = re.search(r'@(\d{1,})', filename) if m: num_shards = int(m.group(1)) return [ re.sub(r'@(\d{1,})', '-%.5d-of-%.5d' % (i, num_shards), filename) for i in range(num_shards) ] else: return [filename] def _generate_filenames(filenames): result = [] for filename in filenames: result += _generate_sharded_filenames(filename) return result def read_data_and_evaluate(input_config, eval_config): """Reads pre-computed object detections and groundtruth from tf_record. Args: input_config: input config proto of type object_detection.protos.InputReader. eval_config: evaluation config proto of type object_detection.protos.EvalConfig. Returns: Evaluated detections metrics. Raises: ValueError: if input_reader type is not supported or metric type is unknown. """ if input_config.WhichOneof('input_reader') == 'tf_record_input_reader': input_paths = input_config.tf_record_input_reader.input_path categories = label_map_util.create_categories_from_labelmap( input_config.label_map_path) object_detection_evaluators = evaluator.get_evaluators( eval_config, categories) # Support a single evaluator object_detection_evaluator = object_detection_evaluators[0] skipped_images = 0 processed_images = 0 for input_path in _generate_filenames(input_paths): tf.logging.info('Processing file: {0}'.format(input_path)) record_iterator = tf.python_io.tf_record_iterator(path=input_path) data_parser = tf_example_parser.TfExampleDetectionAndGTParser() for string_record in record_iterator: tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 1000, processed_images) processed_images += 1 example = tf.train.Example() example.ParseFromString(string_record) decoded_dict = data_parser.parse(example) if decoded_dict: object_detection_evaluator.add_single_ground_truth_image_info( decoded_dict[standard_fields.DetectionResultFields.key], decoded_dict) object_detection_evaluator.add_single_detected_image_info( decoded_dict[standard_fields.DetectionResultFields.key], decoded_dict) else: skipped_images += 1 tf.logging.info('Skipped images: {0}'.format(skipped_images)) return object_detection_evaluator.evaluate() raise ValueError('Unsupported input_reader_config.') def write_metrics(metrics, output_dir): """Write metrics to the output directory. Args: metrics: A dictionary containing metric names and values. output_dir: Directory to write metrics to. """ tf.logging.info('Writing metrics.') with open(os.path.join(output_dir, 'metrics.csv'), 'w') as csvfile: metrics_writer = csv.writer(csvfile, delimiter=',') for metric_name, metric_value in metrics.items(): metrics_writer.writerow([metric_name, str(metric_value)]) def main(argv): del argv required_flags = ['input_config_path', 'eval_config_path', 'eval_dir'] for flag_name in required_flags: if not getattr(FLAGS, flag_name): raise ValueError('Flag --{} is required'.format(flag_name)) configs = config_util.get_configs_from_multiple_files( eval_input_config_path=FLAGS.input_config_path, eval_config_path=FLAGS.eval_config_path) eval_config = configs['eval_config'] input_config = configs['eval_input_config'] metrics = read_data_and_evaluate(input_config, eval_config) # Save metrics write_metrics(metrics, FLAGS.eval_dir) if __name__ == '__main__': tf.app.run(main)
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/offline_eval_map_corloc.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Common IO utils used in offline metric computation. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import csv def write_csv(fid, metrics): """Writes metrics key-value pairs to CSV file. Args: fid: File identifier of an opened file. metrics: A dictionary with metrics to be written. """ metrics_writer = csv.writer(fid, delimiter=',') for metric_name, metric_value in metrics.items(): metrics_writer.writerow([metric_name, str(metric_value)])
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/io_utils.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for tensorflow_models.object_detection.metrics.coco_evaluation.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np import tensorflow as tf from object_detection.core import standard_fields from object_detection.metrics import coco_evaluation def _get_categories_list(): return [{ 'id': 1, 'name': 'person' }, { 'id': 2, 'name': 'dog' }, { 'id': 3, 'name': 'cat' }] class CocoDetectionEvaluationTest(tf.test.TestCase): def testGetOneMAPWithMatchingGroundtruthAndDetections(self): """Tests that mAP is calculated correctly on GT and Detections.""" coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) coco_evaluator.add_single_ground_truth_image_info( image_id='image1', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]) }) coco_evaluator.add_single_detected_image_info( image_id='image1', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) coco_evaluator.add_single_ground_truth_image_info( image_id='image2', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[50., 50., 100., 100.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]) }) coco_evaluator.add_single_detected_image_info( image_id='image2', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[50., 50., 100., 100.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) coco_evaluator.add_single_ground_truth_image_info( image_id='image3', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[25., 25., 50., 50.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]) }) coco_evaluator.add_single_detected_image_info( image_id='image3', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[25., 25., 50., 50.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) metrics = coco_evaluator.evaluate() self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) def testGetOneMAPWithMatchingGroundtruthAndDetectionsSkipCrowd(self): """Tests computing mAP with is_crowd GT boxes skipped.""" coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) coco_evaluator.add_single_ground_truth_image_info( image_id='image1', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[100., 100., 200., 200.], [99., 99., 200., 200.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1, 2]), standard_fields.InputDataFields.groundtruth_is_crowd: np.array([0, 1]) }) coco_evaluator.add_single_detected_image_info( image_id='image1', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) metrics = coco_evaluator.evaluate() self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) def testGetOneMAPWithMatchingGroundtruthAndDetectionsEmptyCrowd(self): """Tests computing mAP with empty is_crowd array passed in.""" coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) coco_evaluator.add_single_ground_truth_image_info( image_id='image1', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]), standard_fields.InputDataFields.groundtruth_is_crowd: np.array([]) }) coco_evaluator.add_single_detected_image_info( image_id='image1', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) metrics = coco_evaluator.evaluate() self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) def testRejectionOnDuplicateGroundtruth(self): """Tests that groundtruth cannot be added more than once for an image.""" coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) # Add groundtruth image_key1 = 'img1' groundtruth_boxes1 = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]], dtype=float) groundtruth_class_labels1 = np.array([1, 3, 1], dtype=int) coco_evaluator.add_single_ground_truth_image_info(image_key1, { standard_fields.InputDataFields.groundtruth_boxes: groundtruth_boxes1, standard_fields.InputDataFields.groundtruth_classes: groundtruth_class_labels1 }) groundtruth_lists_len = len(coco_evaluator._groundtruth_list) # Add groundtruth with the same image id. coco_evaluator.add_single_ground_truth_image_info(image_key1, { standard_fields.InputDataFields.groundtruth_boxes: groundtruth_boxes1, standard_fields.InputDataFields.groundtruth_classes: groundtruth_class_labels1 }) self.assertEqual(groundtruth_lists_len, len(coco_evaluator._groundtruth_list)) def testRejectionOnDuplicateDetections(self): """Tests that detections cannot be added more than once for an image.""" coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) # Add groundtruth coco_evaluator.add_single_ground_truth_image_info( image_id='image1', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[99., 100., 200., 200.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]) }) coco_evaluator.add_single_detected_image_info( image_id='image1', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) detections_lists_len = len(coco_evaluator._detection_boxes_list) coco_evaluator.add_single_detected_image_info( image_id='image1', # Note that this image id was previously added. detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) self.assertEqual(detections_lists_len, len(coco_evaluator._detection_boxes_list)) def testExceptionRaisedWithMissingGroundtruth(self): """Tests that exception is raised for detection with missing groundtruth.""" coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) with self.assertRaises(ValueError): coco_evaluator.add_single_detected_image_info( image_id='image1', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]) }) class CocoEvaluationPyFuncTest(tf.test.TestCase): def testGetOneMAPWithMatchingGroundtruthAndDetections(self): coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) image_id = tf.placeholder(tf.string, shape=()) groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) detection_scores = tf.placeholder(tf.float32, shape=(None)) detection_classes = tf.placeholder(tf.float32, shape=(None)) input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields eval_dict = { input_data_fields.key: image_id, input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, detection_fields.detection_boxes: detection_boxes, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes } eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] with self.test_session() as sess: sess.run(update_op, feed_dict={ image_id: 'image1', groundtruth_boxes: np.array([[100., 100., 200., 200.]]), groundtruth_classes: np.array([1]), detection_boxes: np.array([[100., 100., 200., 200.]]), detection_scores: np.array([.8]), detection_classes: np.array([1]) }) sess.run(update_op, feed_dict={ image_id: 'image2', groundtruth_boxes: np.array([[50., 50., 100., 100.]]), groundtruth_classes: np.array([3]), detection_boxes: np.array([[50., 50., 100., 100.]]), detection_scores: np.array([.7]), detection_classes: np.array([3]) }) sess.run(update_op, feed_dict={ image_id: 'image3', groundtruth_boxes: np.array([[25., 25., 50., 50.]]), groundtruth_classes: np.array([2]), detection_boxes: np.array([[25., 25., 50., 50.]]), detection_scores: np.array([.9]), detection_classes: np.array([2]) }) metrics = {} for key, (value_op, _) in eval_metric_ops.iteritems(): metrics[key] = value_op metrics = sess.run(metrics) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._detection_boxes_list) self.assertFalse(coco_evaluator._image_ids) def testGetOneMAPWithMatchingGroundtruthAndDetectionsIsAnnotated(self): coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) image_id = tf.placeholder(tf.string, shape=()) groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) is_annotated = tf.placeholder(tf.bool, shape=()) detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) detection_scores = tf.placeholder(tf.float32, shape=(None)) detection_classes = tf.placeholder(tf.float32, shape=(None)) input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields eval_dict = { input_data_fields.key: image_id, input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, 'is_annotated': is_annotated, detection_fields.detection_boxes: detection_boxes, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes } eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] with self.test_session() as sess: sess.run(update_op, feed_dict={ image_id: 'image1', groundtruth_boxes: np.array([[100., 100., 200., 200.]]), groundtruth_classes: np.array([1]), is_annotated: True, detection_boxes: np.array([[100., 100., 200., 200.]]), detection_scores: np.array([.8]), detection_classes: np.array([1]) }) sess.run(update_op, feed_dict={ image_id: 'image2', groundtruth_boxes: np.array([[50., 50., 100., 100.]]), groundtruth_classes: np.array([3]), is_annotated: True, detection_boxes: np.array([[50., 50., 100., 100.]]), detection_scores: np.array([.7]), detection_classes: np.array([3]) }) sess.run(update_op, feed_dict={ image_id: 'image3', groundtruth_boxes: np.array([[25., 25., 50., 50.]]), groundtruth_classes: np.array([2]), is_annotated: True, detection_boxes: np.array([[25., 25., 50., 50.]]), detection_scores: np.array([.9]), detection_classes: np.array([2]) }) sess.run(update_op, feed_dict={ image_id: 'image4', groundtruth_boxes: np.zeros((0, 4)), groundtruth_classes: np.zeros((0)), is_annotated: False, # Note that this image isn't annotated. detection_boxes: np.array([[25., 25., 50., 50.], [25., 25., 70., 50.], [25., 25., 80., 50.], [25., 25., 90., 50.]]), detection_scores: np.array([0.6, 0.7, 0.8, 0.9]), detection_classes: np.array([1, 2, 2, 3]) }) metrics = {} for key, (value_op, _) in eval_metric_ops.iteritems(): metrics[key] = value_op metrics = sess.run(metrics) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._detection_boxes_list) self.assertFalse(coco_evaluator._image_ids) def testGetOneMAPWithMatchingGroundtruthAndDetectionsPadded(self): coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) image_id = tf.placeholder(tf.string, shape=()) groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) detection_scores = tf.placeholder(tf.float32, shape=(None)) detection_classes = tf.placeholder(tf.float32, shape=(None)) input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields eval_dict = { input_data_fields.key: image_id, input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, detection_fields.detection_boxes: detection_boxes, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes } eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] with self.test_session() as sess: sess.run( update_op, feed_dict={ image_id: 'image1', groundtruth_boxes: np.array([[100., 100., 200., 200.], [-1, -1, -1, -1]]), groundtruth_classes: np.array([1, -1]), detection_boxes: np.array([[100., 100., 200., 200.], [0., 0., 0., 0.]]), detection_scores: np.array([.8, 0.]), detection_classes: np.array([1, -1]) }) sess.run( update_op, feed_dict={ image_id: 'image2', groundtruth_boxes: np.array([[50., 50., 100., 100.], [-1, -1, -1, -1]]), groundtruth_classes: np.array([3, -1]), detection_boxes: np.array([[50., 50., 100., 100.], [0., 0., 0., 0.]]), detection_scores: np.array([.7, 0.]), detection_classes: np.array([3, -1]) }) sess.run( update_op, feed_dict={ image_id: 'image3', groundtruth_boxes: np.array([[25., 25., 50., 50.], [10., 10., 15., 15.]]), groundtruth_classes: np.array([2, 2]), detection_boxes: np.array([[25., 25., 50., 50.], [10., 10., 15., 15.]]), detection_scores: np.array([.95, .9]), detection_classes: np.array([2, 2]) }) metrics = {} for key, (value_op, _) in eval_metric_ops.iteritems(): metrics[key] = value_op metrics = sess.run(metrics) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 0.83333331) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._detection_boxes_list) self.assertFalse(coco_evaluator._image_ids) def testGetOneMAPWithMatchingGroundtruthAndDetectionsBatched(self): coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) batch_size = 3 image_id = tf.placeholder(tf.string, shape=(batch_size)) groundtruth_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) groundtruth_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) detection_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) detection_scores = tf.placeholder(tf.float32, shape=(batch_size, None)) detection_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields eval_dict = { input_data_fields.key: image_id, input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, detection_fields.detection_boxes: detection_boxes, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes } eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] with self.test_session() as sess: sess.run(update_op, feed_dict={ image_id: ['image1', 'image2', 'image3'], groundtruth_boxes: np.array([[[100., 100., 200., 200.]], [[50., 50., 100., 100.]], [[25., 25., 50., 50.]]]), groundtruth_classes: np.array([[1], [3], [2]]), detection_boxes: np.array([[[100., 100., 200., 200.]], [[50., 50., 100., 100.]], [[25., 25., 50., 50.]]]), detection_scores: np.array([[.8], [.7], [.9]]), detection_classes: np.array([[1], [3], [2]]) }) metrics = {} for key, (value_op, _) in eval_metric_ops.iteritems(): metrics[key] = value_op metrics = sess.run(metrics) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._detection_boxes_list) self.assertFalse(coco_evaluator._image_ids) def testGetOneMAPWithMatchingGroundtruthAndDetectionsPaddedBatches(self): coco_evaluator = coco_evaluation.CocoDetectionEvaluator( _get_categories_list()) batch_size = 3 image_id = tf.placeholder(tf.string, shape=(batch_size)) groundtruth_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) groundtruth_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) num_gt_boxes_per_image = tf.placeholder(tf.int32, shape=(None)) detection_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) detection_scores = tf.placeholder(tf.float32, shape=(batch_size, None)) detection_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) num_det_boxes_per_image = tf.placeholder(tf.int32, shape=(None)) input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields eval_dict = { input_data_fields.key: image_id, input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, detection_fields.detection_boxes: detection_boxes, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes, 'num_groundtruth_boxes_per_image': num_gt_boxes_per_image, 'num_det_boxes_per_image': num_det_boxes_per_image } eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] with self.test_session() as sess: sess.run( update_op, feed_dict={ image_id: ['image1', 'image2', 'image3'], groundtruth_boxes: np.array([[[100., 100., 200., 200.], [-1, -1, -1, -1]], [[50., 50., 100., 100.], [-1, -1, -1, -1]], [[25., 25., 50., 50.], [10., 10., 15., 15.]]]), groundtruth_classes: np.array([[1, -1], [3, -1], [2, 2]]), num_gt_boxes_per_image: np.array([1, 1, 2]), detection_boxes: np.array([[[100., 100., 200., 200.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[50., 50., 100., 100.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[25., 25., 50., 50.], [10., 10., 15., 15.], [10., 10., 15., 15.]]]), detection_scores: np.array([[.8, 0., 0.], [.7, 0., 0.], [.95, .9, 0.9]]), detection_classes: np.array([[1, -1, -1], [3, -1, -1], [2, 2, 2]]), num_det_boxes_per_image: np.array([1, 1, 3]), }) # Check the number of bounding boxes added. self.assertEqual(len(coco_evaluator._groundtruth_list), 4) self.assertEqual(len(coco_evaluator._detection_boxes_list), 5) metrics = {} for key, (value_op, _) in eval_metric_ops.iteritems(): metrics[key] = value_op metrics = sess.run(metrics) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 0.83333331) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._detection_boxes_list) self.assertFalse(coco_evaluator._image_ids) class CocoMaskEvaluationTest(tf.test.TestCase): def testGetOneMAPWithMatchingGroundtruthAndDetections(self): coco_evaluator = coco_evaluation.CocoMaskEvaluator(_get_categories_list()) coco_evaluator.add_single_ground_truth_image_info( image_id='image1', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]), standard_fields.InputDataFields.groundtruth_instance_masks: np.pad(np.ones([1, 100, 100], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) coco_evaluator.add_single_detected_image_info( image_id='image1', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[100., 100., 200., 200.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]), standard_fields.DetectionResultFields.detection_masks: np.pad(np.ones([1, 100, 100], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) coco_evaluator.add_single_ground_truth_image_info( image_id='image2', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[50., 50., 100., 100.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]), standard_fields.InputDataFields.groundtruth_instance_masks: np.pad(np.ones([1, 50, 50], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) coco_evaluator.add_single_detected_image_info( image_id='image2', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[50., 50., 100., 100.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]), standard_fields.DetectionResultFields.detection_masks: np.pad(np.ones([1, 50, 50], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) coco_evaluator.add_single_ground_truth_image_info( image_id='image3', groundtruth_dict={ standard_fields.InputDataFields.groundtruth_boxes: np.array([[25., 25., 50., 50.]]), standard_fields.InputDataFields.groundtruth_classes: np.array([1]), standard_fields.InputDataFields.groundtruth_instance_masks: np.pad(np.ones([1, 25, 25], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) coco_evaluator.add_single_detected_image_info( image_id='image3', detections_dict={ standard_fields.DetectionResultFields.detection_boxes: np.array([[25., 25., 50., 50.]]), standard_fields.DetectionResultFields.detection_scores: np.array([.8]), standard_fields.DetectionResultFields.detection_classes: np.array([1]), standard_fields.DetectionResultFields.detection_masks: np.pad(np.ones([1, 25, 25], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) metrics = coco_evaluator.evaluate() self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP'], 1.0) coco_evaluator.clear() self.assertFalse(coco_evaluator._image_id_to_mask_shape_map) self.assertFalse(coco_evaluator._image_ids_with_detections) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._detection_masks_list) class CocoMaskEvaluationPyFuncTest(tf.test.TestCase): def testGetOneMAPWithMatchingGroundtruthAndDetections(self): coco_evaluator = coco_evaluation.CocoMaskEvaluator(_get_categories_list()) image_id = tf.placeholder(tf.string, shape=()) groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) groundtruth_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) detection_scores = tf.placeholder(tf.float32, shape=(None)) detection_classes = tf.placeholder(tf.float32, shape=(None)) detection_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields eval_dict = { input_data_fields.key: image_id, input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, input_data_fields.groundtruth_instance_masks: groundtruth_masks, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes, detection_fields.detection_masks: detection_masks, } eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) _, update_op = eval_metric_ops['DetectionMasks_Precision/mAP'] with self.test_session() as sess: sess.run( update_op, feed_dict={ image_id: 'image1', groundtruth_boxes: np.array([[100., 100., 200., 200.], [50., 50., 100., 100.]]), groundtruth_classes: np.array([1, 2]), groundtruth_masks: np.stack([ np.pad( np.ones([100, 100], dtype=np.uint8), ((10, 10), (10, 10)), mode='constant'), np.pad( np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), mode='constant') ]), detection_scores: np.array([.9, .8]), detection_classes: np.array([2, 1]), detection_masks: np.stack([ np.pad( np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), mode='constant'), np.pad( np.ones([100, 100], dtype=np.uint8), ((10, 10), (10, 10)), mode='constant'), ]) }) sess.run(update_op, feed_dict={ image_id: 'image2', groundtruth_boxes: np.array([[50., 50., 100., 100.]]), groundtruth_classes: np.array([1]), groundtruth_masks: np.pad(np.ones([1, 50, 50], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant'), detection_scores: np.array([.8]), detection_classes: np.array([1]), detection_masks: np.pad(np.ones([1, 50, 50], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) sess.run(update_op, feed_dict={ image_id: 'image3', groundtruth_boxes: np.array([[25., 25., 50., 50.]]), groundtruth_classes: np.array([1]), groundtruth_masks: np.pad(np.ones([1, 25, 25], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant'), detection_scores: np.array([.8]), detection_classes: np.array([1]), detection_masks: np.pad(np.ones([1, 25, 25], dtype=np.uint8), ((0, 0), (10, 10), (10, 10)), mode='constant') }) metrics = {} for key, (value_op, _) in eval_metric_ops.iteritems(): metrics[key] = value_op metrics = sess.run(metrics) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (small)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@1'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@10'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (small)'], 1.0) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._image_ids_with_detections) self.assertFalse(coco_evaluator._image_id_to_mask_shape_map) self.assertFalse(coco_evaluator._detection_masks_list) def testGetOneMAPWithMatchingGroundtruthAndDetectionsBatched(self): coco_evaluator = coco_evaluation.CocoMaskEvaluator(_get_categories_list()) batch_size = 3 image_id = tf.placeholder(tf.string, shape=(batch_size)) groundtruth_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) groundtruth_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) groundtruth_masks = tf.placeholder( tf.uint8, shape=(batch_size, None, None, None)) detection_scores = tf.placeholder(tf.float32, shape=(batch_size, None)) detection_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) detection_masks = tf.placeholder( tf.uint8, shape=(batch_size, None, None, None)) input_data_fields = standard_fields.InputDataFields detection_fields = standard_fields.DetectionResultFields eval_dict = { input_data_fields.key: image_id, input_data_fields.groundtruth_boxes: groundtruth_boxes, input_data_fields.groundtruth_classes: groundtruth_classes, input_data_fields.groundtruth_instance_masks: groundtruth_masks, detection_fields.detection_scores: detection_scores, detection_fields.detection_classes: detection_classes, detection_fields.detection_masks: detection_masks, } eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) _, update_op = eval_metric_ops['DetectionMasks_Precision/mAP'] with self.test_session() as sess: sess.run( update_op, feed_dict={ image_id: ['image1', 'image2', 'image3'], groundtruth_boxes: np.array([[[100., 100., 200., 200.]], [[50., 50., 100., 100.]], [[25., 25., 50., 50.]]]), groundtruth_classes: np.array([[1], [1], [1]]), groundtruth_masks: np.stack([ np.pad( np.ones([1, 100, 100], dtype=np.uint8), ((0, 0), (0, 0), (0, 0)), mode='constant'), np.pad( np.ones([1, 50, 50], dtype=np.uint8), ((0, 0), (25, 25), (25, 25)), mode='constant'), np.pad( np.ones([1, 25, 25], dtype=np.uint8), ((0, 0), (37, 38), (37, 38)), mode='constant') ], axis=0), detection_scores: np.array([[.8], [.8], [.8]]), detection_classes: np.array([[1], [1], [1]]), detection_masks: np.stack([ np.pad( np.ones([1, 100, 100], dtype=np.uint8), ((0, 0), (0, 0), (0, 0)), mode='constant'), np.pad( np.ones([1, 50, 50], dtype=np.uint8), ((0, 0), (25, 25), (25, 25)), mode='constant'), np.pad( np.ones([1, 25, 25], dtype=np.uint8), ((0, 0), (37, 38), (37, 38)), mode='constant') ], axis=0) }) metrics = {} for key, (value_op, _) in eval_metric_ops.iteritems(): metrics[key] = value_op metrics = sess.run(metrics) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/[email protected]'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (small)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@1'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@10'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (large)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (medium)'], 1.0) self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (small)'], 1.0) self.assertFalse(coco_evaluator._groundtruth_list) self.assertFalse(coco_evaluator._image_ids_with_detections) self.assertFalse(coco_evaluator._image_id_to_mask_shape_map) self.assertFalse(coco_evaluator._detection_masks_list) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/coco_evaluation_test.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== r"""Runs evaluation using OpenImages groundtruth and predictions. Example usage: python models/research/object_detection/metrics/oid_od_challenge_evaluation.py \ --input_annotations_boxes=/path/to/input/annotations-human-bbox.csv \ --input_annotations_labels=/path/to/input/annotations-label.csv \ --input_class_labelmap=/path/to/input/class_labelmap.pbtxt \ --input_predictions=/path/to/input/predictions.csv \ --output_metrics=/path/to/output/metric.csv \ CSVs with bounding box annotations and image label (including the image URLs) can be downloaded from the Open Images Challenge website: https://storage.googleapis.com/openimages/web/challenge.html The format of the input csv and the metrics itself are described on the challenge website. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import pandas as pd from google.protobuf import text_format from object_detection.metrics import io_utils from object_detection.metrics import oid_od_challenge_evaluation_utils as utils from object_detection.protos import string_int_label_map_pb2 from object_detection.utils import object_detection_evaluation def _load_labelmap(labelmap_path): """Loads labelmap from the labelmap path. Args: labelmap_path: Path to the labelmap. Returns: A dictionary mapping class name to class numerical id A list with dictionaries, one dictionary per category. """ label_map = string_int_label_map_pb2.StringIntLabelMap() with open(labelmap_path, 'r') as fid: label_map_string = fid.read() text_format.Merge(label_map_string, label_map) labelmap_dict = {} categories = [] for item in label_map.item: labelmap_dict[item.name] = item.id categories.append({'id': item.id, 'name': item.name}) return labelmap_dict, categories def main(parsed_args): all_box_annotations = pd.read_csv(parsed_args.input_annotations_boxes) all_label_annotations = pd.read_csv(parsed_args.input_annotations_labels) all_label_annotations.rename( columns={'Confidence': 'ConfidenceImageLabel'}, inplace=True) all_annotations = pd.concat([all_box_annotations, all_label_annotations]) class_label_map, categories = _load_labelmap(parsed_args.input_class_labelmap) challenge_evaluator = ( object_detection_evaluation.OpenImagesDetectionChallengeEvaluator( categories)) for _, groundtruth in enumerate(all_annotations.groupby('ImageID')): image_id, image_groundtruth = groundtruth groundtruth_dictionary = utils.build_groundtruth_boxes_dictionary( image_groundtruth, class_label_map) challenge_evaluator.add_single_ground_truth_image_info( image_id, groundtruth_dictionary) all_predictions = pd.read_csv(parsed_args.input_predictions) for _, prediction_data in enumerate(all_predictions.groupby('ImageID')): image_id, image_predictions = prediction_data prediction_dictionary = utils.build_predictions_dictionary( image_predictions, class_label_map) challenge_evaluator.add_single_detected_image_info(image_id, prediction_dictionary) metrics = challenge_evaluator.evaluate() with open(parsed_args.output_metrics, 'w') as fid: io_utils.write_csv(fid, metrics) if __name__ == '__main__': parser = argparse.ArgumentParser( description='Evaluate Open Images Object Detection Challenge predictions.' ) parser.add_argument( '--input_annotations_boxes', required=True, help='File with groundtruth boxes annotations.') parser.add_argument( '--input_annotations_labels', required=True, help='File with groundtruth labels annotations') parser.add_argument( '--input_predictions', required=True, help="""File with detection predictions; NOTE: no postprocessing is applied in the evaluation script.""") parser.add_argument( '--input_class_labelmap', required=True, help='Open Images Challenge labelmap.') parser.add_argument( '--output_metrics', required=True, help='Output file with csv metrics') args = parser.parse_args() main(args)
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/metrics/oid_od_challenge_evaluation.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Faster R-CNN meta-architecture definition. General tensorflow implementation of Faster R-CNN detection models. See Faster R-CNN: Ren, Shaoqing, et al. "Faster R-CNN: Towards real-time object detection with region proposal networks." Advances in neural information processing systems. 2015. We allow for three modes: number_of_stages={1, 2, 3}. In case of 1 stage, all of the user facing methods (e.g., predict, postprocess, loss) can be used as if the model consisted only of the RPN, returning class agnostic proposals (these can be thought of as approximate detections with no associated class information). In case of 2 stages, proposals are computed, then passed through a second stage "box classifier" to yield (multi-class) detections. Finally, in case of 3 stages which is only used during eval, proposals are computed, then passed through a second stage "box classifier" that will compute refined boxes and classes, and then features are pooled from the refined and non-maximum suppressed boxes and are passed through the box classifier again. If number of stages is 3 during training it will be reduced to two automatically. Implementations of Faster R-CNN models must define a new FasterRCNNFeatureExtractor and override three methods: `preprocess`, `_extract_proposal_features` (the first stage of the model), and `_extract_box_classifier_features` (the second stage of the model). Optionally, the `restore_fn` method can be overridden. See tests for an example. A few important notes: + Batching conventions: We support batched inference and training where all images within a batch have the same resolution. Batch sizes are determined dynamically via the shape of the input tensors (rather than being specified directly as, e.g., a model constructor). A complication is that due to non-max suppression, we are not guaranteed to get the same number of proposals from the first stage RPN (region proposal network) for each image (though in practice, we should often get the same number of proposals). For this reason we pad to a max number of proposals per image within a batch. This `self.max_num_proposals` property is set to the `first_stage_max_proposals` parameter at inference time and the `second_stage_batch_size` at training time since we subsample the batch to be sent through the box classifier during training. For the second stage of the pipeline, we arrange the proposals for all images within the batch along a single batch dimension. For example, the input to _extract_box_classifier_features is a tensor of shape `[total_num_proposals, crop_height, crop_width, depth]` where total_num_proposals is batch_size * self.max_num_proposals. (And note that per the above comment, a subset of these entries correspond to zero paddings.) + Coordinate representations: Following the API (see model.DetectionModel definition), our outputs after postprocessing operations are always normalized boxes however, internally, we sometimes convert to absolute --- e.g. for loss computation. In particular, anchors and proposal_boxes are both represented as absolute coordinates. Images are resized in the `preprocess` method. The Faster R-CNN meta architecture has two post-processing methods `_postprocess_rpn` which is applied after first stage and `_postprocess_box_classifier` which is applied after second stage. There are three different ways post-processing can happen depending on number_of_stages configured in the meta architecture: 1. When number_of_stages is 1: `_postprocess_rpn` is run as part of the `postprocess` method where true_image_shapes is used to clip proposals, perform non-max suppression and normalize them. 2. When number of stages is 2: `_postprocess_rpn` is run as part of the `_predict_second_stage` method where `resized_image_shapes` is used to clip proposals, perform non-max suppression and normalize them. In this case `postprocess` method skips `_postprocess_rpn` and only runs `_postprocess_box_classifier` using `true_image_shapes` to clip detections, perform non-max suppression and normalize them. 3. When number of stages is 3: `_postprocess_rpn` is run as part of the `_predict_second_stage` using `resized_image_shapes` to clip proposals, perform non-max suppression and normalize them. Subsequently, `_postprocess_box_classifier` is run as part of `_predict_third_stage` using `true_image_shapes` to clip detections, peform non-max suppression and normalize them. In this case, the `postprocess` method skips both `_postprocess_rpn` and `_postprocess_box_classifier`. """ from abc import abstractmethod from functools import partial import tensorflow as tf from object_detection.anchor_generators import grid_anchor_generator from object_detection.builders import box_predictor_builder from object_detection.core import box_list from object_detection.core import box_list_ops from object_detection.core import box_predictor from object_detection.core import losses from object_detection.core import model from object_detection.core import standard_fields as fields from object_detection.core import target_assigner from object_detection.utils import ops from object_detection.utils import shape_utils slim = tf.contrib.slim class FasterRCNNFeatureExtractor(object): """Faster R-CNN Feature Extractor definition.""" def __init__(self, is_training, first_stage_features_stride, batch_norm_trainable=False, reuse_weights=None, weight_decay=0.0): """Constructor. Args: is_training: A boolean indicating whether the training version of the computation graph should be constructed. first_stage_features_stride: Output stride of extracted RPN feature map. batch_norm_trainable: Whether to update batch norm parameters during training or not. When training with a relative large batch size (e.g. 8), it could be desirable to enable batch norm update. reuse_weights: Whether to reuse variables. Default is None. weight_decay: float weight decay for feature extractor (default: 0.0). """ self._is_training = is_training self._first_stage_features_stride = first_stage_features_stride self._train_batch_norm = (batch_norm_trainable and is_training) self._reuse_weights = reuse_weights self._weight_decay = weight_decay @abstractmethod def preprocess(self, resized_inputs): """Feature-extractor specific preprocessing (minus image resizing).""" pass def extract_proposal_features(self, preprocessed_inputs, scope): """Extracts first stage RPN features. This function is responsible for extracting feature maps from preprocessed images. These features are used by the region proposal network (RPN) to predict proposals. Args: preprocessed_inputs: A [batch, height, width, channels] float tensor representing a batch of images. scope: A scope name. Returns: rpn_feature_map: A tensor with shape [batch, height, width, depth] activations: A dictionary mapping activation tensor names to tensors. """ with tf.variable_scope(scope, values=[preprocessed_inputs]): return self._extract_proposal_features(preprocessed_inputs, scope) @abstractmethod def _extract_proposal_features(self, preprocessed_inputs, scope): """Extracts first stage RPN features, to be overridden.""" pass def extract_box_classifier_features(self, proposal_feature_maps, scope): """Extracts second stage box classifier features. Args: proposal_feature_maps: A 4-D float tensor with shape [batch_size * self.max_num_proposals, crop_height, crop_width, depth] representing the feature map cropped to each proposal. scope: A scope name. Returns: proposal_classifier_features: A 4-D float tensor with shape [batch_size * self.max_num_proposals, height, width, depth] representing box classifier features for each proposal. """ with tf.variable_scope( scope, values=[proposal_feature_maps], reuse=tf.AUTO_REUSE): return self._extract_box_classifier_features(proposal_feature_maps, scope) @abstractmethod def _extract_box_classifier_features(self, proposal_feature_maps, scope): """Extracts second stage box classifier features, to be overridden.""" pass def restore_from_classification_checkpoint_fn( self, first_stage_feature_extractor_scope, second_stage_feature_extractor_scope): """Returns a map of variables to load from a foreign checkpoint. Args: first_stage_feature_extractor_scope: A scope name for the first stage feature extractor. second_stage_feature_extractor_scope: A scope name for the second stage feature extractor. Returns: A dict mapping variable names (to load from a checkpoint) to variables in the model graph. """ variables_to_restore = {} for variable in tf.global_variables(): for scope_name in [first_stage_feature_extractor_scope, second_stage_feature_extractor_scope]: if variable.op.name.startswith(scope_name): var_name = variable.op.name.replace(scope_name + '/', '') variables_to_restore[var_name] = variable return variables_to_restore class FasterRCNNMetaArch(model.DetectionModel): """Faster R-CNN Meta-architecture definition.""" def __init__(self, is_training, num_classes, image_resizer_fn, feature_extractor, number_of_stages, first_stage_anchor_generator, first_stage_target_assigner, first_stage_atrous_rate, first_stage_box_predictor_arg_scope_fn, first_stage_box_predictor_kernel_size, first_stage_box_predictor_depth, first_stage_minibatch_size, first_stage_sampler, first_stage_non_max_suppression_fn, first_stage_max_proposals, first_stage_localization_loss_weight, first_stage_objectness_loss_weight, crop_and_resize_fn, initial_crop_size, maxpool_kernel_size, maxpool_stride, second_stage_target_assigner, second_stage_mask_rcnn_box_predictor, second_stage_batch_size, second_stage_sampler, second_stage_non_max_suppression_fn, second_stage_score_conversion_fn, second_stage_localization_loss_weight, second_stage_classification_loss_weight, second_stage_classification_loss, second_stage_mask_prediction_loss_weight=1.0, hard_example_miner=None, parallel_iterations=16, add_summaries=True, clip_anchors_to_image=False, use_static_shapes=False, resize_masks=True): """FasterRCNNMetaArch Constructor. Args: is_training: A boolean indicating whether the training version of the computation graph should be constructed. num_classes: Number of classes. Note that num_classes *does not* include the background category, so if groundtruth labels take values in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the assigned classification targets can range from {0,... K}). image_resizer_fn: A callable for image resizing. This callable takes a rank-3 image tensor of shape [height, width, channels] (corresponding to a single image), an optional rank-3 instance mask tensor of shape [num_masks, height, width] and returns a resized rank-3 image tensor, a resized mask tensor if one was provided in the input. In addition this callable must also return a 1-D tensor of the form [height, width, channels] containing the size of the true image, as the image resizer can perform zero padding. See protos/image_resizer.proto. feature_extractor: A FasterRCNNFeatureExtractor object. number_of_stages: An integer values taking values in {1, 2, 3}. If 1, the function will construct only the Region Proposal Network (RPN) part of the model. If 2, the function will perform box refinement and other auxiliary predictions all in the second stage. If 3, it will extract features from refined boxes and perform the auxiliary predictions on the non-maximum suppressed refined boxes. If is_training is true and the value of number_of_stages is 3, it is reduced to 2 since all the model heads are trained in parallel in second stage during training. first_stage_anchor_generator: An anchor_generator.AnchorGenerator object (note that currently we only support grid_anchor_generator.GridAnchorGenerator objects) first_stage_target_assigner: Target assigner to use for first stage of Faster R-CNN (RPN). first_stage_atrous_rate: A single integer indicating the atrous rate for the single convolution op which is applied to the `rpn_features_to_crop` tensor to obtain a tensor to be used for box prediction. Some feature extractors optionally allow for producing feature maps computed at denser resolutions. The atrous rate is used to compensate for the denser feature maps by using an effectively larger receptive field. (This should typically be set to 1). first_stage_box_predictor_arg_scope_fn: A function to construct tf-slim arg_scope for conv2d, separable_conv2d and fully_connected ops for the RPN box predictor. first_stage_box_predictor_kernel_size: Kernel size to use for the convolution op just prior to RPN box predictions. first_stage_box_predictor_depth: Output depth for the convolution op just prior to RPN box predictions. first_stage_minibatch_size: The "batch size" to use for computing the objectness and location loss of the region proposal network. This "batch size" refers to the number of anchors selected as contributing to the loss function for any given image within the image batch and is only called "batch_size" due to terminology from the Faster R-CNN paper. first_stage_sampler: Sampler to use for first stage loss (RPN loss). first_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression callable that takes `boxes`, `scores` and optional `clip_window`(with all other inputs already set) and returns a dictionary containing tensors with keys: `detection_boxes`, `detection_scores`, `detection_classes`, `num_detections`. This is used to perform non max suppression on the boxes predicted by the Region Proposal Network (RPN). See `post_processing.batch_multiclass_non_max_suppression` for the type and shape of these tensors. first_stage_max_proposals: Maximum number of boxes to retain after performing Non-Max Suppression (NMS) on the boxes predicted by the Region Proposal Network (RPN). first_stage_localization_loss_weight: A float first_stage_objectness_loss_weight: A float crop_and_resize_fn: A differentiable resampler to use for cropping RPN proposal features. initial_crop_size: A single integer indicating the output size (width and height are set to be the same) of the initial bilinear interpolation based cropping during ROI pooling. maxpool_kernel_size: A single integer indicating the kernel size of the max pool op on the cropped feature map during ROI pooling. maxpool_stride: A single integer indicating the stride of the max pool op on the cropped feature map during ROI pooling. second_stage_target_assigner: Target assigner to use for second stage of Faster R-CNN. If the model is configured with multiple prediction heads, this target assigner is used to generate targets for all heads (with the correct `unmatched_class_label`). second_stage_mask_rcnn_box_predictor: Mask R-CNN box predictor to use for the second stage. second_stage_batch_size: The batch size used for computing the classification and refined location loss of the box classifier. This "batch size" refers to the number of proposals selected as contributing to the loss function for any given image within the image batch and is only called "batch_size" due to terminology from the Faster R-CNN paper. second_stage_sampler: Sampler to use for second stage loss (box classifier loss). second_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression callable that takes `boxes`, `scores`, optional `clip_window` and optional (kwarg) `mask` inputs (with all other inputs already set) and returns a dictionary containing tensors with keys: `detection_boxes`, `detection_scores`, `detection_classes`, `num_detections`, and (optionally) `detection_masks`. See `post_processing.batch_multiclass_non_max_suppression` for the type and shape of these tensors. second_stage_score_conversion_fn: Callable elementwise nonlinearity (that takes tensors as inputs and returns tensors). This is usually used to convert logits to probabilities. second_stage_localization_loss_weight: A float indicating the scale factor for second stage localization loss. second_stage_classification_loss_weight: A float indicating the scale factor for second stage classification loss. second_stage_classification_loss: Classification loss used by the second stage classifier. Either losses.WeightedSigmoidClassificationLoss or losses.WeightedSoftmaxClassificationLoss. second_stage_mask_prediction_loss_weight: A float indicating the scale factor for second stage mask prediction loss. This is applicable only if second stage box predictor is configured to predict masks. hard_example_miner: A losses.HardExampleMiner object (can be None). parallel_iterations: (Optional) The number of iterations allowed to run in parallel for calls to tf.map_fn. add_summaries: boolean (default: True) controlling whether summary ops should be added to tensorflow graph. clip_anchors_to_image: Normally, anchors generated for a given image size are pruned during training if they lie outside the image window. This option clips the anchors to be within the image instead of pruning. use_static_shapes: If True, uses implementation of ops with static shape guarantees. resize_masks: Indicates whether the masks presend in the groundtruth should be resized in the model with `image_resizer_fn` Raises: ValueError: If `second_stage_batch_size` > `first_stage_max_proposals` at training time. ValueError: If first_stage_anchor_generator is not of type grid_anchor_generator.GridAnchorGenerator. """ # TODO(rathodv): add_summaries is currently unused. Respect that directive # in the future. super(FasterRCNNMetaArch, self).__init__(num_classes=num_classes) if not isinstance(first_stage_anchor_generator, grid_anchor_generator.GridAnchorGenerator): raise ValueError('first_stage_anchor_generator must be of type ' 'grid_anchor_generator.GridAnchorGenerator.') self._is_training = is_training self._image_resizer_fn = image_resizer_fn self._resize_masks = resize_masks self._feature_extractor = feature_extractor self._number_of_stages = number_of_stages self._proposal_target_assigner = first_stage_target_assigner self._detector_target_assigner = second_stage_target_assigner # Both proposal and detector target assigners use the same box coder self._box_coder = self._proposal_target_assigner.box_coder # (First stage) Region proposal network parameters self._first_stage_anchor_generator = first_stage_anchor_generator self._first_stage_atrous_rate = first_stage_atrous_rate self._first_stage_box_predictor_arg_scope_fn = ( first_stage_box_predictor_arg_scope_fn) self._first_stage_box_predictor_kernel_size = ( first_stage_box_predictor_kernel_size) self._first_stage_box_predictor_depth = first_stage_box_predictor_depth self._first_stage_minibatch_size = first_stage_minibatch_size self._first_stage_sampler = first_stage_sampler self._first_stage_box_predictor = ( box_predictor_builder.build_convolutional_box_predictor( is_training=self._is_training, num_classes=1, conv_hyperparams_fn=self._first_stage_box_predictor_arg_scope_fn, use_dropout=False, dropout_keep_prob=1.0, box_code_size=self._box_coder.code_size, kernel_size=1, num_layers_before_predictor=0, min_depth=0, max_depth=0)) self._first_stage_nms_fn = first_stage_non_max_suppression_fn self._first_stage_max_proposals = first_stage_max_proposals self._use_static_shapes = use_static_shapes self._first_stage_localization_loss = ( losses.WeightedSmoothL1LocalizationLoss()) self._first_stage_objectness_loss = ( losses.WeightedSoftmaxClassificationLoss()) self._first_stage_loc_loss_weight = first_stage_localization_loss_weight self._first_stage_obj_loss_weight = first_stage_objectness_loss_weight # Per-region cropping parameters self._crop_and_resize_fn = crop_and_resize_fn self._initial_crop_size = initial_crop_size self._maxpool_kernel_size = maxpool_kernel_size self._maxpool_stride = maxpool_stride self._mask_rcnn_box_predictor = second_stage_mask_rcnn_box_predictor self._second_stage_batch_size = second_stage_batch_size self._second_stage_sampler = second_stage_sampler self._second_stage_nms_fn = second_stage_non_max_suppression_fn self._second_stage_score_conversion_fn = second_stage_score_conversion_fn self._second_stage_localization_loss = ( losses.WeightedSmoothL1LocalizationLoss()) self._second_stage_classification_loss = second_stage_classification_loss self._second_stage_mask_loss = ( losses.WeightedSigmoidClassificationLoss()) self._second_stage_loc_loss_weight = second_stage_localization_loss_weight self._second_stage_cls_loss_weight = second_stage_classification_loss_weight self._second_stage_mask_loss_weight = ( second_stage_mask_prediction_loss_weight) self._hard_example_miner = hard_example_miner self._parallel_iterations = parallel_iterations self.clip_anchors_to_image = clip_anchors_to_image if self._number_of_stages <= 0 or self._number_of_stages > 3: raise ValueError('Number of stages should be a value in {1, 2, 3}.') @property def first_stage_feature_extractor_scope(self): return 'FirstStageFeatureExtractor' @property def second_stage_feature_extractor_scope(self): return 'SecondStageFeatureExtractor' @property def first_stage_box_predictor_scope(self): return 'FirstStageBoxPredictor' @property def second_stage_box_predictor_scope(self): return 'SecondStageBoxPredictor' @property def max_num_proposals(self): """Max number of proposals (to pad to) for each image in the input batch. At training time, this is set to be the `second_stage_batch_size` if hard example miner is not configured, else it is set to `first_stage_max_proposals`. At inference time, this is always set to `first_stage_max_proposals`. Returns: A positive integer. """ if self._is_training and not self._hard_example_miner: return self._second_stage_batch_size return self._first_stage_max_proposals @property def anchors(self): if not self._anchors: raise RuntimeError('anchors have not been constructed yet!') if not isinstance(self._anchors, box_list.BoxList): raise RuntimeError('anchors should be a BoxList object, but is not.') return self._anchors def preprocess(self, inputs): """Feature-extractor specific preprocessing. See base class. For Faster R-CNN, we perform image resizing in the base class --- each class subclassing FasterRCNNMetaArch is responsible for any additional preprocessing (e.g., scaling pixel values to be in [-1, 1]). Args: inputs: a [batch, height_in, width_in, channels] float tensor representing a batch of images with values between 0 and 255.0. Returns: preprocessed_inputs: a [batch, height_out, width_out, channels] float tensor representing a batch of images. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Raises: ValueError: if inputs tensor does not have type tf.float32 """ if inputs.dtype is not tf.float32: raise ValueError('`preprocess` expects a tf.float32 tensor') with tf.name_scope('Preprocessor'): outputs = shape_utils.static_or_dynamic_map_fn( self._image_resizer_fn, elems=inputs, dtype=[tf.float32, tf.int32], parallel_iterations=self._parallel_iterations) resized_inputs = outputs[0] true_image_shapes = outputs[1] return (self._feature_extractor.preprocess(resized_inputs), true_image_shapes) def _compute_clip_window(self, image_shapes): """Computes clip window for non max suppression based on image shapes. This function assumes that the clip window's left top corner is at (0, 0). Args: image_shapes: A 2-D int32 tensor of shape [batch_size, 3] containing shapes of images in the batch. Each row represents [height, width, channels] of an image. Returns: A 2-D float32 tensor of shape [batch_size, 4] containing the clip window for each image in the form [ymin, xmin, ymax, xmax]. """ clip_heights = image_shapes[:, 0] clip_widths = image_shapes[:, 1] clip_window = tf.to_float(tf.stack([tf.zeros_like(clip_heights), tf.zeros_like(clip_heights), clip_heights, clip_widths], axis=1)) return clip_window def predict(self, preprocessed_inputs, true_image_shapes): """Predicts unpostprocessed tensors from input tensor. This function takes an input batch of images and runs it through the forward pass of the network to yield "raw" un-postprocessed predictions. If `number_of_stages` is 1, this function only returns first stage RPN predictions (un-postprocessed). Otherwise it returns both first stage RPN predictions as well as second stage box classifier predictions. Other remarks: + Anchor pruning vs. clipping: following the recommendation of the Faster R-CNN paper, we prune anchors that venture outside the image window at training time and clip anchors to the image window at inference time. + Proposal padding: as described at the top of the file, proposals are padded to self._max_num_proposals and flattened so that proposals from all images within the input batch are arranged along the same batch dimension. Args: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Returns: prediction_dict: a dictionary holding "raw" prediction tensors: 1) rpn_box_predictor_features: A 4-D float32 tensor with shape [batch_size, height, width, depth] to be used for predicting proposal boxes and corresponding objectness scores. 2) rpn_features_to_crop: A 4-D float32 tensor with shape [batch_size, height, width, depth] representing image features to crop using the proposal boxes predicted by the RPN. 3) image_shape: a 1-D tensor of shape [4] representing the input image shape. 4) rpn_box_encodings: 3-D float tensor of shape [batch_size, num_anchors, self._box_coder.code_size] containing predicted boxes. 5) rpn_objectness_predictions_with_background: 3-D float tensor of shape [batch_size, num_anchors, 2] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions (at class index 0). 6) anchors: A 2-D tensor of shape [num_anchors, 4] representing anchors for the first stage RPN (in absolute coordinates). Note that `num_anchors` can differ depending on whether the model is created in training or inference mode. (and if number_of_stages > 1): 7) refined_box_encodings: a 3-D tensor with shape [total_num_proposals, num_classes, self._box_coder.code_size] representing predicted (final) refined box encodings, where total_num_proposals=batch_size*self._max_num_proposals. If using a shared box across classes the shape will instead be [total_num_proposals, 1, self._box_coder.code_size]. 8) class_predictions_with_background: a 3-D tensor with shape [total_num_proposals, num_classes + 1] containing class predictions (logits) for each of the anchors, where total_num_proposals=batch_size*self._max_num_proposals. Note that this tensor *includes* background class predictions (at class index 0). 9) num_proposals: An int32 tensor of shape [batch_size] representing the number of proposals generated by the RPN. `num_proposals` allows us to keep track of which entries are to be treated as zero paddings and which are not since we always pad the number of proposals to be `self.max_num_proposals` for each image. 10) proposal_boxes: A float32 tensor of shape [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes in absolute coordinates. 11) mask_predictions: (optional) a 4-D tensor with shape [total_num_padded_proposals, num_classes, mask_height, mask_width] containing instance mask predictions. Raises: ValueError: If `predict` is called before `preprocess`. """ (rpn_box_predictor_features, rpn_features_to_crop, anchors_boxlist, image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) (rpn_box_encodings, rpn_objectness_predictions_with_background ) = self._predict_rpn_proposals(rpn_box_predictor_features) # The Faster R-CNN paper recommends pruning anchors that venture outside # the image window at training time and clipping at inference time. clip_window = tf.to_float(tf.stack([0, 0, image_shape[1], image_shape[2]])) if self._is_training: if self.clip_anchors_to_image: anchors_boxlist = box_list_ops.clip_to_window( anchors_boxlist, clip_window, filter_nonoverlapping=False) else: (rpn_box_encodings, rpn_objectness_predictions_with_background, anchors_boxlist) = self._remove_invalid_anchors_and_predictions( rpn_box_encodings, rpn_objectness_predictions_with_background, anchors_boxlist, clip_window) else: anchors_boxlist = box_list_ops.clip_to_window( anchors_boxlist, clip_window, filter_nonoverlapping=not self._use_static_shapes) self._anchors = anchors_boxlist prediction_dict = { 'rpn_box_predictor_features': rpn_box_predictor_features, 'rpn_features_to_crop': rpn_features_to_crop, 'image_shape': image_shape, 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'anchors': self._anchors.get() } if self._number_of_stages >= 2: # If mixed-precision training on TPU is enabled, rpn_box_encodings and # rpn_objectness_predictions_with_background are bfloat16 tensors. # Considered prediction results, they need to be casted to float32 # tensors for correct postprocess_rpn computation in predict_second_stage. prediction_dict.update(self._predict_second_stage( tf.to_float(rpn_box_encodings), tf.to_float(rpn_objectness_predictions_with_background), rpn_features_to_crop, self._anchors.get(), image_shape, true_image_shapes)) if self._number_of_stages == 3: prediction_dict = self._predict_third_stage( prediction_dict, true_image_shapes) return prediction_dict def _image_batch_shape_2d(self, image_batch_shape_1d): """Takes a 1-D image batch shape tensor and converts it to a 2-D tensor. Example: If 1-D image batch shape tensor is [2, 300, 300, 3]. The corresponding 2-D image batch tensor would be [[300, 300, 3], [300, 300, 3]] Args: image_batch_shape_1d: 1-D tensor of the form [batch_size, height, width, channels]. Returns: image_batch_shape_2d: 2-D tensor of shape [batch_size, 3] were each row is of the form [height, width, channels]. """ return tf.tile(tf.expand_dims(image_batch_shape_1d[1:], 0), [image_batch_shape_1d[0], 1]) def _predict_second_stage(self, rpn_box_encodings, rpn_objectness_predictions_with_background, rpn_features_to_crop, anchors, image_shape, true_image_shapes): """Predicts the output tensors from second stage of Faster R-CNN. Args: rpn_box_encodings: 4-D float tensor of shape [batch_size, num_valid_anchors, self._box_coder.code_size] containing predicted boxes. rpn_objectness_predictions_with_background: 2-D float tensor of shape [batch_size, num_valid_anchors, 2] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions (at class index 0). rpn_features_to_crop: A 4-D float32 or bfloat16 tensor with shape [batch_size, height, width, depth] representing image features to crop using the proposal boxes predicted by the RPN. anchors: 2-D float tensor of shape [num_anchors, self._box_coder.code_size]. image_shape: A 1D int32 tensors of size [4] containing the image shape. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Returns: prediction_dict: a dictionary holding "raw" prediction tensors: 1) refined_box_encodings: a 3-D tensor with shape [total_num_proposals, num_classes, self._box_coder.code_size] representing predicted (final) refined box encodings, where total_num_proposals=batch_size*self._max_num_proposals. If using a shared box across classes the shape will instead be [total_num_proposals, 1, self._box_coder.code_size]. 2) class_predictions_with_background: a 3-D tensor with shape [total_num_proposals, num_classes + 1] containing class predictions (logits) for each of the anchors, where total_num_proposals=batch_size*self._max_num_proposals. Note that this tensor *includes* background class predictions (at class index 0). 3) num_proposals: An int32 tensor of shape [batch_size] representing the number of proposals generated by the RPN. `num_proposals` allows us to keep track of which entries are to be treated as zero paddings and which are not since we always pad the number of proposals to be `self.max_num_proposals` for each image. 4) proposal_boxes: A float32 tensor of shape [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes in absolute coordinates. 5) proposal_boxes_normalized: A float32 tensor of shape [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes in normalized coordinates. Can be used to override the boxes proposed by the RPN, thus enabling one to extract features and get box classification and prediction for externally selected areas of the image. 6) box_classifier_features: a 4-D float32 or bfloat16 tensor representing the features for each proposal. """ image_shape_2d = self._image_batch_shape_2d(image_shape) proposal_boxes_normalized, _, num_proposals = self._postprocess_rpn( rpn_box_encodings, rpn_objectness_predictions_with_background, anchors, image_shape_2d, true_image_shapes) # If mixed-precision training on TPU is enabled, the dtype of # rpn_features_to_crop is bfloat16, otherwise it is float32. tf.cast is # used to match the dtype of proposal_boxes_normalized to that of # rpn_features_to_crop for further computation. flattened_proposal_feature_maps = ( self._compute_second_stage_input_feature_maps( rpn_features_to_crop, tf.cast(proposal_boxes_normalized, rpn_features_to_crop.dtype))) box_classifier_features = ( self._feature_extractor.extract_box_classifier_features( flattened_proposal_feature_maps, scope=self.second_stage_feature_extractor_scope)) if self._mask_rcnn_box_predictor.is_keras_model: box_predictions = self._mask_rcnn_box_predictor( [box_classifier_features], prediction_stage=2) else: box_predictions = self._mask_rcnn_box_predictor.predict( [box_classifier_features], num_predictions_per_location=[1], scope=self.second_stage_box_predictor_scope, prediction_stage=2) refined_box_encodings = tf.squeeze( box_predictions[box_predictor.BOX_ENCODINGS], axis=1, name='all_refined_box_encodings') class_predictions_with_background = tf.squeeze( box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1, name='all_class_predictions_with_background') absolute_proposal_boxes = ops.normalized_to_image_coordinates( proposal_boxes_normalized, image_shape, self._parallel_iterations) prediction_dict = { 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'num_proposals': num_proposals, 'proposal_boxes': absolute_proposal_boxes, 'box_classifier_features': box_classifier_features, 'proposal_boxes_normalized': proposal_boxes_normalized, } return prediction_dict def _predict_third_stage(self, prediction_dict, image_shapes): """Predicts non-box, non-class outputs using refined detections. For training, masks as predicted directly on the box_classifier_features, which are region-features from the initial anchor boxes. For inference, this happens after calling the post-processing stage, such that masks are only calculated for the top scored boxes. Args: prediction_dict: a dictionary holding "raw" prediction tensors: 1) refined_box_encodings: a 3-D tensor with shape [total_num_proposals, num_classes, self._box_coder.code_size] representing predicted (final) refined box encodings, where total_num_proposals=batch_size*self._max_num_proposals. If using a shared box across classes the shape will instead be [total_num_proposals, 1, self._box_coder.code_size]. 2) class_predictions_with_background: a 3-D tensor with shape [total_num_proposals, num_classes + 1] containing class predictions (logits) for each of the anchors, where total_num_proposals=batch_size*self._max_num_proposals. Note that this tensor *includes* background class predictions (at class index 0). 3) num_proposals: An int32 tensor of shape [batch_size] representing the number of proposals generated by the RPN. `num_proposals` allows us to keep track of which entries are to be treated as zero paddings and which are not since we always pad the number of proposals to be `self.max_num_proposals` for each image. 4) proposal_boxes: A float32 tensor of shape [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes in absolute coordinates. 5) box_classifier_features: a 4-D float32 tensor representing the features for each proposal. image_shapes: A 2-D int32 tensors of shape [batch_size, 3] containing shapes of images in the batch. Returns: prediction_dict: a dictionary that in addition to the input predictions does hold the following predictions as well: 1) mask_predictions: a 4-D tensor with shape [batch_size, max_detection, mask_height, mask_width] containing instance mask predictions. """ if self._is_training: curr_box_classifier_features = prediction_dict['box_classifier_features'] detection_classes = prediction_dict['class_predictions_with_background'] if self._mask_rcnn_box_predictor.is_keras_model: mask_predictions = self._mask_rcnn_box_predictor( [curr_box_classifier_features], prediction_stage=3) else: mask_predictions = self._mask_rcnn_box_predictor.predict( [curr_box_classifier_features], num_predictions_per_location=[1], scope=self.second_stage_box_predictor_scope, prediction_stage=3) prediction_dict['mask_predictions'] = tf.squeeze(mask_predictions[ box_predictor.MASK_PREDICTIONS], axis=1) else: detections_dict = self._postprocess_box_classifier( prediction_dict['refined_box_encodings'], prediction_dict['class_predictions_with_background'], prediction_dict['proposal_boxes'], prediction_dict['num_proposals'], image_shapes) prediction_dict.update(detections_dict) detection_boxes = detections_dict[ fields.DetectionResultFields.detection_boxes] detection_classes = detections_dict[ fields.DetectionResultFields.detection_classes] rpn_features_to_crop = prediction_dict['rpn_features_to_crop'] batch_size = tf.shape(detection_boxes)[0] max_detection = tf.shape(detection_boxes)[1] flattened_detected_feature_maps = ( self._compute_second_stage_input_feature_maps( rpn_features_to_crop, detection_boxes)) curr_box_classifier_features = ( self._feature_extractor.extract_box_classifier_features( flattened_detected_feature_maps, scope=self.second_stage_feature_extractor_scope)) if self._mask_rcnn_box_predictor.is_keras_model: mask_predictions = self._mask_rcnn_box_predictor( [curr_box_classifier_features], prediction_stage=3) else: mask_predictions = self._mask_rcnn_box_predictor.predict( [curr_box_classifier_features], num_predictions_per_location=[1], scope=self.second_stage_box_predictor_scope, prediction_stage=3) detection_masks = tf.squeeze(mask_predictions[ box_predictor.MASK_PREDICTIONS], axis=1) _, num_classes, mask_height, mask_width = ( detection_masks.get_shape().as_list()) _, max_detection = detection_classes.get_shape().as_list() prediction_dict['mask_predictions'] = tf.reshape( detection_masks, [-1, num_classes, mask_height, mask_width]) if num_classes > 1: detection_masks = self._gather_instance_masks( detection_masks, detection_classes) prediction_dict[fields.DetectionResultFields.detection_masks] = ( tf.reshape(tf.sigmoid(detection_masks), [batch_size, max_detection, mask_height, mask_width])) return prediction_dict def _gather_instance_masks(self, instance_masks, classes): """Gathers the masks that correspond to classes. Args: instance_masks: A 4-D float32 tensor with shape [K, num_classes, mask_height, mask_width]. classes: A 2-D int32 tensor with shape [batch_size, max_detection]. Returns: masks: a 3-D float32 tensor with shape [K, mask_height, mask_width]. """ _, num_classes, height, width = instance_masks.get_shape().as_list() k = tf.shape(instance_masks)[0] instance_masks = tf.reshape(instance_masks, [-1, height, width]) classes = tf.to_int32(tf.reshape(classes, [-1])) gather_idx = tf.range(k) * num_classes + classes return tf.gather(instance_masks, gather_idx) def _extract_rpn_feature_maps(self, preprocessed_inputs): """Extracts RPN features. This function extracts two feature maps: a feature map to be directly fed to a box predictor (to predict location and objectness scores for proposals) and a feature map from which to crop regions which will then be sent to the second stage box classifier. Args: preprocessed_inputs: a [batch, height, width, channels] image tensor. Returns: rpn_box_predictor_features: A 4-D float32 tensor with shape [batch, height, width, depth] to be used for predicting proposal boxes and corresponding objectness scores. rpn_features_to_crop: A 4-D float32 tensor with shape [batch, height, width, depth] representing image features to crop using the proposals boxes. anchors: A BoxList representing anchors (for the RPN) in absolute coordinates. image_shape: A 1-D tensor representing the input image shape. """ image_shape = tf.shape(preprocessed_inputs) rpn_features_to_crop, self.endpoints = ( self._feature_extractor.extract_proposal_features( preprocessed_inputs, scope=self.first_stage_feature_extractor_scope)) feature_map_shape = tf.shape(rpn_features_to_crop) anchors = box_list_ops.concatenate( self._first_stage_anchor_generator.generate([(feature_map_shape[1], feature_map_shape[2])])) with slim.arg_scope(self._first_stage_box_predictor_arg_scope_fn()): kernel_size = self._first_stage_box_predictor_kernel_size reuse = tf.get_variable_scope().reuse rpn_box_predictor_features = slim.conv2d( rpn_features_to_crop, self._first_stage_box_predictor_depth, kernel_size=[kernel_size, kernel_size], rate=self._first_stage_atrous_rate, activation_fn=tf.nn.relu6, scope='Conv', reuse=reuse) return (rpn_box_predictor_features, rpn_features_to_crop, anchors, image_shape) def _predict_rpn_proposals(self, rpn_box_predictor_features): """Adds box predictors to RPN feature map to predict proposals. Note resulting tensors will not have been postprocessed. Args: rpn_box_predictor_features: A 4-D float32 tensor with shape [batch, height, width, depth] to be used for predicting proposal boxes and corresponding objectness scores. Returns: box_encodings: 3-D float tensor of shape [batch_size, num_anchors, self._box_coder.code_size] containing predicted boxes. objectness_predictions_with_background: 3-D float tensor of shape [batch_size, num_anchors, 2] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions (at class index 0). Raises: RuntimeError: if the anchor generator generates anchors corresponding to multiple feature maps. We currently assume that a single feature map is generated for the RPN. """ num_anchors_per_location = ( self._first_stage_anchor_generator.num_anchors_per_location()) if len(num_anchors_per_location) != 1: raise RuntimeError('anchor_generator is expected to generate anchors ' 'corresponding to a single feature map.') if self._first_stage_box_predictor.is_keras_model: box_predictions = self._first_stage_box_predictor( [rpn_box_predictor_features]) else: box_predictions = self._first_stage_box_predictor.predict( [rpn_box_predictor_features], num_anchors_per_location, scope=self.first_stage_box_predictor_scope) box_encodings = tf.concat( box_predictions[box_predictor.BOX_ENCODINGS], axis=1) objectness_predictions_with_background = tf.concat( box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) return (tf.squeeze(box_encodings, axis=2), objectness_predictions_with_background) def _remove_invalid_anchors_and_predictions( self, box_encodings, objectness_predictions_with_background, anchors_boxlist, clip_window): """Removes anchors that (partially) fall outside an image. Also removes associated box encodings and objectness predictions. Args: box_encodings: 3-D float tensor of shape [batch_size, num_anchors, self._box_coder.code_size] containing predicted boxes. objectness_predictions_with_background: 3-D float tensor of shape [batch_size, num_anchors, 2] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions (at class index 0). anchors_boxlist: A BoxList representing num_anchors anchors (for the RPN) in absolute coordinates. clip_window: a 1-D tensor representing the [ymin, xmin, ymax, xmax] extent of the window to clip/prune to. Returns: box_encodings: 4-D float tensor of shape [batch_size, num_valid_anchors, self._box_coder.code_size] containing predicted boxes, where num_valid_anchors <= num_anchors objectness_predictions_with_background: 2-D float tensor of shape [batch_size, num_valid_anchors, 2] containing class predictions (logits) for each of the anchors, where num_valid_anchors <= num_anchors. Note that this tensor *includes* background class predictions (at class index 0). anchors: A BoxList representing num_valid_anchors anchors (for the RPN) in absolute coordinates. """ pruned_anchors_boxlist, keep_indices = box_list_ops.prune_outside_window( anchors_boxlist, clip_window) def _batch_gather_kept_indices(predictions_tensor): return shape_utils.static_or_dynamic_map_fn( partial(tf.gather, indices=keep_indices), elems=predictions_tensor, dtype=tf.float32, parallel_iterations=self._parallel_iterations, back_prop=True) return (_batch_gather_kept_indices(box_encodings), _batch_gather_kept_indices(objectness_predictions_with_background), pruned_anchors_boxlist) def _flatten_first_two_dimensions(self, inputs): """Flattens `K-d` tensor along batch dimension to be a `(K-1)-d` tensor. Converts `inputs` with shape [A, B, ..., depth] into a tensor of shape [A * B, ..., depth]. Args: inputs: A float tensor with shape [A, B, ..., depth]. Note that the first two and last dimensions must be statically defined. Returns: A float tensor with shape [A * B, ..., depth] (where the first and last dimension are statically defined. """ combined_shape = shape_utils.combined_static_and_dynamic_shape(inputs) flattened_shape = tf.stack([combined_shape[0] * combined_shape[1]] + combined_shape[2:]) return tf.reshape(inputs, flattened_shape) def postprocess(self, prediction_dict, true_image_shapes): """Convert prediction tensors to final detections. This function converts raw predictions tensors to final detection results. See base class for output format conventions. Note also that by default, scores are to be interpreted as logits, but if a score_converter is used, then scores are remapped (and may thus have a different interpretation). If number_of_stages=1, the returned results represent proposals from the first stage RPN and are padded to have self.max_num_proposals for each image; otherwise, the results can be interpreted as multiclass detections from the full two-stage model and are padded to self._max_detections. Args: prediction_dict: a dictionary holding prediction tensors (see the documentation for the predict method. If number_of_stages=1, we expect prediction_dict to contain `rpn_box_encodings`, `rpn_objectness_predictions_with_background`, `rpn_features_to_crop`, and `anchors` fields. Otherwise we expect prediction_dict to additionally contain `refined_box_encodings`, `class_predictions_with_background`, `num_proposals`, `proposal_boxes` and, optionally, `mask_predictions` fields. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Returns: detections: a dictionary containing the following fields detection_boxes: [batch, max_detection, 4] detection_scores: [batch, max_detections] detection_classes: [batch, max_detections] (this entry is only created if rpn_mode=False) num_detections: [batch] Raises: ValueError: If `predict` is called before `preprocess`. """ with tf.name_scope('FirstStagePostprocessor'): if self._number_of_stages == 1: proposal_boxes, proposal_scores, num_proposals = self._postprocess_rpn( prediction_dict['rpn_box_encodings'], prediction_dict['rpn_objectness_predictions_with_background'], prediction_dict['anchors'], true_image_shapes, true_image_shapes) return { fields.DetectionResultFields.detection_boxes: proposal_boxes, fields.DetectionResultFields.detection_scores: proposal_scores, fields.DetectionResultFields.num_detections: tf.to_float(num_proposals), } # TODO(jrru): Remove mask_predictions from _post_process_box_classifier. if (self._number_of_stages == 2 or (self._number_of_stages == 3 and self._is_training)): with tf.name_scope('SecondStagePostprocessor'): mask_predictions = prediction_dict.get(box_predictor.MASK_PREDICTIONS) detections_dict = self._postprocess_box_classifier( prediction_dict['refined_box_encodings'], prediction_dict['class_predictions_with_background'], prediction_dict['proposal_boxes'], prediction_dict['num_proposals'], true_image_shapes, mask_predictions=mask_predictions) if 'rpn_features_to_crop' in prediction_dict and self._initial_crop_size: self._add_detection_features_output_node( detections_dict[fields.DetectionResultFields.detection_boxes], prediction_dict['rpn_features_to_crop']) return detections_dict if self._number_of_stages == 3: # Post processing is already performed in 3rd stage. We need to transfer # postprocessed tensors from `prediction_dict` to `detections_dict`. return prediction_dict def _add_detection_features_output_node(self, detection_boxes, rpn_features_to_crop): """Add the detection features to the output node. The detection features are from cropping rpn_features with boxes. Each bounding box has one feature vector of length depth, which comes from mean_pooling of the cropped rpn_features. Args: detection_boxes: a 3-D float32 tensor of shape [batch_size, max_detection, 4] which represents the bounding boxes. rpn_features_to_crop: A 4-D float32 tensor with shape [batch, height, width, depth] representing image features to crop using the proposals boxes. """ with tf.name_scope('SecondStageDetectionFeaturesExtract'): flattened_detected_feature_maps = ( self._compute_second_stage_input_feature_maps( rpn_features_to_crop, detection_boxes)) detection_features_unpooled = ( self._feature_extractor.extract_box_classifier_features( flattened_detected_feature_maps, scope=self.second_stage_feature_extractor_scope)) batch_size = tf.shape(detection_boxes)[0] max_detection = tf.shape(detection_boxes)[1] detection_features_pool = tf.reduce_mean( detection_features_unpooled, axis=[1, 2]) detection_features = tf.reshape( detection_features_pool, [batch_size, max_detection, tf.shape(detection_features_pool)[-1]]) detection_features = tf.identity( detection_features, 'detection_features') def _postprocess_rpn(self, rpn_box_encodings_batch, rpn_objectness_predictions_with_background_batch, anchors, image_shapes, true_image_shapes): """Converts first stage prediction tensors from the RPN to proposals. This function decodes the raw RPN predictions, runs non-max suppression on the result. Note that the behavior of this function is slightly modified during training --- specifically, we stop the gradient from passing through the proposal boxes and we only return a balanced sampled subset of proposals with size `second_stage_batch_size`. Args: rpn_box_encodings_batch: A 3-D float32 tensor of shape [batch_size, num_anchors, self._box_coder.code_size] containing predicted proposal box encodings. rpn_objectness_predictions_with_background_batch: A 3-D float tensor of shape [batch_size, num_anchors, 2] containing objectness predictions (logits) for each of the anchors with 0 corresponding to background and 1 corresponding to object. anchors: A 2-D tensor of shape [num_anchors, 4] representing anchors for the first stage RPN. Note that `num_anchors` can differ depending on whether the model is created in training or inference mode. image_shapes: A 2-D tensor of shape [batch, 3] containing the shapes of images in the batch. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Returns: proposal_boxes: A float tensor with shape [batch_size, max_num_proposals, 4] representing the (potentially zero padded) proposal boxes for all images in the batch. These boxes are represented as normalized coordinates. proposal_scores: A float tensor with shape [batch_size, max_num_proposals] representing the (potentially zero padded) proposal objectness scores for all images in the batch. num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] representing the number of proposals predicted for each image in the batch. """ rpn_box_encodings_batch = tf.expand_dims(rpn_box_encodings_batch, axis=2) rpn_encodings_shape = shape_utils.combined_static_and_dynamic_shape( rpn_box_encodings_batch) tiled_anchor_boxes = tf.tile( tf.expand_dims(anchors, 0), [rpn_encodings_shape[0], 1, 1]) proposal_boxes = self._batch_decode_boxes(rpn_box_encodings_batch, tiled_anchor_boxes) proposal_boxes = tf.squeeze(proposal_boxes, axis=2) rpn_objectness_softmax_without_background = tf.nn.softmax( rpn_objectness_predictions_with_background_batch)[:, :, 1] clip_window = self._compute_clip_window(image_shapes) (proposal_boxes, proposal_scores, _, _, _, num_proposals) = self._first_stage_nms_fn( tf.expand_dims(proposal_boxes, axis=2), tf.expand_dims(rpn_objectness_softmax_without_background, axis=2), clip_window=clip_window) if self._is_training: proposal_boxes = tf.stop_gradient(proposal_boxes) if not self._hard_example_miner: (groundtruth_boxlists, groundtruth_classes_with_background_list, _, groundtruth_weights_list ) = self._format_groundtruth_data(true_image_shapes) (proposal_boxes, proposal_scores, num_proposals) = self._sample_box_classifier_batch( proposal_boxes, proposal_scores, num_proposals, groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list) # normalize proposal boxes def normalize_boxes(args): proposal_boxes_per_image = args[0] image_shape = args[1] normalized_boxes_per_image = box_list_ops.to_normalized_coordinates( box_list.BoxList(proposal_boxes_per_image), image_shape[0], image_shape[1], check_range=False).get() return normalized_boxes_per_image normalized_proposal_boxes = shape_utils.static_or_dynamic_map_fn( normalize_boxes, elems=[proposal_boxes, image_shapes], dtype=tf.float32) return normalized_proposal_boxes, proposal_scores, num_proposals def _sample_box_classifier_batch( self, proposal_boxes, proposal_scores, num_proposals, groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list): """Samples a minibatch for second stage. Args: proposal_boxes: A float tensor with shape [batch_size, num_proposals, 4] representing the (potentially zero padded) proposal boxes for all images in the batch. These boxes are represented in absolute coordinates. proposal_scores: A float tensor with shape [batch_size, num_proposals] representing the (potentially zero padded) proposal objectness scores for all images in the batch. num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] representing the number of proposals predicted for each image in the batch. groundtruth_boxlists: A list of BoxLists containing (absolute) coordinates of the groundtruth boxes. groundtruth_classes_with_background_list: A list of 2-D one-hot (or k-hot) tensors of shape [num_boxes, num_classes+1] containing the class targets with the 0th index assumed to map to the background class. groundtruth_weights_list: A list of 1-D tensors of shape [num_boxes] indicating the weight associated with the groundtruth boxes. Returns: proposal_boxes: A float tensor with shape [batch_size, second_stage_batch_size, 4] representing the (potentially zero padded) proposal boxes for all images in the batch. These boxes are represented in absolute coordinates. proposal_scores: A float tensor with shape [batch_size, second_stage_batch_size] representing the (potentially zero padded) proposal objectness scores for all images in the batch. num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] representing the number of proposals predicted for each image in the batch. """ single_image_proposal_box_sample = [] single_image_proposal_score_sample = [] single_image_num_proposals_sample = [] for (single_image_proposal_boxes, single_image_proposal_scores, single_image_num_proposals, single_image_groundtruth_boxlist, single_image_groundtruth_classes_with_background, single_image_groundtruth_weights) in zip( tf.unstack(proposal_boxes), tf.unstack(proposal_scores), tf.unstack(num_proposals), groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list): single_image_boxlist = box_list.BoxList(single_image_proposal_boxes) single_image_boxlist.add_field(fields.BoxListFields.scores, single_image_proposal_scores) sampled_boxlist = self._sample_box_classifier_minibatch_single_image( single_image_boxlist, single_image_num_proposals, single_image_groundtruth_boxlist, single_image_groundtruth_classes_with_background, single_image_groundtruth_weights) sampled_padded_boxlist = box_list_ops.pad_or_clip_box_list( sampled_boxlist, num_boxes=self._second_stage_batch_size) single_image_num_proposals_sample.append(tf.minimum( sampled_boxlist.num_boxes(), self._second_stage_batch_size)) bb = sampled_padded_boxlist.get() single_image_proposal_box_sample.append(bb) single_image_proposal_score_sample.append( sampled_padded_boxlist.get_field(fields.BoxListFields.scores)) return (tf.stack(single_image_proposal_box_sample), tf.stack(single_image_proposal_score_sample), tf.stack(single_image_num_proposals_sample)) def _format_groundtruth_data(self, true_image_shapes): """Helper function for preparing groundtruth data for target assignment. In order to be consistent with the model.DetectionModel interface, groundtruth boxes are specified in normalized coordinates and classes are specified as label indices with no assumed background category. To prepare for target assignment, we: 1) convert boxes to absolute coordinates, 2) add a background class at class index 0 3) groundtruth instance masks, if available, are resized to match image_shape. Args: true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Returns: groundtruth_boxlists: A list of BoxLists containing (absolute) coordinates of the groundtruth boxes. groundtruth_classes_with_background_list: A list of 2-D one-hot (or k-hot) tensors of shape [num_boxes, num_classes+1] containing the class targets with the 0th index assumed to map to the background class. groundtruth_masks_list: If present, a list of 3-D tf.float32 tensors of shape [num_boxes, image_height, image_width] containing instance masks. This is set to None if no masks exist in the provided groundtruth. """ groundtruth_boxlists = [ box_list_ops.to_absolute_coordinates( box_list.BoxList(boxes), true_image_shapes[i, 0], true_image_shapes[i, 1]) for i, boxes in enumerate( self.groundtruth_lists(fields.BoxListFields.boxes)) ] groundtruth_classes_with_background_list = [ tf.to_float( tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')) for one_hot_encoding in self.groundtruth_lists( fields.BoxListFields.classes)] groundtruth_masks_list = self._groundtruth_lists.get( fields.BoxListFields.masks) # TODO(rathodv): Remove mask resizing once the legacy pipeline is deleted. if groundtruth_masks_list is not None and self._resize_masks: resized_masks_list = [] for mask in groundtruth_masks_list: _, resized_mask, _ = self._image_resizer_fn( # Reuse the given `image_resizer_fn` to resize groundtruth masks. # `mask` tensor for an image is of the shape [num_masks, # image_height, image_width]. Below we create a dummy image of the # the shape [image_height, image_width, 1] to use with # `image_resizer_fn`. image=tf.zeros(tf.stack([tf.shape(mask)[1], tf.shape(mask)[2], 1])), masks=mask) resized_masks_list.append(resized_mask) groundtruth_masks_list = resized_masks_list if self.groundtruth_has_field(fields.BoxListFields.weights): groundtruth_weights_list = self.groundtruth_lists( fields.BoxListFields.weights) else: # Set weights for all batch elements equally to 1.0 groundtruth_weights_list = [] for groundtruth_classes in groundtruth_classes_with_background_list: num_gt = tf.shape(groundtruth_classes)[0] groundtruth_weights = tf.ones(num_gt) groundtruth_weights_list.append(groundtruth_weights) return (groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_masks_list, groundtruth_weights_list) def _sample_box_classifier_minibatch_single_image( self, proposal_boxlist, num_valid_proposals, groundtruth_boxlist, groundtruth_classes_with_background, groundtruth_weights): """Samples a mini-batch of proposals to be sent to the box classifier. Helper function for self._postprocess_rpn. Args: proposal_boxlist: A BoxList containing K proposal boxes in absolute coordinates. num_valid_proposals: Number of valid proposals in the proposal boxlist. groundtruth_boxlist: A Boxlist containing N groundtruth object boxes in absolute coordinates. groundtruth_classes_with_background: A tensor with shape `[N, self.num_classes + 1]` representing groundtruth classes. The classes are assumed to be k-hot encoded, and include background as the zero-th class. groundtruth_weights: Weights attached to the groundtruth_boxes. Returns: a BoxList contained sampled proposals. """ (cls_targets, cls_weights, _, _, _) = self._detector_target_assigner.assign( proposal_boxlist, groundtruth_boxlist, groundtruth_classes_with_background, unmatched_class_label=tf.constant( [1] + self._num_classes * [0], dtype=tf.float32), groundtruth_weights=groundtruth_weights) # Selects all boxes as candidates if none of them is selected according # to cls_weights. This could happen as boxes within certain IOU ranges # are ignored. If triggered, the selected boxes will still be ignored # during loss computation. cls_weights = tf.reduce_mean(cls_weights, axis=-1) positive_indicator = tf.greater(tf.argmax(cls_targets, axis=1), 0) valid_indicator = tf.logical_and( tf.range(proposal_boxlist.num_boxes()) < num_valid_proposals, cls_weights > 0 ) selected_positions = self._second_stage_sampler.subsample( valid_indicator, self._second_stage_batch_size, positive_indicator) return box_list_ops.boolean_mask( proposal_boxlist, selected_positions, use_static_shapes=self._use_static_shapes, indicator_sum=(self._second_stage_batch_size if self._use_static_shapes else None)) def _compute_second_stage_input_feature_maps(self, features_to_crop, proposal_boxes_normalized): """Crops to a set of proposals from the feature map for a batch of images. Helper function for self._postprocess_rpn. This function calls `tf.image.crop_and_resize` to create the feature map to be passed to the second stage box classifier for each proposal. Args: features_to_crop: A float32 tensor with shape [batch_size, height, width, depth] proposal_boxes_normalized: A float32 tensor with shape [batch_size, num_proposals, box_code_size] containing proposal boxes in normalized coordinates. Returns: A float32 tensor with shape [K, new_height, new_width, depth]. """ cropped_regions = self._flatten_first_two_dimensions( self._crop_and_resize_fn( features_to_crop, proposal_boxes_normalized, [self._initial_crop_size, self._initial_crop_size])) return slim.max_pool2d( cropped_regions, [self._maxpool_kernel_size, self._maxpool_kernel_size], stride=self._maxpool_stride) def _postprocess_box_classifier(self, refined_box_encodings, class_predictions_with_background, proposal_boxes, num_proposals, image_shapes, mask_predictions=None): """Converts predictions from the second stage box classifier to detections. Args: refined_box_encodings: a 3-D float tensor with shape [total_num_padded_proposals, num_classes, self._box_coder.code_size] representing predicted (final) refined box encodings. If using a shared box across classes the shape will instead be [total_num_padded_proposals, 1, 4] class_predictions_with_background: a 3-D tensor float with shape [total_num_padded_proposals, num_classes + 1] containing class predictions (logits) for each of the proposals. Note that this tensor *includes* background class predictions (at class index 0). proposal_boxes: a 3-D float tensor with shape [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes in absolute coordinates. num_proposals: a 1-D int32 tensor of shape [batch] representing the number of proposals predicted for each image in the batch. image_shapes: a 2-D int32 tensor containing shapes of input image in the batch. mask_predictions: (optional) a 4-D float tensor with shape [total_num_padded_proposals, num_classes, mask_height, mask_width] containing instance mask prediction logits. Returns: A dictionary containing: `detection_boxes`: [batch, max_detection, 4] in normalized co-ordinates. `detection_scores`: [batch, max_detections] `detection_classes`: [batch, max_detections] `num_detections`: [batch] `detection_masks`: (optional) [batch, max_detections, mask_height, mask_width]. Note that a pixel-wise sigmoid score converter is applied to the detection masks. """ refined_box_encodings_batch = tf.reshape( refined_box_encodings, [-1, self.max_num_proposals, refined_box_encodings.shape[1], self._box_coder.code_size]) class_predictions_with_background_batch = tf.reshape( class_predictions_with_background, [-1, self.max_num_proposals, self.num_classes + 1] ) refined_decoded_boxes_batch = self._batch_decode_boxes( refined_box_encodings_batch, proposal_boxes) class_predictions_with_background_batch = ( self._second_stage_score_conversion_fn( class_predictions_with_background_batch)) class_predictions_batch = tf.reshape( tf.slice(class_predictions_with_background_batch, [0, 0, 1], [-1, -1, -1]), [-1, self.max_num_proposals, self.num_classes]) clip_window = self._compute_clip_window(image_shapes) mask_predictions_batch = None if mask_predictions is not None: mask_height = mask_predictions.shape[2].value mask_width = mask_predictions.shape[3].value mask_predictions = tf.sigmoid(mask_predictions) mask_predictions_batch = tf.reshape( mask_predictions, [-1, self.max_num_proposals, self.num_classes, mask_height, mask_width]) (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, _, num_detections) = self._second_stage_nms_fn( refined_decoded_boxes_batch, class_predictions_batch, clip_window=clip_window, change_coordinate_frame=True, num_valid_boxes=num_proposals, masks=mask_predictions_batch) detections = { fields.DetectionResultFields.detection_boxes: nmsed_boxes, fields.DetectionResultFields.detection_scores: nmsed_scores, fields.DetectionResultFields.detection_classes: nmsed_classes, fields.DetectionResultFields.num_detections: tf.to_float(num_detections) } if nmsed_masks is not None: detections[fields.DetectionResultFields.detection_masks] = nmsed_masks return detections def _batch_decode_boxes(self, box_encodings, anchor_boxes): """Decodes box encodings with respect to the anchor boxes. Args: box_encodings: a 4-D tensor with shape [batch_size, num_anchors, num_classes, self._box_coder.code_size] representing box encodings. anchor_boxes: [batch_size, num_anchors, self._box_coder.code_size] representing decoded bounding boxes. If using a shared box across classes the shape will instead be [total_num_proposals, 1, self._box_coder.code_size]. Returns: decoded_boxes: a [batch_size, num_anchors, num_classes, self._box_coder.code_size] float tensor representing bounding box predictions (for each image in batch, proposal and class). If using a shared box across classes the shape will instead be [batch_size, num_anchors, 1, self._box_coder.code_size]. """ combined_shape = shape_utils.combined_static_and_dynamic_shape( box_encodings) num_classes = combined_shape[2] tiled_anchor_boxes = tf.tile( tf.expand_dims(anchor_boxes, 2), [1, 1, num_classes, 1]) tiled_anchors_boxlist = box_list.BoxList( tf.reshape(tiled_anchor_boxes, [-1, 4])) decoded_boxes = self._box_coder.decode( tf.reshape(box_encodings, [-1, self._box_coder.code_size]), tiled_anchors_boxlist) return tf.reshape(decoded_boxes.get(), tf.stack([combined_shape[0], combined_shape[1], num_classes, 4])) def loss(self, prediction_dict, true_image_shapes, scope=None): """Compute scalar loss tensors given prediction tensors. If number_of_stages=1, only RPN related losses are computed (i.e., `rpn_localization_loss` and `rpn_objectness_loss`). Otherwise all losses are computed. Args: prediction_dict: a dictionary holding prediction tensors (see the documentation for the predict method. If number_of_stages=1, we expect prediction_dict to contain `rpn_box_encodings`, `rpn_objectness_predictions_with_background`, `rpn_features_to_crop`, `image_shape`, and `anchors` fields. Otherwise we expect prediction_dict to additionally contain `refined_box_encodings`, `class_predictions_with_background`, `num_proposals`, and `proposal_boxes` fields. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. scope: Optional scope name. Returns: a dictionary mapping loss keys (`first_stage_localization_loss`, `first_stage_objectness_loss`, 'second_stage_localization_loss', 'second_stage_classification_loss') to scalar tensors representing corresponding loss values. """ with tf.name_scope(scope, 'Loss', prediction_dict.values()): (groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_masks_list, groundtruth_weights_list ) = self._format_groundtruth_data(true_image_shapes) loss_dict = self._loss_rpn( prediction_dict['rpn_box_encodings'], prediction_dict['rpn_objectness_predictions_with_background'], prediction_dict['anchors'], groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list) if self._number_of_stages > 1: loss_dict.update( self._loss_box_classifier( prediction_dict['refined_box_encodings'], prediction_dict['class_predictions_with_background'], prediction_dict['proposal_boxes'], prediction_dict['num_proposals'], groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list, prediction_dict['image_shape'], prediction_dict.get('mask_predictions'), groundtruth_masks_list, prediction_dict.get( fields.DetectionResultFields.detection_boxes), prediction_dict.get( fields.DetectionResultFields.num_detections))) return loss_dict def _loss_rpn(self, rpn_box_encodings, rpn_objectness_predictions_with_background, anchors, groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list): """Computes scalar RPN loss tensors. Uses self._proposal_target_assigner to obtain regression and classification targets for the first stage RPN, samples a "minibatch" of anchors to participate in the loss computation, and returns the RPN losses. Args: rpn_box_encodings: A 4-D float tensor of shape [batch_size, num_anchors, self._box_coder.code_size] containing predicted proposal box encodings. rpn_objectness_predictions_with_background: A 2-D float tensor of shape [batch_size, num_anchors, 2] containing objectness predictions (logits) for each of the anchors with 0 corresponding to background and 1 corresponding to object. anchors: A 2-D tensor of shape [num_anchors, 4] representing anchors for the first stage RPN. Note that `num_anchors` can differ depending on whether the model is created in training or inference mode. groundtruth_boxlists: A list of BoxLists containing coordinates of the groundtruth boxes. groundtruth_classes_with_background_list: A list of 2-D one-hot (or k-hot) tensors of shape [num_boxes, num_classes+1] containing the class targets with the 0th index assumed to map to the background class. groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape [num_boxes] containing weights for groundtruth boxes. Returns: a dictionary mapping loss keys (`first_stage_localization_loss`, `first_stage_objectness_loss`) to scalar tensors representing corresponding loss values. """ with tf.name_scope('RPNLoss'): (batch_cls_targets, batch_cls_weights, batch_reg_targets, batch_reg_weights, _) = target_assigner.batch_assign_targets( target_assigner=self._proposal_target_assigner, anchors_batch=box_list.BoxList(anchors), gt_box_batch=groundtruth_boxlists, gt_class_targets_batch=(len(groundtruth_boxlists) * [None]), gt_weights_batch=groundtruth_weights_list) batch_cls_weights = tf.reduce_mean(batch_cls_weights, axis=2) batch_cls_targets = tf.squeeze(batch_cls_targets, axis=2) def _minibatch_subsample_fn(inputs): cls_targets, cls_weights = inputs return self._first_stage_sampler.subsample( tf.cast(cls_weights, tf.bool), self._first_stage_minibatch_size, tf.cast(cls_targets, tf.bool)) batch_sampled_indices = tf.to_float(shape_utils.static_or_dynamic_map_fn( _minibatch_subsample_fn, [batch_cls_targets, batch_cls_weights], dtype=tf.bool, parallel_iterations=self._parallel_iterations, back_prop=True)) # Normalize by number of examples in sampled minibatch normalizer = tf.reduce_sum(batch_sampled_indices, axis=1) batch_one_hot_targets = tf.one_hot( tf.to_int32(batch_cls_targets), depth=2) sampled_reg_indices = tf.multiply(batch_sampled_indices, batch_reg_weights) losses_mask = None if self.groundtruth_has_field(fields.InputDataFields.is_annotated): losses_mask = tf.stack(self.groundtruth_lists( fields.InputDataFields.is_annotated)) localization_losses = self._first_stage_localization_loss( rpn_box_encodings, batch_reg_targets, weights=sampled_reg_indices, losses_mask=losses_mask) objectness_losses = self._first_stage_objectness_loss( rpn_objectness_predictions_with_background, batch_one_hot_targets, weights=tf.expand_dims(batch_sampled_indices, axis=-1), losses_mask=losses_mask) localization_loss = tf.reduce_mean( tf.reduce_sum(localization_losses, axis=1) / normalizer) objectness_loss = tf.reduce_mean( tf.reduce_sum(objectness_losses, axis=1) / normalizer) localization_loss = tf.multiply(self._first_stage_loc_loss_weight, localization_loss, name='localization_loss') objectness_loss = tf.multiply(self._first_stage_obj_loss_weight, objectness_loss, name='objectness_loss') loss_dict = {localization_loss.op.name: localization_loss, objectness_loss.op.name: objectness_loss} return loss_dict def _loss_box_classifier(self, refined_box_encodings, class_predictions_with_background, proposal_boxes, num_proposals, groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list, image_shape, prediction_masks=None, groundtruth_masks_list=None, detection_boxes=None, num_detections=None): """Computes scalar box classifier loss tensors. Uses self._detector_target_assigner to obtain regression and classification targets for the second stage box classifier, optionally performs hard mining, and returns losses. All losses are computed independently for each image and then averaged across the batch. Please note that for boxes and masks with multiple labels, the box regression and mask prediction losses are only computed for one label. This function assumes that the proposal boxes in the "padded" regions are actually zero (and thus should not be matched to). Args: refined_box_encodings: a 3-D tensor with shape [total_num_proposals, num_classes, box_coder.code_size] representing predicted (final) refined box encodings. If using a shared box across classes this will instead have shape [total_num_proposals, 1, box_coder.code_size]. class_predictions_with_background: a 2-D tensor with shape [total_num_proposals, num_classes + 1] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions (at class index 0). proposal_boxes: [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes. num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] representing the number of proposals predicted for each image in the batch. groundtruth_boxlists: a list of BoxLists containing coordinates of the groundtruth boxes. groundtruth_classes_with_background_list: a list of 2-D one-hot (or k-hot) tensors of shape [num_boxes, num_classes + 1] containing the class targets with the 0th index assumed to map to the background class. groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape [num_boxes] containing weights for groundtruth boxes. image_shape: a 1-D tensor of shape [4] representing the image shape. prediction_masks: an optional 4-D tensor with shape [total_num_proposals, num_classes, mask_height, mask_width] containing the instance masks for each box. groundtruth_masks_list: an optional list of 3-D tensors of shape [num_boxes, image_height, image_width] containing the instance masks for each of the boxes. detection_boxes: 3-D float tensor of shape [batch, max_total_detections, 4] containing post-processed detection boxes in normalized co-ordinates. num_detections: 1-D int32 tensor of shape [batch] containing number of valid detections in `detection_boxes`. Returns: a dictionary mapping loss keys ('second_stage_localization_loss', 'second_stage_classification_loss') to scalar tensors representing corresponding loss values. Raises: ValueError: if `predict_instance_masks` in second_stage_mask_rcnn_box_predictor is True and `groundtruth_masks_list` is not provided. """ with tf.name_scope('BoxClassifierLoss'): paddings_indicator = self._padded_batched_proposals_indicator( num_proposals, proposal_boxes.shape[1]) proposal_boxlists = [ box_list.BoxList(proposal_boxes_single_image) for proposal_boxes_single_image in tf.unstack(proposal_boxes)] batch_size = len(proposal_boxlists) num_proposals_or_one = tf.to_float(tf.expand_dims( tf.maximum(num_proposals, tf.ones_like(num_proposals)), 1)) normalizer = tf.tile(num_proposals_or_one, [1, self.max_num_proposals]) * batch_size (batch_cls_targets_with_background, batch_cls_weights, batch_reg_targets, batch_reg_weights, _) = target_assigner.batch_assign_targets( target_assigner=self._detector_target_assigner, anchors_batch=proposal_boxlists, gt_box_batch=groundtruth_boxlists, gt_class_targets_batch=groundtruth_classes_with_background_list, unmatched_class_label=tf.constant( [1] + self._num_classes * [0], dtype=tf.float32), gt_weights_batch=groundtruth_weights_list) class_predictions_with_background = tf.reshape( class_predictions_with_background, [batch_size, self.max_num_proposals, -1]) flat_cls_targets_with_background = tf.reshape( batch_cls_targets_with_background, [batch_size * self.max_num_proposals, -1]) one_hot_flat_cls_targets_with_background = tf.argmax( flat_cls_targets_with_background, axis=1) one_hot_flat_cls_targets_with_background = tf.one_hot( one_hot_flat_cls_targets_with_background, flat_cls_targets_with_background.get_shape()[1]) # If using a shared box across classes use directly if refined_box_encodings.shape[1] == 1: reshaped_refined_box_encodings = tf.reshape( refined_box_encodings, [batch_size, self.max_num_proposals, self._box_coder.code_size]) # For anchors with multiple labels, picks refined_location_encodings # for just one class to avoid over-counting for regression loss and # (optionally) mask loss. else: reshaped_refined_box_encodings = ( self._get_refined_encodings_for_postitive_class( refined_box_encodings, one_hot_flat_cls_targets_with_background, batch_size)) losses_mask = None if self.groundtruth_has_field(fields.InputDataFields.is_annotated): losses_mask = tf.stack(self.groundtruth_lists( fields.InputDataFields.is_annotated)) second_stage_loc_losses = self._second_stage_localization_loss( reshaped_refined_box_encodings, batch_reg_targets, weights=batch_reg_weights, losses_mask=losses_mask) / normalizer second_stage_cls_losses = ops.reduce_sum_trailing_dimensions( self._second_stage_classification_loss( class_predictions_with_background, batch_cls_targets_with_background, weights=batch_cls_weights, losses_mask=losses_mask), ndims=2) / normalizer second_stage_loc_loss = tf.reduce_sum( second_stage_loc_losses * tf.to_float(paddings_indicator)) second_stage_cls_loss = tf.reduce_sum( second_stage_cls_losses * tf.to_float(paddings_indicator)) if self._hard_example_miner: (second_stage_loc_loss, second_stage_cls_loss ) = self._unpad_proposals_and_apply_hard_mining( proposal_boxlists, second_stage_loc_losses, second_stage_cls_losses, num_proposals) localization_loss = tf.multiply(self._second_stage_loc_loss_weight, second_stage_loc_loss, name='localization_loss') classification_loss = tf.multiply(self._second_stage_cls_loss_weight, second_stage_cls_loss, name='classification_loss') loss_dict = {localization_loss.op.name: localization_loss, classification_loss.op.name: classification_loss} second_stage_mask_loss = None if prediction_masks is not None: if groundtruth_masks_list is None: raise ValueError('Groundtruth instance masks not provided. ' 'Please configure input reader.') if not self._is_training: (proposal_boxes, proposal_boxlists, paddings_indicator, one_hot_flat_cls_targets_with_background ) = self._get_mask_proposal_boxes_and_classes( detection_boxes, num_detections, image_shape, groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list) unmatched_mask_label = tf.zeros(image_shape[1:3], dtype=tf.float32) (batch_mask_targets, _, _, batch_mask_target_weights, _) = target_assigner.batch_assign_targets( target_assigner=self._detector_target_assigner, anchors_batch=proposal_boxlists, gt_box_batch=groundtruth_boxlists, gt_class_targets_batch=groundtruth_masks_list, unmatched_class_label=unmatched_mask_label, gt_weights_batch=groundtruth_weights_list) # Pad the prediction_masks with to add zeros for background class to be # consistent with class predictions. if prediction_masks.get_shape().as_list()[1] == 1: # Class agnostic masks or masks for one-class prediction. Logic for # both cases is the same since background predictions are ignored # through the batch_mask_target_weights. prediction_masks_masked_by_class_targets = prediction_masks else: prediction_masks_with_background = tf.pad( prediction_masks, [[0, 0], [1, 0], [0, 0], [0, 0]]) prediction_masks_masked_by_class_targets = tf.boolean_mask( prediction_masks_with_background, tf.greater(one_hot_flat_cls_targets_with_background, 0)) mask_height = prediction_masks.shape[2].value mask_width = prediction_masks.shape[3].value reshaped_prediction_masks = tf.reshape( prediction_masks_masked_by_class_targets, [batch_size, -1, mask_height * mask_width]) batch_mask_targets_shape = tf.shape(batch_mask_targets) flat_gt_masks = tf.reshape(batch_mask_targets, [-1, batch_mask_targets_shape[2], batch_mask_targets_shape[3]]) # Use normalized proposals to crop mask targets from image masks. flat_normalized_proposals = box_list_ops.to_normalized_coordinates( box_list.BoxList(tf.reshape(proposal_boxes, [-1, 4])), image_shape[1], image_shape[2]).get() flat_cropped_gt_mask = self._crop_and_resize_fn( tf.expand_dims(flat_gt_masks, -1), tf.expand_dims(flat_normalized_proposals, axis=1), [mask_height, mask_width]) # Without stopping gradients into cropped groundtruth masks the # performance with 100-padded groundtruth masks when batch size > 1 is # about 4% worse. # TODO(rathodv): Investigate this since we don't expect any variables # upstream of flat_cropped_gt_mask. flat_cropped_gt_mask = tf.stop_gradient(flat_cropped_gt_mask) batch_cropped_gt_mask = tf.reshape( flat_cropped_gt_mask, [batch_size, -1, mask_height * mask_width]) mask_losses_weights = ( batch_mask_target_weights * tf.to_float(paddings_indicator)) mask_losses = self._second_stage_mask_loss( reshaped_prediction_masks, batch_cropped_gt_mask, weights=tf.expand_dims(mask_losses_weights, axis=-1), losses_mask=losses_mask) total_mask_loss = tf.reduce_sum(mask_losses) normalizer = tf.maximum( tf.reduce_sum(mask_losses_weights * mask_height * mask_width), 1.0) second_stage_mask_loss = total_mask_loss / normalizer if second_stage_mask_loss is not None: mask_loss = tf.multiply(self._second_stage_mask_loss_weight, second_stage_mask_loss, name='mask_loss') loss_dict[mask_loss.op.name] = mask_loss return loss_dict def _get_mask_proposal_boxes_and_classes( self, detection_boxes, num_detections, image_shape, groundtruth_boxlists, groundtruth_classes_with_background_list, groundtruth_weights_list): """Returns proposal boxes and class targets to compute evaluation mask loss. During evaluation, detection boxes are used to extract features for mask prediction. Therefore, to compute mask loss during evaluation detection boxes must be used to compute correct class and mask targets. This function returns boxes and classes in the correct format for computing mask targets during evaluation. Args: detection_boxes: A 3-D float tensor of shape [batch, max_detection_boxes, 4] containing detection boxes in normalized co-ordinates. num_detections: A 1-D float tensor of shape [batch] containing number of valid boxes in `detection_boxes`. image_shape: A 1-D tensor of shape [4] containing image tensor shape. groundtruth_boxlists: A list of groundtruth boxlists. groundtruth_classes_with_background_list: A list of groundtruth classes. groundtruth_weights_list: A list of groundtruth weights. Return: mask_proposal_boxes: detection boxes to use for mask proposals in absolute co-ordinates. mask_proposal_boxlists: `mask_proposal_boxes` in a list of BoxLists in absolute co-ordinates. mask_proposal_paddings_indicator: a tensor indicating valid boxes. mask_proposal_one_hot_flat_cls_targets_with_background: Class targets computed using detection boxes. """ batch, max_num_detections, _ = detection_boxes.shape.as_list() proposal_boxes = tf.reshape(box_list_ops.to_absolute_coordinates( box_list.BoxList(tf.reshape(detection_boxes, [-1, 4])), image_shape[1], image_shape[2]).get(), [batch, max_num_detections, 4]) proposal_boxlists = [ box_list.BoxList(detection_boxes_single_image) for detection_boxes_single_image in tf.unstack(proposal_boxes) ] paddings_indicator = self._padded_batched_proposals_indicator( tf.to_int32(num_detections), detection_boxes.shape[1]) (batch_cls_targets_with_background, _, _, _, _) = target_assigner.batch_assign_targets( target_assigner=self._detector_target_assigner, anchors_batch=proposal_boxlists, gt_box_batch=groundtruth_boxlists, gt_class_targets_batch=groundtruth_classes_with_background_list, unmatched_class_label=tf.constant( [1] + self._num_classes * [0], dtype=tf.float32), gt_weights_batch=groundtruth_weights_list) flat_cls_targets_with_background = tf.reshape( batch_cls_targets_with_background, [-1, self._num_classes + 1]) one_hot_flat_cls_targets_with_background = tf.argmax( flat_cls_targets_with_background, axis=1) one_hot_flat_cls_targets_with_background = tf.one_hot( one_hot_flat_cls_targets_with_background, flat_cls_targets_with_background.get_shape()[1]) return (proposal_boxes, proposal_boxlists, paddings_indicator, one_hot_flat_cls_targets_with_background) def _get_refined_encodings_for_postitive_class( self, refined_box_encodings, flat_cls_targets_with_background, batch_size): # We only predict refined location encodings for the non background # classes, but we now pad it to make it compatible with the class # predictions refined_box_encodings_with_background = tf.pad(refined_box_encodings, [[0, 0], [1, 0], [0, 0]]) refined_box_encodings_masked_by_class_targets = ( box_list_ops.boolean_mask( box_list.BoxList( tf.reshape(refined_box_encodings_with_background, [-1, self._box_coder.code_size])), tf.reshape(tf.greater(flat_cls_targets_with_background, 0), [-1]), use_static_shapes=self._use_static_shapes, indicator_sum=batch_size * self.max_num_proposals if self._use_static_shapes else None).get()) return tf.reshape( refined_box_encodings_masked_by_class_targets, [ batch_size, self.max_num_proposals, self._box_coder.code_size ]) def _padded_batched_proposals_indicator(self, num_proposals, max_num_proposals): """Creates indicator matrix of non-pad elements of padded batch proposals. Args: num_proposals: Tensor of type tf.int32 with shape [batch_size]. max_num_proposals: Maximum number of proposals per image (integer). Returns: A Tensor of type tf.bool with shape [batch_size, max_num_proposals]. """ batch_size = tf.size(num_proposals) tiled_num_proposals = tf.tile( tf.expand_dims(num_proposals, 1), [1, max_num_proposals]) tiled_proposal_index = tf.tile( tf.expand_dims(tf.range(max_num_proposals), 0), [batch_size, 1]) return tf.greater(tiled_num_proposals, tiled_proposal_index) def _unpad_proposals_and_apply_hard_mining(self, proposal_boxlists, second_stage_loc_losses, second_stage_cls_losses, num_proposals): """Unpads proposals and applies hard mining. Args: proposal_boxlists: A list of `batch_size` BoxLists each representing `self.max_num_proposals` representing decoded proposal bounding boxes for each image. second_stage_loc_losses: A Tensor of type `float32`. A tensor of shape `[batch_size, self.max_num_proposals]` representing per-anchor second stage localization loss values. second_stage_cls_losses: A Tensor of type `float32`. A tensor of shape `[batch_size, self.max_num_proposals]` representing per-anchor second stage classification loss values. num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] representing the number of proposals predicted for each image in the batch. Returns: second_stage_loc_loss: A scalar float32 tensor representing the second stage localization loss. second_stage_cls_loss: A scalar float32 tensor representing the second stage classification loss. """ for (proposal_boxlist, single_image_loc_loss, single_image_cls_loss, single_image_num_proposals) in zip( proposal_boxlists, tf.unstack(second_stage_loc_losses), tf.unstack(second_stage_cls_losses), tf.unstack(num_proposals)): proposal_boxlist = box_list.BoxList( tf.slice(proposal_boxlist.get(), [0, 0], [single_image_num_proposals, -1])) single_image_loc_loss = tf.slice(single_image_loc_loss, [0], [single_image_num_proposals]) single_image_cls_loss = tf.slice(single_image_cls_loss, [0], [single_image_num_proposals]) return self._hard_example_miner( location_losses=tf.expand_dims(single_image_loc_loss, 0), cls_losses=tf.expand_dims(single_image_cls_loss, 0), decoded_boxlist_list=[proposal_boxlist]) def regularization_losses(self): """Returns a list of regularization losses for this model. Returns a list of regularization losses for this model that the estimator needs to use during training/optimization. Returns: A list of regularization loss tensors. """ return tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) def restore_map(self, fine_tune_checkpoint_type='detection', load_all_detection_checkpoint_vars=False): """Returns a map of variables to load from a foreign checkpoint. See parent class for details. Args: fine_tune_checkpoint_type: whether to restore from a full detection checkpoint (with compatible variable names) or to restore from a classification checkpoint for initialization prior to training. Valid values: `detection`, `classification`. Default 'detection'. load_all_detection_checkpoint_vars: whether to load all variables (when `fine_tune_checkpoint_type` is `detection`). If False, only variables within the feature extractor scopes are included. Default False. Returns: A dict mapping variable names (to load from a checkpoint) to variables in the model graph. Raises: ValueError: if fine_tune_checkpoint_type is neither `classification` nor `detection`. """ if fine_tune_checkpoint_type not in ['detection', 'classification']: raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format( fine_tune_checkpoint_type)) if fine_tune_checkpoint_type == 'classification': return self._feature_extractor.restore_from_classification_checkpoint_fn( self.first_stage_feature_extractor_scope, self.second_stage_feature_extractor_scope) variables_to_restore = tf.global_variables() variables_to_restore.append(slim.get_or_create_global_step()) # Only load feature extractor variables to be consistent with loading from # a classification checkpoint. include_patterns = None if not load_all_detection_checkpoint_vars: include_patterns = [ self.first_stage_feature_extractor_scope, self.second_stage_feature_extractor_scope ] feature_extractor_variables = tf.contrib.framework.filter_variables( variables_to_restore, include_patterns=include_patterns) return {var.op.name: var for var in feature_extractor_variables} def updates(self): """Returns a list of update operators for this model. Returns a list of update operators for this model that must be executed at each training step. The estimator's train op needs to have a control dependency on these updates. Returns: A list of update operators. """ return tf.get_collection(tf.GraphKeys.UPDATE_OPS)
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.meta_architectures.rfcn_meta_arch.""" import tensorflow as tf from object_detection.meta_architectures import faster_rcnn_meta_arch_test_lib from object_detection.meta_architectures import rfcn_meta_arch class RFCNMetaArchTest( faster_rcnn_meta_arch_test_lib.FasterRCNNMetaArchTestBase): def _get_second_stage_box_predictor_text_proto(self): box_predictor_text_proto = """ rfcn_box_predictor { conv_hyperparams { op: CONV activation: NONE regularizer { l2_regularizer { weight: 0.0005 } } initializer { variance_scaling_initializer { factor: 1.0 uniform: true mode: FAN_AVG } } } } """ return box_predictor_text_proto def _get_model(self, box_predictor, **common_kwargs): return rfcn_meta_arch.RFCNMetaArch( second_stage_rfcn_box_predictor=box_predictor, **common_kwargs) def _get_box_classifier_features_shape(self, image_size, batch_size, max_num_proposals, initial_crop_size, maxpool_stride, num_features): return (batch_size, image_size, image_size, num_features) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/rfcn_meta_arch_test.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Helper functions for SSD models meta architecture tests.""" import functools import tensorflow as tf from object_detection.core import anchor_generator from object_detection.core import balanced_positive_negative_sampler as sampler from object_detection.core import box_list from object_detection.core import losses from object_detection.core import post_processing from object_detection.core import region_similarity_calculator as sim_calc from object_detection.core import target_assigner from object_detection.meta_architectures import ssd_meta_arch from object_detection.protos import model_pb2 from object_detection.utils import ops from object_detection.utils import test_case from object_detection.utils import test_utils slim = tf.contrib.slim keras = tf.keras.layers class FakeSSDFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): """Fake ssd feature extracture for ssd meta arch tests.""" def __init__(self): super(FakeSSDFeatureExtractor, self).__init__( is_training=True, depth_multiplier=0, min_depth=0, pad_to_multiple=1, conv_hyperparams_fn=None) def preprocess(self, resized_inputs): return tf.identity(resized_inputs) def extract_features(self, preprocessed_inputs): with tf.variable_scope('mock_model'): features = slim.conv2d( inputs=preprocessed_inputs, num_outputs=32, kernel_size=1, scope='layer1') return [features] class FakeSSDKerasFeatureExtractor(ssd_meta_arch.SSDKerasFeatureExtractor): """Fake keras based ssd feature extracture for ssd meta arch tests.""" def __init__(self): with tf.name_scope('mock_model'): super(FakeSSDKerasFeatureExtractor, self).__init__( is_training=True, depth_multiplier=0, min_depth=0, pad_to_multiple=1, conv_hyperparams=None, freeze_batchnorm=False, inplace_batchnorm_update=False, ) self._conv = keras.Conv2D(filters=32, kernel_size=1, name='layer1') def preprocess(self, resized_inputs): return tf.identity(resized_inputs) def _extract_features(self, preprocessed_inputs, **kwargs): with tf.name_scope('mock_model'): return [self._conv(preprocessed_inputs)] class MockAnchorGenerator2x2(anchor_generator.AnchorGenerator): """A simple 2x2 anchor grid on the unit square used for test only.""" def name_scope(self): return 'MockAnchorGenerator' def num_anchors_per_location(self): return [1] def _generate(self, feature_map_shape_list, im_height, im_width): return [ box_list.BoxList( tf.constant( [ [0, 0, .5, .5], [0, .5, .5, 1], [.5, 0, 1, .5], [1., 1., 1.5, 1.5] # Anchor that is outside clip_window. ], tf.float32)) ] def num_anchors(self): return 4 class SSDMetaArchTestBase(test_case.TestCase): """Base class to test SSD based meta architectures.""" def _create_model( self, model_fn=ssd_meta_arch.SSDMetaArch, apply_hard_mining=True, normalize_loc_loss_by_codesize=False, add_background_class=True, random_example_sampling=False, expected_loss_weights=model_pb2.DetectionModel().ssd.loss.NONE, min_num_negative_samples=1, desired_negative_sampling_ratio=3, use_keras=False, predict_mask=False, use_static_shapes=False, nms_max_size_per_class=5): is_training = False num_classes = 1 mock_anchor_generator = MockAnchorGenerator2x2() if use_keras: mock_box_predictor = test_utils.MockKerasBoxPredictor( is_training, num_classes, add_background_class=add_background_class) else: mock_box_predictor = test_utils.MockBoxPredictor( is_training, num_classes, add_background_class=add_background_class) mock_box_coder = test_utils.MockBoxCoder() if use_keras: fake_feature_extractor = FakeSSDKerasFeatureExtractor() else: fake_feature_extractor = FakeSSDFeatureExtractor() mock_matcher = test_utils.MockMatcher() region_similarity_calculator = sim_calc.IouSimilarity() encode_background_as_zeros = False def image_resizer_fn(image): return [tf.identity(image), tf.shape(image)] classification_loss = losses.WeightedSigmoidClassificationLoss() localization_loss = losses.WeightedSmoothL1LocalizationLoss() non_max_suppression_fn = functools.partial( post_processing.batch_multiclass_non_max_suppression, score_thresh=-20.0, iou_thresh=1.0, max_size_per_class=nms_max_size_per_class, max_total_size=nms_max_size_per_class, use_static_shapes=use_static_shapes) classification_loss_weight = 1.0 localization_loss_weight = 1.0 negative_class_weight = 1.0 normalize_loss_by_num_matches = False hard_example_miner = None if apply_hard_mining: # This hard example miner is expected to be a no-op. hard_example_miner = losses.HardExampleMiner( num_hard_examples=None, iou_threshold=1.0) random_example_sampler = None if random_example_sampling: random_example_sampler = sampler.BalancedPositiveNegativeSampler( positive_fraction=0.5) target_assigner_instance = target_assigner.TargetAssigner( region_similarity_calculator, mock_matcher, mock_box_coder, negative_class_weight=negative_class_weight) model_config = model_pb2.DetectionModel() if expected_loss_weights == model_config.ssd.loss.NONE: expected_loss_weights_fn = None else: raise ValueError('Not a valid value for expected_loss_weights.') code_size = 4 kwargs = {} if predict_mask: kwargs.update({ 'mask_prediction_fn': test_utils.MockMaskHead(num_classes=1).predict, }) model = model_fn( is_training=is_training, anchor_generator=mock_anchor_generator, box_predictor=mock_box_predictor, box_coder=mock_box_coder, feature_extractor=fake_feature_extractor, encode_background_as_zeros=encode_background_as_zeros, image_resizer_fn=image_resizer_fn, non_max_suppression_fn=non_max_suppression_fn, score_conversion_fn=tf.identity, classification_loss=classification_loss, localization_loss=localization_loss, classification_loss_weight=classification_loss_weight, localization_loss_weight=localization_loss_weight, normalize_loss_by_num_matches=normalize_loss_by_num_matches, hard_example_miner=hard_example_miner, target_assigner_instance=target_assigner_instance, add_summaries=False, normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize, freeze_batchnorm=False, inplace_batchnorm_update=False, add_background_class=add_background_class, random_example_sampler=random_example_sampler, expected_loss_weights_fn=expected_loss_weights_fn, **kwargs) return model, num_classes, mock_anchor_generator.num_anchors(), code_size def _get_value_for_matching_key(self, dictionary, suffix): for key in dictionary.keys(): if key.endswith(suffix): return dictionary[key] raise ValueError('key not found {}'.format(suffix)) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/ssd_meta_arch_test_lib.py
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/__init__.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.meta_architectures.faster_rcnn_meta_arch.""" import functools from absl.testing import parameterized import numpy as np import tensorflow as tf from google.protobuf import text_format from object_detection.anchor_generators import grid_anchor_generator from object_detection.builders import box_predictor_builder from object_detection.builders import hyperparams_builder from object_detection.builders import post_processing_builder from object_detection.core import balanced_positive_negative_sampler as sampler from object_detection.core import losses from object_detection.core import post_processing from object_detection.core import target_assigner from object_detection.meta_architectures import faster_rcnn_meta_arch from object_detection.protos import box_predictor_pb2 from object_detection.protos import hyperparams_pb2 from object_detection.protos import post_processing_pb2 from object_detection.utils import ops from object_detection.utils import test_case from object_detection.utils import test_utils slim = tf.contrib.slim BOX_CODE_SIZE = 4 class FakeFasterRCNNFeatureExtractor( faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): """Fake feature extracture to use in tests.""" def __init__(self): super(FakeFasterRCNNFeatureExtractor, self).__init__( is_training=False, first_stage_features_stride=32, reuse_weights=None, weight_decay=0.0) def preprocess(self, resized_inputs): return tf.identity(resized_inputs) def _extract_proposal_features(self, preprocessed_inputs, scope): with tf.variable_scope('mock_model'): proposal_features = 0 * slim.conv2d( preprocessed_inputs, num_outputs=3, kernel_size=1, scope='layer1') return proposal_features, {} def _extract_box_classifier_features(self, proposal_feature_maps, scope): with tf.variable_scope('mock_model'): return 0 * slim.conv2d(proposal_feature_maps, num_outputs=3, kernel_size=1, scope='layer2') class FasterRCNNMetaArchTestBase(test_case.TestCase, parameterized.TestCase): """Base class to test Faster R-CNN and R-FCN meta architectures.""" def _build_arg_scope_with_hyperparams(self, hyperparams_text_proto, is_training): hyperparams = hyperparams_pb2.Hyperparams() text_format.Merge(hyperparams_text_proto, hyperparams) return hyperparams_builder.build(hyperparams, is_training=is_training) def _get_second_stage_box_predictor_text_proto(self): box_predictor_text_proto = """ mask_rcnn_box_predictor { fc_hyperparams { op: FC activation: NONE regularizer { l2_regularizer { weight: 0.0005 } } initializer { variance_scaling_initializer { factor: 1.0 uniform: true mode: FAN_AVG } } } } """ return box_predictor_text_proto def _add_mask_to_second_stage_box_predictor_text_proto( self, masks_are_class_agnostic=False): agnostic = 'true' if masks_are_class_agnostic else 'false' box_predictor_text_proto = """ mask_rcnn_box_predictor { predict_instance_masks: true masks_are_class_agnostic: """ + agnostic + """ mask_height: 14 mask_width: 14 conv_hyperparams { op: CONV regularizer { l2_regularizer { weight: 0.0 } } initializer { truncated_normal_initializer { stddev: 0.01 } } } } """ return box_predictor_text_proto def _get_second_stage_box_predictor(self, num_classes, is_training, predict_masks, masks_are_class_agnostic): box_predictor_proto = box_predictor_pb2.BoxPredictor() text_format.Merge(self._get_second_stage_box_predictor_text_proto(), box_predictor_proto) if predict_masks: text_format.Merge( self._add_mask_to_second_stage_box_predictor_text_proto( masks_are_class_agnostic), box_predictor_proto) return box_predictor_builder.build( hyperparams_builder.build, box_predictor_proto, num_classes=num_classes, is_training=is_training) def _get_model(self, box_predictor, **common_kwargs): return faster_rcnn_meta_arch.FasterRCNNMetaArch( initial_crop_size=3, maxpool_kernel_size=1, maxpool_stride=1, second_stage_mask_rcnn_box_predictor=box_predictor, **common_kwargs) def _build_model(self, is_training, number_of_stages, second_stage_batch_size, first_stage_max_proposals=8, num_classes=2, hard_mining=False, softmax_second_stage_classification_loss=True, predict_masks=False, pad_to_max_dimension=None, masks_are_class_agnostic=False, use_matmul_crop_and_resize=False, clip_anchors_to_image=False, use_matmul_gather_in_matcher=False, use_static_shapes=False): def image_resizer_fn(image, masks=None): """Fake image resizer function.""" resized_inputs = [] resized_image = tf.identity(image) if pad_to_max_dimension is not None: resized_image = tf.image.pad_to_bounding_box(image, 0, 0, pad_to_max_dimension, pad_to_max_dimension) resized_inputs.append(resized_image) if masks is not None: resized_masks = tf.identity(masks) if pad_to_max_dimension is not None: resized_masks = tf.image.pad_to_bounding_box(tf.transpose(masks, [1, 2, 0]), 0, 0, pad_to_max_dimension, pad_to_max_dimension) resized_masks = tf.transpose(resized_masks, [2, 0, 1]) resized_inputs.append(resized_masks) resized_inputs.append(tf.shape(image)) return resized_inputs # anchors in this test are designed so that a subset of anchors are inside # the image and a subset of anchors are outside. first_stage_anchor_scales = (0.001, 0.005, 0.1) first_stage_anchor_aspect_ratios = (0.5, 1.0, 2.0) first_stage_anchor_strides = (1, 1) first_stage_anchor_generator = grid_anchor_generator.GridAnchorGenerator( first_stage_anchor_scales, first_stage_anchor_aspect_ratios, anchor_stride=first_stage_anchor_strides) first_stage_target_assigner = target_assigner.create_target_assigner( 'FasterRCNN', 'proposal', use_matmul_gather=use_matmul_gather_in_matcher) fake_feature_extractor = FakeFasterRCNNFeatureExtractor() first_stage_box_predictor_hyperparams_text_proto = """ op: CONV activation: RELU regularizer { l2_regularizer { weight: 0.00004 } } initializer { truncated_normal_initializer { stddev: 0.03 } } """ first_stage_box_predictor_arg_scope_fn = ( self._build_arg_scope_with_hyperparams( first_stage_box_predictor_hyperparams_text_proto, is_training)) first_stage_box_predictor_kernel_size = 3 first_stage_atrous_rate = 1 first_stage_box_predictor_depth = 512 first_stage_minibatch_size = 3 first_stage_sampler = sampler.BalancedPositiveNegativeSampler( positive_fraction=0.5, is_static=use_static_shapes) first_stage_nms_score_threshold = -1.0 first_stage_nms_iou_threshold = 1.0 first_stage_max_proposals = first_stage_max_proposals first_stage_non_max_suppression_fn = functools.partial( post_processing.batch_multiclass_non_max_suppression, score_thresh=first_stage_nms_score_threshold, iou_thresh=first_stage_nms_iou_threshold, max_size_per_class=first_stage_max_proposals, max_total_size=first_stage_max_proposals, use_static_shapes=use_static_shapes) first_stage_localization_loss_weight = 1.0 first_stage_objectness_loss_weight = 1.0 post_processing_text_proto = """ batch_non_max_suppression { score_threshold: -20.0 iou_threshold: 1.0 max_detections_per_class: 5 max_total_detections: 5 use_static_shapes: """ +'{}'.format(use_static_shapes) + """ } """ post_processing_config = post_processing_pb2.PostProcessing() text_format.Merge(post_processing_text_proto, post_processing_config) second_stage_target_assigner = target_assigner.create_target_assigner( 'FasterRCNN', 'detection', use_matmul_gather=use_matmul_gather_in_matcher) second_stage_non_max_suppression_fn, _ = post_processing_builder.build( post_processing_config) second_stage_sampler = sampler.BalancedPositiveNegativeSampler( positive_fraction=1.0, is_static=use_static_shapes) second_stage_score_conversion_fn = tf.identity second_stage_localization_loss_weight = 1.0 second_stage_classification_loss_weight = 1.0 if softmax_second_stage_classification_loss: second_stage_classification_loss = ( losses.WeightedSoftmaxClassificationLoss()) else: second_stage_classification_loss = ( losses.WeightedSigmoidClassificationLoss()) hard_example_miner = None if hard_mining: hard_example_miner = losses.HardExampleMiner( num_hard_examples=1, iou_threshold=0.99, loss_type='both', cls_loss_weight=second_stage_classification_loss_weight, loc_loss_weight=second_stage_localization_loss_weight, max_negatives_per_positive=None) crop_and_resize_fn = ( ops.matmul_crop_and_resize if use_matmul_crop_and_resize else ops.native_crop_and_resize) common_kwargs = { 'is_training': is_training, 'num_classes': num_classes, 'image_resizer_fn': image_resizer_fn, 'feature_extractor': fake_feature_extractor, 'number_of_stages': number_of_stages, 'first_stage_anchor_generator': first_stage_anchor_generator, 'first_stage_target_assigner': first_stage_target_assigner, 'first_stage_atrous_rate': first_stage_atrous_rate, 'first_stage_box_predictor_arg_scope_fn': first_stage_box_predictor_arg_scope_fn, 'first_stage_box_predictor_kernel_size': first_stage_box_predictor_kernel_size, 'first_stage_box_predictor_depth': first_stage_box_predictor_depth, 'first_stage_minibatch_size': first_stage_minibatch_size, 'first_stage_sampler': first_stage_sampler, 'first_stage_non_max_suppression_fn': first_stage_non_max_suppression_fn, 'first_stage_max_proposals': first_stage_max_proposals, 'first_stage_localization_loss_weight': first_stage_localization_loss_weight, 'first_stage_objectness_loss_weight': first_stage_objectness_loss_weight, 'second_stage_target_assigner': second_stage_target_assigner, 'second_stage_batch_size': second_stage_batch_size, 'second_stage_sampler': second_stage_sampler, 'second_stage_non_max_suppression_fn': second_stage_non_max_suppression_fn, 'second_stage_score_conversion_fn': second_stage_score_conversion_fn, 'second_stage_localization_loss_weight': second_stage_localization_loss_weight, 'second_stage_classification_loss_weight': second_stage_classification_loss_weight, 'second_stage_classification_loss': second_stage_classification_loss, 'hard_example_miner': hard_example_miner, 'crop_and_resize_fn': crop_and_resize_fn, 'clip_anchors_to_image': clip_anchors_to_image, 'use_static_shapes': use_static_shapes, 'resize_masks': True, } return self._get_model( self._get_second_stage_box_predictor( num_classes=num_classes, is_training=is_training, predict_masks=predict_masks, masks_are_class_agnostic=masks_are_class_agnostic), **common_kwargs) def test_predict_gives_correct_shapes_in_inference_mode_first_stage_only( self, use_static_shapes=False): batch_size = 2 height = 10 width = 12 input_image_shape = (batch_size, height, width, 3) def graph_fn(images): """Function to construct tf graph for the test.""" model = self._build_model( is_training=False, number_of_stages=1, second_stage_batch_size=2, clip_anchors_to_image=use_static_shapes, use_static_shapes=use_static_shapes) preprocessed_inputs, true_image_shapes = model.preprocess(images) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) return (prediction_dict['rpn_box_predictor_features'], prediction_dict['rpn_features_to_crop'], prediction_dict['image_shape'], prediction_dict['rpn_box_encodings'], prediction_dict['rpn_objectness_predictions_with_background'], prediction_dict['anchors']) images = np.zeros(input_image_shape, dtype=np.float32) # In inference mode, anchors are clipped to the image window, but not # pruned. Since MockFasterRCNN.extract_proposal_features returns a # tensor with the same shape as its input, the expected number of anchors # is height * width * the number of anchors per location (i.e. 3x3). expected_num_anchors = height * width * 3 * 3 expected_output_shapes = { 'rpn_box_predictor_features': (batch_size, height, width, 512), 'rpn_features_to_crop': (batch_size, height, width, 3), 'rpn_box_encodings': (batch_size, expected_num_anchors, 4), 'rpn_objectness_predictions_with_background': (batch_size, expected_num_anchors, 2), 'anchors': (expected_num_anchors, 4) } if use_static_shapes: results = self.execute(graph_fn, [images]) else: results = self.execute_cpu(graph_fn, [images]) self.assertAllEqual(results[0].shape, expected_output_shapes['rpn_box_predictor_features']) self.assertAllEqual(results[1].shape, expected_output_shapes['rpn_features_to_crop']) self.assertAllEqual(results[2], input_image_shape) self.assertAllEqual(results[3].shape, expected_output_shapes['rpn_box_encodings']) self.assertAllEqual( results[4].shape, expected_output_shapes['rpn_objectness_predictions_with_background']) self.assertAllEqual(results[5].shape, expected_output_shapes['anchors']) # Check that anchors are clipped to window. anchors = results[5] self.assertTrue(np.all(np.greater_equal(anchors, 0))) self.assertTrue(np.all(np.less_equal(anchors[:, 0], height))) self.assertTrue(np.all(np.less_equal(anchors[:, 1], width))) self.assertTrue(np.all(np.less_equal(anchors[:, 2], height))) self.assertTrue(np.all(np.less_equal(anchors[:, 3], width))) def test_predict_gives_valid_anchors_in_training_mode_first_stage_only(self): test_graph = tf.Graph() with test_graph.as_default(): model = self._build_model( is_training=True, number_of_stages=1, second_stage_batch_size=2) batch_size = 2 height = 10 width = 12 input_image_shape = (batch_size, height, width, 3) _, true_image_shapes = model.preprocess(tf.zeros(input_image_shape)) preprocessed_inputs = tf.placeholder( dtype=tf.float32, shape=(batch_size, None, None, 3)) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) expected_output_keys = set([ 'rpn_box_predictor_features', 'rpn_features_to_crop', 'image_shape', 'rpn_box_encodings', 'rpn_objectness_predictions_with_background', 'anchors']) # At training time, anchors that exceed image bounds are pruned. Thus # the `expected_num_anchors` in the above inference mode test is now # a strict upper bound on the number of anchors. num_anchors_strict_upper_bound = height * width * 3 * 3 init_op = tf.global_variables_initializer() with self.test_session(graph=test_graph) as sess: sess.run(init_op) prediction_out = sess.run(prediction_dict, feed_dict={ preprocessed_inputs: np.zeros(input_image_shape) }) self.assertEqual(set(prediction_out.keys()), expected_output_keys) self.assertAllEqual(prediction_out['image_shape'], input_image_shape) # Check that anchors have less than the upper bound and # are clipped to window. anchors = prediction_out['anchors'] self.assertTrue(len(anchors.shape) == 2 and anchors.shape[1] == 4) num_anchors_out = anchors.shape[0] self.assertLess(num_anchors_out, num_anchors_strict_upper_bound) self.assertTrue(np.all(np.greater_equal(anchors, 0))) self.assertTrue(np.all(np.less_equal(anchors[:, 0], height))) self.assertTrue(np.all(np.less_equal(anchors[:, 1], width))) self.assertTrue(np.all(np.less_equal(anchors[:, 2], height))) self.assertTrue(np.all(np.less_equal(anchors[:, 3], width))) self.assertAllEqual(prediction_out['rpn_box_encodings'].shape, (batch_size, num_anchors_out, 4)) self.assertAllEqual( prediction_out['rpn_objectness_predictions_with_background'].shape, (batch_size, num_anchors_out, 2)) def test_predict_correct_shapes_in_inference_mode_two_stages( self, use_static_shapes=False): def compare_results(results, expected_output_shapes): """Checks if the shape of the predictions are as expected.""" self.assertAllEqual(results[0].shape, expected_output_shapes['rpn_box_predictor_features']) self.assertAllEqual(results[1].shape, expected_output_shapes['rpn_features_to_crop']) self.assertAllEqual(results[2].shape, expected_output_shapes['image_shape']) self.assertAllEqual(results[3].shape, expected_output_shapes['rpn_box_encodings']) self.assertAllEqual( results[4].shape, expected_output_shapes['rpn_objectness_predictions_with_background']) self.assertAllEqual(results[5].shape, expected_output_shapes['anchors']) self.assertAllEqual(results[6].shape, expected_output_shapes['refined_box_encodings']) self.assertAllEqual( results[7].shape, expected_output_shapes['class_predictions_with_background']) self.assertAllEqual(results[8].shape, expected_output_shapes['num_proposals']) self.assertAllEqual(results[9].shape, expected_output_shapes['proposal_boxes']) self.assertAllEqual(results[10].shape, expected_output_shapes['proposal_boxes_normalized']) self.assertAllEqual(results[11].shape, expected_output_shapes['box_classifier_features']) batch_size = 2 image_size = 10 max_num_proposals = 8 initial_crop_size = 3 maxpool_stride = 1 input_shapes = [(batch_size, image_size, image_size, 3), (None, image_size, image_size, 3), (batch_size, None, None, 3), (None, None, None, 3)] def graph_fn_tpu(images): """Function to construct tf graph for the test.""" model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=2, predict_masks=False, use_matmul_crop_and_resize=use_static_shapes, clip_anchors_to_image=use_static_shapes, use_static_shapes=use_static_shapes) preprocessed_inputs, true_image_shapes = model.preprocess(images) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) return (prediction_dict['rpn_box_predictor_features'], prediction_dict['rpn_features_to_crop'], prediction_dict['image_shape'], prediction_dict['rpn_box_encodings'], prediction_dict['rpn_objectness_predictions_with_background'], prediction_dict['anchors'], prediction_dict['refined_box_encodings'], prediction_dict['class_predictions_with_background'], prediction_dict['num_proposals'], prediction_dict['proposal_boxes'], prediction_dict['proposal_boxes_normalized'], prediction_dict['box_classifier_features']) expected_num_anchors = image_size * image_size * 3 * 3 expected_shapes = { 'rpn_box_predictor_features': (2, image_size, image_size, 512), 'rpn_features_to_crop': (2, image_size, image_size, 3), 'image_shape': (4,), 'rpn_box_encodings': (2, expected_num_anchors, 4), 'rpn_objectness_predictions_with_background': (2, expected_num_anchors, 2), 'anchors': (expected_num_anchors, 4), 'refined_box_encodings': (2 * max_num_proposals, 2, 4), 'class_predictions_with_background': (2 * max_num_proposals, 2 + 1), 'num_proposals': (2,), 'proposal_boxes': (2, max_num_proposals, 4), 'proposal_boxes_normalized': (2, max_num_proposals, 4), 'box_classifier_features': self._get_box_classifier_features_shape(image_size, batch_size, max_num_proposals, initial_crop_size, maxpool_stride, 3) } if use_static_shapes: input_shape = (batch_size, image_size, image_size, 3) images = np.zeros(input_shape, dtype=np.float32) results = self.execute(graph_fn_tpu, [images]) compare_results(results, expected_shapes) else: for input_shape in input_shapes: test_graph = tf.Graph() with test_graph.as_default(): model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=2, predict_masks=False) preprocessed_inputs = tf.placeholder(tf.float32, shape=input_shape) _, true_image_shapes = model.preprocess(preprocessed_inputs) result_tensor_dict = model.predict( preprocessed_inputs, true_image_shapes) init_op = tf.global_variables_initializer() with self.test_session(graph=test_graph) as sess: sess.run(init_op) tensor_dict_out = sess.run(result_tensor_dict, feed_dict={ preprocessed_inputs: np.zeros((batch_size, image_size, image_size, 3))}) self.assertEqual(set(tensor_dict_out.keys()), set(expected_shapes.keys())) for key in expected_shapes: self.assertAllEqual(tensor_dict_out[key].shape, expected_shapes[key]) def test_predict_gives_correct_shapes_in_train_mode_both_stages( self, use_static_shapes=False): batch_size = 2 image_size = 10 max_num_proposals = 7 initial_crop_size = 3 maxpool_stride = 1 def graph_fn(images, gt_boxes, gt_classes, gt_weights): """Function to construct tf graph for the test.""" model = self._build_model( is_training=True, number_of_stages=2, second_stage_batch_size=7, predict_masks=False, use_matmul_crop_and_resize=use_static_shapes, clip_anchors_to_image=use_static_shapes, use_static_shapes=use_static_shapes) preprocessed_inputs, true_image_shapes = model.preprocess(images) model.provide_groundtruth( groundtruth_boxes_list=tf.unstack(gt_boxes), groundtruth_classes_list=tf.unstack(gt_classes), groundtruth_weights_list=tf.unstack(gt_weights)) result_tensor_dict = model.predict(preprocessed_inputs, true_image_shapes) return (result_tensor_dict['refined_box_encodings'], result_tensor_dict['class_predictions_with_background'], result_tensor_dict['proposal_boxes'], result_tensor_dict['proposal_boxes_normalized'], result_tensor_dict['anchors'], result_tensor_dict['rpn_box_encodings'], result_tensor_dict['rpn_objectness_predictions_with_background'], result_tensor_dict['rpn_features_to_crop'], result_tensor_dict['rpn_box_predictor_features'], ) image_shape = (batch_size, image_size, image_size, 3) images = np.zeros(image_shape, dtype=np.float32) gt_boxes = np.stack([ np.array([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=np.float32), np.array([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=np.float32) ]) gt_classes = np.stack([ np.array([[1, 0], [0, 1]], dtype=np.float32), np.array([[1, 0], [1, 0]], dtype=np.float32) ]) gt_weights = np.stack([ np.array([1, 1], dtype=np.float32), np.array([1, 1], dtype=np.float32) ]) if use_static_shapes: results = self.execute(graph_fn, [images, gt_boxes, gt_classes, gt_weights]) else: results = self.execute_cpu(graph_fn, [images, gt_boxes, gt_classes, gt_weights]) expected_shapes = { 'rpn_box_predictor_features': (2, image_size, image_size, 512), 'rpn_features_to_crop': (2, image_size, image_size, 3), 'refined_box_encodings': (2 * max_num_proposals, 2, 4), 'class_predictions_with_background': (2 * max_num_proposals, 2 + 1), 'proposal_boxes': (2, max_num_proposals, 4), 'rpn_box_encodings': (2, image_size * image_size * 9, 4), 'proposal_boxes_normalized': (2, max_num_proposals, 4), 'box_classifier_features': self._get_box_classifier_features_shape( image_size, batch_size, max_num_proposals, initial_crop_size, maxpool_stride, 3), 'rpn_objectness_predictions_with_background': (2, image_size * image_size * 9, 2) } # TODO(rathodv): Possibly change utils/test_case.py to accept dictionaries # and return dicionaries so don't have to rely on the order of tensors. self.assertAllEqual(results[0].shape, expected_shapes['refined_box_encodings']) self.assertAllEqual(results[1].shape, expected_shapes['class_predictions_with_background']) self.assertAllEqual(results[2].shape, expected_shapes['proposal_boxes']) self.assertAllEqual(results[3].shape, expected_shapes['proposal_boxes_normalized']) anchors_shape = results[4].shape self.assertAllEqual(results[5].shape, [batch_size, anchors_shape[0], 4]) self.assertAllEqual(results[6].shape, [batch_size, anchors_shape[0], 2]) self.assertAllEqual(results[7].shape, expected_shapes['rpn_features_to_crop']) self.assertAllEqual(results[8].shape, expected_shapes['rpn_box_predictor_features']) def test_postprocess_first_stage_only_inference_mode( self, use_static_shapes=False, pad_to_max_dimension=None): batch_size = 2 first_stage_max_proposals = 4 if use_static_shapes else 8 def graph_fn(images, rpn_box_encodings, rpn_objectness_predictions_with_background, rpn_features_to_crop, anchors): """Function to construct tf graph for the test.""" model = self._build_model( is_training=False, number_of_stages=1, second_stage_batch_size=6, use_matmul_crop_and_resize=use_static_shapes, clip_anchors_to_image=use_static_shapes, use_static_shapes=use_static_shapes, use_matmul_gather_in_matcher=use_static_shapes, first_stage_max_proposals=first_stage_max_proposals, pad_to_max_dimension=pad_to_max_dimension) _, true_image_shapes = model.preprocess(images) proposals = model.postprocess({ 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'rpn_features_to_crop': rpn_features_to_crop, 'anchors': anchors}, true_image_shapes) return (proposals['num_detections'], proposals['detection_boxes'], proposals['detection_scores']) anchors = np.array( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=np.float32) rpn_box_encodings = np.zeros( (batch_size, anchors.shape[0], BOX_CODE_SIZE), dtype=np.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = np.array([ [[-10, 13], [10, -10], [10, -11], [-10, 12]], [[10, -10], [-10, 13], [-10, 12], [10, -11]]], dtype=np.float32) rpn_features_to_crop = np.ones((batch_size, 8, 8, 10), dtype=np.float32) image_shape = (batch_size, 32, 32, 3) images = np.zeros(image_shape, dtype=np.float32) if use_static_shapes: results = self.execute(graph_fn, [images, rpn_box_encodings, rpn_objectness_predictions_with_background, rpn_features_to_crop, anchors]) else: results = self.execute_cpu(graph_fn, [images, rpn_box_encodings, rpn_objectness_predictions_with_background, rpn_features_to_crop, anchors]) expected_proposal_boxes = [ [[0, 0, .5, .5], [.5, .5, 1, 1], [0, .5, .5, 1], [.5, 0, 1.0, .5]] + 4 * [4 * [0]], [[0, .5, .5, 1], [.5, 0, 1.0, .5], [0, 0, .5, .5], [.5, .5, 1, 1]] + 4 * [4 * [0]]] expected_proposal_scores = [[1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0]] expected_num_proposals = [4, 4] self.assertAllClose(results[0], expected_num_proposals) for indx, num_proposals in enumerate(expected_num_proposals): self.assertAllClose(results[1][indx][0:num_proposals], expected_proposal_boxes[indx][0:num_proposals]) self.assertAllClose(results[2][indx][0:num_proposals], expected_proposal_scores[indx][0:num_proposals]) def _test_postprocess_first_stage_only_train_mode(self, pad_to_max_dimension=None): model = self._build_model( is_training=True, number_of_stages=1, second_stage_batch_size=2, pad_to_max_dimension=pad_to_max_dimension) batch_size = 2 anchors = tf.constant( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=tf.float32) rpn_box_encodings = tf.zeros( [batch_size, anchors.get_shape().as_list()[0], BOX_CODE_SIZE], dtype=tf.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = tf.constant([ [[-10, 13], [-10, 12], [-10, 11], [-10, 10]], [[-10, 13], [-10, 12], [-10, 11], [-10, 10]]], dtype=tf.float32) rpn_features_to_crop = tf.ones((batch_size, 8, 8, 10), dtype=tf.float32) image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) groundtruth_boxes_list = [ tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32)] groundtruth_classes_list = [tf.constant([[1, 0], [0, 1]], dtype=tf.float32), tf.constant([[1, 0], [1, 0]], dtype=tf.float32)] groundtruth_weights_list = [ tf.constant([1, 1], dtype=tf.float32), tf.constant([1, 1], dtype=tf.float32) ] _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth( groundtruth_boxes_list, groundtruth_classes_list, groundtruth_weights_list=groundtruth_weights_list) proposals = model.postprocess({ 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'rpn_features_to_crop': rpn_features_to_crop, 'anchors': anchors}, true_image_shapes) expected_proposal_boxes = [ [[0, 0, .5, .5], [.5, .5, 1, 1]], [[0, .5, .5, 1], [.5, 0, 1, .5]]] expected_proposal_scores = [[1, 1], [1, 1]] expected_num_proposals = [2, 2] expected_output_keys = set(['detection_boxes', 'detection_scores', 'num_detections']) self.assertEqual(set(proposals.keys()), expected_output_keys) with self.test_session() as sess: proposals_out = sess.run(proposals) for image_idx in range(batch_size): self.assertTrue( test_utils.first_rows_close_as_set( proposals_out['detection_boxes'][image_idx].tolist(), expected_proposal_boxes[image_idx])) self.assertAllClose(proposals_out['detection_scores'], expected_proposal_scores) self.assertAllEqual(proposals_out['num_detections'], expected_num_proposals) def test_postprocess_first_stage_only_train_mode(self): self._test_postprocess_first_stage_only_train_mode() def test_postprocess_first_stage_only_train_mode_padded_image(self): self._test_postprocess_first_stage_only_train_mode(pad_to_max_dimension=56) def test_postprocess_second_stage_only_inference_mode( self, use_static_shapes=False, pad_to_max_dimension=None): batch_size = 2 num_classes = 2 image_shape = np.array((2, 36, 48, 3), dtype=np.int32) first_stage_max_proposals = 8 total_num_padded_proposals = batch_size * first_stage_max_proposals def graph_fn(images, refined_box_encodings, class_predictions_with_background, num_proposals, proposal_boxes): """Function to construct tf graph for the test.""" model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=6, use_matmul_crop_and_resize=use_static_shapes, clip_anchors_to_image=use_static_shapes, use_static_shapes=use_static_shapes, use_matmul_gather_in_matcher=use_static_shapes, pad_to_max_dimension=pad_to_max_dimension) _, true_image_shapes = model.preprocess(images) detections = model.postprocess({ 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'num_proposals': num_proposals, 'proposal_boxes': proposal_boxes, }, true_image_shapes) return (detections['num_detections'], detections['detection_boxes'], detections['detection_scores'], detections['detection_classes']) proposal_boxes = np.array( [[[1, 1, 2, 3], [0, 0, 1, 1], [.5, .5, .6, .6], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], [[2, 3, 6, 8], [1, 2, 5, 3], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]], dtype=np.float32) num_proposals = np.array([3, 2], dtype=np.int32) refined_box_encodings = np.zeros( [total_num_padded_proposals, num_classes, 4], dtype=np.float32) class_predictions_with_background = np.ones( [total_num_padded_proposals, num_classes+1], dtype=np.float32) images = np.zeros(image_shape, dtype=np.float32) if use_static_shapes: results = self.execute(graph_fn, [images, refined_box_encodings, class_predictions_with_background, num_proposals, proposal_boxes]) else: results = self.execute_cpu(graph_fn, [images, refined_box_encodings, class_predictions_with_background, num_proposals, proposal_boxes]) expected_num_detections = [5, 4] expected_detection_classes = [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]] expected_detection_scores = [[1, 1, 1, 1, 1], [1, 1, 1, 1, 0]] self.assertAllClose(results[0], expected_num_detections) for indx, num_proposals in enumerate(expected_num_detections): self.assertAllClose(results[2][indx][0:num_proposals], expected_detection_scores[indx][0:num_proposals]) self.assertAllClose(results[3][indx][0:num_proposals], expected_detection_classes[indx][0:num_proposals]) if not use_static_shapes: self.assertAllEqual(results[1].shape, [2, 5, 4]) def test_preprocess_preserves_input_shapes(self): image_shapes = [(3, None, None, 3), (None, 10, 10, 3), (None, None, None, 3)] for image_shape in image_shapes: model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=6) image_placeholder = tf.placeholder(tf.float32, shape=image_shape) preprocessed_inputs, _ = model.preprocess(image_placeholder) self.assertAllEqual(preprocessed_inputs.shape.as_list(), image_shape) # TODO(rathodv): Split test into two - with and without masks. def test_loss_first_stage_only_mode(self): model = self._build_model( is_training=True, number_of_stages=1, second_stage_batch_size=6) batch_size = 2 anchors = tf.constant( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=tf.float32) rpn_box_encodings = tf.zeros( [batch_size, anchors.get_shape().as_list()[0], BOX_CODE_SIZE], dtype=tf.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = tf.constant([ [[-10, 13], [10, -10], [10, -11], [-10, 12]], [[10, -10], [-10, 13], [-10, 12], [10, -11]]], dtype=tf.float32) image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) groundtruth_boxes_list = [ tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32)] groundtruth_classes_list = [tf.constant([[1, 0], [0, 1]], dtype=tf.float32), tf.constant([[1, 0], [1, 0]], dtype=tf.float32)] prediction_dict = { 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'image_shape': image_shape, 'anchors': anchors } _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list) loss_dict = model.loss(prediction_dict, true_image_shapes) with self.test_session() as sess: loss_dict_out = sess.run(loss_dict) self.assertAllClose(loss_dict_out['Loss/RPNLoss/localization_loss'], 0) self.assertAllClose(loss_dict_out['Loss/RPNLoss/objectness_loss'], 0) self.assertNotIn('Loss/BoxClassifierLoss/localization_loss', loss_dict_out) self.assertNotIn('Loss/BoxClassifierLoss/classification_loss', loss_dict_out) # TODO(rathodv): Split test into two - with and without masks. def test_loss_full(self): model = self._build_model( is_training=True, number_of_stages=2, second_stage_batch_size=6) batch_size = 3 anchors = tf.constant( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=tf.float32) rpn_box_encodings = tf.zeros( [batch_size, anchors.get_shape().as_list()[0], BOX_CODE_SIZE], dtype=tf.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = tf.constant( [[[-10, 13], [10, -10], [10, -11], [-10, 12]], [[10, -10], [-10, 13], [ -10, 12 ], [10, -11]], [[10, -10], [-10, 13], [-10, 12], [10, -11]]], dtype=tf.float32) image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) num_proposals = tf.constant([6, 6, 6], dtype=tf.int32) proposal_boxes = tf.constant( 3 * [[[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32], [0, 0, 16, 16], [0, 16, 16, 32]]], dtype=tf.float32) refined_box_encodings = tf.zeros( (batch_size * model.max_num_proposals, model.num_classes, BOX_CODE_SIZE), dtype=tf.float32) class_predictions_with_background = tf.constant( [ [-10, 10, -10], # first image [10, -10, -10], [10, -10, -10], [-10, -10, 10], [-10, 10, -10], [10, -10, -10], [10, -10, -10], # second image [-10, 10, -10], [-10, 10, -10], [10, -10, -10], [10, -10, -10], [-10, 10, -10], [10, -10, -10], # third image [-10, 10, -10], [-10, 10, -10], [10, -10, -10], [10, -10, -10], [-10, 10, -10] ], dtype=tf.float32) mask_predictions_logits = 20 * tf.ones((batch_size * model.max_num_proposals, model.num_classes, 14, 14), dtype=tf.float32) groundtruth_boxes_list = [ tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32), tf.constant([[0, .5, .5, 1], [.5, 0, 1, 1]], dtype=tf.float32) ] groundtruth_classes_list = [ tf.constant([[1, 0], [0, 1]], dtype=tf.float32), tf.constant([[1, 0], [1, 0]], dtype=tf.float32), tf.constant([[1, 0], [0, 1]], dtype=tf.float32) ] # Set all elements of groundtruth mask to 1.0. In this case all proposal # crops of the groundtruth masks should return a mask that covers the entire # proposal. Thus, if mask_predictions_logits element values are all greater # than 20, the loss should be zero. groundtruth_masks_list = [ tf.convert_to_tensor(np.ones((2, 32, 32)), dtype=tf.float32), tf.convert_to_tensor(np.ones((2, 32, 32)), dtype=tf.float32), tf.convert_to_tensor(np.ones((2, 32, 32)), dtype=tf.float32) ] groundtruth_weights_list = [ tf.constant([1, 1], dtype=tf.float32), tf.constant([1, 1], dtype=tf.float32), tf.constant([1, 0], dtype=tf.float32) ] prediction_dict = { 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'image_shape': image_shape, 'anchors': anchors, 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'proposal_boxes': proposal_boxes, 'num_proposals': num_proposals, 'mask_predictions': mask_predictions_logits } _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth( groundtruth_boxes_list, groundtruth_classes_list, groundtruth_masks_list, groundtruth_weights_list=groundtruth_weights_list) loss_dict = model.loss(prediction_dict, true_image_shapes) with self.test_session() as sess: loss_dict_out = sess.run(loss_dict) self.assertAllClose(loss_dict_out['Loss/RPNLoss/localization_loss'], 0) self.assertAllClose(loss_dict_out['Loss/RPNLoss/objectness_loss'], 0) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/localization_loss'], 0) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/classification_loss'], 0) self.assertAllClose(loss_dict_out['Loss/BoxClassifierLoss/mask_loss'], 0) def test_loss_full_zero_padded_proposals(self): model = self._build_model( is_training=True, number_of_stages=2, second_stage_batch_size=6) batch_size = 1 anchors = tf.constant( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=tf.float32) rpn_box_encodings = tf.zeros( [batch_size, anchors.get_shape().as_list()[0], BOX_CODE_SIZE], dtype=tf.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = tf.constant([ [[-10, 13], [10, -10], [10, -11], [10, -12]],], dtype=tf.float32) image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) # box_classifier_batch_size is 6, but here we assume that the number of # actual proposals (not counting zero paddings) is fewer (3). num_proposals = tf.constant([3], dtype=tf.int32) proposal_boxes = tf.constant( [[[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [0, 0, 0, 0], # begin paddings [0, 0, 0, 0], [0, 0, 0, 0]]], dtype=tf.float32) refined_box_encodings = tf.zeros( (batch_size * model.max_num_proposals, model.num_classes, BOX_CODE_SIZE), dtype=tf.float32) class_predictions_with_background = tf.constant( [[-10, 10, -10], [10, -10, -10], [10, -10, -10], [0, 0, 0], # begin paddings [0, 0, 0], [0, 0, 0]], dtype=tf.float32) mask_predictions_logits = 20 * tf.ones((batch_size * model.max_num_proposals, model.num_classes, 14, 14), dtype=tf.float32) groundtruth_boxes_list = [ tf.constant([[0, 0, .5, .5]], dtype=tf.float32)] groundtruth_classes_list = [tf.constant([[1, 0]], dtype=tf.float32)] # Set all elements of groundtruth mask to 1.0. In this case all proposal # crops of the groundtruth masks should return a mask that covers the entire # proposal. Thus, if mask_predictions_logits element values are all greater # than 20, the loss should be zero. groundtruth_masks_list = [tf.convert_to_tensor(np.ones((1, 32, 32)), dtype=tf.float32)] prediction_dict = { 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'image_shape': image_shape, 'anchors': anchors, 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'proposal_boxes': proposal_boxes, 'num_proposals': num_proposals, 'mask_predictions': mask_predictions_logits } _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list, groundtruth_masks_list) loss_dict = model.loss(prediction_dict, true_image_shapes) with self.test_session() as sess: loss_dict_out = sess.run(loss_dict) self.assertAllClose(loss_dict_out['Loss/RPNLoss/localization_loss'], 0) self.assertAllClose(loss_dict_out['Loss/RPNLoss/objectness_loss'], 0) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/localization_loss'], 0) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/classification_loss'], 0) self.assertAllClose(loss_dict_out['Loss/BoxClassifierLoss/mask_loss'], 0) def test_loss_full_multiple_label_groundtruth(self): model = self._build_model( is_training=True, number_of_stages=2, second_stage_batch_size=6, softmax_second_stage_classification_loss=False) batch_size = 1 anchors = tf.constant( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=tf.float32) rpn_box_encodings = tf.zeros( [batch_size, anchors.get_shape().as_list()[0], BOX_CODE_SIZE], dtype=tf.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = tf.constant([ [[-10, 13], [10, -10], [10, -11], [10, -12]],], dtype=tf.float32) image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) # box_classifier_batch_size is 6, but here we assume that the number of # actual proposals (not counting zero paddings) is fewer (3). num_proposals = tf.constant([3], dtype=tf.int32) proposal_boxes = tf.constant( [[[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [0, 0, 0, 0], # begin paddings [0, 0, 0, 0], [0, 0, 0, 0]]], dtype=tf.float32) # second_stage_localization_loss should only be computed for predictions # that match groundtruth. For multiple label groundtruth boxes, the loss # should only be computed once for the label with the smaller index. refined_box_encodings = tf.constant( [[[0, 0, 0, 0], [1, 1, -1, -1]], [[1, 1, -1, -1], [1, 1, 1, 1]], [[1, 1, -1, -1], [1, 1, 1, 1]], [[1, 1, -1, -1], [1, 1, 1, 1]], [[1, 1, -1, -1], [1, 1, 1, 1]], [[1, 1, -1, -1], [1, 1, 1, 1]]], dtype=tf.float32) class_predictions_with_background = tf.constant( [[-100, 100, 100], [100, -100, -100], [100, -100, -100], [0, 0, 0], # begin paddings [0, 0, 0], [0, 0, 0]], dtype=tf.float32) mask_predictions_logits = 20 * tf.ones((batch_size * model.max_num_proposals, model.num_classes, 14, 14), dtype=tf.float32) groundtruth_boxes_list = [ tf.constant([[0, 0, .5, .5]], dtype=tf.float32)] # Box contains two ground truth labels. groundtruth_classes_list = [tf.constant([[1, 1]], dtype=tf.float32)] # Set all elements of groundtruth mask to 1.0. In this case all proposal # crops of the groundtruth masks should return a mask that covers the entire # proposal. Thus, if mask_predictions_logits element values are all greater # than 20, the loss should be zero. groundtruth_masks_list = [tf.convert_to_tensor(np.ones((1, 32, 32)), dtype=tf.float32)] prediction_dict = { 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'image_shape': image_shape, 'anchors': anchors, 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'proposal_boxes': proposal_boxes, 'num_proposals': num_proposals, 'mask_predictions': mask_predictions_logits } _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list, groundtruth_masks_list) loss_dict = model.loss(prediction_dict, true_image_shapes) with self.test_session() as sess: loss_dict_out = sess.run(loss_dict) self.assertAllClose(loss_dict_out['Loss/RPNLoss/localization_loss'], 0) self.assertAllClose(loss_dict_out['Loss/RPNLoss/objectness_loss'], 0) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/localization_loss'], 0) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/classification_loss'], 0) self.assertAllClose(loss_dict_out['Loss/BoxClassifierLoss/mask_loss'], 0) def test_loss_full_zero_padded_proposals_nonzero_loss_with_two_images( self, use_static_shapes=False, shared_boxes=False): batch_size = 2 first_stage_max_proposals = 8 second_stage_batch_size = 6 num_classes = 2 def graph_fn(anchors, rpn_box_encodings, rpn_objectness_predictions_with_background, images, num_proposals, proposal_boxes, refined_box_encodings, class_predictions_with_background, groundtruth_boxes, groundtruth_classes): """Function to construct tf graph for the test.""" model = self._build_model( is_training=True, number_of_stages=2, second_stage_batch_size=second_stage_batch_size, first_stage_max_proposals=first_stage_max_proposals, num_classes=num_classes, use_matmul_crop_and_resize=use_static_shapes, clip_anchors_to_image=use_static_shapes, use_static_shapes=use_static_shapes) prediction_dict = { 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'image_shape': tf.shape(images), 'anchors': anchors, 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'proposal_boxes': proposal_boxes, 'num_proposals': num_proposals } _, true_image_shapes = model.preprocess(images) model.provide_groundtruth(tf.unstack(groundtruth_boxes), tf.unstack(groundtruth_classes)) loss_dict = model.loss(prediction_dict, true_image_shapes) return (loss_dict['Loss/RPNLoss/localization_loss'], loss_dict['Loss/RPNLoss/objectness_loss'], loss_dict['Loss/BoxClassifierLoss/localization_loss'], loss_dict['Loss/BoxClassifierLoss/classification_loss']) anchors = np.array( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=np.float32) rpn_box_encodings = np.zeros( [batch_size, anchors.shape[1], BOX_CODE_SIZE], dtype=np.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = np.array( [[[-10, 13], [10, -10], [10, -11], [10, -12]], [[-10, 13], [10, -10], [10, -11], [10, -12]]], dtype=np.float32) images = np.zeros([batch_size, 32, 32, 3], dtype=np.float32) # box_classifier_batch_size is 6, but here we assume that the number of # actual proposals (not counting zero paddings) is fewer. num_proposals = np.array([3, 2], dtype=np.int32) proposal_boxes = np.array( [[[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [0, 0, 0, 0], # begin paddings [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 16, 16], [0, 16, 16, 32], [0, 0, 0, 0], # begin paddings [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]], dtype=np.float32) refined_box_encodings = np.zeros( (batch_size * second_stage_batch_size, 1 if shared_boxes else num_classes, BOX_CODE_SIZE), dtype=np.float32) class_predictions_with_background = np.array( [[-10, 10, -10], # first image [10, -10, -10], [10, -10, -10], [0, 0, 0], # begin paddings [0, 0, 0], [0, 0, 0], [-10, -10, 10], # second image [10, -10, -10], [0, 0, 0], # begin paddings [0, 0, 0], [0, 0, 0], [0, 0, 0],], dtype=np.float32) # The first groundtruth box is 4/5 of the anchor size in both directions # experiencing a loss of: # 2 * SmoothL1(5 * log(4/5)) / num_proposals # = 2 * (abs(5 * log(1/2)) - .5) / 3 # The second groundtruth box is identical to the prediction and thus # experiences zero loss. # Total average loss is (abs(5 * log(1/2)) - .5) / 3. groundtruth_boxes = np.stack([ np.array([[0.05, 0.05, 0.45, 0.45]], dtype=np.float32), np.array([[0.0, 0.0, 0.5, 0.5]], dtype=np.float32)]) groundtruth_classes = np.stack([np.array([[1, 0]], dtype=np.float32), np.array([[0, 1]], dtype=np.float32)]) execute_fn = self.execute_cpu if use_static_shapes: execute_fn = self.execute results = execute_fn(graph_fn, [ anchors, rpn_box_encodings, rpn_objectness_predictions_with_background, images, num_proposals, proposal_boxes, refined_box_encodings, class_predictions_with_background, groundtruth_boxes, groundtruth_classes ]) exp_loc_loss = (-5 * np.log(.8) - 0.5) / 3.0 self.assertAllClose(results[0], exp_loc_loss, rtol=1e-4, atol=1e-4) self.assertAllClose(results[1], 0.0) self.assertAllClose(results[2], exp_loc_loss, rtol=1e-4, atol=1e-4) self.assertAllClose(results[3], 0.0) def test_loss_with_hard_mining(self): model = self._build_model(is_training=True, number_of_stages=2, second_stage_batch_size=None, first_stage_max_proposals=6, hard_mining=True) batch_size = 1 anchors = tf.constant( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=tf.float32) rpn_box_encodings = tf.zeros( [batch_size, anchors.get_shape().as_list()[0], BOX_CODE_SIZE], dtype=tf.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = tf.constant( [[[-10, 13], [-10, 12], [10, -11], [10, -12]]], dtype=tf.float32) image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) # box_classifier_batch_size is 6, but here we assume that the number of # actual proposals (not counting zero paddings) is fewer (3). num_proposals = tf.constant([3], dtype=tf.int32) proposal_boxes = tf.constant( [[[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [0, 0, 0, 0], # begin paddings [0, 0, 0, 0], [0, 0, 0, 0]]], dtype=tf.float32) refined_box_encodings = tf.zeros( (batch_size * model.max_num_proposals, model.num_classes, BOX_CODE_SIZE), dtype=tf.float32) class_predictions_with_background = tf.constant( [[-10, 10, -10], # first image [-10, -10, 10], [10, -10, -10], [0, 0, 0], # begin paddings [0, 0, 0], [0, 0, 0]], dtype=tf.float32) # The first groundtruth box is 4/5 of the anchor size in both directions # experiencing a loss of: # 2 * SmoothL1(5 * log(4/5)) / num_proposals # = 2 * (abs(5 * log(1/2)) - .5) / 3 # The second groundtruth box is 46/50 of the anchor size in both directions # experiencing a loss of: # 2 * SmoothL1(5 * log(42/50)) / num_proposals # = 2 * (.5(5 * log(.92))^2 - .5) / 3. # Since the first groundtruth box experiences greater loss, and we have # set num_hard_examples=1 in the HardMiner, the final localization loss # corresponds to that of the first groundtruth box. groundtruth_boxes_list = [ tf.constant([[0.05, 0.05, 0.45, 0.45], [0.02, 0.52, 0.48, 0.98],], dtype=tf.float32)] groundtruth_classes_list = [tf.constant([[1, 0], [0, 1]], dtype=tf.float32)] exp_loc_loss = 2 * (-5 * np.log(.8) - 0.5) / 3.0 prediction_dict = { 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'image_shape': image_shape, 'anchors': anchors, 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'proposal_boxes': proposal_boxes, 'num_proposals': num_proposals } _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list) loss_dict = model.loss(prediction_dict, true_image_shapes) with self.test_session() as sess: loss_dict_out = sess.run(loss_dict) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/localization_loss'], exp_loc_loss) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/classification_loss'], 0) def test_loss_with_hard_mining_and_losses_mask(self): model = self._build_model(is_training=True, number_of_stages=2, second_stage_batch_size=None, first_stage_max_proposals=6, hard_mining=True) batch_size = 2 number_of_proposals = 3 anchors = tf.constant( [[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], [16, 16, 32, 32]], dtype=tf.float32) rpn_box_encodings = tf.zeros( [batch_size, anchors.get_shape().as_list()[0], BOX_CODE_SIZE], dtype=tf.float32) # use different numbers for the objectness category to break ties in # order of boxes returned by NMS rpn_objectness_predictions_with_background = tf.constant( [[[-10, 13], [-10, 12], [10, -11], [10, -12]], [[-10, 13], [-10, 12], [10, -11], [10, -12]]], dtype=tf.float32) image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) # box_classifier_batch_size is 6, but here we assume that the number of # actual proposals (not counting zero paddings) is fewer (3). num_proposals = tf.constant([number_of_proposals, number_of_proposals], dtype=tf.int32) proposal_boxes = tf.constant( [[[0, 0, 16, 16], # first image [0, 16, 16, 32], [16, 0, 32, 16], [0, 0, 0, 0], # begin paddings [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 16, 16], # second image [0, 16, 16, 32], [16, 0, 32, 16], [0, 0, 0, 0], # begin paddings [0, 0, 0, 0], [0, 0, 0, 0]]], dtype=tf.float32) refined_box_encodings = tf.zeros( (batch_size * model.max_num_proposals, model.num_classes, BOX_CODE_SIZE), dtype=tf.float32) class_predictions_with_background = tf.constant( [[-10, 10, -10], # first image [-10, -10, 10], [10, -10, -10], [0, 0, 0], # begin paddings [0, 0, 0], [0, 0, 0], [-10, 10, -10], # second image [-10, -10, 10], [10, -10, -10], [0, 0, 0], # begin paddings [0, 0, 0], [0, 0, 0]], dtype=tf.float32) # The first groundtruth box is 4/5 of the anchor size in both directions # experiencing a loss of: # 2 * SmoothL1(5 * log(4/5)) / (num_proposals * batch_size) # = 2 * (abs(5 * log(1/2)) - .5) / 3 # The second groundtruth box is 46/50 of the anchor size in both directions # experiencing a loss of: # 2 * SmoothL1(5 * log(42/50)) / (num_proposals * batch_size) # = 2 * (.5(5 * log(.92))^2 - .5) / 3. # Since the first groundtruth box experiences greater loss, and we have # set num_hard_examples=1 in the HardMiner, the final localization loss # corresponds to that of the first groundtruth box. groundtruth_boxes_list = [ tf.constant([[0.05, 0.05, 0.45, 0.45], [0.02, 0.52, 0.48, 0.98]], dtype=tf.float32), tf.constant([[0.05, 0.05, 0.45, 0.45], [0.02, 0.52, 0.48, 0.98]], dtype=tf.float32)] groundtruth_classes_list = [ tf.constant([[1, 0], [0, 1]], dtype=tf.float32), tf.constant([[1, 0], [0, 1]], dtype=tf.float32)] is_annotated_list = [tf.constant(True, dtype=tf.bool), tf.constant(False, dtype=tf.bool)] exp_loc_loss = (2 * (-5 * np.log(.8) - 0.5) / (number_of_proposals * batch_size)) prediction_dict = { 'rpn_box_encodings': rpn_box_encodings, 'rpn_objectness_predictions_with_background': rpn_objectness_predictions_with_background, 'image_shape': image_shape, 'anchors': anchors, 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'proposal_boxes': proposal_boxes, 'num_proposals': num_proposals } _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list, is_annotated_list=is_annotated_list) loss_dict = model.loss(prediction_dict, true_image_shapes) with self.test_session() as sess: loss_dict_out = sess.run(loss_dict) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/localization_loss'], exp_loc_loss) self.assertAllClose(loss_dict_out[ 'Loss/BoxClassifierLoss/classification_loss'], 0) def test_restore_map_for_classification_ckpt(self): # Define mock tensorflow classification graph and save variables. test_graph_classification = tf.Graph() with test_graph_classification.as_default(): image = tf.placeholder(dtype=tf.float32, shape=[1, 20, 20, 3]) with tf.variable_scope('mock_model'): net = slim.conv2d(image, num_outputs=3, kernel_size=1, scope='layer1') slim.conv2d(net, num_outputs=3, kernel_size=1, scope='layer2') init_op = tf.global_variables_initializer() saver = tf.train.Saver() save_path = self.get_temp_dir() with self.test_session(graph=test_graph_classification) as sess: sess.run(init_op) saved_model_path = saver.save(sess, save_path) # Create tensorflow detection graph and load variables from # classification checkpoint. test_graph_detection = tf.Graph() with test_graph_detection.as_default(): model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=6) inputs_shape = (2, 20, 20, 3) inputs = tf.to_float(tf.random_uniform( inputs_shape, minval=0, maxval=255, dtype=tf.int32)) preprocessed_inputs, true_image_shapes = model.preprocess(inputs) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) model.postprocess(prediction_dict, true_image_shapes) var_map = model.restore_map(fine_tune_checkpoint_type='classification') self.assertIsInstance(var_map, dict) saver = tf.train.Saver(var_map) with self.test_session(graph=test_graph_classification) as sess: saver.restore(sess, saved_model_path) for var in sess.run(tf.report_uninitialized_variables()): self.assertNotIn(model.first_stage_feature_extractor_scope, var) self.assertNotIn(model.second_stage_feature_extractor_scope, var) def test_restore_map_for_detection_ckpt(self): # Define first detection graph and save variables. test_graph_detection1 = tf.Graph() with test_graph_detection1.as_default(): model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=6) inputs_shape = (2, 20, 20, 3) inputs = tf.to_float(tf.random_uniform( inputs_shape, minval=0, maxval=255, dtype=tf.int32)) preprocessed_inputs, true_image_shapes = model.preprocess(inputs) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) model.postprocess(prediction_dict, true_image_shapes) another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable init_op = tf.global_variables_initializer() saver = tf.train.Saver() save_path = self.get_temp_dir() with self.test_session(graph=test_graph_detection1) as sess: sess.run(init_op) saved_model_path = saver.save(sess, save_path) # Define second detection graph and restore variables. test_graph_detection2 = tf.Graph() with test_graph_detection2.as_default(): model2 = self._build_model(is_training=False, number_of_stages=2, second_stage_batch_size=6, num_classes=42) inputs_shape2 = (2, 20, 20, 3) inputs2 = tf.to_float(tf.random_uniform( inputs_shape2, minval=0, maxval=255, dtype=tf.int32)) preprocessed_inputs2, true_image_shapes = model2.preprocess(inputs2) prediction_dict2 = model2.predict(preprocessed_inputs2, true_image_shapes) model2.postprocess(prediction_dict2, true_image_shapes) another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable var_map = model2.restore_map(fine_tune_checkpoint_type='detection') self.assertIsInstance(var_map, dict) saver = tf.train.Saver(var_map) with self.test_session(graph=test_graph_detection2) as sess: saver.restore(sess, saved_model_path) uninitialized_vars_list = sess.run(tf.report_uninitialized_variables()) self.assertIn('another_variable', uninitialized_vars_list) for var in uninitialized_vars_list: self.assertNotIn(model2.first_stage_feature_extractor_scope, var) self.assertNotIn(model2.second_stage_feature_extractor_scope, var) def test_load_all_det_checkpoint_vars(self): test_graph_detection = tf.Graph() with test_graph_detection.as_default(): model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=6, num_classes=42) inputs_shape = (2, 20, 20, 3) inputs = tf.to_float( tf.random_uniform(inputs_shape, minval=0, maxval=255, dtype=tf.int32)) preprocessed_inputs, true_image_shapes = model.preprocess(inputs) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) model.postprocess(prediction_dict, true_image_shapes) another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable var_map = model.restore_map( fine_tune_checkpoint_type='detection', load_all_detection_checkpoint_vars=True) self.assertIsInstance(var_map, dict) self.assertIn('another_variable', var_map) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test_lib.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.meta_architectures.ssd_meta_arch.""" from absl.testing import parameterized import numpy as np import tensorflow as tf from object_detection.meta_architectures import ssd_meta_arch from object_detection.meta_architectures import ssd_meta_arch_test_lib from object_detection.protos import model_pb2 from object_detection.utils import test_utils slim = tf.contrib.slim keras = tf.keras.layers @parameterized.parameters( {'use_keras': False}, {'use_keras': True}, ) class SsdMetaArchTest(ssd_meta_arch_test_lib.SSDMetaArchTestBase, parameterized.TestCase): def _create_model( self, apply_hard_mining=True, normalize_loc_loss_by_codesize=False, add_background_class=True, random_example_sampling=False, expected_loss_weights=model_pb2.DetectionModel().ssd.loss.NONE, min_num_negative_samples=1, desired_negative_sampling_ratio=3, use_keras=False, predict_mask=False, use_static_shapes=False, nms_max_size_per_class=5): return super(SsdMetaArchTest, self)._create_model( model_fn=ssd_meta_arch.SSDMetaArch, apply_hard_mining=apply_hard_mining, normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize, add_background_class=add_background_class, random_example_sampling=random_example_sampling, expected_loss_weights=expected_loss_weights, min_num_negative_samples=min_num_negative_samples, desired_negative_sampling_ratio=desired_negative_sampling_ratio, use_keras=use_keras, predict_mask=predict_mask, use_static_shapes=use_static_shapes, nms_max_size_per_class=nms_max_size_per_class) def test_preprocess_preserves_shapes_with_dynamic_input_image( self, use_keras): image_shapes = [(3, None, None, 3), (None, 10, 10, 3), (None, None, None, 3)] model, _, _, _ = self._create_model(use_keras=use_keras) for image_shape in image_shapes: image_placeholder = tf.placeholder(tf.float32, shape=image_shape) preprocessed_inputs, _ = model.preprocess(image_placeholder) self.assertAllEqual(preprocessed_inputs.shape.as_list(), image_shape) def test_preprocess_preserves_shape_with_static_input_image(self, use_keras): def graph_fn(input_image): model, _, _, _ = self._create_model(use_keras=use_keras) return model.preprocess(input_image) input_image = np.random.rand(2, 3, 3, 3).astype(np.float32) preprocessed_inputs, _ = self.execute(graph_fn, [input_image]) self.assertAllEqual(preprocessed_inputs.shape, [2, 3, 3, 3]) def test_predict_result_shapes_on_image_with_dynamic_shape(self, use_keras): batch_size = 3 image_size = 2 input_shapes = [(None, image_size, image_size, 3), (batch_size, None, None, 3), (None, None, None, 3)] for input_shape in input_shapes: tf_graph = tf.Graph() with tf_graph.as_default(): model, num_classes, num_anchors, code_size = self._create_model( use_keras=use_keras) preprocessed_input_placeholder = tf.placeholder(tf.float32, shape=input_shape) prediction_dict = model.predict( preprocessed_input_placeholder, true_image_shapes=None) self.assertIn('box_encodings', prediction_dict) self.assertIn('class_predictions_with_background', prediction_dict) self.assertIn('feature_maps', prediction_dict) self.assertIn('anchors', prediction_dict) init_op = tf.global_variables_initializer() with self.test_session(graph=tf_graph) as sess: sess.run(init_op) prediction_out = sess.run(prediction_dict, feed_dict={ preprocessed_input_placeholder: np.random.uniform( size=(batch_size, 2, 2, 3))}) expected_box_encodings_shape_out = (batch_size, num_anchors, code_size) expected_class_predictions_with_background_shape_out = (batch_size, num_anchors, num_classes + 1) self.assertAllEqual(prediction_out['box_encodings'].shape, expected_box_encodings_shape_out) self.assertAllEqual( prediction_out['class_predictions_with_background'].shape, expected_class_predictions_with_background_shape_out) def test_predict_result_shapes_on_image_with_static_shape(self, use_keras): with tf.Graph().as_default(): _, num_classes, num_anchors, code_size = self._create_model( use_keras=use_keras) def graph_fn(input_image): model, _, _, _ = self._create_model() predictions = model.predict(input_image, true_image_shapes=None) return (predictions['box_encodings'], predictions['class_predictions_with_background'], predictions['feature_maps'], predictions['anchors']) batch_size = 3 image_size = 2 channels = 3 input_image = np.random.rand(batch_size, image_size, image_size, channels).astype(np.float32) expected_box_encodings_shape = (batch_size, num_anchors, code_size) expected_class_predictions_shape = (batch_size, num_anchors, num_classes+1) (box_encodings, class_predictions, _, _) = self.execute(graph_fn, [input_image]) self.assertAllEqual(box_encodings.shape, expected_box_encodings_shape) self.assertAllEqual(class_predictions.shape, expected_class_predictions_shape) def test_postprocess_results_are_correct(self, use_keras): batch_size = 2 image_size = 2 input_shapes = [(batch_size, image_size, image_size, 3), (None, image_size, image_size, 3), (batch_size, None, None, 3), (None, None, None, 3)] expected_boxes = [ [ [0, 0, .5, .5], [0, .5, .5, 1], [.5, 0, 1, .5], [0, 0, 0, 0], # pruned prediction [0, 0, 0, 0] ], # padding [ [0, 0, .5, .5], [0, .5, .5, 1], [.5, 0, 1, .5], [0, 0, 0, 0], # pruned prediction [0, 0, 0, 0] ] ] # padding expected_scores = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] expected_classes = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] expected_num_detections = np.array([3, 3]) for input_shape in input_shapes: tf_graph = tf.Graph() with tf_graph.as_default(): model, _, _, _ = self._create_model(use_keras=use_keras) input_placeholder = tf.placeholder(tf.float32, shape=input_shape) preprocessed_inputs, true_image_shapes = model.preprocess( input_placeholder) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) detections = model.postprocess(prediction_dict, true_image_shapes) self.assertIn('detection_boxes', detections) self.assertIn('detection_scores', detections) self.assertIn('detection_classes', detections) self.assertIn('num_detections', detections) init_op = tf.global_variables_initializer() with self.test_session(graph=tf_graph) as sess: sess.run(init_op) detections_out = sess.run(detections, feed_dict={ input_placeholder: np.random.uniform( size=(batch_size, 2, 2, 3))}) for image_idx in range(batch_size): self.assertTrue( test_utils.first_rows_close_as_set( detections_out['detection_boxes'][image_idx].tolist(), expected_boxes[image_idx])) self.assertAllClose(detections_out['detection_scores'], expected_scores) self.assertAllClose(detections_out['detection_classes'], expected_classes) self.assertAllClose(detections_out['num_detections'], expected_num_detections) def test_loss_results_are_correct(self, use_keras): with tf.Graph().as_default(): _, num_classes, num_anchors, _ = self._create_model(use_keras=use_keras) def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2): groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] model, _, _, _ = self._create_model(apply_hard_mining=False) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list) prediction_dict = model.predict(preprocessed_tensor, true_image_shapes=None) loss_dict = model.loss(prediction_dict, true_image_shapes=None) return (self._get_value_for_matching_key(loss_dict, 'Loss/localization_loss'), self._get_value_for_matching_key(loss_dict, 'Loss/classification_loss')) batch_size = 2 preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_classes1 = np.array([[1]], dtype=np.float32) groundtruth_classes2 = np.array([[1]], dtype=np.float32) expected_localization_loss = 0.0 expected_classification_loss = (batch_size * num_anchors * (num_classes+1) * np.log(2.0)) (localization_loss, classification_loss) = self.execute(graph_fn, [preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2]) self.assertAllClose(localization_loss, expected_localization_loss) self.assertAllClose(classification_loss, expected_classification_loss) def test_loss_results_are_correct_with_normalize_by_codesize_true( self, use_keras): with tf.Graph().as_default(): _, _, _, _ = self._create_model(use_keras=use_keras) def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2): groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] model, _, _, _ = self._create_model(apply_hard_mining=False, normalize_loc_loss_by_codesize=True, use_keras=use_keras) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list) prediction_dict = model.predict(preprocessed_tensor, true_image_shapes=None) loss_dict = model.loss(prediction_dict, true_image_shapes=None) return (self._get_value_for_matching_key(loss_dict, 'Loss/localization_loss'),) batch_size = 2 preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) groundtruth_boxes1 = np.array([[0, 0, 1, 1]], dtype=np.float32) groundtruth_boxes2 = np.array([[0, 0, 1, 1]], dtype=np.float32) groundtruth_classes1 = np.array([[1]], dtype=np.float32) groundtruth_classes2 = np.array([[1]], dtype=np.float32) expected_localization_loss = 0.5 / 4 localization_loss = self.execute(graph_fn, [preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2]) self.assertAllClose(localization_loss, expected_localization_loss) def test_loss_results_are_correct_with_hard_example_mining(self, use_keras): with tf.Graph().as_default(): _, num_classes, num_anchors, _ = self._create_model(use_keras=use_keras) def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2): groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] model, _, _, _ = self._create_model() model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list) prediction_dict = model.predict(preprocessed_tensor, true_image_shapes=None) loss_dict = model.loss(prediction_dict, true_image_shapes=None) return (self._get_value_for_matching_key(loss_dict, 'Loss/localization_loss'), self._get_value_for_matching_key(loss_dict, 'Loss/classification_loss')) batch_size = 2 preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_classes1 = np.array([[1]], dtype=np.float32) groundtruth_classes2 = np.array([[1]], dtype=np.float32) expected_localization_loss = 0.0 expected_classification_loss = (batch_size * num_anchors * (num_classes+1) * np.log(2.0)) (localization_loss, classification_loss) = self.execute_cpu( graph_fn, [ preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2 ]) self.assertAllClose(localization_loss, expected_localization_loss) self.assertAllClose(classification_loss, expected_classification_loss) def test_loss_results_are_correct_without_add_background_class( self, use_keras): with tf.Graph().as_default(): _, num_classes, num_anchors, _ = self._create_model( add_background_class=False, use_keras=use_keras) def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2): groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] model, _, _, _ = self._create_model( apply_hard_mining=False, add_background_class=False, use_keras=use_keras) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list) prediction_dict = model.predict( preprocessed_tensor, true_image_shapes=None) loss_dict = model.loss(prediction_dict, true_image_shapes=None) return (loss_dict['Loss/localization_loss'], loss_dict['Loss/classification_loss']) batch_size = 2 preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_classes1 = np.array([[1]], dtype=np.float32) groundtruth_classes2 = np.array([[1]], dtype=np.float32) expected_localization_loss = 0.0 expected_classification_loss = ( batch_size * num_anchors * num_classes * np.log(2.0)) (localization_loss, classification_loss) = self.execute( graph_fn, [ preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2 ]) self.assertAllClose(localization_loss, expected_localization_loss) self.assertAllClose(classification_loss, expected_classification_loss) def test_loss_results_are_correct_with_losses_mask(self, use_keras): with tf.Graph().as_default(): _, num_classes, num_anchors, _ = self._create_model(use_keras=use_keras) def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, groundtruth_boxes3, groundtruth_classes1, groundtruth_classes2, groundtruth_classes3): groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2, groundtruth_boxes3] groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2, groundtruth_classes3] is_annotated_list = [tf.constant(True), tf.constant(True), tf.constant(False)] model, _, _, _ = self._create_model(apply_hard_mining=False) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list, is_annotated_list=is_annotated_list) prediction_dict = model.predict(preprocessed_tensor, true_image_shapes=None) loss_dict = model.loss(prediction_dict, true_image_shapes=None) return (self._get_value_for_matching_key(loss_dict, 'Loss/localization_loss'), self._get_value_for_matching_key(loss_dict, 'Loss/classification_loss')) batch_size = 3 preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_boxes3 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_classes1 = np.array([[1]], dtype=np.float32) groundtruth_classes2 = np.array([[1]], dtype=np.float32) groundtruth_classes3 = np.array([[1]], dtype=np.float32) expected_localization_loss = 0.0 # Note that we are subtracting 1 from batch_size, since the final image is # not annotated. expected_classification_loss = ((batch_size - 1) * num_anchors * (num_classes+1) * np.log(2.0)) (localization_loss, classification_loss) = self.execute(graph_fn, [preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, groundtruth_boxes3, groundtruth_classes1, groundtruth_classes2, groundtruth_classes3]) self.assertAllClose(localization_loss, expected_localization_loss) self.assertAllClose(classification_loss, expected_classification_loss) def test_restore_map_for_detection_ckpt(self, use_keras): model, _, _, _ = self._create_model(use_keras=use_keras) model.predict(tf.constant(np.array([[[[0, 0], [1, 1]], [[1, 0], [0, 1]]]], dtype=np.float32)), true_image_shapes=None) init_op = tf.global_variables_initializer() saver = tf.train.Saver() save_path = self.get_temp_dir() with self.test_session() as sess: sess.run(init_op) saved_model_path = saver.save(sess, save_path) var_map = model.restore_map( fine_tune_checkpoint_type='detection', load_all_detection_checkpoint_vars=False) self.assertIsInstance(var_map, dict) saver = tf.train.Saver(var_map) saver.restore(sess, saved_model_path) for var in sess.run(tf.report_uninitialized_variables()): self.assertNotIn('FeatureExtractor', var) def test_restore_map_for_classification_ckpt(self, use_keras): # Define mock tensorflow classification graph and save variables. test_graph_classification = tf.Graph() with test_graph_classification.as_default(): image = tf.placeholder(dtype=tf.float32, shape=[1, 20, 20, 3]) if use_keras: with tf.name_scope('mock_model'): layer_one = keras.Conv2D(32, kernel_size=1, name='layer1') net = layer_one(image) layer_two = keras.Conv2D(3, kernel_size=1, name='layer2') layer_two(net) else: with tf.variable_scope('mock_model'): net = slim.conv2d(image, num_outputs=32, kernel_size=1, scope='layer1') slim.conv2d(net, num_outputs=3, kernel_size=1, scope='layer2') init_op = tf.global_variables_initializer() saver = tf.train.Saver() save_path = self.get_temp_dir() with self.test_session(graph=test_graph_classification) as sess: sess.run(init_op) saved_model_path = saver.save(sess, save_path) # Create tensorflow detection graph and load variables from # classification checkpoint. test_graph_detection = tf.Graph() with test_graph_detection.as_default(): model, _, _, _ = self._create_model(use_keras=use_keras) inputs_shape = [2, 2, 2, 3] inputs = tf.to_float(tf.random_uniform( inputs_shape, minval=0, maxval=255, dtype=tf.int32)) preprocessed_inputs, true_image_shapes = model.preprocess(inputs) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) model.postprocess(prediction_dict, true_image_shapes) another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable var_map = model.restore_map(fine_tune_checkpoint_type='classification') self.assertNotIn('another_variable', var_map) self.assertIsInstance(var_map, dict) saver = tf.train.Saver(var_map) with self.test_session(graph=test_graph_detection) as sess: saver.restore(sess, saved_model_path) for var in sess.run(tf.report_uninitialized_variables()): self.assertNotIn('FeatureExtractor', var) def test_load_all_det_checkpoint_vars(self, use_keras): test_graph_detection = tf.Graph() with test_graph_detection.as_default(): model, _, _, _ = self._create_model(use_keras=use_keras) inputs_shape = [2, 2, 2, 3] inputs = tf.to_float( tf.random_uniform(inputs_shape, minval=0, maxval=255, dtype=tf.int32)) preprocessed_inputs, true_image_shapes = model.preprocess(inputs) prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) model.postprocess(prediction_dict, true_image_shapes) another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable var_map = model.restore_map( fine_tune_checkpoint_type='detection', load_all_detection_checkpoint_vars=True) self.assertIsInstance(var_map, dict) self.assertIn('another_variable', var_map) def test_loss_results_are_correct_with_random_example_sampling( self, use_keras): with tf.Graph().as_default(): _, num_classes, _, _ = self._create_model( random_example_sampling=True, use_keras=use_keras) def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2): groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] model, _, _, _ = self._create_model(random_example_sampling=True, use_keras=use_keras) model.provide_groundtruth(groundtruth_boxes_list, groundtruth_classes_list) prediction_dict = model.predict( preprocessed_tensor, true_image_shapes=None) loss_dict = model.loss(prediction_dict, true_image_shapes=None) return (self._get_value_for_matching_key(loss_dict, 'Loss/localization_loss'), self._get_value_for_matching_key(loss_dict, 'Loss/classification_loss')) batch_size = 2 preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) groundtruth_classes1 = np.array([[1]], dtype=np.float32) groundtruth_classes2 = np.array([[1]], dtype=np.float32) expected_localization_loss = 0.0 # Among 4 anchors (1 positive, 3 negative) in this test, only 2 anchors are # selected (1 positive, 1 negative) since random sampler will adjust number # of negative examples to make sure positive example fraction in the batch # is 0.5. expected_classification_loss = ( batch_size * 2 * (num_classes + 1) * np.log(2.0)) (localization_loss, classification_loss) = self.execute_cpu( graph_fn, [ preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, groundtruth_classes1, groundtruth_classes2 ]) self.assertAllClose(localization_loss, expected_localization_loss) self.assertAllClose(classification_loss, expected_classification_loss) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/ssd_meta_arch_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.meta_architectures.faster_rcnn_meta_arch.""" from absl.testing import parameterized import numpy as np import tensorflow as tf from object_detection.meta_architectures import faster_rcnn_meta_arch_test_lib class FasterRCNNMetaArchTest( faster_rcnn_meta_arch_test_lib.FasterRCNNMetaArchTestBase, parameterized.TestCase): def test_postprocess_second_stage_only_inference_mode_with_masks(self): model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=6) batch_size = 2 total_num_padded_proposals = batch_size * model.max_num_proposals proposal_boxes = tf.constant( [[[1, 1, 2, 3], [0, 0, 1, 1], [.5, .5, .6, .6], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], [[2, 3, 6, 8], [1, 2, 5, 3], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]], dtype=tf.float32) num_proposals = tf.constant([3, 2], dtype=tf.int32) refined_box_encodings = tf.zeros( [total_num_padded_proposals, model.num_classes, 4], dtype=tf.float32) class_predictions_with_background = tf.ones( [total_num_padded_proposals, model.num_classes+1], dtype=tf.float32) image_shape = tf.constant([batch_size, 36, 48, 3], dtype=tf.int32) mask_height = 2 mask_width = 2 mask_predictions = 30. * tf.ones( [total_num_padded_proposals, model.num_classes, mask_height, mask_width], dtype=tf.float32) exp_detection_masks = np.array([[[[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]]], [[[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]], [[0, 0], [0, 0]]]]) _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) detections = model.postprocess({ 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'num_proposals': num_proposals, 'proposal_boxes': proposal_boxes, 'image_shape': image_shape, 'mask_predictions': mask_predictions }, true_image_shapes) with self.test_session() as sess: detections_out = sess.run(detections) self.assertAllEqual(detections_out['detection_boxes'].shape, [2, 5, 4]) self.assertAllClose(detections_out['detection_scores'], [[1, 1, 1, 1, 1], [1, 1, 1, 1, 0]]) self.assertAllClose(detections_out['detection_classes'], [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]]) self.assertAllClose(detections_out['num_detections'], [5, 4]) self.assertAllClose(detections_out['detection_masks'], exp_detection_masks) self.assertTrue(np.amax(detections_out['detection_masks'] <= 1.0)) self.assertTrue(np.amin(detections_out['detection_masks'] >= 0.0)) def test_postprocess_second_stage_only_inference_mode_with_shared_boxes(self): model = self._build_model( is_training=False, number_of_stages=2, second_stage_batch_size=6) batch_size = 2 total_num_padded_proposals = batch_size * model.max_num_proposals proposal_boxes = tf.constant( [[[1, 1, 2, 3], [0, 0, 1, 1], [.5, .5, .6, .6], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], [[2, 3, 6, 8], [1, 2, 5, 3], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]], dtype=tf.float32) num_proposals = tf.constant([3, 2], dtype=tf.int32) # This has 1 box instead of one for each class. refined_box_encodings = tf.zeros( [total_num_padded_proposals, 1, 4], dtype=tf.float32) class_predictions_with_background = tf.ones( [total_num_padded_proposals, model.num_classes+1], dtype=tf.float32) image_shape = tf.constant([batch_size, 36, 48, 3], dtype=tf.int32) _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) detections = model.postprocess({ 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'num_proposals': num_proposals, 'proposal_boxes': proposal_boxes, 'image_shape': image_shape, }, true_image_shapes) with self.test_session() as sess: detections_out = sess.run(detections) self.assertAllEqual(detections_out['detection_boxes'].shape, [2, 5, 4]) self.assertAllClose(detections_out['detection_scores'], [[1, 1, 1, 1, 1], [1, 1, 1, 1, 0]]) self.assertAllClose(detections_out['detection_classes'], [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]]) self.assertAllClose(detections_out['num_detections'], [5, 4]) @parameterized.parameters( {'masks_are_class_agnostic': False}, {'masks_are_class_agnostic': True}, ) def test_predict_correct_shapes_in_inference_mode_three_stages_with_masks( self, masks_are_class_agnostic): batch_size = 2 image_size = 10 max_num_proposals = 8 initial_crop_size = 3 maxpool_stride = 1 input_shapes = [(batch_size, image_size, image_size, 3), (None, image_size, image_size, 3), (batch_size, None, None, 3), (None, None, None, 3)] expected_num_anchors = image_size * image_size * 3 * 3 expected_shapes = { 'rpn_box_predictor_features': (2, image_size, image_size, 512), 'rpn_features_to_crop': (2, image_size, image_size, 3), 'image_shape': (4,), 'rpn_box_encodings': (2, expected_num_anchors, 4), 'rpn_objectness_predictions_with_background': (2, expected_num_anchors, 2), 'anchors': (expected_num_anchors, 4), 'refined_box_encodings': (2 * max_num_proposals, 2, 4), 'class_predictions_with_background': (2 * max_num_proposals, 2 + 1), 'num_proposals': (2,), 'proposal_boxes': (2, max_num_proposals, 4), 'proposal_boxes_normalized': (2, max_num_proposals, 4), 'box_classifier_features': self._get_box_classifier_features_shape(image_size, batch_size, max_num_proposals, initial_crop_size, maxpool_stride, 3) } for input_shape in input_shapes: test_graph = tf.Graph() with test_graph.as_default(): model = self._build_model( is_training=False, number_of_stages=3, second_stage_batch_size=2, predict_masks=True, masks_are_class_agnostic=masks_are_class_agnostic) preprocessed_inputs = tf.placeholder(tf.float32, shape=input_shape) _, true_image_shapes = model.preprocess(preprocessed_inputs) result_tensor_dict = model.predict(preprocessed_inputs, true_image_shapes) init_op = tf.global_variables_initializer() with self.test_session(graph=test_graph) as sess: sess.run(init_op) tensor_dict_out = sess.run(result_tensor_dict, feed_dict={ preprocessed_inputs: np.zeros((batch_size, image_size, image_size, 3))}) self.assertEqual( set(tensor_dict_out.keys()), set(expected_shapes.keys()).union( set([ 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks', 'num_detections', 'mask_predictions', ]))) for key in expected_shapes: self.assertAllEqual(tensor_dict_out[key].shape, expected_shapes[key]) self.assertAllEqual(tensor_dict_out['detection_boxes'].shape, [2, 5, 4]) self.assertAllEqual(tensor_dict_out['detection_masks'].shape, [2, 5, 14, 14]) self.assertAllEqual(tensor_dict_out['detection_classes'].shape, [2, 5]) self.assertAllEqual(tensor_dict_out['detection_scores'].shape, [2, 5]) self.assertAllEqual(tensor_dict_out['num_detections'].shape, [2]) num_classes = 1 if masks_are_class_agnostic else 2 self.assertAllEqual(tensor_dict_out['mask_predictions'].shape, [10, num_classes, 14, 14]) @parameterized.parameters( {'masks_are_class_agnostic': False}, {'masks_are_class_agnostic': True}, ) def test_predict_gives_correct_shapes_in_train_mode_both_stages_with_masks( self, masks_are_class_agnostic): test_graph = tf.Graph() with test_graph.as_default(): model = self._build_model( is_training=True, number_of_stages=3, second_stage_batch_size=7, predict_masks=True, masks_are_class_agnostic=masks_are_class_agnostic) batch_size = 2 image_size = 10 max_num_proposals = 7 initial_crop_size = 3 maxpool_stride = 1 image_shape = (batch_size, image_size, image_size, 3) preprocessed_inputs = tf.zeros(image_shape, dtype=tf.float32) groundtruth_boxes_list = [ tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32) ] groundtruth_classes_list = [ tf.constant([[1, 0], [0, 1]], dtype=tf.float32), tf.constant([[1, 0], [1, 0]], dtype=tf.float32) ] groundtruth_weights_list = [ tf.constant([1, 1], dtype=tf.float32), tf.constant([1, 1], dtype=tf.float32)] _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) model.provide_groundtruth( groundtruth_boxes_list, groundtruth_classes_list, groundtruth_weights_list=groundtruth_weights_list) result_tensor_dict = model.predict(preprocessed_inputs, true_image_shapes) mask_shape_1 = 1 if masks_are_class_agnostic else model._num_classes expected_shapes = { 'rpn_box_predictor_features': (2, image_size, image_size, 512), 'rpn_features_to_crop': (2, image_size, image_size, 3), 'image_shape': (4,), 'refined_box_encodings': (2 * max_num_proposals, 2, 4), 'class_predictions_with_background': (2 * max_num_proposals, 2 + 1), 'num_proposals': (2,), 'proposal_boxes': (2, max_num_proposals, 4), 'proposal_boxes_normalized': (2, max_num_proposals, 4), 'box_classifier_features': self._get_box_classifier_features_shape( image_size, batch_size, max_num_proposals, initial_crop_size, maxpool_stride, 3), 'mask_predictions': (2 * max_num_proposals, mask_shape_1, 14, 14) } init_op = tf.global_variables_initializer() with self.test_session(graph=test_graph) as sess: sess.run(init_op) tensor_dict_out = sess.run(result_tensor_dict) self.assertEqual( set(tensor_dict_out.keys()), set(expected_shapes.keys()).union( set([ 'rpn_box_encodings', 'rpn_objectness_predictions_with_background', 'anchors', ]))) for key in expected_shapes: self.assertAllEqual(tensor_dict_out[key].shape, expected_shapes[key]) anchors_shape_out = tensor_dict_out['anchors'].shape self.assertEqual(2, len(anchors_shape_out)) self.assertEqual(4, anchors_shape_out[1]) num_anchors_out = anchors_shape_out[0] self.assertAllEqual(tensor_dict_out['rpn_box_encodings'].shape, (2, num_anchors_out, 4)) self.assertAllEqual( tensor_dict_out['rpn_objectness_predictions_with_background'].shape, (2, num_anchors_out, 2)) def test_postprocess_third_stage_only_inference_mode(self): num_proposals_shapes = [(2), (None)] refined_box_encodings_shapes = [(16, 2, 4), (None, 2, 4)] class_predictions_with_background_shapes = [(16, 3), (None, 3)] proposal_boxes_shapes = [(2, 8, 4), (None, 8, 4)] batch_size = 2 image_shape = np.array((2, 36, 48, 3), dtype=np.int32) for (num_proposals_shape, refined_box_encoding_shape, class_predictions_with_background_shape, proposal_boxes_shape) in zip(num_proposals_shapes, refined_box_encodings_shapes, class_predictions_with_background_shapes, proposal_boxes_shapes): tf_graph = tf.Graph() with tf_graph.as_default(): model = self._build_model( is_training=False, number_of_stages=3, second_stage_batch_size=6, predict_masks=True) total_num_padded_proposals = batch_size * model.max_num_proposals proposal_boxes = np.array( [[[1, 1, 2, 3], [0, 0, 1, 1], [.5, .5, .6, .6], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], [[2, 3, 6, 8], [1, 2, 5, 3], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]]) num_proposals = np.array([3, 2], dtype=np.int32) refined_box_encodings = np.zeros( [total_num_padded_proposals, model.num_classes, 4]) class_predictions_with_background = np.ones( [total_num_padded_proposals, model.num_classes+1]) num_proposals_placeholder = tf.placeholder(tf.int32, shape=num_proposals_shape) refined_box_encodings_placeholder = tf.placeholder( tf.float32, shape=refined_box_encoding_shape) class_predictions_with_background_placeholder = tf.placeholder( tf.float32, shape=class_predictions_with_background_shape) proposal_boxes_placeholder = tf.placeholder( tf.float32, shape=proposal_boxes_shape) image_shape_placeholder = tf.placeholder(tf.int32, shape=(4)) _, true_image_shapes = model.preprocess( tf.zeros(image_shape_placeholder)) detections = model.postprocess({ 'refined_box_encodings': refined_box_encodings_placeholder, 'class_predictions_with_background': class_predictions_with_background_placeholder, 'num_proposals': num_proposals_placeholder, 'proposal_boxes': proposal_boxes_placeholder, 'image_shape': image_shape_placeholder, 'detection_boxes': tf.zeros([2, 5, 4]), 'detection_masks': tf.zeros([2, 5, 14, 14]), 'detection_scores': tf.zeros([2, 5]), 'detection_classes': tf.zeros([2, 5]), 'num_detections': tf.zeros([2]), }, true_image_shapes) with self.test_session(graph=tf_graph) as sess: detections_out = sess.run( detections, feed_dict={ refined_box_encodings_placeholder: refined_box_encodings, class_predictions_with_background_placeholder: class_predictions_with_background, num_proposals_placeholder: num_proposals, proposal_boxes_placeholder: proposal_boxes, image_shape_placeholder: image_shape }) self.assertAllEqual(detections_out['detection_boxes'].shape, [2, 5, 4]) self.assertAllEqual(detections_out['detection_masks'].shape, [2, 5, 14, 14]) self.assertAllClose(detections_out['detection_scores'].shape, [2, 5]) self.assertAllClose(detections_out['detection_classes'].shape, [2, 5]) self.assertAllClose(detections_out['num_detections'].shape, [2]) self.assertTrue(np.amax(detections_out['detection_masks'] <= 1.0)) self.assertTrue(np.amin(detections_out['detection_masks'] >= 0.0)) def _get_box_classifier_features_shape(self, image_size, batch_size, max_num_proposals, initial_crop_size, maxpool_stride, num_features): return (batch_size * max_num_proposals, initial_crop_size/maxpool_stride, initial_crop_size/maxpool_stride, num_features) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """R-FCN meta-architecture definition. R-FCN: Dai, Jifeng, et al. "R-FCN: Object Detection via Region-based Fully Convolutional Networks." arXiv preprint arXiv:1605.06409 (2016). The R-FCN meta architecture is similar to Faster R-CNN and only differs in the second stage. Hence this class inherits FasterRCNNMetaArch and overrides only the `_predict_second_stage` method. Similar to Faster R-CNN we allow for two modes: number_of_stages=1 and number_of_stages=2. In the former setting, all of the user facing methods (e.g., predict, postprocess, loss) can be used as if the model consisted only of the RPN, returning class agnostic proposals (these can be thought of as approximate detections with no associated class information). In the latter setting, proposals are computed, then passed through a second stage "box classifier" to yield (multi-class) detections. Implementations of R-FCN models must define a new FasterRCNNFeatureExtractor and override three methods: `preprocess`, `_extract_proposal_features` (the first stage of the model), and `_extract_box_classifier_features` (the second stage of the model). Optionally, the `restore_fn` method can be overridden. See tests for an example. See notes in the documentation of Faster R-CNN meta-architecture as they all apply here. """ import tensorflow as tf from object_detection.core import box_predictor from object_detection.meta_architectures import faster_rcnn_meta_arch from object_detection.utils import ops class RFCNMetaArch(faster_rcnn_meta_arch.FasterRCNNMetaArch): """R-FCN Meta-architecture definition.""" def __init__(self, is_training, num_classes, image_resizer_fn, feature_extractor, number_of_stages, first_stage_anchor_generator, first_stage_target_assigner, first_stage_atrous_rate, first_stage_box_predictor_arg_scope_fn, first_stage_box_predictor_kernel_size, first_stage_box_predictor_depth, first_stage_minibatch_size, first_stage_sampler, first_stage_non_max_suppression_fn, first_stage_max_proposals, first_stage_localization_loss_weight, first_stage_objectness_loss_weight, crop_and_resize_fn, second_stage_target_assigner, second_stage_rfcn_box_predictor, second_stage_batch_size, second_stage_sampler, second_stage_non_max_suppression_fn, second_stage_score_conversion_fn, second_stage_localization_loss_weight, second_stage_classification_loss_weight, second_stage_classification_loss, hard_example_miner, parallel_iterations=16, add_summaries=True, clip_anchors_to_image=False, use_static_shapes=False, resize_masks=False): """RFCNMetaArch Constructor. Args: is_training: A boolean indicating whether the training version of the computation graph should be constructed. num_classes: Number of classes. Note that num_classes *does not* include the background category, so if groundtruth labels take values in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the assigned classification targets can range from {0,... K}). image_resizer_fn: A callable for image resizing. This callable always takes a rank-3 image tensor (corresponding to a single image) and returns a rank-3 image tensor, possibly with new spatial dimensions. See builders/image_resizer_builder.py. feature_extractor: A FasterRCNNFeatureExtractor object. number_of_stages: Valid values are {1, 2}. If 1 will only construct the Region Proposal Network (RPN) part of the model. first_stage_anchor_generator: An anchor_generator.AnchorGenerator object (note that currently we only support grid_anchor_generator.GridAnchorGenerator objects) first_stage_target_assigner: Target assigner to use for first stage of R-FCN (RPN). first_stage_atrous_rate: A single integer indicating the atrous rate for the single convolution op which is applied to the `rpn_features_to_crop` tensor to obtain a tensor to be used for box prediction. Some feature extractors optionally allow for producing feature maps computed at denser resolutions. The atrous rate is used to compensate for the denser feature maps by using an effectively larger receptive field. (This should typically be set to 1). first_stage_box_predictor_arg_scope_fn: A function to generate tf-slim arg_scope for conv2d, separable_conv2d and fully_connected ops for the RPN box predictor. first_stage_box_predictor_kernel_size: Kernel size to use for the convolution op just prior to RPN box predictions. first_stage_box_predictor_depth: Output depth for the convolution op just prior to RPN box predictions. first_stage_minibatch_size: The "batch size" to use for computing the objectness and location loss of the region proposal network. This "batch size" refers to the number of anchors selected as contributing to the loss function for any given image within the image batch and is only called "batch_size" due to terminology from the Faster R-CNN paper. first_stage_sampler: The sampler for the boxes used to calculate the RPN loss after the first stage. first_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression callable that takes `boxes`, `scores` and optional `clip_window`(with all other inputs already set) and returns a dictionary containing tensors with keys: `detection_boxes`, `detection_scores`, `detection_classes`, `num_detections`. This is used to perform non max suppression on the boxes predicted by the Region Proposal Network (RPN). See `post_processing.batch_multiclass_non_max_suppression` for the type and shape of these tensors. first_stage_max_proposals: Maximum number of boxes to retain after performing Non-Max Suppression (NMS) on the boxes predicted by the Region Proposal Network (RPN). first_stage_localization_loss_weight: A float first_stage_objectness_loss_weight: A float crop_and_resize_fn: A differentiable resampler to use for cropping RPN proposal features. second_stage_target_assigner: Target assigner to use for second stage of R-FCN. If the model is configured with multiple prediction heads, this target assigner is used to generate targets for all heads (with the correct `unmatched_class_label`). second_stage_rfcn_box_predictor: RFCN box predictor to use for second stage. second_stage_batch_size: The batch size used for computing the classification and refined location loss of the box classifier. This "batch size" refers to the number of proposals selected as contributing to the loss function for any given image within the image batch and is only called "batch_size" due to terminology from the Faster R-CNN paper. second_stage_sampler: The sampler for the boxes used for second stage box classifier. second_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression callable that takes `boxes`, `scores`, optional `clip_window` and optional (kwarg) `mask` inputs (with all other inputs already set) and returns a dictionary containing tensors with keys: `detection_boxes`, `detection_scores`, `detection_classes`, `num_detections`, and (optionally) `detection_masks`. See `post_processing.batch_multiclass_non_max_suppression` for the type and shape of these tensors. second_stage_score_conversion_fn: Callable elementwise nonlinearity (that takes tensors as inputs and returns tensors). This is usually used to convert logits to probabilities. second_stage_localization_loss_weight: A float second_stage_classification_loss_weight: A float second_stage_classification_loss: A string indicating which loss function to use, supports 'softmax' and 'sigmoid'. hard_example_miner: A losses.HardExampleMiner object (can be None). parallel_iterations: (Optional) The number of iterations allowed to run in parallel for calls to tf.map_fn. add_summaries: boolean (default: True) controlling whether summary ops should be added to tensorflow graph. clip_anchors_to_image: The anchors generated are clip to the window size without filtering the nonoverlapping anchors. This generates a static number of anchors. This argument is unused. use_static_shapes: If True, uses implementation of ops with static shape guarantees. resize_masks: Indicates whether the masks presend in the groundtruth should be resized in the model with `image_resizer_fn` Raises: ValueError: If `second_stage_batch_size` > `first_stage_max_proposals` ValueError: If first_stage_anchor_generator is not of type grid_anchor_generator.GridAnchorGenerator. """ # TODO(rathodv): add_summaries and crop_and_resize_fn is currently # unused. Respect that directive in the future. super(RFCNMetaArch, self).__init__( is_training, num_classes, image_resizer_fn, feature_extractor, number_of_stages, first_stage_anchor_generator, first_stage_target_assigner, first_stage_atrous_rate, first_stage_box_predictor_arg_scope_fn, first_stage_box_predictor_kernel_size, first_stage_box_predictor_depth, first_stage_minibatch_size, first_stage_sampler, first_stage_non_max_suppression_fn, first_stage_max_proposals, first_stage_localization_loss_weight, first_stage_objectness_loss_weight, crop_and_resize_fn, None, # initial_crop_size is not used in R-FCN None, # maxpool_kernel_size is not use in R-FCN None, # maxpool_stride is not use in R-FCN second_stage_target_assigner, None, # fully_connected_box_predictor is not used in R-FCN. second_stage_batch_size, second_stage_sampler, second_stage_non_max_suppression_fn, second_stage_score_conversion_fn, second_stage_localization_loss_weight, second_stage_classification_loss_weight, second_stage_classification_loss, 1.0, # second stage mask prediction loss weight isn't used in R-FCN. hard_example_miner, parallel_iterations, add_summaries, clip_anchors_to_image, use_static_shapes, resize_masks) self._rfcn_box_predictor = second_stage_rfcn_box_predictor def _predict_second_stage(self, rpn_box_encodings, rpn_objectness_predictions_with_background, rpn_features, anchors, image_shape, true_image_shapes): """Predicts the output tensors from 2nd stage of R-FCN. Args: rpn_box_encodings: 3-D float tensor of shape [batch_size, num_valid_anchors, self._box_coder.code_size] containing predicted boxes. rpn_objectness_predictions_with_background: 3-D float tensor of shape [batch_size, num_valid_anchors, 2] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions (at class index 0). rpn_features: A 4-D float32 tensor with shape [batch_size, height, width, depth] representing image features from the RPN. anchors: 2-D float tensor of shape [num_anchors, self._box_coder.code_size]. image_shape: A 1D int32 tensors of size [4] containing the image shape. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Returns: prediction_dict: a dictionary holding "raw" prediction tensors: 1) refined_box_encodings: a 3-D tensor with shape [total_num_proposals, num_classes, 4] representing predicted (final) refined box encodings, where total_num_proposals=batch_size*self._max_num_proposals 2) class_predictions_with_background: a 2-D tensor with shape [total_num_proposals, num_classes + 1] containing class predictions (logits) for each of the anchors, where total_num_proposals=batch_size*self._max_num_proposals. Note that this tensor *includes* background class predictions (at class index 0). 3) num_proposals: An int32 tensor of shape [batch_size] representing the number of proposals generated by the RPN. `num_proposals` allows us to keep track of which entries are to be treated as zero paddings and which are not since we always pad the number of proposals to be `self.max_num_proposals` for each image. 4) proposal_boxes: A float32 tensor of shape [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes (in absolute coordinates). 5) proposal_boxes_normalized: A float32 tensor of shape [batch_size, self.max_num_proposals, 4] representing decoded proposal bounding boxes (in normalized coordinates). Can be used to override the boxes proposed by the RPN, thus enabling one to extract box classification and prediction for externally selected areas of the image. 6) box_classifier_features: a 4-D float32 tensor, of shape [batch_size, feature_map_height, feature_map_width, depth], representing the box classifier features. """ image_shape_2d = tf.tile(tf.expand_dims(image_shape[1:], 0), [image_shape[0], 1]) proposal_boxes_normalized, _, num_proposals = self._postprocess_rpn( rpn_box_encodings, rpn_objectness_predictions_with_background, anchors, image_shape_2d, true_image_shapes) box_classifier_features = ( self._feature_extractor.extract_box_classifier_features( rpn_features, scope=self.second_stage_feature_extractor_scope)) if self._rfcn_box_predictor.is_keras_model: box_predictions = self._rfcn_box_predictor( [box_classifier_features], proposal_boxes=proposal_boxes_normalized) else: box_predictions = self._rfcn_box_predictor.predict( [box_classifier_features], num_predictions_per_location=[1], scope=self.second_stage_box_predictor_scope, proposal_boxes=proposal_boxes_normalized) refined_box_encodings = tf.squeeze( tf.concat(box_predictions[box_predictor.BOX_ENCODINGS], axis=1), axis=1) class_predictions_with_background = tf.squeeze( tf.concat( box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1), axis=1) absolute_proposal_boxes = ops.normalized_to_image_coordinates( proposal_boxes_normalized, image_shape, parallel_iterations=self._parallel_iterations) prediction_dict = { 'refined_box_encodings': refined_box_encodings, 'class_predictions_with_background': class_predictions_with_background, 'num_proposals': num_proposals, 'proposal_boxes': absolute_proposal_boxes, 'box_classifier_features': box_classifier_features, 'proposal_boxes_normalized': proposal_boxes_normalized, } return prediction_dict
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/rfcn_meta_arch.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """SSD Meta-architecture definition. General tensorflow implementation of convolutional Multibox/SSD detection models. """ from abc import abstractmethod import tensorflow as tf from object_detection.core import box_list from object_detection.core import box_list_ops from object_detection.core import model from object_detection.core import standard_fields as fields from object_detection.core import target_assigner from object_detection.utils import ops from object_detection.utils import shape_utils from object_detection.utils import visualization_utils slim = tf.contrib.slim class SSDFeatureExtractor(object): """SSD Slim Feature Extractor definition.""" def __init__(self, is_training, depth_multiplier, min_depth, pad_to_multiple, conv_hyperparams_fn, reuse_weights=None, use_explicit_padding=False, use_depthwise=False, override_base_feature_extractor_hyperparams=False): """Constructor. Args: is_training: whether the network is in training mode. depth_multiplier: float depth multiplier for feature extractor. min_depth: minimum feature extractor depth. pad_to_multiple: the nearest multiple to zero pad the input height and width dimensions to. conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d and separable_conv2d ops in the layers that are added on top of the base feature extractor. reuse_weights: whether to reuse variables. Default is None. use_explicit_padding: Whether to use explicit padding when extracting features. Default is False. use_depthwise: Whether to use depthwise convolutions. Default is False. override_base_feature_extractor_hyperparams: Whether to override hyperparameters of the base feature extractor with the one from `conv_hyperparams_fn`. """ self._is_training = is_training self._depth_multiplier = depth_multiplier self._min_depth = min_depth self._pad_to_multiple = pad_to_multiple self._conv_hyperparams_fn = conv_hyperparams_fn self._reuse_weights = reuse_weights self._use_explicit_padding = use_explicit_padding self._use_depthwise = use_depthwise self._override_base_feature_extractor_hyperparams = ( override_base_feature_extractor_hyperparams) @property def is_keras_model(self): return False @abstractmethod def preprocess(self, resized_inputs): """Preprocesses images for feature extraction (minus image resizing). Args: resized_inputs: a [batch, height, width, channels] float tensor representing a batch of images. Returns: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. """ pass @abstractmethod def extract_features(self, preprocessed_inputs): """Extracts features from preprocessed inputs. This function is responsible for extracting feature maps from preprocessed images. Args: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. Returns: feature_maps: a list of tensors where the ith tensor has shape [batch, height_i, width_i, depth_i] """ raise NotImplementedError def restore_from_classification_checkpoint_fn(self, feature_extractor_scope): """Returns a map of variables to load from a foreign checkpoint. Args: feature_extractor_scope: A scope name for the feature extractor. Returns: A dict mapping variable names (to load from a checkpoint) to variables in the model graph. """ variables_to_restore = {} for variable in tf.global_variables(): var_name = variable.op.name if var_name.startswith(feature_extractor_scope + '/'): var_name = var_name.replace(feature_extractor_scope + '/', '') variables_to_restore[var_name] = variable return variables_to_restore class SSDKerasFeatureExtractor(tf.keras.Model): """SSD Feature Extractor definition.""" def __init__(self, is_training, depth_multiplier, min_depth, pad_to_multiple, conv_hyperparams, freeze_batchnorm, inplace_batchnorm_update, use_explicit_padding=False, use_depthwise=False, override_base_feature_extractor_hyperparams=False, name=None): """Constructor. Args: is_training: whether the network is in training mode. depth_multiplier: float depth multiplier for feature extractor. min_depth: minimum feature extractor depth. pad_to_multiple: the nearest multiple to zero pad the input height and width dimensions to. conv_hyperparams: `hyperparams_builder.KerasLayerHyperparams` object containing convolution hyperparameters for the layers added on top of the base feature extractor. freeze_batchnorm: Whether to freeze batch norm parameters during training or not. When training with a small batch size (e.g. 1), it is desirable to freeze batch norm update and use pretrained batch norm params. inplace_batchnorm_update: Whether to update batch norm moving average values inplace. When this is false train op must add a control dependency on tf.graphkeys.UPDATE_OPS collection in order to update batch norm statistics. use_explicit_padding: Whether to use explicit padding when extracting features. Default is False. use_depthwise: Whether to use depthwise convolutions. Default is False. override_base_feature_extractor_hyperparams: Whether to override hyperparameters of the base feature extractor with the one from `conv_hyperparams_config`. name: A string name scope to assign to the model. If 'None', Keras will auto-generate one from the class name. """ super(SSDKerasFeatureExtractor, self).__init__(name=name) self._is_training = is_training self._depth_multiplier = depth_multiplier self._min_depth = min_depth self._pad_to_multiple = pad_to_multiple self._conv_hyperparams = conv_hyperparams self._freeze_batchnorm = freeze_batchnorm self._inplace_batchnorm_update = inplace_batchnorm_update self._use_explicit_padding = use_explicit_padding self._use_depthwise = use_depthwise self._override_base_feature_extractor_hyperparams = ( override_base_feature_extractor_hyperparams) @property def is_keras_model(self): return True @abstractmethod def preprocess(self, resized_inputs): """Preprocesses images for feature extraction (minus image resizing). Args: resized_inputs: a [batch, height, width, channels] float tensor representing a batch of images. Returns: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. """ raise NotImplementedError @abstractmethod def _extract_features(self, preprocessed_inputs): """Extracts features from preprocessed inputs. This function is responsible for extracting feature maps from preprocessed images. Args: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. Returns: feature_maps: a list of tensors where the ith tensor has shape [batch, height_i, width_i, depth_i] """ raise NotImplementedError # This overrides the keras.Model `call` method with the _extract_features # method. def call(self, inputs, **kwargs): return self._extract_features(inputs) def restore_from_classification_checkpoint_fn(self, feature_extractor_scope): """Returns a map of variables to load from a foreign checkpoint. Args: feature_extractor_scope: A scope name for the feature extractor. Returns: A dict mapping variable names (to load from a checkpoint) to variables in the model graph. """ variables_to_restore = {} for variable in tf.global_variables(): var_name = variable.op.name if var_name.startswith(feature_extractor_scope + '/'): var_name = var_name.replace(feature_extractor_scope + '/', '') variables_to_restore[var_name] = variable return variables_to_restore class SSDMetaArch(model.DetectionModel): """SSD Meta-architecture definition.""" def __init__(self, is_training, anchor_generator, box_predictor, box_coder, feature_extractor, encode_background_as_zeros, image_resizer_fn, non_max_suppression_fn, score_conversion_fn, classification_loss, localization_loss, classification_loss_weight, localization_loss_weight, normalize_loss_by_num_matches, hard_example_miner, target_assigner_instance, add_summaries=True, normalize_loc_loss_by_codesize=False, freeze_batchnorm=False, inplace_batchnorm_update=False, add_background_class=True, explicit_background_class=False, random_example_sampler=None, expected_loss_weights_fn=None, use_confidences_as_targets=False, implicit_example_weight=0.5, equalization_loss_config=None): """SSDMetaArch Constructor. TODO(rathodv,jonathanhuang): group NMS parameters + score converter into a class and loss parameters into a class and write config protos for postprocessing and losses. Args: is_training: A boolean indicating whether the training version of the computation graph should be constructed. anchor_generator: an anchor_generator.AnchorGenerator object. box_predictor: a box_predictor.BoxPredictor object. box_coder: a box_coder.BoxCoder object. feature_extractor: a SSDFeatureExtractor object. encode_background_as_zeros: boolean determining whether background targets are to be encoded as an all zeros vector or a one-hot vector (where background is the 0th class). image_resizer_fn: a callable for image resizing. This callable always takes a rank-3 image tensor (corresponding to a single image) and returns a rank-3 image tensor, possibly with new spatial dimensions and a 1-D tensor of shape [3] indicating shape of true image within the resized image tensor as the resized image tensor could be padded. See builders/image_resizer_builder.py. non_max_suppression_fn: batch_multiclass_non_max_suppression callable that takes `boxes`, `scores` and optional `clip_window` inputs (with all other inputs already set) and returns a dictionary hold tensors with keys: `detection_boxes`, `detection_scores`, `detection_classes` and `num_detections`. See `post_processing. batch_multiclass_non_max_suppression` for the type and shape of these tensors. score_conversion_fn: callable elementwise nonlinearity (that takes tensors as inputs and returns tensors). This is usually used to convert logits to probabilities. classification_loss: an object_detection.core.losses.Loss object. localization_loss: a object_detection.core.losses.Loss object. classification_loss_weight: float localization_loss_weight: float normalize_loss_by_num_matches: boolean hard_example_miner: a losses.HardExampleMiner object (can be None) target_assigner_instance: target_assigner.TargetAssigner instance to use. add_summaries: boolean (default: True) controlling whether summary ops should be added to tensorflow graph. normalize_loc_loss_by_codesize: whether to normalize localization loss by code size of the box encoder. freeze_batchnorm: Whether to freeze batch norm parameters during training or not. When training with a small batch size (e.g. 1), it is desirable to freeze batch norm update and use pretrained batch norm params. inplace_batchnorm_update: Whether to update batch norm moving average values inplace. When this is false train op must add a control dependency on tf.graphkeys.UPDATE_OPS collection in order to update batch norm statistics. add_background_class: Whether to add an implicit background class to one-hot encodings of groundtruth labels. Set to false if training a single class model or using groundtruth labels with an explicit background class. explicit_background_class: Set to true if using groundtruth labels with an explicit background class, as in multiclass scores. random_example_sampler: a BalancedPositiveNegativeSampler object that can perform random example sampling when computing loss. If None, random sampling process is skipped. Note that random example sampler and hard example miner can both be applied to the model. In that case, random sampler will take effect first and hard example miner can only process the random sampled examples. expected_loss_weights_fn: If not None, use to calculate loss by background/foreground weighting. Should take batch_cls_targets as inputs and return foreground_weights, background_weights. See expected_classification_loss_by_expected_sampling and expected_classification_loss_by_reweighting_unmatched_anchors in third_party/tensorflow_models/object_detection/utils/ops.py as examples. use_confidences_as_targets: Whether to use groundtruth_condifences field to assign the targets. implicit_example_weight: a float number that specifies the weight used for the implicit negative examples. equalization_loss_config: a namedtuple that specifies configs for computing equalization loss. """ super(SSDMetaArch, self).__init__(num_classes=box_predictor.num_classes) self._is_training = is_training self._freeze_batchnorm = freeze_batchnorm self._inplace_batchnorm_update = inplace_batchnorm_update self._anchor_generator = anchor_generator self._box_predictor = box_predictor self._box_coder = box_coder self._feature_extractor = feature_extractor self._add_background_class = add_background_class self._explicit_background_class = explicit_background_class if add_background_class and explicit_background_class: raise ValueError("Cannot have both 'add_background_class' and" " 'explicit_background_class' true.") # Needed for fine-tuning from classification checkpoints whose # variables do not have the feature extractor scope. if self._feature_extractor.is_keras_model: # Keras feature extractors will have a name they implicitly use to scope. # So, all contained variables are prefixed by this name. # To load from classification checkpoints, need to filter out this name. self._extract_features_scope = feature_extractor.name else: # Slim feature extractors get an explicit naming scope self._extract_features_scope = 'FeatureExtractor' if encode_background_as_zeros: background_class = [0] else: background_class = [1] if self._add_background_class: num_foreground_classes = self.num_classes else: num_foreground_classes = self.num_classes - 1 self._unmatched_class_label = tf.constant( background_class + num_foreground_classes * [0], tf.float32) self._target_assigner = target_assigner_instance self._classification_loss = classification_loss self._localization_loss = localization_loss self._classification_loss_weight = classification_loss_weight self._localization_loss_weight = localization_loss_weight self._normalize_loss_by_num_matches = normalize_loss_by_num_matches self._normalize_loc_loss_by_codesize = normalize_loc_loss_by_codesize self._hard_example_miner = hard_example_miner self._random_example_sampler = random_example_sampler self._parallel_iterations = 16 self._image_resizer_fn = image_resizer_fn self._non_max_suppression_fn = non_max_suppression_fn self._score_conversion_fn = score_conversion_fn self._anchors = None self._add_summaries = add_summaries self._batched_prediction_tensor_names = [] self._expected_loss_weights_fn = expected_loss_weights_fn self._use_confidences_as_targets = use_confidences_as_targets self._implicit_example_weight = implicit_example_weight self._equalization_loss_config = equalization_loss_config @property def anchors(self): if not self._anchors: raise RuntimeError('anchors have not been constructed yet!') if not isinstance(self._anchors, box_list.BoxList): raise RuntimeError('anchors should be a BoxList object, but is not.') return self._anchors @property def batched_prediction_tensor_names(self): if not self._batched_prediction_tensor_names: raise RuntimeError('Must call predict() method to get batched prediction ' 'tensor names.') return self._batched_prediction_tensor_names def preprocess(self, inputs): """Feature-extractor specific preprocessing. SSD meta architecture uses a default clip_window of [0, 0, 1, 1] during post-processing. On calling `preprocess` method, clip_window gets updated based on `true_image_shapes` returned by `image_resizer_fn`. Args: inputs: a [batch, height_in, width_in, channels] float tensor representing a batch of images with values between 0 and 255.0. Returns: preprocessed_inputs: a [batch, height_out, width_out, channels] float tensor representing a batch of images. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Raises: ValueError: if inputs tensor does not have type tf.float32 """ if inputs.dtype is not tf.float32: raise ValueError('`preprocess` expects a tf.float32 tensor') with tf.name_scope('Preprocessor'): # TODO(jonathanhuang): revisit whether to always use batch size as # the number of parallel iterations vs allow for dynamic batching. outputs = shape_utils.static_or_dynamic_map_fn( self._image_resizer_fn, elems=inputs, dtype=[tf.float32, tf.int32]) resized_inputs = outputs[0] true_image_shapes = outputs[1] return (self._feature_extractor.preprocess(resized_inputs), true_image_shapes) def _compute_clip_window(self, preprocessed_images, true_image_shapes): """Computes clip window to use during post_processing. Computes a new clip window to use during post-processing based on `resized_image_shapes` and `true_image_shapes` only if `preprocess` method has been called. Otherwise returns a default clip window of [0, 0, 1, 1]. Args: preprocessed_images: the [batch, height, width, channels] image tensor. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Or None if the clip window should cover the full image. Returns: a 2-D float32 tensor of the form [batch_size, 4] containing the clip window for each image in the batch in normalized coordinates (relative to the resized dimensions) where each clip window is of the form [ymin, xmin, ymax, xmax] or a default clip window of [0, 0, 1, 1]. """ if true_image_shapes is None: return tf.constant([0, 0, 1, 1], dtype=tf.float32) resized_inputs_shape = shape_utils.combined_static_and_dynamic_shape( preprocessed_images) true_heights, true_widths, _ = tf.unstack( tf.to_float(true_image_shapes), axis=1) padded_height = tf.to_float(resized_inputs_shape[1]) padded_width = tf.to_float(resized_inputs_shape[2]) return tf.stack( [ tf.zeros_like(true_heights), tf.zeros_like(true_widths), true_heights / padded_height, true_widths / padded_width ], axis=1) def predict(self, preprocessed_inputs, true_image_shapes): """Predicts unpostprocessed tensors from input tensor. This function takes an input batch of images and runs it through the forward pass of the network to yield unpostprocessesed predictions. A side effect of calling the predict method is that self._anchors is populated with a box_list.BoxList of anchors. These anchors must be constructed before the postprocess or loss functions can be called. Args: preprocessed_inputs: a [batch, height, width, channels] image tensor. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Returns: prediction_dict: a dictionary holding "raw" prediction tensors: 1) preprocessed_inputs: the [batch, height, width, channels] image tensor. 2) box_encodings: 4-D float tensor of shape [batch_size, num_anchors, box_code_dimension] containing predicted boxes. 3) class_predictions_with_background: 3-D float tensor of shape [batch_size, num_anchors, num_classes+1] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions (at class index 0). 4) feature_maps: a list of tensors where the ith tensor has shape [batch, height_i, width_i, depth_i]. 5) anchors: 2-D float tensor of shape [num_anchors, 4] containing the generated anchors in normalized coordinates. """ batchnorm_updates_collections = (None if self._inplace_batchnorm_update else tf.GraphKeys.UPDATE_OPS) if self._feature_extractor.is_keras_model: feature_maps = self._feature_extractor(preprocessed_inputs) else: with slim.arg_scope([slim.batch_norm], is_training=(self._is_training and not self._freeze_batchnorm), updates_collections=batchnorm_updates_collections): with tf.variable_scope(None, self._extract_features_scope, [preprocessed_inputs]): feature_maps = self._feature_extractor.extract_features( preprocessed_inputs) feature_map_spatial_dims = self._get_feature_map_spatial_dims( feature_maps) image_shape = shape_utils.combined_static_and_dynamic_shape( preprocessed_inputs) self._anchors = box_list_ops.concatenate( self._anchor_generator.generate( feature_map_spatial_dims, im_height=image_shape[1], im_width=image_shape[2])) if self._box_predictor.is_keras_model: predictor_results_dict = self._box_predictor(feature_maps) else: with slim.arg_scope([slim.batch_norm], is_training=(self._is_training and not self._freeze_batchnorm), updates_collections=batchnorm_updates_collections): predictor_results_dict = self._box_predictor.predict( feature_maps, self._anchor_generator.num_anchors_per_location()) predictions_dict = { 'preprocessed_inputs': preprocessed_inputs, 'feature_maps': feature_maps, 'anchors': self._anchors.get() } for prediction_key, prediction_list in iter(predictor_results_dict.items()): prediction = tf.concat(prediction_list, axis=1) if (prediction_key == 'box_encodings' and prediction.shape.ndims == 4 and prediction.shape[2] == 1): prediction = tf.squeeze(prediction, axis=2) predictions_dict[prediction_key] = prediction self._batched_prediction_tensor_names = [x for x in predictions_dict if x != 'anchors'] return predictions_dict def _get_feature_map_spatial_dims(self, feature_maps): """Return list of spatial dimensions for each feature map in a list. Args: feature_maps: a list of tensors where the ith tensor has shape [batch, height_i, width_i, depth_i]. Returns: a list of pairs (height, width) for each feature map in feature_maps """ feature_map_shapes = [ shape_utils.combined_static_and_dynamic_shape( feature_map) for feature_map in feature_maps ] return [(shape[1], shape[2]) for shape in feature_map_shapes] def postprocess(self, prediction_dict, true_image_shapes): """Converts prediction tensors to final detections. This function converts raw predictions tensors to final detection results by slicing off the background class, decoding box predictions and applying non max suppression and clipping to the image window. See base class for output format conventions. Note also that by default, scores are to be interpreted as logits, but if a score_conversion_fn is used, then scores are remapped (and may thus have a different interpretation). Args: prediction_dict: a dictionary holding prediction tensors with 1) preprocessed_inputs: a [batch, height, width, channels] image tensor. 2) box_encodings: 3-D float tensor of shape [batch_size, num_anchors, box_code_dimension] containing predicted boxes. 3) class_predictions_with_background: 3-D float tensor of shape [batch_size, num_anchors, num_classes+1] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions. 4) mask_predictions: (optional) a 5-D float tensor of shape [batch_size, num_anchors, q, mask_height, mask_width]. `q` can be either number of classes or 1 depending on whether a separate mask is predicted per class. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. Or None, if the clip window should cover the full image. Returns: detections: a dictionary containing the following fields detection_boxes: [batch, max_detections, 4] detection_scores: [batch, max_detections] detection_classes: [batch, max_detections] detection_keypoints: [batch, max_detections, num_keypoints, 2] (if encoded in the prediction_dict 'box_encodings') detection_masks: [batch_size, max_detections, mask_height, mask_width] (optional) num_detections: [batch] Raises: ValueError: if prediction_dict does not contain `box_encodings` or `class_predictions_with_background` fields. """ if ('box_encodings' not in prediction_dict or 'class_predictions_with_background' not in prediction_dict): raise ValueError('prediction_dict does not contain expected entries.') with tf.name_scope('Postprocessor'): preprocessed_images = prediction_dict['preprocessed_inputs'] box_encodings = prediction_dict['box_encodings'] box_encodings = tf.identity(box_encodings, 'raw_box_encodings') class_predictions = prediction_dict['class_predictions_with_background'] detection_boxes, detection_keypoints = self._batch_decode(box_encodings) detection_boxes = tf.identity(detection_boxes, 'raw_box_locations') detection_boxes = tf.expand_dims(detection_boxes, axis=2) detection_scores = self._score_conversion_fn(class_predictions) detection_scores = tf.identity(detection_scores, 'raw_box_scores') if self._add_background_class or self._explicit_background_class: detection_scores = tf.slice(detection_scores, [0, 0, 1], [-1, -1, -1]) additional_fields = None batch_size = ( shape_utils.combined_static_and_dynamic_shape(preprocessed_images)[0]) if 'feature_maps' in prediction_dict: feature_map_list = [] for feature_map in prediction_dict['feature_maps']: feature_map_list.append(tf.reshape(feature_map, [batch_size, -1])) box_features = tf.concat(feature_map_list, 1) box_features = tf.identity(box_features, 'raw_box_features') if detection_keypoints is not None: additional_fields = { fields.BoxListFields.keypoints: detection_keypoints} (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections) = self._non_max_suppression_fn( detection_boxes, detection_scores, clip_window=self._compute_clip_window(preprocessed_images, true_image_shapes), additional_fields=additional_fields, masks=prediction_dict.get('mask_predictions')) detection_dict = { fields.DetectionResultFields.detection_boxes: nmsed_boxes, fields.DetectionResultFields.detection_scores: nmsed_scores, fields.DetectionResultFields.detection_classes: nmsed_classes, fields.DetectionResultFields.num_detections: tf.to_float(num_detections) } if (nmsed_additional_fields is not None and fields.BoxListFields.keypoints in nmsed_additional_fields): detection_dict[fields.DetectionResultFields.detection_keypoints] = ( nmsed_additional_fields[fields.BoxListFields.keypoints]) if nmsed_masks is not None: detection_dict[ fields.DetectionResultFields.detection_masks] = nmsed_masks return detection_dict def loss(self, prediction_dict, true_image_shapes, scope=None): """Compute scalar loss tensors with respect to provided groundtruth. Calling this function requires that groundtruth tensors have been provided via the provide_groundtruth function. Args: prediction_dict: a dictionary holding prediction tensors with 1) box_encodings: 3-D float tensor of shape [batch_size, num_anchors, box_code_dimension] containing predicted boxes. 2) class_predictions_with_background: 3-D float tensor of shape [batch_size, num_anchors, num_classes+1] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions. true_image_shapes: int32 tensor of shape [batch, 3] where each row is of the form [height, width, channels] indicating the shapes of true images in the resized images, as resized images can be padded with zeros. scope: Optional scope name. Returns: a dictionary mapping loss keys (`localization_loss` and `classification_loss`) to scalar tensors representing corresponding loss values. """ with tf.name_scope(scope, 'Loss', prediction_dict.values()): keypoints = None if self.groundtruth_has_field(fields.BoxListFields.keypoints): keypoints = self.groundtruth_lists(fields.BoxListFields.keypoints) weights = None if self.groundtruth_has_field(fields.BoxListFields.weights): weights = self.groundtruth_lists(fields.BoxListFields.weights) confidences = None if self.groundtruth_has_field(fields.BoxListFields.confidences): confidences = self.groundtruth_lists(fields.BoxListFields.confidences) (batch_cls_targets, batch_cls_weights, batch_reg_targets, batch_reg_weights, match_list) = self._assign_targets( self.groundtruth_lists(fields.BoxListFields.boxes), self.groundtruth_lists(fields.BoxListFields.classes), keypoints, weights, confidences) if self._add_summaries: self._summarize_target_assignment( self.groundtruth_lists(fields.BoxListFields.boxes), match_list) if self._random_example_sampler: batch_cls_per_anchor_weights = tf.reduce_mean( batch_cls_weights, axis=-1) batch_sampled_indicator = tf.to_float( shape_utils.static_or_dynamic_map_fn( self._minibatch_subsample_fn, [batch_cls_targets, batch_cls_per_anchor_weights], dtype=tf.bool, parallel_iterations=self._parallel_iterations, back_prop=True)) batch_reg_weights = tf.multiply(batch_sampled_indicator, batch_reg_weights) batch_cls_weights = tf.multiply( tf.expand_dims(batch_sampled_indicator, -1), batch_cls_weights) losses_mask = None if self.groundtruth_has_field(fields.InputDataFields.is_annotated): losses_mask = tf.stack(self.groundtruth_lists( fields.InputDataFields.is_annotated)) location_losses = self._localization_loss( prediction_dict['box_encodings'], batch_reg_targets, ignore_nan_targets=True, weights=batch_reg_weights, losses_mask=losses_mask) cls_losses = self._classification_loss( prediction_dict['class_predictions_with_background'], batch_cls_targets, weights=batch_cls_weights, losses_mask=losses_mask) if self._expected_loss_weights_fn: # Need to compute losses for assigned targets against the # unmatched_class_label as well as their assigned targets. # simplest thing (but wasteful) is just to calculate all losses # twice batch_size, num_anchors, num_classes = batch_cls_targets.get_shape() unmatched_targets = tf.ones([batch_size, num_anchors, 1 ]) * self._unmatched_class_label unmatched_cls_losses = self._classification_loss( prediction_dict['class_predictions_with_background'], unmatched_targets, weights=batch_cls_weights, losses_mask=losses_mask) if cls_losses.get_shape().ndims == 3: batch_size, num_anchors, num_classes = cls_losses.get_shape() cls_losses = tf.reshape(cls_losses, [batch_size, -1]) unmatched_cls_losses = tf.reshape(unmatched_cls_losses, [batch_size, -1]) batch_cls_targets = tf.reshape( batch_cls_targets, [batch_size, num_anchors * num_classes, -1]) batch_cls_targets = tf.concat( [1 - batch_cls_targets, batch_cls_targets], axis=-1) location_losses = tf.tile(location_losses, [1, num_classes]) foreground_weights, background_weights = ( self._expected_loss_weights_fn(batch_cls_targets)) cls_losses = ( foreground_weights * cls_losses + background_weights * unmatched_cls_losses) location_losses *= foreground_weights classification_loss = tf.reduce_sum(cls_losses) localization_loss = tf.reduce_sum(location_losses) elif self._hard_example_miner: cls_losses = ops.reduce_sum_trailing_dimensions(cls_losses, ndims=2) (localization_loss, classification_loss) = self._apply_hard_mining( location_losses, cls_losses, prediction_dict, match_list) if self._add_summaries: self._hard_example_miner.summarize() else: cls_losses = ops.reduce_sum_trailing_dimensions(cls_losses, ndims=2) localization_loss = tf.reduce_sum(location_losses) classification_loss = tf.reduce_sum(cls_losses) # Optionally normalize by number of positive matches normalizer = tf.constant(1.0, dtype=tf.float32) if self._normalize_loss_by_num_matches: normalizer = tf.maximum(tf.to_float(tf.reduce_sum(batch_reg_weights)), 1.0) localization_loss_normalizer = normalizer if self._normalize_loc_loss_by_codesize: localization_loss_normalizer *= self._box_coder.code_size localization_loss = tf.multiply((self._localization_loss_weight / localization_loss_normalizer), localization_loss, name='localization_loss') classification_loss = tf.multiply((self._classification_loss_weight / normalizer), classification_loss, name='classification_loss') loss_dict = { str(localization_loss.op.name): localization_loss, str(classification_loss.op.name): classification_loss } return loss_dict def _minibatch_subsample_fn(self, inputs): """Randomly samples anchors for one image. Args: inputs: a list of 2 inputs. First one is a tensor of shape [num_anchors, num_classes] indicating targets assigned to each anchor. Second one is a tensor of shape [num_anchors] indicating the class weight of each anchor. Returns: batch_sampled_indicator: bool tensor of shape [num_anchors] indicating whether the anchor should be selected for loss computation. """ cls_targets, cls_weights = inputs if self._add_background_class: # Set background_class bits to 0 so that the positives_indicator # computation would not consider background class. background_class = tf.zeros_like(tf.slice(cls_targets, [0, 0], [-1, 1])) regular_class = tf.slice(cls_targets, [0, 1], [-1, -1]) cls_targets = tf.concat([background_class, regular_class], 1) positives_indicator = tf.reduce_sum(cls_targets, axis=1) return self._random_example_sampler.subsample( tf.cast(cls_weights, tf.bool), batch_size=None, labels=tf.cast(positives_indicator, tf.bool)) def _summarize_anchor_classification_loss(self, class_ids, cls_losses): positive_indices = tf.where(tf.greater(class_ids, 0)) positive_anchor_cls_loss = tf.squeeze( tf.gather(cls_losses, positive_indices), axis=1) visualization_utils.add_cdf_image_summary(positive_anchor_cls_loss, 'PositiveAnchorLossCDF') negative_indices = tf.where(tf.equal(class_ids, 0)) negative_anchor_cls_loss = tf.squeeze( tf.gather(cls_losses, negative_indices), axis=1) visualization_utils.add_cdf_image_summary(negative_anchor_cls_loss, 'NegativeAnchorLossCDF') def _assign_targets(self, groundtruth_boxes_list, groundtruth_classes_list, groundtruth_keypoints_list=None, groundtruth_weights_list=None, groundtruth_confidences_list=None): """Assign groundtruth targets. Adds a background class to each one-hot encoding of groundtruth classes and uses target assigner to obtain regression and classification targets. Args: groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4] containing coordinates of the groundtruth boxes. Groundtruth boxes are provided in [y_min, x_min, y_max, x_max] format and assumed to be normalized and clipped relative to the image window with y_min <= y_max and x_min <= x_max. groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of shape [num_boxes, num_classes] containing the class targets with the 0th index assumed to map to the first non-background class. groundtruth_keypoints_list: (optional) a list of 3-D tensors of shape [num_boxes, num_keypoints, 2] groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape [num_boxes] containing weights for groundtruth boxes. groundtruth_confidences_list: A list of 2-D tf.float32 tensors of shape [num_boxes, num_classes] containing class confidences for groundtruth boxes. Returns: batch_cls_targets: a tensor with shape [batch_size, num_anchors, num_classes], batch_cls_weights: a tensor with shape [batch_size, num_anchors], batch_reg_targets: a tensor with shape [batch_size, num_anchors, box_code_dimension] batch_reg_weights: a tensor with shape [batch_size, num_anchors], match_list: a list of matcher.Match objects encoding the match between anchors and groundtruth boxes for each image of the batch, with rows of the Match objects corresponding to groundtruth boxes and columns corresponding to anchors. """ groundtruth_boxlists = [ box_list.BoxList(boxes) for boxes in groundtruth_boxes_list ] train_using_confidences = (self._is_training and self._use_confidences_as_targets) if self._add_background_class: groundtruth_classes_with_background_list = [ tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT') for one_hot_encoding in groundtruth_classes_list ] if train_using_confidences: groundtruth_confidences_with_background_list = [ tf.pad(groundtruth_confidences, [[0, 0], [1, 0]], mode='CONSTANT') for groundtruth_confidences in groundtruth_confidences_list ] else: groundtruth_classes_with_background_list = groundtruth_classes_list if groundtruth_keypoints_list is not None: for boxlist, keypoints in zip( groundtruth_boxlists, groundtruth_keypoints_list): boxlist.add_field(fields.BoxListFields.keypoints, keypoints) if train_using_confidences: return target_assigner.batch_assign_confidences( self._target_assigner, self.anchors, groundtruth_boxlists, groundtruth_confidences_with_background_list, groundtruth_weights_list, self._unmatched_class_label, self._add_background_class, self._implicit_example_weight) else: return target_assigner.batch_assign_targets( self._target_assigner, self.anchors, groundtruth_boxlists, groundtruth_classes_with_background_list, self._unmatched_class_label, groundtruth_weights_list) def _summarize_target_assignment(self, groundtruth_boxes_list, match_list): """Creates tensorflow summaries for the input boxes and anchors. This function creates four summaries corresponding to the average number (over images in a batch) of (1) groundtruth boxes, (2) anchors marked as positive, (3) anchors marked as negative, and (4) anchors marked as ignored. Args: groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4] containing corners of the groundtruth boxes. match_list: a list of matcher.Match objects encoding the match between anchors and groundtruth boxes for each image of the batch, with rows of the Match objects corresponding to groundtruth boxes and columns corresponding to anchors. """ num_boxes_per_image = tf.stack( [tf.shape(x)[0] for x in groundtruth_boxes_list]) pos_anchors_per_image = tf.stack( [match.num_matched_columns() for match in match_list]) neg_anchors_per_image = tf.stack( [match.num_unmatched_columns() for match in match_list]) ignored_anchors_per_image = tf.stack( [match.num_ignored_columns() for match in match_list]) tf.summary.scalar('AvgNumGroundtruthBoxesPerImage', tf.reduce_mean(tf.to_float(num_boxes_per_image)), family='TargetAssignment') tf.summary.scalar('AvgNumPositiveAnchorsPerImage', tf.reduce_mean(tf.to_float(pos_anchors_per_image)), family='TargetAssignment') tf.summary.scalar('AvgNumNegativeAnchorsPerImage', tf.reduce_mean(tf.to_float(neg_anchors_per_image)), family='TargetAssignment') tf.summary.scalar('AvgNumIgnoredAnchorsPerImage', tf.reduce_mean(tf.to_float(ignored_anchors_per_image)), family='TargetAssignment') def _apply_hard_mining(self, location_losses, cls_losses, prediction_dict, match_list): """Applies hard mining to anchorwise losses. Args: location_losses: Float tensor of shape [batch_size, num_anchors] representing anchorwise location losses. cls_losses: Float tensor of shape [batch_size, num_anchors] representing anchorwise classification losses. prediction_dict: p a dictionary holding prediction tensors with 1) box_encodings: 3-D float tensor of shape [batch_size, num_anchors, box_code_dimension] containing predicted boxes. 2) class_predictions_with_background: 3-D float tensor of shape [batch_size, num_anchors, num_classes+1] containing class predictions (logits) for each of the anchors. Note that this tensor *includes* background class predictions. match_list: a list of matcher.Match objects encoding the match between anchors and groundtruth boxes for each image of the batch, with rows of the Match objects corresponding to groundtruth boxes and columns corresponding to anchors. Returns: mined_location_loss: a float scalar with sum of localization losses from selected hard examples. mined_cls_loss: a float scalar with sum of classification losses from selected hard examples. """ class_predictions = tf.slice( prediction_dict['class_predictions_with_background'], [0, 0, 1], [-1, -1, -1]) decoded_boxes, _ = self._batch_decode(prediction_dict['box_encodings']) decoded_box_tensors_list = tf.unstack(decoded_boxes) class_prediction_list = tf.unstack(class_predictions) decoded_boxlist_list = [] for box_location, box_score in zip(decoded_box_tensors_list, class_prediction_list): decoded_boxlist = box_list.BoxList(box_location) decoded_boxlist.add_field('scores', box_score) decoded_boxlist_list.append(decoded_boxlist) return self._hard_example_miner( location_losses=location_losses, cls_losses=cls_losses, decoded_boxlist_list=decoded_boxlist_list, match_list=match_list) def _batch_decode(self, box_encodings): """Decodes a batch of box encodings with respect to the anchors. Args: box_encodings: A float32 tensor of shape [batch_size, num_anchors, box_code_size] containing box encodings. Returns: decoded_boxes: A float32 tensor of shape [batch_size, num_anchors, 4] containing the decoded boxes. decoded_keypoints: A float32 tensor of shape [batch_size, num_anchors, num_keypoints, 2] containing the decoded keypoints if present in the input `box_encodings`, None otherwise. """ combined_shape = shape_utils.combined_static_and_dynamic_shape( box_encodings) batch_size = combined_shape[0] tiled_anchor_boxes = tf.tile( tf.expand_dims(self.anchors.get(), 0), [batch_size, 1, 1]) tiled_anchors_boxlist = box_list.BoxList( tf.reshape(tiled_anchor_boxes, [-1, 4])) decoded_boxes = self._box_coder.decode( tf.reshape(box_encodings, [-1, self._box_coder.code_size]), tiled_anchors_boxlist) decoded_keypoints = None if decoded_boxes.has_field(fields.BoxListFields.keypoints): decoded_keypoints = decoded_boxes.get_field( fields.BoxListFields.keypoints) num_keypoints = decoded_keypoints.get_shape()[1] decoded_keypoints = tf.reshape( decoded_keypoints, tf.stack([combined_shape[0], combined_shape[1], num_keypoints, 2])) decoded_boxes = tf.reshape(decoded_boxes.get(), tf.stack( [combined_shape[0], combined_shape[1], 4])) return decoded_boxes, decoded_keypoints def regularization_losses(self): """Returns a list of regularization losses for this model. Returns a list of regularization losses for this model that the estimator needs to use during training/optimization. Returns: A list of regularization loss tensors. """ losses = [] slim_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) # Copy the slim losses to avoid modifying the collection if slim_losses: losses.extend(slim_losses) if self._box_predictor.is_keras_model: losses.extend(self._box_predictor.losses) if self._feature_extractor.is_keras_model: losses.extend(self._feature_extractor.losses) return losses def restore_map(self, fine_tune_checkpoint_type='detection', load_all_detection_checkpoint_vars=False): """Returns a map of variables to load from a foreign checkpoint. See parent class for details. Args: fine_tune_checkpoint_type: whether to restore from a full detection checkpoint (with compatible variable names) or to restore from a classification checkpoint for initialization prior to training. Valid values: `detection`, `classification`. Default 'detection'. load_all_detection_checkpoint_vars: whether to load all variables (when `fine_tune_checkpoint_type='detection'`). If False, only variables within the appropriate scopes are included. Default False. Returns: A dict mapping variable names (to load from a checkpoint) to variables in the model graph. Raises: ValueError: if fine_tune_checkpoint_type is neither `classification` nor `detection`. """ if fine_tune_checkpoint_type not in ['detection', 'classification']: raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format( fine_tune_checkpoint_type)) if fine_tune_checkpoint_type == 'classification': return self._feature_extractor.restore_from_classification_checkpoint_fn( self._extract_features_scope) if fine_tune_checkpoint_type == 'detection': variables_to_restore = {} for variable in tf.global_variables(): var_name = variable.op.name if load_all_detection_checkpoint_vars: variables_to_restore[var_name] = variable else: if var_name.startswith(self._extract_features_scope): variables_to_restore[var_name] = variable return variables_to_restore def updates(self): """Returns a list of update operators for this model. Returns a list of update operators for this model that must be executed at each training step. The estimator's train op needs to have a control dependency on these updates. Returns: A list of update operators. """ update_ops = [] slim_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # Copy the slim ops to avoid modifying the collection if slim_update_ops: update_ops.extend(slim_update_ops) if self._box_predictor.is_keras_model: update_ops.extend(self._box_predictor.get_updates_for(None)) update_ops.extend(self._box_predictor.get_updates_for( self._box_predictor.inputs)) if self._feature_extractor.is_keras_model: update_ops.extend(self._feature_extractor.get_updates_for(None)) update_ops.extend(self._feature_extractor.get_updates_for( self._feature_extractor.inputs)) return update_ops
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/meta_architectures/ssd_meta_arch.py
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/protos/__init__.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Base target assigner module. The job of a TargetAssigner is, for a given set of anchors (bounding boxes) and groundtruth detections (bounding boxes), to assign classification and regression targets to each anchor as well as weights to each anchor (specifying, e.g., which anchors should not contribute to training loss). It assigns classification/regression targets by performing the following steps: 1) Computing pairwise similarity between anchors and groundtruth boxes using a provided RegionSimilarity Calculator 2) Computing a matching based on the similarity matrix using a provided Matcher 3) Assigning regression targets based on the matching and a provided BoxCoder 4) Assigning classification targets based on the matching and groundtruth labels Note that TargetAssigners only operate on detections from a single image at a time, so any logic for applying a TargetAssigner to multiple images must be handled externally. """ import tensorflow as tf from object_detection.box_coders import faster_rcnn_box_coder from object_detection.box_coders import mean_stddev_box_coder from object_detection.core import box_coder as bcoder from object_detection.core import box_list from object_detection.core import matcher as mat from object_detection.core import region_similarity_calculator as sim_calc from object_detection.core import standard_fields as fields from object_detection.matchers import argmax_matcher from object_detection.matchers import bipartite_matcher from object_detection.utils import shape_utils class TargetAssigner(object): """Target assigner to compute classification and regression targets.""" def __init__(self, similarity_calc, matcher, box_coder, negative_class_weight=1.0): """Construct Object Detection Target Assigner. Args: similarity_calc: a RegionSimilarityCalculator matcher: an object_detection.core.Matcher used to match groundtruth to anchors. box_coder: an object_detection.core.BoxCoder used to encode matching groundtruth boxes with respect to anchors. negative_class_weight: classification weight to be associated to negative anchors (default: 1.0). The weight must be in [0., 1.]. Raises: ValueError: if similarity_calc is not a RegionSimilarityCalculator or if matcher is not a Matcher or if box_coder is not a BoxCoder """ if not isinstance(similarity_calc, sim_calc.RegionSimilarityCalculator): raise ValueError('similarity_calc must be a RegionSimilarityCalculator') if not isinstance(matcher, mat.Matcher): raise ValueError('matcher must be a Matcher') if not isinstance(box_coder, bcoder.BoxCoder): raise ValueError('box_coder must be a BoxCoder') self._similarity_calc = similarity_calc self._matcher = matcher self._box_coder = box_coder self._negative_class_weight = negative_class_weight @property def box_coder(self): return self._box_coder # TODO(rathodv): move labels, scores, and weights to groundtruth_boxes fields. def assign(self, anchors, groundtruth_boxes, groundtruth_labels=None, unmatched_class_label=None, groundtruth_weights=None): """Assign classification and regression targets to each anchor. For a given set of anchors and groundtruth detections, match anchors to groundtruth_boxes and assign classification and regression targets to each anchor as well as weights based on the resulting match (specifying, e.g., which anchors should not contribute to training loss). Anchors that are not matched to anything are given a classification target of self._unmatched_cls_target which can be specified via the constructor. Args: anchors: a BoxList representing N anchors groundtruth_boxes: a BoxList representing M groundtruth boxes groundtruth_labels: a tensor of shape [M, d_1, ... d_k] with labels for each of the ground_truth boxes. The subshape [d_1, ... d_k] can be empty (corresponding to scalar inputs). When set to None, groundtruth_labels assumes a binary problem where all ground_truth boxes get a positive label (of 1). unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] which is consistent with the classification target for each anchor (and can be empty for scalar targets). This shape must thus be compatible with the groundtruth labels that are passed to the "assign" function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). If set to None, unmatched_cls_target is set to be [0] for each anchor. groundtruth_weights: a float tensor of shape [M] indicating the weight to assign to all anchors match to a particular groundtruth box. The weights must be in [0., 1.]. If None, all weights are set to 1. Generally no groundtruth boxes with zero weight match to any anchors as matchers are aware of groundtruth weights. Additionally, `cls_weights` and `reg_weights` are calculated using groundtruth weights as an added safety. Returns: cls_targets: a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k], where the subshape [d_1, ..., d_k] is compatible with groundtruth_labels which has shape [num_gt_boxes, d_1, d_2, ... d_k]. cls_weights: a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k], representing weights for each element in cls_targets. reg_targets: a float32 tensor with shape [num_anchors, box_code_dimension] reg_weights: a float32 tensor with shape [num_anchors] match: a matcher.Match object encoding the match between anchors and groundtruth boxes, with rows corresponding to groundtruth boxes and columns corresponding to anchors. Raises: ValueError: if anchors or groundtruth_boxes are not of type box_list.BoxList """ if not isinstance(anchors, box_list.BoxList): raise ValueError('anchors must be an BoxList') if not isinstance(groundtruth_boxes, box_list.BoxList): raise ValueError('groundtruth_boxes must be an BoxList') if unmatched_class_label is None: unmatched_class_label = tf.constant([0], tf.float32) if groundtruth_labels is None: groundtruth_labels = tf.ones(tf.expand_dims(groundtruth_boxes.num_boxes(), 0)) groundtruth_labels = tf.expand_dims(groundtruth_labels, -1) unmatched_shape_assert = shape_utils.assert_shape_equal( shape_utils.combined_static_and_dynamic_shape(groundtruth_labels)[1:], shape_utils.combined_static_and_dynamic_shape(unmatched_class_label)) labels_and_box_shapes_assert = shape_utils.assert_shape_equal( shape_utils.combined_static_and_dynamic_shape( groundtruth_labels)[:1], shape_utils.combined_static_and_dynamic_shape( groundtruth_boxes.get())[:1]) if groundtruth_weights is None: num_gt_boxes = groundtruth_boxes.num_boxes_static() if not num_gt_boxes: num_gt_boxes = groundtruth_boxes.num_boxes() groundtruth_weights = tf.ones([num_gt_boxes], dtype=tf.float32) with tf.control_dependencies( [unmatched_shape_assert, labels_and_box_shapes_assert]): match_quality_matrix = self._similarity_calc.compare(groundtruth_boxes, anchors) match = self._matcher.match(match_quality_matrix, valid_rows=tf.greater(groundtruth_weights, 0)) reg_targets = self._create_regression_targets(anchors, groundtruth_boxes, match) cls_targets = self._create_classification_targets(groundtruth_labels, unmatched_class_label, match) reg_weights = self._create_regression_weights(match, groundtruth_weights) cls_weights = self._create_classification_weights(match, groundtruth_weights) # convert cls_weights from per-anchor to per-class. class_label_shape = tf.shape(cls_targets)[1:] weights_shape = tf.shape(cls_weights) weights_multiple = tf.concat( [tf.ones_like(weights_shape), class_label_shape], axis=0) for _ in range(len(cls_targets.get_shape()[1:])): cls_weights = tf.expand_dims(cls_weights, -1) cls_weights = tf.tile(cls_weights, weights_multiple) num_anchors = anchors.num_boxes_static() if num_anchors is not None: reg_targets = self._reset_target_shape(reg_targets, num_anchors) cls_targets = self._reset_target_shape(cls_targets, num_anchors) reg_weights = self._reset_target_shape(reg_weights, num_anchors) cls_weights = self._reset_target_shape(cls_weights, num_anchors) return cls_targets, cls_weights, reg_targets, reg_weights, match def _reset_target_shape(self, target, num_anchors): """Sets the static shape of the target. Args: target: the target tensor. Its first dimension will be overwritten. num_anchors: the number of anchors, which is used to override the target's first dimension. Returns: A tensor with the shape info filled in. """ target_shape = target.get_shape().as_list() target_shape[0] = num_anchors target.set_shape(target_shape) return target def _create_regression_targets(self, anchors, groundtruth_boxes, match): """Returns a regression target for each anchor. Args: anchors: a BoxList representing N anchors groundtruth_boxes: a BoxList representing M groundtruth_boxes match: a matcher.Match object Returns: reg_targets: a float32 tensor with shape [N, box_code_dimension] """ matched_gt_boxes = match.gather_based_on_match( groundtruth_boxes.get(), unmatched_value=tf.zeros(4), ignored_value=tf.zeros(4)) matched_gt_boxlist = box_list.BoxList(matched_gt_boxes) if groundtruth_boxes.has_field(fields.BoxListFields.keypoints): groundtruth_keypoints = groundtruth_boxes.get_field( fields.BoxListFields.keypoints) matched_keypoints = match.gather_based_on_match( groundtruth_keypoints, unmatched_value=tf.zeros(groundtruth_keypoints.get_shape()[1:]), ignored_value=tf.zeros(groundtruth_keypoints.get_shape()[1:])) matched_gt_boxlist.add_field(fields.BoxListFields.keypoints, matched_keypoints) matched_reg_targets = self._box_coder.encode(matched_gt_boxlist, anchors) match_results_shape = shape_utils.combined_static_and_dynamic_shape( match.match_results) # Zero out the unmatched and ignored regression targets. unmatched_ignored_reg_targets = tf.tile( self._default_regression_target(), [match_results_shape[0], 1]) matched_anchors_mask = match.matched_column_indicator() reg_targets = tf.where(matched_anchors_mask, matched_reg_targets, unmatched_ignored_reg_targets) return reg_targets def _default_regression_target(self): """Returns the default target for anchors to regress to. Default regression targets are set to zero (though in this implementation what these targets are set to should not matter as the regression weight of any box set to regress to the default target is zero). Returns: default_target: a float32 tensor with shape [1, box_code_dimension] """ return tf.constant([self._box_coder.code_size*[0]], tf.float32) def _create_classification_targets(self, groundtruth_labels, unmatched_class_label, match): """Create classification targets for each anchor. Assign a classification target of for each anchor to the matching groundtruth label that is provided by match. Anchors that are not matched to anything are given the target self._unmatched_cls_target Args: groundtruth_labels: a tensor of shape [num_gt_boxes, d_1, ... d_k] with labels for each of the ground_truth boxes. The subshape [d_1, ... d_k] can be empty (corresponding to scalar labels). unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] which is consistent with the classification target for each anchor (and can be empty for scalar targets). This shape must thus be compatible with the groundtruth labels that are passed to the "assign" function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). match: a matcher.Match object that provides a matching between anchors and groundtruth boxes. Returns: a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k], where the subshape [d_1, ..., d_k] is compatible with groundtruth_labels which has shape [num_gt_boxes, d_1, d_2, ... d_k]. """ return match.gather_based_on_match( groundtruth_labels, unmatched_value=unmatched_class_label, ignored_value=unmatched_class_label) def _create_regression_weights(self, match, groundtruth_weights): """Set regression weight for each anchor. Only positive anchors are set to contribute to the regression loss, so this method returns a weight of 1 for every positive anchor and 0 for every negative anchor. Args: match: a matcher.Match object that provides a matching between anchors and groundtruth boxes. groundtruth_weights: a float tensor of shape [M] indicating the weight to assign to all anchors match to a particular groundtruth box. Returns: a float32 tensor with shape [num_anchors] representing regression weights. """ return match.gather_based_on_match( groundtruth_weights, ignored_value=0., unmatched_value=0.) def _create_classification_weights(self, match, groundtruth_weights): """Create classification weights for each anchor. Positive (matched) anchors are associated with a weight of positive_class_weight and negative (unmatched) anchors are associated with a weight of negative_class_weight. When anchors are ignored, weights are set to zero. By default, both positive/negative weights are set to 1.0, but they can be adjusted to handle class imbalance (which is almost always the case in object detection). Args: match: a matcher.Match object that provides a matching between anchors and groundtruth boxes. groundtruth_weights: a float tensor of shape [M] indicating the weight to assign to all anchors match to a particular groundtruth box. Returns: a float32 tensor with shape [num_anchors] representing classification weights. """ return match.gather_based_on_match( groundtruth_weights, ignored_value=0., unmatched_value=self._negative_class_weight) def get_box_coder(self): """Get BoxCoder of this TargetAssigner. Returns: BoxCoder object. """ return self._box_coder # TODO(rathodv): This method pulls in all the implementation dependencies into # core. Therefore its best to have this factory method outside of core. def create_target_assigner(reference, stage=None, negative_class_weight=1.0, use_matmul_gather=False): """Factory function for creating standard target assigners. Args: reference: string referencing the type of TargetAssigner. stage: string denoting stage: {proposal, detection}. negative_class_weight: classification weight to be associated to negative anchors (default: 1.0) use_matmul_gather: whether to use matrix multiplication based gather which are better suited for TPUs. Returns: TargetAssigner: desired target assigner. Raises: ValueError: if combination reference+stage is invalid. """ if reference == 'Multibox' and stage == 'proposal': similarity_calc = sim_calc.NegSqDistSimilarity() matcher = bipartite_matcher.GreedyBipartiteMatcher() box_coder = mean_stddev_box_coder.MeanStddevBoxCoder() elif reference == 'FasterRCNN' and stage == 'proposal': similarity_calc = sim_calc.IouSimilarity() matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.7, unmatched_threshold=0.3, force_match_for_each_row=True, use_matmul_gather=use_matmul_gather) box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder( scale_factors=[10.0, 10.0, 5.0, 5.0]) elif reference == 'FasterRCNN' and stage == 'detection': similarity_calc = sim_calc.IouSimilarity() # Uses all proposals with IOU < 0.5 as candidate negatives. matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, negatives_lower_than_unmatched=True, use_matmul_gather=use_matmul_gather) box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder( scale_factors=[10.0, 10.0, 5.0, 5.0]) elif reference == 'FastRCNN': similarity_calc = sim_calc.IouSimilarity() matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, unmatched_threshold=0.1, force_match_for_each_row=False, negatives_lower_than_unmatched=False, use_matmul_gather=use_matmul_gather) box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder() else: raise ValueError('No valid combination of reference and stage.') return TargetAssigner(similarity_calc, matcher, box_coder, negative_class_weight=negative_class_weight) def batch_assign_targets(target_assigner, anchors_batch, gt_box_batch, gt_class_targets_batch, unmatched_class_label=None, gt_weights_batch=None): """Batched assignment of classification and regression targets. Args: target_assigner: a target assigner. anchors_batch: BoxList representing N box anchors or list of BoxList objects with length batch_size representing anchor sets. gt_box_batch: a list of BoxList objects with length batch_size representing groundtruth boxes for each image in the batch gt_class_targets_batch: a list of tensors with length batch_size, where each tensor has shape [num_gt_boxes_i, classification_target_size] and num_gt_boxes_i is the number of boxes in the ith boxlist of gt_box_batch. unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] which is consistent with the classification target for each anchor (and can be empty for scalar targets). This shape must thus be compatible with the groundtruth labels that are passed to the "assign" function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). gt_weights_batch: A list of 1-D tf.float32 tensors of shape [num_boxes] containing weights for groundtruth boxes. Returns: batch_cls_targets: a tensor with shape [batch_size, num_anchors, num_classes], batch_cls_weights: a tensor with shape [batch_size, num_anchors, num_classes], batch_reg_targets: a tensor with shape [batch_size, num_anchors, box_code_dimension] batch_reg_weights: a tensor with shape [batch_size, num_anchors], match_list: a list of matcher.Match objects encoding the match between anchors and groundtruth boxes for each image of the batch, with rows of the Match objects corresponding to groundtruth boxes and columns corresponding to anchors. Raises: ValueError: if input list lengths are inconsistent, i.e., batch_size == len(gt_box_batch) == len(gt_class_targets_batch) and batch_size == len(anchors_batch) unless anchors_batch is a single BoxList. """ if not isinstance(anchors_batch, list): anchors_batch = len(gt_box_batch) * [anchors_batch] if not all( isinstance(anchors, box_list.BoxList) for anchors in anchors_batch): raise ValueError('anchors_batch must be a BoxList or list of BoxLists.') if not (len(anchors_batch) == len(gt_box_batch) == len(gt_class_targets_batch)): raise ValueError('batch size incompatible with lengths of anchors_batch, ' 'gt_box_batch and gt_class_targets_batch.') cls_targets_list = [] cls_weights_list = [] reg_targets_list = [] reg_weights_list = [] match_list = [] if gt_weights_batch is None: gt_weights_batch = [None] * len(gt_class_targets_batch) for anchors, gt_boxes, gt_class_targets, gt_weights in zip( anchors_batch, gt_box_batch, gt_class_targets_batch, gt_weights_batch): (cls_targets, cls_weights, reg_targets, reg_weights, match) = target_assigner.assign( anchors, gt_boxes, gt_class_targets, unmatched_class_label, gt_weights) cls_targets_list.append(cls_targets) cls_weights_list.append(cls_weights) reg_targets_list.append(reg_targets) reg_weights_list.append(reg_weights) match_list.append(match) batch_cls_targets = tf.stack(cls_targets_list) batch_cls_weights = tf.stack(cls_weights_list) batch_reg_targets = tf.stack(reg_targets_list) batch_reg_weights = tf.stack(reg_weights_list) return (batch_cls_targets, batch_cls_weights, batch_reg_targets, batch_reg_weights, match_list) def batch_assign_confidences(target_assigner, anchors_batch, gt_box_batch, gt_class_confidences_batch, gt_weights_batch=None, unmatched_class_label=None, include_background_class=True, implicit_class_weight=1.0): """Batched assignment of classification and regression targets. This differences between batch_assign_confidences and batch_assign_targets: - 'batch_assign_targets' supports scalar (agnostic), vector (multiclass) and tensor (high-dimensional) targets. 'batch_assign_confidences' only support scalar (agnostic) and vector (multiclass) targets. - 'batch_assign_targets' assumes the input class tensor using the binary one/K-hot encoding. 'batch_assign_confidences' takes the class confidence scores as the input, where 1 means positive classes, 0 means implicit negative classes, and -1 means explicit negative classes. - 'batch_assign_confidences' assigns the targets in the similar way as 'batch_assign_targets' except that it gives different weights for implicit and explicit classes. This allows user to control the negative gradients pushed differently for implicit and explicit examples during the training. Args: target_assigner: a target assigner. anchors_batch: BoxList representing N box anchors or list of BoxList objects with length batch_size representing anchor sets. gt_box_batch: a list of BoxList objects with length batch_size representing groundtruth boxes for each image in the batch gt_class_confidences_batch: a list of tensors with length batch_size, where each tensor has shape [num_gt_boxes_i, classification_target_size] and num_gt_boxes_i is the number of boxes in the ith boxlist of gt_box_batch. Note that in this tensor, 1 means explicit positive class, -1 means explicit negative class, and 0 means implicit negative class. gt_weights_batch: A list of 1-D tf.float32 tensors of shape [num_gt_boxes_i] containing weights for groundtruth boxes. unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] which is consistent with the classification target for each anchor (and can be empty for scalar targets). This shape must thus be compatible with the groundtruth labels that are passed to the "assign" function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). include_background_class: whether or not gt_class_confidences_batch includes the background class. implicit_class_weight: the weight assigned to implicit examples. Returns: batch_cls_targets: a tensor with shape [batch_size, num_anchors, num_classes], batch_cls_weights: a tensor with shape [batch_size, num_anchors, num_classes], batch_reg_targets: a tensor with shape [batch_size, num_anchors, box_code_dimension] batch_reg_weights: a tensor with shape [batch_size, num_anchors], match_list: a list of matcher.Match objects encoding the match between anchors and groundtruth boxes for each image of the batch, with rows of the Match objects corresponding to groundtruth boxes and columns corresponding to anchors. Raises: ValueError: if input list lengths are inconsistent, i.e., batch_size == len(gt_box_batch) == len(gt_class_targets_batch) and batch_size == len(anchors_batch) unless anchors_batch is a single BoxList, or if any element in gt_class_confidences_batch has rank > 2. """ if not isinstance(anchors_batch, list): anchors_batch = len(gt_box_batch) * [anchors_batch] if not all( isinstance(anchors, box_list.BoxList) for anchors in anchors_batch): raise ValueError('anchors_batch must be a BoxList or list of BoxLists.') if not (len(anchors_batch) == len(gt_box_batch) == len(gt_class_confidences_batch)): raise ValueError('batch size incompatible with lengths of anchors_batch, ' 'gt_box_batch and gt_class_confidences_batch.') cls_targets_list = [] cls_weights_list = [] reg_targets_list = [] reg_weights_list = [] match_list = [] if gt_weights_batch is None: gt_weights_batch = [None] * len(gt_class_confidences_batch) for anchors, gt_boxes, gt_class_confidences, gt_weights in zip( anchors_batch, gt_box_batch, gt_class_confidences_batch, gt_weights_batch): if (gt_class_confidences is not None and len(gt_class_confidences.get_shape().as_list()) > 2): raise ValueError('The shape of the class target is not supported. ', gt_class_confidences.get_shape()) cls_targets, _, reg_targets, _, match = target_assigner.assign( anchors, gt_boxes, gt_class_confidences, unmatched_class_label, groundtruth_weights=gt_weights) if include_background_class: cls_targets_without_background = tf.slice( cls_targets, [0, 1], [-1, -1]) else: cls_targets_without_background = cls_targets positive_mask = tf.greater(cls_targets_without_background, 0.0) negative_mask = tf.less(cls_targets_without_background, 0.0) explicit_example_mask = tf.logical_or(positive_mask, negative_mask) positive_anchors = tf.reduce_any(positive_mask, axis=-1) regression_weights = tf.to_float(positive_anchors) regression_targets = ( reg_targets * tf.expand_dims(regression_weights, axis=-1)) regression_weights_expanded = tf.expand_dims(regression_weights, axis=-1) cls_targets_without_background = ( cls_targets_without_background * (1 - tf.to_float(negative_mask))) cls_weights_without_background = ( (1 - implicit_class_weight) * tf.to_float(explicit_example_mask) + implicit_class_weight) if include_background_class: cls_weights_background = ( (1 - implicit_class_weight) * regression_weights_expanded + implicit_class_weight) classification_weights = tf.concat( [cls_weights_background, cls_weights_without_background], axis=-1) cls_targets_background = 1 - regression_weights_expanded classification_targets = tf.concat( [cls_targets_background, cls_targets_without_background], axis=-1) else: classification_targets = cls_targets_without_background classification_weights = cls_weights_without_background cls_targets_list.append(classification_targets) cls_weights_list.append(classification_weights) reg_targets_list.append(regression_targets) reg_weights_list.append(regression_weights) match_list.append(match) batch_cls_targets = tf.stack(cls_targets_list) batch_cls_weights = tf.stack(cls_weights_list) batch_reg_targets = tf.stack(reg_targets_list) batch_reg_weights = tf.stack(reg_weights_list) return (batch_cls_targets, batch_cls_weights, batch_reg_targets, batch_reg_weights, match_list)
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/target_assigner.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.core.prefetcher.""" import tensorflow as tf from object_detection.core import prefetcher slim = tf.contrib.slim class PrefetcherTest(tf.test.TestCase): def test_prefetch_tensors_with_fully_defined_shapes(self): with self.test_session() as sess: batch_size = 10 image_size = 32 num_batches = 5 examples = tf.Variable(tf.constant(0, dtype=tf.int64)) counter = examples.count_up_to(num_batches) image = tf.random_normal([batch_size, image_size, image_size, 3], dtype=tf.float32, name='images') label = tf.random_uniform([batch_size, 1], 0, 10, dtype=tf.int32, name='labels') prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter, 'image': image, 'label': label}, capacity=100) tensor_dict = prefetch_queue.dequeue() self.assertAllEqual(tensor_dict['image'].get_shape().as_list(), [batch_size, image_size, image_size, 3]) self.assertAllEqual(tensor_dict['label'].get_shape().as_list(), [batch_size, 1]) tf.initialize_all_variables().run() with slim.queues.QueueRunners(sess): for _ in range(num_batches): results = sess.run(tensor_dict) self.assertEquals(results['image'].shape, (batch_size, image_size, image_size, 3)) self.assertEquals(results['label'].shape, (batch_size, 1)) with self.assertRaises(tf.errors.OutOfRangeError): sess.run(tensor_dict) def test_prefetch_tensors_with_partially_defined_shapes(self): with self.test_session() as sess: batch_size = 10 image_size = 32 num_batches = 5 examples = tf.Variable(tf.constant(0, dtype=tf.int64)) counter = examples.count_up_to(num_batches) image = tf.random_normal([batch_size, tf.Variable(image_size), tf.Variable(image_size), 3], dtype=tf.float32, name='image') image.set_shape([batch_size, None, None, 3]) label = tf.random_uniform([batch_size, tf.Variable(1)], 0, 10, dtype=tf.int32, name='label') label.set_shape([batch_size, None]) prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter, 'image': image, 'label': label}, capacity=100) tensor_dict = prefetch_queue.dequeue() self.assertAllEqual(tensor_dict['image'].get_shape().as_list(), [batch_size, None, None, 3]) self.assertAllEqual(tensor_dict['label'].get_shape().as_list(), [batch_size, None]) tf.initialize_all_variables().run() with slim.queues.QueueRunners(sess): for _ in range(num_batches): results = sess.run(tensor_dict) self.assertEquals(results['image'].shape, (batch_size, image_size, image_size, 3)) self.assertEquals(results['label'].shape, (batch_size, 1)) with self.assertRaises(tf.errors.OutOfRangeError): sess.run(tensor_dict) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/prefetcher_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Interface for data decoders. Data decoders decode the input data and return a dictionary of tensors keyed by the entries in core.reader.Fields. """ from abc import ABCMeta from abc import abstractmethod class DataDecoder(object): """Interface for data decoders.""" __metaclass__ = ABCMeta @abstractmethod def decode(self, data): """Return a single image and associated labels. Args: data: a string tensor holding a serialized protocol buffer corresponding to data for a single image. Returns: tensor_dict: a dictionary containing tensors. Possible keys are defined in reader.Fields. """ pass
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/data_decoder.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Matcher interface and Match class. This module defines the Matcher interface and the Match object. The job of the matcher is to match row and column indices based on the similarity matrix and other optional parameters. Each column is matched to at most one row. There are three possibilities for the matching: 1) match: A column matches a row. 2) no_match: A column does not match any row. 3) ignore: A column that is neither 'match' nor no_match. The ignore case is regularly encountered in object detection: when an anchor has a relatively small overlap with a ground-truth box, one neither wants to consider this box a positive example (match) nor a negative example (no match). The Match class is used to store the match results and it provides simple apis to query the results. """ from abc import ABCMeta from abc import abstractmethod import tensorflow as tf from object_detection.utils import ops class Match(object): """Class to store results from the matcher. This class is used to store the results from the matcher. It provides convenient methods to query the matching results. """ def __init__(self, match_results, use_matmul_gather=False): """Constructs a Match object. Args: match_results: Integer tensor of shape [N] with (1) match_results[i]>=0, meaning that column i is matched with row match_results[i]. (2) match_results[i]=-1, meaning that column i is not matched. (3) match_results[i]=-2, meaning that column i is ignored. use_matmul_gather: Use matrix multiplication based gather instead of standard tf.gather. (Default: False). Raises: ValueError: if match_results does not have rank 1 or is not an integer int32 scalar tensor """ if match_results.shape.ndims != 1: raise ValueError('match_results should have rank 1') if match_results.dtype != tf.int32: raise ValueError('match_results should be an int32 or int64 scalar ' 'tensor') self._match_results = match_results self._gather_op = tf.gather if use_matmul_gather: self._gather_op = ops.matmul_gather_on_zeroth_axis @property def match_results(self): """The accessor for match results. Returns: the tensor which encodes the match results. """ return self._match_results def matched_column_indices(self): """Returns column indices that match to some row. The indices returned by this op are always sorted in increasing order. Returns: column_indices: int32 tensor of shape [K] with column indices. """ return self._reshape_and_cast(tf.where(tf.greater(self._match_results, -1))) def matched_column_indicator(self): """Returns column indices that are matched. Returns: column_indices: int32 tensor of shape [K] with column indices. """ return tf.greater_equal(self._match_results, 0) def num_matched_columns(self): """Returns number (int32 scalar tensor) of matched columns.""" return tf.size(self.matched_column_indices()) def unmatched_column_indices(self): """Returns column indices that do not match any row. The indices returned by this op are always sorted in increasing order. Returns: column_indices: int32 tensor of shape [K] with column indices. """ return self._reshape_and_cast(tf.where(tf.equal(self._match_results, -1))) def unmatched_column_indicator(self): """Returns column indices that are unmatched. Returns: column_indices: int32 tensor of shape [K] with column indices. """ return tf.equal(self._match_results, -1) def num_unmatched_columns(self): """Returns number (int32 scalar tensor) of unmatched columns.""" return tf.size(self.unmatched_column_indices()) def ignored_column_indices(self): """Returns column indices that are ignored (neither Matched nor Unmatched). The indices returned by this op are always sorted in increasing order. Returns: column_indices: int32 tensor of shape [K] with column indices. """ return self._reshape_and_cast(tf.where(self.ignored_column_indicator())) def ignored_column_indicator(self): """Returns boolean column indicator where True means the colum is ignored. Returns: column_indicator: boolean vector which is True for all ignored column indices. """ return tf.equal(self._match_results, -2) def num_ignored_columns(self): """Returns number (int32 scalar tensor) of matched columns.""" return tf.size(self.ignored_column_indices()) def unmatched_or_ignored_column_indices(self): """Returns column indices that are unmatched or ignored. The indices returned by this op are always sorted in increasing order. Returns: column_indices: int32 tensor of shape [K] with column indices. """ return self._reshape_and_cast(tf.where(tf.greater(0, self._match_results))) def matched_row_indices(self): """Returns row indices that match some column. The indices returned by this op are ordered so as to be in correspondence with the output of matched_column_indicator(). For example if self.matched_column_indicator() is [0,2], and self.matched_row_indices() is [7, 3], then we know that column 0 was matched to row 7 and column 2 was matched to row 3. Returns: row_indices: int32 tensor of shape [K] with row indices. """ return self._reshape_and_cast( self._gather_op(self._match_results, self.matched_column_indices())) def _reshape_and_cast(self, t): return tf.cast(tf.reshape(t, [-1]), tf.int32) def gather_based_on_match(self, input_tensor, unmatched_value, ignored_value): """Gathers elements from `input_tensor` based on match results. For columns that are matched to a row, gathered_tensor[col] is set to input_tensor[match_results[col]]. For columns that are unmatched, gathered_tensor[col] is set to unmatched_value. Finally, for columns that are ignored gathered_tensor[col] is set to ignored_value. Note that the input_tensor.shape[1:] must match with unmatched_value.shape and ignored_value.shape Args: input_tensor: Tensor to gather values from. unmatched_value: Constant tensor value for unmatched columns. ignored_value: Constant tensor value for ignored columns. Returns: gathered_tensor: A tensor containing values gathered from input_tensor. The shape of the gathered tensor is [match_results.shape[0]] + input_tensor.shape[1:]. """ input_tensor = tf.concat( [tf.stack([ignored_value, unmatched_value]), tf.to_float(input_tensor)], axis=0) gather_indices = tf.maximum(self.match_results + 2, 0) gathered_tensor = self._gather_op(input_tensor, gather_indices) return gathered_tensor class Matcher(object): """Abstract base class for matcher. """ __metaclass__ = ABCMeta def __init__(self, use_matmul_gather=False): """Constructs a Matcher. Args: use_matmul_gather: Force constructed match objects to use matrix multiplication based gather instead of standard tf.gather. (Default: False). """ self._use_matmul_gather = use_matmul_gather def match(self, similarity_matrix, valid_rows=None, scope=None): """Computes matches among row and column indices and returns the result. Computes matches among the row and column indices based on the similarity matrix and optional arguments. Args: similarity_matrix: Float tensor of shape [N, M] with pairwise similarity where higher value means more similar. valid_rows: A boolean tensor of shape [N] indicating the rows that are valid for matching. scope: Op scope name. Defaults to 'Match' if None. Returns: A Match object with the results of matching. """ with tf.name_scope(scope, 'Match') as scope: if valid_rows is None: valid_rows = tf.ones(tf.shape(similarity_matrix)[0], dtype=tf.bool) return Match(self._match(similarity_matrix, valid_rows), self._use_matmul_gather) @abstractmethod def _match(self, similarity_matrix, valid_rows): """Method to be overridden by implementations. Args: similarity_matrix: Float tensor of shape [N, M] with pairwise similarity where higher value means more similar. valid_rows: A boolean tensor of shape [N] indicating the rows that are valid for matching. Returns: match_results: Integer tensor of shape [M]: match_results[i]>=0 means that column i is matched to row match_results[i], match_results[i]=-1 means that the column is not matched. match_results[i]=-2 means that the column is ignored (usually this happens when there is a very weak match which one neither wants as positive nor negative example). """ pass
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/matcher.py
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.core.freezable_batch_norm.""" import numpy as np import tensorflow as tf from object_detection.core import freezable_batch_norm class FreezableBatchNormTest(tf.test.TestCase): """Tests for FreezableBatchNorm operations.""" def _build_model(self, training=None): model = tf.keras.models.Sequential() norm = freezable_batch_norm.FreezableBatchNorm(training=training, input_shape=(10,), momentum=0.8) model.add(norm) return model, norm def _train_freezable_batch_norm(self, training_mean, training_var): model, _ = self._build_model() model.compile(loss='mse', optimizer='sgd') # centered on training_mean, variance training_var train_data = np.random.normal( loc=training_mean, scale=training_var, size=(1000, 10)) model.fit(train_data, train_data, epochs=4, verbose=0) return model.weights def test_batchnorm_freezing_training_true(self): with self.test_session(): training_mean = 5.0 training_var = 10.0 testing_mean = -10.0 testing_var = 5.0 # Initially train the batch norm, and save the weights trained_weights = self._train_freezable_batch_norm(training_mean, training_var) # Load the batch norm weights, freezing training to True. # Apply the batch norm layer to testing data and ensure it is normalized # according to the batch statistics. model, norm = self._build_model(training=True) for trained_weight, blank_weight in zip(trained_weights, model.weights): weight_copy = blank_weight.assign(tf.keras.backend.eval(trained_weight)) tf.keras.backend.eval(weight_copy) # centered on testing_mean, variance testing_var test_data = np.random.normal( loc=testing_mean, scale=testing_var, size=(1000, 10)) out_tensor = norm(tf.convert_to_tensor(test_data, dtype=tf.float32)) out = tf.keras.backend.eval(out_tensor) out -= tf.keras.backend.eval(norm.beta) out /= tf.keras.backend.eval(norm.gamma) np.testing.assert_allclose(out.mean(), 0.0, atol=1.5e-1) np.testing.assert_allclose(out.std(), 1.0, atol=1.5e-1) def test_batchnorm_freezing_training_false(self): with self.test_session(): training_mean = 5.0 training_var = 10.0 testing_mean = -10.0 testing_var = 5.0 # Initially train the batch norm, and save the weights trained_weights = self._train_freezable_batch_norm(training_mean, training_var) # Load the batch norm back up, freezing training to False. # Apply the batch norm layer to testing data and ensure it is normalized # according to the training data's statistics. model, norm = self._build_model(training=False) for trained_weight, blank_weight in zip(trained_weights, model.weights): weight_copy = blank_weight.assign(tf.keras.backend.eval(trained_weight)) tf.keras.backend.eval(weight_copy) # centered on testing_mean, variance testing_var test_data = np.random.normal( loc=testing_mean, scale=testing_var, size=(1000, 10)) out_tensor = norm(tf.convert_to_tensor(test_data, dtype=tf.float32)) out = tf.keras.backend.eval(out_tensor) out -= tf.keras.backend.eval(norm.beta) out /= tf.keras.backend.eval(norm.gamma) out *= training_var out += (training_mean - testing_mean) out /= testing_var np.testing.assert_allclose(out.mean(), 0.0, atol=1.5e-1) np.testing.assert_allclose(out.std(), 1.0, atol=1.5e-1) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/freezable_batch_norm_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.core.box_list_ops.""" import numpy as np import tensorflow as tf from object_detection.core import box_list from object_detection.core import box_list_ops from object_detection.utils import test_case class BoxListOpsTest(test_case.TestCase): """Tests for common bounding box operations.""" def test_area(self): corners = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]]) exp_output = [200.0, 4.0] boxes = box_list.BoxList(corners) areas = box_list_ops.area(boxes) with self.test_session() as sess: areas_output = sess.run(areas) self.assertAllClose(areas_output, exp_output) def test_height_width(self): corners = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]]) exp_output_heights = [10., 2.] exp_output_widths = [20., 2.] boxes = box_list.BoxList(corners) heights, widths = box_list_ops.height_width(boxes) with self.test_session() as sess: output_heights, output_widths = sess.run([heights, widths]) self.assertAllClose(output_heights, exp_output_heights) self.assertAllClose(output_widths, exp_output_widths) def test_scale(self): corners = tf.constant([[0, 0, 100, 200], [50, 120, 100, 140]], dtype=tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('extra_data', tf.constant([[1], [2]])) y_scale = tf.constant(1.0/100) x_scale = tf.constant(1.0/200) scaled_boxes = box_list_ops.scale(boxes, y_scale, x_scale) exp_output = [[0, 0, 1, 1], [0.5, 0.6, 1.0, 0.7]] with self.test_session() as sess: scaled_corners_out = sess.run(scaled_boxes.get()) self.assertAllClose(scaled_corners_out, exp_output) extra_data_out = sess.run(scaled_boxes.get_field('extra_data')) self.assertAllEqual(extra_data_out, [[1], [2]]) def test_clip_to_window_filter_boxes_which_fall_outside_the_window( self): window = tf.constant([0, 0, 9, 14], tf.float32) corners = tf.constant([[5.0, 5.0, 6.0, 6.0], [-1.0, -2.0, 4.0, 5.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], [-100.0, -100.0, 300.0, 600.0], [-10.0, -10.0, -9.0, -9.0]]) boxes = box_list.BoxList(corners) boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) exp_output = [[5.0, 5.0, 6.0, 6.0], [0.0, 0.0, 4.0, 5.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], [0.0, 0.0, 9.0, 14.0]] pruned = box_list_ops.clip_to_window( boxes, window, filter_nonoverlapping=True) with self.test_session() as sess: pruned_output = sess.run(pruned.get()) self.assertAllClose(pruned_output, exp_output) extra_data_out = sess.run(pruned.get_field('extra_data')) self.assertAllEqual(extra_data_out, [[1], [2], [3], [4], [5]]) def test_clip_to_window_without_filtering_boxes_which_fall_outside_the_window( self): window = tf.constant([0, 0, 9, 14], tf.float32) corners = tf.constant([[5.0, 5.0, 6.0, 6.0], [-1.0, -2.0, 4.0, 5.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], [-100.0, -100.0, 300.0, 600.0], [-10.0, -10.0, -9.0, -9.0]]) boxes = box_list.BoxList(corners) boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) exp_output = [[5.0, 5.0, 6.0, 6.0], [0.0, 0.0, 4.0, 5.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], [0.0, 0.0, 9.0, 14.0], [0.0, 0.0, 0.0, 0.0]] pruned = box_list_ops.clip_to_window( boxes, window, filter_nonoverlapping=False) with self.test_session() as sess: pruned_output = sess.run(pruned.get()) self.assertAllClose(pruned_output, exp_output) extra_data_out = sess.run(pruned.get_field('extra_data')) self.assertAllEqual(extra_data_out, [[1], [2], [3], [4], [5], [6]]) def test_prune_outside_window_filters_boxes_which_fall_outside_the_window( self): window = tf.constant([0, 0, 9, 14], tf.float32) corners = tf.constant([[5.0, 5.0, 6.0, 6.0], [-1.0, -2.0, 4.0, 5.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], [-10.0, -10.0, -9.0, -9.0], [-100.0, -100.0, 300.0, 600.0]]) boxes = box_list.BoxList(corners) boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) exp_output = [[5.0, 5.0, 6.0, 6.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0]] pruned, keep_indices = box_list_ops.prune_outside_window(boxes, window) with self.test_session() as sess: pruned_output = sess.run(pruned.get()) self.assertAllClose(pruned_output, exp_output) keep_indices_out = sess.run(keep_indices) self.assertAllEqual(keep_indices_out, [0, 2, 3]) extra_data_out = sess.run(pruned.get_field('extra_data')) self.assertAllEqual(extra_data_out, [[1], [3], [4]]) def test_prune_completely_outside_window(self): window = tf.constant([0, 0, 9, 14], tf.float32) corners = tf.constant([[5.0, 5.0, 6.0, 6.0], [-1.0, -2.0, 4.0, 5.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], [-10.0, -10.0, -9.0, -9.0], [-100.0, -100.0, 300.0, 600.0]]) boxes = box_list.BoxList(corners) boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) exp_output = [[5.0, 5.0, 6.0, 6.0], [-1.0, -2.0, 4.0, 5.0], [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], [-100.0, -100.0, 300.0, 600.0]] pruned, keep_indices = box_list_ops.prune_completely_outside_window(boxes, window) with self.test_session() as sess: pruned_output = sess.run(pruned.get()) self.assertAllClose(pruned_output, exp_output) keep_indices_out = sess.run(keep_indices) self.assertAllEqual(keep_indices_out, [0, 1, 2, 3, 5]) extra_data_out = sess.run(pruned.get_field('extra_data')) self.assertAllEqual(extra_data_out, [[1], [2], [3], [4], [6]]) def test_prune_completely_outside_window_with_empty_boxlist(self): window = tf.constant([0, 0, 9, 14], tf.float32) corners = tf.zeros(shape=[0, 4], dtype=tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('extra_data', tf.zeros(shape=[0], dtype=tf.int32)) pruned, keep_indices = box_list_ops.prune_completely_outside_window(boxes, window) pruned_boxes = pruned.get() extra = pruned.get_field('extra_data') exp_pruned_boxes = np.zeros(shape=[0, 4], dtype=np.float32) exp_extra = np.zeros(shape=[0], dtype=np.int32) with self.test_session() as sess: pruned_boxes_out, keep_indices_out, extra_out = sess.run( [pruned_boxes, keep_indices, extra]) self.assertAllClose(exp_pruned_boxes, pruned_boxes_out) self.assertAllEqual([], keep_indices_out) self.assertAllEqual(exp_extra, extra_out) def test_intersection(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) exp_output = [[2.0, 0.0, 6.0], [1.0, 0.0, 5.0]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) intersect = box_list_ops.intersection(boxes1, boxes2) with self.test_session() as sess: intersect_output = sess.run(intersect) self.assertAllClose(intersect_output, exp_output) def test_matched_intersection(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) exp_output = [2.0, 0.0] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) intersect = box_list_ops.matched_intersection(boxes1, boxes2) with self.test_session() as sess: intersect_output = sess.run(intersect) self.assertAllClose(intersect_output, exp_output) def test_iou(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0], [1.0 / 16.0, 0.0, 5.0 / 400.0]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou = box_list_ops.iou(boxes1, boxes2) with self.test_session() as sess: iou_output = sess.run(iou) self.assertAllClose(iou_output, exp_output) def test_matched_iou(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) exp_output = [2.0 / 16.0, 0] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou = box_list_ops.matched_iou(boxes1, boxes2) with self.test_session() as sess: iou_output = sess.run(iou) self.assertAllClose(iou_output, exp_output) def test_iouworks_on_empty_inputs(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) boxes_empty = box_list.BoxList(tf.zeros((0, 4))) iou_empty_1 = box_list_ops.iou(boxes1, boxes_empty) iou_empty_2 = box_list_ops.iou(boxes_empty, boxes2) iou_empty_3 = box_list_ops.iou(boxes_empty, boxes_empty) with self.test_session() as sess: iou_output_1, iou_output_2, iou_output_3 = sess.run( [iou_empty_1, iou_empty_2, iou_empty_3]) self.assertAllEqual(iou_output_1.shape, (2, 0)) self.assertAllEqual(iou_output_2.shape, (0, 3)) self.assertAllEqual(iou_output_3.shape, (0, 0)) def test_ioa(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) exp_output_1 = [[2.0 / 12.0, 0, 6.0 / 400.0], [1.0 / 12.0, 0.0, 5.0 / 400.0]] exp_output_2 = [[2.0 / 6.0, 1.0 / 5.0], [0, 0], [6.0 / 6.0, 5.0 / 5.0]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) ioa_1 = box_list_ops.ioa(boxes1, boxes2) ioa_2 = box_list_ops.ioa(boxes2, boxes1) with self.test_session() as sess: ioa_output_1, ioa_output_2 = sess.run([ioa_1, ioa_2]) self.assertAllClose(ioa_output_1, exp_output_1) self.assertAllClose(ioa_output_2, exp_output_2) def test_prune_non_overlapping_boxes(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) minoverlap = 0.5 exp_output_1 = boxes1 exp_output_2 = box_list.BoxList(tf.constant(0.0, shape=[0, 4])) output_1, keep_indices_1 = box_list_ops.prune_non_overlapping_boxes( boxes1, boxes2, min_overlap=minoverlap) output_2, keep_indices_2 = box_list_ops.prune_non_overlapping_boxes( boxes2, boxes1, min_overlap=minoverlap) with self.test_session() as sess: (output_1_, keep_indices_1_, output_2_, keep_indices_2_, exp_output_1_, exp_output_2_) = sess.run( [output_1.get(), keep_indices_1, output_2.get(), keep_indices_2, exp_output_1.get(), exp_output_2.get()]) self.assertAllClose(output_1_, exp_output_1_) self.assertAllClose(output_2_, exp_output_2_) self.assertAllEqual(keep_indices_1_, [0, 1]) self.assertAllEqual(keep_indices_2_, []) def test_prune_small_boxes(self): boxes = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0], [3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) exp_boxes = [[3.0, 4.0, 6.0, 8.0], [0.0, 0.0, 20.0, 20.0]] boxes = box_list.BoxList(boxes) pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3) with self.test_session() as sess: pruned_boxes = sess.run(pruned_boxes.get()) self.assertAllEqual(pruned_boxes, exp_boxes) def test_prune_small_boxes_prunes_boxes_with_negative_side(self): boxes = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0], [3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0], [2.0, 3.0, 1.5, 7.0], # negative height [2.0, 3.0, 5.0, 1.7]]) # negative width exp_boxes = [[3.0, 4.0, 6.0, 8.0], [0.0, 0.0, 20.0, 20.0]] boxes = box_list.BoxList(boxes) pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3) with self.test_session() as sess: pruned_boxes = sess.run(pruned_boxes.get()) self.assertAllEqual(pruned_boxes, exp_boxes) def test_change_coordinate_frame(self): corners = tf.constant([[0.25, 0.5, 0.75, 0.75], [0.5, 0.0, 1.0, 1.0]]) window = tf.constant([0.25, 0.25, 0.75, 0.75]) boxes = box_list.BoxList(corners) expected_corners = tf.constant([[0, 0.5, 1.0, 1.0], [0.5, -0.5, 1.5, 1.5]]) expected_boxes = box_list.BoxList(expected_corners) output = box_list_ops.change_coordinate_frame(boxes, window) with self.test_session() as sess: output_, expected_boxes_ = sess.run([output.get(), expected_boxes.get()]) self.assertAllClose(output_, expected_boxes_) def test_ioaworks_on_empty_inputs(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) boxes_empty = box_list.BoxList(tf.zeros((0, 4))) ioa_empty_1 = box_list_ops.ioa(boxes1, boxes_empty) ioa_empty_2 = box_list_ops.ioa(boxes_empty, boxes2) ioa_empty_3 = box_list_ops.ioa(boxes_empty, boxes_empty) with self.test_session() as sess: ioa_output_1, ioa_output_2, ioa_output_3 = sess.run( [ioa_empty_1, ioa_empty_2, ioa_empty_3]) self.assertAllEqual(ioa_output_1.shape, (2, 0)) self.assertAllEqual(ioa_output_2.shape, (0, 3)) self.assertAllEqual(ioa_output_3.shape, (0, 0)) def test_pairwise_distances(self): corners1 = tf.constant([[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.0, 2.0]]) corners2 = tf.constant([[3.0, 4.0, 1.0, 0.0], [-4.0, 0.0, 0.0, 3.0], [0.0, 0.0, 0.0, 0.0]]) exp_output = [[26, 25, 0], [18, 27, 6]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) dist_matrix = box_list_ops.sq_dist(boxes1, boxes2) with self.test_session() as sess: dist_output = sess.run(dist_matrix) self.assertAllClose(dist_output, exp_output) def test_boolean_mask(self): corners = tf.constant( [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) indicator = tf.constant([True, False, True, False, True], tf.bool) expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] boxes = box_list.BoxList(corners) subset = box_list_ops.boolean_mask(boxes, indicator) with self.test_session() as sess: subset_output = sess.run(subset.get()) self.assertAllClose(subset_output, expected_subset) def test_static_boolean_mask_with_field(self): def graph_fn(corners, weights, indicator): boxes = box_list.BoxList(corners) boxes.add_field('weights', weights) subset = box_list_ops.boolean_mask( boxes, indicator, ['weights'], use_static_shapes=True, indicator_sum=3) return (subset.get_field('boxes'), subset.get_field('weights')) corners = np.array( [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]], dtype=np.float32) indicator = np.array([True, False, True, False, True], dtype=np.bool) weights = np.array([[.1], [.3], [.5], [.7], [.9]], dtype=np.float32) result_boxes, result_weights = self.execute(graph_fn, [corners, weights, indicator]) expected_boxes = [4 * [0.0], 4 * [2.0], 4 * [4.0]] expected_weights = [[.1], [.5], [.9]] self.assertAllClose(result_boxes, expected_boxes) self.assertAllClose(result_weights, expected_weights) def test_dynamic_boolean_mask_with_field(self): corners = tf.placeholder(tf.float32, [None, 4]) indicator = tf.placeholder(tf.bool, [None]) weights = tf.placeholder(tf.float32, [None, 1]) expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] expected_weights = [[.1], [.5], [.9]] boxes = box_list.BoxList(corners) boxes.add_field('weights', weights) subset = box_list_ops.boolean_mask(boxes, indicator, ['weights']) with self.test_session() as sess: subset_output, weights_output = sess.run( [subset.get(), subset.get_field('weights')], feed_dict={ corners: np.array( [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]), indicator: np.array([True, False, True, False, True]).astype(np.bool), weights: np.array([[.1], [.3], [.5], [.7], [.9]]) }) self.assertAllClose(subset_output, expected_subset) self.assertAllClose(weights_output, expected_weights) def test_gather(self): corners = tf.constant( [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) indices = tf.constant([0, 2, 4], tf.int32) expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] boxes = box_list.BoxList(corners) subset = box_list_ops.gather(boxes, indices) with self.test_session() as sess: subset_output = sess.run(subset.get()) self.assertAllClose(subset_output, expected_subset) def test_static_gather_with_field(self): def graph_fn(corners, weights, indices): boxes = box_list.BoxList(corners) boxes.add_field('weights', weights) subset = box_list_ops.gather( boxes, indices, ['weights'], use_static_shapes=True) return (subset.get_field('boxes'), subset.get_field('weights')) corners = np.array([4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]], dtype=np.float32) weights = np.array([[.1], [.3], [.5], [.7], [.9]], dtype=np.float32) indices = np.array([0, 2, 4], dtype=np.int32) result_boxes, result_weights = self.execute(graph_fn, [corners, weights, indices]) expected_boxes = [4 * [0.0], 4 * [2.0], 4 * [4.0]] expected_weights = [[.1], [.5], [.9]] self.assertAllClose(result_boxes, expected_boxes) self.assertAllClose(result_weights, expected_weights) def test_dynamic_gather_with_field(self): corners = tf.placeholder(tf.float32, [None, 4]) indices = tf.placeholder(tf.int32, [None]) weights = tf.placeholder(tf.float32, [None, 1]) expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] expected_weights = [[.1], [.5], [.9]] boxes = box_list.BoxList(corners) boxes.add_field('weights', weights) subset = box_list_ops.gather(boxes, indices, ['weights'], use_static_shapes=True) with self.test_session() as sess: subset_output, weights_output = sess.run( [subset.get(), subset.get_field('weights')], feed_dict={ corners: np.array( [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]), indices: np.array([0, 2, 4]).astype(np.int32), weights: np.array([[.1], [.3], [.5], [.7], [.9]]) }) self.assertAllClose(subset_output, expected_subset) self.assertAllClose(weights_output, expected_weights) def test_gather_with_invalid_field(self): corners = tf.constant([4 * [0.0], 4 * [1.0]]) indices = tf.constant([0, 1], tf.int32) weights = tf.constant([[.1], [.3]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('weights', weights) with self.assertRaises(ValueError): box_list_ops.gather(boxes, indices, ['foo', 'bar']) def test_gather_with_invalid_inputs(self): corners = tf.constant( [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) indices_float32 = tf.constant([0, 2, 4], tf.float32) boxes = box_list.BoxList(corners) with self.assertRaises(ValueError): _ = box_list_ops.gather(boxes, indices_float32) indices_2d = tf.constant([[0, 2, 4]], tf.int32) boxes = box_list.BoxList(corners) with self.assertRaises(ValueError): _ = box_list_ops.gather(boxes, indices_2d) def test_gather_with_dynamic_indexing(self): corners = tf.constant([4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0] ]) weights = tf.constant([.5, .3, .7, .1, .9], tf.float32) indices = tf.reshape(tf.where(tf.greater(weights, 0.4)), [-1]) expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] expected_weights = [.5, .7, .9] boxes = box_list.BoxList(corners) boxes.add_field('weights', weights) subset = box_list_ops.gather(boxes, indices, ['weights']) with self.test_session() as sess: subset_output, weights_output = sess.run([subset.get(), subset.get_field( 'weights')]) self.assertAllClose(subset_output, expected_subset) self.assertAllClose(weights_output, expected_weights) def test_sort_by_field_ascending_order(self): exp_corners = [[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]] exp_scores = [.95, .9, .75, .6, .5, .3] exp_weights = [.2, .45, .6, .75, .8, .92] shuffle = [2, 4, 0, 5, 1, 3] corners = tf.constant([exp_corners[i] for i in shuffle], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant( [exp_scores[i] for i in shuffle], tf.float32)) boxes.add_field('weights', tf.constant( [exp_weights[i] for i in shuffle], tf.float32)) sort_by_weight = box_list_ops.sort_by_field( boxes, 'weights', order=box_list_ops.SortOrder.ascend) with self.test_session() as sess: corners_out, scores_out, weights_out = sess.run([ sort_by_weight.get(), sort_by_weight.get_field('scores'), sort_by_weight.get_field('weights')]) self.assertAllClose(corners_out, exp_corners) self.assertAllClose(scores_out, exp_scores) self.assertAllClose(weights_out, exp_weights) def test_sort_by_field_descending_order(self): exp_corners = [[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]] exp_scores = [.95, .9, .75, .6, .5, .3] exp_weights = [.2, .45, .6, .75, .8, .92] shuffle = [2, 4, 0, 5, 1, 3] corners = tf.constant([exp_corners[i] for i in shuffle], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant( [exp_scores[i] for i in shuffle], tf.float32)) boxes.add_field('weights', tf.constant( [exp_weights[i] for i in shuffle], tf.float32)) sort_by_score = box_list_ops.sort_by_field(boxes, 'scores') with self.test_session() as sess: corners_out, scores_out, weights_out = sess.run([sort_by_score.get( ), sort_by_score.get_field('scores'), sort_by_score.get_field('weights')]) self.assertAllClose(corners_out, exp_corners) self.assertAllClose(scores_out, exp_scores) self.assertAllClose(weights_out, exp_weights) def test_sort_by_field_invalid_inputs(self): corners = tf.constant([4 * [0.0], 4 * [0.5], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) misc = tf.constant([[.95, .9], [.5, .3]], tf.float32) weights = tf.constant([.1, .2], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('misc', misc) boxes.add_field('weights', weights) with self.assertRaises(ValueError): box_list_ops.sort_by_field(boxes, 'area') with self.assertRaises(ValueError): box_list_ops.sort_by_field(boxes, 'misc') with self.assertRaises(ValueError): box_list_ops.sort_by_field(boxes, 'weights') def test_visualize_boxes_in_image(self): image = tf.zeros((6, 4, 3)) corners = tf.constant([[0, 0, 5, 3], [0, 0, 3, 2]], tf.float32) boxes = box_list.BoxList(corners) image_and_boxes = box_list_ops.visualize_boxes_in_image(image, boxes) image_and_boxes_bw = tf.to_float( tf.greater(tf.reduce_sum(image_and_boxes, 2), 0.0)) exp_result = [[1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0], [1, 0, 1, 0], [1, 1, 1, 0], [0, 0, 0, 0]] with self.test_session() as sess: output = sess.run(image_and_boxes_bw) self.assertAllEqual(output.astype(int), exp_result) def test_filter_field_value_equals(self): corners = tf.constant([[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('classes', tf.constant([1, 2, 1, 2, 2, 1])) exp_output1 = [[0, 0, 1, 1], [0, -0.1, 1, 0.9], [0, 100, 1, 101]] exp_output2 = [[0, 0.1, 1, 1.1], [0, 10, 1, 11], [0, 10.1, 1, 11.1]] filtered_boxes1 = box_list_ops.filter_field_value_equals( boxes, 'classes', 1) filtered_boxes2 = box_list_ops.filter_field_value_equals( boxes, 'classes', 2) with self.test_session() as sess: filtered_output1, filtered_output2 = sess.run([filtered_boxes1.get(), filtered_boxes2.get()]) self.assertAllClose(filtered_output1, exp_output1) self.assertAllClose(filtered_output2, exp_output2) def test_filter_greater_than(self): corners = tf.constant([[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant([.1, .75, .9, .5, .5, .8])) thresh = .6 exp_output = [[0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 100, 1, 101]] filtered_boxes = box_list_ops.filter_greater_than(boxes, thresh) with self.test_session() as sess: filtered_output = sess.run(filtered_boxes.get()) self.assertAllClose(filtered_output, exp_output) def test_clip_box_list(self): boxlist = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], [0.6, 0.6, 0.8, 0.8], [0.2, 0.2, 0.3, 0.3]], tf.float32)) boxlist.add_field('classes', tf.constant([0, 0, 1, 1])) boxlist.add_field('scores', tf.constant([0.75, 0.65, 0.3, 0.2])) num_boxes = 2 clipped_boxlist = box_list_ops.pad_or_clip_box_list(boxlist, num_boxes) expected_boxes = [[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]] expected_classes = [0, 0] expected_scores = [0.75, 0.65] with self.test_session() as sess: boxes_out, classes_out, scores_out = sess.run( [clipped_boxlist.get(), clipped_boxlist.get_field('classes'), clipped_boxlist.get_field('scores')]) self.assertAllClose(expected_boxes, boxes_out) self.assertAllEqual(expected_classes, classes_out) self.assertAllClose(expected_scores, scores_out) def test_pad_box_list(self): boxlist = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], tf.float32)) boxlist.add_field('classes', tf.constant([0, 1])) boxlist.add_field('scores', tf.constant([0.75, 0.2])) num_boxes = 4 padded_boxlist = box_list_ops.pad_or_clip_box_list(boxlist, num_boxes) expected_boxes = [[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], [0, 0, 0, 0], [0, 0, 0, 0]] expected_classes = [0, 1, 0, 0] expected_scores = [0.75, 0.2, 0, 0] with self.test_session() as sess: boxes_out, classes_out, scores_out = sess.run( [padded_boxlist.get(), padded_boxlist.get_field('classes'), padded_boxlist.get_field('scores')]) self.assertAllClose(expected_boxes, boxes_out) self.assertAllEqual(expected_classes, classes_out) self.assertAllClose(expected_scores, scores_out) def test_select_random_box(self): boxes = [[0., 0., 1., 1.], [0., 1., 2., 3.], [0., 2., 3., 4.]] corners = tf.constant(boxes, dtype=tf.float32) boxlist = box_list.BoxList(corners) random_bbox, valid = box_list_ops.select_random_box(boxlist) with self.test_session() as sess: random_bbox_out, valid_out = sess.run([random_bbox, valid]) norm_small = any( [np.linalg.norm(random_bbox_out - box) < 1e-6 for box in boxes]) self.assertTrue(norm_small) self.assertTrue(valid_out) def test_select_random_box_with_empty_boxlist(self): corners = tf.constant([], shape=[0, 4], dtype=tf.float32) boxlist = box_list.BoxList(corners) random_bbox, valid = box_list_ops.select_random_box(boxlist) with self.test_session() as sess: random_bbox_out, valid_out = sess.run([random_bbox, valid]) expected_bbox_out = np.array([[-1., -1., -1., -1.]], dtype=np.float32) self.assertAllEqual(expected_bbox_out, random_bbox_out) self.assertFalse(valid_out) def test_get_minimal_coverage_box(self): boxes = [[0., 0., 1., 1.], [-1., 1., 2., 3.], [0., 2., 3., 4.]] expected_coverage_box = [[-1., 0., 3., 4.]] corners = tf.constant(boxes, dtype=tf.float32) boxlist = box_list.BoxList(corners) coverage_box = box_list_ops.get_minimal_coverage_box(boxlist) with self.test_session() as sess: coverage_box_out = sess.run(coverage_box) self.assertAllClose(expected_coverage_box, coverage_box_out) def test_get_minimal_coverage_box_with_empty_boxlist(self): corners = tf.constant([], shape=[0, 4], dtype=tf.float32) boxlist = box_list.BoxList(corners) coverage_box = box_list_ops.get_minimal_coverage_box(boxlist) with self.test_session() as sess: coverage_box_out = sess.run(coverage_box) self.assertAllClose([[0.0, 0.0, 1.0, 1.0]], coverage_box_out) class ConcatenateTest(tf.test.TestCase): def test_invalid_input_box_list_list(self): with self.assertRaises(ValueError): box_list_ops.concatenate(None) with self.assertRaises(ValueError): box_list_ops.concatenate([]) with self.assertRaises(ValueError): corners = tf.constant([[0, 0, 0, 0]], tf.float32) boxlist = box_list.BoxList(corners) box_list_ops.concatenate([boxlist, 2]) def test_concatenate_with_missing_fields(self): corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32) scores1 = tf.constant([1.0, 2.1]) corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8]], tf.float32) boxlist1 = box_list.BoxList(corners1) boxlist1.add_field('scores', scores1) boxlist2 = box_list.BoxList(corners2) with self.assertRaises(ValueError): box_list_ops.concatenate([boxlist1, boxlist2]) def test_concatenate_with_incompatible_field_shapes(self): corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32) scores1 = tf.constant([1.0, 2.1]) corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8]], tf.float32) scores2 = tf.constant([[1.0, 1.0], [2.1, 3.2]]) boxlist1 = box_list.BoxList(corners1) boxlist1.add_field('scores', scores1) boxlist2 = box_list.BoxList(corners2) boxlist2.add_field('scores', scores2) with self.assertRaises(ValueError): box_list_ops.concatenate([boxlist1, boxlist2]) def test_concatenate_is_correct(self): corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32) scores1 = tf.constant([1.0, 2.1]) corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8], [1, 0, 5, 10]], tf.float32) scores2 = tf.constant([1.0, 2.1, 5.6]) exp_corners = [[0, 0, 0, 0], [1, 2, 3, 4], [0, 3, 1, 6], [2, 4, 3, 8], [1, 0, 5, 10]] exp_scores = [1.0, 2.1, 1.0, 2.1, 5.6] boxlist1 = box_list.BoxList(corners1) boxlist1.add_field('scores', scores1) boxlist2 = box_list.BoxList(corners2) boxlist2.add_field('scores', scores2) result = box_list_ops.concatenate([boxlist1, boxlist2]) with self.test_session() as sess: corners_output, scores_output = sess.run( [result.get(), result.get_field('scores')]) self.assertAllClose(corners_output, exp_corners) self.assertAllClose(scores_output, exp_scores) class NonMaxSuppressionTest(tf.test.TestCase): def test_select_from_three_clusters(self): corners = tf.constant([[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant([.9, .75, .6, .95, .5, .3])) iou_thresh = .5 max_output_size = 3 exp_nms = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 100, 1, 101]] nms = box_list_ops.non_max_suppression( boxes, iou_thresh, max_output_size) with self.test_session() as sess: nms_output = sess.run(nms.get()) self.assertAllClose(nms_output, exp_nms) def test_select_at_most_two_boxes_from_three_clusters(self): corners = tf.constant([[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant([.9, .75, .6, .95, .5, .3])) iou_thresh = .5 max_output_size = 2 exp_nms = [[0, 10, 1, 11], [0, 0, 1, 1]] nms = box_list_ops.non_max_suppression( boxes, iou_thresh, max_output_size) with self.test_session() as sess: nms_output = sess.run(nms.get()) self.assertAllClose(nms_output, exp_nms) def test_select_at_most_thirty_boxes_from_three_clusters(self): corners = tf.constant([[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant([.9, .75, .6, .95, .5, .3])) iou_thresh = .5 max_output_size = 30 exp_nms = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 100, 1, 101]] nms = box_list_ops.non_max_suppression( boxes, iou_thresh, max_output_size) with self.test_session() as sess: nms_output = sess.run(nms.get()) self.assertAllClose(nms_output, exp_nms) def test_select_single_box(self): corners = tf.constant([[0, 0, 1, 1]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant([.9])) iou_thresh = .5 max_output_size = 3 exp_nms = [[0, 0, 1, 1]] nms = box_list_ops.non_max_suppression( boxes, iou_thresh, max_output_size) with self.test_session() as sess: nms_output = sess.run(nms.get()) self.assertAllClose(nms_output, exp_nms) def test_select_from_ten_identical_boxes(self): corners = tf.constant(10 * [[0, 0, 1, 1]], tf.float32) boxes = box_list.BoxList(corners) boxes.add_field('scores', tf.constant(10 * [.9])) iou_thresh = .5 max_output_size = 3 exp_nms = [[0, 0, 1, 1]] nms = box_list_ops.non_max_suppression( boxes, iou_thresh, max_output_size) with self.test_session() as sess: nms_output = sess.run(nms.get()) self.assertAllClose(nms_output, exp_nms) def test_copy_extra_fields(self): corners = tf.constant([[0, 0, 1, 1], [0, 0.1, 1, 1.1]], tf.float32) boxes = box_list.BoxList(corners) tensor1 = np.array([[1], [4]]) tensor2 = np.array([[1, 1], [2, 2]]) boxes.add_field('tensor1', tf.constant(tensor1)) boxes.add_field('tensor2', tf.constant(tensor2)) new_boxes = box_list.BoxList(tf.constant([[0, 0, 10, 10], [1, 3, 5, 5]], tf.float32)) new_boxes = box_list_ops._copy_extra_fields(new_boxes, boxes) with self.test_session() as sess: self.assertAllClose(tensor1, sess.run(new_boxes.get_field('tensor1'))) self.assertAllClose(tensor2, sess.run(new_boxes.get_field('tensor2'))) class CoordinatesConversionTest(tf.test.TestCase): def test_to_normalized_coordinates(self): coordinates = tf.constant([[0, 0, 100, 100], [25, 25, 75, 75]], tf.float32) img = tf.ones((128, 100, 100, 3)) boxlist = box_list.BoxList(coordinates) normalized_boxlist = box_list_ops.to_normalized_coordinates( boxlist, tf.shape(img)[1], tf.shape(img)[2]) expected_boxes = [[0, 0, 1, 1], [0.25, 0.25, 0.75, 0.75]] with self.test_session() as sess: normalized_boxes = sess.run(normalized_boxlist.get()) self.assertAllClose(normalized_boxes, expected_boxes) def test_to_normalized_coordinates_already_normalized(self): coordinates = tf.constant([[0, 0, 1, 1], [0.25, 0.25, 0.75, 0.75]], tf.float32) img = tf.ones((128, 100, 100, 3)) boxlist = box_list.BoxList(coordinates) normalized_boxlist = box_list_ops.to_normalized_coordinates( boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: with self.assertRaisesOpError('assertion failed'): sess.run(normalized_boxlist.get()) def test_to_absolute_coordinates(self): coordinates = tf.constant([[0, 0, 1, 1], [0.25, 0.25, 0.75, 0.75]], tf.float32) img = tf.ones((128, 100, 100, 3)) boxlist = box_list.BoxList(coordinates) absolute_boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) expected_boxes = [[0, 0, 100, 100], [25, 25, 75, 75]] with self.test_session() as sess: absolute_boxes = sess.run(absolute_boxlist.get()) self.assertAllClose(absolute_boxes, expected_boxes) def test_to_absolute_coordinates_already_abolute(self): coordinates = tf.constant([[0, 0, 100, 100], [25, 25, 75, 75]], tf.float32) img = tf.ones((128, 100, 100, 3)) boxlist = box_list.BoxList(coordinates) absolute_boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: with self.assertRaisesOpError('assertion failed'): sess.run(absolute_boxlist.get()) def test_convert_to_normalized_and_back(self): coordinates = np.random.uniform(size=(100, 4)) coordinates = np.round(np.sort(coordinates) * 200) coordinates[:, 2:4] += 1 coordinates[99, :] = [0, 0, 201, 201] img = tf.ones((128, 202, 202, 3)) boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32)) boxlist = box_list_ops.to_normalized_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: out = sess.run(boxlist.get()) self.assertAllClose(out, coordinates) def test_convert_to_absolute_and_back(self): coordinates = np.random.uniform(size=(100, 4)) coordinates = np.sort(coordinates) coordinates[99, :] = [0, 0, 1, 1] img = tf.ones((128, 202, 202, 3)) boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32)) boxlist = box_list_ops.to_absolute_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) boxlist = box_list_ops.to_normalized_coordinates(boxlist, tf.shape(img)[1], tf.shape(img)[2]) with self.test_session() as sess: out = sess.run(boxlist.get()) self.assertAllClose(out, coordinates) def test_to_absolute_coordinates_maximum_coordinate_check(self): coordinates = tf.constant([[0, 0, 1.2, 1.2], [0.25, 0.25, 0.75, 0.75]], tf.float32) img = tf.ones((128, 100, 100, 3)) boxlist = box_list.BoxList(coordinates) absolute_boxlist = box_list_ops.to_absolute_coordinates( boxlist, tf.shape(img)[1], tf.shape(img)[2], maximum_normalized_coordinate=1.1) with self.test_session() as sess: with self.assertRaisesOpError('assertion failed'): sess.run(absolute_boxlist.get()) class BoxRefinementTest(tf.test.TestCase): def test_box_voting(self): candidates = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4], [0.6, 0.6, 0.8, 0.8]], tf.float32)) candidates.add_field('ExtraField', tf.constant([1, 2])) pool = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], [0.6, 0.6, 0.8, 0.8]], tf.float32)) pool.add_field('scores', tf.constant([0.75, 0.25, 0.3])) averaged_boxes = box_list_ops.box_voting(candidates, pool) expected_boxes = [[0.1, 0.1, 0.425, 0.425], [0.6, 0.6, 0.8, 0.8]] expected_scores = [0.5, 0.3] with self.test_session() as sess: boxes_out, scores_out, extra_field_out = sess.run( [averaged_boxes.get(), averaged_boxes.get_field('scores'), averaged_boxes.get_field('ExtraField')]) self.assertAllClose(expected_boxes, boxes_out) self.assertAllClose(expected_scores, scores_out) self.assertAllEqual(extra_field_out, [1, 2]) def test_box_voting_fails_with_negative_scores(self): candidates = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) pool = box_list.BoxList(tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) pool.add_field('scores', tf.constant([-0.2])) averaged_boxes = box_list_ops.box_voting(candidates, pool) with self.test_session() as sess: with self.assertRaisesOpError('Scores must be non negative'): sess.run([averaged_boxes.get()]) def test_box_voting_fails_when_unmatched(self): candidates = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) pool = box_list.BoxList(tf.constant([[0.6, 0.6, 0.8, 0.8]], tf.float32)) pool.add_field('scores', tf.constant([0.2])) averaged_boxes = box_list_ops.box_voting(candidates, pool) with self.test_session() as sess: with self.assertRaisesOpError('Each box in selected_boxes must match ' 'with at least one box in pool_boxes.'): sess.run([averaged_boxes.get()]) def test_refine_boxes(self): pool = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], [0.6, 0.6, 0.8, 0.8]], tf.float32)) pool.add_field('ExtraField', tf.constant([1, 2, 3])) pool.add_field('scores', tf.constant([0.75, 0.25, 0.3])) refined_boxes = box_list_ops.refine_boxes(pool, 0.5, 10) expected_boxes = [[0.1, 0.1, 0.425, 0.425], [0.6, 0.6, 0.8, 0.8]] expected_scores = [0.5, 0.3] with self.test_session() as sess: boxes_out, scores_out, extra_field_out = sess.run( [refined_boxes.get(), refined_boxes.get_field('scores'), refined_boxes.get_field('ExtraField')]) self.assertAllClose(expected_boxes, boxes_out) self.assertAllClose(expected_scores, scores_out) self.assertAllEqual(extra_field_out, [1, 3]) def test_refine_boxes_multi_class(self): pool = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], [0.6, 0.6, 0.8, 0.8], [0.2, 0.2, 0.3, 0.3]], tf.float32)) pool.add_field('classes', tf.constant([0, 0, 1, 1])) pool.add_field('scores', tf.constant([0.75, 0.25, 0.3, 0.2])) refined_boxes = box_list_ops.refine_boxes_multi_class(pool, 3, 0.5, 10) expected_boxes = [[0.1, 0.1, 0.425, 0.425], [0.6, 0.6, 0.8, 0.8], [0.2, 0.2, 0.3, 0.3]] expected_scores = [0.5, 0.3, 0.2] with self.test_session() as sess: boxes_out, scores_out, extra_field_out = sess.run( [refined_boxes.get(), refined_boxes.get_field('scores'), refined_boxes.get_field('classes')]) self.assertAllClose(expected_boxes, boxes_out) self.assertAllClose(expected_scores, scores_out) self.assertAllEqual(extra_field_out, [0, 1, 1]) def test_sample_boxes_by_jittering(self): boxes = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], [0.6, 0.6, 0.8, 0.8], [0.2, 0.2, 0.3, 0.3]], tf.float32)) sampled_boxes = box_list_ops.sample_boxes_by_jittering( boxlist=boxes, num_boxes_to_sample=10) iou = box_list_ops.iou(boxes, sampled_boxes) iou_max = tf.reduce_max(iou, axis=0) with self.test_session() as sess: (np_sampled_boxes, np_iou_max) = sess.run([sampled_boxes.get(), iou_max]) self.assertAllEqual(np_sampled_boxes.shape, [10, 4]) self.assertAllGreater(np_iou_max, 0.5) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/box_list_ops_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for tensorflow_models.object_detection.core.post_processing.""" import numpy as np import tensorflow as tf from object_detection.core import post_processing from object_detection.core import standard_fields as fields from object_detection.utils import test_case class MulticlassNonMaxSuppressionTest(test_case.TestCase): def test_multiclass_nms_select_with_shared_boxes(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002], [0, 100, 1, 101]] exp_nms_scores = [.95, .9, .85, .3] exp_nms_classes = [0, 0, 1, 0] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size) with self.test_session() as sess: nms_corners_output, nms_scores_output, nms_classes_output = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) # TODO(bhattad): Remove conditional after CMLE moves to TF 1.9 def test_multiclass_nms_select_with_shared_boxes_given_keypoints(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) num_keypoints = 6 keypoints = tf.tile( tf.reshape(tf.range(8), [8, 1, 1]), [1, num_keypoints, 2]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002], [0, 100, 1, 101]] exp_nms_scores = [.95, .9, .85, .3] exp_nms_classes = [0, 0, 1, 0] exp_nms_keypoints_tensor = tf.tile( tf.reshape(tf.constant([3, 0, 6, 5], dtype=tf.float32), [4, 1, 1]), [1, num_keypoints, 2]) nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size, additional_fields={fields.BoxListFields.keypoints: keypoints}) with self.test_session() as sess: (nms_corners_output, nms_scores_output, nms_classes_output, nms_keypoints, exp_nms_keypoints) = sess.run([ nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes), nms.get_field(fields.BoxListFields.keypoints), exp_nms_keypoints_tensor ]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) self.assertAllEqual(nms_keypoints, exp_nms_keypoints) def test_multiclass_nms_with_shared_boxes_given_keypoint_heatmaps(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) num_boxes = tf.shape(boxes)[0] heatmap_height = 5 heatmap_width = 5 num_keypoints = 17 keypoint_heatmaps = tf.ones( [num_boxes, heatmap_height, heatmap_width, num_keypoints], dtype=tf.float32) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002], [0, 100, 1, 101]] exp_nms_scores = [.95, .9, .85, .3] exp_nms_classes = [0, 0, 1, 0] exp_nms_keypoint_heatmaps = np.ones( (4, heatmap_height, heatmap_width, num_keypoints), dtype=np.float32) nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size, additional_fields={ fields.BoxListFields.keypoint_heatmaps: keypoint_heatmaps }) with self.test_session() as sess: (nms_corners_output, nms_scores_output, nms_classes_output, nms_keypoint_heatmaps) = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes), nms.get_field(fields.BoxListFields.keypoint_heatmaps)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) self.assertAllEqual(nms_keypoint_heatmaps, exp_nms_keypoint_heatmaps) def test_multiclass_nms_with_additional_fields(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) coarse_boxes_key = 'coarse_boxes' coarse_boxes = tf.constant([[0.1, 0.1, 1.1, 1.1], [0.1, 0.2, 1.1, 1.2], [0.1, -0.2, 1.1, 1.0], [0.1, 10.1, 1.1, 11.1], [0.1, 10.2, 1.1, 11.2], [0.1, 100.1, 1.1, 101.1], [0.1, 1000.1, 1.1, 1002.1], [0.1, 1000.1, 1.1, 1002.2]], tf.float32) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = np.array([[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002], [0, 100, 1, 101]], dtype=np.float32) exp_nms_coarse_corners = np.array([[0.1, 10.1, 1.1, 11.1], [0.1, 0.1, 1.1, 1.1], [0.1, 1000.1, 1.1, 1002.1], [0.1, 100.1, 1.1, 101.1]], dtype=np.float32) exp_nms_scores = [.95, .9, .85, .3] exp_nms_classes = [0, 0, 1, 0] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size, additional_fields={coarse_boxes_key: coarse_boxes}) with self.test_session() as sess: (nms_corners_output, nms_scores_output, nms_classes_output, nms_coarse_corners) = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes), nms.get_field(coarse_boxes_key)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) self.assertAllEqual(nms_coarse_corners, exp_nms_coarse_corners) def test_multiclass_nms_select_with_shared_boxes_given_masks(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) num_classes = 2 mask_height = 3 mask_width = 3 masks = tf.tile( tf.reshape(tf.range(8), [8, 1, 1, 1]), [1, num_classes, mask_height, mask_width]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002], [0, 100, 1, 101]] exp_nms_scores = [.95, .9, .85, .3] exp_nms_classes = [0, 0, 1, 0] exp_nms_masks_tensor = tf.tile( tf.reshape(tf.constant([3, 0, 6, 5], dtype=tf.float32), [4, 1, 1]), [1, mask_height, mask_width]) nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size, masks=masks) with self.test_session() as sess: (nms_corners_output, nms_scores_output, nms_classes_output, nms_masks, exp_nms_masks) = sess.run([nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes), nms.get_field(fields.BoxListFields.masks), exp_nms_masks_tensor]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) self.assertAllEqual(nms_masks, exp_nms_masks) def test_multiclass_nms_select_with_clip_window(self): boxes = tf.constant([[[0, 0, 10, 10]], [[1, 1, 11, 11]]], tf.float32) scores = tf.constant([[.9], [.75]]) clip_window = tf.constant([5, 4, 8, 7], tf.float32) score_thresh = 0.0 iou_thresh = 0.5 max_output_size = 100 exp_nms_corners = [[5, 4, 8, 7]] exp_nms_scores = [.9] exp_nms_classes = [0] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size, clip_window=clip_window) with self.test_session() as sess: nms_corners_output, nms_scores_output, nms_classes_output = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) def test_multiclass_nms_select_with_clip_window_change_coordinate_frame(self): boxes = tf.constant([[[0, 0, 10, 10]], [[1, 1, 11, 11]]], tf.float32) scores = tf.constant([[.9], [.75]]) clip_window = tf.constant([5, 4, 8, 7], tf.float32) score_thresh = 0.0 iou_thresh = 0.5 max_output_size = 100 exp_nms_corners = [[0, 0, 1, 1]] exp_nms_scores = [.9] exp_nms_classes = [0] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size, clip_window=clip_window, change_coordinate_frame=True) with self.test_session() as sess: nms_corners_output, nms_scores_output, nms_classes_output = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) def test_multiclass_nms_select_with_per_class_cap(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) score_thresh = 0.1 iou_thresh = .5 max_size_per_class = 2 exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002]] exp_nms_scores = [.95, .9, .85] exp_nms_classes = [0, 0, 1] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class) with self.test_session() as sess: nms_corners_output, nms_scores_output, nms_classes_output = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) def test_multiclass_nms_select_with_total_cap(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) score_thresh = 0.1 iou_thresh = .5 max_size_per_class = 4 max_total_size = 2 exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1]] exp_nms_scores = [.95, .9] exp_nms_classes = [0, 0] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class, max_total_size) with self.test_session() as sess: nms_corners_output, nms_scores_output, nms_classes_output = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) def test_multiclass_nms_threshold_then_select_with_shared_boxes(self): boxes = tf.constant([[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) scores = tf.constant([[.9], [.75], [.6], [.95], [.5], [.3], [.01], [.01]]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 3 exp_nms = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 100, 1, 101]] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size) with self.test_session() as sess: nms_output = sess.run(nms.get()) self.assertAllClose(nms_output, exp_nms) def test_multiclass_nms_select_with_separate_boxes(self): boxes = tf.constant([[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]], [[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]], tf.float32) scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 999, 2, 1004], [0, 100, 1, 101]] exp_nms_scores = [.95, .9, .85, .3] exp_nms_classes = [0, 0, 1, 0] nms, _ = post_processing.multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_output_size) with self.test_session() as sess: nms_corners_output, nms_scores_output, nms_classes_output = sess.run( [nms.get(), nms.get_field(fields.BoxListFields.scores), nms.get_field(fields.BoxListFields.classes)]) self.assertAllClose(nms_corners_output, exp_nms_corners) self.assertAllClose(nms_scores_output, exp_nms_scores) self.assertAllClose(nms_classes_output, exp_nms_classes) def test_batch_multiclass_nms_with_batch_size_1(self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]], [[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[[0, 10, 1, 11], [0, 0, 1, 1], [0, 999, 2, 1004], [0, 100, 1, 101]]] exp_nms_scores = [[.95, .9, .85, .3]] exp_nms_classes = [[0, 0, 1, 0]] (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size) self.assertIsNone(nmsed_masks) self.assertIsNone(nmsed_additional_fields) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) self.assertEqual(num_detections, [4]) def test_batch_multiclass_nms_with_batch_size_2(self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = np.array([[[0, 10, 1, 11], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 999, 2, 1004], [0, 10.1, 1, 11.1], [0, 100, 1, 101], [0, 0, 0, 0]]]) exp_nms_scores = np.array([[.95, .9, 0, 0], [.85, .5, .3, 0]]) exp_nms_classes = np.array([[0, 0, 0, 0], [1, 0, 0, 0]]) (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size) self.assertIsNone(nmsed_masks) self.assertIsNone(nmsed_additional_fields) # Check static shapes self.assertAllEqual(nmsed_boxes.shape.as_list(), exp_nms_corners.shape) self.assertAllEqual(nmsed_scores.shape.as_list(), exp_nms_scores.shape) self.assertAllEqual(nmsed_classes.shape.as_list(), exp_nms_classes.shape) self.assertEqual(num_detections.shape.as_list(), [2]) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) self.assertAllClose(num_detections, [2, 3]) def test_batch_multiclass_nms_with_per_batch_clip_window(self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) clip_window = tf.constant([0., 0., 200., 200.]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = np.array([[[0, 10, 1, 11], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 10.1, 1, 11.1], [0, 100, 1, 101], [0, 0, 0, 0], [0, 0, 0, 0]]]) exp_nms_scores = np.array([[.95, .9, 0, 0], [.5, .3, 0, 0]]) exp_nms_classes = np.array([[0, 0, 0, 0], [0, 0, 0, 0]]) (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size, clip_window=clip_window) self.assertIsNone(nmsed_masks) self.assertIsNone(nmsed_additional_fields) # Check static shapes self.assertAllEqual(nmsed_boxes.shape.as_list(), exp_nms_corners.shape) self.assertAllEqual(nmsed_scores.shape.as_list(), exp_nms_scores.shape) self.assertAllEqual(nmsed_classes.shape.as_list(), exp_nms_classes.shape) self.assertEqual(num_detections.shape.as_list(), [2]) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) self.assertAllClose(num_detections, [2, 2]) def test_batch_multiclass_nms_with_per_image_clip_window(self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) clip_window = tf.constant([[0., 0., 5., 5.], [0., 0., 200., 200.]]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = np.array([[[0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 10.1, 1, 11.1], [0, 100, 1, 101], [0, 0, 0, 0], [0, 0, 0, 0]]]) exp_nms_scores = np.array([[.9, 0., 0., 0.], [.5, .3, 0, 0]]) exp_nms_classes = np.array([[0, 0, 0, 0], [0, 0, 0, 0]]) (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size, clip_window=clip_window) self.assertIsNone(nmsed_masks) self.assertIsNone(nmsed_additional_fields) # Check static shapes self.assertAllEqual(nmsed_boxes.shape.as_list(), exp_nms_corners.shape) self.assertAllEqual(nmsed_scores.shape.as_list(), exp_nms_scores.shape) self.assertAllEqual(nmsed_classes.shape.as_list(), exp_nms_classes.shape) self.assertEqual(num_detections.shape.as_list(), [2]) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) self.assertAllClose(num_detections, [1, 2]) def test_batch_multiclass_nms_with_masks(self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) masks = tf.constant([[[[[0, 1], [2, 3]], [[1, 2], [3, 4]]], [[[2, 3], [4, 5]], [[3, 4], [5, 6]]], [[[4, 5], [6, 7]], [[5, 6], [7, 8]]], [[[6, 7], [8, 9]], [[7, 8], [9, 10]]]], [[[[8, 9], [10, 11]], [[9, 10], [11, 12]]], [[[10, 11], [12, 13]], [[11, 12], [13, 14]]], [[[12, 13], [14, 15]], [[13, 14], [15, 16]]], [[[14, 15], [16, 17]], [[15, 16], [17, 18]]]]], tf.float32) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = np.array([[[0, 10, 1, 11], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 999, 2, 1004], [0, 10.1, 1, 11.1], [0, 100, 1, 101], [0, 0, 0, 0]]]) exp_nms_scores = np.array([[.95, .9, 0, 0], [.85, .5, .3, 0]]) exp_nms_classes = np.array([[0, 0, 0, 0], [1, 0, 0, 0]]) exp_nms_masks = np.array([[[[6, 7], [8, 9]], [[0, 1], [2, 3]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[13, 14], [15, 16]], [[8, 9], [10, 11]], [[10, 11], [12, 13]], [[0, 0], [0, 0]]]]) (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size, masks=masks) self.assertIsNone(nmsed_additional_fields) # Check static shapes self.assertAllEqual(nmsed_boxes.shape.as_list(), exp_nms_corners.shape) self.assertAllEqual(nmsed_scores.shape.as_list(), exp_nms_scores.shape) self.assertAllEqual(nmsed_classes.shape.as_list(), exp_nms_classes.shape) self.assertAllEqual(nmsed_masks.shape.as_list(), exp_nms_masks.shape) self.assertEqual(num_detections.shape.as_list(), [2]) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) self.assertAllClose(num_detections, [2, 3]) self.assertAllClose(nmsed_masks, exp_nms_masks) def test_batch_multiclass_nms_with_additional_fields(self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) additional_fields = { 'keypoints': tf.constant( [[[[6, 7], [8, 9]], [[0, 1], [2, 3]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[13, 14], [15, 16]], [[8, 9], [10, 11]], [[10, 11], [12, 13]], [[0, 0], [0, 0]]]], tf.float32) } score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = np.array([[[0, 10, 1, 11], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 999, 2, 1004], [0, 10.1, 1, 11.1], [0, 100, 1, 101], [0, 0, 0, 0]]]) exp_nms_scores = np.array([[.95, .9, 0, 0], [.85, .5, .3, 0]]) exp_nms_classes = np.array([[0, 0, 0, 0], [1, 0, 0, 0]]) exp_nms_additional_fields = { 'keypoints': np.array([[[[0, 0], [0, 0]], [[6, 7], [8, 9]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[10, 11], [12, 13]], [[13, 14], [15, 16]], [[8, 9], [10, 11]], [[0, 0], [0, 0]]]]) } (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size, additional_fields=additional_fields) self.assertIsNone(nmsed_masks) # Check static shapes self.assertAllEqual(nmsed_boxes.shape.as_list(), exp_nms_corners.shape) self.assertAllEqual(nmsed_scores.shape.as_list(), exp_nms_scores.shape) self.assertAllEqual(nmsed_classes.shape.as_list(), exp_nms_classes.shape) self.assertEqual(len(nmsed_additional_fields), len(exp_nms_additional_fields)) for key in exp_nms_additional_fields: self.assertAllEqual(nmsed_additional_fields[key].shape.as_list(), exp_nms_additional_fields[key].shape) self.assertEqual(num_detections.shape.as_list(), [2]) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_additional_fields, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_additional_fields, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) for key in exp_nms_additional_fields: self.assertAllClose(nmsed_additional_fields[key], exp_nms_additional_fields[key]) self.assertAllClose(num_detections, [2, 3]) def test_batch_multiclass_nms_with_dynamic_batch_size(self): boxes_placeholder = tf.placeholder(tf.float32, shape=(None, None, 2, 4)) scores_placeholder = tf.placeholder(tf.float32, shape=(None, None, 2)) masks_placeholder = tf.placeholder(tf.float32, shape=(None, None, 2, 2, 2)) boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]]) scores = np.array([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) masks = np.array([[[[[0, 1], [2, 3]], [[1, 2], [3, 4]]], [[[2, 3], [4, 5]], [[3, 4], [5, 6]]], [[[4, 5], [6, 7]], [[5, 6], [7, 8]]], [[[6, 7], [8, 9]], [[7, 8], [9, 10]]]], [[[[8, 9], [10, 11]], [[9, 10], [11, 12]]], [[[10, 11], [12, 13]], [[11, 12], [13, 14]]], [[[12, 13], [14, 15]], [[13, 14], [15, 16]]], [[[14, 15], [16, 17]], [[15, 16], [17, 18]]]]]) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = np.array([[[0, 10, 1, 11], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 999, 2, 1004], [0, 10.1, 1, 11.1], [0, 100, 1, 101], [0, 0, 0, 0]]]) exp_nms_scores = np.array([[.95, .9, 0, 0], [.85, .5, .3, 0]]) exp_nms_classes = np.array([[0, 0, 0, 0], [1, 0, 0, 0]]) exp_nms_masks = np.array([[[[6, 7], [8, 9]], [[0, 1], [2, 3]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[13, 14], [15, 16]], [[8, 9], [10, 11]], [[10, 11], [12, 13]], [[0, 0], [0, 0]]]]) (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes_placeholder, scores_placeholder, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size, masks=masks_placeholder) self.assertIsNone(nmsed_additional_fields) # Check static shapes self.assertAllEqual(nmsed_boxes.shape.as_list(), [None, 4, 4]) self.assertAllEqual(nmsed_scores.shape.as_list(), [None, 4]) self.assertAllEqual(nmsed_classes.shape.as_list(), [None, 4]) self.assertAllEqual(nmsed_masks.shape.as_list(), [None, 4, 2, 2]) self.assertEqual(num_detections.shape.as_list(), [None]) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, num_detections], feed_dict={boxes_placeholder: boxes, scores_placeholder: scores, masks_placeholder: masks}) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) self.assertAllClose(num_detections, [2, 3]) self.assertAllClose(nmsed_masks, exp_nms_masks) def test_batch_multiclass_nms_with_masks_and_num_valid_boxes(self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) masks = tf.constant([[[[[0, 1], [2, 3]], [[1, 2], [3, 4]]], [[[2, 3], [4, 5]], [[3, 4], [5, 6]]], [[[4, 5], [6, 7]], [[5, 6], [7, 8]]], [[[6, 7], [8, 9]], [[7, 8], [9, 10]]]], [[[[8, 9], [10, 11]], [[9, 10], [11, 12]]], [[[10, 11], [12, 13]], [[11, 12], [13, 14]]], [[[12, 13], [14, 15]], [[13, 14], [15, 16]]], [[[14, 15], [16, 17]], [[15, 16], [17, 18]]]]], tf.float32) num_valid_boxes = tf.constant([1, 1], tf.int32) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[[0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 10.1, 1, 11.1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]] exp_nms_scores = [[.9, 0, 0, 0], [.5, 0, 0, 0]] exp_nms_classes = [[0, 0, 0, 0], [0, 0, 0, 0]] exp_nms_masks = [[[[0, 1], [2, 3]], [[0, 0], [0, 0]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[8, 9], [10, 11]], [[0, 0], [0, 0]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]]] (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size, num_valid_boxes=num_valid_boxes, masks=masks) self.assertIsNone(nmsed_additional_fields) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) self.assertAllClose(num_detections, [1, 1]) self.assertAllClose(nmsed_masks, exp_nms_masks) def test_batch_multiclass_nms_with_additional_fields_and_num_valid_boxes( self): boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [0, 10, 1, 11]]], [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], [[0, 100, 1, 101], [0, 100, 1, 101]], [[0, 1000, 1, 1002], [0, 999, 2, 1004]], [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], tf.float32) scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0]], [[.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) additional_fields = { 'keypoints': tf.constant( [[[[6, 7], [8, 9]], [[0, 1], [2, 3]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[13, 14], [15, 16]], [[8, 9], [10, 11]], [[10, 11], [12, 13]], [[0, 0], [0, 0]]]], tf.float32) } num_valid_boxes = tf.constant([1, 1], tf.int32) score_thresh = 0.1 iou_thresh = .5 max_output_size = 4 exp_nms_corners = [[[0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 10.1, 1, 11.1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]] exp_nms_scores = [[.9, 0, 0, 0], [.5, 0, 0, 0]] exp_nms_classes = [[0, 0, 0, 0], [0, 0, 0, 0]] exp_nms_additional_fields = { 'keypoints': np.array([[[[6, 7], [8, 9]], [[0, 0], [0, 0]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[13, 14], [15, 16]], [[0, 0], [0, 0]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]]]) } (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, nmsed_additional_fields, num_detections ) = post_processing.batch_multiclass_non_max_suppression( boxes, scores, score_thresh, iou_thresh, max_size_per_class=max_output_size, max_total_size=max_output_size, num_valid_boxes=num_valid_boxes, additional_fields=additional_fields) self.assertIsNone(nmsed_masks) with self.test_session() as sess: (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_additional_fields, num_detections) = sess.run([nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_additional_fields, num_detections]) self.assertAllClose(nmsed_boxes, exp_nms_corners) self.assertAllClose(nmsed_scores, exp_nms_scores) self.assertAllClose(nmsed_classes, exp_nms_classes) for key in exp_nms_additional_fields: self.assertAllClose(nmsed_additional_fields[key], exp_nms_additional_fields[key]) self.assertAllClose(num_detections, [1, 1]) # TODO(bhattad): Remove conditional after CMLE moves to TF 1.9 if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/post_processing_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for object_detection.core.balanced_positive_negative_sampler.""" import numpy as np import tensorflow as tf from object_detection.core import balanced_positive_negative_sampler from object_detection.utils import test_case class BalancedPositiveNegativeSamplerTest(test_case.TestCase): def test_subsample_all_examples_dynamic(self): numpy_labels = np.random.permutation(300) indicator = tf.constant(np.ones(300) == 1) numpy_labels = (numpy_labels - 200) > 0 labels = tf.constant(numpy_labels) sampler = ( balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) is_sampled = sampler.subsample(indicator, 64, labels) with self.test_session() as sess: is_sampled = sess.run(is_sampled) self.assertTrue(sum(is_sampled) == 64) self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 32) self.assertTrue(sum(np.logical_and( np.logical_not(numpy_labels), is_sampled)) == 32) def test_subsample_all_examples_static(self): numpy_labels = np.random.permutation(300) indicator = np.array(np.ones(300) == 1, np.bool) numpy_labels = (numpy_labels - 200) > 0 labels = np.array(numpy_labels, np.bool) def graph_fn(indicator, labels): sampler = ( balanced_positive_negative_sampler.BalancedPositiveNegativeSampler( is_static=True)) return sampler.subsample(indicator, 64, labels) is_sampled = self.execute(graph_fn, [indicator, labels]) self.assertTrue(sum(is_sampled) == 64) self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 32) self.assertTrue(sum(np.logical_and( np.logical_not(numpy_labels), is_sampled)) == 32) def test_subsample_selection_dynamic(self): # Test random sampling when only some examples can be sampled: # 100 samples, 20 positives, 10 positives cannot be sampled numpy_labels = np.arange(100) numpy_indicator = numpy_labels < 90 indicator = tf.constant(numpy_indicator) numpy_labels = (numpy_labels - 80) >= 0 labels = tf.constant(numpy_labels) sampler = ( balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) is_sampled = sampler.subsample(indicator, 64, labels) with self.test_session() as sess: is_sampled = sess.run(is_sampled) self.assertTrue(sum(is_sampled) == 64) self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 10) self.assertTrue(sum(np.logical_and( np.logical_not(numpy_labels), is_sampled)) == 54) self.assertAllEqual(is_sampled, np.logical_and(is_sampled, numpy_indicator)) def test_subsample_selection_static(self): # Test random sampling when only some examples can be sampled: # 100 samples, 20 positives, 10 positives cannot be sampled. numpy_labels = np.arange(100) numpy_indicator = numpy_labels < 90 indicator = np.array(numpy_indicator, np.bool) numpy_labels = (numpy_labels - 80) >= 0 labels = np.array(numpy_labels, np.bool) def graph_fn(indicator, labels): sampler = ( balanced_positive_negative_sampler.BalancedPositiveNegativeSampler( is_static=True)) return sampler.subsample(indicator, 64, labels) is_sampled = self.execute(graph_fn, [indicator, labels]) self.assertTrue(sum(is_sampled) == 64) self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 10) self.assertTrue(sum(np.logical_and( np.logical_not(numpy_labels), is_sampled)) == 54) self.assertAllEqual(is_sampled, np.logical_and(is_sampled, numpy_indicator)) def test_subsample_selection_larger_batch_size_dynamic(self): # Test random sampling when total number of examples that can be sampled are # less than batch size: # 100 samples, 50 positives, 40 positives cannot be sampled, batch size 64. numpy_labels = np.arange(100) numpy_indicator = numpy_labels < 60 indicator = tf.constant(numpy_indicator) numpy_labels = (numpy_labels - 50) >= 0 labels = tf.constant(numpy_labels) sampler = ( balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) is_sampled = sampler.subsample(indicator, 64, labels) with self.test_session() as sess: is_sampled = sess.run(is_sampled) self.assertTrue(sum(is_sampled) == 60) self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 10) self.assertTrue( sum(np.logical_and(np.logical_not(numpy_labels), is_sampled)) == 50) self.assertAllEqual(is_sampled, np.logical_and(is_sampled, numpy_indicator)) def test_subsample_selection_larger_batch_size_static(self): # Test random sampling when total number of examples that can be sampled are # less than batch size: # 100 samples, 50 positives, 40 positives cannot be sampled, batch size 64. # It should still return 64 samples, with 4 of them that couldn't have been # sampled. numpy_labels = np.arange(100) numpy_indicator = numpy_labels < 60 indicator = np.array(numpy_indicator, np.bool) numpy_labels = (numpy_labels - 50) >= 0 labels = np.array(numpy_labels, np.bool) def graph_fn(indicator, labels): sampler = ( balanced_positive_negative_sampler.BalancedPositiveNegativeSampler( is_static=True)) return sampler.subsample(indicator, 64, labels) is_sampled = self.execute(graph_fn, [indicator, labels]) self.assertTrue(sum(is_sampled) == 64) self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) >= 10) self.assertTrue( sum(np.logical_and(np.logical_not(numpy_labels), is_sampled)) >= 50) self.assertTrue(sum(np.logical_and(is_sampled, numpy_indicator)) == 60) def test_subsample_selection_no_batch_size(self): # Test random sampling when only some examples can be sampled: # 1000 samples, 6 positives (5 can be sampled). numpy_labels = np.arange(1000) numpy_indicator = numpy_labels < 999 indicator = tf.constant(numpy_indicator) numpy_labels = (numpy_labels - 994) >= 0 labels = tf.constant(numpy_labels) sampler = (balanced_positive_negative_sampler. BalancedPositiveNegativeSampler(0.01)) is_sampled = sampler.subsample(indicator, None, labels) with self.test_session() as sess: is_sampled = sess.run(is_sampled) self.assertTrue(sum(is_sampled) == 500) self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 5) self.assertTrue(sum(np.logical_and( np.logical_not(numpy_labels), is_sampled)) == 495) self.assertAllEqual(is_sampled, np.logical_and(is_sampled, numpy_indicator)) def test_subsample_selection_no_batch_size_static(self): labels = tf.constant([[True, False, False]]) indicator = tf.constant([True, False, True]) sampler = ( balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) with self.assertRaises(ValueError): sampler.subsample(indicator, None, labels) def test_raises_error_with_incorrect_label_shape(self): labels = tf.constant([[True, False, False]]) indicator = tf.constant([True, False, True]) sampler = (balanced_positive_negative_sampler. BalancedPositiveNegativeSampler()) with self.assertRaises(ValueError): sampler.subsample(indicator, 64, labels) def test_raises_error_with_incorrect_indicator_shape(self): labels = tf.constant([True, False, False]) indicator = tf.constant([[True, False, True]]) sampler = (balanced_positive_negative_sampler. BalancedPositiveNegativeSampler()) with self.assertRaises(ValueError): sampler.subsample(indicator, 64, labels) if __name__ == '__main__': tf.test.main()
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/balanced_positive_negative_sampler_test.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Records previous preprocessing operations and allows them to be repeated. Used with object_detection.core.preprocessor. Passing a PreprocessorCache into individual data augmentation functions or the general preprocess() function will store all randomly generated variables in the PreprocessorCache. When a preprocessor function is called multiple times with the same PreprocessorCache object, that function will perform the same augmentation on all calls. """ from collections import defaultdict class PreprocessorCache(object): """Dictionary wrapper storing random variables generated during preprocessing. """ # Constant keys representing different preprocessing functions ROTATION90 = 'rotation90' HORIZONTAL_FLIP = 'horizontal_flip' VERTICAL_FLIP = 'vertical_flip' PIXEL_VALUE_SCALE = 'pixel_value_scale' IMAGE_SCALE = 'image_scale' RGB_TO_GRAY = 'rgb_to_gray' ADJUST_BRIGHTNESS = 'adjust_brightness' ADJUST_CONTRAST = 'adjust_contrast' ADJUST_HUE = 'adjust_hue' ADJUST_SATURATION = 'adjust_saturation' DISTORT_COLOR = 'distort_color' STRICT_CROP_IMAGE = 'strict_crop_image' CROP_IMAGE = 'crop_image' PAD_IMAGE = 'pad_image' CROP_TO_ASPECT_RATIO = 'crop_to_aspect_ratio' RESIZE_METHOD = 'resize_method' PAD_TO_ASPECT_RATIO = 'pad_to_aspect_ratio' BLACK_PATCHES = 'black_patches' ADD_BLACK_PATCH = 'add_black_patch' SELECTOR = 'selector' SELECTOR_TUPLES = 'selector_tuples' SSD_CROP_SELECTOR_ID = 'ssd_crop_selector_id' SSD_CROP_PAD_SELECTOR_ID = 'ssd_crop_pad_selector_id' # 23 permitted function ids _VALID_FNS = [ROTATION90, HORIZONTAL_FLIP, VERTICAL_FLIP, PIXEL_VALUE_SCALE, IMAGE_SCALE, RGB_TO_GRAY, ADJUST_BRIGHTNESS, ADJUST_CONTRAST, ADJUST_HUE, ADJUST_SATURATION, DISTORT_COLOR, STRICT_CROP_IMAGE, CROP_IMAGE, PAD_IMAGE, CROP_TO_ASPECT_RATIO, RESIZE_METHOD, PAD_TO_ASPECT_RATIO, BLACK_PATCHES, ADD_BLACK_PATCH, SELECTOR, SELECTOR_TUPLES, SSD_CROP_SELECTOR_ID, SSD_CROP_PAD_SELECTOR_ID] def __init__(self): self._history = defaultdict(dict) def clear(self): """Resets cache.""" self._history = defaultdict(dict) def get(self, function_id, key): """Gets stored value given a function id and key. Args: function_id: identifier for the preprocessing function used. key: identifier for the variable stored. Returns: value: the corresponding value, expected to be a tensor or nested structure of tensors. Raises: ValueError: if function_id is not one of the 23 valid function ids. """ if function_id not in self._VALID_FNS: raise ValueError('Function id not recognized: %s.' % str(function_id)) return self._history[function_id].get(key) def update(self, function_id, key, value): """Adds a value to the dictionary. Args: function_id: identifier for the preprocessing function used. key: identifier for the variable stored. value: the value to store, expected to be a tensor or nested structure of tensors. Raises: ValueError: if function_id is not one of the 23 valid function ids. """ if function_id not in self._VALID_FNS: raise ValueError('Function id not recognized: %s.' % str(function_id)) self._history[function_id][key] = value
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/preprocessor_cache.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Base box coder. Box coders convert between coordinate frames, namely image-centric (with (0,0) on the top left of image) and anchor-centric (with (0,0) being defined by a specific anchor). Users of a BoxCoder can call two methods: encode: which encodes a box with respect to a given anchor (or rather, a tensor of boxes wrt a corresponding tensor of anchors) and decode: which inverts this encoding with a decode operation. In both cases, the arguments are assumed to be in 1-1 correspondence already; it is not the job of a BoxCoder to perform matching. """ from abc import ABCMeta from abc import abstractmethod from abc import abstractproperty import tensorflow as tf # Box coder types. FASTER_RCNN = 'faster_rcnn' KEYPOINT = 'keypoint' MEAN_STDDEV = 'mean_stddev' SQUARE = 'square' class BoxCoder(object): """Abstract base class for box coder.""" __metaclass__ = ABCMeta @abstractproperty def code_size(self): """Return the size of each code. This number is a constant and should agree with the output of the `encode` op (e.g. if rel_codes is the output of self.encode(...), then it should have shape [N, code_size()]). This abstractproperty should be overridden by implementations. Returns: an integer constant """ pass def encode(self, boxes, anchors): """Encode a box list relative to an anchor collection. Args: boxes: BoxList holding N boxes to be encoded anchors: BoxList of N anchors Returns: a tensor representing N relative-encoded boxes """ with tf.name_scope('Encode'): return self._encode(boxes, anchors) def decode(self, rel_codes, anchors): """Decode boxes that are encoded relative to an anchor collection. Args: rel_codes: a tensor representing N relative-encoded boxes anchors: BoxList of anchors Returns: boxlist: BoxList holding N boxes encoded in the ordinary way (i.e., with corners y_min, x_min, y_max, x_max) """ with tf.name_scope('Decode'): return self._decode(rel_codes, anchors) @abstractmethod def _encode(self, boxes, anchors): """Method to be overriden by implementations. Args: boxes: BoxList holding N boxes to be encoded anchors: BoxList of N anchors Returns: a tensor representing N relative-encoded boxes """ pass @abstractmethod def _decode(self, rel_codes, anchors): """Method to be overriden by implementations. Args: rel_codes: a tensor representing N relative-encoded boxes anchors: BoxList of anchors Returns: boxlist: BoxList holding N boxes encoded in the ordinary way (i.e., with corners y_min, x_min, y_max, x_max) """ pass def batch_decode(encoded_boxes, box_coder, anchors): """Decode a batch of encoded boxes. This op takes a batch of encoded bounding boxes and transforms them to a batch of bounding boxes specified by their corners in the order of [y_min, x_min, y_max, x_max]. Args: encoded_boxes: a float32 tensor of shape [batch_size, num_anchors, code_size] representing the location of the objects. box_coder: a BoxCoder object. anchors: a BoxList of anchors used to encode `encoded_boxes`. Returns: decoded_boxes: a float32 tensor of shape [batch_size, num_anchors, coder_size] representing the corners of the objects in the order of [y_min, x_min, y_max, x_max]. Raises: ValueError: if batch sizes of the inputs are inconsistent, or if the number of anchors inferred from encoded_boxes and anchors are inconsistent. """ encoded_boxes.get_shape().assert_has_rank(3) if encoded_boxes.get_shape()[1].value != anchors.num_boxes_static(): raise ValueError('The number of anchors inferred from encoded_boxes' ' and anchors are inconsistent: shape[1] of encoded_boxes' ' %s should be equal to the number of anchors: %s.' % (encoded_boxes.get_shape()[1].value, anchors.num_boxes_static())) decoded_boxes = tf.stack([ box_coder.decode(boxes, anchors).get() for boxes in tf.unstack(encoded_boxes) ]) return decoded_boxes
DeepLearningExamples-master
TensorFlow/Detection/SSD/models/research/object_detection/core/box_coder.py