运行torchsummary报错

如果你想使用torchsummary打印模型结构,但是报错,那么其实笔者便已经不推荐使用torchsummary了,你可以尝试测试代码的第一种方法,他的方法更加通用,而且不依赖别的库。

测试代码的两种方式

模仿dataset.py, train.py加print(model)测试网络结构

但是这里你需要注意的是print(model)打印的只是继承自nn.Module的子类并且打印的是__init__方法中全部的继承自nn.Module的对象。

from torch.utils.data import DataLoader
from torch.utils.data.dataset import Dataset
#from torchsummary import summary
class TrainSet(Dataset):
    def __init__(self, X, Y):
        # 定义好 image 的路径
        self.X, self.Y = X, Y

    def __getitem__(self, index):
        return self.X[index], self.Y[index]

    def __len__(self):
        return len(self.X)
def main():
    X_tensor = torch.ones((4,1,32, 256, 256))
    Y_tensor = torch.zeros((4,1,32, 256, 256))
    mydataset = TrainSet(X_tensor, Y_tensor)
    train_loader = DataLoader(mydataset, batch_size=2, shuffle=True)

    net=Net()
    print(net)
    import torch.nn as nn
    loss_fn = nn.MSELoss()
    optimizer = torch.optim.SGD(net.parameters(), lr=1e-3)

    # 3) Training loop
    for epoch in range(10):
        for i, (X, y) in enumerate(train_loader):
            # predict = forward pass with our model
            pred = net(X)
            loss = loss_fn(pred, y)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            print('epoch={},i={}'.format(epoch,i))
if __name__ == '__main__':
    main()

使用torchsummary

def torch_summary():
    from torchsummary import summary
    import os, sys
    os.environ["CUDA_VISIBLE_DEVICES"] = '3'
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = Net()
    model = model.to(device=device)
    input_tensor: tuple = (1, 32, 256, 256)
    summary(model, input_size=input_tensor)    
if __name__ == '__main__':
    torch_summary()

torchsummary报错

torchsummary报错,建议使用print(model)。以下是集中在运行torchsummary时候常见错误:

未将model放入GPU上

报错内如如下:“RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same”

由于input_size会自动将Tensor放入GPU中,所以当报这个错误时,那是你没有将生成的model放入GPU,需要添加如下代码:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device=device)
summary(model, input_size=(3, 224, 224))

GPU指定错误

报错内容如下:“RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:5! (when checking argument for argument weight in method wrapper__cudnn_convolution)”

或者 “RuntimeError: CUDA error: out of memory
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.”

或者 “RuntimeError: CUDA error: invalid device ordinal
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.”

或者 “RuntimeError: CUDA error: out of memory
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.”

实验室服务器上有多台GPU设备,训练时候会指定cuda:0, cuda:1。报这个错误是因为input_size会自动将参数传入cuda:0,而你有可能将mode传入了cuda:5,输入Tensor和model在两张不同的显卡上!!这个时候你需要将物理上的cuda:5设置为你环境变量中的cuda:0便可以解决问题,代码如下:

    from torchsummary import summary
    import os, sys
    os.environ["CUDA_VISIBLE_DEVICES"] = '5'
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = model.to(device=device)
    summary(model, input_size=(3, 224, 224))    

需要注意的事项

input_size并不需要传入batch_size参数,除了batch_size之外的channel,slice(dimension),height,width都需要传入。而你在train时则需要传入batch_size参数,下面两句话是等价的,虽然他俩Tensor.shape的维度不同。

input = torch.randn(1, 3, 224, 224)
summary(model, input_size=(3, 224, 224))    
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是什么以及如何使用它查看模型的结构和参数量? torchsummary是一个用于查看PyTorch模型结构和参数量的工具。通过使用torchsummary.summary()函数,可以输出模型的输入和输出的形状,以及网络的顺序结构、参数量和模型大小等信息。 要使用torchsummary,首先需要在Anaconda prompt中进入自己的PyTorch环境,并安装依赖包torchsummary,可以使用pip install torchsummary命令进行安装。 然后,在代码中导入torchsummary模块,并调用summary()函数,传入模型、输入大小、批次大小和设备等参数。summary()函数将会输出模型的结构和参数量等信息。 这样,就可以使用torchsummary工具来更加清晰地查看PyTorch模型的结构和参数量了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [pytorch 中的torchsummary](https://blog.csdn.net/qq_41468616/article/details/121164258)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [pytorch 网络可视化(一):torchsummary](https://blog.csdn.net/Wenyuanbo/article/details/118514709)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值