【python 走进pytotch】pytorch实现用Resnet提取特征

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,
而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。人工智能教程

准备一张图片,pytorch可以方便地实现用预训练的网络提取特征。
下面我们用pytorch提取图片采用预训练网络resnet50,提取图片特征。

# -*- 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
import warnings
warnings.filterwarnings("ignore")


features_dir = 'F:/img_spam/test/features/'


img_path = "F:/img_spam/test/10064004487036357500320010026498.jpg"
file_name = img_path.split('/')[-1]
feature_path = os.path.join(features_dir, file_name + '.txt')

print(feature_path)

transform1 = transforms.Compose([
    transforms.Scale(256),
    transforms.CenterCrop(224),
    transforms.ToTensor()]
)

img = Image.open(img_path)
img1 = transform1(img)

print(img1)

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

x = Variable(torch.unsqueeze(img1, dim=0).float(), requires_grad=False)

print(x)
y = resnet50_feature_extractor(x)
y = y.data.numpy()
print(y)

np.savetxt(feature_path, y, delimiter=',')
y_ = np.loadtxt(feature_path, delimiter=',').reshape(1, 2048)

print(y_)

运行结果:

E:\laidefa\python.exe C:/Users/xiaohu/PycharmProjects/深度学习/pytorch实战/resNet提取图片特征.py
F:/img_spam/test/features/10064004487036357500320010026498.jpg.txt
tensor([[[ 0.9961,  0.9961,  0.9961,  ...,  0.9961,  0.9961,  0.9961],
         [ 0.9961,  0.9961,  0.9961,  ...,  0.9961,  0.9961,  0.9961],
         [ 0.9961,  0.9961,  0.9961,  ...,  0.9961,  0.9961,  0.9961],
         ...,
         [ 0.9059,  0.9059,  0.8902,  ...,  0.9020,  0.9569,  0.9412],
         [ 0.9255,  0.9098,  0.9255,  ...,  0.8667,  0.9294,  0.9216],
         [ 0.9059,  0.9098,  0.9059,  ...,  0.9216,  0.9529,  0.9412]],

        [[ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
         ...,
         [ 0.3412,  0.1137,  0.3255,  ...,  0.3608,  0.5176,  0.1569],
         [ 0.4667,  0.2824,  0.4314,  ...,  0.3020,  0.5216,  0.1490],
         [ 0.3137,  0.0667,  0.2863,  ...,  0.2510,  0.3020,  0.0784]],

        [[ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
         ...,
         [ 0.0627,  0.0275,  0.0627,  ...,  0.0980,  0.1294,  0.0471],
         [ 0.1098,  0.0941,  0.1255,  ...,  0.0627,  0.1098,  0.0275],
         [ 0.0588,  0.0353,  0.0667,  ...,  0.0941,  0.0902,  0.0392]]])
tensor([[[[ 0.9961,  0.9961,  0.9961,  ...,  0.9961,  0.9961,  0.9961],
          [ 0.9961,  0.9961,  0.9961,  ...,  0.9961,  0.9961,  0.9961],
          [ 0.9961,  0.9961,  0.9961,  ...,  0.9961,  0.9961,  0.9961],
          ...,
          [ 0.9059,  0.9059,  0.8902,  ...,  0.9020,  0.9569,  0.9412],
          [ 0.9255,  0.9098,  0.9255,  ...,  0.8667,  0.9294,  0.9216],
          [ 0.9059,  0.9098,  0.9059,  ...,  0.9216,  0.9529,  0.9412]],

         [[ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
          ...,
          [ 0.3412,  0.1137,  0.3255,  ...,  0.3608,  0.5176,  0.1569],
          [ 0.4667,  0.2824,  0.4314,  ...,  0.3020,  0.5216,  0.1490],
          [ 0.3137,  0.0667,  0.2863,  ...,  0.2510,  0.3020,  0.0784]],

         [[ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  ...,  0.0000,  0.0000,  0.0000],
          ...,
          [ 0.0627,  0.0275,  0.0627,  ...,  0.0980,  0.1294,  0.0471],
          [ 0.1098,  0.0941,  0.1255,  ...,  0.0627,  0.1098,  0.0275],
          [ 0.0588,  0.0353,  0.0667,  ...,  0.0941,  0.0902,  0.0392]]]])
[[0.36967766 0.5629435  0.49159744 ... 0.33528978 0.42739153 0.3224204 ]]
[[0.36967766 0.56294352 0.49159744 ... 0.33528978 0.42739153 0.32242039]]

Process finished with exit code 0

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页