【神经网络】一次性连续加载30张手写数字识别项目、服装项目识别

利用tensorflow、pycharm做手写数字识别、服装项目识别


前言

TensorFlow™是一个基于数据流编程(dataflow programming)的符号数学系统,被广泛应用于各类机器学习(machine learning)算法的编程实现,其前身是谷歌的神经网络算法库DistBelief 。
Tensorflow拥有多层级结构,可部署于各类服务器、PC终端和网页并支持GPU和TPU高性能数值计算,被广泛应用于谷歌内部的产品开发和各领域的科学研究 。
TensorFlow由谷歌人工智能团队谷歌大脑(Google Brain)开发和维护,拥有包括TensorFlow Hub、TensorFlow Lite、TensorFlow Research Cloud在内的多个项目以及各类应用程序接口(Application Programming Interface, API) 。自2015年11月9日起,TensorFlow依据阿帕奇授权协议(Apache 2.0 open source license)开放源代码 。


一、手写数字识别项目?

基于tensorflow做一个连续加载30张手写数字识别项目

1.导入数据库

·可以去tensorflow官网下载https://tensorflow.google.cn/tutorials/keras/classification?hl=zh-cn#%E5%AF%BC%E5%85%A5_fashion_mnist_%E6%95%B0%E6%8D%AE%E9%9B%86
·有需要的也可以留言
​​​​​​​​​​​​​​​​​​在这里插入图片描述

2.一次性连续加载30张手写数字识别源码

import tensorflow as tf
mnist = tf.keras.datasets.mnist
import matplotlib.pyplot as plt
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train /255
x_test = x_test/255
plt.figure(figsize=(10,10))
for i in range(30):
    plt.subplot(6,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.show()

model = tf.keras.models.Sequential([
    # 拉平,参数是数据的维度(把二维数据转换成一维数据)
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    # 定义神经网络全连接层,参数是神经元个数以及使用的激活函数
    tf.keras.layers.Dense(128, activation='relu'),
    # 设置遗忘率(百分之二十)
    tf.keras.layers.Dropout(0.2),
    # 定义最终的输出(输出10种类别,softmax实现分类的概率分布)
    tf.keras.layers.Dense(10, activation='softmax')
])
#优化模型
model.compile(#选择优化器
             optimizer='adam',
             #定义模型的损失的数
             loss='sparse_categorical_crossentropy',
             #模型的评估
             metrics=['accuracy'])
#训练模型(训练集,训练次数)
print("验证模型*************")
model.fit(x_train, y_train, epochs=10)
#验证模型(测试集)
print("验证模型*************")
model.evalvate(x_test, y_test)

3.实现结果

在这里插入图片描述
在这里插入图片描述

二、服装识别

训练一个神经网络模型,对运动鞋和衬衫等服装图像进行分类。

1.导入数据库

如第一步所述

2.服装识别项目源码

import numpy
import tensorflow as tf

fashion = tf.keras.datasets.fashion_mnist
import matplotlib.pyplot as plt

# 裁入并准备好FashionMNIST数据集
# #(x_train,y_train)是训练集
# #(x_test.y_test)是测试集
(x_train, y_train), (x_test, y_test) = fashion.load_data()
print("第一张图片**********************")
print(x_train[0])
print("第一张图片对应的数字**********************")
print(y_train[0])

# 可视化查看图片
plt.imshow(x_train[0], cmap=plt.cm.gray_r)
plt.show()
model = tf.keras.models.Sequential([
    # 拉平,参数是数据的维度(把二维数据转换成一维数据)
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    # 定义神经网络全连接层,参数是神经元个数以及使用的激活函数
    tf.keras.layers.Dense(128, activation='relu'),
    # 设置遗忘率(百分之二十)
    tf.keras.layers.Dropout(0.2),
    # 定义最终的输出(输出10种类别,softmax实现分类的概率分布)
    tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
              # 定义模型的损失回热
              loss='sparse_categorical_crossentropy',
              # 模型的评告
              metrics=['accuracy'])
# 训练
model.fit(x_train, y_train, epochs=10)

print("验证模型*************")
model.evalvate(x_test, y_test)

# 测试集测试
class_names = ["上衣", "裤子", "老头衫", "连衣裙", "外套",
               "凉鞋", "衬衫", "运动鞋", "包包", "短靴"]

# 展开数组的形状
imgData = (numpy.expand_dins(x_test[0], 0))
# print(imgData.shape)#输出(1,28,28)

# 预训结果是一个包含10个数字的数组,它们代表模型对10种不同服装中每种服装的可信度”
predictions_single = model.predict(imgData)
print("可信度", predictions_single)
max = numpy.argmax(predictions_single)
# 在列表中排出最大值
print(class_names[max])
# 电英示刷试及图片
plt.inshow(x_test[0], cmap=plt.cm.gray_r)
plt.show()

3.实现结果

在这里插入图片描述
在这里插入图片描述

需要文件数据集可以留言哦~

总结

以上就是一次性连续加载30张手写数字识别和服装项目识别的项目了,基于神经网络做起来相对来说还是可以的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@¥文竹¥

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值