def layer_op(self, images, is_training=True, layer_id=-1, **unused_kwargs): """ :param images: tensor, concatenation of multiple input modalities :param is_training: boolean, True if network is in training mode :param layer_id: not in use :param unused_kwargs: :return: predicted tensor """ n_modality = images.shape.as_list()[-1] rank = images.shape.ndims assert n_modality > 1 roots = tf.split(images, n_modality, axis=rank - 1) for (idx, root) in enumerate(roots): conv_layer = ConvolutionalLayer( n_output_chns=self.n_features, kernel_size=3, w_initializer=self.initializers['w'], w_regularizer=self.regularizers['w'], acti_func=self.acti_func, name='conv_{}'.format(idx)) roots[idx] = conv_layer(root, is_training) roots = tf.stack(roots, axis=-1) back_end = ScaleBlock('AVERAGE', n_layers=1) output_tensor = back_end(roots, is_training) front_end = HighRes3DNet(self.num_classes) output_tensor = front_end(output_tensor, is_training) return output_tensor
def test_3d_shape(self): input_shape = (2, 32, 32, 32, 1) x = tf.ones(input_shape) highres_layer = HighRes3DNet(num_classes=5) out = highres_layer(x, is_training=True) with self.test_session() as sess: sess.run(tf.global_variables_initializer()) out = sess.run(out) self.assertAllClose((2, 32, 32, 32, 5), out.shape)
def test_2d_reg_shape(self): input_shape = (2, 32, 32, 1) x = tf.ones(input_shape) highres_layer = HighRes3DNet( num_classes=5, w_regularizer=regularizers.l2_regularizer(0.5), b_regularizer=regularizers.l2_regularizer(0.5)) out = highres_layer(x, is_training=True) # print(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) with self.test_session() as sess: sess.run(tf.global_variables_initializer()) out = sess.run(out) self.assertAllClose((2, 32, 32, 5), out.shape)
def shape_test(self, input_shape, expected_shape): x = tf.ones(input_shape) highres_layer = HighRes3DNet(num_classes=5) highres_layer_small = HighRes3DNetSmall(num_classes=5) highres_layer_large = HighRes3DNetLarge(num_classes=5) out = highres_layer(x, is_training=True) out_small = highres_layer_small(x, is_training=True) out_large = highres_layer_large(x, is_training=True) with self.cached_session() as sess: sess.run(tf.global_variables_initializer()) out, out_large, out_small = sess.run([out, out_large, out_small]) self.assertAllClose(expected_shape, out.shape) self.assertAllClose(expected_shape, out_large.shape) self.assertAllClose(expected_shape, out_small.shape)
def shape_test_reg(self, input_shape, expected_shape): x = tf.ones(input_shape) layer_param = { 'num_classes': 5, 'w_regularizer': regularizers.l2_regularizer(0.5), 'b_regularizer': regularizers.l2_regularizer(0.5)} highres_layer = HighRes3DNet(**layer_param) highres_layer_small = HighRes3DNetSmall(**layer_param) highres_layer_large = HighRes3DNetLarge(**layer_param) out = highres_layer(x, is_training=True) out_small = highres_layer_small(x, is_training=True) out_large = highres_layer_large(x, is_training=True) with self.cached_session() as sess: sess.run(tf.global_variables_initializer()) out, out_large, out_small = sess.run([out, out_large, out_small]) self.assertAllClose(expected_shape, out.shape) self.assertAllClose(expected_shape, out_large.shape) self.assertAllClose(expected_shape, out_small.shape)
def layer_op(self, images, is_training=True, layer_id=-1, **unused_kwargs): n_modality = images.shape.as_list()[-1] rank = images.shape.ndims assert n_modality > 1 roots = tf.split(images, n_modality, axis=rank - 1) for (idx, root) in enumerate(roots): conv_layer = ConvolutionalLayer( n_output_chns=self.n_features, kernel_size=3, w_initializer=self.initializers['w'], w_regularizer=self.regularizers['w'], acti_func=self.acti_func, name='conv_{}'.format(idx)) roots[idx] = conv_layer(root, is_training) roots = tf.stack(roots, axis=-1) back_end = ScaleBlock('AVERAGE', n_layers=1) output_tensor = back_end(roots, is_training) front_end = HighRes3DNet(self.num_classes) output_tensor = front_end(output_tensor, is_training) return output_tensor