IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> Tensorflow2.0实现断点续训 -> 正文阅读

[人工智能]Tensorflow2.0实现断点续训

参考: https://www.bilibili.com/video/BV16A41157LW?p=17
视频及课件来源 北京大学 曹建
使用的识别图片
请添加图片描述

获取训练数据集

def get_mnist_data():
    # 参考: https://www.codenong.com/53310656/
    # 获取数据 return  (x_train, y_train), (x_test, y_test)
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
    # 数据归一化 0-255之间的灰度值变成 变成0 或者 1
    # 把输入特征的数值变小 更利于神经网络的吸收
    x_train, x_test = (x_train > 128).astype(int), (x_test > 128).astype(int)
    return (x_train, y_train), (x_test, y_test)

加载训练模型

def local_load_weights(checkpoint_save_path):
    local_weights_model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    local_weights_model.compile(optimizer='adam',
                                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                                metrics=['sparse_categorical_accuracy'])
    if os.path.exists(checkpoint_save_path + '.index'):
        tf.print('-------------load the model-----------------')
        local_weights_model.load_weights(checkpoint_save_path)
    return local_weights_model

断点续练

def weights_train(local_weights_model, checkpoint_save_path):
    # 回调函数 用于保存模型
    cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,  # 文件存储路径
                                                     save_weights_only=True,  # 是否只保留模型参数
                                                     save_best_only=True)  # 是否只保留最优结果
    (x_train, y_train), (x_test, y_test) = get_mnist_data()
    history = local_weights_model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test),
                                      validation_freq=1,
                                      callbacks=[cp_callback])  # fit中假如回调函数

还有一种方法
加载模型

def local_load_model(model_path):
    if os.path.exists(model_path + '/saved_model.pb'):
        tf.print('-------------load the model-----------------')
        local_model = tf.keras.models.load_model(model_path)
    else:
        local_model = tf.keras.models.Sequential([
            tf.keras.layers.Flatten(),  # 拉直层  将数据拉直成1维
            tf.keras.layers.Dense(128, activation='relu'),
            tf.keras.layers.Dense(10, activation='softmax')
        ])
        local_model.compile(optimizer='adam',
                            loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                            metrics=['sparse_categorical_accuracy'])
    return local_model

训练

 (x_train, y_train), (x_test, y_test) = get_mnist_data()
    history = local_model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test),
                              validation_freq=1)

整个文件如下

# -*- coding: utf-8 -*-

import tensorflow as tf
import os
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt


def get_mnist_data():
    # 参考: https://www.codenong.com/53310656/
    # 获取数据 return  (x_train, y_train), (x_test, y_test)
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
    # 数据归一化 0-255之间的灰度值变成 变成0 或者 1
    # 把输入特征的数值变小 更利于神经网络的吸收
    x_train, x_test = (x_train > 128).astype(int), (x_test > 128).astype(int)
    return (x_train, y_train), (x_test, y_test)


def get_local_image():
    img = Image.open('./data/image/2.jpg')  # 载入自己的图片
    img = img.resize((28, 28))  # 设置图片大小
    gray_img = img.convert('L')
    mun_img = np.array(gray_img)
    # convert.show()
    mun_img = (mun_img > 64).astype(int)
    x_test__reshape = mun_img.reshape(1, 28, 28)
    return x_test__reshape


def local_load_weights(checkpoint_save_path):
    local_weights_model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    local_weights_model.compile(optimizer='adam',
                                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                                metrics=['sparse_categorical_accuracy'])
    if os.path.exists(checkpoint_save_path + '.index'):
        tf.print('-------------load the model-----------------')
        local_weights_model.load_weights(checkpoint_save_path)
        tf.print("加载文件参数")
    return local_weights_model


def local_load_model(model_path):
    if os.path.exists(model_path + '/saved_model.pb'):
        tf.print('-------------load the model-----------------')
        local_model = tf.keras.models.load_model(model_path)
    else:
        local_model = tf.keras.models.Sequential([
            tf.keras.layers.Flatten(),  # 拉直层  将数据拉直成1维
            tf.keras.layers.Dense(128, activation='relu'),
            tf.keras.layers.Dense(10, activation='softmax')
        ])
        local_model.compile(optimizer='adam',
                            loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                            metrics=['sparse_categorical_accuracy'])
    return local_model


# 断点续练
def weights_train(local_weights_model, checkpoint_save_path):
    # 回调函数 用于保存模型
    cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,  # 文件存储路径
                                                     save_weights_only=True,  # 是否只保留模型参数
                                                     save_best_only=True)  # 是否只保留最优结果
    (x_train, y_train), (x_test, y_test) = get_mnist_data()
    history = local_weights_model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test),
                                      validation_freq=1,
                                      callbacks=[cp_callback])  # fit中假如回调函数
    local_weights_model.summary()
    show_train_line(history)


def show_train_line(history):
    # 显示训练集和验证集的acc和loss曲线
    acc = history.history['sparse_categorical_accuracy']
    val_acc = history.history['val_sparse_categorical_accuracy']
    loss = history.history['loss']
    val_loss = history.history['val_loss']

    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()


def forecast_demo(model):
    # 预测
    local_image = get_local_image()
    loaded_evaluate = model.predict(local_image)
    print(loaded_evaluate)
    prediction = np.argmax(loaded_evaluate, axis=1)  # 找出最大值
    print('预测结果:', prediction)


def demo_1():
    checkpoint_path = "./data/model/checkpoint/mnist.ckpt"
    local_weights_model = local_load_weights(checkpoint_path)
    # 继续训练
    weights_train(local_weights_model, checkpoint_path)
    forecast_demo(local_weights_model)


def demo_2():
    model_path = "./data/model/breakpoint"
    local_model = local_load_model(model_path)
    (x_train, y_train), (x_test, y_test) = get_mnist_data()
    history = local_model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test),
                              validation_freq=1)
    show_train_line(history)
    local_model.summary()
    #   保存模型
    local_model.save(model_path, save_format="tf")
    forecast_demo(local_model)


if __name__ == '__main__':
    demo_1()
    # demo_2()
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-02-05 21:44:41  更:2022-02-05 21:47:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 4:01:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码