text
stringlengths
81
112k
Extract the title from a page. Args: page: a unicode string Returns: a unicode string def _page_to_title(page): """Extract the title from a page. Args: page: a unicode string Returns: a unicode string """ # print("page=%s" % page) start_tag = u"<title>" end_tag = u"</title>" start_pos = page.find(start_tag) end_pos = page.find(end_tag) assert start_pos != -1 assert end_pos != -1 start_pos += len(start_tag) return page[start_pos:end_pos]
Extract the text from a page. Args: page: a unicode string Returns: a unicode string def _page_to_text(page): """Extract the text from a page. Args: page: a unicode string Returns: a unicode string """ # text start tag looks like "<text ..otherstuff>" start_pos = page.find(u"<text") assert start_pos != -1 end_tag_pos = page.find(u">", start_pos) assert end_tag_pos != -1 end_tag_pos += len(u">") end_pos = page.find(u"</text>") if end_pos == -1: return u"" return page[end_tag_pos:end_pos]
Remove everything found between instances of start_string and end_string. Replace each such instance with replace_fn(removed_text) e.g. _find_and_replace(u"the [[fat]] cat [[sat]]", u"[[", u"]]", lambda x: x) = u"the fat cat sat" Args: text: a unicode string start_string: a unicode string end_string: a unicode string replace_fn: a unary function from unicode string to unicode string Returns: a string def _find_and_replace(text, start_string, end_string, replace_fn): """Remove everything found between instances of start_string and end_string. Replace each such instance with replace_fn(removed_text) e.g. _find_and_replace(u"the [[fat]] cat [[sat]]", u"[[", u"]]", lambda x: x) = u"the fat cat sat" Args: text: a unicode string start_string: a unicode string end_string: a unicode string replace_fn: a unary function from unicode string to unicode string Returns: a string """ ret = u"" current_pos = 0 while True: start_pos = text.find(start_string, current_pos) if start_pos == -1: ret += text[current_pos:] break ret += text[current_pos:start_pos] end_pos = text.find(end_string, start_pos + len(start_string)) if end_pos == -1: break ret += replace_fn(text[start_pos + len(start_string):end_pos]) current_pos = end_pos + len(end_string) return ret
Remove double brackets (internal links) but leave the viewable text. Args: text: a unicode string Returns: a unicode string def _remove_double_brackets(text): """Remove double brackets (internal links) but leave the viewable text. Args: text: a unicode string Returns: a unicode string """ def replacement_fn(s): if u":" in s: # this is probably a category or something like that. return "" # keep the part after the bar. bar_pos = s.find(u"|") if bar_pos == -1: return s return s[bar_pos + 1:] return _find_and_replace(text, u"[[", u"]]", replacement_fn)
A stack of self attention layers. def image_encoder(image_feat, hparams, name="image_encoder", save_weights_to=None, make_image_summary=True): """A stack of self attention layers.""" x = image_feat image_hidden_size = hparams.image_hidden_size or hparams.hidden_size image_filter_size = hparams.image_filter_size or hparams.filter_size with tf.variable_scope(name): for layer in range(hparams.num_encoder_layers or hparams.num_hidden_layers): with tf.variable_scope("layer_%d" % layer): with tf.variable_scope("self_attention"): y = vqa_layers.multihead_attention( common_layers.layer_preprocess(x, hparams), None, None, hparams.attention_key_channels or image_hidden_size, hparams.attention_value_channels or image_hidden_size, image_hidden_size, hparams.num_heads, hparams.attention_dropout, attention_type=hparams.image_self_attention_type, save_weights_to=save_weights_to, make_image_summary=make_image_summary, scale_dotproduct=hparams.scale_dotproduct, ) utils.collect_named_outputs( "norms", "image_feat_self_attention_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "image_feat_self_attention_postprocess_%d"%(layer), tf.norm(x, axis=-1)) with tf.variable_scope("ffn"): y = common_layers.dense_relu_dense( common_layers.layer_preprocess(x, hparams), image_filter_size, image_hidden_size, dropout=hparams.relu_dropout, ) utils.collect_named_outputs( "norms", "image_feat_ffn_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "image_feat_ffn_postprocess_%d"%(layer), tf.norm(x, axis=-1)) # if normalization is done in layer_preprocess, then it should also be done # on the output, since the output can grow very large, being the sum of # a whole stack of unnormalized layer outputs. return common_layers.layer_preprocess(x, hparams)
Prepare question encoder. Args: inputs: a Tensor. hparams: run hyperparameters Returns: encoder_input: a Tensor, bottom of encoder stack encoder_self_attention_bias: a bias tensor for use in encoder self-attention def prepare_question_encoder(inputs, hparams): """Prepare question encoder. Args: inputs: a Tensor. hparams: run hyperparameters Returns: encoder_input: a Tensor, bottom of encoder stack encoder_self_attention_bias: a bias tensor for use in encoder self-attention """ encoder_input = inputs # Usual case - not a packed dataset. encoder_padding = common_attention.embedding_to_padding(encoder_input) ignore_padding = common_attention.attention_bias_ignore_padding( encoder_padding) encoder_self_attention_bias = ignore_padding if hparams.pos == "timing": encoder_input = common_attention.add_timing_signal_1d(encoder_input) elif hparams.pos == "emb": encoder_input = common_attention.add_positional_embedding( encoder_input, hparams.max_length, "inputs_positional_embedding", None) return (encoder_input, encoder_self_attention_bias)
A stack of self attention layers. def question_encoder(question, question_self_attention_bias, hparams, name="question_encoder", save_weights_to=None, make_image_summary=True): """A stack of self attention layers.""" x = question with tf.variable_scope(name): for layer in range(hparams.num_encoder_layers or hparams.num_hidden_layers): with tf.variable_scope("layer_%d" % layer): with tf.variable_scope("self_attention"): y = vqa_layers.multihead_attention( common_layers.layer_preprocess(x, hparams), None, question_self_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, attention_type=hparams.question_self_attention_type, block_length=hparams.block_length, save_weights_to=save_weights_to, make_image_summary=make_image_summary, scale_dotproduct=hparams.scale_dotproduct, ) utils.collect_named_outputs( "norms", "query_self_attention_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "query_self_attention_postprocess_%d"%(layer), tf.norm(x, axis=-1)) with tf.variable_scope("ffn"): y = common_layers.dense_relu_dense( common_layers.layer_preprocess(x, hparams), hparams.filter_size, hparams.hidden_size, dropout=hparams.relu_dropout, ) utils.collect_named_outputs( "norms", "query_ffn_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "query_ffn_postprocess_%d"%(layer), tf.norm(x, axis=-1)) # if normalization is done in layer_preprocess, then it should also be done # on the output, since the output can grow very large, being the sum of # a whole stack of unnormalized layer outputs. return common_layers.layer_preprocess(x, hparams)
Attention on image feature with question as query. def attn(image_feat, query, hparams, name="attn", save_weights_to=None, make_image_summary=True): """Attention on image feature with question as query.""" with tf.variable_scope(name, "attn", values=[image_feat, query]): total_key_depth = hparams.attention_key_channels or hparams.hidden_size total_value_depth = hparams.attention_value_channels or hparams.hidden_size num_heads = hparams.num_heads query = tf.expand_dims(query, 1) q, k, v = common_attention.compute_qkv( query, image_feat, total_key_depth, total_value_depth, ) q = common_attention.split_heads(q, num_heads) k = common_attention.split_heads(k, num_heads) v = common_attention.split_heads(v, num_heads) if hparams.scale_dotproduct: key_depth_per_head = total_key_depth // num_heads q *= key_depth_per_head**-0.5 # image_feat is input as v x = common_attention.dot_product_attention( q, k, v, None, dropout_rate=hparams.attention_dropout, image_shapes=None, save_weights_to=save_weights_to, make_image_summary=make_image_summary) x = common_attention.combine_heads(x) return tf.squeeze(x, axis=1)
Multi layer perceptron with dropout and relu activation. def mlp(feature, hparams, name="mlp"): """Multi layer perceptron with dropout and relu activation.""" with tf.variable_scope(name, "mlp", values=[feature]): num_mlp_layers = hparams.num_mlp_layers mlp_size = hparams.mlp_size for _ in range(num_mlp_layers): feature = common_layers.dense(feature, mlp_size, activation=None) utils.collect_named_outputs("norms", "mlp_feature", tf.norm(feature, axis=-1)) feature = common_layers.layer_norm(feature) feature = tf.nn.relu(feature) feature = tf.nn.dropout(feature, keep_prob=1.-hparams.dropout) return feature
Prepare encoder. Args: image_feat: a Tensor. question: a Tensor. hparams: run hyperparameters Returns: encoder_input: a Tensor, bottom of encoder stack encoder_self_attention_bias: a bias tensor for use in encoder self-attention def prepare_image_question_encoder(image_feat, question, hparams): """Prepare encoder. Args: image_feat: a Tensor. question: a Tensor. hparams: run hyperparameters Returns: encoder_input: a Tensor, bottom of encoder stack encoder_self_attention_bias: a bias tensor for use in encoder self-attention """ encoder_input = tf.concat([image_feat, question], axis=1) encoder_padding = common_attention.embedding_to_padding(encoder_input) ignore_padding = common_attention.attention_bias_ignore_padding( encoder_padding) encoder_self_attention_bias = ignore_padding encoder_decoder_attention_bias = ignore_padding # Usual case - not a packed dataset. if hparams.pos == "timing": question = common_attention.add_timing_signal_1d(question) elif hparams.pos == "emb": question = common_attention.add_positional_embedding( question, hparams.max_length, "inputs_positional_embedding", None) encoder_input = tf.concat([image_feat, question], axis=1) return (encoder_input, encoder_self_attention_bias, encoder_decoder_attention_bias)
A stack of self attention layers. def image_question_encoder(encoder_inputs, encoder_self_attention_bias, hparams, query=None, name="image_question_encoder", save_weights_to=None, make_image_summary=True): """A stack of self attention layers.""" x = encoder_inputs with tf.variable_scope(name): for layer in range(hparams.num_encoder_layers or hparams.num_hidden_layers): with tf.variable_scope("layer_%d" % layer): with tf.variable_scope("self_attention"): y = vqa_layers.multihead_attention( common_layers.layer_preprocess(x, hparams), None, encoder_self_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, attention_type=hparams.self_attention_type, block_length=hparams.block_length, save_weights_to=save_weights_to, make_image_summary=make_image_summary, scale_dotproduct=hparams.scale_dotproduct, ) utils.collect_named_outputs( "norms", "encoder_self_attention_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "encoder_self_attention_postprocess_%d"%(layer), tf.norm(x, axis=-1)) if query is not None: with tf.variable_scope("encdec_attention"): y = common_attention.multihead_attention( common_layers.layer_preprocess(x, hparams), query, None, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, attention_type=hparams.self_attention_type, block_length=hparams.block_length, save_weights_to=save_weights_to, make_image_summary=make_image_summary, scale_dotproduct=hparams.scale_dotproduct, ) utils.collect_named_outputs( "norms", "encoder_decoder_attention_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "encoder_decoder_attention_post_%d"%(layer), tf.norm(x, axis=-1)) with tf.variable_scope("ffn"): y = common_layers.dense_relu_dense( common_layers.layer_preprocess(x, hparams), hparams.filter_size, hparams.hidden_size, dropout=hparams.relu_dropout, ) utils.collect_named_outputs( "norms", "encoder_ffn_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "encoder_ffn_postprocess_%d"%(layer), tf.norm(x, axis=-1)) # if normalization is done in layer_preprocess, then it should also be done # on the output, since the output can grow very large, being the sum of # a whole stack of unnormalized layer outputs. return common_layers.layer_preprocess(x, hparams)
A stack of transformer layers. Args: decoder_input: a Tensor encoder_output: a Tensor decoder_self_attention_bias: bias Tensor for self-attention (see common_attention.attention_bias()) encoder_decoder_attention_bias: bias Tensor for encoder-decoder attention (see common_attention.attention_bias()) hparams: hyperparameters for model name: a string save_weights_to: an optional dictionary to capture attention weights for visualization; the weights tensor will be appended there under a string key created from the variable scope (including name). make_image_summary: Whether to make an attention image summary. Returns: y: a Tensors def decoder(decoder_input, encoder_output, decoder_self_attention_bias, encoder_decoder_attention_bias, hparams, name="decoder", save_weights_to=None, make_image_summary=True,): """A stack of transformer layers. Args: decoder_input: a Tensor encoder_output: a Tensor decoder_self_attention_bias: bias Tensor for self-attention (see common_attention.attention_bias()) encoder_decoder_attention_bias: bias Tensor for encoder-decoder attention (see common_attention.attention_bias()) hparams: hyperparameters for model name: a string save_weights_to: an optional dictionary to capture attention weights for visualization; the weights tensor will be appended there under a string key created from the variable scope (including name). make_image_summary: Whether to make an attention image summary. Returns: y: a Tensors """ x = decoder_input with tf.variable_scope(name): for layer in range(hparams.num_decoder_layers or hparams.num_hidden_layers): layer_name = "layer_%d" % layer with tf.variable_scope(layer_name): with tf.variable_scope("self_attention"): y = common_attention.multihead_attention( common_layers.layer_preprocess(x, hparams), None, decoder_self_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, attention_type=hparams.self_attention_type, save_weights_to=save_weights_to, make_image_summary=make_image_summary, ) utils.collect_named_outputs("norms", "decoder_self_attention_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs("norms", "decoder_self_attention_post_%d"%(layer), tf.norm(x, axis=-1)) if encoder_output is not None: with tf.variable_scope("encdec_attention"): y = common_attention.multihead_attention( common_layers.layer_preprocess(x, hparams), encoder_output, encoder_decoder_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, save_weights_to=save_weights_to, make_image_summary=make_image_summary, ) utils.collect_named_outputs( "norms", "decoder_encoder_attention_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs( "norms", "decoder_encoder_attention_post_%d"%(layer), tf.norm(x, axis=-1)) with tf.variable_scope("ffn"): y = common_layers.dense_relu_dense( common_layers.layer_preprocess(x, hparams), hparams.filter_size, hparams.hidden_size, dropout=hparams.relu_dropout, ) utils.collect_named_outputs("norms", "decoder_ffn_%d"%(layer), tf.norm(y, axis=-1)) x = common_layers.layer_postprocess(x, y, hparams) utils.collect_named_outputs("norms", "decoder_ffn_post_%d"%(layer), tf.norm(x, axis=-1)) # if normalization is done in layer_preprocess, then it should also be done # on the output, since the output can grow very large, being the sum of # a whole stack of unnormalized layer outputs. return common_layers.layer_preprocess(x, hparams)
Iterative encoder decoder. def iterative_encoder_decoder(encoder_input, encoder_self_attention_bias, encoder_decoder_attention_bias, query, hparams): """Iterative encoder decoder.""" for _ in range(hparams.num_rec_steps): with tf.variable_scope("step", reuse=tf.AUTO_REUSE): encoder_output = image_question_encoder( encoder_input, encoder_self_attention_bias, hparams, query) decoder_output = decoder( query, encoder_output, None, encoder_decoder_attention_bias, hparams) encoder_input = encoder_output query = decoder_output return decoder_output
VQA attention baseline hparams. def vqa_self_attention_base(): """VQA attention baseline hparams.""" hparams = common_hparams.basic_params1() hparams.batch_size = 128 hparams.use_fixed_batch_size = True, hparams.optimizer = "adam" hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.997 hparams.optimizer_adam_epsilon = 1e-9 hparams.weight_decay = 0. hparams.clip_grad_norm = 0. hparams.initializer = "xavier" hparams.learning_rate_schedule = ( "constant*linear_warmup*rsqrt_normalized_decay") hparams.learning_rate_warmup_steps = 8000 hparams.learning_rate_constant = 1e-3 hparams.learning_rate_decay_rate = 0.5 hparams.learning_rate_decay_steps = 50000 hparams.dropout = 0.5 hparams.summarize_grads = True hparams.summarize_vars = True # not used hparams hparams.label_smoothing = 0. hparams.multiply_embedding_mode = "sqrt_depth" # add new hparams # use raw image as input hparams.add_hparam("image_input_type", "image") hparams.add_hparam("image_model_fn", "resnet_v1_152") hparams.add_hparam("resize_side", 512) hparams.add_hparam("height", 448) hparams.add_hparam("width", 448) hparams.add_hparam("distort", True) hparams.add_hparam("train_resnet", False) # image parts hparams.add_hparam("image_feat_preprocess_proj", True) hparams.add_hparam("image_feat_preprocess_layernorm", True) hparams.add_hparam("image_feat_encode", True) hparams.add_hparam("image_hidden_size", 0) # default to hidden_size hparams.add_hparam("image_filter_size", 0) # defaults to filter_size # question hidden size hparams.hidden_size = 512 hparams.filter_size = 1024 hparams.num_hidden_layers = 4 hparams.add_hparam("multimodal_combine", "concat") hparams.add_hparam("num_mlp_layers", 1) hparams.add_hparam("mlp_size", 1024) # self attention parts hparams.norm_type = "layer" hparams.layer_preprocess_sequence = "n" hparams.layer_postprocess_sequence = "da" hparams.layer_prepostprocess_dropout = 0.1 hparams.attention_dropout = 0.1 hparams.relu_dropout = 0.1 hparams.add_hparam("pos", "timing") hparams.add_hparam("num_encoder_layers", 0) hparams.add_hparam("num_decoder_layers", 0) hparams.add_hparam("num_heads", 8) hparams.add_hparam("attention_key_channels", 0) hparams.add_hparam("attention_value_channels", 0) hparams.add_hparam("self_attention_type", "dot_product") hparams.add_hparam("image_self_attention_type", "dot_product") hparams.add_hparam("question_self_attention_type", "dot_product") hparams.add_hparam("block_length", 1) hparams.add_hparam("scale_dotproduct", True) # iterative part hparams.add_hparam("num_rec_steps", 3) return hparams
Big model. def vqa_self_attention_feature_batch1024_big(): """Big model.""" hparams = vqa_self_attention_feature_batch1024() hparams.learning_rate_constant = 7e-4 hparams.batch_size = 256 hparams.hidden_size = 1024 hparams.filter_size = 4096 hparams.num_heads = 16 hparams.layer_prepostprocess_dropout = 0.3 hparams.attention_dropout = 0.3 hparams.relu_dropout = 0.3 return hparams
A default set of length-bucket boundaries. def _bucket_boundaries(max_length, min_length=8, length_bucket_step=1.1): """A default set of length-bucket boundaries.""" assert length_bucket_step > 1.0 x = min_length boundaries = [] while x < max_length: boundaries.append(x) x = max(x + 1, int(x * length_bucket_step)) return boundaries
A batching scheme based on model hyperparameters. Every batch contains a number of sequences divisible by `shard_multiplier`. Args: batch_size: int, total number of tokens in a batch. max_length: int, sequences longer than this will be skipped. Defaults to batch_size. min_length_bucket: int length_bucket_step: float greater than 1.0 drop_long_sequences: bool, if True, then sequences longer than `max_length` are dropped. This prevents generating batches with more than the usual number of tokens, which can cause out-of-memory errors. shard_multiplier: an integer increasing the batch_size to suit splitting across datashards. length_multiplier: an integer multiplier that is used to increase the batch sizes and sequence length tolerance. min_length: int, sequences shorter than this will be skipped. Returns: A dictionary with parameters that can be passed to input_pipeline: * boundaries: list of bucket boundaries * batch_sizes: list of batch sizes for each length bucket * max_length: int, maximum length of an example Raises: ValueError: If min_length > max_length def batching_scheme(batch_size, max_length, min_length_bucket, length_bucket_step, drop_long_sequences=False, shard_multiplier=1, length_multiplier=1, min_length=0): """A batching scheme based on model hyperparameters. Every batch contains a number of sequences divisible by `shard_multiplier`. Args: batch_size: int, total number of tokens in a batch. max_length: int, sequences longer than this will be skipped. Defaults to batch_size. min_length_bucket: int length_bucket_step: float greater than 1.0 drop_long_sequences: bool, if True, then sequences longer than `max_length` are dropped. This prevents generating batches with more than the usual number of tokens, which can cause out-of-memory errors. shard_multiplier: an integer increasing the batch_size to suit splitting across datashards. length_multiplier: an integer multiplier that is used to increase the batch sizes and sequence length tolerance. min_length: int, sequences shorter than this will be skipped. Returns: A dictionary with parameters that can be passed to input_pipeline: * boundaries: list of bucket boundaries * batch_sizes: list of batch sizes for each length bucket * max_length: int, maximum length of an example Raises: ValueError: If min_length > max_length """ max_length = max_length or batch_size if max_length < min_length: raise ValueError("max_length must be greater or equal to min_length") boundaries = _bucket_boundaries(max_length, min_length_bucket, length_bucket_step) boundaries = [boundary * length_multiplier for boundary in boundaries] max_length *= length_multiplier batch_sizes = [ max(1, batch_size // length) for length in boundaries + [max_length] ] max_batch_size = max(batch_sizes) # Since the Datasets API only allows a single constant for window_size, # and it needs divide all bucket_batch_sizes, we pick a highly-composite # window size and then round down all batch sizes to divisors of that window # size, so that a window can always be divided evenly into batches. # TODO(noam): remove this when Dataset API improves. highly_composite_numbers = [ 1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560, 10080, 15120, 20160, 25200, 27720, 45360, 50400, 55440, 83160, 110880, 166320, 221760, 277200, 332640, 498960, 554400, 665280, 720720, 1081080, 1441440, 2162160, 2882880, 3603600, 4324320, 6486480, 7207200, 8648640, 10810800, 14414400, 17297280, 21621600, 32432400, 36756720, 43243200, 61261200, 73513440, 110270160 ] window_size = max( [i for i in highly_composite_numbers if i <= 3 * max_batch_size]) divisors = [i for i in range(1, window_size + 1) if window_size % i == 0] batch_sizes = [max([d for d in divisors if d <= bs]) for bs in batch_sizes] window_size *= shard_multiplier batch_sizes = [bs * shard_multiplier for bs in batch_sizes] # The Datasets API splits one window into multiple batches, which # produces runs of many consecutive batches of the same size. This # is bad for training. To solve this, we will shuffle the batches # using a queue which must be several times as large as the maximum # number of batches per window. max_batches_per_window = window_size // min(batch_sizes) shuffle_queue_size = max_batches_per_window * 3 ret = { "boundaries": boundaries, "batch_sizes": batch_sizes, "min_length": min_length, "max_length": (max_length if drop_long_sequences else 10**9), "shuffle_queue_size": shuffle_queue_size, } return ret
Wrapper around _batching_scheme with hparams. def hparams_to_batching_scheme(hparams, drop_long_sequences=False, shard_multiplier=1, length_multiplier=1): """Wrapper around _batching_scheme with hparams.""" return batching_scheme( batch_size=hparams.batch_size, min_length=hparams.min_length, max_length=hparams.max_length, min_length_bucket=hparams.min_length_bucket, length_bucket_step=hparams.length_bucket_step, drop_long_sequences=drop_long_sequences, shard_multiplier=shard_multiplier, length_multiplier=length_multiplier)
Pads unknown features' dimensions for TPU. def pad_for_tpu(shapes_dict, hparams, max_length): """Pads unknown features' dimensions for TPU.""" padded_shapes = {} def get_filler(specified_max_length): if not specified_max_length: return max_length return min(specified_max_length, max_length) inputs_none_filler = get_filler(hparams.max_input_seq_length) targets_none_filler = get_filler(hparams.max_target_seq_length) def pad_one_shape(shape, none_filler): return [ (dim if dim is not None else none_filler) for dim in shape.as_list() ] for key, shape in six.iteritems(shapes_dict): if key == "inputs": padded_shapes[key] = pad_one_shape(shape, inputs_none_filler) elif key == "targets": padded_shapes[key] = pad_one_shape(shape, targets_none_filler) else: padded_shapes[key] = pad_one_shape(shape, max_length) return padded_shapes
Set the right shapes for the features. def standardize_shapes(features, batch_size=None): """Set the right shapes for the features.""" for fname in ["inputs", "targets"]: if fname not in features: continue f = features[fname] while len(f.get_shape()) < 4: f = tf.expand_dims(f, axis=-1) features[fname] = f if batch_size: # Ensure batch size is set on all features for _, t in six.iteritems(features): shape = t.get_shape().as_list() shape[0] = batch_size t.set_shape(t.get_shape().merge_with(shape)) # Assert shapes are fully known t.get_shape().assert_is_fully_defined() return features
Return the number of TFRecords in a file. def _file_num_records_cached(filename): """Return the number of TFRecords in a file.""" # Cache the result, as this is expensive to compute if filename in _file_num_records_cache: return _file_num_records_cache[filename] ret = 0 for _ in tf.python_io.tf_record_iterator(filename): ret += 1 _file_num_records_cache[filename] = ret return ret
Pad batch dim of features to nearest multiple of batch_multiple. def pad_batch(features, batch_multiple): """Pad batch dim of features to nearest multiple of batch_multiple.""" feature = list(features.items())[0][1] batch_size = tf.shape(feature)[0] mod = batch_size % batch_multiple has_mod = tf.cast(tf.cast(mod, tf.bool), tf.int32) batch_padding = batch_multiple * has_mod - mod padded_features = {} for k, feature in features.items(): rank = len(feature.shape) paddings = [[0, 0] for _ in range(rank)] paddings[0][1] = batch_padding padded_feature = tf.pad(feature, paddings) padded_features[k] = padded_feature return padded_features
Builds input pipeline for problem. Args: dataset: the dataset to make input function from. filepattern: the pattern of files to read from. skip_random_fraction_when_training: whether to skip randomly when training. batch_size_means_tokens_param: whether batch size should mean tokens. batch_size_multiplier: how to multiply batch size when bucketing. max_length: maximum length, mode: tf.estimator.ModeKeys hparams: HParams, model hparams data_dir: str, data directory; if None, will use hparams.data_dir params: dict, may include "batch_size" config: RunConfig; should have the data_parallelism attribute if not using TPU force_repeat: bool, whether to repeat the data even if not training prevent_repeat: bool, whether to not repeat when in training mode. Overrides force_repeat. Returns: (features_dict<str name, Tensor feature>, Tensor targets) def input_fn(dataset, filepattern, skip_random_fraction_when_training, batch_size_means_tokens_param, batch_size_multiplier, max_length, mode, hparams, data_dir=None, params=None, config=None, force_repeat=False, prevent_repeat=False): """Builds input pipeline for problem. Args: dataset: the dataset to make input function from. filepattern: the pattern of files to read from. skip_random_fraction_when_training: whether to skip randomly when training. batch_size_means_tokens_param: whether batch size should mean tokens. batch_size_multiplier: how to multiply batch size when bucketing. max_length: maximum length, mode: tf.estimator.ModeKeys hparams: HParams, model hparams data_dir: str, data directory; if None, will use hparams.data_dir params: dict, may include "batch_size" config: RunConfig; should have the data_parallelism attribute if not using TPU force_repeat: bool, whether to repeat the data even if not training prevent_repeat: bool, whether to not repeat when in training mode. Overrides force_repeat. Returns: (features_dict<str name, Tensor feature>, Tensor targets) """ is_training = mode == tf.estimator.ModeKeys.TRAIN if config and config.use_tpu: num_threads = 64 else: num_threads = cpu_count() if is_training else 1 if config and hasattr(config, "data_parallelism") and config.data_parallelism: num_shards = config.data_parallelism.n else: num_shards = 1 mlperf_log.transformer_print( key=mlperf_log.INPUT_MAX_LENGTH, value=max_length) def tpu_valid_size(example): return example_valid_size(example, hparams.min_length, max_length) def gpu_valid_size(example): drop_long_sequences = is_training or hparams.eval_drop_long_sequences max_validate_length = max_length if drop_long_sequences else 10**9 return example_valid_size(example, hparams.min_length, max_validate_length) def define_shapes(example): batch_size = config and config.use_tpu and params["batch_size"] return standardize_shapes(example, batch_size=batch_size) # Read and preprocess data_dir = data_dir or (hasattr(hparams, "data_dir") and hparams.data_dir) if (force_repeat or is_training) and not prevent_repeat: # Repeat and skip a random number of records dataset = dataset.repeat() if is_training and skip_random_fraction_when_training: data_files = tf.contrib.slim.parallel_reader.get_data_files(filepattern) # In continuous_train_and_eval when switching between train and # eval, this input_fn method gets called multiple times and it # would give you the exact same samples from the last call # (because the Graph seed is set). So this skip gives you some # shuffling. dataset = skip_random_fraction(dataset, data_files[0]) dataset = dataset.map(cast_ints_to_int32, num_parallel_calls=num_threads) if batch_size_means_tokens_param: batch_size_means_tokens = True else: if _are_shapes_fully_defined(dataset.output_shapes): batch_size_means_tokens = False else: tf.logging.warning( "Shapes are not fully defined. Assuming batch_size means tokens.") batch_size_means_tokens = True # Batching if not batch_size_means_tokens: # Batch size means examples per datashard. if config and config.use_tpu: # on TPU, we use params["batch_size"], which specifies the number of # examples across all datashards batch_size = params["batch_size"] dataset = dataset.batch(batch_size, drop_remainder=True) else: batch_size = hparams.batch_size * num_shards dataset = dataset.batch(batch_size) else: # batch_size means tokens per datashard if config and config.use_tpu: dataset = dataset.filter(tpu_valid_size) padded_shapes = pad_for_tpu(dataset.output_shapes, hparams, max_length) # on TPU, we use params["batch_size"], which specifies the number of # examples across all datashards batch_size = params["batch_size"] if hparams.pad_batch: tf.logging.warn( "Padding the batch to ensure that remainder eval batches are " "processed. This may lead to incorrect metrics for " "non-zero-padded features, e.g. images. Use a smaller batch " "size that has no remainder in that case.") dataset = dataset.padded_batch( batch_size, padded_shapes, drop_remainder=False) dataset = dataset.map( functools.partial(pad_batch, batch_multiple=batch_size), num_parallel_calls=num_threads) else: dataset = dataset.padded_batch( batch_size, padded_shapes, drop_remainder=True) else: # On GPU, bucket by length dataset = dataset.filter(gpu_valid_size) cur_batching_scheme = hparams_to_batching_scheme( hparams, shard_multiplier=num_shards, length_multiplier=batch_size_multiplier) if hparams.use_fixed_batch_size: # Here batch_size really means examples per datashard. cur_batching_scheme["batch_sizes"] = [hparams.batch_size] cur_batching_scheme["boundaries"] = [] dataset = dataset.apply( tf.data.experimental.bucket_by_sequence_length( example_length, cur_batching_scheme["boundaries"], cur_batching_scheme["batch_sizes"])) if not is_training: batch_multiple = num_shards if hparams.use_fixed_batch_size: # Make sure the last batch has the same fixed size as the rest. batch_multiple *= hparams.batch_size if batch_multiple > 1: tf.logging.warn( "Padding the batch to ensure that remainder eval batches have " "a batch size divisible by the number of data shards. This may " "lead to incorrect metrics for non-zero-padded features, e.g. " "images. Use a single datashard (i.e. 1 GPU) in that case.") dataset = dataset.map( functools.partial(pad_batch, batch_multiple=batch_multiple), num_parallel_calls=num_threads) dataset = dataset.map(define_shapes, num_parallel_calls=num_threads) # Add shuffling for training batches. This is necessary along with record # level shuffling in the dataset generation. Record shuffling will shuffle # the examples. However, in some cases, it's possible that the shuffle # buffer size for record shuffling is smaller than the batch size. In such # cases, adding batch shuffling ensures that the data is in random order # during training if (is_training and hasattr(hparams, "batch_shuffle_size") and hparams.batch_shuffle_size): dataset = dataset.shuffle(hparams.batch_shuffle_size) # Split batches into chunks if targets are too long. # The new "chunk_number" feature is 0 for the first chunk and goes up then. # Chunks are reversed so the 0th chunk comes first, then the 1st and so on, # so models can attend to them in the order they arrive. The last chunk is # usually the one containing the end of the target sentence (EOS). chunk_length = hparams.get("split_targets_chunk_length", 0) max_chunks = hparams.get("split_targets_max_chunks", 100) if chunk_length > 0: def is_nonzero_chunk(example): """A chunk is zero if all targets are 0s.""" return tf.less(0, tf.reduce_sum(tf.abs(example["targets"]))) def split_on_length(example): """Split a batch of ditcs on length.""" x = example["targets"] # TODO(kitaev): This code breaks if chunk_length * max_chunks < batch_size length_diff = chunk_length * max_chunks - tf.shape(x)[1] padded_x = tf.pad(x, [(0, 0), (0, length_diff), (0, 0), (0, 0)]) chunks = [padded_x[:, i*chunk_length:(i+1)*chunk_length, :, :] for i in range(max_chunks - 1)] chunks.append(padded_x[:, (max_chunks - 1)*chunk_length:, :, :]) new_example = {} # Setting chunk_number to be tf.range(max_chunks) is incompatible with TPU new_example["chunk_number"] = tf.concat([ tf.expand_dims(tf.ones_like(c) * n, axis=0) for n, c in enumerate(chunks) ], axis=0) new_example["targets"] = tf.concat( [tf.expand_dims(c, axis=0) for c in chunks], axis=0) for k in example: if k != "targets": assert k != "chunk_number", ( "Chunking code expects the chunk_number feature name to be " "available" ) new_example[k] = tf.concat( [tf.expand_dims(example[k], axis=0) for _ in range(max_chunks)], axis=0) return tf.data.Dataset.from_tensor_slices(new_example) dataset = dataset.flat_map(split_on_length) dataset = dataset.filter(is_nonzero_chunk) # The chunking data pipeline thus far creates batches of examples where all # of the examples have the same chunk number. This can lead to periodic # fluctuations in the loss; for example, when all examples in the batch have # chunk number 0 the loss may be higher than midway through a sequence. # Enabling split_targets_strided_training adjusts the data so that each # batch includes examples at various points within a sequence. if is_training and hparams.split_targets_strided_training: # TODO(kitaev): make sure that shape inference works on GPU, not just TPU. inferred_batch_size = dataset.output_shapes["targets"].as_list()[0] if inferred_batch_size is None: raise ValueError( "Strided training is only implemented when the batch size can be " "inferred statically, for example when training on TPU." ) chunk_stride = inferred_batch_size * max( 1, max_chunks // inferred_batch_size) + 1 def collapse_nested_datasets(example): """Converts a dataset of datasets to a dataset of tensor features.""" new_example = {} for k, v in example.items(): v = tf.data.experimental.get_single_element( v.batch(inferred_batch_size, drop_remainder=True)) new_example[k] = v return tf.data.Dataset.from_tensor_slices(new_example) dataset = dataset.apply(tf.data.experimental.unbatch()) dataset = dataset.window(inferred_batch_size, inferred_batch_size, chunk_stride) dataset = dataset.flat_map(collapse_nested_datasets) dataset = dataset.batch(inferred_batch_size, drop_remainder=True) def prepare_for_output(example): if not config or not config.use_tpu: _summarize_features(example, num_shards) if mode == tf.estimator.ModeKeys.PREDICT: example["infer_targets"] = example.pop("targets") return example else: return example, example["targets"] dataset = dataset.map(prepare_for_output, num_parallel_calls=num_threads) dataset = dataset.prefetch(2) if mode == tf.estimator.ModeKeys.PREDICT: # This is because of a bug in the Estimator that short-circuits prediction # if it doesn't see a QueueRunner. DummyQueueRunner implements the # minimal expected interface but does nothing. tf.add_to_collection(tf.GraphKeys.QUEUE_RUNNERS, DummyQueueRunner()) return dataset
Generate start and end indices per outfile. def generate_shard_args(outfiles, num_examples): """Generate start and end indices per outfile.""" num_shards = len(outfiles) num_examples_per_shard = num_examples // num_shards start_idxs = [i * num_examples_per_shard for i in range(num_shards)] end_idxs = list(start_idxs) end_idxs.pop(0) end_idxs.append(num_examples) return zip(start_idxs, end_idxs, outfiles)
Generate example dicts. def dataset_generator(filepath, dataset, chunk_size=1, start_idx=None, end_idx=None): """Generate example dicts.""" encoder = dna_encoder.DNAEncoder(chunk_size=chunk_size) with h5py.File(filepath, "r") as h5_file: # Get input keys from h5_file src_keys = [s % dataset for s in ["%s_in", "%s_na", "%s_out"]] src_values = [h5_file[k] for k in src_keys] inp_data, mask_data, out_data = src_values assert len(set([v.len() for v in src_values])) == 1 if start_idx is None: start_idx = 0 if end_idx is None: end_idx = inp_data.len() for i in range(start_idx, end_idx): if i % 100 == 0: print("Generating example %d for %s" % (i, dataset)) inputs, mask, outputs = inp_data[i], mask_data[i], out_data[i] ex_dict = to_example_dict(encoder, inputs, mask, outputs) # Original data has one output for every 128 input bases. Ensure that the # ratio has been maintained given the chunk size and removing EOS. assert (len(ex_dict["inputs"]) - 1) == (( 128 // chunk_size) * ex_dict["targets_shape"][0]) yield ex_dict
Convert single h5 record to an example dict. def to_example_dict(encoder, inputs, mask, outputs): """Convert single h5 record to an example dict.""" # Inputs bases = [] input_ids = [] last_idx = -1 for row in np.argwhere(inputs): idx, base_id = row idx, base_id = int(idx), int(base_id) assert idx > last_idx # if not, means 2 True values in 1 row # Some rows are all False. Those rows are mapped to UNK_ID. while idx != last_idx + 1: bases.append(encoder.UNK) last_idx += 1 bases.append(encoder.BASES[base_id]) last_idx = idx assert len(inputs) == len(bases) input_ids = encoder.encode(bases) input_ids.append(text_encoder.EOS_ID) # Targets: mask and output targets_mask = [float(v) for v in mask] # The output is (n, m); store targets_shape so that it can be reshaped # properly on the other end. targets = [float(v) for v in outputs.flatten()] targets_shape = [int(dim) for dim in outputs.shape] assert mask.shape[0] == outputs.shape[0] example_keys = ["inputs", "targets_mask", "targets", "targets_shape"] ex_dict = dict( zip(example_keys, [input_ids, targets_mask, targets, targets_shape])) return ex_dict
Linearly interpolate between two tensors at coeff. Args: tensor1: 4-D Tensor, shape=(NHWC) tensor2: 4-D Tensor, shape=(NHWC) coeffs: list of floats. Returns: interp_latents: 5-D Tensor, with interp_latents[i] representing interpolations at coeffs[i]. shape=(len(coeffs), NHWC) def linear_interpolate(tensor1, tensor2, coeffs): """Linearly interpolate between two tensors at coeff. Args: tensor1: 4-D Tensor, shape=(NHWC) tensor2: 4-D Tensor, shape=(NHWC) coeffs: list of floats. Returns: interp_latents: 5-D Tensor, with interp_latents[i] representing interpolations at coeffs[i]. shape=(len(coeffs), NHWC) """ interp_tensors = [] for coeff in coeffs: interp_tensor = tensor1 + coeff * (tensor2 - tensor1) interp_tensors.append(interp_tensor) return tf.concat(interp_tensors, axis=0)
Linearly interpolate channel at "rank" between two tensors. The channels are ranked according to their L2 norm between tensor1[channel] and tensor2[channel]. Args: tensor1: 4-D Tensor, NHWC tensor2: 4-D Tensor, NHWC coeffs: list of floats. rank: integer. Returns: interp_latents: list of interpolated 4-D Tensors, shape=(NHWC) def linear_interpolate_rank(tensor1, tensor2, coeffs, rank=1): """Linearly interpolate channel at "rank" between two tensors. The channels are ranked according to their L2 norm between tensor1[channel] and tensor2[channel]. Args: tensor1: 4-D Tensor, NHWC tensor2: 4-D Tensor, NHWC coeffs: list of floats. rank: integer. Returns: interp_latents: list of interpolated 4-D Tensors, shape=(NHWC) """ # sum across space, max across channels. _, _, _, num_channels = common_layers.shape_list(tensor1) diff_sq_sum = tf.reduce_sum((tensor1 - tensor2)**2, axis=(0, 1, 2)) _, feature_ranks = tf.math.top_k(diff_sq_sum, k=rank) feature_rank = feature_ranks[-1] channel_inds = tf.range(num_channels, dtype=tf.int32) channel_mask = tf.equal(channel_inds, feature_rank) ones_t = tf.ones(num_channels, dtype=tf.float32) zeros_t = tf.zeros(num_channels, dtype=tf.float32) interp_tensors = [] for coeff in coeffs: curr_coeff = tf.where(channel_mask, coeff * ones_t, zeros_t) interp_tensor = tensor1 + curr_coeff * (tensor2 - tensor1) interp_tensors.append(interp_tensor) return tf.concat(interp_tensors, axis=0)
Converts x from [-0.5, 0.5], to [0, 255]. Args: x: 3-D or 4-D Tensor normalized between [-0.5, 0.5] n_bits_x: Number of bits representing each pixel of the output. Defaults to 8, to default to 256 possible values. Returns: x: 3-D or 4-D Tensor representing images or videos. def postprocess(x, n_bits_x=8): """Converts x from [-0.5, 0.5], to [0, 255]. Args: x: 3-D or 4-D Tensor normalized between [-0.5, 0.5] n_bits_x: Number of bits representing each pixel of the output. Defaults to 8, to default to 256 possible values. Returns: x: 3-D or 4-D Tensor representing images or videos. """ x = tf.where(tf.is_finite(x), x, tf.ones_like(x)) x = tf.clip_by_value(x, -0.5, 0.5) x += 0.5 x = x * 2**n_bits_x return tf.cast(tf.clip_by_value(x, 0, 255), dtype=tf.uint8)
Returns a single or list of conditional latents at level 'level'. def get_cond_latents_at_level(cond_latents, level, hparams): """Returns a single or list of conditional latents at level 'level'.""" if cond_latents: if hparams.latent_dist_encoder in ["conv_net", "conv3d_net"]: return [cond_latent[level] for cond_latent in cond_latents] elif hparams.latent_dist_encoder in ["pointwise", "conv_lstm"]: return cond_latents[level]
Shape checking for cond_latents. def check_cond_latents(cond_latents, hparams): """Shape checking for cond_latents.""" if cond_latents is None: return if not isinstance(cond_latents[0], list): cond_latents = [cond_latents] exp_num_latents = hparams.num_cond_latents if hparams.latent_dist_encoder == "conv_net": exp_num_latents += int(hparams.cond_first_frame) if len(cond_latents) != exp_num_latents: raise ValueError("Expected number of cond_latents: %d, got %d" % (exp_num_latents, len(cond_latents))) for cond_latent in cond_latents: if len(cond_latent) != hparams.n_levels - 1: raise ValueError("Expected level_latents to be %d, got %d" % (hparams.n_levels - 1, len(cond_latent)))
Wrapper for data-dependent initialization. def get_variable_ddi(name, shape, initial_value, dtype=tf.float32, init=False, trainable=True): """Wrapper for data-dependent initialization.""" # If init is a tf bool: w is assigned dynamically at runtime. # If init is a python bool: then w is determined during graph construction. w = tf.get_variable(name, shape, dtype, None, trainable=trainable) if isinstance(init, bool): if init: return assign(w, initial_value) return w else: return tf.cond(init, lambda: assign(w, initial_value), lambda: w)
Dropout x with dropout_rate = rate. Apply zero dropout during init or prediction time. Args: x: 4-D Tensor, shape=(NHWC). rate: Dropout rate. init: Initialization. Returns: x: activations after dropout. def get_dropout(x, rate=0.0, init=True): """Dropout x with dropout_rate = rate. Apply zero dropout during init or prediction time. Args: x: 4-D Tensor, shape=(NHWC). rate: Dropout rate. init: Initialization. Returns: x: activations after dropout. """ if init or rate == 0: return x return tf.layers.dropout(x, rate=rate, training=True)
Applies actnorm to each time-step independently. There are a total of 2*n_channels*n_steps parameters learnt. Args: name: variable scope. x: 5-D Tensor, (NTHWC) logscale_factor: Increases the learning rate of the scale by logscale_factor. Returns: x: 5-D Tensor, (NTHWC) with the per-timestep, per-channel normalization. def actnorm_3d(name, x, logscale_factor=3.): """Applies actnorm to each time-step independently. There are a total of 2*n_channels*n_steps parameters learnt. Args: name: variable scope. x: 5-D Tensor, (NTHWC) logscale_factor: Increases the learning rate of the scale by logscale_factor. Returns: x: 5-D Tensor, (NTHWC) with the per-timestep, per-channel normalization. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x = tf.unstack(x, axis=1) x_normed = [] for ind, x_step in enumerate(x): x_step, _ = actnorm("actnorm_%d" % ind, x_step, logscale_factor=logscale_factor) x_normed.append(x_step) return tf.stack(x_normed, axis=1), None
x_{ij} = s x x_{ij} + b. Per-channel scaling and bias. If init is set to True, the scaling and bias are initialized such that the mean and variance of the output activations of the first minibatch are zero and one respectively. Args: name: variable scope. x: input logscale_factor: Used in actnorm_scale. Optimizes f(ls*s') instead of f(s) where s' = s / ls. Helps in faster convergence. reverse: forward or reverse operation. init: Whether or not to do data-dependent initialization. trainable: Returns: x: output after adding bias and scaling. objective: log(sum(s)) def actnorm(name, x, logscale_factor=3., reverse=False, init=False, trainable=True): """x_{ij} = s x x_{ij} + b. Per-channel scaling and bias. If init is set to True, the scaling and bias are initialized such that the mean and variance of the output activations of the first minibatch are zero and one respectively. Args: name: variable scope. x: input logscale_factor: Used in actnorm_scale. Optimizes f(ls*s') instead of f(s) where s' = s / ls. Helps in faster convergence. reverse: forward or reverse operation. init: Whether or not to do data-dependent initialization. trainable: Returns: x: output after adding bias and scaling. objective: log(sum(s)) """ var_arg_scope = arg_scope([get_variable_ddi], trainable=trainable) var_scope = tf.variable_scope(name, reuse=tf.AUTO_REUSE) with var_scope, var_arg_scope: if not reverse: x = actnorm_center(name + "_center", x, reverse, init=init) x, objective = actnorm_scale( name + "_scale", x, logscale_factor=logscale_factor, reverse=reverse, init=init) else: x, objective = actnorm_scale( name + "_scale", x, logscale_factor=logscale_factor, reverse=reverse, init=init) x = actnorm_center(name + "_center", x, reverse, init=init) return x, objective
Add a bias to x. Initialize such that the output of the first minibatch is zero centered per channel. Args: name: scope x: 2-D or 4-D Tensor. reverse: Forward or backward operation. init: data-dependent initialization. Returns: x_center: (x + b), if reverse is True and (x - b) otherwise. def actnorm_center(name, x, reverse=False, init=False): """Add a bias to x. Initialize such that the output of the first minibatch is zero centered per channel. Args: name: scope x: 2-D or 4-D Tensor. reverse: Forward or backward operation. init: data-dependent initialization. Returns: x_center: (x + b), if reverse is True and (x - b) otherwise. """ shape = common_layers.shape_list(x) with tf.variable_scope(name, reuse=tf.AUTO_REUSE): assert len(shape) == 2 or len(shape) == 4 if len(shape) == 2: x_mean = tf.reduce_mean(x, [0], keepdims=True) b = get_variable_ddi("b", (1, shape[1]), initial_value=-x_mean, init=init) elif len(shape) == 4: x_mean = tf.reduce_mean(x, [0, 1, 2], keepdims=True) b = get_variable_ddi( "b", (1, 1, 1, shape[3]), initial_value=-x_mean, init=init) if not reverse: x += b else: x -= b return x
Per-channel scaling of x. def actnorm_scale(name, x, logscale_factor=3., reverse=False, init=False): """Per-channel scaling of x.""" x_shape = common_layers.shape_list(x) with tf.variable_scope(name, reuse=tf.AUTO_REUSE): # Variance initialization logic. assert len(x_shape) == 2 or len(x_shape) == 4 if len(x_shape) == 2: x_var = tf.reduce_mean(x**2, [0], keepdims=True) logdet_factor = 1 var_shape = (1, x_shape[1]) elif len(x_shape) == 4: x_var = tf.reduce_mean(x**2, [0, 1, 2], keepdims=True) logdet_factor = x_shape[1]*x_shape[2] var_shape = (1, 1, 1, x_shape[3]) init_value = tf.log(1.0 / (tf.sqrt(x_var) + 1e-6)) / logscale_factor logs = get_variable_ddi("logs", var_shape, initial_value=init_value, init=init) logs = logs * logscale_factor # Function and reverse function. if not reverse: x = x * tf.exp(logs) else: x = x * tf.exp(-logs) # Objective calculation, h * w * sum(log|s|) dlogdet = tf.reduce_sum(logs) * logdet_factor if reverse: dlogdet *= -1 return x, dlogdet
1X1 convolution on x. The 1X1 convolution is parametrized as P*L*(U + sign(s)*exp(log(s))) where 1. P is a permutation matrix. 2. L is a lower triangular matrix with diagonal entries unity. 3. U is a upper triangular matrix where the diagonal entries zero. 4. s is a vector. sign(s) and P are fixed and the remaining are optimized. P, L, U and s are initialized by the PLU decomposition of a random rotation matrix. Args: name: scope x: Input Tensor. reverse: whether the pass is from z -> x or x -> z. Returns: x_conv: x after a 1X1 convolution is applied on x. objective: sum(log(s)) def invertible_1x1_conv(name, x, reverse=False): """1X1 convolution on x. The 1X1 convolution is parametrized as P*L*(U + sign(s)*exp(log(s))) where 1. P is a permutation matrix. 2. L is a lower triangular matrix with diagonal entries unity. 3. U is a upper triangular matrix where the diagonal entries zero. 4. s is a vector. sign(s) and P are fixed and the remaining are optimized. P, L, U and s are initialized by the PLU decomposition of a random rotation matrix. Args: name: scope x: Input Tensor. reverse: whether the pass is from z -> x or x -> z. Returns: x_conv: x after a 1X1 convolution is applied on x. objective: sum(log(s)) """ _, height, width, channels = common_layers.shape_list(x) w_shape = [channels, channels] # Random rotation-matrix Q random_matrix = np.random.rand(channels, channels) np_w = scipy.linalg.qr(random_matrix)[0].astype("float32") # Initialize P,L,U and s from the LU decomposition of a random rotation matrix np_p, np_l, np_u = scipy.linalg.lu(np_w) np_s = np.diag(np_u) np_sign_s = np.sign(np_s) np_log_s = np.log(np.abs(np_s)) np_u = np.triu(np_u, k=1) with tf.variable_scope(name, reuse=tf.AUTO_REUSE): p = tf.get_variable("P", initializer=np_p, trainable=False) l = tf.get_variable("L", initializer=np_l) sign_s = tf.get_variable( "sign_S", initializer=np_sign_s, trainable=False) log_s = tf.get_variable("log_S", initializer=np_log_s) u = tf.get_variable("U", initializer=np_u) # W = P * L * (U + sign_s * exp(log_s)) l_mask = np.tril(np.ones([channels, channels], dtype=np.float32), -1) l = l * l_mask + tf.eye(channels, channels) u = u * np.transpose(l_mask) + tf.diag(sign_s * tf.exp(log_s)) w = tf.matmul(p, tf.matmul(l, u)) # If height or width cannot be statically determined then they end up as # tf.int32 tensors, which cannot be directly multiplied with a floating # point tensor without a cast. objective = tf.reduce_sum(log_s) * tf.cast(height * width, log_s.dtype) if not reverse: w = tf.reshape(w, [1, 1] + w_shape) x = tf.nn.conv2d(x, w, [1, 1, 1, 1], "SAME", data_format="NHWC") else: # TODO(b/111271662): Remove when supported. def tpu_inv(m): """tf.linalg.inv workaround until it is supported on TPU.""" q, r = tf.linalg.qr(m) return tf.linalg.triangular_solve(r, tf.transpose(q), lower=False) w_inv = tf.reshape(tpu_inv(w), [1, 1]+w_shape) x = tf.nn.conv2d( x, w_inv, [1, 1, 1, 1], "SAME", data_format="NHWC") objective *= -1 return x, objective
Pad x and concatenates an edge bias across the depth of x. The edge bias can be thought of as a binary feature which is unity when the filter is being convolved over an edge and zero otherwise. Args: x: Input tensor, shape (NHWC) filter_size: filter_size to determine padding. Returns: x_pad: Input tensor, shape (NHW(c+1)) def add_edge_bias(x, filter_size): """Pad x and concatenates an edge bias across the depth of x. The edge bias can be thought of as a binary feature which is unity when the filter is being convolved over an edge and zero otherwise. Args: x: Input tensor, shape (NHWC) filter_size: filter_size to determine padding. Returns: x_pad: Input tensor, shape (NHW(c+1)) """ x_shape = common_layers.shape_list(x) if filter_size[0] == 1 and filter_size[1] == 1: return x a = (filter_size[0] - 1) // 2 # vertical padding size b = (filter_size[1] - 1) // 2 # horizontal padding size padding = [[0, 0], [a, a], [b, b], [0, 0]] x_bias = tf.zeros(x_shape[:-1] + [1]) x = tf.pad(x, padding) x_pad = tf.pad(x_bias, padding, constant_values=1) return tf.concat([x, x_pad], axis=3)
Pad left across time and pad valid across the spatial components. Also concats a binary feature that indicates if a feature is padded or not. Args: x: 5-D Tensor, (NTHWC) filter_size: list of ints dilations: list of ints, dilations - 1 specifies the number of holes between two filter elements. Returns: x_pad: 5-D Tensor. def time_pad(x, filter_size, dilations): """Pad left across time and pad valid across the spatial components. Also concats a binary feature that indicates if a feature is padded or not. Args: x: 5-D Tensor, (NTHWC) filter_size: list of ints dilations: list of ints, dilations - 1 specifies the number of holes between two filter elements. Returns: x_pad: 5-D Tensor. """ x_shape = common_layers.shape_list(x) if filter_size == [1, 1, 1]: return x _, h, w = filter_size eff_h = h + (h - 1)*(dilations[2] - 1) eff_w = w + (w - 1)*(dilations[3] - 1) a = (eff_h - 1) // 2 # vertical padding size b = (eff_w - 1) // 2 # horizontal padding size c = filter_size[0] - 1 # pad across edges. padding = [[0, 0], [c, 0], [a, a], [b, b], [0, 0]] # concat a binary feature across channels to indicate a padding. # 1 indicates that the feature is a padding. x_bias = tf.zeros(x_shape[:-1] + [1]) x_bias = tf.pad(x_bias, padding, constant_values=1) x_pad = tf.pad(x, padding) x_pad = tf.concat((x_bias, x_pad), axis=-1) return x_pad
Convolutional layer with edge bias padding and optional actnorm. If x is 5-dimensional, actnorm is applied independently across every time-step. Args: name: variable scope. x: 4-D Tensor or 5-D Tensor of shape NHWC or NTHWC output_channels: Number of output channels. filter_size: list of ints, if None [3, 3] and [2, 3, 3] are defaults for 4-D and 5-D input tensors respectively. stride: list of ints, default stride: 1 logscale_factor: see actnorm for parameter meaning. apply_actnorm: if apply_actnorm the activations of the first minibatch have zero mean and unit variance. Else, there is no scaling applied. conv_init: default or zeros. default is a normal distribution with 0.05 std. dilations: List of integers, apply dilations. Returns: x: actnorm(conv2d(x)) Raises: ValueError: if init is set to "zeros" and apply_actnorm is set to True. def conv(name, x, output_channels, filter_size=None, stride=None, logscale_factor=3.0, apply_actnorm=True, conv_init="default", dilations=None): """Convolutional layer with edge bias padding and optional actnorm. If x is 5-dimensional, actnorm is applied independently across every time-step. Args: name: variable scope. x: 4-D Tensor or 5-D Tensor of shape NHWC or NTHWC output_channels: Number of output channels. filter_size: list of ints, if None [3, 3] and [2, 3, 3] are defaults for 4-D and 5-D input tensors respectively. stride: list of ints, default stride: 1 logscale_factor: see actnorm for parameter meaning. apply_actnorm: if apply_actnorm the activations of the first minibatch have zero mean and unit variance. Else, there is no scaling applied. conv_init: default or zeros. default is a normal distribution with 0.05 std. dilations: List of integers, apply dilations. Returns: x: actnorm(conv2d(x)) Raises: ValueError: if init is set to "zeros" and apply_actnorm is set to True. """ if conv_init == "zeros" and apply_actnorm: raise ValueError("apply_actnorm is unstable when init is set to zeros.") x_shape = common_layers.shape_list(x) is_2d = len(x_shape) == 4 num_steps = x_shape[1] # set filter_size, stride and in_channels if is_2d: if filter_size is None: filter_size = [3, 3] if stride is None: stride = [1, 1] if dilations is None: dilations = [1, 1, 1, 1] actnorm_func = actnorm x = add_edge_bias(x, filter_size=filter_size) conv_filter = tf.nn.conv2d else: if filter_size is None: if num_steps == 1: filter_size = [1, 3, 3] else: filter_size = [2, 3, 3] if stride is None: stride = [1, 1, 1] if dilations is None: dilations = [1, 1, 1, 1, 1] actnorm_func = actnorm_3d x = time_pad(x, filter_size=filter_size, dilations=dilations) conv_filter = tf.nn.conv3d in_channels = common_layers.shape_list(x)[-1] filter_shape = filter_size + [in_channels, output_channels] stride_shape = [1] + stride + [1] with tf.variable_scope(name, reuse=tf.AUTO_REUSE): if conv_init == "default": initializer = default_initializer() elif conv_init == "zeros": initializer = tf.zeros_initializer() w = tf.get_variable("W", filter_shape, tf.float32, initializer=initializer) x = conv_filter(x, w, stride_shape, padding="VALID", dilations=dilations) if apply_actnorm: x, _ = actnorm_func("actnorm", x, logscale_factor=logscale_factor) else: x += tf.get_variable("b", [1, 1, 1, output_channels], initializer=tf.zeros_initializer()) logs = tf.get_variable("logs", [1, output_channels], initializer=tf.zeros_initializer()) x *= tf.exp(logs * logscale_factor) return x
2 layer conv block used in the affine coupling layer. Args: name: variable scope. x: 4-D or 5-D Tensor. mid_channels: Output channels of the second layer. dilations: Optional, list of integers. activation: relu or gatu. If relu, the second layer is relu(W*x) If gatu, the second layer is tanh(W1*x) * sigmoid(W2*x) dropout: Dropout probability. Returns: x: 4-D Tensor: Output activations. def conv_block(name, x, mid_channels, dilations=None, activation="relu", dropout=0.0): """2 layer conv block used in the affine coupling layer. Args: name: variable scope. x: 4-D or 5-D Tensor. mid_channels: Output channels of the second layer. dilations: Optional, list of integers. activation: relu or gatu. If relu, the second layer is relu(W*x) If gatu, the second layer is tanh(W1*x) * sigmoid(W2*x) dropout: Dropout probability. Returns: x: 4-D Tensor: Output activations. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x_shape = common_layers.shape_list(x) is_2d = len(x_shape) == 4 num_steps = x_shape[1] if is_2d: first_filter = [3, 3] second_filter = [1, 1] else: # special case when number of steps equal 1 to avoid # padding. if num_steps == 1: first_filter = [1, 3, 3] else: first_filter = [2, 3, 3] second_filter = [1, 1, 1] # Edge Padding + conv2d + actnorm + relu: # [output: 512 channels] x = conv("1_1", x, output_channels=mid_channels, filter_size=first_filter, dilations=dilations) x = tf.nn.relu(x) x = get_dropout(x, rate=dropout) # Padding + conv2d + actnorm + activation. # [input, output: 512 channels] if activation == "relu": x = conv("1_2", x, output_channels=mid_channels, filter_size=second_filter, dilations=dilations) x = tf.nn.relu(x) elif activation == "gatu": # x = tanh(w1*x) * sigm(w2*x) x_tanh = conv("1_tanh", x, output_channels=mid_channels, filter_size=second_filter, dilations=dilations) x_sigm = conv("1_sigm", x, output_channels=mid_channels, filter_size=second_filter, dilations=dilations) x = tf.nn.tanh(x_tanh) * tf.nn.sigmoid(x_sigm) x = get_dropout(x, rate=dropout) return x
Dilated convolutional stack. Features at different rates are computed independently using a 3 layer convolutional stack and added. Args: name: variable scope. x: 5-D Tensor. mid_channels: Number of output channels of the first layer in the conv stack. output_channels: Number of output channels of the last layer. dilation_rates: A list of dilation rates. activation: Can be either "relu" or "gatu" dropout: dropout. Returns: output: 5-D Tensor. def dilated_conv_stack(name, x, mid_channels, output_channels, dilation_rates, activation="relu", dropout=0.0): """Dilated convolutional stack. Features at different rates are computed independently using a 3 layer convolutional stack and added. Args: name: variable scope. x: 5-D Tensor. mid_channels: Number of output channels of the first layer in the conv stack. output_channels: Number of output channels of the last layer. dilation_rates: A list of dilation rates. activation: Can be either "relu" or "gatu" dropout: dropout. Returns: output: 5-D Tensor. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): output = 0.0 for dil_ind, dil_rate in enumerate(dilation_rates): # TODO(mechcoder) try (concat across channels + 1x1) modulo memory issues. curr_out = conv_stack("dil_%d" % dil_ind, x, mid_channels=mid_channels, output_channels=output_channels, dilations=dil_rate, activation=activation, dropout=dropout) output += curr_out return output
3-layer convolutional stack. Args: name: variable scope. x: 5-D Tensor. mid_channels: Number of output channels of the first layer. output_channels: Number of output channels. dilations: Dilations to apply in the first 3x3 layer and the last 3x3 layer. By default, apply no dilations. activation: relu or gatu. If relu, the second layer is relu(W*x) If gatu, the second layer is tanh(W1*x) * sigmoid(W2*x) dropout: float, 0.0 Returns: output: output of 3 layer conv network. def conv_stack(name, x, mid_channels, output_channels, dilations=None, activation="relu", dropout=0.0): """3-layer convolutional stack. Args: name: variable scope. x: 5-D Tensor. mid_channels: Number of output channels of the first layer. output_channels: Number of output channels. dilations: Dilations to apply in the first 3x3 layer and the last 3x3 layer. By default, apply no dilations. activation: relu or gatu. If relu, the second layer is relu(W*x) If gatu, the second layer is tanh(W1*x) * sigmoid(W2*x) dropout: float, 0.0 Returns: output: output of 3 layer conv network. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x = conv_block("conv_block", x, mid_channels=mid_channels, dilations=dilations, activation=activation, dropout=dropout) # Final layer. x = conv("zeros", x, apply_actnorm=False, conv_init="zeros", output_channels=output_channels, dilations=dilations) return x
Reversible additive coupling layer. Args: name: variable scope. x: 4-D Tensor, shape=(NHWC). mid_channels: number of channels in the coupling layer. reverse: Forward or reverse operation. activation: "relu" or "gatu" dropout: default, 0.0 Returns: output: 4-D Tensor, shape=(NHWC) objective: 0.0 def additive_coupling(name, x, mid_channels=512, reverse=False, activation="relu", dropout=0.0): """Reversible additive coupling layer. Args: name: variable scope. x: 4-D Tensor, shape=(NHWC). mid_channels: number of channels in the coupling layer. reverse: Forward or reverse operation. activation: "relu" or "gatu" dropout: default, 0.0 Returns: output: 4-D Tensor, shape=(NHWC) objective: 0.0 """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): output_channels = common_layers.shape_list(x)[-1] // 2 x1, x2 = tf.split(x, num_or_size_splits=2, axis=-1) z1 = x1 shift = conv_stack("nn", x1, mid_channels, output_channels=output_channels, activation=activation, dropout=dropout) if not reverse: z2 = x2 + shift else: z2 = x2 - shift return tf.concat([z1, z2], axis=3), 0.0
Reversible affine coupling layer. Args: name: variable scope. x: 4-D Tensor. mid_channels: number of channels in the coupling layer. activation: Can be either "relu" or "gatu". reverse: Forward or reverse operation. dropout: default, 0.0 Returns: output: x shifted and scaled by an affine transformation. objective: log-determinant of the jacobian def affine_coupling(name, x, mid_channels=512, activation="relu", reverse=False, dropout=0.0): """Reversible affine coupling layer. Args: name: variable scope. x: 4-D Tensor. mid_channels: number of channels in the coupling layer. activation: Can be either "relu" or "gatu". reverse: Forward or reverse operation. dropout: default, 0.0 Returns: output: x shifted and scaled by an affine transformation. objective: log-determinant of the jacobian """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x_shape = common_layers.shape_list(x) x1, x2 = tf.split(x, num_or_size_splits=2, axis=-1) # scale, shift = NN(x1) # If reverse: # z2 = scale * (x2 + shift) # Else: # z2 = (x2 / scale) - shift z1 = x1 log_scale_and_shift = conv_stack( "nn", x1, mid_channels, x_shape[-1], activation=activation, dropout=dropout) shift = log_scale_and_shift[:, :, :, 0::2] scale = tf.nn.sigmoid(log_scale_and_shift[:, :, :, 1::2] + 2.0) if not reverse: z2 = (x2 + shift) * scale else: z2 = x2 / scale - shift objective = tf.reduce_sum(tf.log(scale), axis=[1, 2, 3]) if reverse: objective *= -1 return tf.concat([z1, z2], axis=3), objective
Block-wise spatial squeezing of x to increase the number of channels. Args: name: Used for variable scoping. x: 4-D Tensor of shape (batch_size X H X W X C) factor: Factor by which the spatial dimensions should be squeezed. reverse: Squueze or unsqueeze operation. Returns: x: 4-D Tensor of shape (batch_size X (H//factor) X (W//factor) X (cXfactor^2). If reverse is True, then it is factor = (1 / factor) def squeeze(name, x, factor=2, reverse=True): """Block-wise spatial squeezing of x to increase the number of channels. Args: name: Used for variable scoping. x: 4-D Tensor of shape (batch_size X H X W X C) factor: Factor by which the spatial dimensions should be squeezed. reverse: Squueze or unsqueeze operation. Returns: x: 4-D Tensor of shape (batch_size X (H//factor) X (W//factor) X (cXfactor^2). If reverse is True, then it is factor = (1 / factor) """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): shape = common_layers.shape_list(x) if factor == 1: return x height = int(shape[1]) width = int(shape[2]) n_channels = int(shape[3]) if not reverse: assert height % factor == 0 and width % factor == 0 x = tf.reshape(x, [-1, height//factor, factor, width//factor, factor, n_channels]) x = tf.transpose(x, [0, 1, 3, 5, 2, 4]) x = tf.reshape(x, [-1, height//factor, width // factor, n_channels*factor*factor]) else: x = tf.reshape( x, (-1, height, width, int(n_channels/factor**2), factor, factor)) x = tf.transpose(x, [0, 1, 4, 2, 5, 3]) x = tf.reshape(x, (-1, int(height*factor), int(width*factor), int(n_channels/factor**2))) return x
Get a list of valid dilation rates. Args: hparams: HParams. width: spatial dimension. Ensures that the effective filter size is not larger than the spatial dimension. Returns: allowed_dilations: A list of dilation rates. def get_dilation_rates(hparams, width): """Get a list of valid dilation rates. Args: hparams: HParams. width: spatial dimension. Ensures that the effective filter size is not larger than the spatial dimension. Returns: allowed_dilations: A list of dilation rates. """ # dil_rate=1 means no dilation. allowed_dilations = [[1]*5] apply_dilations = hparams.get("latent_apply_dilations", False) dilation_rates = hparams.get("latent_dilation_rates", [1, 3]) if apply_dilations: for rate in dilation_rates: # k + (k - 1) * rate but k is harcoded to be 3 everywhere. filter_size = 3 + 2 * rate if filter_size <= width: curr_dilation = [1, 1, rate+1, rate+1, 1] allowed_dilations.append(curr_dilation) return allowed_dilations
Network that maps a time-indexed list of 3-D latents to a gaussian. Args: name: variable scope. x: List of 4-D Tensors indexed by time, (NHWC) hparams: tf.contrib.training.Hparams. output_channels: int, Number of channels of the output gaussian mean. Returns: dist: tfp.distributions.Normal def temporal_latent_to_dist(name, x, hparams, output_channels=None): """Network that maps a time-indexed list of 3-D latents to a gaussian. Args: name: variable scope. x: List of 4-D Tensors indexed by time, (NHWC) hparams: tf.contrib.training.Hparams. output_channels: int, Number of channels of the output gaussian mean. Returns: dist: tfp.distributions.Normal """ _, _, width, _, res_channels = common_layers.shape_list(x) if output_channels is None: output_channels = res_channels dilation_rates = get_dilation_rates(hparams, width) with tf.variable_scope(name, reuse=tf.AUTO_REUSE): h = x for i in range(hparams.latent_encoder_depth): if hparams.latent_apply_dilations: h2 = dilated_conv_stack("dil_latent_3d_res_%d" % i, h, mid_channels=hparams.latent_encoder_width, output_channels=res_channels, dilation_rates=dilation_rates, activation=hparams.latent_activation, dropout=hparams.latent_dropout) else: h2 = conv_stack("latent_3d_res_%d" % i, h, mid_channels=hparams.latent_encoder_width, output_channels=res_channels, activation=hparams.latent_activation, dropout=hparams.latent_dropout) h += h2 # take last activation that should capture all context since padding is # on left. h = h[:, -1, :, :, :] h = conv("res_final", h, apply_actnorm=False, conv_init="zeros", output_channels=2*output_channels, filter_size=[1, 1]) mean, log_scale = h[:, :, :, 0::2], h[:, :, :, 1::2] return tfp.distributions.Normal(mean, tf.exp(log_scale))
A 3x3 convolution mapping x to a standard normal distribution at init. Args: name: variable scope. x: 4-D Tensor. output_channels: number of channels of the mean and std. def single_conv_dist(name, x, output_channels=None): """A 3x3 convolution mapping x to a standard normal distribution at init. Args: name: variable scope. x: 4-D Tensor. output_channels: number of channels of the mean and std. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x_shape = common_layers.shape_list(x) if output_channels is None: output_channels = x_shape[-1] mean_log_scale = conv("conv2d", x, output_channels=2*output_channels, conv_init="zeros", apply_actnorm=False) mean = mean_log_scale[:, :, :, 0::2] log_scale = mean_log_scale[:, :, :, 1::2] return tf.distributions.Normal(mean, tf.exp(log_scale))
Map latent to the mean and log-scale of a Gaussian. Args: name: variable scope. x: 4-D Tensor of shape (NHWC) hparams: HParams. latent_architecture - can be "single_conv", "glow_nn" or "glow_resnet", default = single_conv latent_encoder_depth - int, depth of architecture, valid if latent_architecture is "glow_nn" or "glow_resnet". latent_pre_output_channels - 512, valid only when latent_architecture is "glow_nn". latent_encoder_width - 512, maximum width of the network output_channels: int, number of output channels of the mean (and std). if not provided, set it to be the output channels of x. Returns: dist: instance of tfp.distributions.Normal Raises: ValueError: If architecture not in ["single_conv", "glow_nn"] def latent_to_dist(name, x, hparams, output_channels=None): """Map latent to the mean and log-scale of a Gaussian. Args: name: variable scope. x: 4-D Tensor of shape (NHWC) hparams: HParams. latent_architecture - can be "single_conv", "glow_nn" or "glow_resnet", default = single_conv latent_encoder_depth - int, depth of architecture, valid if latent_architecture is "glow_nn" or "glow_resnet". latent_pre_output_channels - 512, valid only when latent_architecture is "glow_nn". latent_encoder_width - 512, maximum width of the network output_channels: int, number of output channels of the mean (and std). if not provided, set it to be the output channels of x. Returns: dist: instance of tfp.distributions.Normal Raises: ValueError: If architecture not in ["single_conv", "glow_nn"] """ architecture = hparams.get("latent_architecture", "single_conv") depth = hparams.get("latent_encoder_depth", 1) pre_output_channels = hparams.get("latent_pre_output_channels", 512) width = hparams.get("latent_encoder_width", 512) with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x_shape = common_layers.shape_list(x) if output_channels is None: output_channels = x_shape[-1] if architecture == "single_conv": return single_conv_dist("single_conv", x, output_channels) if architecture == "glow_nn": mean_log_scale = x for layer in range(1, depth + 1): mid_channels = pre_output_channels // 2**(depth - layer) mean_log_scale = conv_block("glow_nn_%d" % layer, mean_log_scale, mid_channels=mid_channels) mean_log_scale = conv("glow_nn_zeros", mean_log_scale, filter_size=[3, 3], stride=[1, 1], output_channels=2*output_channels, apply_actnorm=False, conv_init="zeros") elif architecture == "glow_resnet": h = x for layer in range(depth): h3 = conv_stack("latent_resnet_%d" % layer, h, mid_channels=width, output_channels=x_shape[-1], dropout=hparams.coupling_dropout) h += h3 mean_log_scale = conv("glow_res_final", h, conv_init="zeros", output_channels=2*output_channels, apply_actnorm=False) else: raise ValueError("expected architecture to be single_conv or glow_nn " "got %s" % architecture) mean = mean_log_scale[:, :, :, 0::2] log_scale = mean_log_scale[:, :, :, 1::2] return tfp.distributions.Normal(mean, tf.exp(log_scale))
Adds isotropic gaussian-noise to each latent. Args: latents: 4-D or 5-D tensor, shape=(NTHWC) or (NHWC). hparams: HParams. Returns: latents: latents with isotropic gaussian noise appended. def noise_op(latents, hparams): """Adds isotropic gaussian-noise to each latent. Args: latents: 4-D or 5-D tensor, shape=(NTHWC) or (NHWC). hparams: HParams. Returns: latents: latents with isotropic gaussian noise appended. """ if hparams.latent_noise == 0 or hparams.mode != tf.estimator.ModeKeys.TRAIN: return latents latent_shape = common_layers.shape_list(latents) return latents + tf.random_normal(latent_shape, stddev=hparams.latent_noise)
Merge level_dist and latent_dist. new_dist ~ N(level_dist.mean + latent_dis.mean, std) where std is determined according to merge_std. Args: level_dist: instance of tfp.distributions.Normal latent_dist: instance of tfp.distributions.Normal merge_std: can be "prev_level", "prev_step" or "normal". Returns: merged_dist: instance of tfp.distributions.Normal def merge_level_and_latent_dist(level_dist, latent_dist, merge_std="prev_level"): """Merge level_dist and latent_dist. new_dist ~ N(level_dist.mean + latent_dis.mean, std) where std is determined according to merge_std. Args: level_dist: instance of tfp.distributions.Normal latent_dist: instance of tfp.distributions.Normal merge_std: can be "prev_level", "prev_step" or "normal". Returns: merged_dist: instance of tfp.distributions.Normal """ level_mean, level_std = level_dist.loc, level_dist.scale latent_mean, latent_std = latent_dist.loc, latent_dist.scale new_mean = level_mean + latent_mean if merge_std == "normal": z_shape = common_layers.shape_list(latent_mean) log_scale = tf.get_variable( "merge_std", shape=z_shape, dtype=tf.float32, initializer=tf.zeros_initializer(), trainable=False) scale = tf.exp(log_scale * 3.0) elif merge_std == "prev_level": scale = level_std elif merge_std == "prev_step": scale = latent_std return tfp.distributions.Normal(loc=new_mean, scale=scale)
Returns a conditional prior for each level. Args: prior_dist: Distribution conditioned on the previous levels. z: Tensor, output of the previous levels. latent: Tensor or a list of tensors to condition the latent_distribution. hparams: next_frame_glow hparams. state: Current LSTM state. Used only if hparams.latent_dist_encoder is a lstm. Raises: ValueError: If hparams.latent_dist_encoder is "pointwise" and if the shape of latent is different from z. def level_cond_prior(prior_dist, z, latent, hparams, state): """Returns a conditional prior for each level. Args: prior_dist: Distribution conditioned on the previous levels. z: Tensor, output of the previous levels. latent: Tensor or a list of tensors to condition the latent_distribution. hparams: next_frame_glow hparams. state: Current LSTM state. Used only if hparams.latent_dist_encoder is a lstm. Raises: ValueError: If hparams.latent_dist_encoder is "pointwise" and if the shape of latent is different from z. """ latent_dist_encoder = hparams.get("latent_dist_encoder", None) latent_skip = hparams.get("latent_skip", False) if latent_dist_encoder == "pointwise": last_latent = latent merge_std = hparams.level_scale latent_shape = common_layers.shape_list(latent) z_shape = common_layers.shape_list(z) if latent_shape != z_shape: raise ValueError("Expected latent_shape to be %s, got %s" % (latent_shape, z_shape)) latent_dist = scale_gaussian_prior( "latent_prior", latent, logscale_factor=3.0) cond_dist = merge_level_and_latent_dist(prior_dist, latent_dist, merge_std=merge_std) elif latent_dist_encoder == "conv_net": output_channels = common_layers.shape_list(z)[-1] last_latent = latent[-1] latent_stack = tf.concat([prior_dist.loc] + latent, axis=-1) latent_stack = noise_op(latent_stack, hparams) cond_dist = latent_to_dist( "latent_stack", latent_stack, hparams=hparams, output_channels=output_channels) elif latent_dist_encoder == "conv3d_net": last_latent = latent[-1] output_channels = common_layers.shape_list(last_latent)[-1] num_steps = len(latent) # Stack across time. cond_latents = tf.stack(latent, axis=1) # Concat latents from previous levels across channels. prev_latents = tf.tile(tf.expand_dims(prior_dist.loc, axis=1), [1, num_steps, 1, 1, 1]) cond_latents = tf.concat((cond_latents, prev_latents), axis=-1) cond_latents = noise_op(cond_latents, hparams) cond_dist = temporal_latent_to_dist( "latent_stack", cond_latents, hparams, output_channels=output_channels) elif latent_dist_encoder == "conv_lstm": last_latent = latent output_channels = common_layers.shape_list(z)[-1] latent_stack = tf.concat((prior_dist.loc, latent), axis=-1) latent_stack = noise_op(latent_stack, hparams) _, state = common_video.conv_lstm_2d( latent_stack, state, hparams.latent_encoder_width, kernel_size=3, name="conv_lstm") cond_dist = single_conv_dist( "state_to_dist", state.h, output_channels=output_channels) if latent_skip: new_mean = cond_dist.loc + last_latent cond_dist = tfp.distributions.Normal(new_mean, cond_dist.scale) return cond_dist.loc, cond_dist.scale, state
Distribution on z_t conditioned on z_{t-1} and latent. Args: name: variable scope. z: 4-D Tensor. latent: optional, if hparams.latent_dist_encoder == "pointwise", this is a list of 4-D Tensors of length hparams.num_cond_latents. else, this is just a 4-D Tensor The first-three dimensions of the latent should be the same as z. hparams: next_frame_glow_hparams. condition: Whether or not to condition the distribution on latent. state: tf.nn.rnn_cell.LSTMStateTuple. the current state of a LSTM used to model the distribution. Used only if hparams.latent_dist_encoder = "conv_lstm". temperature: float, temperature with which to sample from the Gaussian. Returns: prior_dist: instance of tfp.distributions.Normal state: Returns updated state. Raises: ValueError: If hparams.latent_dist_encoder is "pointwise" and if the shape of latent is different from z. def compute_prior(name, z, latent, hparams, condition=False, state=None, temperature=1.0): """Distribution on z_t conditioned on z_{t-1} and latent. Args: name: variable scope. z: 4-D Tensor. latent: optional, if hparams.latent_dist_encoder == "pointwise", this is a list of 4-D Tensors of length hparams.num_cond_latents. else, this is just a 4-D Tensor The first-three dimensions of the latent should be the same as z. hparams: next_frame_glow_hparams. condition: Whether or not to condition the distribution on latent. state: tf.nn.rnn_cell.LSTMStateTuple. the current state of a LSTM used to model the distribution. Used only if hparams.latent_dist_encoder = "conv_lstm". temperature: float, temperature with which to sample from the Gaussian. Returns: prior_dist: instance of tfp.distributions.Normal state: Returns updated state. Raises: ValueError: If hparams.latent_dist_encoder is "pointwise" and if the shape of latent is different from z. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): if isinstance(condition, bool): condition = tf.constant(condition, dtype=tf.bool) prior_dist = single_conv_dist("level_prior", z) prior_mean, prior_scale = prior_dist.loc, prior_dist.scale if latent is None: mean, scale = prior_mean, prior_scale else: cond_mean, cond_scale, state = level_cond_prior( prior_dist, z, latent, hparams, state) mean, scale = tf.cond( condition, lambda: (cond_mean, cond_scale), lambda: (prior_mean, prior_scale)) dist = TemperedNormal(mean, scale, temperature) return dist, state
Splits / concatenates x into x1 and x2 across number of channels. For the forward pass, x2 is assumed be gaussian, i.e P(x2 | x1) ~ N(mu, sigma) where mu and sigma are the outputs of a network conditioned on x1 and optionally on cond_latents. For the reverse pass, x2 is determined from mu(x1) and sigma(x1). This is deterministic/stochastic depending on whether eps is provided. Args: name: variable scope. x: 4-D Tensor, shape (NHWC). reverse: Forward or reverse pass. eps: If eps is provided, x2 is set to be mu(x1) + eps * sigma(x1). eps_std: Sample x2 with the provided eps_std. cond_latents: optionally condition x2 on cond_latents. hparams: next_frame_glow hparams. state: tf.nn.rnn_cell.LSTMStateTuple.. Current state of the LSTM over z_2. Used only when hparams.latent_dist_encoder == "conv_lstm" condition: bool, Whether or not to condition the distribution on cond_latents. temperature: Temperature with which to sample from the gaussian. Returns: If reverse: x: 4-D Tensor, concats input and x2 across channels. x2: 4-D Tensor, a sample from N(mu(x1), sigma(x1)) Else: x1: 4-D Tensor, Output of the split operation. logpb: log-probability of x2 belonging to mu(x1), sigma(x1) eps: 4-D Tensor, (x2 - mu(x1)) / sigma(x1) x2: 4-D Tensor, Latent representation at the current level. state: Current LSTM state. 4-D Tensor, only if hparams.latent_dist_encoder is set to conv_lstm. Raises: ValueError: If latent is provided and shape is not equal to NHW(C/2) where (NHWC) is the size of x. def split(name, x, reverse=False, eps=None, eps_std=None, cond_latents=None, hparams=None, state=None, condition=False, temperature=1.0): """Splits / concatenates x into x1 and x2 across number of channels. For the forward pass, x2 is assumed be gaussian, i.e P(x2 | x1) ~ N(mu, sigma) where mu and sigma are the outputs of a network conditioned on x1 and optionally on cond_latents. For the reverse pass, x2 is determined from mu(x1) and sigma(x1). This is deterministic/stochastic depending on whether eps is provided. Args: name: variable scope. x: 4-D Tensor, shape (NHWC). reverse: Forward or reverse pass. eps: If eps is provided, x2 is set to be mu(x1) + eps * sigma(x1). eps_std: Sample x2 with the provided eps_std. cond_latents: optionally condition x2 on cond_latents. hparams: next_frame_glow hparams. state: tf.nn.rnn_cell.LSTMStateTuple.. Current state of the LSTM over z_2. Used only when hparams.latent_dist_encoder == "conv_lstm" condition: bool, Whether or not to condition the distribution on cond_latents. temperature: Temperature with which to sample from the gaussian. Returns: If reverse: x: 4-D Tensor, concats input and x2 across channels. x2: 4-D Tensor, a sample from N(mu(x1), sigma(x1)) Else: x1: 4-D Tensor, Output of the split operation. logpb: log-probability of x2 belonging to mu(x1), sigma(x1) eps: 4-D Tensor, (x2 - mu(x1)) / sigma(x1) x2: 4-D Tensor, Latent representation at the current level. state: Current LSTM state. 4-D Tensor, only if hparams.latent_dist_encoder is set to conv_lstm. Raises: ValueError: If latent is provided and shape is not equal to NHW(C/2) where (NHWC) is the size of x. """ # TODO(mechcoder) Change the return type to be a dict. with tf.variable_scope(name, reuse=tf.AUTO_REUSE): if not reverse: x1, x2 = tf.split(x, num_or_size_splits=2, axis=-1) # objective: P(x2|x1) ~N(x2 ; NN(x1)) prior_dist, state = compute_prior( "prior_on_z2", x1, cond_latents, hparams, condition, state=state) logpb = tf.reduce_sum(prior_dist.log_prob(x2), axis=[1, 2, 3]) eps = get_eps(prior_dist, x2) return x1, logpb, eps, x2, state else: prior_dist, state = compute_prior( "prior_on_z2", x, cond_latents, hparams, condition, state=state, temperature=temperature) if eps is not None: x2 = set_eps(prior_dist, eps) elif eps_std is not None: x2 = eps_std * tf.random_normal(common_layers.shape_list(x)) else: x2 = prior_dist.sample() return tf.concat([x, x2], 3), x2, state
One step of glow generative flow. Actnorm + invertible 1X1 conv + affine_coupling. Args: name: used for variable scope. x: input hparams: coupling_width is the only hparam that is being used in this function. reverse: forward or reverse pass. Returns: z: Output of one step of reversible flow. def revnet_step(name, x, hparams, reverse=True): """One step of glow generative flow. Actnorm + invertible 1X1 conv + affine_coupling. Args: name: used for variable scope. x: input hparams: coupling_width is the only hparam that is being used in this function. reverse: forward or reverse pass. Returns: z: Output of one step of reversible flow. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): if hparams.coupling == "additive": coupling_layer = functools.partial( additive_coupling, name="additive", reverse=reverse, mid_channels=hparams.coupling_width, activation=hparams.activation, dropout=hparams.coupling_dropout) else: coupling_layer = functools.partial( affine_coupling, name="affine", reverse=reverse, mid_channels=hparams.coupling_width, activation=hparams.activation, dropout=hparams.coupling_dropout) ops = [ functools.partial(actnorm, name="actnorm", reverse=reverse), functools.partial(invertible_1x1_conv, name="invertible", reverse=reverse), coupling_layer] if reverse: ops = ops[::-1] objective = 0.0 for op in ops: x, curr_obj = op(x=x) objective += curr_obj return x, objective
hparams.depth' steps of generative flow. Args: name: variable scope for the revnet block. x: 4-D Tensor, shape=(NHWC). hparams: HParams. reverse: bool, forward or backward pass. Returns: x: 4-D Tensor, shape=(NHWC). objective: float. def revnet(name, x, hparams, reverse=True): """'hparams.depth' steps of generative flow. Args: name: variable scope for the revnet block. x: 4-D Tensor, shape=(NHWC). hparams: HParams. reverse: bool, forward or backward pass. Returns: x: 4-D Tensor, shape=(NHWC). objective: float. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): steps = np.arange(hparams.depth) if reverse: steps = steps[::-1] objective = 0.0 for step in steps: x, curr_obj = revnet_step( "revnet_step_%d" % step, x, hparams, reverse=reverse) objective += curr_obj return x, objective
Returns N(s^i * z^i, std^i) where s^i and std^i are pre-component. s^i is a learnable parameter with identity initialization. std^i is optionally learnable with identity initialization. Args: name: variable scope. z: input_tensor logscale_factor: equivalent to scaling up the learning_rate by a factor of logscale_factor. trainable: Whether or not std^i is learnt. def scale_gaussian_prior(name, z, logscale_factor=3.0, trainable=True): """Returns N(s^i * z^i, std^i) where s^i and std^i are pre-component. s^i is a learnable parameter with identity initialization. std^i is optionally learnable with identity initialization. Args: name: variable scope. z: input_tensor logscale_factor: equivalent to scaling up the learning_rate by a factor of logscale_factor. trainable: Whether or not std^i is learnt. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): z_shape = common_layers.shape_list(z) latent_multiplier = tf.get_variable( "latent_multiplier", shape=z_shape, dtype=tf.float32, initializer=tf.ones_initializer()) log_scale = tf.get_variable( "log_scale_latent", shape=z_shape, dtype=tf.float32, initializer=tf.zeros_initializer(), trainable=trainable) log_scale = log_scale * logscale_factor return tfp.distributions.Normal( loc=latent_multiplier * z, scale=tf.exp(log_scale))
Unconditional prior distribution. Args: name: variable scope z_shape: Shape of the mean / scale of the prior distribution. learn_prior: Possible options are "normal" and "single_conv". If set to "single_conv", the gaussian is parametrized by a single convolutional layer whose input are an array of zeros and initialized such that the mean and std are zero and one. If set to "normal", the prior is just a Gaussian with zero mean and unit variance. temperature: Temperature with which to sample from the Gaussian. Returns: objective: 1-D Tensor shape=(batch_size,) summed across spatial components. Raises: ValueError: If learn_prior not in "normal" or "single_conv" def top_prior(name, z_shape, learn_prior="normal", temperature=1.0): """Unconditional prior distribution. Args: name: variable scope z_shape: Shape of the mean / scale of the prior distribution. learn_prior: Possible options are "normal" and "single_conv". If set to "single_conv", the gaussian is parametrized by a single convolutional layer whose input are an array of zeros and initialized such that the mean and std are zero and one. If set to "normal", the prior is just a Gaussian with zero mean and unit variance. temperature: Temperature with which to sample from the Gaussian. Returns: objective: 1-D Tensor shape=(batch_size,) summed across spatial components. Raises: ValueError: If learn_prior not in "normal" or "single_conv" """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): h = tf.zeros(z_shape, dtype=tf.float32) if learn_prior == "normal": prior_dist = tfp.distributions.Normal(h, tf.exp(h)) elif learn_prior == "single_conv": prior_dist = single_conv_dist("top_learn_prior", h) else: raise ValueError("Expected learn_prior to be normal or single_conv " "got %s" % learn_prior) return TemperedNormal(prior_dist.loc, prior_dist.scale, temperature)
Replaces x^i with q^i(x) = U(x, x + 1.0 / 256.0). Args: x: 4-D Tensor of shape (NHWC) n_bits: optional. Returns: x: x ~ U(x, x + 1.0 / 256) objective: Equivalent to -q(x)*log(q(x)). def uniform_binning_correction(x, n_bits=8): """Replaces x^i with q^i(x) = U(x, x + 1.0 / 256.0). Args: x: 4-D Tensor of shape (NHWC) n_bits: optional. Returns: x: x ~ U(x, x + 1.0 / 256) objective: Equivalent to -q(x)*log(q(x)). """ n_bins = 2**n_bits batch_size, height, width, n_channels = common_layers.shape_list(x) hwc = float(height * width * n_channels) x = x + tf.random_uniform( shape=(batch_size, height, width, n_channels), minval=0.0, maxval=1.0/n_bins) objective = -np.log(n_bins) * hwc * tf.ones(batch_size) return x, objective
Glow encoder-decoder. n_levels of (Squeeze + Flow + Split.) operations. Args: name: variable scope. x: 4-D Tensor, shape=(NHWC). hparams: HParams. eps: Stores (glow(x) - mu) / sigma during the forward pass. Used only to test if the network is reversible. reverse: Forward or reverse pass. cond_latents: list of lists of tensors. outer length equals hparams.num_cond_latents innter length equals hparams.num_levels - 1. condition: If set to True, condition the encoder/decoder on cond_latents. states: LSTM states, used only if hparams.latent_dist_encoder is set to "conv_lstm. temperature: Temperature set during sampling. Returns: x: If reverse, decoded image, else the encoded glow latent representation. objective: log-likelihood. eps: list of tensors, shape=(num_levels-1). Stores (glow(x) - mu_level(x)) / sigma_level(x)) for each level. all_latents: list of tensors, shape=(num_levels-1). Latent representatios for each level. new_states: list of tensors, shape=(num_levels-1). useful only if hparams.latent_dist_encoder="conv_lstm", returns the current state of each level. def encoder_decoder(name, x, hparams, eps=None, reverse=False, cond_latents=None, condition=False, states=None, temperature=1.0): """Glow encoder-decoder. n_levels of (Squeeze + Flow + Split.) operations. Args: name: variable scope. x: 4-D Tensor, shape=(NHWC). hparams: HParams. eps: Stores (glow(x) - mu) / sigma during the forward pass. Used only to test if the network is reversible. reverse: Forward or reverse pass. cond_latents: list of lists of tensors. outer length equals hparams.num_cond_latents innter length equals hparams.num_levels - 1. condition: If set to True, condition the encoder/decoder on cond_latents. states: LSTM states, used only if hparams.latent_dist_encoder is set to "conv_lstm. temperature: Temperature set during sampling. Returns: x: If reverse, decoded image, else the encoded glow latent representation. objective: log-likelihood. eps: list of tensors, shape=(num_levels-1). Stores (glow(x) - mu_level(x)) / sigma_level(x)) for each level. all_latents: list of tensors, shape=(num_levels-1). Latent representatios for each level. new_states: list of tensors, shape=(num_levels-1). useful only if hparams.latent_dist_encoder="conv_lstm", returns the current state of each level. """ # TODO(mechcoder) Change return_type to a dict to be backward compatible. with tf.variable_scope(name, reuse=tf.AUTO_REUSE): if states and len(states) != hparams.n_levels - 1: raise ValueError("Expected length of states to be %d, got %d" % (hparams.n_levels - 1, len(states))) if states is None: states = [None] * (hparams.n_levels - 1) if eps and len(eps) != hparams.n_levels - 1: raise ValueError("Expected length of eps to be %d, got %d" % (hparams.n_levels - 1, len(eps))) if eps is None: eps = [None] * (hparams.n_levels - 1) check_cond_latents(cond_latents, hparams) objective = 0.0 all_eps = [] all_latents = [] new_states = [] if not reverse: # Squeeze + Flow + Split for level in range(hparams.n_levels): x = squeeze("squeeze_%d" % level, x, factor=2, reverse=False) x, obj = revnet("revnet_%d" % level, x, hparams, reverse=False) objective += obj if level < hparams.n_levels - 1: curr_cond_latents = get_cond_latents_at_level( cond_latents, level, hparams) x, obj, eps, z, state = split("split_%d" % level, x, reverse=False, cond_latents=curr_cond_latents, condition=condition, hparams=hparams, state=states[level]) objective += obj all_eps.append(eps) all_latents.append(z) new_states.append(state) return x, objective, all_eps, all_latents, new_states else: for level in reversed(range(hparams.n_levels)): if level < hparams.n_levels - 1: curr_cond_latents = get_cond_latents_at_level( cond_latents, level, hparams) x, latent, state = split("split_%d" % level, x, eps=eps[level], reverse=True, cond_latents=curr_cond_latents, condition=condition, hparams=hparams, state=states[level], temperature=temperature) new_states.append(state) all_latents.append(latent) x, obj = revnet( "revnet_%d" % level, x, hparams=hparams, reverse=True) objective += obj x = squeeze("squeeze_%d" % level, x, reverse=True) return x, objective, all_latents[::-1], new_states[::-1]
A custom getter function for float32 parameters and bfloat16 activations. Args: getter: custom getter *args: arguments **kwargs: keyword arguments Returns: variables with the correct dtype. Raises: KeyError: if "dtype" is not provided as a kwarg. def bfloat16_activations_var_getter(getter, *args, **kwargs): """A custom getter function for float32 parameters and bfloat16 activations. Args: getter: custom getter *args: arguments **kwargs: keyword arguments Returns: variables with the correct dtype. Raises: KeyError: if "dtype" is not provided as a kwarg. """ requested_dtype = kwargs["dtype"] if requested_dtype == tf.bfloat16: kwargs["dtype"] = tf.float32 var = getter(*args, **kwargs) # This if statement is needed to guard the cast, because batch norm # assigns directly to the return value of this custom getter. The cast # makes the return value not a variable so it cannot be assigned. Batch # norm variables are always in fp32 so this if statement is never # triggered for them. if var.dtype.base_dtype != requested_dtype: var = tf.cast(var, requested_dtype) return var
A custom getter function for float32 parameters and float16 activations. This function ensures the following: 1. All variables requested with type fp16 are stored as type fp32. 2. All variables requested with type fp32 are returned as type fp16. See https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/ #training_tensorflow for more information on this strategy. Args: getter: custom getter *args: arguments **kwargs: keyword arguments Returns: variables with the correct dtype. Raises: KeyError: if "dtype" is not provided as a kwarg. def float16_activations_var_getter(getter, *args, **kwargs): """A custom getter function for float32 parameters and float16 activations. This function ensures the following: 1. All variables requested with type fp16 are stored as type fp32. 2. All variables requested with type fp32 are returned as type fp16. See https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/ #training_tensorflow for more information on this strategy. Args: getter: custom getter *args: arguments **kwargs: keyword arguments Returns: variables with the correct dtype. Raises: KeyError: if "dtype" is not provided as a kwarg. """ requested_dtype = kwargs["dtype"] if requested_dtype == tf.float16: kwargs["dtype"] = tf.float32 if requested_dtype == tf.float32: requested_dtype = tf.float16 var = getter(*args, **kwargs) # This if statement is needed to guard the cast, because batch norm # assigns directly to the return value of this custom getter. The cast # makes the return value not a variable so it cannot be assigned. Batch # norm variables are always in fp32 so this if statement is never # triggered for them. if var.dtype.base_dtype != requested_dtype: var = tf.cast(var, requested_dtype) return var
Simulate quantization to num_bits bits, with externally-stored scale. num_bits is the number of bits used to store each value. noise is a float32 Tensor containing values in [0, 1). Each value in noise should take different values across different steps, approximating a uniform distribution over [0, 1). In the case of replicated TPU training, noise should be identical across replicas in order to keep the parameters identical across replicas. The natural choice for noise would be tf.random_uniform(), but this is not possible for TPU, since there is currently no way to seed the different cores to produce identical values across replicas. Instead we use noise_from_step_num() (see below). The quantization scheme is as follows: Compute the maximum absolute value by row (call this max_abs). Store this either in an auxiliary variable or in an extra column. Divide the parameters by (max_abs / (2^(num_bits-1)-1)). This gives a float32 value in the range [-2^(num_bits-1)-1, 2^(num_bits-1)-1] Unbiased randomized roundoff by adding noise and rounding down. This produces a signed integer with num_bits bits which can then be stored. Args: x: a float32 Tensor num_bits: an integer between 1 and 22 noise: a float Tensor broadcastable to the shape of x. Returns: a float32 Tensor def simulated_quantize(x, num_bits, noise): """Simulate quantization to num_bits bits, with externally-stored scale. num_bits is the number of bits used to store each value. noise is a float32 Tensor containing values in [0, 1). Each value in noise should take different values across different steps, approximating a uniform distribution over [0, 1). In the case of replicated TPU training, noise should be identical across replicas in order to keep the parameters identical across replicas. The natural choice for noise would be tf.random_uniform(), but this is not possible for TPU, since there is currently no way to seed the different cores to produce identical values across replicas. Instead we use noise_from_step_num() (see below). The quantization scheme is as follows: Compute the maximum absolute value by row (call this max_abs). Store this either in an auxiliary variable or in an extra column. Divide the parameters by (max_abs / (2^(num_bits-1)-1)). This gives a float32 value in the range [-2^(num_bits-1)-1, 2^(num_bits-1)-1] Unbiased randomized roundoff by adding noise and rounding down. This produces a signed integer with num_bits bits which can then be stored. Args: x: a float32 Tensor num_bits: an integer between 1 and 22 noise: a float Tensor broadcastable to the shape of x. Returns: a float32 Tensor """ shape = x.get_shape().as_list() if not (len(shape) >= 2 and shape[-1] > 1): return x max_abs = tf.reduce_max(tf.abs(x), -1, keepdims=True) + 1e-9 max_int = 2 ** (num_bits - 1) - 1 scale = max_abs / max_int x /= scale x = tf.floor(x + noise) # dequantize before storing (since this is a simulation) x *= scale return x
Quantization noise equal to (phi * (step_num + 1)) mod 1.0. Not using random_uniform here due to a problem on TPU in that random seeds are not respected, which may cause the parameters on different replicas to go out-of-sync. Returns: a float32 scalar def noise_from_step_num(): """Quantization noise equal to (phi * (step_num + 1)) mod 1.0. Not using random_uniform here due to a problem on TPU in that random seeds are not respected, which may cause the parameters on different replicas to go out-of-sync. Returns: a float32 scalar """ step = tf.to_int32(tf.train.get_or_create_global_step()) + 1 phi = ((5 ** 0.5) - 1) / 2 # Naive computation tf.mod(phi * step, 1.0) in float32 would be disastrous # due to loss of precision when the step number gets large. # Computation in doubles does not work on TPU, so we use this complicated # alternative computation which does not suffer from these roundoff errors. ret = 0.0 for i in range(30): ret += (((phi * (2 ** i)) % 1.0) # double-precision computation in python * tf.to_float(tf.mod(step // (2 ** i), 2))) return tf.mod(ret, 1.0)
Round-off x to cand1 or to cand2 in an unbiased way. Cand1 and cand2 are the same shape as x. For every element of x, the corresponding elements of cand1 and cand2 should be the two closest bfloat16 values to x. Order does not matter. cand1 and cand2 must differ from each other. Args: x: A float32 Tensor. noise: A Tensor broadcastable to the shape of x containing random uniform values in [0.0, 1.0]. cand1: A bfloat16 Tensor the same shape as x. cand2: A bfloat16 Tensor the same shape as x. Returns: A bfloat16 Tensor. def _randomized_roundoff_to_bfloat16(x, noise, cand1, cand2): """Round-off x to cand1 or to cand2 in an unbiased way. Cand1 and cand2 are the same shape as x. For every element of x, the corresponding elements of cand1 and cand2 should be the two closest bfloat16 values to x. Order does not matter. cand1 and cand2 must differ from each other. Args: x: A float32 Tensor. noise: A Tensor broadcastable to the shape of x containing random uniform values in [0.0, 1.0]. cand1: A bfloat16 Tensor the same shape as x. cand2: A bfloat16 Tensor the same shape as x. Returns: A bfloat16 Tensor. """ cand1_f = tf.to_float(cand1) cand2_f = tf.to_float(cand2) step_size = cand2_f - cand1_f fpart = (x - cand1_f) / step_size ret = tf.where(tf.greater(fpart, noise), cand2, cand1) return ret
Convert a float32 to a bfloat16 using randomized roundoff. Args: x: A float32 Tensor. noise: a float32 Tensor with values in [0, 1), broadcastable to tf.shape(x) Returns: A float32 Tensor. def _to_bfloat16_unbiased(x, noise): """Convert a float32 to a bfloat16 using randomized roundoff. Args: x: A float32 Tensor. noise: a float32 Tensor with values in [0, 1), broadcastable to tf.shape(x) Returns: A float32 Tensor. """ x_sign = tf.sign(x) # Make sure x is positive. If it is zero, the two candidates are identical. x = x * x_sign + 1e-30 cand1 = tf.to_bfloat16(x) cand1_f = tf.to_float(cand1) # This relies on the fact that for a positive bfloat16 b, # b * 1.005 gives you the next higher bfloat16 and b*0.995 gives you the # next lower one. Both 1.005 and 0.995 are ballpark estimation. cand2 = tf.to_bfloat16( tf.where(tf.greater(x, cand1_f), cand1_f * 1.005, cand1_f * 0.995)) ret = _randomized_roundoff_to_bfloat16(x, noise, cand1, cand2) return ret * tf.to_bfloat16(x_sign)
A custom getter that uses the encoding for bfloat16 and float32 vars. When a bfloat16 or float32 variable is requsted, an encoded float16 varaible is created, which is then decoded and cast to a bfloat16 activation. Args: activation_dtype: a dtype to which to convert the decoded value. Returns: a function. def custom_getter(self, activation_dtype=tf.bfloat16): """A custom getter that uses the encoding for bfloat16 and float32 vars. When a bfloat16 or float32 variable is requsted, an encoded float16 varaible is created, which is then decoded and cast to a bfloat16 activation. Args: activation_dtype: a dtype to which to convert the decoded value. Returns: a function. """ def getter_fn(getter, *args, **kwargs): requested_dtype = kwargs["dtype"] if requested_dtype in (tf.bfloat16, tf.float32): kwargs["dtype"] = tf.bfloat16 kwargs["initializer"] = _EncodingInitializer( kwargs["initializer"], self) ret = self._decode_with_identity_gradient(getter(*args, **kwargs)) return tf.cast(ret, activation_dtype) return getter(*args, **kwargs) return getter_fn
Loads videos from files. Args: template: template string for listing the image files. video_length: length of the video. frame_shape: shape of each frame. Returns: dataset: the tf dataset frame by frame. dataset_len: number of the items which is the number of image files. Raises: ValueError: if no files found. def load_videos(template, video_length, frame_shape): """Loads videos from files. Args: template: template string for listing the image files. video_length: length of the video. frame_shape: shape of each frame. Returns: dataset: the tf dataset frame by frame. dataset_len: number of the items which is the number of image files. Raises: ValueError: if no files found. """ filenames = tf.gfile.Glob(template) if not filenames: raise ValueError("no files found.") filenames = sorted(filenames) dataset_len = len(filenames) filenames = tf.constant(filenames) dataset = tf.data.Dataset.from_tensor_slices(filenames) dataset = dataset.apply(tf.data.experimental.map_and_batch( lambda filename: load_image_map_function(filename, frame_shape), video_length, drop_remainder=True)) return dataset, dataset_len
Compute the PSNR and SSIM. Args: output: 4-D Tensor, shape=(num_frames, height, width, num_channels) target: 4-D Tensor, shape=(num_frames, height, width, num_channels) Returns: psnr: 1-D Tensor, shape=(num_frames,) ssim: 1-D Tensor, shape=(num_frames,) def psnr_and_ssim(output, target): """Compute the PSNR and SSIM. Args: output: 4-D Tensor, shape=(num_frames, height, width, num_channels) target: 4-D Tensor, shape=(num_frames, height, width, num_channels) Returns: psnr: 1-D Tensor, shape=(num_frames,) ssim: 1-D Tensor, shape=(num_frames,) """ output = tf.cast(output, dtype=tf.int32) target = tf.cast(target, dtype=tf.int32) psnr = tf.image.psnr(output, target, max_val=255) ssim = tf.image.ssim(output, target, max_val=255) return psnr, ssim
Creates dataset from in-memory predictions. def get_zipped_dataset_from_predictions(predictions): """Creates dataset from in-memory predictions.""" targets = stack_data_given_key(predictions, "targets") outputs = stack_data_given_key(predictions, "outputs") num_videos, num_steps = targets.shape[:2] # Truncate output time-steps to match target time-steps outputs = outputs[:, :num_steps] targets_placeholder = tf.placeholder(targets.dtype, targets.shape) outputs_placeholder = tf.placeholder(outputs.dtype, outputs.shape) dataset = tf.data.Dataset.from_tensor_slices( (targets_placeholder, outputs_placeholder)) iterator = dataset.make_initializable_iterator() feed_dict = {targets_placeholder: targets, outputs_placeholder: outputs} return iterator, feed_dict, num_videos
Computes the average of all the metric for one decoding. Args: iterator: dataset iterator. feed_dict: feed dict to initialize iterator. num_videos: number of videos. Returns: all_psnr: 2-D Numpy array, shape=(num_samples, num_frames) all_ssim: 2-D Numpy array, shape=(num_samples, num_frames) def compute_one_decoding_video_metrics(iterator, feed_dict, num_videos): """Computes the average of all the metric for one decoding. Args: iterator: dataset iterator. feed_dict: feed dict to initialize iterator. num_videos: number of videos. Returns: all_psnr: 2-D Numpy array, shape=(num_samples, num_frames) all_ssim: 2-D Numpy array, shape=(num_samples, num_frames) """ output, target = iterator.get_next() metrics = psnr_and_ssim(output, target) with tf.Session() as sess: sess.run(tf.local_variables_initializer()) initalizer = iterator._initializer # pylint: disable=protected-access if initalizer is not None: sess.run(initalizer, feed_dict=feed_dict) all_psnr, all_ssim = [], [] for i in range(num_videos): print("Computing video: %d" % i) psnr_np, ssim_np = sess.run(metrics) all_psnr.append(psnr_np) all_ssim.append(ssim_np) all_psnr = np.array(all_psnr) all_ssim = np.array(all_ssim) return all_psnr, all_ssim
Extracts the best-decode from the metrics according to reduce_func. Args: metrics: 3-D numpy array, shape=(num_decodes, num_samples, num_frames) reduce_func: callable, np.argmax or np.argmin. Returns: best_metrics: 2-D numpy array, shape=(num_samples, num_frames). best_decode_ind: 1-D numpy array, shape=(num_samples,) def reduce_to_best_decode(metrics, reduce_func): """Extracts the best-decode from the metrics according to reduce_func. Args: metrics: 3-D numpy array, shape=(num_decodes, num_samples, num_frames) reduce_func: callable, np.argmax or np.argmin. Returns: best_metrics: 2-D numpy array, shape=(num_samples, num_frames). best_decode_ind: 1-D numpy array, shape=(num_samples,) """ num_videos = metrics.shape[1] # Take mean of the metric across the frames to approximate the video # closest to the ground truth. mean_across_frames = np.mean(metrics, axis=-1) # For every sample, use the decode that has a maximum mean-metric. best_decode_ind = reduce_func(mean_across_frames, axis=0) best_metrics = metrics[best_decode_ind, np.arange(num_videos), :] return best_metrics, best_decode_ind
Computes statistics of metrics across multiple decodings. Args: all_results: dict of 3-D numpy arrays. Each array has shape=(num_decodes, num_samples, num_frames). Returns: statistics: dict of 1-D numpy arrays, shape=(num_frames). First the statistic (max/mean/std) is computed across the decodes, then the mean is taken across num_samples. decode_inds: dict of 1-D numpy arrays, shape=(num_samples,) Each element represents the index of the decode corresponding to the best statistic. def compute_all_metrics_statistics(all_results): """Computes statistics of metrics across multiple decodings. Args: all_results: dict of 3-D numpy arrays. Each array has shape=(num_decodes, num_samples, num_frames). Returns: statistics: dict of 1-D numpy arrays, shape=(num_frames). First the statistic (max/mean/std) is computed across the decodes, then the mean is taken across num_samples. decode_inds: dict of 1-D numpy arrays, shape=(num_samples,) Each element represents the index of the decode corresponding to the best statistic. """ statistics = {} decode_inds = {} all_metrics = all_results.keys() for key in all_metrics: values = all_results[key] statistics[key + "_MEAN"] = np.mean(values, axis=0) statistics[key + "_STD"] = np.std(values, axis=0) min_stats, min_decode_ind = reduce_to_best_decode(values, np.argmin) statistics[key + "_MIN"] = min_stats decode_inds[key + "_MIN_DECODE"] = min_decode_ind max_stats, max_decode_ind = reduce_to_best_decode(values, np.argmax) statistics[key + "_MAX"] = max_stats decode_inds[key + "_MAX_DECODE"] = max_decode_ind # Computes mean of each statistic across the dataset. for key in statistics: statistics[key] = np.mean(statistics[key], axis=0) return statistics, decode_inds
Computes metrics from predictions. Args: predictions: list of list of dicts. outer length: num_decodes, inner_length: num_samples decode_hparams: Decode hparams. instance of HParams. Returns: statistics: dict of Tensors, key being the metric with each Tensor having the shape (num_samples, num_frames). def compute_video_metrics_from_predictions(predictions, decode_hparams): """Computes metrics from predictions. Args: predictions: list of list of dicts. outer length: num_decodes, inner_length: num_samples decode_hparams: Decode hparams. instance of HParams. Returns: statistics: dict of Tensors, key being the metric with each Tensor having the shape (num_samples, num_frames). """ all_results = {} ssim_all_decodes, psnr_all_decodes = [], [] for single_decode in predictions: args = get_zipped_dataset_from_predictions(single_decode) psnr_single, ssim_single = compute_one_decoding_video_metrics(*args) psnr_all_decodes.append(psnr_single) ssim_all_decodes.append(ssim_single) psnr_all_decodes = np.array(psnr_all_decodes) ssim_all_decodes = np.array(ssim_all_decodes) all_results.update({"PSNR": psnr_all_decodes, "SSIM": ssim_all_decodes}) return compute_all_metrics_statistics(all_results)
Computes the average of all the metric for one decoding. This function assumes that all the predicted and target frames have been saved on the disk and sorting them by name will result to consecutive frames saved in order. Args: output_dirs: directory with all the saved frames. problem_name: prefix of the saved frames usually name of the problem. video_length: length of the videos. frame_shape: shape of each frame in HxWxC format. Returns: Dictionary which contains the average of each metric per frame. def compute_video_metrics_from_png_files( output_dirs, problem_name, video_length, frame_shape): """Computes the average of all the metric for one decoding. This function assumes that all the predicted and target frames have been saved on the disk and sorting them by name will result to consecutive frames saved in order. Args: output_dirs: directory with all the saved frames. problem_name: prefix of the saved frames usually name of the problem. video_length: length of the videos. frame_shape: shape of each frame in HxWxC format. Returns: Dictionary which contains the average of each metric per frame. """ ssim_all_decodes, psnr_all_decodes = [], [] for output_dir in output_dirs: output_files, target_files = get_target_and_output_filepatterns( output_dir, problem_name) args = get_zipped_dataset_from_png_files( output_files, target_files, video_length, frame_shape) psnr_single, ssim_single = compute_one_decoding_video_metrics(*args) psnr_all_decodes.append(psnr_single) ssim_all_decodes.append(ssim_single) psnr_all_decodes = np.array(psnr_all_decodes) ssim_all_decodes = np.array(ssim_all_decodes) all_results = {"PSNR": psnr_all_decodes, "SSIM": ssim_all_decodes} return compute_all_metrics_statistics(all_results)
Compute and saves the video metrics. def compute_and_save_video_metrics( output_dirs, problem_name, video_length, frame_shape): """Compute and saves the video metrics.""" statistics, all_results = compute_video_metrics_from_png_files( output_dirs, problem_name, video_length, frame_shape) for results, output_dir in zip(all_results, output_dirs): save_results(results, output_dir, problem_name) parent_dir = os.path.join(output_dirs[0], os.pardir) final_dir = os.path.join(parent_dir, "decode") tf.gfile.MakeDirs(parent_dir) save_results(statistics, final_dir, problem_name)
Swaps time and batch axis (the first two axis). def swap_time_and_batch_axes(inputs): """Swaps time and batch axis (the first two axis).""" transposed_axes = tf.concat([[1, 0], tf.range(2, tf.rank(inputs))], axis=0) return tf.transpose(inputs, transposed_axes)
Encode the given tensor to given image shape. def encode_to_shape(inputs, shape, scope): """Encode the given tensor to given image shape.""" with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): w, h = shape[1], shape[2] x = inputs x = tfl.flatten(x) x = tfl.dense(x, w * h, activation=None, name="enc_dense") x = tf.reshape(x, (-1, w, h, 1)) return x
Encode the given tensor to given image shape. def decode_to_shape(inputs, shape, scope): """Encode the given tensor to given image shape.""" with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): x = inputs x = tfl.flatten(x) x = tfl.dense(x, shape[2], activation=None, name="dec_dense") x = tf.expand_dims(x, axis=1) return x
Basic LSTM. def basic_lstm(inputs, state, num_units, name=None): """Basic LSTM.""" input_shape = common_layers.shape_list(inputs) # reuse parameters across time-steps. cell = tf.nn.rnn_cell.BasicLSTMCell( num_units, name=name, reuse=tf.AUTO_REUSE) if state is None: state = cell.zero_state(input_shape[0], tf.float32) outputs, new_state = cell(inputs, state) return outputs, new_state
Full LSTM cell. def lstm_cell(inputs, state, num_units, use_peepholes=False, cell_clip=0.0, initializer=None, num_proj=None, num_unit_shards=None, num_proj_shards=None, reuse=None, name=None): """Full LSTM cell.""" input_shape = common_layers.shape_list(inputs) cell = tf.nn.rnn_cell.LSTMCell(num_units, use_peepholes=use_peepholes, cell_clip=cell_clip, initializer=initializer, num_proj=num_proj, num_unit_shards=num_unit_shards, num_proj_shards=num_proj_shards, reuse=reuse, name=name, state_is_tuple=False) if state is None: state = cell.zero_state(input_shape[0], tf.float32) outputs, new_state = cell(inputs, state) return outputs, new_state
2D Convolutional LSTM. def conv_lstm_2d(inputs, state, output_channels, kernel_size=5, name=None, spatial_dims=None): """2D Convolutional LSTM.""" input_shape = common_layers.shape_list(inputs) batch_size, input_channels = input_shape[0], input_shape[-1] if spatial_dims is None: input_shape = input_shape[1:] else: input_shape = spatial_dims + [input_channels] cell = tf.contrib.rnn.ConvLSTMCell( 2, input_shape, output_channels, [kernel_size, kernel_size], name=name) if state is None: state = cell.zero_state(batch_size, tf.float32) outputs, new_state = cell(inputs, state) return outputs, new_state
Sample batch with specified mix of groundtruth and generated data points. Args: ground_truth_x: tensor of ground-truth data points. generated_x: tensor of generated data points. batch_size: batch size scheduled_sample_var: number of ground-truth examples to include in batch. Returns: New batch with num_ground_truth sampled from ground_truth_x and the rest from generated_x. def scheduled_sample_count(ground_truth_x, generated_x, batch_size, scheduled_sample_var): """Sample batch with specified mix of groundtruth and generated data points. Args: ground_truth_x: tensor of ground-truth data points. generated_x: tensor of generated data points. batch_size: batch size scheduled_sample_var: number of ground-truth examples to include in batch. Returns: New batch with num_ground_truth sampled from ground_truth_x and the rest from generated_x. """ num_ground_truth = scheduled_sample_var idx = tf.random_shuffle(tf.range(batch_size)) ground_truth_idx = tf.gather(idx, tf.range(num_ground_truth)) generated_idx = tf.gather(idx, tf.range(num_ground_truth, batch_size)) ground_truth_examps = tf.gather(ground_truth_x, ground_truth_idx) generated_examps = tf.gather(generated_x, generated_idx) output = tf.dynamic_stitch([ground_truth_idx, generated_idx], [ground_truth_examps, generated_examps]) # if batch size is known set it. if isinstance(batch_size, int): output.set_shape([batch_size] + common_layers.shape_list(output)[1:]) return output
Injects the additional input into the layer. Args: layer: layer that the input should be injected to. inputs: inputs to be injected. name: TF scope name. mode: how the infor should be added to the layer: "concat" concats as additional channels. "multiplicative" broadcasts inputs and multiply them to the channels. "multi_additive" broadcasts inputs and multiply and add to the channels. Returns: updated layer. Raises: ValueError: in case of unknown mode. def inject_additional_input(layer, inputs, name, mode="concat"): """Injects the additional input into the layer. Args: layer: layer that the input should be injected to. inputs: inputs to be injected. name: TF scope name. mode: how the infor should be added to the layer: "concat" concats as additional channels. "multiplicative" broadcasts inputs and multiply them to the channels. "multi_additive" broadcasts inputs and multiply and add to the channels. Returns: updated layer. Raises: ValueError: in case of unknown mode. """ layer_shape = common_layers.shape_list(layer) input_shape = common_layers.shape_list(inputs) zeros_mask = tf.zeros(layer_shape, dtype=tf.float32) if mode == "concat": emb = encode_to_shape(inputs, layer_shape, name) layer = tf.concat(values=[layer, emb], axis=-1) elif mode == "multiplicative": filters = layer_shape[-1] input_reshaped = tf.reshape(inputs, [-1, 1, 1, input_shape[-1]]) input_mask = tf.layers.dense(input_reshaped, filters, name=name) input_broad = input_mask + zeros_mask layer *= input_broad elif mode == "multi_additive": filters = layer_shape[-1] input_reshaped = tf.reshape(inputs, [-1, 1, 1, input_shape[-1]]) input_mul = tf.layers.dense(input_reshaped, filters, name=name + "_mul") layer *= tf.nn.sigmoid(input_mul) input_add = tf.layers.dense(input_reshaped, filters, name=name + "_add") layer += input_add else: raise ValueError("Unknown injection mode: %s" % mode) return layer
Probability based scheduled sampling. Args: ground_truth_x: tensor of ground-truth data points. generated_x: tensor of generated data points. batch_size: batch size scheduled_sample_var: probability of choosing from ground_truth. Returns: New batch with randomly selected data points. def scheduled_sample_prob(ground_truth_x, generated_x, batch_size, scheduled_sample_var): """Probability based scheduled sampling. Args: ground_truth_x: tensor of ground-truth data points. generated_x: tensor of generated data points. batch_size: batch size scheduled_sample_var: probability of choosing from ground_truth. Returns: New batch with randomly selected data points. """ probability_threshold = scheduled_sample_var probability_of_generated = tf.random_uniform([batch_size]) return tf.where(probability_of_generated > probability_threshold, generated_x, ground_truth_x)
Apply dynamic neural advection to previous image. Args: prev_image: previous image to be transformed. dna_input: hidden lyaer to be used for computing DNA transformation. dna_kernel_size: dna kernel size. relu_shift: shift for ReLU function. Returns: List of images transformed by the predicted CDNA kernels. def dna_transformation(prev_image, dna_input, dna_kernel_size, relu_shift): """Apply dynamic neural advection to previous image. Args: prev_image: previous image to be transformed. dna_input: hidden lyaer to be used for computing DNA transformation. dna_kernel_size: dna kernel size. relu_shift: shift for ReLU function. Returns: List of images transformed by the predicted CDNA kernels. """ # Construct translated images. prev_image_pad = tf.pad(prev_image, [[0, 0], [2, 2], [2, 2], [0, 0]]) image_height = int(prev_image.get_shape()[1]) image_width = int(prev_image.get_shape()[2]) inputs = [] for xkern in range(dna_kernel_size): for ykern in range(dna_kernel_size): inputs.append( tf.expand_dims( tf.slice(prev_image_pad, [0, xkern, ykern, 0], [-1, image_height, image_width, -1]), [3])) inputs = tf.concat(axis=3, values=inputs) # Normalize channels to 1. kernel = tf.nn.relu(dna_input - relu_shift) + relu_shift kernel = tf.expand_dims( kernel / tf.reduce_sum(kernel, [3], keep_dims=True), [4]) return tf.reduce_sum(kernel * inputs, [3], keep_dims=False)
Apply convolutional dynamic neural advection to previous image. Args: prev_image: previous image to be transformed. cdna_input: hidden lyaer to be used for computing CDNA kernels. num_masks: number of masks and hence the number of CDNA transformations. color_channels: the number of color channels in the images. dna_kernel_size: dna kernel size. relu_shift: shift for ReLU function. Returns: List of images transformed by the predicted CDNA kernels. def cdna_transformation(prev_image, cdna_input, num_masks, color_channels, dna_kernel_size, relu_shift): """Apply convolutional dynamic neural advection to previous image. Args: prev_image: previous image to be transformed. cdna_input: hidden lyaer to be used for computing CDNA kernels. num_masks: number of masks and hence the number of CDNA transformations. color_channels: the number of color channels in the images. dna_kernel_size: dna kernel size. relu_shift: shift for ReLU function. Returns: List of images transformed by the predicted CDNA kernels. """ batch_size = tf.shape(cdna_input)[0] height = int(prev_image.get_shape()[1]) width = int(prev_image.get_shape()[2]) # Predict kernels using linear function of last hidden layer. cdna_kerns = tfl.dense( cdna_input, dna_kernel_size * dna_kernel_size * num_masks, name="cdna_params", activation=None) # Reshape and normalize. cdna_kerns = tf.reshape( cdna_kerns, [batch_size, dna_kernel_size, dna_kernel_size, 1, num_masks]) cdna_kerns = (tf.nn.relu(cdna_kerns - relu_shift) + relu_shift) norm_factor = tf.reduce_sum(cdna_kerns, [1, 2, 3], keep_dims=True) cdna_kerns /= norm_factor # Treat the color channel dimension as the batch dimension since the same # transformation is applied to each color channel. # Treat the batch dimension as the channel dimension so that # depthwise_conv2d can apply a different transformation to each sample. cdna_kerns = tf.transpose(cdna_kerns, [1, 2, 0, 4, 3]) cdna_kerns = tf.reshape( cdna_kerns, [dna_kernel_size, dna_kernel_size, batch_size, num_masks]) # Swap the batch and channel dimensions. prev_image = tf.transpose(prev_image, [3, 1, 2, 0]) # Transform image. transformed = tf.nn.depthwise_conv2d( prev_image, cdna_kerns, [1, 1, 1, 1], "SAME") # Transpose the dimensions to where they belong. transformed = tf.reshape( transformed, [color_channels, height, width, batch_size, num_masks]) transformed = tf.transpose(transformed, [3, 1, 2, 0, 4]) transformed = tf.unstack(transformed, axis=-1) return transformed
A layer of VGG network with batch norm. Args: inputs: image tensor nout: number of output channels kernel_size: size of the kernel activation: activation function padding: padding of the image is_training: whether it is training mode or not has_batchnorm: whether batchnorm is applied or not scope: variable scope of the op Returns: net: output of layer def vgg_layer(inputs, nout, kernel_size=3, activation=tf.nn.leaky_relu, padding="SAME", is_training=True, has_batchnorm=False, scope=None): """A layer of VGG network with batch norm. Args: inputs: image tensor nout: number of output channels kernel_size: size of the kernel activation: activation function padding: padding of the image is_training: whether it is training mode or not has_batchnorm: whether batchnorm is applied or not scope: variable scope of the op Returns: net: output of layer """ with tf.variable_scope(scope): net = tfl.conv2d(inputs, nout, kernel_size=kernel_size, padding=padding, activation=None, name="conv") if has_batchnorm: net = tfl.batch_normalization(net, training=is_training, name="bn") net = activation(net) return net
Tile latent and concatenate to image across depth. Args: image: 4-D Tensor, (batch_size X height X width X channels) latent: 2-D Tensor, (batch_size X latent_dims) concat_latent: If set to False, the image is returned as is. Returns: concat_latent: 4-D Tensor, (batch_size X height X width X channels+1) latent tiled and concatenated to the image across the channels. def tile_and_concat(image, latent, concat_latent=True): """Tile latent and concatenate to image across depth. Args: image: 4-D Tensor, (batch_size X height X width X channels) latent: 2-D Tensor, (batch_size X latent_dims) concat_latent: If set to False, the image is returned as is. Returns: concat_latent: 4-D Tensor, (batch_size X height X width X channels+1) latent tiled and concatenated to the image across the channels. """ if not concat_latent: return image image_shape = common_layers.shape_list(image) latent_shape = common_layers.shape_list(latent) height, width = image_shape[1], image_shape[2] latent_dims = latent_shape[1] height_multiples = height // latent_dims pad = height - (height_multiples * latent_dims) latent = tf.reshape(latent, (-1, latent_dims, 1, 1)) latent = tf.tile(latent, (1, height_multiples, width, 1)) latent = tf.pad(latent, [[0, 0], [pad // 2, pad // 2], [0, 0], [0, 0]]) return tf.concat([image, latent], axis=-1)
Encodes numpy images into gif string. Args: images: A 4-D `uint8` `np.array` (or a list of 3-D images) of shape `[time, height, width, channels]` where `channels` is 1 or 3. fps: frames per second of the animation Returns: The encoded gif string. Raises: IOError: If the ffmpeg command returns an error. def _encode_gif(images, fps): """Encodes numpy images into gif string. Args: images: A 4-D `uint8` `np.array` (or a list of 3-D images) of shape `[time, height, width, channels]` where `channels` is 1 or 3. fps: frames per second of the animation Returns: The encoded gif string. Raises: IOError: If the ffmpeg command returns an error. """ writer = WholeVideoWriter(fps) writer.write_multi(images) return writer.finish()
Tries to encode images with ffmpeg to check if it works. def ffmpeg_works(): """Tries to encode images with ffmpeg to check if it works.""" images = np.zeros((2, 32, 32, 3), dtype=np.uint8) try: _encode_gif(images, 2) return True except (IOError, OSError): return False
Outputs a `Summary` protocol buffer with gif animations. Args: tag: Name of the summary. images: A 5-D `uint8` `np.array` of shape `[batch_size, time, height, width, channels]` where `channels` is 1 or 3. max_outputs: Max number of batch elements to generate gifs for. fps: frames per second of the animation. return_summary_value: If set to True, return a list of tf.Summary.Value objects in addition to the protocol buffer. Returns: The serialized `Summary` protocol buffer. Raises: ValueError: If `images` is not a 5-D `uint8` array with 1 or 3 channels. def py_gif_summary(tag, images, max_outputs, fps, return_summary_value=False): """Outputs a `Summary` protocol buffer with gif animations. Args: tag: Name of the summary. images: A 5-D `uint8` `np.array` of shape `[batch_size, time, height, width, channels]` where `channels` is 1 or 3. max_outputs: Max number of batch elements to generate gifs for. fps: frames per second of the animation. return_summary_value: If set to True, return a list of tf.Summary.Value objects in addition to the protocol buffer. Returns: The serialized `Summary` protocol buffer. Raises: ValueError: If `images` is not a 5-D `uint8` array with 1 or 3 channels. """ images = np.asarray(images) if images.dtype != np.uint8: raise ValueError("Tensor must have dtype uint8 for gif summary.") if images.ndim != 5: raise ValueError("Tensor must be 5-D for gif summary.") batch_size, _, height, width, channels = images.shape if channels not in (1, 3): raise ValueError("Tensors must have 1 or 3 channels for gif summary.") summ = tf.Summary() all_summ_values = [] num_outputs = min(batch_size, max_outputs) for i in range(num_outputs): image_summ = tf.Summary.Image() image_summ.height = height image_summ.width = width image_summ.colorspace = channels # 1: grayscale, 3: RGB try: image_summ.encoded_image_string = _encode_gif(images[i], fps) except (IOError, OSError) as e: tf.logging.warning( "Unable to encode images to a gif string because either ffmpeg is " "not installed or ffmpeg returned an error: %s. Falling back to an " "image summary of the first frame in the sequence.", e) try: from PIL import Image # pylint: disable=g-import-not-at-top import io # pylint: disable=g-import-not-at-top with io.BytesIO() as output: Image.fromarray(images[i][0]).save(output, "PNG") image_summ.encoded_image_string = output.getvalue() except ImportError as e: tf.logging.warning( "Gif summaries requires ffmpeg or PIL to be installed: %s", e) image_summ.encoded_image_string = "" if num_outputs == 1: summ_tag = "{}/gif".format(tag) else: summ_tag = "{}/gif/{}".format(tag, i) curr_summ_value = tf.Summary.Value(tag=summ_tag, image=image_summ) all_summ_values.append(curr_summ_value) summ.value.add(tag=summ_tag, image=image_summ) summ_str = summ.SerializeToString() if return_summary_value: return all_summ_values, summ_str return summ_str
Outputs a `Summary` protocol buffer with gif animations. Args: name: Name of the summary. tensor: A 5-D `uint8` `Tensor` of shape `[batch_size, time, height, width, channels]` where `channels` is 1 or 3. max_outputs: Max number of batch elements to generate gifs for. fps: frames per second of the animation collections: Optional list of tf.GraphKeys. The collections to add the summary to. Defaults to [tf.GraphKeys.SUMMARIES] family: Optional; if provided, used as the prefix of the summary tag name, which controls the tab name used for display on Tensorboard. Returns: A scalar `Tensor` of type `string`. The serialized `Summary` protocol buffer. Raises: ValueError: if the given tensor has the wrong shape. def gif_summary(name, tensor, max_outputs=3, fps=10, collections=None, family=None): """Outputs a `Summary` protocol buffer with gif animations. Args: name: Name of the summary. tensor: A 5-D `uint8` `Tensor` of shape `[batch_size, time, height, width, channels]` where `channels` is 1 or 3. max_outputs: Max number of batch elements to generate gifs for. fps: frames per second of the animation collections: Optional list of tf.GraphKeys. The collections to add the summary to. Defaults to [tf.GraphKeys.SUMMARIES] family: Optional; if provided, used as the prefix of the summary tag name, which controls the tab name used for display on Tensorboard. Returns: A scalar `Tensor` of type `string`. The serialized `Summary` protocol buffer. Raises: ValueError: if the given tensor has the wrong shape. """ tensor = tf.convert_to_tensor(tensor) if len(tensor.get_shape()) != 5: raise ValueError("Assuming videos given as tensors in the format " "[batch, time, height, width, channels] but got one " "of shape: %s" % str(tensor.get_shape())) tensor = tf.cast(tensor, tf.uint8) if distribute_summary_op_util.skip_summary(): return tf.constant("") with summary_op_util.summary_scope( name, family, values=[tensor]) as (tag, scope): val = tf.py_func( py_gif_summary, [tag, tensor, max_outputs, fps], tf.string, stateful=False, name=scope) summary_op_util.collect(val, collections, [tf.GraphKeys.SUMMARIES]) return val
Builds convolutional latent tower for stochastic model. At training time this tower generates a latent distribution (mean and std) conditioned on the entire video. This latent variable will be fed to the main tower as an extra variable to be used for future frames prediction. At inference time, the tower is disabled and only returns latents sampled from N(0,1). If the multi_latent flag is on, a different latent for every timestep would be generated. Args: images: tensor of ground truth image sequences time_axis: the time axis in images tensor latent_channels: number of latent channels min_logvar: minimum value for log_var is_training: whether or not it is training mode random_latent: whether or not generate random latents tiny_mode: whether or not it is tiny_mode. tiny_mode sets the number of conv channels to 1 at each layer. useful for testing the integration tests. small_mode: whether or not it is small_mode. small mode is the same model with less conv and lstm layers and also lower number of channels. suitable for videos with less complexity and testing. Returns: latent_mean: predicted latent mean latent_logvar: predicted latent log variance def conv_latent_tower(images, time_axis, latent_channels=1, min_logvar=-5, is_training=False, random_latent=False, tiny_mode=False, small_mode=False): """Builds convolutional latent tower for stochastic model. At training time this tower generates a latent distribution (mean and std) conditioned on the entire video. This latent variable will be fed to the main tower as an extra variable to be used for future frames prediction. At inference time, the tower is disabled and only returns latents sampled from N(0,1). If the multi_latent flag is on, a different latent for every timestep would be generated. Args: images: tensor of ground truth image sequences time_axis: the time axis in images tensor latent_channels: number of latent channels min_logvar: minimum value for log_var is_training: whether or not it is training mode random_latent: whether or not generate random latents tiny_mode: whether or not it is tiny_mode. tiny_mode sets the number of conv channels to 1 at each layer. useful for testing the integration tests. small_mode: whether or not it is small_mode. small mode is the same model with less conv and lstm layers and also lower number of channels. suitable for videos with less complexity and testing. Returns: latent_mean: predicted latent mean latent_logvar: predicted latent log variance """ conv_size = tinyify([32, 64, 64], tiny_mode, small_mode) with tf.variable_scope("latent", reuse=tf.AUTO_REUSE): images = tf.to_float(images) images = tf.unstack(images, axis=time_axis) images = tf.concat(images, axis=3) x = images x = common_layers.make_even_size(x) x = tfl.conv2d(x, conv_size[0], [3, 3], strides=(2, 2), padding="SAME", activation=tf.nn.relu, name="latent_conv1") x = tfcl.layer_norm(x) if not small_mode: x = tfl.conv2d(x, conv_size[1], [3, 3], strides=(2, 2), padding="SAME", activation=tf.nn.relu, name="latent_conv2") x = tfcl.layer_norm(x) x = tfl.conv2d(x, conv_size[2], [3, 3], strides=(1, 1), padding="SAME", activation=tf.nn.relu, name="latent_conv3") x = tfcl.layer_norm(x) nc = latent_channels mean = tfl.conv2d(x, nc, [3, 3], strides=(2, 2), padding="SAME", activation=None, name="latent_mean") logv = tfl.conv2d(x, nc, [3, 3], strides=(2, 2), padding="SAME", activation=tf.nn.relu, name="latent_std") logvar = logv + min_logvar # No latent tower at inference time, just standard gaussian. if not is_training: return tf.zeros_like(mean), tf.zeros_like(logvar) # No latent in the first phase ret_mean, ret_logvar = tf.cond( random_latent, lambda: (tf.zeros_like(mean), tf.zeros_like(logvar)), lambda: (mean, logvar)) return ret_mean, ret_logvar
Get KL multiplier (beta) based on the schedule. def beta_schedule(schedule, global_step, final_beta, decay_start, decay_end): """Get KL multiplier (beta) based on the schedule.""" if decay_start > decay_end: raise ValueError("decay_end is smaller than decay_end.") # Since some of the TF schedules do not support incrementing a value, # in all of the schedules, we anneal the beta from final_beta to zero # and then reverse it at the bottom. if schedule == "constant": decayed_value = 0.0 elif schedule == "linear": decayed_value = tf.train.polynomial_decay( learning_rate=final_beta, global_step=global_step - decay_start, decay_steps=decay_end - decay_start, end_learning_rate=0.0) elif schedule == "noisy_linear_cosine_decay": decayed_value = tf.train.noisy_linear_cosine_decay( learning_rate=final_beta, global_step=global_step - decay_start, decay_steps=decay_end - decay_start) # TODO(mechcoder): Add log_annealing schedule. else: raise ValueError("Unknown beta schedule.") increased_value = final_beta - decayed_value increased_value = tf.maximum(0.0, increased_value) beta = tf.case( pred_fn_pairs={ tf.less(global_step, decay_start): lambda: 0.0, tf.greater(global_step, decay_end): lambda: final_beta}, default=lambda: increased_value) return beta
For every video, extract a random consecutive patch of num_frames. Args: videos: 5-D Tensor, (NTHWC) num_frames: Integer, if -1 then the entire video is returned. Returns: video_patch: 5-D Tensor, (NTHWC) with T = num_frames. Raises: ValueError: If num_frames is greater than the number of total frames in the video. def extract_random_video_patch(videos, num_frames=-1): """For every video, extract a random consecutive patch of num_frames. Args: videos: 5-D Tensor, (NTHWC) num_frames: Integer, if -1 then the entire video is returned. Returns: video_patch: 5-D Tensor, (NTHWC) with T = num_frames. Raises: ValueError: If num_frames is greater than the number of total frames in the video. """ if num_frames == -1: return videos batch_size, num_total_frames, h, w, c = common_layers.shape_list(videos) if num_total_frames < num_frames: raise ValueError("Expected num_frames <= %d, got %d" % (num_total_frames, num_frames)) # Randomly choose start_inds for each video. frame_start = tf.random_uniform( shape=(batch_size,), minval=0, maxval=num_total_frames - num_frames + 1, dtype=tf.int32) # [start[0], start[0] + 1, ... start[0] + num_frames - 1] + ... # [start[batch_size-1], ... start[batch_size-1] + num_frames - 1] range_inds = tf.expand_dims(tf.range(num_frames), axis=0) frame_inds = range_inds + tf.expand_dims(frame_start, axis=1) frame_inds = tf.reshape(frame_inds, [-1]) # [0]*num_frames + [1]*num_frames + ... [batch_size-1]*num_frames batch_inds = tf.expand_dims(tf.range(batch_size), axis=1) batch_inds = tf.tile(batch_inds, [1, num_frames]) batch_inds = tf.reshape(batch_inds, [-1]) gather_inds = tf.stack((batch_inds, frame_inds), axis=1) video_patches = tf.gather_nd(videos, gather_inds) return tf.reshape(video_patches, (batch_size, num_frames, h, w, c))
Writes multiple video frames. def write_multi(self, frames, encoded_frames=None): """Writes multiple video frames.""" if encoded_frames is None: # Infinite iterator. encoded_frames = iter(lambda: None, 1) for (frame, encoded_frame) in zip(frames, encoded_frames): self.write(frame, encoded_frame)
Initializes ffmpeg to write frames. def __init_ffmpeg(self, image_shape): """Initializes ffmpeg to write frames.""" import itertools # pylint: disable=g-import-not-at-top from subprocess import Popen, PIPE # pylint: disable=g-import-not-at-top,g-multiple-import,g-importing-member ffmpeg = "ffmpeg" height, width, channels = image_shape self.cmd = [ ffmpeg, "-y", "-f", "rawvideo", "-vcodec", "rawvideo", "-r", "%.02f" % self.fps, "-s", "%dx%d" % (width, height), "-pix_fmt", {1: "gray", 3: "rgb24"}[channels], "-i", "-", "-filter_complex", "[0:v]split[x][z];[x]fifo[w];[z]palettegen,fifo[y];" "[w][y]paletteuse,fifo", "-r", "%.02f" % self.fps, "-f", self.file_format, "-qscale", "0", "-" ] self.proc = Popen( self.cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=-1 ) (self._out_thread, self._err_thread) = itertools.starmap( self._start_reader_thread, [ (self.proc.stdout, self._out_chunks), (self.proc.stderr, self._err_chunks) ] )