def call(self, inputs, mask=None): steps_axis = 1 if mask is not None: mask = math_ops.cast(mask, K.floatx()) input_shape = inputs.shape.as_list() broadcast_shape = [-1, input_shape[steps_axis], 1] mask = array_ops.reshape(mask, broadcast_shape) inputs *= mask if self.pooling == 'mean': res = K.sum(inputs, axis=steps_axis) / math_ops.reduce_sum(mask, axis=steps_axis) elif self.pooling == 'sum': res = K.sum(inputs, axis=steps_axis) elif self.pooling == 'max': res = K.max(inputs, axis=steps_axis) elif self.pooling == 'min': res = K.min(inputs, axis=steps_axis) else: if self.pooling == 'mean': res = K.mean(inputs, axis=steps_axis) elif self.pooling == 'sum': res = K.sum(inputs, axis=steps_axis) elif self.pooling == 'max': res = K.max(inputs, axis=steps_axis) elif self.pooling == 'min': res = K.min(inputs, axis=steps_axis) if self.keepdims: return K.expand_dims(res, axis=1) else: return res
def aucMetric(true, pred): #We want strictly 1D arrays - cannot have (batch, 1), for instance true = (true - K.min(true)) / (K.max(true) - K.min(true)) pred = (pred - K.min(pred)) / (K.max(pred) - K.min(pred)) true = K.flatten(true) pred = K.flatten(pred) #total number of elements in this batch totalCount = K.shape(true)[0] #sorting the prediction values in descending order values, indices = tf.nn.top_k(pred, k=totalCount) #sorting the ground truth values based on the predictions above sortedTrue = K.gather(true, indices) #getting the ground negative elements (already sorted above) negatives = 1 - sortedTrue #the true positive count per threshold TPCurve = K.cumsum(sortedTrue) #area under the curve auc = K.sum(TPCurve * negatives) #normalizing the result between 0 and 1 totalCount = K.cast(totalCount, K.floatx()) positiveCount = K.sum(true) negativeCount = totalCount - positiveCount totalArea = positiveCount * negativeCount return auc / totalArea
def kld_loss(y_true, y_pred): y_pred -= K.min(y_pred) y_pred /= K.max(y_pred) y_true -= K.min(y_true) y_true /= K.max(y_true) y_true = K.clip(y_true, K.epsilon(), 1.0) y_pred = K.clip(y_pred, K.epsilon(), 1.0) return -K.sum( (y_true * K.log(y_true / y_pred)), axis=[1, 2, 3], keepdims=True)
def calc_area_loss(ear_true, ear_pred): left_x = K.expand_dims(K.min(ear_true[:, ::2], axis=-1)) left_y = K.expand_dims(K.min(ear_true[:, 1::2], axis=-1)) right_x = K.expand_dims(K.max(ear_true[:, ::2], axis=-1)) right_y = K.expand_dims(K.max(ear_true[:, 1::2], axis=-1)) # 予測のX,y pred_x = ear_pred[:, ::2] pred_y = ear_pred[:, 1::2] # ペナルティ penalty_x = K.maximum(left_x - pred_x, 0.0) + K.maximum(pred_x - right_x, 0.0) penalty_y = K.maximum(left_y - pred_y, 0.0) + K.maximum(pred_y - right_y, 0.0) return K.mean(penalty_x + penalty_y, axis=-1)
def compute_centerness_targets(reg_targets_flatten): left_right=K.concatenate( reg_targets_flatten[:,0].reshape(-1,1),reg_targets_flatten[:,2].reshape(-1,1),axis=-1 ) top_bottom=K.concatenate( reg_targets_flatten[:,1].reshape(-1,1),reg_targets_flatten[:,3].reshape(-1,1),axis=-1 ) centerness=( K.min(left_right,axis=-1)/K.max(left_right,axis=-1)*\ K.min(top_bottom,axis=-1)/K.max(top_bottom,axis=-1) ) centerness=K.sqrt(centerness) return centerness
def payoffv2_Eq_MSE(game, pureStrategies_perPlayer, nashEq_true, nashEq_pred, computePayoff_function, num_players): """ Function to compute the the mean square error of equilibria and the mean square error of payoffs resulted from the equilibria. This is not a loss function. It is used by other loss functions to compute their final loss values. """ # Compute payoffs given the equilibria payoff_true = computePayoff_function['computePayoff_2dBatch']( game, nashEq_true, pureStrategies_perPlayer, num_players) payoff_pred = computePayoff_function['computePayoff']( game, tf.gather(nashEq_pred, 0, axis=1), pureStrategies_perPlayer, num_players) payoff_pred = tf.tile(tf.expand_dims(payoff_pred, axis=1), [1, tf.shape(nashEq_true)[1], 1]) # Compute MSE of payoffs loss_payoff_MSE = K.mean( K.min(K.mean(K.square(payoff_true - payoff_pred), axis=2), axis=1)) # Compute MSE of equilibria loss_Eq_MSE = MSE(nashEq_true, nashEq_pred) return loss_Eq_MSE, loss_payoff_MSE
def yolo_correct_box(box_xy, box_wh, input_shape, image_shape): """ 校正预测框 :param box_xy: 同上box_xy :param box_wh: 同上box_wh :param input_shape: 输入图像尺寸 :param image_shape: 原图尺寸 :return: """ box_yx = box_xy[..., ::-1] box_hw = box_wh[..., ::-1] input_shape = K.cast(input_shape, K.dtype(box_yx)) # 强制转换格式 image_shape = K.cast(image_shape, K.dtype(box_yx)) # 强制转换格式 new_shape = K.round( image_shape * K.min(input_shape / image_shape)) # 求取输入/原图尺寸的最小值,四舍五入取整获得新图像的尺寸 offset = (input_shape - new_shape) / 2. / input_shape # 补偿值 scale = input_shape / new_shape # 收缩尺度 box_yx = (box_yx - offset) * scale box_hw *= scale box_mins = box_yx - (box_hw / 2.) box_maxs = box_yx + (box_hw / 2.) boxes = K.concatenate([ box_mins[..., 0:1], # y_min box_mins[..., 1:2], # x_min box_maxs[..., 0:1], # y_max box_maxs[..., 1:2] # x_max ]) # 将预测框缩放到原始图像形状 boxes *= K.concatenate([image_shape, image_shape]) return boxes
def yolo3_correct_boxes(box_xy, box_wh, input_shape, image_shape): '''Get corrected boxes''' input_shape = K.cast(input_shape, K.dtype(box_xy)) image_shape = K.cast(image_shape, K.dtype(box_xy)) #reshape the image_shape tensor to align with boxes dimension image_shape = K.reshape(image_shape, [-1, 1, 1, 1, 2]) new_shape = K.round(image_shape * K.min(input_shape/image_shape)) offset = (input_shape-new_shape)/2./input_shape scale = input_shape/new_shape box_xy = (box_xy - offset) * scale box_wh *= scale box_mins = box_xy - (box_wh / 2.) box_maxes = box_xy + (box_wh / 2.) boxes = K.concatenate([ box_mins[..., 0:1], # x_min box_mins[..., 1:2], # y_min box_maxes[..., 0:1], # x_max box_maxes[..., 1:2] # y_max ]) # Scale boxes back to original image shape. boxes *= K.concatenate([image_shape, image_shape]) return boxes
def yolo_correct_boxes(box_xy, box_wh, input_shape, image_shape): """ :param box_xy: (N, 13, 13, 3, 2) :param box_wh: (N, 13, 13, 3, 2) :param input_shape: (416, 416) :param image_shape: (None, None) :return: """ box_yx = box_xy[..., ::-1] box_hw = box_wh[..., ::-1] input_shape = K.cast(input_shape, K.dtype(box_yx)) image_shape = K.cast(image_shape, K.dtype(box_yx)) # (None, None) new_shape = K.round(image_shape * K.min(input_shape / image_shape)) offset = (input_shape - new_shape) / 2. / input_shape scale = input_shape / new_shape box_yx = (box_yx - offset) * scale box_hw *= scale box_mins = box_yx - (box_hw / 2.) box_maxes = box_yx + (box_hw / 2.) boxes = K.concatenate([ box_mins[..., 0:1], # y_min box_mins[..., 1:2], # x_min box_maxes[..., 0:1], # y_max box_maxes[..., 1:2] # x_max ]) # Scale boxes back to original image shape. boxes *= K.concatenate([image_shape, image_shape]) # (N, 13, 13, 3, 4) return boxes
def __init__(self, n_clusters, weights=None, alpha=1.0, symdec=False, **kwargs): if 'input_shape' not in kwargs and 'input_dim' in kwargs: kwargs['input_shape'] = (kwargs.pop('input_dim'), ) super(ClusteringLayer, self).__init__(**kwargs) self.n_clusters = n_clusters self.alpha = alpha self.initial_weights = weights self.input_spec = InputSpec(ndim=2) if symdec == False: self.q_fn = lambda inputs: 1.0 / (1.0 + (K.sum( K.square(K.expand_dims(inputs, axis=1) - self.clusters), axis=2) / self.alpha)) else: self.q_fn = lambda inputs: 1.0 / (1.0 + ((K.min(K.sqrt( K.sum(K.square( K.expand_dims( (2 * self.clusters) - K.expand_dims(inputs, axis=1), axis=1) - K.expand_dims(inputs, axis=1)), axis=-1)), axis=1)) / self. alpha))
def get_corrected_boxes(*, box_width, box_height, box_x, box_y, orig_image_shape, model_image_shape): orig_image_w, orig_image_h = orig_image_shape[0], orig_image_shape[1] model_w, model_h = model_image_shape[0], model_image_shape[1] scale = K.min( K.concatenate([(model_w / orig_image_w), (model_h / orig_image_h)])) w_without_padding = orig_image_w * scale h_without_padding = orig_image_h * scale x_shift = (model_w - w_without_padding) / 2.0 / model_w y_shift = (model_h - h_without_padding) / 2.0 / model_h box_x = (box_x - x_shift) / (w_without_padding / model_w) box_y = (box_y - y_shift) / (h_without_padding / model_h) box_width *= model_w / w_without_padding box_height *= model_h / h_without_padding left = (box_x - (box_width / 2.)) * orig_image_w right = (box_x + (box_width / 2.)) * orig_image_w top = (box_y - (box_height / 2.)) * orig_image_h bottom = (box_y + (box_height / 2.)) * orig_image_h output_boxes = K.concatenate([ K.reshape(left, [-1, 1]), K.reshape(top, [-1, 1]), K.reshape(right, [-1, 1]), K.reshape(bottom, [-1, 1]) ]) return output_boxes
def _init_cel(self, A_graph, b_graph, c_graph, y): # Sanity Checks y = tf.check_numerics(y, 'Problem with input y') # Find intersection points between Ax-b and the line joining the c and y Ac = tf.reduce_sum(A_graph * tf.expand_dims(c_graph, axis=-2), axis=-1) bMinusAc = b_graph - Ac yMinusc = y - c_graph ADotyMinusc = tf.reduce_sum((A_graph * tf.expand_dims(yMinusc, -2)), axis=-1) intersection_alphas = bMinusAc / (ADotyMinusc + K.epsilon()) # Enforce intersection_alpha > 0 because the point must lie on the ray from c to y less_equal_0 = K.less_equal(intersection_alphas, K.zeros_like(intersection_alphas)) candidate_alpha = K.switch( less_equal_0, K.ones_like(intersection_alphas) * tf.constant(np.inf, dtype='float32'), intersection_alphas) # Find closest the intersection point closest to the interior point to get projection point intersection_alpha = K.min(candidate_alpha, axis=-1, keepdims=True) # If it is an interior point, y itself is the projection point is_interior_point = K.greater_equal(intersection_alpha, K.ones_like(intersection_alpha)) alpha = K.switch(is_interior_point, K.ones_like(intersection_alpha), intersection_alpha) # Return z = \alpha.y + (1 - \alpha).c z = alpha * y + ((1 - alpha) * c_graph) return z
def AddMax(inputs): xmax = K.max(inputs, axis=1, keepdims=True) xmin = K.min(inputs, axis=1, keepdims=True) xlocmax = K.expand_dims(K.cast_to_floatx(K.argmax(inputs, axis=1))) xlocmin = K.expand_dims(K.cast_to_floatx(K.argmin(inputs, axis=1))) output = K.concatenate([inputs, xmax, xmin, xlocmax, xlocmin], axis=1) return output
def _process_channel(args): __kps, __hm_hp = args thresh = 0.1 __hm_scores, __hm_inds = tf.math.top_k(__hm_hp, k=k, sorted=True) __hm_xs = K.cast(__hm_inds % width, 'float32') __hm_ys = K.cast(K.cast(__hm_inds / width, 'int32'), 'float32') __hp_offset = K.gather(_hp_offset, __hm_inds) __hm_xs = __hm_xs + __hp_offset[..., 0] __hm_ys = __hm_ys + __hp_offset[..., 1] mask = K.cast(__hm_scores > thresh, 'float32') __hm_scores = (1. - mask) * -1. + mask * __hm_scores __hm_xs = (1. - mask) * -10000. + mask * __hm_xs __hm_ys = (1. - mask) * -10000. + mask * __hm_ys __hm_kps = K.stack([__hm_xs, __hm_ys], -1) # k x 2 __broadcast_hm_kps = K.expand_dims(__hm_kps, 1) # k x 1 x 2 __broadcast_kps = K.expand_dims(__kps, 0) # 1 x k x 2 dist = K.sqrt( K.sum(K.pow(__broadcast_kps - __broadcast_hm_kps, 2), 2)) # k, k min_dist = K.min(dist, 0) min_ind = K.argmin(dist, 0) __hm_scores = K.gather(__hm_scores, min_ind) __hm_kps = K.gather(__hm_kps, min_ind) mask = (K.cast(__hm_kps[..., 0] < _x1, 'float32') + K.cast(__hm_kps[..., 0] > _x2, 'float32') + K.cast(__hm_kps[..., 1] < _y1, 'float32') + K.cast(__hm_kps[..., 1] > _y2, 'float32') + K.cast(__hm_scores < thresh, 'float32') + K.cast( min_dist > 0.3 * (K.maximum(_wh[..., 0], _wh[..., 1])), 'float32')) mask = K.expand_dims(mask, -1) mask = K.cast(mask > 0, 'float32') __kps = (1. - mask) * __hm_kps + mask * __kps return __kps
def step(self, input_energy_t, states, return_logZ=True): # not in the following `prev_target_val` has shape = (B, F) # where B = batch_size, F = output feature dim # Note: `i` is of float32, due to the behavior of `K.rnn` prev_target_val, i, chain_energy = states[:3] t = K.cast(i[0, 0], dtype='int32') if len(states) > 3: if K.backend() == 'theano': m = states[3][:, t:(t + 2)] else: m = K.slice(states[3], [0, t], [-1, 2]) input_energy_t = input_energy_t * K.expand_dims(m[:, 0]) # (1, F, F)*(B, 1, 1) -> (B, F, F) chain_energy = chain_energy * K.expand_dims( K.expand_dims(m[:, 0] * m[:, 1])) if return_logZ: # shapes: (1, B, F) + (B, F, 1) -> (B, F, F) energy = chain_energy + K.expand_dims( input_energy_t - prev_target_val, 2) new_target_val = K.logsumexp(-energy, 1) # shapes: (B, F) return new_target_val, [new_target_val, i + 1] else: energy = chain_energy + K.expand_dims( input_energy_t + prev_target_val, 2) min_energy = K.min(energy, 1) # cast for tf-version `K.rnn argmin_table = K.cast(K.argmin(energy, 1), K.floatx()) return argmin_table, [min_energy, i + 1]
def call(self, x): #Shape may be a little different from the layers used for classification #shape [Batchsize,Npoints,Nstars,Ndimensions] diff = x[:,:,None,:]-self.stars[None,None,:,:] #difference between each input point and each star in the volume pointfeatures = tf.norm(diff, axis=3) #euclidean distance between each input point and each star globalfeatures = K.min(pointfeatures, axis = 1) ## For each star, find the distance to the closest input point return tf.concat([pointfeatures,tf.tile(globalfeatures[:,None,:],(1,x.shape[1],1))],axis=2)
def call(self, x): #shape [Batchsize,Nstars,Npoints,Ndimensions] diff = x[:,None,:,:]-self.stars[None,:,None,:] #difference between each input point and each star in the volume dists = K.min ( tf.norm(diff, axis=3), #euclidean distance between each input point and each star axis = 2) ## For each star, find the distance to the closest input point return dists
def one_error(y_true, y_pred): min_predicted = K.min(y_pred * y_true, axis=-1) print("min_predicted", min_predicted) max_nonlabels = K.max((1 - y_true) * y_pred, axis=-1) print("max_nonlabels", max_nonlabels) print("min_predicted + max_nonpredictions", min_predicted + max_nonlabels) return K.mean(min_predicted + max_nonlabels, axis=-1)
def yolo_correct_boxes(box_xy, box_wh, input_shape, image_shape): #-----------------------------------------------------------------# # 把y轴放前面是因为方便预测框和图像的宽高进行相乘 #-----------------------------------------------------------------# box_yx = box_xy[..., ::-1] box_hw = box_wh[..., ::-1] input_shape = K.cast(input_shape, K.dtype(box_yx)) image_shape = K.cast(image_shape, K.dtype(box_yx)) new_shape = K.round(image_shape * K.min(input_shape / image_shape)) #-----------------------------------------------------------------# # 这里求出来的offset是图像有效区域相对于图像左上角的偏移情况 # new_shape指的是宽高缩放情况 #-----------------------------------------------------------------# offset = (input_shape - new_shape) / 2. / input_shape scale = input_shape / new_shape box_yx = (box_yx - offset) * scale box_hw *= scale box_mins = box_yx - (box_hw / 2.) box_maxes = box_yx + (box_hw / 2.) boxes = K.concatenate([ box_mins[..., 0:1], # y_min box_mins[..., 1:2], # x_min box_maxes[..., 0:1], # y_max box_maxes[..., 1:2] # x_max ]) boxes *= K.concatenate([image_shape, image_shape]) return boxes
def yolo_correct_boxes(box_xy, box_wh, input_shape, image_shape): '''Get corrected boxes''' box_yx = box_xy[..., ::-1] box_hw = box_wh[..., ::-1] input_shape = K.cast(input_shape, K.dtype(box_yx)) image_shape = K.cast(image_shape, K.dtype(box_yx)) new_shape = K.round(image_shape * K.min(input_shape / image_shape)) # print("input_shape:{} image_shape:{} new_shape:{}".format(tf.keras.backend.int_shape(input_shape), # tf.keras.backend.int_shape(image_shape), # tf.keras.backend.int_shape(new_shape))) offset = (input_shape - new_shape) / 2. / input_shape scale = input_shape / new_shape box_yx = (box_yx - offset) * scale box_hw *= scale box_mins = box_yx - (box_hw / 2.) box_maxes = box_yx + (box_hw / 2.) boxes = K.concatenate([ box_mins[..., 0:1], # y_min box_mins[..., 1:2], # x_min box_maxes[..., 0:1], # y_max box_maxes[..., 1:2] # x_max ]) # Scale boxes back to original image shape. boxes *= K.concatenate([image_shape, image_shape]) return boxes
def call(self, x): A = x[0] #adjacency matrix Ar = K.reshape(A, (-1, self.ogs, self.c, self.ogs, self.c)) #print("Ar",Ar.shape) if self.mode == "mean": Am = K.mean(Ar, axis=(2, 4)) if self.mode == "min": Am = K.min(Ar, axis=(2, 4)) if self.mode == "max": Am = K.max(Ar, axis=(2, 4)) #print("Am",Am.shape) C = self.c_const cut = self.cut Ar = K.relu(1 + C * (Am - cut)) - K.relu(C * (Am - cut)) #print("Ar",Ar.shape) #exit() return Ar exit()
def hydra_oneSided_MSE(nashEq_proposer, nashEq_proposed): """ Function to compute MSE of equilibria with a proposer and proposed defined (in Deferred Acceptance Algorithm terms). """ # Create a row-wise meshgrid of proposed equilibria for each sample in the batch by adding a new dimension and replicate the array along that proposed_grid = tf.tile(tf.expand_dims(nashEq_proposed, axis=2), [1, 1, tf.shape(nashEq_proposed)[1], 1, 1]) # Create a column-wise meshgrid of proposer equilibria for each sample in the batch by adding a new dimension and replicate the array along that proposer_grid = tf.tile(tf.expand_dims(nashEq_proposer, axis=1), [1, tf.shape(nashEq_proposer)[1], 1, 1, 1]) # Compute the weight for the final result to recompense the replacement of nans with zeros and its effect # on the averaging nan_count = tf.reduce_sum( tf.cast(tf.math.is_nan(nashEq_proposer[0][0] + nashEq_proposed[0][0]), tf.int32)) eq_n_elements = tf.size(nashEq_proposer[0][0]) compensation_factor = tf.cast(eq_n_elements / (eq_n_elements - nan_count), tf.float32) # Compute error grid error_grid = proposed_grid - proposer_grid # Replace nan values with 0 error_grid = tf.where(tf.math.is_nan(error_grid), tf.zeros_like(error_grid), error_grid) return K.max(K.min(K.mean(K.square(error_grid), axis=[3, 4]), axis=2), axis=1) * compensation_factor
def boundbox_correction(b_xy , b_wh , input_shape , image_shape): """Get corrected boxes""" box_yx = b_xy[..., ::-1] box_hw = b_wh[..., ::-1] input_shape = K.cast(input_shape, K.dtype(box_yx)) image_shape = K.cast(image_shape, K.dtype(box_yx)) new_shape = K.round(image_shape * K.min(input_shape / image_shape)) offset = (input_shape - new_shape) / 2.0 / input_shape scale = input_shape / new_shape box_yx = (box_yx - offset) * scale box_hw *= scale box_mins = box_yx - (box_hw / 2.0) box_maxes = box_yx + (box_hw / 2.0) boxes = K.concatenate( [ box_mins[..., 0:1], # y_min box_mins[..., 1:2], # x_min box_maxes[..., 0:1], # y_max box_maxes[..., 1:2], # x_max ] ) # Scale boxes back to original image shape. boxes *= K.concatenate([image_shape, image_shape]) return boxes
def hydra_oneSided_payoffv2_Eq_MSE(nashEq_proposer, nashEq_proposed, game, pureStrategies_perPlayer, computePayoff_function, num_players): """ Function to compute MSE of equilibria and payoffs with a proposer and proposed defined (in Deferred Acceptance Algorithm terms). """ # Compute payoffs given the equilibria payoff_proposed = computePayoff_function['computePayoff_2dBatch']( game, nashEq_proposed, pureStrategies_perPlayer, num_players) payoff_proposer = computePayoff_function['computePayoff_2dBatch']( game, nashEq_proposer, pureStrategies_perPlayer, num_players) # Create a row-wise meshgrid of proposed payoffs for each sample in the batch by adding a new dimension and # replicate the array along that proposed_grid = tf.tile(tf.expand_dims(payoff_proposed, axis=2), [1, 1, tf.shape(payoff_proposed)[1], 1]) # Create a column-wise meshgrid of proposer payoffs for each sample in the batch by adding a new dimension and # replicate the array along that proposer_grid = tf.tile(tf.expand_dims(payoff_proposer, axis=1), [1, tf.shape(payoff_proposer)[1], 1, 1]) # Compute MSE of payoffs squared_error = K.square(proposed_grid - proposer_grid) loss_payoff_MSE = K.mean( K.max(K.min(K.mean(squared_error, axis=3), axis=2), axis=1)) # Compute MSE of equilibria loss_Eq_MSE = hydra_MSE(nashEq_proposed, nashEq_proposer) return loss_Eq_MSE, loss_payoff_MSE
def yolo_correct_boxes(box_xy, box_wh, input_shape, image_shape): box_yx = box_xy[..., ::-1] box_hw = box_wh[..., ::-1] input_shape = K.cast(input_shape, K.dtype(box_yx)) image_shape = K.cast(image_shape, K.dtype(box_yx)) new_shape = K.round(image_shape * K.min(input_shape/image_shape)) offset = (input_shape-new_shape)/2./input_shape scale = input_shape/new_shape box_yx = (box_yx - offset) * scale box_hw *= scale box_mins = box_yx - (box_hw / 2.) box_maxes = box_yx + (box_hw / 2.) boxes = K.concatenate([ box_mins[..., 0:1], # y_min box_mins[..., 1:2], # x_min box_maxes[..., 0:1], # y_max box_maxes[..., 1:2] # x_max ]) boxes *= K.concatenate([image_shape, image_shape]) return boxes
def dtw_grad_tensor(X):#, gamma=1.0): D, gamma = X D = K.cast(D, dtype=FLOAT_TYPE) gamma = K.cast(gamma, dtype=FLOAT_TYPE) gamma = K.min(gamma) # D MUST be a numpy array or numba will throw an error M = D.shape[0] N = D.shape[1] #print('success') V = np.zeros((M + 1, N + 1), dtype=FLOAT_TYPE) V[:, 0] = 1e10 V[0, :] = 1e10 V[0, 0] = 0 # Q = np.zeros((M + 2, N + 2, 3)) Q[M + 1, N + 1] = 1 # E = np.zeros((M + 2, N + 2)) E[M + 1, :] = 0 E[:, N + 1] = 0 E[M + 1, N + 1] = 1 # V = K.cast(V, FLOAT_TYPE) Q = K.cast(Q, FLOAT_TYPE) E = K.cast(E, FLOAT_TYPE) op = tf.numpy_function(dtw_grad, (D, V, Q, E, gamma), (FLOAT_TYPE, FLOAT_TYPE, FLOAT_TYPE, FLOAT_TYPE)) #print('success') return op
def call(self, x): A = x[0] #adjacency matrix x = x[1] #parameters #print("x",x.shape) #print("A",A.shape) #print("trafo",self.trafo.shape) #currently just uses the last value as sorting param values = x[:, :, -1] #K.reshape(K.dot(x,self.metrik),(-1,self.gs)) #print("values",values.shape) _, valueorder = t.math.top_k(values, k=self.gs) #print("valueorder",valueorder.shape) #valueorder=t.argsort(values,axis=-1) #print("valueorder",valueorder.shape) xg = t.gather(params=x, indices=valueorder, axis=1, batch_dims=1) #print("xg",xg.shape) #exit() xs = K.reshape(xg, (-1, self.ogs, self.param * self.c)) #print("xs",xs.shape) traf = K.dot(xs, self.trafo) #print("traf",traf.shape) At1 = t.gather(params=A, indices=valueorder, axis=1, batch_dims=1) At2 = t.gather(params=At1, indices=valueorder, axis=2, batch_dims=1) #print("At1",At1.shape,"At2",At2.shape) Ar = K.reshape(At2, (-1, self.ogs, self.c, self.ogs, self.c)) #print("Ar",Ar.shape) if self.mode == "mean": Am = K.mean(Ar, axis=(2, 4)) if self.mode == "min": Am = K.min(Ar, axis=(2, 4)) if self.mode == "max": Am = K.max(Ar, axis=(2, 4)) #print("Am",Am.shape) C = self.c_const cut = self.cut Ar = K.relu(1 + C * (Am - cut)) - K.relu(C * (Am - cut)) #print("Ar",Ar.shape) #exit() return Ar, traf exit()
def loss(y_true, y_pred): eps = 1e-6 y_true = K.reshape(y_true, [w, h]) gt_points = K.cast(tf.where(y_true > 0.5), dtype=tf.float32) num_gt_points = tf.shape(gt_points)[0] y_pred = K.flatten(y_pred) p = y_pred p_replicated = tf.squeeze(K.repeat(tf.expand_dims(p, axis=-1), num_gt_points)) d_matrix = cdist(all_img_locations, gt_points) num_est_pts = tf.reduce_sum(p) term_1 = (1 / (num_est_pts + eps)) * K.sum(p * K.min(d_matrix, 1)) d_div_p = K.min((d_matrix + eps) / (p_replicated ** alpha + (eps / max_dist)), 0) d_div_p = K.clip(d_div_p, 0, max_dist) term_2 = K.mean(d_div_p, axis=0) return term_1 + term_2
def update_state(self, y_true, y_pred, sample_weight=None): row = tf.range(0, tf.shape(y_true)[0], dtype=tf.int64) col = k.argmin(y_pred, axis=1) pred_val = tf.gather_nd(y_true, tf.stack([row, col], axis=1)) values = pred_val - k.min(y_true, axis=1) # tf.print(tf.gather(y_true,0), tf.gather(y_pred,0), tf.gather(pred_val,0),k.min(y_true, axis=1), values, sep='\n') return super(DBM, self).update_state(values, sample_weight=sample_weight)
def _init_models(self): # make sure that the policy loss is set to 'sac' if self.policy.update_strategy != 'sac': self.policy.update_strategy = 'sac' self.logger.warn("policy.update_strategy has been set to 'sac'") # inputs S, A = self.policy.train_model.inputs[:2] G = keras.Input(name='G', shape=(1,), dtype='float') # constuct log(pi(a_sampled, s)) A_sampled = self.policy.dist.sample() # differentiable log_pi = self.policy.dist.log_proba(A_sampled) # use target models for q-values, because they're non-trainable Q1 = self._get_q_value(self.q_func1, S, A_sampled) Q2 = self._get_q_value(self.q_func2, S, A_sampled) Q_both = keras.layers.Concatenate()([Q1, Q2]) check_tensor(Q_both, ndim=2, axis_size=2, axis=1) # construct entropy-corrected target for state value function Q_min = keras.layers.Lambda(lambda x: K.min(x, axis=1))(Q_both) V_target = K.stop_gradient(Q_min - self.policy.entropy_beta * log_pi) check_tensor(V_target, ndim=1) # compute advantages from q-function V = self.v_func.predict_model(S) check_tensor(V, axis_size=1, axis=1) V = K.stop_gradient(K.squeeze(V, axis=1)) Q = keras.layers.Lambda(lambda x: K.mean(x, axis=1))(Q_both) Adv = Q - self.policy.entropy_beta * log_pi - V # update loss with advantage coming directly from graph policy_loss, metrics = self.policy.policy_loss_with_metrics(Adv) v_loss = self.v_func.train_model([S, V_target]) q_loss1 = self.q_func1.train_model([S, A, G]) q_loss2 = self.q_func2.train_model([S, A, G]) value_loss = (v_loss + q_loss1 + q_loss2) / 3. # add losses to metrics dict metrics.update({ 'policy/loss': policy_loss, 'v_func/loss': v_loss, 'q_func1/loss': q_loss1, 'q_func2/loss': q_loss2, 'value/loss': value_loss, }) # combined loss function loss = policy_loss + self.value_loss_weight * value_loss check_tensor(loss, ndim=0) # should be a scalar # joint model self.train_model = keras.Model([S, A, G], loss) self.train_model.add_loss(loss) for name, metric in metrics.items(): self.train_model.add_metric(metric, name=name, aggregation='mean') self.train_model.compile(optimizer=self.policy.train_model.optimizer)