示例#1
0
def main(config):

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    train_transform = transforms.Compose([
        transforms.Scale(256),
        transforms.RandomCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor()
    ])

    val_transform = transforms.Compose([
        transforms.Scale(256),
        transforms.RandomCrop(224),
        transforms.ToTensor()
    ])

    trainset = AVADataset(csv_file=config.train_csv_file,
                          root_dir=config.train_img_path,
                          transform=train_transform)
    valset = AVADataset(csv_file=config.val_csv_file,
                        root_dir=config.val_img_path,
                        transform=val_transform)

    train_loader = torch.utils.data.DataLoader(
        trainset,
        batch_size=config.train_batch_size,
        shuffle=True,
        num_workers=config.num_workers)
    val_loader = torch.utils.data.DataLoader(valset,
                                             batch_size=config.val_batch_size,
                                             shuffle=False,
                                             num_workers=config.num_workers)

    base_model = models.vgg16(pretrained=True)
    model = NIMA(base_model)

    if config.warm_start:
        model.load_state_dict(
            torch.load(
                os.path.join(config.ckpt_path,
                             'epoch-%d.pkl' % config.warm_start_epoch)))
        print('Successfully loaded model epoch-%d.pkl' %
              config.warm_start_epoch)

    if config.multi_gpu:
        model.features = torch.nn.DataParallel(model.features,
                                               device_ids=config.gpu_ids)
        model = model.to(device)
    else:
        model = model.to(device)

    conv_base_lr = config.conv_base_lr
    dense_lr = config.dense_lr
    optimizer = optim.SGD([{
        'params': model.features.parameters(),
        'lr': conv_base_lr
    }, {
        'params': model.classifier.parameters(),
        'lr': dense_lr
    }],
                          momentum=0.9)

    # send hyperparams
    lrs.send({
        'title': 'EMD Loss',
        'train_batch_size': config.train_batch_size,
        'val_batch_size': config.val_batch_size,
        'optimizer': 'SGD',
        'conv_base_lr': config.conv_base_lr,
        'dense_lr': config.dense_lr,
        'momentum': 0.9
    })

    param_num = 0
    for param in model.parameters():
        param_num += int(np.prod(param.shape))
    print('Trainable params: %.2f million' % (param_num / 1e6))

    if config.train:
        # for early stopping
        count = 0
        init_val_loss = float('inf')
        train_losses = []
        val_losses = []
        for epoch in range(config.warm_start_epoch, config.epochs):
            lrs.send('epoch', epoch)
            batch_losses = []
            for i, data in enumerate(train_loader):
                images = data['image'].to(device)
                labels = data['annotations'].to(device).float()
                outputs = model(images)
                outputs = outputs.view(-1, 10, 1)

                optimizer.zero_grad()

                loss = emd_loss(labels, outputs)
                batch_losses.append(loss.item())

                loss.backward()

                optimizer.step()

                lrs.send('train_emd_loss', loss.item())

                print('Epoch: %d/%d | Step: %d/%d | Training EMD loss: %.4f' %
                      (epoch + 1, config.epochs, i + 1, len(trainset) //
                       config.train_batch_size + 1, loss.data[0]))

            avg_loss = sum(batch_losses) / (
                len(trainset) // config.train_batch_size + 1)
            train_losses.append(avg_loss)
            print('Epoch %d averaged training EMD loss: %.4f' %
                  (epoch + 1, avg_loss))

            # exponetial learning rate decay
            if (epoch + 1) % 10 == 0:
                conv_base_lr = conv_base_lr * config.lr_decay_rate**(
                    (epoch + 1) / config.lr_decay_freq)
                dense_lr = dense_lr * config.lr_decay_rate**(
                    (epoch + 1) / config.lr_decay_freq)
                optimizer = optim.SGD([{
                    'params': model.features.parameters(),
                    'lr': conv_base_lr
                }, {
                    'params': model.classifier.parameters(),
                    'lr': dense_lr
                }],
                                      momentum=0.9)

                # send decay hyperparams
                lrs.send({
                    'lr_decay_rate': config.lr_decay_rate,
                    'lr_decay_freq': config.lr_decay_freq,
                    'conv_base_lr': config.conv_base_lr,
                    'dense_lr': config.dense_lr
                })

            # do validation after each epoch
            batch_val_losses = []
            for data in val_loader:
                images = data['image'].to(device)
                labels = data['annotations'].to(device).float()
                with torch.no_grad():
                    outputs = model(images)
                outputs = outputs.view(-1, 10, 1)
                val_loss = emd_loss(labels, outputs)
                batch_val_losses.append(val_loss.item())
            avg_val_loss = sum(batch_val_losses) / (
                len(valset) // config.val_batch_size + 1)
            val_losses.append(avg_val_loss)

            lrs.send('val_emd_loss', avg_val_loss)

            print('Epoch %d completed. Averaged EMD loss on val set: %.4f.' %
                  (epoch + 1, avg_val_loss))

            # Use early stopping to monitor training
            if avg_val_loss < init_val_loss:
                init_val_loss = avg_val_loss
                # save model weights if val loss decreases
                print('Saving model...')
                torch.save(
                    model.state_dict(),
                    os.path.join(config.ckpt_path,
                                 'epoch-%d.pkl' % (epoch + 1)))
                print('Done.\n')
                # reset count
                count = 0
            elif avg_val_loss >= init_val_loss:
                count += 1
                if count == config.early_stopping_patience:
                    print(
                        'Val EMD loss has not decreased in %d epochs. Training terminated.'
                        % config.early_stopping_patience)
                    break

        print('Training completed.')

        if config.save_fig:
            # plot train and val loss
            epochs = range(1, epoch + 2)
            plt.plot(epochs, train_losses, 'b-', label='train loss')
            plt.plot(epochs, val_losses, 'g-', label='val loss')
            plt.title('EMD loss')
            plt.legend()
            plt.savefig('./loss.png')

    if config.test:
        model.eval()
        # compute mean score
        test_transform = val_transform
        testset = AVADataset(csv_file=config.test_csv_file,
                             root_dir=config.test_img_path,
                             transform=val_transform)
        test_loader = torch.utils.data.DataLoader(
            testset,
            batch_size=config.test_batch_size,
            shuffle=False,
            num_workers=config.num_workers)

        mean_preds = []
        std_preds = []
        for data in test_loader:
            image = data['image'].to(device)
            output = model(image)
            output = output.view(10, 1)
            predicted_mean, predicted_std = 0.0, 0.0
            for i, elem in enumerate(output, 1):
                predicted_mean += i * elem
            for j, elem in enumerate(output, 1):
                predicted_std += elem * (i - predicted_mean)**2
            mean_preds.append(predicted_mean)
            std_preds.append(predicted_std)
示例#2
0
    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res


model = my_model.sanity_model(100)
mlp = my_model.mlp()

dataset = dsets.CIFAR10(root=opt.dataroot,
                        train=True,
                        download=False,
                        transform=transforms.Compose([
                            transforms.Scale(opt.imageSize),
                            transforms.ToTensor(),
                            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                                 (0.2023, 0.1994, 0.2010)),
                        ]))
assert dataset

dataset_val = dsets.CIFAR10(root=opt.dataroot,
                            train=False,
                            download=False,
                            transform=transforms.Compose([
                                transforms.Scale(opt.imageSize),
                                transforms.ToTensor(),
                                transforms.Normalize((0.4914, 0.4822, 0.4465),
                                                     (0.2023, 0.1994, 0.2010)),
                            ]))
示例#3
0
def main():
    global args, best_prec1
    args = parser.parse_args()
    print args
    # create model
    print("=> creating model '{}'".format(args.arch))
    if args.arch.lower().startswith('wideresnet'):
        # a customized resnet model with last feature map size as 14x14 for better class activation mapping
        model = wideresnet.resnet50(num_classes=args.num_classes)
    else:
        model = models.__dict__[args.arch](num_classes=args.num_classes)

    if args.arch.lower().startswith('alexnet') or args.arch.lower().startswith(
            'vgg'):
        model.features = torch.nn.DataParallel(model.features)
        model.cuda()
    else:
        model = torch.nn.DataParallel(model).cuda()
    print model
    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            model.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    # Data loading code
    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    train_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomSizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=args.workers,
                                               pin_memory=True)

    val_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        valdir,
        transforms.Compose([
            transforms.Scale(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            normalize,
        ])),
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    # define loss function (criterion) and pptimizer
    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.SGD(model.parameters(),
                                args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    if args.evaluate:
        validate(val_loader, model, criterion)
        return

    for epoch in range(args.start_epoch, args.epochs):
        adjust_learning_rate(optimizer, epoch)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch)

        # evaluate on validation set
        prec1 = validate(val_loader, model, criterion)

        # remember best prec@1 and save checkpoint
        is_best = prec1 > best_prec1
        best_prec1 = max(prec1, best_prec1)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'arch': args.arch,
                'state_dict': model.state_dict(),
                'best_prec1': best_prec1,
            }, is_best, args.arch.lower())
示例#4
0
def main():
    global args, best_err1
    args = parser.parse_args()
    if args.tensorboard: configure("runs_WHOI/%s"%(args.expname))
    
    # Data loading code
    normalize = transforms.Normalize(mean=[x/255.0 for x in [125.3, 123.0, 113.9]],
                                     std=[x/255.0 for x in [63.0, 62.1, 66.7]])
    # normalize = transforms.Normalize(mean=[x/255.0 for x in [125.3]],
                                     # std=[x/255.0 for x in [63.0]])
    
    transform_test = transforms.Compose([
        transforms.Scale(36),
        transforms.CenterCrop(32),
        transforms.ToTensor(),
        normalize
        ])

    kwargs = {'num_workers': 1, 'pin_memory': True}

    numberofclass = 89  # changed to 89
 
    if args.dataset == 'WHOI':
        val_loader = torch.utils.data.DataLoader(
            datasets.ImageFolder('/media/ouc/30bd7817-d3a1-4e83-b7d9-5c0e373ae434/LiuJing/WHOI-MVCO/TestSet', transform=transform_test),
            batch_size=args.batchsize, shuffle=True, **kwargs)
	numberofclass = 89 
    else: 
        raise Exception ('unknown dataset: {}'.format(args.dataset)) 

    print('Training PyramidNet-{} on {} dataset:'.format(args.depth, args.dataset.upper()))
    
    # create model
    # model = RN.ResNet(args.depth, 10, bottleneck=args.bottleneck) # for ResNet
    model = PYRM.PyramidNet(args.depth, args.alpha, numberofclass, bottleneck=args.bottleneck) # for PyramidNet

    # get the number of model parameters
    
    print('Number of model parameters: {}'.format(sum([p.data.nelement() for p in model.parameters()])))
    
    # for training on multiple GPUs. 
    # Use CUDA_VISIBLE_DEVICES=0,1 to specify which GPUs to use
    #model = torch.nn.DataParallel(model).cuda()
    model = model.cuda()

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_err1 = checkpoint['best_err1']
            model.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    print(model)

    cudnn.benchmark = True

    for epoch in range(args.start_epoch, args.epochs):

        # evaluate on validation set
        err1 = validate(val_loader, model, criterion, epoch)
        confusion_matrix = (val_loader, model, criterion, epoch)
        # remember best err1 and save checkpoint
        is_best = err1 <= best_err1
        best_err1 = min(err1, best_err1)
        print 'Current best accuracy (error):', best_err1
        save_checkpoint({
            'epoch': epoch + 1,
            'state_dict': model.state_dict(),
            'best_err1': best_err1,
        }, is_best)
    print 'Best accuracy (error): ', best_err1
示例#5
0
        torch.cuda.manual_seed_all(args.manualSeed)

    now = datetime.datetime.now(dateutil.tz.tzlocal())
    timestamp = now.strftime('%Y_%m_%d_%H_%M_%S')
    output_dir = '../output/%s_%s_%s' % \
        (cfg.DATASET_NAME, cfg.CONFIG_NAME, timestamp)

    split_dir, bshuffle = 'train', True
    if not cfg.TRAIN.FLAG:
        # bshuffle = False
        split_dir = 'test'

    # Get data loader
    imsize = cfg.TREE.BASE_SIZE * (2**(cfg.TREE.BRANCH_NUM - 1))
    image_transform = transforms.Compose([
        transforms.Scale(int(imsize * 76 / 64)),
        transforms.RandomCrop(imsize),
        transforms.RandomHorizontalFlip()
    ])
    dataset = TextDataset(cfg.DATA_DIR,
                          split_dir,
                          base_size=cfg.TREE.BASE_SIZE,
                          transform=image_transform)
    assert dataset
    dataloader = torch.utils.data.DataLoader(dataset,
                                             batch_size=cfg.TRAIN.BATCH_SIZE,
                                             drop_last=True,
                                             shuffle=bshuffle,
                                             num_workers=int(cfg.WORKERS))

    # Define models and go to train/evaluate
示例#6
0
        self.conv2 = nn.Conv2d(16, 32, kernel_size=5, stride=2)
        self.bn2 = nn.BatchNorm2d(32)
        self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2)
        self.bn3 = nn.BatchNorm2d(32)
        self.head = nn.Linear(448, 2)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.relu(self.bn2(self.conv2(x)))
        x = F.relu(self.bn3(self.conv3(x)))
        return self.head(x.view(x.size(0), -1))


resize = T.Compose(
    [T.ToPILImage(),
     T.Scale(40, interpolation=Image.CUBIC),
     T.ToTensor()])

# This is based on the code from gym.
screen_width = 600


def get_cart_location():
    world_width = env.x_threshold * 2
    scale = screen_width / world_width
    return int(env.state[0] * scale + screen_width / 2.0)  # MIDDLE OF CART


def get_screen():
    screen = env.render(mode='rgb_array').transpose(
        (2, 0, 1))  # transpose into torch order (CHW)
示例#7
0
#    model = torch.load(model_obj)
#else:
#    model = torch.load(model_obj, map_location=lambda storage, loc: storage) # model trained in GPU could be deployed in CPU machine like this!

## if you encounter the UnicodeDecodeError when use python3 to load the model, add the following line will fix it. Thanks to @soravux
# from functools import partial
# import pickle
# pickle.load = partial(pickle.load, encoding="latin1")
# pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
# model = torch.load(model_file, map_location=lambda storage, loc: storage, pickle_module=pickle)

model.eval()

# load the image transformer
centre_crop = trn.Compose([
    trn.Scale(256),
    trn.CenterCrop(224),
    trn.ToTensor(),
    trn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

# load the class label

#if not os.access(file_name, os.W_OK):
#    synset_url = 'https://raw.githubusercontent.com/csailvision/places365/master/categories_places365.txt'
#    os.system('wget ' + synset_url)
#classes = list()
#for a in range(0,80):
#	classes.append(str(a))
#classes = tuple(classes)
示例#8
0
def main():
    global args, best_prec1
    args = parser.parse_args()

    # create model
    if args.pretrained:
        print("=> using pre-trained model '{}'".format(args.arch))
        #        if args.arch == 'resnet50':
        #            import resnet_model
        #            model = resnet_model.resnet50_new(pretrained=True)
        #            print('save resnet50 to resnet50.weights')
        #            model.saveas_darknet_weights('resnet50.weights')
        if args.arch == 'resnet50-pytorch':
            model = models.resnet50(pretrained=True)
        elif args.arch == 'resnet50-darknet':
            from darknet import Darknet
            model = Darknet('cfg/resnet50.cfg')
            print('load weights from resnet50.weights')
            model.load_weights('resnet50.weights')
        elif args.arch == 'resnet50-caffe2darknet':
            from darknet import Darknet
            model = Darknet('resnet50-caffe2darknet.cfg')
            print('load weights from resnet50-caffe2darknet.weights')
            model.load_weights('resnet50-caffe2darknet.weights')
        elif args.arch == 'vgg16-pytorch2darknet':
            from darknet import Darknet
            model = Darknet('vgg16-pytorch2darknet.cfg')
            print('load weights from vgg16-pytorch2darknet.weights')
            model.load_weights('vgg16-pytorch2darknet.weights')
        elif args.arch == 'resnet50-pytorch2caffe':
            from caffenet import CaffeNet
            model = CaffeNet('resnet50-pytorch2caffe.prototxt')
            print('load weights resnet50-pytorch2caffe.caffemodel')
            model.load_weights('resnet50-pytorch2caffe.caffemodel')
        elif args.arch == 'resnet50-pytorch2caffe.nobn':
            from caffenet import CaffeNet
            model = CaffeNet('resnet50-pytorch2caffe.nobn.prototxt')
            print('load weights resnet50-pytorch2caffe.nobn.caffemodel')
            model.load_weights('resnet50-pytorch2caffe.nobn.caffemodel')
        elif args.arch == 'resnet50-darknet2caffe':
            from caffenet import CaffeNet
            model = CaffeNet('resnet50-darknet2caffe.prototxt')
            print('load weights resnet50-darknet2caffe.caffemodel')
            model.load_weights('resnet50-darknet2caffe.caffemodel')
        elif args.arch == 'resnet50-kaiming':
            from caffenet import CaffeNet
            model = CaffeNet('ResNet-50-deploy.prototxt')
            print('load weights from ResNet-50-model.caffemodel')
            model.load_weights('ResNet-50-model.caffemodel')
        elif args.arch == 'resnet50-kaiming-dk':
            from darknet import Darknet
            model = Darknet('ResNet-50-model.cfg')
            print('load weights from ResNet-50-model.weights')
            model.load_weights('ResNet-50-model.weights')
        elif args.arch == 'resnet18-caffe':
            from caffenet import CaffeNet
            model = CaffeNet('cfg/resnet-18.prototxt')
            print('load weights from resnet-18.caffemodel')
            model.load_weights('resnet-18.caffemodel')
        elif args.arch == 'resnet18-darknet':
            from darknet import Darknet
            model = Darknet('resnet-18.cfg')
            print('load weights from resnet-18.weights')
            model.load_weights('resnet-18.weights')
        elif args.arch == 'resnet50-test':
            from darknet import Darknet
            model = Darknet('test/ResNet-50-model.cfg')
            print('load weights from test/ResNet-50-model.weights')
            model.load_weights('test/ResNet-50-model.weights')
        else:
            model = models.__dict__[args.arch](pretrained=True)
    else:
        print("=> creating model '{}'".format(args.arch))
        if args.arch.startswith('mobilenet'):
            model = Net()
            print(model)
        else:
            model = models.__dict__[args.arch]()

    if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
        if args.arch != 'vgg16-pytorch2darknet':
            model.features = torch.nn.DataParallel(model.features)
            model.cuda()
        else:
            model = torch.nn.DataParallel(model).cuda()
    else:
        model = torch.nn.DataParallel(model).cuda()

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.SGD(model.parameters(),
                                args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    # Data loading code
    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')
    if args.arch == 'resnet50-test' or args.arch == 'resnet50-kaiming' or args.arch == 'resnet50-kaiming-dk':
        normalize = transforms.Normalize(mean=[0.0, 0.0, 0.0],
                                         std=[1.0, 1.0, 1.0])
    elif args.arch == 'resnet18-darknet' or args.arch == 'resnet18-caffe':
        normalize = transforms.Normalize(
            mean=[104 / 255.0, 117 / 255.0, 123 / 255.0], std=[1.0, 1.0, 1.0])
    else:
        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])

    train_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomSizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=args.workers,
                                               pin_memory=True)

    val_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        valdir,
        transforms.Compose([
            transforms.Scale(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            normalize,
        ])),
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    if args.evaluate:
        validate(val_loader, model, criterion)
        return

    for epoch in range(args.start_epoch, args.epochs):
        adjust_learning_rate(optimizer, epoch)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch)

        # evaluate on validation set
        prec1 = validate(val_loader, model, criterion)

        # remember best prec@1 and save checkpoint
        is_best = prec1 > best_prec1
        best_prec1 = max(prec1, best_prec1)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'arch': args.arch,
                'state_dict': model.state_dict(),
                'best_prec1': best_prec1,
                'optimizer': optimizer.state_dict(),
            }, is_best)
示例#9
0
                        help='learning rate (default: 1e-3)')
    parser.add_argument(
        '--log_interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging training status')
    parser.add_argument('--cuda',
                        action='store_true',
                        default=False,
                        help='enables CUDA training')
    args = parser.parse_args()
    args.cuda = args.cuda and torch.cuda.is_available()

    preprocess_data = transforms.Compose([
        transforms.Scale(64),
        transforms.CenterCrop(64),
        transforms.ToTensor()
    ])

    # create loaders for CelebA
    train_loader = torch.utils.data.DataLoader(datasets.CelebAttributes(
        partition='train', image_transform=preprocess_data),
                                               batch_size=args.batch_size,
                                               shuffle=True)
    test_loader = torch.utils.data.DataLoader(datasets.CelebAttributes(
        partition='val', image_transform=preprocess_data),
                                              batch_size=args.batch_size,
                                              shuffle=True)

    # load multimodal VAE
示例#10
0
    def __init__(self,
                 dataset_name,
                 validation=True,
                 verbose=True,
                 imgSize=32,
                 batchsize=128):
        # dataset_name: name of data set ('bsds'(color) or 'bsds_gray')
        # validation: [True]  model train/validation mode
        #             [False] model test mode for final evaluation of the evolved model
        #                     (raining data : all training data, test data : all test data)
        # verbose: flag of display
        self.verbose = verbose
        self.imgSize = imgSize
        self.validation = validation
        self.batchsize = batchsize
        self.dataset_name = dataset_name

        # load dataset
        if dataset_name == 'cifar10' or dataset_name == 'mnist' or dataset_name == 'coronahack':
            if dataset_name == 'coronahack':
                self.n_class = 2
                self.channel = 3
                if self.validation:
                    self.dataloader, self.test_dataloader = get_train_valid_loader(
                        data_dir='./data/',
                        batch_size=self.batchsize,
                        augment=True,
                        random_seed=2018,
                        num_workers=1,
                        pin_memory=True)
                    # self.dataloader, self.test_dataloader = loaders[0], loaders[1]
                else:
                    train_dataset = dset.CIFAR10(
                        root='./',
                        train=True,
                        download=True,
                        transform=transforms.Compose([
                            transforms.RandomHorizontalFlip(),
                            transforms.Scale(self.imgSize),
                            transforms.ToTensor(),
                            transforms.Normalize((0.485, 0.456, 0.406),
                                                 (0.229, 0.224, 0.225)),
                        ]))
                    test_dataset = dset.CIFAR10(
                        root='./',
                        train=False,
                        download=True,
                        transform=transforms.Compose([
                            transforms.Scale(self.imgSize),
                            transforms.ToTensor(),
                            transforms.Normalize((0.485, 0.456, 0.406),
                                                 (0.229, 0.224, 0.225)),
                        ]))
                    self.dataloader = torch.utils.data.DataLoader(
                        train_dataset,
                        batch_size=self.batchsize,
                        shuffle=True,
                        num_workers=int(2))
                    self.test_dataloader = torch.utils.data.DataLoader(
                        test_dataset,
                        batch_size=self.batchsize,
                        shuffle=True,
                        num_workers=int(2))
            print('train num    ', len(self.dataloader.dataset))
            print('test num     ', len(self.test_dataloader.dataset))
        else:
            print('\tInvalid input dataset name at CNN_train()')
            exit(1)
示例#11
0
def main():
    start_time = time()

    in_args = get_input_args()

    use_gpu = torch.cuda.is_available() and in_args.gpu

    print("Training on {} using {}".format("GPU" if use_gpu else "CPU",
                                           in_args.arch))

    print("Learning rate:{}, Hidden Units:{}, Epochs:{}".format(
        in_args.learning_rate, in_args.hidden_units, in_args.epochs))

    if not os.path.exists(in_args.save_dir):
        os.makedirs(in_args.save_dir)

    training_dir = in_args.data_dir + '/train'
    validation_dir = in_args.data_dir + '/valid'
    testing_dir = in_args.data_dir + '/test'

    data_transforms = {
        'training':
        transforms.Compose([
            transforms.RandomRotation(30),
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'validation':
        transforms.Compose([
            transforms.Scale(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'testing':
        transforms.Compose([
            transforms.Scale(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])
    }

    dirs = {
        'training': training_dir,
        'validation': validation_dir,
        'testing': testing_dir
    }

    image_datasets = {
        x: datasets.ImageFolder(dirs[x], transform=data_transforms[x])
        for x in ['training', 'validation', 'testing']
    }

    dataloaders = {
        x: torch.utils.data.DataLoader(image_datasets[x],
                                       batch_size=64,
                                       shuffle=True)
        for x in ['training', 'validation', 'testing']
    }

    model, optimizer, criterion = model_helper.create_model(
        in_args.arch, in_args.hidden_units, in_args.learning_rate,
        image_datasets['training'].class_to_idx)

    if use_gpu:
        model.cuda()
        criterion.cuda()

    model_helper.train(model, criterion, optimizer, in_args.epochs,
                       dataloaders['training'], dataloaders['validation'],
                       use_gpu)

    file_path = in_args.save_dir + '/' + in_args.arch + \
        '_epoch' + str(in_args.epochs) + '.pth'

    model_helper.save_checkpoint(file_path, model, optimizer, in_args.arch,
                                 in_args.hidden_units, in_args.epochs)

    test_loss, accuracy = model_helper.validate(model, criterion,
                                                dataloaders['testing'],
                                                use_gpu)
    print("Post load Validation Accuracy: {:.3f}".format(accuracy))
    image_path = 'flowers/test/28/image_05230.jpg'
    print("Predication for: {}".format(image_path))
    probs, classes = model_helper.predict(image_path, model, use_gpu)
    print(probs)
    print(classes)

    end_time = time()
    utility.print_elapsed_time(end_time - start_time)
示例#12
0
def main():
    # Get Config
    args = parseConfig("Tanuj's ConvNet Model")
    config = Config(args)
    config.log(config)

    # Transformations
    size = 112
    transformations = transforms.Compose([
        transforms.Scale(size + 5),
        transforms.RandomCrop(size),
        transforms.RandomHorizontalFlip(),
        transforms.Lambda(lambda x: randomTranspose(np.array(x))),
        transforms.Lambda(lambda x: np.array(x)[:, :, :3]),
        transforms.ToTensor(),
    ])

    # Datasets
    train_dataset = NaiveDataset(TRAIN_DATA_PATH,
                                 TRAIN_LABELS_PATH,
                                 num_examples=NUM_TRAIN,
                                 transforms=transformations)
    train_idx, val_idx = splitIndices(train_dataset, config, shuffle=True)

    weights = UpsamplingWeights(train_dataset)

    train_sampler = WeightedRandomSampler(weights=weights[train_idx],
                                          replacement=True,
                                          num_samples=config.num_train)
    val_sampler = SubsetRandomSampler(val_idx)

    # Loaders
    train_loader = DataLoader(train_dataset,
                              batch_size=config.batch_size,
                              num_workers=4,
                              sampler=train_sampler)
    val_loader = DataLoader(train_dataset,
                            batch_size=config.batch_size,
                            num_workers=1,
                            sampler=val_sampler)

    config.train_loader = train_loader
    config.val_loader = val_loader

    #get test data
    test_dataset = NaiveDataset(TEST_DATA_PATH, TEST_LABELS_PATH)
    test_loader = DataLoader(test_dataset,
                             batch_size=config.batch_size,
                             shuffle=False,
                             num_workers=2)

    # Create Model
    model = createModel(config)

    # Init Weights
    model.apply(initialize_weights)

    # Train and Eval Model
    results = train(model, config, lr_decay=0.0001, weight_decay=0.0005)
    visualize.plot_results(results, config)

    make_predictions = False
    if make_predictions:
        predict(model, config, test_loader, dataset="test")
示例#13
0
def train(args):
    ############################################
    # Setup Dataloader
    data_path = get_data_path(args.dataset)
    dataset = dset.ImageFolder(root=data_path,
                               transform=transforms.Compose([
                                   transforms.Scale(2*args.img_rows),
                                   transforms.RandomCrop(args.img_rows),
                                   transforms.RandomHorizontalFlip(),
                                   transforms.ToTensor(),
                                   transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                               ]))

    n_classes = len(dataset.classes)
    dataloader = data.DataLoader(dataset, batch_size=args.batch_size, num_workers=4, shuffle=True)
    ############################################

    ######## pretrained = FALSE ===>>> NO PRE-TRAINING ########
    ############################################
    # Setup Model
    """
    model = models.alexnet(pretrained=False)
    block = list(model.classifier.children())
    old_layer = block.pop()
    num_feats = old_layer.in_features
    block.append(nn.Linear(num_feats, n_classes))
    model.classifier = nn.Sequential(*block)
    """
    model = AlexNet(num_classes=n_classes)
    ############################################

    ############################################
    # Random weight initialization
    model.apply(weights_init)
    ############################################

    ############################################
    # If resuming the training from a saved model
    if args.model_path != '':
        print('\n' + '-' * 40)
        model = torch.load(args.model_path)
        print('Restored the trained network {}'.format(args.model_path))
    ############################################


    ############################################
    criterion = nn.CrossEntropyLoss() # Loss criterion
    # Porting the networks to CUDA
    if torch.cuda.is_available():
        model.cuda(args.gpu)
        criterion.cuda(args.gpu)
    ############################################

    ############################################
    # Defining the optimizer over the network parameters
    optimizerSS = torch.optim.SGD([{'params': model.features.parameters()},
                                        {'params': model.classifier.parameters(), 'lr':1*args.l_rate}],
                                  lr=args.l_rate, momentum=0.9, weight_decay=5e-4)
    optimizerSS_init = copy.deepcopy(optimizerSS)
    ############################################

    ############################################
    # TRAINING:
    for epoch in range(args.n_epoch):
        for i, (images, labels) in enumerate(dataloader):
            ######################
            # Porting the data to Autograd variables and CUDA (if available)
            if torch.cuda.is_available():
                images = Variable(images.cuda(args.gpu))
                labels = Variable(labels.cuda(args.gpu))
            else:
                images = Variable(images)
                labels = Variable(labels)

            ######################
            # Scheduling the learning rate
            #adjust_learning_rate(optimizerSS, args.l_rate, epoch)
            adjust_learning_rate_v2(optimizerSS, optimizerSS_init, epoch, step=20)

            ######################
            # Setting the gradients to zero at each iteration
            optimizerSS.zero_grad()
            model.zero_grad()

            ######################
            # Passing the data through the network
            outputs = model(images)

            ######################
            # Computing the loss and doing back-propagation
            loss = criterion(outputs, labels)
            loss.backward()

            ######################
            # Updating the parameters
            optimizerSS.step()

            if (i+1) % 20 == 0:
                print("Iter [%d/%d], Epoch [%d/%d] Loss: %.4f" % (i+1, len(dataloader), epoch+1, args.n_epoch, loss.data[0]))

        # test_output = model(test_image)
        # predicted = loader.decode_segmap(test_output[0].cpu().data.numpy().argmax(0))
        # target = loader.decode_segmap(test_segmap.numpy())

        # vis.image(test_image[0].cpu().data.numpy(), opts=dict(title='Input' + str(epoch)))
        # vis.image(np.transpose(target, [2,0,1]), opts=dict(title='GT' + str(epoch)))
        # vis.image(np.transpose(predicted, [2,0,1]), opts=dict(title='Predicted' + str(epoch)))
        if (epoch+1) % 5 == 0:
            if args.model_path != '':
                torch.save(model, "./{}/double_scale_model_{}_{}_{}_from_{}.pkl" .format(args.save_folder, args.arch, args.dataset, epoch, args.model_path))
            else:
                torch.save(model, "./{}/double_scale_model_{}_{}_{}.pkl".format(args.save_folder, args.arch, args.dataset, epoch))
示例#14
0
from torch.autograd import Variable
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader

# define image transforms to do data augumentation
data_transforms = {
    'train':
    transforms.Compose([
        transforms.RandomSizedCrop(299),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ]),
    'val':
    transforms.Compose([
        transforms.Scale(320),
        transforms.CenterCrop(299),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
}

# define data folder using ImageFolder to get images and classes from folder
root = '/media/sherlock/Files/kaggle_dog_vs_cat/'
data_folder = {
    'train':
    ImageFolder(os.path.join(root, 'data/train'),
                transform=data_transforms['train']),
    'val':
    ImageFolder(os.path.join(root, 'data/val'),
                transform=data_transforms['val'])
batch_size = opt.batch_size
num_test_img = opt.num_test_img
lr = 0.0002
train_epoch = 400
img_size = 256
use_LSGAN_loss = False
use_history_batch = False
one_sided_label_smoothing = 1 #default = 1, put 0.9 for smoothing
optim_step_size = 80
optim_gamma = 0.5
CRITIC_ITERS = 5
lambda_FM = 10
num_res_blocks = 2

#----------------------------Load Dataset--------------------------------------
transform = transforms.Compose([transforms.Scale((img_size,img_size)),
                                transforms.ToTensor(),
                                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                               ]) 

dataset = CocoData(root = opt.train_imgs,
                            annFile = opt.train_annotation,
                            category_names = category_names,
                            transform=transform,
                            final_img_size=img_size)  


#Discarding images contain small instances  
dataset.discard_small(min_area=0.01, max_area= 0.5)
#dataset.discard_bad_examples('bad_examples_list.txt')
dataset.discard_num_objects()
示例#16
0
#LOSS
class GramMSELoss(nn.Module):
    def forward(self, input, target):
        out = nn.MSELoss()(GramMatrix()(input), target)
        return out


#--------------------------
#-----IMAGE PROCESSING-----
#--------------------------

img_size = 512

#PRE-PROCESSING
prep = transforms.Compose([
    transforms.Scale(img_size),
    transforms.ToTensor(),
    transforms.Lambda(
        lambda x: x[torch.LongTensor([2, 1, 0])]),  #CONVERT TO BGR FOR VGG NET
    transforms.Normalize(mean=[0.40760392, 0.45795686, 0.48501961],
                         std=[1, 1, 1]),  #SUBTRACT IMAGENET MEAN
    transforms.Lambda(
        lambda x: x.mul_(255)),  #VGG WAS TRAINED WITH PIXEL VALUES 0-255
])

#POST PROCESSING A
postpa = transforms.Compose([
    transforms.Lambda(lambda x: x.mul_(1. / 255)),
    transforms.Normalize(mean=[-0.40760392, -0.45795686, -0.48501961],
                         std=[1, 1, 1]),
    transforms.Lambda(lambda x: x[torch.LongTensor([2, 1, 0])]),
示例#17
0
def main():
    global best_acc
    start_epoch = args.start_epoch  # start from epoch 0 or last checkpoint epoch

    if not os.path.isdir(args.checkpoint):
        mkdir_p(args.checkpoint)

    # Data loading code
    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    train_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomSizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ])),
                                               batch_size=args.train_batch,
                                               shuffle=True,
                                               num_workers=args.workers,
                                               pin_memory=True)

    val_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        valdir,
        transforms.Compose([
            transforms.Scale(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            normalize,
        ])),
                                             batch_size=args.test_batch,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    # create model
    if args.pretrained:
        print("=> using pre-trained model '{}'".format(args.arch))
        model = models.__dict__[args.arch](pretrained=True)
    elif args.arch.startswith('resnext'):
        model = models.__dict__[args.arch](
            baseWidth=args.base_width,
            cardinality=args.cardinality,
        )
    else:
        print("=> creating model '{}'".format(args.arch))
        model = models.__dict__[args.arch]()

    if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
        model.features = torch.nn.DataParallel(model.features)
        model.cuda()
    else:
        model = torch.nn.DataParallel(model).cuda()

    cudnn.benchmark = True
    print('    Total params: %.2fM' %
          (sum(p.numel() for p in model.parameters()) / 1000000.0))

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda()
    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr,
                          momentum=args.momentum,
                          weight_decay=args.weight_decay)

    # Resume
    title = 'ImageNet-' + args.arch
    if args.resume:
        # Load checkpoint.
        print('==> Resuming from checkpoint..')
        assert os.path.isfile(
            args.resume), 'Error: no checkpoint directory found!'
        args.checkpoint = os.path.dirname(args.resume)
        checkpoint = torch.load(args.resume)
        best_acc = checkpoint['best_acc']
        start_epoch = checkpoint['epoch']
        model.load_state_dict(checkpoint['state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        logger = Logger(os.path.join(args.checkpoint, 'log.txt'),
                        title=title,
                        resume=True)
    else:
        logger = Logger(os.path.join(args.checkpoint, 'log.txt'), title=title)
        logger.set_names([
            'Learning Rate', 'Train Loss', 'Valid Loss', 'Train Acc.',
            'Valid Acc.'
        ])

    if args.evaluate:
        print('\nEvaluation only')
        test_loss, test_acc = test(val_loader, model, criterion, start_epoch,
                                   use_cuda)
        print(' Test Loss:  %.8f, Test Acc:  %.2f' % (test_loss, test_acc))
        return

    # Train and val
    for epoch in range(start_epoch, args.epochs):
        adjust_learning_rate(optimizer, epoch)

        print('\nEpoch: [%d | %d] LR: %f' %
              (epoch + 1, args.epochs, state['lr']))

        train_loss, train_acc = train(train_loader, model, criterion,
                                      optimizer, epoch, use_cuda)
        test_loss, test_acc = test(val_loader, model, criterion, epoch,
                                   use_cuda)

        # append logger file
        logger.append(
            [state['lr'], train_loss, test_loss, train_acc, test_acc])

        # save model
        is_best = test_acc > best_acc
        best_acc = max(test_acc, best_acc)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'state_dict': model.state_dict(),
                'acc': test_acc,
                'best_acc': best_acc,
                'optimizer': optimizer.state_dict(),
            },
            is_best,
            checkpoint=args.checkpoint)

    logger.close()
    logger.plot()
    savefig(os.path.join(args.checkpoint, 'log.eps'))

    print('Best acc:')
    print(best_acc)
示例#18
0
import torchvision.transforms as transforms
import torchvision.models as models

# cuda

use_cuda = torch.cuda.is_available()
dtype = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor

# images

imsize = 200  # desired size of the output image

# Transforms are common image transforms. They can be chained together using Compose
#class torchvision.transforms.compose
loader = transforms.Compose([
    transforms.Scale(imsize),  # scale imported image
    transforms.ToTensor()
])  # transform it into a torch tensor


def image_loader(image_name):
    image = Image.open(image_name)
    # handle the image to tensor and change it to variable
    image = Variable(loader(image))
    # fake batch dimension required to fit network's input dimensions
    image = image.unsqueeze(0)
    return image


# dtype tensor.float
style = image_loader("images/picasso.jpg").type(dtype)
示例#19
0
import json
import os
import pickle
import PIL
import numpy as np
from functools import partial
from time import strftime
from basic_module import BasicModule

imagepath = "/home1/szh/scene-baseline-master/checkpoints/a.jpg"
pickle.load = partial(pickle.load, encoding="latin1")
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[1.229, 0.224, 0.225])
transformsfunc = transforms.Compose([
    transforms.Scale(224),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    normalize,
])


def InputImage(imagepath):
    im = PIL.Image.open(imagepath).convert('RGB')
    return transformsfunc(im)


class ResNet(BasicModule):
    def __init__(self, model, opt=None, feature_dim=2048, name='resnet'):
        super(ResNet, self).__init__(opt)
        self.model_name = name
示例#20
0
# set the manual seed.
random.seed(opt.manualseed)
torch.manual_seed(opt.manualseed)
torch.cuda.manual_seed_all(opt.manualseed)

# save
if not opt.save:
    contrast = 0.2
    brightness = 0.2
    noise = 0.1
    normal_imgs = [0.59, 0.25]
    transform = transforms.Compose([
        AddRandomContrast(contrast),
        AddRandomBrightness(brightness),
        transforms.Scale(opt.imagesize),
    ])
else:
    contrast = 0.00001
    brightness = 0.00001
    noise = 0.00001
    normal_imgs = None
    transform = transforms.Compose(
        [transforms.Resize(opt.imagesize),
         transforms.ToTensor()])

print("load data")
#load the dataset using the loader in utils_pose
trainingdata = None
if not opt.data == "":
    train_dataset = MultipleVertexJson(
示例#21
0
num_test_img = opt.num_test_img
lr = 0.0002
train_epoch = 20
img_size = 256
use_LSGAN_loss = False
use_history_batch = False
one_sided_label_smoothing = 1  #default = 1, put 0.9 for smoothing
optim_step_size = 80
optim_gamma = 0.5
CRITIC_ITERS = 5
lambda_FM = 10
num_res_blocks = 2

#----------------------------Load Dataset--------------------------------------
transform = transforms.Compose([
    transforms.Scale((img_size, img_size)),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])

allmasks = sorted(
    glob.glob(os.path.join(opt.mask_imgs, '**', '*.png'), recursive=True))
print('Number of masks: %d' % len(allmasks))
dataset = chairs(imfile=opt.train_imgs,
                 mfiles=allmasks,
                 category_names=category_names,
                 transform=transform,
                 final_img_size=img_size)

#Discarding images contain small instances
# dataset.discard_small(min_area=0.01, max_area= 0.5)
示例#22
0
def main():
    global args, best_prec1
    args = parser.parse_args()

    args.distributed = args.world_size > 1

    if args.distributed:
        dist.init_process_group(backend=args.dist_backend,
                                init_method=args.dist_url,
                                world_size=args.world_size)

    # create model
    if args.pretrained:
        print("=> using pre-trained model '{}'".format(args.arch))
        model = models.__dict__[args.arch](pretrained=True)
    else:
        print("=> creating model '{}'".format(args.arch))
        model = models.__dict__[args.arch]()
    print(model)
    ######################
    if args.arch.startswith('vgg'):
        mod = list(model.classifier.children())
        mod.pop()
        mod.append(torch.nn.Linear(4096, 30))
        new_classifier = torch.nn.Sequential(*mod)
        model.classifier = new_classifier

    elif args.arch.startswith('alexnet'):
        mod = list(model.classifier.children())
        mod.pop()
        mod.append(torch.nn.Linear(4096, 30))
        new_classifier = torch.nn.Sequential(*mod)
        model.classifier = new_classifier
    else:
        model.fc = torch.nn.Linear(2048, 30)

    print(model)

    ########################
    class MyResNet(nn.Module):
        def __init__(self, model):
            super(MyResNet, self).__init__()
            self.conv1 = model.conv1
            self.bn1 = model.bn1
            self.relu = model.relu
            self.maxpool = model.maxpool
            self.layer1 = model.layer1
            self.layer2 = model.layer2
            self.layer3 = model.layer3
            self.layer4 = model.layer4
            self.avgpool = model.avgpool
            self.dropout = nn.Dropout()
            self.fc = model.fc

        def forward(self, x):
            x = self.conv1(x)
            x = self.bn1(x)
            x = self.relu(x)
            x = self.maxpool(x)

            x = self.layer1(x)
            x = self.layer2(x)
            x = self.layer3(x)
            x = self.layer4(x)

            x = self.avgpool(x)
            x = x.view(x.size(0), -1)
            x = self.dropout(x)
            x = self.fc(x)

            return x

    my_model = MyResNet(model)
    model = my_model.cuda()

    model.cuda()

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.SGD(model.parameters(),
                                args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            # best_prec1 = checkpoint['best_prec1']
            model.load_state_dict(checkpoint['state_dict'])
            #optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))


######################
    logpath_val = './log/test/'
    txtname = 'test.csv'
    if not os.path.exists(logpath_val):
        os.makedirs(logpath_val)
    if os.path.exists(logpath_val + txtname):
        os.remove(logpath_val + txtname)
    f_val = file(logpath_val + txtname, 'a+')
    #################
    cudnn.benchmark = True

    valdir = os.path.join(args.data, 'filling')
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    transform = transforms.Compose([
        transforms.Scale([299, 299]),
        transforms.ToTensor(),
        normalize,
    ])
    ####################################################
    class_index = [
        1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25,
        26, 27, 28, 29, 3, 30, 4, 5, 6, 7, 8, 9
    ]
    model.eval()
    images = np.array([])
    test_list = get_files(valdir)
    for i, item in enumerate(test_list):
        print('Processing %i of %i (%s)' % (i + 1, len(test_list), item))
        test_image_name = item.split('/')[-1].split('.')[0]
        test_image = transform(Image.open(item))
        input_var = torch.autograd.Variable(test_image,
                                            volatile=True).unsqueeze(0).cuda()
        output = nn.functional.softmax(model(input_var))
        f_val.writelines(test_image_name + ',' + str(1) + ',' +
                         str('%.10f' % output.data[0][0]) + '\n')
        f_val.writelines(test_image_name + ',' + str(2) + ',' +
                         str('%.10f' % output.data[0][11]) + '\n')
        f_val.writelines(test_image_name + ',' + str(3) + ',' +
                         str('%.10f' % output.data[0][22]) + '\n')
        f_val.writelines(test_image_name + ',' + str(4) + ',' +
                         str('%.10f' % output.data[0][24]) + '\n')
        f_val.writelines(test_image_name + ',' + str(5) + ',' +
                         str('%.10f' % output.data[0][25]) + '\n')
        f_val.writelines(test_image_name + ',' + str(6) + ',' +
                         str('%.10f' % output.data[0][26]) + '\n')
        f_val.writelines(test_image_name + ',' + str(7) + ',' +
                         str('%.10f' % output.data[0][27]) + '\n')
        f_val.writelines(test_image_name + ',' + str(8) + ',' +
                         str('%.10f' % output.data[0][28]) + '\n')
        f_val.writelines(test_image_name + ',' + str(9) + ',' +
                         str('%.10f' % output.data[0][29]) + '\n')
        f_val.writelines(test_image_name + ',' + str(10) + ',' +
                         str('%.10f' % output.data[0][1]) + '\n')
        f_val.writelines(test_image_name + ',' + str(11) + ',' +
                         str('%.10f' % output.data[0][2]) + '\n')
        f_val.writelines(test_image_name + ',' + str(12) + ',' +
                         str('%.10f' % output.data[0][3]) + '\n')
        f_val.writelines(test_image_name + ',' + str(13) + ',' +
                         str('%.10f' % output.data[0][4]) + '\n')
        f_val.writelines(test_image_name + ',' + str(14) + ',' +
                         str('%.10f' % output.data[0][5]) + '\n')
        f_val.writelines(test_image_name + ',' + str(15) + ',' +
                         str('%.10f' % output.data[0][6]) + '\n')
        f_val.writelines(test_image_name + ',' + str(16) + ',' +
                         str('%.10f' % output.data[0][7]) + '\n')
        f_val.writelines(test_image_name + ',' + str(17) + ',' +
                         str('%.10f' % output.data[0][8]) + '\n')
        f_val.writelines(test_image_name + ',' + str(18) + ',' +
                         str('%.10f' % output.data[0][9]) + '\n')
        f_val.writelines(test_image_name + ',' + str(19) + ',' +
                         str('%.10f' % output.data[0][10]) + '\n')
        f_val.writelines(test_image_name + ',' + str(20) + ',' +
                         str('%.10f' % output.data[0][12]) + '\n')
        f_val.writelines(test_image_name + ',' + str(21) + ',' +
                         str('%.10f' % output.data[0][13]) + '\n')
        f_val.writelines(test_image_name + ',' + str(22) + ',' +
                         str('%.10f' % output.data[0][14]) + '\n')
        f_val.writelines(test_image_name + ',' + str(23) + ',' +
                         str('%.10f' % output.data[0][15]) + '\n')
        f_val.writelines(test_image_name + ',' + str(24) + ',' +
                         str('%.10f' % output.data[0][16]) + '\n')
        f_val.writelines(test_image_name + ',' + str(25) + ',' +
                         str('%.10f' % output.data[0][17]) + '\n')
        f_val.writelines(test_image_name + ',' + str(26) + ',' +
                         str('%.10f' % output.data[0][18]) + '\n')
        f_val.writelines(test_image_name + ',' + str(27) + ',' +
                         str('%.10f' % output.data[0][19]) + '\n')
        f_val.writelines(test_image_name + ',' + str(28) + ',' +
                         str('%.10f' % output.data[0][20]) + '\n')
        f_val.writelines(test_image_name + ',' + str(29) + ',' +
                         str('%.10f' % output.data[0][21]) + '\n')
        f_val.writelines(test_image_name + ',' + str(30) + ',' +
                         str('%.10f' % output.data[0][23]) + '\n')

    return
def main(args):
    # Figure out the datatype we will use; this will determine whether we run on
    # CPU or on GPU. Run on GPU by adding the command-line flag --use_gpu
    dtype = torch.FloatTensor
    if args.use_gpu:
        dtype = torch.cuda.FloatTensor

    # Use the torchvision.transforms package to set up a transformation to use
    # for our images at training time. The train-time transform will incorporate
    # data augmentation and preprocessing. At training time we will perform the
    # following preprocessing on our images:
    # (1) Resize the image so its smaller side is 256 pixels long
    # (2) Take a random 224 x 224 crop to the scaled image
    # (3) Horizontally flip the image with probability 1/2
    # (4) Convert the image from a PIL Image to a Torch Tensor
    # (5) Normalize the image using the mean and variance of each color channel
    #     computed on the ImageNet dataset.
    train_transform = T.Compose([
        T.Scale(256),
        T.RandomSizedCrop(224),
        T.RandomHorizontalFlip(),
        T.ToTensor(),
        T.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
    ])

    # You load data in PyTorch by first constructing a Dataset object which
    # knows how to load individual data points (images and labels) and apply a
    # transform. The Dataset object is then wrapped in a DataLoader, which iterates
    # over the Dataset to construct minibatches. The num_workers flag to the
    # DataLoader constructor is the number of background threads to use for loading
    # data; this allows dataloading to happen off the main thread. You can see the
    # definition for the base Dataset class here:
    # https://github.com/pytorch/pytorch/blob/master/torch/utils/data/dataset.py
    #
    # and you can see the definition for the DataLoader class here:
    # https://github.com/pytorch/pytorch/blob/master/torch/utils/data/dataloader.py#L262
    #
    # The torchvision package provides an ImageFolder Dataset class which knows
    # how to read images off disk, where the image from each category are stored
    # in a subdirectory.
    #
    # You can read more about the ImageFolder class here:
    # https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py
    train_dset = ImageFolder(args.train_dir, transform=train_transform)
    train_loader = DataLoader(train_dset,
                              batch_size=args.batch_size,
                              num_workers=args.num_workers,
                              shuffle=True)

    # Set up a transform to use for validation data at test-time. For validation
    # images we will simply resize so the smaller edge has 224 pixels, then take
    # a 224 x 224 center crop. We will then construct an ImageFolder Dataset object
    # for the validation data, and a DataLoader for the validation set.
    val_transform = T.Compose([
        T.Scale(224),
        T.CenterCrop(224),
        T.ToTensor(),
        T.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
    ])
    val_dset = ImageFolder(args.val_dir, transform=val_transform)
    val_loader = DataLoader(val_dset,
                            batch_size=args.batch_size,
                            num_workers=args.num_workers)

    # Now that we have set up the data, it's time to set up the model.
    # For this example we will finetune a ResNet-18 model which has been
    # pretrained on ImageNet. We will first reinitialize the last layer of the
    # model, and train only the last layer for a few epochs. We will then finetune
    # the entire model on our dataset for a few more epochs.

    # First load the pretrained ResNet-18 model; this will download the model
    # weights from the web the first time you run it.
    model = torchvision.models.resnet18(pretrained=True)

    # Reinitialize the last layer of the model. Each pretrained model has a
    # slightly different structure, but from the ResNet class definition
    # we see that the final fully-connected layer is stored in model.fc:
    # https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py#L111
    num_classes = len(train_dset.classes)
    model.fc = nn.Linear(model.fc.in_features, num_classes)
    model.conv1 = nn.Conv2d(3,
                            64,
                            kernel_size=7,
                            stride=2,
                            padding=3,
                            bias=False)

    # Cast the model to the correct datatype, and create a loss function for
    # training the model.
    model.type(dtype)
    loss_fn = nn.CrossEntropyLoss().type(dtype)

    # First we want to train only the reinitialized last layer for a few epochs.
    # During this phase we do not need to compute gradients with respect to the
    # other weights of the model, so we set the requires_grad flag to False for
    # all model parameters, then set requires_grad=True for the parameters in the
    # last layer only.
    for param in model.parameters():
        param.requires_grad = False
    for param in model.fc.parameters():
        param.requires_grad = True
    for param in model.conv1.parameters():
        param.requires_grad = True

    # Construct an Optimizer object for updating the last layer only.
    optimizer = torch.optim.Adam(model.fc.parameters(), lr=1e-3)
    optimizer = torch.optim.Adam(model.conv1.parameters(), lr=1e-3)

    # Update only the last layer for a few epochs.
    for epoch in range(args.num_epochs1):
        # Run an epoch over the training data.
        print('Starting epoch %d / %d' % (epoch + 1, args.num_epochs1))
        run_epoch(model, loss_fn, train_loader, optimizer, dtype)

        # Check accuracy on the train and val sets.
        train_acc = check_accuracy(model, train_loader, dtype)
        val_acc = check_accuracy(model, val_loader, dtype)
        print('Train accuracy: ', train_acc)
        print('Val accuracy: ', val_acc)
        print()

    # Now we want to finetune the entire model for a few epochs. To do thise we
    # will need to compute gradients with respect to all model parameters, so
    # we flag all parameters as requiring gradients.
    for param in model.parameters():
        param.requires_grad = True

    # Construct a new Optimizer that will update all model parameters. Note the
    # small learning rate.
    optimizer = torch.optim.Adam(model.parameters(), lr=1e-5)

    # Train the entire model for a few more epochs, checking accuracy on the
    # train and validation sets after each epoch.
    for epoch in range(args.num_epochs2):
        print('Starting epoch %d / %d' % (epoch + 1, args.num_epochs2))
        run_epoch(model, loss_fn, train_loader, optimizer, dtype)

        train_acc = check_accuracy(model, train_loader, dtype)
        val_acc = check_accuracy(model, val_loader, dtype)
        print('Train accuracy: ', train_acc)
        print('Val accuracy: ', val_acc)
        print()
示例#24
0
print(len(all_xray_df['path']))

train_x, test_x, train_y, test_y = train_test_split(
    all_xray_df['path'].to_numpy(),
    all_xray_df['disease_vec'].to_numpy(),
    test_size=0.40,
    random_state=2018)
test_x, val_x, test_y, val_y = train_test_split(test_x,
                                                test_y,
                                                test_size=0.50,
                                                random_state=2018)

test = ChestDataset(image_names=test_x,
                    labels=test_y,
                    transform=transforms.Compose([
                        transforms.Scale(256),
                        transforms.Grayscale(),
                        transforms.ToTensor(),
                        transforms.Normalize(mean=[0.485], std=[0.229])
                    ]))
test_loader = DataLoader(test, batch_size=1, shuffle=True)

train = ChestDataset(train_x,
                     train_y,
                     transform=transforms.Compose([
                         transforms.Scale(256),
                         transforms.Grayscale(),
                         transforms.ToTensor(),
                         transforms.Normalize(mean=[0.485], std=[0.229])
                     ]))
train_loader = DataLoader(train, batch_size=batch_size, shuffle=True)
示例#25
0
def main():
    global net
    global trainloader
    global valloader
    global best_loss
    global log_file
    global optimizer
    global criterion
    #initialize
    start_epoch = 0
    best_loss = np.finfo(np.float32).max

    #augmentation
    random_rotate_func = lambda x: x.rotate(random.randint(-15, 15),
                                            resample=Image.BICUBIC)
    random_scale_func = lambda x: transforms.Scale(int(random.uniform(1.0,1.4)\
                                                   * max(x.size)))(x)
    gaus_blur_func = lambda x: x.filter(PIL.ImageFilter.GaussianBlur(radius=1))
    median_blur_func = lambda x: x.filter(PIL.ImageFilter.MedianFilter(size=3))

    #train preprocessing
    transform_train = transforms.Compose([
        transforms.Lambda(lambd=random_rotate_func),
        transforms.CenterCrop(224),
        transforms.Scale((112, 112)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=MEAN, std=STD),
    ])

    #validation preprocessing
    transform_val = transforms.Compose([
        transforms.CenterCrop(224),
        transforms.Scale((112, 112)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=MEAN, std=STD)
    ])

    print('==> Preparing data..')
    trainset = ImageListDataset(root=args.root,
                                list_path=args.datalist,
                                split='train',
                                transform=transform_train)

    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              num_workers=8,
                                              pin_memory=True)

    valset = ImageListDataset(root=args.root,
                              list_path=args.datalist,
                              split='val',
                              transform=transform_val)

    valloader = torch.utils.data.DataLoader(valset,
                                            batch_size=args.batch_size,
                                            shuffle=False,
                                            num_workers=8,
                                            pin_memory=True)

    # Create model
    net = None
    if args.model_name == 'ResNet18':
        net = ResNet18()
    elif args.model_name == 'ResNet34':
        net = ResNet34()
    elif args.model_name == 'ResNet50':
        net = ResNet50()
    elif args.model_name == 'DenseNet':
        net = DenseNet121()
    elif args.model_name == 'VGG11':
        net = VGG('VGG11')
    elif args.model_name == 'ResNet152':
        net = ResNet152()
    elif args / model_name == 'ResNet101':
        net = ResNet101()

    print('==> Building model..')

    if args.resume:
        # Load checkpoint
        print('==> Resuming from checkpoint..')
        assert os.path.isdir(
            'checkpoint'), 'Error: no checkpoint directory found!'
        checkpoint = torch.load('./checkpoint/{0}/best_model_chkpt.t7'.format(
            args.name))
        net.load_state_dict(checkpoint['net'])
        best_loss = checkpoint['loss']
        start_epoch = checkpoint['epoch'] + 1

    # Choosing of criterion
    if args.criterion == 'MSE':
        criterion = nn.MSELoss()
    else:
        criterion = None  # Add your criterion

    # Choosing of optimizer
    if args.optimizer == 'adam':
        optimizer = optim.Adam(net.parameters(), lr=args.lr)
    elif args.optimizer == 'adadelta':
        optimizer = optim.Adadelta(net.parameters(), lr=args.lr)
    else:
        optimizer = optim.SGD(net.parameters(),
                              lr=args.lr,
                              momentum=0.9,
                              weight_decay=5e-4)

    # Load on GPU
    if args.cuda:
        print('==> Using CUDA')
        print(torch.cuda.device_count())
        if torch.cuda.device_count() > 1:
            net = torch.nn.DataParallel(net).cuda()
        else:
            net = net.cuda()
        cudnn.benchmark = True
        print('==> model on GPU')
        criterion = criterion.cuda()
    else:
        print('==> model on CPU')

    if not os.path.isdir(args.log_dir_path):
        os.makedirs(args.log_dir_path)
    log_file_path = os.path.join(args.log_dir_path, args.name + '.log')
    # logger file openning
    log_file = open(log_file_path, 'w')
    log_file.write('type,epoch,batch,loss,acc\n')

    print('==> Model')
    print(net)

    try:
        for epoch in range(start_epoch, args.epochs):
            trainloader = torch.utils.data.DataLoader(
                trainset,
                batch_size=args.batch_size,
                shuffle=True,
                num_workers=8,
                pin_memory=True)
            train(epoch)
            validation(epoch)
        print('==> Best loss: {0:.5f}'.format(best_loss))
    except Exception as e:
        print(e.message)
        log_file.write(e.message)
    finally:
        log_file.close()
示例#26
0
                    help='Path to gan-generated dataset, if required')

parser.add_argument('--train_dataset',
                    default='../data/GTSRB/Final_Training/Images',
                    help='Path to a training dataset')
parser.add_argument('--test_dataset',
                    default='../data/GTSRB/Final_Test/Images',
                    help='Path to a test dataset, if required')
parser.add_argument('--batchsize', default=64, help='batchsize, default=64')
parser.add_argument('--imsize', default=64, help='image size, default=64')

opt = parser.parse_args()

fake_dataset = dset.ImageFolder(root='./generated_dataset',
                                transform=transforms.Compose([
                                    transforms.Scale(opt.imsize),
                                    transforms.CenterCrop(opt.imsize),
                                    transforms.ToTensor(),
                                    transforms.Normalize((0.5, 0.5, 0.5),
                                                         (0.5, 0.5, 0.5)),
                                ]))

train_dataset = dset.ImageFolder(root='../data/GTSRB/Final_Training/Images',
                                 transform=transforms.Compose([
                                     transforms.Scale(opt.imsize),
                                     transforms.CenterCrop(opt.imsize),
                                     transforms.ToTensor(),
                                     transforms.Normalize((0.5, 0.5, 0.5),
                                                          (0.5, 0.5, 0.5)),
                                 ]))
    if args.snapshot == '':
        load_filtered_state_dict(
            model,
            model_zoo.load_url(
                'https://download.pytorch.org/models/resnet50-19c8e357.pth'))
    else:
        saved_state_dict = torch.load(args.snapshot)
        model.load_state_dict(saved_state_dict)
    model.cuda(gpu)

    ##########################################
    ##           Dataset Loading            ##
    ##########################################
    print('Loading data.')
    transformations = transforms.Compose([
        transforms.Scale(240),
        transforms.RandomCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

    if args.dataset == 'Pose_300W_LP':
        train_filename_list = os.path.join(args.filename_list,
                                           'train_filename_all.npy')
        val_filename_list = os.path.join(args.filename_list,
                                         'val_filename_all.npy')
        pose_dataset_train = datasets.Pose_300W_LP(args.data_dir, num_bins,
                                                   train_filename_list,
                                                   transformations, args.debug)
        pose_dataset_val = datasets.Pose_300W_LP(args.data_dir, num_bins,
    # print "UP",unique_point
    # print "P",point
    # point = unique_point
    if len(point) != 2:
        unique_point = [list(x) for x in set(tuple(x) for x in point)]
        if len(unique_point) == 2:
            return unique_point
        else:
            print("erro==================")

    else:
        return point


trans = transforms.Compose([
    transforms.Scale((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(
        [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
    )  # from http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html
])


def process_points(points):
    filt = 4
    y = len(points) / filt
    final_points = []

    for x in range(y):
        final_points.append(list(points[x * filt:x * filt + filt]))
示例#29
0
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import torchvision
from torchvision import datasets, transforms
import cnndemo1
# import visdom

path = 'd:/debug/python/pytorchdemo/'
mymodel = torch.load('mycnn2')
mymodel.eval()
test_data = torchvision.datasets.ImageFolder(
    'D:/debug/python/pytorchdemo/pic/val',
    transform=transforms.Compose([
        transforms.Scale(512),
        # transforms.CenterCrop(256),
        transforms.ToTensor()
    ]))
test_loader = torch.utils.data.DataLoader(test_data,
                                          batch_size=2,
                                          shuffle=True)

loss_func = nn.CrossEntropyLoss()
optimizer = optim.SGD(mymodel.parameters(), lr=0.02)  # 设置学习方法


def test_fun():

    eval_loss = 0
    eval_acc = 0
示例#30
0
# Implementation of https://arxiv.org/pdf/1512.03385.pdf.
# See section 4.2 for model architecture on CIFAR-10.
# Some part of the code was referenced below.
# https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable

# Image Preprocessing
transform = transforms.Compose([
    transforms.Scale(40),
    transforms.RandomHorizontalFlip(),
    transforms.RandomCrop(32),
    transforms.ToTensor()
])

# CIFAR-10 Dataset
train_dataset = dsets.CIFAR10(root='./data/',
                              train=True,
                              transform=transform,
                              download=True)

test_dataset = dsets.CIFAR10(root='./data/',
                             train=False,
                             transform=transforms.ToTensor())

# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=100,