本文整理汇总了Python中misc.resnet方法的典型用法代码示例。如果您正苦于以下问题:Python misc.resnet方法的具体用法?Python misc.resnet怎么用?Python misc.resnet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类misc
的用法示例。
在下文中一共展示了misc.resnet方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import misc [as 别名]
# 或者: from misc import resnet [as 别名]
def main(params):
net = getattr(resnet, params['model'])()
net.load_state_dict(torch.load(os.path.join(params['model_root'],params['model']+'.pth')))
my_resnet = myResnet(net)
my_resnet.cuda()
my_resnet.eval()
imgs = json.load(open(params['input_json'], 'r'))
imgs = imgs['images']
N = len(imgs)
seed(123) # make reproducible
dir_fc = params['output_dir']+'_fc'
dir_att = params['output_dir']+'_att'
if not os.path.isdir(dir_fc):
os.mkdir(dir_fc)
if not os.path.isdir(dir_att):
os.mkdir(dir_att)
for i,img in enumerate(imgs):
# load the image
I = skimage.io.imread(os.path.join(params['images_root'], img['filepath'], img['filename']))
# handle grayscale input images
if len(I.shape) == 2:
I = I[:,:,np.newaxis]
I = np.concatenate((I,I,I), axis=2)
I = I.astype('float32')/255.0
I = torch.from_numpy(I.transpose([2,0,1])).cuda()
I = preprocess(I)
with torch.no_grad():
tmp_fc, tmp_att = my_resnet(I, params['att_size'])
# write to pkl
np.save(os.path.join(dir_fc, str(img['cocoid'])), tmp_fc.data.cpu().float().numpy())
np.savez_compressed(os.path.join(dir_att, str(img['cocoid'])), feat=tmp_att.data.cpu().float().numpy())
if i % 1000 == 0:
print('processing %d/%d (%.2f%% done)' % (i, N, i*100.0/N))
print('wrote ', params['output_dir'])
示例2: main
# 需要导入模块: import misc [as 别名]
# 或者: from misc import resnet [as 别名]
def main(params):
net = getattr(resnet, params['model'])()
net.load_state_dict(torch.load(os.path.join(params['model_root'],params['model']+'.pth')))
my_resnet = myResnet(net)
my_resnet.cuda()
my_resnet.eval()
imgs = json.load(open(params['input_json'], 'r'))
imgs = imgs['images']
N = len(imgs)
seed(123) # make reproducible
dir_fc = params['output_dir']+'_fc'
dir_att = params['output_dir']+'_att'
if not os.path.isdir(dir_fc):
os.mkdir(dir_fc)
if not os.path.isdir(dir_att):
os.mkdir(dir_att)
for i,img in enumerate(imgs):
# load the image
I = skimage.io.imread(os.path.join(params['images_root'], img['filepath'], img['filename']))
# handle grayscale input images
if len(I.shape) == 2:
I = I[:,:,np.newaxis]
I = np.concatenate((I,I,I), axis=2)
I = I.astype('float32')/255.0
I = torch.from_numpy(I.transpose([2,0,1])).cuda()
I = Variable(preprocess(I), volatile=True)
tmp_fc, tmp_att = my_resnet(I, params['att_size'])
# write to pkl
np.save(os.path.join(dir_fc, str(img['cocoid'])), tmp_fc.data.cpu().float().numpy())
np.savez_compressed(os.path.join(dir_att, str(img['cocoid'])), feat=tmp_att.data.cpu().float().numpy())
if i % 1000 == 0:
print('processing %d/%d (%.2f%% done)' % (i, N, i*100.0/N))
print('wrote ', params['output_dir'])
示例3: main
# 需要导入模块: import misc [as 别名]
# 或者: from misc import resnet [as 别名]
def main(params):
assert params['feature_type'] in ['fc', 'conv', 'both']
compute_fc = params['feature_type'] == 'fc' or params['feature_type'] == 'both'
compute_conv = params['feature_type'] == 'conv' or params['feature_type'] == 'both'
net = getattr(resnet, params['model'])()
net.load_state_dict(torch.load(os.path.join(params['model_root'], params['model'] + '.pth')))
my_resnet = myResnet(net)
my_resnet.cuda()
my_resnet.eval()
if compute_fc:
dir_fc = os.path.join(params['out_dir'], 'fc')
if not os.path.exists(dir_fc):
os.makedirs(dir_fc)
if compute_conv:
dir_conv = os.path.join(params['out_dir'], 'conv')
if not os.path.exists(dir_conv):
os.makedirs(dir_conv)
for split in ['train', 'val', 'test']:
count = 0
if compute_fc and not os.path.exists(os.path.join(dir_fc, split)):
os.makedirs(os.path.join(dir_fc, split))
if compute_conv and not os.path.exists(os.path.join(dir_conv, split)):
os.makedirs(os.path.join(dir_conv, split))
files = glob.glob("{}/{}/*.jpg".format(params['img_dir'], split))
start = time.time()
for file in files:
count += 1
basename = os.path.basename(file)
img_id = splitext(basename)[0]
try:
I = imread(file)
except:
I = np.zeros((224, 224, 3), 'float32')
# handle grayscale input frames
if len(I.shape) == 2:
I = I[:, :, np.newaxis]
I = np.concatenate((I, I, I), axis=2)
I = I.astype('float32') / 255.0
I = torch.from_numpy(I.transpose([2, 0, 1])).cuda()
I = Variable(preprocess(I), volatile=True)
tmp_fc, tmp_conv = my_resnet(I, params['att_size'])
# write to pkl
if compute_fc:
np.save(os.path.join(dir_fc, split, img_id), tmp_fc.data.cpu().float().numpy())
if compute_conv:
np.savez_compressed(os.path.join(dir_conv, split, img_id), tmp_conv.data.cpu().float().numpy())
if count % 100 == 0:
print('processing {} set -- {}/{} {:.3}%, time used: {}s'.format(split, count, len(files),
count * 100.0 / len(files),
time.time() - start))
start = time.time()