def output(self,x): if(self.no_bias): return output_no_bias(self,x) if(self.activation == 'sigmoid'): return tf.nn.sigmoid(tf.matmul(x,self.W+self.b)) elif(self.activation == 'relu'): return tf.nn.relu(tf.matmul(x,self.W+self.b)) elif(self.activation == 'relu6'): return tf.nn.relu6(tf.matmul(x,self.W+self.b)) elif(self.activation == 'leaky_relu'): return tf.maximum(0.1*tf.matmul(x,self.W+self.b),tf.matmul(x,self.W+self.b)) elif(self.activation == 'leaky_relu6'): return tf.maximum(0.1*tf.matmul(x,self.W+self.b),6) elif(self.activation == 'linear'): return tf.matmul(x,self.W)+self.b elif(self.activation == 'softplus'): return tf.nn.softplus(tf.matmul(x,self.W+self.b)) elif(self.activation == 'tanh'): return tf.tanh(tf.matmul(x,self.W+self.b)) else: print "No known activation function selected, using linear" return tf.matmul(x,self.W)+self.b
def atrous_residual(self, outer_dim, inner_dim, name=None, inp_layer=None, rates=[1,4]): name = name or self.get_unique_name_('residual') if inp_layer is None: inp_layer = self.get_output() with tf.variable_scope(name): if inp_layer.get_shape().as_list()[-1] != outer_dim: input = layers.conv2d(inp_layer, 3, outer_dim, name+'_rch_conv', func=None) input = layers.batch_norm(input, self.phase_train, name=name+'_bn') input = tf.maximum(.01*input, input) else: input = inp_layer with tf.variable_scope('atrous_conv'): atrous_layers = [] for rate in rates: atrous_layers.append(layers.atrous_conv2d(input, 5, outer_dim // len(rates), 'aconv_'+str(rate), rate=rate, func=None)) conv = tf.concat(3, atrous_layers) bn = layers.batch_norm(conv, self.phase_train, name='bn1') leaky_relu = tf.maximum(.01*bn, bn) conv2 = layers.conv2d(leaky_relu, 3, outer_dim, 'conv_outer', func=None) #bn2 = layers.batch_norm(conv, self.phase_train, name='bn2') res = conv2 + input bn2 = layers.batch_norm(res, self.phase_train, name='bn2') out = tf.maximum(.01*bn2, bn2) self.add_(name, out) return self
def compute_IOU(bboxA, bboxB): """Compute the Intersection Over Union. Args: bboxA: [N X 4 tensor] format = [left, top, right, bottom] bboxB: [N X 4 tensor] Return: IOU: [N X 1 tensor] """ x1A, y1A, x2A, y2A = tf.split(1, 4, bboxA) x1B, y1B, x2B, y2B = tf.split(1, 4, bboxB) # compute intersection x1_max = tf.maximum(x1A, x1B) y1_max = tf.maximum(y1A, y1B) x2_min = tf.minimum(x2A, x2B) y2_min = tf.minimum(y2A, y2B) # overlap_flag = tf.logical_and( tf.less(x1_max, x2_min), tf.less(y1_max, y2_min)) overlap_flag = tf.to_float(tf.less(x1_max, x2_min)) * \ tf.to_float(tf.less(y1_max, y2_min)) overlap_area = tf.mul(overlap_flag, tf.mul( x2_min - x1_max, y2_min - y1_max)) # compute union areaA = tf.mul(x2A - x1A, y2A - y1A) areaB = tf.mul(x2B - x1B, y2B - y1B) union_area = areaA + areaB - overlap_area return tf.div(overlap_area, union_area)
def adv_net_loss(input, model, labels, target, adv_output_layer, confidence, c): # calculate l2 distance between ori_input and adversarial examples adv_output = model.get_layer(input, adv_output_layer) dif = tf.subtract(adv_output, input) # reshape_dif = tf.reshape(dif, shape=(dif.get_shape()[0],-1)) # l2_dis_loss = tf.norm(reshape_dif, axis=1) l2_dis_loss = tf.square(dif) l2_dis_loss = tf.reduce_mean(l2_dis_loss, name='l2_dis_loss') tf.add_to_collection('losses', l2_dis_loss) # attack target loss logits = model(input) one_hot_labels = tf.one_hot(labels,10) real = tf.reduce_sum(one_hot_labels*logits, 1) other_max = tf.reduce_max((1-one_hot_labels)*logits-one_hot_labels*10000, 1) if target: attack_loss = tf.maximum(0.0, other_max - real + confidence) else: attack_loss = tf.maximum(0.0, real - other_max + confidence) attack_loss = tf.reduce_mean(attack_loss, name='attack_loss') tf.add_to_collection('losses', attack_loss) # total loss total_loss = l2_dis_loss*c + attack_loss*0 return total_loss
def output_dropout_no_bias(self,x,keep_prob=0.5): if(self.activation == 'sigmoid'): return tf.nn.dropout(tf.nn.sigmoid(tf.matmul(x,self.W)), keep_prob) elif(self.activation == 'relu'): return tf.nn.dropout(tf.nn.relu(tf.matmul(x,self.W)), keep_prob) elif(self.activation == 'relu6'): return tf.nn.dropout(tf.nn.relu6(tf.matmul(x,self.W)), keep_prob) elif(self.activation == 'leaky_relu'): return tf.nn.dropout(tf.maximum(0.1*tf.matmul(x,self.W),tf.matmul(x,self.W)),keep_prob) elif(self.activation == 'leaky_relu6'): return tf.nn.dropout(tf.maximum(0.1*tf.matmul(x,self.W),6),keep_prob) elif(self.activation == 'linear'): return tf.nn.dropout(tf.matmul(x,self.W),keep_prob) elif(self.activation == 'softplus'): return tf.nn.dropout(tf.nn.softplus(tf.matmul(x,self.W)),keep_prob) elif(self.activation == 'tanh'): return tf.nn.dropout(tf.tanh(tf.matmul(x,self.W)),keep_prob) else: print "No known activation function selected, using linear" return tf.matmul(x,self.W)
def bboxes_jaccard(bbox_ref, bboxes, name=None): """Compute jaccard score between a reference box and a collection of bounding boxes. Args: bbox_ref: (N, 4) or (4,) Tensor with reference bounding box(es). bboxes: (N, 4) Tensor, collection of bounding boxes. Return: (N,) Tensor with Jaccard scores. """ with tf.name_scope(name, 'bboxes_jaccard'): # Should be more efficient to first transpose. bboxes = tf.transpose(bboxes) bbox_ref = tf.transpose(bbox_ref) # Intersection bbox and volume. int_ymin = tf.maximum(bboxes[0], bbox_ref[0]) int_xmin = tf.maximum(bboxes[1], bbox_ref[1]) int_ymax = tf.minimum(bboxes[2], bbox_ref[2]) int_xmax = tf.minimum(bboxes[3], bbox_ref[3]) h = tf.maximum(int_ymax - int_ymin, 0.) w = tf.maximum(int_xmax - int_xmin, 0.) # Volumes. inter_vol = h * w union_vol = -inter_vol \ + (bboxes[2] - bboxes[0]) * (bboxes[3] - bboxes[1]) \ + (bbox_ref[2] - bbox_ref[0]) * (bbox_ref[3] - bbox_ref[1]) jaccard = tfe_math.safe_divide(inter_vol, union_vol, 'jaccard') return jaccard
def bboxes_intersection(bbox_ref, bboxes, name=None): """Compute relative intersection between a reference box and a collection of bounding boxes. Namely, compute the quotient between intersection area and box area. Args: bbox_ref: (N, 4) or (4,) Tensor with reference bounding box(es). bboxes: (N, 4) Tensor, collection of bounding boxes. Return: (N,) Tensor with relative intersection. """ with tf.name_scope(name, 'bboxes_intersection'): # Should be more efficient to first transpose. bboxes = tf.transpose(bboxes) bbox_ref = tf.transpose(bbox_ref) # Intersection bbox and volume. int_ymin = tf.maximum(bboxes[0], bbox_ref[0]) int_xmin = tf.maximum(bboxes[1], bbox_ref[1]) int_ymax = tf.minimum(bboxes[2], bbox_ref[2]) int_xmax = tf.minimum(bboxes[3], bbox_ref[3]) h = tf.maximum(int_ymax - int_ymin, 0.) w = tf.maximum(int_xmax - int_xmin, 0.) # Volumes. inter_vol = h * w bboxes_vol = (bboxes[2] - bboxes[0]) * (bboxes[3] - bboxes[1]) scores = tfe_math.safe_divide(inter_vol, bboxes_vol, 'intersection') return scores
def batch_iou(bboxes, bbox): """Compute iou of a batch of boxes with another box. Box format '[y_min, x_min, y_max, x_max]'. Args: bboxes: A batch of boxes. 2-D with shape `[B, 4]`. bbox: A single box. 1-D with shape `[4]`. Returns: Batch of IOUs """ lr = tf.maximum( tf.minimum(bboxes[:, 3], bbox[3]) - tf.maximum(bboxes[:, 1], bbox[1]), 0 ) tb = tf.maximum( tf.minimum(bboxes[:, 2], bbox[2]) - tf.maximum(bboxes[:, 0], bbox[0]), 0 ) intersection = tf.multiply(tb, lr) union = tf.subtract( tf.multiply((bboxes[:, 3] - bboxes[:, 1]), (bboxes[:, 2] - bboxes[:, 0])) + tf.multiply((bbox[3] - bbox[1]), (bbox[2] - bbox[0])), intersection ) iou = tf.div(intersection, union) return iou
def bboxes_clip(bbox_ref, bboxes, scope=None): """Clip bounding boxes to a reference box. Batch-compatible if the first dimension of `bbox_ref` and `bboxes` can be broadcasted. Args: bbox_ref: Reference bounding box. Nx4 or 4 shaped-Tensor; bboxes: Bounding boxes to clip. Nx4 or 4 shaped-Tensor or dictionary. Return: Clipped bboxes. """ # Bboxes is dictionary. if isinstance(bboxes, dict): with tf.name_scope(scope, 'bboxes_clip_dict'): d_bboxes = {} for c in bboxes.keys(): d_bboxes[c] = bboxes_clip(bbox_ref, bboxes[c]) return d_bboxes # Tensors inputs. with tf.name_scope(scope, 'bboxes_clip'): # Easier with transposed bboxes. Especially for broadcasting. bbox_ref = tf.transpose(bbox_ref) bboxes = tf.transpose(bboxes) # Intersection bboxes and reference bbox. ymin = tf.maximum(bboxes[0], bbox_ref[0]) xmin = tf.maximum(bboxes[1], bbox_ref[1]) ymax = tf.minimum(bboxes[2], bbox_ref[2]) xmax = tf.minimum(bboxes[3], bbox_ref[3]) # Double check! Empty boxes when no-intersection. ymin = tf.minimum(ymin, ymax) xmin = tf.minimum(xmin, xmax) bboxes = tf.transpose(tf.stack([ymin, xmin, ymax, xmax], axis=0)) return bboxes
def discriminator(images, reuse=False): """ Create the discriminator network :param image: Tensor of input image(s) :param reuse: Boolean if the weights should be reused :return: Tuple of (tensor output of the discriminator, tensor logits of the discriminator) """ # TODO: Implement Function with tf.variable_scope("discriminator",reuse=reuse): x1 = tf.layers.conv2d(images, 64, 5, strides=2, kernel_initializer=tf.contrib.layers.xavier_initializer(),padding='same') relu1 = tf.maximum(0.1 * x1, x1) #14*14*64 x2 = tf.layers.conv2d(relu1, 128, 5, strides=2, kernel_initializer=tf.contrib.layers.xavier_initializer(),padding='same') bn2 = tf.layers.batch_normalization(x2, training=True) relu2 = tf.maximum(0.1 * bn2, bn2) #7*7*128 #4*4*256 # Flatten it flat = tf.reshape(relu2, (-1, 7*7*128)) #flat = tf.nn.dropout(flat,0.8) logits = tf.layers.dense(flat, 1) out = tf.sigmoid(logits) return out, logits
def _conv(self, input, shape, strides, name, alpha=0.1): """ args: shape : [3, 3, in, out] """ if self.bn_mode: with tf.variable_scope(name) as scope: kernel = self._variable_trunc_normal('weights', shape) conv = tf.nn.conv2d(input, kernel, strides, padding='SAME') bn_conv = self._batch_normalization(conv, shape[-1], [0, 1, 2]) conv_ = tf.maximum(bn_conv, alpha*bn_conv, name=scope.name) if tf.get_variable_scope().reuse is False: self._add_weight_decay(kernel) self._activation_summary(conv_) else: with tf.variable_scope(name) as scope: kernel = self._variable_trunc_normal('weights', shape) conv = tf.nn.conv2d(input,kernel,strides, padding='SAME') biases = self._variable_constant('biases', shape[-1], value=0.01) bias = tf.nn.bias_add(conv, biases) conv_ = tf.maximum(bias, alpha*bias, name=scope.name) if tf.get_variable_scope().reuse is False: self._add_weight_decay(kernel) self._activation_summary(conv_) return conv_
def margin_loss(y, preds): y = tf.cast(y,tf.float32) loss = y * tf.square(tf.maximum(0., 0.9 - preds)) + \ 0.25 * (1.0 - y) * tf.square(tf.maximum(0., preds - 0.1)) loss = tf.reduce_mean(tf.reduce_sum(loss, axis=1)) # loss = tf.reduce_mean(loss) return loss
def loss(self): # 1. The margin loss # [batch_size, 10, 1, 1] # max_l = max(0, m_plus-||v_c||)^2 max_l = tf.square(tf.maximum(0., cfg.m_plus - self.v_length)) # max_r = max(0, ||v_c||-m_minus)^2 max_r = tf.square(tf.maximum(0., self.v_length - cfg.m_minus)) assert max_l.get_shape() == [cfg.batch_size, 10, 1, 1] # reshape: [batch_size, 10, 1, 1] => [batch_size, 10] max_l = tf.reshape(max_l, shape=(cfg.batch_size, -1)) max_r = tf.reshape(max_r, shape=(cfg.batch_size, -1)) # calc T_c: [batch_size, 10] # T_c = Y, is my understanding correct? Try it. T_c = self.Y # [batch_size, 10], element-wise multiply L_c = T_c * max_l + cfg.lambda_val * (1 - T_c) * max_r self.margin_loss = tf.reduce_mean(tf.reduce_sum(L_c, axis=1)) # 2. The reconstruction loss orgin = tf.reshape(self.X, shape=(cfg.batch_size, -1)) squared = tf.square(self.decoded - orgin) self.reconstruction_err = tf.reduce_mean(squared) # 3. Total loss # The paper uses sum of squared error as reconstruction error, but we # have used reduce_mean in `# 2 The reconstruction loss` to calculate # mean squared error. In order to keep in line with the paper,the # regularization scale should be 0.0005*784=0.392 self.total_loss = self.margin_loss + cfg.regularization_scale * self.reconstruction_err
def cosine_distance(v1, v2): """ Calculate the cosine distance between the representations of the words of the two sentences. Parameters ---------- v1: Tensor Tensor of shape (batch_size, 1, num_sentence_words, context_rnn_hidden_size) representing the first sentence to take the cosine similarity with. v2: Tensor Tensor of shape (batch_size, num_sentence_words, 1, context_rnn_hidden_size) representing the second sentence to take the cosine similarity with. """ # The product of the two vectors is shape # (batch_size, num_sentence_words, num_sentence_words, rnn_hidden_size) # Taking the sum over the last axis reesults in shape: # (batch_size, num_sentence_words, num_sentence_words) cosine_numerator = tf.reduce_sum(tf.multiply(v1, v2), axis=-1) # Shape: (batch_size, 1, num_sentence_words) v1_norm = tf.sqrt(tf.maximum(tf.reduce_sum(tf.square(v1), axis=-1), EPSILON)) # Shape: (batch_size, num_sentence_words, 1) v2_norm = tf.sqrt(tf.maximum(tf.reduce_sum(tf.square(v2), axis=-1), EPSILON)) # Shape: (batch_size, num_sentence_words, num_sentence_words) return cosine_numerator / v1_norm / v2_norm
def f_iou_box(top_left_a, bot_right_a, top_left_b, bot_right_b): """Computes IoU of boxes. Args: top_left_a: [B, T, 2] or [B, 2] bot_right_a: [B, T, 2] or [B, 2] top_left_b: [B, T, 2] or [B, 2] bot_right_b: [B, T, 2] or [B, 2] Returns: iou: [B, T] """ inter_area = f_inter_box(top_left_a, bot_right_a, top_left_b, bot_right_b) inter_area = tf.maximum(inter_area, 1e-6) ndims = tf.shape(tf.shape(top_left_a)) # area_a = tf.reduce_prod(bot_right_a - top_left_a, ndims - 1) # area_b = tf.reduce_prod(bot_right_b - top_left_b, ndims - 1) check_a = tf.reduce_prod(tf.to_float(top_left_a < bot_right_a), ndims - 1) area_a = check_a * tf.reduce_prod(bot_right_a - top_left_a, ndims - 1) check_b = tf.reduce_prod(tf.to_float(top_left_b < bot_right_b), ndims - 1) area_b = check_b * tf.reduce_prod(bot_right_b - top_left_b, ndims - 1) union_area = (area_a + area_b - inter_area + 1e-5) union_area = tf.maximum(union_area, 1e-5) iou = inter_area / union_area iou = tf.maximum(iou, 1e-5) iou = tf.minimum(iou, 1.0) return iou
def __init__(self, model, mask, prob, coords, offset_xy_min, offset_xy_max, areas): self.model = model with tf.name_scope('true'): self.mask = tf.identity(mask, name='mask') self.prob = tf.identity(prob, name='prob') self.coords = tf.identity(coords, name='coords') self.offset_xy_min = tf.identity(offset_xy_min, name='offset_xy_min') self.offset_xy_max = tf.identity(offset_xy_max, name='offset_xy_max') self.areas = tf.identity(areas, name='areas') with tf.name_scope('iou') as name: _offset_xy_min = tf.maximum(model.offset_xy_min, self.offset_xy_min, name='_offset_xy_min') _offset_xy_max = tf.minimum(model.offset_xy_max, self.offset_xy_max, name='_offset_xy_max') _wh = tf.maximum(_offset_xy_max - _offset_xy_min, 0.0, name='_wh') _areas = tf.reduce_prod(_wh, -1, name='_areas') areas = tf.maximum(self.areas + model.areas - _areas, 1e-10, name='areas') iou = tf.truediv(_areas, areas, name=name) with tf.name_scope('mask'): best_box_iou = tf.reduce_max(iou, 2, True, name='best_box_iou') best_box = tf.to_float(tf.equal(iou, best_box_iou), name='best_box') mask_best = tf.identity(self.mask * best_box, name='mask_best') mask_normal = tf.identity(1 - mask_best, name='mask_normal') with tf.name_scope('dist'): iou_dist = tf.square(model.iou - mask_best, name='iou_dist') coords_dist = tf.square(model.coords - self.coords, name='coords_dist') prob_dist = tf.square(model.prob - self.prob, name='prob_dist') with tf.name_scope('objectives'): cnt = np.multiply.reduce(iou_dist.get_shape().as_list()) self['iou_best'] = tf.identity(tf.reduce_sum(mask_best * iou_dist) / cnt, name='iou_best') self['iou_normal'] = tf.identity(tf.reduce_sum(mask_normal * iou_dist) / cnt, name='iou_normal') self['coords'] = tf.identity(tf.reduce_sum(tf.expand_dims(mask_best, -1) * coords_dist) / cnt, name='coords') self['prob'] = tf.identity(tf.reduce_sum(tf.expand_dims(self.mask, -1) * prob_dist) / cnt, name='prob')
def IoU(bbox, gt): # bbox = [ x , y , w , h ] ( x , y left up) shape = [-1, 1] x1 = tf.maximum(tf.cast(bbox[0], tf.float32), tf.reshape(tf.cast(gt[:,0], tf.float32), shape)) y1 = tf.maximum(tf.cast(bbox[1], tf.float32), tf.reshape(tf.cast(gt[:,1], tf.float32), shape)) x2 = tf.minimum(tf.cast(bbox[2] + bbox[0], tf.float32), tf.reshape(tf.cast(gt[:,2] + gt[:,0], tf.float32), shape)) y2 = tf.minimum(tf.cast(bbox[3] + bbox[1], tf.float32), tf.reshape(tf.cast(gt[:,3] + gt[:,1], tf.float32), shape)) inter_w = tf.sub(x2,x1) inter_h = tf.sub(y2,y1) inter = tf.cast(inter_w * inter_h, tf.float32) bounding_box = tf.cast(tf.mul(bbox[2],bbox[3]), tf.float32) ground_truth = tf.reshape(tf.cast(tf.mul(gt[:,2],gt[:,3]), tf.float32), shape) #iou = tf.div(inter,tf.sub(tf.add(bounding_box,tf.reshape(ground_truth,shape)),inter)) iou = inter / (bounding_box + ground_truth - inter) # limit the iou range between 0 and 1 mask_less = tf.cast(tf.logical_not(tf.less(iou, tf.zeros_like(iou))), tf.float32) #mask_great = tf.cast(tf.logical_not(tf.greater(iou, tf.ones_like(iou))), tf.float32) iou = tf.mul(iou, mask_less) #iou = tf.mul(iou, positive_mask) return iou
def _apply_dense(self, grad, var): gradients_sum = self.get_slot(var, "gradients_sum") grad_norm_sum = self.get_slot(var, "grad_norm_sum") tilde_w = self.get_slot(var, "tilde_w") L = self.get_slot(var, "L") reward = self.get_slot(var, "reward") L_update = tf.maximum(L, tf.abs(grad)) gradients_sum_update = gradients_sum + grad grad_norm_sum_update = grad_norm_sum + tf.abs(grad) reward_update = tf.maximum(reward - grad * tilde_w, 0) new_w = -gradients_sum_update / ( L_update * (tf.maximum(grad_norm_sum_update + L_update, self._alpha * L_update))) * ( reward_update + L_update) var_update = var - tilde_w + new_w tilde_w_update = new_w gradients_sum_update_op = state_ops.assign(gradients_sum, gradients_sum_update) grad_norm_sum_update_op = state_ops.assign(grad_norm_sum, grad_norm_sum_update) var_update_op = state_ops.assign(var, var_update) tilde_w_update_op = state_ops.assign(tilde_w, tilde_w_update) L_update_op = state_ops.assign(L, L_update) reward_update_op = state_ops.assign(reward, reward_update) return control_flow_ops.group(*[gradients_sum_update_op, var_update_op, grad_norm_sum_update_op, tilde_w_update_op, reward_update_op, L_update_op])
def _tf_loss(self, sim, sim_emb): """Define loss""" if self.use_max_sim_neg: max_sim_neg = tf.reduce_max(sim[:, 1:], -1) loss = tf.reduce_mean(tf.maximum(0., self.mu_pos - sim[:, 0]) + tf.maximum(0., self.mu_neg + max_sim_neg)) else: # create an array for mu mu = self.mu_neg * np.ones(self.num_neg + 1) mu[0] = self.mu_pos factors = tf.concat([-1 * tf.ones([1, 1]), tf.ones([1, tf.shape(sim)[1] - 1])], 1) max_margin = tf.maximum(0., mu + factors * sim) loss = tf.reduce_mean(tf.reduce_sum(max_margin, -1)) max_sim_emb = tf.maximum(0., tf.reduce_max(sim_emb, -1)) loss = (loss + # penalize max similarity between intent embeddings tf.reduce_mean(max_sim_emb) * self.C_emb + # add regularization losses tf.losses.get_regularization_loss()) return loss
def IoULoss(self, pd, gt): mask = tf.cast( tf.greater(tf.reduce_sum( tf.cast(tf.greater(gt, 0), tf.int8), 3), 3), tf.float32 ) npd = tf.transpose(pd, [3, 0, 1, 2]) ngt = tf.transpose(gt, [3, 0, 1, 2]) area_x = tf.mul( tf.add(tf.gather(npd, 0), tf.gather(npd, 2)), tf.add(tf.gather(npd, 1), tf.gather(npd, 3)), ) area_g = tf.mul( tf.add(tf.gather(ngt, 0), tf.gather(ngt, 2)), tf.add(tf.gather(ngt, 1), tf.gather(ngt, 3)), ) w_overlap = tf.maximum(tf.constant(0, tf.float32), tf.add( tf.minimum(tf.gather(npd, 0), tf.gather(ngt, 0)), tf.minimum(tf.gather(npd, 2), tf.gather(ngt, 2)), )) h_overlap = tf.maximum(tf.constant(0, tf.float32), tf.add( tf.minimum(tf.gather(npd, 1), tf.gather(ngt, 1)), tf.minimum(tf.gather(npd, 3), tf.gather(ngt, 3)), )) area_overlap = tf.mul(w_overlap, h_overlap) area_u = tf.sub(tf.add(area_x, area_g), area_overlap) iou = tf.div(area_overlap, tf.add(area_u, tf.constant(1, tf.float32))) iou = tf.maximum(iou, tf.constant(1e-4, tf.float32)) cost = -tf.log(iou) cost = tf.mul(cost, mask) cost = tf.reduce_sum(cost) return cost
def sample_from_discretized_mix_logistic(l, nr_mix): ls = int_shape(l) xs = ls[:-1] + [3] # unpack parameters logit_probs = l[:, :, :, :nr_mix] l = tf.reshape(l[:, :, :, nr_mix:], xs + [nr_mix * 3]) # sample mixture indicator from softmax sel = tf.one_hot(tf.argmax(logit_probs - tf.log(-tf.log(tf.random_uniform( logit_probs.get_shape(), minval=1e-5, maxval=1. - 1e-5))), 3), depth=nr_mix, dtype=tf.float32) sel = tf.reshape(sel, xs[:-1] + [1, nr_mix]) # select logistic parameters means = tf.reduce_sum(l[:, :, :, :, :nr_mix] * sel, 4) log_scales = tf.maximum(tf.reduce_sum( l[:, :, :, :, nr_mix:2 * nr_mix] * sel, 4), -7.) coeffs = tf.reduce_sum(tf.nn.tanh( l[:, :, :, :, 2 * nr_mix:3 * nr_mix]) * sel, 4) # sample from logistic & clip to interval # we don't actually round to the nearest 8bit value when sampling u = tf.random_uniform(means.get_shape(), minval=1e-5, maxval=1. - 1e-5) x = means + tf.exp(log_scales) * (tf.log(u) - tf.log(1. - u)) x0 = tf.minimum(tf.maximum(x[:, :, :, 0], -1.), 1.) x1 = tf.minimum(tf.maximum( x[:, :, :, 1] + coeffs[:, :, :, 0] * x0, -1.), 1.) x2 = tf.minimum(tf.maximum( x[:, :, :, 2] + coeffs[:, :, :, 1] * x0 + coeffs[:, :, :, 2] * x1, -1.), 1.) return tf.concat([tf.reshape(x0, xs[:-1] + [1]), tf.reshape(x1, xs[:-1] + [1]), tf.reshape(x2, xs[:-1] + [1])], 3)
def sample_from_discretized_mix_logistic(y, log_scale_min=-7.): ''' Args: y: Tensor, [batch_size, channels, time_length] Returns: Tensor: sample in range of [-1, 1] ''' with tf.control_dependencies([tf.assert_equal(tf.mod(tf.shape(y)[1], 3), 0)]): nr_mix = tf.shape(y)[1] // 3 #[batch_size, time_length, channels] y = tf.transpose(y, [0, 2, 1]) logit_probs = y[:, :, :nr_mix] #sample mixture indicator from softmax temp = tf.random_uniform(tf.shape(logit_probs), minval=1e-5, maxval=1. - 1e-5) temp = logit_probs - tf.log(-tf.log(temp)) argmax = tf.argmax(temp, -1) #[batch_size, time_length] -> [batch_size, time_length, nr_mix] one_hot = tf.one_hot(argmax, depth=nr_mix, dtype=tf.float32) #select logistic parameters means = tf.reduce_sum(y[:, :, nr_mix:2 * nr_mix] * one_hot, axis=-1) log_scales = tf.maximum(tf.reduce_sum( y[:, :, 2 * nr_mix:3 * nr_mix] * one_hot, axis=-1), log_scale_min) #sample from logistic & clip to interval #we don't actually round to the nearest 8-bit value when sampling u = tf.random_uniform(tf.shape(means), minval=1e-5, maxval=1. - 1e-5) x = means + tf.exp(log_scales) * (tf.log(u) - tf.log(1 -u)) return tf.minimum(tf.maximum(x, -1.), 1.)
def get_next_input(output): # the next location is computed by the location network baseline = tf.sigmoid(tf.matmul(output,Wb_h_b) + Bb_h_b) baselines.append(baseline) # compute the next location, then impose noise if eyeCentered: # add the last sampled glimpse location # TODO max(-1, min(1, u + N(output, sigma) + prevLoc)) mean_loc = tf.maximum(-1.0, tf.minimum(1.0, tf.matmul(output, Wl_h_l) + sampled_locs[-1] )) else: mean_loc = tf.matmul(output, Wl_h_l) # mean_loc = tf.stop_gradient(mean_loc) mean_locs.append(mean_loc) mean_locs_stopGrad.append(tf.stop_gradient(mean_loc)) # add noise # sample_loc = tf.tanh(mean_loc + tf.random_normal(mean_loc.get_shape(), 0, loc_sd)) sample_loc = tf.maximum(-1.0, tf.minimum(1.0, mean_loc + tf.random_normal(mean_loc.get_shape(), 0, loc_sd))) # don't propagate throught the locations # sample_loc = tf.stop_gradient(sample_loc) sampled_locs.append(sample_loc) sampled_locs_stopGrad.append(tf.stop_gradient(sample_loc)) return get_glimpse(sample_loc)
def prune_conv_w(self, w, w_abs_mean): with tf.name_scope("Prune_conv"): conv_gamma = 0.25 * self.gamma log_w = tf.log(tf.maximum(self.eps, tf.abs(w) / (w_abs_mean * conv_gamma))) if self.max_ratio > 0: log_w = tf.minimum(self.max_ratio, self.beta * log_w) return w * tf.maximum(self.alpha / self.beta * log_w, log_w)
def preprocess(img, input_size, model): # Convert RGB to BGR img_r, img_g, img_b = tf.split(axis=2, num_or_size_splits=3, value=img) img = tf.cast(tf.concat(axis=2, values=[img_b, img_g, img_r]), dtype=tf.float32) # Extract mean. img -= IMG_MEAN if model == 'fcn-8s': shape = tf.shape(img) img = tf.expand_dims(img, dim=0) output = tf.image.resize_bilinear(img, input_size) return output, shape elif model == 'pspnet50': shape = tf.shape(img) h, w = (tf.maximum(input_size[0], shape[0]), tf.maximum(input_size[1], shape[1])) pad_img = tf.image.pad_to_bounding_box(img, 0, 0, h, w) output = tf.expand_dims(pad_img, dim=0) return output, h, w, shape elif model == 'icnet': img = tf.expand_dims(img, dim=0) output = tf.image.resize_bilinear(img, input_size) return output, input_size
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: Epilson, 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 = tf.clip_by_value(eta, -eps, eps) else: if ord == 1: norm = tf.maximum(avoid_zero_div, reduce_sum(tf.abs(eta), reduc_ind, keepdims=True)) 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., eps / norm) eta = eta * factor return eta
def prune_w(self, w, w_abs, w_abs_mean, w_abs_std): self.cursor += 1 with tf.name_scope("Prune"): if self.cond_placeholder is None: log_w = tf.log(tf.maximum(self.eps, w_abs / (w_abs_mean * self.gamma))) if self.max_ratio > 0: log_w = tf.minimum(self.max_ratio, self.beta * log_w) self.masks.append(tf.maximum(self.alpha / self.beta * log_w, log_w)) return w * self.masks[self.cursor] self.masks.append(tf.Variable(np.ones(w.get_shape(), np.float32), trainable=False)) def prune(i, do_prune): def sub(): if not do_prune: mask = self.masks[i] self.masks[i] = tf.assign(mask, tf.where( tf.logical_and( tf.equal(mask, 1), tf.less_equal(w_abs, 0.9 * tf.maximum(w_abs_mean + self.beta * w_abs_std, self.eps)) ), tf.zeros_like(mask), mask )) mask = self.masks[i] self.masks[i] = tf.assign(mask, tf.where( tf.logical_and( tf.equal(mask, 0), tf.greater(w_abs, 1.1 * tf.maximum(w_abs_mean + self.beta * w_abs_std, self.eps)) ), tf.ones_like(mask), mask )) return w * self.masks[i] return sub return tf.cond(self.cond_placeholder, prune(self.cursor, True), prune(self.cursor, False))
def _update_lipschitz(self,v,i): config = self.config if len(v.shape) > 1: k = self.config.weight_constraint_k or 100.0000 wi_hat = v if len(v.shape) == 4: #fij = tf.reduce_sum(tf.abs(wi_hat), axis=[0,1]) fij = wi_hat fij = tf.reduce_sum(tf.abs(fij), axis=[1]) fij = tf.reduce_max(fij, axis=[0]) else: fij = wi_hat if self.config.ortho_pnorm == "inf": wp = tf.reduce_max(tf.reduce_sum(tf.abs(fij), axis=0), axis=0) else: # conv wp = tf.reduce_max(tf.reduce_sum(tf.abs(fij), axis=1), axis=0) ratio = (1.0/tf.maximum(1.0, wp/k)) if self.config.weight_bounce: bounce = tf.minimum(1.0, tf.ceil(wp/k-0.999)) ratio -= tf.maximum(0.0, bounce) * 0.2 if self.config.weight_scaleup: up = tf.minimum(1.0, tf.ceil(0.02-wp/k)) ratio += tf.maximum(0.0, up) * k/wp * 0.2 wi = ratio*(wi_hat) #self.gan.metrics['wi'+str(i)]=wp #self.gan.metrics['wk'+str(i)]=ratio #self.gan.metrics['bouce'+str(i)]=bounce return tf.assign(v, wi) return None
def run_tf_simulation(self, c_in, h_in, timesteps=100, dt=0.005): r_e = tf.Variable( tf.zeros([self.N_pairs, self.N_pairs]) ) r_i = tf.Variable( tf.zeros([self.N_pairs, self.N_pairs]) ) W_EE = tf.placeholder(tf.float32) W_EI = tf.placeholder(tf.float32) W_IE = tf.placeholder(tf.float32) W_II = tf.placeholder(tf.float32) k = tf.placeholder(tf.float32) n_E = tf.placeholder(tf.float32) n_I = tf.placeholder(tf.float32) tau_E = tf.placeholder(tf.float32) tau_I = tf.placeholder(tf.float32) c0 = tf.constant(c_in) h0 = tf.constant(h_in) # Compile functions: I_E = c0*h0 + tf.transpose(tf.reshape(tf.reduce_sum(W_EE * r_e, [1,2]), [75,75])) \ - tf.transpose(tf.reshape(tf.reduce_sum(W_EI * r_i, [1,2]), [75,75])) I_I = c0*h0 + tf.transpose(tf.reshape(tf.reduce_sum(W_IE * r_e, [1,2]), [75,75])) \ - tf.transpose(tf.reshape(tf.reduce_sum(W_II * r_i, [1,2]), [75,75])) I_thresh_E = tf.maximum(0., I_E) I_thresh_I = tf.maximum(0., I_I) r_SS_E = k * tf.pow(I_thresh_E, n_E) r_SS_I = k * tf.pow(I_thresh_I, n_I) rE_out = r_e + dt*(-r_e+r_SS_E)/tau_E rI_out = r_i + dt*(-r_i+r_SS_I)/tau_I update_rE = tf.assign(r_e, rE_out) update_rI = tf.assign(r_i, rI_out) init = tf.initialize_all_variables() rE = 0 rI = 0 fd = {W_EE:self.W_EE.astype(np.float32), W_EI:self.W_EI.astype(np.float32), W_IE:self.W_IE.astype(np.float32), W_II:self.W_II.astype(np.float32), k:self.k.astype(np.float32), n_E:self.n_E.astype(np.float32), n_I:self.n_I.astype(np.float32), tau_E:self.tau_E.astype(np.float32), tau_I:self.tau_I.astype(np.float32)} with tf.Session() as sess: sess.run(init, feed_dict=fd) for t in range(timesteps): # run the simulation sess.run([update_rE, update_rI], feed_dict=fd) # fetch the rates rE = sess.run([r_e], feed_dict=fd) rI = sess.run([r_i], feed_dict=fd) return rE, rI
def tf_kl_gaussgauss(mu_1, sigma_1, mu_2, sigma_2): with tf.variable_scope("kl_gaussgauss"): return tf.reduce_sum(0.5 * ( 2 * tf.log(tf.maximum(1e-9,sigma_2),name='log_sigma_2') - 2 * tf.log(tf.maximum(1e-9,sigma_1),name='log_sigma_1') + (tf.square(sigma_1) + tf.square(mu_1 - mu_2)) / tf.maximum(1e-9,(tf.square(sigma_2))) - 1 ), 1)
def leaky_relu(x, alpha=0.2): with tf.name_scope('LeakyRelu'): alpha = tf.constant(alpha, dtype=x.dtype, name='alpha') return tf.maximum(x * alpha, x)
def detection_targets_graph(proposals, gt_class_ids, gt_boxes, gt_masks, config): """ For a single image proposals: N, 4 normalized (x1, y1, x2, y2) gt_class_ids: max_instance gt_boxes: max_instance, 4 normalized gt_masks: h, w, max_instance """ asserts = [tf.Assert(tf.greater(tf.shape(proposals)[0], 0), [proposals], name="roi_assertion"),] # control_inputs: A list of Operation or Tensor objects which must be # executed or computed before running the operations defined in the context. # Can also be None to clear the control dependencies. with tf.control_dependencies(asserts): proposals = tf.identity(proposals) # remove zero padding proposals, _ = trim_zeros_graph(proposals, name="trim_proposals") gt_boxes, non_zeros = trim_zeros_graph(gt_boxes, name="trim_gt_boxes") gt_class_ids = tf.boolean_mask(gt_class_ids, non_zeros, name="trim_gt_class_ids") gt_masks = tf.gather(gt_masks, tf.where(non_zeros)[:, 0], axis=2, name="trim_gt_masks") # exclude crowd boxes crowd_ix = tf.where(gt_class_ids < 0)[:, 0] non_crowd_ix = tf.where(gt_class_ids > 0) crowd_boxes = tf.gather(gt_boxes, crowd_ix) # tf.gather indices slices along specified axis crowd_masks = tf.gather(gt_masks, crowd_ix, axis=2) gt_class_ids = tf.gather(gt_class_ids, non_crowd_ix) gt_boxes = tf.gather(gt_boxes, non_crowd_ix) gt_masks = tf.gather(gt_masks, non_crowd_ix, axis=2) # compute overlap matrix [proposal, gt_boxes] overlaps = overlaps_graph(proposals, gt_boxes) # overlap with crowd boxes [anchors???, crowds] crowd_overlaps = overlap_graph(proposals, crowd_boxes) crowd_iou_max = tf.reduce_max(crowd_overlaps, axis=1) non_crowd_bool = (crowd_iou_max < 0.001) # ??? # Determine positive and negative ROIs roi_iou_max = tf.reduce_max(overlaps, axis=1) # 1. pos rois has over 50% overlap with gt boxes positive_roi_bool = (roi_iou_max >= 0.5) positive_indices = tf.where(positive_roi_bool)[:,0] # 2. neg roi has less 50% overlap with gt_boxes. Skip Crowd?? negative_indices = tf.where(tf.logical_and(roi_iou_max<0.5, non_crowd_bool))[:, 0] # Subsample ROIs. Aim for 33% positive # Pos ROI positive_count = int(config.TRIAN_ROIS_PER_IMAGE * config.ROI_POSITIVE_RATIO) positive_indices = tf.random_shuffle(positive_indices)[:positive_count] positive_count = tf.shape(positive_indices)[0] # Neg ROI r = 1.0 / config.ROI_POSITIVE_RATIO negative_count = tf.cast(r * tf.cast(positive_count, tf.float32), tf.int32) - positive_count # gather selected ROIs positive_rois = tf.gather(proposals, positive_indices) negative_rois = tf.gather(proposals, negative_indices) positive_overlaps = tf.gather(overlaps, positive_indices) roi_gt_box_assignment = tf.argmax(positive_overlaps, axis=1) roi_gt_boxes = tf.gather(gt_boxes, roi_gt_box_assignment) roi_gt_class_ids = tf.gather(gt_class_ids, roi_gt_box_assignment) # box refinement for positive rois: delta ??? deltas = utils.box_refinement_graph(positive_rois, roi_gt_boxes) deltas /= config.BBOX_STD_DEV transposed_masks = tf.expand_dims(tf.transpose(gt_masks, [2,0,1]), -1) # N, imgh, imgw, 1 roi_masks = tf.gather(transposed_masks, roi_gt_box_assignment) # Compute mask targets boxes = positive_rois if config.USE_MINI_MASK: # ??? y1, x1, y2, x2 = tf.split(positive_rois, 4, axis=1) # equally split gt_y1, gt_x1, gt_y2, gt_x2 = tf.split(roi_gt_boxes, 4, axis=1) gt_h = gt_y2 - gt_y1 gt_w = gt_x2 - gt_x1 # normalize proposal ROIs coordinates based gt box y1 = (y1 - gt_y1) / gt_h x1 = (x1 - gt_x1) / gt_w y2 = (y2 - gt_y2) / gt_h x2 = (x2 - gt_x2) / gt_w boxes = tf.concat([x1, y1, x2, y2], 1) # N_roi, 4 box_ids = tf.range(0, tf.shape(roi_masks)[0]) masks = tf.image.crop_and_resize(tf.cast(roi_masks, tf.float32), boxes, box_ids, config.MINI_MASK_SHAPE) masks = tf.squeeze(masks, axis=3) # only keep n, h, w masks = tf.round(masks) rois = tf.concat([positive_rois, negative_rois], axis=0) # npos+nneg, 4 N = tf.shape(negative_rois)[0] P = tf.maximum(config.TRAIN_ROIS_PER_IMAGE - tf.shape(rois)[0], 0) # padding size rois = tf.pad(rois, [(0, P), (0, 0)]) # MAX_TRAIN_ROI_PER_IMAGE, 4 roi_gt_boxes = tf.pad(roi_gt_boxes, [(0, N+P), (0, 0)]) roi_gt_class_ids = tf.pad(roi_gt_class_ids, [(0, N+P)]) deltas = tf.pad(deltas, [(0, N+P), (0, 0)]) masks = tf.pad(masks, [(0, N+P), (0, 0)]) # padding-zero cropped gt masks return rois, roi_gt_class_ids, deltas, masks
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) W_fc1 = utils.weight_variable([7*7*64, 1024], 'fc1_weight') b_fc1 = utils.bias_variable([1024], 'fc1_bias') h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) with tf.name_scope('slice_data'): h_s = tf.cond(train_flag, lambda: tf.slice(h_fc1, [0, 0], [batch_size / 2, -1]), lambda: h_fc1) h_t = tf.cond(train_flag, lambda: tf.slice(h_fc1, [batch_size / 2, 0], [batch_size / 2, -1]), lambda: h_fc1) ys_true = tf.cond(train_flag, lambda: tf.slice(y_, [0, 0], [batch_size / 2, -1]), lambda: y_) with tf.name_scope('mmd'): sigmas = [1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6] gaussian_kernel = partial(gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) loss_value = utils.maximum_mean_discrepancy(h_s, h_t, kernel=gaussian_kernel) mmd_loss = mmd_param * tf.maximum(1e-4, loss_value) with tf.name_scope('classifier'): W_fc2 = utils.weight_variable([1024, 10], 'fc2_weight') b_fc2 = utils.bias_variable([10], 'fc2_bias') pred_logit = tf.matmul(h_s, W_fc2) + b_fc2 clf_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred_logit, labels=ys_true)) clf_acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(ys_true, 1), tf.argmax(pred_logit, 1)), tf.float32)) all_variables = tf.trainable_variables() l2_loss = l2_param * tf.add_n([tf.nn.l2_loss(v) for v in all_variables if 'bias' not in v.name]) total_loss = clf_loss + l2_loss + mmd_loss train_op = tf.train.AdamOptimizer(lr).minimize(total_loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer())
def random_cutout(images, labels, mask_size, constant_values=0, seed=None): batch_size = tf.shape(images)[0] mask_size, image_height, image_width = _norm_params(images, mask_size) cutout_center_height = tf.random.uniform(shape=[batch_size], minval=0, maxval=image_height, dtype=tf.int32, seed=seed) cutout_center_width = tf.random.uniform(shape=[batch_size], minval=0, maxval=image_width, dtype=tf.int32, seed=seed) offset = tf.transpose([cutout_center_height, cutout_center_width], [1, 0]) origin_shape = images.shape offset = tf.convert_to_tensor(offset) mask_size = mask_size // 2 if tf.rank(offset) == 1: offset = tf.expand_dims(offset, 0) cutout_center_heights = offset[:, 0] cutout_center_widths = offset[:, 1] lower_pads = tf.maximum(0, cutout_center_heights - mask_size[0]) upper_pads = tf.maximum( 0, image_height - cutout_center_heights - mask_size[0]) left_pads = tf.maximum(0, cutout_center_widths - mask_size[1]) right_pads = tf.maximum(0, image_width - cutout_center_widths - mask_size[1]) cutout_shape = tf.transpose( [ image_height - (lower_pads + upper_pads), image_width - (left_pads + right_pads), ], [1, 0], ) masks = tf.TensorArray(images.dtype, 0, dynamic_size=True) for i in tf.range(tf.shape(cutout_shape)[0]): padding_dims = [ [lower_pads[i], upper_pads[i]], [left_pads[i], right_pads[i]], ] mask = tf.pad( tf.zeros(cutout_shape[i], dtype=images.dtype), padding_dims, constant_values=1, ) masks = masks.write(i, mask) mask_4d = tf.expand_dims(masks.stack(), -1) mask = tf.tile(mask_4d, [1, 1, 1, tf.shape(images)[-1]]) images = tf.where( mask == 0, tf.ones_like(images, dtype=images.dtype) * constant_values, images, ) images.set_shape(origin_shape) return images, labels
def read_cell_query(name): with tf.name_scope(name): taps = {} sources = [] def add_taps(prefix, extra_taps): for k, v in extra_taps.items(): taps[prefix + "_" + k] = v # -------------------------------------------------------------------------- # Produce all the difference sources of addressing query # -------------------------------------------------------------------------- # Content address the question tokens token_query = tf.layers.dense(attention_master_signal, args["input_width"]) token_signal, _, x_taps = attention(in_question_tokens, token_query) sources.append(token_signal) add_taps("token_content", x_taps) # Index address the question tokens padding = [[0, 0], [ 0, tf.maximum( 0, args["max_seq_len"] - tf.shape(in_question_tokens)[1]) ], [0, 0]] # batch, seq_len, token in_question_tokens_padded = tf.pad(in_question_tokens, padding) in_question_tokens_padded.set_shape( [None, args["max_seq_len"], None]) token_index_signal, query = attention_by_index( in_question_tokens_padded, attention_master_signal) sources.append(token_index_signal) taps["token_index_attn"] = tf.expand_dims(query, 2) # Use the iteration id step_const_signal = tf.layers.dense(in_iter_id, args["input_width"]) sources.append(step_const_signal) # Use the memory contents if args["use_memory_cell"]: memory_shape = [ features["d_batch_size"], args["memory_width"] // args["input_width"], args["input_width"] ] memory_query = tf.layers.dense(attention_master_signal, args["input_width"]) memory_signal, _, x_taps = attention( tf.reshape(in_memory_state, memory_shape), memory_query) sources.append(memory_signal) add_taps("memory", x_taps) # Use the previous output of the network prev_output_query = tf.layers.dense(attention_master_signal, args["output_width"]) in_prev_outputs_padded = tf.pad(in_prev_outputs, [[ 0, 0 ], [ 0, args["max_decode_iterations"] - tf.shape(in_prev_outputs)[1] ], [0, 0]]) prev_output_signal, _, x_taps = attention(in_prev_outputs_padded, prev_output_query) sources.append(prev_output_signal) add_taps("prev_output", x_taps) # -------------------------------------------------------------------------- # Choose a query source # -------------------------------------------------------------------------- query_signal, q_tap = attention_by_index(tf.stack(sources, 1), attention_master_signal) taps["switch_attn"] = q_tap return query_signal, taps
def prelu(_x, name=None): if name is None: name = "alpha" alpha = tf.get_variable(name,shape=_x.get_shape(),initializer=tf.constant_initializer(0.0),dtype=_x.dtype) return tf.maximum(_alpha*_x, _x)
def lrelu(input, scope_name, leak=0.2): return tf.maximum(input, input * leak, name=scope_name)
def _last_token(x: tf.Tensor, sequence_lengths: tf.Tensor) -> tf.Tensor: last_sequence_index = tf.maximum(0, sequence_lengths - 1) batch_index = tf.range(tf.shape(last_sequence_index)[0]) indices = tf.stack([batch_index, last_sequence_index], axis=1) return tf.gather_nd(x, indices)
def _add_interpretation_graph(self): """Interpret NN output.""" mc = self.mc with tf.variable_scope('interpret_output') as scope: preds = self.preds # probability num_class_probs = mc.ANCHOR_PER_GRID * mc.CLASSES self.pred_class_probs = tf.reshape( tf.nn.softmax( tf.reshape(preds[:, :, :, :num_class_probs], [-1, mc.CLASSES])), [mc.BATCH_SIZE, mc.ANCHORS, mc.CLASSES], name='pred_class_probs') print("pred_class_probs shape: ", self.pred_class_probs.get_shape()) # confidence num_confidence_scores = mc.ANCHOR_PER_GRID + num_class_probs self.pred_conf = tf.sigmoid(tf.reshape( preds[:, :, :, num_class_probs:num_confidence_scores], [mc.BATCH_SIZE, mc.ANCHORS]), name='pred_confidence_score') print("pred_confidence_score: ", self.pred_conf.get_shape()) # bbox_delta self.pred_box_delta = tf.reshape(preds[:, :, :, num_confidence_scores:], [mc.BATCH_SIZE, mc.ANCHORS, 4], name='bbox_delta') print("bbox_delta: ", self.pred_box_delta.get_shape()) # number of object. Used to normalize bbox and classification loss self.num_objects = tf.reduce_sum(self.input_mask, name='num_objects') with tf.variable_scope('bbox') as scope: with tf.variable_scope('stretching'): delta_x, delta_y, delta_w, delta_h = tf.unstack( self.pred_box_delta, axis=2) anchor_x = mc.ANCHOR_BOX[:, 0] anchor_y = mc.ANCHOR_BOX[:, 1] anchor_w = mc.ANCHOR_BOX[:, 2] anchor_h = mc.ANCHOR_BOX[:, 3] box_center_x = tf.identity(anchor_x + delta_x * anchor_w, name='bbox_cx') box_center_y = tf.identity(anchor_y + delta_y * anchor_h, name='bbox_cy') box_width = tf.identity(anchor_w * util.safe_exp(delta_w, mc.EXP_THRESH), name='bbox_width') box_height = tf.identity(anchor_h * util.safe_exp(delta_h, mc.EXP_THRESH), name='bbox_height') self._activation_summary(delta_x, 'delta_x') self._activation_summary(delta_y, 'delta_y') self._activation_summary(delta_w, 'delta_w') self._activation_summary(delta_h, 'delta_h') self._activation_summary(box_center_x, 'bbox_cx') self._activation_summary(box_center_y, 'bbox_cy') self._activation_summary(box_width, 'bbox_width') self._activation_summary(box_height, 'bbox_height') with tf.variable_scope('trimming'): xmins, ymins, xmaxs, ymaxs = util.bbox_transform( [box_center_x, box_center_y, box_width, box_height]) # The max x position is mc.IMAGE_WIDTH - 1 since we use zero-based # pixels. Same for y. xmins = tf.minimum(tf.maximum(0.0, xmins), mc.IMAGE_WIDTH - 1.0, name='bbox_xmin') self._activation_summary(xmins, 'box_xmin') ymins = tf.minimum(tf.maximum(0.0, ymins), mc.IMAGE_HEIGHT - 1.0, name='bbox_ymin') self._activation_summary(ymins, 'box_ymin') xmaxs = tf.maximum(tf.minimum(mc.IMAGE_WIDTH - 1.0, xmaxs), 0.0, name='bbox_xmax') self._activation_summary(xmaxs, 'box_xmax') ymaxs = tf.maximum(tf.minimum(mc.IMAGE_HEIGHT - 1.0, ymaxs), 0.0, name='bbox_ymax') self._activation_summary(ymaxs, 'box_ymax') self.det_boxes = tf.transpose(tf.stack( util.bbox_transform_inv([xmins, ymins, xmaxs, ymaxs])), (1, 2, 0), name='bbox') with tf.variable_scope('IOU'): def _tensor_iou(box1, box2): with tf.variable_scope('intersection'): xmin = tf.maximum(box1[0], box2[0], name='xmin') ymin = tf.maximum(box1[1], box2[1], name='ymin') xmax = tf.minimum(box1[2], box2[2], name='xmax') ymax = tf.minimum(box1[3], box2[3], name='ymax') w = tf.maximum(0.0, xmax - xmin, name='inter_w') h = tf.maximum(0.0, ymax - ymin, name='inter_h') intersection = tf.multiply(w, h, name='intersection') with tf.variable_scope('union'): w1 = tf.subtract(box1[2], box1[0], name='w1') h1 = tf.subtract(box1[3], box1[1], name='h1') w2 = tf.subtract(box2[2], box2[0], name='w2') h2 = tf.subtract(box2[3], box2[1], name='h2') union = w1 * h1 + w2 * h2 - intersection return intersection/(union+mc.EPSILON) \ * tf.reshape(self.input_mask, [mc.BATCH_SIZE, mc.ANCHORS]) self.ious = self.ious.assign( _tensor_iou( util.bbox_transform(tf.unstack(self.det_boxes, axis=2)), util.bbox_transform(tf.unstack(self.box_input, axis=2)))) self._activation_summary(self.ious, 'conf_score') with tf.variable_scope('probability') as scope: self._activation_summary(self.pred_class_probs, 'class_probs') probs = tf.multiply(self.pred_class_probs, tf.reshape(self.pred_conf, [mc.BATCH_SIZE, mc.ANCHORS, 1]), name='final_class_prob') self._activation_summary(probs, 'final_class_prob') self.det_probs = tf.reduce_max(probs, 2, name='score') self.det_class = tf.argmax(probs, 2, name='class_idx')
def lrelu(x, leak=0.2, name="lrelu"): return tf.maximum(x, leak*x)
def LeakyReLU(x, beta=0.2): return tf.maximum(beta * x, x)
def _build(self, state_input, name=None): """Build model given input placeholder(s). Args: state_input (tf.Tensor): Place holder for state input. name (str): Inner model name, also the variable scope of the inner model, if exist. One example is garage.tf.models.Sequential. Return: tf.Tensor: Sampled action. tf.Tensor: Mean. tf.Tensor: Parameterized log_std. tf.Tensor: log_std. garage.tf.distributions.DiagonalGaussian: Policy distribution. """ del name action_dim = self._output_dim with tf.compat.v1.variable_scope('dist_params'): if self._std_share_network: # mean and std networks share an CNN b = np.concatenate([ np.zeros(action_dim), np.full(action_dim, self._init_std_param) ], axis=0) # yapf: disable mean_std_conv = cnn( input_var=state_input, filter_dims=self._filter_dims, hidden_nonlinearity=self._hidden_nonlinearity, hidden_w_init=self._hidden_w_init, hidden_b_init=self._hidden_b_init, num_filters=self._num_filters, strides=self._strides, padding=self._padding, name='mean_std_cnn') mean_std_network = mlp( mean_std_conv, output_dim=action_dim * 2, hidden_sizes=self._hidden_sizes, hidden_nonlinearity=self._hidden_nonlinearity, hidden_w_init=self._hidden_w_init, hidden_b_init=self._hidden_b_init, output_nonlinearity=self._output_nonlinearity, output_w_init=self._output_w_init, output_b_init=tf.constant_initializer(b), name='mean_std_network', layer_normalization=self._layer_normalization) with tf.compat.v1.variable_scope('mean_network'): mean_network = mean_std_network[..., :action_dim] with tf.compat.v1.variable_scope('log_std_network'): log_std_network = mean_std_network[..., action_dim:] else: # separate MLPs for mean and std networks # mean network mean_conv = cnn(input_var=state_input, filter_dims=self._filter_dims, hidden_nonlinearity=self._hidden_nonlinearity, hidden_w_init=self._hidden_w_init, hidden_b_init=self._hidden_b_init, num_filters=self._num_filters, strides=self._strides, padding=self._padding, name='mean_cnn') mean_network = mlp( mean_conv, output_dim=action_dim, hidden_sizes=self._hidden_sizes, hidden_nonlinearity=self._hidden_nonlinearity, hidden_w_init=self._hidden_w_init, hidden_b_init=self._hidden_b_init, output_nonlinearity=self._output_nonlinearity, output_w_init=self._output_w_init, output_b_init=self._output_b_init, name='mean_network', layer_normalization=self._layer_normalization) # std network if self._adaptive_std: log_std_conv = cnn( input_var=state_input, filter_dims=self._std_filter_dims, hidden_nonlinearity=self._std_hidden_nonlinearity, hidden_w_init=self._std_hidden_w_init, hidden_b_init=self._std_hidden_b_init, num_filters=self._std_num_filters, strides=self._std_strides, padding=self._std_padding, name='log_std_cnn') log_std_network = mlp( log_std_conv, output_dim=action_dim, hidden_sizes=self._std_hidden_sizes, hidden_nonlinearity=self._std_hidden_nonlinearity, hidden_w_init=self._std_hidden_w_init, hidden_b_init=self._std_hidden_b_init, output_nonlinearity=self._std_output_nonlinearity, output_w_init=self._std_output_w_init, output_b_init=tf.constant_initializer( self._init_std_param), name='log_std_network', layer_normalization=self._layer_normalization) else: log_std_network = parameter( input_var=state_input, length=action_dim, initializer=tf.constant_initializer( self._init_std_param), trainable=self._learn_std, name='log_std_network') mean_var = mean_network std_param = log_std_network with tf.compat.v1.variable_scope('std_limits'): if self._min_std_param is not None: std_param = tf.maximum(std_param, self._min_std_param) if self._max_std_param is not None: std_param = tf.minimum(std_param, self._max_std_param) with tf.compat.v1.variable_scope('std_parameterization'): # build std_var with std parameterization if self._std_parameterization == 'exp': log_std_var = std_param else: # we know it must be softplus here log_std_var = tf.math.log(tf.math.log(1. + tf.exp(std_param))) dist = DiagonalGaussian(self._output_dim) rnd = tf.random.normal(shape=mean_var.get_shape().as_list()[1:]) action_var = rnd * tf.exp(log_std_var) + mean_var return action_var, mean_var, log_std_var, std_param, dist
def soft_thresholding(input, thr): zero_tensor = tf.zeros(input.get_shape().as_list(), input.dtype.base_dtype) one_tensor = tf.ones(input.get_shape().as_list(), input.dtype.base_dtype) return tf.sign(input) * tf.maximum(zero_tensor, tf.abs(input) - thr * one_tensor)
import tflearn from oxnnet.data_loader import StandardDataLoaderPowerDoppler from oxnnet.record import RecordWriter, PowerDopplerProcessTup, RecordReader from oxnnet.full_inferer import PowerDopplerFullInferer from oxnnet.feats_writer import StandardFeatsWriter train_eval_test_no = [75, 15, 10] segment_size_in = np.array([64] * 3) segment_size_out = segment_size_in crop_by = 0 stride = np.array([32] * 3, dtype=np.int) data_loader = StandardDataLoaderPowerDoppler(stride, segment_size_in) #https://github.com/caglar/noisy_units/blob/master/codes/tf/nunits.py HardTanh = lambda x: tf.minimum(tf.maximum(x, -1.), 1.) lin_sigmoid = lambda x: 0.25 * x + 0.5 # Sigmoid = lambda x, use_noise=0: T.nnet.sigmoid(x) HardSigmoid = lambda x, angle=0.25: tf.maximum( tf.minimum(angle * x + 0.5, 1.0), 0.0) HardSigmoid = lambda x: tf.minimum(tf.maximum(lin_sigmoid(x), 0.), 1.) def NTanh(x, use_noise, alpha=1.05, c=0.5, half_normal=False): """ Noisy Hard Tanh Units: NAN without learning p ---------------------------------------------------- Arguments: x: tensorflow tensor variable, input of the function. use_noise: bool, whether to add noise or not to the activations, this is in particular useful for the test time, in order to disable the noise injection.
def mentornet(epoch, loss, labels, loss_p_percentile, example_dropout_rates, burn_in_epoch=18, fixed_epoch_after_burn_in=False, loss_moving_average_decay=0.5, mentornet_net_hparams=None, avg_name='cumulative', debug=False): """The MentorNet to train with the StudentNet. The details are in: Jiang, Lu, et al. "MentorNet: Learning Data-Driven Curriculum for Very Deep Neural Networks on Corrupted Labels." ICML. 2018. Args: epoch: a tensor [batch_size, 1] representing the training percentage. Each epoch is an integer between 0 and 99. loss: a tensor [batch_size, 1] representing the sample loss. labels: a tensor [batch_size, 1] representing the label. Every label is set to 0 in the current version. loss_p_percentile: a 1-d tensor of size 100, where each element is the p-percentile at that epoch to compute the moving average. example_dropout_rates: a 1-d tensor of size 100, where each element is the dropout rate at that epoch. Dropping out means the probability of setting sample weights to zeros proposed in Liang, Junwei, et al. "Learning to Detect Concepts from Webly-Labeled Video Data." IJCAI. 2016. burn_in_epoch: the number of burn_in_epoch. In the first burn_in_epoch, all samples have 1.0 weights. fixed_epoch_after_burn_in: whether to fix the epoch after the burn-in. loss_moving_average_decay: the decay factor to compute the moving average. mentornet_net_hparams: mentornet hyperparameters. avg_name: name of the loss moving average variable. debug: whether to print the weight information for debugging purposes. Returns: v: [batch_size, 1] weight vector. """ with tf.variable_scope('mentor_inputs'): loss_moving_avg = tf.get_variable(avg_name, [], initializer=tf.zeros_initializer(), trainable=False) if not fixed_epoch_after_burn_in: cur_epoch = epoch else: cur_epoch = tf.to_int32(tf.minimum(epoch, burn_in_epoch)) v_ones = tf.ones(tf.shape(loss), tf.float32) v_zeros = tf.zeros(tf.shape(loss), tf.float32) upper_bound = tf.cond(cur_epoch < (burn_in_epoch - 1), lambda: v_ones, lambda: v_zeros) this_dropout_rate = tf.squeeze( tf.nn.embedding_lookup(example_dropout_rates, cur_epoch)) this_percentile = tf.squeeze( tf.nn.embedding_lookup(loss_p_percentile, cur_epoch)) percentile_loss = tf.contrib.distributions.percentile( loss, this_percentile * 100) percentile_loss = tf.convert_to_tensor(percentile_loss) loss_moving_avg = loss_moving_avg.assign( loss_moving_avg * loss_moving_average_decay + (1 - loss_moving_average_decay) * percentile_loss) slim.summaries.add_scalar_summary( percentile_loss, '{}/percentile_loss'.format(avg_name)) slim.summaries.add_scalar_summary( cur_epoch, '{}/percentile_loss'.format(avg_name)) slim.summaries.add_scalar_summary( loss_moving_avg, '{}/percentile_loss'.format(avg_name)) ones = tf.ones([tf.shape(loss)[0], 1], tf.float32) epoch_vec = tf.scalar_mul(tf.to_float(cur_epoch), ones) lossdiff = loss - tf.scalar_mul(loss_moving_avg, ones) input_data = tf.squeeze(tf.stack([loss, lossdiff, labels, epoch_vec], 1)) hparams = mentornet_net_hparams if hparams: v = tf.nn.sigmoid(mentornet_nn( input_data, label_embedding_size=hparams.label_embedding_size, epoch_embedding_size=hparams.epoch_embedding_size, num_label_embedding=hparams.num_label_embedding, num_fc_nodes=hparams.num_fc_nodes), name='v') else: v = tf.nn.sigmoid(mentornet_nn(input_data), name='v') # Force select all samples in the first burn_in_epochs v = tf.maximum(v, upper_bound, 'v_bound') v_dropout = tf.py_func(probabilistic_sample, [v, this_dropout_rate, 'random'], tf.float32) v_dropout = tf.reshape(v_dropout, [-1, 1], name='v_dropout') # Print information in the debug mode. if debug: v_dropout = tf.Print( v_dropout, data=[cur_epoch, loss_moving_avg, percentile_loss], summarize=64, message='epoch, loss_moving_avg, percentile_loss') v_dropout = tf.Print(v_dropout, data=[lossdiff], summarize=64, message='loss_diff') v_dropout = tf.Print(v_dropout, data=[v], summarize=64, message='v') v_dropout = tf.Print(v_dropout, data=[v_dropout], summarize=64, message='v_dropout') return v_dropout
def lrelu(x, leak=0.1): return tf.maximum(x, leak * x)
def __init__(self, *, policy, ob_space, ac_space, nbatch_act, nbatch_train, nsteps, ent_coef, vf_coef, max_grad_norm): self.max_grad_norm = max_grad_norm self.head_idx_current_batch = 0 self.critic_idx_current_batch = 0 sess = tf.compat.v1.get_default_session() self.running_stats_s = RunningStats() self.running_stats_s_ = RunningStats() self.running_stats_r = RunningStats() self.running_stats_r_i = RunningStats() train_model = policy(sess, ob_space, ac_space, nbatch_train, nsteps, max_grad_norm) act_model = policy(sess, ob_space, ac_space, nbatch_act, 1, max_grad_norm) self.train_model = train_model # in case we don't use rep loss rep_loss = None # HEAD_IDX = tf.compat.v1.placeholder(tf.int32, [None]) A = train_model.pdtype.sample_placeholder([None],name='A') A_i = train_model.A_i LATENT_FACTORS = train_model.pdtype.sample_placeholder([Config.REP_LOSS_M,Config.POLICY_NHEADS,None,count_latent_factors(Config.ENVIRONMENT)],name='LATENT_FACTORS') ADV = tf.compat.v1.placeholder(tf.float32, [None],name='ADV') R = tf.compat.v1.placeholder(tf.float32, [None],name='R') R_NCE = tf.compat.v1.placeholder(tf.float32, [Config.REP_LOSS_M,1,None],name='R_NCE') OLDNEGLOGPAC = tf.compat.v1.placeholder(tf.float32, [None],name='OLDNEGLOGPAC') OLDNEGLOGPAC_i = tf.compat.v1.placeholder(tf.float32, [None],name='OLDNEGLOGPAC_i') LR = tf.compat.v1.placeholder(tf.float32, [],name='LR') CLIPRANGE = tf.compat.v1.placeholder(tf.float32, [],name='CLIPRANGE') if Config.CUSTOM_REP_LOSS: ADV_i= tf.compat.v1.placeholder(tf.float32, [None]) R_i = tf.compat.v1.placeholder(tf.float32, [None]) OLDVPRED_i = tf.compat.v1.placeholder(tf.float32, [None]) vpred_i = train_model.vf_i_train # Same as vf_run for SNI and default, but noisy for SNI2 while the boostrap is not vpredclipped_i = OLDVPRED_i + tf.clip_by_value(vpred_i - OLDVPRED_i, - CLIPRANGE, CLIPRANGE) vf_losses1_i = tf.square(vpred_i - R_i) vf_losses2_i = tf.square(vpredclipped_i - R_i) vf_loss_i = .5 * tf.reduce_mean(input_tensor=tf.maximum(vf_losses1_i, vf_losses2_i)) # ADV = ADV + ADV_i # TD loss for critic # VF loss OLDVPRED = tf.compat.v1.placeholder(tf.float32, [None],name='OLDVPRED') vpred = train_model.vf_train # Same as vf_run for SNI and default, but noisy for SNI2 while the boostrap is not if Config.CUSTOM_REP_LOSS and Config.POLICY_NHEADS > 1: vpred = vpred[self.critic_idx_current_batch] vpredclipped = OLDVPRED + tf.clip_by_value(vpred - OLDVPRED, - CLIPRANGE, CLIPRANGE) vf_losses1 = tf.square(vpred - R) vf_losses2 = tf.square(vpredclipped - R) vf_loss = .5 * tf.reduce_mean(input_tensor=tf.maximum(vf_losses1, vf_losses2)) neglogpac_train = train_model.pd_train[0].neglogp(A) ratio_train = tf.exp(OLDNEGLOGPAC - neglogpac_train) pg_losses_train = -ADV * ratio_train pg_losses2_train = -ADV * tf.clip_by_value(ratio_train, 1.0 - CLIPRANGE, 1.0 + CLIPRANGE) pg_loss = tf.reduce_mean(input_tensor=tf.maximum(pg_losses_train, pg_losses2_train)) approxkl_train = .5 * tf.reduce_mean(input_tensor=tf.square(neglogpac_train - OLDNEGLOGPAC)) clipfrac_train = tf.reduce_mean(input_tensor=tf.cast(tf.greater(tf.abs(ratio_train - 1.0), CLIPRANGE), dtype=tf.float32)) if Config.CUSTOM_REP_LOSS: neglogpac_train_i = train_model.pd_train_i.neglogp(A_i[:,0,self.head_idx_current_batch]) ratio_train_i = tf.exp(OLDNEGLOGPAC_i - neglogpac_train_i) pg_losses_train_i = -ADV_i * ratio_train_i pg_losses2_train_i = -ADV_i * tf.clip_by_value(ratio_train_i, 1.0 - CLIPRANGE, 1.0 + CLIPRANGE) pg_loss_i = tf.reduce_mean(input_tensor=tf.maximum(pg_losses_train_i, pg_losses2_train_i)) else: pg_loss_i = tf.constant(0.,dtype=tf.float32) if Config.BETA >= 0: entropy = tf.reduce_mean(input_tensor=train_model.pd_train[0]._components_distribution.entropy()) else: entropy = tf.reduce_mean(input_tensor=train_model.pd_train[0].entropy()) # Add entropy and policy loss for the samples as well if Config.SNI or Config.SNI2: neglogpac_run = train_model.pd_run.neglogp(A) ratio_run = tf.exp(OLDNEGLOGPAC - neglogpac_run) pg_losses_run = -ADV * ratio_run pg_losses2_run = -ADV * tf.clip_by_value(ratio_run, 1.0 - CLIPRANGE, 1.0 + CLIPRANGE) pg_loss += tf.reduce_mean(input_tensor=tf.maximum(pg_losses_run, pg_losses2_run)) pg_loss /= 2. entropy += tf.reduce_mean(input_tensor=train_model.pd_run.entropy()) entropy /= 2. approxkl_run = .5 * tf.reduce_mean(input_tensor=tf.square(neglogpac_run - OLDNEGLOGPAC)) clipfrac_run = tf.reduce_mean(input_tensor=tf.cast(tf.greater(tf.abs(ratio_run - 1.0), CLIPRANGE), dtype=tf.float32)) else: approxkl_run = tf.constant(0.) clipfrac_run = tf.constant(0.) params = tf.compat.v1.trainable_variables() weight_params = [v for v in params if '/b' not in v.name] total_num_params = 0 for p in params: shape = p.get_shape().as_list() num_params = np.prod(shape) mpi_print('param', p, num_params) total_num_params += num_params mpi_print('total num params:', total_num_params) l2_loss = tf.reduce_sum(input_tensor=[tf.nn.l2_loss(v) for v in weight_params]) # The first occurance should be in the train_model if Config.BETA >= 0: info_loss = tf.compat.v1.get_collection( key="INFO_LOSS", scope="model/info_loss" ) beta = Config.BETA elif Config.BETA_L2A >= 0: info_loss = tf.compat.v1.get_collection( key="INFO_LOSS_L2A", scope="model/info_loss" ) beta = Config.BETA_L2A else: info_loss = [tf.constant(0.)] beta = 0 # print(info_loss) assert len(info_loss) == 1 info_loss = info_loss[0] if Config.CUSTOM_REP_LOSS: rep_loss = tf.reduce_mean(train_model.rep_loss) loss = pg_loss - entropy * ent_coef + vf_loss * vf_coef + l2_loss * Config.L2_WEIGHT + beta * info_loss if Config.SYNC_FROM_ROOT: trainer = MpiAdamOptimizer(MPI.COMM_WORLD, learning_rate=LR, epsilon=1e-5) trainer_encoder = MpiAdamOptimizer(MPI.COMM_WORLD, learning_rate=LR, epsilon=1e-5) trainer_latent_transition = MpiAdamOptimizer(MPI.COMM_WORLD, learning_rate=LR, epsilon=1e-5) else: trainer = tf.compat.v1.train.AdamOptimizer(learning_rate=LR, epsilon=1e-5) self.opt = trainer grads_and_var = trainer.compute_gradients(loss, params) grads, var = zip(*grads_and_var) if max_grad_norm is not None: grads, _grad_norm = tf.clip_by_global_norm(grads, max_grad_norm) grads_and_var = list(zip(grads, var)) tot_norm = tf.zeros((1,)) for g,v in grads_and_var: tot_norm += tf.norm(g) tot_norm = tf.reshape(tot_norm, []) _train = trainer.apply_gradients(grads_and_var) grads_and_var_encoder = trainer_encoder.compute_gradients(train_model.encoder_bisimilarity_loss, params) grads_encoder, var_encoder = zip(*grads_and_var_encoder) if max_grad_norm is not None: grads_encoder, _grad_norm = tf.clip_by_global_norm(grads_encoder, max_grad_norm) grads_and_var_encoder = list(zip(grads_encoder, var_encoder)) _train_encoder = trainer_encoder.apply_gradients(grads_and_var_encoder) grads_and_var_latent = trainer_latent_transition.compute_gradients(train_model.latent_transition_loss, params) grads_latent, var_latent = zip(*grads_and_var_latent) if max_grad_norm is not None: grads_latent, _grad_norm = tf.clip_by_global_norm(grads_latent, max_grad_norm) grads_and_var_latent = list(zip(grads_latent, var_latent)) _train_latent = trainer_latent_transition.apply_gradients(grads_and_var_latent) def train(lr, cliprange, obs, returns, masks, actions, infos, values, neglogpacs, rewards, train_target='policy'): values = values[:,self.critic_idx_current_batch] if Config.CUSTOM_REP_LOSS else values advs = returns - values adv_mean = np.mean(advs, axis=0, keepdims=True) adv_std = np.std(advs, axis=0, keepdims=True) advs = (advs - adv_mean) / (adv_std + 1e-8) if Config.CUSTOM_REP_LOSS: advs_i = returns_i - values_i td_map = {train_model.X:obs, A:actions, ADV:advs, R:returns, train_model.R:rewards, train_model.A:actions, LR:lr, CLIPRANGE:cliprange, OLDNEGLOGPAC:neglogpacs, OLDVPRED:values } # import ipdb;ipdb.set_trace() if train_target == 'policy': rets = sess.run( [pg_loss, vf_loss, entropy, approxkl_train, clipfrac_train, approxkl_run, clipfrac_run, l2_loss, info_loss, tot_norm, _train],td_map)[:-1] return rets elif train_target == 'encoder': rets = sess.run( [_train_encoder],td_map)[:-1] return rets elif train_target == 'latent': rets = sess.run( [_train_latent],td_map)[:-1] return rets self.loss_names = ['policy_loss', 'value_loss', 'policy_entropy', 'approxkl_train', 'clipfrac_train', 'approxkl_run', 'clipfrac_run', 'l2_loss', 'info_loss_cv', 'rep_loss', 'value_i_loss', 'policy_loss_i','gradient_norm'] def save(save_path): ps = sess.run(params) joblib.dump(ps, save_path) def load(load_path): loaded_params = joblib.load(load_path) restores = [] for p, loaded_p in zip(params, loaded_params): restores.append(p.assign(loaded_p)) sess.run(restores) self.train = train self.train_model = train_model self.act_model = act_model self.step = act_model.step self.value = act_model.value self.initial_state = act_model.initial_state self.save = save self.load = load self.rep_vec = act_model.rep_vec self.custom_train = train_model.custom_train if Config.SYNC_FROM_ROOT: if MPI.COMM_WORLD.Get_rank() == 0: initialize() global_variables = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope="") sess.run(tf.compat.v1.global_variables_initializer()) sync_from_root(sess, global_variables) #pylint: disable=E1101 else: initialize()
def lrelu(x, leak, name): return tf.maximum(x, leak * x, name=name)
def __init__(self, sequence_length, num_classes, vocab_size, emd_dim, filter_sizes, num_filters, l2_reg_lambda=0.0, batch_size=32, reference_size=16, dropout_keep_prob=.75): # Placeholders for input, output and dropout self.input_x = tf.placeholder(tf.int32, [batch_size, sequence_length], name="input_x") self.input_ref = tf.placeholder(tf.int32, [reference_size, sequence_length], name="input_ref") self.input_y = tf.placeholder(tf.float32, [batch_size, num_classes], name="input_y") self.dropout_keep_prob = dropout_keep_prob # Keeping track of l2 regularization loss (optional) l2_loss = tf.constant(0.0) with tf.variable_scope('discriminator'): # Embedding layer with tf.device('/cpu:0'), tf.name_scope("embedding"): self.W = tf.Variable(tf.random_uniform([vocab_size, emd_dim], -1.0, 1.0), name="W") self.embedded_chars = tf.nn.embedding_lookup( self.W, self.input_x) self.embedded_chars_expanded = tf.expand_dims( self.embedded_chars, -1) self.embedded_chars_ref = tf.nn.embedding_lookup( self.W, self.input_ref) self.embedded_chars_expanded_ref = tf.expand_dims( self.embedded_chars_ref, -1) # Create a convolution + maxpool layer for each filter size pooled_outputs = [] pooled_outputs_ref = [] for filter_size, num_filter in zip(filter_sizes, num_filters): with tf.name_scope("conv-maxpool-%s" % filter_size): # Convolution Layer filter_shape = [filter_size, emd_dim, 1, num_filter] W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W") b = tf.Variable(tf.constant(0.1, shape=[num_filter]), name="b") conv = tf.nn.conv2d(self.embedded_chars_expanded, W, strides=[1, 1, 1, 1], padding="VALID", name="conv") conv_ref = tf.nn.conv2d(self.embedded_chars_expanded_ref, W, strides=[1, 1, 1, 1], padding="VALID", name="conv_ref") # Apply nonlinearity h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu") h_ref = tf.nn.relu( tf.nn.bias_add(conv_ref, b, name="relu_ref")) # Maxpooling over the outputs pooled = tf.nn.max_pool( h, ksize=[1, sequence_length - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name="pool") pooled_ref = tf.nn.max_pool( h_ref, ksize=[1, sequence_length - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name="pool_ref") pooled_outputs.append(pooled) pooled_outputs_ref.append(pooled_ref) # Combine all the pooled features num_filters_total = sum(num_filters) self.h_pool = tf.concat(pooled_outputs, 3) self.h_pool_flat = tf.reshape(self.h_pool, [-1, num_filters_total]) self.h_pool_ref = tf.concat(pooled_outputs_ref, 3) self.h_pool_flat_ref = tf.reshape(self.h_pool_ref, [-1, num_filters_total]) # Add highway with tf.name_scope("highway"): self.h_highway = highway(self.h_pool_flat, self.h_pool_flat.get_shape()[1], 1, 0, scope="highway") self.h_highway_ref = highway( self.h_pool_flat_ref, self.h_pool_flat_ref.get_shape()[1], 1, 0, scope="highway") # Add dropout with tf.name_scope("dropout"): self.h_drop = tf.nn.dropout(self.h_highway, self.dropout_keep_prob) self.h_drop_ref = tf.nn.dropout(self.h_highway_ref, self.dropout_keep_prob) # Final (unnormalized) scores and predictions with tf.name_scope("output"): """ scores = tf.TensorArray(dtype=tf.float32, size=batch_size, dynamic_size=False, infer_shape=True) def rank_recurrence(i, scores): rank_score = get_rank_score(tf.nn.embedding_lookup(self.h_drop, i), self.h_drop_ref) scores = scores.write(i, rank_score) return i + 1, scores _, self.scores = control_flow_ops.while_loop( cond=lambda i, _1: i < batch_size, body=rank_recurrence, loop_vars=(tf.constant(0, dtype=tf.int32), scores) ) """ score = [] """ for i in range(batch_size): value = tf.constant(0.0, dtype=tf.float32) for j in range(reference_size): value += cosine_distance(tf.nn.embedding_lookup(self.h_drop, i), tf.nn.embedding_lookup(self.h_drop_ref, j)) score.append(value) self.scores = tf.stack(score) self.scores = tf.reshape(self.scores, [-1]) """ self.reference = tf.reduce_mean(tf.nn.l2_normalize( self.h_drop_ref, axis=-1), axis=0, keep_dims=True) self.feature = tf.nn.l2_normalize(self.h_drop, axis=-1) self.scores = tf.reshape( self.feature @ tf.transpose(self.reference, perm=[1, 0]), [-1]) self.ypred_for_auc = tf.reshape(tf.nn.softmax(self.scores), [-1]) self.log_score = tf.log(self.ypred_for_auc) # CalculateMean cross-entropy loss with tf.name_scope("loss"): self.neg_vec = tf.nn.embedding_lookup( tf.transpose(self.input_y), 1) self.pos_vec = tf.nn.embedding_lookup( tf.transpose(self.input_y), 0) losses_minus = self.log_score * self.neg_vec losses_posit = self.log_score * self.pos_vec self.loss = (-tf.reduce_sum(losses_minus) / tf.maximum(tf.reduce_sum(self.neg_vec), 1e-5) + tf.reduce_sum(losses_posit) / tf.maximum(tf.reduce_sum(self.pos_vec), 1e-5) ) / reference_size self.params = [ param for param in tf.trainable_variables() if 'discriminator' in param.name ] d_optimizer = tf.train.AdamOptimizer(1e-4) grads_and_vars = d_optimizer.compute_gradients(self.loss, self.params, aggregation_method=2) self.train_op = d_optimizer.apply_gradients(grads_and_vars) from ...models.rankgan import SAVING_PATH SavableModel.__init__(self, self.params, SAVING_PATH, 'discriminator')
def avg_rec(y_true, y_pred): mask_shape = tf.shape(y_true)[:4] cell_x = tf.to_float( tf.reshape(tf.tile(tf.range(GRID_W), [GRID_H]), (1, GRID_H, GRID_W, 1, 1))) cell_y = tf.transpose(cell_x, (0, 2, 1, 3, 4)) cell_grid = tf.tile(tf.concat([cell_x, cell_y], -1), [BATCH_SIZE, 1, 1, 5, 1]) coord_mask = tf.zeros(mask_shape) conf_mask = tf.zeros(mask_shape) class_mask = tf.zeros(mask_shape) seen = tf.Variable(0.) total_recall = tf.Variable(0.) """ Adjust prediction """ ### adjust x and y pred_box_xy = tf.sigmoid(y_pred[..., :2]) + cell_grid ### adjust w and h pred_box_wh = tf.exp(y_pred[..., 2:4]) * np.reshape( ANCHORS, [1, 1, 1, BOX, 2]) ### adjust confidence pred_box_conf = tf.sigmoid(y_pred[..., 4]) ### adjust class probabilities pred_box_class = y_pred[..., 5:] """ Adjust ground truth """ ### adjust x and y true_box_xy = y_true[..., 0:2] # relative position to the containing cell ### adjust w and h true_box_wh = y_true[ ..., 2:4] # number of cells accross, horizontally and vertically ### adjust confidence true_wh_half = true_box_wh / 2. true_mins = true_box_xy - true_wh_half true_maxes = true_box_xy + true_wh_half pred_wh_half = pred_box_wh / 2. pred_mins = pred_box_xy - pred_wh_half pred_maxes = pred_box_xy + pred_wh_half intersect_mins = tf.maximum(pred_mins, true_mins) intersect_maxes = tf.minimum(pred_maxes, true_maxes) intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] true_areas = true_box_wh[..., 0] * true_box_wh[..., 1] pred_areas = pred_box_wh[..., 0] * pred_box_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = tf.truediv(intersect_areas, union_areas) true_box_conf = iou_scores * y_true[..., 4] ### adjust class probabilities true_box_class = tf.argmax(y_true[..., 5:], -1) """ Determine the masks """ ### coordinate mask: simply the position of the ground truth boxes (the predictors) coord_mask = tf.expand_dims(y_true[..., 4], axis=-1) * COORD_SCALE ### confidence mask: penelize predictors + penalize boxes with low IOU # penalize the confidence of the boxes, which have IOU with some ground truth box < 0.6 true_xy = true_boxes[..., 0:2] true_wh = true_boxes[..., 2:4] true_wh_half = true_wh / 2. true_mins = true_xy - true_wh_half true_maxes = true_xy + true_wh_half pred_xy = tf.expand_dims(pred_box_xy, 4) pred_wh = tf.expand_dims(pred_box_wh, 4) pred_wh_half = pred_wh / 2. pred_mins = pred_xy - pred_wh_half pred_maxes = pred_xy + pred_wh_half intersect_mins = tf.maximum(pred_mins, true_mins) intersect_maxes = tf.minimum(pred_maxes, true_maxes) intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] true_areas = true_wh[..., 0] * true_wh[..., 1] pred_areas = pred_wh[..., 0] * pred_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = tf.truediv(intersect_areas, union_areas) best_ious = tf.reduce_max(iou_scores, axis=4) conf_mask = conf_mask + tf.to_float( best_ious < 0.6) * (1 - y_true[..., 4]) * NO_OBJECT_SCALE # penalize the confidence of the boxes, which are reponsible for corresponding ground truth box conf_mask = conf_mask + y_true[..., 4] * OBJECT_SCALE ### class mask: simply the position of the ground truth boxes (the predictors) class_mask = y_true[..., 4] * tf.gather(CLASS_WEIGHTS, true_box_class) * CLASS_SCALE """ Warm-up training """ no_boxes_mask = tf.to_float(coord_mask < COORD_SCALE / 2.) seen = tf.assign_add(seen, 1.) true_box_xy, true_box_wh, coord_mask = tf.cond( tf.less(seen, WARM_UP_BATCHES), lambda: [ true_box_xy + (0.5 + cell_grid) * no_boxes_mask, true_box_wh + tf.ones_like(true_box_wh) * np.reshape( ANCHORS, [1, 1, 1, BOX, 2]) * no_boxes_mask, tf.ones_like(coord_mask) ], lambda: [true_box_xy, true_box_wh, coord_mask]) """ Finalize the loss """ nb_coord_box = tf.reduce_sum(tf.to_float(coord_mask > 0.0)) nb_conf_box = tf.reduce_sum(tf.to_float(conf_mask > 0.0)) nb_class_box = tf.reduce_sum(tf.to_float(class_mask > 0.0)) loss_xy = tf.reduce_sum(tf.square(true_box_xy - pred_box_xy) * coord_mask) / (nb_coord_box + 1e-6) / 2. loss_wh = tf.reduce_sum(tf.square(true_box_wh - pred_box_wh) * coord_mask) / (nb_coord_box + 1e-6) / 2. loss_conf = tf.reduce_sum( tf.square(true_box_conf - pred_box_conf) * conf_mask) / (nb_conf_box + 1e-6) / 2. loss_class = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=true_box_class, logits=pred_box_class) loss_class = tf.reduce_sum(loss_class * class_mask) / (nb_class_box + 1e-6) loss = loss_xy + loss_wh + loss_conf + loss_class nb_true_box = tf.reduce_sum(y_true[..., 4]) nb_pred_box = tf.reduce_sum( tf.to_float(true_box_conf > 0.5) * tf.to_float(pred_box_conf > 0.3)) """ Debugging code """ current_recall = nb_pred_box / (nb_true_box + 1e-6) total_recall = tf.assign_add(total_recall, current_recall) return total_recall / seen
def maximum(x, y): '''Element-wise maximum of two tensors. ''' return tf.maximum(x, y)
def get_iou(boxes1, boxes2, iou_type='iou'): """ 计算IoU 注意这里yx是反的,对应维度位置 Args: boxes1: [..., [y_min, x_min, y_max, x_max]] boxes2: [..., [y_min, x_min, y_max, x_max]] iou_type: ['iou', 'ciou', 'diou', 'giou']其中一个 Returns: IoU: [...,], 会将boxes1与boxes2相同维度不变,把各自为1的维度扩展到其中的最大值。 """ # t_ denotes target boxes and p_ denotes predicted boxes. b1_ymin = boxes1[..., 0] b1_xmin = boxes1[..., 1] b1_ymax = boxes1[..., 2] b1_xmax = boxes1[..., 3] b2_ymin = boxes2[..., 0] b2_xmin = boxes2[..., 1] b2_ymax = boxes2[..., 2] b2_xmax = boxes2[..., 3] zero = tf.zeros_like(b1_xmin, b1_xmin.dtype) b1_width = tf.maximum(zero, b1_xmax - b1_xmin) b1_height = tf.maximum(zero, b1_ymax - b1_ymin) b2_width = tf.maximum(zero, b2_xmax - b2_xmin) b2_height = tf.maximum(zero, b2_ymax - b2_ymin) b1_area = b1_width * b1_height b2_area = b2_width * b2_height intersect_ymin = tf.maximum(b1_ymin, b2_ymin) intersect_xmin = tf.maximum(b1_xmin, b2_xmin) intersect_ymax = tf.minimum(b1_ymax, b2_ymax) intersect_xmax = tf.minimum(b1_xmax, b2_xmax) intersect_width = tf.maximum(zero, intersect_xmax - intersect_xmin) intersect_height = tf.maximum(zero, intersect_ymax - intersect_ymin) intersect_area = intersect_width * intersect_height union_area = b1_area + b2_area - intersect_area iou_v = tf.math.divide_no_nan(intersect_area, union_area) if iou_type == 'iou': return iou_v # iou is the simplest form. enclose_ymin = tf.minimum(b1_ymin, b2_ymin) enclose_xmin = tf.minimum(b1_xmin, b2_xmin) enclose_ymax = tf.maximum(b1_ymax, b2_ymax) enclose_xmax = tf.maximum(b1_xmax, b2_xmax) assert iou_type in ('giou', 'diou', 'ciou') if iou_type == 'giou': # giou is the generalized iou. enclose_width = tf.maximum(zero, enclose_xmax - enclose_xmin) enclose_height = tf.maximum(zero, enclose_ymax - enclose_ymin) enclose_area = enclose_width * enclose_height giou_v = iou_v - tf.math.divide_no_nan( (enclose_area - union_area), enclose_area) return giou_v assert iou_type in ('diou', 'ciou') b1_center = tf.stack([(b1_ymin + b1_ymax) / 2, (b1_xmin + b1_xmax) / 2], axis=-1) b2_center = tf.stack([(b2_ymin + b2_ymax) / 2, (b2_xmin + b2_xmax) / 2], axis=-1) euclidean = tf.linalg.norm(b2_center - b1_center, axis=-1) diag_length = tf.linalg.norm(tf.stack( [enclose_ymax - enclose_ymin, enclose_xmax - enclose_xmin], axis=-1), axis=-1) diou_v = iou_v - tf.math.divide_no_nan(euclidean**2, diag_length**2) if iou_type == 'diou': # diou is the distance iou. return diou_v assert iou_type == 'ciou' v = _get_v(b1_height, b1_width, b2_height, b2_width) alpha = tf.math.divide_no_nan(v, ((1 - iou_v) + v)) return diou_v - alpha * v # the last one is ciou.
def take_step(self, i, prev, state): if self.output_fn is not None: #[batch_size * beam_size, num_units] -> [batch_size * beam_size, num_classes] try: output = self.output_fn(prev) except Exception: output = self.output_fn(prev, state) else: output = prev self.output = output #[batch_size * beam_size, num_classes], here use log sofmax if self.need_softmax: logprobs = tf.nn.log_softmax(output) else: logprobs = tf.log(tf.maximum(output, 1e-12)) if self.num_classes is None: self.num_classes = tf.shape(logprobs)[1] need_logprobs_history = self.need_logprobs_history #->[batch_size, beam_size, num_classes] logprobs_batched = tf.reshape(logprobs, [-1, self.beam_size, self.num_classes]) logprobs_batched.set_shape((None, self.beam_size, None)) # Note: masking out entries to -inf plays poorly with top_k, so just subtract out a large number. nondone_mask = tf.reshape( tf.cast(tf.equal(tf.range(self.num_classes), self.done_token), tf.float32) * -1e18, [1, 1, self.num_classes]) if self.past_logprobs is None: #[batch_size, beam_size, num_classes] -> [batch_size, num_classes] #-> past_logprobs[batch_size, beam_size], indices[batch_size, beam_size] self.past_logprobs, indices = tf.nn.top_k( (logprobs_batched + nondone_mask)[:, 0, :], self.beam_size) if need_logprobs_history: step_logprobs = self.past_logprobs else: #logprobs_batched [batch_size, beam_size, num_classes] -> [batch_size, beam_size, num_classes] #past_logprobs [batch_size, beam_size] -> [batch_size, beam_size, 1] if need_logprobs_history: step_logprobs_batched = logprobs_batched logprobs_batched = logprobs_batched + tf.expand_dims( self.past_logprobs, 2) #get [batch_size, beam_size] each self.past_logprobs, indices = tf.nn.top_k( #[batch_size, beam_size * num_classes] tf.reshape(logprobs_batched + nondone_mask, [-1, self.beam_size * self.num_classes]), self.beam_size) if need_logprobs_history: #get current step logprobs [batch_size, beam_size] step_logprobs = tf.gather_nd( tf.reshape(step_logprobs_batched, [-1, self.beam_size * self.num_classes]), melt.to_nd_indices(indices)) # For continuing to the next symbols [batch_size, beam_size] symbols = indices % self.num_classes #from wich beam it comes [batch_size, beam_size] parent_refs = indices // self.num_classes # NOTE: outputing a zero-length sequence is not supported for simplicity reasons #hasky/jupter/tensorflow/beam-search2.ipynb below for mergeing path #here when i >= 2 # tf.reshape( # (tf.range(3 * 5) // 5) * 5, # [3, 5] # ).eval() # array([[ 0, 0, 0, 0, 0], # [ 5, 5, 5, 5, 5], # [10, 10, 10, 10, 10]], dtype=int32) parent_refs_offsets = tf.reshape( (tf.range(self.batch_size * self.beam_size) // self.beam_size) * self.beam_size, [self.batch_size, self.beam_size]) #[batch_size, beam_size] past_indices = parent_refs + parent_refs_offsets #[batch_size * beam_size] flattened_past_indices = tf.reshape(past_indices, [-1]) need_alignments = self.need_alignment_history and hasattr( state, 'alignments') if need_alignments: ori_alignments = tf.reshape(state.alignments, [self.batch_size, self.beam_size, -1]) #TODO might use nest.map_structure also #[batch_size, beam_size, attention_size] <- ([batch_size * beam_size, attention_size], [batch_size, beam_size]) alignments = tf.gather(state.alignments, past_indices) #TODO not support tf.TensorArray right now, can not use alignment_history in attention_wrapper def try_gather(x, indices): #if isinstance(x, tf.Tensor) and x.shape.ndims >= 2: assert isinstance(x, tf.Tensor) if x.shape.ndims >= 2: return tf.gather(x, indices) else: return x #must reorder state! including attention state = nest.map_structure( lambda x: try_gather(x, flattened_past_indices), state) if self.past_symbols is None: #here when i == 1, when i==0 will not do take step it just do one rnn() get output and use it for i==1 here #here will not need to gather state for inital state of each beam is the same #[batch_size, beam_size] -> [batch_size, beam_size, 1] self.past_symbols = tf.expand_dims(symbols, 2) if need_logprobs_history: self.past_step_logprobs = tf.expand_dims(step_logprobs, 2) if need_alignments: #[batch_size, beam_size, 1, attention_size] self.past_alignments = tf.expand_dims(alignments, 2) else: #self.past_symbols [batch_size, beam_size, i - 1] -> past_symbols_batch_major [batch_size * beam_size, i - 1] past_symbols_batch_major = tf.reshape(self.past_symbols, [-1, i - 1]) if need_logprobs_history: past_step_logprobs_batch_major = tf.reshape( self.past_step_logprobs, [-1, i - 1]) if need_alignments: past_alignments = tf.reshape( self.past_alignments, [self.batch_size * self.beam_size, i - 1, -1]) #[batch_size, beam_size, i - 1] <- ([batch_size * beam_size, i - 1], [batch_size, beam_size]) beam_past_symbols = tf.gather(past_symbols_batch_major, past_indices) if need_logprobs_history: beam_past_step_logprobs = tf.gather( past_step_logprobs_batch_major, past_indices) if need_alignments: beam_past_alignments = tf.gather(past_alignments, past_indices) if not self.fast_greedy: #[batch_size, beam_size, max_len] path = tf.concat([ self.past_symbols, tf.ones_like(tf.expand_dims(symbols, 2)) * self.done_token, tf.tile( tf.ones_like(tf.expand_dims(symbols, 2)) * self.pad_token, [1, 1, self.max_len - i]) ], 2) if need_logprobs_history: step_logprobs_path = tf.concat([ self.past_step_logprobs, tf.expand_dims( step_logprobs_batched[:, :, self.done_token], 2), tf.tile( tf.ones_like(tf.expand_dims(step_logprobs, 2)) * -float('inf'), [1, 1, self.max_len - i]) ], 2) if need_alignments: #[batch_size, beam_size, max_len, attention_size] #NOTICE this is not correct for done token alignments(but currently best choice tokens state) need to modify the step before alignments_path = tf.concat([ self.past_alignments, tf.expand_dims(ori_alignments, 2), tf.tile(tf.zeros_like(tf.expand_dims(alignments, 2)), [1, 1, self.max_len - i, 1]) ], 2) #[batch_size, 1, beam_size, max_len] path = tf.expand_dims(path, 1) self.path_list.append(path) if need_logprobs_history: step_logprobs_path = tf.expand_dims(step_logprobs_path, 1) self.step_logprobs_list.append(step_logprobs_path) if need_alignments: #[batch_size, 1, beam_size, max_len, attention_size] alignments_path = tf.expand_dims(alignments_path, 1) self.alignments_path_list.append(alignments_path) #[batch_size * beam_size, i - 1] -> [batch_size, beam_size, i] the best beam_size paths until step i self.past_symbols = tf.concat( [beam_past_symbols, tf.expand_dims(symbols, 2)], 2) if need_logprobs_history: self.past_step_logprobs = tf.concat([ beam_past_step_logprobs, tf.expand_dims(step_logprobs, 2) ], 2) if need_alignments: self.past_alignments = tf.concat( [beam_past_alignments, tf.expand_dims(alignments, 2)], 2) # For finishing the beam #[batch_size, beam_size] logprobs_done = logprobs_batched[:, :, self.done_token] if not self.fast_greedy: self.logprobs_list.append(logprobs_done / i**self.length_normalization_factor) else: done_parent_refs = tf.cast(tf.argmax(logprobs_done, 1), tf.int32) done_parent_refs_offsets = tf.range( self.batch_size) * self.beam_size done_past_symbols = tf.gather( past_symbols_batch_major, done_parent_refs + done_parent_refs_offsets) #[batch_size, max_len] symbols_done = tf.concat([ done_past_symbols, tf.ones_like(done_past_symbols[:, 0:1]) * self.done_token, tf.tile(tf.zeros_like(done_past_symbols[:, 0:1]), [1, self.max_len - i]) ], 1) #[batch_size, beam_size] -> [batch_size,] logprobs_done_max = tf.reduce_max(logprobs_done, 1) if self.length_normalization_factor > 0: logprobs_done_max /= i**self.length_normalization_factor #[batch_size, max_len] self.finished_beams = tf.where( logprobs_done_max > self.logprobs_finished_beams, symbols_done, self.finished_beams) self.logprobs_finished_beams = tf.maximum( logprobs_done_max, self.logprobs_finished_beams) #->[batch_size * beam_size,] symbols_flat = tf.reshape(symbols, [-1]) self.final_state = state return symbols_flat, state
def leaky_relu(x, alpha=0.2): return tf.maximum(x, alpha * x)
def leaky_relu(x, alpha=0.1): return tf.maximum(tf.minimum(0.0, alpha * x), x) # this is equivalent to: x \leq 0: \alpha x, x > 0: x, here \alpha \leq 1
def custom_loss(self, y_true, y_pred): mask_shape = tf.shape(y_true)[:4] cell_x = tf.to_float(tf.reshape(tf.tile(tf.range(self.grid_w), [self.grid_h]), (1, self.grid_h, self.grid_w, 1, 1))) cell_y = tf.transpose(cell_x, (0,2,1,3,4)) cell_grid = tf.tile(tf.concat([cell_x,cell_y], -1), [self.batch_size, 1, 1, self.nb_box, 1]) coord_mask = tf.zeros(mask_shape) conf_mask = tf.zeros(mask_shape) class_mask = tf.zeros(mask_shape) seen = tf.Variable(0.) total_recall = tf.Variable(0.) """ Adjust prediction """ ### adjust x and y pred_box_xy = tf.sigmoid(y_pred[..., :2]) + cell_grid ### adjust w and h pred_box_wh = tf.exp(y_pred[..., 2:4]) * np.reshape(self.anchors, [1,1,1,self.nb_box,2]) ### adjust confidence pred_box_conf = tf.sigmoid(y_pred[..., 4]) ### adjust class probabilities pred_box_class = y_pred[..., 5:] """ Adjust ground truth """ ### adjust x and y true_box_xy = y_true[..., 0:2] # relative position to the containing cell ### adjust w and h true_box_wh = y_true[..., 2:4] # number of cells accross, horizontally and vertically ### adjust confidence true_wh_half = true_box_wh / 2. true_mins = true_box_xy - true_wh_half true_maxes = true_box_xy + true_wh_half pred_wh_half = pred_box_wh / 2. pred_mins = pred_box_xy - pred_wh_half pred_maxes = pred_box_xy + pred_wh_half intersect_mins = tf.maximum(pred_mins, true_mins) intersect_maxes = tf.minimum(pred_maxes, true_maxes) intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] true_areas = true_box_wh[..., 0] * true_box_wh[..., 1] pred_areas = pred_box_wh[..., 0] * pred_box_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = tf.truediv(intersect_areas, union_areas) true_box_conf = iou_scores * y_true[..., 4] ### adjust class probabilities true_box_class = tf.argmax(y_true[..., 5:], -1) """ Determine the masks """ ### coordinate mask: simply the position of the ground truth boxes (the predictors) coord_mask = tf.expand_dims(y_true[..., 4], axis=-1) * self.coord_scale ### confidence mask: penelize predictors + penalize boxes with low IOU # penalize the confidence of the boxes, which have IOU with some ground truth box < 0.6 true_xy = self.true_boxes[..., 0:2] true_wh = self.true_boxes[..., 2:4] true_wh_half = true_wh / 2. true_mins = true_xy - true_wh_half true_maxes = true_xy + true_wh_half pred_xy = tf.expand_dims(pred_box_xy, 4) pred_wh = tf.expand_dims(pred_box_wh, 4) pred_wh_half = pred_wh / 2. pred_mins = pred_xy - pred_wh_half pred_maxes = pred_xy + pred_wh_half intersect_mins = tf.maximum(pred_mins, true_mins) intersect_maxes = tf.minimum(pred_maxes, true_maxes) intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] true_areas = true_wh[..., 0] * true_wh[..., 1] pred_areas = pred_wh[..., 0] * pred_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = tf.truediv(intersect_areas, union_areas) best_ious = tf.reduce_max(iou_scores, axis=4) conf_mask = conf_mask + tf.to_float(best_ious < 0.6) * (1 - y_true[..., 4]) * self.no_object_scale # penalize the confidence of the boxes, which are reponsible for corresponding ground truth box conf_mask = conf_mask + y_true[..., 4] * self.object_scale ### class mask: simply the position of the ground truth boxes (the predictors) class_mask = y_true[..., 4] * tf.gather(self.class_wt, true_box_class) * self.class_scale """ Warm-up training """ no_boxes_mask = tf.to_float(coord_mask < self.coord_scale/2.) seen = tf.assign_add(seen, 1.) true_box_xy, true_box_wh, coord_mask = tf.cond(tf.less(seen, self.warmup_batches+1), lambda: [true_box_xy + (0.5 + cell_grid) * no_boxes_mask, true_box_wh + tf.ones_like(true_box_wh) * \ np.reshape(self.anchors, [1,1,1,self.nb_box,2]) * \ no_boxes_mask, tf.ones_like(coord_mask)], lambda: [true_box_xy, true_box_wh, coord_mask]) """ Finalize the loss """ nb_coord_box = tf.reduce_sum(tf.to_float(coord_mask > 0.0)) nb_conf_box = tf.reduce_sum(tf.to_float(conf_mask > 0.0)) nb_class_box = tf.reduce_sum(tf.to_float(class_mask > 0.0)) loss_xy = tf.reduce_sum(tf.square(true_box_xy-pred_box_xy) * coord_mask) / (nb_coord_box + 1e-6) / 2. loss_wh = tf.reduce_sum(tf.square(true_box_wh-pred_box_wh) * coord_mask) / (nb_coord_box + 1e-6) / 2. loss_conf = tf.reduce_sum(tf.square(true_box_conf-pred_box_conf) * conf_mask) / (nb_conf_box + 1e-6) / 2. loss_class = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=true_box_class, logits=pred_box_class) loss_class = tf.reduce_sum(loss_class * class_mask) / (nb_class_box + 1e-6) loss = tf.cond(tf.less(seen, self.warmup_batches+1), lambda: loss_xy + loss_wh + loss_conf + loss_class + 10, lambda: loss_xy + loss_wh + loss_conf + loss_class) if self.debug: nb_true_box = tf.reduce_sum(y_true[..., 4]) nb_pred_box = tf.reduce_sum(tf.to_float(true_box_conf > 0.5) * tf.to_float(pred_box_conf > 0.3)) current_recall = nb_pred_box/(nb_true_box + 1e-6) total_recall = tf.assign_add(total_recall, current_recall) loss = tf.Print(loss, [loss_xy], message='Loss XY \t', summarize=1000) loss = tf.Print(loss, [loss_wh], message='Loss WH \t', summarize=1000) loss = tf.Print(loss, [loss_conf], message='Loss Conf \t', summarize=1000) loss = tf.Print(loss, [loss_class], message='Loss Class \t', summarize=1000) loss = tf.Print(loss, [loss], message='Total Loss \t', summarize=1000) loss = tf.Print(loss, [current_recall], message='Current Recall \t', summarize=1000) loss = tf.Print(loss, [total_recall/seen], message='Average Recall \t', summarize=1000) return loss
def __init__(self, *, policy, nbatch_act, nbatch_train, nsteps, ent_coef, vf_coef, max_grad_norm, microbatch_size=None, np_mask=None): self.sess = sess = get_session() with tf.variable_scope('ppo2_model', reuse=tf.AUTO_REUSE): # CREATE OUR TWO MODELS # act_model that is used for sampling act_model = policy(nbatch_act, 1, sess, np_mask=np_mask, is_act_model=True) # Train model for training if microbatch_size is None: train_model = policy(nbatch_train, nsteps, sess, np_mask=np_mask, is_act_model=False) else: train_model = policy(microbatch_size, nsteps, sess, np_mask=np_mask, is_act_model=False) # CREATE THE PLACEHOLDERS self.A = A = train_model.pdtype.sample_placeholder([None]) self.ADV = ADV = tf.placeholder(tf.float32, [None]) self.R = R = tf.placeholder(tf.float32, [None]) # Keep track of old actor self.OLDNEGLOGPAC = OLDNEGLOGPAC = tf.placeholder(tf.float32, [None]) # Keep track of old critic self.OLDVPRED = OLDVPRED = tf.placeholder(tf.float32, [None]) self.LR = LR = tf.placeholder(tf.float32, []) # Cliprange self.CLIPRANGE = CLIPRANGE = tf.placeholder(tf.float32, []) neglogpac = train_model.pd.neglogp(A) # Calculate the entropy # Entropy is used to improve exploration by limiting the premature convergence to suboptimal policy. entropy = tf.reduce_mean(train_model.pd.entropy()) # CALCULATE THE LOSS # Total loss = Policy gradient loss - entropy * entropy coefficient + Value coefficient * value loss # Clip the value to reduce variability during Critic training # Get the predicted value vpred = train_model.vf vpredclipped = OLDVPRED + tf.clip_by_value(train_model.vf - OLDVPRED, -CLIPRANGE, CLIPRANGE) # Unclipped value vf_losses1 = tf.square(vpred - R) # Clipped value vf_losses2 = tf.square(vpredclipped - R) vf_loss = .5 * tf.reduce_mean(tf.maximum(vf_losses1, vf_losses2)) # Calculate ratio (pi current policy / pi old policy) ratio = tf.exp(OLDNEGLOGPAC - neglogpac) # Defining Loss = - J is equivalent to max J pg_losses = -ADV * ratio pg_losses2 = -ADV * tf.clip_by_value(ratio, 1.0 - CLIPRANGE, 1.0 + CLIPRANGE) # Final PG loss pg_loss = tf.reduce_mean(tf.maximum(pg_losses, pg_losses2)) approxkl = .5 * tf.reduce_mean(tf.square(neglogpac - OLDNEGLOGPAC)) clipfrac = tf.reduce_mean( tf.to_float(tf.greater(tf.abs(ratio - 1.0), CLIPRANGE))) # Total loss loss = pg_loss - entropy * ent_coef + vf_loss * vf_coef # UPDATE THE PARAMETERS USING LOSS # 1. Get the model parameters params = tf.trainable_variables('ppo2_model') # 2. Build our trainer self.trainer = tf.train.AdamOptimizer(learning_rate=LR, epsilon=1e-5) # 3. Calculate the gradients grads_and_var = self.trainer.compute_gradients(loss, params) grads, var = zip(*grads_and_var) if max_grad_norm is not None: # Clip the gradients (normalize) grads, _grad_norm = tf.clip_by_global_norm(grads, max_grad_norm) grads_and_var = list(zip(grads, var)) # zip aggregate each gradient with parameters associated # For instance zip(ABCD, xyza) => Ax, By, Cz, Da self.grads = grads self.var = var self._train_op = self.trainer.apply_gradients(grads_and_var) self.loss_names = [ 'policy_loss', 'value_loss', 'policy_entropy', 'approxkl', 'clipfrac' ] self.stats_list = [pg_loss, vf_loss, entropy, approxkl, clipfrac] self.train_model = train_model self.act_model = act_model self.step = act_model.step self.value = act_model.value self.initial_state = act_model.initial_state initialize()
def _mel_to_linear_tensorflow(mel_spectrogram, hparams): global _inv_mel_basis if _inv_mel_basis is None: _inv_mel_basis = np.linalg.pinv(_build_mel_basis(hparams)) return tf.transpose(tf.maximum(1e-10, tf.matmul(tf.cast(_inv_mel_basis, tf.float32), tf.transpose(mel_spectrogram, [1, 0]))), [1, 0])
def cvar_loss_function(): alpha = self.__config['cvar_constrains']['alpha'] omega = -tf.log(self.pv_vector) gamma = self.__config['cvar_constrains']['gamma'] return tf.reduce_mean(omega + self.lamda * (self.c_value + tf.maximum((1/(1-alpha)) * (omega - self.c_value), 0) - gamma))
def __init__(self, config, device, loader, mode): self.config = config self.mode = mode if mode == "Train": self.is_training = False self.batch_size = self.config.train_batch_size self.maxstep_size = self.config.train_step_size reuse = None elif mode == "Valid": self.is_training = False self.batch_size = self.config.valid_batch_size self.maxstep_size = self.config.valid_step_size reuse = True else: self.is_training = False self.batch_size = self.config.test_batch_size self.maxstep_size = self.config.test_step_size reuse = True self.hidden_size = hidden_size = config.hidden_size self.GNN_step = GNN_step = config.GNN_step # self.learning_rate = learning_rate = config.learning_rate opt = config.sgd_opt beta = config.beta batch_size = self.batch_size hidden_stdv = np.sqrt(1. / (hidden_size)) # embedding initial with tf.device(device), tf.name_scope(mode), tf.variable_scope( "gnn", reuse=reuse): feature_embedding_list = [] for idx, each_feature_num in enumerate(config.feature_num): feature_embedding_list.append( tf.get_variable( name='feature_embedding_' + str(idx), shape=[each_feature_num, hidden_size], initializer=tf.random_normal_initializer(hidden_stdv))) w_attention = tf.get_variable( name='w_attetion', shape=[ hidden_size * len(config.feature_num), len(config.feature_num) ], initializer=tf.random_normal_initializer(stddev=0.2)) w_score2 = tf.get_variable( name='w_score_2', shape=[hidden_size, 1], initializer=tf.random_normal_initializer(hidden_stdv)) # #------------feed-----------------## self.input_x = input_x = tf.placeholder( tf.int32, [batch_size, len(config.feature_num)]) self.input_y = input_y = tf.placeholder(tf.int32, [batch_size, 1]) # # # #--------init graph---------## # self.graph = graph = init_graph(config, loader) # # input_x = input_x.transpose((1, 0)) input_x_unstack = tf.unstack(tf.transpose(input_x, (1, 0)), axis=0) feature_embedding_input = [] # translate into embedding (lookup) for idx in range(len(input_x_unstack)): feature_embedding_input.append( tf.nn.embedding_lookup(feature_embedding_list[idx], input_x_unstack[idx])) self.feature_input = tf.transpose( tf.stack(feature_embedding_input, axis=0), (1, 0, 2)) with tf.device(device), tf.name_scope(mode), tf.variable_scope( "gnn", reuse=reuse): self.w_A = self.weights('a_value', hidden_size, 0) self.b_A = self.biases('a_value', hidden_size, 0) graph = self.init_graph(config, self.feature_input) final_state, test1 = self.GNN( self.feature_input, batch_size, hidden_size, GNN_step, len(config.feature_num), graph) # output: [batch_size, config.feature_num, hiddensize] atten_pos = self.attention_layer(final_state, w_attention, batch_size, hidden_size, len(config.feature_num)) score_pos = tf.matmul(tf.reshape(final_state, [-1, hidden_size]), w_score2) score_pos = tf.maximum(0.01 * score_pos, score_pos) score_pos = tf.reshape( score_pos, [batch_size, len(config.feature_num)]) s_pos = tf.reshape(tf.reduce_sum(score_pos * atten_pos, axis=1), [batch_size, 1]) # s_pos = self.dense_layer(final_state, batch_size, hidden_size, len(config.feature_num)) # s_pos = tf.reshape(s_pos, [batch_size, 1]) self.predict = predict = tf.sigmoid(s_pos) # -------------evaluation-------------- self.auc_result, self.auc_opt = tf.metrics.auc(labels=self.input_y, predictions=predict) self.s_pos = s_pos # -------------cost --------------- cost_parameter = 0. num_parameter = 0. # for variable in tf.trainable_variables(): # print (variable) # cost_parameter += tf.contrib.layers.l2_regularizer(beta)(variable) # num_parameter += 1. # cost_parameter /= num_parameter # score = tf.nn.sigmoid(s_pos) # score_mean = tf.reduce_mean(score) score_mean = tf.losses.log_loss(labels=self.input_y, predictions=predict) self.cost = cost = score_mean + cost_parameter # ---------------optimizer---------------# self.no_opt = tf.no_op() self.learning_rate = tf.Variable(config.learning_rate, trainable=False) if mode == 'Train': self.auc_opt = tf.no_op() self.auc_result = tf.no_op() if opt == 'Adam': self.optimizer = tf.train.AdamOptimizer( self.learning_rate).minimize(cost) if opt == 'Momentum': self.optimizer = tf.train.MomentumOptimizer( self.learning_rate, 0.9).minimize(cost) if opt == 'RMSProp': self.optimizer = tf.train.RMSPropOptimizer( self.learning_rate).minimize(cost) if opt == 'Adadelta': self.optimizer = tf.train.AdadeltaOptimizer( self.learning_rate).minimize(cost) else: self.optimizer = tf.no_op()