Python torch.autograd.Variable() Examples

The following are 30 code examples of torch.autograd.Variable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch.autograd , or try the search function .
Example #1
Source File: DDPAE.py    From DDPAE-video-prediction with MIT License 6 votes vote down vote up
def test(self, input, output):
    '''
    Return decoded output.
    '''
    input = Variable(input.cuda())
    batch_size, _, _, H, W = input.size()
    output = Variable(output.cuda())
    gt = torch.cat([input, output], dim=1)

    latent = self.encode(input, sample=False)
    decoded_output, components = self.decode(latent, input.size(0))
    decoded_output = decoded_output.view(*gt.size())
    components = components.view(batch_size, self.n_frames_total, self.total_components,
                                 self.n_channels, H, W)
    latent['components'] = components
    decoded_output = decoded_output.clamp(0, 1)

    self.save_visuals(gt, decoded_output, components, latent)
    return decoded_output.cpu(), latent 
Example #2
Source File: googlenet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def forward(self, x):
        out = self.pre_layers(x)
        out = self.a3(out)
        out = self.b3(out)
        out = self.maxpool(out)
        out = self.a4(out)
        out = self.b4(out)
        out = self.c4(out)
        out = self.d4(out)
        out = self.e4(out)
        out = self.maxpool(out)
        out = self.a5(out)
        out = self.b5(out)
        out = self.avgpool(out)
        out = out.view(out.size(0), -1)
        out = self.linear(out)
        return out

# net = GoogLeNet()
# x = torch.randn(1,3,32,32)
# y = net(Variable(x))
# print(y.size()) 
Example #3
Source File: vgg.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _make_layers(self, cfg):
        layers = []
        in_channels = 3
        for x in cfg:
            if x == 'M':
                layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
            else:
                layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
                           nn.BatchNorm2d(x),
                           nn.ReLU(inplace=True)]
                in_channels = x
        layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
        return nn.Sequential(*layers)

# net = VGG('VGG11')
# x = torch.randn(2,3,32,32)
# print(net(Variable(x)).size()) 
Example #4
Source File: vgg.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _make_layers(self, cfg):
        layers = []
        in_channels = 3
        for x in cfg:
            if x == 'M':
                layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
            else:
                layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
                           nn.BatchNorm2d(x),
                           nn.ReLU(inplace=True)]
                in_channels = x
        layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
        return nn.Sequential(*layers)

# net = VGG('VGG11')
# x = torch.randn(2,3,32,32)
# print(net(Variable(x)).size()) 
Example #5
Source File: DDPAE.py    From DDPAE-video-prediction with MIT License 6 votes vote down vote up
def train(self, input, output):
    '''
    param input: video of size (batch_size, n_frames_input, C, H, W)
    param output: video of size (batch_size, self.n_frames_output, C, H, W)
    Return video_dict, loss_dict
    '''
    input = Variable(input.cuda(), requires_grad=False)
    output = Variable(output.cuda(), requires_grad=False)
    assert input.size(1) == self.n_frames_input

    # SVI
    batch_size, _, C, H, W = input.size()
    numel = batch_size * self.n_frames_total * C * H * W
    loss_dict = {}
    for name, svi in self.svis.items():
      # loss = svi.step(input, output)
      # Note: backward() is already called in loss_and_grads.
      loss = svi.loss_and_grads(svi.model, svi.guide, input, output)
      loss_dict[name] = loss / numel

    # Update parameters
    self.optimizer.step()
    self.optimizer.zero_grad()

    return {}, loss_dict 
Example #6
Source File: vgg.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _make_layers(self, cfg):
        layers = []
        in_channels = 3
        for x in cfg:
            if x == 'M':
                layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
            else:
                layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
                           nn.BatchNorm2d(x),
                           nn.ReLU(inplace=True)]
                in_channels = x
        layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
        return nn.Sequential(*layers)

# net = VGG('VGG11')
# x = torch.randn(2,3,32,32)
# print(net(Variable(x)).size()) 
Example #7
Source File: DDPAE_utils.py    From DDPAE-video-prediction with MIT License 6 votes vote down vote up
def pose_inv_full(pose):
  '''
  param pose: N x 6
  Inverse the 2x3 transformer matrix.
  '''
  N, _ = pose.size()
  b = pose.view(N, 2, 3)[:, :, 2:]
  # A^{-1}
  # Calculate determinant
  determinant = (pose[:, 0] * pose[:, 4] - pose[:, 1] * pose[:, 3] + 1e-8).view(N, 1)
  indices = Variable(torch.LongTensor([4, 1, 3, 0]).cuda())
  scale = Variable(torch.Tensor([1, -1, -1, 1]).cuda())
  A_inv = torch.index_select(pose, 1, indices) * scale / determinant
  A_inv = A_inv.view(N, 2, 2)
  # b' = - A^{-1} b
  b_inv = - A_inv.matmul(b).view(N, 2, 1)
  transformer_inv = torch.cat([A_inv, b_inv], dim=2)
  return transformer_inv 
Example #8
Source File: googlenet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def forward(self, x):
        out = self.pre_layers(x)
        out = self.a3(out)
        out = self.b3(out)
        out = self.maxpool(out)
        out = self.a4(out)
        out = self.b4(out)
        out = self.c4(out)
        out = self.d4(out)
        out = self.e4(out)
        out = self.maxpool(out)
        out = self.a5(out)
        out = self.b5(out)
        out = self.avgpool(out)
        out = out.view(out.size(0), -1)
        out = self.linear(out)
        return out

# net = GoogLeNet()
# x = torch.randn(1,3,32,32)
# y = net(Variable(x))
# print(y.size()) 
Example #9
Source File: metrics.py    From DDPAE-video-prediction with MIT License 6 votes vote down vote up
def update(self, gt, pred):
    """
    gt, pred are tensors of size (..., 1, H, W) in the range [0, 1].
    """
    C, H, W = gt.size()[-3:]
    if isinstance(gt, torch.Tensor):
      gt = Variable(gt)
    if isinstance(pred, torch.Tensor):
      pred = Variable(pred)

    mse_score = self.mse_loss(pred, gt)
    eps = 1e-4
    pred.data[pred.data < eps] = eps
    pred.data[pred.data > 1 - eps] = 1 -eps
    bce_score = self.bce_loss(pred, gt)
    bce_score = bce_score.item() * C * H * W
    mse_score = mse_score.item() * C * H * W
    self.bce_results.append(bce_score)
    self.mse_results.append(mse_score) 
Example #10
Source File: shufflenet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = ShuffleNetG2()
    x = Variable(torch.randn(1,3,32,32))
    y = net(x)
    print(y)

# test() 
Example #11
Source File: mnist_tutorial_pytorch.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), 2)
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, 64 * 7 * 7)  # reshape Variable
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=-1) 
Example #12
Source File: shufflenet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = ShuffleNetG2()
    x = Variable(torch.randn(1,3,32,32))
    y = net(x)
    print(y)

# test() 
Example #13
Source File: preact_resnet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = PreActResNet18()
    y = net(Variable(torch.randn(1,3,32,32)))
    print(y.size())

# test() 
Example #14
Source File: senet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = SENet18()
    y = net(Variable(torch.randn(1,3,32,32)))
    print(y.size())

# test() 
Example #15
Source File: dpn.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = DPN92()
    x = Variable(torch.randn(1,3,32,32))
    y = net(x)
    print(y)

# test() 
Example #16
Source File: resnext.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_resnext():
    net = ResNeXt29_2x64d()
    x = torch.randn(1,3,32,32)
    y = net(Variable(x))
    print(y.size())

# test_resnext() 
Example #17
Source File: resnet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = ResNet18()
    y = net(Variable(torch.randn(1,3,32,32)))
    print(y.size())

# test() 
Example #18
Source File: shufflenet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = ShuffleNetG2()
    x = Variable(torch.randn(1,3,32,32))
    y = net(x)
    print(y)

# test() 
Example #19
Source File: util.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def t2var(t,cuda):
    if cuda:
        t = t.cuda()
    t = Variable(t, volatile=True)
    return t 
Example #20
Source File: densenet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_densenet():
    net = densenet_cifar()
    x = torch.randn(1,3,32,32)
    y = net(Variable(x))
    print(y)

# test_densenet() 
Example #21
Source File: resnet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = ResNet18()
    y = net(Variable(torch.randn(1,3,32,32)))
    print(y.size())

# test() 
Example #22
Source File: preact_resnet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = PreActResNet18()
    y = net(Variable(torch.randn(1,3,32,32)))
    print(y.size())

# test() 
Example #23
Source File: senet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = SENet18()
    y = net(Variable(torch.randn(1,3,32,32)))
    print(y.size())

# test() 
Example #24
Source File: dpn.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = DPN92()
    x = Variable(torch.randn(1,3,32,32))
    y = net(x)
    print(y)

# test() 
Example #25
Source File: pnasnet.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test():
    net = PNASNetB()
    print(net)
    x = Variable(torch.randn(1,3,32,32))
    y = net(x)
    print(y)

# test() 
Example #26
Source File: resnext.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_resnext():
    net = ResNeXt29_2x64d()
    x = torch.randn(1,3,32,32)
    y = net(Variable(x))
    print(y.size())

# test_resnext() 
Example #27
Source File: pose_rnn.py    From DDPAE-video-prediction with MIT License 5 votes vote down vote up
def predict(self, encoder_outputs, hidden_states):
    '''
    Second part of the model.
    input: encoder outputs and hidden_states of each component.
    Return predicted betas.
    '''
    batch_size = encoder_outputs.size(0)
    pred_beta_mu, pred_beta_sigma = None, None
    pred_outputs = []
    prev_hidden = [Variable(torch.zeros(batch_size, 1, self.hidden_size).cuda())] \
                       * self.n_frames_output
    for i in range(self.n_components):
      hidden = hidden_states[i]
      prev_outputs = encoder_outputs[:, -1:, i, :]
      frame_outputs = []
      # Manual unroll
      for j in range(self.n_frames_output):
        if self.independent_components:
          rnn_input = prev_outputs
        else:
          rnn_input = torch.cat([prev_outputs, prev_hidden[j]], dim=2)
        output, hidden = self.predict_rnn(rnn_input, hidden)
        prev_outputs = output
        prev_hidden[j] = hidden[0].view(batch_size, 1, -1)
        frame_outputs.append(output)
      # frame_outputs: batch_size x n_frames_output x (hidden_size * 2)
      frame_outputs = torch.cat(frame_outputs, dim=1)
      pred_outputs.append(frame_outputs)

    # batch_size x n_frames_output x n_components x hidden_size
    pred_outputs = torch.stack(pred_outputs, dim=2)
    pred_beta_mu = self.beta_mu_layer(pred_outputs).view(-1, self.output_size)
    pred_beta_sigma = self.beta_sigma_layer(pred_outputs).view(-1, self.output_size)
    pred_beta_sigma = F.softplus(pred_beta_sigma)
    return pred_beta_mu, pred_beta_sigma 
Example #28
Source File: DDPAE.py    From DDPAE-video-prediction with MIT License 5 votes vote down vote up
def model(self, input, output):
    '''
    Likelihood model: sample from prior, then decode to video.
    param input: video of size (batch_size, self.n_frames_input, C, H, W)
    param output: video of size (batch_size, self.n_frames_output, C, H, W)
    '''
    # Register networks
    for name, net in self.model_modules.items():
      pyro.module(name, net)

    observation = torch.cat([input, output], dim=1)

    # Sample from prior
    latent = self.sample_latent_prior(input)
    # Decode
    decoded_output, components = self.decode(latent, input.size(0))
    decoded_output = decoded_output.view(*observation.size())
    if self.predict_loss_only:
      # Only consider loss from the predicted frames
      decoded_output = decoded_output[:, self.n_frames_input:]
      observation = observation[:, self.n_frames_input:]
      components = components[:, self.n_frames_input:, ...]

    # pyro observe
    sd = Variable(0.3 * torch.ones(*decoded_output.size()).cuda())
    pyro.sample('obs', dist.Normal(decoded_output, sd), obs=observation) 
Example #29
Source File: DDPAE_utils.py    From DDPAE-video-prediction with MIT License 5 votes vote down vote up
def pose_inv(pose):
  '''
  param pose: N x 3
  [s,x,y] -> [1/s,-x/s,-y/s]
  '''
  N, _ = pose.size()
  ones = Variable(torch.ones(N, 1).cuda(), requires_grad=False)
  out = torch.cat([ones, -pose[:, 1:]], dim=1)
  out = out / pose[:, 0:1]
  return out 
Example #30
Source File: predict_video.py    From cat-bbs with MIT License 5 votes vote down vote up
def find_bbs(img, model, conf_threshold, input_size):
    """Find bounding boxes in an image."""
    # pad image so that its square
    img_pad, (pad_top, pad_right, pad_bottom, pad_left) = to_aspect_ratio_add(img, 1.0, return_paddings=True)

    # resize padded image to desired input size
    # "linear" interpolation seems to be enough here for 400x400 or larger images
    # change to "area" or "cubic" for marginally better quality
    img_rs = ia.imresize_single_image(img_pad, (input_size, input_size), interpolation="linear")

    # convert to torch-ready input variable
    inputs_np = (np.array([img_rs])/255.0).astype(np.float32).transpose(0, 3, 1, 2)
    inputs = torch.from_numpy(inputs_np)
    inputs = Variable(inputs, volatile=True)
    if GPU >= 0:
        inputs = inputs.cuda(GPU)

    # apply model and measure the model's time
    time_start = time.time()
    outputs_pred = model(inputs)
    time_req = time.time() - time_start

    # process the model's output (i.e. convert heatmaps to BBs)
    result = ModelResult(
        outputs_pred,
        inputs_np,
        img,
        (pad_top, pad_right, pad_bottom, pad_left)
    )
    bbs = result.get_bbs()

    return bbs, time_req