欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

深度学习框架tensorflow实战(Cifar图像分类任务)

程序员文章站 2024-03-19 23:10:40
...

~Cifar分类前需理解:

Numpy中stack(),hstack(),vstack()函数详解:https://blog.csdn.net/csdn15698845876/article/details/73380803

python 的np.mean()解析:https://blog.csdn.net/fu6543210/article/details/80223711

关于numpy中random-seed函数:https://blog.csdn.net/IAMoldpan/article/details/78429165

数据源:The CIFAR-10 dataset

http://www.cs.toronto.edu/~kriz/cifar.html
CIFAR-10数据集包含10个类中的60000张32x32彩色图像,每个类包含6000张图像。训练图像50000张,测试图像10000张。
深度学习框架tensorflow实战(Cifar图像分类任务)

返回字典类型的读取

import pickle

def unpickle(file):
    fo = open(file, 'rb')
    dict = pickle.load(fo, encoding='latin1')
    fo.close()
    return dict

在字典结构中,每一张图片是以被展开的形式存储,即一张32乘以32乘以3的图片被展开成3072长度的list,每一个数据的格式为unit8,前1024为红色通道,中间1024为绿色通道,后1024为蓝色通道。

对图像进行预处理

对数据进行标准化操作,按照一定比例进行缩放,使其落入一个特定的区域,便于操作处理。提高了处理速度。

'''
对RGB图像进行处理,输入5000乘3072返回5000乘574(24乘24),后面会用灰度图像输出
'''
import numpy as np

def clean(data):
    imgs = data.reshape(data.shape[0], 3, 32, 32)
    grayscale_imgs = imgs.mean(1)
    cropped_imgs = grayscale_imgs[:, 4:28, 4:28]
    img_data = cropped_imgs.reshape(data.shape[0], -1)
    img_size = np.shape(img_data)[1]
    means = np.mean(img_data, axis=1)
    meansT = means.reshape(len(means), 1)
    stds = np.std(img_data, axis=1)
    stdsT = stds.reshape(len(stds), 1)
    adj_stds = np.maximum(stdsT, 1.0 / np.sqrt(img_size))
    normalized = (img_data - meansT) / adj_stds
    return normalized

读取数据

def read_data(directory):
    names = unpickle('{}/batches.meta'.format(directory))['label_names']
    print('names', names)

    data, labels = [], []
    for i in range(1, 6):
        filename = '{}/data_batch_{}'.format(directory, i)
        batch_data = unpickle(filename)
        if len(data) > 0:                          #拼接五个文件的数据
            data = np.vstack((data, batch_data['data']))
            labels = np.hstack((labels, batch_data['labels']))
        else:
            data = batch_data['data']
            labels = batch_data['labels']

    print(np.shape(data), np.shape(labels))

    data = clean(data)
    data = data.astype(np.float32)
    return names, data, labels

显示数据

import matplotlib.pyplot as plt
import random
random.seed(1)   #指定random.seed后,每次运行产生的图片都会是那几个图像,不会每次都发生改变

names, data, labels = read_data('./cifar-10-batches-py')

def show_some_examples(names, data, labels):
    plt.figure()
    rows, cols = 4, 4
    random_idxs = random.sample(range(len(data)), rows * cols)
    for i in range(rows * cols):
        plt.subplot(rows, cols, i + 1)
        j = random_idxs[i]
        plt.title(names[labels[j]])
        img = np.reshape(data[j, :], (24, 24))
        plt.imshow(img, cmap='Greys_r')
        plt.axis('off')
    plt.tight_layout()
    plt.savefig('cifar_examples.png')
    plt.show()

show_some_examples(names, data, labels)

random.sample(sequence,k)函数

从指定序列中随机获取指定长度的片段。

plt.subplot(r,c,num)函数

当需要包含多个子图时使用,分成r行和c列,从左到右从上到下对每个子区进行编号,num指定创建的对象在哪个区域。

输出结果:

names ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
(50000, 3072) (50000,)

深度学习框架tensorflow实战(Cifar图像分类任务)

显示卷积后的图片以及参数的函数


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

names, data, labels = read_data('./cifar-10-batches-py')


def show_conv_results(data, filename=None):
    plt.figure(1)
    rows, cols = 4, 8
    for i in range(np.shape(data)[3]):
        img = data[0, :, :, i]
        plt.subplot(rows, cols, i + 1)
        plt.imshow(img, cmap='Greys_r', interpolation='none')
        plt.axis('off')
    if filename:
        plt.savefig(filename)
    else:
        plt.show()


def show_weights(W, filename=None):
    plt.figure()
    rows, cols = 4, 8
    for i in range(np.shape(W)[3]):
        img = W[:, :, 0, i]
        plt.subplot(rows, cols, i + 1)
        plt.imshow(img, cmap='Greys_r', interpolation='none')
        plt.axis('off')
    if filename:
        plt.savefig(filename)
    else:
        plt.show()

读取某张图片把它显示出来

print(np.shape(data))
raw_data = data[4, :]
raw_img = np.reshape(raw_data, (24, 24))
plt.figure()
plt.imshow(raw_img, cmap='Greys_r')
plt.show()

深度学习框架tensorflow实战(Cifar图像分类任务)

卷积参数初始化,进行卷积和池化


x = tf.reshape(raw_data, shape=[-1, 24, 24, 1])
W = tf.Variable(tf.random_normal([5, 5, 1, 32]))
b = tf.Variable(tf.random_normal([32]))
'''
以下为卷积操作
'''
conv = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
conv_with_b = tf.nn.bias_add(conv, b)
conv_out = tf.nn.relu(conv_with_b)
'''
以下为池化操作
'''
k = 2
maxpool = tf.nn.max_pool(conv_out, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')

分类过程中输出四种情况

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    W_val = sess.run(W)
    print('weights:')
    show_weights(W_val)

    conv_val = sess.run(conv)
    print('convolution results:')
    print(np.shape(conv_val))
    show_conv_results(conv_val)

    conv_out_val = sess.run(conv_out)
    print('convolution with bias and relu:')
    print(np.shape(conv_out_val))
    show_conv_results(conv_out_val)

    maxpool_val = sess.run(maxpool)
    print('maxpool after all the convolutions:')
    print(np.shape(maxpool_val))
    show_conv_results(maxpool_val)

产生的32种随机卷积核:
深度学习框架tensorflow实战(Cifar图像分类任务)
经过32种卷积核卷积产生的图片
(1, 24, 24, 32)

深度学习框架tensorflow实战(Cifar图像分类任务)
通过**函数后的图片
(1, 24, 24, 32)
深度学习框架tensorflow实战(Cifar图像分类任务)
通过池化产生的图像
(1, 12, 12, 32)
深度学习框架tensorflow实战(Cifar图像分类任务)