def dense(ninputs, noutputs): inputs = Input(shape=(ninputs, ), name='input') #x = BatchNormalization(name='bn_1')(inputs) x = Dense(64, name='dense_1', activation='relu')(inputs) x = Dropout(rate=0.1)(x) x = Dense(32, name='dense_2', activation='relu')(x) x = Dropout(rate=0.1)(x) x = Dense(32, name='dense_3', activation='relu')(x) x = Dropout(rate=0.1)(x) outputs = Dense(noutputs, name='output', activation='linear')(x) outputs0 = Lambda(lambda x: slice(x, (0, 0), (-1, 1)))(outputs) outputs1 = Lambda(lambda x: slice(x, (0, 1), (-1, -1)))(outputs) outputs_softmax = Dense(2, name='output_softmax', activation='softmax')(outputs1) outputs_softmax0 = Lambda(lambda x: slice(x, (0, 0), (-1, 1)))( outputs_softmax) outputs_softmax1 = Lambda(lambda x: slice(x, (0, 1), (-1, -1)))( outputs_softmax) keras_model = Model(inputs=inputs, outputs=[outputs0, outputs_softmax0, outputs_softmax1]) return keras_model
def syn_loss(self, inputs): img_tgt, img_syn = inputs img_tgt_cropped = K.slice(img_tgt, (0, 40, 40, 0), (-1, 400, 560, -1)) img_syn_cropped = K.slice(img_syn, (0, 40, 40, 0), (-1, 400, 560, -1)) loss = K.mean(mean_absolute_error(img_tgt_cropped, img_syn_cropped)) return loss
def iou_loss_core(y_true, y_pred, smooth=1): y_true = K.slice(y_true, [0, 0, 0, 0], [-1, -1, -1, 1]) y_pred = K.slice(y_pred, [0, 0, 0, 0], [-1, -1, -1, 1]) intersection = K.sum(K.abs(y_true * y_pred), axis=-1) union = K.sum(y_true, -1) + K.sum(y_pred, -1) - intersection iou = (intersection + smooth) / (union + smooth) return iou
def call(self, inputs, **kwargs): one_hot_feature_index = K.cast( K.slice(inputs, (0, 0), (-1, self.feature_num)), "int32") numeric_feature = K.slice(inputs, (0, self.feature_num), (-1, -1)) ## first order first_order_index = K.reshape(one_hot_feature_index, (-1, )) get_first_order_weights = K.gather(self.w_one_hot, first_order_index) first_order_weights = K.reshape(get_first_order_weights, (-1, self.feature_num)) first_order = K.sum(first_order_weights, 1) + K.sum( K.dot(numeric_feature, self.w_numeric), 1) ## second order get_second_order_weights = K.gather(self.v_one_hot, first_order_index) second_order_weights = K.reshape( get_second_order_weights, (-1, self.feature_num, self.embedding_size)) numeric_weights = K.expand_dims(self.v_numeric, 0) * K.expand_dims( numeric_feature, -1) all_weights = K.concatenate([second_order_weights, numeric_weights], axis=1) weights_sum_square = K.sum(K.square(all_weights), 1) weights_square_sum = K.square(K.sum(all_weights, 1)) second_order = 0.5 * K.sum(weights_square_sum - weights_sum_square, 1) output = first_order + second_order + self.b if self.activation is not None: output = self.activation(output) output = K.expand_dims(output, -1) return output '''X_square = K.square(inputs)
def make_online(self): embedding = K.variable( np.random.uniform(0, 1, (self.dataset.nsize, self.flowargs['embdim']))) prevemb = K.placeholder(ndim=2, dtype='float32') # (nsize, d) data = K.placeholder( ndim=2, dtype='int32' ) # (batchsize, 5), [k, from_pos, to_pos, from_neg, to_neg] weight = K.placeholder(ndim=1, dtype='float32') # (batchsize, ) if K._BACKEND == 'theano': # (batchsize, d) => (batchsize, ) # data[:, 0] should be always 0, so we simply ignore it # note, when you want to use it, that according to data generation procedure, the actual data[:, 0] is not 0 dist_pos = embedding[data[:, 1]] - embedding[data[:, 2]] dist_pos = K.sum(dist_pos * dist_pos, axis=-1) dist_neg = embedding[data[:, 3]] - embedding[data[:, 4]] dist_neg = K.sum(dist_neg * dist_neg, axis=-1) else: dist_pos = K.gather(embedding, K.squeeze(K.slice(data, [0, 1], [-1, 1]), axis=1)) - \ K.gather(embedding, K.squeeze(K.slice(data, [0, 2], [-1, 1]), axis=1)) dist_pos = K.sum(dist_pos * dist_pos, axis=-1) dist_neg = K.gather(embedding, K.squeeze(K.slice(data, [0, 3], [-1, 1]), axis=1)) - \ K.gather(embedding, K.squeeze(K.slice(data, [0, 4], [-1, 1]), axis=1)) dist_neg = K.sum(dist_neg * dist_neg, axis=-1) # (batchsize, ) margin = 1 lprox = K.maximum(margin + dist_pos - dist_neg, 0) * weight # (1, ) lprox = K.mean(lprox) # lsmooth lsmooth = embedding - prevemb # (nsize, d) lsmooth = K.sum(K.square(lsmooth), axis=-1) # (nsize) lsmooth = K.mean(lsmooth) loss = lprox + self.flowargs['beta'][0] * lsmooth opt = optimizers.get({ 'class_name': 'Adagrad', 'config': { 'lr': self.lr } }) cstr = { embedding: constraints.get({ 'class_name': 'maxnorm', 'config': { 'max_value': 1, 'axis': 1 } }) } upd = opt.get_updates([embedding], cstr, loss) lf = K.function([data, weight, prevemb], [loss], updates=upd) return lf, None, [embedding], {}
def call(self, x): # extract time-series from feature vector n_examples = K.int_shape(x)[0] if n_examples is None: n_examples = self.batch_size x1 = K.slice(x, (0, 0, 0), (1, self.time_steps, 1)) x1 = K.reshape(x1, (self.time_steps, )) x2 = K.slice(x, (1, self.time_steps - 1, 0), (n_examples - 1, 1, 1)) x2 = K.reshape(x2, (n_examples - 1, )) ts = K.concatenate([x1, x2]) x_norm = [] # normalized values of time-series ls = [] # coeffients for denormalization of forecasts l_t_minus_1 = self.level for i in range(n_examples + self.time_steps - 1): # compute l_t y_t = ts[i] s_t = self.seasonality_queue.popleft() l_t = self.alpha * y_t / s_t + (1 - self.alpha) * l_t_minus_1 # compute s_{t+m} s_t_plus_m = self.gamma * y_t / l_t + (1 - self.gamma) * s_t self.seasonality_queue.append(s_t_plus_m) # normalize y_t x_norm.append(y_t / (s_t * l_t)) l_t_minus_1 = l_t if i >= self.time_steps - 1: l = [l_t] * self.horizon l = K.concatenate(l) s = [self.seasonality_queue[i] for i in range(self.horizon) ] # we assume here that horizon < m s = K.concatenate(s) ls_t = K.concatenate([K.expand_dims(l), K.expand_dims(s)]) ls.append(K.expand_dims(ls_t, axis=0)) self.level = l_t x_norm = K.concatenate(x_norm) # create x_out x_out = [] for i in range(n_examples): norm_features = K.slice(x_norm, (i, ), (self.time_steps, )) norm_features = K.expand_dims(norm_features, axis=0) x_out.append(norm_features) x_out = K.concatenate(x_out, axis=0) x_out = K.expand_dims(x_out) # create tensor of denormalization coefficients denorm_coeff = K.concatenate(ls, axis=0) return [x_out, denorm_coeff]
def cls_loss_func(y_true, y_pred): cls_true = K.slice(y_true, start_cls, size_cls) cls_pred = K.slice(y_pred, start_cls, size_cls) cls_loss = crossentropy(cls_true, cls_pred) return cls_loss * cls_weight
def call(self, inputs, states=None, mask=None, training=None): if states is None: state = self.get_initial_state(inputs, mask=False) if mask is not None: if isinstance(mask, list): mask = mask[0] if mask.dtype != tf.bool: mask = K.cast(mask, tf.bool) mask = K.permute_dimensions(mask, [1, 0]) def get_match_mask(mask, tensor): ndim = K.ndim(tensor) for _ in range(ndim - 1): mask = K.expand_dims(mask) add_shape = K.shape(tensor)[1:] multiple = K.concatenate([[1], add_shape], 0) return K.tile(mask, multiple) h = states[0] c = states[1] state = K.concatenate([h, c]) #(samples,units*2) state = K.expand_dims(state, axis=1) #(samples,1,units*2) state = K.tile(state, [1, self.input_dim, 1]) #(samples,input_dim,units*2) axes = [0, 2, 1] step_inputs = K.permute_dimensions(inputs, axes) #(samples,input_dim,steps) """ K.transpose == tf.transpose(x) K.permuter_dimensions == tf.transpose(x,axes) """ e = K.dot(step_inputs, self.kernel_U) #(samples,input_dim,steps) if self.use_bias: e = K.bias_add(e, self.bias) e = self.activation( e + K.dot(state, self.kernel_W)) #(samples,input_dim,steps) e = K.dot(e, K.expand_dims(self.kernel_V)) #(sample,input_dim,1) attention = K.squeeze(self.attention_activation(e), axis=-1) #(sample,input_dim) #使用tensorflow的slice函数,由于keras在切片操作上,会丢失一些信息,需要使用内置函数来操作。 x = K.squeeze(K.slice(inputs, [0, self.index, 0], [-1, 1, -1]), axis=1) update_x = attention * x #(sample,input_dim) if mask is not None: initial_state = self.get_initial_state(inputs) mask_t = K.squeeze(K.slice(mask, [self.index, 0], [1, -1]), axis=0) update_x = tf.where(get_match_mask(mask_t, update_x), update_x, initial_state) update_x = K.expand_dims(update_x, axis=1) return [attention, update_x]
def reg_loss_func(y_true, y_pred): reg_true = K.slice(y_true, start_reg, size_reg) reg_pred = K.slice(y_pred, start_reg, size_reg) cls_true = K.slice(y_true, start_cls, size_cls) reg_mask = K.sum(cls_true, axis=-1, keepdims=True) reg_loss = logcosh(reg_true, reg_mask * reg_pred) return reg_loss * reg_weight
def create_big_model(input_for_model, encoder, decoder, classificator): x = encoder(input_for_model) x1 = Lambda(lambda x: K.slice(x, (0, 0, 0), (1, -1, -1)))(x) x2 = Lambda(lambda x: K.slice(x, (1, 0, 0), (1, -1, -1)))(x) center = Lambda(lambda x: x * 0.5)(Add()([x1, x2])) psevdo_ecg = decoder(center) out_of_classificator = classificator(psevdo_ecg) # print("out_of_classificator") # print(out_of_classificator) out_of_decoder = decoder(x) return Model(inputs=input_for_model, outputs=[out_of_decoder, out_of_classificator])
def The_First_Loss_Func(y_true, y_pred): """ actual computation of loss for all elements compute residual loss of equation for all elements, and loss in continuity across element boundary, and loss in boundary condition of domain :param y_true: :param y_pred: :return: """ # ========================== # # domain boundary condition BCL_out = first_sub_model(ZL_input_tensor) # shape: (1,1) T_bc_L = BCL_out - BCL_tensor # shape: (1,1) BCR_out = last_sub_model(ZR_input_tensor) # shape: (1,1) T_bc_R = BCR_out - BCR_tensor # shape: (1,1) # ========================= # # equation residual bcl_pair = (ZL_input_tensor, BCL_out) bcr_pair = (ZR_input_tensor, BCR_out) T_tot = Equation_Residual(bcl_pair, bcr_pair) # now T_tot contains residual tensor for equation of all elements, shape: (n_elem, n_modes) # ========================== # # extract element boundary and interior modes # assemble boundary modes bmode = K.slice(T_tot, (0, 0), (n_elem, 2)) # bmode contains boundary mode contributions, shape: (n_elem, 2) interior_mode = K.slice(T_tot, (0, 2), (n_elem, n_modes - 2)) # interior_mode contains interior mode contributions, shape: (n_elem, n_modes-2) bmode_2 = K.reshape(bmode, (n_elem * 2, 1)) # shape (n_elem*2,1) bmode_glob = K.dot(bmode_assembler_tensor, bmode_2) # now bmode_glob contains global mode contributions, shape: (n_elem+1,1) # interior_mode contains interior mode contributions value_1 = K.flatten(bmode_glob) value_2 = K.flatten(interior_mode) value = K.concatenate((value_1, value_2)) # now value contains all global modes, shape: (n_elem+1+n_elem*(n_modes-2),), 1D array # ========================= # # C^k continuity across element boundary Ck_loss = The_Loss_Func_Ck() # now Ck_loss contains loss value for C_k continuity across element boundaries # ========================= # this_loss = K.mean(K.square(value)) \ + (K.sum(K.square(T_bc_L)) + K.sum(K.square(T_bc_R)) + Ck_loss) return this_loss
def parse_input(self, x): input_ids = K.squeeze( K.slice(x, [0, 0, 0], [-1, 1, self.config.max_seq_len]), 1) attention_mask = K.cast(K.squeeze( K.slice(x, [0, 1, 0], [-1, 1, self.config.max_seq_len]), 1), dtype="float32") token_type_ids = K.squeeze( K.slice(x, [0, 2, 0], [-1, 1, self.config.max_seq_len]), 1) position_ids = K.squeeze( K.slice(x, [0, 3, 0], [-1, 1, self.config.max_seq_len]), 1) return input_ids, attention_mask, token_type_ids, position_ids
def loss(y_true, y_pred): size = K.int_shape(y_pred)[0] shape = K.shape(y_pred) batch_size = shape[:1] input_shape = shape[1:] step = batch_size // 2 size = step size = K.concatenate([size, input_shape], axis=0) stride = K.concatenate([step, input_shape * 0], axis=0) start = stride * 0 p1 = K.slice(y_pred, start, size) start = stride * 1 p2 = K.slice(y_pred, start, size) return K.mean(-K.log(1.0 - K.sum(p1 * p2, axis=1)))
def call(self, inputs): # Layer logic sum_of_activations = 0 activ_return = K.placeholder(shape=self.units) activ_calc = np.zeros(self.units) for n in range(self.units): for d in range(self.dendrites): # print(K.dot(inputs, self.kernel[d])) # self.dendriteInput[d] = K.dot(inputs, self.kernel[d]) dendrite_in = K.dot( inputs, K.slice(self.kernel, [d, 0, 0], [self.dendrites, self.input_dim, self.units])) # if self.use_bias: # self.dendriteInput[d, :] = K.bias_add(self.dendriteInput[d, :], self.dendriteBias, # data_format='channels_last') # self.dendriteActivations[d] = self.dendritic_transfer( self.dendriteInput[d]) dendrite_out = self.dendritic_transfer(dendrite_in) sum_of_activations += dendrite_out if self.use_bias: sum_of_activations = K.bias_add(sum_of_activations, self.bias, data_format='channels_last') if self.activation is not None: # CURRENT: Make 'Activation' a tensor where each entry is the output of a single neuron activ_calc[n] = self.activation(sum_of_activations) K.set_value(activ_return, activ_calc) return activ_return
def call(self, inputs, mask=None): if self.use_token_type: assert len( inputs ) == 2, "`token_type_ids` must be specified if `use_token_type` is True." output = inputs[0] _, seq_length, input_width = K.int_shape(output) # print(inputs) assert seq_length == K.int_shape( inputs[1] )[1], "width of `token_type_ids` must be equal to `seq_length`" token_type_ids = inputs[1] # assert K.int_shape(token_type_ids)[1] <= self.token_type_vocab_size flat_token_type_ids = K.reshape(token_type_ids, [-1]) flat_token_type_ids = K.cast(flat_token_type_ids, dtype='int32') token_type_one_hot_ids = K.one_hot( flat_token_type_ids, num_classes=self.token_type_vocab_size) token_type_embeddings = K.dot(token_type_one_hot_ids, self.token_type_table) token_type_embeddings = K.reshape( token_type_embeddings, shape=[-1, seq_length, input_width]) # print(token_type_embeddings) output += token_type_embeddings else: output = inputs seq_length = K.int_shape(inputs)[1] if self.use_position_embeddings: position_embeddings = K.slice(self.full_position_embeddings, [0, 0], [seq_length, -1]) output += position_embeddings return output
def binary_tcn_accuracy(y_true, y_pred): #want the accuracy just for the lstm output shape = K.shape(y_pred) return K.mean(K.mean(K.slice(K.equal(y_true, K.round(y_pred)), (0, shape[1] - 1), (shape[0], 1)), axis=-1), axis=-1)
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 SpaceDetector(x): print("x-sh", x.shape) # print("input: ", K.eval(x)) sp_idx = ctable.char_indices[' '] sp = np.zeros((INPUT_VOCAB_SIZE)) sp[sp_idx] = 1 filtered = x * sp # print("filtered:", K.eval(filtered)) sp_positions = K.tf.where(K.tf.equal(filtered, 1)) # row indices print(sp_positions.shape) # print("sp-p:", K.eval(sp_positions)) starts = sp_positions[:-1] + [0, 1, 0] stops = sp_positions[1:] + [0, 0, INPUT_VOCAB_SIZE] sizes = stops - starts + [1, 0, 0] where = K.tf.equal(sizes[:, 0], 1) starts = K.tf.boolean_mask(starts, where) # Remove multi-sample rows sizes = K.tf.boolean_mask(sizes, where) # Same where = K.tf.greater(sizes[:, 1], 0) starts = K.tf.boolean_mask( starts, where) # Remove words with 0 length (consecutive spaces) sizes = K.tf.boolean_mask(sizes, where) # Same print("starts:", starts, "sh:", starts.shape) print("stops:", stops) print("sizes:", sizes, "sh:", sizes.shape) slices = K.map_fn( lambda info: K.tf.pad(K.squeeze(K.slice(x, info[0], info[ 1]), 0), [[0, MAX_WORD_SIZE - info[1][1]], [0, 0]], "CONSTANT"), [starts, sizes], dtype=float) return slices
def triplet_loss(y_true, y_pred): """ :param y_true: :param y_pred: shape = (? x embedding*3) :param alpha: :return: """ embedding_anchor = backend.slice(y_pred,[0,0],[-1,embedding_length]) embedding_positive = backend.slice(y_pred,[0,embedding_length],[-1,embedding_length]) embedding_negative = backend.slice(y_pred,[0,embedding_length*2],[-1,embedding_length]) distance_positive = distance_function(embedding_anchor,embedding_positive) distance_negative = distance_function(embedding_anchor,embedding_negative) loss = distance_positive - distance_negative + alpha return backend.maximum(loss, 0.0)
def slice_averse(y, yhat, batchsize, posbatchsize): if len(yhat.shape) == 2: start_normal = [0, 0] size_normal = [batchsize, 1] start_pos = [batchsize, 0] size_pos = [posbatchsize, 1] else: start_normal = [0, 0, 0, 0] yhat_shape = yhat.shape.as_list()[1:] size_normal = [batchsize, *yhat_shape] start_pos = [batchsize, 0, 0, 0] size_pos = [posbatchsize, *yhat_shape] y_normal = K.slice(y, start_normal, size_normal) yhat_normal = K.slice(yhat, start_normal, size_normal) y_pos = K.slice(y, start_pos, size_pos) yhat_pos = K.slice(yhat, start_pos, size_pos) return (y_normal, yhat_normal), (y_pos, yhat_pos)
def call(self, inputs): # print(inputs.shape[1]) seq_len = inputs.shape[1] list_of_ngrams = [] for i in range(self.n_value): begin = max(0, i - math.floor(self.n_value / 2)) end = min(seq_len - 1 + i - math.floor(self.n_value / 2), seq_len - 1) # print(begin,end) l = K.slice(inputs, [0, begin], [-1, end - begin + 1]) # print(l.shape) padded_zeros = K.zeros_like( K.slice(inputs, [0, 0], [-1, int(seq_len - (end - begin + 1))])) # print(padded_zeros.shape) if begin == 0: #left_padding list_of_ngrams.append( K.expand_dims(K.concatenate([padded_zeros, l]))) #right_padding else: list_of_ngrams.append( K.expand_dims(K.concatenate([l, padded_zeros]))) ngram_mat = K.concatenate(list_of_ngrams, axis=-1) # w_list = [] # seq_len = inputs.shape[1] # # print(math.floor(self.n_value/2)) # for n in range(self.n_value): # w = np.zeros(shape = (seq_len,seq_len)) # for i in range(seq_len): # if (i+n-math.floor(self.n_value/2)>= 0) and (i+n-math.floor(self.n_value/2) < seq_len): # w[i+n-math.floor(self.n_value/2),i] = 1 # w_list.append(w) # kernel = K.zeros(shape =(self.n_value,inputs.shape[1],inputs.shape[1])) # print(np.asarray(w_list).shape) # K.set_value(kernel, np.asarray(w_list)) # output = K.dot(inputs,kernel) # output = K.permute_dimensions(output, pattern = (0,2,1)) # output = K.gather(inputs, (:,[0,-3])) # print(output.shape) # output = inputs[:,self.index,:] return (ngram_mat)
def actions_model(start, scale): model = Sequential() model.add( Lambda( lambda _x: k.slice(_x, start=[0, start], size=[-1, scale]), input_shape=input_shape, batch_size=self.batch_size)) model.add(Dense(embed_size, activation='relu')) return model
def gcn(x): supports = list() for i in range(support): num , v , feature_shape = K.int_shape(x) LF = list() for j in range(batch_size): LF.append(K.expand_dims(K.dot(g[i], K.squeeze(K.slice(x, [j,0,0],[1,v,feature_shape]), axis = 0)), axis=0)) supports.append(K.concatenate(LF,axis=0)) x = K.concatenate(supports) return x
def loss_func(y_true, y_pred): #y[:,:,0:8] is reg #y[:,:,8:] is classes reg_true = K.slice(y_true, start_reg, size_reg) reg_pred = K.slice(y_pred, start_reg, size_reg) cls_true = K.slice(y_true, start_cls, size_cls) cls_pred = K.slice(y_pred, start_cls, size_cls) cls_loss = crossentropy(cls_true, cls_pred) # reg_mask = obj_true reg_mask = K.sum(cls_true, axis=-1, keepdims=True) reg_loss = logcosh(reg_true, reg_mask * reg_pred) return reg_loss * reg_weight + cls_weight * cls_loss
def triplet_loss_mult(y_true, y_preds, margin=0.5, n_poses=4, n_imgs=40): """Triplet semi-hard loss averaged over poses""" # Reshape batch (num_img * n_poses, emb_size) to (n_poses, num_img, emb_size) y_preds_comb = tf.reshape(y_preds, [n_poses, n_imgs, -1]) labels = K.slice(y_true, (0, 0), [n_imgs, -1]) temp_loss = lambda x: triplet_semihard_loss(labels, x, margin) # NOQA triplet_losses = tf.map_fn(temp_loss, y_preds_comb) return tf.reduce_mean(triplet_losses, name='triplet_loss_mult')
def call(self, inputs): ndims = len(inputs.shape) # print(inputs.shape[1]) slice_begin_index = [0] * ndims slice_end_index = [-1] * ndims seq_len = inputs.shape[self.axis] list_of_ngrams = [] for i in range(self.n_value): begin = max(0, i - math.floor(self.n_value / 2)) end = min(seq_len - 1 + i - math.floor(self.n_value / 2), seq_len - 1) # print(begin,end) slice_begin_index[self.axis] = begin slice_end_index[self.axis] = end - begin + 1 l = K.slice(inputs, slice_begin_index, slice_end_index) # print(l.shape) slice_begin_index[self.axis] = 0 slice_end_index[self.axis] = int(seq_len - (end - begin + 1)) # print(slice_end_index) padded_zeros = K.zeros_like( K.slice(inputs, slice_begin_index, slice_end_index)) # print(padded_zeros.shape) if begin == 0: #left_padding list_of_ngrams.append( K.expand_dims(K.concatenate([padded_zeros, l], axis=self.axis), axis=self.axis + 1)) #right_padding else: list_of_ngrams.append( K.expand_dims(K.concatenate([l, padded_zeros], axis=self.axis), axis=self.axis + 1)) ngram_mat = K.concatenate(list_of_ngrams, axis=self.axis + 1) return (ngram_mat)
def cam2pixel(cam_coords, proj): """Transforms coordinates in a camera frame to the pixel frame. Args: cam_coords: [batch, 4, height, width] proj: [batch, 4, 4] Returns: Pixel coordinates projected from the camera frame [batch, height, width, 2] """ batch, _, height, width = cam_coords.get_shape().as_list() cam_coords = K.reshape(cam_coords, [batch, 4, -1]) unnormalized_pixel_coords = K.batch_dot(proj, cam_coords) x_u = K.slice(unnormalized_pixel_coords, [0, 0, 0], [-1, 1, -1]) y_u = K.slice(unnormalized_pixel_coords, [0, 1, 0], [-1, 1, -1]) z_u = K.slice(unnormalized_pixel_coords, [0, 2, 0], [-1, 1, -1]) x_n = x_u / (z_u + 1e-10) y_n = y_u / (z_u + 1e-10) pixel_coords = K.concatenate([x_n, y_n], axis=1) pixel_coords = K.reshape(pixel_coords, [batch, 2, height, width]) return tf.transpose(pixel_coords, perm=[0, 2, 3, 1])
def build_model(W,L): x_in = keras.layers.Input(shape=(W,2)) # Batch, Length, Dimension ## Block 1 x_tp = keras.layers.Conv1D(kernel_size=7, filters=40, dilation_rate=1, padding='causal')(x_in) # x_tp = keras.layers.BatchNormalization()(x_tp) x_tp = keras.layers.Activation('relu')(x_tp) x_tp = keras.layers.Dropout(0.6)(x_tp) x_tp = keras.layers.Conv1D(kernel_size=7, filters=40, dilation_rate=1, padding='causal')(x_tp) # x_tp = keras.layers.BatchNormalization()(x_tp) x_tp = keras.layers.Activation('relu')(x_tp) x_tp = keras.layers.Dropout(0.6)(x_tp) ## add res for block 1 x_res = keras.layers.Conv1D(kernel_size=1, filters=40, dilation_rate=1, padding='causal')(x_in) x_tp = keras.layers.Add()([x_tp, x_res]) x_tp = keras.layers.Activation('relu')(x_tp) ## Block 2 x_block1 = x_tp x_tp = keras.layers.Conv1D(kernel_size=7, filters=20, dilation_rate=6, padding='causal')(x_tp) # x_tp = keras.layers.BatchNormalization()(x_tp) x_tp = keras.layers.Activation('relu')(x_tp) x_tp = keras.layers.Dropout(0.6)(x_tp) x_tp = keras.layers.Conv1D(kernel_size=7, filters=20, dilation_rate=6, padding='causal')(x_tp) # x_tp = keras.layers.BatchNormalization()(x_tp) x_tp = keras.layers.Activation('relu')(x_tp) x_tp = keras.layers.Dropout(0.6)(x_tp) ## add res for block 2 x_res = keras.layers.Conv1D(kernel_size=1, filters=20, dilation_rate=1, padding='causal')(x_block1) x_tp = keras.layers.Add()([x_tp, x_res]) # x_tp = keras.layers.Add()([x_tp, x_block1]) x_tp = keras.layers.Activation('relu')(x_tp) ## Block 3 x_block2 = x_tp x_tp = keras.layers.Conv1D(kernel_size=7, filters=20, dilation_rate=12, padding='causal')(x_tp) # x_tp = keras.layers.BatchNormalization()(x_tp) x_tp = keras.layers.Activation('relu')(x_tp) x_tp = keras.layers.Dropout(0.6)(x_tp) x_tp = keras.layers.Conv1D(kernel_size=7, filters=20, dilation_rate=12, padding='causal')(x_tp) # x_tp = keras.layers.BatchNormalization()(x_tp) x_tp = keras.layers.Activation('relu')(x_tp) x_tp = keras.layers.Dropout(0.6)(x_tp) ## add res for block 3 # x_res = keras.layers.Conv1D(kernel_size=1, filters=20, dilation_rate=1, padding='causal')(x_block2) # x_tp = keras.layers.Add()([x_tp, x_res]) x_tp = keras.layers.Add()([x_tp, x_block2]) x_tp = keras.layers.Activation('relu')(x_tp) ## SLICE x_tp = keras.layers.Lambda(lambda x:slice(x,(0,80,0),(-1,-1,-1)))(x_tp) # batch, length, channels x_tp = keras.layers.Flatten()(x_tp) x_tp = keras.layers.Dropout(0.5)(x_tp) x_tp = keras.layers.Dense(100, activation='relu')(x_tp) x_tp = keras.layers.Dropout(0.5)(x_tp) x_out = keras.layers.Dense(1)(x_tp) model = Model(inputs=x_in, outputs=x_out) return model
def vec2mat(vec): """Converts 6DoF parameters to transformation matrix Args: vec: 6DoF parameters in the order of tx, ty, tz, rx, ry, rz -- [6,] Returns: A transformation matrix -- [4, 4] """ translation = K.slice(vec, [0, 0], [-1, 3]) translation = K.expand_dims(translation, -1) rx = K.slice(vec, [0, 3], [-1, 1]) ry = K.slice(vec, [0, 4], [-1, 1]) rz = K.slice(vec, [0, 5], [-1, 1]) rot_mat = euler2mat(rz, ry, rx) # 3x3 filler = K.constant([0.0, 0.0, 0.0, 1.0], shape=[1, 1, 4]) filler = K.tile(filler, [rot_mat.shape.as_list()[0], 1, 1]) transform_mat = K.concatenate([rot_mat, translation], axis=2) transform_mat = K.concatenate([transform_mat, filler], axis=1) return transform_mat
def get_slice(data, i, parts): shape = K.shape(data) batch_size = shape[:1] input_shape = shape[1:] step = batch_size // parts if i == parts - 1: size = batch_size - step * i else: size = step size = K.concatenate([size, input_shape], axis=0) stride = K.concatenate([step, input_shape * 0], axis=0) start = stride * i return K.slice(data, start, size)
def _pairwise_distances(self, inputs: List[Tensor]) -> Tensor: emb_c, emb_r = inputs bs = K.shape(emb_c)[0] embeddings = K.concatenate([emb_c, emb_r], 0) dot_product = K.dot(embeddings, K.transpose(embeddings)) square_norm = K.batch_dot(embeddings, embeddings, axes=1) distances = K.transpose(square_norm) - 2.0 * dot_product + square_norm distances = K.slice(distances, (0, bs), (bs, bs)) distances = K.clip(distances, 0.0, None) mask = K.cast(K.equal(distances, 0.0), K.dtype(distances)) distances = distances + mask * 1e-16 distances = K.sqrt(distances) distances = distances * (1.0 - mask) return distances