Exemplo n.º 1
0
if __name__ == '__main__':

    # Hyper Parameter
    BATCH_SIZE = 18
    LR = 1e-3
    EPOCH = [10, 10]
    MOMENTUM = 0.9
    WEIGHT_DECAY = 5e-4
    loss_funct = nn.CrossEntropyLoss()

    x1, x2, xd, y1, y2, yd = 0, 10, 2, 72, 92, 2
    title = 'Result Comparison(ResNet'

    # load data
    train_data = dload('./data/', 'train')
    train_len = dload.__len__(train_data)
    test_data = dload('./data/', 'test')
    test_len = dload.__len__(test_data)

    train_loader = Data.DataLoader(
        dataset=train_data,
        batch_size=BATCH_SIZE,
        shuffle=False,
        num_workers=12,
    )
    test_loader = Data.DataLoader(
        dataset=test_data,
        batch_size=BATCH_SIZE,
        shuffle=False,
        num_workers=12,
    )
Exemplo n.º 2
0
    args = parser.parse_args()
    # parameter
    EPOCH = 10
    lr = args.lr
    BSIZE = args.bsize
    device = torch.device('cuda:0')
    name = f'{args.model}_{args.pretrain}_{args.bsize}'

    # load data
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.RandomHorizontalFlip(0.5),
        transforms.RandomVerticalFlip(p=0.5),
    ])
    train_dataloader = Data.DataLoader(RetinopathyLoader('data',
                                                         'train',
                                                         transform=transform),
                                       batch_size=BSIZE)
    test_dataloader = Data.DataLoader(RetinopathyLoader('data', 'test'),
                                      batch_size=BSIZE)

    # model
    loss_func = nn.CrossEntropyLoss()
    if not args.pretrain:
        if args.model == '18':
            model = ResNet(BasicBlock, [2, 2, 2, 2])
        else:
            model = ResNet(BottleNeck, [3, 4, 23, 3])
            EPOCH = 5
    else:
        model = torchvision.models.__dict__['resnet{}'.format(int(
Exemplo n.º 3
0
        print('Test acc {:.2f}%' .format(test_acc[-1]))

device = torch.cuda.current_device()
print('device:',device)

#Parameter
epochs=7
feat_ext_epochs=3
lr=5e-4
batch_size=12
momentum=0.9
criterion=nn.CrossEntropyLoss()
num_classes=5
weight_decay=5e-4

train_loader=RetinopathyLoader('data/','train')
trainset_size=len(train_loader)
train_loader=DataLoader(train_loader,batch_size=batch_size,shuffle=True)
test_loader=RetinopathyLoader('data/','test')
testset_size=len(test_loader)
test_loader=DataLoader(test_loader,batch_size=batch_size,shuffle=False)

if model_type=='18':
    model = models.resnet18(pretrained=args.pretrain)
else:
    model = models.resnet50(pretrained=args.pretrain)

if args.pretrain:
    for param in model.parameters():
        param.requires_grad=False
num_neurons = model.fc.in_features
Exemplo n.º 4
0
import torchvision.models
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import copy

# Hyperparameter setting
batch_size = 4
learning_rate = 1e-3
epochs_18 = 10
epochs_50 = 5
momentum = 0.9
weight_decay = 5e-4
criterion = nn.CrossEntropyLoss()

train_data = RetinopathyLoader(root='/home/ubuntu/Retinopathy_detection/data/',
                               mode='train')
test_data = RetinopathyLoader(root='/home/ubuntu/Retinopathy_detection/data/',
                              mode='test')
train_loader = DataLoader(train_data, batch_size=batch_size)
test_loader = DataLoader(test_data, batch_size=batch_size)

ResNet18_1 = torchvision.models.resnet18(pretrained=False)
ResNet18_1.fc = nn.Linear(512, 5)
ResNet18_2 = torchvision.models.resnet18(pretrained=True)
ResNet18_2.fc = nn.Linear(512, 5)
ResNet50_1 = torchvision.models.resnet50(pretrained=False)
ResNet50_1.fc = nn.Linear(2048, 5)
ResNet50_2 = torchvision.models.resnet50(pretrained=True)
ResNet50_2.fc = nn.Linear(2048, 5)

#models = [ResNet50_1, ResNet50_2]
Exemplo n.º 5
0
loss_func = nn.CrossEntropyLoss()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
data_root_path = './data/'

# load data
train_transform = transforms.Compose([
    transforms.RandomVerticalFlip(),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(90),
    transforms.ToTensor(),
])
test_transform = transforms.Compose([
    transforms.ToTensor(),
])

train_dataset = RetinopathyLoader(data_root_path, 'train', train_transform)
test_dataset = RetinopathyLoader(data_root_path, 'test', test_transform)

train_loader = DataLoader(
    dataset=train_dataset,
    batch_size=batch_size,
    shuffle=True,
)
test_loader = DataLoader(
    dataset=test_dataset,
    batch_size=batch_size,
)

# ResNet50 with pretraining
model = resnet50(pretrained=True)
model.to(device)
Exemplo n.º 6
0
def main(args):
    train_data = RetinopathyLoader('data', 'train')
    test_data = RetinopathyLoader('data', 'test')
    train_loader = Data.DataLoader(dataset=train_data,
                                   batch_size=args.batch,
                                   shuffle=True)
    net, optimizer, loss_function = handle_param(args)
    full_step = ((train_data.data_size // args.batch) +\
                ((train_data.data_size % args.batch)>0)) * args.epochs

    train_tag = '_untrained' if args.untrained else '_pretrained'
    file_name = args.model + train_tag + '_' +\
                args.optimizer + '_ep' + str(args.epochs) + '_b' + str(args.batch) +\
                '_lr' + str(args.learning_rate) + '_wd' + str(args.weight_decay)
    if args.load:
        net.load_state_dict(torch.load(args.load))
        test_acc, pred, gt = test_accuracy(net, test_data)

        with open(args.model + train_tag + '.json', 'w') as f:
            json.dump({
                'pred_y': pred,
                'gt': gt,
                'class': list(range(5)),
            }, f)
        print('test_acc: {:.4f} %'.format(test_acc))
        return
    # start training
    max_acc = 0
    count = 0
    acc_dict = {'train' + train_tag: [], 'test' + train_tag: []}

    print(
        datetime.now(timezone(timedelta(hours=8))).strftime("%m-%d %H:%M"),
        'start training...')
    for epoch in range(args.epochs):
        print('-' * 10, 'epoch', epoch + 1, '-' * 10)
        y_list, gt_list = [], []
        for b_x, b_y in train_loader:
            count += 1
            b_x = b_x.type(torch.FloatTensor).to(device)
            b_y = b_y.to(device)
            output = net(b_x)
            loss = loss_function(output, b_y.long())
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            y_list.extend(torch.max(output, 1)[1].data.tolist())
            gt_list.extend(b_y.data.tolist())
            if count % args.step == 0:
                print(datetime.now().strftime("%m-%d %H:%M"))
                print('({} / {}) loss: {:.4f} | train_acc: {:.2f} %'.format(
                    count, full_step, loss, train_accuracy(y_list, gt_list)))

        test_acc, _, _ = test_accuracy(net, test_data)
        if test_acc > max_acc:
            max_acc = test_acc
            torch.save(net.state_dict(), file_name + '.pkl')
        acc_dict['train'].append(train_accuracy(y_list, gt_list))
        acc_dict['test'].append(test_acc)
        print(datetime.now().strftime("%m-%d %H:%M"))
        print('({} / {}) train_acc: {:.2f} % | test_acc: {:.2f} %'.format(
            count, full_step, train_accuracy(y_list, gt_list), test_acc))
        with open(file_name + '.json', 'w') as f:
            json.dump(
                {
                    'x': list(range(args.epochs)),
                    'y_dict': acc_dict,
                    'title': args.model + train_tag,
                }, f)
    print('[*] max test accuracy: {} %'.format(max_acc))
Exemplo n.º 7
0
CUDA = True if torch.cuda.is_available() else False

LOAD = True
CONT = False
EPOCH = 10
Model = './weight/pre_resnet50_82021.pkl'

from utils import progress_bar
import torchvision.models as models

from confusion import plot_confusion_matrix

from dataloader import RetinopathyLoader
import torch.utils.data as utils

train_dataset = RetinopathyLoader('./data/', 'train')
test_dataset = RetinopathyLoader('./data/', 'test')

trainloader = utils.DataLoader(train_dataset,
                               batch_size=8,
                               shuffle=True,
                               num_workers=2)
testloader = utils.DataLoader(test_dataset,
                              batch_size=1,
                              shuffle=False,
                              num_workers=2)


class ResNet(nn.Module):
    def __init__(self, pretrained=True):
        super(ResNet, self).__init__()
Exemplo n.º 8
0
    disp = ConfusionMatrixDisplay(confusion_matrix(y_true, y_pred),
                                  display_labels=[0, 1, 2, 3, 4])
    disp.plot()
    plt.savefig(f'./{name}_Matrix.png')
    plt.clf()


if __name__ == '__main__':

    device = torch.device('cuda:0')

    test_transform = torchvision.transforms.Compose([
        torchvision.transforms.ToTensor(),
    ])
    test_dataset = RetinopathyLoader('./data/', 'test', test_transform)
    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=8,
                                              shuffle=True,
                                              num_workers=4)

    for _model in ['ResNet18', 'ResNet50']:
        for _pre in ['with pretraining', 'without pretraining']:
            if _model == 'ResNet18':
                model = torchvision.models.resnet18()
            else:
                model = torchvision.models.resnet50()
            in_features = model.fc.in_features
            model.fc = torch.nn.Linear(in_features, 5)
            model = model.to(device)
            model.load_state_dict(
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            if i % 500 == 0:
                print(str(i) + "th batch")

        data.Cancel_stuff()
        if epoch == 9:
            Train_acc, Test_acc = test_model(net, "last")
        else:
            Train_acc, Test_acc = test_model(net, "notlast")

        print("Epoch: " + str(epoch + 1) + ", Train Accuracy = " +
              str(Train_acc) + ", Test Accuracy = " + str(Test_acc))
        print(output_dict)


all_train_img = RetinopathyLoader('data/', 'train')
all_test_img = RetinopathyLoader('data/', 'test')
net = resnet18.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),
                      lr=0.001,
                      momentum=0.9,
                      weight_decay=5e-4)
#optimizer = optim.Adam(net.parameters(), lr=0.0005, weight_decay=5e-4)
batch_size = 4

train_model(net, all_train_img)
    #plt.show()
    filename = title + "_ConfusionMatrix"
    plt.savefig(filename + ".png")


if __name__ == '__main__':
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    augmentation = [
        transforms.RandomHorizontalFlip(p=0.5),
        transforms.RandomVerticalFlip(p=0.5),
    ]

    # Train with data augmentation
    train_dataset = RetinopathyLoader('data',
                                      'train',
                                      augmentation=augmentation)
    test_dataset = RetinopathyLoader('data', 'test')

    # ResNet18
    models = {
        "ResNet18": ResNet(BasicBlock, [2, 2, 2, 2]).to(device),
        "ResNet18_Pretrained": PretrainResNet(5, 18).to(device)
    }

    ResNet18Acc = trainAndTestModels(train_dataset,
                                     test_dataset,
                                     models,
                                     epochs=10,
                                     batch_size=16,
                                     learning_rate=1e-3)