Tensorflow object detection api 使用和数据增强旋转

前面的部分参考TensorFlow 训练自己的数据集(包括修改文件等等)
https://blog.csdn.net/int93/article/details/79064428

项目结构

my_train
    — dataset
    — models
    — object_detection
    — create_pascal_tf_record.py
    — eval.py
    — export_inference_graph.py
    — train.py
    — img_augmentation.py
    — pascal_label_map.pbtxt
    — ssd_mobilenet_v2_coco.config

创建tf record文件

python3 create_pascal_tf_record.py 
--data_dir=/Users/anida.qin/Desktop/Projects/Work_02/obj_train/my_train/dataset
--year=my_VOC 
--set=val
--output_path=/Users/anida.qin/Desktop/Projects/Work_02/obj_train/my_train/val.record 
--label_map_path=/Users/anida.qin/Desktop/Projects/Work_02/obj_train/my_train/pascal_label_map.pbtxt

python3 create_pascal_tf_record.py 
--data_dir=/Users/anida.qin/Desktop/Projects/Work_02/obj_train/my_train/dataset 
--year=my_VOC 
--set=train 
--output_path=/Users/anida.qin/Desktop/Projects/Work_02/obj_train/my_train/train.record 
--label_map_path=/Users/anida.qin/Desktop/Projects/Work_02/obj_train/my_train/pascal_label_map.pbtxt

训练

nohup python3 train.py --logtostderr &

输出.pb模型文件

python export_inference_graph.py 
--input_type image_tensor 
--pipeline_config_path ssd_mobilenet_v2_coco.config 
--trained_checkpoint_prefix train/model.ckpt-200000 --output_directory train/

图片旋转原理见 https://www.oschina.net/translate/opencv-rotation?print
有两种思路

思路1
思路2

注意
因为这个旋转会修改图片尺寸,因此千万别忘记修改xml文件里的图片大小。因为bbox regression需要用的,影响loss的。
具体解释见 https://blog.csdn.net/weixin_42280271/article/details/82052614

#!/usr/bin/env python

import cv2
import math
import numpy as np
import os
import xml.etree.ElementTree as ET
import random

class ImgAugemention():
    def __init__(self):
        self.angle = 90

    # rotate_img
    def rotate_image(self, src, angle, scale=1.):
        w = src.shape[1]
        h = src.shape[0]
        # convet angle into rad
        rangle = np.deg2rad(angle)  # angle in radians
        # calculate new image width and height
        nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
        nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
        # ask OpenCV for the rotation matrix
        rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)
        # calculate the move from the old center to the new center combined
   
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值