我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用torch.nn.Upsample()。
def forward(self,x): output = self.Scale(x) # for original scale output_size = output.size()[2] input_size = x.size()[2] self.interp1 = nn.Upsample(size=(int(input_size*0.75)+1, int(input_size*0.75)+1), mode='bilinear') self.interp2 = nn.Upsample(size=(int(input_size*0.5)+1, int(input_size*0.5)+1), mode='bilinear') self.interp3 = nn.Upsample(size=(output_size, output_size), mode='bilinear') x75 = self.interp1(x) output75 = self.interp3(self.Scale(x75)) # for 0.75x scale x5 = self.interp2(x) output5 = self.interp3(self.Scale(x5)) # for 0.5x scale out_max = torch.max(torch.max(output, output75), output5) return [output, output75, output5, out_max]
def __init__(self, conv_channels, input_nch, output_nch, groups=1): super(TriangleNet, self).__init__() self.input_nch = input_nch self.output_nch = output_nch self.pyramid_height = len(conv_channels) blocks = [list() for _ in range(self.pyramid_height)] for i in range(self.pyramid_height): for j in range(i, self.pyramid_height): if i == 0 and j == 0: blocks[i].append(BasicResBlock(input_nch, conv_channels[j], groups=groups)) else: blocks[i].append(BasicResBlock(conv_channels[j-1], conv_channels[j], groups=groups)) for i in range(self.pyramid_height): blocks[i] = nn.ModuleList(blocks[i]) self.blocks = nn.ModuleList(blocks) self.down_sample = nn.MaxPool2d(3, 2, 1) self.up_samples = nn.ModuleList([nn.Upsample(scale_factor=2**i, mode='bilinear') for i in range(1, self.pyramid_height)]) self.channel_out_convs = nn.ModuleList([nn.Conv2d(conv_channels[-1], output_nch, 1) for _ in range(self.pyramid_height)]) self.out_conv = nn.Conv2d(self.pyramid_height * conv_channels[-1], output_nch, 1)
def __init__(self, conv_channels, input_nch, output_nch, groups): super(PSPTriangleNet, self).__init__() self.input_nch = input_nch self.output_nch = output_nch self.pyramid_height = len(conv_channels) blocks = [] for i in range(self.pyramid_height-1): if i == 0: blocks.append(BasicResBlock(input_nch, conv_channels[i], groups=groups)) else: blocks.append(BasicResBlock(conv_channels[i-1], conv_channels[i], groups=groups)) ms_blocks = [] for i in range(self.pyramid_height): ms_blocks.append(BasicResBlock(conv_channels[-2], conv_channels[-1]//self.pyramid_height)) self.blocks = nn.ModuleList(blocks) self.ms_blocks = nn.ModuleList(ms_blocks) self.down_samples = nn.ModuleList([nn.MaxPool2d(2**i+1, 2**i, 2**(i-1)) for i in range(1, self.pyramid_height)]) self.up_samples = nn.ModuleList([nn.Upsample(scale_factor=2**i, mode='bilinear') for i in range(1, self.pyramid_height)]) self.channel_out_convs = nn.ModuleList([nn.Conv2d(conv_channels[-1]//self.pyramid_height, output_nch, 1) for _ in range(self.pyramid_height)]) self.out_conv = nn.Conv2d(conv_channels[-1], output_nch, 1)
def upconv2x2(in_channels, out_channels, mode='transpose'): if mode == 'transpose': return nn.ConvTranspose2d( in_channels, out_channels, kernel_size=2, stride=2) else: # out_channels is always going to be the same # as in_channels return nn.Sequential( nn.Upsample(mode='bilinear', scale_factor=2), conv1x1(in_channels, out_channels))
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upconv'): super().__init__() self.l1 = Conv3BN(in_, out, bn, activation) self.l2 = Conv3BN(out, out, bn, activation) if up == 'upconv': self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) elif up == 'upsample': self.up = nn.Upsample(scale_factor=2)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upsample'): super().__init__() self.l1 = Conv3BN(in_, out, bn, activation) self.l2 = Conv3BN(out, out, bn, activation) self.l3 = Conv3BN(out, out, bn, activation) if up == 'upconv': self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) elif up == 'upsample': self.up = nn.Upsample(scale_factor=2)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upsample'): super().__init__() self.l1 = Conv3BN(in_, out, bn, activation) self.l2 = Conv3BN(out, out, bn, activation) self.l3 = Conv3BN(out, out, bn, activation) self.l4 = Conv3BN(out, out, bn, activation) if up == 'upconv': self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) elif up == 'upsample': self.up = nn.Upsample(scale_factor=2)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upsample'): super().__init__() self.l1 = Conv3BN(in_, out, bn, activation) self.l2 = Conv3BN(out, out, bn, activation) self.l3 = Conv3BN(out, out, bn, activation) self.l4 = Conv3BN(out, out, bn, activation) self.l5 = Conv3BN(out, out, bn, activation) if up == 'upconv': self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) elif up == 'upsample': self.up = nn.Upsample(scale_factor=2)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upsample'): super().__init__() self.l1 = Dilation_Conv3BN(in_, out, bn, activation, dilation=1) self.l2 = Dilation_Conv3BN(out, out, bn, activation, dilation=2) self.l3 = Dilation_Conv3BN(out, out, bn, activation, dilation=3) if up == 'upconv': self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) elif up == 'upsample': self.up = nn.Upsample(scale_factor=2)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upsample'): super().__init__() self.l1 = InceptionModule(in_, out, bn, activation) if up == 'upconv': self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) elif up == 'upsample': self.up = nn.Upsample(scale_factor=2)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upconv'): super().__init__() self.l1 = Conv3BN(in_, out, bn, activation) self.l2 = Conv3BN(out, out, bn, activation) self.l3 = Conv3BN(out, out, bn, activation) #if up == 'upconv': # self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) #elif up == 'upsample': # self.up = nn.Upsample(scale_factor=2) channel=in_-out self.up = DUC(channel, channel*4)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upconv'): super().__init__() self.l1 = Conv3BN(in_, out, bn, activation) self.l2 = Conv3BN(out, out, bn, activation) self.l3 = Conv3BN(out, out, bn, activation) self.l4 = Conv3BN(out, out, bn, activation) #if up == 'upconv': # self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) #elif up == 'upsample': # self.up = nn.Upsample(scale_factor=2) channel=in_-out self.up = DUC(channel, channel*4)
def __init__(self, in_: int, out: int, *, bn=True, activation='relu', up='upsample'): super().__init__() self.l1 = DenseUNetModule(in_, out, bn=bn, activation=activation) if up == 'upconv': self.up = nn.ConvTranspose2d(in_, out, 2, stride=2) elif up == 'upsample': self.up = nn.Upsample(scale_factor=2)
def main(): """Create the model and start the evaluation process.""" args = get_arguments() gpu0 = args.gpu model = Res_Deeplab(num_classes=args.num_classes) saved_state_dict = torch.load(args.restore_from) model.load_state_dict(saved_state_dict) model.eval() model.cuda(gpu0) testloader = data.DataLoader(VOCDataSet(args.data_dir, args.data_list, crop_size=(505, 505), mean=IMG_MEAN, scale=False, mirror=False), batch_size=1, shuffle=False, pin_memory=True) interp = nn.Upsample(size=(505, 505), mode='bilinear') data_list = [] for index, batch in enumerate(testloader): if index % 100 == 0: print('%d processd'%(index)) image, label, size, name = batch size = size[0].numpy() output = model(Variable(image, volatile=True).cuda(gpu0)) output = interp(output).cpu().data[0].numpy() output = output[:,:size[0],:size[1]] gt = np.asarray(label[0].numpy()[:size[0],:size[1]], dtype=np.int) output = output.transpose(1,2,0) output = np.asarray(np.argmax(output, axis=2), dtype=np.int) # show_all(gt, output) data_list.append([gt.flatten(), output.flatten()]) get_iou(data_list, args.num_classes)
def __init__(self): super(VAE, self).__init__() self.hidden_size = 64 #Encoder layers self.enc_conv1 = nn.Conv2d(2, 128, 5, stride=2, padding=2) self.enc_bn1 = nn.BatchNorm2d(128) self.enc_conv2 = nn.Conv2d(128, 256, 5, stride=2, padding=2) self.enc_bn2 = nn.BatchNorm2d(256) self.enc_conv3 = nn.Conv2d(256, 512, 5, stride=2, padding=2) self.enc_bn3 = nn.BatchNorm2d(512) self.enc_conv4 = nn.Conv2d(512, 1024, 3, stride=2, padding=1) self.enc_bn4 = nn.BatchNorm2d(1024) self.enc_fc1 = nn.Linear(4*4*1024, self.hidden_size*2) self.enc_dropout1 = nn.Dropout(p=.7) #Cond encoder layers self.cond_enc_conv1 = nn.Conv2d(1, 128, 5, stride=2, padding=2) self.cond_enc_bn1 = nn.BatchNorm2d(128) self.cond_enc_conv2 = nn.Conv2d(128, 256, 5, stride=2, padding=2) self.cond_enc_bn2 = nn.BatchNorm2d(256) self.cond_enc_conv3 = nn.Conv2d(256, 512, 5, stride=2, padding=2) self.cond_enc_bn3 = nn.BatchNorm2d(512) self.cond_enc_conv4 = nn.Conv2d(512, 1024, 3, stride=2, padding=1) self.cond_enc_bn4 = nn.BatchNorm2d(1024) #Decoder layers self.dec_upsamp1 = nn.Upsample(scale_factor=4, mode='bilinear') self.dec_conv1 = nn.Conv2d(1024+self.hidden_size, 512, 3, stride=1, padding=1) self.dec_bn1 = nn.BatchNorm2d(512) self.dec_upsamp2 = nn.Upsample(scale_factor=2, mode='bilinear') self.dec_conv2 = nn.Conv2d(512*2, 256, 5, stride=1, padding=2) self.dec_bn2 = nn.BatchNorm2d(256) self.dec_upsamp3 = nn.Upsample(scale_factor=2, mode='bilinear') self.dec_conv3 = nn.Conv2d(256*2, 128, 5, stride=1, padding=2) self.dec_bn3 = nn.BatchNorm2d(128) self.dec_upsamp4 = nn.Upsample(scale_factor=2, mode='bilinear') self.dec_conv4 = nn.Conv2d(128*2, 64, 5, stride=1, padding=2) self.dec_bn4 = nn.BatchNorm2d(64) self.dec_upsamp5 = nn.Upsample(scale_factor=2, mode='bilinear') self.dec_conv5 = nn.Conv2d(64, 2, 5, stride=1, padding=2)
def test_upsamplingNearest1d(self): m = nn.Upsample(size=4, mode='nearest') in_t = torch.ones(1, 1, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4), out_t.data) input = Variable(torch.randn(1, 1, 2), requires_grad=True) self.assertTrue(gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), (input,)))
def test_upsamplingLinear1d(self): m = nn.Upsample(size=4, mode='linear') in_t = torch.ones(1, 1, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4), out_t.data) input = Variable(torch.randn(1, 1, 2), requires_grad=True) self.assertTrue(gradcheck(lambda x: F.upsample(x, 4, mode='linear'), (input,)))
def test_upsamplingNearest2d(self): m = nn.Upsample(size=4, mode='nearest') in_t = torch.ones(1, 1, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2), requires_grad=True) self.assertTrue(gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), (input,)))
def test_upsamplingBilinear2d(self): m = nn.Upsample(size=4, mode='bilinear') in_t = torch.ones(1, 1, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2), requires_grad=True) self.assertTrue(gradcheck(lambda x: F.upsample(x, 4, mode='bilinear'), (input,)))
def test_upsamplingNearest3d(self): m = nn.Upsample(size=4, mode='nearest') in_t = torch.ones(1, 1, 2, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2, 2), requires_grad=True) self.assertTrue(gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), (input,)))
def __init__(self, block, num_blocks, planes, depth): super(Hourglass, self).__init__() self.depth = depth self.block = block self.upsample = nn.Upsample(scale_factor=2) self.hg = self._make_hour_glass(block, num_blocks, planes, depth)
def test_upsamplingNearest1d(self): m = nn.Upsample(size=4, mode='nearest') in_t = torch.ones(1, 1, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4), out_t.data) input = Variable(torch.randn(1, 1, 2), requires_grad=True) gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input])
def test_upsamplingLinear1d(self): m = nn.Upsample(size=4, mode='linear') in_t = torch.ones(1, 1, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4), out_t.data) input = Variable(torch.randn(1, 1, 2), requires_grad=True) gradcheck(lambda x: F.upsample(x, 4, mode='linear'), (input,))
def test_upsamplingNearest2d(self): m = nn.Upsample(size=4, mode='nearest') in_t = torch.ones(1, 1, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2), requires_grad=True) self.assertEqual( F.upsample(input, 4, mode='nearest'), F.upsample(input, scale_factor=2, mode='nearest')) gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input]) gradgradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input])
def test_upsamplingBilinear2d(self): m = nn.Upsample(size=4, mode='bilinear') in_t = torch.ones(1, 1, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2), requires_grad=True) gradcheck(lambda x: F.upsample(x, 4, mode='bilinear'), [input])
def test_upsamplingNearest3d(self): m = nn.Upsample(size=4, mode='nearest') in_t = torch.ones(1, 1, 2, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2, 2), requires_grad=True) gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input])
def __init__(self, in_channels, out_channels): super(ConvBlock, self).__init__() self.upsample = nn.Upsample(scale_factor = 2, mode = 'nearest') self.conv = nn.Conv2d(in_channels, out_channels, kernel_size = 3, stride = 1, padding = 1, bias = False) self.bn = nn.BatchNorm2d(out_channels)
def main(): """Create the model and start the evaluation process.""" args = get_arguments() gpu0 = args.gpu model = Res_Deeplab(num_classes=args.num_classes) saved_state_dict = torch.load(args.restore_from) model.load_state_dict(saved_state_dict) model.eval() model.cuda(gpu0) testloader = data.DataLoader(VOCDataSet(args.data_dir, args.data_list, crop_size=(505, 505), mean=IMG_MEAN, scale=False, mirror=False), batch_size=1, shuffle=False, pin_memory=True) interp = nn.Upsample(size=(505, 505), mode='bilinear') data_list = [] for index, batch in enumerate(testloader): if index % 100 == 0: print('%d processd'%(index)) images, label, size, name = batch images = Variable(images, volatile=True) h, w, c = size[0].numpy() images075 = nn.Upsample(size=(int(h*0.75), int(w*0.75)), mode='bilinear')(images) images05 = nn.Upsample(size=(int(h*0.5), int(w*0.5)), mode='bilinear')(images) out100 = model(images.cuda(args.gpu)) out075 = model(images075.cuda(args.gpu)) out05 = model(images05.cuda(args.gpu)) o_h, o_w = out100.size()[2:] interpo1 = nn.Upsample(size=(o_h, o_w), mode='bilinear') out_max = torch.max(torch.stack([out100, interpo1(out075), interpo1(out05)]), dim=0)[0] output = interp(out_max).cpu().data[0].numpy() output = output[:,:h,:w] output = output.transpose(1,2,0) output = np.asarray(np.argmax(output, axis=2), dtype=np.int) gt = np.asarray(label[0].numpy()[:h,:w], dtype=np.int) # show_all(gt, output) data_list.append([gt.flatten(), output.flatten()]) get_iou(data_list, args.num_classes)
def __init__(self, pretrain=False): super(DarkNet, self).__init__() self.pretrain = pretrain self.net = nn.Sequential( # nn.Upsample(scale_factor=2), nn.Conv2d(3, 64, 7, stride=2, padding=3), nn.LeakyReLU(0.1, inplace=True), nn.MaxPool2d(2), nn.Conv2d(64, 192, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.MaxPool2d(2), nn.Conv2d(192, 128, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(128, 256, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(256, 256, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(256, 512, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.MaxPool2d(2), nn.Conv2d(512, 256, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(256, 512, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(512, 256, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(256, 512, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(512, 256, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(256, 512, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(512, 256, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(256, 512, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(512, 512, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(512, 1024, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.MaxPool2d(2), nn.Conv2d(1024, 512, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(512, 1024, 3, padding=1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(1024, 512, 1), nn.LeakyReLU(0.1, inplace=True), nn.Conv2d(512, 1024, 3, padding=1), nn.LeakyReLU(0.1, inplace=True) ) if self.pretrain: self.fc = nn.Linear(1024, 1000)