示例#1
0
    def __init__(self, trg_vocab_size, max_out=True):

        super(Decoder, self).__init__()

        self.laycnt = wargs.dec_layer_cnt - 1
        self.max_out = max_out
        self.trg_lookup_table = nn.Embedding(trg_vocab_size,
                                             wargs.trg_wemb_size,
                                             padding_idx=PAD)
        self.attention = Attention(wargs.dec_hid_size, wargs.align_size)

        self.tanh = nn.Tanh()
        self.ran1 = RAN(wargs.trg_wemb_size,
                        wargs.dec_hid_size,
                        residual=False)
        self.ran2 = RAN(wargs.dec_hid_size + wargs.enc_hid_size,
                        wargs.dec_hid_size,
                        residual=True,
                        laycnt=self.laycnt,
                        share_weight=False)

        out_size = 2 * wargs.out_size if max_out else wargs.out_size
        self.ls = nn.Linear(wargs.dec_hid_size, out_size)

        self.dropout = nn.Dropout(wargs.drop_rate)
示例#2
0
    def __init__(self,
                 src_vocab_size,
                 input_size,
                 output_size,
                 with_ln=False,
                 prefix='Encoder',
                 **kwargs):

        super(Encoder, self).__init__()

        self.output_size = output_size
        self.laycnt = wargs.enc_layer_cnt
        f = lambda name: str_cat(prefix, name
                                 )  # return 'Encoder_' + parameters name

        self.src_lookup_table = nn.Embedding(src_vocab_size,
                                             wargs.src_wemb_size,
                                             padding_idx=PAD)

        self.forw_ran = RAN(input_size,
                            output_size,
                            residual=True,
                            with_ln=with_ln,
                            prefix=f('Forw'))
        self.back_ran = RAN(output_size,
                            output_size,
                            residual=True,
                            with_ln=with_ln,
                            prefix=f('Back'))
示例#3
0
    def __init__(self, dec_hid_size, align_size):

        super(Attention, self).__init__()
        self.align_size = align_size
        self.sa = nn.Linear(dec_hid_size, self.align_size)
        self.ha = nn.Linear(dec_hid_size, self.align_size)
        self.tanh = nn.Tanh()
        #self.a1 = nn.Linear(self.align_size, 1)
        self.ran = RAN(wargs.dec_hid_size, wargs.dec_hid_size, residual=False)
示例#4
0
    def __init__(self,
                 rnn_type,
                 ntoken,
                 ninp,
                 nhid,
                 nlayers,
                 dropout=0.5,
                 tie_weights=False):
        super(RNNModel, self).__init__()
        self.drop = nn.Dropout(dropout)
        self.encoder = nn.Embedding(ntoken, ninp)
        if rnn_type == "RAN":
            self.rnn = RAN(ninp, nhid, nlayers, dropout=dropout)
        elif rnn_type in ['LSTM', 'GRU']:
            self.rnn = getattr(nn, rnn_type)(ninp,
                                             nhid,
                                             nlayers,
                                             dropout=dropout)
        else:
            try:
                nonlinearity = {
                    'RNN_TANH': 'tanh',
                    'RNN_RELU': 'relu'
                }[rnn_type]
            except KeyError:
                raise ValueError(
                    """An invalid option for `--model` was supplied,
                                 options are ['LSTM', 'GRU', 'RAN', 'RNN_TANH' or 'RNN_RELU']"""
                )
            self.rnn = nn.RNN(ninp,
                              nhid,
                              nlayers,
                              nonlinearity=nonlinearity,
                              dropout=dropout)
        self.decoder = nn.Linear(nhid, ntoken)

        # Optionally tie weights as in:
        # "Using the Output Embedding to Improve Language Models" (Press & Wolf 2016)
        # https://arxiv.org/abs/1608.05859
        # and
        # "Tying Word Vectors and Word Classifiers: A Loss Framework for Language Modeling" (Inan et al. 2016)
        # https://arxiv.org/abs/1611.01462
        if tie_weights:
            if nhid != ninp:
                raise ValueError(
                    'When using the tied flag, nhid must be equal to emsize')
            self.decoder.weight = self.encoder.weight

        self.init_weights()

        self.rnn_type = rnn_type
        self.nhid = nhid
        self.nlayers = nlayers
    def __init__(self,
                 rnn_type,
                 ntoken,
                 emsize,
                 nunits,
                 nlayers,
                 dropout=0.5,
                 tie_weights=False):
        super(RANModel, self).__init__()
        self.drop = nn.Dropout(dropout)
        self.encoder = nn.Embedding(ntoken, emsize)
        if rnn_type == "RAN":
            self.rnn = RAN(emsize, nunits, nlayers, dropout=dropout)
        elif rnn_type in ['LSTM', 'GRU']:
            self.rnn = getattr(nn, rnn_type)(emsize,
                                             nunits,
                                             nlayers,
                                             dropout=dropout)
        else:
            try:
                nonlinearity = {
                    'RNN_TANH': 'tanh',
                    'RNN_RELU': 'relu'
                }[rnn_type]
            except KeyError:
                raise ValueError(
                    """An invalid option for `--model` was supplied,
                                 options are ['LSTM', 'GRU', 'RAN', 'RNN_TANH' or 'RNN_RELU']"""
                )
            self.rnn = nn.RNN(emsize,
                              nunits,
                              nlayers,
                              nonlinearity=nonlinearity,
                              dropout=dropout)
        self.decoder = nn.Linear(nunits, 30)
        self.decoder2 = nn.Linear(30, 3)
        self.softmax = nn.Softmax()
        if tie_weights:
            if nunits != emsize:
                raise ValueError(
                    'When using the tied flag, nhid must be equal to emsize')
            self.decoder.weight = self.encoder.weight

        self.init_weights()

        self.rnn_type = rnn_type
        self.nunits = nunits
        self.nlayers = nlayers

        self.ntoken = ntoken
        self.emsize = emsize
示例#6
0
    def __init__(self,
                 rnn_type,
                 ntoken,
                 emsize,
                 nunits,
                 nreduced,
                 nlayers,
                 dropout=0.5,
                 tie_weights=False):
        super(BI_RANModel, self).__init__()
        torch.manual_seed(1234)
        self.drop = nn.Dropout(dropout)
        self.encoder = nn.Embedding(ntoken, emsize)
        if rnn_type in ["RAN_BIDIR"]:
            self.rnn1 = RAN(emsize, nunits, nlayers, dropout=dropout)
            self.rnn2 = RAN(emsize, nunits, nlayers, dropout=dropout)

        self.reducer1 = nn.Linear(nunits, nreduced)
        self.reducer2 = nn.Linear(nunits, nreduced)

        self.decoder = nn.Linear(nreduced, 3)
        self.softmax = nn.Softmax()
        if tie_weights:
            if nunits != emsize:
                raise ValueError(
                    'When using the tied flag, nhid must be equal to emsize')
            self.decoder.weight = self.encoder.weight

        self.init_weights()

        self.rnn_type = rnn_type
        self.nlayers = nlayers
        self.nreduced = nreduced
        self.nunits1 = nunits
        self.nunits2 = nunits

        self.ntoken = ntoken
        self.emsize = emsize
示例#7
0
class smallDetector():
    def __init__(self):
        ran_weight = '/home/mcc/working/detect-region/pytorch-deeplab-xception/run/tt100k/deeplab-mobile-region/experiment_33/checkpoint_122.pth.tar'
        self.ran = RAN(weight=ran_weight, gpu_ids=0)
        self.rbg = RBG()
        self.gdet = generalDetector()

    def inference(self, img_path):
        self.img = cv2.imread(img_path)
        output = self.ran.inference(self.img)
        preds = output.data.cpu().numpy()
        self.mask = np.argmax(preds, axis=1)[0]
        self.show_result(self.img, self.mask)
        return self.mask

    def show_result(self, img, pred):
        plt.figure(figsize=(10, 10))
        plt.subplot(1, 2, 1).imshow(img[:, :, ::-1])
        plt.subplot(1, 2, 2).imshow(pred.astype(np.uint8) * 255)
        plt.show()
        cv2.waitKey()
示例#8
0
 def __init__(self):
     ran_weight = '/home/mcc/working/detect-region/pytorch-deeplab-xception/run/tt100k/deeplab-mobile-region/experiment_33/checkpoint_122.pth.tar'
     self.ran = RAN(weight=ran_weight, gpu_ids=0)
     self.rbg = RBG()
     self.gdet = generalDetector()
示例#9
0
    def __init__(self,
                 rnn_type,
                 vocab_size,
                 embed_dims,
                 n_units,
                 n_layers,
                 embeddings=None,
                 bidirectional=False,
                 dropout=0.2,
                 tie_weights=False):
        super(RNNModel, self).__init__()

        # optionally add dropout regularisation
        self.dropout = nn.Dropout(dropout)

        # the embedding matrix of size |V| x d
        self.embed = nn.Embedding(vocab_size, embed_dims)
        if embeddings is not None:
            self.embed.weight = nn.Parameter(to_tensor(embeddings))

        self.bidir = bidirectional

        # select the correct architecture

        if rnn_type in ['LSTM', 'GRU']:
            self.rnn_type = rnn_type
            self.rnn = getattr(nn, rnn_type)(embed_dims,
                                             n_units,
                                             n_layers,
                                             dropout=dropout,
                                             bidirectional=self.bidir)
        elif rnn_type == 'RAN':
            self.rnn = RAN(embed_dims, n_units, n_layers, dropout=dropout)
        else:
            try:
                model_info = rnn_type.split("_")
                self.rnn_type = model_info[0]
                nonlinearity = model_info[1].lower()
            except KeyError:
                raise ValueError(
                    "An invalid option for `--model` was supplied.\
                                 Options are ['LSTM', 'GRU', 'RNN_TANH', or\
                                 'RNN_RELU']")
            self.rnn = nn.RNN(embed_dims,
                              n_units,
                              n_layers,
                              nonlinearity=nonlinearity,
                              dropout=dropout,
                              bidirectional=self.bidir)

        # bidirectional needs 2x units
        n = int(self.bidir) + 1
        # output is linear as softmax is applied within the loss function
        self.output = nn.Linear(n * n_units, vocab_size)

        # Optionally tie weights as in:
        # "Using the Output Embedding to Improve Language Models" (Press & Wolf,
        # 2016) https://arxiv.org/abs/1608.05859
        # and
        # "Tying Word Vectors and Word Classifiers: A Loss Framework for
        # Language Modeling" (Inan et al. 2016) https://arxiv.org/abs/1611.01462
        if tie_weights:
            if n_units != embed_dims:
                raise ValueError('When using the tied flag, n_units must be\
                                 equal to embdims')
            self.output.weight = self.embed.weight

        self.init_weights()

        self.rnn_type = rnn_type
        self.n_units = n_units
        self.n_layers = n_layers