import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt
from scipy import misc
import random
from sklearn.model_selection import train_test_split
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
#设置随机种子
tf.set_random_seed(777)
path = r"D:\catdog\train" #图片存储位置
#定义归一化读取图片函数
def readimg(file_path):
img = plt.imread(file_path)
#将图片修改为100*100尺寸
img = misc.imresize(img,(100,100))
img = img/255
return img
#获取数据
x_path = []
y_data = []
#读取图片路径以及生成标签数据
for item in os.listdir(path):
file_path = path+"\\"+item
x_path.append(file_path)
if "cat" in item: #猫标记为0
y_data.append([1,0])
else:
y_data.append([0,1])
#将列表转为矩阵
x_path = np.array(x_path)
y_data = np.array(y_data)
#乱序原数据
np.random.seed(100)
order = np.random.permutation(len(y_data))
x_path = x_path[order]
y_data = y_data[order]
#读取前1000个样本作为数据集
x_path = x_path[:1000]
y_data = y_data[:1000]
#加载图片数据
x_data = []
for path in x_path:
img = readimg(path)
x_data.append(img)
#切分测试集和训练集
train_x,test_x,train_y,test_y = train_test_split(x_data,y_data,test_size=0.1,random_state=100)
#实现next_bacth 函数
g_b = 0
def next_batch(size):
global g_b
x = train_x[g_b:g_b+size]
y = train_y[g_b:g_b+size]
g_b += size
return x,y
#定义占位符
x = tf.placeholder(tf.float32,shape=[None,100,100,3])
y = tf.placeholder(tf.float32,shape=[None,2])
#第一层卷积
w1 = tf.Variable(tf.random_normal([3,3,3,16]))
L1 = tf.nn.conv2d(x,w1,strides=[1,1,1,1],padding="SAME")
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
#第二层卷积
w2 = tf.Variable(tf.random_normal([3,3,16,16]))
L2 = tf.nn.conv2d(L1,w2,strides=[1,1,1,1],padding="SAME")
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
#全连接层
dim = L2.get_shape()[1].value*L2.get_shape()[2].value*L2.get_shape()[3].value
L2_flat = tf.reshape(L2,shape=[-1,dim])
w3 = tf.Variable(tf.random_normal([dim,2]))
b = tf.Variable(tf.random_normal([2]))
logits = tf.matmul(L2_flat,w3)+b
#定义损失
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)
#精度
y_pred = tf.argmax(logits,1)
corr = tf.equal(y_pred,tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(corr,tf.float32))
#会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())
#开始迭代
training_epoch = 2
batch_size = 100
for epoch in range(training_epoch):
avg_cost = 0
total_batch = int(len(train_x)/batch_size)
g_b = 0
for i in range(total_batch):
batch_x,batch_y = next_batch(batch_size)
c,_,acc = sess.run([loss,optimizer,accuracy],feed_dict={x:batch_x,y:batch_y})
avg_cost += c/total_batch
print(c,acc)
#测试集精度
print("测试集精度是:",sess.run(accuracy,feed_dict={x:test_x,y:test_y}))
#随机抽取一个样本测试
r = np.random.randint(0,100)
print("实际值是:",np.argmax(test_y[r]))
print("预测值是:",sess.run(y_pred,feed_dict={x:test_x[r:r+1]}))