CNN模型给出了错误的预测

2024-04-26 04:08:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在研究地区语言的手写数字识别。目前,我关注的是奥利娅。我通过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_148225419970/work/opencv-3.1.0/modules/imgproc/src/图像地址:3229:错误:(-215)大小面积()>;0在函数大小调整中


Tags: ofthefromtestimportaddimgdata
1条回答
网友
1楼 · 发布于 2024-04-26 04:08:02

How to improve the accuracy?

有点难从你发布的内容中给出一个详细的答案,而且没有看到一些数据样本,但还是会尝试尝试一下。我认为有助于提高准确性的是:

  1. 获取更多数据。在深度学习中,通常需要处理大量的数据,当添加更多数据时,模型几乎总是会得到改进。如果你不能获得新的数据,你可以通过添加噪声或类似的修改,用你得到的数据生成更多的样本。

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

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

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

相关问题 更多 >