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

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

 更新時間:2020年01月03日 08:47:34   作者:樸素.無恙  
今天小編就為大家分享一篇Pytorch提取模型特征向量保存至csv的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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項目整潔度使用import?linter實例探究

    提升Python項目整潔度使用import?linter實例探究

    在復雜的Python項目中,良好的代碼組織結構是維護性和可讀性的關鍵,本文將深入研究?import-linter?工具,它是一個強大的靜態(tài)分析工具,旨在優(yōu)化項目的模塊導入,提高代碼質量和可維護性
    2024-01-01
  • int在python中的含義以及用法

    int在python中的含義以及用法

    在本篇文章中小編給大家整理了關于int在python中的含義以及用法,對此有興趣的朋友們可以跟著學習下。
    2019-06-06
  • Python使用Gradio實現免費的內網穿透

    Python使用Gradio實現免費的內網穿透

    內網穿透是一種將內部網絡服務暴露到公共網絡的技術,可以讓外部用戶訪問內部網絡上的服務,本文將介紹如何使用Gradio實現免費的內網穿透,需要的可以參考下
    2024-03-03
  • python導包的幾種方法(自定義包的生成以及導入詳解)

    python導包的幾種方法(自定義包的生成以及導入詳解)

    這篇文章主要介紹了python導包的幾種方法(自定義包的生成以及導入詳解),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • 表格梳理python內置數學模塊math分析詳解

    表格梳理python內置數學模塊math分析詳解

    這篇文章主要為大家介紹了python內置數學模塊math的分析詳解,文中通過表格梳理的方式以便讓大家在學習過程中一目望去清晰明了,有需要的朋友可以借鑒參考下
    2021-10-10
  • PyQt使用QPropertyAnimation開發(fā)簡單動畫

    PyQt使用QPropertyAnimation開發(fā)簡單動畫

    這篇文章主要介紹了PyQt使用QPropertyAnimation開發(fā)簡單動畫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • python?對excel交互工具的使用詳情

    python?對excel交互工具的使用詳情

    這篇文章主要介紹了python?對excel交互工具的使用詳情,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07
  • Python3使用正則表達式爬取內涵段子示例

    Python3使用正則表達式爬取內涵段子示例

    這篇文章主要介紹了Python3使用正則表達式爬取內涵段子,涉及Python正則匹配與文件讀寫相關操作技巧,需要的朋友可以參考下
    2018-04-04
  • python3在各種服務器環(huán)境中安裝配置過程

    python3在各種服務器環(huán)境中安裝配置過程

    這篇文章主要介紹了python3在各種服務器環(huán)境中安裝配置過程,源碼包編譯安裝步驟詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • Pycharm及python安裝詳細教程(圖解)

    Pycharm及python安裝詳細教程(圖解)

    這篇文章主要介紹了Pycharm及python安裝詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-07-07

最新評論

太原市| 湾仔区| 无极县| 高尔夫| 尤溪县| 淳化县| 米林县| 池州市| 宜黄县| 金秀| 阜宁县| 津市市| 衡山县| 武平县| 嵊州市| 台南市| 靖西县| 容城县| 随州市| 政和县| 富民县| 唐山市| 芦山县| 巩留县| 闻喜县| 普陀区| 河源市| 镇原县| 阜宁县| 渑池县| 岳阳市| 平武县| 沙坪坝区| 顺平县| 彩票| 双鸭山市| 永州市| 通化县| 福清市| 长治县| 吴忠市|