tensorflow 的object detection api有一系列预处理方法(如flip等),但不包括旋转rotation,这可能是因为常规项目不会涉及到图像的360度旋转。但旋转在某些特定任务中尤为重要,笔者最近的遥感图像检测和血细胞检测项目采用的都是俯瞰视角,这种情况下,训练数据集的旋转操作可以使得网络具有方向的鲁棒性。
初步发现,api中常规的预处理操作是封装在.proto文件里面了,再通过.config文件设置、选择,preprogress.py等文件集成、调用。要直接添加,需要修改最初的配置文件,笔者水平有限,程序改不了那么深。。。
因此,直接在数据集上进行操作,对图像进行旋转,并对相应的annotation中的.xml文件进行修改,使得图像和目标按特定角度旋转。暂定每张图像旋转20次,每次18度。这样,原来的一张图像就生成了20张旋转后的图像,同时也进行了数据集的扩充。方法也比较粗暴,但简单可行。
首先是图像旋转的程序,
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 9 09:57:04 2018
@author: PC
"""
import tensorflow as tf
from scipy import misc
import numpy as np
import os
from skimage import transform
global angle
angle = 0.0
#文件名
def file_name_original(file):
return os.path.splitext(file)[0]
#扩展名
def file_name_extension(file):
return os.path.splitext(file)[1]
#随机旋转图片
def random_rotate_image(image_input_path, image_output_path, image_file, num):
global angle
with tf.Graph().as_default():
# tf.set_random_seed(666)
file_contents = tf.read_file(os.path.join(image_input_path, image_file))
image = tf.image.decode_image(file_contents, channels=3)
image_rotate_en_list = []
def random_rotate_image_func(image, angle):
return misc.imrotate(image, angle, 'bicubic')
# return transform.rotate(image, angle,resize=True)
for i in range(num):
image_rotate = tf.py_func(random_rotate_image_func, [image, angle], tf.uint8)
angle = (angle + 18)
# image_rotate = misc.imrotate(image, 90, 'bicubic')