|
|
|
|
# -*- coding=UTF-8 -*-
|
|
|
|
|
import os
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
import numpy as np
|
|
|
|
|
import time
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
w = 64
|
|
|
|
|
h = 64
|
|
|
|
|
c = 3
|
|
|
|
|
TOTAL_TYPE = 5
|
|
|
|
|
|
|
|
|
|
path = 'D:/tensorflow/imgaes/'
|
|
|
|
|
model_path='D:/tensorflow/saver/model.ckpt'
|
|
|
|
|
|
|
|
|
|
def read_source():
|
|
|
|
|
imgs,labels = [],[]
|
|
|
|
|
for dir in os.listdir(path):
|
|
|
|
|
idx = int(dir)
|
|
|
|
|
folder = os.path.join(path,dir)
|
|
|
|
|
zeros = np.zeros(TOTAL_TYPE)
|
|
|
|
|
zeros[idx]=1
|
|
|
|
|
print("folder :%s"%(folder))
|
|
|
|
|
total = 0
|
|
|
|
|
for f in os.listdir(folder):
|
|
|
|
|
file = os.path.join(folder,f)
|
|
|
|
|
image = Image.open(file).convert('RGB').resize((w, h), Image.ANTIALIAS)
|
|
|
|
|
arr = np.asarray(image)
|
|
|
|
|
imgs.append(arr)
|
|
|
|
|
labels.append(zeros)
|
|
|
|
|
total += 1
|
|
|
|
|
print(total)
|
|
|
|
|
return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
|
|
|
|
|
|
|
|
|
|
data,label=read_source()
|
|
|
|
|
num_example = data.shape[0]
|
|
|
|
|
arr = np.arange(num_example)
|
|
|
|
|
np.random.shuffle(arr)
|
|
|
|
|
data = data[arr]
|
|
|
|
|
label = label[arr]
|
|
|
|
|
|
|
|
|
|
# 将所有数据分为训练集和验证集
|
|
|
|
|
ratio = 0.8
|
|
|
|
|
s = np.int(num_example * ratio)
|
|
|
|
|
x_train = data[:s]
|
|
|
|
|
y_train = label[:s]
|
|
|
|
|
x_val = data[s:]
|
|
|
|
|
y_val = label[s:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------构建网络----------------------
|
|
|
|
|
x = tf.placeholder(tf.float32, shape=[None, w, h, 3], name='x')
|
|
|
|
|
y_ = tf.placeholder(tf.float32, shape=[None,5 ], name='y_')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def inference(input_tensor, train, regularizer):
|
|
|
|
|
with tf.variable_scope('layer1-conv1'):
|
|
|
|
|
conv1_weights = tf.get_variable("weight", [5, 5, 3, 64],
|
|
|
|
|
initializer=tf.truncated_normal_initializer(stddev=0.1))
|
|
|
|
|
conv1_biases = tf.get_variable("bias", [64], initializer=tf.constant_initializer(0.0))
|
|
|
|
|
conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
|
|
|
|
|
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
|
|
|
|
|
relu1 = tf.layers.batch_normalization(relu1,training=train)
|
|
|
|
|
|
|
|
|
|
with tf.name_scope("layer2-pool1"):
|
|
|
|
|
pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
|
|
|
|
|
|
|
|
|
|
with tf.variable_scope("layer3-conv2"):
|
|
|
|
|
conv2_weights = tf.get_variable("weight", [3, 3, 64, 128],
|
|
|
|
|
initializer=tf.truncated_normal_initializer(stddev=0.1))
|
|
|
|
|
conv2_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0))
|
|
|
|
|
conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
|
|
|
|
|
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
|
|
|
|
|
relu2 = tf.layers.batch_normalization(relu2, training=train)
|
|
|
|
|
|
|
|
|
|
with tf.name_scope("layer4-pool2"):
|
|
|
|
|
pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
|
|
|
|
|
|
|
|
|
with tf.variable_scope("layer5-conv3"):
|
|
|
|
|
conv3_weights = tf.get_variable("weight", [3, 3, 128, 256],
|
|
|
|
|
initializer=tf.truncated_normal_initializer(stddev=0.1))
|
|
|
|
|
conv3_biases = tf.get_variable("bias", [256], initializer=tf.constant_initializer(0.0))
|
|
|
|
|
conv3 = tf.nn.conv2d(pool2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME')
|
|
|
|
|
relu3 = tf.nn.relu(tf.nn.bias_add(conv3, conv3_biases))
|
|
|
|
|
relu3 = tf.layers.batch_normalization(relu3, training=train)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with tf.name_scope("layer8-pool4"):
|
|
|
|
|
pool4 = tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
|
|
|
|
norm4 = tf.nn.lrn(pool4, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
|
|
|
|
|
nodes = 8 * 8* 256
|
|
|
|
|
reshaped = tf.reshape(norm4, [-1, nodes])
|
|
|
|
|
|
|
|
|
|
with tf.variable_scope('layer9-fc1'):
|
|
|
|
|
fc1_weights = tf.get_variable("weight", [nodes, 1024],
|
|
|
|
|
initializer=tf.truncated_normal_initializer(stddev=0.1))
|
|
|
|
|
if regularizer != None: tf.add_to_collection('losses', regularizer(fc1_weights))
|
|
|
|
|
fc1_biases = tf.get_variable("bias", [1024], initializer=tf.constant_initializer(0.1))
|
|
|
|
|
|
|
|
|
|
fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)
|
|
|
|
|
if train: fc1 = tf.nn.dropout(fc1, 0.8)
|
|
|
|
|
|
|
|
|
|
with tf.variable_scope('layer10-fc2'):
|
|
|
|
|
fc2_weights = tf.get_variable("weight", [1024, 512],
|
|
|
|
|
initializer=tf.truncated_normal_initializer(stddev=0.1))
|
|
|
|
|
if regularizer != None: tf.add_to_collection('losses', regularizer(fc2_weights))
|
|
|
|
|
fc2_biases = tf.get_variable("bias", [512], initializer=tf.constant_initializer(0.1))
|
|
|
|
|
|
|
|
|
|
fc2 = tf.nn.relu(tf.matmul(fc1, fc2_weights) + fc2_biases)
|
|
|
|
|
if train: fc2 = tf.nn.dropout(fc2, 0.8)
|
|
|
|
|
|
|
|
|
|
with tf.variable_scope('layer11-fc3'):
|
|
|
|
|
fc3_weights = tf.get_variable("weight", [512, 5],
|
|
|
|
|
initializer=tf.truncated_normal_initializer(stddev=0.1))
|
|
|
|
|
if regularizer != None: tf.add_to_collection('losses', regularizer(fc3_weights))
|
|
|
|
|
fc3_biases = tf.get_variable("bias", [5], initializer=tf.constant_initializer(0.1))
|
|
|
|
|
logit = tf.matmul(fc2, fc3_weights) + fc3_biases
|
|
|
|
|
|
|
|
|
|
return logit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------网络结束---------------------------
|
|
|
|
|
regularizer = tf.contrib.layers.l2_regularizer(0.0001)
|
|
|
|
|
logits = inference(x, False, regularizer)
|
|
|
|
|
|
|
|
|
|
# (小处理)将logits乘以1赋值给logits_eval,定义name,方便在后续调用模型时通过tensor名字调用输出tensor
|
|
|
|
|
b = tf.constant(value=1, dtype=tf.float32)
|
|
|
|
|
logits_eval = tf.multiply(logits, b, name='logits_eval')
|
|
|
|
|
|
|
|
|
|
# loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y_)
|
|
|
|
|
# loss = -tf.reduce_sum(y_*tf.log(logits))
|
|
|
|
|
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,labels=y_))
|
|
|
|
|
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
|
|
|
|
|
with tf.control_dependencies(update_ops):
|
|
|
|
|
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
|
|
|
|
|
correct_prediction = tf.equal(tf.argmax(logits, 1),tf.argmax(y_,1))
|
|
|
|
|
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 定义一个函数,按批次取数据
|
|
|
|
|
def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False):
|
|
|
|
|
assert len(inputs) == len(targets)
|
|
|
|
|
if shuffle:
|
|
|
|
|
indices = np.arange(len(inputs))
|
|
|
|
|
np.random.shuffle(indices)
|
|
|
|
|
for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
|
|
|
|
|
if shuffle:
|
|
|
|
|
excerpt = indices[start_idx:start_idx + batch_size]
|
|
|
|
|
else:
|
|
|
|
|
excerpt = slice(start_idx, start_idx + batch_size)
|
|
|
|
|
yield inputs[excerpt], targets[excerpt]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 训练和测试数据,可将n_epoch设置更大一些
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n_epoch = 200
|
|
|
|
|
batch_size = 64
|
|
|
|
|
saver = tf.train.Saver()
|
|
|
|
|
sess = tf.Session()
|
|
|
|
|
sess.run(tf.global_variables_initializer())
|
|
|
|
|
for epoch in range(n_epoch):
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
# training
|
|
|
|
|
train_loss, train_acc, n_batch = 0, 0, 0
|
|
|
|
|
for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size, shuffle=True):
|
|
|
|
|
_, err, ac = sess.run([train_op, loss, acc], feed_dict={x: x_train_a, y_: y_train_a})
|
|
|
|
|
train_loss += err;
|
|
|
|
|
train_acc += ac;
|
|
|
|
|
n_batch += 1
|
|
|
|
|
print(" train loss: %f" % (np.sum(train_loss) / n_batch))
|
|
|
|
|
print(" train acc: %f" % (np.sum(train_acc) / n_batch))
|
|
|
|
|
|
|
|
|
|
# validation
|
|
|
|
|
val_loss, val_acc, n_batch = 0, 0, 0
|
|
|
|
|
for x_val_a, y_val_a in minibatches(x_val, y_val, batch_size, shuffle=False):
|
|
|
|
|
err, ac = sess.run([loss, acc], feed_dict={x: x_val_a, y_: y_val_a})
|
|
|
|
|
val_loss += err
|
|
|
|
|
val_acc += ac
|
|
|
|
|
n_batch += 1
|
|
|
|
|
print(" validation loss: %f" % (np.sum(val_loss) / n_batch))
|
|
|
|
|
print(" validation acc: %f" % (np.sum(val_acc) / n_batch))
|
|
|
|
|
saver.save(sess,model_path)
|
|
|
|
|
sess.close()
|
|
|
|
|
|