前面的部分参考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
- 先以图片中心旋转图片,得到旋转矩阵
- 然后平移到新的图片尺寸下(不会被遮挡的)的图像中心
- (Rp+t)+m 所以m=(new_w-old_w)/2, (new_h-old_h)/2
- 代码 https://www.cnblogs.com/darkknightzh/p/5070576.html
思路2
- 以新图片尺寸的中心为轴,得到旋转矩阵
- 然后再计算平移量
- R(Ip+m)+t=Rp+Rm+t 所以应该求Rm, m和思路1 的m一样,但是要乘R
- 代码 https://www.oschina.net/translate/opencv-rotation?print
注意
因为这个旋转会修改图片尺寸,因此千万别忘记修改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