【深度学习-图像识别】脑PET图像分析和疾病预测
比赛简介:Baseline:1. 读取数据2. 定义数据加载器:3. 构建模型:4. 训练模型:5. 生成预测结果:
最终提交结果:有需求的大佬欢迎加入我的接单群,需求详情请群里戳群主获取数据集:
这里提供一个baseline;
比赛简介:
比赛地址:http://challenge.xfyun.cn/topic/info?type=PET
数据长这个样子:
Baseline:
1. 读取数据
base
= './train'
count
= 0
with open(os
.path
.join
('train_list.txt'), 'w') as f
:
for i
, cls_fold
in enumerate(['AD', 'CN']):
try:
cls_base
= os
.path
.join
(base
, cls_fold
)
files
= os
.listdir
(cls_base
)
print('{} train num:'.format(cls_fold
), len(files
))
for pt
in files
:
img
= os
.path
.join
(cls_fold
, pt
)
info
= img
+ ' ' + str(i
) + '\n'
f
.write
(info
)
count
+= 1
except Exception
as e
:
continue
print(count
)
with open(os
.path
.join
('labels.txt'), 'w') as f
:
for i
, cls_fold
in enumerate(['AD', 'CN']):
f
.write
(cls_fold
+'\n')
print('done.')
2. 定义数据加载器:
import torch
import torch
.nn
as nn
import torch
.optim
as optim
from torch
.optim
import lr_scheduler
from torch
.utils
.data
import DataLoader
import torchvision
from torchvision
.transforms
import transforms
from torchvision
import models
from torchvision
.models
import ResNet
import numpy
as np
import matplotlib
.pyplot
as plt
import os
data_dir
= './'
train_dataset
= torchvision
.datasets
.ImageFolder
(root
=os
.path
.join
(data_dir
, 'train'),
transform
=transforms
.Compose
(
[
transforms
.Resize
(299),
transforms
.ToTensor
(),
transforms
.Normalize
(
mean
=(0.485, 0.456, 0.406),
std
=(0.229, 0.224, 0.225))
]))
val_dataset
= torchvision
.datasets
.ImageFolder
(root
=os
.path
.join
(data_dir
, 'train'),
transform
=transforms
.Compose
(
[
transforms
.Resize
(299),
transforms
.ToTensor
(),
transforms
.Normalize
(
mean
=(0.485, 0.456, 0.406),
std
=(0.229, 0.224, 0.225))
]))
train_dataloader
= DataLoader
(dataset
=train_dataset
, batch_size
=64, shuffle
=True)
val_dataloader
= DataLoader
(dataset
=val_dataset
, batch_size
=1, shuffle
=True)
class_names
= train_dataset
.classes
print('class_names:{}'.format(class_names
))
device
= torch
.device
('cuda:0' if torch
.cuda
.is_available
() else 'cpu')
print('trian_device:{}'.format(device
.type))
3. 构建模型:
这里使用vgg16:
model_name
= 'vgg16'
model
= models
.vgg16
(pretrained
=True)
num_classes
= 2
model
.classifier
[6] = nn
.Linear
(4096,num_classes
)
model
= model
.to
(device
)
loss_fc
= nn
.CrossEntropyLoss
()
optimizer
= optim
.Adam
(model
.parameters
(), lr
=2e-4)
exp_lr_scheduler
= lr_scheduler
.StepLR
(optimizer
=optimizer
, step_size
=30, gamma
=0.5)
print('done.')
4. 训练模型:
num_epochs
= 101
for epoch
in range(num_epochs
):
running_loss
= 0.0
exp_lr_scheduler
.step
()
for i
, sample_batch
in enumerate(train_dataloader
):
inputs
= sample_batch
[0]
labels
= sample_batch
[1]
model
.train
()
inputs
= inputs
.to
(device
)
labels
= labels
.to
(device
)
optimizer
.zero_grad
()
outputs
= model
(inputs
)
loss
= loss_fc
(outputs
, labels
)
loss
.backward
()
optimizer
.step
()
running_loss
+= loss
.item
()
if epoch
% 5 == 0 and i
== 1:
correct
= 0
total
= 0
model
.eval()
for images_test
, labels_test
in val_dataloader
:
images_test
= images_test
.to
(device
)
labels_test
= labels_test
.to
(device
)
outputs_test
= model
(images_test
)
_
, prediction
= torch
.max(outputs_test
, 1)
correct
+= (torch
.sum((prediction
== labels_test
))).item
()
total
+= labels_test
.size
(0)
print('[{}, {}] running_loss = {:.5f} accurcay = {:.5f}'.format(epoch
+ 1, i
+ 1, running_loss
/ 20,
correct
/ total
))
running_loss
= 0.0
print('training finish !')
torch
.save
(model
.state_dict
(), './model.pth')
5. 生成预测结果:
test_base
= './test/AD&CN'
import pandas
as pd
import cv2
from PIL
import Image
from torchvision
import transforms
from torch
.autograd
import Variable
as V
import torch
as t
import pickle
as pkl
trans
=transforms
.Compose
(
[
transforms
.Resize
(299),
transforms
.ToTensor
(),
transforms
.Normalize
(
mean
=(0.485, 0.456, 0.406),
std
=(0.229, 0.224, 0.225))
]
)
model
.eval()
submission
= {'uuid':[], 'label':[]}
for i
in tqdm
(range(1, 1000+1)):
im_pt
= os
.path
.join
(test_base
, '{}.png'.format(i
))
img
= cv2
.imread
(im_pt
)
img
= cv2
.cvtColor
(img
, cv2
.COLOR_BGR2RGB
)
img
= Image
.fromarray
(img
)
img
= trans
(img
)
img
= img
.unsqueeze
(0)
img
= V
(img
.cuda
())
predict
= model
(img
)
predict
= F
.softmax
(predict
)[0]
temp
= {'AD':float(predict
[0]), 'CN':float(predict
[1]), 'id':i
}
_
, pred
= torch
.max(predict
, 0)
pred
= class_names
[int(pred
)]
submission
['uuid'].append
(i
)
submission
['label'].append
(pred
)
data
= pd
.DataFrame
(submission
)
data
.to_csv
('./submission_{}.csv'.format(model_name
), index
=False)
print('done.')
最终提交结果:
大概 0.71 左右;
有需求的大佬欢迎加入我的接单群,需求详情请群里戳群主
获取数据集:
关注我的公众号——可达鸭的深度学习教程,公众号内回复:“脑PET图像” 获取数据集: