Esempio n. 1
0
def launch_action(parameters, verbose=True):
    """
    Function that set the step by step to train and predict the model 
    :INPUT:
        parameters: dict, 
            {   
                "gpu": integer, ID of the GPU to use 
                "mode": str, action to execute 'train', 'test' or 'kfold'  
                "model_path": str, Path to the model. This model if going to use for make the predictions ['mode'='test'] 
                              or to continue the training process [mode='train' and 'trainFromCKP'=True]. 
                "path2data":str, path to the folder  that contain the data [train/*.ply, test/*.ply, fold_x/*.ply]
                "path2output": str, path to the output folder, this is only use when the mode is set to train 
                "protocol":str, training protocol, 'xyz', 'field', 'kfold' 
                "trainFromCKP":bool, only use it if the models is going to be trained from a checkpoint
            }        
    """
    GPU_ID = parameters["gpu"]
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ['CUDA_VISIBLE_DEVICES'] = str(GPU_ID)
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

    Mode = parameters["mode"]

    dataset = AppleTree(protocol, path2dataset=inputDir)
    dataset.init_input_pipeline(mode=Mode)
    
    if Mode == 'train' and not parameters["trainFromCKP"]:
        model = Network(dataset, cfg)
        model.train(dataset)

    elif Mode == "train" and parameters["trainFromCKP"]:
        model = Network(dataset, cfg, restore_snap=parameters["model_path"])
        model.train(dataset)

    elif Mode == "validation":

        cfg.saving = False
        if parameters["model_path"] is not 'None':
            chosen_snap = parameters["model_path"]
        else:
            chosen_snapshot = -1
            # logs = np.sort([os.path.join('results', f) for f in os.listdir('results') if f.startswith('Log')])
            # chosen_folder = logs[-1]
            chosen_folder = parameters["outputDir"]
            snap_path = join(chosen_folder, 'snapshots')
            snap_steps = [int(f[:-5].split('-')[-1]) for f in os.listdir(snap_path) if f[-5:] == '.meta']
            chosen_step = np.sort(snap_steps)[-1]
            chosen_snap = os.path.join(snap_path, 'snap-{:d}'.format(chosen_step))
        
        model = Network(dataset, cfg, restore_snap=chosen_snap)
        model.evaluate(dataset)

    elif Mode == 'test':
        cfg.saving = False
        model = Network(dataset, cfg)
        if parameters["model_path"] is not 'None':
            chosen_snap = parameters["model_path"]
        else:
            chosen_snapshot = -1
            # logs = np.sort([os.path.join('results', f) for f in os.listdir('results') if f.startswith('Log')])
            # chosen_folder = logs[-1]
            chosen_folder = parameters["outputDir"]
            snap_path = join(chosen_folder, 'snapshots')
            snap_steps = [int(f[:-5].split('-')[-1]) for f in os.listdir(snap_path) if f[-5:] == '.meta']
            chosen_step = np.sort(snap_steps)[-1]
            chosen_snap = os.path.join(snap_path, 'snap-{:d}'.format(chosen_step))
        
        if(verbose):
            print("Test snap: ", chosen_snap)
        tester = ModelTester(model, dataset, restore_snap=chosen_snap)
        tester.test(model, dataset)

    else:
        ##################
        # Visualize data #
        ##################

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(dataset.train_init_op)
            while True:
                flat_inputs = sess.run(dataset.flat_inputs)
                pc_xyz = flat_inputs[0]
                sub_pc_xyz = flat_inputs[1]
                labels = flat_inputs[21]
                Plot.draw_pc_sem_ins(pc_xyz[0, :, :], labels[0, :])
                Plot.draw_pc_sem_ins(sub_pc_xyz[0, :, :], labels[0, 0:np.shape(sub_pc_xyz)[1]])
Esempio n. 2
0
def launch_training(protocol, inputDir, parameters=None):
    GPU_ID = parameters["gpu"]
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ['CUDA_VISIBLE_DEVICES'] = str(GPU_ID)
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

    Mode = parameters["mode"]

    dataset = AppleTree(protocol, path2dataset=inputDir)
    dataset.init_input_pipeline(mode=Mode)

    if Mode == 'train' and not parameters["restoreTrain"]:
        model = Network(dataset, cfg)
        model.train(dataset)

    elif Mode == "train" and parameters["restoreTrain"]:
        model = Network(dataset, cfg, restore_snap=parameters["model_path"])
        model.train(dataset)

    elif Mode == "validation":

        cfg.saving = False
        if parameters["model_path"] is not 'None':
            chosen_snap = parameters["model_path"]
        else:
            chosen_snapshot = -1
            # logs = np.sort([os.path.join('results', f) for f in os.listdir('results') if f.startswith('Log')])
            # chosen_folder = logs[-1]
            chosen_folder = parameters["outputDir"]
            snap_path = join(chosen_folder, 'snapshots')
            snap_steps = [
                int(f[:-5].split('-')[-1]) for f in os.listdir(snap_path)
                if f[-5:] == '.meta'
            ]
            chosen_step = np.sort(snap_steps)[-1]
            chosen_snap = os.path.join(snap_path,
                                       'snap-{:d}'.format(chosen_step))

        model = Network(dataset, cfg, restore_snap=chosen_snap)
        model.evaluate(dataset)

    elif Mode == 'test':
        cfg.saving = False
        model = Network(dataset, cfg)
        if parameters["model_path"] is not 'None':
            chosen_snap = parameters["model_path"]
        else:
            chosen_snapshot = -1
            # logs = np.sort([os.path.join('results', f) for f in os.listdir('results') if f.startswith('Log')])
            # chosen_folder = logs[-1]
            chosen_folder = parameters["outputDir"]
            snap_path = join(chosen_folder, 'snapshots')
            snap_steps = [
                int(f[:-5].split('-')[-1]) for f in os.listdir(snap_path)
                if f[-5:] == '.meta'
            ]
            chosen_step = np.sort(snap_steps)[-1]
            chosen_snap = os.path.join(snap_path,
                                       'snap-{:d}'.format(chosen_step))

        print(chosen_snap)
        tester = ModelTester(model, dataset, restore_snap=chosen_snap)
        tester.test(model, dataset)

    else:
        ##################
        # Visualize data #
        ##################

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(dataset.train_init_op)
            while True:
                flat_inputs = sess.run(dataset.flat_inputs)
                pc_xyz = flat_inputs[0]
                sub_pc_xyz = flat_inputs[1]
                labels = flat_inputs[21]
                Plot.draw_pc_sem_ins(pc_xyz[0, :, :], labels[0, :])
                Plot.draw_pc_sem_ins(sub_pc_xyz[0, :, :],
                                     labels[0, 0:np.shape(sub_pc_xyz)[1]])