def load_model(self, pretrained=False):
        if pretrained:
            model = models.deeplabv3_resnet101(pretrained=True)
        else:
            model = models.deeplabv3_resnet101()

        model.to(self.device)
        model.eval()
        return model
예제 #2
0
    def setUp(self) -> None:
        super().setUp()
        self._model = Model(arch=deeplabv3_resnet101(pretrained=True))
        self._model_ema = EMA_Model(Model(deeplabv3_resnet101(False)),
                                    alpha=0.9,
                                    weight_decay=1e-4)
        # self._model_ema._model.load_state_dict(self._model.state_dict())
        self._device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")
        self._img = (ToTensor()(
            Image.open("img1.jpg").convert("RGB")).unsqueeze(0).to(
                self._device))

        self._model.to(self._device)
        self._model_ema.to(self._device)
예제 #3
0
def main():
    print('Loading training and validation set images...')

    # Getting training and validation data
    dataloaders = get_dataloaders(random_transform=True,
                                  random_rotate=True,
                                  random_hflip=True,
                                  random_vflip=True,
                                  train_ratio=0.8)

    print('Starting training of model...')

    # # The way the winning submission was trained, takes about ~3 hours with GPU
    # model = deeplabv3_resnet101(num_classes=1, pretrained=False)
    # model, epoch_stats, model_path, best_f1 = train_model(
    #     model, dataloaders, num_epochs=5, learning_rate=0.0002, deeplab=True)

    print('Loading best model and writing predictions for test set...')

    # Getting the best model from training and using it on test set
    empty_model = deeplabv3_resnet101(num_classes=1, pretrained=False)
    model_path = 'trained_models/20191209-020809-net.pth'  # submission: 31396
    write_predictions(empty_model,
                      model_path,
                      deeplab=True,
                      patched=False,
                      image_path='predictions')

    print('Creating csv submission from prediction images...')

    # Creating csv
    create_submission()

    print('Done.')
예제 #4
0
 def test_deeplab(self):
     x = Variable(torch.randn(BATCH_SIZE, 3, 224, 224).fill_(1.0))
     self.exportTest(toC(
         deeplabv3_resnet101(pretrained=False, pretrained_backbone=False)),
                     toC(x),
                     rtol=1e-3,
                     atol=1e-5)
예제 #5
0
def discriminator(model='MobileNet', n_classes=3):
    default_scope = True
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print("Device: %s" % device)

    model_dict = {}
    if model == 'DeepLab':
        model_dict[model] = deeplabv3_resnet101(pretrained=True,
                                                progress=True,
                                                num_classes=21,
                                                aux_loss=None)
        if default_scope:
            grad_check(model_dict[model])
        else:
            grad_check(model_dict[model], model_layers='All')
        model_dict[model].classifier[-1] = torch.nn.Conv2d(
            256, n_classes, kernel_size=(1, 1),
            stride=(1, 1)).requires_grad_()
        model_dict[model].aux_classifier[-1] = torch.nn.Conv2d(
            256, n_classes, kernel_size=(1, 1),
            stride=(1, 1)).requires_grad_()

    if model == "MobileNet":
        model_dict[model] = _segm_mobilenet('deeplabv3',
                                            'mobile_net',
                                            output_stride=8,
                                            num_classes=n_classes,
                                            pretrained_backbone=True)
        grad_check(model_dict[model], model_layers='All')
    # Setup visualization

    for model_name, model in model_dict.items():
        print(model_name)
        return model_dict
예제 #6
0
    def __init__(self):
        super(Encoder101, self).__init__()

        self.resnet = deeplabv3_resnet101(pretrained=True)
        self.conv1_p = nn.Conv2d(1,
                                 64,
                                 kernel_size=7,
                                 stride=2,
                                 padding=3,
                                 bias=True)

        resnet = fcn_resnet101(pretrained=True)
        self.conv1 = resnet.backbone.conv1
        self.bn1 = resnet.backbone.bn1
        self.relu = resnet.backbone.relu  # 1/2, 64
        self.maxpool = resnet.backbone.maxpool

        self.res2 = resnet.backbone.layer1  # 1/4, 256
        self.res3 = resnet.backbone.layer2  # 1/8, 512
        self.res4 = resnet.backbone.layer3  # 1/16, 1024
        self.res5 = resnet.backbone.layer4  # 1/32, 2048

        self.register_buffer(
            'mean',
            torch.FloatTensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1))
        self.register_buffer(
            'std',
            torch.FloatTensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1))
예제 #7
0
def load_seg_model(cfg, load_model_only=False):
    modeltype = cfg['training']['model']
    if modeltype == 'fcn_resnet101':
        n_channels = len(cfg['data']['input_classes'])
        n_outputs = len(cfg['data']['output_classes'])
        model = fcn_resnet101(pretrained=False, num_classes=n_outputs)
        model.backbone.conv1 = nn.Conv2d(n_channels, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
    elif modeltype == 'deeplabv3_resnet101':
        n_channels = len(cfg['data']['input_classes'])
        n_outputs = len(cfg['data']['output_classes'])
        aux_loss = cfg['training']['aux_loss'] is not False
        model = deeplabv3_resnet101(pretrained=False, num_classes=n_outputs, aux_loss=aux_loss)
        model.backbone.conv1 = nn.Conv2d(n_channels, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
    elif modeltype == 'higher_hrnet':
        hrnet_cfg = get_hrnet_cfg(cfg)
        model = get_hrnet_model(hrnet_cfg)
    else:
        raise ValueError()

    if modelpath := cfg['resume'].get('path', None):
        state = torch.load(modelpath)
        model.load_state_dict(state['state_dict'])
        if load_model_only:
            return model
        starting_epoch = state['epoch']
        if conf_epoch := cfg['resume'].get('epoch', None):
            print(
                f"WARNING: Loaded model trained for {starting_epoch - 1} epochs but config explicitly overrides to {conf_epoch}")
            starting_epoch = conf_epoch
예제 #8
0
    def __init__(
        self,
        segmenter: nn.Module = segmentation.deeplabv3_resnet101(
            pretrained=True),
        keypoint_estimator: nn.Module = detection.keypointrcnn_resnet50_fpn(
            pretrained=True),
        input_height: int = 600,
    ):
        self.segmenter = segmenter
        self.keypoint_estimator = keypoint_estimator
        self.input_height = input_height

        self.cache = {}
        self.cache["keypoints"] = {}
        self.cache["masks"] = {}
        self.cache["images"] = {}
        # Move to GPUs if available
        # BodyPoseEstimator handles this for itself
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")
        if self.segmenter:
            segmenter.to(self.device)
        self.segmenter.eval()
        if self.keypoint_estimator:
            keypoint_estimator.to(self.device)
        self.keypoint_estimator.eval()
예제 #9
0
def get_model(model_name: str, device, num_classes: int, pretrained: bool = False) -> torch.nn:
    """
    Gets the model requested from the config file

    :param model_name: name of model used in the program
    :param device: name of device model is hosted on
    :param num_classes: number of classes in the model (including background 0,0,0)
    :param pretrained: boolean of starting on a pretrained model
    :return: the nn.model to be trained
    """
    print(pretrained)
    model_dict = {'fcn_resnet50': models.fcn_resnet50(pretrained=pretrained, num_classes=num_classes).to(device).eval(),
                  'fcn_resnet101': models.fcn_resnet101(pretrained=pretrained,
                                                        num_classes=num_classes).to(device).eval(),
                  'deeplabv3_resnet50': models.deeplabv3_resnet50(pretrained=pretrained,
                                                                  num_classes=num_classes).to(device).eval(),
                  'deeplabv3_resnet101': models.deeplabv3_resnet101(pretrained=pretrained,
                                                                    num_classes=num_classes).to(device).eval(),
                  'deeplabv3_mobilenet_v3_large': models.deeplabv3_mobilenet_v3_large(pretrained=pretrained,
                                                                                      num_classes=num_classes).to(
                      device).eval(),
                  'lraspp_mobilenet_v3_large': models.lraspp_mobilenet_v3_large(pretrained=pretrained,
                                                                                num_classes=num_classes).to(
                      device).eval()
                  }
    try:
        model = model_dict[model_name]
    except KeyError:
        print(f"KeyError, model_name is not valid allowable names are: {model_dict.keys()}")
        model = None
    model = model.eval()
    return model
예제 #10
0
 def __init__(self):
     super(DeepLab, self).__init__()
     # define model
     self.model = deeplabv3_resnet101(num_classes=7, progress=True)
     self.model.classifier[4] = nn.Conv2d(in_channels=256,
                                          out_channels=8,
                                          kernel_size=1,
                                          stride=1)
예제 #11
0
def loadModel():
    global model
    # model = torch.hub.load('pytorch/vision:v0.5.0', 'deeplabv3_resnet101',
    #                         num_classes=NUM_LABELS, pretrained=False, progress=True)
    model = deeplabv3_resnet101(num_classes=NUM_LABELS)
    model.train()
    print(model)
    return model
예제 #12
0
파일: utils.py 프로젝트: bonlime/opencities
def tv_deeplab(arch, **kwargs):
    if arch == "resnet50":
        return tv_segm.deeplabv3_resnet50(pretrained=False,
                                          progress=False,
                                          num_classes=1)
    elif arch == "resnet101":
        return tv_segm.deeplabv3_resnet101(pretrained=True,
                                           progress=False,
                                           num_classes=1)
예제 #13
0
def load_model():
    global model
    if opt.model == 50:
        model = deeplabv3_resnet50(num_classes=NUM_LABELS, pretrained=False)
    elif opt.model == 101:
        model = deeplabv3_resnet101(num_classes=NUM_LABELS, pretrained=False)
    else:
        raise Exception(f'Invalid model: {opt.model}')
    model = model.to(device)
예제 #14
0
    def __init__(self, num_classes, pretrained=True):
        super(DeepLabv3ResNet101Wrapper, self).__init__()

        self.deeplab = deeplabv3_resnet101(pretrained=False, num_classes=num_classes)
        if pretrained:
            state_dict = model_zoo.load_url(self.URL)
            final_keys = [k for k in state_dict.keys() if k.startswith('classifier.4')]
            for k in final_keys:
                del state_dict[k]
            self.deeplab.load_state_dict(state_dict, strict=False)
예제 #15
0
def get_network(params):
    if params.architecture == 'deeplab_resnet101':
        net = deeplabv3_resnet101(pretrained=params.pretrained)
    if params.architecture == 'deeplab_resnet50':
        net = deeplabv3_resnet50(pretrained=params.pretrained)
    if 'activation' in params.dict:
        net.classifier[-1] = nn.Sequential(nn.Conv2d(256, params.num_classes, 1, 1 ), get_activation_func(params.activation))
    else:
        net.classifier[-1] = nn.Conv2d(256, params.num_classes, 1, 1 )
    return net
예제 #16
0
 def __init__(self, in_channels=256, out_channels=1):
     super(SPM, self).__init__()
     self.ResNet = deeplabv3_resnet101(pretrained=True)
     self.ResNet.classifier[4] = nn.Conv2d(in_channels,
                                           out_channels,
                                           kernel_size=(1, 1),
                                           stride=(1, 1))
     self.ResNet.aux_classifier[4] = nn.Conv2d(in_channels,
                                               out_channels,
                                               kernel_size=(1, 1),
                                               stride=(1, 1))
def resnet101_deeplabv3_coco(num_classes=21, pretrained=True):
    if deeplabv3_resnet101 is None:
        raise NotImplementedError(
            'DeepLab v3 not available on this installation; requires PyTorch 1.1 and '
            'torchvision 0.3')
    deeplab = deeplabv3_resnet101(pretrained=False, num_classes=num_classes)
    if pretrained:
        url_coco = 'https://download.pytorch.org/models/deeplabv3_resnet101_coco-586e9e4e.pth'
        state_dict = model_zoo.load_url(url_coco)
        deeplab2._load_state_into_model(deeplab, state_dict)
    return deeplab3plus.DeepLabv3Wrapper(deeplab)
def load_model(device, model_type, model_path, n):
    if model_type == 50:
        model = deeplabv3_resnet50(num_classes=NUM_LABELS, pretrained=False)
    elif model_type == 101:
        model = deeplabv3_resnet101(num_classes=NUM_LABELS, pretrained=False)
    else:
        raise Exception(f'Invalid model: {model_type}')
    model = model.to(device)
    model.load_state_dict(
        torch.load(os.path.join(model_path, f'model_{n}.pth')))
    model.eval()
    return model
예제 #19
0
    def __init__(self, use_cuda=True):
        self.device = torch.device(
            'cuda' if torch.cuda.is_available() and use_cuda else 'cpu')
        self.model = deeplabv3_resnet101(pretrained=True)
        self.model.to(self.device)

        self.transform = transforms.Compose([
            ResizeCenterCrop(480),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225]),
        ])
예제 #20
0
    def test_VerboseModel(self):

        from torchvision.models.segmentation import deeplabv3_resnet101

        verbose_resnet = VerboseModel(deeplabv3_resnet101(pretrained=True),
                                      submodels=[
                                          'backbone',
                                          'classifier',
                                      ])
        dummy_input = torch.ones(10, 3, 256, 256)
        _ = verbose_resnet(dummy_input)

        pass
예제 #21
0
def get_model(num_classes):
    # load an instance segmentation model pre-trained pre-trained on COCO
    model = deeplabv3_resnet101(pretrained=True)

    for param in model.parameters():
        param.requires_grad = False

    deeplab_head_in_channels = model.classifier[0].convs[0][0].in_channels
    fcn_head_in_channels = model.classifier[1].in_channels

    model.classifier = DeepLabHead(deeplab_head_in_channels, num_classes)

    return model
예제 #22
0
    def __init__(self, num_classes):
        super().__init__()

        self.num_classes = num_classes

        self.segm = deeplabv3_resnet101(pretrained=True)

        for param in self.segm.parameters():
            param.requires_grad = False

        self.segm.classifier = DeepLabHead(in_channels=2048,
                                           num_classes=num_classes)

        self.segm.classifier.apply(_init_weights)
예제 #23
0
    def __init__(self,
                 n_channels=3,
                 n_classes=21,
                 softmax_out=True,
                 resnet_type=101,
                 pretrained=False):
        super(DeepLabv3, self).__init__()

        self.resnet_type = resnet_type
        self.n_channels = n_channels
        self.n_classes = n_classes
        self.pretrained = pretrained

        # Input conv is applied to convert the input to 3 ch depth
        self.inconv = None
        if n_channels != 3:
            self.inconv = FwdConv(n_channels, 3, kernel_size=1, padding=0)
        # Pre-trained model needs to be an identical network
        if pretrained:
            mid_classes = 21
        else:
            mid_classes = n_classes
        # Maind body
        if resnet_type == 50:
            self.deeplab = deeplabv3_resnet50(pretrained=False,
                                              num_classes=mid_classes)
            self.pretrained = False
        else:
            self.deeplab = deeplabv3_resnet101(pretrained=pretrained,
                                               num_classes=mid_classes)

        if n_classes != 21:
            self.deeplab.classifier[-1] = nn.Conv2d(256,
                                                    n_classes,
                                                    kernel_size=(1, 1),
                                                    stride=(1, 1))

            if self.deeplab.aux_classifier != None:
                self.deeplab.aux_classifier[-1] = nn.Conv2d(256,
                                                            n_classes,
                                                            kernel_size=(1, 1),
                                                            stride=(1, 1))

        # Softmax alternative
        self.has_softmax = softmax_out
        if softmax_out:
            self.softmax = nn.Softmax2d()
        else:
            self.softmax = None
def resnet101_deeplabv3_imagenet(num_classes=21, pretrained=True):
    if deeplabv3_resnet101 is None:
        raise NotImplementedError(
            'DeepLab v3 not available on this installation; requires PyTorch 1.1 and '
            'torchvision 0.3')
    deeplab = deeplabv3_resnet101(pretrained=False, num_classes=num_classes)
    if pretrained:
        # We can used the DeepLab2 ResNet-101 weights URL
        state_dict = model_zoo.load_url(deeplab2._RESNET_101_IMAGENET_URL)
        state_dict = {
            'backbone.{}'.format(key): value
            for key, value in state_dict.items()
        }
        deeplab2._load_state_into_model(deeplab, state_dict)
    return deeplab3plus.DeepLabv3Wrapper(deeplab)
예제 #25
0
 def __init__(self,
              end_module,
              keyword='out',
              pretrain=False,
              freeze=False):
     super(ResNet101_DeeplabV3, self).__init__()
     self.deeplab = models.deeplabv3_resnet101(pretrained=pretrain,
                                               progress=True,
                                               num_classes=21)
     if freeze:
         for module in self.deeplab.parameters():
             module.requires_grad = False
         print("module frozen.")
     self.end_module = end_module
     self.output = None
     self.key = keyword
예제 #26
0
    def __init__(self, num_classes=23):
        super(SegmentationNN, self).__init__()

        #######################################################################
        #                             YOUR CODE                               #
        #######################################################################
        self.num_classes = num_classes
        self.base = models.deeplabv3_resnet101(pretrained=True, progress=True)

        aux_classifier = list(self.base.aux_classifier.children())
        self.base.aux_classifier = nn.Sequential(*aux_classifier[:-1])
        self.base.aux_classifier.add_module('4',
                                            nn.Conv2d(256, num_classes, 1))

        classifier = list(self.base.classifier.children())
        self.base.classifier = nn.Sequential(*classifier[:-1])
        self.base.classifier.add_module('4', nn.Conv2d(256, num_classes, 1))
예제 #27
0
파일: deeplab.py 프로젝트: Britefury/dextr
def dextr_deeplab3(num_classes=1):
    model = segmentation.deeplabv3_resnet101(pretrained=True)

    # Create new input `conv1` layer that takes a 4-channel input
    new_conv1 = nn.Conv2d(4,
                          64,
                          kernel_size=(7, 7),
                          stride=(2, 2),
                          padding=(3, 3),
                          bias=False)
    new_conv1.weight[:, :3, :, :] = model.backbone.conv1.weight
    model.backbone.conv1.weight.data = new_conv1.weight.data

    new_parameters = []

    # Create new classifier output layer
    model.classifier[-1] = nn.Conv2d(256,
                                     num_classes,
                                     kernel_size=(1, 1),
                                     stride=(1, 1))
    new_parameters.extend(list(model.classifier[-1].parameters()))

    if model.aux_classifier is not None:
        # Create new aux classifier output layer
        model.aux_classifier[-1] = nn.Conv2d(256,
                                             num_classes,
                                             kernel_size=(1, 1),
                                             stride=(1, 1))
        new_parameters.extend(list(model.aux_classifier[-1].parameters()))

    # Setup new and pre-trained parameters for fine-tuning
    new_param_ids = {id(p) for p in new_parameters}
    pretrained_parameters = [
        p for p in model.parameters() if id(p) not in new_param_ids
    ]

    model.pretrained_parameters = pretrained_parameters
    model.new_parameters = new_parameters

    return model
예제 #28
0
    def __init__(self, num_classes: int = 2, ignore_index: Optional[int] = None, lr: float = 0.001,
                 weight_decay: float = 0.001, aux_loss_factor: float = 0.3):

        super().__init__()
        self.num_classes = num_classes
        self.ignore_index = ignore_index
        self.lr = lr
        self.weight_decay = weight_decay
        self.aux_loss_factor = aux_loss_factor

        # Create model from pre-trained DeepLabv3
        self.model = deeplabv3_resnet101(pretrained=True, progress=True)
        self.model.aux_classifier = FCNHead(1024, self.num_classes)
        self.model.classifier = DeepLabHead(2048, self.num_classes)

        # Setup trainable layers
        self.model.requires_grad_(True)
        self.model.backbone.requires_grad_(False)

        # Loss function and metrics
        self.focal_tversky_loss = FocalTverskyMetric(
            self.num_classes,
            alpha=0.7,
            beta=0.3,
            gamma=4.0 / 3.0,
            ignore_index=self.ignore_index,
        )
        self.accuracy_metric = Accuracy(ignore_index=self.ignore_index)
        self.iou_metric = JaccardIndex(
            num_classes=self.num_classes,
            reduction="none",
            ignore_index=self.ignore_index,
        )
        self.precision_metric = Precision(num_classes=self.num_classes, ignore_index=self.ignore_index,
                                          average='weighted', mdmc_average='global')
        self.recall_metric = Recall(num_classes=self.num_classes, ignore_index=self.ignore_index,
                                    average='weighted', mdmc_average='global')
예제 #29
0
    limit_val_num_samples=100 if debug else None,
    random_seed=seed)

prepare_batch = prepare_batch_fp32

# Image denormalization function to plot predictions with images
img_denormalize = partial(denormalize,
                          mean=(0.485, 0.456, 0.406),
                          std=(0.229, 0.224, 0.225))

# ##############################
# Setup models
# ##############################

num_classes = 21
model = deeplabv3_resnet101(num_classes=num_classes)


def model_output_transform(output):
    return output['out']


# ##############################
# Setup solver
# ##############################

num_epochs = 100

criterion = nn.CrossEntropyLoss()

lr = 0.007
예제 #30
0
    path_meta_data = r'samples/model_comparison.csv'
    save_path = r'/zhome/db/f/128823/Bachelor/model_predictions/3_classes/random'
    model_path = r'/work3/s173934/Bachelorprojekt/exp_results/resize_vs_randomcrop/3_class_dataset/randomcrop/DeepLab_ThreeClass_resize_false0.01.pt'
else:
    path_original_data = r'C:\Users\Mads-_uop20qq\Documents\5. Semester\BachelorProj\leather_patches'
    path_train = r"C:\Users\Mads-_uop20qq\Documents\5. Semester\BachelorProj\Bachelorprojekt\tif_images"
    path_val = r"C:\Users\Mads-_uop20qq\Documents\5. Semester\BachelorProj\Bachelorprojekt\tif_images"
    path_meta_data = r'samples/model_comparison.csv'
    save_path = r'C:\Users\Mads-_uop20qq\Documents\5. Semester\BachelorProj\Bachelorprojekt\slet\predictions'
    model_path = r'E:\downloads_hpc_bachelor\exp_results\backbone\classifier_only\ResNet\DeepLab_backbone_exp0.01.pt'

checkpoint = torch.load(model_path, map_location=device)

if model_name == 'DeepLab':
    model = deeplabv3_resnet101(pretrained=True,
                                progress=True,
                                num_classes=21,
                                aux_loss=None)
    model.classifier[-1] = torch.nn.Conv2d(256,
                                           n_classes + 2,
                                           kernel_size=(1, 1),
                                           stride=(1, 1)).requires_grad_()
    model.aux_classifier[-1] = torch.nn.Conv2d(256,
                                               n_classes + 2,
                                               kernel_size=(1, 1),
                                               stride=(1, 1)).requires_grad_()
else:
    model = _segm_mobilenet('deeplabv3',
                            'mobile_net',
                            output_stride=8,
                            num_classes=n_classes + 2,
                            pretrained_backbone=True)