예제 #1
0
def init(f):
    global LOG_FOUT
    global MODEL
    global MODEL_CONF
    global TRAIN_FILES
    global TEST_FILES

    # IMPORT network module
    module = importlib.import_module(f['model'])
    MODEL = getattr(module, f['model'])
    MODEL_CONF = getattr(module, f['model'] + '_conf')
    MODEL_FILE = os.path.join(BASE_DIR, 'models', f['model'] + '.py')

    # MAKE log directory
    if not os.path.exists(f['log_dir']):
        os.mkdir(f['log_dir'])
    os.system('cp %s %s' % (MODEL_FILE, f['log_dir']))  # bkp of model def
    # CREATE log file
    LOG_FOUT = open(os.path.join(f['log_dir'], 'log_train.txt'), 'a')
    LOG_FOUT.write(dict2str(f))

    # GET dataset files' list
    TRAIN_FILES = provider.getDataFiles(
        os.path.join(f['dataset_path'], 'train_files.txt'))
    TEST_FILES = provider.getDataFiles(
        os.path.join(f['dataset_path'], 'test_files.txt'))
예제 #2
0
def load_test_data():
    """Load test data."""
    cwd = Path(os.path.abspath(os.path.dirname(__file__)))
    data = cwd / 'data'
    hdf5_dir = (data / 'hdf5_data').as_posix()
    test_hdf5_dir = os.path.join(hdf5_dir, 'test')
    test_files = provider.getDataFiles(
        os.path.join(test_hdf5_dir, 'all_files.txt'))

    data_batch_list = []
    label_batch_list = []
    count = 0
    for h5_filename in test_files:
        data_batch, label_batch = provider.load_h5(h5_filename)
        #       data_batch = provider.jitter_point_cloud(data_batch)
        print(f'h5_filename = {h5_filename}')
        print(f'data_batch.shape = {data_batch.shape}')
        count += data_batch.shape[0]
        data_batch_list.append(data_batch)
        label_batch_list.append(label_batch)

    data_batches = np.concatenate(data_batch_list, 0)
    label_batches = np.concatenate(label_batch_list, 0)
    print(f'data_batches.shape = {data_batches.shape}')
    print(f'label_batches.shape = {label_batches.shape}')

    test_idxs = list(range(0, count))
    print(f'len(test_idxs) = {len(test_idxs)}')

    test_data = data_batches[test_idxs, ...]
    test_label = label_batches[test_idxs]
    print(
        f'test_data.shape, test_label.shape = {test_data.shape}, {test_label.shape}'
    )
    return test_data, test_label
예제 #3
0
def load_train_data(start, stop):
    cwd = Path(os.path.abspath(os.path.dirname(__file__)))
    data = cwd / 'data'
    hdf5_dir = (data / 'hdf5_data').as_posix()
    train_hdf5_dir = os.path.join(hdf5_dir, 'train')
    files = provider.getDataFiles(os.path.join(train_hdf5_dir,
                                               'all_files.txt'))

    data_batch_list = []
    label_batch_list = []

    count = 0
    for h5_filename in files[start:stop]:
        data_batch, label_batch = provider.load_h5(h5_filename)
        #       data_batch = provider.jitter_point_cloud(data_batch)
        print(f'h5_filename = {h5_filename}')
        print(f'data_batch.shape = {data_batch.shape}')
        count += data_batch.shape[0]
        data_batch_list.append(data_batch)
        label_batch_list.append(label_batch)

    data_batches = np.concatenate(data_batch_list, 0)
    label_batches = np.concatenate(label_batch_list, 0)
    print(f'data_batches.shape = {data_batches.shape}')
    print(f'label_batches.shape = {label_batches.shape}')

    print(f'count = {count}')
    train_idxs = list(range(0, count))

    train_data = data_batches[train_idxs, ...]
    train_label = label_batches[train_idxs]
    print(
        f'train_data.shape, train_label.shape = {train_data.shape}, {train_label.shape}'
    )
    return train_data, train_label
예제 #4
0
def get_test_data(num_point=1024):
    print('get test num_point ', num_point)
    test_files = provider.getDataFiles(
        os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/test_files.txt'))
    print("test_file : {}".format(test_files))
    data = (PlyDataset(filepath, num_point=num_point, augment=False) for filepath in test_files)
    return data
예제 #5
0
def get_test_dataset(num_point=1024):
    print('get test num_point ', num_point)
    test_files = provider.getDataFiles(
        os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/test_files.txt'))
    return ConcatenatedDataset(
        *(PlyDataset(filepath, num_point=num_point, augment=False)
          for filepath in test_files))
예제 #6
0
    def __init__(self, train_test):
        all_files = provider.getDataFiles(
            'indoor3d_sem_seg_hdf5_data/all_files.txt')
        room_filelist = [
            line.rstrip()
            for line in open('indoor3d_sem_seg_hdf5_data/room_filelist.txt')
        ]
        self.data = []
        self.label = []
        for h5_filename in all_files:
            data_batch, label_batch = provider.loadDataFile(h5_filename)
            self.data.append(data_batch)
            self.label.append(label_batch)
        self.data = np.concatenate(self.data, 0)
        self.label = np.concatenate(self.label, 0)
        print(self.data.shape)
        print(self.label.shape)

        test_area = 'Area_' + str(6)
        train_idxs = []
        test_idxs = []
        for i, room_name in enumerate(room_filelist):
            if test_area in room_name:
                test_idxs.append(i)
            else:
                train_idxs.append(i)
        if train_test == "test":
            self.data = self.data[test_idxs, ...]
            self.label = self.label[test_idxs]
        else:
            self.data = self.data[train_idxs, ...]
            self.label = self.label[train_idxs]
        self.label = torch.tensor(self.label, dtype=torch.long)
예제 #7
0
def estimate(area):
    LOG_DIR = 'log{}'.format(area)
    num_classes = 13
    file_path = "data/train_hdf5_file_list_woArea{}.txt".format(area)

    train_file_list = provider.getDataFiles(file_path) 

    mean_ins_size = np.zeros(num_classes)
    ptsnum_in_gt = [[] for itmp in range(num_classes)]

    train_data = []
    train_group = []
    train_sem = []
    for h5_filename in train_file_list:
        cur_data, cur_group, _, cur_sem = provider.loadDataFile_with_groupseglabel_stanfordindoor(h5_filename)
        cur_data = np.reshape(cur_data, [-1, cur_data.shape[-1]])
        cur_group = np.reshape(cur_group, [-1])
        cur_sem = np.reshape(cur_sem, [-1])

        un = np.unique(cur_group)
        for ig, g in enumerate(un):
            tmp = (cur_group == g)
            sem_seg_g = int(stats.mode(cur_sem[tmp])[0])
            ptsnum_in_gt[sem_seg_g].append(np.sum(tmp))

    for idx in range(num_classes):
        mean_ins_size[idx] = np.mean(ptsnum_in_gt[idx]).astype(np.int)

    print(mean_ins_size)
    np.savetxt(os.path.join(LOG_DIR, 'mean_ins_size.txt'),mean_ins_size)
예제 #8
0
def get_data():

    ALL_FILES = provider.getDataFiles(
        'indoor3d_sem_seg_hdf5_data/all_files.txt')
    room_filelist = [
        line.rstrip()
        for line in open('indoor3d_sem_seg_hdf5_data/room_filelist.txt')
    ]

    # Load ALL data
    data_batch_list = []
    label_batch_list = []
    for h5_filename in ALL_FILES:
        data_batch, label_batch = provider.loadDataFile(h5_filename)
        data_batch_list.append(data_batch)
        label_batch_list.append(label_batch)

    test_area = 'Area_' + str(FLAGS.test_area)
    train_idxs = []
    test_idxs = []
    for i, room_name in enumerate(room_filelist):
        if test_area in room_name:
            test_idxs.append(i)
        else:
            train_idxs.append(i)

    data_batches = np.concatenate(data_batch_list, 0)
    label_batches = np.concatenate(label_batch_list, 0)

    train_data = data_batches[train_idxs, ...]
    train_label = label_batches[train_idxs]
    test_data = data_batches[test_idxs, ...]
    test_label = label_batches[test_idxs]

    return train_data, train_label, test_data, test_label
 def loadModelNet40(self):
     file_path_name = askopenfilenames()
     self.path_labelText.set(file_path_name[0].split('/')[-1])
     self.display_count = 0
     # ModelNet40 official train/test split
     TRAIN_FILES = provider.getDataFiles(file_path_name[0])
     # Shuffle train files
     train_file_idxs = np.arange(0, len(TRAIN_FILES))
     np.random.shuffle(train_file_idxs)
     for fn in range(len(TRAIN_FILES)):
         print("目前讀取的檔案是: %s" % (TRAIN_FILES[train_file_idxs[fn]], ))
         current_data, current_label = provider.loadDataFile(
             TRAIN_FILES[train_file_idxs[fn]])
         current_data, current_label, _ = provider.shuffle_data(
             current_data, np.squeeze(current_label))
         if fn == 0:
             point_cloud_collection = current_data
         else:
             point_cloud_collection = np.vstack(
                 (point_cloud_collection, current_data))
     self.POINT_CLOUD_SET_LIST = point_cloud_collection
     self.idxs = np.arange(0, len(self.POINT_CLOUD_SET_LIST))
     print(self.idxs)
     np.random.shuffle(self.idxs)
     # Load model 進來後停止使用此button
     self.btn_loadModelNet40.configure(state='disabled')
     self.btn_loadMyOwnData.configure(state='normal')
     self.Confirm_btn.configure(state='normal')
     self.Confirm_btn_own_data.configure(state='disabled')
예제 #10
0
def load_data(test_area=6):
    # 读取点云数据
    data_batch_list = []
    label_batch_list = []
    all_files = provider.getDataFiles(hyper_parameter.ALL_FILES_PATH)
    for h5_filename in all_files:
        file_path = provider.getPath(hyper_parameter.DATASET_PATH, h5_filename)
        data_batch, label_batch = provider.loadDataFile(file_path)
        data_batch_list.append(data_batch)
        label_batch_list.append(label_batch)
    data_batches = np.concatenate(data_batch_list, 0)
    label_batches = np.concatenate(label_batch_list, 0)

    # 划分训练集测试集
    room_filelist_path = provider.getPath(hyper_parameter.DATASET_PATH,
                                          hyper_parameter.ROOM_FILELIST_PATH)
    room_filelist = provider.getDataFiles(room_filelist_path)
    test_area = 'Area_' + str(test_area)
    train_idxs = []
    test_idxs = []
    for i_, room_name in enumerate(room_filelist):
        if test_area in room_name:
            test_idxs.append(i_)
        else:
            train_idxs.append(i_)

    # 利用列表索引数组
    train_data_ = data_batches[train_idxs, ...]
    train_label_ = label_batches[train_idxs]
    test_data_ = data_batches[test_idxs, ...]
    test_label_ = label_batches[test_idxs]
    train_tiles = train_data_.shape[0]
    test_tiles = test_data_.shape[0]
    train_points = train_data_.shape[0] * train_data_.shape[1]
    test_points = test_data_.shape[0] * test_data_.shape[1]
    print("*-*-*-*-*-*-*-*-Dataset-*-*-*-*-*-*-*-*-*")
    print("*-* Train block:", train_tiles, "Points:", train_points)
    print("*-* Test  block:", test_tiles, " Points:", test_points)
    print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")

    train_data_ = np.expand_dims(train_data_, -1)
    test_data_ = np.expand_dims(test_data_, -1)
    train_label_ = train_label_.astype(np.int32)
    test_label_ = test_label_.astype(np.int32)
    return train_data_.astype(np.float32), train_label_, test_data_.astype(
        np.float32), test_label_
예제 #11
0
def init(f):
    global LOG_FOUT
    global DUMP_FILES
    global TEST_FILES

    # MAKE log directory
    if not os.path.exists(f['visual_dir']):
        os.mkdir(f['visual_dir'])
    # CREATE log file
    LOG_FOUT = open(os.path.join(f['visual_dir'], 'log_visualization.txt'),
                    'w')
    LOG_FOUT.write(dict2str(f))

    DUMP_FILES = provider.getDataFiles( \
        os.path.join(BASE_DIR, os.path.join(f['dump_dir'], 'list_evaluate.txt')))
    TEST_FILES = provider.getDataFiles( \
        os.path.join(BASE_DIR, os.path.join(f['dataset_path'], 'test_files.txt')))
예제 #12
0
def main(config):
    symbol, arg_params, aux_params = mx.model.load_checkpoint(
        './model/' + config.model_load_prefix, config.model_load_epoch)

    model = mx.model.FeedForward(symbol,
                                 mx.gpu(0),
                                 arg_params=arg_params,
                                 aux_params=aux_params)
    kv = mx.kvstore.create(config.kv_store)
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    # ModelNet40 official train/test split
    TRAIN_FILES = provider.getDataFiles( \
        os.path.join(BASE_DIR, config.train_files))
    TEST_FILES = provider.getDataFiles( \
        os.path.join(BASE_DIR, config.val_files))
    _, val, _ = dummy_iterator(config.batch_size, config.num_points,
                               TRAIN_FILES, TEST_FILES)
    print model.score(val)
예제 #13
0
def make_ModelNet_data_A_B(err_num, batch_size):
    ModelNet_TRAIN_FILES = provider.getDataFiles(
        '../data/modelnet40_ply_hdf5_2048/train_files.txt')

    class_data = {l: [] for l in range(40)}
    for data_set in ModelNet_TRAIN_FILES:
        file = h5py.File('..//' + data_set, 'r')
        data = file['data'][...]
        label = np.reshape(file['label'][...], -1)
        normal = file['normal'][...]
        for i in range(data.shape[0]):
            class_data[label[i]].append(data[i])
        file.close()

    for i in range(40):
        class_data[i] = np.array(class_data[i])

    j = 0
    for data_set in ModelNet_TRAIN_FILES:
        print('making ModelNet_A_B_' + str(j) + '.h5')
        file = h5py.File('..//' + data_set, 'r')
        data_A = file['data'][...]
        label_A = file['label'][...]

        data_A, label_A, _ = provider.shuffle_data(data_A, label_A)
        ERR = err_num * int(data_A.shape[0] / 9843)
        data_B, label_B = provider.get_data_with_err_ModelNet(
            label_A, class_data, err_num)
        label_B = np.reshape(label_B, [-1, 1])
        data_A_B = np.zeros([2 * data_A.shape[0], 2048, 3])
        label_A_B = np.zeros([2 * data_A.shape[0], 1])

        for i in range(int(data_A.shape[0] / (batch_size / 2))):
            data_A_B[int((i + 0) *
                         batch_size):int((i + 0.5) * batch_size)] = data_A[int(
                             (i + 0) * (batch_size / 2)):int((i + 1) *
                                                             (batch_size / 2))]
            data_A_B[int((i + 0.5) *
                         batch_size):int((i + 1) * batch_size)] = data_B[int(
                             (i + 0) * (batch_size / 2)):int((i + 1) *
                                                             (batch_size / 2))]
            label_A_B[int((i + 0) * batch_size):int(
                (i + 0.5) * batch_size)] = label_A[int(
                    (i + 0) * (batch_size / 2)):int((i + 1) *
                                                    (batch_size / 2))]
            label_A_B[int((i + 0.5) *
                          batch_size):int((i + 1) * batch_size)] = label_B[int(
                              (i + 0) *
                              (batch_size / 2)):int((i + 1) *
                                                    (batch_size / 2))]

        data_set_A_B = h5py.File('data/ModelNet_A_B_' + str(j) + '.h5', 'w')
        data_set_A_B['data'] = data_A_B
        data_set_A_B['label'] = label_A_B
        data_set_A_B.close()
        file.close()
        j += 1
예제 #14
0
def load_data(
    train_files,
    test_files,
    num_points=1024,
    shuffle=False,
    rotate=False,
    rotate_val=False,
):

    train_files = provider.getDataFiles(train_files)
    test_files = provider.getDataFiles(test_files)

    x_train, y_train = load(
        train_files, points=num_points, shuffle=shuffle, rotate=rotate
    )
    x_test, y_test = load(
        test_files, points=num_points, shuffle=shuffle, rotate=rotate_val
    )

    return (x_train, y_train), (x_test, y_test)
예제 #15
0
def load_config(filename):
    global globalConfig
    assert filename.endswith('.json')
    name = os.path.basename(filename)[:-5]
    with open(filename, 'r') as handle:
        dump_to_namespace(configGlobal, json.load(handle))
    configGlobal.__dict__["name"] = name
    configGlobal.data.__dict__["basename"] = os.path.basename(
        configGlobal.data.basepath)
    configGlobal.logging.__dict__[
        "logdir"] = configGlobal.logging.basedir + f'/{name}'
    if configGlobal.evaluation.has('special'):
        if configGlobal.evaluation.special.mode == 'icp':
            configGlobal.logging.__dict__[
                "logdir"] = configGlobal.logging.basedir + f'/icp_{configGlobal.data.basename}/{name}'

    TRAIN_INDICES = provider.getDataFiles(
        f'{configGlobal.data.basepath}/split/train.txt')
    VAL_INDICES = provider.getDataFiles(
        f'{configGlobal.data.basepath}/split/val.txt')
    configGlobal.data.__dict__["ntrain"] = len(TRAIN_INDICES)
    configGlobal.data.__dict__["nval"] = len(VAL_INDICES)
예제 #16
0
def estimate_mean_thresholds():
    with tf.Graph().as_default():
        with tf.device('/gpu:' + str(GPU_INDEX)):
            pointclouds_pl, labels_pl, sem_labels_pl = placeholder_inputs(BATCH_SIZE, NUM_POINT)
            is_training_pl = tf.placeholder(tf.bool, shape=())
            # Get model
            pred_sem, pred_ins = get_model(pointclouds_pl, is_training_pl, NUM_CLASSES)

            loader = tf.train.Saver()

        # Create a session
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        config.allow_soft_placement = True
        config.log_device_placement = True
        sess = tf.Session(config=config)

        is_training = False

        # Restore all the variables
        loader.restore(sess, MODEL_PATH)
        print("Model restored")
        train_file_list = provider.getDataFiles(ESTIMATE_FILE_PATH)

        ths = np.zeros(NUM_CLASSES)
        cnt = np.zeros(NUM_CLASSES)
        ths_ = np.zeros(NUM_CLASSES)

        for shape_idx in range(len(train_file_list)):
            h5_filename = train_file_list[shape_idx]
            print('Loading file ' + h5_filename)
            cur_data, cur_group, _, cur_sem = provider.loadDataFile_with_groupseglabel_stanfordindoor(h5_filename)
            num_data = cur_data.shape[0]
            for j in range(num_data):
                print("Processing: Shape [%d] Block[%d]" % (shape_idx, j))
                pts = cur_data[j, ...]
                seg = cur_sem[j, ...]
                ins = cur_group[j, ...]
                feed_dict = {
                    pointclouds_pl: np.expand_dims(pts, 0),
                    is_training_pl: is_training
                }
                pred_ins_val = sess.run([pred_ins], feed_dict=feed_dict)
                pred_ins_val = np.squeeze(pred_ins_val, axis=0)
                dis_mat = np.expand_dims(pred_ins_val, axis=1) - np.expand_dims(pred_ins_val, axis=0)
                dis_mat = np.linalg.norm(dis_mat, ord=1, axis=2)
                ths, ths_, cnt = Get_Ths(dis_mat, seg, ins, ths, ths_, cnt)
                print(cnt)
        ths = [ths[i] / cnt[i] if cnt[i] != 0 else 0.2 for i in range(len(cnt))]
        print(ths)
        np.savetxt(os.path.join(LOG_DIR, 'mean_thresholds.txt'), ths)
예제 #17
0
def Load_Stanford_Sampled_Hdf5(test_area,
                               channel_elementes=['xyz_1norm'],
                               max_test_fn=None):
    CHANNEL_INDEXS, NUM_CHANNELS = get_channel_indexs(channel_elementes)

    ALL_FILES = provider.getDataFiles(DefaultPointFileList)
    room_filelist = [line.rstrip() for line in open(DefaultROOMFileList, 'r')]

    data_batch_list = []
    label_batch_list = []
    for i, h5_filename in enumerate(ALL_FILES):
        data_batch, label_batch = provider.loadDataFile(h5_filename)
        data_batch = data_batch[..., CHANNEL_INDEXS]
        data_batch_list.append(data_batch)
        label_batch_list.append(label_batch)

    data_batches = np.concatenate(data_batch_list, 0)
    label_batches = np.concatenate(label_batch_list, 0)

    if test_area > 0:
        test_area_str = 'Area_' + str(test_area)
    else:
        test_area_str = 'Area_'

    train_idxs = []
    test_idxs = []
    for i, room_name in enumerate(room_filelist):
        if test_area_str in room_name:
            test_idxs.append(i)
        else:
            train_idxs.append(i)

    if max_test_fn != None and len(test_idxs) > max_test_fn:
        if len(test_idxs) > max_test_fn:
            test_idxs = test_idxs[0:max_test_fn]
        if len(train_idxs) > max_test_fn * 5:
            train_idxs = train_idxs[0:max_test_fn * 5]
        print('\n!!! in  small data scale: only read %d test files\n' %
              (max_test_fn))

    train_data = data_batches[train_idxs, ...]
    train_label = label_batches[train_idxs]
    test_data = data_batches[test_idxs, ...]
    test_label = label_batches[test_idxs]
    return train_data, train_label, test_data, test_label
예제 #18
0
def visualize_fv_pc_clas():
    num_points = 1024
    n_classes = 40
    clas = 'person'
    #Create new gaussian
    subdev = 5
    variance = 0.04
    export = False
    display = True
    exp_path = '/home/itzikbs/PycharmProjects/fisherpointnet/paper_images/'

    shape_names = provider.getDataFiles( \
        os.path.join(BASE_DIR, 'data/modelnet' + str(n_classes) + '_ply_hdf5_2048/shape_names.txt'))
    shape_dict = {shape_names[i]: i for i in range(len(shape_names))}

    gmm = ult_functions.get_grid_gmm(subdivisions=[subdev, subdev, subdev], variance=variance)
    # compute fv
    w = tf.constant(gmm.weights_, dtype=tf.float32)
    mu = tf.constant(gmm.means_, dtype=tf.float32)
    sigma = tf.constant(gmm.covariances_, dtype=tf.float32)

    for clas in shape_dict:
        points = provider.load_single_model_class(clas=clas, ind=0, test_train='train', file_idxs=0, num_points=1024,
                                                  n_classes=n_classes)
        points = np.expand_dims(points,0)

        points_tensor = tf.constant(points, dtype=tf.float32)  # convert points into a tensor
        fv_tensor = tf_util.get_fv_minmax(points_tensor, w, mu, sigma, flatten=False)

        sess = tf_util.get_session(2)
        with sess:
            fv = fv_tensor.eval()
        #
        # visualize_single_fv_with_pc(fv_train, points, label_title=clas,
        #                      fig_title='fv_pc', type='paper', pos=[750, 800, 0, 0], export=export,
        #                      filename=BASE_DIR + '/paper_images/fv_pc_' + clas)

        visualize_fv(fv, gmm, label_title=[clas], max_n_images=5, normalization=True, export=export, display=display,
                     filename=exp_path + clas+'_fv', n_scales=1, type='none', fig_title='Figure')
        visualize_pc(points, label_title=clas, fig_title='figure', export=export, filename=exp_path +clas+'_pc')
        plt.close('all')
예제 #19
0
def init(f):
    global LOG_FOUT
    global MODEL
    global MODEL_CONF
    global TEST_FILES
    global FLIST_FOUT

    # IMPORT network module
    sys.path.append(f['model_path'])
    module = importlib.import_module(f['model'])
    MODEL = getattr(module, f['model'])
    MODEL_CONF = getattr(module, f['model'] + '_conf')

    # MAKE log directory
    if not os.path.exists(f['dump_dir']):
        os.mkdir(f['dump_dir'])
    # CREATE log file
    LOG_FOUT = open(os.path.join(f['dump_dir'], 'log_evaluate.txt'), 'w')
    LOG_FOUT.write(dict2str(f))
    FLIST_FOUT = open(os.path.join(f['dump_dir'], 'list_evaluate.txt'), 'w')

    TEST_FILES = provider.getDataFiles( \
        os.path.join(BASE_DIR, os.path.join(f['dataset_path'], 'test_files.txt')))
예제 #20
0
MODEL_PATH = FLAGS.model_path
GPU_INDEX = FLAGS.gpu
MODEL = importlib.import_module(FLAGS.model) # import network module
DUMP_DIR = FLAGS.dump_dir
if not os.path.exists(DUMP_DIR): os.mkdir(DUMP_DIR)
LOG_FOUT = open(os.path.join(DUMP_DIR, 'log_evaluate.txt'), 'w')
LOG_FOUT.write(str(FLAGS)+'\n')

NUM_CLASSES = 40
SHAPE_NAMES = [line.rstrip() for line in \
    open(os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/shape_names.txt'))] 

HOSTNAME = socket.gethostname()

# ModelNet40 official train/test split
TRAIN_FILES = provider.getDataFiles( \
    os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/train_files.txt'))
TEST_FILES = provider.getDataFiles(\
    os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/test_files.txt'))

def log_string(out_str):
    LOG_FOUT.write(out_str+'\n')
    LOG_FOUT.flush()
    print(out_str)

def evaluate(num_votes):
    is_training = False
     
    with tf.device('/gpu:'+str(GPU_INDEX)):
        pointclouds_pl, labels_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT)
        is_training_pl = tf.placeholder(tf.bool, shape=())
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs, leave=False)

    def __iter__(self):
        return super().__iter__()

    def __del__(self):
        self.ascii = True
        self.dynamic_ncols = False
        self.ncols = 100
        logger.info(self.__repr__())
        return super().__del__()


TRAIN_INDICES = provider.getDataFiles(f'{cfg.data.basepath}/split/train.txt')
VAL_INDICES = provider.getDataFiles(f'{cfg.data.basepath}/split/val.txt')


def get_learning_rate(batch):
    num_batches_per_epoch = len(TRAIN_INDICES) // cfg.training.batch_size

    if cfg.training.lr_extension.mode == 'decay':
        lr_decay_step = cfg.training.lr_extension.step
        if cfg.training.lr_extension.per == 'step':
            pass
        elif cfg.training.lr_extension.per == 'epoch':
            lr_decay_step *= cfg.training.batch_size * num_batches_per_epoch
        else:
            assert False
예제 #22
0
os.system('cp train.py %s' % (LOG_DIR)) # bkp of train procedure
LOG_FOUT = open(os.path.join(LOG_DIR, 'log_train.txt'), 'w')
LOG_FOUT.write(str(FLAGS)+'\n')

MAX_NUM_POINT = 4096
NUM_CLASSES = 12

BN_INIT_DECAY = 0.5
BN_DECAY_DECAY_RATE = 0.5
#BN_DECAY_DECAY_STEP = float(DECAY_STEP * 2)
BN_DECAY_DECAY_STEP = float(DECAY_STEP)
BN_DECAY_CLIP = 0.99

HOSTNAME = socket.gethostname()

ALL_FILES = provider.getDataFiles(os.path.join(ROOT_DIR,'data/indoor3d_sem_seg_hdf5_data/all_files.txt'))
room_filelist = [line.rstrip() for line in open(os.path.join(ROOT_DIR,'data/indoor3d_sem_seg_hdf5_data/room_filelist.txt'))]

# Create a session
#config = tf.ConfigProto()
#config.gpu_options.allow_growth = True
#config.allow_soft_placement = True
#config.log_device_placement = True
#sess = tf.Session(config=config)

# Restore variables from disk.
#if FLAGS.checkpoint_dir is not None:
# restoring from the checkpoint file
    #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    #tf.train.Saver().restore(sess, ckpt.model_checkpoint_path, latest_filename= 'model.ckpt.index')
def train():
    with tf.Graph().as_default(), tf.device('/cpu:0'):

        batch = tf.Variable(0, trainable=False)
        
        learning_rate = tf.train.exponential_decay(
                        BASE_LEARNING_RATE,     # base learning rate
                        batch * batch_size,     # global_var indicating the number of steps
                        DECAY_STEP,             # step size
                        DECAY_RATE,             # decay rate
                        staircase=True          # Stair-case or continuous decreasing
                        )
        learning_rate = tf.maximum(learning_rate, LEARNING_RATE_CLIP)
    
        bn_momentum = tf.train.exponential_decay(
                    BN_INIT_DECAY,
                    batch*batch_size,
                    BN_DECAY_DECAY_STEP,
                    BN_DECAY_DECAY_RATE,
                    staircase=True)
        bn_decay = tf.minimum(BN_DECAY_CLIP, 1 - bn_momentum)

        lr_op = tf.summary.scalar('learning_rate', learning_rate)
        batch_op = tf.summary.scalar('batch_number', batch)
        bn_decay_op = tf.summary.scalar('bn_decay', bn_decay)

        trainer = tf.train.AdamOptimizer(learning_rate)

        # store tensors for different gpus
        tower_grads = []
        pointclouds_phs = []
        input_label_phs = []
        seg_phs =[]
        is_training_phs =[]

        with tf.variable_scope(tf.get_variable_scope()):
            for i in range(FLAGS.num_gpu):
                with tf.device('/gpu:%d' % i):
                    with tf.name_scope('%s_%d' % (TOWER_NAME, i)) as scope:
                        pointclouds_phs.append(tf.placeholder(tf.float32, shape=(batch_size, point_num, 3))) # for points
                        input_label_phs.append(tf.placeholder(tf.float32, shape=(batch_size, NUM_CATEGORIES))) # for one-hot category label
                        seg_phs.append(tf.placeholder(tf.int32, shape=(batch_size, point_num))) # for part labels
                        is_training_phs.append(tf.placeholder(tf.bool, shape=()))

                        seg_pred = model.get_model(pointclouds_phs[-1], input_label_phs[-1], \
                                is_training=is_training_phs[-1], bn_decay=bn_decay, cat_num=NUM_CATEGORIES, \
                                part_num=NUM_PART_CATS, batch_size=batch_size, num_point=point_num, graphnum=graphnum, featnum = featnum, weight_decay=FLAGS.wd)


                        loss, per_instance_seg_loss, per_instance_seg_pred_res  \
                            = model.get_loss(seg_pred, seg_phs[-1])

                        total_training_loss_ph = tf.placeholder(tf.float32, shape=())
                        total_testing_loss_ph = tf.placeholder(tf.float32, shape=())

                        seg_training_acc_ph = tf.placeholder(tf.float32, shape=())
                        seg_testing_acc_ph = tf.placeholder(tf.float32, shape=())
                        seg_testing_acc_avg_cat_ph = tf.placeholder(tf.float32, shape=())

                        total_train_loss_sum_op = tf.summary.scalar('total_training_loss', total_training_loss_ph)
                        total_test_loss_sum_op = tf.summary.scalar('total_testing_loss', total_testing_loss_ph)

                
                        seg_train_acc_sum_op = tf.summary.scalar('seg_training_acc', seg_training_acc_ph)
                        seg_test_acc_sum_op = tf.summary.scalar('seg_testing_acc', seg_testing_acc_ph)
                        seg_test_acc_avg_cat_op = tf.summary.scalar('seg_testing_acc_avg_cat', seg_testing_acc_avg_cat_ph)

                        tf.get_variable_scope().reuse_variables()

                        grads = trainer.compute_gradients(loss)

                        tower_grads.append(grads)

        grads = average_gradients(tower_grads)

        train_op = trainer.apply_gradients(grads, global_step=batch)

        saver = tf.train.Saver(tf.global_variables(), sharded=True, max_to_keep=20)

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        config.allow_soft_placement = True
        sess = tf.Session(config=config)
        
        # Resume model if possible
        if FLAGS.model_path != None:
            saver.restore(sess, FLAGS.model_path)
        else:
            init = tf.group(tf.global_variables_initializer(),
                             tf.local_variables_initializer())
            sess.run(init)

        train_writer = tf.summary.FileWriter(SUMMARIES_FOLDER + '/train', sess.graph)
        test_writer = tf.summary.FileWriter(SUMMARIES_FOLDER + '/test')

        train_file_list = provider.getDataFiles(TRAINING_FILE_LIST)
        num_train_file = len(train_file_list)
        test_file_list = provider.getDataFiles(TESTING_FILE_LIST)
        num_test_file = len(test_file_list)

        fcmd = open(os.path.join(LOG_STORAGE_PATH, 'cmd.txt'), 'w')
        fcmd.write(str(FLAGS))
        fcmd.close()

        # write logs to the disk
        flog = open(os.path.join(LOG_STORAGE_PATH, 'log.txt'), 'w')

        def train_one_epoch(train_file_idx, epoch_num):
            is_training = True

            for i in range(num_train_file):
                cur_train_filename = os.path.join(hdf5_data_dir, train_file_list[train_file_idx[i]])
                printout(flog, 'Loading train file ' + cur_train_filename)

                cur_data, cur_labels, cur_seg = provider.load_h5_data_label_seg(cur_train_filename)
                cur_data, cur_labels, order = provider.shuffle_data(cur_data, np.squeeze(cur_labels))
                cur_seg = cur_seg[order, ...]

                cur_labels_one_hot = convert_label_to_one_hot(cur_labels)

                num_data = len(cur_labels)
                num_batch = num_data // (FLAGS.num_gpu * batch_size) # For all working gpus

                total_loss = 0.0
                total_seg_acc = 0.0

                for j in range(num_batch):
                    begidx_0 = j * batch_size
                    endidx_0 = (j + 1) * batch_size

                    feed_dict = {
                            # For the first gpu
                            pointclouds_phs[0]: cur_data[begidx_0: endidx_0, ...], 
                            input_label_phs[0]: cur_labels_one_hot[begidx_0: endidx_0, ...], 
                            seg_phs[0]: cur_seg[begidx_0: endidx_0, ...],
                            is_training_phs[0]: is_training, 
                            }


                    # train_op is for both gpus, and the others are for gpu_1
                    _, loss_val, per_instance_seg_loss_val, seg_pred_val, pred_seg_res \
                            = sess.run([train_op, loss, per_instance_seg_loss, seg_pred, per_instance_seg_pred_res], \
                            feed_dict=feed_dict)

                    per_instance_part_acc = np.mean(pred_seg_res == cur_seg[begidx_0: endidx_0, ...], axis=1)
                    average_part_acc = np.mean(per_instance_part_acc)

                    total_loss += loss_val
                    total_seg_acc += average_part_acc

                total_loss = total_loss * 1.0 / num_batch
                total_seg_acc = total_seg_acc * 1.0 / num_batch

                lr_sum, bn_decay_sum, batch_sum, train_loss_sum, train_seg_acc_sum = sess.run(\
                        [lr_op, bn_decay_op, batch_op, total_train_loss_sum_op, seg_train_acc_sum_op], \
                        feed_dict={total_training_loss_ph: total_loss, seg_training_acc_ph: total_seg_acc})

                train_writer.add_summary(train_loss_sum, i + epoch_num * num_train_file)
                train_writer.add_summary(lr_sum, i + epoch_num * num_train_file)
                train_writer.add_summary(bn_decay_sum, i + epoch_num * num_train_file)
                train_writer.add_summary(train_seg_acc_sum, i + epoch_num * num_train_file)
                train_writer.add_summary(batch_sum, i + epoch_num * num_train_file)

                printout(flog, '\tTraining Total Mean_loss: %f' % total_loss)
                printout(flog, '\t\tTraining Seg Accuracy: %f' % total_seg_acc)

        def eval_one_epoch(epoch_num):
            is_training = False

            total_loss = 0.0
            total_seg_acc = 0.0
            total_seen = 0

            total_seg_acc_per_cat = np.zeros((NUM_CATEGORIES)).astype(np.float32)
            total_seen_per_cat = np.zeros((NUM_CATEGORIES)).astype(np.int32)

            for i in range(num_test_file):
                cur_test_filename = os.path.join(hdf5_data_dir, test_file_list[i])
                printout(flog, 'Loading test file ' + cur_test_filename)

                cur_data, cur_labels, cur_seg = provider.load_h5_data_label_seg(cur_test_filename)
                cur_labels = np.squeeze(cur_labels)

                cur_labels_one_hot = convert_label_to_one_hot(cur_labels)

                num_data = len(cur_labels)
                num_batch = num_data // batch_size

                # Run on gpu_1, since the tensors used for evaluation are defined on gpu_1
                for j in range(num_batch):
                    begidx = j * batch_size
                    endidx = (j + 1) * batch_size
                    feed_dict = {
                            pointclouds_phs[0]: cur_data[begidx: endidx, ...], 
                            input_label_phs[0]: cur_labels_one_hot[begidx: endidx, ...], 
                            seg_phs[0]: cur_seg[begidx: endidx, ...],
                            is_training_phs[0]: is_training}

                    loss_val, per_instance_seg_loss_val, seg_pred_val, pred_seg_res \
                            = sess.run([loss, per_instance_seg_loss, seg_pred, per_instance_seg_pred_res], \
                            feed_dict=feed_dict)

                    per_instance_part_acc = np.mean(pred_seg_res == cur_seg[begidx: endidx, ...], axis=1)
                    average_part_acc = np.mean(per_instance_part_acc)

                    total_seen += 1
                    total_loss += loss_val
                    
                    total_seg_acc += average_part_acc

                    for shape_idx in range(begidx, endidx):
                        total_seen_per_cat[cur_labels[shape_idx]] += 1
                        total_seg_acc_per_cat[cur_labels[shape_idx]] += per_instance_part_acc[shape_idx - begidx]

            total_loss = total_loss * 1.0 / total_seen
            total_seg_acc = total_seg_acc * 1.0 / total_seen

            test_loss_sum, test_seg_acc_sum = sess.run(\
                    [total_test_loss_sum_op, seg_test_acc_sum_op], \
                    feed_dict={total_testing_loss_ph: total_loss, \
                    seg_testing_acc_ph: total_seg_acc})

            test_writer.add_summary(test_loss_sum, (epoch_num+1) * num_train_file-1)
            test_writer.add_summary(test_seg_acc_sum, (epoch_num+1) * num_train_file-1)

            printout(flog, '\tTesting Total Mean_loss: %f' % total_loss)
            printout(flog, '\t\tTesting Seg Accuracy: %f' % total_seg_acc)

            for cat_idx in range(NUM_CATEGORIES):
                if total_seen_per_cat[cat_idx] > 0:
                    printout(flog, '\n\t\tCategory %s Object Number: %d' % (all_obj_cats[cat_idx][0], total_seen_per_cat[cat_idx]))
                    printout(flog, '\t\tCategory %s Seg Accuracy: %f' % (all_obj_cats[cat_idx][0], total_seg_acc_per_cat[cat_idx]/total_seen_per_cat[cat_idx]))

        if not os.path.exists(MODEL_STORAGE_PATH):
            os.mkdir(MODEL_STORAGE_PATH)

        for epoch in range(FLAGS.start_epoch, TRAINING_EPOCHES):
            printout(flog, '\n<<< Testing on the test dataset ...')
            eval_one_epoch(epoch)

            printout(flog, '\n>>> Training for the epoch %d/%d ...' % (epoch, TRAINING_EPOCHES))

            train_file_idx = np.arange(0, len(train_file_list))
            np.random.shuffle(train_file_idx)

            train_one_epoch(train_file_idx, epoch)

            if epoch % 10 == 0:
                cp_filename = saver.save(sess, os.path.join(MODEL_STORAGE_PATH, 'epoch_' + str(epoch)+'.ckpt'))
                printout(flog, 'Successfully store the checkpoint model into ' + cp_filename)

            flog.flush()

        flog.close()
예제 #24
0
os.system('cp  train.py %s' % (LOG_DIR)) # bkp of train procedure
LOG_FOUT = open(os.path.join(LOG_DIR, 'log_train.txt'), 'w')
LOG_FOUT.write(str(FLAGS)+'\n')

MAX_NUM_POINT = 2048
NUM_CLASSES = 5

BN_INIT_DECAY = 0.5
BN_DECAY_DECAY_RATE = 0.5
BN_DECAY_DECAY_STEP = float(DECAY_STEP)
BN_DECAY_CLIP = 0.99

HOSTNAME = socket.gethostname()

# ModelNet40 official train/test split
TRAIN_FILES = provider.getDataFiles( \
    os.path.join(BASE_DIR, 'data/challenge1/colors_only_train_files.txt'))
TEST_FILES = provider.getDataFiles(\
    os.path.join(BASE_DIR, 'data/challenge1/colors_only_test_files.txt'))

def log_string(out_str):
    LOG_FOUT.write(out_str+'\n')
    LOG_FOUT.flush()
    print(out_str)


def get_learning_rate(batch):
    learning_rate = tf.train.exponential_decay(
                        BASE_LEARNING_RATE,  # Base learning rate.
                        batch * BATCH_SIZE,  # Current index into the dataset.
                        DECAY_STEP,          # Decay step.
                        DECAY_RATE,          # Decay rate.
예제 #25
0
파일: train.py 프로젝트: martinimass/dgcnn
MAX_NUM_POINT = 4096

#NUM_CLASSES = 13
with open("meta/class_names.txt", "r") as fr:
    NUM_CLASSES = len(fr.readlines())
print("NUM_CLASSES:", NUM_CLASSES)

BN_INIT_DECAY = 0.5
BN_DECAY_DECAY_RATE = 0.5
BN_DECAY_DECAY_STEP = float(DECAY_STEP)
BN_DECAY_CLIP = 0.99

HOSTNAME = socket.gethostname()

ALL_FILES = provider.getDataFiles('indoor3d_sem_seg_hdf5_data/all_files.txt')
room_filelist = [
    line.rstrip()
    for line in open('indoor3d_sem_seg_hdf5_data/room_filelist.txt')
]
print("room_filelist:", len(room_filelist))

# Load ALL data
data_batch_list = []
label_batch_list = []
for h5_filename in ALL_FILES:
    data_batch, label_batch = provider.loadDataFile(h5_filename)
    data_batch_list.append(data_batch)
    label_batch_list.append(label_batch)
data_batches = np.concatenate(data_batch_list, 0)
label_batches = np.concatenate(label_batch_list, 0)
예제 #26
0
def train():
  with tf.Graph().as_default(), tf.device('/cpu:0'):

    batch = tf.Variable(0, trainable=False)
    
    learning_rate = tf.train.exponential_decay(
            BASE_LEARNING_RATE,     # base learning rate
            batch * batch_size,     # global_var indicating the number of steps
            DECAY_STEP,             # step size
            DECAY_RATE,             # decay rate
            staircase=True          # Stair-case or continuous decreasing
            )
    learning_rate = tf.maximum(learning_rate, LEARNING_RATE_CLIP)
  
    bn_momentum = tf.train.exponential_decay(
          BN_INIT_DECAY,
          batch*batch_size,
          BN_DECAY_DECAY_STEP,
          BN_DECAY_DECAY_RATE,
          staircase=True)
    bn_decay = tf.minimum(BN_DECAY_CLIP, 1 - bn_momentum)

    lr_op = tf.summary.scalar('learning_rate', learning_rate)
    batch_op = tf.summary.scalar('batch_number', batch)
    bn_decay_op = tf.summary.scalar('bn_decay', bn_decay)

    trainer = tf.train.AdamOptimizer(learning_rate)

    # store tensors for different gpus
    tower_grads = []
    pointclouds_phs = []
    input_label_phs = []
    seg_phs =[]
    is_training_phs =[]

    # 变量
    with tf.variable_scope(tf.get_variable_scope()):
      for i in range(FLAGS.num_gpu):
        with tf.device('/gpu:%d' % i):
          with tf.name_scope('%s_%d' % (TOWER_NAME, i)) as scope:
            pointclouds_phs.append(tf.placeholder(tf.float32, shape=(batch_size, point_num, 3))) # for points  4*2048*3
            input_label_phs.append(tf.placeholder(tf.float32, shape=(batch_size, NUM_CATEGORIES))) # for one-hot category label  # 4*16
            seg_phs.append(tf.placeholder(tf.int32, shape=(batch_size, point_num))) # for part labels     # labels  4*2048
            is_training_phs.append(tf.placeholder(tf.bool, shape=()))

            # 预测值
            # seg_pred : B*N*part_num=4*2048*50
            seg_pred = model.get_model(pointclouds_phs[-1], input_label_phs[-1], \
                is_training=is_training_phs[-1], bn_decay=bn_decay, cat_num=NUM_CATEGORIES, \
                part_num=NUM_PART_CATS, batch_size=batch_size, num_point=point_num, weight_decay=FLAGS.wd)      # cat_num=16,part_num=50

            # loss:这个batch的总的loss
            # per_instance_loss:每个点云的loss,shape=4
            # per_instance_seg_pred_res : 每个点云的分割结果,由seg_pred取max得到的,shape=4*2048
            loss, per_instance_seg_loss, per_instance_seg_pred_res  \
              = model.get_loss(seg_pred, seg_phs[-1])

            # placeholder
            total_training_loss_ph = tf.placeholder(tf.float32, shape=())
            total_testing_loss_ph = tf.placeholder(tf.float32, shape=())

            seg_training_acc_ph = tf.placeholder(tf.float32, shape=())
            seg_testing_acc_ph = tf.placeholder(tf.float32, shape=())
            seg_testing_acc_avg_cat_ph = tf.placeholder(tf.float32, shape=())

            # scalar
            total_train_loss_sum_op = tf.summary.scalar('total_training_loss', total_training_loss_ph)
            total_test_loss_sum_op = tf.summary.scalar('total_testing_loss', total_testing_loss_ph)

            seg_train_acc_sum_op = tf.summary.scalar('seg_training_acc', seg_training_acc_ph)
            seg_test_acc_sum_op = tf.summary.scalar('seg_testing_acc', seg_testing_acc_ph)
            seg_test_acc_avg_cat_op = tf.summary.scalar('seg_testing_acc_avg_cat', seg_testing_acc_avg_cat_ph)

            tf.get_variable_scope().reuse_variables()

            grads = trainer.compute_gradients(loss)

            tower_grads.append(grads)

    grads = average_gradients(tower_grads)   # 计算平均梯度

    train_op = trainer.apply_gradients(grads, global_step=batch)

    saver = tf.train.Saver(tf.global_variables(), sharded=True, max_to_keep=20)     # 保存变量

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True
    sess = tf.Session(config=config)
    
    init = tf.group(tf.global_variables_initializer(),
             tf.local_variables_initializer())
    sess.run(init)

    train_writer = tf.summary.FileWriter(SUMMARIES_FOLDER + '/train', sess.graph)  # 保存graph
    test_writer = tf.summary.FileWriter(SUMMARIES_FOLDER + '/test')

    train_file_list = provider.getDataFiles(TRAINING_FILE_LIST)    # [train0.h5,train1.h5,...train5.h5]
    num_train_file = len(train_file_list)               # 6
    test_file_list = provider.getDataFiles(TESTING_FILE_LIST)     # [ply_data_val0.h5]
    num_test_file = len(test_file_list)                     # 1

    # 保存终端输入
    fcmd = open(os.path.join(LOG_STORAGE_PATH, 'cmd.txt'), 'w')
    fcmd.write(str(FLAGS))
    fcmd.close()

    # write logs to the disk
    flog = open(os.path.join(LOG_STORAGE_PATH, 'log.txt'), 'w')

    def train_one_epoch(train_file_idx, epoch_num):     # 输入为打乱后的顺序
      is_training = True

      # 遍历每一个train文件
      for i in range(num_train_file):
        cur_train_filename = os.path.join(hdf5_data_dir, train_file_list[train_file_idx[i]])  #  获取当前的train的点云文件
        printout(flog, 'Loading train file ' + cur_train_filename)

        cur_data, cur_labels, cur_seg = provider.load_h5_data_label_seg(cur_train_filename)
        cur_data, cur_labels, order = provider.shuffle_data(cur_data, np.squeeze(cur_labels))
        cur_seg = cur_seg[order, ...]

        cur_labels_one_hot = convert_label_to_one_hot(cur_labels)

        num_data = len(cur_labels)
        num_batch = num_data // (FLAGS.num_gpu * batch_size) # For all working gpus  num——batch代表这个train文件分几个batch

        total_loss = 0.0
        total_seg_acc = 0.0

        # 对每一个batch
        for j in range(num_batch):
          begidx_0 = j * batch_size       # 第一个gpu
          endidx_0 = (j + 1) * batch_size
          begidx_1 = (j + 1) * batch_size   # 第二个gpu
          endidx_1 = (j + 2) * batch_size

          feed_dict = {
              # For the first gpu
              pointclouds_phs[0]: cur_data[begidx_0: endidx_0, ...],      # 4*2048*3
              input_label_phs[0]: cur_labels_one_hot[begidx_0: endidx_0, ...],    # 4*16
              seg_phs[0]: cur_seg[begidx_0: endidx_0, ...],     # 4*2048  ,每一个数都在0-49之间
              is_training_phs[0]: is_training, 
              # # For the second gpu
              # pointclouds_phs[1]: cur_data[begidx_1: endidx_1, ...],
              # input_label_phs[1]: cur_labels_one_hot[begidx_1: endidx_1, ...],
              # seg_phs[1]: cur_seg[begidx_1: endidx_1, ...],
              # is_training_phs[1]: is_training,
              }


          # train_op is for both gpus, and the others are for gpu_1
          # 每一个batch的平均损失、每一个点云的损失、分割预测值
          _, loss_val, per_instance_seg_loss_val, seg_pred_val, pred_seg_res \
              = sess.run([train_op, loss, per_instance_seg_loss, seg_pred, per_instance_seg_pred_res], \
              feed_dict=feed_dict)

          # per_instance_part_acc = np.mean(pred_seg_res == cur_seg[begidx_1: endidx_1, ...], axis=1)
          per_instance_part_acc = np.mean(pred_seg_res == cur_seg[begidx_0: endidx_0, ...], axis=1)     # 每一个点云的分割精度

          average_part_acc = np.mean(per_instance_part_acc)        # 当前batch的平均精度

          total_loss += loss_val
          total_seg_acc += average_part_acc
          # 至此,一个train文件遍历完成

        total_loss = total_loss * 1.0 / num_batch     # 每一个train文件都得到一个loss和seg_acc
        total_seg_acc = total_seg_acc * 1.0 / num_batch

        # 绘制图
        lr_sum, bn_decay_sum, batch_sum, train_loss_sum, train_seg_acc_sum = sess.run(\
            [lr_op, bn_decay_op, batch_op, total_train_loss_sum_op, seg_train_acc_sum_op], \
            feed_dict={total_training_loss_ph: total_loss, seg_training_acc_ph: total_seg_acc})

        train_writer.add_summary(train_loss_sum, i + epoch_num * num_train_file)   # epoch_num是一个不断变化的值
        train_writer.add_summary(lr_sum, i + epoch_num * num_train_file)
        train_writer.add_summary(bn_decay_sum, i + epoch_num * num_train_file)
        train_writer.add_summary(train_seg_acc_sum, i + epoch_num * num_train_file)
        train_writer.add_summary(batch_sum, i + epoch_num * num_train_file)

        printout(flog, '\tTanin_file: {},Training Total Mean_loss: {}'.format(i,total_loss))    # 每一个train文件的loss和acc
        printout(flog, '\t\tTanin_file: {} ,Training Seg Accuracy: {}'.format(i,total_seg_acc))

    def eval_one_epoch(epoch_num):
      is_training = False

      total_loss = 0.0
      total_seg_acc = 0.0
      total_seen = 0

      total_seg_acc_per_cat = np.zeros((NUM_CATEGORIES)).astype(np.float32)  # [0. 0. ....0.]  16个0
      total_seen_per_cat = np.zeros((NUM_CATEGORIES)).astype(np.int32)  # [0 0 0 0 ...0]

      for i in range(num_test_file):   # i= 0  num_test_file=1
        cur_test_filename = os.path.join(hdf5_data_dir, test_file_list[i])    # ply_data_val0.h5
        printout(flog, 'Loading test file ' + cur_test_filename)  # ply_data_val0.h5

        # 读取数据
        # data = f['data'][:] # (1870, 2048, 3)
        # label = f['label'][:] # (1870,1)  1870个点云的类别0-15
        # seg = f['pid'][:] # (1870, 2048)  ,表示每一个点属于的类别0-49,一共50类点
        cur_data, cur_labels, cur_seg = provider.load_h5_data_label_seg(cur_test_filename)
        cur_labels = np.squeeze(cur_labels)         # shape:(1870,)

        cur_labels_one_hot = convert_label_to_one_hot(cur_labels)   # 1870*16

        num_data = len(cur_labels)      # 1870个点云
        num_batch = num_data // batch_size    # 467个batch

        # Run on gpu_1, since the tensors used for evaluation are defined on gpu_1
        for j in range(num_batch):      # 0-466
          begidx = j * batch_size
          endidx = (j + 1) * batch_size
          # feed_dict = {
          #     pointclouds_phs[1]: cur_data[begidx: endidx, ...],
          #     input_label_phs[1]: cur_labels_one_hot[begidx: endidx, ...],
          #     seg_phs[1]: cur_seg[begidx: endidx, ...],
          #     is_training_phs[1]: is_training}

          feed_dict = {
            pointclouds_phs[0]: cur_data[begidx: endidx, ...],      # 4*2048*3
            input_label_phs[0]: cur_labels_one_hot[begidx: endidx, ...],    # 4*16
            seg_phs[0]: cur_seg[begidx: endidx, ...],                # 4*2048
            is_training_phs[0]: is_training}

          # pred_seg_res:每个点的分割结果 shape:4*2048
          # seg_pred_val:分割结果,概率编码 shape:4*2048*50
          # per_instance_seg_loss_val: 每一个点云的损失 shape:(4,)
          # loss_val:当前batch的平均损失 shape:()
          loss_val, per_instance_seg_loss_val, seg_pred_val, pred_seg_res \
              = sess.run([loss, per_instance_seg_loss, seg_pred, per_instance_seg_pred_res], \
              feed_dict=feed_dict)

          per_instance_part_acc = np.mean(pred_seg_res == cur_seg[begidx: endidx, ...], axis=1)  # 4个点云的每一个的分割精度  shape:(4,)
          average_part_acc = np.mean(per_instance_part_acc)     # 4个点云的平均分割精度

          total_seen += 1   # 以batch为单位
          total_loss += loss_val
          
          total_seg_acc += average_part_acc     # average_part_acc:当前batch的分割精度

          for shape_idx in range(begidx, endidx):   # 对当前batch中的每一个点云
            total_seen_per_cat[cur_labels[shape_idx]] += 1      # 统计看过的每一类点云的数量
            total_seg_acc_per_cat[cur_labels[shape_idx]] += per_instance_part_acc[shape_idx - begidx]   # 统计每一类点云的精度

      total_loss = total_loss * 1.0 / total_seen            # 总的分割损失
      total_seg_acc = total_seg_acc * 1.0 / total_seen      # 总的平均分割精度,以batch为单位

      # 绘制图
      test_loss_sum, test_seg_acc_sum = sess.run(\
          [total_test_loss_sum_op, seg_test_acc_sum_op], \
          feed_dict={total_testing_loss_ph: total_loss, \
          seg_testing_acc_ph: total_seg_acc})

      test_writer.add_summary(test_loss_sum, (epoch_num+1) * num_train_file-1)
      test_writer.add_summary(test_seg_acc_sum, (epoch_num+1) * num_train_file-1)


      printout(flog, '\t\tTesting Total Mean_loss: %f' % total_loss)
      printout(flog, '\t\tTesting Seg Accuracy: %f' % total_seg_acc)

      for cat_idx in range(NUM_CATEGORIES):     # 0-15
        if total_seen_per_cat[cat_idx] > 0:  # 如果看过这类物体,就打印看得数量
          printout(flog, '\n\t\tCategory %s Object Number: %d' % (all_obj_cats[cat_idx][0], total_seen_per_cat[cat_idx]))
          printout(flog, '\t\tCategory %s Seg Accuracy: %f' % (all_obj_cats[cat_idx][0], total_seg_acc_per_cat[cat_idx]/total_seen_per_cat[cat_idx]))

    if not os.path.exists(MODEL_STORAGE_PATH):
      os.mkdir(MODEL_STORAGE_PATH)

    for epoch in range(TRAINING_EPOCHES):
      printout(flog, '\n<<< Testing on the test dataset ...')
      eval_one_epoch(epoch)     # 评估

      printout(flog, '\n>>> Training for the epoch %d/%d ...' % (epoch, TRAINING_EPOCHES))

      train_file_idx = np.arange(0, len(train_file_list))
      np.random.shuffle(train_file_idx)

      train_one_epoch(train_file_idx, epoch)      # 训练

      if epoch % 5 == 0:
        cp_filename = saver.save(sess, os.path.join(MODEL_STORAGE_PATH, 'epoch_' + str(epoch)+'.ckpt'))
        printout(flog, 'Successfully store the checkpoint model into ' + cp_filename)

      flog.flush()

    flog.close()
예제 #27
0
import pickle

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, 'models'))
sys.path.append(os.path.join(BASE_DIR, 'ult_functions'))
from ult_functions import utils as utils
from ult_functions import visualization as visualization
from ult_functions import tf_util as tf_util
import provider


# ModelNet40 official train/test split. MOdelNet10 requires separate downloading and sampling.
MAX_N_POINTS = 512
NUM_CLASSES = 7
TRAIN_FILES = provider.getDataFiles(
    os.path.join(BASE_DIR, 'data/test_data_hdf5_512' + '/train_files.txt'))
TEST_FILES = provider.getDataFiles(
    os.path.join(BASE_DIR,  'data/test_data_hdf5_512' + '/test_files.txt'))
LABEL_MAP = provider.getDataFiles(
    os.path.join(BASE_DIR,  'data/test_data_hdf5_512' + '/shape_names.txt'))



# ModelNet40 official train/test split. MOdelNet10 requires separate downloading and sampling.
# MAX_N_POINTS = 2048
# NUM_CLASSES = 40
# TRAIN_FILES = provider.getDataFiles( \
#     os.path.join(BASE_DIR, 'data/modelnet'+str(NUM_CLASSES)+'_ply_hdf5_'+ str(MAX_N_POINTS)+ '/train_files.txt'))
# TEST_FILES = provider.getDataFiles(\
#     os.path.join(BASE_DIR, 'data/modelnet'+str(NUM_CLASSES)+'_ply_hdf5_'+ str(MAX_N_POINTS)+ '/test_files.txt'))
# LABEL_MAP = provider.getDataFiles(\
예제 #28
0
NUM_POINT = FLAGS.num_point
BATCH_SIZE = FLAGS.batch
NFEATURES = FLAGS.nfeat

NUM_CATEGORIES = FLAGS.ncat
#Only used to get how many parts per category

print('#### Batch Size : {0}'.format(BATCH_SIZE))
print('#### Point Number: {0}'.format(NUM_POINT))
print('#### Using GPUs: {0}'.format(FLAGS.gpu))

if not os.path.exists(FLAGS.plot_path): os.mkdir(FLAGS.plot_path)

print('### Starting evaluation')

EVALUATE_FILES = provider.getDataFiles(
    os.path.join(H5_DIR, 'evaluate_files.txt'))
print("Loading: ", os.path.join(H5_DIR, 'evaluate_files.txt'))


def printout(flog, data):
    print(data)
    flog.write(data + '\n')


def convert_label_to_one_hot(labels):
    label_one_hot = np.zeros((labels.shape[0], NUM_CATEGORIES))
    for idx in range(labels.shape[0]):
        label_one_hot[idx, labels[idx] - MIN_LABEL] = 1
    return label_one_hot

예제 #29
0
import os
import sys

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, '../../VisionProcess'))
from FileIO import FileIO
import provider

# load dataset
TRAIN_FILES_VEC = [[],[]]
TEST_FILES_VEC = [[],[]]
for i in range(1):
    path = 'data/extracted_feature'
#    path = 'data/modelnet40_ply_hdf5_2048/'
    TRAIN_FILES_VEC[i] = provider.getDataFiles( \
        os.path.join(BASE_DIR, path + '/train_files.txt'))
    TEST_FILES_VEC[i] = provider.getDataFiles(\
        os.path.join(BASE_DIR, path + '/test_files.txt'))

## data are point cloud 
#data = np.array([], dtype=np.float32).reshape(0,2048,3)
#label = np.array([], dtype=np.float32).reshape(0,1)

## data are features
data = np.array([], dtype=np.float32).reshape(0,1024) 
label = np.array([], dtype=np.float32).reshape(0)
for i in range(len(TEST_FILES_VEC[0])):
    data_temp, label_temp = FileIO.load_h5(TEST_FILES_VEC[0][i])
    data = np.concatenate((data, data_temp), axis=0)
    label = np.concatenate((label, label_temp), axis=0)
예제 #30
0
import sys
import time
import numpy as np


def random_shuffle(x):
    perm = np.arange(x.shape[0])
    np.random.shuffle(perm)
    x = x[perm]
    return x, perm


# Start
start_time = time.time()
print('Start testing')

train_files = pv.getDataFiles('trainval_shape_voxel_data_list.txt')
test_files = pv.getDataFiles('test_shape_voxel_data_list.txt')
print('#train_files = {}'.format(train_files.shape[0]))
print('#test_files = {}'.format(test_files.shape[0]))

num_of_files = train_files.shape[0]
train_files, _ = random_shuffle(train_files)

print('|', end='')
for fn in range(num_of_files):
    current_data, current_label = pv.loadDataFile(train_files[fn], 42)
    print('*', end='')
    sys.stdout.flush()
print('|')
print('total time: ', time.time() - start_time)
예제 #31
0
BATCH_SIZE = FLAGS.batch_size
NUM_POINT = FLAGS.num_point
MODEL_PATH = FLAGS.model_path  # to change
GPU_INDEX = FLAGS.gpu
MODEL = importlib.import_module(FLAGS.model)  # import network module
DUMP_DIR = FLAGS.dump_dir
if not os.path.exists(DUMP_DIR): os.mkdir(DUMP_DIR)
LOG_FOUT = open(os.path.join(DUMP_DIR, 'log_evaluate.txt'), 'w')
LOG_FOUT.write(str(FLAGS) + '\n')

NUM_CLASSES = 40
SHAPE_NAMES = [line.rstrip() for line in \
    open(os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/shape_names.txt'))]

# ModelNet40 official train/test split
TRAIN_FILES = provider.getDataFiles( \
    os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/train_files.txt'))
TEST_FILES = provider.getDataFiles(\
    os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/test_files.txt'))


def log_string(out_str):
    LOG_FOUT.write(out_str + '\n')
    LOG_FOUT.flush()
    print(out_str)


def evaluate(num_votes=1):
    is_training = False

    with tf.device('/gpu: ' + str(GPU_INDEX)):
        pointclouds_pl, labels_pl = MODEL.placeholder_inputs(1, NUM_POINT)
예제 #32
0
os.system('cp train.py %s' % (LOG_DIR)) # bkp of train procedure
LOG_FOUT = open(os.path.join(LOG_DIR, 'log_train.txt'), 'w')
LOG_FOUT.write(str(FLAGS)+'\n')

MAX_NUM_POINT = 4096
NUM_CLASSES = 13

BN_INIT_DECAY = 0.5
BN_DECAY_DECAY_RATE = 0.5
#BN_DECAY_DECAY_STEP = float(DECAY_STEP * 2)
BN_DECAY_DECAY_STEP = float(DECAY_STEP)
BN_DECAY_CLIP = 0.99

HOSTNAME = socket.gethostname()

ALL_FILES = provider.getDataFiles('indoor3d_sem_seg_hdf5_data/all_files.txt')
room_filelist = [line.rstrip() for line in open('indoor3d_sem_seg_hdf5_data/room_filelist.txt')]

# Load ALL data
data_batch_list = []
label_batch_list = []
for h5_filename in ALL_FILES:
    data_batch, label_batch = provider.loadDataFile(h5_filename)
    data_batch_list.append(data_batch)
    label_batch_list.append(label_batch)
data_batches = np.concatenate(data_batch_list, 0)
label_batches = np.concatenate(label_batch_list, 0)
print(data_batches.shape)
print(label_batches.shape)

test_area = 'Area_'+str(FLAGS.test_area)