最新国产好看的视频,伊人天堂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的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 提升Python項(xiàng)目整潔度使用import?linter實(shí)例探究

    提升Python項(xiàng)目整潔度使用import?linter實(shí)例探究

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

    int在python中的含義以及用法

    在本篇文章中小編給大家整理了關(guān)于int在python中的含義以及用法,對此有興趣的朋友們可以跟著學(xué)習(xí)下。
    2019-06-06
  • Python使用Gradio實(shí)現(xiàn)免費(fèi)的內(nèi)網(wǎng)穿透

    Python使用Gradio實(shí)現(xiàn)免費(fèi)的內(nèi)網(wǎng)穿透

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

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

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

    表格梳理python內(nèi)置數(shù)學(xué)模塊math分析詳解

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

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

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

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

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

    Python3使用正則表達(dá)式爬取內(nèi)涵段子示例

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

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

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

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

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

最新評論

锦州市| 汤阴县| 潼南县| 南川市| 赤城县| 广德县| 曲阜市| 乡城县| 白河县| 绩溪县| 繁峙县| 久治县| 依兰县| 平罗县| 枣强县| 西宁市| 宜兰县| 平陆县| 同心县| 石门县| 新沂市| 盐津县| 桐城市| 永善县| 黄山市| 县级市| 芦溪县| 大兴区| 攀枝花市| 南岸区| 广宗县| 文昌市| 额济纳旗| 玉树县| 洮南市| 遵义市| 五寨县| 封丘县| 恩平市| 宝坻区| 奇台县|