U-Net医学图像分割实战
时间: 2023-07-08 09:08:02 浏览: 178
U-Net是一种用于医学图像分割的深度学习模型,它在2015年由Olaf Ronneberger等人提出。U-Net的结构类似于一个U形,因此得名,它基于卷积神经网络(CNN)的思想,使用反卷积层实现了图像的上采样,在这方面比其他图像分割模型更具优势。
下面是U-Net模型的结构:
![U-Net模型](https://www.jeremyjordan.me/content/images/2018/05/u-net-architecture.png)
U-Net模型分为两个部分:编码器和解码器。编码器部分由卷积层和最大池化层组成,在特征提取的同时缩小输入图像的大小。解码器部分由反卷积层和卷积层组成,将特征图像上采样到原始大小,并输出分割结果。
为了更好地理解U-Net模型,我们可以通过一个医学图像分割的实战来进一步学习。
## 实战:使用U-Net进行肝脏图像分割
### 数据集
我们使用了一个公共的医学图像分割数据集,名为MICCAI 2017 Liver Tumor Segmentation (LiTS) Challenge Data。该数据集包含131个肝脏CT图像,每个图像的大小为512x512,以及相应的肝脏和肝癌分割结果。
数据集可以从以下网址下载:https://competitions.codalab.org/competitions/17094
### 环境配置
- Python 3.6
- TensorFlow 1.14
- keras 2.2.4
### 数据预处理
在训练U-Net模型之前,我们需要对数据进行预处理。这里我们使用了一些常见的数据增强技术,包括旋转、翻转、缩放和随机裁剪等。
```python
import numpy as np
import cv2
import os
def data_augmentation(image, label):
if np.random.random() < 0.5:
# rotate image and label
angle = np.random.randint(-10, 10)
rows, cols = image.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1)
image = cv2.warpAffine(image, M, (cols, rows))
label = cv2.warpAffine(label, M, (cols, rows))
if np.random.random() < 0.5:
# flip image and label
image = cv2.flip(image, 1)
label = cv2.flip(label, 1)
if np.random.random() < 0.5:
# scale image and label
scale = np.random.uniform(0.8, 1.2)
rows, cols = image.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), 0, scale)
image = cv2.warpAffine(image, M, (cols, rows), borderMode=cv2.BORDER_REFLECT)
label = cv2.warpAffine(label, M, (cols, rows), borderMode=cv2.BORDER_REFLECT)
if np.random.random() < 0.5:
# crop image and label
rows, cols = image.shape[:2]
x = np.random.randint(0, rows - 256)
y = np.random.randint(0, cols - 256)
image = image[x:x+256, y:y+256]
label = label[x:x+256, y:y+256]
return image, label
def preprocess_data(image_path, label_path):
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE).astype(np.float32)
label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE).astype(np.float32)
# normalize image
image = (image - np.mean(image)) / np.std(image)
# resize image and label
image = cv2.resize(image, (256, 256))
label = cv2.resize(label, (256, 256))
# perform data augmentation
image, label = data_augmentation(image, label)
# convert label to binary mask
label[label > 0] = 1
return image, label
```
### 构建U-Net模型
我们使用了Keras来构建U-Net模型,代码如下:
```python
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, Dropout, UpSampling2D, concatenate
def unet(input_size=(256, 256, 1)):
inputs = Input(input_size)
# encoder
conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(inputs)
conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool1)
conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool2)
conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool3)
conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv4)
drop4 = Dropout(0.5)(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
# decoder
up5 = UpSampling2D(size=(2, 2))(pool4)
up5 = Conv2D(512, 2, activation='relu', padding='same', kernel_initializer='he_normal')(up5)
merge5 = concatenate([drop4, up5], axis=3)
conv5 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge5)
conv5 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv5)
up6 = UpSampling2D(size=(2, 2))(conv5)
up6 = Conv2D(256, 2, activation='relu', padding='same', kernel_initializer='he_normal')(up6)
merge6 = concatenate([conv3, up6], axis=3)
conv6 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge6)
conv6 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv6)
up7 = UpSampling2D(size=(2, 2))(conv6)
up7 = Conv2D(128, 2, activation='relu', padding='same', kernel_initializer='he_normal')(up7)
merge7 = concatenate([conv2, up7], axis=3)
conv7 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge7)
conv7 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv7)
up8 = UpSampling2D(size=(2, 2))(conv7)
up8 = Conv2D(64, 2, activation='relu', padding='same', kernel_initializer='he_normal')(up8)
merge8 = concatenate([conv1, up8], axis=3)
conv8 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge8)
conv8 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv8)
outputs = Conv2D(1, 1, activation='sigmoid')(conv8)
model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
```
### 训练模型
我们将数据集分为训练集和测试集,然后使用Keras的fit方法来训练模型。
```python
from keras.callbacks import ModelCheckpoint
# set paths
train_path = '/path/to/train'
test_path = '/path/to/test'
# get list of images and labels
train_images = sorted(os.listdir(os.path.join(train_path, 'images')))
train_labels = sorted(os.listdir(os.path.join(train_path, 'labels')))
test_images = sorted(os.listdir(os.path.join(test_path, 'images')))
test_labels = sorted(os.listdir(os.path.join(test_path, 'labels')))
# initialize model
model = unet()
# train model
checkpoint = ModelCheckpoint('model.h5', verbose=1, save_best_only=True)
model.fit_generator(generator(train_path, train_images, train_labels), steps_per_epoch=100, epochs=10, validation_data=generator(test_path, test_images, test_labels), validation_steps=50, callbacks=[checkpoint])
```
### 评估模型
训练完成后,我们需要对模型进行评估。这里我们使用了Dice系数和交并比(IoU)这两个常用的评估指标。
```python
def dice_coef(y_true, y_pred):
smooth = 1e-5
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def iou(y_true, y_pred):
smooth = 1e-5
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
union = K.sum(y_true_f) + K.sum(y_pred_f) - intersection
return (intersection + smooth) / (union + smooth)
model = load_model('model.h5', custom_objects={'dice_coef': dice_coef, 'iou': iou})
test_images = sorted(os.listdir(os.path.join(test_path, 'images')))
test_labels = sorted(os.listdir(os.path.join(test_path, 'labels')))
dice_coefficients = []
ious = []
for i in range(len(test_images)):
# preprocess image and label
image_path = os.path.join(test_path, 'images', test_images[i])
label_path = os.path.join(test_path, 'labels', test_labels[i])
image, label = preprocess_data(image_path, label_path)
# predict label
pred = model.predict(np.expand_dims(image, axis=0))[0]
# calculate dice coefficient and IoU
dice_coefficient = dice_coef(np.expand_dims(label, axis=0), np.expand_dims(pred, axis=0))
iou_ = iou(np.expand_dims(label, axis=0), np.expand_dims(pred, axis=0))
dice_coefficients.append(dice_coefficient)
ious.append(iou_)
# calculate average dice coefficient and IoU
print('Dice coefficient:', np.mean(dice_coefficients))
print('IoU:', np.mean(ious))
```
通过实战,我们可以更加深入地了解U-Net模型的原理和使用方法。
阅读全文