本文整理汇总了Python中keras.backend.set_image_data_format方法的典型用法代码示例。如果您正苦于以下问题:Python backend.set_image_data_format方法的具体用法?Python backend.set_image_data_format怎么用?Python backend.set_image_data_format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.set_image_data_format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_segment_2d
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_segment_2d():
from keras import backend as K
K.set_image_data_format("channels_last") # Set at channels_first in test_deepseg_lesion.test_segment()
contrast_test = 't2'
model_path = os.path.join(sct.__sct_dir__, 'data', 'deepseg_sc_models', '{}_sc.h5'.format(contrast_test))
fname_t2 = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2.nii.gz') # install: sct_download_data -d sct_testing_data
fname_t2_seg = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2_seg.nii.gz') # install: sct_download_data -d sct_testing_data
img, gt = _preprocess_segment(fname_t2, fname_t2_seg, contrast_test)
seg = deepseg_sc.segment_2d(model_fname=model_path, contrast_type=contrast_test, input_size=(64,64), im_in=img)
assert seg.dtype == np.dtype('float32')
seg_im = img.copy()
seg_im.data = (seg > 0.5).astype(np.uint8)
assert msct_image.compute_dice(seg_im, gt) > 0.80
示例2: test_segment_3d
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_segment_3d():
from keras import backend as K
K.set_image_data_format("channels_last") # Set at channels_first in test_deepseg_lesion.test_segment()
contrast_test = 't2'
model_path = os.path.join(sct.__sct_dir__, 'data', 'deepseg_sc_models', '{}_sc_3D.h5'.format(contrast_test))
fname_t2 = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2.nii.gz') # install: sct_download_data -d sct_testing_data
fname_t2_seg = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2_seg.nii.gz') # install: sct_download_data -d sct_testing_data
img, gt = _preprocess_segment(fname_t2, fname_t2_seg, contrast_test, dim_3=True)
seg = deepseg_sc.segment_3d(model_fname=model_path, contrast_type=contrast_test, im_in=img)
assert seg.dtype == np.dtype('float32')
seg_im = img.copy()
seg_im.data = (seg > 0.5).astype(np.uint8)
assert msct_image.compute_dice(seg_im, gt) > 0.80
示例3: across_data_formats
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def across_data_formats(func):
"""Function wrapper to run tests on multiple keras data_format and clean up after TensorFlow tests.
Args:
func: test function to clean up after.
Returns:
A function wrapping the input function.
"""
@six.wraps(func)
def wrapper(*args, **kwargs):
for data_format in {'channels_first', 'channels_last'}:
K.set_image_data_format(data_format)
func(*args, **kwargs)
if K.backend() == 'tensorflow':
K.clear_session()
tf.reset_default_graph()
return wrapper
示例4: build_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def build_model(config: BEGANConfig):
K.set_image_data_format('channels_last')
autoencoder = build_autoencoder(config)
generator = build_generator(config)
discriminator = build_discriminator(config, autoencoder)
return autoencoder, generator, discriminator
示例5: set_img_format
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def set_img_format():
try:
if K.backend() == 'theano':
K.set_image_data_format('channels_first')
else:
K.set_image_data_format('channels_last')
except AttributeError:
if K._BACKEND == 'theano':
K.set_image_dim_ordering('th')
else:
K.set_image_dim_ordering('tf')
示例6: test_DSSIM_channels_last
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_DSSIM_channels_last():
prev_data = K.image_data_format()
K.set_image_data_format('channels_last')
for input_dim, kernel_size in zip([32, 33], [2, 3]):
input_shape = [input_dim, input_dim, 3]
X = np.random.random_sample(4 * input_dim * input_dim * 3)
X = X.reshape([4] + input_shape)
y = np.random.random_sample(4 * input_dim * input_dim * 3)
y = y.reshape([4] + input_shape)
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape,
activation='relu'))
model.add(Conv2D(3, (3, 3), padding='same', input_shape=input_shape,
activation='relu'))
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(loss=DSSIMObjective(kernel_size=kernel_size),
metrics=['mse'],
optimizer=adam)
model.fit(X, y, batch_size=2, epochs=1, shuffle='batch')
# Test same
x1 = K.constant(X, 'float32')
x2 = K.constant(X, 'float32')
dssim = DSSIMObjective(kernel_size=kernel_size)
assert_allclose(0.0, K.eval(dssim(x1, x2)), atol=1e-4)
# Test opposite
x1 = K.zeros([4] + input_shape)
x2 = K.ones([4] + input_shape)
dssim = DSSIMObjective(kernel_size=kernel_size)
assert_allclose(0.5, K.eval(dssim(x1, x2)), atol=1e-4)
K.set_image_data_format(prev_data)
示例7: test_DSSIM_channels_first
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_DSSIM_channels_first():
prev_data = K.image_data_format()
K.set_image_data_format('channels_first')
for input_dim, kernel_size in zip([32, 33], [2, 3]):
input_shape = [3, input_dim, input_dim]
X = np.random.random_sample(4 * input_dim * input_dim * 3)
X = X.reshape([4] + input_shape)
y = np.random.random_sample(4 * input_dim * input_dim * 3)
y = y.reshape([4] + input_shape)
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape,
activation='relu'))
model.add(Conv2D(3, (3, 3), padding='same', input_shape=input_shape,
activation='relu'))
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(loss=DSSIMObjective(kernel_size=kernel_size), metrics=['mse'],
optimizer=adam)
model.fit(X, y, batch_size=2, epochs=1, shuffle='batch')
# Test same
x1 = K.constant(X, 'float32')
x2 = K.constant(X, 'float32')
dssim = DSSIMObjective(kernel_size=kernel_size)
assert_allclose(0.0, K.eval(dssim(x1, x2)), atol=1e-4)
# Test opposite
x1 = K.zeros([4] + input_shape)
x2 = K.ones([4] + input_shape)
dssim = DSSIMObjective(kernel_size=kernel_size)
assert_allclose(0.5, K.eval(dssim(x1, x2)), atol=1e-4)
K.set_image_data_format(prev_data)
示例8: test_get_img_shape_on_2d_image
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_get_img_shape_on_2d_image():
n = 5
channels = 4
dim1 = 1
dim2 = 2
K.set_image_data_format('channels_first')
assert (n, channels, dim1, dim2) == utils.get_img_shape(K.ones(shape=(n, channels, dim1, dim2)))
K.set_image_data_format('channels_last')
assert (n, channels, dim1, dim2) == utils.get_img_shape(K.ones(shape=(n, dim1, dim2, channels)))
示例9: test_get_img_shape_on_3d_image
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_get_img_shape_on_3d_image():
n = 5
channels = 4
dim1 = 1
dim2 = 2
dim3 = 3
K.set_image_data_format('channels_first')
assert (n, channels, dim1, dim2, dim3) == utils.get_img_shape(K.ones(shape=(n, channels, dim1, dim2, dim3)))
K.set_image_data_format('channels_last')
assert (n, channels, dim1, dim2, dim3) == utils.get_img_shape(K.ones(shape=(n, dim1, dim2, dim3, channels)))
示例10: test_dssim_channels_last
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_dssim_channels_last(dummy): # pylint:disable=unused-argument
""" Basic test for DSSIM Loss """
prev_data = K.image_data_format()
K.set_image_data_format('channels_last')
for input_dim, kernel_size in zip([32, 33], [2, 3]):
input_shape = [input_dim, input_dim, 3]
var_x = np.random.random_sample(4 * input_dim * input_dim * 3)
var_x = var_x.reshape([4] + input_shape)
var_y = np.random.random_sample(4 * input_dim * input_dim * 3)
var_y = var_y.reshape([4] + input_shape)
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape,
activation='relu'))
model.add(Conv2D(3, (3, 3), padding='same', input_shape=input_shape,
activation='relu'))
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(loss=losses.DSSIMObjective(kernel_size=kernel_size),
metrics=['mse'],
optimizer=adam)
model.fit(var_x, var_y, batch_size=2, epochs=1, shuffle='batch')
# Test same
x_1 = K.constant(var_x, 'float32')
x_2 = K.constant(var_x, 'float32')
dssim = losses.DSSIMObjective(kernel_size=kernel_size)
assert_allclose(0.0, K.eval(dssim(x_1, x_2)), atol=1e-4)
# Test opposite
x_1 = K.zeros([4] + input_shape)
x_2 = K.ones([4] + input_shape)
dssim = losses.DSSIMObjective(kernel_size=kernel_size)
assert_allclose(0.5, K.eval(dssim(x_1, x_2)), atol=1e-4)
K.set_image_data_format(prev_data)
示例11: Panotti_CNN
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def Panotti_CNN(X_shape, nb_classes, nb_layers=4):
# Inputs:
# X_shape = [ # spectrograms per batch, # audio channels, # spectrogram freq bins, # spectrogram time bins ]
# nb_classes = number of output n_classes
# nb_layers = number of conv-pooling sets in the CNN
from keras import backend as K
K.set_image_data_format('channels_last') # SHH changed on 3/1/2018 b/c tensorflow prefers channels_last
nb_filters = 32 # number of convolutional filters = "feature maps"
kernel_size = (3, 3) # convolution kernel size
pool_size = (2, 2) # size of pooling area for max pooling
cl_dropout = 0.5 # conv. layer dropout
dl_dropout = 0.6 # dense layer dropout
print(" MyCNN_Keras2: X_shape = ",X_shape,", channels = ",X_shape[3])
input_shape = (X_shape[1], X_shape[2], X_shape[3])
model = Sequential()
model.add(Conv2D(nb_filters, kernel_size, padding='same', input_shape=input_shape, name="Input"))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Activation('relu')) # Leave this relu & BN here. ELU is not good here (my experience)
model.add(BatchNormalization(axis=-1)) # axis=1 for 'channels_first'; but tensorflow preferse channels_last (axis=-1)
for layer in range(nb_layers-1): # add more layers than just the first
model.add(Conv2D(nb_filters, kernel_size, padding='same'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Activation('elu'))
model.add(Dropout(cl_dropout))
#model.add(BatchNormalization(axis=-1)) # ELU authors reccommend no BatchNorm. I confirm.
model.add(Flatten())
model.add(Dense(128)) # 128 is 'arbitrary' for now
#model.add(Activation('relu')) # relu (no BN) works ok here, however ELU works a bit better...
model.add(Activation('elu'))
model.add(Dropout(dl_dropout))
model.add(Dense(nb_classes))
model.add(Activation("softmax",name="Output"))
return model
# Used for when you want to use weights from a previously-trained model,
# with a different set/number of output classes
示例12: resnet3d_test
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def resnet3d_test():
"""resnet3d test helper."""
def f(model):
K.set_image_data_format('channels_last')
model.compile(loss="categorical_crossentropy", optimizer="sgd")
assert True, "Failed to build with {}".format(K.image_data_format())
return f
示例13: test_resnet3d_18
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_resnet3d_18(resnet3d_test):
"""Test 18."""
K.set_image_data_format('channels_last')
model = Resnet3DBuilder.build_resnet_18((224, 224, 224, 1), 2)
resnet3d_test(model)
K.set_image_data_format('channels_first')
model = Resnet3DBuilder.build_resnet_18((1, 512, 512, 256), 2)
resnet3d_test(model)
示例14: test_resnet3d_34
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_resnet3d_34(resnet3d_test):
"""Test 34."""
K.set_image_data_format('channels_last')
model = Resnet3DBuilder.build_resnet_34((224, 224, 224, 1), 2)
resnet3d_test(model)
K.set_image_data_format('channels_first')
model = Resnet3DBuilder.build_resnet_34((1, 512, 512, 256), 2)
resnet3d_test(model)
示例15: test_resnet3d_50
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import set_image_data_format [as 别名]
def test_resnet3d_50(resnet3d_test):
"""Test 50."""
K.set_image_data_format('channels_last')
model = Resnet3DBuilder.build_resnet_50((224, 224, 224, 1), 1, 1e-2)
resnet3d_test(model)
K.set_image_data_format('channels_first')
model = Resnet3DBuilder.build_resnet_50((1, 512, 512, 256), 1, 1e-2)
resnet3d_test(model)