最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow之MNIST CNN實現(xiàn)并保存、加載模型

 更新時間:2020年06月17日 10:25:55   作者:uflswe  
這篇文章主要為大家詳細(xì)介紹了Tensorflow之MNIST CNN實現(xiàn)并保存、加載模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Tensorflow之MNIST CNN實現(xiàn)并保存、加載模型的具體代碼,供大家參考,具體內(nèi)容如下

廢話不說,直接上代碼

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
 
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
import os
 
#download the data
mnist = keras.datasets.mnist
 
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
 
class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
 
train_images = train_images / 255.0
test_images = test_images / 255.0
 
def create_model():
 # It's necessary to give the input_shape,or it will fail when you load the model
 # The error will be like : You are trying to load the 4 layer models to the 0 layer 
 model = keras.Sequential([
   keras.layers.Conv2D(32,[5,5], activation=tf.nn.relu,input_shape = (28,28,1)),
   keras.layers.MaxPool2D(),
   keras.layers.Conv2D(64,[7,7], activation=tf.nn.relu),
   keras.layers.MaxPool2D(),
   keras.layers.Flatten(),
   keras.layers.Dense(576, activation=tf.nn.relu),
   keras.layers.Dense(10, activation=tf.nn.softmax)
 ])
 
 model.compile(optimizer=tf.train.AdamOptimizer(), 
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
 
 return model
 
#reshape the shape before using it, for that the input of cnn is 4 dimensions
train_images = np.reshape(train_images,[-1,28,28,1])
test_images = np.reshape(test_images,[-1,28,28,1])
 
 
#train
model = create_model()                         
model.fit(train_images, train_labels, epochs=4)
 
#save the model
model.save('my_model.h5')
 
#Evaluate
test_loss, test_acc = model.evaluate(test_images, test_labels,verbose = 0)
print('Test accuracy:', test_acc)

模型保存后,自己手寫了幾張圖片,放在文件夾C:\pythonp\testdir2下,開始測試

#Load the model
 
new_model = keras.models.load_model('my_model.h5')
new_model.compile(optimizer=tf.train.AdamOptimizer(), 
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
new_model.summary()
 
#Evaluate
 
# test_loss, test_acc = new_model.evaluate(test_images, test_labels)
# print('Test accuracy:', test_acc)
 
#Predicte
 
mypath = 'C:\\pythonp\\testdir2'
 
def getimg(mypath):
  listdir = os.listdir(mypath)
  imgs = []
  for p in listdir:
    img = plt.imread(mypath+'\\'+p)
    # I save the picture that I draw myself under Windows, but the saved picture's
    # encode style is just opposite with the experiment data, so I transfer it with
    # this line. 
    img = np.abs(img/255-1)
    imgs.append(img[:,:,0])
  return np.array(imgs),len(imgs)
 
imgs = getimg(mypath)
 
test_images = np.reshape(imgs[0],[-1,28,28,1])
 
predictions = new_model.predict(test_images)
 
plt.figure()
 
for i in range(imgs[1]):
 c = np.argmax(predictions[i])
 plt.subplot(3,3,i+1)
 plt.xticks([])
 plt.yticks([])
 plt.imshow(test_images[i,:,:,0])
 plt.title(class_names[c])
plt.show()

測試結(jié)果

自己手寫的圖片截的時候要注意,空白部分盡量不要太大,否則測試結(jié)果就呵呵了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何修改新版Python的pip默認(rèn)安裝路徑

    如何修改新版Python的pip默認(rèn)安裝路徑

    pip安裝的第三方庫默認(rèn)存放在C盤中,為了便于管理和不過度占用C盤空間所以想修改默認(rèn)的pip路徑,這篇文章主要介紹了修改新版Python的pip默認(rèn)安裝路徑的過程,需要的朋友可以參考下
    2024-03-03
  • Python?Pandas中l(wèi)oc和iloc函數(shù)的基本用法示例

    Python?Pandas中l(wèi)oc和iloc函數(shù)的基本用法示例

    無論是loc還是iloc都是pandas中數(shù)據(jù)篩選的函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python?Pandas中l(wèi)oc和iloc函數(shù)的基本用法示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • python實現(xiàn)在線翻譯

    python實現(xiàn)在線翻譯

    這篇文章主要介紹了python實現(xiàn)在線翻譯,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Python 獲得命令行參數(shù)的方法(推薦)

    Python 獲得命令行參數(shù)的方法(推薦)

    本篇將介紹python中sys, getopt模塊處理命令行參數(shù)的方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友參考下吧
    2018-01-01
  • 詳解Python3序列賦值、序列解包

    詳解Python3序列賦值、序列解包

    這篇文章主要介紹了Python3序列賦值、序列解包的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • 深入解讀Python如何進(jìn)行文件讀寫

    深入解讀Python如何進(jìn)行文件讀寫

    文件的作用 就是把一些存儲存放起來,可以讓程序下一次執(zhí)行的時候直接使用,而不必重新制作一份,省時省力,本文將帶你了解通過python如何進(jìn)行文件的讀寫操作
    2021-10-10
  • Pytorch-mlu?實現(xiàn)添加逐層算子方法詳解

    Pytorch-mlu?實現(xiàn)添加逐層算子方法詳解

    本文主要分享了在寒武紀(jì)設(shè)備上?pytorch-mlu?中添加逐層算子的方法教程,代碼具有一定學(xué)習(xí)價值,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • 使用pycallgraph分析python代碼函數(shù)調(diào)用流程以及框架解析

    使用pycallgraph分析python代碼函數(shù)調(diào)用流程以及框架解析

    這篇文章主要介紹了使用pycallgraph分析python代碼函數(shù)調(diào)用流程以及框架解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • python:接口間數(shù)據(jù)傳遞與調(diào)用方法

    python:接口間數(shù)據(jù)傳遞與調(diào)用方法

    今天小編就為大家分享一篇python:接口間數(shù)據(jù)傳遞與調(diào)用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python使用篩選法計算小于給定數(shù)字的所有素數(shù)

    python使用篩選法計算小于給定數(shù)字的所有素數(shù)

    這篇文章主要為大家詳細(xì)介紹了python使用篩選法計算小于給定數(shù)字的所有素數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評論

汾西县| 廉江市| 公安县| 化州市| 石家庄市| 岐山县| 黔江区| 东安县| 绩溪县| 平顺县| 德清县| 苍梧县| 建水县| 罗平县| 公安县| 天水市| 邛崃市| 定结县| 抚顺市| 涪陵区| 原阳县| 讷河市| 常德市| 金坛市| 阿鲁科尔沁旗| 三河市| 玛纳斯县| 武隆县| 奇台县| 滕州市| 栖霞市| 章丘市| 托里县| 新闻| 建宁县| 台北县| 来凤县| 自贡市| 周至县| 行唐县| 吐鲁番市|