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

利用Pytorch實(shí)現(xiàn)獲取特征圖的方法詳解

 更新時(shí)間:2022年10月13日 11:41:55   作者:拜陽(yáng)  
這篇文章主要為大家詳細(xì)介紹了如何利用Pytorch實(shí)現(xiàn)獲取特征圖,包括提取單個(gè)特征圖和提取多個(gè)特征圖,文中的示例代碼講解詳細(xì),需要的可以參考一下

簡(jiǎn)單加載官方預(yù)訓(xùn)練模型

torchvision.models預(yù)定義了很多公開(kāi)的模型結(jié)構(gòu)

如果pretrained參數(shù)設(shè)置為False,那么僅僅設(shè)定模型結(jié)構(gòu);如果設(shè)置為True,那么會(huì)啟動(dòng)一個(gè)下載流程,下載預(yù)訓(xùn)練參數(shù)

如果只想調(diào)用模型,不想訓(xùn)練,那么設(shè)置model.eval()和model.requires_grad_(False)

想查看模型參數(shù)可以使用modules和named_modules,其中named_modules是一個(gè)長(zhǎng)度為2的tuple,第一個(gè)變量是name,第二個(gè)變量是module本身。

# -*- coding: utf-8 -*-
from torch import nn
from torchvision import models

# load model. If pretrained is True, there will be a downloading process
model = models.vgg19(pretrained=True)
model.eval()
model.requires_grad_(False)

# get model component
features = model.features
modules = features.modules()
named_modules = features.named_modules()

# print modules
for module in modules:
    if isinstance(module, nn.Conv2d):
        weight = module.weight
        bias = module.bias
        print(module, weight.shape, bias.shape,
              weight.requires_grad, bias.requires_grad)
    elif isinstance(module, nn.ReLU):
        print(module)

print()
for named_module in named_modules:
    name = named_module[0]
    module = named_module[1]
    if isinstance(module, nn.Conv2d):
        weight = module.weight
        bias = module.bias
        print(name, module, weight.shape, bias.shape,
              weight.requires_grad, bias.requires_grad)
    elif isinstance(module, nn.ReLU):
        print(name, module)

圖片預(yù)處理

使用opencv和pil讀圖都可以使用transforms.ToTensor()把原本[H, W, 3]的數(shù)據(jù)轉(zhuǎn)成[3, H, W]的tensor。但opencv要注意把數(shù)據(jù)改成RGB順序。

vgg系列模型需要做normalization,建議配合torchvision.transforms來(lái)實(shí)現(xiàn)。

mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be at least 224. The images have to be loaded in to a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406] and std = [0.229, 0.224, 0.225].

參考:https://pytorch.org/hub/pytorch_vision_vgg/

# -*- coding: utf-8 -*-
from PIL import Image
import cv2
import torch
from torchvision import transforms

# transforms for preprocess
preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])

# load image using cv2
image_cv2 = cv2.imread('lena_std.bmp')
image_cv2 = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
image_cv2 = preprocess(image_cv2)

# load image using pil
image_pil = Image.open('lena_std.bmp')
image_pil = preprocess(image_pil)

# check whether image_cv2 and image_pil are same
print(torch.all(image_cv2 == image_pil))
print(image_cv2.shape, image_pil.shape)

提取單個(gè)特征圖

如果只提取單層特征圖,可以把模型截?cái)啵怨?jié)省算力和顯存消耗。

下面索引之所以有+1是因?yàn)閜ytorch預(yù)訓(xùn)練模型里面第一個(gè)索引的module總是完整模塊結(jié)構(gòu),第二個(gè)才開(kāi)始子模塊。

# -*- coding: utf-8 -*-
from PIL import Image
from torchvision import models
from torchvision import transforms

# load model. If pretrained is True, there will be a downloading process
model = models.vgg19(pretrained=True)
model = model.features[:16 + 1]  # 16 = conv3_4
model.eval()
model.requires_grad_(False)
model.to('cuda')
print(model)

# load and preprocess image
preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225]),
    transforms.Resize(size=(224, 224))
])
image = Image.open('lena_std.bmp')
image = preprocess(image)
inputs = image.unsqueeze(0)  # add batch dimension
inputs = inputs.cuda()

# forward
output = model(inputs)
print(output.shape)

提取多個(gè)特征圖

第一種方式:逐層運(yùn)行model,如果碰到了需要保存的feature map就存下來(lái)。

第二種方式:使用register_forward_hook,使用這種方式需要用一個(gè)類把feature map以成員變量的形式緩存下來(lái)。

兩種方式的運(yùn)行效率差不多

第一種方式簡(jiǎn)單直觀,但是只能處理類似VGG這種沒(méi)有跨層連接的網(wǎng)絡(luò);第二種方式更加通用。

# -*- coding: utf-8 -*-
from PIL import Image
import torch
from torchvision import models
from torchvision import transforms

# load model. If pretrained is True, there will be a downloading process
model = models.vgg19(pretrained=True)
model = model.features[:16 + 1]  # 16 = conv3_4
model.eval()
model.requires_grad_(False)
model.to('cuda')

# check module name
for named_module in model.named_modules():
    name = named_module[0]
    module = named_module[1]
    print('-------- %s --------' % name)
    print(module)
    print()

# load and preprocess image
preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225]),
    transforms.Resize(size=(224, 224))
])
image = Image.open('lena_std.bmp')
image = preprocess(image)
inputs = image.unsqueeze(0)  # add batch dimension
inputs = inputs.cuda()

# forward - 1
layers = [2, 7, 8, 9, 16]
layers = sorted(set(layers))
feature_maps = {}
feature = inputs
for i in range(max(layers) + 1):
    feature = model[i](feature)
    if i in layers:
        feature_maps[i] = feature
for key in feature_maps:
    print(key, feature_maps.get(key).shape)


# forward - 2
class FeatureHook:
    def __init__(self, module):
        self.inputs = None
        self.output = None
        self.hook = module.register_forward_hook(self.get_features)

    def get_features(self, module, inputs, output):
        self.inputs = inputs
        self.output = output


layer_names = ['2', '7', '8', '9', '16']
hook_modules = []
for named_module in model.named_modules():
    name = named_module[0]
    module = named_module[1]
    if name in layer_names:
        hook_modules.append(module)

hooks = [FeatureHook(module) for module in hook_modules]
output = model(inputs)
features = [hook.output for hook in hooks]
for feature in features:
    print(feature.shape)

# check correctness
for i, layer in enumerate(layers):
    feature1 = feature_maps.get(layer)
    feature2 = features[i]
    print(torch.all(feature1 == feature2))

使用第二種方式(register_forward_hook),resnet特征圖也可以順利拿到。

而由于resnet的model已經(jīng)不可以用model[i]的形式索引,所以無(wú)法使用第一種方式。

# -*- coding: utf-8 -*-
from PIL import Image
from torchvision import models
from torchvision import transforms

# load model. If pretrained is True, there will be a downloading process
model = models.resnet18(pretrained=True)
model.eval()
model.requires_grad_(False)
model.to('cuda')

# check module name
for named_module in model.named_modules():
    name = named_module[0]
    module = named_module[1]
    print('-------- %s --------' % name)
    print(module)
    print()

# load and preprocess image
preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225]),
    transforms.Resize(size=(224, 224))
])
image = Image.open('lena_std.bmp')
image = preprocess(image)
inputs = image.unsqueeze(0)  # add batch dimension
inputs = inputs.cuda()


class FeatureHook:
    def __init__(self, module):
        self.inputs = None
        self.output = None
        self.hook = module.register_forward_hook(self.get_features)

    def get_features(self, module, inputs, output):
        self.inputs = inputs
        self.output = output


layer_names = [
    'conv1',
    'layer1.0.relu',
    'layer2.0.conv1'
]

hook_modules = []
for named_module in model.named_modules():
    name = named_module[0]
    module = named_module[1]
    if name in layer_names:
        hook_modules.append(module)

hooks = [FeatureHook(module) for module in hook_modules]
output = model(inputs)
features = [hook.output for hook in hooks]
for feature in features:
    print(feature.shape)

問(wèn)題來(lái)了,resnet這種類型的網(wǎng)絡(luò)結(jié)構(gòu)怎么截?cái)啵?/p>

使用如下命令就可以,print查看需要截?cái)嗟侥睦?,然后用nn.Sequential重組即可。

需注意重組后網(wǎng)絡(luò)的module_name會(huì)發(fā)生變化。

print(list(model.children())
model = torch.nn.Sequential(*list(model.children())[:6])

以上就是利用Pytorch實(shí)現(xiàn)獲取特征圖的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Pytorch獲取特征圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Python中where()函數(shù)的用法

    詳解Python中where()函數(shù)的用法

    本篇文章主要介紹了詳解Python中where()函數(shù)的用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Python?torch.onnx.export用法詳細(xì)介紹

    Python?torch.onnx.export用法詳細(xì)介紹

    這篇文章主要給大家介紹了關(guān)于Python?torch.onnx.export用法詳細(xì)介紹的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Python logging模塊用法示例

    Python logging模塊用法示例

    這篇文章主要介紹了Python logging模塊用法,結(jié)合實(shí)例形式分析了Python logging模塊相關(guān)配置、函數(shù)、組件等操作方法與注意事項(xiàng),需要的朋友可以參考下
    2018-08-08
  • 完美解決Python2操作中文名文件亂碼的問(wèn)題

    完美解決Python2操作中文名文件亂碼的問(wèn)題

    下面小編就為大家?guī)?lái)一篇完美解決Python2操作中文名文件亂碼的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Matplotlab顯示OpenCV讀取到的圖像

    Matplotlab顯示OpenCV讀取到的圖像

    本文介紹了如何使用OpenCV和Matplotlib處理圖像,本文通過(guò)實(shí)例講解的非常詳細(xì),對(duì)Matplotlab?OpenCV圖像相關(guān)處理方法感興趣的朋友一起看看吧
    2025-02-02
  • python學(xué)生管理系統(tǒng)開(kāi)發(fā)

    python學(xué)生管理系統(tǒng)開(kāi)發(fā)

    這篇文章主要為大家詳細(xì)介紹了基礎(chǔ)版和函數(shù)版的python學(xué)生管理系統(tǒng)開(kāi)發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python模擬點(diǎn)擊在ios中實(shí)現(xiàn)的實(shí)例講解

    python模擬點(diǎn)擊在ios中實(shí)現(xiàn)的實(shí)例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python模擬點(diǎn)擊在ios中實(shí)現(xiàn)的實(shí)例講解內(nèi)容,有需要的朋友們可以參考下。
    2020-11-11
  • python使用tkinter包實(shí)現(xiàn)進(jìn)度條

    python使用tkinter包實(shí)現(xiàn)進(jìn)度條

    python中的tkinter包是一種常見(jiàn)的設(shè)計(jì)程序的GUI界面用的包,本文將使用tkinter包實(shí)現(xiàn)不同風(fēng)格的進(jìn)度條,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例

    python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例

    今天小編就為大家分享一篇python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • python使用turtle繪制國(guó)際象棋棋盤

    python使用turtle繪制國(guó)際象棋棋盤

    這篇文章主要為大家詳細(xì)介紹了python使用turtle畫國(guó)際象棋棋盤,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05

最新評(píng)論

隆昌县| 修武县| 通道| 织金县| 凤台县| 乌鲁木齐县| 平度市| 桐柏县| 高雄县| 当阳市| 隆尧县| 抚松县| 清远市| 泗阳县| 黄龙县| 长乐市| 潢川县| 平果县| 海口市| 离岛区| 波密县| 吉木乃县| 新昌县| 黎城县| 钦州市| 民权县| 玉山县| 苏尼特左旗| 腾冲县| 水富县| 长垣县| 会昌县| 梅河口市| 葫芦岛市| 丰台区| 嵩明县| 务川| 廊坊市| 绥阳县| 合江县| 安庆市|