keras 入门 --手写数字识别

深度学习keras库中的helloworld:

 

#
#搭建一个简单的全连接神经网络,用于手写数字识别
#

from keras.layers import Input,Dense
from keras.models import Model
from keras.datasets import mnist
from keras.utils import np_utils    

(X_train,y_train),(X_test,y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0],784).astype('float32')
X_test = X_test.reshape(X_test.shape[0],784).astype('float32')
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

input = Input(shape=(784,))
x = Dense(64, activation='relu')(input)
x = Dense(64, activation='relu')(x)
output = Dense(10, activation='softmax')(x)


model = Model(inputs=input, outputs=output)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', 
              metrics=['accuracy'])
model.fit(X_train, y_train) 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器视觉手写数字识别是一种将手写数字的图像与其对应的数字进行关联的任务。这个问题通常被认为是计算机视觉领域的一个入门任务,也是许多深度学习框架和算法的基础测试案例之一。 以下是一个使用Python和OpenCV库进行机器视觉手写数字识别的简单示例: ```python import cv2 import numpy as np from keras.models import load_model # 加载训练好的模型 model = load_model('digit_recognition_model.h5') # 加载图像并进行预处理 image = cv2.imread('handwritten_digit.png') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) resized = cv2.resize(gray, (28, 28)) normalized = resized / 255.0 reshaped = np.reshape(normalized, (1, 28, 28, 1)) # 进行预测 prediction = model.predict(reshaped) digit = np.argmax(prediction) # 显示结果 cv2.putText(image, str(digit), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow('Handwritten Digit Recognition', image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个示例中,我们首先加载了一个训练好的模型,该模型可以识别手写数字。然后,我们加载了一个手写数字的图像,并对其进行了预处理,包括灰度化、调整大小、归一化和重塑。接下来,我们使用模型对预处理后的图像进行预测,并找到预测结果中概率最高的数字。最后,我们在图像上显示识别结果。 请注意,这只是一个简单的示例,实际的机器视觉手写数字识别系统可能需要更复杂的算法和模型来提高准确性和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值