def __init__(self):
        super(ResNet, self).__init__()
        self.model = resnet152(pretrained=True)

        def save_output(module, input, output):
            self.buffer = output
        self.model.layer4.register_forward_hook(save_output)
Beispiel #2
0
 def __init__(self, nclass, backbone, aux, se_loss, dilated=True, norm_layer=None,
              base_size=576, crop_size=608, mean=[.485, .456, .406],
              std=[.229, .224, .225], root='./pretrain_models',
              multi_grid=False, multi_dilation=None):
     super(BaseNet, self).__init__()
     self.nclass = nclass
     self.aux = aux
     self.se_loss = se_loss
     self.mean = mean
     self.std = std
     self.base_size = base_size
     self.crop_size = crop_size
     # copying modules from pretrained models
     if backbone == 'resnet50':
         self.pretrained = resnet.resnet50(pretrained=False, dilated=dilated,
                                           norm_layer=norm_layer, root=root,
                                           multi_grid=multi_grid, multi_dilation=multi_dilation)
     elif backbone == 'resnet101':
         self.pretrained = resnet.resnet101(pretrained=True, dilated=dilated,
                                            norm_layer=norm_layer, root=root,
                                            multi_grid=multi_grid,multi_dilation=multi_dilation)
     elif backbone == 'resnet152':
         self.pretrained = resnet.resnet152(pretrained=True, dilated=dilated,
                                            norm_layer=norm_layer, root=root,
                                            multi_grid=multi_grid, multi_dilation=multi_dilation)
     else:
         raise RuntimeError('unknown backbone: {}'.format(backbone))
     # bilinear upsample options
     self._up_kwargs = up_kwargs
Beispiel #3
0
    def __init__(self, nclass, backbone):
        super(DeepTen, self).__init__()
        self.backbone = backbone
        # copying modules from pretrained models
        if self.backbone == 'resnet50':
            self.pretrained = resnet50(pretrained=True, dilated=False)
        elif self.backbone == 'resnet101':
            self.pretrained = resnet101(pretrained=True, dilated=False)
        elif self.backbone == 'resnet152':
            self.pretrained = resnet152(pretrained=True, dilated=False)
        else:
            raise RuntimeError('unknown backbone: {}'.format(self.backbone))
        self.scalenum = 4
        n_codes = 32

        self.fc = nn.Sequential(
            Normalize(),
            nn.Linear(64 * 64, 128),
        )
        self.classifier = nn.Sequential(Normalize(),
                                        nn.Linear(128 * self.scalenum, nclass))
        self.head1 = self.make_layer_head_customize(256, n_codes, 56)
        self.head2 = self.make_layer_head_customize(512, n_codes, 28)
        self.head3 = self.make_layer_head_customize(1024, n_codes, 14)
        self.head4 = self.make_layer_head_customize(2048, n_codes, 7)
        self.pool1 = self.make_layer_pooling_customize(256, 56)
        self.pool2 = self.make_layer_pooling_customize(512, 28)
        self.pool3 = self.make_layer_pooling_customize(1024, 14)
        self.pool4 = self.make_layer_pooling_customize(2048, 7)
Beispiel #4
0
def main(opt):
    # モデル定義
    model = resnet.resnet152(pretrained=True)
    if not os.path.exists(opt.output):
        os.makedirs(opt.output)

    if torch.cuda.is_available():  # GPUが利用可能か確認
        device = 'cuda'
    else:
        device = 'cpu'
    print('device is {0}'.format(device))
    model.to(device)
    model.fc = nn.Linear(2048, opt.ftclass)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
    transform = transform_images()
    training_data = TrainDataset(opt.input, transform)
    train_loader = torch.utils.data.DataLoader(training_data,
                                               batch_size=32,
                                               shuffle=True,
                                               num_workers=4,
                                               pin_memory=True)
    for epoch in range(50):
        train(epoch + 1, model, criterion, optimizer, train_loader, device)
        if (epoch + 1) % 2 == 0:
            savefile = os.path.join(opt.output,
                                    'save_{:0>3}.pth'.format(epoch + 1))
            torch.save(model.state_dict(), savefile)
Beispiel #5
0
def conv_validation():
    model = resnet.resnet152(pretrained=False)
    checkpoint = torch.load('resnet152\\checkpoint_blu100.pth.tar')
    model.load_state_dict(moduledict_to_dict(checkpoint['state_dict']))
    model.quantize_from('quant_param152.data')
    model.load_blu('blu152.data')
    #normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
    normalize = transforms.Normalize(mean=[128 / 255, 128 / 255, 128 / 255],
                                     std=[0.226, 0.226, 0.226])
    val_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        '..\\data\\imagenet_test\\val',
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            normalize,
        ])),
                                             batch_size=50,
                                             shuffle=False,
                                             num_workers=4,
                                             pin_memory=True)

    acc1_t = torch.tensor([0.0])
    acc5_t = torch.tensor([0.0])
    for i, (input, target) in enumerate(val_loader):
        #input=(input-0.5)/0.226
        #output=model.forward_stat_blu(input)
        #model.blu('blu152.data')
        output = model.forward_quant(input)
        acc1, acc5 = accuracy(output, target, topk=(1, 5))
        print('{}:acc1:{},acc5:{}'.format(i, acc1, acc5))
        acc1_t += acc1
        acc5_t += acc5
    print('acc1:{}\nacc5:{}'.format(acc1_t / (i + 1), acc5_t / (i + 1)))
def init_model(model_name):
    # initialize model depending on env. variable set in dockerfile
    if model_name == 'pnasnet5':
        model = pnasnet5(pretrained=True)
    elif model_name == 'resnet152':
        model = resnet152(pretrained=True)
    elif model_name == 'resnet50':
        model = resnet50(pretrained=True)
    return model
Beispiel #7
0
    def __init__(self, num_class = 9, input_channel = 3, output_stride=16, layer=101):
        super(resnet_backbone, self).__init__()

        if layer == 101:
            self.resnet = resnet101(pretrained=True, output_stride=output_stride)
        elif layer == 152:
            self.resnet = resnet152(pretrained=True, output_stride=output_stride)

        self.conv1 = self.resnet.conv1
Beispiel #8
0
    def init_net(self):

        net_args = {
            "pretrained": True,
            "n_input_channels": len(self.kwargs["static"]["imagery_bands"])
        }

        # https://pytorch.org/docs/stable/torchvision/models.html
        if self.kwargs["net"] == "resnet18":
            self.model = resnet.resnet18(**net_args)
        elif self.kwargs["net"] == "resnet34":
            self.model = resnet.resnet34(**net_args)
        elif self.kwargs["net"] == "resnet50":
            self.model = resnet.resnet50(**net_args)
        elif self.kwargs["net"] == "resnet101":
            self.model = resnet.resnet101(**net_args)
        elif self.kwargs["net"] == "resnet152":
            self.model = resnet.resnet152(**net_args)
        elif self.kwargs["net"] == "vgg11":
            self.model = vgg.vgg11(**net_args)
        elif self.kwargs["net"] == "vgg11_bn":
            self.model = vgg.vgg11_bn(**net_args)
        elif self.kwargs["net"] == "vgg13":
            self.model = vgg.vgg13(**net_args)
        elif self.kwargs["net"] == "vgg13_bn":
            self.model = vgg.vgg13_bn(**net_args)
        elif self.kwargs["net"] == "vgg16":
            self.model = vgg.vgg16(**net_args)
        elif self.kwargs["net"] == "vgg16_bn":
            self.model = vgg.vgg16_bn(**net_args)
        elif self.kwargs["net"] == "vgg19":
            self.model = vgg.vgg19(**net_args)
        elif self.kwargs["net"] == "vgg19_bn":
            self.model = vgg.vgg19_bn(**net_args)

        else:
            raise ValueError("Invalid network specified: {}".format(
                self.kwargs["net"]))

        #  run type: 1 = fine tune, 2 = fixed feature extractor
        #  - replace run type option with "# of layers to fine tune"
        if self.kwargs["run_type"] == 2:
            layer_count = len(list(self.model.parameters()))
            for layer, param in enumerate(self.model.parameters()):
                if layer <= layer_count - 5:
                    param.requires_grad = False

        # Parameters of newly constructed modules have requires_grad=True by default
        # get existing number for input features
        # set new number for output features to number of categories being classified
        # see: https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html
        if "resnet" in self.kwargs["net"]:
            num_ftrs = self.model.fc.in_features
            self.model.fc = nn.Linear(num_ftrs, self.ncats)
        elif "vgg" in self.kwargs["net"]:
            num_ftrs = self.model.classifier[6].in_features
            self.model.classifier[6] = nn.Linear(num_ftrs, self.ncats)
Beispiel #9
0
 def __init__(self, opt):
     super(ImageEncoder, self).__init__()
     self.embed_size = opt.embed_size
     self.cnn = resnet152(pretrained=True)
     self.fc = nn.Sequential(nn.Linear(opt.img_dim, opt.embed_size), nn.ReLU(), nn.Dropout(0.1))
     if not opt.ft_res:
         print('image-encoder-resnet no grad!')
         for param in self.cnn.parameters():
             param.requires_grad = False
     else:
         print('image-encoder-resnet fine-tuning !')
Beispiel #10
0
def build_model(model_name, pretrained=False):
    if model_name == 'resnet34':
        model = resnet.resnet34(pretrained=False)
    elif model_name == 'resnet50':
        model = resnet.resnet50(pretrained=False)
    elif model_name == 'resnet101':
        model = resnet.resnet101(pretrained=False)
    elif model_name == 'resnet152':
        model = resnet.resnet152(pretrained=False)

    if model_name == 'resnet18':
        model.conv1 = nn.Conv2d(2,
                                64,
                                kernel_size=7,
                                stride=2,
                                padding=3,
                                bias=False)
        model.avg_pool = nn.AdaptiveAvgPool2d(1)
        model.last_linear = nn.Linear(512, 8)
    elif model_name == 'resnet34':
        model.conv1 = nn.Conv2d(2,
                                64,
                                kernel_size=7,
                                stride=2,
                                padding=3,
                                bias=False)
        model.avgpool = nn.AdaptiveAvgPool2d(1)
        model.fc = nn.Linear(512, 8)  # Nx8
    else:
        model.conv1 = nn.Conv2d(2,
                                64,
                                kernel_size=7,
                                stride=2,
                                padding=3,
                                bias=False)
        model.avgpool = nn.AdaptiveAvgPool2d(1)
        model.fc = nn.Linear(2048, 8)  # Nx8

    if pretrained == True:
        exclude_dict = ['conv1.weight', 'fc.weight', 'fc.bias']
        pretrained_dict = model_zoo.load_url(model_urls[model_name])
        model_dict = model.state_dict()

        pretrained_dict = {
            k: v
            for k, v in pretrained_dict.items() if k not in exclude_dict
        }

        model_dict.update(pretrained_dict)
        model.load_state_dict(model_dict)

    return model
Beispiel #11
0
def main():
    # Create a pytorch dataset
    data_dir = pathlib.Path('./data/tiny-imagenet-200')
    image_count = len(list(data_dir.glob('**/*.JPEG')))
    CLASS_NAMES = np.array(
        [item.name for item in (data_dir / 'train').glob('*')])
    print('Discovered {} images'.format(image_count))

    # Create the training data generator
    batch_size = 32
    im_height = 64
    im_width = 64
    num_epochs = 1

    data_transforms = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0, 0, 0), tuple(np.sqrt((255, 255, 255)))),
    ])
    train_set = torchvision.datasets.ImageFolder(data_dir / 'train',
                                                 data_transforms)
    train_loader = torch.utils.data.DataLoader(train_set,
                                               batch_size=batch_size,
                                               shuffle=True,
                                               num_workers=4,
                                               pin_memory=True)

    # Create a simple model
    # model = ResNet(len(CLASS_NAMES), im_height, im_width)
    model = resnet152(pretrained=False)
    optim = torch.optim.Adam(model.parameters())
    criterion = nn.CrossEntropyLoss()
    for i in range(num_epochs):
        train_total, train_correct = 0, 0
        for idx, (inputs, targets) in enumerate(train_loader):
            optim.zero_grad()
            outputs = model(inputs)
            loss = criterion(outputs, targets)
            loss.backward()
            optim.step()
            _, predicted = outputs.max(1)
            train_total += targets.size(0)
            train_correct += predicted.eq(targets).sum().item()
            print("\r", end='')
            print(
                f'training {100 * idx / len(train_loader):.2f}%: {train_correct / train_total:.3f}',
                end='')
        torch.save({
            'net': model.state_dict(),
        }, 'latest.pt')
Beispiel #12
0
def main():
    # モデル定義
    model = resnet.resnet152(pretrained=True)
    if torch.cuda.is_available():  # GPUが利用可能か確認
        device = 'cuda'
    else:
        device = 'cpu'
    print('device is {0}'.format(device))
    # print(model.weight.type)
    model.to(device)
    # 絶対パスに変換
    opt = parse_opts()
    outpath = os.path.abspath(opt.output)
    apath = os.path.abspath(opt.input)
    video_names = sorted(glob.glob(os.path.join(apath, '*')))

    if os.path.exists('tmp'):
        subprocess.call('rm -rf tmp', shell=True)

    for vpath in video_names:
        vname = os.path.splitext(os.path.basename(vpath))[0]

        subprocess.call('mkdir tmp', shell=True)
        subprocess.call(
            'ffmpeg -loglevel warning -i {} tmp/image_%05d.jpg'.format(vpath),
            shell=True)
        images = sorted(glob.glob('tmp/*.jpg'))
        if opt.only_hand:
            print('convert to masked images')
            for im in tqdm(images):
                frame = cv2.imread(im)
                maskedframe, _ = hd.detect(frame)
                cv2.imwrite(im, maskedframe)
            print('complete convert images')

        print('extract {}\'s DeepFeatrue'.format(vname))

        outputs = input_image(images, model)

        # ファイルに保存
        if not os.path.exists(outpath):
            subprocess.call('mkdir {}'.format(outpath), shell=True)

        savename = os.path.join(outpath, vname + '.npy')
        np.save(savename, outputs)
        subprocess.call('rm -rf tmp', shell=True)
Beispiel #13
0
    def __init__(self):
        super().__init__()
        self.r_model = resnet.resnet152(pretrained=True)
        self.r_model.eval()
        self.r_model.to(device)

        self.buffer = {}
        lock = threading.Lock()

        # Since we only use the output of the 4th layer from the resnet model and do not
        # need to do forward pass all the way to the final layer we can terminate forward
        # execution in the forward hook of that layer after obtaining the output of it.
        # For that reason, we can define a custom Exception class that will be used for
        # raising early termination error.
        def save_output(module, input, output):
            with lock:
                self.buffer[output.device] = output

        self.r_model.layer4.register_forward_hook(save_output)
Beispiel #14
0
 def __init__(self,
              channel_sets=[2048, 1024, 512, 256],
              resnet='101',
              out_shape=(64, 48),
              n_class=17,
              pretrained=True):
     super(CPN, self).__init__()
     if resnet == '50':
         self.resnet = resnet50(pretrained)
     elif resnet == '101':
         self.resnet = resnet101(pretrained)
     else:
         self.resnet = resnet152(pretrained)
     self.global_net = GlobalNet(channel_sets=channel_sets,
                                 out_shape=out_shape,
                                 num_class=n_class)
     self.refine_net = RefineNet(lateral_channel=channel_sets[-1],
                                 out_shape=out_shape,
                                 num_class=n_class)
Beispiel #15
0
    def __init__(self, backbone='resnet', layers=50, bins=(1, 2, 3, 6), dropout=0.1, classes=2, zoom_factor=8, use_ppm=True, use_softmax=True, pretrained=True, syncbn=True, group_size=8, group=None):
        super(PSPNet, self).__init__()
        assert layers in [18, 34, 50, 101, 152]
        self.zoom_factor = zoom_factor
        self.use_ppm = use_ppm
        self.use_softmax = use_softmax

        if backbone == 'resnet':
            import resnet as models
        else:
            raise NameError('Backbone type not defined!')

        if syncbn:
            # from lib.syncbn import SynchronizedBatchNorm2d as BatchNorm
            def BNFunc(*args, **kwargs):
                return SyncBatchNorm2d(*args, **kwargs, group_size=group_size, group=group, sync_stats=True)
            BatchNorm = BNFunc
        else:
            from torch.nn import BatchNorm2d as BatchNorm
        models.BatchNorm = BatchNorm

        if layers == 34:
            resnet = models.resnet34(pretrained=pretrained)
        elif layers == 18:
            resnet = models.resnet18(pretrained=pretrained)
        elif layers == 50:
            resnet = models.resnet50(pretrained=pretrained)
        elif layers == 101:
            resnet = models.resnet101(pretrained=pretrained)
        else:
            resnet = models.resnet152(pretrained=pretrained)
            
        self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool)
        self.layer1, self.layer2, self.layer3 = resnet.layer1, resnet.layer2, resnet.layer3
        self.layer4_ICR, self.layer4_PFR, self.layer4_PRP = resnet.layer4_ICR, resnet.layer4_PFR, resnet.layer4_PRP
        self.avgpool = nn.AvgPool2d(7, stride=1)
Beispiel #16
0
    def __init__(self,
                 num_layers=50,
                 pretrained=True,
                 num_classes=Config.num_class,
                 ratios=Config.ratios,
                 scales=Config.scales,
                 stride=Config.stride):
        if num_layers == 18:
            model = resnet18(pretrained=pretrained)
        elif num_layers == 34:
            model = resnet34(pretrained=pretrained)
        elif num_layers == 50:
            model = resnet50(pretrained=pretrained)
        elif num_layers == 101:
            model = resnet101(pretrained=pretrained)
        elif num_layers == 152:
            model = resnet152(pretrained=pretrained)
        else:
            raise Exception('layer number must be one of 18, 34, 50, 101, 152')

        rpn = RPN(256, 512, ratios, scales, stride)
        head = ROIHead(num_classes, [7, 7], stride)

        super(FPNResNet, self).__init__(model, rpn, head, num_classes)
Beispiel #17
0
def resnet152(pretrained=False, progress=True):
    return resnet.resnet152(pretrained, progress)
Beispiel #18
0
    def __init__(self,
                 layers=50,
                 bins=(1, 2, 3, 6),
                 dropout=0.1,
                 classes=2,
                 zoom_factor=8,
                 use_softmax=True,
                 use_aux=True,
                 pretrained=True,
                 syncbn=True,
                 group_size=1,
                 group=None,
                 sync_stats=False):
        super(PSPNet, self).__init__()
        assert layers in [50, 101, 152]
        assert 2048 % len(bins) == 0
        assert classes > 1
        assert zoom_factor in [1, 2, 4, 8]
        self.zoom_factor = zoom_factor
        self.use_softmax = use_softmax
        self.use_aux = use_aux

        if syncbn:
            # from lib.syncbn import SynchronizedBatchNorm2d as BatchNorm
            def BNFunc(*args, **kwargs):
                return SyncBatchNorm2d(*args,
                                       **kwargs,
                                       eps=1e-4,
                                       momentum=0.9,
                                       group_size=group_size,
                                       group=group,
                                       sync_stats=sync_stats)

            BatchNorm = BNFunc
        else:
            from torch.nn import BatchNorm2d as BatchNorm
        models.BatchNorm = BatchNorm

        if layers == 50:
            resnet = models.resnet50(pretrained=pretrained)
        elif layers == 101:
            resnet = models.resnet101(pretrained=pretrained)
        else:
            resnet = models.resnet152(pretrained=pretrained)

        self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu,
                                    resnet.maxpool)
        self.layer1, self.layer2, self.layer3, self.layer4 = resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4

        for n, m in self.layer3.named_modules():
            if 'conv2' in n:
                m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
            elif 'downsample.0' in n:
                m.stride = (1, 1)
        for n, m in self.layer4.named_modules():
            if 'conv2' in n:
                m.dilation, m.padding, m.stride = (4, 4), (4, 4), (1, 1)
            elif 'downsample.0' in n:
                m.stride = (1, 1)

        fea_dim = 2048
        self.ppm = PPM(fea_dim, int(fea_dim / len(bins)), bins, BatchNorm)
        fea_dim *= 2
        self.cls = nn.Sequential(
            nn.Conv2d(512, 256, kernel_size=3, padding=1, bias=False),
            BatchNorm(256), nn.ReLU(inplace=True), nn.Dropout2d(p=dropout),
            nn.Conv2d(256, classes, kernel_size=1))
        self.conv6 = nn.Sequential(
            nn.Conv2d(1280, 256, kernel_size=1, padding=0, bias=True),
            BatchNorm(256), nn.ReLU(inplace=True))
        self.conv1_1x1 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=1, padding=0, bias=True),
            BatchNorm(256), nn.ReLU(inplace=True))
        if use_aux:
            self.aux = nn.Sequential(
                nn.Conv2d(1024, 256, kernel_size=3, padding=1, bias=False),
                BatchNorm(256), nn.ReLU(inplace=True), nn.Dropout2d(p=dropout),
                nn.Conv2d(256, classes, kernel_size=1))
Beispiel #19
0
from torch.autograd import Variable
# from torch.optim.lr_scheduler import *

import os
import math
import re
import glob
import pickle
import numpy as np
from PIL import Image
import sys
import resnet
from modules import MFH, GatedTanh, CSF, CS
from config import cfg

stdModule = resnet.resnet152(True)
#print(list(list(stdModule.layer4.children())[0:-1]))
from ipdb import set_trace


class MFHBaseline(nn.Module):
    def __init__(self,
                 use_gru,
                 layers,
                 submodel,
                 grad,
                 num_words,
                 num_ans,
                 hidden_size=1024,
                 emb_size=300,
                 co_att=False,
Beispiel #20
0
def main(args):

    if args.output_dir:
        utils.mkdir(args.output_dir)

    utils.init_distributed_mode(args)
    print(args)

    device = torch.device(args.device)
    torch.backends.cudnn.benchmark = True

    train_dir = args.train_file
    val_dir = args.val_file
    dataset, dataset_test, train_sampler, test_sampler = load_data(
        train_dir, val_dir, args.antibodytrn, args.antibodyval,
        args.cache_dataset, args.distributed)
    data_loader = torch.utils.data.DataLoader(dataset,
                                              batch_size=args.batch_size,
                                              sampler=train_sampler,
                                              num_workers=args.workers,
                                              pin_memory=True)

    data_loader_test = torch.utils.data.DataLoader(dataset_test,
                                                   batch_size=args.batch_size,
                                                   sampler=test_sampler,
                                                   num_workers=args.workers,
                                                   pin_memory=True)

    print("Creating model")
    model = resnet152(num_classes=2, antibody_nums=6)  # 6 antibodies
    image_checkpoint = "../hashimoto_thyroiditis/model_79.pth"
    flag = os.path.exists(image_checkpoint)

    if flag:
        checkpoint = torch.load(image_checkpoint, map_location='cpu')
        msg = model.load_state_dict(checkpoint['model'], strict=False)
        print(msg)

        print("Parameters to be updated:")
        parameters_to_be_updated = ['fc.weight', 'fc.bias'] + msg.missing_keys
        print(parameters_to_be_updated)

        for name, param in model.named_parameters():
            if name not in parameters_to_be_updated:
                param.requires_grad = False

    model.to(device)
    if args.distributed and args.sync_bn:
        model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)

    if flag:
        parameters = list(filter(lambda p: p.requires_grad,
                                 model.parameters()))
        assert len(parameters) == len(parameters_to_be_updated)
    else:
        parameters = model.parameters()

    criterion = nn.CrossEntropyLoss()

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

    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                   step_size=args.lr_step_size,
                                                   gamma=args.lr_gamma)

    model_without_ddp = model
    if args.distributed:
        model = torch.nn.parallel.DistributedDataParallel(
            model, device_ids=[args.gpu])
        model_without_ddp = model.module

    if args.resume:
        checkpoint = torch.load(args.resume, map_location='cpu')
        model_without_ddp.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        lr_scheduler.load_state_dict(checkpoint['lr_scheduler'])
        args.start_epoch = checkpoint['epoch'] + 1

    if args.test_only:
        evaluate(model, criterion, data_loader_test, device=device)
        return

    print("Start training")
    start_time = time.time()
    for epoch in range(args.start_epoch, args.epochs):
        if args.distributed:
            train_sampler.set_epoch(epoch)
        train_one_epoch(model, criterion, optimizer, data_loader, device,
                        epoch, args.print_freq)
        lr_scheduler.step()
        evaluate(model, criterion, data_loader_test, device=device)
        if args.output_dir:
            checkpoint = {
                'model': model_without_ddp.state_dict(),
                'optimizer': optimizer.state_dict(),
                'lr_scheduler': lr_scheduler.state_dict(),
                'epoch': epoch,
                'args': args
            }
            utils.save_on_master(
                checkpoint,
                os.path.join(args.output_dir, 'model_{}.pth'.format(epoch)))
            utils.save_on_master(
                checkpoint, os.path.join(args.output_dir, 'checkpoint.pth'))

    total_time = time.time() - start_time
    total_time_str = str(datetime.timedelta(seconds=int(total_time)))
    print('Training time {}'.format(total_time_str))
Beispiel #21
0
                                              batch_size=batch_size,
                                              shuffle=True,
                                              num_workers=workers,
                                              pin_memory=True)

#####################################################################
num_batches = len(
    train_loader)  # now we can get your batches since dataset is chosen

#initialise your model here
if args.model_name == "resnet50":
    target_model = resnet.resnet50(num_classes=num_classes, pretrained=True)
    nf = target_model.fc.in_features
    target_model.fc = torch.nn.Linear(nf, num_classes)
elif args.model_name == "resnet152":
    target_model = resnet.resnet152(num_classes=num_classes, pretrained=True)
    nf = target_model.fc.in_features
    target_model.fc = torch.nn.Linear(nf, num_classes)
else:
    target_model = torchvision.models.densenet161(num_classes=num_classes,
                                                  pretrained=True)
    nf = target_model.classifier.in_features
    target_model.classifier = torch.nn.Linear(nf, num_classes)

if args.adam:
    optimizer = torch.optim.Adam(target_model.parameters())
    scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=1)
else:
    optimizer = torch.optim.SGD(target_model.parameters(),
                                lr=learning_rate,
                                momentum=momentum_mod)
Beispiel #22
0
def resnet152():
    """Load resnet50"""
    return resnet.resnet152(pretrained=True, progress=True)
Beispiel #23
0
        model = resnet18(pretrained=True, fc_flag=True)
        torch.save(model.state_dict(), 'pretrainedfile/' + 'resnet18.pth')
    elif model_name == 'resnet34':
        model = resnet34(pretrained=True, fc_flag=True)
        torch.save(model.state_dict(), 'pretrainedfile/' + 'resnet34.pth')

    elif model_name == 'resnet50':
        model = resnet50(pretrained=True, fc_flag=True)
        torch.save(model.state_dict(), 'pretrainedfile/' + 'resnet50.pth')

    elif model_name == 'resnet101':
        model = resnet101(pretrained=True, fc_flag=True)
        torch.save(model.state_dict(), 'pretrainedfile/' + 'resnet101.pth')

    elif model_name == 'resnet152':
        model = resnet152(pretrained=True, fc_flag=True)
        torch.save(model.state_dict(), 'pretrainedfile/' + 'resnet152.pth')
    else:
        print("model name is error")

    checkpoint = torch.load('pretrainedfile/' + model_name + '.pth')
    model.load_state_dict(checkpoint)
    print("The original {} model parameter: ".format(model_name),
          netParams(model))

    if model_name == 'resnet18':
        model_nofc = resnet18(pretrained=None, fc_flag=False)
    elif model_name == 'resnet34':
        model_nofc = resnet34(pretrained=None, fc_flag=False)

    elif model_name == 'resnet50':
Beispiel #24
0
def main():
    args = get_arguments()

    if args.seed is not None:
        random.seed(args.seed)
        torch.manual_seed(args.seed)
        cudnn.deterministic = True

    BATCH_SIZE = args.b
    GPU = args.gpu
    ROOT_DIR = args.root_dir
    MEAN = [0.485, 0.456, 0.406]
    STD = [0.229, 0.224, 0.225]

    os.environ['CUDA_VISIBLE_DEVICES'] = GPU
    if torch.cuda.is_available():
        print('using Cuda devices, num:', torch.cuda.device_count())

    if not args.evaluate:
        if not os.path.exists(args.save_dir):
            os.makedirs(args.save_dir)

    def ToCudaVariable(xs, volatile=False, requires_grad=True):
        if torch.cuda.is_available():
            return [
                Variable(x.cuda(),
                         volatile=volatile,
                         requires_grad=requires_grad) for x in xs
            ]
        else:
            return [
                Variable(x, volatile=volatile, requires_grad=requires_grad)
                for x in xs
            ]

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

    if args.arch == 'resnet18_base':
        model = nn.DataParallel(
            resnet.resnet18(pretrained=True if not args.resume else False,
                            num_classes=6,
                            use_att=args.use_att,
                            att_mode=args.att_mode).to(device))
    elif args.arch == 'resnet34_base':
        model = nn.DataParallel(
            resnet.resnet34(
                pretrained=not args.no_pretrain if not args.resume else False,
                num_classes=6,
                use_att=args.use_att,
                att_mode=args.att_mode).to(device))
    elif args.arch == 'resnet50_base':
        model = nn.DataParallel(
            resnet.resnet50(
                pretrained=not args.no_pretrain if not args.resume else False,
                num_classes=6,
                use_att=args.use_att,
                att_mode=args.att_mode).to(device))
    elif args.arch == 'resnet101_base':
        model = nn.DataParallel(
            resnet.resnet101(
                pretrained=not args.no_pretrain if not args.resume else False,
                num_classes=6,
                use_att=args.use_att,
                att_mode=args.att_mode).to(device))
    elif args.arch == 'resnet152_base':
        model = nn.DataParallel(
            resnet.resnet152(
                pretrained=not args.no_pretrain if not args.resume else False,
                num_classes=6,
                use_att=args.use_att,
                att_mode=args.att_mode).to(device))

    print(model)
    print('Number of model parameters: {}'.format(
        sum([p.data.nelement() for p in model.parameters()])))

    criterion = nn.CrossEntropyLoss().to(device)
    # att_params = [p for n,p in model.named_parameters() if n.startswith('module.att') and p.requires_grad]
    # non_att_params = [p for n,p in model.named_parameters() if not n.startswith('module.att') and p.requires_grad]
    # params = [{'params': non_att_params, 'lr': args.lr / 10.0}, {'params': att_params}]

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

    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_acc1 = checkpoint['best_acc1']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
            print('=> best accuracy {}'.format(best_acc1))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    train_img_transform = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=MEAN, std=STD)
    ])
    train_dataset = TrashDataset(ROOT_DIR, train_img_transform, 'train')
    train_loader = DataLoader(train_dataset,
                              batch_size=BATCH_SIZE,
                              shuffle=True,
                              num_workers=args.workers,
                              pin_memory=True)

    val_img_transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=MEAN, std=STD)
    ])
    val_dataset = TrashDataset(ROOT_DIR, val_img_transform, 'val')
    val_loader = DataLoader(val_dataset,
                            batch_size=BATCH_SIZE,
                            shuffle=False,
                            num_workers=args.workers,
                            pin_memory=True)

    test_img_transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=MEAN, std=STD)
    ])
    test_dataset = TrashDataset(ROOT_DIR, test_img_transform, 'test')
    test_loader = DataLoader(test_dataset,
                             batch_size=BATCH_SIZE,
                             shuffle=False,
                             num_workers=args.workers,
                             pin_memory=True)

    if args.evaluate:
        # validate(args, val_loader, model, criterion, device)
        test(args, test_loader, model, criterion, device)
        return

    best_acc1 = 0
    for epoch in range(args.start_epoch, args.epochs):
        adjust_learning_rate(args, optimizer, epoch, args.adjust_freq)

        train(args, train_loader, model, criterion, optimizer, epoch, device)

        acc1 = validate(args, val_loader, model, criterion, device)

        is_best = acc1 > best_acc1
        best_acc1 = max(acc1, best_acc1)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'arch': args.arch,
                'state_dict': model.state_dict(),
                'best_acc1': best_acc1,
                'optimizer': optimizer.state_dict(),
            }, is_best, args.save_dir)
Beispiel #25
0
                                              batch_size=batch_size,
                                              shuffle=True,
                                              num_workers=workers,
                                              pin_memory=True)

#####################################################################
num_batches = len(
    train_loader)  # now we can get your batches since dataset is chosen

#initialise your model here
if args.model_name == "resnet50":
    target_model = resnet.resnet50(pretrained=True)
    nf = target_model.fc.in_features
    target_model.fc = torch.nn.Linear(nf, num_classes)
elif args.model_name == "resnet152":
    target_model = resnet.resnet152(pretrained=True)
    nf = target_model.fc.in_features
    target_model.fc = torch.nn.Linear(nf, num_classes)
else:
    target_model = torchvision.models.densenet161(pretrained=True)
    nf = target_model.classifier.in_features
    target_model.classifier = torch.nn.Linear(nf, num_classes)

if args.adam:
    optimizer = torch.optim.Adam(target_model.parameters())
    scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=1)
else:
    optimizer = torch.optim.SGD(target_model.parameters(),
                                lr=learning_rate,
                                momentum=momentum_mod)
    scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.999)
Beispiel #26
0
def main_worker(gpu, ngpus_per_node, args):
    global best_acc1
    args.gpu = gpu

    if args.gpu is not None:
        print("Use GPU: {} for training".format(args.gpu))

    if args.distributed:
        if args.dist_url == "env://" and args.rank == -1:
            args.rank = int(os.environ["RANK"])
        if args.multiprocessing_distributed:
            # For multiprocessing distributed training, rank needs to be the
            # global rank among all the processes
            args.rank = args.rank * ngpus_per_node + gpu
        dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
                                world_size=args.world_size, rank=args.rank)

# create model
#####################################################################################

    if args.pretrained:
        if args.arch.startswith('efficientnet-b'):
            print('=> using pre-trained {}'.format(args.arch))
            model = EfficientNet.from_pretrained(args.arch, advprop=args.advprop)

        else:
            print("=> using pre-trained model '{}'".format(args.arch))
            model = models.__dict__[args.arch](pretrained=True)
    else:
        if args.arch.startswith('efficientnet-b'):
            print("=> creating model {}".format(args.arch))
            model = EfficientNet.from_name(args.arch)
        elif args.arch.startswith('Dense'):
            print("=> creating model {}".format(args.arch))
            model = DenseNet40()
        else:
            print("=> creating model '{}'".format(args.arch))
            model = models.__dict__[args.arch]()

    # create teacher model
    if args.kd:
        print('=> loading teacher model')
        if args.teacher_arch.startswith('efficientnet-b'):
            teacher = EfficientNet.from_pretrained(args.teacher_arch)
            teacher.eval()
            print('=> {} loaded'.format(args.teacher_arch))

        elif args.teacher_arch.startswith('resnext101_32'):
            teacher = torch.hub.load('facebookresearch/WSL-Images', '{}_wsl'.format(args.teacher_arch))
            teacher.eval()
            print('=> {} loaded'.format(args.teacher_arch))
        elif args.overhaul:
            teacher = resnet.resnet152(pretrained=True)
        else:
            teacher = models.__dict__[args.teacher_arch](pretrained=True)
            teacher.eval()
            print('=> {} loaded'.format(args.teacher_arch))

        if args.overhaul:
            print('=> using overhaul distillation')
            d_net = Distiller(teacher, model)

    if args.distributed:
        if args.gpu is not None:
            torch.cuda.set_device(args.gpu)
            model.cuda(args.gpu)
            args.batch_size = int(args.batch_size / ngpus_per_node)
            args.workers = int((args.workers + ngpus_per_node - 1) / ngpus_per_node)
            model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
        else:
            model.cuda()
            model = torch.nn.parallel.DistributedDataParallel(model)
    elif args.gpu is not None:
        torch.cuda.set_device(args.gpu)
        model = model.cuda(args.gpu)
    else:
        # DataParallel will divide and allocate batch_size to all available GPUs
        if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
            model.features = torch.nn.DataParallel(model.features)
            model.cuda()
            model.cuda()
        else:
            model = torch.nn.DataParallel(model).cuda()
            if args.kd:
                teacher = torch.nn.DataParallel(teacher).cuda()
                if args.overhaul:
                    d_net = torch.nn.DataParallel(d_net).cuda()

    if args.pretrained:
        if args.arch.startswith('efficientnet-b'):
            loc = 'cuda:{}'.format(args.gpu)
            checkpoint = torch.load(args.pth_path, map_location=loc)
            model.load_state_dict(checkpoint['state_dict'])
#####################################################################################


# define loss function (criterion) and optimizer, scheduler
#####################################################################################
    if args.kd:
        criterion = kd_criterion
        if args.overhaul:
            criterion = nn.CrossEntropyLoss().cuda(args.gpu)
    else:
        criterion = nn.CrossEntropyLoss().cuda(args.gpu)


    if args.overhaul:
        optimizer = torch.optim.SGD(list(model.parameters()) + list(d_net.module.Connectors.parameters()), args.lr,
                                    momentum=args.momentum,
                                    weight_decay=args.weight_decay)  # nesterov
    else:
        optimizer = torch.optim.SGD(model.parameters(), args.lr,
                                    momentum=args.momentum,
                                    weight_decay=args.weight_decay)
        optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=args.weight_decay, amsgrad=False)
        scheduler = CosineAnnealingLR(optimizer, T_max=args.epochs * int(1281167 / args.batch_size), eta_min=0,
                                      last_epoch=-1)
        args.lr = 0.048
        args.bs = 384
        optimizer = torch.optim.RMSprop(
            model.parameters(), lr=args.lr, alpha=0.9, eps=.001,
            momentum=0.9, weight_decay=args.weight_decay)

        from typing import Dict, Any
        class Scheduler:
            """ Parameter Scheduler Base Class
            A scheduler base class that can be used to schedule any optimizer parameter groups.
            Unlike the builtin PyTorch schedulers, this is intended to be consistently called
            * At the END of each epoch, before incrementing the epoch count, to calculate next epoch's value
            * At the END of each optimizer update, after incrementing the update count, to calculate next update's value
            The schedulers built on this should try to remain as stateless as possible (for simplicity).
            This family of schedulers is attempting to avoid the confusion of the meaning of 'last_epoch'
            and -1 values for special behaviour. All epoch and update counts must be tracked in the training
            code and explicitly passed in to the schedulers on the corresponding step or step_update call.
            Based on ideas from:
             * https://github.com/pytorch/fairseq/tree/master/fairseq/optim/lr_scheduler
             * https://github.com/allenai/allennlp/tree/master/allennlp/training/learning_rate_schedulers
            """

            def __init__(self,
                         optimizer: torch.optim.Optimizer,
                         param_group_field: str,
                         noise_range_t=None,
                         noise_type='normal',
                         noise_pct=0.67,
                         noise_std=1.0,
                         noise_seed=None,
                         initialize: bool = True) -> None:
                self.optimizer = optimizer
                self.param_group_field = param_group_field
                self._initial_param_group_field = f"initial_{param_group_field}"
                if initialize:
                    for i, group in enumerate(self.optimizer.param_groups):
                        if param_group_field not in group:
                            raise KeyError(f"{param_group_field} missing from param_groups[{i}]")
                        group.setdefault(self._initial_param_group_field, group[param_group_field])
                else:
                    for i, group in enumerate(self.optimizer.param_groups):
                        if self._initial_param_group_field not in group:
                            raise KeyError(f"{self._initial_param_group_field} missing from param_groups[{i}]")
                self.base_values = [group[self._initial_param_group_field] for group in self.optimizer.param_groups]
                self.metric = None  # any point to having this for all?
                self.noise_range_t = noise_range_t
                self.noise_pct = noise_pct
                self.noise_type = noise_type
                self.noise_std = noise_std
                self.noise_seed = noise_seed if noise_seed is not None else 42
                self.update_groups(self.base_values)

            def state_dict(self) -> Dict[str, Any]:
                return {key: value for key, value in self.__dict__.items() if key != 'optimizer'}

            def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
                self.__dict__.update(state_dict)

            def get_epoch_values(self, epoch: int):
                return None

            def get_update_values(self, num_updates: int):
                return None

            def step(self, epoch: int, metric: float = None) -> None:
                self.metric = metric
                values = self.get_epoch_values(epoch)
                if values is not None:
                    values = self._add_noise(values, epoch)
                    self.update_groups(values)

            def step_update(self, num_updates: int, metric: float = None):
                self.metric = metric
                values = self.get_update_values(num_updates)
                if values is not None:
                    values = self._add_noise(values, num_updates)
                    self.update_groups(values)

            def update_groups(self, values):
                if not isinstance(values, (list, tuple)):
                    values = [values] * len(self.optimizer.param_groups)
                for param_group, value in zip(self.optimizer.param_groups, values):
                    param_group[self.param_group_field] = value

            def _add_noise(self, lrs, t):
                if self.noise_range_t is not None:
                    if isinstance(self.noise_range_t, (list, tuple)):
                        apply_noise = self.noise_range_t[0] <= t < self.noise_range_t[1]
                    else:
                        apply_noise = t >= self.noise_range_t
                    if apply_noise:
                        g = torch.Generator()
                        g.manual_seed(self.noise_seed + t)
                        if self.noise_type == 'normal':
                            while True:
                                # resample if noise out of percent limit, brute force but shouldn't spin much
                                noise = torch.randn(1, generator=g).item()
                                if abs(noise) < self.noise_pct:
                                    break
                        else:
                            noise = 2 * (torch.rand(1, generator=g).item() - 0.5) * self.noise_pct
                        lrs = [v + v * noise for v in lrs]
                return lrs
        class StepLRScheduler(Scheduler):
            """
            """

            def __init__(self,
                         optimizer: torch.optim.Optimizer,
                         decay_t: float,
                         decay_rate: float = 1.,
                         warmup_t=0,
                         warmup_lr_init=0,
                         t_in_epochs=True,
                         noise_range_t=None,
                         noise_pct=0.67,
                         noise_std=1.0,
                         noise_seed=42,
                         initialize=True,
                         ) -> None:
                super().__init__(
                    optimizer, param_group_field="lr",
                    noise_range_t=noise_range_t, noise_pct=noise_pct, noise_std=noise_std, noise_seed=noise_seed,
                    initialize=initialize)

                self.decay_t = decay_t
                self.decay_rate = decay_rate
                self.warmup_t = warmup_t
                self.warmup_lr_init = warmup_lr_init
                self.t_in_epochs = t_in_epochs
                if self.warmup_t:
                    self.warmup_steps = [(v - warmup_lr_init) / self.warmup_t for v in self.base_values]
                    super().update_groups(self.warmup_lr_init)
                else:
                    self.warmup_steps = [1 for _ in self.base_values]

            def _get_lr(self, t):
                if t < self.warmup_t:
                    lrs = [self.warmup_lr_init + t * s for s in self.warmup_steps]
                else:
                    lrs = [v * (self.decay_rate ** (t // self.decay_t)) for v in self.base_values]
                return lrs

            def get_epoch_values(self, epoch: int):
                if self.t_in_epochs:
                    return self._get_lr(epoch)
                else:
                    return None

            def get_update_values(self, num_updates: int):
                if not self.t_in_epochs:
                    return self._get_lr(num_updates)
                else:
                    return None

        scheduler = StepLRScheduler(
            optimizer,
            decay_t=2.4,
            decay_rate=0.97,
            warmup_lr_init=1e-6,
            warmup_t=3,
            noise_range_t=None,
            noise_pct=getattr(args, 'lr_noise_pct', 0.67),
            noise_std=getattr(args, 'lr_noise_std', 1.),
            noise_seed=getattr(args, 'seed', 42),
        )
    # scheduler = MultiStepLR(optimizer, milestones=args.schedule, gamma=args.gamma)
    # milestone = np.ceil(np.arange(0,300,2.4))

    # scheduler = MultiStepLR(optimizer, milestones=[30,60,90,120,150,180,210,240,270], gamma=0.1)
    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            if args.gpu is None:
                checkpoint = torch.load(args.resume)
            else:
                # Map model to be loaded to specified single gpu.
                loc = 'cuda:{}'.format(args.gpu)
                checkpoint = torch.load(args.resume, map_location=loc)
            args.start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            if args.gpu is not None:
                # best_acc1 may be from a checkpoint from a different GPU
                best_acc1 = best_acc1.to(args.gpu)
            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.advprop:
        normalize = transforms.Lambda(lambda img: img * 2.0 - 1.0)
    else:
        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])


    train_dataset = ImageFolder_iid(
        traindir,
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(p=0.5),
            ImageNetPolicy(),
            transforms.ToTensor(),
            normalize,
        ]))

    if args.distributed:
        train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
    else:
        train_sampler = None

    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None),
        num_workers=args.workers, pin_memory=True, sampler=train_sampler)

    val_loader = torch.utils.data.DataLoader(
        ImageFolder_iid(valdir, transforms.Compose([
            transforms.Resize(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, args)

# Start training
#####################################################################################
    best_acc1 = 0
    teacher_name = ''
    student_name = ''
    for epoch in range(args.start_epoch, args.epochs):

        if args.distributed:
            train_sampler.set_epoch(epoch)

        # train for one epoch
        if args.kd:
            if args.overhaul:
                train_with_overhaul(train_loader, d_net, optimizer, criterion, epoch, args)
                acc1 = validate_overhaul(val_loader, model, criterion, epoch, args)
            else:
                train_kd(train_loader, teacher, model, criterion, optimizer, epoch, args)
                acc1 = validate_kd(val_loader, teacher, model, criterion, args)

                teacher_name = teacher.module.__class__.__name__

        else:
            student_name = model.module.__class__.__name__
            train(train_loader, model, criterion, optimizer, epoch, args)
            acc1 = validate(val_loader, model, criterion, args)

        # remember best acc@1 and save checkpoint
        #writer.add_scalars('acc1', acc1, epoch)
        is_best = acc1 > best_acc1
        if acc1 < 65:
            print(colored('not saving... accuracy smaller than 65','green'))
            is_best = False
        best_acc1 = max(acc1, best_acc1)


        if not args.multiprocessing_distributed or (args.multiprocessing_distributed
                and args.rank % ngpus_per_node == 0):
            save_checkpoint({
                'epoch': epoch + 1,
                'arch': args.arch,
                'state_dict': model.state_dict(),
                'best_acc1': best_acc1,
                'optimizer' : optimizer.state_dict(),
            }, is_best, teacher_name=teacher_name, student_name=student_name, save_path=args.save_path, acc=acc1)

        scheduler.step(epoch)
Beispiel #27
0
import torchvision
from torchvision import datasets, models
import matplotlib.pyplot as plt
import time
import os
import sys
import threading
import resnet
import transforms

# for interactive purpose
plt.ion()

model_sel = {
    'inception': torchvision.models.inception.inception_v3(pretrained=True),
    'resnet152': resnet.resnet152(pretrained=True),
    'resnet101': resnet.resnet101(pretrained=True),
    'resnet50': resnet.resnet50(pretrained=True),
    'resnet18': resnet.resnet18(pretrained=True),
}

args = {
    # arch params
    'data_dir': '/home/ubuntu/jd_ai/data_crop',
    'test_data_dir': '/home/ubuntu/jd_ai/data/test',
    'model_name': 'resnet101',
    'input_size': 400,
    'output_size': 30,
    'batch_size': 16,
    'requires_grad': True,
    'use_gpu': torch.cuda.is_available(),
Beispiel #28
0
def generate_model(opt):
    assert opt.model in [
        'resnet', 'preresnet', 'wideresnet', 'resnext', 'densenet'
    ]

    if opt.model == 'resnet':
        assert opt.model_depth in [10, 18, 34, 50, 101, 152, 200]

        from resnet import get_fine_tuning_parameters

        if opt.model_depth == 10:
            model = resnet.resnet10(num_classes=opt.n_classes,
                                    shortcut_type=opt.resnet_shortcut,
                                    sample_size=opt.sample_size,
                                    sample_duration=opt.sample_duration)
        elif opt.model_depth == 18:
            model = resnet.resnet18(num_classes=opt.n_classes,
                                    shortcut_type=opt.resnet_shortcut,
                                    sample_size=opt.sample_size,
                                    sample_duration=opt.sample_duration)
        elif opt.model_depth == 34:
            model = resnet.resnet34(num_classes=opt.n_classes,
                                    shortcut_type=opt.resnet_shortcut,
                                    sample_size=opt.sample_size,
                                    sample_duration=opt.sample_duration)
        elif opt.model_depth == 50:
            model = resnet.resnet50(num_classes=opt.n_classes,
                                    shortcut_type=opt.resnet_shortcut,
                                    sample_size=opt.sample_size,
                                    sample_duration=opt.sample_duration)
        elif opt.model_depth == 101:
            model = resnet.resnet101(num_classes=opt.n_classes,
                                     shortcut_type=opt.resnet_shortcut,
                                     sample_size=opt.sample_size,
                                     sample_duration=opt.sample_duration)
        elif opt.model_depth == 152:
            model = resnet.resnet152(num_classes=opt.n_classes,
                                     shortcut_type=opt.resnet_shortcut,
                                     sample_size=opt.sample_size,
                                     sample_duration=opt.sample_duration)
        elif opt.model_depth == 200:
            model = resnet.resnet200(num_classes=opt.n_classes,
                                     shortcut_type=opt.resnet_shortcut,
                                     sample_size=opt.sample_size,
                                     sample_duration=opt.sample_duration)
    elif opt.model == 'wideresnet':
        assert opt.model_depth in [50]

        from models.wide_resnet import get_fine_tuning_parameters

        if opt.model_depth == 50:
            model = wide_resnet.resnet50(num_classes=opt.n_classes,
                                         shortcut_type=opt.resnet_shortcut,
                                         k=opt.wide_resnet_k,
                                         sample_size=opt.sample_size,
                                         sample_duration=opt.sample_duration)
    elif opt.model == 'resnext':
        assert opt.model_depth in [50, 101, 152]

        from models.resnext import get_fine_tuning_parameters

        if opt.model_depth == 50:
            model = resnext.resnet50(num_classes=opt.n_classes,
                                     shortcut_type=opt.resnet_shortcut,
                                     cardinality=opt.resnext_cardinality,
                                     sample_size=opt.sample_size,
                                     sample_duration=opt.sample_duration)
        elif opt.model_depth == 101:
            model = resnext.resnet101(num_classes=opt.n_classes,
                                      shortcut_type=opt.resnet_shortcut,
                                      cardinality=opt.resnext_cardinality,
                                      sample_size=opt.sample_size,
                                      sample_duration=opt.sample_duration)
        elif opt.model_depth == 152:
            model = resnext.resnet152(num_classes=opt.n_classes,
                                      shortcut_type=opt.resnet_shortcut,
                                      cardinality=opt.resnext_cardinality,
                                      sample_size=opt.sample_size,
                                      sample_duration=opt.sample_duration)
    elif opt.model == 'preresnet':
        assert opt.model_depth in [18, 34, 50, 101, 152, 200]

        from models.pre_act_resnet import get_fine_tuning_parameters

        if opt.model_depth == 18:
            model = pre_act_resnet.resnet18(
                num_classes=opt.n_classes,
                shortcut_type=opt.resnet_shortcut,
                sample_size=opt.sample_size,
                sample_duration=opt.sample_duration)
        elif opt.model_depth == 34:
            model = pre_act_resnet.resnet34(
                num_classes=opt.n_classes,
                shortcut_type=opt.resnet_shortcut,
                sample_size=opt.sample_size,
                sample_duration=opt.sample_duration)
        elif opt.model_depth == 50:
            model = pre_act_resnet.resnet50(
                num_classes=opt.n_classes,
                shortcut_type=opt.resnet_shortcut,
                sample_size=opt.sample_size,
                sample_duration=opt.sample_duration)
        elif opt.model_depth == 101:
            model = pre_act_resnet.resnet101(
                num_classes=opt.n_classes,
                shortcut_type=opt.resnet_shortcut,
                sample_size=opt.sample_size,
                sample_duration=opt.sample_duration)
        elif opt.model_depth == 152:
            model = pre_act_resnet.resnet152(
                num_classes=opt.n_classes,
                shortcut_type=opt.resnet_shortcut,
                sample_size=opt.sample_size,
                sample_duration=opt.sample_duration)
        elif opt.model_depth == 200:
            model = pre_act_resnet.resnet200(
                num_classes=opt.n_classes,
                shortcut_type=opt.resnet_shortcut,
                sample_size=opt.sample_size,
                sample_duration=opt.sample_duration)
    elif opt.model == 'densenet':
        assert opt.model_depth in [121, 169, 201, 264]

        from models.densenet import get_fine_tuning_parameters

        if opt.model_depth == 121:
            model = densenet.densenet121(num_classes=opt.n_classes,
                                         sample_size=opt.sample_size,
                                         sample_duration=opt.sample_duration)
        elif opt.model_depth == 169:
            model = densenet.densenet169(num_classes=opt.n_classes,
                                         sample_size=opt.sample_size,
                                         sample_duration=opt.sample_duration)
        elif opt.model_depth == 201:
            model = densenet.densenet201(num_classes=opt.n_classes,
                                         sample_size=opt.sample_size,
                                         sample_duration=opt.sample_duration)
        elif opt.model_depth == 264:
            model = densenet.densenet264(num_classes=opt.n_classes,
                                         sample_size=opt.sample_size,
                                         sample_duration=opt.sample_duration)

    if not opt.no_cuda:
        model = model.cuda()
        model = nn.DataParallel(model, device_ids=None)

        if opt.pretrain_path:
            print('loading pretrained model {}'.format(opt.pretrain_path))
            pretrain = torch.load(opt.pretrain_path)
            assert opt.arch == pretrain['arch']

            model.load_state_dict(pretrain['state_dict'])

            if opt.model == 'densenet':
                model.module.classifier = nn.Linear(
                    model.module.classifier.in_features,
                    opt.n_finetune_classes)
                model.module.classifier = model.module.classifier.cuda()
            else:
                model.module.fc = nn.Linear(model.module.fc.in_features,
                                            opt.n_finetune_classes)
                model.module.fc = model.module.fc.cuda()

            parameters = get_fine_tuning_parameters(model, opt.ft_begin_index)
            return model, parameters
    else:
        if opt.pretrain_path:
            print('loading pretrained model {}'.format(opt.pretrain_path))
            pretrain = torch.load(opt.pretrain_path)
            assert opt.arch == pretrain['arch']

            model.load_state_dict(pretrain['state_dict'])

            if opt.model == 'densenet':
                model.classifier = nn.Linear(model.classifier.in_features,
                                             opt.n_finetune_classes)
            else:
                model.fc = nn.Linear(model.fc.in_features,
                                     opt.n_finetune_classes)

            parameters = get_fine_tuning_parameters(model, opt.ft_begin_index)
            return model, parameters

    return model, model.parameters()
Beispiel #29
0
    def __init__(self,
                 backbone='resnet',
                 layers=50,
                 bins=(1, 2, 3, 6),
                 dropout=0.1,
                 classes=2,
                 zoom_factor=8,
                 use_ppm=False,
                 use_softmax=True,
                 use_aux=False,
                 pretrained=True,
                 syncbn=True,
                 group_size=8,
                 group=None):
        super(PSPNet, self).__init__()
        assert layers in [50, 101, 152]
        assert 2048 % len(bins) == 0
        assert classes > 1
        assert zoom_factor in [1, 2, 4, 8]
        self.zoom_factor = zoom_factor
        self.use_ppm = use_ppm
        self.use_softmax = use_softmax
        self.use_aux = use_aux

        if backbone == 'resnet':
            import resnet as models
        elif backbone == 'ibnnet_a':
            import ibnnet_a as models
        elif backbone == 'ibnnet_b':
            import ibnnet_b as models
        else:
            raise NameError('Backbone type not defined!')

        if syncbn:
            from torchE.nn import SyncBatchNorm2d

            # from lib.syncbn import SynchronizedBatchNorm2d as BatchNorm
            def BNFunc(*args, **kwargs):
                return SyncBatchNorm2d(*args,
                                       **kwargs,
                                       group_size=group_size,
                                       group=group,
                                       sync_stats=True)

            BatchNorm = BNFunc
        else:
            from torch.nn import BatchNorm2d as BatchNorm
        models.BatchNorm = BatchNorm

        if layers == 50:
            resnet = models.resnet50(pretrained=pretrained)
        elif layers == 101:
            resnet = models.resnet101(pretrained=pretrained)
        else:
            resnet = models.resnet152(pretrained=pretrained)
        if backbone == 'ibnnet_b':
            self.layer0 = nn.Sequential(resnet.conv1, resnet.INCat0,
                                        resnet.relu, resnet.maxpool)
        else:
            self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu,
                                        resnet.maxpool)
        self.layer1, self.layer2, self.layer3, self.layer4 = resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4

        for n, m in self.layer4.named_modules():
            if 'conv2' in n and not 'convbnin.conv2' in n:
                m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
            elif 'downsample.0' in n:
                m.stride = (1, 1)

        fea_dim = 2048
        if use_ppm:
            self.ppm = PPM(fea_dim, int(fea_dim / len(bins)), bins, BatchNorm)
            fea_dim *= 2
        self.cls = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),
            nn.Conv2d(fea_dim, 20, kernel_size=1, bias=False),
        )
Beispiel #30
0
                                           batch_size=args.batch_size,
                                           shuffle=True,
                                           **kwargs)
test_loader = torch.utils.data.DataLoader(datasets.CIFAR100(
    './data',
    train=False,
    transform=transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465),
                             (0.2023, 0.1994, 0.2010)),
    ])),
                                          batch_size=args.test_batch_size,
                                          shuffle=True,
                                          **kwargs)

model = models.resnet152(False, num_classes=100)
if args.cuda:
    model.cuda()


def train(epoch):
    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr * args.lr_decay[(int)((epoch - 1) / 50)],
                          momentum=args.momentum,
                          weight_decay=args.l2_decay)
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        optimizer.zero_grad()