Example #1
0
 def train(self):
     device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
     self.tools.logging("The model is set.")
     self.tools.logging("The {} device is selected.".format(device))
     self.model = learning.AliNet()
     self.model = self.model.to(device)
     self.dataloaders = self.dataloaders.to(device)
     summary(self.model, input_size=(3, 25, 25))
     self.optimizer_ft = optim.Adam(self.model.parameters(), lr=1e-4)
     self.exp_lr_scheduler = lr_scheduler.StepLR(self.optimizer_ft,
                                                 step_size=25,
                                                 gamma=0.1)
     self.model = learning.train_model(self.model,
                                       self.dataloaders,
                                       self.optimizer_ft,
                                       self.exp_lr_scheduler,
                                       num_epochs=10)
def fake_outfits():
    return {
        # '234': ['1', '2', '3'],
        # '235': ['1', '2', '4'],
        # '236': ['1', '5', '6'],
        # '334': ['7', '2', '3'],
        # '335': ['7', '2', '4'],
        # '336': ['7', '5', '6'],
        '431': ['1', '2', '6'],
        '432': ['8', '5', '3']
    }


if __name__ == "__main__":
    if False:
        df = generate_matrix(fake_ratings())
        model = train_model(df)
        print(make_item_prediction('123', '7', model))
        print(make_item_prediction('126', '1', model))
        make_outfit_prediction('123', fake_outfits(), model)
        make_outfit_prediction('126', fake_outfits(), model)
    if False:
        response = requests.get("localhost:4000/items?limit=10")
        print(response.json())
    if False:
        c = pymongo.MongoClient()
        db = c.cloutfits
        items = db.items
        for i in items.find():
            print(i['id'])
Example #3
0
    suffix = '/train'

    model = models.vgg16(pretrained=False)
    # modify the last Fc layer into size 2 for positive sample and negative sample
    model.classifier[6] = nn.Linear(
        in_features=4096, out_features=2,
        bias=True)  #### what does this "6" of classifier[6] ??????
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(),
                                lr=0.01,
                                weight_decay=0.0001)

    for epoch in range(100):
        # print('\nepoch %d:' % epoch)
        # train the model
        train_model(model, tr_data_loader, criterion, optimizer, gpu_id=1)
        if epoch % 1 == 0:
            print('\nepoch %d:' % epoch)
            # validate the trained model for every 1 epoch
            val = validate_model(model,
                                 tr_noaug_data_loader,
                                 criterion,
                                 epoch,
                                 prediction_folder='../results/train_data/',
                                 gpu_id=1)
            cvs_path = 'ep_' + str(epoch) + '_predictions.csv'
            arr = cvs_to_arr(cvs_file='../results/train_data/' + cvs_path)
            metrics = get_metrics(arr)
            print('train_accuracy', metrics[0])

            # validate the test model for every 1 epoch
    os.makedirs(model_dir+'/test_data/')
    os.makedirs(model_dir+'/val_data/')
    os.makedirs(model_dir+'/saved_models/')
    suffix = '/train'

    model = models.vgg16(pretrained=True)
    # modify the last Fc layer into size 2 for positive sample and negative sample
    model.classifier[6] = nn.Linear(in_features=4096, out_features=2,
                                    bias=True)  #### what does this "6" of classifier[6] ??????
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

    for epoch in range(100):
        # print('\nepoch %d:' % epoch)
        # train the model
        train_model(model, tr_data_loader, criterion, optimizer, gpu_id=DEVICE_ID)
        if epoch % 1 == 0:
            print('\nepoch %d:' % epoch)
            # validate the trained model for every 1 epoch
            val = validate_model(model, tr_noaug_data_loader, criterion, epoch,
                                 prediction_folder = model_dir+'/train_data/', gpu_id=DEVICE_ID)
            cvs_path = 'ep_' + str(epoch) + '_predictions.csv'
            arr = cvs_to_arr(cvs_file = model_dir+'/train_data/' + cvs_path)
            metrics = get_metrics(arr)
            print('train_accuracy', metrics[0])

            # validate the test model for every 1 epoch
            val = validate_model(model, ts_noaug_data_loader, criterion, epoch,
                                 prediction_folder = model_dir + '/test_data/', gpu_id=DEVICE_ID)
            cvs_path = 'ep_' + str(epoch) + '_predictions.csv'
            arr = cvs_to_arr(cvs_file = model_dir + '/test_data/' + cvs_path)
Example #5
0
                                                   num_workers=1)

test_im = 'data/test/'
test_img_database = datasets.ImageFolder(root=test_im, transform=transform)
# batch_size, num_workers = 1 always and shuffle=False. Bcz we are loading images individually from the folder
test_dataset_loader = torch.utils.data.DataLoader(test_img_database,
                                                  batch_size=1,
                                                  shuffle=False,
                                                  num_workers=1)
# Assign classes
classes = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

if __name__ == '__main__':

    print(net)
    train_model(train_dataset_loader, num_iteration, learn_rate, mom)

    correct = 0
    total = 0
    for j, data in enumerate(test_dataset_loader, 0):
        test_x = (data)[0]  # test image
        test_y = (data)[1]
        outputs = net(test_x)
        _, predicted = torch.max(outputs.data, 1)
        total += test_y.size(0)
        correct += (predicted == test_y).sum().item()

    print('Accuracy of the network on the 10 * 80 test images: %d %%' %
          (100 * correct / total))

    class_correct = list(0. for i in range(10))