Keras input_tensor形状用于转移学习

时间:2018-06-10 14:33:29

标签: python machine-learning keras

我正在运行CNN,使用Keras对医疗扫描进行分类,并使用imagenet和InceptionV3进行转移学习。我正在使用大小为X_train = (624, 128, 128, 1)Y_train = (624, 2)的一些练习数据构建模型。

我正在尝试使用以下代码调整input_tensor的大小以适应我的图像形状(128 x 128 x 1)。

input_tensor = Input(shape=(128, 128, 1)) 
base_model = InceptionV3(input_tensor=input_tensor,weights='imagenet',include_top=False)

这样做我得到一个值错误:

ValueError: Dimension 0 in both shapes must be equal, but are 3 and 32. Shapes 
are [3,3,1,32] and [32,3,3,3]. for 'Assign_753' (op: 'Assign') with input 
shapes: [3,3,1,32], [32,3,3,3]

有没有办法让这个模型以他们的格式接受我的图像?

编辑: 值得一提的是,这是生成训练数据的代码。

X = []
Y = []
for subj, subj_slice in slices.items():
    # X.extend([s[:, :, np.newaxis, np.newaxis] for s in slice])
    subj_slice_norm = [((imageArray - np.min(imageArray)) / np.ptp(imageArray)) for imageArray in subj_slice]
    X.extend([s[ :, :, np.newaxis] for s in subj_slice_norm])
    subj_status = labels_df['deadstatus.event'][labels_df['PatientID'] == subj]
    subj_status = np.asanyarray(subj_status)
    #print(subj_status)
    Y.extend([subj_status] * len(subj_slice))

X = np.stack(X, axis=0)
Y = to_categorical(np.stack(Y, axis=0))]

n_samp_train = int(X.shape[0]*0.8)
X_train, Y_train = X[:n_samp_train], Y[:n_samp_train]

EDIT2: 我认为另一种选择是采用我的X形状(780, 128, 128, 1),克隆780个图像中的每个并附加两个作为假人。这可能吗?导致(780, 128, 128, 3)

1 个答案:

答案 0 :(得分:1)

我们可以使用现有的keras图层将现有图像形状转换为预训练模型的预期形状,而不是使用numpy复制通道。由于在训练之前复制通道可能会消耗3倍的内存,但是在运行时集成此处理将节省大量内存。

您可以按照这种方式进行。

步骤1 :创建Keras模型,将您输入的图像转换为可以作为base_model输入的形状,如下所示:

DŽDždžLJLjljNJNjnj|
         |

步骤2:如下定义预训练模型 InceptionV3

from keras.models import Model
from keras.layers import RepeatVector, Input, Reshape

inputs = Input(shape=(128, 128, 1))
reshaped1 = Reshape(target_shape=((128 * 128 * 1,)))(inputs)
repeated = RepeatVector(n=3)(reshaped1)
reshaped2 = Reshape(target_shape=(3, 128, 128))(repeated)
input_model = Model(inputs=inputs, outputs=reshaped2)

第3步:按如下方式合并两个模型:

base_model = InceptionV3(input_tensor=input_model.output, weights='imagenet', include_top=False)

此方法的优势在于,keras模型本身将在运行时处理诸如通道复制之类的图像处理工作。因此,我们无需使用numpy自己复制图像通道,并且结果将提高存储效率。