【问题标题】:Finding the appropriate CNN Model Architecture and Parameters找到合适的 CNN 模型架构和参数
【发布时间】:2020-05-31 10:11:10
【问题描述】:

我目前正在创建一个 CNN 模型,用于对字体是否为 ArialVerdanaTimes New RomanGeorgia 进行分类。总而言之,有16 类,因为我还考虑过检测字体是regularbolditalics 还是bold italics。所以4 fonts * 4 styles = 16 classes

我在训练中使用的数据如下:

 Training data set : 800 image patches of 256 * 256 dimension (50 for each class)
 Validation data set : 320 image patches of 256 * 256 dimension (20 for each class)
 Testing data set : 160 image patches of 256 * 256 dimension (10 for each class)

以下是我的数据的示例截图:

下面是我的初始代码:

import numpy as np
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense, Flatten
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import *
from matplotlib import pyplot as plt
import itertools
import matplotlib.pyplot as plt
import pickle


image_width = 256
image_height = 256

train_path = 'font_model_data/train'
valid_path =  'font_model_data/valid'
test_path = 'font_model_data/test'


train_batches = ImageDataGenerator().flow_from_directory(train_path, target_size=(image_width, image_height), classes=['1','2','3','4', '5', '6', '7', '8', '9', '10', '11', '12','13', '14', '15', '16'], batch_size = 16)
valid_batches = ImageDataGenerator().flow_from_directory(valid_path, target_size=(image_width, image_height), classes=['1','2','3','4', '5', '6', '7', '8', '9', '10', '11', '12','13', '14', '15', '16'], batch_size = 16)
test_batches = ImageDataGenerator().flow_from_directory(test_path, target_size=(image_width, image_height), classes=['1','2','3','4', '5', '6', '7', '8', '9', '10', '11', '12','13', '14', '15', '16'], batch_size = 160)


 imgs, labels = next(train_batches)

 #CNN model
 model = Sequential([
     Conv2D(32, (3,3), activation='relu', input_shape=(image_width, image_height, 3)),
     Flatten(),
     Dense(16, activation='softmax'),
 ])

 print(model.summary())

 model.compile(Adam(lr=.0001),loss='categorical_crossentropy', metrics=['accuracy'])
 model.fit_generator(train_batches, steps_per_epoch = 50, validation_data= valid_batches, validation_steps = 20, epochs = 1, verbose = 2)

 model_pickle = open('cnn_font_model.pickle', 'wb')
 pickle.dump(model, model_pickle)
 model_pickle.close()
 print('Training Done.')

 test_imgs, test_labels = next(test_batches)

 predictions = model.predict_generator(test_batches, steps = 160, verbose = 2)
 print(predictions)

谁能建议我如何知道正确的网络架构和参数以获得最佳精度?我应该如何开始调整我的网络?

【问题讨论】:

  • 你考虑过两个输出层吗?一种风格,一种字体?这种架构可能会将输出减少到 8 类。比较这两种方法会很有趣:)
  • @viceriel --> 谢谢,但我不知道如何编码我的训练标签。我正在使用 ImageDataGenerator,它会自动为我的训练数据创建 16 个类。我不知道如何调整它。

标签: machine-learning image-processing neural-network conv-neural-network


【解决方案1】:

在选择网络之前,您需要将图像块分割成带有字符的字幕并提供给以下架构...

# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
classifier.fit_generator(training_set,
                     steps_per_epoch = XXX,
                     epochs = XX,
                     validation_data = test_set,
                     validation_steps = XXX)
from keras.models import load_model
classifier.save('your_classifier.h5')

【讨论】:

  • “将图像块分割成字幕”是什么意思。将我的图像补丁分割成字符?
  • 这个答案不清楚。你为什么建议图像需要以某种方式分割?此外,您的答案不符合问题中的信息。例如,您的输入大小是64 x 64 x 3,而实际图像是256 x 256(我不知道颜色通道)。或者,您正在预测 2 个具有 sigmoid + 二元交叉熵的输出类,而 OP 声明它们有 16 个类。
  • 这个例子是为数字和非数字分类制作的,在你的情况下你可以添加softmax。为什么我更喜欢分割是在卷积和池化过程中,它只关注选择的字符。它适用于我的数字和非数字分类
猜你喜欢
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多