美文网首页
【深度学习TensorFlow(二)】计算机视觉入门——全连接神

【深度学习TensorFlow(二)】计算机视觉入门——全连接神

作者: Geekero | 来源:发表于2021-01-15 09:20 被阅读0次

学习自中国大学MOOC TensorFlow学习课程

这里采用的是全连接神经网络, 优点简单,缺点只能处理位于画面正中的问题,无法处理画面发生旋转的情况



%config IPCompleter.greedy=True   #TAB键代码自动提示
from tensorflow import keras
import tensorflow as tf
import numpy as np

一、加载Fashion MNIST数据集

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

print(train_images.shape) #6万张图,28*28像素

    (60000, 28, 28)
    


print(train_images[0]) #灰度值


    [[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   1   0   0  13  73   0
    ....
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]]

查看一下数据情况:

print(train_labels.shape)


    (60000,)
    
print(train_labels[:5])


    [9 0 0 3 0]
import matplotlib.pyplot as plt
plt.imshow(train_images[0])

    <matplotlib.image.AxesImage at 0x1856ebf7eb0>
output_10_1.png
plt.imshow(train_images[1])

    <matplotlib.image.AxesImage at 0x1856ef37490>
print(test_images.shape)
    (10000, 28, 28)

二、数据归一化

training_images  = train_images / 255.0
testing_images = test_images / 255.0

三、构建神经元模型

#三层结构 (全连接的网络结构)
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)), #输入层
    keras.layers.Dense(128, activation=tf.nn.relu), #中间层自己定神经元数目;中间层激活函数relu,只有输入是正数再进行输出
    keras.layers.Dense(10, activation=tf.nn.softmax) #输出层,10个类别对应10个神经元;输出层激活函数是Softmax,输出压缩在0~1的概率值
])
#或者这样写
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))#输入层
model.add(keras.layers.Dense(128, activation=tf.nn.relu)) #中间层自己定神经元数目;中间层激活函数relu,只有输入是正数再进行输出
model.add(keras.layers.Dense(10, activation=tf.nn.softmax)) #输出层,10个类别对应10个神经元;输出层激活函数是Softmax,输出压缩在0~1的概率值
Sequential: That defines a SEQUENCE of layers in the neural network

Flatten: Remember earlier where our images were a square, when you printed them out? Flatten just takes that square and turns it into a 1 dimensional set.

Dense: Adds a layer of neurons

Each layer of neurons need an activation function to tell them what to do. There's lots of options, but just use these for now.

Relu effectively means "If X>0 return X, else return 0" -- so what it does it it only passes values 0 or greater to the next layer in the network.

Softmax takes a set of values, and effectively picks the biggest one, so, for example, if the output of the last layer looks like [0.1, 0.1, 0.05, 0.1, 9.5, 0.1, 0.05, 0.05, 0.05], it saves you from fishing through it looking for the biggest value, and turns it into [0,0,0,0,1,0,0,0,0] -- The goal is to save a lot of coding!

Sequential。这定义了神经网络中的层数序列。一开始学习神经元网络总是使用序列模型。

Flatten。还记得上面将图像打印出来的时候是一个正方形吗?扁平化只是把这个正方形变成了一个一维的集合。把二维数组变成一维数组。

Dense:增加一层神经元。

每一层神经元都需要一个激活函数 activation来告诉它们输出什么。有很多选项,但目前只用这些(relu和softmax)。

Relu的意思是 "如果X>0返回X,否则返回0"--所以它的作用是它只把大于0的值传递给网络中的下一层,小于0的也当作0。

Softmax激活函数接收到一组值后,选择其中最大的一个输出。例如,上一层的输出为[0.1, 0.1, 0.05, 0.1, 9.5, 0.1, 0.05, 0.05, 0.05],Softmax就省去了你在其中寻找最大的值,并把它变成[0,0,0,0,0,1,0,0,0,0,0] Softmax的意思是 "如果X>0,则返回X,否则返回0" -- 所以它的作用是只把0或更大的值传给下一层的网络。--其目的是节省大量的编码!

The next thing to do, now the model is defined, is to actually build it. You do this by compiling it with an optimizer and loss function as before -- and then you train it by calling *model.fit * asking it to fit your training data to your training labels -- i.e. have it figure out the relationship between the training data and its actual labels, so in future if you have data that looks like the training data, then it can make a prediction for what that data would look like.
#查看模型结构
model.summary()
#(总像素784 + bias 1) * 中间层神经元128 = 100352个parameter
#每一层(除了输出层外)都有一个bias,每一层都加一个(截距)
#1290个paras = (中间层128个神经元 + bias 1) * 输出层10个神经元
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_2 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 128)               100480    
_________________________________________________________________
dense_5 (Dense)              (None, 10)                1290      
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
#指定优化器和损失函数编译它
#model.compile(optimizer='adam', loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.compile(optimizer=tf.optimizers.Adam(), loss=tf.losses.sparse_categorical_crossentropy, metrics=['accuracy'])

#Adam为常用的优化函数;输出为类别判断是用categorical,整数集合用sparse_categorical_crossentropy
#如果输出数据为one_hot类 [0,0,0,0,0,0,0,1],用sparse_categorical_crossentropy

四、训练和评估模型

#训练模型
model.fit(training_images, train_labels, epochs=5)
#需要保证loss在减低,精度accuracy在上升
    Epoch 1/5
    1875/1875 [==============================] - 1s 561us/step - loss: 0.4991 - accuracy: 0.8242
    Epoch 2/5
    1875/1875 [==============================] - 1s 582us/step - loss: 0.3775 - accuracy: 0.8648
    Epoch 3/5
    1875/1875 [==============================] - 1s 552us/step - loss: 0.3374 - accuracy: 0.87620s - loss: 0.3374 - accuracy
    Epoch 4/5
    1875/1875 [==============================] - 1s 618us/step - loss: 0.3123 - accuracy: 0.8853
    Epoch 5/5
    1875/1875 [==============================] - 1s 579us/step - loss: 0.2943 - accuracy: 0.8910
    
    <tensorflow.python.keras.callbacks.History at 0x1857ec81b20>

#评估模型
model.evaluate(testing_images, test_labels)

    313/313 [==============================] - 0s 408us/step - loss: 0.3441 - accuracy: 0.8809
    
    [0.344112366437912, 0.8809000253677368]

五、判断图像类别

testing_images[0].shape
    (28, 28)

np.shape([testing_images[1]])
    (1, 28, 28)

判断类别的标签:

# 0 T-shirt/top, 1 Trouser/pants, 2 Pullover shirt, 3 Dress, 4 Coat, 5 Sandal, 7 Sneaker, 8 Bag, 9 Ankle boot
model.predict(testing_images[0].reshape(1,28,28,1)) #因为卷积层要四维的输入

    array([[1.3536342e-07, 7.5559107e-08, 5.4793019e-09, 2.5590365e-09,
            2.1810264e-08, 1.4773364e-03, 3.4851388e-07, 6.3587718e-02,
            3.2722826e-06, 9.3493104e-01]], dtype=float32)

#找到最大概率值
import numpy as np
np.argmax(model.predict(testing_images[0].reshape(1,28,28,1)))
    9

#检查
print(test_labels[0])
    9
   
#打印图片看看
plt.imshow(testing_images[0])
    <matplotlib.image.AxesImage at 0x1857f0f2e80>

六、自动终止训练

#防止过拟合
class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if (logs.get('loss')<0.4):
            print("\nLoss is low so cancelling training!")
            self.model.stop_training = True

callbacks = myCallback() #生成实例

mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Nomalize
training_images = train_images/255
testing_images = test_images/255

#构建模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

#指定优化函数和损失函数
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')

#训练模型(增加终止训练的callback函数)
model.fit(training_images, train_labels, epochs=5, callbacks=[callbacks])

    Epoch 1/5
    1875/1875 [==============================] - 4s 2ms/step - loss: 0.4759
    Epoch 2/5
    1868/1875 [============================>.] - ETA: 0s - loss: 0.3573
    Loss is low so cancelling training!
    1875/1875 [==============================] - 4s 2ms/step - loss: 0.3572

    <tensorflow.python.keras.callbacks.History at 0x1850f05e910>

相关文章

网友评论

      本文标题:【深度学习TensorFlow(二)】计算机视觉入门——全连接神

      本文链接:https://www.haomeiwen.com/subject/mxayaktx.html