示例#1
0
def accumulate_inception_activations(sample, net, num_inception_images=50000):
  pool, logits, labels = [], [], []
  while (torch.cat(logits, 0).shape[0] if len(logits) else 0) < num_inception_images:
    with torch.no_grad():
      images, labels_val = sample()
      pool_val, logits_val = net(images.astype("float32"))
      pool += [pool_val]
      logits += [F.softmax(logits_val, 1)]
      labels += [labels_val]
  return torch.cat(pool, 0), torch.cat(logits, 0), torch.cat(labels, 0)
示例#2
0
 def forward(self, x, y=None):
     # Apply convs
     theta = self.theta(x)
     phi = F.max_pool2d(self.phi(x), [2, 2])
     g = F.max_pool2d(self.g(x), [2, 2])
     # Perform reshapes
     theta = theta.view(-1, self.ch // 8, x.shape[2] * x.shape[3])
     phi = phi.view(-1, self.ch // 8, x.shape[2] * x.shape[3] // 4)
     g = g.view(-1, self.ch // 2, x.shape[2] * x.shape[3] // 4)
     # Matmul and softmax to get attention maps
     beta = F.softmax(torch.bmm(theta.transpose(1, 2), phi), -1)
     # Attention map times g path
     o = self.o(
         torch.bmm(g, beta.transpose(1, 2)).view(-1, self.ch // 2,
                                                 x.shape[2], x.shape[3]))
     return self.gamma * o + x
def run(config):
    # Get loader
    config['drop_last'] = False
    loaders = utils.get_data_loaders(**config)

    # Load inception net
    net = inception_utils.load_inception_net(parallel=config['parallel'])
    pool, logits, labels = [], [], []
    device = 'cuda'
    for i, (x, y) in enumerate(tqdm(loaders[0])):
        x = x.to(device)
        with torch.no_grad():
            pool_val, logits_val = net(x)
            pool += [np.asarray(pool_val)]
            logits += [np.asarray(F.softmax(logits_val, 1))]
            labels += [np.asarray(y)]

    pool, logits, labels = [
        np.concatenate(item, 0) for item in [pool, logits, labels]
    ]
    # uncomment to save pool, logits, and labels to disk
    # print('Saving pool, logits, and labels to disk...')
    # np.savez(config['dataset']+'_inception_activations.npz',
    #           {'pool': pool, 'logits': logits, 'labels': labels})
    # Calculate inception metrics and report them
    print('Calculating inception metrics...')
    IS_mean, IS_std = inception_utils.calculate_inception_score(logits)
    print('Training data from dataset %s has IS of %5.5f +/- %5.5f' %
          (config['dataset'], IS_mean, IS_std))
    # Prepare mu and sigma, save to disk. Remove "hdf5" by default
    # (the FID code also knows to strip "hdf5")
    print('Calculating means and covariances...')
    mu, sigma = np.mean(pool, axis=0), np.cov(pool, rowvar=False)
    print('Saving calculated means and covariances to disk...')
    np.savez(config['dataset'].strip('_hdf5') + '_inception_moments.npz', **{
        'mu': mu,
        'sigma': sigma
    })