【TensorFlow-windows】keras接口——卷积手写数字识别,模型保存和调用

前言

上一节学习了以TensorFlow为底端的keras接口最简单的使用,这里就继续学习怎么写卷积分类模型和各种保存方法(仅保存权重、权重和网络结构同时保存)

国际惯例,参考博客:
官方教程
【注】其实不用看博客,直接翻到文末看我的colab就行,里面涵盖了学习方法,包括自己的出错内容和一些简单笔记,下面为了展示方便,每次都重新定义了网络结构,对Python熟悉的大佬可以直接def create_model():函数,把模型结构保存起来,后面直接调用就行

构建卷积模型分类

回顾一下上篇博客介绍的构建模型方法,有两种写法:

model = keras.models.Sequential([keras.layers.Flatten(...),keras.layers.Dense(...),...
])
model = keras.models.Sequential()
model.add(keras.layers.Flatten(...))
model.add(keras.layers.Dense(...))
])

第一种简单,第二种舒服,本博文采用第二种写法构建一个简单的卷积网络

引入相关包

保存模型需要路径(引入os),数据归一化处理(引入numpy),此外注意,虽然我们学习keras,但是不仅要引入keras,还得引入tensorflow,具体原因后续再说

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import os

构建数据集

还是用mnist吧,后续根据需要出一个训练本地图片数据的教程,看看是不是还得数据流操作
注意,要把标签改为单热度编码格式,数据也得归一化

mnist_dataset = keras.datasets.mnist
(train_x,train_y),(test_x,test_y)= mnist_dataset.load_data()
train_y = keras.utils.to_categorical(train_y,10)
test_y = keras.utils.to_categorical(test_y,10)
train_x = train_x / 255.0
test_x = test_x / 255.0

还得注意就是keras的卷积操作接受的数据是一个思维矩阵,需要指定是channels_first即<样本,通道,行,列>, 还是channels_last即<样本,行,列,通道>,默认最后的维度是通道(channels_last)

train_x = train_x[ ..., np.newaxis ]
test_x = test_x[..., np.newaxis ]
print(train_x.shape)#(60000, 28, 28, 1)

构建模型

构建简单的AlexNet,但是直接用这个结构可能有问题,因为输入图片总共28\*28,经过多次卷积池化会越变越小,最后可能都不够做卷积池化了,为稍微改了改

model = keras.models.Sequential()
model.add( keras.layers.Conv2D( filters = 64, kernel_size=(11,11),strides = (1,1), padding='valid', activation= tf.keras.activations.relu) )
model.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model.add( keras.layers.Conv2D( filters = 192, kernel_size=(5,5),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model.add( keras.layers.Conv2D( filters = 256, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))model.add( keras.layers.Flatten() )
model.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model.add( keras.layers.Dropout(rate=0.5) )
model.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model.add( keras.layers.Dropout(rate=0.5) )model.add( keras.layers.Dense(units=10 , activation= keras.activations.softmax ) )

编译和训练模型

keras中关于交叉熵分类有两个函数sparse_categorical_crossentropycategorical_crossentropy,这里就出现了第一个坑,如果将标签[batch_size,10]输入到编译器,使用sparse_...的时候回报错

logits and labels must have the same first dimension,got logits shape [200,10] and labels shape [2000]

好像是默认把他拉长拼起来了,所以我们要使用后者

model.compile( optimizer= keras.optimizers.Adam(),loss= keras.losses.categorical_crossentropy, metrics=['accuracy'] )

然后就可以训练模型了

model.fit(train_x,train_y,epochs=2, batch_size=200)
'''
Epoch 1/2
60000/60000 [==============================] - 26s 435us/step - loss: 0.2646 - acc: 0.9110
Epoch 2/2
60000/60000 [==============================] - 24s 407us/step - loss: 0.0510 - acc: 0.9855
<tensorflow.python.keras.callbacks.History at 0x7f65fe2de940>
'''

还能看网络结构和参数,使用summary()函数

model.summary()
'''
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              multiple                  7808      
_________________________________________________________________
max_pooling2d (MaxPooling2D) multiple                  0         
_________________________________________________________________
conv2d_1 (Conv2D)            multiple                  307392    
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 multiple                  0         
_________________________________________________________________
conv2d_2 (Conv2D)            multiple                  663936    
_________________________________________________________________
conv2d_3 (Conv2D)            multiple                  1327488   
_________________________________________________________________
conv2d_4 (Conv2D)            multiple                  884992    
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 multiple                  0         
_________________________________________________________________
flatten (Flatten)            multiple                  0         
_________________________________________________________________
dense (Dense)                multiple                  4198400   
_________________________________________________________________
dropout (Dropout)            multiple                  0         
_________________________________________________________________
dense_1 (Dense)              multiple                  16781312  
_________________________________________________________________
dropout_1 (Dropout)          multiple                  0         
_________________________________________________________________
dense_2 (Dense)              multiple                  40970     
=================================================================
Total params: 24,212,298
Trainable params: 24,212,298
Non-trainable params: 0
_________________________________________________________________
'''

可以用测试集评估模型

print(test_x.shape, test_y.shape)
model.evaluate( test_x ,test_y)
'''
(10000, 28, 28, 1) (10000, 10)
10000/10000 [==============================] - 3s 283us/step
[0.03784323987539392, 0.9897]
'''

还能预测单张图片,但是要注意输入的第一个维度是样本数, 记得增加一个维度

test_img_idx = 1000
test_img = test_x[test_img_idx,...]
test_img= test_img[np.newaxis,...]
img_prob = model.predict( test_img )plt.figure()
plt.imshow( np.squeeze(test_img) )
plt.title(img_prob.argmax())

在这里插入图片描述

保存模型

训练过程中保存检查点

函数为keras.callbacks.ModelCheckpoint

checkpoint_path='./train_save/mnist.ckpt'
checkpoint_dir= os.path.dirname(checkpoint_path)
# 创建检查回调点
cp_callback= keras.callbacks.ModelCheckpoint( checkpoint_path, save_weights_only= True, verbose=1 )model.fit(train_x, train_y,epochs=2, validation_data=(test_x,test_y), callbacks=[cp_callback] )
'''
Train on 60000 samples, validate on 10000 samples
Epoch 1/2
59968/60000 [============================>.] - ETA: 0s - loss: 0.1442 - acc: 0.9681
Epoch 00001: saving model to ./train_save/mnist.ckpt
WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7faff48fae80>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.Consider using a TensorFlow optimizer from `tf.train`.
60000/60000 [==============================] - 93s 2ms/step - loss: 0.1442 - acc: 0.9681 - val_loss: 0.0693 - val_acc: 0.9811
Epoch 2/2
59968/60000 [============================>.] - ETA: 0s - loss: 0.0757 - acc: 0.9840
Epoch 00002: saving model to ./train_save/mnist.ckpt
WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7faff48fae80>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved.Consider using a TensorFlow optimizer from `tf.train`.
60000/60000 [==============================] - 92s 2ms/step - loss: 0.0757 - acc: 0.9839 - val_loss: 0.0489 - val_acc: 0.9876
<tensorflow.python.keras.callbacks.History at 0x7fafead25f98>
'''

发现有个warnning,意思是说模型使用的是keras的优化器,保存以后不是tensorflow能直接使用的模型格式,好像少了个状态,需要使用tensorflow自带的优化器,好吧,调整代码

import os
model.compile(optimizer = tf.train.AdamOptimizer(),loss = keras.losses.categorical_crossentropy, metrics=['accuracy'] )checkpoint_path='./train_save2/mnist.ckpt'
checkpoint_dir= os.path.dirname(checkpoint_path)
# 创建检查回调点
cp_callback= keras.callbacks.ModelCheckpoint( checkpoint_path, save_weights_only= True, verbose=1 )model.fit(train_x, train_y,epochs=2, validation_data=(test_x,test_y), callbacks=[cp_callback] )
'''
Train on 60000 samples, validate on 10000 samples
Epoch 1/2
59936/60000 [============================>.] - ETA: 0s - loss: 0.2241 - acc: 0.9325
Epoch 00001: saving model to ./train_save2/mnist.ckpt
60000/60000 [==============================] - 60s 1ms/step - loss: 0.2239 - acc: 0.9326 - val_loss: 0.1009 - val_acc: 0.9765
Epoch 2/2
59936/60000 [============================>.] - ETA: 0s - loss: 0.0866 - acc: 0.9801
Epoch 00002: saving model to ./train_save2/mnist.ckpt
60000/60000 [==============================] - 56s 930us/step - loss: 0.0867 - acc: 0.9800 - val_loss: 0.0591 - val_acc: 0.9855
<tensorflow.python.keras.callbacks.History at 0x7fad1a90d7b8>
'''

这回没出错了,尝试构建一个没训练的模型,将参数载入进来

model_test = keras.models.Sequential()
model_test.add( keras.layers.Conv2D( filters = 64, kernel_size=(11,11),strides = (1,1), padding='valid', activation= tf.keras.activations.relu) )
model_test.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test.add( keras.layers.Conv2D( filters = 192, kernel_size=(5,5),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test.add( keras.layers.Conv2D( filters = 256, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))model_test.add( keras.layers.Flatten() )
model_test.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test.add( keras.layers.Dropout(rate=0.5) )
model_test.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test.add( keras.layers.Dropout(rate=0.5) )model_test.add( keras.layers.Dense(units=10 , activation= keras.activations.softmax ) )model_test.compile(optimizer = tf.train.RMSPropOptimizer(learning_rate=0.01),loss = keras.losses.categorical_crossentropy, metrics=['accuracy'] )

载入最近的检查点

! ls train_save
latest = tf.train.latest_checkpoint('train_save2')#checkpoint  mnist.ckpt.data-00000-of-00001  mnist.ckpt.index
type(latest)#str
loss,acc = model_test.evaluate(test_x,test_y)
print("未载入权重时:准确率{:5.2f}%".format(100*acc))
model_test.load_weights(latest)
loss,acc = model_test.evaluate(test_x,test_y)
print("载入权重时:准确率{:5.2f}%".format(100*acc))
'''
10000/10000 [==============================] - 3s 308us/step
未载入权重时:准确率 9.60%
10000/10000 [==============================] - 3s 264us/step
载入权重时:准确率98.60%
'''

间隔保存

也可以指定多少次训练保存一次检查点,这样能够有效防止过拟合,以后自己可以挑选比较好的训练参数

checkpoint_path="train_save3/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = keras.callbacks.ModelCheckpoint(checkpoint_path,verbose=1, save_weights_only=True, period=1)
model.fit(train_x,train_y,epochs=2, callbacks=[cp_callback], validation_data=[test_x,test_y],verbose=1)
'''
Train on 60000 samples, validate on 10000 samples
Epoch 1/2
59936/60000 [============================>.] - ETA: 0s - loss: 0.0447 - acc: 0.9897
Epoch 00001: saving model to train_save3/cp-0001.ckpt
60000/60000 [==============================] - 61s 1ms/step - loss: 0.0446 - acc: 0.9897 - val_loss: 0.0421 - val_acc: 0.9920
Epoch 2/2
59936/60000 [============================>.] - ETA: 0s - loss: 0.0478 - acc: 0.9885
Epoch 00002: saving model to train_save3/cp-0002.ckpt
60000/60000 [==============================] - 61s 1ms/step - loss: 0.0478 - acc: 0.9884 - val_loss: 0.0590 - val_acc: 0.9859
<tensorflow.python.keras.callbacks.History at 0x7fafea497dd8>
'''

重新构建一个未训练的模型,调用第一次的训练结果

model_test1 = keras.models.Sequential()
model_test1.add( keras.layers.Conv2D( filters = 64, kernel_size=(11,11),strides = (1,1), padding='valid', activation= tf.keras.activations.relu) )
model_test1.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test1.add( keras.layers.Conv2D( filters = 192, kernel_size=(5,5),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test1.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test1.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test1.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test1.add( keras.layers.Conv2D( filters = 256, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test1.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))model_test1.add( keras.layers.Flatten() )
model_test1.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test1.add( keras.layers.Dropout(rate=0.5) )
model_test1.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test1.add( keras.layers.Dropout(rate=0.5) )model_test1.add( keras.layers.Dense(units=10 , activation= keras.activations.softmax ) )model_test1.compile(optimizer = tf.train.AdamOptimizer(learning_rate=0.01),loss = keras.losses.categorical_crossentropy, metrics=['accuracy'] )

选择第一个检查点载入

loss,acc=model_test1.evaluate(test_x,test_y)
print("未载入权重时:准确率{:5.2f}%".format(100*acc))
model_test1.load_weights("train_save3/cp-0001.ckpt")
loss,acc=model_test1.evaluate(test_x,test_y)
print("载入权重时:准确率{:5.2f}%".format(100*acc))
'''
10000/10000 [==============================] - 3s 260us/step
未载入权重时:准确率10.28%
10000/10000 [==============================] - 2s 244us/step
载入权重时:准确率98.30%
'''

手动保存模型

在训练完毕以后,也可以自行调用save_weights函数保存权重

model.save_weights('./train_save3/mnist_checkpoint')

构建未训练模型

model_test2 = keras.models.Sequential()
model_test2.add( keras.layers.Conv2D( filters = 64, kernel_size=(11,11),strides = (1,1), padding='valid', activation= tf.keras.activations.relu) )
model_test2.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test2.add( keras.layers.Conv2D( filters = 192, kernel_size=(5,5),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test2.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test2.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test2.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test2.add( keras.layers.Conv2D( filters = 256, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test2.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))model_test2.add( keras.layers.Flatten() )
model_test2.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test2.add( keras.layers.Dropout(rate=0.5) )
model_test2.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test2.add( keras.layers.Dropout(rate=0.5) )model_test2.add( keras.layers.Dense(units=10 , activation= keras.activations.softmax ) )model_test2.compile(optimizer = tf.train.AdamOptimizer(learning_rate=0.01),loss = keras.losses.categorical_crossentropy, metrics=['accuracy'] )

读取权重以及评估模型

loss,acc = model_test2.evaluate(test_x,test_y)
print("未载入权重时:准确率{:5.2f}%".format(100*acc))
model_test2.load_weights('./train_save3/mnist_checkpoint')
loss,acc = model_test2.evaluate(test_x,test_y)
print("载入权重时:准确率{:5.2f}%".format(100*acc))
'''
10000/10000 [==============================] - 3s 303us/step
未载入权重时:准确率12.15%
10000/10000 [==============================] - 3s 260us/step
载入权重时:准确率98.59%
'''

全部保存

同时保存模型与参数
构建未训练模型

model_test3 = keras.models.Sequential()
model_test3.add( keras.layers.Conv2D( filters = 64, kernel_size=(11,11),strides = (1,1), padding='valid', activation= tf.keras.activations.relu) )
model_test3.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test3.add( keras.layers.Conv2D( filters = 192, kernel_size=(5,5),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test3.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test3.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test3.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test3.add( keras.layers.Conv2D( filters = 256, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test3.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))model_test3.add( keras.layers.Flatten() )
model_test3.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test3.add( keras.layers.Dropout(rate=0.5) )
model_test3.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test3.add( keras.layers.Dropout(rate=0.5) )model_test3.add( keras.layers.Dense(units=10 , activation= keras.activations.softmax ) )model_test3.compile(optimizer = tf.train.AdamOptimizer(),loss = keras.losses.categorical_crossentropy, metrics=['accuracy'] )
model_test3.fit(train_x,train_y,batch_size=200,epochs=2)

保存

model_test3.save('my_model.h5')
'''
Currently `save` requires model to be a graph network. Consider using `save_weights`, in order to save the weights of the model.
'''

出现错误,意思是需要定义的模型是一个图网络结构,只能保存权重。其实错误原因在于我们的第一层没有定义输入的大小,尝试定义一波

model_test4 = keras.models.Sequential()
model_test4.add( keras.layers.Conv2D( filters = 64, kernel_size=(11,11),strides = (1,1), padding='valid', activation= tf.keras.activations.relu,input_shape=(28,28,1)) )
model_test4.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test4.add( keras.layers.Conv2D( filters = 192, kernel_size=(5,5),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test4.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test4.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test4.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test4.add( keras.layers.Conv2D( filters = 256, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test4.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))model_test4.add( keras.layers.Flatten() )
model_test4.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test4.add( keras.layers.Dropout(rate=0.5) )
model_test4.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test4.add( keras.layers.Dropout(rate=0.5) )model_test4.add( keras.layers.Dense(units=10 , activation= keras.activations.softmax ) )model_test4.compile(optimizer = tf.train.AdamOptimizer(),loss = keras.losses.categorical_crossentropy, metrics=['accuracy'] )
model_test4.fit(train_x,train_y,batch_size=200,epochs=2)
'''
Epoch 1/2
60000/60000 [==============================] - 22s 364us/step - loss: 0.4112 - acc: 0.8556
Epoch 2/2
60000/60000 [==============================] - 21s 342us/step - loss: 0.0584 - acc: 0.9838
<tensorflow.python.keras.callbacks.History at 0x7f137972f4a8>
'''

尝试保存

model_test4.save('my_model.h5')
'''
WARNING:tensorflow:TensorFlow optimizers do not make it possible to access optimizer attributes or optimizer state after instantiation. As a result, we cannot save the optimizer as part of the model save file.You will have to compile your model again after loading it. Prefer using a Keras optimizer instead (see keras.io/optimizers).
'''

又出warning,说是不能使用tensorflow的优化器,要使用keras自带的优化器,好吧,改

model_test5 = keras.models.Sequential()
model_test5.add( keras.layers.Conv2D( filters = 64, kernel_size=(11,11),strides = (1,1), padding='valid', activation= tf.keras.activations.relu,input_shape=(28,28,1)) )
model_test5.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test5.add( keras.layers.Conv2D( filters = 192, kernel_size=(5,5),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test5.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))
model_test5.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test5.add( keras.layers.Conv2D( filters = 384, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test5.add( keras.layers.Conv2D( filters = 256, kernel_size=(3,3),strides = (1,1), padding='same', activation= tf.keras.activations.relu) )
model_test5.add( keras.layers.MaxPool2D( pool_size=(2,2),strides=(2,2) ))model_test5.add( keras.layers.Flatten() )
model_test5.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test5.add( keras.layers.Dropout(rate=0.5) )
model_test5.add( keras.layers.Dense( units=4096, activation= keras.activations.relu ) )
model_test5.add( keras.layers.Dropout(rate=0.5) )model_test5.add( keras.layers.Dense(units=10 , activation= keras.activations.softmax ) )model_test5.compile(optimizer = tf.keras.optimizers.Adam(),loss = keras.losses.categorical_crossentropy, metrics=['accuracy'] )
model_test5.fit(train_x,train_y,batch_size=200,epochs=2)
model_test5.save("my_model2.h5")
'''
Epoch 1/2
60000/60000 [==============================] - 26s 434us/step - loss: 0.2850 - acc: 0.9043
Epoch 2/2
60000/60000 [==============================] - 25s 409us/step - loss: 0.0555 - acc: 0.9847
<tensorflow.python.keras.callbacks.History at 0x7f661033a9e8>
'''

这样就不出错了,尝试调用模型和参数,因为保存了模型结构和参数,所以不需要重新定义网络结构

model_test6= keras.models.load_model("my_model2.h5")
model_test6.summary()
'''
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_10 (Conv2D)           (None, 18, 18, 64)        7808      
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 9, 9, 64)          0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 9, 9, 192)         307392    
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 4, 4, 192)         0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 4, 4, 384)         663936    
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 4, 4, 384)         1327488   
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 4, 4, 256)         884992    
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 2, 2, 256)         0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 1024)              0         
_________________________________________________________________
dense_6 (Dense)              (None, 4096)              4198400   
_________________________________________________________________
dropout_4 (Dropout)          (None, 4096)              0         
_________________________________________________________________
dense_7 (Dense)              (None, 4096)              16781312  
_________________________________________________________________
dropout_5 (Dropout)          (None, 4096)              0         
_________________________________________________________________
dense_8 (Dense)              (None, 10)                40970     
=================================================================
Total params: 24,212,298
Trainable params: 24,212,298
Non-trainable params: 0
_________________________________________________________________
'''

稳如狗,做一下测试

  • 测试集上的测试
model_test6.evaluate(test_x,test_y)
'''
10000/10000 [==============================] - 3s 302us/step
[0.05095283883444499, 0.9868]
'''
  • 单张图片的测试
test_img= test_x[5000,...]
test_img=test_img[ np.newaxis,...]
pred_label = model_test6.predict_classes(test_img)
plt.figure()
plt.imshow( np.squeeze(test_img))
plt.title(pred_label)

在这里插入图片描述

后记

这一章主要学习了如何搭建简单的卷积网络,以及集中保存方法:仅权重以及权重和模型结构。
主要记住的就是如果仅保存权重,注意用tensorflow自带的优化器,而保存网络和权重的时候要用keras的优化器
下一章针对深度学习的几个理论做一下理解以及实验,包括BatchNormResNet等。
博客代码链接:戳这里

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/246599.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【TensorFlow-windows】keras接口——BatchNorm和ResNet

前言 之前学习利用Keras简单地堆叠卷积网络去构建分类模型的方法&#xff0c;但是对于很深的网络结构很难保证梯度在各层能够正常传播&#xff0c;经常发生梯度消失、梯度爆炸或者其它奇奇怪怪的问题。为了解决这类问题&#xff0c;大佬们想了各种办法&#xff0c;比如最原始的…

【TensorFlow-windows】keras接口——卷积核可视化

前言 在机器之心上看到了关于卷积核可视化相关理论&#xff0c;但是作者的源代码是基于fastai写的&#xff0c;而fastai的底层是pytorch&#xff0c;本来准备自己用Keras复现一遍的&#xff0c;但是尴尬地发现Keras还没玩熟练&#xff0c;随后发现了一个keras-vis包可以用于做…

【TensorFlow-windows】投影变换

前言 没什么重要的&#xff0c;就是想测试一下tensorflow的投影变换函数tf.contrib.image.transform中每个参数的含义 国际惯例&#xff0c;参考文档 官方文档 描述 调用方法与默认参数&#xff1a; tf.contrib.image.transform(images,transforms,interpolationNEAREST,…

【TensorFlow-windows】扩展层之STN

前言 读TensorFlow相关代码看到了STN的应用&#xff0c;搜索以后发现可替代池化&#xff0c;增强网络对图像变换(旋转、缩放、偏移等)的抗干扰能力&#xff0c;简单说就是提高卷积神经网络的空间不变性。 国际惯例&#xff0c;参考博客&#xff1a; 理解Spatial Transformer…

【TensorFlow-windows】MobileNet理论概览与实现

前言 轻量级神经网络中&#xff0c;比较重要的有MobileNet和ShuffleNet&#xff0c;其实还有其它的&#xff0c;比如SqueezeNet、Xception等。 本博客为MobileNet的前两个版本的理论简介与Keras中封装好的模块的对应实现方案。 国际惯例&#xff0c;参考博客&#xff1a; 纵…

【TensorFlow-windows】keras接口——ImageDataGenerator裁剪

前言 Keras中有一个图像数据处理器ImageDataGenerator&#xff0c;能够很方便地进行数据增强&#xff0c;并且从文件中批量加载图片&#xff0c;避免数据集过大时&#xff0c;一下子加载进内存会崩掉。但是从官方文档发现&#xff0c;并没有一个比较重要的图像增强方式&#x…

【TensorFlow-windows】TensorBoard可视化

前言 紧接上一篇博客&#xff0c;学习tensorboard可视化训练过程。 国际惯例&#xff0c;参考博客&#xff1a; MNIST机器学习入门 Tensorboard 详解&#xff08;上篇&#xff09; Tensorboard 可视化好帮手 2 tf-dev-summit-tensorboard-tutorial tensorflow官方mnist_…

深度学习特征归一化方法——BN、LN、IN、GN

前言 最近看到Group Normalization的论文&#xff0c;主要提到了四个特征归一化方法&#xff1a;Batch Norm、Layer Norm、Instance Norm、Group Norm。此外&#xff0c;论文还提到了Local Response Normalization(LRN)、Weight Normalization(WN)、Batch Renormalization(BR)…

【TensorFlow-windows】keras接口——利用tensorflow的方法加载数据

前言 之前使用tensorflow和keras的时候&#xff0c;都各自有一套数据读取方法&#xff0c;但是遇到一个问题就是&#xff0c;在训练的时候&#xff0c;GPU的利用率忽高忽低&#xff0c;极大可能是由于训练过程中读取每个batch数据造成的&#xff0c;所以又看了tensorflow官方的…

骨骼动画——论文与代码精读《Phase-Functioned Neural Networks for Character Control》

前言 最近一直玩CV&#xff0c;对之前学的动捕知识都忘得差不多了&#xff0c;最近要好好总结一下一直以来学习的内容&#xff0c;不能学了忘。对2017年的SIGGRAPH论文《Phase-Functioned Neural Networks for Character Control》进行一波深入剖析吧&#xff0c;结合源码。 额…

颜色协调模型Color Harmoniztion

前言 最近做换脸&#xff0c;在肤色调整的那一块&#xff0c;看到一个有意思的文章&#xff0c;复现一波玩玩。不过最后一步掉链子了&#xff0c;有兴趣的可以一起讨论把链子补上。 主要是github上大佬的那个复现代码和原文有点差异&#xff0c;而且代码复杂度过高&#xff0…

Openpose推断阶段原理

前言 之前出过一个关于openpose配置的博客&#xff0c;不过那个代码虽然写的很好&#xff0c;而且是官方的&#xff0c;但是分析起来很困难&#xff0c;然后再opencv相关博客中找到了比较清晰的实现&#xff0c;这里分析一波openpose的推断过程。 国际惯例&#xff0c;参考博…

换脸系列——眼鼻口替换

前言 想着整理一下换脸相关的技术方法&#xff0c;免得以后忘记了&#xff0c;最近脑袋越来越不好使了。应该会包含三个系列&#xff1a; 仅换眼口鼻&#xff1b;换整个面部&#xff1b;3D换脸 先看看2D换脸吧&#xff0c;网上已经有现成的教程了&#xff0c;这里拿过来整理一…

换脸系列——整脸替换

前言 前面介绍了仅替换五官的方法&#xff0c;这里介绍整张脸的方法。 国际惯例&#xff0c;参考博客&#xff1a; [图形算法]Delaunay三角剖分算法 维诺图&#xff08;Voronoi Diagram&#xff09;分析与实现 Delaunay Triangulation and Voronoi Diagram using OpenCV (…

3D人脸重建——PRNet网络输出的理解

前言 之前有款换脸软件不是叫ZAO么&#xff0c;分析了一下&#xff0c;它的实现原理绝对是3D人脸重建&#xff0c;而非deepfake方法&#xff0c;找了一篇3D重建的论文和源码看看。这里对源码中的部分函数做了自己的理解和改写。 国际惯例&#xff0c;参考博客&#xff1a; 什…

tensorflow官方posenet模型解析

前言 tensorflow官方有个姿态估计项目&#xff0c;这个输入和openpose还有点不一样&#xff0c;这里写个单人情况下的模型输出解析方案。 国际惯例&#xff0c;参考博客&#xff1a; 博客: 使用 TensorFlow.js 在浏览器端上实现实时人体姿势检测 tensorflow中posnet的IOS代…

tensorflow2安装时候的一个dll找不到的错误

电脑环境&#xff1a; vs2015python3.7.6&#xff0c;使用anaconda安装的CUDA 10.1cuDnn 7.6.5tensorflow2.1.0 错误内容 File "C:\Users\zb116\anaconda3\lib\imp.py", line 242, in load_modulereturn load_dynamic(name, filename, file)File "C:\Users\z…

PCA、SVD、ZCA白化理论与实现

简介 在UFLDL中介绍了主成分分析这一块的知识&#xff0c;而且当时学机器学习的时候&#xff0c;老师是将PCA和SVD联系起来将的&#xff0c;同时UFLDL也讲到了使用PCA做数据白化whitening处理&#xff0c;这个词经常在论文里面看到。 国际惯例&#xff0c;参考博客&#xff1…

OpenCV使用Tensorflow2-Keras模型

前言 最近工作上需要在C上快速集成Tensorflow/Keras训练好的模型&#xff0c;做算法验证。首先想到的就是opencv里面的dnn模块了&#xff0c;但是它需要的格式文件比较郁闷&#xff0c;是pb格式的模型&#xff0c;但是keras通常保存的是h5文件&#xff0c;查阅了很多资料&…

3D人脸表情驱动——基于eos库

前言 之前出过三篇换脸的博文&#xff0c;遇到一个问题是表情那一块不好处理&#xff0c;可行方法是直接基于2D人脸关键点做网格变形&#xff0c;强行将表情矫正到目标人脸&#xff0c;还有就是使用PRNet的思想&#xff0c;使用目标人脸的顶点模型配合源人脸的纹理&#xff0c…