Example #1
0
 def prepare_data(self):
     self.train_loader, self.valid_loader = make_train_loader(self.hparams)
Example #2
0
model = models.resnet50()
fc_features = model.fc.in_features
model.fc = nn.Linear(fc_features, 3)

valid_size = cfg.DATA.VALIDATION_SIZE
epochs = cfg.MODEL.EPOCH
lr = cfg.MODEL.LR
weight_path = cfg.MODEL.OUTPUT_PATH
use_cuda = cfg.DEVICE.CUDA
gpu_id = cfg.DEVICE.GPU

if use_cuda:
    torch.cuda.set_device(gpu_id)
    model = model.cuda()

train_loader, valid_loader = make_train_loader(cfg)

optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=0.0001)

for epoch in range(1, epochs + 1):
    model.train()
    train_loss = 0.
    valid_loss = 0.

    for data, target in train_loader:
        if use_cuda:
            data, target = data.cuda(), target.cuda()

        optimizer.zero_grad()
        output = model(data)
        loss = torch.nn.functional.cross_entropy(output, target)