def train(data_folder, trained_network_file):
    """
    Function for training the network.
    """
    infer_action = ClassificationNetwork()
    optimizer = torch.optim.Adam(infer_action.parameters(), lr=1e-2)
    observations, actions = load_imitations(data_folder)
    observations = [torch.Tensor(observation) for observation in observations]
    actions = [torch.Tensor(action) for action in actions]

    batches = [
        batch for batch in zip(observations,
                               infer_action.actions_to_classes(actions))
    ]
    gpu = torch.device('cuda')

    nr_epochs = 100
    batch_size = 64
    number_of_classes = 0  # needs to be changed
    start_time = time.time()

    for epoch in range(nr_epochs):
        random.shuffle(batches)

        total_loss = 0
        batch_in = []
        batch_gt = []
        for batch_idx, batch in enumerate(batches):
            batch_in.append(batch[0].to(gpu))
            batch_gt.append(batch[1].to(gpu))

            if (batch_idx +
                    1) % batch_size == 0 or batch_idx == len(batches) - 1:
                batch_in = torch.reshape(torch.cat(batch_in, dim=0),
                                         (-1, 96, 96, 3))
                batch_gt = torch.reshape(torch.cat(batch_gt, dim=0),
                                         (-1, number_of_classes))

                batch_out = infer_action(batch_in)
                loss = cross_entropy_loss(batch_out, batch_gt)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                total_loss += loss

                batch_in = []
                batch_gt = []

        time_per_epoch = (time.time() - start_time) / (epoch + 1)
        time_left = (1.0 * time_per_epoch) * (nr_epochs - 1 - epoch)
        print("Epoch %5d\t[Train]\tloss: %.6f \tETA: +%fs" %
              (epoch + 1, total_loss, time_left))

    torch.save(infer_action, trained_network_file)
Ejemplo n.º 2
0
def train(data_folder, trained_network_file):
    """
    Function for training the network.

    As we were not supposed to change the main file, you can change all the parameters here:
    Available (boolean) variables:
    use_sensors -> Ex 2a, use the extract sensor data in addition to images
    use_multi_binary_class -> Ex 2b, take 4 binary classes as output instead of control classes
    use_regression -> Ex 2c, formulate the problem as a regression task

    """
    print("START TRAINING")

    use_multi_binary_class = False  #TODO: Make this a parameter or smth
    use_sensors = True
    use_regression = True

    gpu = torch.device('cuda')
    infer_action = ClassificationNetwork(
        use_sensors=use_sensors,
        use_multi_binary=use_multi_binary_class,
        use_regression=use_regression)
    infer_action.to(gpu)  #make the network run on the gpu
    optimizer = torch.optim.Adam(infer_action.parameters(), lr=1e-4 * 5)
    observations, actions = load_imitations(data_folder)
    observations = [torch.Tensor(observation) for observation in observations]
    actions = [torch.Tensor(action) for action in actions]

    if use_multi_binary_class:
        batches = [
            batch for batch in zip(observations,
                                   infer_action.infer_to_multi_class(actions))
        ]
        # print(batches[0])
    elif use_regression:
        batches = [batch for batch in zip(observations, actions)]
    else:
        batches = [
            batch for batch in zip(observations,
                                   infer_action.actions_to_classes(actions))
        ]
        # print(batches[0])

    nr_epochs = 100
    batch_size = 128

    if use_multi_binary_class:
        number_of_classes = 4
    elif use_regression:
        number_of_classes = 3  #Not really classes but this defines our output size
    else:
        number_of_classes = 9  # needs to be changed
    start_time = time.time()

    for epoch in range(nr_epochs):
        random.shuffle(batches)

        total_loss = 0
        batch_in = []
        batch_gt = []
        for batch_idx, batch in enumerate(batches):
            batch_in.append(batch[0].to(gpu))
            batch_gt.append(batch[1].to(gpu))

            if (batch_idx +
                    1) % batch_size == 0 or batch_idx == len(batches) - 1:
                batch_in = torch.reshape(torch.cat(batch_in, dim=0),
                                         (-1, 96, 96, 3))
                batch_gt = torch.reshape(torch.cat(batch_gt, dim=0),
                                         (-1, number_of_classes))

                batch_out = infer_action(
                    batch_in)  #changed the order of dimensions

                if use_multi_binary_class:
                    # print("TARGET: " + str(batch_gt))
                    # print("OUTPUT: " + str(batch_out))
                    loss = functional.mse_loss(batch_out, batch_gt.float())
                elif use_regression:
                    loss = functional.mse_loss(batch_out, batch_gt.float())
                else:
                    loss = cross_entropy_loss(
                        batch_out, batch_gt.float())  #targets can only be long

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                total_loss += loss

                batch_in = []
                batch_gt = []

        time_per_epoch = (time.time() - start_time) / (epoch + 1)
        time_left = (1.0 * time_per_epoch) * (nr_epochs - 1 - epoch)
        print("Epoch %5d\t[Train]\tloss: %.6f \tETA: +%fs" %
              (epoch + 1, total_loss, time_left))

    torch.save(infer_action, trained_network_file)
Ejemplo n.º 3
0
            reward_per_episode += reward

        print('episode %d \t reward %f' % (episode, reward_per_episode))
        total_reward += reward_per_episode

    print('---------------------------')
    print(' total score: %f' % (total_reward / 10))
    print('---------------------------')


if __name__ == "__main__":

    logging.basicConfig(level=logging.INFO)

    # test 1.1a)
    from imitations import load_imitations
    load_imitations('./data/teacher')

    if len(sys.argv) == 1 or sys.argv[1] == "train":
        train(imitations_folder, trained_network_file)
    elif sys.argv[1] == "teach":
        record_imitations(imitations_folder)
    elif sys.argv[1] == "test":
        evaluate()
    elif sys.argv[1] == "score":
        calculate_score_for_leaderboard()
    else:
        print(
            'This command is not supported, valid options are: train, teach, '
            'test and score.')
Ejemplo n.º 4
0
def train(data_folder, trained_network_file):
    """
    Function for training the network.
    """
    infer_action = MultiClassNetwork()
    #    infer_action = ClassificationNetwork()
    print(infer_action)
    optimizer = torch.optim.Adam(infer_action.parameters(), lr=1e-2)
    observations, actions = load_imitations(data_folder)
    observations = [torch.Tensor(observation) for observation in observations]
    actions = [torch.Tensor(action) for action in actions]

    batches = [
        batch for batch in zip(observations,
                               infer_action.actions_to_classes(actions))
    ]
    gpu = torch.device('cuda')
    #gpu = torch.device('cpu')

    nr_epochs = 150
    batch_size = 128
    number_of_classes = 4  # needs to be changed: 9 for classificationNetwork() and 4 if MultiClassNetwork()
    start_time = time.time()
    loss_plot = []

    # trying to augment the data
    # transform = transforms.Compose([
    #     transforms.RandomHorizontalFlip(),
    # ])

    for epoch in range(nr_epochs):
        random.shuffle(batches)

        total_loss = 0
        batch_in = []
        batch_gt = []
        for batch_idx, batch in enumerate(batches):
            batch_in.append(batch[0].to(gpu))
            batch_gt.append(batch[1].to(gpu))

            if (batch_idx +
                    1) % batch_size == 0 or batch_idx == len(batches) - 1:
                batch_in = torch.reshape(torch.cat(batch_in, dim=0),
                                         (-1, 96, 96, 3))
                batch_gt = torch.reshape(torch.cat(batch_gt, dim=0),
                                         (-1, number_of_classes))

                batch_out = infer_action(batch_in)
                # we tried another cross entropy loss for the multiclass network,
                # but the results weren't better so we switched back
                #criterion = torch.nn.BCELoss(reduction='mean')
                #loss = criterion(batch_out, batch_gt)
                loss = cross_entropy_loss(batch_out, batch_gt)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                total_loss += loss

                batch_in = []
                batch_gt = []
        time_per_epoch = (time.time() - start_time) / (epoch + 1)
        time_left = (1.0 * time_per_epoch) * (nr_epochs - 1 - epoch)
        print("Epoch %5d\t[Train]\tloss: %.6f \tETA: +%fs" %
              (epoch + 1, total_loss, time_left))
        loss_plot.append(total_loss)
    torch.save(infer_action, trained_network_file)

    # plotting the loss/learning curve
    plt.figure()
    plt.title("training curve")
    plt.xlabel("epochs")
    plt.ylabel("loss")
    plt.plot(loss_plot, label="entropy-loss")
    plt.legend(loc="best")
    plt.show()