当前位置:网站首页>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)
边栏推荐
- LeetCode_53(最大子数组和)
- Common interview questions ①
- TCP server communication flow
- Software testing needs more and more talents. Why do you still not want to take this path?
- 2022-02-15 (399. Division evaluation)
- 2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
- 常用的Transforms中的方法
- Extension fragment
- 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
- The design points of voice dialogue system and the importance of multi round dialogue
猜你喜欢
神经网络-最大池化的使用
分布式事务-解决方案
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
STM32 extended key scan
Dataloader的使用
MySQL winter vacation self-study 2022 12 (5)
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
The longest increasing subsequence and its optimal solution, total animal weight problem
slf4j 简单实现
2022 gas examination question bank and online simulation examination
随机推荐
Construction of Meizhou nursing laboratory: equipment configuration
分布式数据库数据一致性的原理、与技术实现方案
STM32扩展板 温度传感器和温湿度传感器的使用
技术分享| 融合调度中的广播功能设计
【硬十宝典】——1.【基础知识】电源的分类
2022年T电梯修理题库及模拟考试
Dual contractual learning: text classification via label aware data augmentation reading notes
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
AssertionError assert I.ndim == 4 and I.shape[1] == 3
How to do the performance pressure test of "Health Code"
Common UNIX Operation and maintenance commands of shell
OdeInt與GPU
2022 t elevator repair question bank and simulation test
Simple implementation of slf4j
神经网络-使用Sequential搭建神经网络
数据加载及预处理
Matters behind the construction of paint testing laboratory
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
分布式架构系统拆分原则、需求、微服务拆分步骤
Introduction to JVM stack and heap