當前位置: 首頁>>代碼示例>>Python>>正文


Python misc.resnet方法代碼示例

本文整理匯總了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']) 
開發者ID:ltguo19,項目名稱:VSUA-Captioning,代碼行數:42,代碼來源:prepro_feats.py

示例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']) 
開發者ID:tgGuo15,項目名稱:PriorImageCaption,代碼行數:41,代碼來源:prepro_feats.py

示例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() 
開發者ID:eric-xw,項目名稱:AREL,代碼行數:61,代碼來源:extract_features.py


注:本文中的misc.resnet方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。