Python tensorflow.keras.backend.image_data_format() Examples
The following are 30
code examples of tensorflow.keras.backend.image_data_format().
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
tensorflow.keras.backend
, or try the search function
.
Example #1
Source File: dual_path_network.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 6 votes |
def _bn_relu_conv_block(input, filters, kernel=(3, 3), stride=(1, 1), weight_decay=5e-4): ''' Adds a Batchnorm-Relu-Conv block for DPN Args: input: input tensor filters: number of output filters kernel: convolution kernel size stride: stride of convolution Returns: a keras tensor ''' channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 x = Conv2D(filters, kernel, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay), strides=stride)(input) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) return x
Example #2
Source File: time_frequency.py From kapre with MIT License | 6 votes |
def call(self, x): power_spectrogram = super(Melspectrogram, self).call(x) # now, channels_first: (batch_sample, n_ch, n_freq, n_time) # channels_last: (batch_sample, n_freq, n_time, n_ch) if self.image_data_format == 'channels_first': power_spectrogram = K.permute_dimensions(power_spectrogram, [0, 1, 3, 2]) else: power_spectrogram = K.permute_dimensions(power_spectrogram, [0, 3, 2, 1]) # now, whatever image_data_format, (batch_sample, n_ch, n_time, n_freq) output = K.dot(power_spectrogram, self.freq2mel) if self.image_data_format == 'channels_first': output = K.permute_dimensions(output, [0, 1, 3, 2]) else: output = K.permute_dimensions(output, [0, 3, 2, 1]) if self.power_melgram != 2.0: output = K.pow(K.sqrt(output), self.power_melgram) if self.return_decibel_melgram: output = backend_keras.amplitude_to_decibel(output) return output
Example #3
Source File: mobilenet_base.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 6 votes |
def _conv_block(self, inputs, filters, kernel, strides, nl): """Convolution Block This function defines a 2D convolution operation with BN and activation. # 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. strides: 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. nl: String, nonlinearity activation type. # Returns Output tensor. """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 x = Conv2D(filters, kernel, padding='same', strides=strides)(inputs) x = BatchNormalization(axis=channel_axis)(x) return self._return_activation(x, nl)
Example #4
Source File: utils.py From kapre with MIT License | 6 votes |
def __init__( self, win_length: int = 5, mode: str = 'symmetric', data_format: str = 'default', **kwargs ): assert data_format in ('default', 'channels_first', 'channels_last') assert win_length >= 3 assert mode.lower() in ('symmetric', 'reflect', 'constant') if data_format == 'default': self.data_format = K.image_data_format() else: self.data_format = data_format self.win_length = win_length self.mode = mode super(Delta, self).__init__(**kwargs)
Example #5
Source File: coord.py From keras-coordconv with MIT License | 6 votes |
def __init__(self, rank, use_radius=False, data_format=None, **kwargs): super(_CoordinateChannel, self).__init__(**kwargs) if data_format not in [None, 'channels_first', 'channels_last']: raise ValueError('`data_format` must be either "channels_last", "channels_first" ' 'or None.') self.rank = rank self.use_radius = use_radius self.data_format = K.image_data_format() if data_format is None else data_format self.axis = 1 if K.image_data_format() == 'channels_first' else -1 self.input_spec = InputSpec(min_ndim=2) self.supports_masking = True
Example #6
Source File: se_resnext.py From keras-squeeze-excite-network with MIT License | 6 votes |
def __initial_conv_block_inception(input_tensor, weight_decay=5e-4): """ Adds an initial conv block, with batch norm and relu for the inception resnext Args: input_tensor: input Keras tensor weight_decay: weight decay factor Returns: a Keras tensor """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 x = Conv2D(64, (7, 7), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay), strides=(2, 2))(input_tensor) x = BatchNormalization(axis=channel_axis)(x) x = LeakyReLU()(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) return x
Example #7
Source File: mobilenet_v3.py From keras-YOLOv3-model-set with MIT License | 6 votes |
def _se_block(inputs, filters, se_ratio, prefix): x = GlobalAveragePooling2D(name=prefix + 'squeeze_excite/AvgPool')(inputs) if K.image_data_format() == 'channels_first': x = Reshape((filters, 1, 1))(x) else: x = Reshape((1, 1, filters))(x) x = Conv2D(_depth(filters * se_ratio), kernel_size=1, padding='same', name=prefix + 'squeeze_excite/Conv')(x) x = ReLU(name=prefix + 'squeeze_excite/Relu')(x) x = Conv2D(filters, kernel_size=1, padding='same', name=prefix + 'squeeze_excite/Conv_1')(x) x = Activation(hard_sigmoid)(x) #if K.backend() == 'theano': ## For the Theano backend, we have to explicitly make ## the excitation weights broadcastable. #x = Lambda( #lambda br: K.pattern_broadcast(br, [True, True, True, False]), #output_shape=lambda input_shape: input_shape, #name=prefix + 'squeeze_excite/broadcast')(x) x = Multiply(name=prefix + 'squeeze_excite/Mul')([inputs, x]) return x
Example #8
Source File: mobilenet_v3.py From keras-YOLOv3-model-set with MIT License | 6 votes |
def correct_pad(backend, inputs, kernel_size): """Returns a tuple for zero-padding for 2D convolution with downsampling. # Arguments input_size: An integer or tuple/list of 2 integers. kernel_size: An integer or tuple/list of 2 integers. # Returns A tuple. """ img_dim = 2 if backend.image_data_format() == 'channels_first' else 1 input_size = backend.int_shape(inputs)[img_dim:(img_dim + 2)] if isinstance(kernel_size, int): kernel_size = (kernel_size, kernel_size) if input_size[0] is None: adjust = (1, 1) else: adjust = (1 - input_size[0] % 2, 1 - input_size[1] % 2) correct = (kernel_size[0] // 2, kernel_size[1] // 2) return ((correct[0] - adjust[0], correct[0]), (correct[1] - adjust[1], correct[1]))
Example #9
Source File: squeezenet.py From keras-YOLOv3-model-set with MIT License | 6 votes |
def fire_module(x, fire_id, squeeze=16, expand=64): s_id = 'fire' + str(fire_id) + '/' if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = 3 x = Conv2D(squeeze, (1, 1), padding='valid', name=s_id + sq1x1)(x) x = Activation('relu', name=s_id + relu + sq1x1)(x) left = Conv2D(expand, (1, 1), padding='valid', name=s_id + exp1x1)(x) left = Activation('relu', name=s_id + relu + exp1x1)(left) right = Conv2D(expand, (3, 3), padding='same', name=s_id + exp3x3)(x) right = Activation('relu', name=s_id + relu + exp3x3)(right) x = concatenate([left, right], axis=channel_axis, name=s_id + 'concat') return x # Original SqueezeNet from paper.
Example #10
Source File: yolo3_nano.py From keras-YOLOv3-model-set with MIT License | 6 votes |
def correct_pad(backend, inputs, kernel_size): """Returns a tuple for zero-padding for 2D convolution with downsampling. # Arguments input_size: An integer or tuple/list of 2 integers. kernel_size: An integer or tuple/list of 2 integers. # Returns A tuple. """ img_dim = 2 if backend.image_data_format() == 'channels_first' else 1 input_size = backend.int_shape(inputs)[img_dim:(img_dim + 2)] if isinstance(kernel_size, int): kernel_size = (kernel_size, kernel_size) if input_size[0] is None: adjust = (1, 1) else: adjust = (1 - input_size[0] % 2, 1 - input_size[1] % 2) correct = (kernel_size[0] // 2, kernel_size[1] // 2) return ((correct[0] - adjust[0], correct[0]), (correct[1] - adjust[1], correct[1]))
Example #11
Source File: inception_resnet_v1.py From TripletLossFace with MIT License | 6 votes |
def conv2d_bn(x, filters, kernel_size, strides=1, padding='same', activation='relu', use_bias=False, name=None): x = Conv2D(filters, kernel_size, strides=strides, padding=padding, use_bias=use_bias, name=name)(x) if not use_bias: bn_axis = 1 if K.image_data_format() == 'channels_first' else 3 bn_name = _generate_layer_name('BatchNorm', prefix=name) x = BatchNormalization(axis=bn_axis, momentum=0.995, epsilon=0.001, scale=False, name=bn_name)(x) if activation is not None: ac_name = _generate_layer_name('Activation', prefix=name) x = Activation(activation, name=ac_name)(x) return x
Example #12
Source File: mobilenet_base.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def _bottleneck(self, inputs, filters, kernel, e, s, squeeze, nl): """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. e: 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. squeeze: Boolean, Whether to use the squeeze. nl: String, nonlinearity activation type. # Returns Output tensor. """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 input_shape = K.int_shape(inputs) tchannel = input_shape[channel_axis] * e r = s == 1 and input_shape[3] == filters x = self._conv_block(inputs, tchannel, (1, 1), (1, 1), nl) x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x) x = BatchNormalization(axis=channel_axis)(x) if squeeze: x = Lambda(lambda x: x * self._squeeze(x))(x) x = self._return_activation(x, nl) 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 #13
Source File: shufflenet_v2.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def shuffle_unit(inputs, out_channels, bottleneck_ratio,strides=2,stage=1,block=1): if K.image_data_format() == 'channels_last': bn_axis = -1 else: raise ValueError('Only channels last supported') prefix = 'stage{}/block{}'.format(stage, block) bottleneck_channels = int(out_channels * bottleneck_ratio) if strides < 2: c_hat, c = channel_split(inputs, '{}/spl'.format(prefix)) inputs = c x = Conv2D(bottleneck_channels, kernel_size=(1,1), strides=1, padding='same', name='{}/1x1conv_1'.format(prefix))(inputs) x = BatchNormalization(axis=bn_axis, name='{}/bn_1x1conv_1'.format(prefix))(x) x = Activation('relu', name='{}/relu_1x1conv_1'.format(prefix))(x) x = DepthwiseConv2D(kernel_size=3, strides=strides, padding='same', name='{}/3x3dwconv'.format(prefix))(x) x = BatchNormalization(axis=bn_axis, name='{}/bn_3x3dwconv'.format(prefix))(x) x = Conv2D(bottleneck_channels, kernel_size=1,strides=1,padding='same', name='{}/1x1conv_2'.format(prefix))(x) x = BatchNormalization(axis=bn_axis, name='{}/bn_1x1conv_2'.format(prefix))(x) x = Activation('relu', name='{}/relu_1x1conv_2'.format(prefix))(x) if strides < 2: ret = Concatenate(axis=bn_axis, name='{}/concat_1'.format(prefix))([x, c_hat]) else: s2 = DepthwiseConv2D(kernel_size=3, strides=2, padding='same', name='{}/3x3dwconv_2'.format(prefix))(inputs) s2 = BatchNormalization(axis=bn_axis, name='{}/bn_3x3dwconv_2'.format(prefix))(s2) s2 = Conv2D(bottleneck_channels, kernel_size=1,strides=1,padding='same', name='{}/1x1_conv_3'.format(prefix))(s2) s2 = BatchNormalization(axis=bn_axis, name='{}/bn_1x1conv_3'.format(prefix))(s2) s2 = Activation('relu', name='{}/relu_1x1conv_3'.format(prefix))(s2) ret = Concatenate(axis=bn_axis, name='{}/concat_2'.format(prefix))([x, s2]) ret = Lambda(channel_shuffle, name='{}/channel_shuffle'.format(prefix))(ret) return ret
Example #14
Source File: dual_path_network.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def _grouped_convolution_block(input, grouped_channels, cardinality, strides, weight_decay=5e-4): ''' Adds a grouped convolution block. It is an equivalent block from the paper Args: input: input tensor grouped_channels: grouped number of filters cardinality: cardinality factor describing the number of groups strides: performs strided convolution for downscaling if > 1 weight_decay: weight decay term Returns: a keras tensor ''' init = input channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 group_list = [] if cardinality == 1: # with cardinality 1, it is a standard convolution x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=strides, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(init) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) return x for c in range(cardinality): x = Lambda(lambda z: z[:, :, :, c * grouped_channels:(c + 1) * grouped_channels] if K.image_data_format() == 'channels_last' else lambda z: z[:, c * grouped_channels:(c + 1) * grouped_channels, :, :])(input) x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=strides, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x) group_list.append(x) group_merge = concatenate(group_list, axis=channel_axis) group_merge = BatchNormalization(axis=channel_axis)(group_merge) group_merge = Activation('relu')(group_merge) return group_merge
Example #15
Source File: dual_path_network.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def preprocess_input(x, data_format=None): """Preprocesses a tensor encoding a batch of images. Obtained from https://github.com/cypw/DPNs # Arguments x: input Numpy tensor, 4D. data_format: data format of the image tensor. # Returns Preprocessed tensor. """ if data_format is None: data_format = K.image_data_format() assert data_format in {'channels_last', 'channels_first'} if data_format == 'channels_first': # 'RGB'->'BGR' x = x[:, ::-1, :, :] # Zero-center by mean pixel x[:, 0, :, :] -= 104 x[:, 1, :, :] -= 117 x[:, 2, :, :] -= 128 else: # 'RGB'->'BGR' x = x[:, :, :, ::-1] # Zero-center by mean pixel x[:, :, :, 0] -= 104 x[:, :, :, 1] -= 117 x[:, :, :, 2] -= 124 x *= 0.0167 return x
Example #16
Source File: se.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def squeeze_excite_block(input, ratio=16): ''' Create a channel-wise squeeze-excite block Args: input: input tensor filters: number of output filters Returns: a keras tensor References - [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507) ''' init = input channel_axis = 1 if K.image_data_format() == "channels_first" else -1 filters = init._keras_shape[channel_axis] se_shape = (1, 1, filters) se = GlobalAveragePooling2D()(init) se = Reshape(se_shape)(se) se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se) se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se) if K.image_data_format() == 'channels_first': se = Permute((3, 1, 2))(se) x = multiply([init, se]) return x
Example #17
Source File: deeplabcut.py From DeepPoseKit with Apache License 2.0 | 5 votes |
def preprocess_input(x, data_format=None, mode="caffe", **kwargs): """Preprocesses a tensor or Numpy array encoding a batch of images. # Arguments x: Input Numpy or symbolic tensor, 3D or 4D. The preprocessed data is written over the input data if the data types are compatible. To avoid this behaviour, `numpy.copy(x)` can be used. data_format: Data format of the image tensor/array. mode: One of "caffe", "tf" or "torch". - caffe: will convert the images from RGB to BGR, then will zero-center each color channel with respect to the ImageNet dataset, without scaling. - tf: will scale pixels between -1 and 1, sample-wise. - torch: will scale pixels between 0 and 1 and then will normalize each channel with respect to the ImageNet dataset. # Returns Preprocessed tensor or Numpy array. # Raises ValueError: In case of unknown `data_format` argument. """ if data_format is None: data_format = backend.image_data_format() if data_format not in {"channels_first", "channels_last"}: raise ValueError("Unknown data_format " + str(data_format)) return _preprocess_symbolic_input(x, data_format=data_format, mode=mode, **kwargs)
Example #18
Source File: SEResNeXt.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def __init__(self, size=96, num_classes=10, depth=64, reduction_ratio=4, num_split=8, num_block=3): self.depth = depth # number of channels self.ratio = reduction_ratio # ratio of channel reduction in SE module self.num_split = num_split # number of splitting trees for ResNeXt (so called cardinality) self.num_block = num_block # number of residual blocks if K.image_data_format() == 'channels_first': self.channel_axis = 1 else: self.channel_axis = 3 self.model = self.build_model(Input(shape=(size,size,3)), num_classes)
Example #19
Source File: backend.py From DeepPoseKit with Apache License 2.0 | 5 votes |
def depth_to_space(input, scale, data_format=None): """ Uses phase shift algorithm to convert channels/depth to spatial resolution """ if data_format is None: data_format = K.image_data_format() data_format = data_format.lower() input = _preprocess_conv2d_input(input, data_format) out = tf.nn.depth_to_space(input, scale) out = _postprocess_conv2d_output(out, data_format) return out
Example #20
Source File: squeeze_excitation.py From DeepPoseKit with Apache License 2.0 | 5 votes |
def channel_squeeze_excite_block(input, ratio=0.25): init = input channel_axis = 1 if K.image_data_format() == "channels_first" else -1 filters = init._keras_shape[channel_axis] cse_shape = (1, 1, filters) cse = layers.GlobalAveragePooling2D()(init) cse = layers.Reshape(cse_shape)(cse) ratio_filters = int(np.round(filters * ratio)) if ratio_filters < 1: ratio_filters += 1 cse = layers.Conv2D( ratio_filters, (1, 1), padding="same", activation="relu", kernel_initializer="he_normal", use_bias=False, )(cse) cse = layers.BatchNormalization()(cse) cse = layers.Conv2D( filters, (1, 1), activation="sigmoid", kernel_initializer="he_normal", use_bias=False, )(cse) if K.image_data_format() == "channels_first": cse = layers.Permute((3, 1, 2))(cse) cse = layers.Multiply()([init, cse]) return cse
Example #21
Source File: backend.py From DeepPoseKit with Apache License 2.0 | 5 votes |
def space_to_depth(input, scale, data_format=None): """ Uses phase shift algorithm to convert spatial resolution to channels/depth """ if data_format is None: data_format = K.image_data_format() data_format = data_format.lower() input = _preprocess_conv2d_input(input, data_format) out = tf.nn.space_to_depth(input, scale) out = _postprocess_conv2d_output(out, data_format) return out
Example #22
Source File: googlenet.py From keras_imagenet with MIT License | 5 votes |
def inception(x, filters): """Utility function to implement the inception module. # Arguments x: input tensor. filters: a list of filter sizes. # Returns Output tensor after applying the inception. """ if len(filters) != 4: raise ValueError('filters should have 4 components') if len(filters[1]) != 2 or len(filters[2]) != 2: raise ValueError('incorrect spec of filters') branch1x1 = conv2d_bn(x, filters[0], (1, 1)) branch3x3 = conv2d_bn(x, filters[1][0], (1, 1)) branch3x3 = conv2d_bn(branch3x3, filters[1][1], (3, 3)) branch5x5 = conv2d_bn(x, filters[2][0], (1, 1)) branch5x5 = conv2d_bn(branch5x5, filters[2][1], (5, 5)) branchpool = layers.AveragePooling2D( pool_size=(3, 3), strides=(1, 1), padding='same')(x) branchpool = conv2d_bn(branchpool, filters[3], (1, 1)) if backend.image_data_format() == 'channels_first': concat_axis = 1 else: concat_axis = 3 x = layers.concatenate( [branch1x1, branch3x3, branch5x5, branchpool], axis=concat_axis) return x
Example #23
Source File: googlenet.py From keras_imagenet with MIT License | 5 votes |
def conv2d_bn(x, filters, kernel_size=(3, 3), padding='same', strides=(1, 1), name=None): """Utility function to apply conv + BN. # Arguments x: input tensor. filters: filters in `Conv2D`. kernel_size: kernel size of the convolution padding: padding mode in `Conv2D`. strides: strides in `Conv2D`. name: name of the ops; will become `name + '_conv'` for the convolution and `name + '_bn'` for the batch norm layer. # Returns Output tensor after applying `Conv2D` and `BatchNormalization`. """ if name is not None: bn_name = name + '_bn' conv_name = name + '_conv' else: bn_name = None conv_name = None if backend.image_data_format() == 'channels_first': bn_axis = 1 else: bn_axis = 3 x = layers.Conv2D( filters, kernel_size, strides=strides, padding=padding, use_bias=False, name=conv_name)(x) x = layers.BatchNormalization(axis=bn_axis, name=bn_name)(x) x = layers.Activation('relu', name=name)(x) return x
Example #24
Source File: inception_v2.py From keras_imagenet with MIT License | 5 votes |
def inception_s2(x, filters): """Utility function to implement the 'stride-2' inception module. # Arguments x: input tensor. filters: a list of filter sizes. # Returns Output tensor after applying the 'stride-2' inception. """ if len(filters) != 2: raise ValueError('filters should have 2 components') if len(filters[0]) != 2 or len(filters[1]) != 2: raise ValueError('incorrect spec of filters') branch3x3 = conv2d_bn(x, filters[0][0], (1, 1)) branch3x3 = conv2d_bn(branch3x3, filters[0][1], (3, 3), strides=(2, 2)) branch5x5 = conv2d_bn(x, filters[1][0], (1, 1)) branch5x5 = conv2d_bn(branch5x5, filters[1][1], (3, 3)) branch5x5 = conv2d_bn(branch5x5, filters[1][1], (3, 3), strides=(2, 2)) # use MaxPooling2D here branchpool = layers.MaxPooling2D( pool_size=(3, 3), strides=(2, 2), padding='same')(x) concat_axis = 1 if backend.image_data_format() == 'channels_first' else 3 x = layers.concatenate( [branch3x3, branch5x5, branchpool], axis=concat_axis) return x
Example #25
Source File: inception_v2.py From keras_imagenet with MIT License | 5 votes |
def inception(x, filters): """Utility function to implement the inception module. # Arguments x: input tensor. filters: a list of filter sizes. # Returns Output tensor after applying the inception. """ if len(filters) != 4: raise ValueError('filters should have 4 components') if len(filters[1]) != 2 or len(filters[2]) != 2: raise ValueError('incorrect spec of filters') branch1x1 = conv2d_bn(x, filters[0], (1, 1)) branch3x3 = conv2d_bn(x, filters[1][0], (1, 1)) branch3x3 = conv2d_bn(branch3x3, filters[1][1], (3, 3)) # branch5x5 is implemented with two 3x3 conv2d's branch5x5 = conv2d_bn(x, filters[2][0], (1, 1)) branch5x5 = conv2d_bn(branch5x5, filters[2][1], (3, 3)) branch5x5 = conv2d_bn(branch5x5, filters[2][1], (3, 3)) # use AveragePooling2D here branchpool = layers.AveragePooling2D( pool_size=(3, 3), strides=(1, 1), padding='same')(x) branchpool = conv2d_bn(branchpool, filters[3], (1, 1)) concat_axis = 1 if backend.image_data_format() == 'channels_first' else 3 x = layers.concatenate( [branch1x1, branch3x3, branch5x5, branchpool], axis=concat_axis) return x
Example #26
Source File: inception_v2.py From keras_imagenet with MIT License | 5 votes |
def conv2d_bn(x, filters, kernel_size=(3, 3), padding='same', strides=(1, 1), name=None): """Utility function to apply conv + BN. # Arguments x: input tensor. filters: filters in `Conv2D`. kernel_size: kernel size of the convolution padding: padding mode in `Conv2D`. strides: strides in `Conv2D`. name: name of the ops; will become `name + '_conv'` for the convolution and `name + '_bn'` for the batch norm layer. # Returns Output tensor after applying `Conv2D` and `BatchNormalization`. """ if name is not None: bn_name = name + '_bn' conv_name = name + '_conv' else: bn_name = None conv_name = None bn_axis = 1 if backend.image_data_format() == 'channels_first' else 3 x = layers.Conv2D( filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, use_bias=False, name=conv_name)(x) x = layers.BatchNormalization(axis=bn_axis, name=bn_name)(x) x = layers.Activation('relu', name=name)(x) return x
Example #27
Source File: test_pytorch.py From snn_toolbox with MIT License | 5 votes |
def test_loading(self, _model_4, _config): assert backend.image_data_format() == 'channels_first', \ "Pytorch to Keras parser needs image_data_format == channel_first." self.prepare_model(_model_4, _config) updates = { 'tools': {'evaluate_ann': True, 'parse': False, 'normalize': False, 'convert': False, 'simulate': False}, 'input': {'model_lib': 'pytorch'}, 'simulation': {'num_to_test': 100, 'batch_size': 50}} _config.read_dict(updates) initialize_simulator(_config) normset, testset = get_dataset(_config) model_lib = import_module('snntoolbox.parsing.model_libs.' + _config.get('input', 'model_lib') + '_input_lib') input_model = model_lib.load(_config.get('paths', 'path_wd'), _config.get('paths', 'filename_ann')) # Evaluate input model. acc = model_lib.evaluate(input_model['val_fn'], _config.getint('simulation', 'batch_size'), _config.getint('simulation', 'num_to_test'), **testset) assert acc >= 0.8
Example #28
Source File: keras_input_lib.py From snn_toolbox with MIT License | 5 votes |
def parse_convolution(self, layer, attributes): attributes['parameters'] = list(layer.get_weights()) if layer.bias is None: attributes['parameters'].insert(1, np.zeros(layer.filters)) attributes['parameters'] = tuple(attributes['parameters']) attributes['use_bias'] = True assert layer.data_format == k.image_data_format(), ( "The input model was setup with image data format '{}', but your " "keras config file expects '{}'.".format(layer.data_format, k.image_data_format()))
Example #29
Source File: multiclass_3D_CNN.py From CNNArt with Apache License 2.0 | 5 votes |
def _handle_data_format(): global DIM1_AXIS global DIM2_AXIS global DIM3_AXIS global CHANNEL_AXIS if K.image_data_format() == 'channels_last': DIM1_AXIS = 1 DIM2_AXIS = 2 DIM3_AXIS = 3 CHANNEL_AXIS = 4 else: CHANNEL_AXIS = 1 DIM1_AXIS = 2 DIM2_AXIS = 3 DIM3_AXIS = 4
Example #30
Source File: time_frequency.py From kapre with MIT License | 5 votes |
def __init__( self, n_dft=512, n_hop=None, padding='same', power_spectrogram=2.0, return_decibel_spectrogram=False, trainable_kernel=False, image_data_format='default', **kwargs, ): assert n_dft > 1 and ((n_dft & (n_dft - 1)) == 0), ( 'n_dft should be > 1 and power of 2, but n_dft == %d' % n_dft ) assert isinstance(trainable_kernel, bool) assert isinstance(return_decibel_spectrogram, bool) assert padding in ('same', 'valid') if n_hop is None: n_hop = n_dft // 2 assert image_data_format in ('default', 'channels_first', 'channels_last') if image_data_format == 'default': self.image_data_format = K.image_data_format() else: self.image_data_format = image_data_format self.n_dft = n_dft assert n_dft % 2 == 0 self.n_filter = n_dft // 2 + 1 self.trainable_kernel = trainable_kernel self.n_hop = n_hop self.padding = padding self.power_spectrogram = float(power_spectrogram) self.return_decibel_spectrogram = return_decibel_spectrogram super(Spectrogram, self).__init__(**kwargs)