Pytorch官方目前无法像tensorflow, caffe那样直接给出shape信息,详见
https://github.com/pytorch/pytorch/pull/3043
以下代码算一种workaround。由于CNN, RNN等模块实现不一样,添加其他模块支持可能需要改代码。
例如RNN中bias是bool类型,其权重也不是存于weight属性中,不过我们只关注shape够用了。
该方法必须构造一个输入调用forward后(model(x)调用)才可获取shape
#coding:utf-8 from collections import OrderedDict import torch from torch.autograd import Variable import torch.nn as nn import models.crnn as crnn import json def get_output_size(summary_dict, output): if isinstance(output, tuple): for i in xrange(len(output)): summary_dict[i] = OrderedDict() summary_dict[i] = get_output_size(summary_dict[i],output[i]) else: summary_dict['output_shape'] = list(output.size()) return summary_dict def summary(input_size, model): def register_hook(module): def hook(module, input, output): class_name = str(module.__class__).split('.')[-1].split("'")[0] module_idx = len(summary) m_key = '%s-%i' % (class_name, module_idx+1) summary[m_key] = OrderedDict() summary[m_key]['input_shape'] = list(input[0].size()) summary[m_key] = get_output_size(summary[m_key], output) params = 0 if hasattr(module, 'weight'): params += torch.prod(torch.LongTensor(list(module.weight.size()))) if module.weight.requires_grad: summary[m_key]['trainable'] = True else: summary[m_key]['trainable'] = False #if hasattr(module, 'bias'): # params += torch.prod(torch.LongTensor(list(module.bias.size()))) summary[m_key]['nb_params'] = params if not isinstance(module, nn.Sequential) and not isinstance(module, nn.ModuleList) and not (module == model): hooks.append(module.register_forward_hook(hook)) # check if there are multiple inputs to the network if isinstance(input_size[0], (list, tuple)): x = [Variable(torch.rand(1,*in_size)) for in_size in input_size] else: x = Variable(torch.rand(1,*input_size)) # create properties summary = OrderedDict() hooks = [] # register hook model.apply(register_hook) # make a forward pass model(x) # remove these hooks for h in hooks: h.remove() return summary crnn = crnn.CRNN(32, 1, 3755, 256, 1) x = summary([1,32,128],crnn) print json.dumps(x)
以pytorch版CRNN为例,输出shape如下
{ "Conv2d-1": { "input_shape": [1, 1, 32, 128], "output_shape": [1, 64, 32, 128], "trainable": true, "nb_params": 576 }, "ReLU-2": { "input_shape": [1, 64, 32, 128], "output_shape": [1, 64, 32, 128], "nb_params": 0 }, "MaxPool2d-3": { "input_shape": [1, 64, 32, 128], "output_shape": [1, 64, 16, 64], "nb_params": 0 }, "Conv2d-4": { "input_shape": [1, 64, 16, 64], "output_shape": [1, 128, 16, 64], "trainable": true, "nb_params": 73728 }, "ReLU-5": { "input_shape": [1, 128, 16, 64], "output_shape": [1, 128, 16, 64], "nb_params": 0 }, "MaxPool2d-6": { "input_shape": [1, 128, 16, 64], "output_shape": [1, 128, 8, 32], "nb_params": 0 }, "Conv2d-7": { "input_shape": [1, 128, 8, 32], "output_shape": [1, 256, 8, 32], "trainable": true, "nb_params": 294912 }, "BatchNorm2d-8": { "input_shape": [1, 256, 8, 32], "output_shape": [1, 256, 8, 32], "trainable": true, "nb_params": 256 }, "ReLU-9": { "input_shape": [1, 256, 8, 32], "output_shape": [1, 256, 8, 32], "nb_params": 0 }, "Conv2d-10": { "input_shape": [1, 256, 8, 32], "output_shape": [1, 256, 8, 32], "trainable": true, "nb_params": 589824 }, "ReLU-11": { "input_shape": [1, 256, 8, 32], "output_shape": [1, 256, 8, 32], "nb_params": 0 }, "MaxPool2d-12": { "input_shape": [1, 256, 8, 32], "output_shape": [1, 256, 4, 33], "nb_params": 0 }, "Conv2d-13": { "input_shape": [1, 256, 4, 33], "output_shape": [1, 512, 4, 33], "trainable": true, "nb_params": 1179648 }, "BatchNorm2d-14": { "input_shape": [1, 512, 4, 33], "output_shape": [1, 512, 4, 33], "trainable": true, "nb_params": 512 }, "ReLU-15": { "input_shape": [1, 512, 4, 33], "output_shape": [1, 512, 4, 33], "nb_params": 0 }, "Conv2d-16": { "input_shape": [1, 512, 4, 33], "output_shape": [1, 512, 4, 33], "trainable": true, "nb_params": 2359296 }, "ReLU-17": { "input_shape": [1, 512, 4, 33], "output_shape": [1, 512, 4, 33], "nb_params": 0 }, "MaxPool2d-18": { "input_shape": [1, 512, 4, 33], "output_shape": [1, 512, 2, 34], "nb_params": 0 }, "Conv2d-19": { "input_shape": [1, 512, 2, 34], "output_shape": [1, 512, 1, 33], "trainable": true, "nb_params": 1048576 }, "BatchNorm2d-20": { "input_shape": [1, 512, 1, 33], "output_shape": [1, 512, 1, 33], "trainable": true, "nb_params": 512 }, "ReLU-21": { "input_shape": [1, 512, 1, 33], "output_shape": [1, 512, 1, 33], "nb_params": 0 }, "LSTM-22": { "input_shape": [33, 1, 512], "0": { "output_shape": [33, 1, 512] }, "1": { "0": { "output_shape": [2, 1, 256] }, "1": { "output_shape": [2, 1, 256] } }, "nb_params": 0 }, "Linear-23": { "input_shape": [33, 512], "output_shape": [33, 256], "trainable": true, "nb_params": 131072 }, "BidirectionalLSTM-24": { "input_shape": [33, 1, 512], "output_shape": [33, 1, 256], "nb_params": 0 }, "LSTM-25": { "input_shape": [33, 1, 256], "0": { "output_shape": [33, 1, 512] }, "1": { "0": { "output_shape": [2, 1, 256] }, "1": { "output_shape": [2, 1, 256] } }, "nb_params": 0 }, "Linear-26": { "input_shape": [33, 512], "output_shape": [33, 3755], "trainable": true, "nb_params": 1922560 }, "BidirectionalLSTM-27": { "input_shape": [33, 1, 256], "output_shape": [33, 1, 3755], "nb_params": 0 } }
以上这篇pytorch中获取模型input/output shape实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 明达年度发烧碟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]