计算速度优化,摄像头读取逻辑改变,分类器更新。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/python3
|
||||
print("Preparing...")
|
||||
import tensorflow as tf
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
import generate
|
||||
import forward
|
||||
@@ -35,28 +36,22 @@ def save_bias(fp, val):
|
||||
print(val[i], file=fp)
|
||||
|
||||
|
||||
def save_para(folder, paras):
|
||||
with open(folder + "/conv1_w", "w") as fp:
|
||||
save_kernal(fp, paras[0])
|
||||
with open(folder + "/conv1_b", "w") as fp:
|
||||
save_bias(fp, paras[1])
|
||||
with open(folder + "/conv2_w", "w") as fp:
|
||||
save_kernal(fp, paras[2])
|
||||
with open(folder + "/conv2_b", "w") as fp:
|
||||
save_bias(fp, paras[3])
|
||||
with open(folder + "/fc1_w", "w") as fp:
|
||||
save_weight_mat(fp, paras[4])
|
||||
with open(folder + "/fc1_b", "w") as fp:
|
||||
save_bias(fp, paras[5])
|
||||
with open(folder + "/fc2_w", "w") as fp:
|
||||
save_weight_mat(fp, paras[6])
|
||||
with open(folder + "/fc2_b", "w") as fp:
|
||||
save_bias(fp, paras[7])
|
||||
def save_para(folder, paras, names, info):
|
||||
os.system("mkdir %s/%s" % (folder, info))
|
||||
for para, name in zip(paras, names):
|
||||
fp = open("%s/%s/%s" % (folder, info, name), "w")
|
||||
if name[-1:] == "b":
|
||||
save_bias(fp, para)
|
||||
elif name[:2] == "fc":
|
||||
save_weight_mat(fp, para)
|
||||
elif name[:4] == "conv":
|
||||
save_kernal(fp, para)
|
||||
fp.close()
|
||||
|
||||
|
||||
STEPS = 60000
|
||||
BATCH = 50
|
||||
LEARNING_RATE_BASE = 0.001
|
||||
STEPS = 50000
|
||||
BATCH = 30
|
||||
LEARNING_RATE_BASE = 0.0005
|
||||
LEARNING_RATE_DECAY = 0.99
|
||||
MOVING_AVERAGE_DECAY = 0.99
|
||||
|
||||
@@ -65,7 +60,7 @@ def train(dataset, show_bar=False):
|
||||
x = tf.placeholder(tf.float32, [None, generate.SRC_ROWS, generate.SRC_COLS, generate.SRC_CHANNELS])
|
||||
y_= tf.placeholder(tf.float32, [None, forward.OUTPUT_NODES])
|
||||
keep_rate = tf.placeholder(tf.float32)
|
||||
nodes, vars = forward.forward(x, 0.01)
|
||||
nodes, vars, vars_name = forward.forward(x, 0.01)
|
||||
y = nodes[-1]
|
||||
|
||||
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
|
||||
@@ -101,35 +96,38 @@ def train(dataset, show_bar=False):
|
||||
|
||||
_, loss_value, step = sess.run(
|
||||
[train_op, loss, global_step],
|
||||
feed_dict={x: images_samples, y_: labels_samples, keep_rate:0.2}
|
||||
feed_dict={x: images_samples, y_: labels_samples, keep_rate:0.4}
|
||||
)
|
||||
|
||||
if (i-1) % 100 == 0:
|
||||
if (i-1) % 500 == 0:
|
||||
test_images, test_labels = dataset.sample_test_sets(5000)
|
||||
test_acc, output = sess.run([accuracy, y], feed_dict={x: test_images, y_: test_labels, keep_rate:1.0})
|
||||
output = np.argmax(output, axis=1)
|
||||
real = np.argmax(test_labels, axis=1)
|
||||
print("=============test-set===============")
|
||||
for n in range(forward.OUTPUT_NODES):
|
||||
print("label: %d, precise: %f, recall: %f" %
|
||||
(n, np.mean(real[output==n]==n), np.mean(output[real==n]==n)))
|
||||
|
||||
train_images, train_labels = dataset.sample_train_sets(5000)
|
||||
train_acc, output = sess.run([accuracy, y], feed_dict={x: train_images, y_: train_labels, keep_rate:1.0})
|
||||
output = np.argmax(output, axis=1)
|
||||
real = np.argmax(train_labels, axis=1)
|
||||
print("=============train-set===============")
|
||||
for n in range(forward.OUTPUT_NODES):
|
||||
print("label: %d, precise: %f, recall: %f" %
|
||||
(n, np.mean(real[output==n]==n), np.mean(output[real==n]==n)))
|
||||
print("\n")
|
||||
if step % 500 == 0:
|
||||
test_images, test_labels = dataset.sample_test_sets(6000)
|
||||
test_acc, output = sess.run([accuracy, y], feed_dict={x: test_images, y_: test_labels, keep_rate:1.0})
|
||||
output = np.argmax(output, axis=1)
|
||||
real = np.argmax(test_labels, axis=1)
|
||||
print("=============test-set===============")
|
||||
for n in range(forward.OUTPUT_NODES):
|
||||
print("label: %d, precise: %f, recall: %f" %
|
||||
(n, np.mean(real[output==n]==n), np.mean(output[real==n]==n)))
|
||||
|
||||
train_images, train_labels = dataset.sample_train_sets(6000)
|
||||
train_acc, output = sess.run([accuracy, y], feed_dict={x: train_images, y_: train_labels, keep_rate:1.0})
|
||||
output = np.argmax(output, axis=1)
|
||||
real = np.argmax(train_labels, axis=1)
|
||||
print("=============train-set===============")
|
||||
for n in range(forward.OUTPUT_NODES):
|
||||
print("label: %d, precise: %f, recall: %f" %
|
||||
(n, np.mean(real[output==n]==n), np.mean(output[real==n]==n)))
|
||||
print("\n")
|
||||
if train_acc >= 0.99 and test_acc >= 0.99:
|
||||
vars_val = sess.run(vars)
|
||||
save_para(
|
||||
"model",
|
||||
vars_val,
|
||||
vars_name,
|
||||
"steps:%d-train_acc:%f-test_acc:%f" % (step, train_acc, test_acc)
|
||||
)
|
||||
bar.set_postfix({"loss": loss_value, "train_acc": train_acc, "test_acc": test_acc})
|
||||
|
||||
vars_val = sess.run(vars)
|
||||
save_para("/home/xinyang/Workspace/RM_auto-aim/tools/para", vars_val)
|
||||
print("save done!")
|
||||
# print("save done!")
|
||||
|
||||
# pred = sess.run(y, feed_dict={x: test_images, keep_rate:1.0})
|
||||
|
||||
|
||||
@@ -29,16 +29,22 @@ def max_pool_2x2(x):
|
||||
CONV1_KERNAL_SIZE = 5
|
||||
|
||||
# 第一层卷积输出通道数
|
||||
CONV1_OUTPUT_CHANNELS = 8
|
||||
CONV1_OUTPUT_CHANNELS = 4
|
||||
|
||||
# 第二层卷积核大小
|
||||
CONV2_KERNAL_SIZE = 3
|
||||
|
||||
# 第二层卷积输出通道数
|
||||
CONV2_OUTPUT_CHANNELS = 16
|
||||
CONV2_OUTPUT_CHANNELS = 6
|
||||
|
||||
# 第三层卷积核大小
|
||||
CONV3_KERNAL_SIZE = 3
|
||||
|
||||
# 第三层卷积输出通道数
|
||||
CONV3_OUTPUT_CHANNELS = 8
|
||||
|
||||
# 第一层全连接宽度
|
||||
FC1_OUTPUT_NODES = 100
|
||||
FC1_OUTPUT_NODES = 50
|
||||
|
||||
# 第二层全连接宽度(输出标签类型数)
|
||||
FC2_OUTPUT_NODES = 15
|
||||
@@ -49,6 +55,7 @@ OUTPUT_NODES = FC2_OUTPUT_NODES
|
||||
|
||||
def forward(x, regularizer=None, keep_rate=tf.constant(1.0)):
|
||||
vars = []
|
||||
vars_name = []
|
||||
nodes = []
|
||||
|
||||
conv1_w = get_weight(
|
||||
@@ -57,7 +64,10 @@ def forward(x, regularizer=None, keep_rate=tf.constant(1.0)):
|
||||
conv1_b = get_bias([CONV1_OUTPUT_CHANNELS])
|
||||
conv1 = tf.nn.relu(tf.nn.bias_add(conv2d(x, conv1_w), conv1_b))
|
||||
pool1 = avg_pool_2x2(conv1)
|
||||
print("conv1: ", conv1.shape)
|
||||
print("pool1: ", pool1.shape)
|
||||
vars.extend([conv1_w, conv1_b])
|
||||
vars_name.extend(["conv1_w", "conv1_b"])
|
||||
nodes.extend([conv1, pool1])
|
||||
|
||||
conv2_w = get_weight(
|
||||
@@ -66,27 +76,41 @@ def forward(x, regularizer=None, keep_rate=tf.constant(1.0)):
|
||||
conv2_b = get_bias([CONV2_OUTPUT_CHANNELS])
|
||||
conv2 = tf.nn.relu(tf.nn.bias_add(conv2d(pool1, conv2_w), conv2_b))
|
||||
pool2 = avg_pool_2x2(conv2)
|
||||
print("conv2: ", conv2.shape)
|
||||
print("pool2: ", pool2.shape)
|
||||
vars.extend([conv2_w, conv2_b])
|
||||
vars_name.extend(["conv2_w", "conv2_b"])
|
||||
nodes.extend([conv2, pool2])
|
||||
|
||||
pool_shape = pool2.get_shape().as_list()
|
||||
node = pool_shape[1] * pool_shape[2] * pool_shape[3]
|
||||
reshaped = tf.reshape(pool2, [-1, node])
|
||||
conv3_w = get_weight(
|
||||
[CONV3_KERNAL_SIZE, CONV3_KERNAL_SIZE, CONV2_OUTPUT_CHANNELS, CONV3_OUTPUT_CHANNELS]
|
||||
)
|
||||
conv3_b = get_bias([CONV3_OUTPUT_CHANNELS])
|
||||
conv3 = tf.nn.relu(tf.nn.bias_add(conv2d(pool2, conv3_w), conv3_b))
|
||||
print("conv3: ", conv3.shape)
|
||||
vars.extend([conv3_w, conv3_b])
|
||||
vars_name.extend(["conv3_w", "conv3_b"])
|
||||
nodes.extend([conv3])
|
||||
|
||||
conv_shape = conv3.get_shape().as_list()
|
||||
node = conv_shape[1] * conv_shape[2] * conv_shape[3]
|
||||
reshaped = tf.reshape(conv3, [-1, node])
|
||||
reshaped = tf.nn.dropout(reshaped, keep_rate)
|
||||
print("reshaped: ", reshaped.shape)
|
||||
|
||||
fc1_w = get_weight([node, FC1_OUTPUT_NODES], regularizer)
|
||||
fc1_b = get_bias([FC1_OUTPUT_NODES])
|
||||
fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b)
|
||||
fc1 = tf.nn.dropout(fc1, keep_rate)
|
||||
vars.extend([fc1_w, fc1_b])
|
||||
vars_name.extend(["fc1_w", "fc1_b"])
|
||||
nodes.extend([fc1])
|
||||
|
||||
fc2_w = get_weight([FC1_OUTPUT_NODES, FC2_OUTPUT_NODES], regularizer)
|
||||
fc2_b = get_bias([FC2_OUTPUT_NODES])
|
||||
# fc2 = tf.nn.softmax(tf.matmul(fc1, fc2_w) + fc2_b)
|
||||
fc2 = tf.matmul(fc1, fc2_w) + fc2_b
|
||||
fc2 = tf.matmul(fc1, fc2_w) + fc2_b
|
||||
vars.extend([fc2_w, fc2_b])
|
||||
vars_name.extend(["fc2_w", "fc2_b"])
|
||||
nodes.extend([fc2])
|
||||
|
||||
return nodes, vars
|
||||
return nodes, vars, vars_name
|
||||
|
||||
|
||||
Reference in New Issue
Block a user