Pytorch提取模型特征向量保存至csv的例子


Posted in Python onJanuary 03, 2020

Pytorch提取模型特征向量

# -*- coding: utf-8 -*-
"""
dj
"""
import torch
import torch.nn as nn
import os
from torchvision import models, transforms
from torch.autograd import Variable 
import numpy as np
from PIL import Image 
import torchvision.models as models
import pretrainedmodels
import pandas as pd
class FCViewer(nn.Module):
 def forward(self, x):
  return x.view(x.size(0), -1)
class M(nn.Module):
 def __init__(self, backbone1, drop, pretrained=True):
  super(M,self).__init__()
  if pretrained:
   img_model = pretrainedmodels.__dict__[backbone1](num_classes=1000, pretrained='imagenet') 
  else:
   img_model = pretrainedmodels.__dict__[backbone1](num_classes=1000, pretrained=None)  
  self.img_encoder = list(img_model.children())[:-2]
  self.img_encoder.append(nn.AdaptiveAvgPool2d(1))
  self.img_encoder = nn.Sequential(*self.img_encoder)
  if drop > 0:
   self.img_fc = nn.Sequential(FCViewer())         
  else:
   self.img_fc = nn.Sequential(
    FCViewer())
 def forward(self, x_img):
  x_img = self.img_encoder(x_img)
  x_img = self.img_fc(x_img)
  return x_img 
model1=M('resnet18',0,pretrained=True)
features_dir = '/home/cc/Desktop/features' 
transform1 = transforms.Compose([
  transforms.Resize(256),
  transforms.CenterCrop(224),
  transforms.ToTensor()]) 
file_path='/home/cc/Desktop/picture'
names = os.listdir(file_path)
print(names)
for name in names:
 pic=file_path+'/'+name
 img = Image.open(pic)
 img1 = transform1(img)
 x = Variable(torch.unsqueeze(img1, dim=0).float(), requires_grad=False)
 y = model1(x)
 y = y.data.numpy()
 y = y.tolist()
 #print(y)
 test=pd.DataFrame(data=y)
 #print(test)
 test.to_csv("/home/cc/Desktop/features/3.csv",mode='a+',index=None,header=None)

jiazaixunlianhaodemoxing

import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import argparse
class ResidualBlock(nn.Module):
 def __init__(self, inchannel, outchannel, stride=1):
  super(ResidualBlock, self).__init__()
  self.left = nn.Sequential(
   nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=stride, padding=1, bias=False),
   nn.BatchNorm2d(outchannel),
   nn.ReLU(inplace=True),
   nn.Conv2d(outchannel, outchannel, kernel_size=3, stride=1, padding=1, bias=False),
   nn.BatchNorm2d(outchannel)
  )
  self.shortcut = nn.Sequential()
  if stride != 1 or inchannel != outchannel:
   self.shortcut = nn.Sequential(
    nn.Conv2d(inchannel, outchannel, kernel_size=1, stride=stride, bias=False),
    nn.BatchNorm2d(outchannel)
   )

 def forward(self, x):
  out = self.left(x)
  out += self.shortcut(x)
  out = F.relu(out)
  return out

class ResNet(nn.Module):
 def __init__(self, ResidualBlock, num_classes=10):
  super(ResNet, self).__init__()
  self.inchannel = 64
  self.conv1 = nn.Sequential(
   nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False),
   nn.BatchNorm2d(64),
   nn.ReLU(),
  )
  self.layer1 = self.make_layer(ResidualBlock, 64, 2, stride=1)
  self.layer2 = self.make_layer(ResidualBlock, 128, 2, stride=2)
  self.layer3 = self.make_layer(ResidualBlock, 256, 2, stride=2)
  self.layer4 = self.make_layer(ResidualBlock, 512, 2, stride=2)
  self.fc = nn.Linear(512, num_classes)

 def make_layer(self, block, channels, num_blocks, stride):
  strides = [stride] + [1] * (num_blocks - 1) #strides=[1,1]
  layers = []
  for stride in strides:
   layers.append(block(self.inchannel, channels, stride))
   self.inchannel = channels
  return nn.Sequential(*layers)

 def forward(self, x):
  out = self.conv1(x)
  out = self.layer1(out)
  out = self.layer2(out)
  out = self.layer3(out)
  out = self.layer4(out)
  out = F.avg_pool2d(out, 4)
  out = out.view(out.size(0), -1)
  out = self.fc(out)
  return out


def ResNet18():

 return ResNet(ResidualBlock)

import os
from torchvision import models, transforms
from torch.autograd import Variable 
import numpy as np
from PIL import Image 
import torchvision.models as models
import pretrainedmodels
import pandas as pd
class FCViewer(nn.Module):
 def forward(self, x):
  return x.view(x.size(0), -1)
class M(nn.Module):
 def __init__(self, backbone1, drop, pretrained=True):
  super(M,self).__init__()
  if pretrained:
   img_model = pretrainedmodels.__dict__[backbone1](num_classes=1000, pretrained='imagenet') 
  else:
   img_model = ResNet18()
   we='/home/cc/Desktop/dj/model1/incption--7'
   # 模型定义-ResNet
   #net = ResNet18().to(device)
   img_model.load_state_dict(torch.load(we))#diaoyong  
  self.img_encoder = list(img_model.children())[:-2]
  self.img_encoder.append(nn.AdaptiveAvgPool2d(1))
  self.img_encoder = nn.Sequential(*self.img_encoder)
  if drop > 0:
   self.img_fc = nn.Sequential(FCViewer())         
  else:
   self.img_fc = nn.Sequential(
    FCViewer())
 def forward(self, x_img):
  x_img = self.img_encoder(x_img)
  x_img = self.img_fc(x_img)
  return x_img 
model1=M('resnet18',0,pretrained=None)
features_dir = '/home/cc/Desktop/features' 
transform1 = transforms.Compose([
  transforms.Resize(56),
  transforms.CenterCrop(32),
  transforms.ToTensor()]) 
file_path='/home/cc/Desktop/picture'
names = os.listdir(file_path)
print(names)
for name in names:
 pic=file_path+'/'+name
 img = Image.open(pic)
 img1 = transform1(img)
 x = Variable(torch.unsqueeze(img1, dim=0).float(), requires_grad=False)
 y = model1(x)
 y = y.data.numpy()
 y = y.tolist()
 #print(y)
 test=pd.DataFrame(data=y)
 #print(test)
 test.to_csv("/home/cc/Desktop/features/3.csv",mode='a+',index=None,header=None)

以上这篇Pytorch提取模型特征向量保存至csv的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
node.js获取参数的常用方法(总结)
May 29 Python
Python处理中文标点符号大集合
May 14 Python
Python动态生成多维数组的方法示例
Aug 09 Python
解决pandas.DataFrame.fillna 填充Nan失败的问题
Nov 06 Python
python+selenium实现QQ邮箱自动发送功能
Jan 23 Python
python分割一个文本为多个文本的方法
Jul 22 Python
基于python使用tibco ems代码实例
Dec 20 Python
pytorch实现mnist分类的示例讲解
Jan 10 Python
Tensorflow不支持AVX2指令集的解决方法
Feb 03 Python
keras实现多种分类网络的方式
Jun 11 Python
什么是python的自省
Jun 21 Python
Matlab求解数组中的最大值及它所在的具体位置
Apr 16 Python
pytorch查看torch.Tensor和model是否在CUDA上的实例
Jan 03 #Python
python实现上传文件到linux指定目录的方法
Jan 03 #Python
pytorch::Dataloader中的迭代器和生成器应用详解
Jan 03 #Python
django商品分类及商品数据建模实例详解
Jan 03 #Python
PyTorch和Keras计算模型参数的例子
Jan 02 #Python
Pytorch中实现只导入部分模型参数的方式
Jan 02 #Python
PyTorch中topk函数的用法详解
Jan 02 #Python
You might like
php 无法加载mcrypt.dll的解决办法
2013/04/03 PHP
探讨:parse url解析URL,返回其组成部分
2013/06/14 PHP
php ctype函数中文翻译和示例
2014/03/21 PHP
PHP反射机制用法实例
2014/08/28 PHP
大家在抢红包,程序员在研究红包算法
2015/08/31 PHP
php 7新特性之类型申明详解
2017/06/06 PHP
JavaScript 动态改变图片大小
2009/06/11 Javascript
jQuery代码优化 选择符篇
2011/11/01 Javascript
用于deeplink的js方法(判断手机是否安装app)
2014/04/02 Javascript
node.js中的fs.realpathSync方法使用说明
2014/12/16 Javascript
jQuery实现tab选项卡效果的方法
2015/07/08 Javascript
jQuery跨域问题解决方案
2015/08/03 Javascript
jquery Deferred 快速解决异步回调的问题
2016/04/05 Javascript
Ionic快速安装教程
2016/06/03 Javascript
Bootstrap风格的zTree右键菜单
2017/02/17 Javascript
利用Vue.js+Node.js+MongoDB实现一个博客系统(附源码)
2017/04/24 Javascript
详解vue-router 2.0 常用基础知识点之router.push()
2017/05/10 Javascript
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
2017/07/20 Javascript
vue实现添加与删除图书功能
2018/10/07 Javascript
JS实现的雪花飘落特效示例
2019/12/03 Javascript
vue制作抓娃娃机的示例代码
2020/04/17 Javascript
浅谈vant组件Picker 选择器选单选问题
2020/11/04 Javascript
[55:04]海涛DOTA2死魂复燃6.82版本介绍
2014/09/28 DOTA
[03:14]辉夜杯主赛事 12月25日每日之星
2015/12/26 DOTA
python实现获取序列中最小的几个元素
2014/09/25 Python
Python实现多线程抓取妹子图
2015/08/08 Python
Python中return self的用法详解
2018/07/27 Python
python日志logging模块使用方法分析
2019/05/23 Python
基于Python+Appium实现京东双十一自动领金币功能
2019/10/31 Python
pycharm激活码有效到2020年11月底
2020/09/18 Python
使用TFRecord存取多个数据案例
2020/02/17 Python
如何让pre和textarea等HTML元素去掉滚动条自动换行自适应文本内容高度
2019/08/01 HTML / CSS
网络、C以及其他硬件方面的面试题
2016/08/23 面试题
计算机专业毕业生求职信分享
2013/12/24 职场文书
采购求职信
2014/03/17 职场文书
校园安全广播稿范文
2014/09/25 职场文书