机器学习实验 | 用tensorflow简单实现 cifar-10 数据集构建卷积神经网络

cifar-10构建卷积神经网络

说明

以下的代码包含:

1. 数据导入
2. 把数据label标记成one-hot
3. 定义神经网络
4. 定义损失
5. 定义优化器
6. 保存模型
7. 保存日志
8. 测试代码

运行环境:win10 + python 3.6 + CUDA 10.0 + Cudnn 7.5.1 + tensorflow 1.13.1

上面这个版本我在网上查到这是最稳定的,之前笔者的环境是CUDA10.1 + Cudnn7.6.5 这个会报错,具体报错没有及时截图,运行下面的代码也遇到过很多bug。

如果有读者运行下面代码遇到了某些bug,可以留言给我,如果我遇到过,我会及时给你回复如何修改bug噢!

PS:由于是在笔记本上跑,害怕跑崩了,就只是用了部分数据集进行训练,没有使用所有的训练集训练。

提供cifar-10数据集下载链接:http://www.cs.toronto.edu/~kriz/cifar.html
在这里插入图片描述
一定要注意看在什么环境下运行,就选择下载什么环境的数据集。笔者之前下载了最后一类,但是运行并不成功!

cifar-10 参考代码:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/
在这里插入图片描述
具体参考Tensorflow中文社区:http://tensorfly.cn/tfdoc/tutorials/deep_cnn.html

代码

Python 代码如下:

#coding:utf-8
import os 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
import cifar10, cifar10_input
import pickle
import numpy as np
import matplotlib.pyplot as plt
import time


batch_size = 50
max_step = 500
LR = 0.0001

# prepare data
def unpickle(file):
    with open(file, 'rb') as f:
        dict = pickle.load(f, encoding = 'latin1')
    return dict

def onehot(labels):
    # one-hot encoding
    n_sample = len(labels)
    n_class = max(labels) + 1
    onehot_labels = np.zeros((n_sample, n_class))
    onehot_labels[np.arange(n_sample), labels] = 1
    return onehot_labels

# labels = []
# X_train = []
# for i in range(1, 6):
#     files = "D:/Courses/machine learning/test/test06/cifar-10-python/cifar-10-batches-py/data_batch_" + str(i)
#     # print(files)
#     data = unpickle(files)
#     labels.append(data['labels'])
#     X_train.append(data['data'])
# print(X_train)

# input train data
data1 = unpickle('D:/Courses/machine learning/test/test06/cifar-10-python/cifar-10-batches-py/data_batch_1')
data2 = unpickle('D:/Courses/machine learning/test/test06/cifar-10-python/cifar-10-batches-py/data_batch_2')
# data3 = unpickle('D:/Courses/machine learning/test/test06/cifar-10-python/cifar-10-batches-py/data_batch_3')
# data4 = unpickle('D:/Courses/machine learning/test/test06/cifar-10-python/cifar-10-batches-py/data_batch_4')
# data5 = unpickle('D:/Courses/machine learning/test/test06/cifar-10-python/cifar-10-batches-py/data_batch_5')
# X_train = np.concatenate((data1['data'], data2['data'], data3['data'], data4['data'], data5['data']), axis=0)
# labels = np.concatenate((data1['labels'], data2['labels'], data3['labels'], data4['labels'], data5['labels']), axis=0)
X_train = np.concatenate((data1['data'], data2['data']), axis=0)
labels = np.concatenate((data1['labels'], data2['labels']), axis=0)
y_train = onehot(labels)

# input test data
test = unpickle('D:/Courses/machine learning/test/test06/cifar-10-python/cifar-10-batches-py/test_batch')
X_test = test['data'][:5000, :]
y_test = onehot(test['labels'])[:5000, :]

# define network
tf_X = tf.compat.v1.placeholder(tf.float32, shape = [None, 3072])
tf_y = tf.compat.v1.placeholder(tf.float32, shape = [None, 10])
keep_prob = tf.compat.v1.placeholder(tf.float32)  # 解决过拟合问题


# build network
W_conv = {
    'conv1':tf.Variable(tf.random.truncated_normal([3, 3, 3, 32], stddev = 0.0001)),
    'conv2':tf.Variable(tf.random.truncated_normal([3, 3, 32, 64], stddev = 0.01)),
    'fc1':tf.Variable(tf.random.truncated_normal([8*8*64, 384], stddev = 0.1)),
    'fc2':tf.Variable(tf.random.truncated_normal([384, 192], stddev = 0.1)),
    'fc3':tf.Variable(tf.random.truncated_normal([192, 10], stddev = 0.1))
}

b_conv = {
    'conv1':tf.Variable(tf.constant(0.0, dtype = tf.float32, shape = [32])),
    'conv2':tf.Variable(tf.constant(0.1, dtype = tf.float32, shape = [64])),
    'fc1':tf.Variable(tf.constant(0.1, dtype = tf.float32, shape = [384])),
    'fc2' : tf.Variable(tf.constant(0.1, dtype = tf.float32, shape=[192])),
    'fc3' : tf.Variable(tf.constant(0.0, dtype = tf.float32, shape=[10]))
}
    
x_image = tf.reshape(tf_X, [-1, 32, 32, 3])
# conv layer 1
conv1 = tf.nn.conv2d(x_image, W_conv['conv1'], strides=[1, 1, 1, 1], padding='SAME')
conv1 = tf.nn.bias_add(conv1, b_conv['conv1'])
conv1 = tf.nn.relu(conv1)
# pool layer 1
poo11 = tf.nn.avg_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
# LRN layer,Local Response Normalization
norm1 = tf.nn.lrn(poo11, 4, bias=1.0, alpha=0.001/9.0, beta=0.75)
# conv layer 2
conv2 = tf.nn.conv2d(norm1, W_conv['conv2'], strides=[1, 1, 1, 1], padding='SAME')
conv2 = tf.nn.bias_add(conv2, b_conv['conv2'])
conv2 = tf.nn.relu(conv2)
# LRN layer
norm2  = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001/9.0, beta=0.75)
# pool layer 2
pool2 = tf.nn.avg_pool(norm2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
reshape = tf.reshape(pool2, [-1, 8*8*64])
# fc 1
fc1 = tf.add(tf.matmul(reshape, W_conv['fc1']), b_conv['fc1'])
fc1 = tf.nn.relu(fc1)
# fc 2
fc2 = tf.add(tf.matmul(fc1, W_conv['fc2']), b_conv['fc2'])
fc2 = tf.nn.relu(fc2)
# fc 3
fc3 =  tf.nn.softmax(tf.add(tf.matmul(fc2, W_conv['fc3']), b_conv['fc3']))

# define loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc3, labels=tf_y))
tf.compat.v1.summary.scalar("loss", loss)

# define optimizer
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate=LR).minimize(loss)
# define evaluate
correct_pred = tf.equal(tf.argmax(fc3, 1), tf.argmax(tf_y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
tf.compat.v1.summary.scalar("accuracy", accuracy)
init = tf.global_variables_initializer()


# saver 
saver = tf.train.Saver()

isTrain = True
train_steps = 100
checkpoint_steps = 20
checkpoint_path = "D:/Courses/machine learning/test/test06/model/model.ckpt"

with tf.Session() as sess:
    sess.run(init)
    c = []
    total_batch = int(X_train.shape[0] / batch_size)
    # print(total_batch)
    start_time = time.time()
    mysummary = tf.summary.merge_all()
    f_summary = tf.summary.FileWriter(logdir="./log", graph=sess.graph)
    if isTrain:
        # sess.run(init)
        for i in range(train_steps):
            for batch in range(total_batch):
                batch_x = X_train[batch*batch_size : (batch+1)*batch_size, :]
                # print(X_train.shape)
                # print(batch_x.shape)
                # print(y_train.shape)
                # print(batch)
                # print(batch_size)
                batch_y = y_train[batch*batch_size : (batch+1)*batch_size, :]
                sess.run(optimizer, feed_dict={tf_X: batch_x, tf_y : batch_y})
                tmp_summary = sess.run(mysummary, feed_dict={tf_X: batch_x, tf_y : batch_y})
                f_summary.add_summary(summary=tmp_summary, global_step=i+1)
            if (i + 1) % checkpoint_steps == 0:
                save_path = saver.save(sess, checkpoint_path, global_step=i+1)
                # print(save_path)
            else:
                pass
            acc = sess.run(accuracy, feed_dict={tf_X: batch_x, tf_y: batch_y})
            _, loss_ = sess.run([optimizer, loss], {tf_X: batch_x, tf_y: batch_y})
            # print("Train accuracy:", acc)
            print('Step:', i, '| train loss: %.4f' % loss_, '| test accuracy: %.2f' % acc)
            c.append(acc)
            end_time = time.time()
            print('time:', (end_time - start_time))
            start_time = end_time
            print( "--------------%d " %i, "onpech is finished------------")
    else:
        sess.run(init)
        ckpt = tf.train.get_checkpoint_state(checkpoint_path)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            pass
            

    print("Optimization Finished!")

    # TEST
    test_acc = sess.run(accuracy, feed_dict={tf_X : X_test, tf_y : y_test})
    print("Testing Accuracy:", test_acc)
    plt.plot(c)
    plt.xlabel('Iter')
    plt.ylabel('Cost')
    plt.title('lr=%f, ti=%d, bs=%d, acc=%f' % (LR, train_steps,batch_size, test_acc))
    plt.tight_layout()
    plt.savefig('cnn-tf-cifar10-%s.png' % test_acc, dpi=200)

实验结果

(1)日志打印:
在这里插入图片描述
(2)模型保存:
在这里插入图片描述
(3)部分实验结果展示:
在这里插入图片描述
(4)tensorboard展示:(输入tensorboard logdir “log”)
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: tensorflow cifar-10-batches-py是一个经典的深度学习数据集,被广泛用于图像分类任务的训练和评估。 该数据集CIFAR-10数据集的Python版本,提供了10个类别的60000个32x32彩色图像。其中,50000张图像作为训练集,10000张图像作为测试集。 这个数据集是用Python编写的,并且使用了pickle库来加载和处理数据。它可以通过执行"import cifar10"来导入,并使用"cifar10.load_data()"来加载其数据。 加载数据后,可以使用TensorFlow构建一个图像分类模型。TensorFlow是一个开源的深度学习框架,可以用于构建、训练和评估机器学习模型。 使用tensorflow cifar-10-batches-py数据集,可以进行图像分类任务的实验和研究。可以结合卷积神经网络等深度学习模型,对图像进行特征提取和分类。 在训练模型时,可以使用训练集进行权重更新和优化,然后使用测试集来评估模型的性能。 总结来说,tensorflow cifar-10-batches-py是一个常用的深度学习数据集,可以用于图像分类任务的研究和实验。它结合了TensorFlow框架,提供了加载、处理和评估数据的功能。通过使用它,可以建立一个自定义的图像分类模型,并对其进行训练和评估。 ### 回答2: tensorflow cifar-10-batches-py是一个用于在tensorflow框架中处理CIFAR-10数据集的Python脚本。CIFAR-10数据集是一个广泛应用于图像分类的数据集,包含10个不同类别的影像数据,每个类别有6000个32x32大小的彩色图像。 这个Python脚本通过提供一些函数和类来加载CIFAR-10数据集,并且将图像和标签进行预处理,以便于在训练和测试模型时使用。脚本中的函数可以帮助我们将原始的二进制数据转换成可用于训练的张量形式。 该脚本提供的函数可以将CIFAR-10数据集分为训练集和测试集,并提供了一个函数用于获取下一个训练批或测试批的图像和标签。此外,该脚本还提供了一个函数用于显示CIFAR-10数据集中的图像。 使用tensorflow cifar-10-batches-py脚本,我们可以很方便地加载和预处理CIFAR-10数据集,并用于训练和测试图像分类模型。这个脚本是使用Python编写的,可以在tensorflow环境中直接使用。 ### 回答3: TensorFlowcifar-10-batches-py是一个用于训练和验证图像分类模型的数据集。它是基于CIFAR-10数据集的一个版本,其中包含50000张用于训练的图像和10000张用于验证的图像。 CIFAR-10数据集是一个常用的图像分类数据集,包含10个不同的类别,每个类别有大约6000张图像。这些类别包括:飞机、汽车、鸟类、猫、鹿、狗、青蛙、马、船和卡车。每个图像的大小为32x32像素,是彩色图像。 cifar-10-batches-py数据集通过Python脚本cifar10.py提供,它将数据集分为5个训练批次和1个验证批次。在训练过程中,可以使用这些批次中的图像进行训练,并根据验证数据集的结果来评估模型的性能。 这个数据集提供了一个方便的方式来测试和评估不同的图像分类算法和模型。使用TensorFlowcifar10.py脚本可以加载这个数据集,并提供一些函数,用于解析和处理图像数据。 在使用cifar-10-batches-py数据集进行训练时,通常会将图像数据进行预处理,例如将像素值进行归一化处理,以便于模型的训练。同时,还可以使用数据增强的技术,如随机翻转、旋转或裁剪图像,以增加数据的多样性。 总的来说,TensorFlowcifar-10-batches-py数据集是为了方便机器学习研究人员进行图像分类模型训练和验证而提供的一个常用数据集。它可以用于测试和评估不同的图像分类算法和模型的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值