我正在尝试使用 CNN 使用 keras 对 python 中的医学图像进行分类。这些医学图像还包括可以影响模型决策的文本信息,例如年龄和性别。如何训练一个可以同时使用图像和现实世界信息进行训练的 CNN,以便它可以作为两者的分类基础?
我能想到几种可能性,但最简单的是用 CNN 从医学图像中提取一些特征,然后压平 CNN 的结果,并连接非图像数据。假设您有 512x512 图像和 10 个类,这是一个想法。这是函数式 API,允许您有多个输入。
import tensorflow as tf
import numpy as np
num_classes = 10
H,W = 512, 512
# Define inputs with their shapes
imgs = tf.keras.Input((H,W,3), dtype = tf.float32)
genders = tf.keras.Input(1, dtype = tf.float32)
ages = tf.keras.Input(1, dtype = tf.float32)
# Extract image features
features = tf.keras.layers.Conv2D(64, 4, strides = 4, activation = 'relu')(imgs)
features = tf.keras.layers.MaxPooling2D()(features)
features = tf.keras.layers.Conv2D(128,3, strides = 2, activation = 'relu')(features)
features = tf.keras.layers.MaxPooling2D()(features)
features = tf.keras.layers.Conv2D(256, 3, strides = 2, activation = 'relu')(features)
features = tf.keras.layers.Conv2D(512, 3, strides = 2, activation = 'relu')(features)
# #Flatten output
flat_features = tf.keras.layers.Flatten()(features)
#Concatenate gender and age
flat_features = tf.concat([flat_features, genders, ages], -1)
# Downsample
xx = tf.keras.layers.Dense(2048, activation = 'relu')(flat_features)
xx = tf.keras.layers.Dense(1024, activation = 'relu')(xx)
xx = tf.keras.layers.Dense(512, activation = 'relu')(xx)
#Calculate probabilities for each class
logits = tf.keras.layers.Dense(num_classes)(xx)
probs = tf.keras.layers.Softmax()(logits)
model = tf.keras.Model(inputs = [imgs, genders, ages], outputs = probs)
model.summary()
此架构并不是特别标准,您可能希望使解码器更深和/或减少 CNN 编码器中的参数数量。