예제 #1
0
    def __init__(self):
        super(DeepSeaModel, self).__init__()
        self.threshold = 1e-06  # DeepSea uses RELU, but in their lua source code, the threshold is 1e-6 instead of 0

        #self.num_of_feature = 1000
        #self.input_channels = 4
        # number of channels after the three convolution layers (each with window size 8, no padding) and two maxpools with stride 4
        nchannel_fc = math.floor((math.floor((1000 - 7) / 4.0) - 7) / 4.0) - 7

        # input is 4 features (one hot) x 1000 bp
        # layer 1: convolution layer with 4 features, 320 kernels, window size 8, step size 1
        # layer 2: pooling with window size 4, step size 4; drop out p 0.2
        # layer 3: convolution with 480 kernels, window size 8, step size 1
        # layer 4: pooling, window size 4, step size 4, drop out p 0.2
        # layer 5: convolution with 960 kernels, window size 8, step size 1; p dropout 0.5
        # layer 6:	reshape, fully connected layer with 925 neurons
        # layer 7: sigmoid, final output is 919 features

        self.all_layers = nn.Sequential(nn.Conv1d(4, 320, 8, stride=1),
                                        nn.Threshold(0, self.threshold),
                                        nn.MaxPool1d(4, 4), nn.Dropout(p=0.2),
                                        nn.Conv1d(320, 480, 8, stride=1),
                                        nn.Threshold(0, self.threshold),
                                        nn.MaxPool1d(4, 4), nn.Dropout(p=0.2),
                                        nn.Conv1d(480, 960, 8, stride=1),
                                        nn.Threshold(0, self.threshold),
                                        nn.Dropout(p=0.5), nn.Flatten(),
                                        nn.Linear(nchannel_fc * 960, 925),
                                        nn.Threshold(0, self.threshold),
                                        nn.Linear(925, 919), nn.Sigmoid())
예제 #2
0
    def __init__(self):
        super().__init__()

        rnn_dim = 512

        rnn = fixed_rnn
        rnn_num_steps = 8

        # network setup
        # (B, 70, 201)
        self.conv1 = nn.Conv1d(70, 384, kernel_size=4)
        self.threshold1 = nn.Threshold(1e-6, 0)
        self.maxpool1 = nn.MaxPool1d(kernel_size=3, stride=3)

        # (B, 384, 66)
        self.conv2 = nn.Conv1d(384, 512, kernel_size=4)
        self.threshold2 = nn.Threshold(1e-6, 0)
        self.maxpool2 = nn.MaxPool1d(kernel_size=3, stride=3)

        # (B, 512, 21)
        self.conv3 = nn.Conv1d(512, rnn_dim, kernel_size=4)
        self.threshold3 = nn.Threshold(1e-6, 0)
        self.maxpool3 = nn.MaxPool1d(kernel_size=3, stride=2)

        # (B, rnn_dim, rnn_num_steps)
        self.rnn = rnn(num_steps=rnn_num_steps, emb_dim=rnn_dim)

        # (B, rnn_dim)
        self.emb_proj = nn.Linear(rnn_dim, 1024)
    def __init__(self, block, num_blocks, num_classes=10):
        super(StepNet, self).__init__()
        self.in_planes = 64

        models0 = []
        thresholds = [0.0, 0.161, 0.259, 0.341, 0.416, 0.482, 0.553, 0.631, 0.718, 0.839][::-1]
        for index, threshold in enumerate(thresholds):
            models0.append(nn.Threshold(-threshold, (len(thresholds) - 1 - index) / (len(thresholds) - 1)))

        models1 = []
        thresholds = [0.0, 0.161, 0.255, 0.333, 0.404, 0.475, 0.541, 0.616, 0.702, 0.824][::-1]
        for index, threshold in enumerate(thresholds):
            models1.append(nn.Threshold(-threshold, (len(thresholds) - 1 - index) / (len(thresholds) - 1)))

        models2 = []
        thresholds = [0.0, 0.122, 0.2, 0.271, 0.337, 0.408, 0.49, 0.584, 0.698, 0.843][::-1]
        for index, threshold in enumerate(thresholds):
            models2.append(nn.Threshold(-threshold, (len(thresholds) - 1 - index) / (len(thresholds) - 1)))

        self.through0 = nn.Sequential(*models0)
        self.through1 = nn.Sequential(*models1)
        self.through2 = nn.Sequential(*models2)

        self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
        self.linear = nn.Linear(512 * block.expansion, num_classes)
예제 #4
0
 def __init__(self, threshold_value=0, min_value=0, max_value=1):
     super(Step, self).__init__()
     self.threshold_value = threshold_value
     self.min_value = min_value
     self.max_value = max_value
     self.min_threshold = nn.Threshold(self.threshold_value, self.min_value)
     self.max_threshold = nn.Threshold(-1*self.threshold_value, self.max_value)
예제 #5
0
    def __init__(self):
        super().__init__()
        # 第一层卷积
        self.conv1 = nn.Conv1d(in_channels=4, out_channels=320, kernel_size=8)
        self.threshold1 = nn.Threshold(0, 1e-6)
        self.pool1 = nn.MaxPool1d(kernel_size=4, stride=4)
        self.dropout1 = nn.Dropout(0.2)
        # 第二层卷积
        self.conv2 = nn.Conv1d(in_channels=320,
                               out_channels=480,
                               kernel_size=8)
        self.threshold2 = nn.Threshold(0, 1e-6)
        self.pool2 = nn.MaxPool1d(kernel_size=4, stride=4)
        self.dropout2 = nn.Dropout(0.2)
        # 第三层卷积
        self.conv3 = nn.Conv1d(in_channels=480,
                               out_channels=960,
                               kernel_size=8)
        self.threshold3 = nn.Threshold(0, 1e-6)
        self.dropout3 = nn.Dropout(0.5)

        # 展开后过linear
        self.linear1 = nn.Linear(960 * 53, 919)
        self.threshold4 = nn.Threshold(0, 1e-6)
        self.linear2 = nn.Linear(919, 919)
        self.sigmoid = nn.Sigmoid()
    def __init__(self, op):

        super(ParaphraseGenerator, self).__init__()

        # encoder | shared pair-wise discriminator:
        self.emb_layer = nn.Sequential(
            nn.Linear(op["vocab_sz"], op["emb_hid_dim"]),
            nn.Threshold(0.000001, 0),
            nn.Linear(op["emb_hid_dim"], op["emb_dim"]),
            nn.Threshold(0.000001, 0))
        self.enc_rnn = nn.GRU(op["emb_dim"], op["enc_rnn_dim"])
        self.enc_lin = nn.Sequential(
            nn.Dropout(op["enc_dropout"]),
            nn.Linear(op["enc_rnn_dim"], op["enc_dim"]))

        # generator :
        self.gen_emb = nn.Embedding(op["vocab_sz"], op["emb_dim"])
        self.gen_rnn = nn.LSTM(op["enc_dim"], op["gen_rnn_dim"])
        self.gen_lin = nn.Sequential(
            nn.Dropout(op["gen_dropout"]),
            nn.Linear(op["gen_rnn_dim"], op["vocab_sz"]),
            nn.LogSoftmax(dim=-1))

        # some useful constants :
        self.max_seq_len = op["max_seq_len"]
        self.vocab_sz = op["vocab_sz"]
예제 #7
0
def get_seqpred_model(load_weights=True):
    deepsea_cpu = nn.Sequential(  # Sequential,
        nn.Conv2d(4, 320, (1, 8), (1, 1)),
        nn.Threshold(0, 1e-06),
        nn.MaxPool2d((1, 4), (1, 4)),
        nn.Dropout(0.2),
        nn.Conv2d(320, 480, (1, 8), (1, 1)),
        nn.Threshold(0, 1e-06),
        nn.MaxPool2d((1, 4), (1, 4)),
        nn.Dropout(0.2),
        nn.Conv2d(480, 960, (1, 8), (1, 1)),
        nn.Threshold(0, 1e-06),
        nn.Dropout(0.5),
        Lambda(lambda x: x.view(x.size(0), -1)),  # Reshape,
        nn.Sequential(
            Lambda(lambda x: x.view(1, -1) if 1 == len(x.size()) else x),
            nn.Linear(50880, 925)),  # Linear,
        nn.Threshold(0, 1e-06),
        nn.Sequential(
            Lambda(lambda x: x.view(1, -1) if 1 == len(x.size()) else x),
            nn.Linear(925, 919)),  # Linear,
        nn.Sigmoid(),
    )
    if load_weights:
        deepsea_cpu.load_state_dict(torch.load('model_files/deepsea_cpu.pth'))
    return nn.Sequential(ReCodeAlphabet(), ConcatenateRC(), deepsea_cpu,
                         AverageRC())
 def forward(self, x: Tensor) -> Tensor:
     thresh1 = nn.Threshold(self.epsilon, self.epsilon)
     x_lower_clipped = thresh1.forward(x)
     thresh2 = nn.Threshold(-1 + self.epsilon, -1 + self.epsilon)
     x_lower_clipped_minus = -1 * x_lower_clipped
     x_lower_and_upper_clipped_minus = thresh2.forward(
         x_lower_clipped_minus)
     return -1 * x_lower_and_upper_clipped_minus
예제 #9
0
	def __init__(self, num_classes=1000):
		super(HeadPose, self).__init__()
		self.alexnet = HeadPoseAlexnet()
		self.alexnet.load_state_dict(model_zoo.load_url(model_urls['alexnet']))
		self.linear1 = nn.Linear(256*6*6,500)
		self.threshold1 = nn.Threshold(0, 1e-6)
		self.linear2 = nn.Linear(500,200)
		self.threshold2 = nn.Threshold(0, 1e-6)
		self.linear3 = nn.Linear(200,4)
예제 #10
0
 def forward(self, x):
     x = self.fc1(x)
     x = F.relu(x)
     x = self.fc2(x)
     x = F.relu(x)
     x = self.fc3(x)
     message_means = x[:, :self.message_length]
     message_sds = x[:, self.message_length:(2 * self.message_length)]
     message_sds = nn.Threshold(0.01, 0.01)(message_sds)
     action_logits = x[:, (2 * self.message_length):]
     action_probs = F.sigmoid(action_logits)
     action_probs = nn.Threshold(0.05, 0.05)(action_probs)
     return message_means, message_sds, action_probs
예제 #11
0
    def segment_batch(self, tensor_images, downsample=1):
        '''
        Returns a multilabel segmentation for the given batch of (RGB [-1...1])
        images.  Each pixel of the result is a torch.long indicating a
        predicted class number.  Multiple classes can be predicted for
        the same pixel: output shape is (n, multipred, y, x), where
        multipred is 3, 5, or 6, for how many different predicted labels can
        be given for each pixel (depending on whether subdivision is being
        used).  If downsample is specified, then the output y and x dimensions
        are downsampled from the original image.
        '''
        output_images, _, _ = self.model(tensor_images, None,
                                         [])  # N x 512 x W x H
        output_seg = []
        t = 1.5

        for i in range(len(output_images)):
            c_trans = torch.transpose(self.clusters, 0, 1)
            c_trans = c_trans[:, None, :]  # 512 x T
            clust_mean = self.mean_clust.view(-1, 1, 1)
            clust_mean = clust_mean.expand(output_images[i].size(0),
                                           output_images[i].size(1),
                                           output_images[i].size(2))
            std_clust = self.std_clust.view(-1, 1, 1)
            std_clust = std_clust.expand(output_images[i].size(0),
                                         output_images[i].size(1),
                                         output_images[i].size(2))

            im_normalized = (output_images[i] - clust_mean) / (std_clust +
                                                               1e-8)

            matchmap = utils.compute_matchmap(im_normalized,
                                              c_trans)  # H x W x N_clusters

            matchmap = matchmap.permute(2, 0, 1)  # N_c x H x W

            matchmap = torch.nn.functional.interpolate(matchmap[None, :, :, :],
                                                       size=(64, 64),
                                                       mode='bilinear')[0]

            matchmap = nn.Threshold(self.threshold, 0)(matchmap)
            matchmap = -nn.Threshold(-0.1, -1)(-matchmap)

            seg = torch.zeros(self.clusters.size(0), matchmap.size(1),
                              matchmap.size(2)).long().cuda()
            for c in range(self.clusters.size(0)):
                seg[c, :, :] = ((c + 1) * matchmap[c, :, :]).long()
            output_seg.append(seg)

        output_seg = torch.stack(output_seg)
        return output_seg
예제 #12
0
    def __init__(self):
        super(LeNet01, self).__init__()
        self.th1 = nn.Threshold(0.5, 0)
        self.th2 = nn.Threshold(-0.5, 1)
        self.conv1 = nn.Conv2d(1, 32, 3)
        self.conv2 = nn.Conv2d(32, 32, 3)
        self.conv3 = nn.Conv2d(32, 64, 3)
        self.conv4 = nn.Conv2d(64, 64, 3)

        self.fc1 = nn.Linear(1024, 200)
        self.dropout1 = nn.Dropout(0.5, inplace=True)
        self.fc2 = nn.Linear(200, 200)
        self.dropout2 = nn.Dropout(0.5, inplace=True)
        self.fc3 = nn.Linear(200, 10)
예제 #13
0
    def __init__(self):
        super(amplifier, self).__init__()

        self.relu = nn.Threshold(threshold=0, value=0.0001, inplace=True)

        self.conv_post = nn.Sequential(torch.nn.Linear(63, 128),
                                       torch.nn.Linear(128, 1))
예제 #14
0
    def forward(self, input, target):
        batchsize = input.size(0)
        codelength = input.size(1)

        D = pairwise_distances(input, input)
        S = labelToSim(target)

        inputabs = input.abs()
        ones = Variable(torch.ones(batchsize, codelength).cuda())
        input_ones_dist = (inputabs - ones).abs().sum(1)
        input_ones_dist = torch.stack([input_ones_dist] * batchsize)
        input_ones_dist = input_ones_dist + input_ones_dist.transpose(0, 1)
        input_ones_dist = input_ones_dist.abs()

        mask = getMask(batchsize)

        threshold_module = nn.Threshold(0, 0)
        notsimloss = threshold_module(self.bi_margin - D)
        loss = 0.5 * D * S + 0.5 * notsimloss * (1 - S) + self.tradeoff * (
            input_ones_dist)
        loss1 = ((0.5 * D * S * mask).sum() / mask.sum()).data[0]
        loss2 = ((0.5 * notsimloss * (1 - S) * mask).sum() /
                 mask.sum()).data[0]
        loss3 = ((self.tradeoff * (input_ones_dist) * mask).sum() /
                 mask.sum()).data[0]
        # with open(self.log, 'a') as f:
        #     f.write((('%.4f ' % (loss1)).rjust(15)) + '\t' + ('%.4f ' % (loss2)).rjust(15) + '\t' + (
        #             '%.4f ' % (loss3)).rjust(15) + '\n')
        loss = loss * mask
        loss = loss.sum() / mask.sum()
        return loss
예제 #15
0
    def __init__(self):

        super(FastNet50, self).__init__()
        self.trans = LinkNet50(n_classes=32)
        self.tanh = nn.Tanh()
        self.refine0 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
        self.refine1 = nn.Conv2d(64, 20, kernel_size=3, stride=1, padding=1)
        self.refine2 = nn.Conv2d(20, 20, kernel_size=3, stride=1, padding=1)
        self.threshold = nn.Threshold(0.1, 0.1)
        self.conv1010 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm
        self.conv1020 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm
        self.conv1030 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm
        self.conv1040 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm
        self.refine3 = nn.Conv2d(20 + 4, 3, kernel_size=3, stride=1, padding=1)
        self.upsample = F.upsample_nearest
        self.relu0 = nn.LeakyReLU(0.2)
        self.relu1 = nn.LeakyReLU(0.2)
        self.relu2 = nn.LeakyReLU(0.2)
        self.relu3 = nn.LeakyReLU(0.2)
        self.relu4 = nn.LeakyReLU(0.2)
        self.relu5 = nn.LeakyReLU(0.2)
        self.relu6 = nn.LeakyReLU(0.2)
예제 #16
0
 def __init__(self, alphasize, cnn_dim, word_dim):
     super(TextEncoderCNN, self).__init__()
     self.emb = nn.Embedding(alphasize, word_dim)
     # -- alphasize x 201 (201 is the imagined dimension of the sentence)
     self.main = nn.Sequential(
         # nn.Conv1d(alphasize, 384, 4),
         nn.Conv1d(word_dim, 384, 4),
         nn.Threshold(1e-6, 0),
         nn.MaxPool1d(3, 3),
         # -- 384 x 66
         nn.Conv1d(384, 512, 4),
         nn.Threshold(1e-6, 0),
         nn.MaxPool1d(3, 3),
         # -- 512 x 21
         nn.Conv1d(512, cnn_dim, 4),
         nn.Threshold(1e-6, 0))
예제 #17
0
    def __init__(self):
        super(OverlapNet.self).__init__()
        self.start_block = nn.Sequential(nn.Conv2d(1, 64, 3, 1, 1),
                                         nn.BatchNorm2d(64))

        self.hidden_block_1 = generate_nn_block()
        self.hidden_block_2 = generate_nn_block()
        self.hidden_block_3 = generate_nn_block()
        self.hidden_block_4 = generate_nn_block()
        self.hidden_block_5 = generate_nn_block()
        self.hidden_block_6 = generate_nn_block()
        self.hidden_block_7 = generate_nn_block()
        self.hidden_block_8 = generate_nn_block()
        self.hidden_block_9 = generate_nn_block()
        self.hidden_block_10 = generate_nn_block()
        self.hidden_block_11 = generate_nn_block()
        self.hidden_block_12 = generate_nn_block()
        self.hidden_block_13 = generate_nn_block()
        self.hidden_block_14 = generate_nn_block()
        self.hidden_block_15 = generate_nn_block()
        self.hidden_block_16 = generate_nn_block()
        self.hidden_block_17 = generate_nn_block()
        self.hidden_block_18 = generate_nn_block()
        self.hidden_block_19 = generate_nn_block()
        # 最后一层是输出一个channel 利用sigmoid函数再辅助一个threshhold
        self.end_block = nn.Sequential(
            nn.Conv2d(64, 1, 3, 1, 1),
            nn.Sigmoid(),
            nn.Threshold(
                0.5, 0
            )  # 这里我们实际上为了能迭代快一些九八小于0.5的地方全部都变成0 如果能把大于0.5都变成1实际上更好 但是还没找到办法
        )
예제 #18
0
def parse_activation(act):
    if act is None:
        return lambda x: x

    act, kwargs = parse_str(act)

    if act == 'sigmoid': return nn.Sigmoid(**kwargs)
    if act == 'tanh': return nn.Tanh(**kwargs)
    if act == 'relu': return nn.ReLU(**kwargs, inplace=True)
    if act == 'relu6': return nn.ReLU6(**kwargs, inplace=True)
    if act == 'elu': return nn.ELU(**kwargs, inplace=True)
    if act == 'selu': return nn.SELU(**kwargs, inplace=True)
    if act == 'prelu': return nn.PReLU(**kwargs)
    if act == 'leaky_relu': return nn.LeakyReLU(**kwargs, inplace=True)
    if act == 'threshold': return nn.Threshold(**kwargs, inplace=True)
    if act == 'hardtanh': return nn.Hardtanh(**kwargs, inplace=True)
    if act == 'log_sigmoid': return nn.LogSigmoid(**kwargs)
    if act == 'softplus': return nn.Softplus(**kwargs)
    if act == 'softshrink': return nn.Softshrink(**kwargs)
    if act == 'tanhshrink': return nn.Tanhshrink(**kwargs)
    if act == 'softmin': return nn.Softmin(**kwargs)
    if act == 'softmax': return nn.Softmax(**kwargs)
    if act == 'softmax2d': return nn.Softmax2d(**kwargs)
    if act == 'log_softmax': return nn.LogSoftmax(**kwargs)

    raise ValueError(f'unknown activation: {repr(act)}')
    def __init__(self):
        super(VAECUREGrad, self).__init__()

        self.down = nn.Sequential(
            nn.Conv2d(3, 3, 4, stride=2, padding=2, bias=False),
            nn.Threshold(0, 1),
            nn.Conv2d(3, 6, 4, stride=2, padding=2, bias=False),
            nn.Threshold(0, 1),
            nn.Conv2d(6, 9, 4, stride=2, padding=2, bias=False),
            nn.Threshold(0, 1),
            nn.Conv2d(9, 12, 4, stride=2, padding=2, bias=False),
            # nn.Threshold(0, 1),
        )

        self.fc11 = nn.Linear(3 * 3 * 12, 20, bias=False)
        self.sigmoid = nn.Sigmoid()
 def define_module(self):
     self.conv1 = nn.Conv1d(self.d_vocab, 256, 1)
     self.threshold = nn.Threshold(0.000001, 0)
     self.conv2 = nn.Conv1d(256, self.d_text_enc_cnn, 1)
     self.rnn = nn.GRU(self.d_text_enc_cnn, self.d_text_enc_cnn)
     self.linear = nn.Linear(self.d_text_enc_cnn, self.d_text_feature)
     self.dropout_layer = nn.Dropout(self.text_enc_dropout)
예제 #21
0
    def __init__(self,
                 key_size,
                 query_size,
                 hidden_size,
                 novelty_bias=1,
                 local_noise=.5,
                 cosine_threshold=.4):
        super(cosR5, self).__init__()
        self.hidden_size = hidden_size
        self.novelty_bias = novelty_bias

        self.cos = nn.CosineSimilarity(dim=-1)
        self.alpha_l1 = nn.Linear(2, 2, bias=False)
        self.alpha_l2 = nn.Linear(hidden_size, 1, bias=False)
        self.alpha_threshold = nn.Threshold(cosine_threshold, 0.)
        self.alphaMax = nn.Softmax(dim=0)

        self.discount_threshold = .15  #nn.Threshold(.15, 0.)

        # Layers
        self.out = nn.Linear(hidden_size, hidden_size, bias=False)

        # Non linear activations
        self.softmaxEl = nn.Softmax(dim=-1)
        self.relu = nn.ReLU()
        self.sig = nn.Sigmoid()

        self.current_drop = nn.Dropout(local_noise)
        self.alphas = None
예제 #22
0
    def setup(self):

        self.model_names = ['pix2depth', 'hpe']
        self.pix2depth_model = Pix2DepthModel(self.options.pix2depth, self.gpu_ids)
        self.hpe_model = HPEModel(self.options.hpe, self.gpu_ids)

        self.img_size = self.options.general.img_size
        projector = FisheyeProjector(self.img_size)

        self.hpe_model.setup(make_optimizer = False)
        self.pix2depth_model.setup(make_optimizer = False)

        # removes very small depths to create a correct hand segmentation mask.
        self.depth_threshold = nn.Threshold(threshold = self.options.general.min_depth_thrs, value = 0, inplace=True)

        self.is_train = self._is_all_model_train_mode()
        if self.is_train:
            pix2depth_opt = self.options.pix2depth
            hpe_opt = self.options.hpe

            self.pix2depth_optimizer = torch.optim.Adam(self.pix2depth_model.get_net_parameters(), lr=pix2depth_opt.lr, betas = (pix2depth_opt.beta1, 0.999))
            self.hpe_optimizer = torch.optim.Adam(self._get_hpe_parameters(), lr=hpe_opt.lr)

        self.mode = self.pix2depth_model.mode
        self.is_setup = True

        self.check_and_load_pretrained()
예제 #23
0
    def __init__(self, input_nc, output_nc, nf):
        super(dehaze, self).__init__()
        self.tran_est = G(input_nc=3, output_nc=3, nf=64)
        self.atp_est = G2(input_nc=3, output_nc=3, nf=8)

        self.tran_dense = Dense()
        self.relu = nn.LeakyReLU(0.2, inplace=True)
        # self.relu5=nn.ReLU6()

        self.tanh = nn.Tanh()

        self.refine1 = nn.Conv2d(6, 20, kernel_size=3, stride=1, padding=1)
        self.refine2 = nn.Conv2d(20, 20, kernel_size=3, stride=1, padding=1)
        self.threshold = nn.Threshold(0.1, 0.1)

        self.conv1010 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm
        self.conv1020 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm
        self.conv1030 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm
        self.conv1040 = nn.Conv2d(20, 1, kernel_size=1, stride=1,
                                  padding=0)  # 1mm

        self.refine3 = nn.Conv2d(20 + 4, 3, kernel_size=3, stride=1, padding=1)

        self.upsample = F.upsample_nearest

        self.batch1 = nn.BatchNorm2d(20)
예제 #24
0
    def __init__(self, args, no_encoder_attn=False):
        super().__init__()
        self.embed_dim = args.decoder_embed_dim
        self.self_attn = DecoderAttention(
            self.embed_dim,
            args.decoder_attention_heads,
            dropout=args.attention_dropout,
        )
        self.dropout = nn.Dropout(p=args.dropout)
        self.relu_dropout = nn.Dropout(p=args.relu_dropout)
        self.normalize_before = args.decoder_normalize_before
        self.fuse_dropout_add = args.fuse_dropout_add
        self.fuse_relu_dropout = args.fuse_relu_dropout

        self.self_attn_layer_norm = FusedLayerNorm(self.embed_dim)

        if no_encoder_attn:
            self.encoder_attn = None
            self.encoder_attn_layer_norm = None
        else:
            self.encoder_attn = EncoderAttention(
                self.embed_dim,
                args.decoder_attention_heads,
                dropout=args.attention_dropout,
                static_kv=True)
            self.encoder_attn_layer_norm = FusedLayerNorm(self.embed_dim)

        self.fc1 = Linear(self.embed_dim, args.decoder_ffn_embed_dim)
        self.fc2 = Linear(args.decoder_ffn_embed_dim, self.embed_dim)

        self.final_layer_norm = FusedLayerNorm(self.embed_dim)
        self.need_attn = True
        self.threshold = nn.Threshold(0, 0)
def truncate(input, threshold):
    threshold = -threshold
    output = -input
    nn_threshold = nn.Threshold(threshold, -1)
    output = nn_threshold(output)
    output = -output
    return output
예제 #26
0
    def forward(self, input, target, mask):
        mask = torch.cat((mask, mask, mask), dim=1)
        mask = nn.Threshold(0.01, 0)(mask).bool()
        input = torch.masked_select(input, mask)
        target = torch.masked_select(target, mask)

        return nn.L1Loss()(input, target)
예제 #27
0
 def __init__(self,
              noise_dim,
              output_channels=1,
              n_filters=16,
              threshold=0):
     super(Generator, self).__init__()
     # Number of filters in final generator layer
     ngf = n_filters
     # Construct the model as a sequence of layers
     self.network = nn.Sequential(
         nn.ConvTranspose2d(noise_dim, ngf * 8, 4, 1, 0, bias=False),
         nn.BatchNorm2d(ngf * 8),
         nn.ReLU(True),
         # state size. (ngf*8) x 4 x 4
         nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
         nn.BatchNorm2d(ngf * 4),
         nn.ReLU(True),
         # state size. (ngf*4) x 8 x 8
         nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
         nn.BatchNorm2d(ngf * 2),
         nn.ReLU(True),
         # state size. (ngf*2) x 16 x 16
         nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
         nn.BatchNorm2d(ngf),
         nn.ReLU(True),
         # state size. (ngf) x 32 x 32
         nn.ConvTranspose2d(ngf, output_channels, 4, 2, 1, bias=False),
         nn.Sigmoid(),
         nn.Threshold(threshold, 0)
         # state size. (nc) x 64 x 64
     )
예제 #28
0
    def __init__(self,
                 V,
                 rank,
                 max_iterations=100000,
                 tolerance=1e-8,
                 test_conv=1000,
                 gpu_id=0,
                 seed=None,
                 init_method='random',
                 floating_point_precision='double',
                 min_iterations=2000):
        """
        Run non-negative matrix factorisation using GPU. Uses beta-divergence.

        Args:
          V: Matrix to be factorised
          rank: (int) number of latent dimensnions to use in factorisation
          max_iterations: (int) Maximum number of update iterations to use during fitting
          tolerance: tolerance to use in convergence tests. Lower numbers give longer times to convergence
          test_conv: (int) How often to test for convergnce
          gpu_id: (int) Which GPU device to use
          seed: random seed, if None (default) datetime is used
          init_method: how to initialise basis and coefficient matrices, options are:
            - random (will always be the same if seed != None)
            - NNDSVD
            - NNDSVDa (fill in the zero elements with the average),
            - NNDSVDar (fill in the zero elements with random values in the space [0:average/100]).
          floating_point_precision: (string or type). Can be `double`, `float` or any type/string which
              torch can interpret.
          min_iterations: the minimum number of iterations to execute before termination. Useful when using
              fp32 tensors as convergence can happen too early.
        """
        torch.cuda.set_device(gpu_id)

        if seed is None:
            seed = datetime.now().timestamp()

        if floating_point_precision == 'float':
            self._tensor_type = torch.FloatTensor
        elif floating_point_precision == 'double':
            self._tensor_type = torch.DoubleTensor
        else:
            self._tensor_type = floating_point_precision

        torch.manual_seed(seed)
        torch.cuda.manual_seed(seed)

        self.max_iterations = max_iterations
        self.min_iterations = min_iterations

        self._V = V.type(self._tensor_type).cuda()
        self._fix_neg = nn.Threshold(0., 1e-8)
        self._tolerance = tolerance
        self._prev_loss = None
        self._iter = 0
        self._test_conv = test_conv
        self._gpu_id = gpu_id
        self._rank = rank
        self._W, self._H = self._initialise_wh(init_method)
예제 #29
0
 def __init__(self, input_size, hidden_size):
     super(Autoencoder, self).__init__()
     self.layer1 = nn.Sequential(
         nn.Linear(input_size, hidden_size),
         nn.Threshold(1e-5, 0),  # Set units < 1e-5 to zero
         nn.ReLU()  # Activation function
     )
     self.output = nn.Sequential(nn.Linear(hidden_size, input_size))
예제 #30
0
 def __init__(self, threshold=0.5, n_iters=2, k_size=10):
     super().__init__()
     self._opening = MorphologicalOpening(n_iters=n_iters, k_size=k_size)
     self._blur = nn.AvgPool2d(k_size, stride=1)
     self._thresh = nn.Threshold(threshold=threshold,
                                 value=0,
                                 inplace=False)
     self._norm = Normalize()