gpt4 book ai didi

python - 检查输入 : expected input_19 to have 4 dimensions, 但获得形状为 (1190, 200, 200) 的数组时出错

转载 作者:行者123 更新时间:2023-12-01 09:33:34 25 4
gpt4 key购买 nike

我是 CNN 新手,无法确定如何解决这个问题。在此代码中,我正在训练一组图像以从卷积网络获取掩模。图像是形状为 (200,200) 的灰度图像。我无法确定我在哪里犯了错误。而且每次我运行代码时,不同的输入都会出现错误。任何帮助将不胜感激。

以下是生成的日志:

Creating training images...
Saving to .npy files done.
Creating test images...
Saving to .npy files done.
------------------------------
Loading and preprocessing train data...
------------------------------
------------------------------
Creating and compiling model...
------------------------------
C:/Users/Asus/Desktop/training.py:101: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(25, (3, 3), activation="relu", padding="same", data_format="channels_last")`
conv2 = Conv2D(25, (3, 3), activation='relu', padding='same',dim_ordering="th")(inputs)
C:/Users/Asus/Desktop/training.py:102: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(25, (3, 3), activation="relu", padding="same", data_format="channels_first")`
conv2 = Conv2D(25, (3, 3), activation='relu', padding='same',dim_ordering="th")(conv2)
C:/Users/Asus/Desktop/training.py:103: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(pool_size=(2, 2), data_format="channels_last")`
pool2 = MaxPooling2D(pool_size=(2, 2), dim_ordering="tf")(conv2)
C:/Users/Asus/Desktop/training.py:105: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(50, (3, 3), activation="relu", padding="same", data_format="channels_first")`
conv3 = Conv2D(50, (3, 3), activation='relu', padding='same',dim_ordering="th")(pool2)
C:/Users/Asus/Desktop/training.py:106: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(50, (3, 3), activation="relu", padding="same", data_format="channels_first")`
conv3 = Conv2D(50, (3, 3), activation='relu', padding='same',dim_ordering="th")(conv3)
C:/Users/Asus/Desktop/training.py:107: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(pool_size=(2, 2), data_format="channels_last")`
pool3 = MaxPooling2D(pool_size=(2, 2),dim_ordering="tf")(conv3)
C:/Users/Asus/Desktop/training.py:109: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(100, (3, 3), activation="relu", padding="same", data_format="channels_first")`
conv4 = Conv2D(100, (3, 3), activation='relu', padding='same',dim_ordering="th")(pool3)
C:/Users/Asus/Desktop/training.py:110: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(100, (3, 3), activation="relu", padding="same", data_format="channels_first")`
conv4 = Conv2D(100, (3, 3), activation='relu', padding='same',dim_ordering="th")(conv4)
C:/Users/Asus/Desktop/training.py:111: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(pool_size=(2, 2), data_format="channels_last")`
pool4 = MaxPooling2D(pool_size=(2, 2), dim_ordering="tf")(conv4)
C:/Users/Asus/Desktop/training.py:113: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(200, (3, 3), activation="relu", padding="same", data_format="channels_first")`
conv5 = Conv2D(200, (3, 3), activation='relu', padding='same',dim_ordering="th")(pool4)
C:/Users/Asus/Desktop/training.py:114: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(200, (3, 3), activation="relu", padding="same", data_format="channels_first")`
conv5 = Conv2D(200, (3, 3), activation='relu', padding='same',dim_ordering="th")(conv5)
C:/Users/Asus/Desktop/training.py:116: UserWarning: Update your `Conv2DTranspose` call to the Keras 2 API: `Conv2DTranspose(200, (2, 2), strides=(2, 2), padding="same", data_format="channels_first")`
up6 = concatenate([Conv2DTranspose(200, (2, 2), strides=(2, 2), padding='same',dim_ordering="th")(conv5), conv4], axis=3)
Traceback (most recent call last):

File "<ipython-input-25-4b34507d9da0>", line 1, in <module>
runfile('C:/Users/Asus/Desktop/training.py', wdir='C:/Users/Asus/Desktop')

File "C:\Users\Asus\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\Asus\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Asus/Desktop/training.py", line 205, in <module>
train_and_predict()

File "C:/Users/Asus/Desktop/training.py", line 163, in train_and_predict
model = get_unet()

File "C:/Users/Asus/Desktop/training.py", line 116, in get_unet
up6 = concatenate([Conv2DTranspose(200, (2, 2), strides=(2, 2), padding='same',dim_ordering="th")(conv5), conv4], axis=3)

File "C:\Users\Asus\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\keras\layers\merge.py", line 641, in concatenate
return Concatenate(axis=axis, **kwargs)(inputs)

File "C:\Users\Asus\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 594, in __call__
self.build(input_shapes)

File "C:\Users\Asus\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\keras\layers\merge.py", line 354, in build
'Got inputs shapes: %s' % (input_shape))

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 200, 50, 50), (None, 100, 50, 25)]

这是我的代码:

#load dataset
import h5py
h5f = h5py.File('liver_augmented_dataset.h5', 'r')
X = h5f['ct_scans'][:]
Y = h5f['seg_mask'][:]
h5f.close()

X_ax = X[1310:2500]
Y_ax = Y[1310:2500]

X_t=X[2501:2619]
Y_t=Y[2501:2619]

image_rows = 200
image_cols = 200


def get_unet():
inputs = Input(shape=(img_rows, img_cols,1))
# conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
# conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
# pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(25, (3, 3), activation='relu', padding='same',dim_ordering="tf")(inputs)
conv2 = Conv2D(25, (3, 3), activation='relu', padding='same',dim_ordering="tf")(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2), dim_ordering="tf")(conv2)

conv3 = Conv2D(50, (3, 3), activation='relu', padding='same',dim_ordering="tf")(pool2)
conv3 = Conv2D(50, (3, 3), activation='relu', padding='same',dim_ordering="tf")(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2),dim_ordering="tf")(conv3)

conv4 = Conv2D(100, (3, 3), activation='relu', padding='same',dim_ordering="tf")(pool3)
conv4 = Conv2D(100, (3, 3), activation='relu', padding='same',dim_ordering="tf")(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2), dim_ordering="tf")(conv4)

conv5 = Conv2D(200, (3, 3), activation='relu', padding='same',dim_ordering="tf")(pool4)
conv5 = Conv2D(200, (3, 3), activation='relu', padding='same',dim_ordering="tf")(conv5)

up6 = concatenate([Conv2DTranspose(200, (2, 2), strides=(2, 2), padding='same',dim_ordering="tf")(conv5), conv4], axis=3)
conv6 = Conv2D(100, (3, 3), activation='relu', padding='same',dim_ordering="tf")(up6)
conv6 = Conv2D(100, (3, 3), activation='relu', padding='same',dim_ordering="tf")(conv6)

up7 = concatenate([Conv2DTranspose(100, (2, 2), strides=(2, 2), padding='same',dim_ordering="tf")(conv6), conv3], axis=3)
conv7 = Conv2D(50, (3, 3), activation='relu', padding='same',dim_ordering="tf")(up7)
conv7 = Conv2D(50, (3, 3), activation='relu', padding='same',dim_ordering="tf")(conv7)

up8 = concatenate([Conv2DTranspose(50, (2, 2), strides=(2, 2), padding='same',dim_ordering="tf")(conv7), conv2], axis=3)
conv8 = Conv2D(25, (3, 3), activation='relu', padding='same',dim_ordering="tf")(up8)
conv8 = Conv2D(25, (3, 3), activation='relu', padding='same',dim_ordering="tf")(conv8)
#
# up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
# conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
# conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)

conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv8)

model = Model(inputs=[inputs], outputs=[conv10])

model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])

return model

最佳答案

我能够成功编译模型。我无法重新创建日志中提到的连接错误。

您应该检查的另一个是您提供给模型的输入应该在 4 维上进行 reshape ,就像您提到的 (1190, 200, 200) 的 reshape 错误问题一样,您应该将其转换为 (1190, 200, 200, 1) “1”代表频段数。

所以基本上你应该向灰度图像添加一个额外的维度并将其转换为 (img_rows,img_cols,bands)

关于python - 检查输入 : expected input_19 to have 4 dimensions, 但获得形状为 (1190, 200, 200) 的数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49738758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com