深度学习实战案例三:青光眼数据集多分类预测(GoogLeNet)

import  torch
from    torch import optim, nn
import  visdom
import  torchvision
from    torch.utils.data import DataLoader

from    DIYdata_loader import DIYData_loader
from    GoogLeNet import GoogLeNet

batchsz = 20
picture_resize = 224
lr = 1e-3
epochs = 10

device = torch.device('cuda')
torch.manual_seed(1234)


train_db = DIYData_loader('D:\python pycharm learning\清华大佬课程\青光眼GooLeNet\青光眼分类', picture_resize, mode='train')
val_db = DIYData_loader('D:\python pycharm learning\清华大佬课程\青光眼GooLeNet\青光眼分类', picture_resize, mode='val')
test_db = DIYData_loader('D:\python pycharm learning\清华大佬课程\青光眼GooLeNet\青光眼分类', picture_resize, mode='test')
train_loader = DataLoader(train_db, batch_size=batchsz, shuffle=True,
                          num_workers=4)
val_loader = DataLoader(val_db, batch_size=batchsz, num_workers=2)
test_loader = DataLoader(test_db, batch_size=batchsz, num_workers=2)

viz = visdom.Visdom()


def evalute(model, loader):
    model.eval()

    correct = 0
    total = len(loader.dataset)

    for x, y in loader:
        x, y = x.to(device), y.to(device)
        with torch.no_grad():
            logits = model(x)
            pred = logits.argmax(dim=1)
        correct += torch.eq(pred, y).sum().float().item()

    return correct / total

def main():
    model = GoogLeNet(3).to(device)
    optimizer = optim.Adam(model.parameters(),lr=lr)
    criteon = nn.CrossEntropyLoss()

    best_acc,best_epoch =0, 0
    global_step = 0
    viz.line([0],[-1], win='loss',opts = dict(title='loss'))
    viz.line([0],[-1], win='val_acc', opts=dict(title='val_acc'))
    for epoch in range(epochs):

        for step, (x,y) in enumerate(train_loader):
            #x:[b,3,224,224],y :[b]
            x,y = x.to(device),y.to(device)

            model.train()
            logits = model(x)
            loss = criteon(logits,y)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            viz.line([loss.item()], [global_step], win='loss', update = 'append')
            global_step+=1

        if epoch %1 ==0:
            val_acc = evalute(model,val_loader)
            if val_acc > best_acc:
                best_epoch =epoch
                best_acc =val_acc

                torch.save(model.state_dict(),'best.mdl')
                viz.line([val_acc], [global_step], win='val_acc', update='append')

    print('best acc:',best_acc,'best epoch:',best_epoch)

    model.load_state_dict(torch.load('best.mdl'))
    print('loaded from ckpt!')

    test_acc = evalute(model,test_loader)
    print('test acc:', test_acc)



if __name__=='__main__':
    main()
# 数据集共有364张图:Glaucoma:32,Normal:225,Suspect Glaucoma:107
import torch
import os, glob
import random, csv
from torch.utils.data import Dataset, DataLoader
from PIL import Image
from torchvision import transforms


# 自定义数据加载类
class DIYData_loader(Dataset):
    def __init__(self, root, resize, mode):  # root:文件所在目录,resize:图像分辨率调整一致,mode:当前类何功能
        super(DIYData_loader, self).__init__()

        self.root = root
        self.resize = resize

        self.name2label = {}  # 对每个加载的文件进行编码:'bulbasaur': 0, 'charmander': 1, 'mewtwo': 2, 'pikachu': 3, 'squirtle': 4
        for name in sorted(os.listdir((os.path.join(root)))):  # 对指定root中的文件进行排序
            if not os.path.isdir(os.path.join(root, name)):
                continue
            self.name2label[name] = len(self.name2label.keys())  # keys返回列表当中的value,len计算列表长度
        #print(self.name2label)  # 根据文件顺序,以idx:文件名,vlaue:0,1,2,3,4,生成列表
        # images labels
        self.images, self.labels = self.load_csv('images.csv')  # load_csv要么先创建images.csv,要么直接读取images.csv,
        #print('data_len:',len(self.images))
        if mode == 'train':  # train dataset 60% of ALL DATA
            self.images = self.images[:int(0.6 * len(self.images))]
            self.labels = self.labels[:int(0.6 * len(self.labels))]
        elif mode == 'validation':  # val dataset 60%-80% of ALL DATA
            self.images = self.images[int(0.6 * len(self.images)):int(0.8 * len(self.images))]
            self.labels = self.labels[int(0.6 * len(self.labels)):int(0.8 * len(self.labels))]
        else:  # test dataset 80%-100% of ALL DATA
            self.images = self.images[int(0.8 * len(self.images)):int(len(self.images))]
            self.labels = self.labels[int(0.8 * len(self.labels)):int(len(self.labels))]
        # images[0]: D:\python pycharm learning\清华大佬课程\fisrt\pokemon\mewtwo\00000081.png
        # #labels[0]:2
        # images 还是图片的地址列表,需要__getitem__继续转换

    # image,label 不能把所有图片全部加载到内存,可能会爆内存
    def load_csv(self, filename):  # 生成,读取filename文件
        # filename 不存在:生成filename
        if not os.path.exists(os.path.join(self.root, filename)):
            images = []
            for name in self.name2label.keys():
                # .../pokemen/mewtwo/00001.png 加载进images列表
                # 实际上是加载每张图片的地址
                images += glob.glob(os.path.join(self.root, name, '*.png'))
                images += glob.glob(os.path.join(self.root, name, '*.jpg'))
                images += glob.glob(os.path.join(self.root, name, '*.jpeg'))

            #print(len(images), images[0])
            random.shuffle(images)
            with open(os.path.join(self.root, filename), mode='w', newline='') as f:
                writer = csv.writer(f)
                for img in images:  # .....\bulbasaur\00000000.png
                    name = img.split(os.sep)[-2]  # 指:bulbasaur 图片真实类别
                    label = self.name2label[name]  # 在name2label列表根据name找出对应的value:0,1...
                    # .....\bulbasaur\00000000.png , 0
                    writer.writerow([img, label])
                print('writen into csv file:', filename)

        # filename 存在:直接读取filename
        images, labels = [], []
        with open(os.path.join(self.root, filename)) as f:
            reader = csv.reader(f)
            for row in reader:
                # '...pokemon\bulbasaur\00000000.png', 0
                img, label = row
                label = int(label)

                images.append(img)
                labels.append(label)

        assert len(images) == len(labels)
        return images, labels

    def __len__(self):
        return len(self.images)

    def denormalize(self, x_hat):  # 对已经进行规范化处理的totensor,去除规范化
        mean = [0.485, 0.456, 0.406]
        std = [0.229, 0.224, 0.225]

        # x_hat = (x-mean)/std
        # x = x_hat*std +mean
        # x:[c,h,w]
        # mean:[3]=>[3,1,1]
        mean = torch.tensor(mean).unsqueeze(1).unsqueeze(1)
        std = torch.tensor(std).unsqueeze(1).unsqueeze(1)

        x = x_hat * std + mean

        return x

    def __getitem__(self, idx):
        pass
        # idx~[0~len(images)]
        # self.iamges,self.labels
        # images[0]: D:\python pycharm learning\清华大佬课程\fisrt\pokemon\mewtwo\00000081.png
        # #labels[0]:2
        img, label = self.images[idx], self.labels[idx]
        tf = transforms.Compose([
            lambda x: Image.open(x).convert('RGB'),  # string image => image data
            transforms.Resize((int(self.resize * 1.25), int(self.resize * 1.25))),  # 压缩到稍大
            transforms.RandomRotation(20),  # 图片旋转,增加图片的复杂度,但是又不会使网络太复杂
            transforms.CenterCrop(self.resize),  # 可能会有其他的底存在
            transforms.ToTensor(),
            #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
            transforms.Normalize(mean=[0, 0, 0], std=[1, 1, 1])
            # R mean:0.854,std:0.229
        ])
        img = tf(img)
        label = torch.tensor(label)
        # Pokemon类根据一个索引每次返回一个img(三位张量),一个label(0维张量)
        return img, label  # img,label打包成元组返回


def main():
    import visdom  # 启动 python -m visdom.server,http://localhost:8097
    import time
    viz = visdom.Visdom()
    db = DIYData_loader('D:\python pycharm learning\清华大佬课程\second\青光眼分类', 224, 'train')
    # x,y = next(iter(db))
    # print('sample:',x.shape,y.shape,y)
    # viz.images(db.denormalize(x), win='sample_x', opts=dict(title='sample_x'))
    # DataLoader加载器按batch_size打乱所有依次在内存当中按批次顺序加载每次批次,
    # 每个批次内含batch个Pokemon类返回的对象(元组,列表,字符串)
    loader = DataLoader(db, batch_size=20, shuffle=True)
    for x, y in loader:
        print('x_shape:',x.shape)

        viz.images(db.denormalize(x), nrow=8, win='batch', opts=dict(title='batch'))
        viz.text(str(y.numpy()), win='label', opts=dict(title='batch-y'))

        time.sleep(10)

        break
if __name__ == '__main__':
    main()

 

# model.py

import torch.nn as nn
import torch
import torch.nn.functional as F


class GoogLeNet(nn.Module):
    def __init__(self, num_classes=5, aux_logits=False, init_weights=False):
        super(GoogLeNet, self).__init__()
        self.aux_logits = aux_logits

        self.conv1 = BasicConv2d(3, 64, kernel_size=7, stride=2, padding=3)
        self.maxpool1 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.conv2 = BasicConv2d(64, 64, kernel_size=1)
        self.conv3 = BasicConv2d(64, 192, kernel_size=3, padding=1)
        self.maxpool2 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)
        self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)
        self.maxpool3 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64)
        self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64)
        self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64)
        self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64)
        self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128)
        self.maxpool4 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128)
        self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128)

        if self.aux_logits:
            self.aux1 = InceptionAux(512, num_classes)
            self.aux2 = InceptionAux(528, num_classes)

        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.dropout = nn.Dropout(0.4)
        self.fc = nn.Linear(1024, num_classes)
        if init_weights:
            self._initialize_weights()

    def forward(self, x):
        # N x 3 x 224 x 224
        x = self.conv1(x)
        # N x 64 x 112 x 112
        x = self.maxpool1(x)
        # N x 64 x 56 x 56
        x = self.conv2(x)
        # N x 64 x 56 x 56
        x = self.conv3(x)
        # N x 192 x 56 x 56
        x = self.maxpool2(x)

        # N x 192 x 28 x 28
        x = self.inception3a(x)
        # N x 256 x 28 x 28
        x = self.inception3b(x)
        # N x 480 x 28 x 28
        x = self.maxpool3(x)
        # N x 480 x 14 x 14
        x = self.inception4a(x)
        # N x 512 x 14 x 14
        if self.training and self.aux_logits:  # eval model lose this layer
            aux1 = self.aux1(x)

        x = self.inception4b(x)
        # N x 512 x 14 x 14
        x = self.inception4c(x)
        # N x 512 x 14 x 14
        x = self.inception4d(x)
        # N x 528 x 14 x 14
        if self.training and self.aux_logits:  # eval model lose this layer
            aux2 = self.aux2(x)

        x = self.inception4e(x)
        # N x 832 x 14 x 14
        x = self.maxpool4(x)
        # N x 832 x 7 x 7
        x = self.inception5a(x)
        # N x 832 x 7 x 7
        x = self.inception5b(x)
        # N x 1024 x 7 x 7

        x = self.avgpool(x)
        # N x 1024 x 1 x 1
        x = torch.flatten(x, 1)
        # N x 1024
        x = self.dropout(x)
        x = self.fc(x)
        # N x 5 (num_classes)
        if self.training and self.aux_logits:  # eval model lose this layer
            return x, aux2, aux1
        return x

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)


# inception结构
class Inception(nn.Module):
    def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj):
        super(Inception, self).__init__()

        self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)

        self.branch2 = nn.Sequential(
            BasicConv2d(in_channels, ch3x3red, kernel_size=1),
            BasicConv2d(ch3x3red, ch3x3, kernel_size=3, padding=1)  # 保证输出大小等于输入大小
        )

        self.branch3 = nn.Sequential(
            BasicConv2d(in_channels, ch5x5red, kernel_size=1),
            BasicConv2d(ch5x5red, ch5x5, kernel_size=5, padding=2)  # 保证输出大小等于输入大小
        )

        self.branch4 = nn.Sequential(
            nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
            BasicConv2d(in_channels, pool_proj, kernel_size=1)
        )

    def forward(self, x):
        branch1 = self.branch1(x)
        branch2 = self.branch2(x)
        branch3 = self.branch3(x)
        branch4 = self.branch4(x)

        outputs = [branch1, branch2, branch3, branch4]
        return torch.cat(outputs, 1)


# 辅助分类器
class InceptionAux(nn.Module):
    def __init__(self, in_channels, num_classes):
        super(InceptionAux, self).__init__()
        self.averagePool = nn.AvgPool2d(kernel_size=5, stride=3)
        self.conv = BasicConv2d(in_channels, 128, kernel_size=1)  # output[batch, 128, 4, 4]

        self.fc1 = nn.Linear(2048, 1024)
        self.fc2 = nn.Linear(1024, 512)
        self.fc3 = nn.Linear(512, 256)
        self.fc4 = nn.Linear(256, 64)
        self.fc5 = nn.Linear(64, num_classes)

    def forward(self, x):
        # aux1: N x 512 x 14 x 14, aux2: N x 528 x 14 x 14
        x = self.averagePool(x)
        # aux1: N x 512 x 4 x 4, aux2: N x 528 x 4 x 4
        x = self.conv(x)
        # N x 128 x 4 x 4
        x = torch.flatten(x, 1)
        x = F.dropout(x, 0.5, training=self.training)
        # N x 2048
        x = F.relu(self.fc1(x), inplace=True)
        x = F.dropout(x, 0.5, training=self.training)
        # N x 1024
        x = F.relu(self.fc2(x))
        x = F.dropout(x, 0.5, training=self.training)
        # N x 512
        x = F.relu(self.fc3(x))
        x = F.dropout(x, 0.5, training=self.training)
        # N x 256
        x = F.relu(self.fc4(x))
        x = F.dropout(x, 0.5, training=self.training)
        # N x 64
        x = self.fc5(x)
        x = F.dropout(x, 0.5, training=self.training)
        # N x num_classes
        return x


class BasicConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, **kwargs):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, **kwargs)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.conv(x)
        x = self.relu(x)
        return x


def main():
    device = torch.device("cuda")
    blk = GoogLeNet(3)
    blk = blk.to(device)
    tmp = torch.randn(64, 3, 224, 224)
    tmp = tmp.to(device)
    out = blk(tmp)
    print('GoogLeNet:', out.shape)


if __name__ == '__main__':
    main()

运行结果:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值