猫狗大战(分出验证集的代码)下

1._input_data.py

import numpy as np
import tensorflow as tf
import os
import cv2
import matplotlib.pyplot as plt
import os
from PIL import Image

def get_files(file_dir):
    cats = []
    dogs = []
    cats_label = []
    dogs_label = []
    img_dirs = os.listdir(file_dir)#读取文件名下所有!目录名(列表形式)
    for img_name in img_dirs:# cat.0.jpg
        name = img_name.split(".")# ['cat', '0', 'jpg']
        if  name[0] == "cat":
            cats.append(file_dir + img_name)
            cats_label.append(0)
        else:
            if name[0] == "dog":
                dogs.append(file_dir + img_name)
                dogs_label.append(1)

    img_list = np.hstack((cats, dogs))
    label_list = np.hstack((cats_label, dogs_label))

    temp = np.array([img_list, label_list])  # 列表转化为矩阵
    temp = temp.transpose()  # transpose的操作对象是矩阵,转置一下
    np.random.shuffle(temp)  # 打乱顺序

    image_list = list(temp[:, 0])
    label_list = list(temp[:, 1])
    label_list = [int(i) for i in label_list]

    train_image_list = list(image_list[0:int(len(image_list) * 0.7)])
    train_label_list = list(label_list[0:int(len(image_list) * 0.7)])
    valid_image_list = list(image_list[int(len(image_list) * 0.7):len(image_list)])
    valid_label_list = list(label_list[int(len(image_list) * 0.7):len(image_list)])

    return train_image_list,train_label_list,valid_image_list,valid_label_list

#############################################

def get_batch(image, label, image_w, image_h, batch_size, capacity):#capacity: 队列中 最多容纳图片的个数

    input_queue = tf.train.slice_input_producer([image, label])#tf.train.slice_input_producer是一个tensor生成器,作用是
    # 按照设定,每次从一个tensor列表中按顺序或者随机抽取出一个tensor放入文件名队列。
    label = input_queue[1]
    img_contents = tf.read_file(input_queue[0])#一维
    image = tf.image.decode_jpeg(img_contents, channels=3)#解码成三维矩阵
    image = tf.image.resize_image_with_crop_or_pad(image, image_w, image_h)
    image = tf.cast(image, tf.float32)
    image = tf.image.per_image_standardization(image)


    # 生成批次  num_threads 有多少个线程根据电脑配置设置
    image_batch, label_batch = tf.train.batch([image, label], batch_size=batch_size, num_threads=64, capacity=capacity)

    return image_batch, label_batch  #shape = (32, 208, 208, 3)     (32,)


# img_list, label_list = get_files("F:/mytest/2.cat_dog/train/train/")
# image_batch, label_batch = get_batch(img_list, label_list,208,208,32,256)
# print(image_batch.shape)
# print(label_batch.shape)

2._model.py

import tensorflow as tf

def inference(image, batch_size, n_classes):
    with tf.variable_scope("conv1") as scope:#课本108,variable_scope控制get_variable是获取(reuse=True)还是创建变量
        weights = tf.get_variable("weights", shape=[3,3,3,16], dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
        biases = tf.get_variable("biases", shape=[16], dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        conv = tf.nn.conv2d(image, weights, strides=[1,1,1,1], padding="SAME")
        pre_activation = tf.nn.bias_add(conv, biases)
        conv1 = tf.nn.relu(pre_activation, name=scope.name)

    with tf.variable_scope("pooling1_lrn") as scope:
        pool1 = tf.nn.max_pool(conv1, ksize=[1,3,3,1], strides=[1,2,2,1], padding="SAME", name="pooling1")
        norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0,beta=0.75, name="norm1")#局部响应归一化??????
    with tf.variable_scope("conv2") as scope:
        weights = tf.get_variable("weights", shape=[3,3,16,16], dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
        biases = tf.get_variable("biases", shape=[16], dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        conv = tf.nn.conv2d(norm1, weights, strides=[1,1,1,1], padding="SAME")
        pre_activation = tf.nn.bias_add(conv, biases)
        conv2 = tf.nn.relu(pre_activation, name=scope.name)

    with tf.variable_scope("pooling2_lrn") as scope:
        norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0,beta=0.75, name="norm2")
        pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,2,2,1], padding="SAME", name="pooling2")

    pool2_shape = pool2.get_shape().as_list()
    nodes = pool2_shape[1] * pool2_shape[2] * pool2_shape[3]
    dense = tf.reshape(pool2, [batch_size, nodes])

    with tf.variable_scope("local3") as scope:
        weights = tf.get_variable("weights", shape=[nodes, 128], dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
    local3 = tf.nn.relu(tf.matmul(dense, weights) + biases, name=scope.name)

    with tf.variable_scope("local4") as scope:
        weights = tf.get_variable("weights", shape=[128, 128], dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
    local4 = tf.nn.relu(tf.matmul(local3, weights) + biases,name="local4")

    with tf.variable_scope("softmax_linear") as scope:
        weights = tf.get_variable("weights", shape=[128, n_classes], dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases", shape=[n_classes], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
    softmax_linear = tf.matmul(local4, weights) + biases

    return softmax_linear

"""
top_1_op取样本的最大预测概率的索引与实际标签对比,top_2_op取样本的最大和仅次最大的两个预测概率与实际标签对比,
如果实际标签在其中则为True,否则为False。

3.train.py

import tensorflow as tf
import numpy as np
import os
import _input_data
import _model

N_CLASSES = 2
IMG_W = 208
IMG_H = 208
BATCH_SIZE = 32
CAPACITY = 256
STEP = 150   #训练步数应当大于10000
LEARNING_RATE = 0.0001

train_dir = "E:/mytest/2.cat_dog/train/train/"
log_train_dir = "E:/mytest/2.cat_dog/train_savenet/"

train_image_list, train_label_list,valid_image_list, valid_label_list = _input_data.get_files(train_dir)

X_train,Y_train = _input_data.get_batch(train_image_list, train_label_list, IMG_W,IMG_H,BATCH_SIZE,CAPACITY)
X_valid,Y_valid = _input_data.get_batch(valid_image_list, valid_label_list, IMG_W,IMG_H,BATCH_SIZE,CAPACITY)

x = tf.placeholder(tf.float32, shape=[None, 208,208,3])
y_ = tf.placeholder(tf.int64, shape=[None, ])


out = _model.inference(x,BATCH_SIZE, N_CLASSES )
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits= out, labels= y_, name="entropy_per_example")
loss = tf.reduce_mean(cross_entropy)
global_step = tf.Variable(0, name="global_step", trainable=False)  # 定义训练的轮数,为不可训练的参数
train_op =  tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(loss, global_step=global_step)
correct = tf.equal(tf.argmax(out, 1), y_)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float16))

out1 = tf.nn.softmax(out)
correct1 = tf.equal(tf.argmax(out1, 1), y_)
accuracy1 = tf.reduce_mean(tf.cast(correct1, tf.float16))

saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    #  Coordinator  和 start_queue_runners 监控 queue 的状态,不停的入队出队
    coord = tf.train.Coordinator()#https://blog.csdn.net/weixin_42052460/article/details/80714539
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(STEP):
            if coord.should_stop():
                break

            img_train,label_train = sess.run([X_train,Y_train])#注意:1)feed喂的不可以是张量。2)接收的参数名和run()里面的参数名不要一样
            _, tra_loss, tra_acc = sess.run([train_op, loss, accuracy],
                                            feed_dict={x: img_train, y_: label_train})

            if step % 50 == 0:#%.2f表示输出浮点数并保留两位小数。%%表示直接输出一个%
                print("step %d, train loss = %.2f, train accuracy  = %.2f%%" %(step, tra_loss, tra_acc*100.0))

            if step % 2000 == 0 or (step+1) ==STEP:
                # 每隔2000步保存一下模型,模型保存在 checkpoint_path 中
                checkpoint_path = os.path.join(log_train_dir, "model.ckpt")
                saver.save(sess, checkpoint_path, global_step=step)

        img_valid, label_valid = sess.run([X_valid, Y_valid])
        valid_accuracy = sess.run(accuracy1, feed_dict={x: img_valid, y_: label_valid})
        # out2 = sess.run(out1, feed_dict = {x: img_valid})
        # out3  = sess.run(tf.argmax(out2, 1))
        # print(out2)
        # print(out3)
        # correct2 = sess.run(correct1, feed_dict={x: img_valid, y_: label_valid})
        # print(correct2)
        print(" valid accuracy %g" % valid_accuracy)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')

    finally:
        coord.request_stop()
    coord.join(threads)



4.test.py

猜你喜欢

转载自blog.csdn.net/qq_42219077/article/details/82388960