python - 如何禁用 Darkflow YOLOv2 上的图像翻转?
问题描述
我目前正在训练基于方向的模型。我希望能够训练一个模型来判断物体的估计方向。目前,我在 1000 个 epoch 左右,准确度不好。我的理论是翻转操作似乎会导致模型不准确,因为 90 度的方向可能会翻转到 -90 度。因此,这两个单独的类会相互混淆。
def imcv2_affine_trans(im):
# Scale and translate
h, w, c = im.shape
scale = np.random.uniform() / 10. + 1.
max_offx = (scale-1.) * w
max_offy = (scale-1.) * h
offx = int(np.random.uniform() * max_offx)
offy = int(np.random.uniform() * max_offy)
im = cv2.resize(im, (0,0), fx = scale, fy = scale)
im = im[offy : (offy + h), offx : (offx + w)]
flip = np.random.binomial(1, .5)
if flip: im = cv2.flip(im, 1)
return im, [w, h, c], [scale, [offx, offy], flip]
def preprocess(self, im, allobj = None):
"""
Takes an image, return it as a numpy tensor that is readily
to be fed into tfnet. If there is an accompanied annotation (allobj),
meaning this preprocessing is serving the train process, then this
image will be transformed with random noise to augment training data,
using scale, translation, flipping and recolor. The accompanied
parsed annotation (allobj) will also be modified accordingly.
"""
if type(im) is not np.ndarray:
im = cv2.imread(im)
if allobj is not None: # in training mode
result = imcv2_affine_trans(im)
im, dims, trans_param = result
scale, offs, flip = trans_param
for obj in allobj:
_fix(obj, dims, scale, offs)
if not flip: continue
obj_1_ = obj[1]
obj[1] = dims[0] - obj[3]
obj[3] = dims[0] - obj_1_
im = imcv2_recolor(im)
im = self.resize_input(im)
if allobj is None: return im
return im#, np.array(im) # for unit testing
这些是与训练期间的数据增强相关的代码。如果我的理论是正确的,我想咨询你的意见?如果是这样,我如何禁用翻转操作但保留其余的数据扩充?谢谢!
解决方案
我有同样的问题,但我找到了答案。我们可以在darkflow\darkflow\net\yolo\predict.py 上看到“from ...utils.im_transform import imcv2_recolor, imcv2_affine_trans”
函数 imcv2_affine_trans 在 darkflow\darkflow\utils\im_transform.py 中定义
所以我们可以像下面那样禁用翻转。
def imcv2_affine_trans(im):
# Scale and translate
h, w, c = im.shape
scale = np.random.uniform() / 10. + 1.
max_offx = (scale-1.) * w
max_offy = (scale-1.) * h
offx = int(np.random.uniform() * max_offx)
offy = int(np.random.uniform() * max_offy)
im = cv2.resize(im, (0,0), fx = scale, fy = scale)
im = im[offy : (offy + h), offx : (offx + w)]
flip = 0#np.random.binomial(1, .5)
#if flip: im = cv2.flip(im, 1)
return im, [w, h, c], [scale, [offx, offy], flip]
希望这可以帮助!
推荐阅读
- swift - 使用 List<> 值时获取领域值
- python - 如何比较字符串python中的特定项目
- sql - 比较两张表。想要回旗
- python - NLP // 使用“基于计数”的模型创建单词的向量表示
- javascript - 如何获取 td 的数据并将其转换为变量 php?
- rest - 微服务:身份验证的最佳实践
- database - 全部和部分参与 ERD
- javascript - JS Regex:如何将重复模式捕获到不同的数组元素中?
- c# - 为什么我的 GUI 健康条从两个方向而不是仅从 1 个方向流失?
- python-3.x - PYTHON - 如何检查我是否正确调用了这个 API?