我正在运行一个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]
编辑2:
我认为另一种选择是采用形状为
(780, 128, 128, 1)
的X,克隆780张图像中的每张,并附加两个作为假人。这可能吗?结果为(780, 128, 128, 3)
。 最佳答案
我们可以使用现有的keras图层将现有的图像形状转换为预训练模型的预期形状,而不是使用numpy复制通道。由于在训练之前复制通道可能会消耗3倍的内存,但是在运行时集成此处理将节省大量内存。
您可以按照这种方式进行。
步骤1:创建Keras模型,将您的输入图像转换为可以作为base_model的输入的形状,如下所示:
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)
步骤2:定义预先训练的模型InceptionV3,如下所示:
base_model = InceptionV3(input_tensor=input_model.output, weights='imagenet', include_top=False)
步骤3:按以下方式合并两个模型:
combined_model = Model(inputs=input_model.input, outputs=base_model.output)
这种方法的优势在于,keras模型本身将在运行时处理诸如通道复制之类的图像处理工作。因此,我们不需要使用numpy自己复制图像通道,并且结果将具有存储效率。