【发布时间】:2021-05-19 21:35:13
【问题描述】:
无论如何使用带有CNN模型的tensorflow keras来训练和识别大于9的数字或十进制数字?我的意思是值域在0 到20 之间,或者从0 到10 之间的小数,比如0,1; 0,2;......;9,9?
我已经用 MNIST 数据集训练了模型,用于从 0 到 9 的数字,但我想理解大于 9 的小数和值域。希望看到你对此的想法......
例如号码10
或号码11,12,....,17,...
更新 我通过上面给定的图像尝试使用带有一些新数字的训练模型,但预测仍然给出错误值。这是一些代码:
def trainDigitsModel(alwaysTrain=False, writeFile=True):
modelExists = os.path.exists('traineddata/traineddata.model/saved_model.pb')
if modelExists is False or alwaysTrain is True:
# load data
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Reshaping to format which CNN expects (batch, height, width, channels)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1).astype('float32')
# Load your own images to training and test data
X_train, y_train = load_images_to_data('path/to/trainmore', X_train, y_train)
X_test, y_test = load_images_to_data('path/to/trainmore', X_test, y_test)
# normalize inputs from 0-255 to 0-1
X_train/=255
X_test/=255
# one hot encode, value is 18 because i want to train more label from 10 - 17 -> totally has 18 from 0 - 17
number_of_classes = 18
y_train = np_utils.to_categorical(y_train, number_of_classes)
y_test = np_utils.to_categorical(y_test, number_of_classes)
# create model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(X_train.shape[1], X_train.shape[2], 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5)
if writeFile is True:
model.save('traineddata/traineddata.model')
else:
# Load the model
model = tf.keras.models.load_model('traineddata/traineddata.model')
img = cv.imread('trainmore/12.png',0)
# img = cv.GaussianBlur(img, (1,1), 0)
img = cv.resize(img, (28,28))
img = np.array(img)
img = np.invert(img)
plt.imshow(img)
plt.show()
img = img.reshape(1,28,28,1)
prediction = model.predict(img)
result = np.argmax(prediction)
print(prediction)
print(result)
return model
def load_images_to_data(image_label, image_directory, features_data, label_data):
list_of_files = os.listdir(image_directory)
for file in list_of_files:
arr_file = file.split('.')
image_label = arr_file[0]
image_file_name = os.path.join(image_directory, file)
if ".png" in image_file_name:
img = cv.imread(image_file_name, 0)
img = cv.resize(img, (28,28))
im2arr = np.array(img)
im2arr = np.invert(im2arr)
# plt.imshow(im2arr)
# plt.show()
im2arr = im2arr.reshape(1,28,28,1)
features_data = np.append(features_data, im2arr, axis=0)
label_data = np.append(label_data, [image_label], axis=0)
return features_data, label_data
【问题讨论】:
标签: python tensorflow machine-learning keras handwriting-recognition