最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch實現(xiàn)用Resnet提取特征并保存為txt文件的方法

 更新時間:2019年08月20日 09:49:38   作者:qq_32464407  
今天小編大家分享一篇pytorch實現(xiàn)用Resnet提取特征并保存為txt文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

接觸pytorch一天,發(fā)現(xiàn)pytorch上手的確比TensorFlow更快??梢愿奖愕貙崿F(xiàn)用預(yù)訓(xùn)練的網(wǎng)絡(luò)提特征。

以下是提取一張jpg圖像的特征的程序:

# -*- coding: utf-8 -*-
 
import os.path
 
import torch
import torch.nn as nn
from torchvision import models, transforms
from torch.autograd import Variable 
 
import numpy as np
from PIL import Image 
 
features_dir = './features'
 
img_path = "hymenoptera_data/train/ants/0013035.jpg"
file_name = img_path.split('/')[-1]
feature_path = os.path.join(features_dir, file_name + '.txt')
 
 
transform1 = transforms.Compose([
    transforms.Scale(256),
    transforms.CenterCrop(224),
    transforms.ToTensor()  ]
)
 
img = Image.open(img_path)
img1 = transform1(img)
 
#resnet18 = models.resnet18(pretrained = True)
resnet50_feature_extractor = models.resnet50(pretrained = True)
resnet50_feature_extractor.fc = nn.Linear(2048, 2048)
torch.nn.init.eye(resnet50_feature_extractor.fc.weight)
 
for param in resnet50_feature_extractor.parameters():
  param.requires_grad = False
#resnet152 = models.resnet152(pretrained = True)
#densenet201 = models.densenet201(pretrained = True) 
x = Variable(torch.unsqueeze(img1, dim=0).float(), requires_grad=False)
#y1 = resnet18(x)
y = resnet50_feature_extractor(x)
y = y.data.numpy()
np.savetxt(feature_path, y, delimiter=',')
#y3 = resnet152(x)
#y4 = densenet201(x)
 
y_ = np.loadtxt(feature_path, delimiter=',').reshape(1, 2048)

以下是提取一個文件夾下所有jpg、jpeg圖像的程序:

# -*- coding: utf-8 -*-
import os, torch, glob
import numpy as np
from torch.autograd import Variable
from PIL import Image 
from torchvision import models, transforms
import torch.nn as nn
import shutil
data_dir = './hymenoptera_data'
features_dir = './features'
shutil.copytree(data_dir, os.path.join(features_dir, data_dir[2:]))
 
 
def extractor(img_path, saved_path, net, use_gpu):
  transform = transforms.Compose([
      transforms.Scale(256),
      transforms.CenterCrop(224),
      transforms.ToTensor()  ]
  )
  
  img = Image.open(img_path)
  img = transform(img)
  
 
 
  x = Variable(torch.unsqueeze(img, dim=0).float(), requires_grad=False)
  if use_gpu:
    x = x.cuda()
    net = net.cuda()
  y = net(x).cpu()
  y = y.data.numpy()
  np.savetxt(saved_path, y, delimiter=',')
  
if __name__ == '__main__':
  extensions = ['jpg', 'jpeg', 'JPG', 'JPEG']
    
  files_list = []
  sub_dirs = [x[0] for x in os.walk(data_dir) ]
  sub_dirs = sub_dirs[1:]
  for sub_dir in sub_dirs:
    for extention in extensions:
      file_glob = os.path.join(sub_dir, '*.' + extention)
      files_list.extend(glob.glob(file_glob))
    
  resnet50_feature_extractor = models.resnet50(pretrained = True)
  resnet50_feature_extractor.fc = nn.Linear(2048, 2048)
  torch.nn.init.eye(resnet50_feature_extractor.fc.weight)
  for param in resnet50_feature_extractor.parameters():
    param.requires_grad = False  
    
  use_gpu = torch.cuda.is_available()
 
  for x_path in files_list:
    print(x_path)
    fx_path = os.path.join(features_dir, x_path[2:] + '.txt')
    extractor(x_path, fx_path, resnet50_feature_extractor, use_gpu)

另外最近發(fā)現(xiàn)一個很簡單的提取不含F(xiàn)C層的網(wǎng)絡(luò)的方法:

    resnet = models.resnet152(pretrained=True)
    modules = list(resnet.children())[:-1]   # delete the last fc layer.
    convnet = nn.Sequential(*modules)

另一種更簡單的方法:

resnet = models.resnet152(pretrained=True)
del resnet.fc

以上這篇pytorch實現(xiàn)用Resnet提取特征并保存為txt文件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python之生產(chǎn)者消費(fèi)者模型實現(xiàn)詳解

    python之生產(chǎn)者消費(fèi)者模型實現(xiàn)詳解

    這篇文章主要介紹了python之生產(chǎn)者消費(fèi)者模型實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Python3使用TCP編寫一個簡易的文件下載器功能

    Python3使用TCP編寫一個簡易的文件下載器功能

    這篇文章主要介紹了Python3使用TCP編寫一個簡易的文件下載器功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • IntelliJ 中配置 Anaconda的過程圖解

    IntelliJ 中配置 Anaconda的過程圖解

    這篇文章主要介紹了IntelliJ 中配置 Anaconda過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 基于Python的數(shù)據(jù)分析與可視化

    基于Python的數(shù)據(jù)分析與可視化

    在當(dāng)今數(shù)字化時代,數(shù)據(jù)分析和可視化已經(jīng)成為了企業(yè)和個人必備的技能,Python 作為一種高級編程語言,具有易學(xué)易用、高效快捷的特點,在數(shù)據(jù)科學(xué)領(lǐng)域中得到了廣泛應(yīng)用,本篇文章將介紹基于 Python 的數(shù)據(jù)分析與可視化
    2023-07-07
  • Python中plot函數(shù)語法示例詳解

    Python中plot函數(shù)語法示例詳解

    這篇文章主要介紹了Python中plot函數(shù)語法的相關(guān)資料,文章詳細(xì)介紹了MATLAB中的plot()函數(shù),包括其語法、如何繪制二維線圖、設(shè)置線型、標(biāo)記符號和顏色以及如何繪制多組二維線圖等,需要的朋友可以參考下
    2024-11-11
  • Python實現(xiàn)批量替換Excel中字符

    Python實現(xiàn)批量替換Excel中字符

    這篇文章主要為大家詳細(xì)介紹了如何使用Python實現(xiàn)批量替換Excel中字符,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • Python中處理YAML文件的正確方法

    Python中處理YAML文件的正確方法

    在現(xiàn)代軟件開發(fā)中,YAML(YAML Ain’t Markup Language)因其簡潔和易于閱讀的特性,被廣泛用于配置文件,Python 作為一種流行的編程語言,提供了多種處理 YAML 文件的庫,本文給大家介紹了Python中處理YAML文件的正確方法,需要的朋友可以參考下
    2024-11-11
  • python實現(xiàn)寫數(shù)字文件名的遞增保存文件方法

    python實現(xiàn)寫數(shù)字文件名的遞增保存文件方法

    今天小編就為大家分享一篇python實現(xiàn)寫數(shù)字文件名的遞增保存文件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 最新評論

    湖南省| 朔州市| 武城县| 五指山市| 正定县| 普定县| 兖州市| 陈巴尔虎旗| 酉阳| 靖江市| 富锦市| 平舆县| 石狮市| 苍山县| 登封市| 西城区| 伊宁县| 昭平县| 安丘市| 肇州县| 宣化县| 沂南县| 定西市| 自治县| 调兵山市| 南投县| 延津县| 宜川县| 星座| 临朐县| 临武县| 油尖旺区| 孟津县| 白朗县| 巴里| 墨江| 正蓝旗| 金门县| 郧西县| 鄱阳县| 凤山县|