卷积神经网络架构-对吗?

时间:2020-04-30 10:15:39

标签: python architecture conv-neural-network valueerror

我正在尝试训练卷积神经网络。因此,我使用的是646个图像/牌照的数据集,其中包含8个字符(0-9,A-Z;不带字母“ O”和空格,总共36个可能的字符)。这些是我的训练数据X_train。它们的形状是带有颜色代码3的(646, 40, 200, 3)。我将它们调整为相同的形状。

我还有一个数据集,其中包含该图像的标签,我将其热编码为形状为(646, 8, 36)的numpy数组。这些数据是我的y_train数据。

现在,我正在尝试应用如下所示的神经网络: Architecture 该体系结构摘自本文:https://ieeexplore.ieee.org/abstract/document/8078501

我排除了批处理规范化部分,因为这对我来说不是最有趣的部分。但是我不确定该层的顶层。这意味着从model.add(Flatten())开始的最后一个池化层之后的部分...

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape = (40, 200, 3), activation = "relu"))
model.add(Conv2D(32, kernel_size=(3, 3), activation = "relu"))
model.add(Conv2D(32, kernel_size=(3, 3), activation = "relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), activation = "relu"))
model.add(Conv2D(64, kernel_size=(3, 3), activation = "relu"))
model.add(Conv2D(64, kernel_size=(3, 3), activation = "relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=(3, 3), activation = "relu"))
model.add(Conv2D(128, kernel_size=(3, 3), activation = "relu"))
model.add(Conv2D(128, kernel_size=(3, 3), activation = "relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(16000, activation = "relu"))
model.add(Dense(128, activation = "relu"))
model.add(Dense(36, activation = "relu"))
model.add(Dense(8*36, activation="Softmax"))
model.add(keras.layers.Reshape((8, 36)))

非常感谢您!

1 个答案:

答案 0 :(得分:3)

假设下面的图像与您的模型体系结构匹配,则可以使用代码创建模型。确保您对输入图像有一些填充。

Model Architecture

import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate

def create_model(input_shape = (40, 200, 3)):
    input_img = Input(shape=input_shape)
    model = Conv2D(32, kernel_size=(3, 3), input_shape = (40, 200, 3), activation = "relu")(input_img)
    model = Conv2D(32, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = Conv2D(32, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = MaxPooling2D(pool_size=(2, 2))(model)
    model = Conv2D(64, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = Conv2D(64, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = Conv2D(64, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = MaxPooling2D(pool_size=(2, 2))(model)
    model = Conv2D(128, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = Conv2D(128, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = Conv2D(128, kernel_size=(3, 3), padding="same", activation = "relu")(model)
    model = MaxPooling2D(pool_size=(2, 2))(model)
    backbone = Flatten()(model)

    branches = []
    for i in range(8):
        branches.append(backbone)
        branches[i] = Dense(16000, activation = "relu", name="branch_"+str(i)+"_Dense_16000")(branches[i])
        branches[i] = Dense(128, activation = "relu", name="branch_"+str(i)+"_Dense_128")(branches[i])
        branches[i] = Dense(36, activation = "softmax", name="branch_"+str(i)+"_output")(branches[i])
    
    output = Concatenate(axis=1)(branches)
    output = Reshape((8, 36))(output)
    model = Model(input_img, output)

    return model