def inception_module(input, n_filters=64, kernel_sizes=[3, 5], is_training=None, bn_decay=None, scope='inception'): one_by_one = tf_util.conv3d(input, n_filters, [1, 1, 1], scope=scope + '_conv1', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) three_by_three = tf_util.conv3d( one_by_one, int(n_filters / 2), [kernel_sizes[0], kernel_sizes[0], kernel_sizes[0]], scope=scope + '_conv2', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) five_by_five = tf_util.conv3d( one_by_one, int(n_filters / 2), [kernel_sizes[1], kernel_sizes[1], kernel_sizes[1]], scope=scope + '_conv3', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) average_pooling = tf_util.avg_pool3d( input, [kernel_sizes[0], kernel_sizes[0], kernel_sizes[0]], scope=scope + '_avg_pool', stride=[1, 1, 1], padding='SAME') average_pooling = tf_util.conv3d(average_pooling, n_filters, [1, 1, 1], scope=scope + '_conv4', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) output = tf.concat( [one_by_one, three_by_three, five_by_five, average_pooling], axis=4) #output = output + tf.tile(input) ??? #resnet return output
def inception_module(input, n_filters=64, kernel_sizes=[3,5], is_training=None, bn_decay=None, scope='inception'): ''' :param input: [B,K,K,K,FV] :param n_filters: :return: output: [B,K,K,K,n_filters*2] ''' one_by_one = tf_util.conv3d(input, n_filters, [1,1,1], scope= scope + '_conv1', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) three_by_three = tf_util.conv3d(one_by_one, int(n_filters), [kernel_sizes[0], kernel_sizes[0], kernel_sizes[0]], scope= scope + '_conv2', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) five_by_five = tf_util.conv3d(one_by_one, int(n_filters), [kernel_sizes[1], kernel_sizes[1], kernel_sizes[1]], scope=scope + '_conv3', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) average_pooling = tf_util.avg_pool3d(input, [kernel_sizes[0], kernel_sizes[0], kernel_sizes[0]], scope=scope+'_avg_pool', stride=[1, 1, 1], padding='SAME') average_pooling = tf_util.conv3d(average_pooling, n_filters, [1,1,1], scope= scope + '_conv4', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) output = tf.concat([ one_by_one, three_by_three, five_by_five, average_pooling], axis=4) return output
def inception_module(input, n_filters=64, kernel_sizes=[3, 5], is_training=None, bn_decay=None, scope='inception'): """ 3D inception_module """ one_by_one = tf_util.conv3d(input, n_filters, [1, 1, 1], scope= scope + '_conv1', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) three_by_three = tf_util.conv3d(one_by_one, int(n_filters/2), [kernel_sizes[0], kernel_sizes[0], kernel_sizes[0]], scope= scope + '_conv2', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) five_by_five = tf_util.conv3d(one_by_one, int(n_filters/2), [kernel_sizes[1], kernel_sizes[1], kernel_sizes[1]], scope=scope + '_conv3', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) average_pooling = tf_util.avg_pool3d(input, [kernel_sizes[0], kernel_sizes[0], kernel_sizes[0]], scope=scope+'_avg_pool', stride=[1, 1, 1], padding='SAME') average_pooling = tf_util.conv3d(average_pooling, n_filters, [1,1,1], scope= scope + '_conv4', stride=[1, 1, 1], padding='SAME', bn=True, bn_decay=bn_decay, is_training=is_training) output = tf.concat([ one_by_one, three_by_three, five_by_five, average_pooling], axis=4) #output = output + tf.tile(input) ??? #resnet return output if __name__ == '__main__': with tf.Graph().as_default(): inputs = tf.zeros((32, 1024, 3)) outputs = get_model(inputs, tf.constant(True)) print (outputs)
def pointnet_sa_module(cascade_id, xyz, points, bidmap, mlp_configs, block_bottom_center_mm, configs, sgf_config_pls, is_training, bn_decay, scope, bn=True, tnet_spec=None, use_xyz=True, IsShowModel=False): ''' Input cascade_id==0: xyz is grouped_points: (batch_size,nsubblock0,npoint_subblock0,6) points: None bidmap: None Input cascade_id==1: xyz: (batch_size,nsubblock0,3) points: (batch_size,nsubblock0,channel) bidmap: (batch_size,nsubblock1,npoint_subblock1) Medium cascade_id==1: grouped_xyz: (batch_size,nsubblock1,npoint_subblock1,3) new_xyz: (batch_size,nsubblock1,3) group_points: (batch_size,nsubblock1,npoint_subblock1,channel) output cascade_id==1: new_xyz: (batch_size,nsubblock1,3) new_points: (batch_size,nsubblock1,channel) ''' block_bottom_center_mm = tf.cast( block_bottom_center_mm, tf.float32, name='block_bottom_center_mm' ) # gpu_0/sa_layer3/block_bottom_center_mm:0 batch_size = xyz.get_shape()[0].value with tf.variable_scope(scope) as sc: cascade_num = configs['flatten_bm_extract_idx'].shape[ 0] - 1 # include global here (Note: cascade_num does not include global in block_pre_util ) assert configs['sub_block_step_candis'].size == cascade_num - 1 if cascade_id == 0: indrop_keep_mask = tf.get_default_graph().get_tensor_by_name( 'indrop_keep_mask:0') # indrop_keep_mask:0 assert len(xyz.shape) == 3 if bidmap == None: grouped_xyz = tf.expand_dims(xyz, 1) grouped_points = tf.expand_dims(points, 1) new_xyz = None valid_mask = None else: batch_idx = tf.reshape(tf.range(batch_size), [batch_size, 1, 1, 1]) nsubblock = bidmap.get_shape()[1].value npoint_subblock = bidmap.get_shape()[2].value batch_idx_ = tf.tile(batch_idx, [1, nsubblock, npoint_subblock, 1]) bidmap = tf.expand_dims(bidmap, axis=-1, name='bidmap') bidmap_concat = tf.concat( [batch_idx_, bidmap], axis=-1, name='bidmap_concat') # gpu_0/sa_layer0/bidmap_concat:0 # The value for invalid item in bidmap is -17. # On GPU, the responding grouped_xyz and grouped_points is 0. # NOT WORK on CPU !!! # invalid indices comes from merge_blocks_while_fix_bmap # set point_indices_f for invalid points as # NETCONFIG['redundant_points_in_block'] ( shoud be set < -500) valid_mask = tf.greater(bidmap, tf.constant( -500, tf.int32), 'valid_mask') # gpu_0/sa_layer0/valid_mask:0 grouped_xyz = tf.gather_nd( xyz, bidmap_concat, name='grouped_xyz') # gpu_0/sa_layer0/grouped_xyz:0 grouped_points = tf.gather_nd(points, bidmap_concat, name='group_points') if cascade_id == 0 and len(indrop_keep_mask.get_shape()) != 0: grouped_indrop_keep_mask = tf.gather_nd( indrop_keep_mask, bidmap_concat, name='grouped_indrop_keep_mask' ) # gpu_0/sa_layer0/grouped_indrop_keep_mask:0 # new_xyz is the "voxel center" or "mean position of points in the voxel" if configs['mean_grouping_position'] and ( not mlp_configs['block_learning'] == '3DCNN'): new_xyz = tf.reduce_mean(grouped_xyz, -2) else: new_xyz = block_bottom_center_mm[:, :, 3:6] * tf.constant( 0.001, tf.float32) # the mid can be mean or block center, decided by configs['mean_grouping_position'] sub_block_mid = tf.expand_dims( new_xyz, -2, name='sub_block_mid') # gpu_1/sa_layer0/sub_block_mid global_block_mid = tf.reduce_mean(sub_block_mid, 1, keepdims=True, name='global_block_mid') grouped_xyz_submid = grouped_xyz - sub_block_mid grouped_xyz_glomid = grouped_xyz - global_block_mid grouped_xyz_feed = [] if 'raw' in configs['xyz_elements']: grouped_xyz_feed.append(grouped_xyz) if 'sub_mid' in configs['xyz_elements']: grouped_xyz_feed.append(grouped_xyz_submid) if 'global_mid' in configs['xyz_elements']: grouped_xyz_feed.append(grouped_xyz_glomid) grouped_xyz_feed = tf.concat(grouped_xyz_feed, -1) if cascade_id == 0: # xyz must be at the first in feed_data_elements !!!! grouped_points = tf.concat( [grouped_xyz_feed, grouped_points[..., 3:]], -1) if len(indrop_keep_mask.get_shape()) != 0: if InDropMethod == 'set1st': # set all the dropped item as the first item tmp1 = tf.multiply(grouped_points, grouped_indrop_keep_mask) points_1st = grouped_points[:, :, 0:1, :] points_1st = tf.tile(points_1st, [1, 1, grouped_points.shape[2], 1]) indrop_mask_inverse = 1 - grouped_indrop_keep_mask tmp2 = indrop_mask_inverse * points_1st grouped_points = tf.add( tmp1, tmp2, name='grouped_points_droped' ) # gpu_0/sa_layer0/grouped_points_droped #tf.add_to_collection( 'check', grouped_points ) elif InDropMethod == 'set0': valid_mask = tf.logical_and( valid_mask, tf.equal(grouped_indrop_keep_mask, 0), name='valid_mask_droped' ) # gpu_1/sa_layer0/valid_mask_droped elif use_xyz: grouped_points = tf.concat([grouped_xyz_feed, grouped_points], axis=-1) tf.add_to_collection('grouped_xyz', grouped_xyz) tf.add_to_collection('grouped_xyz_submid', grouped_xyz_submid) tf.add_to_collection('grouped_xyz_glomid', grouped_xyz_glomid) if cascade_id > 0 and use_xyz and (not cascade_id == cascade_num - 1): grouped_points = tf.concat([grouped_xyz_feed, grouped_points], axis=-1) nsample = grouped_points.get_shape()[2].value # the conv kernel size if IsShowModel: print( '\n\npointnet_sa_module cascade_id:%d\n xyz:%s\n grouped_xyz:%s\n new_xyz:%s\n grouped_points:%s\n nsample:%d' % (cascade_id, shape_str([xyz]), shape_str([grouped_xyz]), shape_str([new_xyz]), shape_str([grouped_points]), nsample)) new_points = grouped_points if valid_mask != None: new_points = new_points * tf.cast(valid_mask[:, :, :, 0:1], tf.float32) if 'growth_rate' in mlp_configs['point_encoder'][cascade_id]: new_points = tf_util.dense_net( new_points, mlp_configs['point_encoder'][cascade_id], bn, is_training, bn_decay,\ scope = 'dense_cascade_%d_point_encoder'%(cascade_id) , is_show_model = IsShowModel ) else: for i, num_out_channel in enumerate( mlp_configs['point_encoder'][cascade_id]): new_points = tf_util.conv2d(new_points, num_out_channel, [1, 1], padding='VALID', stride=[1, 1], bn=bn, is_training=is_training, scope='conv%d' % (i), bn_decay=bn_decay) if configs['Cnn_keep_prob'] < 1: if (not configs['only_last_layer_ineach_cascade'] ) or i == len( mlp_configs['point_encoder'][cascade_id]) - 1: new_points = tf_util.dropout( new_points, keep_prob=configs['Cnn_keep_prob'], is_training=is_training, scope='dropout', name='cnn_dp%d' % (i)) if IsShowModel: print('point encoder1 %d, new_points:%s' % (i, shape_str([new_points]))) if cascade_id == 0: root_point_features = new_points #if InDropMethod == 'set0': # if len(indrop_keep_mask.get_shape()) != 0: # new_points = tf.identity(new_points,'points_before_droped') # gpu_0/sa_layer0/points_before_droped:0 # new_points = tf.multiply( new_points, grouped_indrop_keep_mask, name='droped_points' ) # gpu_0/sa_layer0/droped_points:0 else: root_point_features = None pooling = mlp_configs['block_learning'] if pooling == '3DCNN' and (cascade_id == 0): pooling = 'max' #if pooling=='avg': # new_points = tf_util.avg_pool2d(new_points, [1,nsample], stride=[1,1], padding='VALID', scope='avgpool1') #elif pooling=='weighted_avg': # with tf.variable_scope('weighted_avg1'): # dists = tf.norm(grouped_xyz,axis=-1,ord=2,keep_dims=True) # exp_dists = tf.exp(-dists * 5) # weights = exp_dists/tf.reduce_sum(exp_dists,axis=2,keep_dims=True) # (batch_size, npoint, nsample, 1) # new_points *= weights # (batch_size, npoint, nsample, mlps_0[-1]) # new_points = tf.reduce_sum(new_points, axis=2, keep_dims=True) if pooling == 'max': # Even the grouped_points and grouped_xyz are 0 for invalid points, the # vaule after mlp will not be. It has to be set as 0 forcely before # pooling. if valid_mask != None: new_points = new_points * tf.cast(valid_mask[:, :, :, 0:1], tf.float32) new_points = tf.identity( new_points, 'points_before_max') # gpu_0/sa_layer0/points_before_max new_points = tf.reduce_max(new_points, axis=[2], keepdims=True, name='points_after_max') #elif pooling=='min': # new_points = tf_util.max_pool2d(-1*new_points, [1,nsample], stride=[1,1], padding='VALID', scope='minpool1') #elif pooling=='max_and_avg': # avg_points = tf_util.max_pool2d(new_points, [1,nsample], stride=[1,1], padding='VALID', scope='maxpool1') # max_points = tf_util.avg_pool2d(new_points, [1,nsample], stride=[1,1], padding='VALID', scope='avgpool1') # new_points = tf.concat([avg_points, max_points], axis=-1) elif pooling == '3DCNN': new_points = grouped_points_to_voxel_points( cascade_id, new_points, valid_mask, block_bottom_center_mm, configs, grouped_xyz, IsShowVoxelModel=IsShowModel) if IsShowModel: print('voxel points:%s' % (shape_str([new_points]))) for i, num_out_channel in enumerate( mlp_configs['voxel_channels'][cascade_id]): kernel_i = [mlp_configs['voxel_kernels'][cascade_id][i]] * 3 stride_i = [mlp_configs['voxel_strides'][cascade_id][i]] * 3 if new_points.shape[1] % 2 == 0: padding_i = np.array([[0, 0], [1, 0], [1, 0], [1, 0], [ 0, 0 ]]) * mlp_configs['voxel_paddings'][cascade_id][i] else: padding_i = np.array([[0, 0], [1, 1], [1, 1], [1, 1], [ 0, 0 ]]) * mlp_configs['voxel_paddings'][cascade_id][i] new_points = tf.pad(new_points, padding_i, "CONSTANT") if type(num_out_channel) == int: new_points = tf_util.conv3d(new_points, num_out_channel, kernel_i, scope='3dconv_%d' % (i), stride=stride_i, padding='VALID', bn=bn, is_training=is_training, bn_decay=bn_decay, name='points_3dcnn_%d' % (i)) if IsShowModel: print('block learning by 3dcnn %d, new_points:%s' % (i, shape_str([new_points]))) elif num_out_channel == 'max': new_points = tf_util.max_pool3d(new_points, kernel_i, scope='3dmax_%d' % (i), stride=stride_i, padding='VALID') if IsShowModel: print('block learning max pooling %d, new_points:%s' % (i, shape_str([new_points]))) elif num_out_channel == 'avg': new_points = tf_util.avg_pool3d(new_points, kernel_i, scope='3dmax_%d' % (i), stride=stride_i, padding='VALID') if IsShowModel: print('block learning avg pooling %d, new_points:%s' % (i, shape_str([new_points]))) # gpu_0/sa_layer1/3dconv_0/points_3dcnn_0:0 if configs['Cnn_keep_prob'] < 1: if (not configs['only_last_layer_ineach_cascade'] ) or i == len( mlp_configs['voxel_channels'][cascade_id]) - 1: new_points = tf_util.dropout( new_points, keep_prob=configs['Cnn_keep_prob'], is_training=is_training, scope='dropout', name='3dcnn_dp%d' % (i)) # gpu_0/sa_layer4/3dconv_0/points_3dcnn_0:0 new_points = tf.squeeze(new_points, [1, 2, 3]) new_points = tf.reshape( new_points, [batch_size, -1, 1, new_points.shape[-1].value]) if IsShowModel: print('after %s, new_points:%s' % (pooling, shape_str([new_points]))) if 'growth_rate' in mlp_configs['block_encoder'][cascade_id]: new_points = tf_util.dense_net( new_points, mlp_configs['block_encoder'][cascade_id], bn, is_training, bn_decay, scope='dense_cascade_%d_block_encoder' % (cascade_id), is_show_model=IsShowModel) else: for i, num_out_channel in enumerate( mlp_configs['block_encoder'][cascade_id]): new_points = tf_util.conv2d(new_points, num_out_channel, [1, 1], padding='VALID', stride=[1, 1], bn=bn, is_training=is_training, scope='conv_post_%d' % (i), bn_decay=bn_decay) if configs['Cnn_keep_prob'] < 1: if (not configs['only_last_layer_ineach_cascade'] ) or i == len( mlp_configs['block_encoder'][cascade_id]) - 1: new_points = tf_util.dropout( new_points, keep_prob=configs['Cnn_keep_prob'], is_training=is_training, scope='dropout', name='cnn_dp%d' % (i)) if IsShowModel: print('block encoder %d, new_points:%s' % (i, shape_str([new_points]))) # (2, 512, 1, 64) new_points = tf.squeeze(new_points, [2]) # (batch_size, npoints, mlps_1[-1]) if IsShowModel: print( 'pointnet_sa_module return\n new_xyz: %s\n new_points:%s\n\n' % (shape_str([new_xyz]), shape_str([new_points]))) #import pdb;pdb.set_trace() # (2, 512, 64) return new_xyz, new_points, root_point_features