【问题标题】:CNN model is giving wrong predictionsCNN 模型给出了错误的预测
【发布时间】:2018-06-12 10:47:42
【问题描述】:

我目前正在研究区域语言的手写数字识别。目前,我专注于奥里亚语。我通过 CNN 模型测试 MNIST 数据集,并尝试将模型应用于我的 Oriya 数据集。模型表现不佳。它给出了错误的预测。我有一个包含 4971 个样本的数据集。
如何提高准确率?
这是我的代码:

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten  
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop,adam
from keras.utils import np_utils

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import os
import theano
from PIL import Image
from numpy import *
# SKLEARN
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split



# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)



# input image dimensions
img_rows, img_cols = 28, 28

# number of channels
img_channels = 1


path2 = '/home/saumya/Desktop/Oriya/p'    #path of folder of images


imlist = os.listdir(path2)


im1 = array(Image.open('/home/saumya/Desktop/Oriya/p' + '/'+ imlist[0]))    # open one image to get size
m,n = im1.shape[0:2] # get the size of the images
imnbr = len(imlist) # get the number of images


# create matrix to store all flattened images
immatrix = array([array(Image.open('/home/saumya/Desktop/Oriya/p' + '/'+  im2)).flatten()
           for im2 in imlist],'f')

label=np.ones((num_samples,),dtype = int)
label[1:503]=0
label[503:1000]=1
label[1000:1497]=2
label[1497:1995]=3
label[1995:2493]=4
label[2493:2983]=5
label[2983:3483]=6
label[3483:3981]=7
label[3981:4479]=8
label[4479:4972]=9

print(label[1000])
data,Label = shuffle(immatrix,label, random_state=2)
train_data = [data,Label]

img=immatrix[2496].reshape(img_rows,img_cols)
plt.imshow(img)
plt.show()



(X, y) = (train_data[0],train_data[1])



X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4)


X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

X_train /= 255
X_test /= 255

print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')



X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')



y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]


def baseline_model():
# create model
model = Sequential()
model.add(Conv2D(32, (3,3), input_shape=(1, 28, 28), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

#model.add(Conv2D(64, (5, 5), input_shape=(1, 10, 10), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax', name = 'first_dense_layer'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model



# build the model
model = baseline_model()
# Fit the model
hist=model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=30, batch_size=100, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("CNN Error: %.2f%%" % (100-scores[1]*100))



score = model.evaluate(X_test, y_test, verbose=0)
print('Test Loss:', score[0])
print('Test accuracy:', score[1])

test_image = X_test[0:1]
print (test_image.shape)

print(model.predict(test_image))
print(model.predict_classes(test_image))
print(y_test[0:1])




# define the larger model
def larger_model():
# create model
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape=(1, 28, 28), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(15, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(50, activation='relu', name='first_dense_layer'))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model


# build the model
model = larger_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Large CNN Error: %.2f%%" % (100-scores[1]*100))

我正在尝试使用 opencv 调整模型的大小,它会生成以下错误:
/root/mc-x64-2.7/conda-bld/opencv-3_1482254119970/work/opencv-3.1.0 /modules/imgproc/src/imgwarp.cpp:3229: 错误:(-215) ssize.area() > 0 in function resize

【问题讨论】:

  • 如果您发布代码和数据示例会更好
  • 数据科学和机器学习有单独的小组,这对你很有用
  • 您最近编辑了您的帖子并包含了另一个问题。请一次只问一个问题,并考虑在另一篇帖子中询问您遇到的错误。
  • 我被告知要等待 6 天,然后再问任何其他问题。由于很紧急,所以我把它贴在这里。

标签: python keras digit


【解决方案1】:

如何提高准确率?

有点难以从您发布的内容中给出详细的答案,并且没有看到一些数据样本,但仍然会尝试对此进行尝试。我可以看到可能有助于提高准确性的是:

  1. 获取更多数据。在深度学习中,通常使用大量数据,并且模型几乎总是在添加更多数据时得到改进。如果您无法获得新数据,您可以尝试使用您获得的数据生成更多样本,方法是添加噪声或类似修改。

  2. 我看到您目前有 30 个和 10 个 epoch 来训练您的模型。 我建议你增加 epoch 的数量,这样你的模型就有更多的时间收敛。这在大多数情况下也可以在一定程度上提高性能。

  3. 我还看到您的模型上的批量大小为 100 和 200。 您可以尝试减少训练过程的批量大小,以便您的训练在每个 epoch 上执行更多次梯度更新(请记住,您甚至可以使用 batch_size=1each 升级模型/em> 样本,而不是批次)。

  4. 或者,您可以尝试迭代地增加架构的复杂性和层次并比较您的性能。最好从一个简单的模型开始,训练和测试,然后添加层和节点,直到您对结果满意为止。我还看到您尝试了混合卷积和非卷积方法;在增加架构的复杂性之前,您可以尝试从其中一种方法开始。

【讨论】:

    猜你喜欢
    • 2018-12-13
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多