卷积神经网络架构 - 正确吗? [英] Convolutional Neural Net Architecture - correct?

查看:34
本文介绍了卷积神经网络架构 - 正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在,我正在尝试应用如下所示的神经网络:架构取自这篇论文:

将tensorflow导入为tf从 tensorflow.keras.models 导入顺序,模型从 tensorflow.keras.layers 导入 Conv2D、Flatten、MaxPooling2D、Dense、Input、Reshape、Concatenatedef create_model(input_shape = (40, 200, 3)):input_img = 输入(形状=输入形状)模型 = Conv2D(32, kernel_size=(3, 3), input_shape = (40, 200, 3), activation = "relu")(input_img)模型 = Conv2D(32, kernel_size=(3, 3), padding=same", activation = relu")(model)模型 = Conv2D(32, kernel_size=(3, 3), padding=same", activation = relu")(model)模型 = MaxPooling2D(pool_size=(2, 2))(model)模型 = Conv2D(64, kernel_size=(3, 3), padding=same",activation = relu")(model)模型 = Conv2D(64, kernel_size=(3, 3), padding=same",activation = relu")(model)模型 = Conv2D(64, kernel_size=(3, 3), padding=same",activation = relu")(model)模型 = MaxPooling2D(pool_size=(2, 2))(model)模型 = Conv2D(128, kernel_size=(3, 3), padding=same",activation = relu")(model)模型 = Conv2D(128, kernel_size=(3, 3), padding=same",activation = relu")(model)模型 = Conv2D(128, kernel_size=(3, 3), padding=same",activation = relu")(model)模型 = MaxPooling2D(pool_size=(2, 2))(model)骨干=展平()(模型)分支 = []对于范围内的 i (8):分支.附加(主干)branch[i] = Dense(16000, activation = "relu", name="branch_"+str(i)+"_Dense_16000")(branches[i])branch[i] = Dense(128, activation = "relu", name="branch_"+str(i)+"_Dense_128")(branches[i])branch[i] = Dense(36, activation = "softmax", name="branch_"+str(i)+"_output")(branches[i])输出 = 连接(轴 = 1)(分支)输出 = 重塑((8, 36))(输出)模型 = 模型(输入图像,输出)回报模式

I am trying to train a convolutional neural net. Therefore I am using a datset of 646 images/license plates which contains 8 characters (0-9, A-Z; without letter 'O' and blank spaces, in total 36 possible characters). These are my training data X_train. Their shape is (646, 40, 200, 3) with color code 3. I resized them to the same shape.

I also have a dataset which contains the labels of this images, which I one-hot-encoded to a numpy array of shape (646, 8, 36). This data is my y_train data.

Now, I am trying to apply a Neural Network which looks like this: The architecture is taken from this paper: https://ieeexplore.ieee.org/abstract/document/8078501

I excluded the batch normalization part, because this part is not the most interesting one for me. But I am very unsure regarding the top of the layer. That means the part after the last pooling layer beginning with 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)))

Thank you very much in advance!

解决方案

Assuming the image below matches your model architecture, the code can be used to create the model. Ensure you have some padding for the input images.

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

这篇关于卷积神经网络架构 - 正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆