圆月山庄资源网 Design By www.vgjia.com
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的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月03日
2024年11月03日
- 明达年度发烧碟MasterSuperiorAudiophile2021[DSF]
- 英文DJ 《致命的温柔》24K德国HD金碟DTS 2CD[WAV+分轨][1.7G]
- 张学友1997《不老的传说》宝丽金首版 [WAV+CUE][971M]
- 张韶涵2024 《不负韶华》开盘母带[低速原抓WAV+CUE][1.1G]
- lol全球总决赛lcs三号种子是谁 S14全球总决赛lcs三号种子队伍介绍
- lol全球总决赛lck三号种子是谁 S14全球总决赛lck三号种子队伍
- 群星.2005-三里屯音乐之男孩女孩的情人节【太合麦田】【WAV+CUE】
- 崔健.2005-给你一点颜色【东西音乐】【WAV+CUE】
- 南台湾小姑娘.1998-心爱,等一下【大旗】【WAV+CUE】
- 【新世纪】群星-美丽人生(CestLaVie)(6CD)[WAV+CUE]
- ProteanQuartet-Tempusomniavincit(2024)[24-WAV]
- SirEdwardElgarconductsElgar[FLAC+CUE]
- 田震《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- BEYOND《大地》24K金蝶限量编号[低速原抓WAV+CUE][986M]
- 陈奕迅《准备中 SACD》[日本限量版] [WAV+CUE][1.2G]