コード例 #1
0
ファイル: pntconv_cls.py プロジェクト: godspeed1989/pntconv
    def get_model(self, point_cloud):
        """ Classification PointNet, input is BxNx3, output Bxnum_class """
        c = self.configuration
        print(colors.warning('Input'), colors.info(point_cloud.shape))

        if c.enable_rtn:
            point_cloud = RotTransLayer(point_cloud, c)

        all_features = []
        if c.with_shortcut:
            all_features.append(point_cloud)
        if c.with_pc_feature:
            pc_feat = GetPCFeature(point_cloud, c)
            print(colors.warning('PC_Feature'), colors.info(pc_feat.shape))
            pc_feat = LinearCombLayer(pc_feat, 128, 'LinearCombLayerPC', True, c)
            pc_feat = FeatureMapLayer(pc_feat, 128, 'FeatureMapLayerPC', c)
            all_features.append(pc_feat)
        if c.with_nn_feature:
            nn_feat = GetNNFeature(point_cloud, c.num_neighbor, 'GetNNFeature', c)
            print(colors.warning('NN_Feature'), colors.info(nn_feat.shape))
            nn_feat = LinearCombLayer(nn_feat, 128, 'LinearCombLayerNN', False, c)
            nn_feat = FeatureMapLayer(nn_feat, 128, 'FeatureMapLayerNN', c)
            all_features.append(nn_feat)
        #
        pt_feat = tf.concat(all_features, axis=2)
        #
        pt_feat = SetConvLayer(pt_feat, 256, 'SetConv_1', c)
        #
        pt_feat = SetConvLayer(pt_feat, 384, 'SetConv_2', c)
        #
        pt_feat = SetConvLayer(pt_feat, 512, 'SetConv_3', c)

        # (N, P, C) -> (N, C)
        net = tf.reduce_max(pt_feat, axis=1, keepdims=False)
        self.aux = {}
        self.aux['max_idx'] = tf.argmax(pt_feat, axis=1)
        self.aux['global_feature'] = net
        # MLP on global point cloud vector
        with tf.variable_scope('MLP_classify'):
            net = tf_util.fully_connected(net, 512, bn=True, is_training=c.is_training,
                                        scope='fc1', bn_decay=c.bn_decay, activation_fn=tf.nn.elu)
            net = tf_util.dropout(net, keep_prob=0.5, is_training=c.is_training,
                                scope='dp1')
            net = tf_util.fully_connected(net, 256, bn=True, is_training=c.is_training,
                                        scope='fc2', bn_decay=c.bn_decay, activation_fn=tf.nn.elu)
            net = tf_util.dropout(net, keep_prob=0.5, is_training=c.is_training,
                                scope='dp2')
            net = tf_util.fully_connected(net, c.num_class, activation_fn=None, scope='fc3')
        return net
コード例 #2
0
ファイル: pntconv_cls.py プロジェクト: godspeed1989/pntconv
def SetConvLayer(pt_feat, channels, tag, conf, bnorm=True, activation=tf.nn.elu):
    with tf.variable_scope(tag):
        # (N, P, C) -> (N, P, 1, C)
        pt_feat = tf.expand_dims(pt_feat, axis=-2)
        # (N, P, 1, C) -> (N, P, 1, channels)
        conv2d_scope = 'conv2d_' + str(channels)
        pt_feature = tf_util.conv2d(pt_feat, channels, [1,1],
                                    padding='VALID', stride=[1,1],
                                    bn=bnorm, is_training=conf.is_training,
                                    scope=conv2d_scope, bn_decay=conf.bn_decay,
                                    activation_fn=activation)
        # (N, P, 1, channels) -> (N, P, channels)
        pt_feature = tf.squeeze(pt_feature, axis=2)
        print(colors.warning(tag), colors.info(pt_feat.shape), colors.info(pt_feature.shape))
    return pt_feature
コード例 #3
0
ファイル: pntconv_cls.py プロジェクト: godspeed1989/pntconv
def FeatureMapLayer(feat, channels, scope, conf):
    with tf.variable_scope(scope, reuse=False):
        # (N, P, C) -> (N, P, 1, C)
        feat = tf.expand_dims(feat, axis=-2)
        # (N, P, 1, C) -> (N, P, 1, channels)
        conv2d_scope = 'conv2d_' + str(channels)
        feature = tf_util.conv2d(feat, channels, [1,1],
                                 padding='VALID', stride=[1,1],
                                 bn=True, is_training=conf.is_training,
                                 scope=conv2d_scope, bn_decay=conf.bn_decay,
                                 activation_fn=tf.nn.elu)
        # (N, P, 1, channels) -> (N, P, channels)
        feature = tf.squeeze(feature, axis=2)
    print(colors.warning(scope), colors.info(feat.shape), colors.info(feature.shape))
    return feature
コード例 #4
0
ファイル: vis_cls.py プロジェクト: godspeed1989/pntconv
def run(flags):
    print(colors.info('pid: %s' % str(os.getpid())))
    init(flags)
    visualization(flags,
                  cfmtrx=True,
                  maxpnt=True,
                  tsne=False,
                  weights_hist=True)
    LOG_FOUT.close()
コード例 #5
0
def predict():
    """Predict unseen images"""
    print(colors.info('Step 0: load data and trained model'))
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    checkpoint_dir = sys.argv[1]

    print(colors.info('Step 1: build the rnn model'))
    x = tf.placeholder("float", [None, n_steps, n_input])
    y = tf.placeholder("float", [None, n_classes])

    weights = tf.Variable(tf.random_normal([n_hidden, n_classes]),
                          name='weights')
    biases = tf.Variable(tf.random_normal([n_classes]), name='biases')

    pred = rnn_model(x, weights, biases)
    correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    print(colors.info('Step 2: predict new images with the trained model'))
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print(colors.info('Step 2.0: load the trained model'))
        checkpoint_file = tf.train.latest_checkpoint(
            os.path.join(checkpoint_dir, 'checkpoints'))
        print('Loaded the trained model: {}'.format(checkpoint_file))

        saver = tf.train.Saver()
        saver.restore(sess, checkpoint_file)

        # Step 2.1: predict new data
        test_len = 500
        test_data = mnist.test.images[:test_len].reshape(
            (-1, n_steps, n_input))
        test_label = mnist.test.labels[:test_len]
        print(
            colors.header("Testing Accuracy: {}".format(
                sess.run(accuracy, feed_dict={
                    x: test_data,
                    y: test_label
                }))))
コード例 #6
0
ファイル: pntconv_cls.py プロジェクト: godspeed1989/pntconv
def LinearCombLayer(feat, channels, scope, expand, conf):
    with tf.variable_scope(scope, reuse=False):
        if expand:
            # (N, P, C) -> (N, P, 1, C)
            feat = tf.expand_dims(feat, axis=-2)
        # (N, P, 1\K, C) -> (N, P, 1\K, channels)
        conv2d_scope = 'conv2d_' + str(channels)
        if conf.with_LC:
            activation = None
        else:
            activation = tf.nn.elu
        feature = tf_util.conv2d(feat, channels, [1,1],
                                 padding='VALID', stride=[1,1],
                                 bn=True, is_training=conf.is_training,
                                 scope=conv2d_scope, bn_decay=conf.bn_decay,
                                 activation_fn=activation)
        if expand:
            # (N, P, 1, channels) -> (N, P, channels)
            feature = tf.squeeze(feature, axis=2)
        else:
            # (N, P, K, channels) -> (N, P, channels)
            feature = tf.reduce_max(feature, axis=2, keepdims=False)
    print(colors.warning(scope), colors.info(feat.shape), colors.info(feature.shape))
    return feature
コード例 #7
0
ファイル: pntconv_cls.py プロジェクト: godspeed1989/pntconv
 def __init__(self, conf):
     print(colors.info('init %s model' % self.__class__.__name__))
     self.configuration = conf
コード例 #8
0
ファイル: eval_cls.py プロジェクト: godspeed1989/pntconv
def log_string(out_str):
    LOG_FOUT.write(out_str + '\n')
    LOG_FOUT.flush()
    print(colors.info(out_str))
コード例 #9
0
ファイル: eval_cls.py プロジェクト: godspeed1989/pntconv
def run(flags):
    print(colors.info('pid: %s' % str(os.getpid())))
    init(flags)
    evaluate(flags)
    LOG_FOUT.close()
    FLIST_FOUT.close()
コード例 #10
0
def run(flags):
    print(colors.info('pid: %s' % str(os.getpid())))
    init(flags)
    train(flags)
    LOG_FOUT.close()
コード例 #11
0
def train():
    """Train an image classifier"""
    print(colors.info('Step 0: load image data and training parameters'))
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    if len(sys.argv) > 1:
        parameter_file = sys.argv[1]
    else:
        parameter_file = 'parameters.json'
    params = json.loads(open(parameter_file).read())

    print(colors.info('Step 1: build a rnn model for image'))
    x = tf.placeholder("float", [None, n_steps, n_input])
    y = tf.placeholder("float", [None, n_classes])

    weights = tf.Variable(tf.random_normal([n_hidden, n_classes]),
                          name='weights')
    biases = tf.Variable(tf.random_normal([n_classes]), name='biases')
    pred = rnn_model(x, weights, biases)

    # optimizer
    cost = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=pred))
    optimizer = tf.train.AdamOptimizer(
        learning_rate=params['learning_rate']).minimize(cost)
    # accuracy
    correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    print(colors.info('Step 2: train the image classification model'))
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        epoch = 1

        print(
            colors.info('Step 2.0: create a directory for saving model files'))
        out_dir = "trained_model_" + str(int(time.time()))
        checkpoint_dir = os.path.join(out_dir, "checkpoints")
        checkpoint_prefix = os.path.join(checkpoint_dir, "model.ckpt")
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=3)

        print(
            colors.info('Step 2.1: train the image classifier batch by batch'))
        while epoch < params['training_iters']:
            batch_x, batch_y = mnist.train.next_batch(params['batch_size'])
            # Reshape data to get 28 seq of 28 elements
            batch_x = batch_x.reshape((params['batch_size'], n_steps, n_input))
            sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})

            # Step 2.2: save the model
            if epoch % params['display_step'] == 0:
                saver.save(sess, checkpoint_prefix, global_step=epoch)
                acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
                loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
                print(
                    colors.header(
                        'Iter: {}, Loss: {:.6f}, Accuracy: {:.6f}'.format(
                            epoch * params['batch_size'], loss, acc)))
            epoch += 1
        print(colors.success('The training is done'))

        # Step 3: test the model
        test_len = 128
        test_data = mnist.test.images[:test_len].reshape(
            (-1, n_steps, n_input))
        test_label = mnist.test.labels[:test_len]
        print(
            colors.header("Testing Accuracy: {}".format(
                sess.run(accuracy, feed_dict={
                    x: test_data,
                    y: test_label
                }))))