当前位置:网站首页>AssertionError assert I.ndim == 4 and I.shape[1] == 3
AssertionError assert I.ndim == 4 and I.shape[1] == 3
2022-07-01 04:45:00 【booze-J】
Run code :
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)
# Be careful dataset in transform The parameter receives an object , So we need to add parentheses , In addition, when using neural network for operation, the data type required is tensor type , therefore transforms Parameters to add .
dataloader = DataLoader(dataset,batch_size=64)
# Build a simple network
class Booze(nn.Module):
# Inherit nn.Module The initialization
def __init__(self):
super().__init__()
# Note that here is to create a global variable, so we need to add a self When out_channels Far greater than in_channels The original image needs to be expanded , That is to say padding The value of cannot be set to 0 了 , According to the formula
self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=(3),stride=1,padding=0)
# rewrite forward function
def forward(self,x):
x = self.conv1(x)
return x
# Initialize the network
obj = Booze()
# Check out the Internet
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 Zhang 3 passageway 32X32 Pictures of the
print(imgs.shape)
# torch.Size([64, 6, 30, 30]) 64 Zhang 6 passageway 30X30 Pictures of the
print(output.shape)
# Use tensorboard visualization Note that multiple images are to be used add_images instead of add_image
writer.add_images("input",imgs,step)
# because output yes 6 The number of channels cannot be displayed , Direct visualization will report an error , So we need to deal with output Conduct reshape reshape When a number is unknown in the second parameter of , You can fill in -1, He will automatically help you calculate , Why is it unknown ? Because I just don't know how much to fill , fill 64 I'm sure not , Then changing the number of channels is equivalent to cutting out the extra pixels
writer.add_images("output",output,step)
step+=1
writer.close()
The error reported by the running code is as follows :
Why did it go wrong ?
The reason is that in the neural network we build self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=(3),stride=1,padding=0)
among out_channels=6
The number of channels for outputting pictures is 6 The tunnel ,6 The picture of the number of channels cannot be displayed , Use it directly tensorboard Visual error , The error is in the above code writer.add_images("output",output,step)
This line of code , So before executing this line of code, you need to output Conduct reshape,reshape become 3 Picture of the number of channels .
Solution output = torch.reshape(output,(-1,3,30,30))
This line of code is added to writer.add_images("output",output,step)
Before ,reshape When a number is unknown in the second parameter of , You can fill in -1, He will automatically help you calculate , Why is it unknown ? Because I just don't know how much to fill , fill 64 I'm sure not , Then changing the number of channels is equivalent to cutting out the extra pixels , Put it in batch_size in .
# (-1,3,30,30) = (batch_size,channels,H,W)
output = torch.reshape(output,(-1,3,30,30))
writer.add_images("output",output,step)
边栏推荐
- Maixll dock quick start
- Codeforces Round #771 (Div. 2) ABCD|E
- 总结全了,低代码还需要解决这4点问题
- Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
- Why is Internet thinking not suitable for AI products?
- 科研狗可能需要的一些工具
- The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
- Talk about testdeploy
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
- 分布式全局唯一ID解决方案详解
猜你喜欢
The longest increasing subsequence and its optimal solution, total animal weight problem
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
测量三相永磁同步电机的交轴直轴电感
VR线上展览所具备应用及特色
Maixll dock quick start
Dede collection plug-in does not need to write rules
分布式架构系统拆分原则、需求、微服务拆分步骤
神经网络-最大池化的使用
Odeint et GPU
神经网络-卷积层
随机推荐
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
The longest increasing subsequence and its optimal solution, total animal weight problem
This sideline workload is small, 10-15k, free unlimited massage
2022年煤气考试题库及在线模拟考试
STM32扩展版 按键扫描
Odeint et GPU
分布式-总结列表
About the transmission pipeline of stage in spark
2022年聚合工艺考试题及模拟考试
Pytorch(一) —— 基本语法
Applications and features of VR online exhibition
LeetCode_58(最后一个单词的长度)
神经网络-卷积层
[pat (basic level) practice] - [simple simulation] 1064 friends
Maixll dock quick start
One click shell to automatically deploy any version of redis
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
CF1638E. Colorful operations Kodori tree + differential tree array
先有网络模型的使用及修改
Take a cold bath