def reduce_mul(x): bid_len = tf.cast(x[self.MAX_SEQ_LEN], dtype=tf.int32) market_len = tf.cast(x[self.MAX_SEQ_LEN + 1], dtype=tf.int32) survival_rate_last_one = tf.reduce_prod(x[0:bid_len]) anlp_rate_last_one = tf.reduce_prod(x[0:market_len + 1]) anlp_rate_last_two = tf.reduce_prod(x[0:market_len]) ret = tf.stack([ survival_rate_last_one, anlp_rate_last_one, anlp_rate_last_two ]) return ret
def compute_cv(vc, iteration): cv_list = [] prod_list = [] min_list = [] if SUM_PRODUCT: vc = tf.clip_by_value(vc, -10, 10) tanh_vc = tf.tanh(vc / 2.0) edge_order = [] for i in range(0, m): # for each check node c for j in range(0, chk_degrees[i]): # edge = u[i][j] edge_order.append(u[i][j]) extrinsic_edges = [] for jj in range(0, chk_degrees[i]): if jj != j: extrinsic_edges.append(u[i][jj]) if SUM_PRODUCT: temp = tf.gather(tanh_vc, extrinsic_edges) temp = tf.reduce_prod(temp, 0) temp = tf.log((1 + temp) / (1 - temp)) cv_list.append(temp) if MIN_SUM: temp = tf.gather(vc, extrinsic_edges) temp1 = tf.reduce_prod(tf.sign(temp), 0) temp2 = tf.reduce_min(tf.abs(temp), 0) prod_list.append(temp1) min_list.append(temp2) if SUM_PRODUCT: cv = tf.stack(cv_list) if MIN_SUM: prods = tf.stack(prod_list) mins = tf.stack(min_list) if decoder.decoder_type == "RNOMS": # offsets = tf.nn.softplus(decoder.B_cv) # mins = tf.nn.relu(mins - tf.tile(tf.reshape(offsets,[-1,1]),[1,batch_size])) mins = tf.nn.relu(mins - decoder.B_cv) elif decoder.decoder_type == "FNOMS": offsets = tf.nn.softplus(decoder.B_cv[iteration]) mins = tf.nn.relu(mins - tf.tile(tf.reshape(offsets, [-1, 1]), [1, batch_size])) cv = prods * mins new_order = np.zeros(num_edges).astype(np.int) new_order[edge_order] = np.array(range(0, num_edges)).astype(np.int) cv = tf.gather(cv, new_order) if decoder.decoder_type == "RNSPA" or decoder.decoder_type == "RNNMS": cv = cv * tf.tile(tf.reshape(decoder.W_cv, [-1, 1]), [1, batch_size]) elif decoder.decoder_type == "FNSPA" or decoder.decoder_type == "FNNMS": cv = cv * tf.tile(tf.reshape(decoder.W_cv[iteration], [-1, 1]), [1, batch_size]) return cv
def _create_var(name: str, value_expr: TfExpression) -> TfExpression: """Internal helper for creating autosummary accumulators.""" assert not _finalized name_id = name.replace("/", "_") v = tf.cast(value_expr, _dtype) if v.shape.is_fully_defined(): size = np.prod(v.shape.as_list()) size_expr = tf.constant(size, dtype=_dtype) else: size = None size_expr = tf.reduce_prod(tf.cast(tf.shape(v), _dtype)) if size == 1: if v.shape.ndims != 0: v = tf.reshape(v, []) v = [size_expr, v, tf.square(v)] else: v = [size_expr, tf.reduce_sum(v), tf.reduce_sum(tf.square(v))] v = tf.cond(tf.is_finite(v[1]), lambda: tf.stack(v), lambda: tf.zeros(3, dtype=_dtype)) with tfutil.absolute_name_scope("Autosummary/" + name_id), tf.control_dependencies(None): var = tf.Variable(tf.zeros(3, dtype=_dtype), trainable=False) # [sum(1), sum(x), sum(x**2)] update_op = tf.cond(tf.is_variable_initialized(var), lambda: tf.assign_add(var, v), lambda: tf.assign(var, v)) if name in _vars: _vars[name].append(var) else: _vars[name] = [var] return update_op
def expand_first_dimension(inputs, dims): """Expands `K-d` tensor along first dimension to be a `(K+n-1)-d` tensor. Converts `inputs` with shape [D0, D1, ..., D(K-1)] into a tensor of shape [dims[0], dims[1], ..., dims[-1], D1, ..., D(k-1)]. Example: `inputs` is a tensor with shape [50, 20, 20, 3]. new_tensor = expand_first_dimension(inputs, [10, 5]). new_tensor.shape -> [10, 5, 20, 20, 3]. Args: inputs: a tensor with shape [D0, D1, ..., D(K-1)]. dims: List with new dimensions to expand first axis into. The length of `dims` is typically 2 or larger. Returns: a tensor with shape [dims[0], dims[1], ..., dims[-1], D1, ..., D(k-1)]. """ inputs_shape = combined_static_and_dynamic_shape(inputs) expanded_shape = tf.stack(dims + inputs_shape[1:]) # Verify that it is possible to expand the first axis of inputs. assert_op = tf.assert_equal( inputs_shape[0], tf.reduce_prod(tf.stack(dims)), message=( 'First dimension of `inputs` cannot be expanded into provided ' '`dims`')) with tf.control_dependencies([assert_op]): inputs_reshaped = tf.reshape(inputs, expanded_shape) return inputs_reshaped
def flatten(index, name='segmented_flatten'): """Flattens a batched index map to a 1d index map. This operation relabels the segments to keep batch elements distinct. The k-th batch element will have indices shifted by `num_segments` * (k - 1). The result is a tensor with `num_segments` multiplied by the number of elements in the batch. Args: index: IndexMap to flatten. name: Name for the TensorFlow operation. Returns: The flattened IndexMap. """ with tf.variable_scope(name): batch_size = tf.reduce_prod(index.batch_shape()) offset = tf.range(batch_size) * index.num_segments offset = tf.reshape(offset, index.batch_shape()) for _ in range(index.batch_dims, index.indices.shape.rank): offset = tf.expand_dims(offset, -1) indices = offset + index.indices return IndexMap(indices=tf.reshape(indices, [-1]), num_segments=index.num_segments * batch_size, batch_dims=0)
def __init__(self, regularizers_to_group): """Creates an instance. Args: regularizers_to_group: A list of generic_regularizers.OpRegularizer objects. Their regularization_vector (alive_vector) are expected to be of the same length. These are expected to be probabilistic regularizers. Currently, the only supported OpRegularizer is ProbGatingRegularizer. Raises: ValueError: regularizers_to_group is not of length at least 2. """ if len(regularizers_to_group) < 2: raise ValueError('Groups must be of at least size 2.') regularization_vectors = [] alive_vectors = [] for reg in regularizers_to_group: if not hasattr(reg, 'is_probabilistic') or not reg.is_probabilistic: raise ValueError('Regularizer is not probabilistic.') regularization_vectors.append( tf.reshape(reg.regularization_vector, [1, -1])) alive_vectors.append(tf.reshape(reg.alive_vector, [1, -1])) regularization_vectors = tf.concat(regularization_vectors, axis=0) alive_vectors = tf.concat(alive_vectors, axis=0) # The probability that at least one of the channels is alive: # 1 - \prod_i{1 - p_i}. self._regularization_vector = 1.0 - tf.reduce_prod( 1.0 - regularization_vectors, axis=0) self._alive_vector = tf.reduce_any(alive_vectors, axis=0)
def bilinear_wrapper(imgs, coords): """Wrapper around bilinear sampling function, handles arbitrary input sizes. Args: imgs: are [B,H,W,C] coords: [B,H,W,2] indicating the source pixels to copy from Returns: [B,H,W,C] images after bilinear sampling from input. """ # the bilinear sampling code only handles 4D input, so we'll need to reshape init_dims = tf.shape(imgs)[:-3] end_dims_img = tf.shape(imgs)[-3:] end_dims_coords = tf.shape(coords)[-3:] prod_init_dims = tf.reduce_prod(init_dims) imgs = tf.reshape( imgs, tf.concat([prod_init_dims[tf.newaxis], end_dims_img], axis=0)) coords = tf.reshape( coords, tf.concat([prod_init_dims[tf.newaxis], end_dims_coords], axis=0)) imgs_sampled = contrib_resampler.resampler(imgs, coords) imgs_sampled = tf.reshape( imgs_sampled, tf.concat([init_dims, tf.shape(imgs_sampled)[-3:]], axis=0)) return imgs_sampled
def train(self, opt): opt.num_source = opt.seq_length - 1 # TODO: currently fixed to 4 opt.num_scales = 4 self.opt = opt self.build_train_graph() self.collect_summaries() with tf.name_scope("parameter_count"): parameter_count = tf.reduce_sum([tf.reduce_prod(tf.shape(v)) \ for v in tf.trainable_variables()]) self.saver = tf.train.Saver([var for var in tf.model_variables()] + \ [self.global_step], max_to_keep=10) sv = tf.train.Supervisor(logdir=opt.checkpoint_dir, save_summaries_secs=0, saver=None) config = tf.ConfigProto() config.gpu_options.allow_growth = True with sv.managed_session(config=config) as sess: print('Trainable variables: ') for var in tf.trainable_variables(): print(var.name) print("parameter_count =", sess.run(parameter_count)) if opt.continue_train: if opt.init_checkpoint_file is None: checkpoint = tf.train.latest_checkpoint(opt.checkpoint_dir) else: checkpoint = opt.init_checkpoint_file print("Resume training from previous checkpoint: %s" % checkpoint) self.saver.restore(sess, checkpoint) start_time = time.time() for step in range(1, opt.max_steps): fetches = { "train": self.train_op, "global_step": self.global_step, "incr_global_step": self.incr_global_step } if step % opt.summary_freq == 0: fetches["loss"] = self.total_loss fetches["summary"] = sv.summary_op results = sess.run(fetches) gs = results["global_step"] if step % opt.summary_freq == 0: sv.summary_writer.add_summary(results["summary"], gs) train_epoch = math.ceil(gs / self.steps_per_epoch) train_step = gs - (train_epoch - 1) * self.steps_per_epoch print("Epoch: [%2d] [%5d/%5d] time: %4.4f/it loss: %.3f" \ % (train_epoch, train_step, self.steps_per_epoch, \ (time.time() - start_time)/opt.summary_freq, results["loss"])) start_time = time.time() if step % opt.save_latest_freq == 0: self.save(sess, opt.checkpoint_dir, 'latest') if step % self.steps_per_epoch == 0: self.save(sess, opt.checkpoint_dir, gs)
def estimate_entropy(entropy_model, inputs, spatial_shape=None) -> EntropyInfo: """Compresses `inputs` with the given entropy model and estimates entropy. Arguments: entropy_model: An `EntropyModel` instance. inputs: The input tensor to be fed to the entropy model. spatial_shape: Shape of the input image (HxW). Must be provided for `valid == False`. Returns: The 'noisy' and quantized inputs, as well as differential and discrete entropy estimates, as an `EntropyInfo` named tuple. """ # We are summing over the log likelihood tensor, so we need to explicitly # divide by the batch size. batch = tf.cast(tf.shape(inputs)[0], tf.float32) # Divide by this to flip sign and convert from nats to bits. quotient = tf.constant(-np.log(2), dtype=tf.float32) num_pixels = tf.cast(tf.reduce_prod(spatial_shape), tf.float32) # Compute noisy outputs and estimate differential entropy. noisy, likelihood = entropy_model(inputs, training=True) log_likelihood = tf.log(likelihood) nbits = tf.reduce_sum(log_likelihood) / (quotient * batch) nbpp = nbits / num_pixels # Compute quantized outputs and estimate discrete entropy. quantized, likelihood = entropy_model(inputs, training=False) log_likelihood = tf.log(likelihood) qbits = tf.reduce_sum(log_likelihood) / (quotient * batch) qbpp = qbits / num_pixels return EntropyInfo(noisy, quantized, nbits, nbpp, qbits, qbpp)
def expand_bit(tensor, axis, activate=True): """Given axis k, expand a tensor x of shape [d0, d1, ..., dk, ..., dn] to y of shape [d0, d1, ..., 2^dk, ..., dn] which satisfies all(reduce_sum(y, axis=k) == 1) is True """ # Sanity check assert isinstance(tensor, (tf.Tensor, tf.Variable)) assert isinstance(axis, int) and axis < len(tensor.shape) shape = get_tensor_shape(tensor) n = shape[axis] # Activate tensor if necessary # TODO: temporally borrow hub.multiple as the multiplier if activate: tensor = tf.sigmoid(tensor * hub.sigmoid_coef) # Expand tensor to shape [d0, ..., dk-1, 1, n, ..., dn] a = tf.expand_dims(tensor, axis=axis) # Get waves of shape (2^n, n) waves = _get_waves(n) # Reshape waves to [ 1, ..., 1, 2^n, n, ..., 1] new_shape = [1] * (len(shape) + 1) new_shape[axis:axis + 2] = (2**n, n) waves = tf.reshape(waves, new_shape) # Calculate bit max coef = tf.subtract(1., tf.multiply(2., a)) y = tf.add(tf.multiply(coef, waves), a) y = tf.reduce_prod(y, axis=axis + 1) return y
def fpn_feature_levels(num_levels, unit_scale_index, image_ratio, boxes): """Returns fpn feature level for each box based on its area. See section 4.2 of https://arxiv.org/pdf/1612.03144.pdf for details. Args: num_levels: An integer indicating the number of feature levels to crop boxes from. unit_scale_index: An 0-based integer indicating the index of feature map which most closely matches the resolution of the pretrained model. image_ratio: A float indicating the ratio of input image area to pretraining image area. boxes: A float tensor of shape [batch, num_boxes, 4] containing boxes of the form [ymin, xmin, ymax, xmax] in normalized coordinates. Returns: An int32 tensor of shape [batch_size, num_boxes] containing feature indices. """ assert num_levels > 0, ( '`num_levels` must be > 0. Found {}'.format(num_levels)) assert unit_scale_index < num_levels and unit_scale_index >= 0, ( '`unit_scale_index` must be in [0, {}). Found {}.'.format( num_levels, unit_scale_index)) box_height_width = boxes[:, :, 2:4] - boxes[:, :, 0:2] areas_sqrt = tf.sqrt(tf.reduce_prod(box_height_width, axis=2)) log_2 = tf.cast(tf.log(2.0), dtype=boxes.dtype) levels = tf.cast( tf.floordiv(tf.log(areas_sqrt * image_ratio), log_2) + unit_scale_index, dtype=tf.int32) levels = tf.maximum(0, tf.minimum(num_levels - 1, levels)) return levels
def flatten_dimensions(inputs, first, last): """Flattens `K-d` tensor along [first, last) dimensions. Converts `inputs` with shape [D0, D1, ..., D(K-1)] into a tensor of shape [D0, D1, ..., D(first) * D(first+1) * ... * D(last-1), D(last), ..., D(K-1)]. Example: `inputs` is a tensor with initial shape [10, 5, 20, 20, 3]. new_tensor = flatten_dimensions(inputs, first=1, last=3) new_tensor.shape -> [10, 100, 20, 3]. Args: inputs: a tensor with shape [D0, D1, ..., D(K-1)]. first: first value for the range of dimensions to flatten. last: last value for the range of dimensions to flatten. Note that the last dimension itself is excluded. Returns: a tensor with shape [D0, D1, ..., D(first) * D(first + 1) * ... * D(last - 1), D(last), ..., D(K-1)]. Raises: ValueError: if first and last arguments are incorrect. """ if first >= inputs.shape.ndims or last > inputs.shape.ndims: raise ValueError( '`first` and `last` must be less than inputs.shape.ndims. ' 'found {} and {} respectively while ndims is {}'.format( first, last, inputs.shape.ndims)) shape = combined_static_and_dynamic_shape(inputs) flattened_dim_prod = tf.reduce_prod(shape[first:last], keepdims=True) new_shape = tf.concat([shape[:first], flattened_dim_prod, shape[last:]], axis=0) return tf.reshape(inputs, new_shape)
def merge_leading_dims(array_or_tensor): """ Merge the first dimensions of a tensor. Returns: Either the input value converted to a Tensor, with the requested dimensions merged, or the unmodified input value if the input has less than `2` dimensions. Raises: ValueError: If the rank of `array_or_tensor` is not well-defined. """ tensor = array_or_tensor #tf.convert_to_tensor(array_or_tensor) tensor_shape_static = tensor.get_shape() # Check if the rank of the input tensor is well-defined. if tensor_shape_static.dims is None: raise ValueError( "Can't merge leading dimensions of tensor of unknown rank.") tensor_shape_list = tensor_shape_static.as_list() if tensor_shape_static.is_fully_defined(): new_shape = ([np.prod(tensor_shape_list[:2])] + tensor_shape_list[2:]) return tf.reshape(tensor, new_shape) # Shape can't be inferred statically. tensor_shape = tf.shape(tensor) new_first_dim = tf.reduce_prod(tensor_shape[:2], keepdims=True) other_dims = tensor_shape[2:] new_size = tf.concat([new_first_dim, other_dims], 0) result = tf.reshape(tensor, new_size) if all(value is not None for value in tensor_shape_list[:2]): merged_leading_size = np.prod(tensor_shape_list[:2]) else: merged_leading_size = None result.set_shape([merged_leading_size] + tensor_shape_list[2:]) return result
def variable_summaries(var, name, collection_key): """Attach a lot of summaries to a Tensor (for TensorBoard visualization). Args: - var: Tensor for variable from which we want to log. - name: Variable name. - collection_key: Collection to save the summary to, can be any key of `VAR_LOG_LEVELS`. """ if collection_key not in VAR_LOG_LEVELS.keys(): raise ValueError('"{}" not in `VAR_LOG_LEVELS`'.format(collection_key)) collections = VAR_LOG_LEVELS[collection_key] with tf.name_scope(name): mean = tf.reduce_mean(var) tf.summary.scalar("mean", mean, collections) num_params = tf.reduce_prod(tf.shape(var)) tf.summary.scalar("num_params", num_params, collections) with tf.name_scope("stddev"): stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) tf.summary.scalar("stddev", stddev, collections) tf.summary.scalar("max", tf.reduce_max(var), collections) tf.summary.scalar("min", tf.reduce_min(var), collections) tf.summary.histogram("histogram", var, collections) tf.summary.scalar("sparsity", tf.nn.zero_fraction(var), collections)
def MS_SSIM2(img1, img2, radius=5, sigma=[0.5, 1, 2, 4, 8], L=1, norm=True, data_format=None, one_dim=False, scope=None): if data_format is None: data_format = DATA_FORMAT with tf.variable_scope(scope, 'MS_SSIM2'): levels = len(sigma) mssim = [] mcs = [] for _ in range(levels): ssim, cs = SS_SSIM(img1, img2, ret_cs=True, mean_metric=False, radius=radius, sigma=sigma[_], L=L, data_format=data_format, one_dim=one_dim) mssim.append(tf.nn.relu(ssim)) # avoiding negative value mcs.append(tf.nn.relu(cs)) # avoiding negative value # list to tensor of dim D+1 mcs = tf.stack(mcs, axis=0) value = tf.reduce_prod(mcs[0:levels - 1], axis=0) * mssim[levels - 1] value = tf.reduce_mean(value) if norm: value **= 1.0 / levels return value
def __init__(self, M, L, N, name, load_weights, trainable, type, number): # Initial buffer self.buffer = list() self.name = name self.learning_rate = 10e-3 self.number = str(number) self.type = type # Build up models self.sesson = tf.Session() # Initial input shape self.M = M self.L = L self.N = N self.global_step = tf.Variable(0, trainable=False) self.state, self.w_previous, self.out = self.build_net() self.future_price = tf.placeholder(tf.float32, [None] + [self.M]) self.pv_vector = tf.reduce_sum(self.out * self.future_price, reduction_indices=[1]) * self.pc() self.profit = tf.reduce_prod(self.pv_vector) self.loss = -tf.reduce_mean(tf.log(self.pv_vector)) self.optimize = tf.train.AdamOptimizer(self.learning_rate).minimize( self.loss, global_step=self.global_step) # Initial saver self.saver = tf.train.Saver(max_to_keep=10) if load_weights == 'True': print("Loading Model") try: checkpoint = tf.train.get_checkpoint_state('./result/PG/' + self.number + '/' + 'saved_network/' + type + '/') print('./saved_network/PG/' + type + '/') if checkpoint and checkpoint.model_checkpoint_path: tf.reset_default_graph() self.saver.restore(self.sesson, checkpoint.model_checkpoint_path) print("Successfully loaded:", checkpoint.model_checkpoint_path) else: print("Could not find old network weights") self.sesson.run(tf.global_variables_initializer()) except: print("Could not find old network weights") self.sesson.run(tf.global_variables_initializer()) else: self.sesson.run(tf.global_variables_initializer()) if trainable == 'True': # Initial summary self.summary_writer = tf.summary.FileWriter( './result/PG/' + self.number + '/' + 'summary/' + type + '/', self.sesson.graph) self.summary_ops, self.summary_vars = self.build_summaries()
def clip_eta(eta, ord, eps): """ Helper function to clip the perturbation to epsilon norm ball. :param eta: A tensor with the current perturbation. :param ord: Order of the norm (mimics Numpy). Possible values: np.inf, 1 or 2. :param eps: Epsilon, bound of the perturbation. """ # Clipping perturbation eta to self.ord norm ball if ord not in [np.inf, 1, 2]: raise ValueError('ord must be np.inf, 1, or 2.') reduc_ind = list(xrange(1, len(eta.get_shape()))) avoid_zero_div = 1e-12 if ord == np.inf: eta = clip_by_value(eta, -eps, eps) elif ord == 1: # Implements a projection algorithm onto the l1-ball from # (Duchi et al. 2008) that runs in time O(d*log(d)) where d is the # input dimension. # Paper link (Duchi et al. 2008): https://dl.acm.org/citation.cfm?id=1390191 eps = tf.cast(eps, eta.dtype) dim = tf.reduce_prod(tf.shape(eta)[1:]) eta_flat = tf.reshape(eta, (-1, dim)) abs_eta = tf.abs(eta_flat) if 'sort' in dir(tf): mu = -tf.sort(-abs_eta, axis=-1) else: # `tf.sort` is only available in TF 1.13 onwards mu = tf.nn.top_k(abs_eta, k=dim, sorted=True)[0] cumsums = tf.cumsum(mu, axis=-1) js = tf.cast(tf.divide(1, tf.range(1, dim + 1)), eta.dtype) t = tf.cast(tf.greater(mu - js * (cumsums - eps), 0), eta.dtype) rho = tf.argmax(t * cumsums, axis=-1) rho_val = tf.reduce_max(t * cumsums, axis=-1) theta = tf.divide(rho_val - eps, tf.cast(1 + rho, eta.dtype)) eta_sgn = tf.sign(eta_flat) eta_proj = eta_sgn * tf.maximum(abs_eta - theta[:, tf.newaxis], 0) eta_proj = tf.reshape(eta_proj, tf.shape(eta)) norm = tf.reduce_sum(tf.abs(eta), reduc_ind) eta = tf.where(tf.greater(norm, eps), eta_proj, eta) elif ord == 2: # avoid_zero_div must go inside sqrt to avoid a divide by zero # in the gradient through this operation norm = tf.sqrt( tf.maximum(avoid_zero_div, reduce_sum(tf.square(eta), reduc_ind, keepdims=True))) # We must *clip* to within the norm ball, not *normalize* onto the # surface of the ball factor = tf.minimum(1., div(eps, norm)) eta = eta * factor return eta
def reduce_logavgexp(input_tensor, axis=None, keepdims=None, name=None): dims = tf.shape(input_tensor) if axis is not None: dims = tf.gather(dims, axis) denominator = tf.reduce_prod(dims) return (tf.reduce_logsumexp( input_tensor, axis=axis, keepdims=keepdims, name=name) - tf.log(tf.to_float(denominator)))
def build_model(x, lmbda, mode='training', layers=None, msssim_loss=False): """Builds the compression model.""" is_training = (mode == 'training') num_pixels = tf.to_float(tf.reduce_prod(tf.shape(x)[:-1])) if layers is None: num_filters = 192 analysis_transform = AnalysisTransform(num_filters) synthesis_transform = SynthesisTransform(num_filters) hyper_analysis_transform = HyperAnalysisTransform(num_filters) hyper_synthesis_transform = HyperSynthesisTransform(num_filters) entropy_bottleneck = tfc.EntropyBottleneck() layers = (analysis_transform, hyper_analysis_transform, entropy_bottleneck, hyper_synthesis_transform, synthesis_transform) else: analysis_transform, hyper_analysis_transform, entropy_bottleneck, \ hyper_synthesis_transform, synthesis_transform = layers y = analysis_transform(x) z = hyper_analysis_transform(y) z_tilde_hat, z_likelihoods = entropy_bottleneck(z, training=is_training) mean, sigma = hyper_synthesis_transform(z_tilde_hat) scale_table = np.exp(np.linspace( np.log(SCALES_MIN), np.log(SCALES_MAX), SCALES_LEVELS)) conditional_bottleneck = tfc.GaussianConditional(sigma, scale_table, mean=mean) y_tilde_hat, y_likelihoods = conditional_bottleneck(y, training=is_training) x_tilde_hat = synthesis_transform(y_tilde_hat) if mode == "testing": side_string = entropy_bottleneck.compress(z_tilde_hat) string = conditional_bottleneck.compress(y_tilde_hat) else: string = None side_string = None bpp = (tf.reduce_sum(tf.log(y_likelihoods)) + tf.reduce_sum(tf.log(z_likelihoods))) / (-np.log(2) * num_pixels) mse = tf.reduce_mean(tf.squared_difference(x, x_tilde_hat)) mse *= 255 ** 2 msssim = tf.reduce_mean(1 - tf.image.ssim_multiscale(x_tilde_hat, x, 1)) distortion = msssim if msssim_loss else mse loss = lmbda * distortion + bpp return loss, bpp, mse, msssim, x_tilde_hat, y_tilde_hat, z_tilde_hat, \ y, z, string, side_string, layers
def generate_heatmap_target_sigmas_rotation(heatmap_size, landmarks, sigmas, rotation, scale=1.0, normalize=False, data_format='channels_first'): """ Generates heatmap images for the given parameters. :param heatmap_size: The image size of a single heatmap. :param landmarks: The list of landmarks. For each landmark, a heatmap on the given coordinate will be generated. If landmark.is_valid is False, then the heatmap will be empty. :param sigmas: The sigmas for the individual heatmaps. May be either fixed, or trainable. :param rotation: The rotation of the heatmap. May be either fixed, or trainable. :param scale: The scale factor for each heatmap. Each pixel value will be multiplied by this value. :param normalize: If true, each heatmap value will be multiplied by the normalization factor of the gaussian. :param data_format: The data format of the resulting tensor of heatmap images. :return: The tensor of heatmap images. """ landmarks_shape = landmarks.get_shape().as_list() sigmas_shape = sigmas.get_shape().as_list() batch_size = landmarks_shape[0] num_landmarks = landmarks_shape[1] dim = landmarks_shape[2] - 1 assert dim == 2, 'Currently only dim == 2 is supported.' assert len(heatmap_size) == dim, 'Dimensions do not match.' assert sigmas_shape[0] == num_landmarks, 'Number of sigmas does not match.' rotation_matrix = tf.stack([tf.stack([tf.cos(rotation), -tf.sin(rotation)], axis=-1), tf.stack([tf.sin(rotation), tf.cos(rotation)], axis=-1)], axis=-1) rotation_matrix_t = tf.stack([tf.stack([tf.cos(rotation), tf.sin(rotation)], axis=-1), tf.stack([-tf.sin(rotation), tf.cos(rotation)], axis=-1)], axis=-1) det_covariances = tf.reduce_prod(sigmas, axis=-1) sigmas_inv_eye = tf.eye(dim, dim, batch_shape=[num_landmarks]) * tf.expand_dims(1.0 / sigmas, -1) inv_covariances = tf.matmul(tf.matmul(rotation_matrix, sigmas_inv_eye), rotation_matrix_t) if data_format == 'channels_first': heatmap_axis = 1 landmarks_reshaped = tf.reshape(landmarks[..., 1:], [batch_size, num_landmarks] + [1] * dim + [dim]) is_valid_reshaped = tf.reshape(landmarks[..., 0], [batch_size, num_landmarks] + [1] * dim) det_covariances_reshaped = tf.reshape(det_covariances, [1, num_landmarks] + [1] * dim) inv_covariances_reshaped = tf.reshape(inv_covariances, [1, num_landmarks] + [1] * dim + [dim, dim]) else: heatmap_axis = dim + 1 landmarks_reshaped = tf.reshape(landmarks[..., 1:], [batch_size] + [1] * dim + [num_landmarks, dim]) is_valid_reshaped = tf.reshape(landmarks[..., 0], [batch_size] + [1] * dim + [num_landmarks]) det_covariances_reshaped = tf.reshape(det_covariances, [1] + [1] * dim + [num_landmarks]) inv_covariances_reshaped = tf.reshape(inv_covariances, [1] + [1] * dim + [num_landmarks, dim, dim]) aranges = [np.arange(s) for s in heatmap_size] grid = tf.meshgrid(*aranges, indexing='ij') grid_stacked = tf.stack(grid, axis=dim) grid_stacked = tf.cast(grid_stacked, tf.float32) grid_stacked = tf.stack([grid_stacked] * batch_size, axis=0) grid_stacked = tf.stack([grid_stacked] * num_landmarks, axis=heatmap_axis) if normalize: scale /= tf.sqrt(tf.pow(2 * np.pi, dim) * det_covariances_reshaped) x_minus_mu = grid_stacked - landmarks_reshaped exp_factor = tf.reduce_sum(tf.reduce_sum(tf.expand_dims(x_minus_mu, -1) * inv_covariances_reshaped * tf.expand_dims(x_minus_mu, -2), axis=-1), axis=-1) heatmap = scale * tf.exp(-0.5 * exp_factor) heatmap_or_zeros = tf.where((is_valid_reshaped + tf.zeros_like(heatmap)) > 0, heatmap, tf.zeros_like(heatmap)) return heatmap_or_zeros
def _conv2d_expression(expr, w, padding, strides): """Scale a linear expression by w (through a convolutional layer).""" b = tf.nn.convolution(expr.b, w, padding=padding, strides=strides) shape = tf.concat([[tf.reduce_prod(tf.shape(expr.w)[:2])], tf.shape(expr.w)[2:]], axis=0) w = tf.nn.convolution(tf.reshape(expr.w, shape), w, padding=padding, strides=strides) shape = tf.concat([tf.shape(expr.w)[:2], tf.shape(w)[1:]], axis=0) w = tf.reshape(w, shape) return LinearExpression(w=w, b=b, lower=expr.lower, upper=expr.upper)
def load_test_model_graph(checkpoint_dir): ''' model used in test mode. (entropy_bootleneck(training=False) ''' # inputs x = tf.placeholder(tf.float32, [1, None, None, 3]) orig_x = tf.placeholder(tf.float32, [1, None, None, 3]) # Instantiate model. analysis_transform = AnalysisTransform(192) synthesis_transform = SynthesisTransform(192) hyper_analysis_transform = HyperAnalysisTransform(192) hyper_synthesis_transform = HyperSynthesisTransform(192) entropy_bottleneck = tfc.EntropyBottleneck() # Transform and compress the image. y = analysis_transform(x) y_shape = tf.shape(y) z = hyper_analysis_transform(abs(y)) z_hat, z_likelihoods = entropy_bottleneck(z, training=False) sigma = hyper_synthesis_transform(z_hat) sigma = sigma[:, :y_shape[1], :y_shape[2], :] scale_table = np.exp( np.linspace(np.log(SCALES_MIN), np.log(SCALES_MAX), SCALES_LEVELS)) conditional_bottleneck = tfc.GaussianConditional(sigma, scale_table) side_string = entropy_bottleneck.compress(z) string = conditional_bottleneck.compress(y) # Transform the quantized image back (if requested). y_hat, y_likelihoods = conditional_bottleneck(y, training=False) x_hat = synthesis_transform(y_hat) # eval bpp num_pixels = tf.cast(tf.reduce_prod(tf.shape(x)[:-1]), dtype=tf.float32) eval_bpp = (tf.reduce_sum(tf.log(y_likelihoods)) + tf.reduce_sum( tf.log(z_likelihoods))) / (-np.log(2) * num_pixels) # reconstruction metric # Bring both images back to 0..255 range. orig_x_255 = orig_x * 255 x_hat = tf.clip_by_value(x_hat, 0, 1) x_hat = tf.round(x_hat * 255) mse = tf.reduce_mean(tf.squared_difference(orig_x_255, x_hat)) psnr = tf.squeeze(tf.image.psnr(x_hat, orig_x_255, 255)) msssim = tf.squeeze(tf.image.ssim_multiscale(x_hat, orig_x_255, 255)) # session sess = tf.Session() # load graph latest = tf.train.latest_checkpoint(checkpoint_dir=checkpoint_dir) tf.train.Saver().restore(sess, save_path=latest) return sess, x, orig_x, [ string, side_string ], eval_bpp, x_hat, mse, psnr, msssim, num_pixels, y, z
def get_probability_of_having_no_connections(self, task_id): """Gets the probability that a given task will have zero active connections. Args: task_id: (int scalar) id of the task. Returns: A float scalar in the [0.0, 1.0] range equal to the requested probability. """ probs = self.get_connection_probs() return tf.reduce_prod(1.0 - probs[task_id])
def bit_max(x, num_classes, heads=1, sum_heads=False, **kwargs): """Bit max :param x: a tensor of shape (batch_size, dim) :param num_classes: output dimension :param heads: heads # :param sum_heads: whether to sum up along head dimension before outputting :return: a tensor y with the same shape as x, sum(y[k, :]) == 1 for all k """ # Sanity check assert isinstance(num_classes, int) and num_classes > 1 assert isinstance(heads, int) and heads > 0 # Get bits num_bits = int(np.ceil(np.log2(num_classes))) # Calculate activations for bits # a.shape = (bs, num_bits*heads) a = neurons(num_bits * heads, x, activation='sigmoid', scope='bit_activation', **kwargs) if heads > 1: # a.shape => (heads, bs, num_bits) a = tf.reshape(a, [-1, num_bits, heads]) a = tf.transpose(a, [2, 0, 1]) # a.shape => ([heads, ]bs, 2**num_bits, num_bits) a = tf.stack([a] * (2**num_bits), axis=-2) # Calculate bit_max # waves.shape = (1, 2**num_bits, num_bits) waves = _get_waves(num_bits) coef = tf.subtract(1., tf.multiply(2., a)) wave_stack = tf.add(tf.multiply(waves, coef), a) # bit_max.shape = ([heads, ]bs, 2**num_bits) bit_max = tf.reduce_prod(wave_stack, axis=-1) # Trim if necessary scale = 2**num_bits if scale > num_classes: delta = (scale - num_classes) // 2 # Trim if heads == 1: bit_max = bit_max[:, delta:num_classes + delta] else: bit_max = bit_max[:, :, delta:num_classes + delta] # Normalize sums = tf.reduce_sum(bit_max, axis=-1, keepdims=True) + 1e-6 bit_max = tf.divide(bit_max, sums) # Add up if necessary if heads > 1 and sum_heads: bit_max = tf.reduce_sum(bit_max, axis=0) # output shape is ([heads, ]bs, num_classes) return bit_max
def _get_moments(self, inputs): # Like tf.nn.moments but unbiased sample std. deviation. # Reduce over channels only. mean = tf.reduce_mean(inputs, [self.axis], keepdims=True, name="mean") variance = tf.reduce_sum( tf.squared_difference(inputs, tf.stop_gradient(mean)), [self.axis], keepdims=True, name="variance_sum") # Divide by N-1 inputs_shape = tf.shape(inputs) counts = tf.reduce_prod([inputs_shape[ax] for ax in [self.axis]]) variance /= (tf.cast(counts, tf.float32) - 1) return mean, variance
def pixel_control_rewards(observations, cell_size): """Calculates pixel control task rewards from observation sequence. The observations are first split in a grid of KxK cells. For each cell a distinct pseudo reward is computed as the average absolute change in pixel intensity for all pixels in the cell. The change in intensity is averaged across both pixels and channels (e.g. RGB). The `observations` provided to this function should be cropped suitably, to ensure that the observations' height and width are a multiple of `cell_size`. The values of the `observations` tensor should be rescaled to [0, 1]. In the UNREAL agent observations are cropped to 80x80, and each cell is 4x4 in size. See "Reinforcement Learning with Unsupervised Auxiliary Tasks" by Jaderberg, Mnih, Czarnecki et al. (https://arxiv.org/abs/1611.05397). Args: observations: A tensor of shape `[T+1,B,H,W,C...]`, where * `T` is the sequence length, `B` is the batch size. * `H` is height, `W` is width. * `C...` is at least one channel dimension (e.g., colour, stack). * `T` and `B` can be statically unknown. cell_size: The size of each cell. Returns: A tensor of pixel control rewards calculated from the observation. The shape is `[T,B,H',W']`, where `H'` and `W'` are determined by the `cell_size`. If evenly-divisible, `H' = H/cell_size`, and similar for `W`. """ # Calculate the absolute differences across the sequence. abs_diff = tf.abs(observations[1:] - observations[:-1]) # Average over cells. `abs_diff` has shape [T,B,H,W,C...], e.g., # [T,B,H,W,C] if we have a colour channel. We want to use the TF avg_pool3d # op, but it expects 5D inputs so we collapse all channel dimensions. # Merge remaining dimensions after W: [T,B,H,W,C']. full_shape = tf.shape(abs_diff) preserved_shape = full_shape[:4] trailing_shape = (tf.reduce_prod(full_shape[4:]),) shape = tf.concat([preserved_shape, trailing_shape], 0) abs_diff = tf.reshape(abs_diff, shape) # Apply the averaging using average pooling and reducing over channel. avg_abs_diff = tf.nn.avg_pool3d( abs_diff, ksize=[1, 1, cell_size, cell_size, 1], strides=[1, 1, cell_size, cell_size, 1], padding="VALID") # [T,B,H',W',C']. pseudo_rewards = tf.reduce_mean( avg_abs_diff, axis=[4], name="pseudo_rewards") # [T,B,H',W']. sequence_batch = abs_diff.get_shape()[:2] new_height_width = avg_abs_diff.get_shape()[2:4] pseudo_rewards.set_shape(sequence_batch.concatenate(new_height_width)) return pseudo_rewards
def kernel_num_elements(tensor): """Returns the number of elements of a kernel. Args: tensor: The weight tensor. Returns: Number of elements of the kernel (either float or tf.float). """ num_elements = np.prod(tensor.shape.dims[1:-1]).value if num_elements: return num_elements return tf.to_float(tf.reduce_prod(tf.shape(tensor)[1:-1]))
def EBKL(p, q, hypers=None, global_step=1.0E99): if isinstance(p, DiagonalGaussianVar): if isinstance(q, InverseGammaVar): m = tf.to_float(tf.reduce_prod(tf.shape(p.mean))) S = tf.reduce_sum(p.var + p.mean*p.mean) m_plus_2alpha_plus_2 = m + 2.0*q.alpha + 2.0 S_plus_2beta = S + 2.0*q.beta tmp = m * tf.log(S_plus_2beta / m_plus_2alpha_plus_2) tmp += S * (m_plus_2alpha_plus_2 / S_plus_2beta) tmp += -(m + tf.reduce_sum(tf.log(p.var))) return 0.5 * tmp print('unsupported KL')
def condition(self, tensor_list): """Computes the p parameter of the Bernoulli distribution.""" # Remove None's from tensor_list tensor_list = [t for t in tensor_list if t is not None] concatted_inputs = tf.concat(tensor_list, axis=-1) input_dim = concatted_inputs.get_shape().as_list()[-1] raw_input_shape = tf.shape(concatted_inputs)[:-1] fcnet_input_shape = [tf.reduce_prod(raw_input_shape), input_dim] fcnet_inputs = tf.reshape(concatted_inputs, fcnet_input_shape) outs = self.fcnet(fcnet_inputs) + self.bias_init # Reshape outputs to the original shape. output_size = tf.concat([raw_input_shape, [self.size]], axis=0) return tf.reshape(outs, output_size)
def __call__(self, x): shape = tf.shape(x) last_shape = shape[-1] #assert self.input_dim == last_shape prefix_shape = shape[:-1] prefix_prod = tf.reduce_prod(prefix_shape) x = tf.reshape(x, [prefix_prod, last_shape]) x = self.first_layer(x) for layer in self.inner_layers: x = layer(x) x = self.activation(x) out = self.final_layer(x) out_shape = tf.concat([prefix_shape, [self.output_dim]], axis=0) return tf.reshape(out, out_shape)