分离曝光,参数更新,取消反陀螺,数据增强。
This commit is contained in:
@@ -8,8 +8,10 @@ import forward
|
||||
import cv2
|
||||
import numpy as np
|
||||
import mvsdk
|
||||
|
||||
print("Finish!")
|
||||
|
||||
|
||||
def save_kernal(fp, val):
|
||||
print(val.shape[2], file=fp)
|
||||
print(val.shape[3], file=fp)
|
||||
@@ -49,24 +51,24 @@ def save_para(folder, paras, names, info):
|
||||
fp.close()
|
||||
|
||||
|
||||
STEPS = 50000
|
||||
BATCH = 30
|
||||
LEARNING_RATE_BASE = 0.0005
|
||||
STEPS = 100000
|
||||
BATCH = 40
|
||||
LEARNING_RATE_BASE = 0.0002
|
||||
LEARNING_RATE_DECAY = 0.99
|
||||
MOVING_AVERAGE_DECAY = 0.99
|
||||
|
||||
|
||||
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])
|
||||
y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODES])
|
||||
keep_rate = tf.placeholder(tf.float32)
|
||||
nodes, vars, vars_name = forward.forward(x, 0.01)
|
||||
nodes, vars, vars_name = forward.forward(x, 0.01, keep_rate)
|
||||
y = nodes[-1]
|
||||
|
||||
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
|
||||
# ce = tf.nn.weighted_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1), pos_weight=1)
|
||||
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
|
||||
# ce = tf.nn.weighted_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1), pos_weight=1)
|
||||
cem = tf.reduce_mean(ce)
|
||||
loss= cem + tf.add_n(tf.get_collection("losses"))
|
||||
loss = cem + tf.add_n(tf.get_collection("losses"))
|
||||
|
||||
global_step = tf.Variable(0, trainable=False)
|
||||
learning_rate = tf.train.exponential_decay(
|
||||
@@ -92,31 +94,33 @@ def train(dataset, show_bar=False):
|
||||
|
||||
bar = tqdm(range(STEPS), ascii=True, dynamic_ncols=True)
|
||||
for i in bar:
|
||||
images_samples, labels_samples = dataset.sample_train_sets(BATCH)
|
||||
images_samples, labels_samples = dataset.sample_train_sets(BATCH, 0.03)
|
||||
|
||||
_, loss_value, step = sess.run(
|
||||
[train_op, loss, global_step],
|
||||
feed_dict={x: images_samples, y_: labels_samples, keep_rate:0.3}
|
||||
feed_dict={x: images_samples, y_: labels_samples, keep_rate: 0.3}
|
||||
)
|
||||
|
||||
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})
|
||||
test_images, test_labels = dataset.sample_test_sets(10000)
|
||||
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)))
|
||||
(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})
|
||||
train_images, train_labels = dataset.sample_train_sets(10000)
|
||||
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)))
|
||||
(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)
|
||||
@@ -131,8 +135,8 @@ def train(dataset, show_bar=False):
|
||||
|
||||
# pred = sess.run(y, feed_dict={x: test_images, keep_rate:1.0})
|
||||
|
||||
# nodes_val = sess.run(nodes, feed_dict={x:test_images})
|
||||
# return vars_val, nodes_val
|
||||
# nodes_val = sess.run(nodes, feed_dict={x:test_images})
|
||||
# return vars_val, nodes_val
|
||||
DevList = mvsdk.CameraEnumerateDevice()
|
||||
nDev = len(DevList)
|
||||
if nDev < 1:
|
||||
@@ -150,7 +154,7 @@ def train(dataset, show_bar=False):
|
||||
try:
|
||||
hCamera = mvsdk.CameraInit(DevInfo, -1, -1)
|
||||
except mvsdk.CameraException as e:
|
||||
print("CameraInit Failed({}): {}".format(e.error_code, e.message) )
|
||||
print("CameraInit Failed({}): {}".format(e.error_code, e.message))
|
||||
return
|
||||
|
||||
# 获取相机特性描述
|
||||
@@ -192,24 +196,25 @@ def train(dataset, show_bar=False):
|
||||
# 把pFrameBuffer转换成opencv的图像格式以进行后续算法处理
|
||||
frame_data = (mvsdk.c_ubyte * FrameHead.uBytes).from_address(pFrameBuffer)
|
||||
frame = np.frombuffer(frame_data, dtype=np.uint8)
|
||||
frame = frame.reshape((FrameHead.iHeight, FrameHead.iWidth, 1 if FrameHead.uiMediaType == mvsdk.CAMERA_MEDIA_TYPE_MONO8 else 3) )
|
||||
frame = frame.reshape((FrameHead.iHeight, FrameHead.iWidth,
|
||||
1 if FrameHead.uiMediaType == mvsdk.CAMERA_MEDIA_TYPE_MONO8 else 3))
|
||||
|
||||
frame = cv2.resize(frame, (640,480), interpolation = cv2.INTER_LINEAR)
|
||||
frame = cv2.resize(frame, (640, 480), interpolation=cv2.INTER_LINEAR)
|
||||
cv2.imshow("Press q to end", frame)
|
||||
if (cv2.waitKey(1)&0xFF) == ord(' '):
|
||||
if (cv2.waitKey(1) & 0xFF) == ord(' '):
|
||||
roi = cv2.selectROI("roi", frame)
|
||||
roi = frame[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2]]
|
||||
roi = frame[roi[1]:roi[1] + roi[3], roi[0]:roi[0] + roi[2]]
|
||||
print(roi)
|
||||
cv2.imshow("box", roi)
|
||||
image = cv2.resize(roi, (48, 36))
|
||||
image = image.astype(np.float32) / 255.0
|
||||
out = sess.run(y, feed_dict={x:[image]})
|
||||
out = sess.run(y, feed_dict={x: [image]})
|
||||
print(out)
|
||||
print(np.argmax(out))
|
||||
|
||||
|
||||
except mvsdk.CameraException as e:
|
||||
if e.error_code != mvsdk.CAMERA_STATUS_TIME_OUT:
|
||||
print("CameraGetImageBuffer failed({}): {}".format(e.error_code, e.message) )
|
||||
print("CameraGetImageBuffer failed({}): {}".format(e.error_code, e.message))
|
||||
|
||||
# 关闭相机
|
||||
mvsdk.CameraUnInit(hCamera)
|
||||
@@ -219,9 +224,9 @@ def train(dataset, show_bar=False):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# import os
|
||||
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
||||
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
||||
# import os
|
||||
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
||||
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
||||
dataset = generate.DataSet("/home/xinyang/Workspace/box_resize")
|
||||
train(dataset, show_bar=True)
|
||||
input("press enter to continue...")
|
||||
|
||||
@@ -35,16 +35,16 @@ CONV1_OUTPUT_CHANNELS = 4
|
||||
CONV2_KERNAL_SIZE = 3
|
||||
|
||||
# 第二层卷积输出通道数
|
||||
CONV2_OUTPUT_CHANNELS = 6
|
||||
CONV2_OUTPUT_CHANNELS = 8
|
||||
|
||||
# 第三层卷积核大小
|
||||
CONV3_KERNAL_SIZE = 3
|
||||
|
||||
# 第三层卷积输出通道数
|
||||
CONV3_OUTPUT_CHANNELS = 8
|
||||
CONV3_OUTPUT_CHANNELS = 16
|
||||
|
||||
# 第一层全连接宽度
|
||||
FC1_OUTPUT_NODES = 50
|
||||
FC1_OUTPUT_NODES = 60
|
||||
|
||||
# 第二层全连接宽度(输出标签类型数)
|
||||
FC2_OUTPUT_NODES = 15
|
||||
@@ -62,8 +62,8 @@ def forward(x, regularizer=None, keep_rate=tf.constant(1.0)):
|
||||
[CONV1_KERNAL_SIZE, CONV1_KERNAL_SIZE, int(x.shape[3]), CONV1_OUTPUT_CHANNELS]
|
||||
)
|
||||
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)
|
||||
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])
|
||||
@@ -100,7 +100,7 @@ def forward(x, regularizer=None, keep_rate=tf.constant(1.0)):
|
||||
|
||||
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.relu(tf.matmul(reshaped, fc1_w) + fc1_b)
|
||||
vars.extend([fc1_w, fc1_b])
|
||||
vars_name.extend(["fc1_w", "fc1_b"])
|
||||
nodes.extend([fc1])
|
||||
@@ -113,4 +113,3 @@ def forward(x, regularizer=None, keep_rate=tf.constant(1.0)):
|
||||
nodes.extend([fc2])
|
||||
|
||||
return nodes, vars, vars_name
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import cv2
|
||||
import random
|
||||
from tqdm import tqdm
|
||||
from forward import OUTPUT_NODES
|
||||
|
||||
# 原图像行数
|
||||
SRC_ROWS = 36
|
||||
|
||||
@@ -40,38 +41,64 @@ class DataSet:
|
||||
files = os.listdir(dir)
|
||||
for file in tqdm(files, postfix={"loading id": i}, dynamic_ncols=True):
|
||||
if file[-3:] == "jpg":
|
||||
sample = self.file2nparray("%s/%s" % (dir, file))
|
||||
label = self.id2label(i)
|
||||
if random.random() > 0.7:
|
||||
self.train_samples.append(self.file2nparray("%s/%s" % (dir, file)))
|
||||
self.train_labels.append(self.id2label(i))
|
||||
self.train_samples.append(sample)
|
||||
self.train_labels.append(label)
|
||||
if i == 0:
|
||||
tmp = sample.copy()
|
||||
tmp = tmp[:, :, ::-1]
|
||||
self.train_samples.append(tmp)
|
||||
self.train_labels.append(label)
|
||||
else:
|
||||
tmp = sample.copy()
|
||||
tmp = 1.2 * tmp
|
||||
tmp = np.where(tmp > 1, 1, tmp)
|
||||
tmp = np.where(tmp < 0, 0, tmp)
|
||||
self.train_samples.append(tmp)
|
||||
self.train_labels.append(label)
|
||||
tmp = sample.copy()
|
||||
tmp = 0.8 * tmp
|
||||
tmp = np.where(tmp > 1, 1, tmp)
|
||||
tmp = np.where(tmp < 0, 0, tmp)
|
||||
self.train_samples.append(tmp)
|
||||
self.train_labels.append(label)
|
||||
else:
|
||||
self.test_samples.append(self.file2nparray("%s/%s" % (dir, file)))
|
||||
self.test_labels.append(self.id2label(i))
|
||||
self.test_samples.append(sample)
|
||||
self.test_labels.append(label)
|
||||
self.train_samples = np.array(self.train_samples)
|
||||
self.train_labels = np.array(self.train_labels)
|
||||
self.test_samples = np.array(self.test_samples)
|
||||
self.test_labels = np.array(self.test_labels)
|
||||
return sets
|
||||
|
||||
def sample_train_sets(self, length):
|
||||
def sample_train_sets(self, length, std=0.0):
|
||||
samples = []
|
||||
labels = []
|
||||
for i in range(length):
|
||||
id = random.randint(0, len(self.train_samples)-1)
|
||||
id = random.randint(0, len(self.train_samples) - 1)
|
||||
samples.append(self.train_samples[id])
|
||||
labels.append(self.train_labels[id])
|
||||
return np.array(samples), np.array(labels)
|
||||
samples = np.array(samples).copy()
|
||||
samples += np.random.normal(0, std, samples.shape)
|
||||
labels = np.array(labels)
|
||||
return samples, labels
|
||||
|
||||
def sample_test_sets(self, length):
|
||||
def sample_test_sets(self, length, std=0.0):
|
||||
samples = []
|
||||
labels = []
|
||||
for i in range(length):
|
||||
id = random.randint(0, len(self.test_samples)-1)
|
||||
id = random.randint(0, len(self.test_samples) - 1)
|
||||
samples.append(self.test_samples[id])
|
||||
labels.append(self.test_labels[id])
|
||||
return np.array(samples), np.array(labels)
|
||||
samples = np.array(samples).copy()
|
||||
samples += np.random.normal(0, std, samples.shape)
|
||||
labels = np.array(labels)
|
||||
return samples, labels
|
||||
|
||||
def all_train_sets(self):
|
||||
def all_train_sets(self, std=0.0):
|
||||
return self.train_samples[:], self.train_labels[:]
|
||||
|
||||
def all_test_sets(self):
|
||||
def all_test_sets(self, std=0.0):
|
||||
return self.test_samples[:], self.test_labels[:]
|
||||
|
||||
Reference in New Issue
Block a user