【问题标题】:Input shape for 3d-CNN in pythonpython中3d-CNN的输入形状
【发布时间】:2020-02-17 19:48:52
【问题描述】:

我想使用 conv3d 将 8 张图像同时输入到同一个 CNN 结构中。我的CNN模型如下:

def build(sample, frame, height, width, channels,  classes):
    model = Sequential()
    inputShape = (sample, frame, height, width, channels)
    chanDim = -1

    if K.image_data_format() == "channels_first":
        inputShape = (sample, frame, channels, height, width)
        chanDim = 1


    model.add(Conv3D(32, (3, 3, 3), padding="same", input_shape=inputShape))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), padding="same", data_format="channels_last"))
    model.add(Dropout(0.25))

    model.add(Conv3D(64, (3, 3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), padding="same", data_format="channels_last"))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128))    #(Dense(1024))
    model.add(Activation("relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    # softmax classifier
    model.add(Dense(classes))
    model.add(Activation("softmax")

模型的训练如下:

IMAGE_DIMS = (57, 8, 60, 60, 3) # since I have 460 images so 57 sample with 8 image each
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# binarize the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
# note: data is a list of all dataset images
(trainX, testX, trainY, testY) train_test_split(data, labels, test_size=0.2, random_state=42)                                                                                                          
aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest")

# initialize the model
model = CNN_Network.build(sample= IMAGE_DIMS[0], frame=IMAGE_DIMS[1],
                      height = IMAGE_DIMS[2], width=IMAGE_DIMS[3],
                      channels=IMAGE_DIMS[4], classes=len(lb.classes_))

opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer= opt, metrics=["accuracy"])

# train the network
model.fit_generator(
aug.flow(trainX, trainY, batch_size=BS),
validation_data=(testX, testY),
steps_per_epoch=len(trainX) // BS,
epochs=EPOCHS, verbose=1)

我对 input_shape 感到困惑,我知道 Conv3D 需要 5D 输入,输入是 4D 并从 keras 添加批量,但我有以下错误:

ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (92, 60, 60, 3)

任何人都可以帮我做什么吗? 92 结果是什么,我用 (57, 8, 60, 60, 3) 确定 input_shape。我的 input_shape 应该是什么,才能将 8 张彩色图像同时输入到同一个模型中。

【问题讨论】:

    标签: python keras deep-learning conv-neural-network


    【解决方案1】:

    在 Keras Python 3 中,输入形状可以如下:

    input_shape = (8, 64, 64, 1)
    

    地点:

    • 值 1 (8) 是帧数
    • 值 2 (64) 是宽度
    • 值 3 (64) 是高度
    • 值 4 (1) 是通道数

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多