示例#1
0
def main():
    # Get parameters from arguments
    parser = argparse.ArgumentParser(description='Model training')
    parser.add_argument('-c',
                        '--config_path',
                        type=str,
                        default='./config/diwu_rematch.py',
                        help='Configuration file')

    arguments = parser.parse_args()
    assert arguments.config_path is not None, 'Please provide a path using -c config/pathname in the command line'
    print('\n > Start Time:')
    print('   ' + datetime.now().strftime('%a, %d %b %Y-%m-%d %H:%M:%S'))
    start_time = time.time()
    # Define the user paths

    # Load configuration files
    configuration = Configuration(arguments.config_path)
    cf = configuration.load()
    configurationPATH(cf)

    process(cf)

    # End Time
    end_time = time.time()
    print('\n > End Time:')
    print('   ' + datetime.now().strftime('%a, %d %b %Y-%m-%d %H:%M:%S'))
    print('\n   ET: ' + HMS(end_time - start_time))
def main():
    # Define environment variables
    # Environment()

    # Get parameters from arguments
    parser = argparse.ArgumentParser(description='Model training')
    parser.add_argument('-c',
                        '--config_path',
                        type=str,
                        default=None,
                        help='Configuration file')
    parser.add_argument('-e',
                        '--exp_name',
                        type=str,
                        default=None,
                        help='Name of the experiment')
    parser.add_argument('-s',
                        '--shared_path',
                        type=str,
                        default='/data/module5',
                        help='Path to shared data folder')
    parser.add_argument('-l',
                        '--local_path',
                        type=str,
                        default='/home/master/sufav/results',
                        help='Path to local data folder')

    arguments = parser.parse_args()

    assert arguments.config_path is not None, 'Please provide a configuration' \
                                              'path using -c config/pathname' \
                                              ' in the command line'
    assert arguments.exp_name is not None, 'Please provide a name for the ' \
                                           'experiment using -e name in the ' \
                                           'command line'

    # Define the user paths
    shared_path = arguments.shared_path
    local_path = arguments.local_path
    dataset_path = os.path.join(local_path, 'Datasets')
    shared_dataset_path = os.path.join(shared_path, 'Datasets')
    experiments_path = os.path.join(local_path, getuser(), 'Experiments')
    # Note: this should not be used
    shared_experiments_path = os.path.join(shared_path, getuser(),
                                           'Experiments')

    usr_path = os.path.join('/home/', getuser())

    # Load configuration files
    configuration = Configuration(arguments.config_path, arguments.exp_name,
                                  dataset_path, shared_dataset_path,
                                  experiments_path, shared_experiments_path,
                                  usr_path)
    cf = configuration.load()

    # Train /test/predict with the network, depending on the configuration
    process(cf)

    # Copy result to shared directory
    configuration.copy_to_shared()
示例#3
0
def main():
    print('Using GPU: ', torch.cuda.get_device_name(0))
    start_time = time.time()
    # Prepare configuration
    config = Configuration()
    cf = config.load()
    # Enable log file
    logger_debug = Logger(cf.log_file_debug)

    logger_debug.write('\n ---------- Init experiment: ' + cf.exp_name +
                       ' ---------- \n')
    # Model building
    logger_debug.write('- Building model: ' + cf.model_name + ' <--- ')
    model = ModelBuilder(cf)
    model.build()
    print(model.net)

    # Problem type
    if cf.problem_type == 'segmentation':
        problem_manager = SemanticSegmentationManager(cf, model)
    elif cf.problem_type == 'classification':
        problem_manager = ClassificationManager(cf, model)
    elif cf.problem_type == 'detection':
        problem_manager = DetectionManager(cf, model)
    else:
        raise ValueError('Unknown problem type')

    # Create dataloader builder
    dataloader = DataLoaderBuilder(cf, model)

    if cf.train:
        model.net.train()  # enable dropout modules and others
        train_time = time.time()
        logger_debug.write('\n- Reading Train dataset: ')
        dataloader.build_train()
        if (cf.valid_dataset_path is not None or
            (cf.valid_images_txt is not None
             and cf.valid_gt_txt is not None)) and cf.valid_samples_epoch != 0:
            logger_debug.write('\n- Reading Validation dataset: ')
            dataloader.build_valid(cf.valid_samples_epoch, cf.valid_images_txt,
                                   cf.valid_gt_txt, cf.resize_image_valid,
                                   cf.valid_batch_size)
            problem_manager.trainer.start(dataloader.train_loader,
                                          dataloader.train_set,
                                          dataloader.valid_set,
                                          dataloader.valid_loader)
        else:
            # Train without validation inside epoch
            problem_manager.trainer.start(dataloader.train_loader,
                                          dataloader.train_set)
        train_time = time.time() - train_time
        logger_debug.write('\t Train step finished: %ds ' % train_time)

    if cf.validation:
        model.net.eval()
        valid_time = time.time()
        if not cf.train:
            logger_debug.write('- Reading Validation dataset: ')
            dataloader.build_valid(cf.valid_samples, cf.valid_images_txt,
                                   cf.valid_gt_txt, cf.resize_image_valid,
                                   cf.valid_batch_size)
        else:
            # If the Dataloader for validation was used on train, only update the total number of images to take
            dataloader.valid_set.update_indexes(
                cf.valid_samples,
                valid=True)  # valid=True avoids shuffle for validation
        logger_debug.write('\n- Starting validation <---')
        problem_manager.validator.start(dataloader.valid_set,
                                        dataloader.valid_loader, 'Validation')
        valid_time = time.time() - valid_time
        logger_debug.write('\t Validation step finished: %ds ' % valid_time)

    if cf.test:
        model.net.eval()
        test_time = time.time()
        logger_debug.write('\n- Reading Test dataset: ')
        dataloader.build_valid(cf.test_samples, cf.test_images_txt,
                               cf.test_gt_txt, cf.resize_image_test,
                               cf.test_batch_size)
        logger_debug.write('\n - Starting test <---')
        problem_manager.validator.start(dataloader.valid_set,
                                        dataloader.valid_loader, 'Test')
        test_time = time.time() - test_time
        logger_debug.write('\t Test step finished: %ds ' % test_time)

    if cf.predict_test:
        model.net.eval()
        pred_time = time.time()
        logger_debug.write('\n- Reading Prediction dataset: ')
        dataloader.build_predict()
        logger_debug.write('\n - Generating predictions <---')
        problem_manager.predictor.start(dataloader.predict_loader)
        pred_time = time.time() - pred_time
        logger_debug.write('\t Prediction step finished: %ds ' % pred_time)

    total_time = time.time() - start_time
    logger_debug.write('\n- Experiment finished: %ds ' % total_time)
    logger_debug.write('\n')
示例#4
0
def main():
    # Define environment variables
    # Environment()

    # Get parameters from arguments
    parser = argparse.ArgumentParser(description='Model training')
    parser.add_argument('-c',
                        '--config_path',
                        type=str,
                        default=None,
                        help='Configuration file')
    parser.add_argument('-e',
                        '--exp_name',
                        type=str,
                        default=None,
                        help='Name of the experiment')
    parser.add_argument('-s',
                        '--shared_path',
                        type=str,
                        default='/data',
                        help='Path to shared data folder')
    parser.add_argument('-l',
                        '--local_path',
                        type=str,
                        default='/datatmp',
                        help='Path to local data folder')

    arguments = parser.parse_args()

    assert arguments.config_path is not None, 'Please provide a configuration'\
                                              'path using -c config/pathname'\
                                              ' in the command line'
    assert arguments.exp_name is not None, 'Please provide a name for the '\
                                           'experiment using -e name in the '\
                                           'command line'

    # Start Time
    print('\n > Start Time:')
    print('   ' + datetime.now().strftime('%a, %d %b %Y-%m-%d %H:%M:%S'))
    start_time = time.time()

    # Define the user paths
    shared_path = arguments.shared_path
    local_path = arguments.local_path
    dataset_path = os.path.join(local_path, 'Datasets')
    shared_dataset_path = os.path.join(shared_path, 'Datasets')
    experiments_path = os.path.join(local_path, 'Experiments')
    shared_experiments_path = os.path.join(shared_path, 'Experiments')
    usr_path = os.path.join('/home/', getuser())

    # Load configuration files
    configuration = Configuration(arguments.config_path, arguments.exp_name,
                                  dataset_path, shared_dataset_path,
                                  experiments_path, shared_experiments_path,
                                  usr_path)

    cf = configuration.load()

    configurationPATH(cf, dataset_path)

    # Train /test/predict with the network, depending on the configuration
    process(cf)

    # Copy result to shared directory
    # configuration.copy_to_shared()

    # End Time
    end_time = time.time()
    print('\n > End Time:')
    print('   ' + datetime.now().strftime('%a, %d %b %Y-%m-%d %H:%M:%S'))
    print('\n   ET: ' + HMS(end_time - start_time))  # -> H:M:S
示例#5
0
def main():
    # Load configuration files
    configuration = Configuration(
        '/home/wzn/PycharmProjects/alibaba_weather_route/config/wzn.py')
    cf = configuration.load()
    # get the original 6*9 maze
    time_length = 25
    maze = Maze_3D(height=9,
                   width=9,
                   time_length=time_length,
                   start_state=(2, 0, 0),
                   goal_states=[[0, 8]],
                   return_to_start=cf.return_to_start,
                   strong_wind_return=cf.strong_wind_return,
                   reward_goal=cf.reward_goal,
                   reward_move=cf.reward_move,
                   reward_obstacle=cf.reward_obstacle,
                   cf=cf)
    # set up goal states
    gs = []
    for t in range(time_length):
        gs.append(tuple([0, 8, t]))
    maze.GOAL_STATES = gs
    # set up obstacles
    maze.wind_real_day_hour_total = np.zeros(shape=(maze.WORLD_HEIGHT,
                                                    maze.WORLD_WIDTH,
                                                    maze.TIME_LENGTH))
    obstacles = [[1, 2], [2, 2], [3, 2], [0, 7], [1, 7], [2, 7], [4, 5]]
    for t in range(time_length - 5):
        for item in obstacles:
            maze.wind_real_day_hour_total[item[0], item[1], t] = cf.wall_wind
    obstacles = [[2, 4], [3, 4], [3, 2], [0, 7], [1, 7], [2, 7], [4, 5]]
    for t in range(5, time_length):
        for item in obstacles:
            maze.wind_real_day_hour_total[item[0], item[1], t] = cf.wall_wind
    for t in range(5, time_length - 3):
        maze.wind_real_day_hour_total[1, 8, t] = cf.wall_wind

    # Dyna model hyper
    rand = np.random.RandomState(0)
    planningSteps = 5
    alpha = 0.5
    gamma = 0.99
    theta = 1e-4
    epsilon = 0.1
    # track the # of backups
    runs = 1
    numOfMazes = 1
    numOfModels = 5
    backups = np.zeros((runs, numOfModels, numOfMazes))

    ############## A star search algorithm  ######################################
    start_time = timer()
    diagram = convert_3D_maze_to_grid(maze, cf)
    came_from, cost_so_far, [final_goal_time
                             ], wind_costs, rainfall_costs = a_star_search_3D(
                                 diagram, tuple(maze.START_STATE),
                                 maze.GOAL_STATES)
    go_to_all, steps = walk_final_grid_go_to(tuple(maze.START_STATE),
                                             maze.GOAL_STATES,
                                             came_from,
                                             final_goal_time,
                                             include_all=cf.include_all)
    if True:
        draw_grid_3d(diagram,
                     came_from=came_from,
                     start=tuple(maze.START_STATE),
                     goal=tuple(maze.GOAL_STATES),
                     title='A star')
    print('Finish, using %.2f sec!' % (timer() - start_time))
    # the following expand dims just to cater to challenge might have 10 models
    maze.wind_real_day_hour_total = np.tile(maze.wind_real_day_hour_total,
                                            [10, 1, 1, 1])
    # we artificially added this
    # for t in range(time_length-2):
    #     for item in [[0, 2]]:
    #         maze.wind_real_day_hour_total[cf.temp_model, item[0], item[1], t] = cf.wall_wind

    ##############End  A star search algorithm  ######################################
    model_Dyna_Q = Dyna_3D(rand=rand,
                           maze=maze,
                           epsilon=epsilon,
                           gamma=gamma,
                           planningSteps=planningSteps,
                           qLearning=True,
                           expected=False,
                           alpha=alpha,
                           priority=False)

    model_Dyna_Prioritized_Sweeping_ES = Dyna_3D(rand=rand,
                                                 maze=maze,
                                                 epsilon=epsilon,
                                                 gamma=gamma,
                                                 planningSteps=planningSteps,
                                                 qLearning=False,
                                                 expected=True,
                                                 alpha=alpha,
                                                 priority=True,
                                                 theta=theta)

    model_Dyna_Prioritized_Sweeping_Q = Dyna_3D(rand=rand,
                                                maze=maze,
                                                epsilon=epsilon,
                                                gamma=gamma,
                                                planningSteps=planningSteps,
                                                qLearning=True,
                                                expected=False,
                                                alpha=alpha,
                                                priority=True,
                                                theta=theta)

    model_Dyna_Prioritized_Sweeping_Q_A_star = Dyna_3D(
        rand=cf.random_state,
        maze=maze,
        epsilon=cf.epsilon,
        gamma=cf.gamma,
        planningSteps=cf.planningSteps,
        qLearning=cf.qLearning,
        expected=cf.expected,
        alpha=cf.alpha,
        priority=cf.priority,
        theta=cf.theta,
        policy_init=go_to_all,
        plus=cf.plus,
        increase_epsilon=cf.increase_epsilon)

    model_Dyna_Prioritized_Sweeping_Q_heuristic = Dyna_3D(
        rand=rand,
        maze=maze,
        epsilon=epsilon,
        gamma=gamma,
        planningSteps=planningSteps,
        qLearning=True,
        expected=False,
        alpha=alpha,
        priority=True,
        theta=theta,
        heuristic=True)

    models = [
        model_Dyna_Prioritized_Sweeping_Q_heuristic, model_Dyna_Q,
        model_Dyna_Prioritized_Sweeping_Q_A_star,
        model_Dyna_Prioritized_Sweeping_Q, model_Dyna_Prioritized_Sweeping_ES
    ]

    models = [
        model_Dyna_Prioritized_Sweeping_Q_A_star,
        model_Dyna_Prioritized_Sweeping_Q, model_Dyna_Prioritized_Sweeping_ES
    ]

    for m, model in enumerate(models):
        for run in range(0, runs):
            print('run:', run, model.name, 'maze size:',
                  maze.WORLD_HEIGHT * maze.WORLD_WIDTH)
            start_time = timer()
            # track steps / backups for each episode
            steps = []
            # play for an episode
            while True:
                steps.append(model.play())
                # print best action w.r.t. current state-action values
                # printActions(currentStateActionValues, maze)
                # check whether the (relaxed) optimal path is found
                came_from = model.checkPath(len(go_to_all.keys()))
                if came_from:
                    draw_grid_3d(diagram,
                                 came_from=came_from,
                                 start=tuple(maze.START_STATE),
                                 goal=tuple(maze.GOAL_STATES),
                                 title=model.name)
                    print(steps)
                    break

            backups[run][m] = np.sum(steps)
            print('Finish, using %.2f sec!' % (timer() - start_time))

    # Dyna-Q performs several backups per step
    backups = np.sum(backups, axis=0)
    # average over independent runs
    backups /= float(runs)
    print(backups)
    plt.show()