tensorflow-自定义op

在处理数据时需要自定义处理方法,这时就可以使用tf.py_func(func, inp, Tout, stateful=True, name=None)来进行处理

具体例子

import tensorflow as tf

import cv2
import numpy as np


def forward_convert(coordinate, with_label=True):
    """
    :param coordinate: format [y_c, x_c, h, w, theta]
    :return: format [y1, x1, y2, x2, y3, x3, y4, x4]
    """
    boxes = []
    if with_label:
        for rect in coordinate:
            box = cv2.boxPoints(((rect[1], rect[0]), (rect[3], rect[2]), rect[4]))
            box = np.reshape(box, [-1, ])
            boxes.append([box[1], box[0], box[3], box[2], box[5], box[4], box[7], box[6], rect[5]])
    else:
        for rect in coordinate:
            box = cv2.boxPoints(((rect[1], rect[0]), (rect[3], rect[2]), rect[4]))
            box = np.reshape(box, [-1, ])
            boxes.append([box[1], box[0], box[3], box[2], box[5], box[4], box[7], box[6]])

    return np.array(boxes, dtype=np.float32)


coord = np.array([[150, 150, 50, 100, -90, 1],
                  [150, 150, 100, 50, -90, 1],
                  [150, 150, 50, 100, -45, 1],
                  [150, 150, 100, 50, -45, 1]])
coord = tf.constant(coord)

convert = tf.py_func(forward_convert, inp=[coord], Tout=tf.float32)

with tf.Session() as sess:
    print(sess.run(convert))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值