1.将一个列表截取为batch_size的整数倍,常用于深度神经网络训练
a=[1,2,3,4,5,6]
batch_size=4
b=len(a)%batch_size
print(a[:-b])
输出结果
[1, 2, 3, 4]
2.Faster RCNN锚点的生成
步骤2.1将特征图上的每个点生成一个列表【x,y,x,y】
def generate_anchors_pre(height, width, feat_stride, anchor_scales=(8, 16, 32), anchor_ratios=(0.5, 1, 2)):
""" A wrapper function to generate anchors given different scales
Also return the number of anchors in variable 'length'
"""
#生成9个目标框
anchors = generate_anchors(ratios=np.array(anchor_ratios),
scales=np.array(anchor_scales))
A = anchors.shape[0]
#步骤1,生成图像网格图
shift_x = np.arange(0, width) * feat_stride
shift_y = np.arange(0, height) * feat_stride
shift_x, shift_y = np.meshgrid(shift_x, shift_y)
#步骤2,生成[x.T,y.T,x.T,y.T]
shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
K = shifts.shape[0]
# width changes faster, so here it is H, W, C
#步骤3,装置为(k,1,4)与瞄点框相加,生成每个特征点的9个目标框
anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
length = np.int32(anchors.shape[0])
return anchors, length
2.2根据给定的一个目标框大小,生成9个目标框
def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
scales=2 ** np.arange(3, 6)):
"""
Generate anchor (reference) windows by enumerating aspect ratios X
scales wrt a reference (0, 0, 15, 15) window.
"""
#生成等面积且长宽比例为ratios=[0.5, 1, 2]的三个框
base_anchor = np.array([1, 1, base_size, base_size]) - 1
ratio_anchors = _ratio_enum(base_anchor, ratios)
# 根据每一个框再生成放大倍数为8,16,32的三个框
anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
for i in range(ratio_anchors.shape[0])])
return anchors
def _whctrs(anchor):
"""
Return width, height, x center, and y center for an anchor (window).
"""
w = anchor[2] - anchor[0] + 1
h = anchor[3] - anchor[1] + 1
x_ctr = anchor[0] + 0.5 * (w - 1)
y_ctr = anchor[1] + 0.5 * (h - 1)
return w, h, x_ctr, y_ctr
def _mkanchors(ws, hs, x_ctr, y_ctr):
"""
Given a vector of widths (ws) and heights (hs) around a center
(x_ctr, y_ctr), output a set of anchors (windows).
【框左上角点X,框左上角点Y,框右下角点X,框右下角点Y】
"""
ws = ws[:, np.newaxis]
hs = hs[:, np.newaxis]
anchors = np.hstack((x_ctr - 0.5 * (ws - 1),
y_ctr - 0.5 * (hs - 1),
x_ctr + 0.5 * (ws - 1),
y_ctr + 0.5 * (hs - 1)))
return anchors
def _ratio_enum(anchor, ratios):
"""
Enumerate a set of anchors for each aspect ratio wrt an anchor.
#生成三个等面积,长宽比不同的的框
"""
w, h, x_ctr, y_ctr = _whctrs(anchor)
size = w * h
size_ratios = size / ratios
ws = np.round(np.sqrt(size_ratios))
hs = np.round(ws * ratios)
anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
return anchors
def _scale_enum(anchor, scales):
"""
Enumerate a set of anchors for each scale wrt an anchor.
#将3个等面积,长宽比不同的的框,每个在放大得到3个框
"""
w, h, x_ctr, y_ctr = _whctrs(anchor)
ws = w * scales
hs = h * scales
anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
return anchors
2.3将2.1和2.2加在一起,为每个特征图上的像素点生成9个目标框
3,tensorflow模型保存于加载,实现中断训练后,下次训练接着之前的模型进行训练
#1.创建会话对象
sess = tf.Session()
#2.创建模型保存对象
saver = tf.train.Saver()
3.初始化变量
sess.run(tf.global_variables_initializer())
4.判断FLAGS.logs_dir路径下是否有模型存在,如果有,加载最新的训练模型
ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print("Model restored...")
5.判断训练,还是验证
5.1若为训练
保存模型
5.2若为验证
直接执行代码