Example #1
0
def get_model(point_cloud, is_training,Transform=False, bn_decay=None):
    forloss = []
    idx=None
    if point_cloud.get_shape()[-1].value>2:
        l0_xyz = point_cloud[:,:,0:2]
        l0_points = point_cloud [:,:,2:]
    else:
        l0_xyz = point_cloud
        l0_points = None


    if Transform:
        l0_xyz, tnet,l0_points= transform_moudule(l0_xyz,l0_points, is_training,idx, [64,128,1024],[[1, 1], [1, 1],[1, 1]], [512,256],num_channle=32, nsample=32, scope='Tnet1', bn_decay=bn_decay, bn=True, K=2)
        #forloss=tnet
    l0_points, plane_feature, _ = plane_module(l0_xyz, l0_points, idx, centralize=True, num_channle=32, npoint=512,
                                               have_sample=False,
                                               nsample=32, is_training=is_training, scope='plane', pool='avg',
                                               use_xyz=True, bn=True, bn_decay=bn_decay, weight_decay=0.0)

    new_points,_ = conv_module(l0_points,plane_feature,mlp=[64,128,128,1024], is_training=is_training, scope='conv', bn=True, bn_decay=bn_decay)
    # Fully connected layers
    net = util.fully_connected(new_points, 512, bn=True, is_training=is_training, scope='fc1', bn_decay=bn_decay)
    net = util.dropout(net, keep_prob=0.5, is_training=is_training, scope='dp1')
    net = util.fully_connected(net, 256, bn=True, is_training=is_training, scope='fc2', bn_decay=bn_decay)
    net = util.dropout(net, keep_prob=0.5, is_training=is_training, scope='dp2')
    net = util.fully_connected(net, 10, activation_fn=None, scope='fc3')


    return net, forloss
Example #2
0
    def create(self):
        self.X = tf.reshape(
            self.X,
            shape=[-1, self.IMAGE_SIZE, self.IMAGE_SIZE, self.CHANEELS])
        conv1 = util.conv_layer(self.X,
                                ksize=[11, 11, self.CHANEELS, 96],
                                strides=[1, 4, 4, 1],
                                name='conv1')
        pool1 = util.max_pool_layer(conv1,
                                    ksize=[1, 3, 3, 1],
                                    strides=[1, 2, 2, 1],
                                    name='pool1')

        conv2 = util.conv_layer(pool1,
                                ksize=[5, 5, 96, 256],
                                strides=[1, 1, 1, 1],
                                name='conv2',
                                padding='SAME',
                                group=2)
        pool2 = util.max_pool_layer(conv2,
                                    ksize=[1, 3, 3, 1],
                                    strides=[1, 2, 2, 1],
                                    name='pool2')

        conv3 = util.conv_layer(pool2,
                                ksize=[3, 3, 256, 384],
                                strides=[1, 1, 1, 1],
                                name='conv3',
                                padding='SAME')

        conv4 = util.conv_layer(conv3,
                                ksize=[3, 3, 384, 384],
                                strides=[1, 1, 1, 1],
                                name='conv4',
                                padding='SAME',
                                group=2)

        conv5 = util.conv_layer(conv4,
                                ksize=[3, 3, 384, 256],
                                strides=[1, 1, 1, 1],
                                name='conv5',
                                padding='SAME',
                                group=2)
        pool5 = util.max_pool_layer(conv5,
                                    ksize=[1, 3, 3, 1],
                                    strides=[1, 2, 2, 1],
                                    name='pool5')

        fc6 = util.full_connected_layer(pool5, 4096, name='fc6')
        fc6_dropout = util.dropout(fc6, self.KEEP_PROB)

        fc7 = util.full_connected_layer(fc6_dropout, 4096, name='fc7')
        fc7_dropout = util.dropout(fc7, self.KEEP_PROB)

        self.fc8 = util.full_connected_layer(fc7_dropout,
                                             self.NUM_CLASSES,
                                             name='fc8',
                                             relu=False)
Example #3
0
    def build_char_states(self, char_embed, is_training, reuse, char_ids,
                          char_lengths):
        """Build char embedding network for the QA model."""
        max_char_length = self.cfg.max_char_length

        inputs = dropout(tf.nn.embedding_lookup(char_embed, char_ids),
                         self.cfg.dropout, is_training)
        inputs = tf.reshape(
            inputs, shape=[max_char_length, -1, self.cfg.char_embed_dim])
        char_lengths = tf.reshape(char_lengths, shape=[-1])
        with tf.variable_scope('char_encoding', reuse=reuse):
            cell_fw = XGRUCell(hidden_dim=self.cfg.char_embed_dim)
            cell_bw = XGRUCell(hidden_dim=self.cfg.char_embed_dim)
            _, (left_right, right_left) = tf.nn.bidirectional_dynamic_rnn(
                cell_fw=cell_fw,
                cell_bw=cell_bw,
                sequence_length=char_lengths,
                inputs=inputs,
                time_major=True,
                dtype=tf.float32)

        left_right = tf.reshape(left_right,
                                shape=[-1, self.cfg.char_embed_dim])

        right_left = tf.reshape(right_left,
                                shape=[-1, self.cfg.char_embed_dim])

        states = tf.concat([left_right, right_left], axis=1)
        out_shape = tf.shape(char_ids)[1:3]
        out_shape = tf.concat([
            out_shape,
            tf.constant(value=[self.cfg.char_embed_dim * 2], dtype=tf.int32)
        ],
                              axis=0)
        return tf.reshape(states, shape=out_shape)
Example #4
0
def model(X, w_L1, w_L2, w_L3, w_L4, p_drop_conv, p_drop_hidden):
    crossnormalizer = crossnorm(alpha=1e-4, k=2, beta=0.75, n=9)
    l1b = rectify(cuconv(X, w_L1, border_mode=(5, 5)))
    l1 = cupool(l1b, (3, 3), stride=(2, 2))
    l1 = crossnormalizer(l1)
    l1 = dropout(l1, p_drop_conv)

    l2b = rectify(cuconv(l1, w_L2, border_mode='full'))
    l2 = cupool(l2b, (3, 3), stride=(2, 2))
    l2 = crossnormalizer(l2)
    l2 = dropout(l2, p_drop_conv)

    l3b = rectify(cuconv(l2, w_L3, border_mode='full'))
    l3 = cupool(l3b, (3, 3), stride=(2, 2))
    l3 = crossnormalizer(l3)

    l4 = T.flatten(l3, outdim=2)

    pyx = dropout(l4, p_drop_hidden)
    pyx = softmax(T.dot(pyx, w_L4))
    return l1, l2, l3, pyx
Example #5
0
def rnn(input_states, sequence_lengths, dropout_rate, is_training, num_units):
    layer_cnt = 1
    states = []
    xs = tf.transpose(input_states, perm=[1, 0, 2])
    for i in range(0, layer_cnt):
        xs = dropout(xs, dropout_rate, is_training)
        with tf.variable_scope('layer_' + str(i)):
            cell_fw = XGRUCell(num_units)
            cell_bw = XGRUCell(num_units)
            outputs, _ = tf.nn.bidirectional_dynamic_rnn(
                cell_fw=cell_fw,
                cell_bw=cell_bw,
                dtype=tf.float32,
                sequence_length=sequence_lengths,
                inputs=xs,
                time_major=True)

        y_lr, y_rl = outputs
        xs = tf.concat([y_lr, y_rl], 2)
        states.append(xs)

    return tf.transpose(dropout(tf.concat(states, axis=2),
                                dropout_rate,
                                is_training), perm=[1, 0, 2])
Example #6
0
def multihead_attention(queries,
                        keys,
                        scope="multihead_attention",
                        num_units=None,
                        num_heads=4,
                        dropout_rate=0,
                        is_training=True,
                        causality=False):
    '''Applies multihead attention.

    Args:
      queries: A 3d tensor with shape of [N, T_q, C_q].
      keys: A 3d tensor with shape of [N, T_k, C_k].
      num_units: A cdscalar. Attention size.
      dropout_rate: A floating point number.
      is_training: Boolean. Controller of mechanism for dropout.
      causality: Boolean. If true, units that reference the future are masked.
      num_heads: An int. Number of heads.
      scope: Optional scope for `variable_scope`.
      reuse: Boolean, whether to reuse the weights of a previous layer
        by the same name.

    Returns
      A 3d tensor with shape of (N, T_q, C)
    '''
    global look5
    with tf.variable_scope(scope):
        # Set the fall back option for num_units
        if num_units is None:
            num_units = queries.get_shape().as_list()[-1]

        Q_ = []
        K_ = []
        V_ = []
        for head_i in range(num_heads):
            Q = tf.layers.dense(queries, num_units / num_heads,
                                activation=tf.nn.relu, name='Query' + str(head_i))  # (N, T_q, C)
            K = tf.layers.dense(keys, num_units / num_heads,
                                activation=tf.nn.relu, name='Key' + str(head_i))  # (N, T_k, C)
            V = tf.layers.dense(keys, num_units / num_heads,
                                activation=tf.nn.relu, name='Value' + str(head_i))  # (N, T_k, C)
            Q_.append(Q)
            K_.append(K)
            V_.append(V)

        # Split and concat
        Q_ = tf.concat(Q_, axis=0)  # (h*N, T_q, C/h)
        K_ = tf.concat(K_, axis=0)  # (h*N, T_k, C/h)
        V_ = tf.concat(V_, axis=0)  # (h*N, T_k, C/h)

        # Multiplication
        outputs = tf.matmul(Q_, tf.transpose(K_, [0, 2, 1]))  # (h*N, T_q, T_k)

        # Scale
        outputs = outputs / (K_.get_shape().as_list()[-1] ** 0.5)

        # Key Masking
        key_masks = tf.sign(tf.abs(tf.reduce_sum(keys, axis=-1)))  # (N, T_k)
        key_masks = tf.tile(key_masks, [num_heads, 1])  # (h*N, T_k)
        key_masks = tf.tile(tf.expand_dims(key_masks, 1),
                            [1, tf.shape(queries)[1], 1])  # (h*N, T_q, T_k)

        paddings = tf.ones_like(outputs) * (-2 ** 32 + 1)
        outputs = tf.where(tf.equal(key_masks, 0), paddings,
                           outputs)  # (h*N, T_q, T_k)

        # Causality = Future blinding
        if causality:
            diag_vals = tf.ones_like(outputs[0, :, :])  # (T_q, T_k)
            tril = tf.contrib.linalg.LinearOperatorTriL(
                diag_vals).to_dense()  # (T_q, T_k)
            masks = tf.tile(tf.expand_dims(tril, 0),
                            [tf.shape(outputs)[0], 1, 1])  # (h*N, T_q, T_k)

            paddings = tf.ones_like(masks) * (-2 ** 32 + 1)
            outputs = tf.where(tf.equal(masks, 0), paddings,
                               outputs)  # (h*N, T_q, T_k)

        # Activation
        look5 = outputs
        outputs = tf.nn.softmax(outputs)  # (h*N, T_q, T_k)

        # Query Masking
        query_masks = tf.sign(
            tf.abs(tf.reduce_sum(queries, axis=-1)))  # (N, T_q)
        query_masks = tf.tile(query_masks, [num_heads, 1])  # (h*N, T_q)
        query_masks = tf.tile(tf.expand_dims(
            query_masks, -1), [1, 1, tf.shape(keys)[1]])  # (h*N, T_q, T_k)
        outputs *= query_masks  # broadcasting. (N, T_q, C)

        # Dropouts
        outputs = dropout(outputs, dropout_rate, is_training)

        # Weighted sum
        outputs = tf.matmul(outputs, V_)  # ( h*N, T_q, C/h)

        # Restore shape
        outputs = tf.concat(tf.split(outputs, num_heads,
                                     axis=0), axis=2)  # (N, T_q, C)

        # Residual connection
        if queries.get_shape().as_list()[-1] == num_units:
            outputs += queries

        # Normalize
        outputs = normalize(outputs, scope=scope)  # (N, T_q, C)

    return outputs
Example #7
0
def graph_to_network(input1,
                     input2,
                     input1_lengths,
                     input2_lengths,
                     p_graph,
                     dropout_rate,
                     is_training,
                     num_heads=1,
                     rnn_units=256):
    topology = p_graph.is_topology()
    layers = dict()
    layers_sequence_lengths = dict()
    num_units = input1.get_shape().as_list()[-1]
    layers[0] = input1*tf.sqrt(tf.cast(num_units, tf.float32)) + \
        positional_encoding(input1, scale=False, zero_pad=False)
    layers[1] = input2*tf.sqrt(tf.cast(num_units, tf.float32))
    layers[0] = dropout(layers[0], dropout_rate, is_training)
    layers[1] = dropout(layers[1], dropout_rate, is_training)
    layers_sequence_lengths[0] = input1_lengths
    layers_sequence_lengths[1] = input2_lengths
    for _, topo_i in enumerate(topology):
        if topo_i == '|':
            continue

        # Note: here we use the `hash_id` of layer as scope name, 
        #       so that we can automatically load sharable weights from previous trained models
        with tf.variable_scope(p_graph.layers[topo_i].hash_id, reuse=tf.AUTO_REUSE):
            if p_graph.layers[topo_i].graph_type == LayerType.input.value:
                continue
            elif p_graph.layers[topo_i].graph_type == LayerType.attention.value:
                with tf.variable_scope('attention'):
                    layer = multihead_attention(layers[p_graph.layers[topo_i].input[0]],
                                                layers[p_graph.layers[topo_i].input[1]],
                                                scope="multihead_attention",
                                                dropout_rate=dropout_rate,
                                                is_training=is_training,
                                                num_heads=num_heads,
                                                num_units=rnn_units * 2)
                    layer = feedforward(layer, scope="feedforward",
                                        num_units=[rnn_units * 2 * 4, rnn_units * 2])
                layers[topo_i] = layer
                layers_sequence_lengths[topo_i] = layers_sequence_lengths[
                    p_graph.layers[topo_i].input[0]]
            elif p_graph.layers[topo_i].graph_type == LayerType.self_attention.value:
                with tf.variable_scope('self-attention'):
                    layer = multihead_attention(layers[p_graph.layers[topo_i].input[0]],
                                                layers[p_graph.layers[topo_i].input[0]],
                                                scope="multihead_attention",
                                                dropout_rate=dropout_rate,
                                                is_training=is_training,
                                                num_heads=num_heads,
                                                num_units=rnn_units * 2)
                    layer = feedforward(layer, scope="feedforward",
                                        num_units=[rnn_units * 2 * 4, rnn_units * 2])
                layers[topo_i] = layer
                layers_sequence_lengths[topo_i] = layers_sequence_lengths[
                    p_graph.layers[topo_i].input[0]]
            elif p_graph.layers[topo_i].graph_type == LayerType.rnn.value:
                with tf.variable_scope('rnn'):
                    layer = rnn(layers[p_graph.layers[topo_i].input[0]],
                                layers_sequence_lengths[p_graph.layers[topo_i].input[0]],
                                dropout_rate,
                                is_training,
                                rnn_units)
                layers[topo_i] = layer
                layers_sequence_lengths[topo_i] = layers_sequence_lengths[
                    p_graph.layers[topo_i].input[0]]
            elif p_graph.layers[topo_i].graph_type == LayerType.output.value:
                layers[topo_i] = layers[p_graph.layers[topo_i].input[0]]
                if layers[topo_i].get_shape().as_list()[-1] != rnn_units * 1 * 2:
                    with tf.variable_scope('add_dense'):
                        layers[topo_i] = tf.layers.dense(
                            layers[topo_i], units=rnn_units*2)
    return layers[2], layers[3]
    def build_model(self, batch_size, spatial_size, points_xyz, points_features, is_training, num_class, batch_normalization_decay=None):
        """ Build a Fully-Convolutional Point Network.

            Args:
            batch_size: int
            spatial_size: np.array
            points_xyz: tf.placeholder
            points_features: tf.placeholder
            is_training: tf.placeholder
            num_class: int
            batch_normalization_decay: float

            Returns: tf.tensor

        """

        self.min_xyz_ = np.array([0, 0, 0])
        self.max_xyz_ = spatial_size
        self.use_batch_normalization_ = self._config['training']['optimizer']['batch_normalization'] != False

        pointnet_locations = util.get_uniformly_spaced_point_grid(self.min_xyz_, self.max_xyz_, self._config['model']['pointnet']['spacing'], batch_size)
        top_level_centroid_locations = util.get_uniformly_spaced_point_grid(self.min_xyz_, self.max_xyz_, self.get_centroid_spacing(self._config['model']['pointnet']['spacing'], self._abstraction_levels), batch_size)

        with tf.variable_scope("abstraction"):

            with tf.variable_scope("points_to_15cm"):

                with tf.variable_scope("simplified_pointnet"):

                    with tf.device('/gpu:' + str(self._config['training']['gpu']['id'])):
                        # Radius search and Grouping
                        grouped_points_xyz_and_features = self.radius_search_and_group(pointnet_locations, self.get_pointnet_radius(self._config['model']['pointnet']['spacing']), self._config['model']['pointnet']['neighbors'], points_xyz, points_features)

                    # 3x 1x1 Convolutions
                    features = util.conv2d(grouped_points_xyz_and_features, self._config['model']['filters']['abstraction']['points_to_15cm'][0], [1, 1], padding='VALID', stride=[1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1_conv_1', bn_decay=batch_normalization_decay)
                    features = util.conv2d(features, self._config['model']['filters']['abstraction']['points_to_15cm'][1], [1, 1], padding='VALID', stride=[1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1_conv_2', bn_decay=batch_normalization_decay)

                    # Max-pooling for permutation invariance
                    features = tf.reduce_max(features, axis=[2], keepdims=True)
                    features = tf.squeeze(features, [2])

                    num_dims = self.get_feature_volume_shape(spatial_size, self._config['model']['pointnet']['spacing'], 1)
                    features = tf.reshape(features, [batch_size, num_dims[0], num_dims[1], num_dims[2], features.get_shape().as_list()[-1]])

                with tf.variable_scope("skip_15cm"):
                
                    skip_15cm = util.conv3d(features, self._config['model']['filters']['skip']['15cm'], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv', bn_decay=batch_normalization_decay)

                with tf.variable_scope("skip_45cm"):
                    
                    padded = tf.pad(features, [[0,0], [1,1], [1,1], [1,1], [0,0]], "SYMMETRIC")
                    skip_45cm = util.conv3d(padded, self._config['model']['filters']['skip']['45cm'], [3, 3, 3], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='3x3x3_conv', bn_decay=batch_normalization_decay)

            with tf.variable_scope("15cm_to_30cm"):

                with tf.variable_scope("3d_convolution"):

                    features = util.conv3d(features, self._config['model']['filters']['abstraction']['15cm_to_30cm'][0], [2, 2, 2], padding='VALID', stride=[2, 2, 2], bn=self.use_batch_normalization_, is_training=is_training, scope='2x2x2_conv', bn_decay=batch_normalization_decay)
                    features = util.conv3d(features, self._config['model']['filters']['abstraction']['15cm_to_30cm'][1], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1_conv_1', bn_decay=batch_normalization_decay)

                with tf.variable_scope("skip_30cm"):

                    skip_30cm = util.conv3d(features, self._config['model']['filters']['skip']['30cm'], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv', bn_decay=batch_normalization_decay)

                with tf.variable_scope("skip_90cm"):

                    padded = tf.pad(features, [[0,0], [1,1], [1,1], [1,1], [0,0]], "SYMMETRIC")
                    skip_90cm = util.conv3d(padded, self._config['model']['filters']['skip']['90cm'], [3, 3, 3], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='3x3x3_conv', bn_decay=batch_normalization_decay)

            with tf.variable_scope("30cm_to_60cm"):

                with tf.variable_scope("3d_convolution"):

                    features = util.conv3d(features, self._config['model']['filters']['abstraction']['30cm_to_60cm'][0], [2, 2, 2], padding='VALID', stride=[2, 2, 2], bn=self.use_batch_normalization_, is_training=is_training, scope='2x2x2_conv', bn_decay=batch_normalization_decay)
                    features = util.conv3d(features, self._config['model']['filters']['abstraction']['30cm_to_60cm'][1], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv_1', bn_decay=batch_normalization_decay)
                    
                with tf.variable_scope("skip_60cm"):

                    skip_60cm = util.conv3d(features, self._config['model']['filters']['skip']['60cm'], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv_1', bn_decay=batch_normalization_decay)

                with tf.variable_scope("skip_180cm"):

                    padded = tf.pad(features, [[0,0], [1,1], [1,1], [1,1], [0,0]], "SYMMETRIC")
                    skip_180cm = util.conv3d(padded, self._config['model']['filters']['skip']['180cm'], [3, 3, 3], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='3x3x3_conv', bn_decay=batch_normalization_decay)

        with tf.variable_scope("spatial_pool"):

            num_cells_in_current_layer = self.get_feature_volume_shape(spatial_size, self._config['model']['pointnet']['spacing'], 3)

            with tf.variable_scope("reshape_and_repeat"):

                # Reshape and repeat feature volume to apply weighted spatial pooling
                features = tf.reshape(features, [batch_size, top_level_centroid_locations.get_shape()[1].value, self._config['model']['filters']['abstraction']['30cm_to_60cm'][-1]])
                features = tf.tile(tf.expand_dims(features, axis=1), [1, top_level_centroid_locations.get_shape()[1].value, 1, 1])

            with tf.variable_scope("pool"):
             
                spatial_pooling_weights = self.get_spatial_pool_weighting(self._config['model']['spatial_pool_radius'], top_level_centroid_locations)
                skip_spatial_pool = features * spatial_pooling_weights
                skip_spatial_pool = tf.reduce_sum(skip_spatial_pool, axis=2)
                skip_spatial_pool = tf.reshape(skip_spatial_pool, [batch_size, num_cells_in_current_layer[0], num_cells_in_current_layer[1], num_cells_in_current_layer[2], self._config['model']['filters']['abstraction']['30cm_to_60cm'][-1]])
                
            with tf.variable_scope("skip_spatial_pool"):
                skip_spatial_pool = util.conv3d(skip_spatial_pool, self._config['model']['filters']['skip']['spatial_pool'], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv', bn_decay=batch_normalization_decay)

        with tf.variable_scope("upsampling"):
            
            with tf.variable_scope("60cm_to_30cm"):

                features = tf.concat([skip_60cm, skip_180cm, skip_spatial_pool], axis=4)

                features = util.conv3d_transpose(features, self._config['model']['filters']['upsampling']['60cm_to_30cm'][0], [2, 2, 2], padding='VALID', stride=[2, 2, 2], bn=self.use_batch_normalization_, is_training=is_training, scope='2x2x2_deconv', bn_decay=batch_normalization_decay)
                features = util.conv3d(features, self._config['model']['filters']['upsampling']['60cm_to_30cm'][1], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv_1', bn_decay=batch_normalization_decay)
                features = util.conv3d(features, self._config['model']['filters']['upsampling']['60cm_to_30cm'][2], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv_2', bn_decay=batch_normalization_decay)
                
            with tf.variable_scope("30cm_to_15cm"):

                features = tf.concat([features, skip_30cm, skip_90cm], axis=4)
                features = util.conv3d_transpose(features, self._config['model']['filters']['upsampling']['30cm_to_15cm'][0], [2, 2, 2], padding='VALID', stride=[2, 2, 2], bn=self.use_batch_normalization_, is_training=is_training, scope='2x2x2_deconv', bn_decay=batch_normalization_decay)
                features = util.conv3d(features, self._config['model']['filters']['upsampling']['30cm_to_15cm'][1], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv_1', bn_decay=batch_normalization_decay)
                features = util.dropout(features, keep_prob=0.5, is_training=is_training, scope='dropout')

                features = tf.concat([features, skip_45cm], axis=4)
                features = util.conv3d(features, self._config['model']['filters']['upsampling']['30cm_to_15cm'][2], [1, 1, 1], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='1x1x1_conv_3', bn_decay=batch_normalization_decay)

            with tf.variable_scope("15cm_to_5cm"):

                features = tf.concat([features, skip_15cm], axis=4)
                features = util.dropout(features, keep_prob=0.5, is_training=is_training, scope='dropout')

                upsample_factor = int(math.ceil(self._config['model']['pointnet']['spacing'] / self._output_voxel_size))
                features = util.conv3d_transpose(features, self._config['model']['filters']['upsampling']['15cm_to_5cm'][0], [upsample_factor, upsample_factor, upsample_factor], padding='VALID', stride=[upsample_factor, upsample_factor, upsample_factor], bn=self.use_batch_normalization_, is_training=is_training, scope='final_deconv', bn_decay=batch_normalization_decay)
                features = tf.pad(features, [[0,0], [1,1], [1,1], [1,1], [0,0]], "SYMMETRIC")
                output = util.conv3d(features, num_class, [3, 3, 3], padding='VALID', stride=[1, 1, 1], bn=self.use_batch_normalization_, is_training=is_training, scope='final_conv', bn_decay=batch_normalization_decay, activation_fn=None)

                num_output_elements = np.prod(self.get_output_volume_shape(spatial_size, self._output_voxel_size))
                output = tf.reshape(output, [batch_size, num_output_elements, num_class])

        return output
Example #9
0
    def create(self):
        self.p = []
        self.X = tf.reshape(
            self.X,
            shape=[-1, self.IMAGE_SIZE, self.IMAGE_SIZE, self.CHANEELS])
        conv1_1 = util.conv_layer(self.X,
                                  kh=3,
                                  kw=3,
                                  n_out=64,
                                  sh=1,
                                  sw=1,
                                  name='conv1_1',
                                  p=self.p,
                                  padding='SAME')
        conv1_2 = util.conv_layer(conv1_1,
                                  kh=3,
                                  kw=3,
                                  n_out=64,
                                  sh=1,
                                  sw=1,
                                  name='conv1_2',
                                  p=self.p,
                                  padding='SAME')
        pool1 = util.max_pool_layer(conv1_2,
                                    kh=2,
                                    kw=2,
                                    sh=2,
                                    sw=2,
                                    name='pool1')

        conv2_1 = util.conv_layer(pool1,
                                  kh=3,
                                  kw=3,
                                  n_out=128,
                                  sh=1,
                                  sw=1,
                                  name='conv2_1',
                                  p=self.p,
                                  padding='SAME')
        conv2_2 = util.conv_layer(conv2_1,
                                  kh=3,
                                  kw=3,
                                  n_out=128,
                                  sh=1,
                                  sw=1,
                                  name='conv2_2',
                                  p=self.p,
                                  padding='SAME')
        pool2 = util.max_pool_layer(conv2_2,
                                    kh=2,
                                    kw=2,
                                    sh=2,
                                    sw=2,
                                    name='pool2')

        conv3_1 = util.conv_layer(pool2,
                                  kh=3,
                                  kw=3,
                                  n_out=256,
                                  sh=1,
                                  sw=1,
                                  name='conv3_1',
                                  p=self.p,
                                  padding='SAME')
        conv3_2 = util.conv_layer(conv3_1,
                                  kh=3,
                                  kw=3,
                                  n_out=256,
                                  sh=1,
                                  sw=1,
                                  name='conv3_2',
                                  p=self.p,
                                  padding='SAME')
        conv3_3 = util.conv_layer(conv3_2,
                                  kh=3,
                                  kw=3,
                                  n_out=256,
                                  sh=1,
                                  sw=1,
                                  name='conv3_3',
                                  p=self.p,
                                  padding='SAME')
        pool3 = util.max_pool_layer(conv3_3,
                                    kh=2,
                                    kw=2,
                                    sh=2,
                                    sw=2,
                                    name='pool3')

        conv4_1 = util.conv_layer(pool3,
                                  kh=3,
                                  kw=3,
                                  n_out=512,
                                  sh=1,
                                  sw=1,
                                  name='conv4_1',
                                  p=self.p,
                                  padding='SAME')
        conv4_2 = util.conv_layer(conv4_1,
                                  kh=3,
                                  kw=3,
                                  n_out=512,
                                  sh=1,
                                  sw=1,
                                  name='conv4_2',
                                  p=self.p,
                                  padding='SAME')
        conv4_3 = util.conv_layer(conv4_2,
                                  kh=3,
                                  kw=3,
                                  n_out=512,
                                  sh=1,
                                  sw=1,
                                  name='conv4_3',
                                  p=self.p,
                                  padding='SAME')
        pool4 = util.max_pool_layer(conv4_3,
                                    kh=2,
                                    kw=2,
                                    sh=2,
                                    sw=2,
                                    name='pool4')

        conv5_1 = util.conv_layer(pool4,
                                  kh=3,
                                  kw=3,
                                  n_out=512,
                                  sh=1,
                                  sw=1,
                                  name='conv5_1',
                                  p=self.p,
                                  padding='SAME')
        conv5_2 = util.conv_layer(conv5_1,
                                  kh=3,
                                  kw=3,
                                  n_out=512,
                                  sh=1,
                                  sw=1,
                                  name='conv5_2',
                                  p=self.p,
                                  padding='SAME')
        conv5_3 = util.conv_layer(conv5_2,
                                  kh=3,
                                  kw=3,
                                  n_out=512,
                                  sh=1,
                                  sw=1,
                                  name='conv5_3',
                                  p=self.p,
                                  padding='SAME')
        pool5 = util.max_pool_layer(conv5_3,
                                    kh=2,
                                    kw=2,
                                    sh=2,
                                    sw=2,
                                    name='pool5')

        fc6 = util.full_connected_layer(pool5,
                                        n_out=4096,
                                        name='fc6',
                                        p=self.p)
        fc6_dropout = util.dropout(fc6, self.KEEP_PROB, name='fc6_drop')

        fc7 = util.full_connected_layer(fc6_dropout,
                                        n_out=4096,
                                        name='fc7',
                                        p=self.p)
        fc7_dropout = util.dropout(fc7, self.KEEP_PROB, name='fc7_drop')

        self.fc8 = util.full_connected_layer(fc7_dropout,
                                             self.NUM_CLASSES,
                                             name='fc8',
                                             p=self.p)
        self.softmax = tf.nn.softmax(self.fc8)
        self.predictions = tf.argmax(self.softmax, 1)