本文整理汇总了Python中keras.backend.image_data_format方法的典型用法代码示例。如果您正苦于以下问题:Python backend.image_data_format方法的具体用法?Python backend.image_data_format怎么用?Python backend.image_data_format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.image_data_format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def call(self, inputs):
if self.data_format is None:
data_format = image_data_format()
if self.data_format not in {'channels_first', 'channels_last'}:
raise ValueError('Unknown data_format ' + str(data_format))
x = _preprocess_conv2d_input(inputs, self.data_format)
padding = _preprocess_padding(self.padding)
strides = (1,) + self.strides + (1,)
outputs = tf.nn.depthwise_conv2d(inputs, self.depthwise_kernel,
strides=strides,
padding=padding,
rate=self.dilation_rate)
if self.bias:
outputs = K.bias_add(
outputs,
self.bias,
data_format=self.data_format)
if self.activation is not None:
return self.activation(outputs)
return outputs
示例2: _conv_block
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def _conv_block(inputs, filters, kernel, strides=1, padding='same', use_activation=False):
"""Convolution Block
This function defines a 2D convolution operation with BN and relu.
# 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.
# Returns
Output tensor.
"""
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = Conv2D(filters, kernel, padding=padding, strides=strides,
use_bias=False)(inputs)
x = BatchNormalization(axis=channel_axis)(x)
if use_activation:
x = Activation('relu')(x)
return x
示例3: _initial_conv_block_inception
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def _initial_conv_block_inception(input, initial_conv_filters, weight_decay=5e-4):
''' Adds an initial conv block, with batch norm and relu for the DPN
Args:
input: input tensor
initial_conv_filters: number of filters for initial conv block
weight_decay: weight decay factor
Returns: a keras tensor
'''
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, kernel_initializer='he_normal',
kernel_regularizer=l2(weight_decay), strides=(2, 2))(input)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
return x
示例4: _bn_relu_conv_block
# 需要导入模块: from keras import backend [as 别名]
# 或者: from 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
示例5: deprocess_image
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def deprocess_image(x):
# normalize tensor: center on 0., ensure std is 0.1
x -= x.mean()
x /= (x.std() + K.epsilon())
x *= 0.1
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
if K.image_data_format() == 'channels_first':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
示例6: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def __init__(self, target_shape=None,factor=None, data_format=None, **kwargs):
# conmpute dataformat
if data_format is None:
data_format = K.image_data_format()
assert data_format in {
'channels_last', 'channels_first'}
self.data_format = data_format
self.input_spec = [InputSpec(ndim=4)]
self.target_shape = target_shape
self.factor = factor
if self.data_format == 'channels_first':
self.target_size = (target_shape[2], target_shape[3])
elif self.data_format == 'channels_last':
self.target_size = (target_shape[1], target_shape[2])
super(BilinearUpSampling2D, self).__init__(**kwargs)
示例7: duc
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def duc(x, factor=8, output_shape=(512, 512, 1)):
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
H, W, c, r = output_shape[0], output_shape[1], output_shape[2], factor
h = H / r
w = W / r
x = Conv2D(
c*r*r,
(3, 3),
padding='same',
name='conv_duc_%s'%factor)(x)
x = BatchNormalization(axis=bn_axis,name='bn_duc_%s'%factor)(x)
x = Activation('relu')(x)
x = Permute((3, 1, 2))(x)
x = Reshape((c, r, r, h, w))(x)
x = Permute((1, 4, 2, 5, 3))(x)
x = Reshape((c, H, W))(x)
x = Permute((2, 3, 1))(x)
return x
# interpolation
示例8: __initial_conv_block_inception
# 需要导入模块: from keras import backend [as 别名]
# 或者: from 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
示例9: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def __init__(self, file, image_size, image_data_generator,
batch_size=32, shuffle=False, seed=None,
data_format=None,
save_to_dir=None, save_prefix='', save_format='png'):
if not os.path.exists(file):
raise ValueError('Cannot find file: %s' % file)
if data_format is None:
data_format = K.image_data_format()
split_lines = [line.rstrip('\n').split(' ') for line in open(file, 'r')]
self.x = np.asarray([e[0] for e in split_lines])
self.y = np.asarray([float(e[1]) for e in split_lines])
self.image_size = image_size
self.image_data_generator = image_data_generator
self.data_format = data_format
self.save_to_dir = save_to_dir
self.save_prefix = save_prefix
self.save_format = save_format
super(FileIterator, self).__init__(self.x.shape[0], batch_size, shuffle, seed)
示例10: fire_module
# 需要导入模块: from keras import backend [as 别名]
# 或者: from 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.
示例11: __transition_block
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def __transition_block(ip, nb_filter, compression=1.0, weight_decay=1e-4):
''' Apply BatchNorm, Relu 1x1, Conv2D, optional compression, dropout and Maxpooling2D
Args:
ip: keras tensor
nb_filter: number of filters
compression: calculated as 1 - reduction. Reduces the number of feature maps
in the transition block.
dropout_rate: dropout rate
weight_decay: weight decay factor
Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool
'''
concat_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(ip)
x = Activation('relu')(x)
x = Conv2D(int(nb_filter * compression), (1, 1), kernel_initializer='he_normal', padding='same', use_bias=False,
kernel_regularizer=l2(weight_decay))(x)
x = AveragePooling2D((2, 2), strides=(2, 2))(x)
# global context block
x = global_context_block(x)
return x
示例12: get_data_generator
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def get_data_generator(data_iterator,
num_classes):
def get_arrays(db):
data = db.data[0].asnumpy()
if K.image_data_format() == "channels_last":
data = data.transpose((0, 2, 3, 1))
labels = to_categorical(
y=db.label[0].asnumpy(),
num_classes=num_classes)
return data, labels
while True:
try:
db = data_iterator.next()
except StopIteration:
# logging.warning("get_data exception due to end of data - resetting iterator")
data_iterator.reset()
db = data_iterator.next()
finally:
yield get_arrays(db)
示例13: get_realpart
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def get_realpart(x):
image_format = K.image_data_format()
ndim = K.ndim(x)
input_shape = K.shape(x)
if (image_format == 'channels_first' and ndim != 3) or ndim == 2:
input_dim = input_shape[1] // 2
return x[:, :input_dim]
input_dim = input_shape[-1] // 2
if ndim == 3:
return x[:, :, :input_dim]
elif ndim == 4:
return x[:, :, :, :input_dim]
elif ndim == 5:
return x[:, :, :, :, :input_dim]
示例14: get_imagpart
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def get_imagpart(x):
image_format = K.image_data_format()
ndim = K.ndim(x)
input_shape = K.shape(x)
if (image_format == 'channels_first' and ndim != 3) or ndim == 2:
input_dim = input_shape[1] // 2
return x[:, input_dim:]
input_dim = input_shape[-1] // 2
if ndim == 3:
return x[:, :, input_dim:]
elif ndim == 4:
return x[:, :, :, input_dim:]
elif ndim == 5:
return x[:, :, :, :, input_dim:]
示例15: compute_error_matrix
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import image_data_format [as 别名]
def compute_error_matrix(y_true, y_pred):
"""Compute Confusion matrix (a.k.a. error matrix).
a predicted
c 0 1 2
t 0 [[ 5, 3, 0],
u 1 [ 2, 3, 1],
a 2 [ 0, 2, 11]]
l
Note true positves are in diagonal
"""
# Find channel axis given backend
if K.image_data_format() == 'channels_last':
ax_chn = 3
else:
ax_chn = 1
classes = y_true.shape[ax_chn]
confusion = get_confusion(K.argmax(y_true, axis=ax_chn).flatten(),
K.argmax(y_pred, axis=ax_chn).flatten(),
classes)
return confusion