Exemple #1
0
    def get_clf(self):
        if self.flags.use_clf:
            dir_clf = self.flags.dir_clf
            if not dir_clf.exists():
                download_zip_from_url(
                    url=
                    'https://www.dropbox.com/sh/lx8669lyok9ois6/AADM7Cs_QReijyo2kF8xzWqua/trained_classifiers/trained_clfs_mst?dl=1',
                    dest_folder=dir_clf)
            model_clf = ClfImgSVHN()
            model_clf.load_state_dict(
                torch.load(os.path.join(self.flags.dir_clf, f"clf_m2"),
                           map_location=self.flags.device))

            return model_clf.to(self.flags.device)
Exemple #2
0
 def get_clf(self):
     if self.flags.use_clf:
         clf_name_mapping = {'PA': 'pa', 'Lateral': 'lat'}
         # finding the directory of the classifier
         img_clf_path = Path(
             __file__
         ).parent.parent / f'classifiers/state_dicts/{clf_name_mapping[self.name]}_clf_{self.flags.img_size}.pth'
         if not img_clf_path.exists():
             download_zip_from_url(
                 url=
                 'http://jimmy123.hopto.org:2095/nextcloud/index.php/s/GTc8pYiDKrq35ky/download',
                 dest_folder=img_clf_path.parent.parent,
                 verbose=True)
         lightning_module = LM_(str_labels=self.labels,
                                transforms=self.clf_transforms)
         lightning_module.model.load_state_dict(
             torch.load(img_clf_path, map_location=self.flags.device))
         return lightning_module.to(self.flags.device)
Exemple #3
0
    def __init__(self, args, alphabet, partition=0, transform=None):
        self.dir_dataset_base = args.dir_data

        if not self.dir_dataset_base.exists():
            log.info(
                f'Dataset not found under {self.dir_dataset_base}. Parent directory contains: '
                f'{list(self.dir_dataset_base.parent)}')
            download_zip_from_url(
                url=
                'https://www.dropbox.com/sh/lx8669lyok9ois6/AACCZqDiZuv0Q8RA3Qmwrwnca/celeba_data.zip?dl=1',
                dest_folder=Path(self.dir_dataset_base).parent,
                verbose=True)

        filename_text = self.dir_dataset_base / (
            'list_attr_text_' + str(args.len_sequence).zfill(3) + '_' +
            str(args.random_text_ordering) + '_' +
            str(args.random_text_startindex) + '_celeba.csv')
        filename_partition = os.path.join(self.dir_dataset_base,
                                          'list_eval_partition.csv')
        filename_attributes = os.path.join(self.dir_dataset_base,
                                           'list_attr_celeba.csv')

        df_text = pd.read_csv(filename_text)
        df_partition = pd.read_csv(filename_partition)
        df_attributes = pd.read_csv(filename_attributes)

        self.args = args
        self.img_dir = os.path.join(self.dir_dataset_base, 'img_align_celeba')
        self.txt_path = filename_text
        self.attrributes_path = filename_attributes
        self.partition_path = filename_partition

        self.alphabet = alphabet
        self.img_names = df_text.loc[df_partition['partition'] ==
                                     partition]['image_id'].values
        self.attributes = df_attributes.loc[df_partition['partition'] ==
                                            partition]
        self.labels = df_attributes.loc[
            df_partition['partition'] ==
            partition].values  # atm, i am just using blond_hair as labels
        self.y = df_text.loc[df_partition['partition'] ==
                             partition]['text'].values
        self.transform = transform
    def __init__(self, flags, alphabet, train=True, transform=None, target_transform=None):
        super(SVHNMNIST, self).__init__(flags.dir_data)
        self.flags = flags;
        self.dataset = 'MNIST_SVHN';
        self.dataset_mnist = 'MNIST';
        self.dataset_svhn = 'SVHN';
        self.len_sequence = flags.len_sequence
        self.transform = transform
        self.target_transform = target_transform
        self.train = train  # training set or test set
        self.alphabet = alphabet;

        self.dir_svhn = os.path.join(self.root, self.dataset_svhn)
        self.dir_mnist = os.path.join(self.root, self.dataset_mnist)
        print(self.dir_svhn)

        if not self._check_exists_mnist():
            download_zip_from_url(
                url='https://www.dropbox.com/sh/lx8669lyok9ois6/AADMhr3EluBXJyZnV1_lYntTa/data_mnistsvhntext.zip?dl=1',
                dest_folder=Path(self.dir_mnist).parent.parent)
            assert self._check_exists_mnist(), 'Dataset MNIST not found.'
        # if not self._check_exists_svhn():
        #     raise RuntimeError('Dataset SVHN not found.')

        if self.train:
            id_file_svhn = self.training_file_svhn_idx;
            id_file_mnist = self.training_file_mnist_idx;
            data_file_svhn = self.training_file_svhn;
            data_file_mnist = self.training_file_mnist;
        else:
            id_file_svhn = self.test_file_svhn_idx;
            id_file_mnist = self.test_file_mnist_idx;
            data_file_svhn = self.test_file_svhn;
            data_file_mnist = self.test_file_mnist;

        # import here rather than at top of file because this is
        # an optional dependency for torchvision
        import scipy.io as sio

        print(os.path.join(self.dir_svhn, data_file_svhn))
        # reading(loading) mat file as array
        loaded_mat = sio.loadmat(os.path.join(self.dir_svhn, data_file_svhn))

        self.data_svhn = loaded_mat['X']
        # loading from the .mat file gives an np array of type np.uint8
        # converting to np.int64, so that we have a LongTensor after
        # the conversion from the numpy array
        # the squeeze is needed to obtain a 1D tensor
        self.labels_svhn = loaded_mat['y'].astype(np.int64).squeeze()

        # the svhn dataset assigns the class label "10" to the digit 0
        # this makes it inconsistent with several loss functions
        # which expect the class labels to be in the range [0, C-1]
        np.place(self.labels_svhn, self.labels_svhn == 10, 0)
        self.data_svhn = np.transpose(self.data_svhn, (3, 2, 0, 1))
        samples_svhn = self.data_svhn.shape[0];
        channels_svhn = self.data_svhn.shape[1];
        width_svhn = self.data_svhn.shape[2];
        height_svhn = self.data_svhn.shape[3];

        self.data_mnist, self.labels_mnist = torch.load(os.path.join(self.processed_folder, data_file_mnist));

        # # get transformed indices
        self.labels_svhn = torch.LongTensor(self.labels_svhn);
        mnist_l, mnist_li = self.labels_mnist.sort()
        svhn_l, svhn_li = self.labels_svhn.sort()
        self.mnist_idx, self.svhn_idx = rand_match_on_idx(mnist_l, mnist_li,
                                                          svhn_l, svhn_li,
                                                          max_d=10000,
                                                          dm=flags.data_multiplications)
        return {_class: i for i, _class in enumerate(self.classes)}

    def _check_exists_mnist(self):
        return (Path(self.processed_folder) / self.training_file_mnist).exists() and (
                Path(self.processed_folder) / self.test_file_mnist).exists()

    def _check_exists_svhn(self):
        return (os.path.exists(os.path.join(self.dir_svhn,
                                            self.training_file_svhn)) and
                os.path.exists(os.path.join(self.dir_svhn,
                                            self.test_file_svhn)))

    @staticmethod
    def extract_gzip(gzip_path, remove_finished=False):
        print('Extracting {}'.format(gzip_path))
        with open(gzip_path.replace('.gz', ''), 'wb') as out_f, \
                gzip.GzipFile(gzip_path) as zip_f:
            out_f.write(zip_f.read())
        if remove_finished:
            os.unlink(gzip_path)

    def extra_repr(self):
        return "Split: {}".format("Train" if self.train is True else "Test")


if __name__ == '__main__':
    config = json2dict(Path(get_config_path(dataset='mnistsvhntext')))
    download_zip_from_url(
        url='https://www.dropbox.com/sh/lx8669lyok9ois6/AADMhr3EluBXJyZnV1_lYntTa/data_mnistsvhntext.zip?dl=1',
        dest_folder=Path(config['dir_data']).expanduser().parent, verbose=True)
Exemple #6
0
        data_size = torch.Size((1, flags.img_size, flags.img_size))
        super().__init__(data_size=data_size,
                         flags=flags,
                         name='PA',
                         labels=labels,
                         rec_weight=rec_weight,
                         plot_img_size=plot_img_size)


class MimicLateral(MimicImg):
    def __init__(self, flags, labels: typing.Iterable[str], rec_weight,
                 plot_img_size):
        data_size = torch.Size((1, flags.img_size, flags.img_size))
        super().__init__(data_size=data_size,
                         flags=flags,
                         name='Lateral',
                         labels=labels,
                         rec_weight=rec_weight,
                         plot_img_size=plot_img_size)


if __name__ == '__main__':
    img_clf_path = Path(
        __file__).parent.parent / f'classifiers/state_dicts/pa_clf_128.pth'
    if not img_clf_path.exists():
        download_zip_from_url(
            url=
            'http://jimmy123.hopto.org:2095/nextcloud/index.php/s/GTc8pYiDKrq35ky/download',
            dest_folder=img_clf_path.parent.parent,
            verbose=True)
Exemple #7
0
        label = torch.from_numpy(
            (self.labels[index, 1:] > 0).astype(int)).float()
        # img = torch.rand((self.args.image_channels, 64, 64))
        # text_str = torch.ones((8, 71))
        # img = torch.ones((1, 28, 28))
        # text_str = torch.rand((256, 71))
        sample = {'img': img, 'text': text_str}
        # sample = {'img': img}
        return sample, label

    def __len__(self):
        return self.y.shape[0]

    def get_text_str(self, index):
        return self.y[index]


if __name__ == '__main__':

    config = json2dict(get_config_path(dataset='celeba'))

    dir_data = Path(config['dir_data']).expanduser()

    if not dir_data.exists():
        download_zip_from_url(
            url=
            'https://www.dropbox.com/sh/lx8669lyok9ois6/AACCZqDiZuv0Q8RA3Qmwrwnca/celeba_data.zip?dl=1',
            dest_folder=dir_data.parent,
            verbose=True)
    print("Done.")
"""Download trianed models and evaluation results from zenodo"""
from pathlib import Path

from modun.download_utils import download_zip_from_url

download_zip_from_url(
    url="https://zenodo.org/record/5588294/files/data.zip?download=1",
    dest_folder=Path(__file__).parent.parent,
    verbose=True)