首页 > 其他分享 >pytorch MRI脑瘤检测

pytorch MRI脑瘤检测

时间:2023-03-11 15:55:51浏览次数:30  
标签:img healthy self tumor np pytorch 脑瘤 128 MRI

读取数据

#reading the images
tumor = []
path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\yes\\*.jpg'#*表示所有
for f in glob.iglob(path):#遍历所有的yes图片
    img = cv2.imread(f)
    img = cv2.resize(img, (128, 128))#相当于reshape改变图片大小
    b, g, r = cv2.split(img)#分割为单独的通道
    img = cv2.merge([r, g, b])#改变通道顺序
    tumor.append(img)

healthy = []
path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\no\\*.jpg'#*表示所有
for f in glob.iglob(path):#遍历所有的yes图片
    img = cv2.imread(f)
    img = cv2.resize(img, (128, 128))#类似于reshape改变图片大小
    b, g, r = cv2.split(img)#分割为单独的通道
    img = cv2.merge([r, g, b])#改变通道顺序
    healthy.append(img)

#our images
#改变数据形式
tumor = np.array(tumor, dtype=np.float32)
healthy = np.array(healthy, dtype=np.float32)
print(tumor.shape)#(154, 128, 128, 3)154表示154张图, 128 rows, 128 columns
print(healthy.shape)#(91, 128, 128, 3)91表示91张图, 128 rows, 128 columns
All = np.concatenate((healthy, tumor))
print(All.shape)#(245, 128, 128, 3)

Brain MRI 可视化

  • 显示一张图片
plt.imshow(healthy[0])
plt.show()
  • 取随机数
np.random.choice(5, 3)#[0, 5)中随机选3个int, e.g.[1 3 0]
  • 定义一个随机取图片的函数
def plot_random(healthy, tumor, num=5):
    #随机返回5张healthy和tumor
    healthy_imgs = healthy[np.random.choice(healthy.shape[0], 5, replace=False)]
    tumor_imgs = tumor[np.random.choice(tumor.shape[0], 5, replace=False)]

    plt.figure(figsize=(16, 9))#fix size
    #显示healthy
    for i in range(num):
        plt.subplot(1, num, i+1)
        plt.title('healthy')
        plt.imshow(healthy_imgs[i])
    # 打开显示的图片
    plt.show()
    ##显示tumor
    for i in range(num):
        plt.subplot(1, num, i+1)
        plt.title('tumor')
        plt.imshow(tumor_imgs[i])
    #打开显示的图片
    plt.show()

plot_random(healthy=healthy, tumor=tumor)

继承抽象类Dataset, Overriding getitem, len, add

  • getitem: 通过index检索数据集的特定数据
  • len: 得到数据集的长度
  • add: 两个数据集相加

构造一个简单的用于数组的类

class MRI(Dataset):
    def __init__(self, scores):
        self.x = scores
    def __getitem__(self, index):
        return self.x[index]
    def __len__(self):
        return len(self.x)

s1 = [1, 2, 3, 4]
d1 = MRI(s1)#s1代入构造函数中的scores
print(d1.x)#[1, 2, 3, 4]
print(d1[2])#自动调用getitem, 输出3

s2 = [100, 200, 300, 400]
d2 = MRI(s2)
#会自动调用父类中的__add__
d = d1 + d2

自定义用于MRI的dataset类

class MRI(Dataset):
    def __init__(self):
        # reading the images
        tumor = []
        path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\yes\\*.jpg'  # *表示所有
        for f in glob.iglob(path):  # 遍历所有的yes图片
            img = cv2.imread(f)
            img = cv2.resize(img, (128, 128))  # 相当于reshape改变图片大小
            b, g, r = cv2.split(img)  # 分割为单独的通道
            img = cv2.merge([r, g, b])  # 改变通道顺序
            tumor.append(img)

        healthy = []
        path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\no\\*.jpg'  # *表示所有
        for f in glob.iglob(path):  # 遍历所有的yes图片
            img = cv2.imread(f)
            img = cv2.resize(img, (128, 128))  # 相当于reshape改变图片大小
            b, g, r = cv2.split(img)  # 分割为单独的通道
            img = cv2.merge([r, g, b])  # 改变通道顺序
            healthy.append(img)

        # our images
        # 改变数据形式
        tumor = np.array(tumor, dtype=np.float32)
        healthy = np.array(healthy, dtype=np.float32)

        # our labels
        tumor_label = np.ones(tumor.shape[0], dtype=np.float32)  # tumor的label用1, 这里生成一堆1; tumor.shape[0]表示有多少张tumor图片
        healthy_label = np.zeros(healthy.shape[0], dtype=np.float32)  # healthy的label用0, 这里生成一堆0

        #定义实例变量; 并且连接
        #在第0轴连接: e.g.(100, 512, 512, 3)和(200, 512, 512, 3)-->(300, 512, 512, 3)最后得到300张图片[第0轴是图片数量]
        self.images = np.concatenate((tumor, healthy), axis=0)
        self.labels = np.concatenate((tumor_label, healthy_label))


    def __len__(self):
        return self.images.shape[0]#在定义类时实例变量都要加self;这里返回有多少张图
    #不仅返回image, 也要返回label; 这里返回一个包含两者的字典
    def __getitem__(self, index):


        #不仅要返回对应的图片, 也要返回label
        sample = {'image': self.images[index], 'label': self.labels[index]}
        return sample

    def normalize(self):
        self.images = self.images/255.0#255是np.max(mri)

mri = MRI()
print(len(mri))
print(mri.__getitem__(5)['image'])#打印图片的矩阵
print(mri.__getitem__(5)['image'].shape)#(128, 128, 3)宽128高128通道3
print(mri.__getitem__(5)['label'])#打印label值

标签:img,healthy,self,tumor,np,pytorch,脑瘤,128,MRI
From: https://www.cnblogs.com/nameless233/p/17206223.html

相关文章

  • pytorch中GroupNorm的使用(笔记)
    不使用BatchNorm的原因  相比起batchNorm,groupNorm对一个batch的size更不敏感。在现有的研究中,batchNorm在batchsize越大的情况下,效果更佳,batchsize=32的情况明显优于ba......
  • 2023-3-8-pytorch关于CNN的描述与代码,ai对话生成的
    左脚踩右脚上天了是什么搞笑笑话这不是搞笑笑话,而是一个玩笑语言游戏。“左脚踩右脚上天了”实际上是一个不合逻辑的语句,因为脚无法踩在自己的脚上,更不可能踩上天。这种语......
  • 深度学习Pytorch(一)
    深度学习Pytorch(一)前言:必须使用英伟达显卡才能使用cuda(显卡加速)!移除环境:condaremove-npytorch--all一、安装Pytorch下载Anaconda打开AnacondaPrompt......
  • Pytorch中norm(几种范数norm的详细介绍)
    1.范数(norm)的简单介绍概念:距离的定义是一个宽泛的概念,只要满足非负,自反,三角不等式就可以称之为距离。范数是一种强化了的距离概念,它在定义上比距离多了一条数乘的运算法......
  • PyTorch中的dim
    PyTorch中对tensor的很多操作如sum,softmax等都可以设置dim参数用来指定操作在哪一维进行。PyTorch中的dim类似于numpy中的axis。dim与方括号的关系创建一个矩阵a=to......
  • 机器学习日志 手写数字识别 pytorch 神经网络
    我是链接第一次用pytorch写机器学习,不得不说是真的好用pytorch的学习可以看这里,看看基本用法就行,个人感觉主要还是要看着实践代码来学习总结了几个点:1.loss出现nan这......
  • 安装pytorch报错 ERROR: Could not install packages due to an OSError: [Errno 28]
    windos安装,报错如下  看了不少回答,大概是缓存和内存满了我的C盘只给了70G,然后意外发现只剩下3G多了,先用系统自带的清理工具清理了一下,然后腾讯电脑管家“工具箱”中......
  • pytorch_debug
    1、报错信息1.1、出错位置1image=Image.open('./img.png')2#图像预处理3transforms=transforms.Compose([transforms.Resize(256),4......
  • 01 Pytorch的数据载体张量与线性回归
    Pytorch的数据载体张量与线性回归Pytorch官方英文文档:https://pytorch.org/docs/stable/torch.html?Pytorch中文文档:https://pytorch-cn.readthedocs.io/zh/latest/1.......
  • DL 基础:PyTorch 常用代码存档
    1pandas读csvimporttorchfromtorchimportnnimportnumpyasnpimportpandasaspdfromcopyimportdeepcopydevice="cuda"iftorch.cuda.is_available()......