pytorch读取数据集(LiTS-肝肿瘤分割挑战数据集)

更新时间:2023-07-06 18:29:06 阅读: 评论:0

pytorch读取数据集(LiTS-肝肿瘤分割挑战数据集)pytorch 读取数据集
我的数据集长这样:
xx.png和xx_mask.png是对应的待分割图像和ground truth
读取数据集
数据集对象被抽象为Datat类,实现⾃定义的数据集需要继承Datat
getitem:返回⼀条数据,或⼀个样本。obj[index]等价于obj.__ getitem __(index)
len:返回样本的数量。len(obj)等价于obj.__ len__()
files = os.listdir(dirpath)打开⽂件夹
img.append([imgPath,maskPath])⽣成⼀个list包含img和label的路径
img_x = Image.open(xPath,‘r’)读取图⽚
img_x = lf.transforms(img_x)做数据集变形操作
class MyDatat(Datat):
def__init__(lf,dirpath,transforms =None,target_transforms =None):
files = os.listdir(dirpath)
len_files =len(files)//2
img =[]
for i in range(len_files):
imgPath = os.path.join(dirpath,"%03d.png"%i)
maskPath = os.path.join(dirpath,"%03d_mask.png"%i)
img.append([imgPath,maskPath])
lf.imgs = img
#lf.imgs 是⼀个list,lf.imgs的⼀个元素是⼀个元组,包含图⽚路径,图⽚标签路径
lf.tatget_transforms = target_transforms
#这个transform⾥边可以实现减均值,除标准差,随机裁剪,旋转,翻转,放射变换,等等操作def__getitem__(lf, index):
xPath,yPath = lf.imgs[index]
img_x = Image.open(xPath,'r')
img_y = Image.open(yPath,'r')
#Image.open读取图⽚信息
ansforms is not None:
img_x = lf.transforms(img_x)
if lf.tatget_transforms is not None:
img_y = lf.tatget_transforms(img_y)
return img_x,img_y
def__len__(lf):
return len(lf.imgs)
a = MyDatat(".\\data\\train")
n = np.array(a[1][0])
print(n.shape)
结果如下所⽰:
计算数据集的均值和⽅差
(图⽚均为512*512)
class MyDatat(Datat):
北京街舞培训班
生于忧患翻译
def__init__(lf,dirpath,transforms =None,target_transforms =None):kuwen
files = os.listdir(dirpath)
len_files =len(files)//2
img =[]
for i in range(len_files):
imgPath = os.path.join(dirpath,"%03d.png"%i)
maskPath = os.path.join(dirpath,"%03d_mask.png"%i)
img.append([imgPath,maskPath])
lf.imgs = img
#lf.imgs 是⼀个list,lf.imgs的⼀个元素是⼀个元组,包含图⽚路径,图⽚标签路径
lf.tatget_transforms = target_transforms
#这个transform⾥边可以实现减均值,除标准差,随机裁剪,旋转,翻转,放射变换,等等操作
def__getitem__(lf, index):
xPath,yPath = lf.imgs[index]
img_x = Image.open(xPath,'r')
img_y = Image.open(yPath,'r')
#Image.open读取图⽚信息
ansforms is not None:
img_x = lf.transforms(img_x)
if lf.tatget_transforms is not None:
img_y = lf.tatget_transforms(img_y)
return img_x,img_y
def__len__(lf):
return len(lf.imgs)
a = MyDatat(".\\data\\train")
def meanX(a):
R_channel_mean =0
G_channel_mean =0
B_channel_mean =0
R_channel_var =0
G_channel_var =0
B_channel_var =0
dinner是什么意思
for i in range(len(a)):
n = np.array(a[i][0])
R_channel_mean = R_channel_mean + np.sum(n[:,:,0])
G_channel_mean = G_channel_mean + np.sum(n[:,:,1])
B_channel_mean = B_channel_mean + np.sum(n[:,:,2])
for i in range(len(a)):
n = np.array(a[i][0])
R_channel_var += np.sum((n[:,:,0]- R_channel_mean)**2)
G_channel_var += np.sum((n[:,:,0]- G_channel_mean)**2)
B_channel_var += np.sum((n[:,:,0]- B_channel_mean)**2)
R_channel_mean = R_channel_mean /512/512/len(a)
G_channel_mean = G_channel_mean /512/512/len(a)
B_channel_mean = B_channel_mean /512/512/len(a)
R_channel_var = np.sqrt(R_channel_var)/512/512/len(a)
G_channel_var = np.sqrt(G_channel_var)/512/512/len(a)
B_channel_var = np.sqrt(B_channel_var)/512/512/len(a)
#图⽚是512*512的
return R_channel_mean,G_channel_mean,B_channel_mean,R_channel_var,G_channel_var,B_channel_var
R,G,B,RV,GV,BV = meanX(a)
print(R,G,B,RV,GV,BV)
结果如下所⽰:
灰度图三通道均⼀致
训练数据集
import os
as nn
from PIL import Image
import torch
ansforms as transforms
from torch.utils.data import DataLoader
udf
import argpar
from torch.utils.data import Datat
import numpy as np
class MyDatat(Datat):
def__init__(lf, root ,transform , target_transform): dir= os.listdir(root)
lendir =len(dir)//2
imgpath =[]
for i in range(lendir):
imgs = os.path.join(root,"%03d.png"%i)
mask = os.path.join(root,"%03d_mask.png"%i)            imgpath.append([imgs,mask])
lf.imgpath = imgpath
lf.target_transform = target_transform
def__getitem__(lf, item):
imgs,masks = lf.imgpath[item]
img = Image.open(imgs,'r')
mask = Image.open(masks,'r')
img = lf.transform(img)
mask = lf.target_transform(mask)
return img,mask
def__len__(lf):
return len(lf.imgpath)
class DoubleConv(nn.Module):
def__init__(lf,num_in,num_out):
super(DoubleConv,lf).__init__()
nn.Conv2d(num_in,num_out,3,1,1),
nn.BatchNorm2d(num_out),
nn.ReLU(inplace=True),
nn.Conv2d(num_out,num_out,3,1,1),
nn.BatchNorm2d(num_out),
nn.ReLU(inplace=True),
)
def forward(lf,x):
v(x)
class Unet(nn.Module):
class Unet(nn.Module):
def__init__(lf,num_in,num_out):
super(Unet,lf).__init__()
lf.pool1 = nn.MaxPool2d(2,2)
lf.pool2 = nn.MaxPool2d(2,2)
lf.pool3 = nn.MaxPool2d(2,2)
lf.pool4 = nn.MaxPool2d(2,2)
lf.up6 = nn.ConvTranspo2d(1024,512,2,2)
中级口译口试报名lf.up7 = nn.ConvTranspo2d(512,256,2,2)
lf.up8 = nn.ConvTranspo2d(256,128,2,2)
lf.up9 = nn.ConvTranspo2d(128,64,2,2)
lf.sigmoid = nn.Sigmoid()
def forward(lf,x):
c1 = lf.conv1(x)
p1 = lf.pool1(c1)
c2 = lf.conv2(p1)
p2 = lf.pool2(c2)
c3 = lf.conv3(p2)
p3 = lf.pool3(c3)
c4 = lf.conv4(p3)孙吉芯
p4 = lf.pool4(c4)
上海化妆培训c5 = lf.conv5(p4)
u6 = lf.up6(c5)
merge6 = torch.cat([c4, u6], dim=1)
名词语法c6 = lf.conv6(merge6)
u7 = lf.up7(c6)
merge7 = torch.cat([u7, c3], dim=1)
c7 = lf.conv7(merge7)
u8 = lf.up8(c7)
merge8 = torch.cat([u8, c2], dim=1)
c8 = lf.conv8(merge8)
u9 = lf.up9(c8)
merge9 = torch.cat([u9, c1], dim=1)
c9 = lf.conv9(merge9)
c10 = lf.conv10(c9)
out = lf.sigmoid(c10)
return out
path ="/content/drive/My Drive/Pytorch_try/unet/train"
path_test ="/content/drive/My Drive/Pytorch_try/unet/val"
x_transform = transforms.Compo([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
]
)
y_transform = transforms.ToTensor()
#y_transform = transforms.ToTensor
device = torch.device("cuda"if torch.cuda.is_available()el"cpu")
def train():
model = Unet(3,1).to(device)siren
optimizer = torch.optim.Adam(model.parameters(),lr=args.learning_rate)    datat = MyDatat(path,x_transform,y_transform)
cretersion = nn.BCELoss()

本文发布于:2023-07-06 18:29:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/169142.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:音响品牌
标签:数据   分割   均值
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图