本文整理匯總了Python中torch.backends.cudnn.benchmark方法的典型用法代碼示例。如果您正苦於以下問題:Python cudnn.benchmark方法的具體用法?Python cudnn.benchmark怎麽用?Python cudnn.benchmark使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.backends.cudnn
的用法示例。
在下文中一共展示了cudnn.benchmark方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def run(self):
self.build_model()
self.resume_and_evaluate()
cudnn.benchmark = True
for self.epoch in range(self.start_epoch, self.nb_epochs):
self.train_1epoch()
prec1, val_loss = self.validate_1epoch()
is_best = prec1 > self.best_prec1
#lr_scheduler
self.scheduler.step(val_loss)
# save model
if is_best:
self.best_prec1 = prec1
with open('record/spatial/spatial_video_preds.pickle','wb') as f:
pickle.dump(self.dic_video_level_preds,f)
f.close()
save_checkpoint({
'epoch': self.epoch,
'state_dict': self.model.state_dict(),
'best_prec1': self.best_prec1,
'optimizer' : self.optimizer.state_dict()
},is_best,'record/spatial/checkpoint.pth.tar','record/spatial/model_best.pth.tar')
示例2: main_inference
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def main_inference():
print("Loading config...")
opt = TestOptions().parse()
print("Loading dataset...")
dset = TVQADataset(opt, mode=opt.mode)
print("Loading model...")
model = STAGE(opt)
model.to(opt.device)
cudnn.benchmark = True
strict_mode = not opt.no_strict
model_path = os.path.join("results", opt.model_dir, "best_valid.pth")
model.load_state_dict(torch.load(model_path), strict=strict_mode)
model.eval()
model.inference_mode = True
torch.set_grad_enabled(False)
print("Evaluation Starts:\n")
predictions = inference(opt, dset, model)
print("predictions {}".format(predictions.keys()))
pred_path = model_path.replace("best_valid.pth",
"{}_inference_predictions.json".format(opt.mode))
save_json(predictions, pred_path)
示例3: test_voc
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def test_voc():
# load net
num_classes = len(VOC_CLASSES) + 1 # +1 background
net = build_ssd('test', 300, num_classes) # initialize SSD
net.load_state_dict(torch.load(args.trained_model))
net.eval()
print('Finished loading model!')
# load data
testset = VOCDetection(args.voc_root, [('2007', 'test')], None, VOCAnnotationTransform())
if args.cuda:
net = net.cuda()
cudnn.benchmark = True
# evaluation
test_net(args.save_folder, net, args.cuda, testset,
BaseTransform(net.size, (104, 117, 123)),
thresh=args.visual_threshold)
示例4: extract_features
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def extract_features(model, data_loader, print_freq=1, metric=None):
cudnn.benchmark = False
model.eval()
batch_time = AverageMeter()
data_time = AverageMeter()
features = OrderedDict()
labels = OrderedDict()
fcs = OrderedDict()
print("Begin to extract features...")
for i, (imgs, fnames, pids, _, _) in enumerate(data_loader):
_fcs, pool5s = extract_cnn_feature(model, imgs)
for fname, fc, pool5, pid in zip(fnames, _fcs, pool5s, pids):
features[fname] = pool5
fcs[fname] = fc
labels[fname] = pid
cudnn.benchmark = True
return features, labels, fcs # 2048 pool5 feature, labels, 1024 fc layers
示例5: set_gpu
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def set_gpu(args, model):
assert torch.cuda.is_available(), "CPU-only experiments currently unsupported"
if args.gpu is not None:
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
elif args.multigpu is None:
device = torch.device("cpu")
else:
# DataParallel will divide and allocate batch_size to all available GPUs
print(f"=> Parallelizing on {args.multigpu} gpus")
torch.cuda.set_device(args.multigpu[0])
args.gpu = args.multigpu[0]
model = torch.nn.DataParallel(model, device_ids=args.multigpu).cuda(
args.multigpu[0]
)
cudnn.benchmark = True
return model
示例6: main
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def main():
cudnn.benchmark = False
test_video=Test_video(short_side=[224,256])
model = slowfastnet.resnet50(class_num=Config.CLASS_NUM)
assert Config.LOAD_MODEL_PATH is not None
print("load model from:", Config.LOAD_MODEL_PATH)
pretrained_dict = torch.load(Config.LOAD_MODEL_PATH, map_location='cpu')
try:
model_dict = model.module.state_dict()
except AttributeError:
model_dict = model.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
model = model.cuda(params['gpu'][0])
validation(model, test_video)
示例7: __init__
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def __init__(self, output_dir):
if cfg.TRAIN.FLAG:
self.model_dir = os.path.join(output_dir, 'Model')
self.image_dir = os.path.join(output_dir, 'Image')
self.log_dir = os.path.join(output_dir, 'Log')
mkdir_p(self.model_dir)
mkdir_p(self.image_dir)
mkdir_p(self.log_dir)
self.summary_writer = FileWriter(self.log_dir)
self.max_epoch = cfg.TRAIN.MAX_EPOCH
self.snapshot_interval = cfg.TRAIN.SNAPSHOT_INTERVAL
s_gpus = cfg.GPU_ID.split(',')
self.gpus = [int(ix) for ix in s_gpus]
self.num_gpus = len(self.gpus)
self.batch_size = cfg.TRAIN.BATCH_SIZE * self.num_gpus
torch.cuda.set_device(self.gpus[0])
cudnn.benchmark = True
# ############# For training stageI GAN #############
示例8: __init__
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def __init__(self, params, dist_model=False):
model_params = params['module']
self.model = models.modules.__dict__[params['module']['arch']](model_params)
utils.init_weights(self.model, init_type='xavier')
self.model.cuda()
if dist_model:
self.model = utils.DistModule(self.model)
self.world_size = dist.get_world_size()
else:
self.model = models.modules.FixModule(self.model)
self.world_size = 1
if params['optim'] == 'SGD':
self.optim = torch.optim.SGD(
self.model.parameters(), lr=params['lr'],
momentum=0.9, weight_decay=0.0001)
elif params['optim'] == 'Adam':
self.optim = torch.optim.Adam(
self.model.parameters(), lr=params['lr'],
betas=(params['beta1'], 0.999))
else:
raise Exception("No such optimizer: {}".format(params['optim']))
cudnn.benchmark = True
示例9: train
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def train(self):
# cudnn.benchmark = True
# self.__val()
if self.configer.get('network', 'resume') is not None:
if self.configer.get('network', 'resume_val'):
self.__val(data_loader=self.data_loader.get_valloader(dataset='val'))
return
elif self.configer.get('network', 'resume_train'):
self.__val(data_loader=self.data_loader.get_valloader(dataset='train'))
return
# return
if self.configer.get('network', 'resume') is not None and self.configer.get('network', 'resume_val'):
self.__val(data_loader=self.data_loader.get_valloader(dataset='val'))
return
while self.configer.get('iters') < self.configer.get('solver', 'max_iters'):
self.__train()
# use swa to average the model
if 'swa' in self.configer.get('lr', 'lr_policy'):
self.optimizer.swap_swa_sgd()
self.optimizer.bn_update(self.train_loader, self.seg_net)
self.__val(data_loader=self.data_loader.get_valloader(dataset='val'))
示例10: main
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def main():
testset = DeployDataset(
image_root=cfg.img_root,
transform=BaseTransform(size=cfg.input_size, mean=cfg.means, std=cfg.stds)
)
test_loader = data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=cfg.num_workers)
# Model
model = TextNet(is_training=False, backbone=cfg.net)
model_path = os.path.join(cfg.save_dir, cfg.exp_name, \
'textsnake_{}_{}.pth'.format(model.backbone_name, cfg.checkepoch))
model.load_model(model_path)
# copy to cuda
model = model.to(cfg.device)
if cfg.cuda:
cudnn.benchmark = True
detector = TextDetector(model, tr_thresh=cfg.tr_thresh, tcl_thresh=cfg.tcl_thresh)
print('Start testing TextSnake.')
output_dir = os.path.join(cfg.output_dir, cfg.exp_name)
inference(detector, test_loader, output_dir)
示例11: initialize
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def initialize(self, fixed=None):
# Parse options
self.args = self.parse(fixed)
# Setting default torch Tensor type
if self.args.cuda and torch.cuda.is_available():
torch.set_default_tensor_type('torch.cuda.FloatTensor')
cudnn.benchmark = True
else:
torch.set_default_tensor_type('torch.FloatTensor')
# Create weights saving directory
if not os.path.exists(self.args.save_dir):
os.mkdir(self.args.save_dir)
# Create weights saving directory of target model
model_save_path = os.path.join(self.args.save_dir, self.args.exp_name)
if not os.path.exists(model_save_path):
os.mkdir(model_save_path)
return self.args
示例12: tes_net
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def tes_net():
# enable cudnn
cudnn.benchmark = True
# get the DataLoader
data_loaders = get_loader(dataset_config, config, mode="test")
#get the solver
if args.model == 'cycleGAN':
solver = Solver_cycleGAN(data_loaders, config, dataset_config)
elif args.model =='makeupGAN':
solver = Solver_makeupGAN(data_loaders, config, dataset_config)
else:
print("model that not support")
exit()
solver.test()
示例13: main
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def main(config):
svhn_loader, mnist_loader = get_loader(config)
solver = Solver(config, svhn_loader, mnist_loader)
cudnn.benchmark = True
# create directories if not exist
if not os.path.exists(config.model_path):
os.makedirs(config.model_path)
if not os.path.exists(config.sample_path):
os.makedirs(config.sample_path)
if config.mode == 'train':
solver.train()
elif config.mode == 'sample':
solver.sample()
示例14: __init__
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def __init__(self, output_dir, data_loader, n_words, ixtoword, dataset):
if cfg.TRAIN.FLAG:
self.model_dir = os.path.join(output_dir, 'Model')
self.image_dir = os.path.join(output_dir, 'Image')
mkdir_p(self.model_dir)
mkdir_p(self.image_dir)
#torch.cuda.set_device(cfg.GPU_ID)
#cudnn.benchmark = True
self.batch_size = cfg.TRAIN.BATCH_SIZE
self.max_epoch = cfg.TRAIN.MAX_EPOCH
self.snapshot_interval = cfg.TRAIN.SNAPSHOT_INTERVAL
self.n_words = n_words
self.ixtoword = ixtoword
self.data_loader = data_loader
self.dataset = dataset
self.num_batches = len(self.data_loader)
示例15: get_model
# 需要導入模塊: from torch.backends import cudnn [as 別名]
# 或者: from torch.backends.cudnn import benchmark [as 別名]
def get_model(conf, num_class=10, data_parallel=True):
name = conf['type']
if name == 'wresnet40_2':
model = WideResNet(40, 2, dropout_rate=0.0, num_classes=num_class)
elif name == 'wresnet28_2':
model = WideResNet(28, 2, dropout_rate=0.0, num_classes=num_class)
elif name == 'wresnet28_10':
model = WideResNet(28, 10, dropout_rate=0.0, num_classes=num_class)
else:
raise NameError('no model named, %s' % name)
if data_parallel:
model = model.cuda()
model = DataParallel(model)
else:
import horovod.torch as hvd
device = torch.device('cuda', hvd.local_rank())
model = model.to(device)
cudnn.benchmark = True
return model