def test_process_whole_image(self, data_format, shape): keras.backend.set_image_data_format(data_format) num_crops = 2 receptive_field = 3 features = 3 images = np.ones(shape) input_shape = running.get_cropped_input_shape( images, num_crops, receptive_field=receptive_field, data_format=data_format) for padding in ['reflect', 'zero']: with self.test_session(): inputs = keras.layers.Input(shape=input_shape) outputs = layers.TensorProduct(features)(inputs) model = keras.models.Model(inputs=inputs, outputs=[outputs, outputs]) output = running.process_whole_image( model, images, num_crops=num_crops, receptive_field=receptive_field, padding=padding) if data_format == 'channels_first': expected_shape = tuple([images.shape[0], features] + list(images.shape[2:])) else: expected_shape = tuple([images.shape[0]] + list(images.shape[1:-1]) + [features]) self.assertEqual(output.shape, expected_shape) with self.assertRaises(ValueError): inputs = keras.layers.Input(shape=(3, 4, 5)) outputs = layers.TensorProduct(features)(inputs) model = keras.models.Model(inputs=inputs, outputs=outputs) output = running.process_whole_image( model, images, num_crops=num_crops, receptive_field=receptive_field, padding='reflect') with self.assertRaises(ValueError): inputs = keras.layers.Input(shape=input_shape) outputs = layers.TensorProduct(features)(inputs) model = keras.models.Model(inputs=inputs, outputs=outputs) output = running.process_whole_image( model, images, num_crops=num_crops, receptive_field=receptive_field, padding=None)
def test_tensorproduct_constraints(self): k_constraint = tf.keras.constraints.max_norm(0.01) b_constraint = tf.keras.constraints.max_norm(0.01) layer = layers.TensorProduct(3, kernel_constraint=k_constraint, bias_constraint=b_constraint) layer(tf.keras.backend.variable(np.ones((2, 4)))) self.assertEqual(layer.kernel.constraint, k_constraint) self.assertEqual(layer.bias.constraint, b_constraint)
def test_tensorproduct_regularization(self): layer = layers.TensorProduct( 3, kernel_regularizer=tf.keras.regularizers.l1(0.01), bias_regularizer='l1', activity_regularizer='l2', name='tensorproduct_reg') layer(tf.keras.backend.variable(np.ones((2, 4)))) self.assertEqual(3, len(layer.losses))