简体   繁体   English

为 CNN 图像分类整合数值/物理数据

[英]Integrating numerical/physical data for CNN image classification

I am attempting to use a CNN to classify medical images in python using keras.我正在尝试使用 CNN 使用 keras 对 python 中的医学图像进行分类。 These medical images also include textual information such as age and gender that can influence the decision of the model.这些医学图像还包括可能影响 model 决策的文本信息,例如年龄和性别。 How can I train a CNN that can train using both the images and the real world information so that it can make classifications base of both.我如何训练一个可以同时使用图像和真实世界信息进行训练的 CNN,以便它可以作为两者的分类基础。

There are a couple of possibilities that I can think of off the type of my head, but the simplest is to extract some features from the medical images with a CNN, then flatten the result of the CNN, and concatenate the non-image data.根据我的脑袋类型,我可以想到几种可能性,但最简单的方法是使用 CNN 从医学图像中提取一些特征,然后将 CNN 的结果展平,并将非图像数据连接起来。 Here is an idea supposing you have 512x512 images and 10 classes.这是一个假设您有 512x512 图像和 10 个类的想法。 This is the functional API which allows you to have multiple inputs.这是功能 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()

This architecture is not especially standard, and you might want to make the decoder deeper and/or decrease the number of parameters in the CNN encoder.这种架构不是特别标准,您可能希望使解码器更深和/或减少 CNN 编码器中的参数数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM