Exemple #1
0
    def __init__(self, text_layer, image_layer, n_texts=100, n_images=10):
        """
        Constructor
        :param text_layer: Pre-trained text layer
        :param image_layer: Pre-trained image layer
        :param n_texts: Number of text in the inputs
        :param n_images: Number of images in the inputs
        """
        # Properties
        self.text_layer = text_layer
        self.image_layer = image_layer
        self.n_texts = n_texts
        self.n_images = n_images

        # Number of features of the final fully connected layer of the text pre-trained network
        text_num_ftrs = self.text_layer.fc.in_features
        self.text_layer.fc = etnn.Identity()

        # Number of features of the final fully connected layer of the image
        image_num_ftrs = self.image_layer.fc.in_features
        self.image_layer.fc = etnn.Identity()

        # Linear layer
        self.linear_size = text_num_ftrs * n_texts + image_num_ftrs * n_images
        self.linear = nn.Linear(self.linear_size, 2)
def load_cgfs(fold=0, dataset_size=100, dataset_start=0, use_cuda=False):
    """
    Load CGFS
    :param fold:
    :return:
    """
    # Path
    path = os.path.join("feature_selectors", "cgfs", "c3",
                        str(int(dataset_size)), str(int(dataset_start)),
                        "cgfs.{}.p".format(fold))
    # print(path)
    # CNN Glove Feature Selector
    cgfs = models.CGFS(n_gram=3, n_features=settings.cgfs_output_dim['c3'])
    if use_cuda:
        cgfs.cuda()
    # end if

    # Eval
    cgfs.eval()

    # Load states
    # print(u"Load {}".format(path))
    state_dict = torch.load(open(path, 'rb'))

    # Load dict
    cgfs.load_state_dict(state_dict)

    # Remove last linear layer
    cgfs.linear2 = etnn.Identity()
    cgfs.linear3 = etnn.Identity()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(
                cgfs, settings.cgfs_output_dim['c3'], to_variable=True),
            torchlanguage.transforms.Reshape(
                (-1, settings.cgfs_output_dim['c3'])),
            # torchlanguage.transforms.Normalize(mean=settings.cgfs_mean, std=settings.cgfs_std, input_dim=90)
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.FeatureSelector(
                cgfs, settings.cgfs_output_dim['c3'], to_variable=True),
            torchlanguage.transforms.Reshape(
                (-1, settings.cgfs_output_dim['c3'])),
            # torchlanguage.transforms.Normalize(mean=settings.cgfs_mean, std=settings.cgfs_std, input_dim=90)
        ])
    # end if
    return cgfs, transformer
def create_wenc_transformer(wenc_model, cgfs_info, use_cuda=True):
    # Remove last linear layer
    wenc_model.linear2 = etnn.Identity()

    # Eval
    wenc_model.eval()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(wenc_model,
                                                     settings.wenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.wenc_output_dim)),
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.FeatureSelector(wenc_model,
                                                     settings.wenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.wenc_output_dim)),
        ])
    # end if
    return transformer
Exemple #4
0
def create_cenc_transformer(cenc_model, cenc_voc, use_cuda=True):
    # Remove last linear layer
    cenc_model.linear2 = etnn.Identity()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.Character(),
            torchlanguage.transforms.ToIndex(token_to_ix=cenc_voc),
            torchlanguage.transforms.ToNGram(n=settings.cenc_text_length,
                                             overlapse=True),
            torchlanguage.transforms.Reshape((-1, settings.cenc_text_length)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(cenc_model,
                                                     settings.cenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.cenc_output_dim))
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.Character(),
            torchlanguage.transforms.ToIndex(token_to_ix=cenc_voc),
            torchlanguage.transforms.ToNGram(n=settings.cenc_text_length,
                                             overlapse=True),
            torchlanguage.transforms.Reshape((-1, settings.cenc_text_length)),
            torchlanguage.transforms.FeatureSelector(cenc_model,
                                                     settings.cenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.cenc_output_dim))
        ])
    # end if
    return transformer
Exemple #5
0
    def __init__(self,
                 reservoir_dim,
                 sfa_dim,
                 ica_dim,
                 pretrained=False,
                 feature_selector='resnet18'):
        """
        Constructor
        """
        # Upper class
        super(NilsNet, self).__init__()

        # ResNet
        if feature_selector == 'resnet18':
            self.feature_selector = torchvision.models.resnet18(
                pretrained=True)
        elif feature_selector == 'resnet34':
            self.feature_selector = torchvision.models.resnet34(
                pretrained=True)
        elif feature_selector == 'resnet50':
            self.feature_selector = torchvision.models.resnet50(
                pretrained=True)
        elif feature_selector == 'alexnet':
            self.feature_selector = torchvision.models.alexnet(pretrained=True)
        # end if

        # Skip last layer
        self.reservoir_input_dim = self.feature_selector.fc.in_features
        self.feature_selector.fc = ecnn.Identity()
Exemple #6
0
def load_cenc35(use_cuda=False):
    """
    Character encoder
    :param fold:
    :return:
    """
    # Path
    path = os.path.join("feature_selectors", "cenc", "cenc35.pth")
    voc_path = os.path.join("feature_selectors", "cenc", "cenc35.voc.pth")
    # print(path)
    # print(voc_path)
    # cEnc
    cenc = torchlanguage.models.cEnc(text_length=settings.ccsaa_text_length,
                                     vocab_size=settings.cenc_voc_size,
                                     embedding_dim=settings.cenc_embedding_dim,
                                     n_classes=settings.n_authors,
                                     n_features=300)
    if use_cuda:
        cenc.cuda()
    # end if

    # Eval
    cenc.eval()

    # Load dict and voc
    cenc.load_state_dict(torch.load(open(path, 'rb')))
    voc = torch.load(open(voc_path, 'rb'))

    # Remove last linear layer
    cenc.linear2 = etnn.Identity()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.Character(),
            torchlanguage.transforms.ToIndex(token_to_ix=voc),
            torchlanguage.transforms.ToNGram(n=settings.cenc_text_length,
                                             overlapse=False),
            torchlanguage.transforms.Reshape((-1, settings.cenc_text_length)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(cenc,
                                                     settings.cenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.cenc_output_dim))
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.Character(),
            torchlanguage.transforms.ToIndex(token_to_ix=voc),
            torchlanguage.transforms.ToNGram(n=settings.cenc_text_length,
                                             overlapse=False),
            torchlanguage.transforms.Reshape((-1, settings.cenc_text_length)),
            torchlanguage.transforms.FeatureSelector(cenc,
                                                     settings.cenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.cenc_output_dim))
        ])
    # end if
    return cenc, transformer
def load_ccsaa35(use_cuda=False):
    """
    Load CNN Character Selector for Authorship Attribution
    :param fold:
    :return:
    """
    # Path
    path = os.path.join("feature_selectors", "ccsaa", "ccsaa35.pth")
    voc_path = os.path.join("feature_selectors", "ccsaa", "ccsaa35.voc.pth")
    print(path)
    print(voc_path)
    # CNN Character Selector for Authorship Attribution
    ccsaa = models.CCSAA(text_length=settings.ccsaa_text_length,
                         vocab_size=settings.ccsaa_pretrain_voc_size,
                         n_classes=settings.n_pretrain_authors,
                         n_features=settings.ccsaa_output_dim)
    if use_cuda:
        ccsaa.cuda()
    # end if

    # Eval
    ccsaa.eval()

    # Load dict and voc
    ccsaa.load_state_dict(torch.load(open(path, 'rb')))
    voc = torch.load(open(voc_path, 'rb'))

    # Remove last linear layer
    ccsaa.linear2 = etnn.Identity()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.Character(),
            torchlanguage.transforms.ToIndex(token_to_ix=voc),
            torchlanguage.transforms.ToNGram(n=settings.ccsaa_text_length,
                                             overlapse=True),
            torchlanguage.transforms.Reshape((-1, settings.ccsaa_text_length)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(ccsaa,
                                                     settings.ccsaa_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.ccsaa_output_dim))
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.Character(),
            torchlanguage.transforms.ToIndex(token_to_ix=voc),
            torchlanguage.transforms.ToNGram(n=settings.ccsaa_text_length,
                                             overlapse=True),
            torchlanguage.transforms.Reshape((-1, settings.ccsaa_text_length)),
            torchlanguage.transforms.FeatureSelector(ccsaa,
                                                     settings.ccsaa_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.ccsaa_output_dim))
        ])
    # end if
    return ccsaa, transformer
def load_cgfs35(use_cuda=False):
    """
    Load CGFS
    :param fold:
    :return:
    """
    # Path
    path = os.path.join("feature_selectors", "cgfs", "c3", "cgfs35.p")
    # print(path)
    # CNN Glove Feature Selector
    cgfs = models.CGFS(n_gram=3,
                       n_features=settings.cgfs_output_dim['c3'],
                       n_authors=settings.n_pretrain_authors)
    if use_cuda:
        cgfs.cuda()
    # end if

    # Eval
    cgfs.eval()

    # Load states
    # print(u"Load {}".format(path))
    state_dict = torch.load(open(path, 'rb'))

    # Load dict
    cgfs.load_state_dict(state_dict)

    # Remove last linear layer
    cgfs.linear3 = etnn.Identity()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(
                cgfs, settings.cgfs_output_dim['c3'], to_variable=True),
            torchlanguage.transforms.Reshape(
                (-1, settings.cgfs_output_dim['c3'])),
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.FeatureSelector(
                cgfs, settings.cgfs_output_dim['c3'], to_variable=True),
            torchlanguage.transforms.Reshape(
                (-1, settings.cgfs_output_dim['c3'])),
        ])
    # end if
    return cgfs, transformer
def load_wenc(fold=0, dataset_size=100, dataset_start=0, use_cuda=False):
    """
    Load wEnc
    :param fold:
    :return:
    """
    # Path
    path = os.path.join("feature_selectors", "wenc", str(int(dataset_size)),
                        str(int(dataset_start)), "wenc.{}.p".format(fold))

    # wEnc
    wenc = torchlanguage.models.wEnc(n_classes=settings.n_authors, n_gram=3)
    if use_cuda:
        wenc.cuda()
        # end if

        # Eval
        wenc.eval()

    # Load states
    # print(u"Load {}".format(path))
    state_dict = torch.load(open(path, 'rb'))

    # Load dict
    wenc.load_state_dict(state_dict)

    # Remove last linear layer
    wenc.linear2 = etnn.Identity()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(wenc,
                                                     settings.wenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.wenc_output_dim)),
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.FeatureSelector(wenc,
                                                     settings.wenc_output_dim,
                                                     to_variable=True),
            torchlanguage.transforms.Reshape((-1, settings.wenc_output_dim)),
        ])
    # end if
    return wenc, transformer
    def __init__(self, image_model, n_images=10):
        """
        Constructor
        :param image_model: Single image model
        :param n_images: Number of images
        """
        # Super
        super(ImageModel, self).__init__()

        # Properties
        self.image_model = image_model
        self.n_images = n_images

        # Number of features of the final fully connected layer of the image
        image_num_ftrs = self.image_model.fc.in_features
        self.image_model.fc = etnn.Identity()

        # Linear layer
        self.linear_size = image_num_ftrs * n_images
        # self.linear1 = nn.Linear(self.linear_size, image_num_ftrs)
        # self.linear2 = nn.Linear(image_num_ftrs, 2)
        self.linear = nn.Linear(self.linear_size, 2)
def create_cgfs_transformer(cgfs_model, cgfs_info, use_cuda=True):
    # Remove last linear layer
    cgfs_model.linear3 = etnn.Identity()

    # Eval
    cgfs_model.eval()

    # Transformer
    if use_cuda:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.ToCUDA(),
            torchlanguage.transforms.FeatureSelector(
                cgfs_model, settings.cgfs_output_dim['c3'], to_variable=True),
            torchlanguage.transforms.Reshape(
                (-1, settings.cgfs_output_dim['c3'])),
            # torchlanguage.transforms.Normalize(mean=cgfs_info['mean'], std=cgfs_info['std'], input_dim=90)
            # torchlanguage.transforms.Normalize(mean=settings.cgfs_mean, std=settings.cgfs_std, input_dim=90)
        ])
    else:
        transformer = torchlanguage.transforms.Compose([
            torchlanguage.transforms.GloveVector(),
            torchlanguage.transforms.ToNGram(n=3, overlapse=True),
            torchlanguage.transforms.Reshape(
                (-1, 1, 3, settings.cgfs_input_dim)),
            torchlanguage.transforms.FeatureSelector(
                cgfs_model, settings.cgfs_output_dim['c3'], to_variable=True),
            torchlanguage.transforms.Reshape(
                (-1, settings.cgfs_output_dim['c3'])),
            # torchlanguage.transforms.Normalize(mean=cgfs_info['mean'], std=cgfs_info['std'], input_dim=90)
            # torchlanguage.transforms.Normalize(mean=settings.cgfs_mean, std=settings.cgfs_std, input_dim=90)
        ])
    # end if
    return transformer
Exemple #12
0
input_sparsity = 0.1
w_sparsity = 0.1
input_scaling = 0.5
n_test = 5
n_samples = 1

# Argument
args = tools.functions.argument_parser_training_model()

# Load feature selector
feature_selector = torchlanguage.models.CCSAA(text_length=20,
                                              vocab_size=84,
                                              embedding_dim=50,
                                              n_classes=15)
feature_selector.load_state_dict(torch.load(open(args.feature_selector, 'rb')))
feature_selector.linear = etnn.Identity()
if args.cuda:
    feature_selector.cuda()
# end if
feature_selector_voc = torch.load(open(args.feature_selector_voc, 'rb'))

# Transforms
transform = transforms.Compose([
    transforms.Character(),
    transforms.ToIndex(token_to_ix=feature_selector_voc),
    transforms.MaxIndex(max_id=83),
    transforms.ToNGram(n=20, overlapse=True),
    transforms.Reshape((-1, 20)),
    transforms.ToCUDA(),
    transforms.FeatureSelector(model=feature_selector,
                               n_features=150,