Python keras.backend.int_shape() Examples
The following are 30
code examples of keras.backend.int_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: timeception.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 6 votes |
def timeception_layers(tensor, n_layers=4, n_groups=8, is_dilated=True): input_shape = K.int_shape(tensor) assert len(input_shape) == 5 expansion_factor = 1.25 _, n_timesteps, side_dim, side_dim, n_channels_in = input_shape # how many layers of timeception for i in range(n_layers): layer_num = i + 1 # get details about grouping n_channels_per_branch, n_channels_out = __get_n_channels_per_branch(n_groups, expansion_factor, n_channels_in) # temporal conv per group tensor = __grouped_convolutions(tensor, n_groups, n_channels_per_branch, is_dilated, layer_num) # downsample over time tensor = MaxPooling3D(pool_size=(2, 1, 1), name='maxpool_tc%d' % (layer_num))(tensor) n_channels_in = n_channels_out return tensor
Example #2
Source File: timeception.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __call_timeception_layers(self, tensor, n_layers, n_groups, expansion_factor): input_shape = K.int_shape(tensor) assert len(input_shape) == 5 _, n_timesteps, side_dim, side_dim, n_channels_in = input_shape # how many layers of timeception for i in range(n_layers): layer_num = i + 1 # get details about grouping n_channels_per_branch, n_channels_out = self.__get_n_channels_per_branch(n_groups, expansion_factor, n_channels_in) # temporal conv per group tensor = self.__call_grouped_convolutions(tensor, n_groups, n_channels_per_branch, layer_num) # downsample over time tensor = getattr(self, 'maxpool_tc%d' % (layer_num))(tensor) n_channels_in = n_channels_out return tensor
Example #3
Source File: timeception.py From timeception with GNU General Public License v3.0 | 6 votes |
def timeception_layers(tensor, n_layers=4, n_groups=8, is_dilated=True): input_shape = K.int_shape(tensor) assert len(input_shape) == 5 expansion_factor = 1.25 _, n_timesteps, side_dim, side_dim, n_channels_in = input_shape # how many layers of timeception for i in range(n_layers): layer_num = i + 1 # get details about grouping n_channels_per_branch, n_channels_out = __get_n_channels_per_branch(n_groups, expansion_factor, n_channels_in) # temporal conv per group tensor = __grouped_convolutions(tensor, n_groups, n_channels_per_branch, is_dilated, layer_num) # downsample over time tensor = MaxPooling3D(pool_size=(2, 1, 1), name='maxpool_tc%d' % (layer_num))(tensor) n_channels_in = n_channels_out return tensor
Example #4
Source File: timeception.py From timeception with GNU General Public License v3.0 | 6 votes |
def __call_timeception_layers(self, tensor, n_layers, n_groups, expansion_factor): input_shape = K.int_shape(tensor) assert len(input_shape) == 5 _, n_timesteps, side_dim, side_dim, n_channels_in = input_shape # how many layers of timeception for i in range(n_layers): layer_num = i + 1 # get details about grouping n_channels_per_branch, n_channels_out = self.__get_n_channels_per_branch(n_groups, expansion_factor, n_channels_in) # temporal conv per group tensor = self.__call_grouped_convolutions(tensor, n_groups, n_channels_per_branch, layer_num) # downsample over time tensor = getattr(self, 'maxpool_tc%d' % (layer_num))(tensor) n_channels_in = n_channels_out return tensor
Example #5
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 #6
Source File: models.py From SeqGAN with MIT License | 6 votes |
def Highway(x, num_layers=1, activation='relu', name_prefix=''): ''' Layer wrapper function for Highway network # Arguments: x: tensor, shape = (B, input_size) # Optional Arguments: num_layers: int, dafault is 1, the number of Highway network layers activation: keras activation, default is 'relu' name_prefix: str, default is '', layer name prefix # Returns: out: tensor, shape = (B, input_size) ''' input_size = K.int_shape(x)[1] for i in range(num_layers): gate_ratio_name = '{}Highway/Gate_ratio_{}'.format(name_prefix, i) fc_name = '{}Highway/FC_{}'.format(name_prefix, i) gate_name = '{}Highway/Gate_{}'.format(name_prefix, i) gate_ratio = Dense(input_size, activation='sigmoid', name=gate_ratio_name)(x) fc = Dense(input_size, activation=activation, name=fc_name)(x) x = Lambda(lambda args: args[0] * args[2] + args[1] * (1 - args[2]), name=gate_name)([fc, x, gate_ratio]) return x
Example #7
Source File: recurrent.py From keras_bn_library with MIT License | 6 votes |
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.input_dim)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) if 0 < self.dropout_W < 1: input_shape = K.int_shape(x) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) return constants
Example #8
Source File: vae.py From pyod with BSD 2-Clause "Simplified" License | 6 votes |
def sampling(self, args): """Reparametrisation by sampling from Gaussian, N(0,I) To sample from epsilon = Norm(0,I) instead of from likelihood Q(z|X) with latent variables z: z = z_mean + sqrt(var) * epsilon Parameters ---------- args : tensor Mean and log of variance of Q(z|X). Returns ------- z : tensor Sampled latent variable. """ z_mean, z_log = args batch = K.shape(z_mean)[0] # batch size dim = K.int_shape(z_mean)[1] # latent dimension epsilon = K.random_normal(shape=(batch, dim)) # mean=0, std=1.0 return z_mean + K.exp(0.5 * z_log) * epsilon
Example #9
Source File: instance_normalization.py From Coloring-greyscale-images with MIT License | 6 votes |
def call(self, inputs, training=None): input_shape = K.int_shape(inputs) reduction_axes = list(range(0, len(input_shape))) if (self.axis is not None): del reduction_axes[self.axis] del reduction_axes[0] mean = K.mean(inputs, reduction_axes, keepdims=True) stddev = K.std(inputs, reduction_axes, keepdims=True) + self.epsilon normed = (inputs - mean) / stddev broadcast_shape = [1] * len(input_shape) if self.axis is not None: broadcast_shape[self.axis] = input_shape[self.axis] if self.scale: broadcast_gamma = K.reshape(self.gamma, broadcast_shape) normed = normed * broadcast_gamma if self.center: broadcast_beta = K.reshape(self.beta, broadcast_shape) normed = normed + broadcast_beta return normed
Example #10
Source File: decoders.py From keras-fcn with MIT License | 6 votes |
def VGGUpsampler(pyramid, scales, classes, weight_decay=0.): """A Functional upsampler for the VGG Nets. :param: pyramid: A list of features in pyramid, scaling from large receptive field to small receptive field. The bottom of the pyramid is the input image. :param: scales: A list of weights for each of the feature map in the pyramid, sorted in the same order as the pyramid. :param: classes: Integer, number of classes. """ if len(scales) != len(pyramid) - 1: raise ValueError('`scales` needs to match the length of' '`pyramid` - 1.') blocks = [] for i in range(len(pyramid) - 1): block_name = 'feat{}'.format(i + 1) block = vgg_upsampling(classes=classes, target_shape=K.int_shape(pyramid[i + 1]), scale=scales[i], weight_decay=weight_decay, block_name=block_name) blocks.append(block) return Decoder(pyramid=pyramid[:-1], blocks=blocks)
Example #11
Source File: test_decoders.py From keras-fcn with MIT License | 6 votes |
def test_vgg_decoder(): if K.image_data_format() == 'channels_last': inputs = Input(shape=(500, 500, 3)) pool3 = Input(shape=(88, 88, 256)) pool4 = Input(shape=(44, 44, 512)) drop7 = Input(shape=(16, 16, 4096)) score_shape = (None, 500, 500, 21) else: inputs = Input(shape=(3, 500, 500)) pool3 = Input(shape=(256, 88, 88)) pool4 = Input(shape=(512, 44, 44)) drop7 = Input(shape=(4096, 16, 16)) score_shape = (None, 21, 500, 500) pyramid = [drop7, pool4, pool3, inputs] scales = [1., 1e-2, 1e-4] score = VGGDecoder(pyramid, scales, classes=21) assert K.int_shape(score) == score_shape
Example #12
Source File: test_blocks.py From keras-fcn with MIT License | 6 votes |
def test_vgg_conv(): if K.image_data_format() == 'channels_first': x = Input(shape=(3, 224, 224)) y1_shape = (None, 64, 112, 112) y2_shape = (None, 128, 56, 56) else: x = Input(shape=(224, 224, 3)) y1_shape = (None, 112, 112, 64) y2_shape = (None, 56, 56, 128) block1 = vgg_conv(filters=64, convs=2, block_name='block1') y = block1(x) assert K.int_shape(y) == y1_shape block2 = vgg_conv(filters=128, convs=2, block_name='block2') y = block2(y) assert K.int_shape(y) == y2_shape
Example #13
Source File: normalizations.py From se_relativisticgan with MIT License | 6 votes |
def call(self, inputs, training=None): input_shape = K.int_shape(inputs) reduction_axes = list(range(0, len(input_shape))) if (self.axis is not None): del reduction_axes[self.axis] del reduction_axes[0] mean = K.mean(inputs, reduction_axes, keepdims=True) stddev = K.std(inputs, reduction_axes, keepdims=True) + self.epsilon normed = (inputs - mean) / stddev broadcast_shape = [1] * len(input_shape) if self.axis is not None: broadcast_shape[self.axis] = input_shape[self.axis] if self.scale: broadcast_gamma = K.reshape(self.gamma, broadcast_shape) normed = normed * broadcast_gamma if self.center: broadcast_beta = K.reshape(self.beta, broadcast_shape) normed = normed + broadcast_beta return normed
Example #14
Source File: hourglass.py From keras-centernet with MIT License | 6 votes |
def residual(_x, out_dim, name, stride=1): shortcut = _x num_channels = K.int_shape(shortcut)[-1] _x = ZeroPadding2D(padding=1, name=name + '.pad1')(_x) _x = Conv2D(out_dim, 3, strides=stride, use_bias=False, name=name + '.conv1')(_x) _x = BatchNormalization(epsilon=1e-5, name=name + '.bn1')(_x) _x = Activation('relu', name=name + '.relu1')(_x) _x = Conv2D(out_dim, 3, padding='same', use_bias=False, name=name + '.conv2')(_x) _x = BatchNormalization(epsilon=1e-5, name=name + '.bn2')(_x) if num_channels != out_dim or stride != 1: shortcut = Conv2D(out_dim, 1, strides=stride, use_bias=False, name=name + '.shortcut.0')( shortcut) shortcut = BatchNormalization(epsilon=1e-5, name=name + '.shortcut.1')(shortcut) _x = Add(name=name + '.add')([_x, shortcut]) _x = Activation('relu', name=name + '.relu')(_x) return _x
Example #15
Source File: model_utils.py From image-segmentation-keras with MIT License | 6 votes |
def resize_image(inp, s, data_format): try: return Lambda(lambda x: K.resize_images(x, height_factor=s[0], width_factor=s[1], data_format=data_format, interpolation='bilinear'))(inp) except Exception as e: # if keras is old, then rely on the tf function # Sorry theano/cntk users!!! assert data_format == 'channels_last' assert IMAGE_ORDERING == 'channels_last' import tensorflow as tf return Lambda( lambda x: tf.image.resize_images( x, (K.int_shape(x)[1]*s[0], K.int_shape(x)[2]*s[1])) )(inp)
Example #16
Source File: pspnet.py From image-segmentation-keras with MIT License | 6 votes |
def pool_block(feats, pool_factor): if IMAGE_ORDERING == 'channels_first': h = K.int_shape(feats)[2] w = K.int_shape(feats)[3] elif IMAGE_ORDERING == 'channels_last': h = K.int_shape(feats)[1] w = K.int_shape(feats)[2] pool_size = strides = [ int(np.round(float(h) / pool_factor)), int(np.round(float(w) / pool_factor))] x = AveragePooling2D(pool_size, data_format=IMAGE_ORDERING, strides=strides, padding='same')(feats) x = Conv2D(512, (1, 1), data_format=IMAGE_ORDERING, padding='same', use_bias=False)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = resize_image(x, strides, data_format=IMAGE_ORDERING) return x
Example #17
Source File: custom_objects.py From keras_mixnets with MIT License | 5 votes |
def call(self, inputs, **kwargs): if len(self._layers) == 1: return self._layers[0](inputs) filters = K.int_shape(inputs)[self._channel_axis] splits = self._split_channels(filters, self.groups) x_splits = tf.split(inputs, splits, self._channel_axis) x_outputs = [c(x) for x, c in zip(x_splits, self._layers)] x = layers.concatenate(x_outputs, axis=self._channel_axis) return x
Example #18
Source File: mixnets.py From keras_mixnets with MIT License | 5 votes |
def __call__(self, inputs): filters = K.int_shape(inputs)[self._channel_axis] grouped_op = GroupConvolution(filters, self.kernels, groups=len(self.kernels), type='depthwise_conv', conv_kwargs=self._conv_kwargs) x = grouped_op(inputs) return x # Obtained from https://github.com/tensorflow/tpu/blob/master/models/official/mnasnet/mixnet/mixnet_model.py
Example #19
Source File: hlnet.py From Face-skin-hair-segmentaiton-and-skin-color-evaluation with Apache License 2.0 | 5 votes |
def _bottleneck(inputs, filters, kernel, t, s, r=False): """Bottleneck This function defines a basic bottleneck structure. # Arguments inputs: Tensor, input tensor of conv layer. filters: Integer, the dimensionality of the output space. kernel: An integer or tuple/list of 2 integers, specifying the width and height of the 2D convolution window. t: Integer, expansion factor. t is always applied to the input size. s: An integer or tuple/list of 2 integers,specifying the strides of the convolution along the width and height.Can be a single integer to specify the same value for all spatial dimensions. r: Boolean, Whether to use the residuals. # Returns Output tensor. """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 tchannel = K.int_shape(inputs)[channel_axis] * t x = _conv_block(inputs, tchannel, (1, 1)) x = DepthwiseConv2D(kernel, strides=( s, s), depth_multiplier=1, padding='same')(x) x = BatchNormalization(axis=channel_axis)(x) # relu6 x = ReLU(max_value=6)(x) x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same')(x) x = BatchNormalization(axis=channel_axis)(x) if r: x = add([x, inputs]) return x
Example #20
Source File: losses.py From gandlf with MIT License | 5 votes |
def rbf_moment_matching(y_true, y_pred, sigmas=[2, 5, 10, 20, 40, 80]): """Generative moment matching loss with RBF kernel. Reference: https://arxiv.org/abs/1502.02761 """ warnings.warn('Moment matching loss is still in development.') if len(K.int_shape(y_pred)) != 2 or len(K.int_shape(y_true)) != 2: raise ValueError('RBF Moment Matching function currently only works ' 'for outputs with shape (batch_size, num_features).' 'Got y_true="%s" and y_pred="%s".' % (str(K.int_shape(y_pred)), str(K.int_shape(y_true)))) sigmas = list(sigmas) if isinstance(sigmas, (list, tuple)) else [sigmas] x = K.concatenate([y_pred, y_true], 0) # Performs dot product between all combinations of rows in X. xx = K.dot(x, K.transpose(x)) # (batch_size, batch_size) # Performs dot product of all rows with themselves. x2 = K.sum(x * x, 1, keepdims=True) # (batch_size, None) # Gets exponent entries of the RBF kernel (without sigmas). exponent = xx - 0.5 * x2 - 0.5 * K.transpose(x2) # Applies all the sigmas. total_loss = None for sigma in sigmas: kernel_val = K.exp(exponent / sigma) loss = K.sum(kernel_val) total_loss = loss if total_loss is None else loss + total_loss return total_loss
Example #21
Source File: attention.py From gandlf with MIT License | 5 votes |
def __init__(self, layer, attention, attn_activation='tanh', attn_gate_func='sigmoid', W_regularizer=None, b_regularizer=None, **kwargs): if not isinstance(layer, keras.layers.Recurrent): raise ValueError('The RecurrentAttention wrapper only works on ' 'recurrent layers.') # Should know this so that we can handle multiple hidden states. self._wraps_lstm = isinstance(layer, keras.layers.LSTM) if not hasattr(attention, '_keras_shape'): raise ValueError('Attention should be a Keras tensor.') if len(K.int_shape(attention)) != 2: raise ValueError('The attention input for RecurrentAttention2D ' 'should be a tensor with shape (batch_size, ' 'num_features). Got shape=%s.' % str(K.int_shape(attention))) self.supports_masking = True self.attention = attention self.attn_activation = keras.activations.get(attn_activation) self.attn_gate_func = keras.activations.get(attn_gate_func) self.W_regularizer = keras.regularizers.get(W_regularizer) self.b_regularizer = keras.regularizers.get(b_regularizer) super(RecurrentAttention1D, self).__init__(layer, **kwargs)
Example #22
Source File: attention.py From gandlf with MIT License | 5 votes |
def __init__(self, layer, attention, time_dist_activation='softmax', attn_gate_func='sigmoid', W_regularizer=None, b_regularizer=None, **kwargs): if not isinstance(layer, keras.layers.Recurrent): raise ValueError('The RecurrentAttention wrapper only works on ' 'recurrent layers.') # Should know this so that we can handle multiple hidden states. self._wraps_lstm = isinstance(layer, keras.layers.LSTM) if not hasattr(attention, '_keras_shape'): raise ValueError('Attention should be a Keras tensor.') if len(K.int_shape(attention)) != 3: raise ValueError('The attention input for RecurrentAttention2D ' 'should be a tensor with shape (batch_size, ' 'num_timesteps, num_features). Got shape=%s.' % str(K.int_shape(attention))) self.supports_masking = True self.attention = attention self.time_dist_activation = keras.activations.get(time_dist_activation) self.attn_gate_func = keras.activations.get(attn_gate_func) self.W_regularizer = keras.regularizers.get(W_regularizer) self.b_regularizer = keras.regularizers.get(b_regularizer) super(RecurrentAttention2D, self).__init__(layer, **kwargs)
Example #23
Source File: attention.py From gandlf with MIT License | 5 votes |
def build(self, input_shape): assert input_shape >= 3 self.input_spec = [keras.engine.InputSpec(shape=input_shape)] # Builds the wrapped layer. if not self.layer.built: self.layer.build(input_shape) super(RecurrentAttention2D, self).build() num_attn_timesteps, num_attn_feats = K.int_shape(self.attention)[1:] output_dim = self.layer.output_dim self.attn_U_t = self.add_weight((output_dim, num_attn_timesteps), initializer=self.layer.inner_init, name='{}_attn_U_t'.format(self.name), regularizer=self.W_regularizer) self.attn_b_t = self.add_weight((num_attn_timesteps,), initializer='zero', name='{}_attn_b_t'.format(self.name), regularizer=self.b_regularizer) self.attn_U_a = self.add_weight((num_attn_feats, output_dim), initializer=self.layer.inner_init, name='{}_attn_U_a'.format(self.name), regularizer=self.W_regularizer) self.attn_b_a = self.add_weight((output_dim,), initializer='zero', name='{}_attn_b_a'.format(self.name), regularizer=self.b_regularizer) self.trainable_weights = [self.attn_U_t, self.attn_b_t, self.attn_U_a, self.attn_b_a]
Example #24
Source File: FixedBatchNormalization.py From FasterRCNN_KERAS with Apache License 2.0 | 5 votes |
def call(self, x, mask=None): assert self.built, 'Layer must be built before being called' input_shape = K.int_shape(x) reduction_axes = list(range(len(input_shape))) del reduction_axes[self.axis] broadcast_shape = [1] * len(input_shape) broadcast_shape[self.axis] = input_shape[self.axis] if sorted(reduction_axes) == range(K.ndim(x))[:-1]: x_normed = K.batch_normalization( x, self.running_mean, self.running_std, self.beta, self.gamma, epsilon=self.epsilon) else: # need broadcasting broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape) broadcast_running_std = K.reshape(self.running_std, broadcast_shape) broadcast_beta = K.reshape(self.beta, broadcast_shape) broadcast_gamma = K.reshape(self.gamma, broadcast_shape) x_normed = K.batch_normalization( x, broadcast_running_mean, broadcast_running_std, broadcast_beta, broadcast_gamma, epsilon=self.epsilon) return x_normed
Example #25
Source File: model.py From dataiku-contrib with Apache License 2.0 | 5 votes |
def mrcnn_bbox_loss_graph(target_bbox, target_class_ids, pred_bbox): """Loss for Mask R-CNN bounding box refinement. target_bbox: [batch, num_rois, (dy, dx, log(dh), log(dw))] target_class_ids: [batch, num_rois]. Integer class IDs. pred_bbox: [batch, num_rois, num_classes, (dy, dx, log(dh), log(dw))] """ # Reshape to merge batch and roi dimensions for simplicity. target_class_ids = K.reshape(target_class_ids, (-1,)) target_bbox = K.reshape(target_bbox, (-1, 4)) pred_bbox = K.reshape(pred_bbox, (-1, K.int_shape(pred_bbox)[2], 4)) # Only positive ROIs contribute to the loss. And only # the right class_id of each ROI. Get their indices. positive_roi_ix = tf.where(target_class_ids > 0)[:, 0] positive_roi_class_ids = tf.cast( tf.gather(target_class_ids, positive_roi_ix), tf.int64) indices = tf.stack([positive_roi_ix, positive_roi_class_ids], axis=1) # Gather the deltas (predicted and true) that contribute to loss target_bbox = tf.gather(target_bbox, positive_roi_ix) pred_bbox = tf.gather_nd(pred_bbox, indices) # Smooth-L1 Loss loss = K.switch(tf.size(target_bbox) > 0, smooth_l1_loss(y_true=target_bbox, y_pred=pred_bbox), tf.constant(0.0)) loss = K.mean(loss) return loss
Example #26
Source File: pixel_shuffler.py From df with Mozilla Public License 2.0 | 5 votes |
def call(self, inputs): input_shape = K.int_shape(inputs) if len(input_shape) != 4: raise ValueError('Inputs should have rank ' + str(4) + '; Received input shape:', str(input_shape)) if self.data_format == 'channels_first': batch_size, c, h, w = input_shape if batch_size is None: batch_size = -1 rh, rw = self.size oh, ow = h * rh, w * rw oc = c // (rh * rw) out = K.reshape(inputs, (batch_size, rh, rw, oc, h, w)) out = K.permute_dimensions(out, (0, 3, 4, 1, 5, 2)) out = K.reshape(out, (batch_size, oc, oh, ow)) return out elif self.data_format == 'channels_last': batch_size, h, w, c = input_shape if batch_size is None: batch_size = -1 rh, rw = self.size oh, ow = h * rh, w * rw oc = c // (rh * rw) out = K.reshape(inputs, (batch_size, h, w, rh, rw, oc)) out = K.permute_dimensions(out, (0, 1, 3, 2, 4, 5)) out = K.reshape(out, (batch_size, oh, ow, oc)) return out
Example #27
Source File: exampleTrainer.py From df with Mozilla Public License 2.0 | 5 votes |
def call(self, inputs): input_shape = K.int_shape(inputs) if len(input_shape) != 4: raise ValueError('Inputs should have rank ' + str(4) + '; Received input shape:', str(input_shape)) if self.data_format == 'channels_first': batch_size, c, h, w = input_shape if batch_size is None: batch_size = -1 rh, rw = self.size oh, ow = h * rh, w * rw oc = c // (rh * rw) out = K.reshape(inputs, (batch_size, rh, rw, oc, h, w)) out = K.permute_dimensions(out, (0, 3, 4, 1, 5, 2)) out = K.reshape(out, (batch_size, oc, oh, ow)) return out elif self.data_format == 'channels_last': batch_size, h, w, c = input_shape if batch_size is None: batch_size = -1 rh, rw = self.size oh, ow = h * rh, w * rw oc = c // (rh * rw) out = K.reshape(inputs, (batch_size, h, w, rh, rw, oc)) out = K.permute_dimensions(out, (0, 1, 3, 2, 4, 5)) out = K.reshape(out, (batch_size, oh, ow, oc)) return out
Example #28
Source File: classifiers.py From AIX360 with Apache License 2.0 | 5 votes |
def __init__(self, model, input_layer=0, output_layer=0): """Initialize KerasClassifier. Args: model: a trained keras classifier model. """ import keras.backend as k super(KerasClassifier, self).__init__() self._model = model if hasattr(model, 'inputs'): self._input = model.inputs[input_layer] else: self._input = model.input if hasattr(model, 'outputs'): self._output = model.outputs[output_layer] else: self._output = model.output _, self._nb_classes = k.int_shape(self._output) self._input_shape = k.int_shape(self._input)[1:]
Example #29
Source File: visualizer.py From backdoor with MIT License | 5 votes |
def reset_opt(self): K.set_value(self.opt.iterations, 0) for w in self.opt.weights: K.set_value(w, np.zeros(K.int_shape(w))) pass
Example #30
Source File: model.py From PanopticSegmentation with MIT License | 5 votes |
def mrcnn_bbox_loss_graph(target_bbox, target_class_ids, pred_bbox): """Loss for Mask R-CNN bounding box refinement. target_bbox: [batch, num_rois, (dy, dx, log(dh), log(dw))] target_class_ids: [batch, num_rois]. Integer class IDs. pred_bbox: [batch, num_rois, num_classes, (dy, dx, log(dh), log(dw))] """ # Reshape to merge batch and roi dimensions for simplicity. target_class_ids = K.reshape(target_class_ids, (-1,)) target_bbox = K.reshape(target_bbox, (-1, 4)) pred_bbox = K.reshape(pred_bbox, (-1, K.int_shape(pred_bbox)[2], 4)) # Only positive ROIs contribute to the loss. And only # the right class_id of each ROI. Get their indices. positive_roi_ix = tf.where(target_class_ids > 0)[:, 0] positive_roi_class_ids = tf.cast( tf.gather(target_class_ids, positive_roi_ix), tf.int64) indices = tf.stack([positive_roi_ix, positive_roi_class_ids], axis=1) # Gather the deltas (predicted and true) that contribute to loss target_bbox = tf.gather(target_bbox, positive_roi_ix) pred_bbox = tf.gather_nd(pred_bbox, indices) # Smooth-L1 Loss loss = K.switch(tf.size(target_bbox) > 0, smooth_l1_loss(y_true=target_bbox, y_pred=pred_bbox), tf.constant(0.0)) loss = K.mean(loss) return loss