当前位置: 首页>>代码示例>>Python>>正文


Python model.NetworkImageNet方法代码示例

本文整理汇总了Python中model.NetworkImageNet方法的典型用法代码示例。如果您正苦于以下问题:Python model.NetworkImageNet方法的具体用法?Python model.NetworkImageNet怎么用?Python model.NetworkImageNet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在model的用法示例。


在下文中一共展示了model.NetworkImageNet方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: write_pytorch_weight

# 需要导入模块: import model [as 别名]
# 或者: from model import NetworkImageNet [as 别名]
def write_pytorch_weight(self):
    model = NetworkImageNet(216, 1001, 12, False, PNASNet)
    model.drop_path_prob = 0
    model.eval()

    self.used_keys = []
    self.convert_conv(model.conv0, 'conv0/weights')
    self.convert_bn(model.conv0_bn, 'conv0_bn/gamma', 'conv0_bn/beta',
        'conv0_bn/moving_mean', 'conv0_bn/moving_variance')
    self.convert_cell(model.stem1, 'cell_stem_0/')
    self.convert_cell(model.stem2, 'cell_stem_1/')

    for i in range(12):
      self.convert_cell(model.cells[i], 'cell_{}/'.format(i))
    
    self.convert_fc(model.classifier, 'final_layer/FC/weights',
        'final_layer/FC/biases')

    print('Conversion complete!')
    print('Check 1: whether all TF variables are used...')
    assert len(self.weight_dict) == len(self.used_keys)
    print('Pass!')

    model = model.cuda()
    image = self.tf_image_proc.transpose((2, 0, 1))
    image = Variable(self.Tensor(image)).cuda()
    logits, _ = model(image.unsqueeze(0))
    self.pytorch_logits = logits.data.cpu().numpy()

    print('Check 2: whether logits have small diff...')
    assert np.max(np.abs(self.tf_logits - self.pytorch_logits)) < 1e-5
    print('Pass!')

    model_path = 'data/PNASNet-5_Large.pth'
    torch.save(model.state_dict(), model_path)
    print('PyTorch model saved to {}'.format(model_path)) 
开发者ID:chenxi116,项目名称:PNASNet.pytorch,代码行数:38,代码来源:convert.py

示例2: main

# 需要导入模块: import model [as 别名]
# 或者: from model import NetworkImageNet [as 别名]
def main():
  args = parser.parse_args()
  assert torch.cuda.is_available()

  image_ph = tf.placeholder(tf.uint8, (None, None, 3))
  image_proc = preprocess_for_eval(image_ph, args.image_size, args.image_size)
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = True
  sess = tf.Session(config=config)

  model = NetworkImageNet(args.num_conv_filters, args.num_classes,
                          args.num_cells, False, PNASNet)
  model.drop_path_prob = 0
  model.eval()
  model.load_state_dict(torch.load('data/PNASNet-5_Large.pth'))
  model = model.cuda()

  c1, c5 = 0, 0
  val_dataset = datasets.ImageFolder(args.valdir)
  for i, (image, label) in enumerate(val_dataset):
    tf_image_proc = sess.run(image_proc, feed_dict={image_ph: image})
    image = torch.from_numpy(tf_image_proc.transpose((2, 0, 1)))
    image = Variable(image).cuda()
    logits, _ = model(image.unsqueeze(0))
    top5 = logits.data.cpu().numpy().squeeze().argsort()[::-1][:5]
    top1 = top5[0]
    if label + 1 == top1:
      c1 += 1
    if label + 1 in top5:
      c5 += 1
    print('Test: [{0}/{1}]\t'
          'Prec@1 {2:.3f}\t'
          'Prec@5 {3:.3f}\t'.format(
          i + 1, len(val_dataset), c1 / (i + 1.), c5 / (i + 1.))) 
开发者ID:chenxi116,项目名称:PNASNet.pytorch,代码行数:36,代码来源:main.py

示例3: main

# 需要导入模块: import model [as 别名]
# 或者: from model import NetworkImageNet [as 别名]
def main():
  if not torch.cuda.is_available():
    logging.info('no gpu device available')
    sys.exit(1)
  cudnn.enabled=True
  logging.info("args = %s", args)

  genotype = eval("genotypes.%s" % args.arch)
  model = Network(args.init_channels, CLASSES, args.layers, args.auxiliary, genotype)
  model = nn.DataParallel(model)
  model = model.cuda()
  model.load_state_dict(torch.load(args.model_path)['state_dict'])

  logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

  criterion = nn.CrossEntropyLoss()
  criterion = criterion.cuda()

  validdir = os.path.join(args.data, 'val')
  normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  valid_data = dset.ImageFolder(
    validdir,
    transforms.Compose([
      transforms.Resize(256),
      transforms.CenterCrop(224),
      transforms.ToTensor(),
      normalize,
    ]))

  valid_queue = torch.utils.data.DataLoader(
    valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=False, num_workers=4)

  model.module.drop_path_prob = 0.0
  valid_acc_top1, valid_acc_top5, valid_obj = infer(valid_queue, model, criterion)
  logging.info('Valid_acc_top1 %f', valid_acc_top1)
  logging.info('Valid_acc_top5 %f', valid_acc_top5) 
开发者ID:lightaime,项目名称:sgas,代码行数:38,代码来源:test_imagenet.py

示例4: main

# 需要导入模块: import model [as 别名]
# 或者: from model import NetworkImageNet [as 别名]
def main():
  if not torch.cuda.is_available():
    logging.info('no gpu device available')
    sys.exit(1)

  np.random.seed(args.seed)
  torch.cuda.set_device(args.gpu)
  cudnn.benchmark = True
  torch.manual_seed(args.seed)
  cudnn.enabled=True
  torch.cuda.manual_seed(args.seed)
  logging.info('gpu device = %d' % args.gpu)
  logging.info("args = %s", args)

  genotype = eval("genotypes.%s" % args.arch)
  model = Network(args.init_channels, CLASSES, args.layers, args.auxiliary, genotype)
  model = model.cuda()
  model.load_state_dict(torch.load(args.model_path)['state_dict'])

  logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

  criterion = nn.CrossEntropyLoss()
  criterion = criterion.cuda()

  validdir = os.path.join(args.data, 'val')
  normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  valid_data = dset.ImageFolder(
    validdir,
    transforms.Compose([
      transforms.Resize(256),
      transforms.CenterCrop(224),
      transforms.ToTensor(),
      normalize,
    ]))

  valid_queue = torch.utils.data.DataLoader(
    valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=4)

  model.drop_path_prob = args.drop_path_prob
  valid_acc_top1, valid_acc_top5, valid_obj = infer(valid_queue, model, criterion)
  logging.info('valid_acc_top1 %f', valid_acc_top1)
  logging.info('valid_acc_top5 %f', valid_acc_top5) 
开发者ID:kcyu2014,项目名称:eval-nas,代码行数:44,代码来源:test_imagenet.py

示例5: main

# 需要导入模块: import model [as 别名]
# 或者: from model import NetworkImageNet [as 别名]
def main():
  if not torch.cuda.is_available():
    logging.info('no gpu device available')
    sys.exit(1)

  torch.cuda.set_device(args.gpu)
  cudnn.enabled=True
  logging.info("args = %s", args)

  genotype = eval("genotypes.%s" % args.arch)
  if args.dataset in LARGE_DATASETS:
    model = NetworkLarge(args.init_channels, CLASSES, args.layers, args.auxiliary, genotype)
  else:
    model = Network(args.init_channels, CLASSES, args.layers, args.auxiliary, genotype)
  model = model.cuda()
  utils.load(model, args.model_path)

  logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

  criterion = nn.CrossEntropyLoss()
  criterion = criterion.cuda()

  _, test_transform = utils.data_transforms(args.dataset,args.cutout,args.cutout_length)
  if args.dataset=="CIFAR100":
    test_data = dset.CIFAR100(root=args.data, train=False, download=True, transform=test_transform)
  elif args.dataset=="CIFAR10":
    test_data = dset.CIFAR10(root=args.data, train=False, download=True, transform=test_transform)
  elif args.dataset=="sport8":
    dset_cls = dset.ImageFolder
    val_path = '%s/Sport8/test' %args.data
    test_data = dset_cls(root=val_path, transform=test_transform)
  elif args.dataset=="mit67":
    dset_cls = dset.ImageFolder
    val_path = '%s/MIT67/test' %args.data
    test_data = dset_cls(root=val_path, transform=test_transform)
  elif args.dataset == "flowers102":
    dset_cls = dset.ImageFolder
    val_path = '%s/flowers102/test' % args.tmp_data_dir
    test_data = dset_cls(root=val_path, transform=test_transform)
  test_queue = torch.utils.data.DataLoader(
      test_data, batch_size=args.batch_size, shuffle=False, pin_memory=False, num_workers=2)

  model.drop_path_prob = 0.0
  test_acc, test_obj = infer(test_queue, model, criterion)
  logging.info('Test_acc %f', test_acc) 
开发者ID:antoyang,项目名称:NAS-Benchmark,代码行数:47,代码来源:test.py


注:本文中的model.NetworkImageNet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。