使用kaggle dog bless 数据集
不使用dataloader的情况下
手动将图像转为tensor,并简单搭建只有一个卷积的神经网络
经过模型卷积后的可视乎效果
知识点:
- cv2->tensor
- model 搭建
- tensor->cv2.show
import cv2
import torch.nn as nn
import torch
import numpy
import torch.nn.functional as F
class LeNet_1(nn.Module):
def __init__(self):
super(LeNet_1, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3, stride=1,padding=1)
)
self.maxpool = nn.Sequential(
nn.MaxPool2d(kernel_size=2)
)
self.fc1 = nn.Sequential(
nn.Linear(in_features= 512*512*3 , out_features=120)
)
def forward(self,x):
x = self.conv1(x)
# x = x.view(-1, 512*512*3) #后面有多分类,整理成[1,n]维度
# x = self.fc1(x) # 全连接层,输出[1,classes]
# x = self.maxpool(x)#降低一半分辨率
return x
if __name__ == '__main__':
model = LeNet_1()
batchsize =1
channel = 3
img_w = 512
img_h = 512
impath = "../dogDataset/train/0a0c223352985ec154fd604d7ddceabd.jpg"
img = cv2.imread(impath)#读入img
img = cv2.resize(img,(img_w,img_h))#调整长宽
img_tensor = torch.from_numpy(img)#手动转tensor[512,512,3]
# tensorx = torch.rand(batchsize, channel, img_w, img_h)
img_tensor = img_tensor.permute(2,0,1) # 手动维度转换[3,512,512]
img_tensor = img_tensor.unsqueeze(0) # 添加维度[1,3,512,512]==>[batchsize, channel, img_w, img_h]
img_tensor = img_tensor.type(torch.float32)#需要float32采用用CEloss计算
out = model.forward(img_tensor)
#接下来的代码仅仅为了可视化,不属于模型必须
img = out.type(torch.uint8).squeeze()#去除第一个维度
img = img.permute(1,2,0)#手动维度转换[3,512,512]==》[512,512,3]
img = img.numpy() # tensor -> np
outmat = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow("dfdfd", outmat) # can't show because it has been transform to tensor
cv2.waitKey(0)