Exemple #1
0
                                    start_epoch_=start_epoch,
                                    adam=False,
                                    patience=7)

        plt.plot(losses, label='Train losses')
        plt.plot(test_losses, label='Test losses')
        plt.legend()
        plt.savefig(f"losses_{siameseNetwork.name}.png")
        plt.clf()

        # Fine tune the model
        sys.stdout.write(f"Starting fine tune of {base}\n")
        ft_lr = lr * 0.01
        ft_epochs = -1
        ft_start_epoch = 0
        siameseNetwork.load(f'./models/triplet/best-{model_name_}.pt')
        siameseNetwork.prepare_for_fine_tuning()
        model_name_ += '_ft'

        losses, test_losses = train(siameseNetwork,
                                    train_loader,
                                    test_loader,
                                    ft_epochs,
                                    ft_lr,
                                    model_name_,
                                    start_epoch_=ft_start_epoch,
                                    adam=False)

        plt.plot(losses, label='Train losses')
        plt.plot(test_losses, label='Test losses')
        plt.legend()
                                    patience=7)

        start_epoch = 0

        plt.plot(losses, label='Train losses')
        plt.plot(test_losses, label='Test losses')
        plt.legend()
        plt.savefig(f"losses_{siameseNetwork.name}.png")
        plt.clf()

        # Fine tune the model
        print("Starting fine tune of", base)
        ft_lr = lr * 0.01
        ft_epochs = 30
        ft_start_epoch = 0
        siameseNetwork.load(f'./models/contrastive/best-{model_name_}.pt')
        siameseNetwork.prepare_for_fine_tuning()
        model_name_ += '_ft'

        losses, test_losses = train(siameseNetwork,
                                    train_loader,
                                    test_loader,
                                    ft_epochs,
                                    ft_lr,
                                    model_name_,
                                    start_epoch_=ft_start_epoch,
                                    adam=False)

        plt.plot(losses, label='Train losses')
        plt.plot(test_losses, label='Test losses')
        plt.legend()

if __name__ == '__main__':
    lr = 0.001
    batch_size = 32
    epochs = 100
    start_epoch = 0

    train_data = FacesDataset(train=True, validation=False, base='resnet101')
    train_loader = DataLoader(train_data, batch_size, False)

    test = FacesDataset(train=False, validation=False, base='resnet101')
    test_loader = DataLoader(test, batch_size, False)

    siamese_network = SiameseNetwork(base='resnet101').cuda()
    siamese_network.load('./models/triplet/resnet101.pt')
    siamese_network.eval()

    predictor_ = LinearPredictor().cuda()

    sys.stdout.write('Training Linear predictor:\n')
    losses, test_losses, train_accuracies_, test_accuracies_ = train(
        siamese_network,
        predictor_,
        train_loader,
        test_loader,
        epochs,
        lr,
        'predictor-linear',
        start_epoch_=start_epoch,
        adam=False,