Example #1
0
    def _read_list(self, file, data_conf):
        with open(file, 'r') as f:
            content = f.readlines()
            for line in content:
                elements = line.strip().split(' ')
                dataset_class_name = elements[0]
                subdataset = elements[1]
                dataset_file = elements[2]

                dataset_class = import_shortcut('datasets', dataset_class_name)
                self._datasets.append(dataset_class(subdataset, data_conf, dataset_file, self._cache))
Example #2
0
    def __init__(self,
                 conf,
                 pretrained=False,
                 dropout=0.5,
                 fix_base_network=False,
                 final_affine_transformation=False,
                 n_fc_layers=3,
                 n_pointwise_conv=2,
                 n_features=128,
                 **kwargs):
        """Multi-Hypothesis model constructor.

        Args:
            conf (dict): Dictionary of the configuration file (json).
            pretrained (:obj:`bool`, optional): Whether used pretrained model
                on ImageNet.
            dropout (:obj:`float`): Probability of dropout.
            fix_base_network (:obj:`bool`): If true, we don't learn the first
                convolution, just use the weights from pretrained model.
            final_affine_transformation (:obj:`bool`): Whether to learn an
                affine transformation of the output of the network.
            n_fc_layers (:obj:`int`): Number of FC layers after conv layers.
            n_pointwise_conv (:obj:`int`): Number of 1x1 conv layers after
                first 3x3 convolution.
            n_features (:obj:`int`): Number of feature for the final 1x1 conv.
        """
        arch = conf['network']['subarch']
        # vgg11 or vgg11 with batch norm
        if arch == 'vgg11':
            super(VggClassification, self).__init__(make_layers(cfgs['A']),
                                                    **kwargs)
        elif arch == 'vgg11_bn':
            super(VggClassification,
                  self).__init__(make_layers(cfgs['A'], batch_norm=True),
                                 **kwargs)
        else:
            raise Exception('Wrong architecture')

        if pretrained:
            self.load_state_dict(
                model_zoo.load_url(torchvision.models.vgg.model_urls[arch]))

        # this is used to remove all cameras not included in this setting,
        # if defined in the conf file.
        if 'cameras' in conf:
            self._cameras = conf['cameras']
        else:
            self._cameras = None

        # load candidate selection method:
        # see folder modules/candidate_selection/ for available methods.
        class_obj = import_shortcut('modules.candidate_selection',
                                    conf['candidate_selection']['name'])
        self.candidate_selection = class_obj(
            conf, **conf['candidate_selection']['params'])
        self._final_affine_transformation = final_affine_transformation

        # we keep only the first VGG convolution!
        self.conv1 = self.features[0]
        self.relu1 = self.features[1]

        # then, we have N="n_pointwise_conv" number of 1x1 convs
        N = n_features
        pointwise_layers = []
        for n_layers in range(n_pointwise_conv):
            n_output = 64
            if n_layers == n_pointwise_conv - 1:
                n_output = N
            pointwise_layers.append(nn.Conv2d(64, n_output, kernel_size=1))
            pointwise_layers.append(nn.ReLU(inplace=True))

        self.pointwise_conv = nn.Sequential(*pointwise_layers)

        # remove VGG features and classifier (FCs of the net)
        del self.features
        del self.classifier

        # if this option is enabled, we don't learn the first conv weights,
        # they are copied from VGG pretrained on Imagenet (from pytorch),
        # the weights: /torch_model_zoo/vgg11-bbd30ac9.pth
        if fix_base_network:
            # do not learn any parameters from VGG
            included = ['conv1.bias', 'conv1.weight']
            for name, param in self.named_parameters():
                if name in included:
                    #print('name',name, param.shape)
                    param.requires_grad = False

        # probability of dropout
        self.dropout = nn.Dropout()

        # final FC layers: from N to 1 (probability for illuminant)
        final_n = 1

        # N: initial feature size
        # n_fc_layers: number of FC layers
        # final_n: size of final prediction
        self.fc = self._fc_layers(N, n_fc_layers, final_n)
        self.softmax = nn.Softmax(1)
        self.logsoftmax = nn.LogSoftmax(1)