def test_keras_import(self):
# Pad 1D
model = Sequential()
model.add(ZeroPadding1D(2, input_shape=(224, 3)))
model.add(Conv1D(32, 7, strides=2))
model.build()
self.pad_test(model, 'pad_w', 2)
# Pad 2D
model = Sequential()
model.add(ZeroPadding2D(2, input_shape=(224, 224, 3)))
model.add(Conv2D(32, 7, strides=2))
model.build()
self.pad_test(model, 'pad_w', 2)
# Pad 3D
model = Sequential()
model.add(ZeroPadding3D(2, input_shape=(224, 224, 224, 3)))
model.add(Conv3D(32, 7, strides=2))
model.build()
self.pad_test(model, 'pad_w', 2)
# ********** Export json tests **********
# ********** Data Layers Test **********
python类add()的实例源码
def test_keras_import(self):
# Conv 1D
model = Sequential()
model.add(LocallyConnected1D(32, 3, kernel_regularizer=regularizers.l2(0.01),
bias_regularizer=regularizers.l2(0.01),
activity_regularizer=regularizers.l2(0.01), kernel_constraint='max_norm',
bias_constraint='max_norm', activation='relu', input_shape=(10, 16)))
model.build()
self.keras_param_test(model, 1, 12)
# Conv 2D
model = Sequential()
model.add(LocallyConnected2D(32, (3, 3), kernel_regularizer=regularizers.l2(0.01),
bias_regularizer=regularizers.l2(0.01),
activity_regularizer=regularizers.l2(0.01), kernel_constraint='max_norm',
bias_constraint='max_norm', activation='relu', input_shape=(10, 16, 16)))
model.build()
self.keras_param_test(model, 1, 14)
# ********** Recurrent Layers **********
def test_keras_import(self):
model = Sequential()
model.add(GaussianNoise(stddev=0.1, input_shape=(1, 16)))
model.build()
self.keras_param_test(model, 0, 1)
def block(self, num_filters, num_layers, kernel_size, strides, input_tensor):
x = Conv2D(num_layers, (1, 1), strides=strides)(input_tensor)
x = Activation(selu)(x)
x = Conv2D(num_filters, kernel_size, padding='same')(x)
x = Activation(selu)(x)
x = Conv2D(num_filters*4, (1, 1))(x)
shortcut = Conv2D(num_filters*4, (1, 1), strides=strides,
)(input_tensor)
x = layers.add([x, shortcut])
x = Activation(selu)(x)
return x
def keepsize_256(nx, ny, noise, depth, activation='relu', n_filters=64, l2_reg=1e-7):
"""
Deep residual network that keeps the size of the input throughout the whole network
"""
def residual(inputs, n_filters):
x = ReflectionPadding2D()(inputs)
x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x)
x = BatchNormalization()(x)
x = Activation(activation)(x)
x = ReflectionPadding2D()(x)
x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x)
x = BatchNormalization()(x)
x = add([x, inputs])
return x
inputs = Input(shape=(nx, ny, 1))
x = GaussianNoise(noise)(inputs)
x = ReflectionPadding2D()(x)
x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x)
x0 = Activation(activation)(x)
x = residual(x0, n_filters)
for i in range(depth-1):
x = residual(x, n_filters)
x = ReflectionPadding2D()(x)
x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x)
x = BatchNormalization()(x)
x = add([x, x0])
# Upsampling for superresolution
x = UpSampling2D()(x)
x = ReflectionPadding2D()(x)
x = Conv2D(4*n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x)
x = Activation(activation)(x)
final = Conv2D(1, (1, 1), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x)
return Model(inputs=inputs, outputs=final)
def prep_model(inputs, N, s0pad, s1pad, c, granlevels=1):
# LSTM
lstm = LSTM(N, return_sequences=True, implementation=2,
kernel_regularizer=l2(c['l2reg']), recurrent_regularizer=l2(c['l2reg']),
bias_regularizer=l2(c['l2reg']))
x1 = inputs[0]
x2 = inputs[1]
h1 = lstm(x1)
h2 = lstm(x2)
W_x = Dense(N, kernel_initializer='glorot_uniform', use_bias=True,
kernel_regularizer=l2(c['l2reg']))
W_h = Dense(N, kernel_initializer='orthogonal', use_bias=True,
kernel_regularizer=l2(c['l2reg']))
sigmoid = Activation('sigmoid')
a1 = multiply([x1, sigmoid( add([W_x(x1), W_h(h1)]) )])
a2 = multiply([x2, sigmoid( add([W_x(x2), W_h(h2)]) )])
# Averaging
avg = Lambda(function=lambda x: K.mean(x, axis=1),
output_shape=lambda shape: (shape[0], ) + shape[2:])
gran1 = avg(a1)
gran2 = avg(a2)
return [gran1, gran2], N
def prep_model(inputs, N, s0pad, s1pad, c, granlevels=1):
# LSTM
lstm = LSTM(N, return_sequences=True, implementation=2,
kernel_regularizer=l2(c['l2reg']), recurrent_regularizer=l2(c['l2reg']),
bias_regularizer=l2(c['l2reg']))
x1 = inputs[0]
x2 = inputs[1]
h1 = lstm(x1)
h2 = lstm(x2)
W_x = Dense(N, kernel_initializer='glorot_uniform', use_bias=True,
kernel_regularizer=l2(c['l2reg']))
W_h = Dense(N, kernel_initializer='orthogonal', use_bias=True,
kernel_regularizer=l2(c['l2reg']))
sigmoid = Activation('sigmoid')
a1 = multiply([x1, sigmoid( add([W_x(x1), W_h(h1)]) )])
a2 = multiply([x2, sigmoid( add([W_x(x2), W_h(h2)]) )])
# Averaging
avg = Lambda(function=lambda x: K.mean(x, axis=1),
output_shape=lambda shape: (shape[0], ) + shape[2:])
gran1 = avg(a1)
gran2 = avg(a2)
return [gran1, gran2], N
def test_tiny_inner_product(self,
model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
# Define a model
model = Sequential()
model.add(Dense(2, input_shape=(2,)))
# Test all zeros
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
self._test_keras_model(model, mode='zeros',
model_precision=model_precision)
# Test all ones
model.set_weights([np.ones(w.shape) for w in model.get_weights()])
self._test_keras_model(model, mode='ones',
model_precision=model_precision)
# Test random
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
self._test_keras_model(model, model_precision=model_precision)
def test_housenet_random(self):
np.random.seed(1988)
num_hidden = 2
num_features = 3
# Define a model
model = Sequential()
model.add(Dense(num_hidden, input_dim = num_features))
model.add(Activation('relu'))
model.add(Dense(1, input_dim = num_features))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_conv_random(self, model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 1)
num_kernels, kernel_height, kernel_width = 3, 5, 5
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape,
filters = num_kernels, kernel_size = (kernel_height, kernel_width)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model, model_precision=model_precision)
def test_tiny_conv_dilated(self, model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 1)
num_kernels, kernel_height, kernel_width = 3, 5, 5
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape, dilation_rate=(2,2),
filters = num_kernels, kernel_size = (kernel_height, kernel_width)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model, model_precision=model_precision)
def test_tiny_conv_dilated_rect_random(self, model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_shape = (32, 20, 3)
num_kernels = 2
kernel_height = 3
kernel_width = 3
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape, dilation_rate=(2,2),
filters = num_kernels, kernel_size = (kernel_height, kernel_width)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model, model_precision=model_precision)
def test_tiny_conv1d_dilated_random(self):
np.random.seed(1988)
input_shape = (20, 1)
num_kernels = 2
filter_length = 3
# Define a model
model = Sequential()
model.add(Conv1D(num_kernels, kernel_size = filter_length, padding = 'valid',
input_shape = input_shape, dilation_rate = 3))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_conv_rect_kernel_x(self):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 1)
num_kernels = 3
kernel_height = 1
kernel_width = 5
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape,
filters = num_kernels, kernel_size = (kernel_height, kernel_width),
padding = 'same'))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_conv_rect_kernel_y(self):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 1)
num_kernels = 3
kernel_height = 5
kernel_width = 1
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape,
filters = num_kernels, kernel_size = (kernel_height, kernel_width),
padding = 'valid'))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_conv_rect_kernel_xy(self,
model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 1)
num_kernels = 3
kernel_height = 5
kernel_width = 3
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape,
filters = num_kernels, kernel_size = (kernel_height, kernel_width),
padding = 'valid'))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model, model_precision=model_precision)
def test_conv_batchnorm_random(self, model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 3)
num_kernels = 3
kernel_height = 5
kernel_width = 5
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape,
filters = num_kernels, kernel_size = (kernel_height, kernel_width)))
model.add(BatchNormalization(epsilon=1e-5))
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Get the coreml model
self._test_keras_model(model, model_precision=model_precision)
def test_conv_batchnorm_no_gamma_no_beta(self, model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 3)
num_kernels = 3
kernel_height = 5
kernel_width = 5
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape,
filters = num_kernels, kernel_size = (kernel_height, kernel_width)))
model.add(BatchNormalization(center=False, scale=False, epsilon=1e-5))
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Get the coreml model
self._test_keras_model(model, model_precision=model_precision)
def test_tiny_deconv_random(self):
# In Keras 2, deconvolution auto computes the output shape.
np.random.seed(1988)
input_dim = 13
input_shape = (input_dim, input_dim, 5)
num_kernels = 16
kernel_height = 3
kernel_width = 3
# Define a model
model = Sequential()
model.add(Conv2DTranspose(filters = num_kernels, kernel_size=(kernel_height, kernel_width),
input_shape = input_shape, padding = 'valid', strides = (2,2)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_deconv_random_same_padding(self):
np.random.seed(1988)
input_dim = 14
input_shape = (input_dim, input_dim, 3)
num_kernels = 16
kernel_height = 3
kernel_width = 3
# Define a model
model = Sequential()
model.add(Conv2DTranspose(filters = num_kernels, kernel_size=(kernel_height, kernel_width),
input_shape = input_shape, padding = 'same', strides = (2,2)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_depthwise_conv_valid_pad(self):
np.random.seed(1988)
input_dim = 16
input_shape = (input_dim, input_dim, 3)
depth_multiplier = 1
kernel_height = 3
kernel_width = 3
# Define a model
model = Sequential()
model.add(DepthwiseConv2D(depth_multiplier = depth_multiplier, kernel_size=(kernel_height, kernel_width),
input_shape = input_shape, padding = 'valid', strides = (1,1)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_depthwise_conv_same_pad_depth_multiplier(self):
np.random.seed(1988)
input_dim = 16
input_shape = (input_dim, input_dim, 3)
depth_multiplier = 4
kernel_height = 3
kernel_width = 3
# Define a model
model = Sequential()
model.add(DepthwiseConv2D(depth_multiplier = depth_multiplier, kernel_size=(kernel_height, kernel_width),
input_shape = input_shape, padding = 'same', strides = (1,1)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_depthwise_conv_valid_pad_depth_multiplier(self):
np.random.seed(1988)
input_dim = 16
input_shape = (input_dim, input_dim, 3)
depth_multiplier = 2
kernel_height = 3
kernel_width = 3
# Define a model
model = Sequential()
model.add(DepthwiseConv2D(depth_multiplier = depth_multiplier, kernel_size=(kernel_height, kernel_width),
input_shape = input_shape, padding = 'valid', strides = (1,1)))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_separable_conv_valid(self):
np.random.seed(1988)
input_dim = 16
input_shape = (input_dim, input_dim, 3)
depth_multiplier = 1
kernel_height = 3
kernel_width = 3
num_kernels = 4
# Define a model
model = Sequential()
model.add(SeparableConv2D(filters = num_kernels, kernel_size=(kernel_height, kernel_width),
padding = 'valid', strides = (1,1), depth_multiplier = depth_multiplier,
input_shape = input_shape))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_separable_conv_same_fancy(self):
np.random.seed(1988)
input_dim = 16
input_shape = (input_dim, input_dim, 3)
depth_multiplier = 1
kernel_height = 3
kernel_width = 3
num_kernels = 4
# Define a model
model = Sequential()
model.add(SeparableConv2D(filters = num_kernels, kernel_size=(kernel_height, kernel_width),
padding = 'same', strides = (2,2), activation='relu', depth_multiplier = depth_multiplier,
input_shape = input_shape))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_separable_conv_same_fancy_depth_multiplier(
self,
model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 16
input_shape = (input_dim, input_dim, 3)
depth_multiplier = 2
kernel_height = 3
kernel_width = 3
num_kernels = 40
# Define a model
model = Sequential()
model.add(SeparableConv2D(filters = num_kernels, kernel_size=(kernel_height, kernel_width),
padding = 'same', strides = (2,2), activation='relu', depth_multiplier = depth_multiplier,
input_shape = input_shape))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model, model_precision=model_precision)
def test_tiny_conv_upsample_random(self):
np.random.seed(1988)
input_dim = 10
input_shape = (input_dim, input_dim, 1)
num_kernels = 3
kernel_height = 5
kernel_width = 5
# Define a model
model = Sequential()
model.add(Conv2D(input_shape = input_shape,
filters = num_kernels, kernel_size = (kernel_height, kernel_width)))
model.add(UpSampling2D(size = 2))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_conv_upsample_1d_random(self):
np.random.seed(1988)
input_dim = 2
input_length = 10
filter_length = 3
nb_filters = 4
model = Sequential()
model.add(Conv1D(nb_filters, kernel_size = filter_length, padding='same',
input_shape=(input_length, input_dim)))
model.add(UpSampling1D(size = 2))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model)
def test_tiny_conv_crop_1d_random(self, model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 2
input_length = 10
filter_length = 3
nb_filters = 4
model = Sequential()
model.add(Conv1D(nb_filters, kernel_size = filter_length, padding='same',
input_shape=(input_length, input_dim)))
model.add(Cropping1D(cropping = 2))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model, model_precision=model_precision)
def test_tiny_conv_pad_1d_random(self, model_precision=_MLMODEL_FULL_PRECISION):
np.random.seed(1988)
input_dim = 2
input_length = 10
filter_length = 3
nb_filters = 4
model = Sequential()
model.add(Conv1D(nb_filters, kernel_size = filter_length, padding='same',
input_shape=(input_length, input_dim)))
model.add(ZeroPadding1D(padding = 2))
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Test the keras model
self._test_keras_model(model, model_precision=model_precision)