以下內容主要用於完善上節六步法搭建神經網絡的功能,
- import
- train, test <數據增強>
- model = tf.keras.models.Sequential
- model.compile
- model.fit <斷點續訓>
- model.summary <參數提取,acc/loss 可視化>
- <前向推理實現應用>
1 數據增強 (增大數據量)
image_gen_train = tf.keras.preprocessing.image.ImageDataGenerator( rescale = 所有數據將乘以該數值 rotation_range = 隨機旋轉角度數范圍 width_shift_range = 隨機寬度偏移量 height_shift_range = 隨機高度偏移量 水平翻轉:horizontal_flip = 是否隨機水平翻轉 隨機縮放:zoom_range = 隨機縮放的范圍 [1-n,1+n] ) image_gen_train.fit(x_train)
mnist 數據集示例:
import tensorflow as tf mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) # 給數據增加一個維度,使數據和網絡結構匹配 x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) image_gen_train = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1. / 1., # 如為圖像,分母為255時,可歸至0~1 rotation_range=45, # 隨機45度旋轉 width_shift_range=.15, # 寬度偏移 height_shift_range=.15, # 高度偏移 horizontal_flip=True, # 水平翻轉 zoom_range=0.5 # 將圖像隨機縮放閾量50% ) image_gen_train.fit(x_train) model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28, 1)), tf.keras.layers.Dense(128, activation=tf.keras.activations.relu), tf.keras.layers.Dense(10, activation=tf.keras.activations.softmax) ]) model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=[tf.keras.metrics.sparse_categorical_accuracy]) model.fit(image_gen_train.flow(x_train, y_train, batch_size=32), epochs=5, validation_data=(x_test, y_test), validation_freq=1) model.summary()
2 斷點續訓,存取模型
保存模型:借助 tensorflow 給出的回調函數,直接保存參數和網絡。
tf.keras.callbacks.ModelCheckpoint( filepath=路徑文件名, save_weights_only=True, monitor='val_loss', # val_loss or loss save_best_only=True) history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1, callbacks=[cp_callback])
注:monitor 配合 save_best_only 可以保存最優模型,包括訓練損失最小模型、測試損失最小模型、訓練准確率最高模型、測試准確率最高模型等。
讀取模型:
checkpoint_save_path = './checkpoint/mnist.ckpt' if os.path.exists(checkpoint_save_path + '.index'): print('----------load the model----------') model.load_weights(checkpoint_save_path)
示例:
import tensorflow as tf import os mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation=tf.keras.activations.relu), tf.keras.layers.Dense(10, activation=tf.keras.activations.softmax) ]) model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=[tf.keras.metrics.sparse_categorical_accuracy]) checkpoint_save_path = './checkpoint/mnist.ckpt' if os.path.exists(checkpoint_save_path + '.index'): print('----------load the model----------') model.load_weights(checkpoint_save_path) cp_callback = tf.keras.callbacks.ModelCheckpoint( filepath=checkpoint_save_path, save_weights_only=True, monitor='val_loss', save_best_only=True ) history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1, callbacks=[cp_callback]) model.summary()
3 參數提取,寫入文本
np.set_printoptions(threshold=np.inf) # 超過多少省略顯示,np.inf表示無限大 print(model.trainable_variables) # 模型中可訓練的參數 file = open('./weigths.txt', 'w') for v in model.trainable_variables: file.write(str(v.name) + '\n') file.write(str(v.shape) + '\n') file.write(str(v.numpy()) + '\n') file.close()
4 acc/loss 可視化
history = model.fit()
history:
訓練集loss: loss
測試集loss: val_loss
訓練集准確率: sparse_categorical_accuracy
測試集准確率: val_sparse_categorical_accuracy
acc = history.history['sparse_categorical_accuracy'] val_acc = history.history['val_sparse_categorical_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] # show plt.figure(figsize=(8, 8)) plt.subplot(1, 2, 1) plt.plot(acc, label='Training Accuracy') plt.plot(val_acc, label='Validation Accuracy') plt.title('Training and Validation Accuracy') plt.legend() plt.subplot(1, 2, 2) plt.plot(loss, label='Training loss') plt.plot(val_loss, label='Validation loss') plt.title('Training and Validation loss') plt.legend() plt.show()
5 實現給圖識物的應用程序
輸入一張手寫數字圖片,輸出識別值:
import tensorflow as tf import os import numpy as np from PIL import Image from matplotlib import pyplot as plt model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation=tf.keras.activations.relu), tf.keras.layers.Dense(10, activation=tf.keras.activations.softmax) ]) checkpoint_save_path = './checkpoint/mnist.ckpt.index' model.load_weights(checkpoint_save_path) preNum = int(input('input the number of test pictures:')) for i in range(preNum): image_path = input('the path of test picture:') img = Image.open(image_path) img = img.resize((28, 28), Image.ANTIALIAS) img_arr = np.array(img.convert('L')) for i in range(28): for j in range(28): if img_arr[i][j] < 200: img_arr[i][j] = 255 else: img_arr[i][j] = 0 img_arr = img_arr / 255.0 x_predict = img_arr[tf.newaxis, ...] result = model.predict(x_predict) pred = tf.argmax(result, axis=1) print('\n') tf.print(pred)