Python keras.backend.shape() Examples
The following are 30
code examples of keras.backend.shape().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.backend
, or try the search function
.
Example #1
Source File: timedistributed.py From fancy-cnn with MIT License | 7 votes |
def get_output(self, train=False): def format_shape(shape): if K._BACKEND == 'tensorflow': def trf(x): try: return int(x) except TypeError: return x return map(trf, shape) return shape X = self.get_input(train) in_shape = format_shape(K.shape(X)) batch_flatten_len = K.prod(in_shape[:2]) cast_in_shape = (batch_flatten_len, ) + tuple(in_shape[i] for i in range(2, K.ndim(X))) pre_outs = self.layer(K.reshape(X, cast_in_shape)) out_shape = format_shape(K.shape(pre_outs)) cast_out_shape = (in_shape[0], in_shape[1]) + tuple(out_shape[i] for i in range(1, K.ndim(pre_outs))) outputs = K.reshape(pre_outs, cast_out_shape) return outputs
Example #2
Source File: model.py From PanopticSegmentation with MIT License | 6 votes |
def get_anchors(self, image_shape): """Returns anchor pyramid for the given image size.""" backbone_shapes = compute_backbone_shapes(self.config, image_shape) # Cache anchors and reuse if image shape is the same if not hasattr(self, "_anchor_cache"): self._anchor_cache = {} if not tuple(image_shape) in self._anchor_cache: # Generate Anchors a = utils.generate_pyramid_anchors( self.config.RPN_ANCHOR_SCALES, self.config.RPN_ANCHOR_RATIOS, backbone_shapes, self.config.BACKBONE_STRIDES, self.config.RPN_ANCHOR_STRIDE) # Keep a copy of the latest anchors in pixel coordinates because # it's used in inspect_model notebooks. # TODO: Remove this after the notebook are refactored to not use it self.anchors = a # Normalize coordinates self._anchor_cache[tuple(image_shape)] = utils.norm_boxes(a, image_shape[:2]) return self._anchor_cache[tuple(image_shape)]
Example #3
Source File: attention.py From keras-utility-layer-collection with MIT License | 6 votes |
def step(self, x, states): h = states[0] # states[1] necessary? # equals K.dot(X, self._W1) + self._b2 with X.shape=[bs, T, input_dim] total_x_prod = states[-1] # comes from the constants (equals the input sequence) X = states[-2] # expand dims to add the vector which is only valid for this time step # to total_x_prod which is valid for all time steps hw = K.expand_dims(K.dot(h, self._W2), 1) additive_atn = total_x_prod + hw attention = K.softmax(K.dot(additive_atn, self._V), axis=1) x_weighted = K.sum(attention * X, [1]) x = K.dot(K.concatenate([x, x_weighted], 1), self._W3) + self._b3 h, new_states = self.layer.cell.call(x, states[:-2]) return h, new_states
Example #4
Source File: attention.py From keras-utility-layer-collection with MIT License | 6 votes |
def build(self, input_shape): self._validate_input_shape(input_shape) self.input_spec = InputSpec(shape=input_shape) if not self.layer.built: self.layer.build(input_shape) self.layer.built = True input_dim = input_shape[-1] if self.layer.return_sequences: output_dim = self.layer.compute_output_shape(input_shape)[0][-1] else: output_dim = self.layer.compute_output_shape(input_shape)[-1] self._W1 = self.add_weight(shape=(input_dim, input_dim), name="{}_W1".format(self.name), initializer=self.weight_initializer) self._W2 = self.add_weight(shape=(output_dim, input_dim), name="{}_W2".format(self.name), initializer=self.weight_initializer) self._W3 = self.add_weight(shape=(2*input_dim, input_dim), name="{}_W3".format(self.name), initializer=self.weight_initializer) self._b2 = self.add_weight(shape=(input_dim,), name="{}_b2".format(self.name), initializer=self.weight_initializer) self._b3 = self.add_weight(shape=(input_dim,), name="{}_b3".format(self.name), initializer=self.weight_initializer) self._V = self.add_weight(shape=(input_dim,1), name="{}_V".format(self.name), initializer=self.weight_initializer) super(AttentionRNNWrapper, self).build()
Example #5
Source File: model.py From models with MIT License | 6 votes |
def predict_on_batch(self, inputs): if inputs.shape == (2,): inputs = inputs[np.newaxis, :] # Encode max_len = len(max(inputs, key=len)) one_hot_ref = self.encode(inputs[:,0]) one_hot_alt = self.encode(inputs[:,1]) # Construct dummy library indicator indicator = np.zeros((inputs.shape[0],2)) indicator[:,1] = 1 # Compute fold change for all three frames fc_changes = [] for shift in range(3): if shift > 0: shifter = np.zeros((one_hot_ref.shape[0],1,4)) one_hot_ref = np.concatenate([one_hot_ref, shifter], axis=1) one_hot_alt = np.concatenate([one_hot_alt, shifter], axis=1) pred_ref = self.model.predict_on_batch([one_hot_ref, indicator]).reshape(-1) pred_variant = self.model.predict_on_batch([one_hot_alt, indicator]).reshape(-1) fc_changes.append(np.log2(pred_variant/pred_ref)) # Return return {"mrl_fold_change":fc_changes[0], "shift_1":fc_changes[1], "shift_2":fc_changes[2]}
Example #6
Source File: attention.py From keras-utility-layer-collection with MIT License | 6 votes |
def step(self, x, states): h = states[0] # states[1] necessary? # comes from the constants X_static = states[-2] # equals K.dot(static_x, self._W1) + self._b2 with X.shape=[bs, L, static_input_dim] total_x_static_prod = states[-1] # expand dims to add the vector which is only valid for this time step # to total_x_prod which is valid for all time steps hw = K.expand_dims(K.dot(h, self._W2), 1) additive_atn = total_x_static_prod + hw attention = K.softmax(K.dot(additive_atn, self._V), axis=1) static_x_weighted = K.sum(attention * X_static, [1]) x = K.dot(K.concatenate([x, static_x_weighted], 1), self._W3) + self._b3 h, new_states = self.layer.cell.call(x, states[:-2]) # append attention to the states to "smuggle" it out of the RNN wrapper attention = K.squeeze(attention, -1) h = K.concatenate([h, attention]) return h, new_states
Example #7
Source File: contrib.py From steppy-toolkit with MIT License | 6 votes |
def call(self, x, mask=None): # computes a probability distribution over the timesteps # uses 'max trick' for numerical stability # reshape is done to avoid issue with Tensorflow # and 1-dimensional weights logits = K.dot(x, self.W) x_shape = K.shape(x) logits = K.reshape(logits, (x_shape[0], x_shape[1])) ai = K.exp(logits - K.max(logits, axis=-1, keepdims=True)) # masked timesteps have zero weight if mask is not None: mask = K.cast(mask, K.floatx()) ai = ai * mask att_weights = ai / (K.sum(ai, axis=1, keepdims=True) + K.epsilon()) weighted_input = x * K.expand_dims(att_weights) result = K.sum(weighted_input, axis=1) if self.return_attention: return [result, att_weights] return result
Example #8
Source File: capsule.py From CapsNet with MIT License | 6 votes |
def call(self, inputs, **kwargs): # (batch_size, 1, input_num_capsule, input_dim_capsule) expand_inputs = K.expand_dims(inputs, axis=1) # (batch_size, num_capsule, input_num_capsule, input_dim_capsule) expand_inputs = K.tile(expand_inputs, (1, self.num_capsule, 1, 1)) # (batch_size, num_capsule, input_num_capsule, dim_capsule) u_hat = K.map_fn(lambda x: K.batch_dot(x, self.W, axes=[2, 3]), expand_inputs) if self.num_routing <= 0: self.num_routing = 3 # (batch_size, num_capsule, input_num_capsule) b = K.zeros((K.shape(u_hat)[0], self.num_capsule, self.input_num_capsule)) for i in xrange(self.num_routing): # (batch_size, num_capsule, input_num_capsule) c = softmax(b, axis=1) # (batch_size, num_capsule, dim_capsule) s = K.batch_dot(c, u_hat, axes=[2, 2]) squashed_s = squash(s) if i < self.num_routing - 1: # (batch_size, num_capsule, input_num_capsule) b += K.batch_dot(squashed_s, u_hat, axes=[2, 3]) return squashed_s
Example #9
Source File: transform_rnn.py From View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition with MIT License | 6 votes |
def call(self,x,mask=None): conv_input,theta = x s = theta.shape theta = T.reshape(theta,[-1,s[2]]) m = K.not_equal(conv_input,0.) #### For translation trans = _trans(theta) output = _transform_trans(trans, conv_input) output = output * K.cast(m,K.floatx()) ### For rotation M = _fusion(theta) output = _transform_rot(M,output) return output
Example #10
Source File: transform_rnn.py From View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition with MIT License | 6 votes |
def call(self,x,training=None): deta1 = 0.3 deta2 = 0.3 deta3 = 0.3 seed = np.random.randint(1, 10e6) rng = RandomStreams(seed=seed) theta1 = rng.uniform(size=(x.shape[0],1),low=-deta1,high=deta1,dtype='float32') theta2 = rng.uniform(size=(x.shape[0],1),low=-deta2,high=deta2,dtype='float32') theta3 = rng.uniform(size=(x.shape[0],1),low=-deta3,high=deta3,dtype='float32') theta = K.concatenate([theta1,theta2,theta3],axis=-1) theta = K.tile(theta,x.shape[1]) theta = theta.reshape((x.shape[0], x.shape[1], 3)) theta = theta.reshape((theta.shape[0]*theta.shape[1], theta.shape[2])) M = _fusion(theta) output = _transform_rot(M, x) return K.in_train_phase(output,x,training = training)
Example #11
Source File: model.py From dataiku-contrib with Apache License 2.0 | 6 votes |
def clip_boxes_graph(boxes, window): """ boxes: [N, (y1, x1, y2, x2)] window: [4] in the form y1, x1, y2, x2 """ # Split wy1, wx1, wy2, wx2 = tf.split(window, 4) y1, x1, y2, x2 = tf.split(boxes, 4, axis=1) # Clip y1 = tf.maximum(tf.minimum(y1, wy2), wy1) x1 = tf.maximum(tf.minimum(x1, wx2), wx1) y2 = tf.maximum(tf.minimum(y2, wy2), wy1) x2 = tf.maximum(tf.minimum(x2, wx2), wx1) clipped = tf.concat([y1, x1, y2, x2], axis=1, name="clipped_boxes") clipped.set_shape((clipped.shape[0], 4)) return clipped
Example #12
Source File: model.py From keras-yolo3 with MIT License | 6 votes |
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)) 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
Example #13
Source File: model.py From dataiku-contrib with Apache License 2.0 | 6 votes |
def get_anchors(self, image_shape): """Returns anchor pyramid for the given image size.""" backbone_shapes = compute_backbone_shapes(self.config, image_shape) # Cache anchors and reuse if image shape is the same if not hasattr(self, "_anchor_cache"): self._anchor_cache = {} if not tuple(image_shape) in self._anchor_cache: # Generate Anchors a = utils.generate_pyramid_anchors( self.config.RPN_ANCHOR_SCALES, self.config.RPN_ANCHOR_RATIOS, backbone_shapes, self.config.BACKBONE_STRIDES, self.config.RPN_ANCHOR_STRIDE) # Keep a copy of the latest anchors in pixel coordinates because # it's used in inspect_model notebooks. # TODO: Remove this after the notebook are refactored to not use it self.anchors = a # Normalize coordinates self._anchor_cache[tuple(image_shape)] = utils.norm_boxes(a, image_shape[:2]) return self._anchor_cache[tuple(image_shape)]
Example #14
Source File: model.py From PanopticSegmentation with MIT License | 6 votes |
def build_rpn_model(anchor_stride, anchors_per_location, depth): """Builds a Keras model of the Region Proposal Network. It wraps the RPN graph so it can be used multiple times with shared weights. anchors_per_location: number of anchors per pixel in the feature map anchor_stride: Controls the density of anchors. Typically 1 (anchors for every pixel in the feature map), or 2 (every other pixel). depth: Depth of the backbone feature map. Returns a Keras Model object. The model outputs, when called, are: rpn_class_logits: [batch, H * W * anchors_per_location, 2] Anchor classifier logits (before softmax) rpn_probs: [batch, H * W * anchors_per_location, 2] Anchor classifier probabilities. rpn_bbox: [batch, H * W * anchors_per_location, (dy, dx, log(dh), log(dw))] Deltas to be applied to anchors. """ input_feature_map = KL.Input(shape=[None, None, depth], name="input_rpn_feature_map") outputs = rpn_graph(input_feature_map, anchors_per_location, anchor_stride) return KM.Model([input_feature_map], outputs, name="rpn_model") ############################################################ # Feature Pyramid Network Heads ############################################################
Example #15
Source File: model.py From PanopticSegmentation with MIT License | 6 votes |
def clip_boxes_graph(boxes, window): """ boxes: [N, (y1, x1, y2, x2)] window: [4] in the form y1, x1, y2, x2 """ # Split wy1, wx1, wy2, wx2 = tf.split(window, 4) y1, x1, y2, x2 = tf.split(boxes, 4, axis=1) # Clip y1 = tf.maximum(tf.minimum(y1, wy2), wy1) x1 = tf.maximum(tf.minimum(x1, wx2), wx1) y2 = tf.maximum(tf.minimum(y2, wy2), wy1) x2 = tf.maximum(tf.minimum(x2, wx2), wx1) clipped = tf.concat([y1, x1, y2, x2], axis=1, name="clipped_boxes") clipped.set_shape((clipped.shape[0], 4)) return clipped
Example #16
Source File: layers.py From DeepLearn with MIT License | 6 votes |
def call(self , x, mask=None): e1=x[0].T e2=x[1].T batch_size = K.shape(x[0])[0] sim = [] V_out = K.dot(self.V, K.concatenate([e1,e2],axis=0)) for i in range(self.k): temp = K.batch_dot(K.dot(e1.T,self.W[i,:,:]),e2.T,axes=1) sim.append(temp) sim=K.reshape(sim,(self.k,batch_size)) tensor_bi_product = self.activation(V_out+sim) tensor_bi_product = K.dot(self.U.T, tensor_bi_product).T return tensor_bi_product
Example #17
Source File: layers.py From DeepLearn with MIT License | 6 votes |
def build(self,input_shape): self.W = self.add_weight(name='w',shape=(self.k, self.d, self.d), initializer='glorot_uniform',trainable=True) #initializer='ones',trainable=False) self.V = self.add_weight(name='v',shape=(self.k, self.d*2), initializer='glorot_uniform',trainable=True) #initializer='ones',trainable=False) #self.b = self.add_weight(name='b',shape=(self.k,1), # initializer='glorot_uniform',trainable=True) # initializer='ones',trainable=False) self.U = self.add_weight(name='u',shape=(self.k,1), # initializer='ones',trainable=False) initializer='glorot_uniform',trainable=True) super(ntn, self).build(input_shape)
Example #18
Source File: model.py From vision-web-service with MIT License | 6 votes |
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)) 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
Example #19
Source File: model.py From multi-object-tracking with GNU General Public License v3.0 | 6 votes |
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)) 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
Example #20
Source File: chapter_06_001.py From Python-Deep-Learning-SE with MIT License | 6 votes |
def sampling(args: tuple): """ Reparameterization trick by sampling z from unit Gaussian :param args: (tensor, tensor) mean and log of variance of q(z|x) :returns tensor: sampled latent vector z """ # unpack the input tuple z_mean, z_log_var = args # mini-batch size mb_size = K.shape(z_mean)[0] # latent space size dim = K.int_shape(z_mean)[1] # random normal vector with mean=0 and std=1.0 epsilon = K.random_normal(shape=(mb_size, dim)) return z_mean + K.exp(0.5 * z_log_var) * epsilon
Example #21
Source File: model.py From dataiku-contrib with Apache License 2.0 | 6 votes |
def build_rpn_model(anchor_stride, anchors_per_location, depth): """Builds a Keras model of the Region Proposal Network. It wraps the RPN graph so it can be used multiple times with shared weights. anchors_per_location: number of anchors per pixel in the feature map anchor_stride: Controls the density of anchors. Typically 1 (anchors for every pixel in the feature map), or 2 (every other pixel). depth: Depth of the backbone feature map. Returns a Keras Model object. The model outputs, when called, are: rpn_class_logits: [batch, H * W * anchors_per_location, 2] Anchor classifier logits (before softmax) rpn_probs: [batch, H * W * anchors_per_location, 2] Anchor classifier probabilities. rpn_bbox: [batch, H * W * anchors_per_location, (dy, dx, log(dh), log(dw))] Deltas to be applied to anchors. """ input_feature_map = KL.Input(shape=[None, None, depth], name="input_rpn_feature_map") outputs = rpn_graph(input_feature_map, anchors_per_location, anchor_stride) return KM.Model([input_feature_map], outputs, name="rpn_model") ############################################################ # Feature Pyramid Network Heads ############################################################
Example #22
Source File: attention.py From keras-utility-layer-collection with MIT License | 5 votes |
def build(self, input_shape): super(SequenceAttention, self).build(input_shape) self._validate_input_shape(input_shape) self._weights = {} if self._similarity == self._additive_similarity: self._weights["w_a"] = self.add_weight( name='w_a', shape=(input_shape[0][-1] + input_shape[1][-1], input_shape[0][-1]), initializer=self._kernel_initializer, trainable=True ) self._weights["v_a"] = self.add_weight( name='v_a', shape=(1, input_shape[0][-1]), initializer=self._kernel_initializer, trainable=True ) elif self._similarity == self._multiplicative_similarity: self._weights["w_a"] = self.add_weight( name='w_a', shape=(input_shape[1][-1], input_shape[0][-1]), initializer=self._kernel_initializer, trainable=True ) self.built = True
Example #23
Source File: model.py From vision-web-service with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #24
Source File: attention.py From keras-utility-layer-collection with MIT License | 5 votes |
def call(self, x, mask=None): q, k, v = x d_k = q.shape.as_list()[2] # in pure tensorflow: # weights = tf.matmul(x_batch, tf.transpose(y_batch, perm=[0, 2, 1])) # normalized_weights = tf.nn.softmax(weights/scaling) # output = tf.matmul(normalized_weights, x_batch) weights = K.batch_dot(q, k, axes=[2, 2]) if mask is not None: # add mask weights if isinstance(mask, (list, tuple)): if len(mask) > 0: raise ValueError("mask can only be a Tensor or a list of length 1 containing a tensor.") mask = mask[0] weights += -1e10*(1-mask) normalized_weights = K.softmax(weights / np.sqrt(d_k)) output = K.batch_dot(normalized_weights, v) if self._return_attention: return [output, normalized_weights] else: return output
Example #25
Source File: model.py From vision-web-service with MIT License | 5 votes |
def yolo_head(feats, anchors, num_classes, input_shape, calc_loss=False): """Convert final layer features to bounding box parameters.""" num_anchors = len(anchors) # Reshape to batch, height, width, num_anchors, box_params. anchors_tensor = K.reshape(K.constant(anchors), [1, 1, 1, num_anchors, 2]) grid_shape = K.shape(feats)[1:3] # height, width grid_y = K.tile(K.reshape(K.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]), [1, grid_shape[1], 1, 1]) grid_x = K.tile(K.reshape(K.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]), [grid_shape[0], 1, 1, 1]) grid = K.concatenate([grid_x, grid_y]) grid = K.cast(grid, K.dtype(feats)) feats = K.reshape( feats, [-1, grid_shape[0], grid_shape[1], num_anchors, num_classes + 5]) # Adjust preditions to each spatial grid point and anchor size. box_xy = (K.sigmoid(feats[..., :2]) + grid) / K.cast(grid_shape[::-1], K.dtype(feats)) box_wh = K.exp(feats[..., 2:4]) * anchors_tensor / K.cast(input_shape[::-1], K.dtype(feats)) box_confidence = K.sigmoid(feats[..., 4:5]) box_class_probs = K.sigmoid(feats[..., 5:]) if calc_loss == True: return grid, feats, box_xy, box_wh return box_xy, box_wh, box_confidence, box_class_probs
Example #26
Source File: model.py From multi-object-tracking with GNU General Public License v3.0 | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #27
Source File: attention.py From keras-utility-layer-collection with MIT License | 5 votes |
def __call__(self, x, mask=None): if isinstance(x, (list, tuple)): self.build([it.shape for it in x]) else: self.build(x.shape) q, k, v = x outputs = [] attentions = [] for i in range(self._h): qi = self._q_layers[i](q) ki = self._k_layers[i](k) vi = self._v_layers[i](v) if self._return_attention: output, attention = self._sdp_layer([qi, ki, vi], mask=mask) outputs.append(output) attentions.append(attention) else: output = self._sdp_layer([qi, ki, vi], mask=mask) outputs.append(output) concatenated_outputs = Concatenate()(outputs) output = self._output(concatenated_outputs) if self._return_attention: attention = Concatenate()(attentions) # print("attention", attention, attention.shape) if self._return_attention: return [output, attention] else: return output # https://wanasit.github.io/attention-based-sequence-to-sequence-in-keras.html # https://arxiv.org/pdf/1508.04025.pdf
Example #28
Source File: model.py From multi-object-tracking with GNU General Public License v3.0 | 5 votes |
def yolo_head(feats, anchors, num_classes, input_shape): """Convert final layer features to bounding box parameters.""" num_anchors = len(anchors) # Reshape to batch, height, width, num_anchors, box_params. anchors_tensor = K.reshape(K.constant(anchors), [1, 1, 1, num_anchors, 2]) grid_shape = K.shape(feats)[1:3] # height, width grid_y = K.tile(K.reshape(K.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]), [1, grid_shape[1], 1, 1]) grid_x = K.tile(K.reshape(K.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]), [grid_shape[0], 1, 1, 1]) grid = K.concatenate([grid_x, grid_y]) grid = K.cast(grid, K.dtype(feats)) feats = K.reshape( feats, [-1, grid_shape[0], grid_shape[1], num_anchors, num_classes + 5]) box_xy = K.sigmoid(feats[..., :2]) box_wh = K.exp(feats[..., 2:4]) box_confidence = K.sigmoid(feats[..., 4:5]) box_class_probs = K.sigmoid(feats[..., 5:]) # Adjust preditions to each spatial grid point and anchor size. box_xy = (box_xy + grid) / K.cast(grid_shape[::-1], K.dtype(feats)) box_wh = box_wh * anchors_tensor / K.cast(input_shape[::-1], K.dtype(feats)) return box_xy, box_wh, box_confidence, box_class_probs
Example #29
Source File: attention.py From keras-utility-layer-collection with MIT License | 5 votes |
def build(self, input_shape): self._validate_input_shape(input_shape) for i, x in enumerate(input_shape): self.input_spec[i] = InputSpec(shape=x) if not self.layer.built: self.layer.build(input_shape) self.layer.built = True temporal_input_dim = input_shape[0][-1] static_input_dim = input_shape[1][-1] if self.layer.return_sequences: output_dim = self.layer.compute_output_shape(input_shape[0])[0][-1] else: output_dim = self.layer.compute_output_shape(input_shape[0])[-1] self._W1 = self.add_weight(shape=(static_input_dim, temporal_input_dim), name="{}_W1".format(self.name), initializer=self.weight_initializer) self._W2 = self.add_weight(shape=(output_dim, temporal_input_dim), name="{}_W2".format(self.name), initializer=self.weight_initializer) self._W3 = self.add_weight(shape=(temporal_input_dim + static_input_dim, temporal_input_dim), name="{}_W3".format(self.name), initializer=self.weight_initializer) self._b2 = self.add_weight(shape=(temporal_input_dim,), name="{}_b2".format(self.name), initializer=self.weight_initializer) self._b3 = self.add_weight(shape=(temporal_input_dim,), name="{}_b3".format(self.name), initializer=self.weight_initializer) self._V = self.add_weight(shape=(temporal_input_dim, 1), name="{}_V".format(self.name), initializer=self.weight_initializer) super(ExternalAttentionRNNWrapper, self).build()
Example #30
Source File: model.py From keras-yolo3 with MIT License | 5 votes |
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou