第四讲网络八股拓展

人工智能113

第四讲网络八股拓展

目标:

  1. 自制数据集,解决本领域应用
  2. 数据增强,扩充数据集
  3. 断点续训,存取模型
  4. 参数提取,把参数存入文本
  5. acc/loss可视化,查看训练效果
  6. 应用程序,给图识物

1、自制数据集


def generateds(图片路径, 标签文件)

在自制本地数据集之前,先观察数据集的结构。
第四讲网络八股拓展
由上图,第一列value[0]用于索引到每张图片,value[1]这一列就是每张图片对应的标签。
代码实现:

def generateds(path, txt):

    f = open(txt, 'r')

    contents = f.readlines()
    f.close()
    x, y_ = [], []

    for content in contents:

        value = content.split()

        img_path = path + value[0]

        img = Image.open(img_path)

        img = np.array(img.convert('L'))

        img = img / 255.

        x.append(img)

        y_.append(value[1])

        print('loading:' + content)

    x = np.array(x)

    y_ = np.array(y_)
    y_ = y_.astype(np.int64)
    return x, y_
import tensorflow as tf
from PIL import Image
import numpy as np
import os

train_path = './mnist_image_label/mnist_train_jpg_60000/'
train_txt = './mnist_image_label/mnist_train_jpg_60000.txt'

x_train_savepath = './mnist_image_label/mnist_x_train.npy'
y_train_savepath = './mnist_image_label/mnist_y_train.npy'

test_path = './mnist_image_label/mnist_test_jpg_10000/'
test_txt = './mnist_image_label/mnist_test_jpg_10000.txt'
x_test_savepath = './mnist_image_label/mnist_x_test.npy'
y_test_savepath = './mnist_image_label/mnist_y_test.npy'

def generateds(path, txt):
    f = open(txt, 'r')
    contents = f.readlines()
    f.close()
    x, y_ = [], []
    for content in contents:
        value = content.split()
        img_path = path + value[0]
        img = Image.open(img_path)
        img = np.array(img.convert('L'))
        img = img / 255.

        x.append(img)
        y_.append(value[1])
        print('loading : ' + content)

    x = np.array(x)
    y_ = np.array(y_)
    y_ = y_.astype(np.int64)
    return x, y_

if os.path.exists(x_train_savepath) and os.path.exists(y_train_savepath) and os.path.exists(
        x_test_savepath) and os.path.exists(y_test_savepath):
    print('-------------Load Datasets-----------------')
    x_train_save = np.load(x_train_savepath)
    y_train = np.load(y_train_savepath)
    x_test_save = np.load(x_test_savepath)
    y_test = np.load(y_test_savepath)
    x_train = np.reshape(x_train_save, (len(x_train_save), 28, 28))
    x_test = np.reshape(x_test_save, (len(x_test_save), 28, 28))

else:
    print('-------------Generate Datasets-----------------')
    x_train, y_train = generateds(train_path, train_txt)
    x_test, y_test = generateds(test_path, test_txt)

    print('-------------Save Datasets-----------------')
    x_train_save = np.reshape(x_train, (len(x_train), -1))
    x_test_save = np.reshape(x_test, (len(x_test), -1))
    np.save(x_train_savepath, x_train_save)
    np.save(y_train_savepath, y_train)
    np.save(x_test_savepath, x_test_save)
    np.save(y_test_savepath, y_test)

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()

第四讲网络八股拓展
第一次运行路径中没有npy数据集,创建数据集
第四讲网络八股拓展

2、数据增强

数据增强可以帮助拓展数据集,对图像的增强就是对图像的简单形变,用来因对拍照角度不同引起的图片变形。
TensorFlow2给出了数据增强函数:

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)

model.fit(image_gen_train.flow(x_train, y_train, batch_size=32), ...)

例:

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
image_gen_train = ImageDataGenerator(
    rescale=1. / 1.,
    rotation_range=45,
    width_shift_range=.15,
    height_shift_range=.15,
    horizontal_flip=False,
    zoom_range=0.5
)
image_gen_train.fit(x_train)
model.fit(image_gen_train.flow(x_train, y_train, batch_size=32),
            epochs=5,
             validation_data=(x_test, y_test),
            validation_freq=1)

3、断点续训

断点续训可以存取模型。
读取模型:

load_weights(路径文件名)

checkpoint_save_path = "./checkpoint/mnist.ckpt"

if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')

    model.load_weights(checkpoint_save_path)

保存模型:

tf.keras.callbacks.ModelCheckpoint(
    filepath=路径文件名,

    save_weights_only=True/False,

    save_best_only=True/False)

history = model.fit(callbacks=[cp_callback])

例:

cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
                                                 save_weights_only=True,
                                                 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])

完整代码:

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(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              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,
                                                 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()

保存模型之后再次运行会提升准确率。

4、参数提取

把参数存入文本
提取可训练参数:


model.trainable_variables

设置print输出格式:


np.set_printoptions(threshold=超过多少省略显示)

np.set_printoptions(threshold=np.inf)
print(model.trainable_variables)

file = open('./weights.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()

5、acc和loss可视化

在fit函数执行过程中,已经记录了
训练集loss:loss
测试集loss:val_loss
训练集准确率:sparse_categorical_accuracy
测试集准确率:val_sparse_categorical_accuracy
可以用history.history提取出来

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

在断点续训的基础上加上画图

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

np.set_printoptions(threshold=np.inf)

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(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              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,
                                                 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()

print(model.trainable_variables)
file = open('./weights.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()

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

6、应用程序,给图识物

这里希望输入一张手写照片,神经网络能输别出数字结果

TensorFlow给出了predict函数,它可以根据输入特征给出预测结果。

predict(输入特征, batch_size = 整数)

第一步:复现模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')])

第二步:加载参数
model.load_weights(model_save_path)

第三步:预测结果
result = model.predict(x_predict)

图片识别:

from PIL import Image
import numpy as np
import tensorflow as tf

model_save_path = './checkpoint/mnist.ckpt'

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')])

model.load_weights(model_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)

Original: https://blog.csdn.net/csd_NB/article/details/122894693
Author: 贤菜salty
Title: 第四讲网络八股拓展



相关阅读

Title: tensorflow-gpu 2.3.0安装 及 相关对应版本库安装(Anaconda安装)

目录
如需转载,请标明出处,谢谢。
一、安装tensorflow-gpu2.3.0
二、配置其他相关的库
很多人以为安装完tensorflow-gpu就是一切都结束了,但是殊不知,python中的很多库,比如numpy,matplotlib等库,就与我们的tensorflow的版本有对应
总结

如需转载,请标明出处,谢谢。

对于anaconda的下载,网上的教程很多,而且很方便,我就不在这里赘述了

一、安装tensorflow-gpu2.3.0

打开我们的anaconda的控制台,点击这个蓝圈的这个,输入下方的代码

第四讲网络八股拓展

在这里我创造了一个虚拟环境去安装相关的包,大家按照自己的需要来,代码如下

conda create -n ttt python=3.6

;ttt是自己创建的环境的名字(大家自行取即可),python=3.6 代表创建的python版本为3.6

第四讲网络八股拓展

输入y代表同意创建

接下来我们输入下面这行代码去进入我们的虚拟环境中

activate ttt

在安装tensorflow-gpu2.3.0之前,必须要先配置好cudatoolkit和cudnn,这个可以直接在我们的anaconda中下载,我看网上有很多去官网下得之类的方法,反正我是没搞起。我在这篇文献对应的版本都会在下面呈现出来滴

我们输入以下代码(可能会下载比较慢,大家可以耐心等待)

conda install cudatoolkit=10.1 cudnn=7.6.5

下载完成功之后,我们就可以安装tensorflow-gpu2.3.0啦,控制台中输入下方的代码 ,加入清华源下载更快哦

pip install --default-time=300 tensorflow-gpu==2.3.0 keras==2.4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple

可以输入conda list 去查看我们安装的相关库

第四讲网络八股拓展

我们接着输入python,可以进入我们python的编译器中

第四讲网络八股拓展

输入import tensorflow来查看安装情况,可以使用如下代码判断是否安装成功,代码原链接如下

import tensorflow as tf
tensorflow_version = tf.__version__
gpu_available = tf.test.is_gpu_available()
print('tensorflow version:',tensorflow_version, '\tGPU available:', gpu_available)
a = tf.constant([1.0, 2.0], name='a')
b = tf.constant([1.0, 2.0], name='b')
result = tf.add(a,b, name='add')
print(result)

https://blog.csdn.net/zeronose/article/details/117336507 第四讲网络八股拓展https://blog.csdn.net/zeronose/article/details/117336507 ;

关于如何在pycharm中配置我们创建的tf2环境,可以参照我这篇文章(如下),原理是相同的

pyQt5 + pyUIC + pycharm 安装心得(Anaconda安装)_G氏yousa的博客-CSDN博客_pyqt5 pyuic 第四讲网络八股拓展https://blog.csdn.net/m0_51440939/article/details/125104403 ;

二、配置其他相关的库

很多人以为安装完tensorflow-gpu就是一切都结束了,但是殊不知,python中的很多库,比如numpy,matplotlib等库,就与我们的tensorflow的版本有对应关系,如下是我总结出来的适配版本

numpy==1.18.5
matplotlib==3.3.3
seaborn==0.9.0
pandas==1.1.5
scikit-learn==0.24.2

&#x5927;&#x5BB6;&#x6309;&#x7167;&#x4E0A;&#x9762;&#x7684;&#x5BF9;&#x5E94;&#x7248;&#x672C;&#x5B89;&#x88C5;&#x5373;&#x53EF;&#xFF0C;&#x540E;&#x9762;&#x5E26;&#x4E0A;&#x6E05;&#x534E;&#x6E90;&#x4E0B;&#x8F7D;&#x66F4;&#x5FEB;&#x54E6;&#xFF01;

总结

如果这篇文章对您有帮助的话,麻烦点一个小赞,嘿嘿谢谢大家。如果有其他问题没有解决,都可以在下方留言,我会尽力帮大家解决。

第四讲网络八股拓展

Original: https://blog.csdn.net/m0_51440939/article/details/125695072
Author: G氏yousa
Title: tensorflow-gpu 2.3.0安装 及 相关对应版本库安装(Anaconda安装)