Tensorflow建立数据集(mnist为例)

网上的mnist的demo大部分都是按照实战google那本来的,但是那个在数据集的处理上用的是TensorFlow的官方api,我们在正常做标签的时候并不一定要那样做,本文讲解了两种标签方式区别于实战google的demo。
folder方式:
ROOT_FOLDER
|-------- SUBFOLDER (CLASS 0)
| |
| | ----- image1.jpg
| | ----- image2.jpg
| | ----- etc…
|
|-------- SUBFOLDER (CLASS 1)
| |
| | ----- image1.jpg
| | ----- image2.jpg
| | ----- etc…

text(file)方式:

  • From a plain text file, that will list all images with their class ID:

    /path/to/image/1.jpg CLASS_ID
    /path/to/image/2.jpg CLASS_ID
    /path/to/image/3.jpg CLASS_ID
    /path/to/image/4.jpg CLASS_ID
    etc…
    这两种方式先记住,只选用一种即可,
    demo如下,简单的已不再注释,可以参考这篇文章 https://blog.csdn.net/qq_32166779/article/details/83035409

from __future__ import print_function

import tensorflow as tf
import os


MODE = 'folder' # or 'file',选择方式取决于 上面我写的folder方式还是text方式.
DATASET_PATH = 'MNIST_data' # the dataset file or root folder path.

N_CLASSES = 2
IMG_HEIGHT = 64
IMG_WIDTH = 64
CHANNELS = 3

def read_images(dataset_path, mode, batch_size):
    imagepaths, labels = list(), list()
    if mode == 'file':

        data = open(dataset_path, 'r').read().splitlines()
        for d in data:
            imagepaths.append(d.split(' ')[0])
            labels.append(int(d.split(' ')[1]))
    elif mode == 'folder':

        label = 0
        try:  # Python 2
            classes = sorted(os.walk(dataset_path).next()[1])
        except Exception:  # Python 3
            classes = sorted(os.walk(dataset_path).__next__()[1])

        for c in classes:
            c_dir = os.path.join(dataset_path, c)
            try:  # Python 2
                walk = os.walk(c_dir).next()
            except Exception:  # Python 3
                walk = os.walk(c_dir).__next__()
            # 将每个图像添加到训练集
            for sample in walk[2]:

                if sample.endswith('.jpg') or sample.endswith('.jpeg'):
                    imagepaths.append(os.path.join(c_dir, sample))
                    labels.append(label)
            label += 1
    else:
        raise Exception("Unknown mode.")

    imagepaths = tf.convert_to_tensor(imagepaths, dtype=tf.string)
    labels = tf.convert_to_tensor(labels, dtype=tf.int32)
    # Build a TF Queue, shuffle data
    image, label = tf.train.slice_input_producer([imagepaths, labels],
                                                 shuffle=True)

    image = tf.read_file(image)
    image = tf.image.decode_jpeg(image, channels=CHANNELS)

    image = tf.image.resize_images(image, [IMG_HEIGHT, IMG_WIDTH])

    # 规范化
    image = image * 1.0/127.5 - 1.0

    # Create batches
    X, Y = tf.train.batch([image, label], batch_size=batch_size,
                          capacity=batch_size * 8,
                          num_threads=4)

    return X, Y


learning_rate = 0.001
num_steps = 10000
batch_size = 128
display_step = 100


dropout = 0.75 # Dropout

X, Y = read_images(DATASET_PATH, MODE, batch_size)


# Create model
def conv_net(x, n_classes, dropout, reuse, is_training):

    with tf.variable_scope('ConvNet', reuse=reuse):

        # Convolution Layer with 32 filters and a kernel size of 5
        conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

        # Convolution Layer with 32 filters and a kernel size of 5
        conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv2 = tf.layers.max_pooling2d(conv2, 2, 2)

        # Flatten the data to a 1-D vector for the fully connected layer
        fc1 = tf.contrib.layers.flatten(conv2)

        # Fully connected layer (in contrib folder for now)
        fc1 = tf.layers.dense(fc1, 1024)
        # Apply Dropout (if is_training is False, dropout is not applied)
        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)

        # Output layer, class prediction
        out = tf.layers.dense(fc1, n_classes)
        # Because 'softmax_cross_entropy_with_logits' already apply softmax,
        # we only apply softmax to testing network
        out = tf.nn.softmax(out) if not is_training else out

    return out


#因为Dropout在训练和预测时有不同的行为,我们
#需要创建两个共享相同权重的不同计算图。.


logits_train = conv_net(X, N_CLASSES, dropout, reuse=False, is_training=True)

logits_test = conv_net(X, N_CLASSES, dropout, reuse=True, is_training=False)


loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
    logits=logits_train, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)

correct_pred = tf.equal(tf.argmax(logits_test, 1), tf.cast(Y, tf.int64))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

init = tf.global_variables_initializer()

saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(init)
    # 启动线程
    tf.train.start_queue_runners()
    for step in range(1, num_steps+1):

        if step % display_step == 0:
            _, loss, acc = sess.run([train_op, loss_op, accuracy])
            print("Step " + str(step) + ", Minibatch Loss= " + \
                  "{:.4f}".format(loss) + ", Training Accuracy= " + \
                  "{:.3f}".format(acc))
        else:
            sess.run(train_op)
    print("Optimization Finished!")
    saver.save(sess, 'my_tf_model')

猜你喜欢

转载自blog.csdn.net/qq_32166779/article/details/83341803