【发布时间】:2022-08-04 03:36:38
【问题描述】:
我是深度学习的初学者,正在尝试开发一个 U-Net 模型,用于在 nifti 图像上进行血管分割(血管(白色像素)、背景(黑色像素))。我对定义类的数量和 sigmoid/softmax 激活函数感到困惑。我应该为此设置 n_classes = 2 和 softmax 激活函数,还是设置 n_classes = 1 和 sigmoid 激活函数?下面是 DataGenerator 和 UNet 模型的代码。
n_classes = 2
class DataGenerator(tf.keras.utils.Sequence):
def __init__(self, img_paths, mask_paths, batch_size, n_classes):
self.x, self.y = img_paths, mask_paths
self.batch_size = batch_size
self.n_classes = n_classes
def __len__(self):
return math.ceil(len(self.x) / self.batch_size)
def read_nifti(self, filepath):
volume = nib.load(filepath).get_fdata()
volume = np.array(volume)
return volume
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
image = [self.read_nifti(image_file) for image_file in batch_x]
image = np.array(image, dtype=np.float32)
image = tf.expand_dims(image, axis=-1)
label = [self.read_nifti(mask_file) for mask_file in batch_y]
label = np.array(label, dtype=np.float32)
label = tf.keras.utils.to_categorical(label, num_classes=self.n_classes)
return image, label
\'\'\'---------------------build CNN model -------------------\'\'\'
def unet3d_model1(nx= 224, ny=224, nz=64):
inputs = Input((nx, ny, nz, 1))
conv1 = Conv3D(32, (3, 3, 3), activation=\'relu\', padding=\'same\')(inputs)
conv1 = Conv3D(32, (3, 3, 3), activation=\'relu\', padding=\'same\')(conv1)
pool1 = MaxPool3D(pool_size=(2, 2, 2))(conv1)
conv2 = Conv3D(64, (3, 3, 3), activation=\'relu\', padding=\'same\')(pool1)
conv2 = Conv3D(64, (3, 3, 3), activation=\'relu\', padding=\'same\')(conv2)
pool2 = MaxPool3D(pool_size=(2, 2, 2))(conv2)
conv3 = Conv3D(128, (3, 3, 3), activation=\'relu\', padding=\'same\')(pool2)
conv3 = Conv3D(128, (3, 3, 3), activation=\'relu\', padding=\'same\')(conv3)
pool3 = MaxPool3D(pool_size=(2, 2, 2))(conv3)
conv4 = Conv3D(256, (3, 3, 3), activation=\'relu\', padding=\'same\')(pool3)
conv4 = Conv3D(256, (3, 3, 3), activation=\'relu\', padding=\'same\')(conv4)
up5 = UpSampling3D(size=(2, 2, 2))(conv4)
merge5 = concatenate([up5, conv3])
conv5 = Conv3D(128, (3, 3, 3), activation=\'relu\', padding=\'same\')(merge5)
conv5 = Conv3D(128, (3, 3, 3), activation=\'relu\', padding=\'same\')(conv5)
up6 = UpSampling3D(size=(2, 2, 2))(conv5)
merge6 = concatenate([up6, conv2])
conv6 = Conv3D(64, (3, 3, 3), activation=\'relu\', padding=\'same\')(merge6)
conv6 = Conv3D(64, (3, 3, 3), activation=\'relu\', padding=\'same\')(conv6)
up7 = UpSampling3D(size=(2, 2, 2))(conv6)
merge7 = concatenate([up7, conv1])
conv7 = Conv3D(32, (3, 3, 3), activation=\'relu\', padding=\'same\')(merge7)
conv7 = Conv3D(32, (3, 3, 3), activation=\'relu\', padding=\'same\')(conv7)
conv8 = Conv3D(n_classes, (1, 1, 1), activation=\'softmax\')(conv7)
model = Model(inputs=inputs, outputs=conv8)
return model
标签: python keras deep-learning image-segmentation