3D-resnet 50 医学3D图像二分类python代码

离上次发布3D-resnet代码时隔两年,最近让AI推荐3D-resnet的文章给我,AI推荐了三篇
其中两篇是我两年前发的,另一篇在这里Resnet3D预训练网络......
于是决定更新之前代码,供诸位参考

1. 可以用cpu或gpu(推荐8G以上,已设置好CUDA参看)
2. 代码参考的腾讯的MedicalNet,预训练模型可以在腾讯微云下载(文件里有很多模型,选用resnet_50.pth这个)
3. 文件格式为常见的nii

文件夹格式如下

文件夹结构如下  
    |--data/:
    |   |--images/:# 原始图像
    |   |    |--1.nii.gz
    |   |    |--......
    |   |    |--n.nii.gz
    |	|--rois/: # ROI
    |   |    |--1.nii.gz
    |   |    |--......
    |   |    |--n.nii.gz
    |   |--test.txt # 外部验证数据列表
    |   |--train.txt# 训练数据列表
    |   |--val.txt  # 内部验证数据列表
    |--pretrain/:# 预训练模型
    |   |--resnet_50.pth
    |--trails/:  # 训练(finturn)后最好的模型
    |   |--best.pth
    |--train.py: 


train.txt等格式如下,三列分别为原始图像(绝对路径),ROI(绝对路径),以及标签(数字),空格分隔
E:\...\1.nrrd E:\...\1.nrrd 0
E:\...\2.nrrd E:\...\2.nrrd 1

代码

import torch
from torch import nn
import torchio as tio
import os
import numpy as np
from torch.utils.data import Dataset
from scipy import ndimage
from torch import optim
from torch.utils.data import DataLoader
import time
import nibabel
from sklearn.metrics import roc_curve, auc
'''
以下是可修改的参数部分
'''
no_cuda = True # 这里设置为True是使用cpu训练和验证,设置为False是使用gpu。action='store_true', help='If true, cuda is not used.
train_img_list = r'E:\...\data\train.txt'  # type=str, help='Path of image list file for train'
val_img_list = r'E:...\data\val.txt'  # type=str, help='Path of image list file for validation
test_img_list = r'E:\...\data\test.txt'  # type=str, help='Path of image list file for test'
pretrain_path = r'E:\...\pretrain\resnet_50.pth'  # type=str, help='Path for pretrained model.'
save_folder = r'E:\...\trails'  # type=str, help='Path for saving model.'
best_model_path = r'E:\...\trails\best.pth' # type=str, help='the best trained model for test.'
total_epochs = 200  # type=int, help='Number of total epochs to run' #
save_intervals = 10  # type=int, help='Interation for saving model'
learning_rate = 0.001  # set to 0.001 when finetune, type=float, help= 'Initial learning rate (divided by 10 while training by lr scheduler)'
new_layer_names = ['conv_cls'] # type=list, help='New layer except for backbone, used for transforlearn'
batch_size = 10  # type=int, help='Batch Size'
input_D = 56; input_H = 56; input_W = 56  # type=int, help='Input size of depth,height,width'
torch.manual_seed(1)
'''
以下不可修改
'''
class Bottleneck(nn.Module): # 瓶颈模块
    expansion = 4
    def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv3d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm3d(planes)
        self.conv2 = nn.Conv3d(planes, planes, kernel_size=3, stride=stride, dilation=dilation, padding=dilation, bias=False)
        self.bn2 = nn.BatchNorm3d(planes)
        self.conv3 = nn.Conv3d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm3d(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride
        self.dilation = dilation
    def forward(self, x):
        residual =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dr_yingli

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值