当前位置:网站首页>AssertionError assert I.ndim == 4 and I.shape[1] == 3
AssertionError assert I.ndim == 4 and I.shape[1] == 3
2022-07-01 04:35:00 【booze-J】
运行代码:
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
# 注意dataset中transform参数接收的是个对象,所以要加上括号,还有就是之后使用神经网络进行运算的时候需要的数据类型是tensor类型,所以transforms参数要加上。
dataloader = DataLoader(dataset,batch_size=64)
# 搭建一个简单的网络
class Booze(nn.Module):
# 继承nn.Module的初始化
def __init__(self):
super().__init__()
# 注意这里是创建一个全局变量所以要加上一个self 当out_channels远大于in_channels时需要对原图像进行扩充,也就是padding的值不能设为0了,需要根据公式
self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=(3),stride=1,padding=0)
# 重写forward函数
def forward(self,x):
x = self.conv1(x)
return x
# 初始化网络
obj = Booze()
# 查看网络
print(obj)
''' Booze( (conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1)) ) '''
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs,targets = data
output = obj(imgs)
# torch.Size([64, 3, 32, 32]) 64张3通道32X32的图片
print(imgs.shape)
# torch.Size([64, 6, 30, 30]) 64张6通道30X30的图片
print(output.shape)
# 使用tensorboard可视化 注意多张图片是要使用add_images而不是add_image
writer.add_images("input",imgs,step)
# 由于output是6通道数的无法显示,直接可视化会报错,所以我们需要对output进行reshape reshape的第二参数中当一个数未知时,你可以填入-1,他会自动帮你计算,为什么会未知呢?因为就是不知道填多少,填64的话肯定不行吧,然后改变通道数相当于把多余的像素给切出来了
writer.add_images("output",output,step)
step+=1
writer.close()
运行代码报错如下:
为什么会出错呢?
原因是我们搭建的神经网络中self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=(3),stride=1,padding=0)
其中out_channels=6
就是输出图片的通道数是6通道的,6通道数的图片无法显示,直接使用tensorboard可视化会报错,报错的就是上述代码中的writer.add_images("output",output,step)
这一行代码,所以在执行这行代码前需要对output进行reshape,reshape成3通道数的图片。
解决方案output = torch.reshape(output,(-1,3,30,30))
这一行代码添加到writer.add_images("output",output,step)
之前,reshape的第二参数中当一个数未知时,你可以填入-1,他会自动帮你计算,为什么会未知呢?因为就是不知道填多少,填64的话肯定不行吧,然后改变通道数相当于把多余的像素给切出来了,放到了batch_size中。
# (-1,3,30,30) = (batch_size,channels,H,W)
output = torch.reshape(output,(-1,3,30,30))
writer.add_images("output",output,step)
边栏推荐
- Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
- CUDA development and debugging tool
- The index is invalid
- Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
- 2022 question bank and answers for safety production management personnel of hazardous chemical production units
- [godot] unity's animator is different from Godot's animplayer
- Difference between cookie and session
- 2022-02-15 (399. Division evaluation)
- Kodori tree board
猜你喜欢
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
Offline installation of Wireshark 2.6.10
How to do the performance pressure test of "Health Code"
Dede collection plug-in does not need to write rules
[godot] unity's animator is different from Godot's animplayer
Pytorch(四) —— 可视化工具 Visdom
Openresty rewrites the location of 302
Grey correlation cases and codes
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
Basic usage, principle and details of session
随机推荐
2022 t elevator repair question bank and simulation test
Task04 | statistiques mathématiques
Simple implementation of slf4j
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
TCP server communication flow
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
Dede collection plug-in does not need to write rules
MySQL advanced -- you will have a new understanding of MySQL
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
2022年T电梯修理题库及模拟考试
Maixll-Dock 快速上手
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
One job hopping up 8K, three times in five years
Grey correlation cases and codes
Daily question - line 10
Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
Threejs opening
How to view the changes and opportunities in the construction of smart cities?
Basic usage, principle and details of session
软件研发的十大浪费:研发效能的另一面