本文整理匯總了Python中tensorflow.keras.backend.image_data_format方法的典型用法代碼示例。如果您正苦於以下問題:Python backend.image_data_format方法的具體用法?Python backend.image_data_format怎麽用?Python backend.image_data_format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.image_data_format方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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
示例2: __init__
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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)
示例3: conv2d_bn
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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
示例4: __initial_conv_block_inception
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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
示例5: __init__
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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
示例6: correct_pad
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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]))
示例7: fire_module
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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.
示例8: correct_pad
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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]))
示例9: _se_block
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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
示例10: _conv_block
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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)
示例11: _bn_relu_conv_block
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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
示例12: __init__
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
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)
示例13: build
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
def build(self, input_shape):
self.n_ch = input_shape[1]
self.len_src = input_shape[2]
self.is_mono = self.n_ch == 1
if self.image_data_format == 'channels_first':
self.ch_axis_idx = 1
else:
self.ch_axis_idx = 3
if self.len_src is not None:
assert self.len_src >= self.n_dft, 'Hey! The input is too short!'
self.n_frame = conv_output_length(self.len_src, self.n_dft, self.padding, self.n_hop)
dft_real_kernels, dft_imag_kernels = backend.get_stft_kernels(self.n_dft)
self.dft_real_kernels = K.variable(dft_real_kernels, dtype=K.floatx(), name="real_kernels")
self.dft_imag_kernels = K.variable(dft_imag_kernels, dtype=K.floatx(), name="imag_kernels")
# kernels shapes: (filter_length, 1, input_dim, nb_filter)?
if self.trainable_kernel:
self.trainable_weights.append(self.dft_real_kernels)
self.trainable_weights.append(self.dft_imag_kernels)
else:
self.non_trainable_weights.append(self.dft_real_kernels)
self.non_trainable_weights.append(self.dft_imag_kernels)
super(Spectrogram, self).build(input_shape)
# self.built = True
示例14: compute_output_shape
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
def compute_output_shape(self, input_shape):
if self.image_data_format == 'channels_first':
return input_shape[0], self.n_ch, self.n_filter, self.n_frame
else:
return input_shape[0], self.n_filter, self.n_frame, self.n_ch
示例15: get_config
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import image_data_format [as 別名]
def get_config(self):
config = {
'n_dft': self.n_dft,
'n_hop': self.n_hop,
'padding': self.padding,
'power_spectrogram': self.power_spectrogram,
'return_decibel_spectrogram': self.return_decibel_spectrogram,
'trainable_kernel': self.trainable_kernel,
'image_data_format': self.image_data_format,
}
base_config = super(Spectrogram, self).get_config()
return dict(list(base_config.items()) + list(config.items()))