[英]Custom layer with Keras and Tensorflow
I am building a custom layer using Keras and Tensorflow, that calculate the intersection over union between a set of boxes.我正在使用 Keras 和 Tensorflow 构建一个自定义层,它计算一组框之间并集的交集。 a have two sets anchors and gt_boxes with different dmensions, and I will calculate the intersection over union between each element in anchors and gt_boxes when I execute the code the following error:
a有两组不同尺寸的anchors和gt_boxes,当我执行代码时,我将计算anchors和gt_boxes中每个元素之间的并集交集,出现以下错误:
Use fn_output_signature instead
Traceback (most recent call last):
File "acceuil.py", line 60, in <module>
train(train_data[1], val_data[1], dataset_name)
File "acceuil.py", line 28, in train
model.train()
File "/home/imene/APP-M/ROI/mod.py", line 40, in train
inputs, outputs = self.worker()
File "/home/imene/APP-M/ROI/mod.py", line 135, in worker
iou_anchors = ioulayer([tf_rois, tf_anchors])
File "/home/imene/anaconda3/envs/master/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 786, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/home/imene/anaconda3/envs/master/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 670, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
/home/imene/APP-M/ROI/IoULayer.py:25 call *
IoU_anchors = tf.map_fn(compute_IoU, (inputs[0], inputs[1] ), dtype=tf.float32)
/home/imene/anaconda3/envs/master/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py:538 new_func **
return func(*args, **kwargs)
/home/imene/anaconda3/envs/master/lib/python3.6/site-packages/tensorflow/python/ops/map_fn.py:451 map_fn
tensor.get_shape().with_rank_at_least(1)[0])))
/home/imene/anaconda3/envs/master/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py:315 merge_with
self.assert_is_compatible_with(other)
/home/imene/anaconda3/envs/master/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py:282 assert_is_compatible_with
(self, other))
ValueError: Dimensions 3 and 10 are not compatible
here is the code of my custom layer, written with python:这是我用python编写的自定义层的代码:
import sys
import numpy as np
import keras
from tensorflow.keras.layers import Layer
from tensorflow.python.keras.backend import map_fn
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
class IoULayer(Layer):
def __init__(self, **kwargs):
super(IoULayer, self).__init__(**kwargs)
# -------------------------------------------------------------------------------------------------
def call(self, inputs):
'''
inputs[0]: ground truth boxes
inputs[1]: anchors boxes
'''
def compute_IoU(inputs):
return IoULayer.multiple_IoU(inputs[0], inputs[1])
IoU_anchors = tf.map_fn(compute_IoU, (inputs[0], inputs[1] ), dtype=tf.float32)
return IoU_anchors
def multiple_IoU(gt_boxes, anchors):
def get_anchor_IoUs(gt_boxes, anchor):
return IoULayer.get_single_IoU(gt_boxes, anchor)
IoU = tf.map_fn(get_anchor_IoUs, gt_boxes, anchors, dtype=tf.float32)
return IoU
def get_single_IoU(gt_boxes, anchors):
iou_list = []
def single_iou(anchor):
result = []
for gt_bbx in gt_boxes:
x_left = max(gt_bbx[0], anchor[0])
y_top = max(gt_bbx[1], anchor[1])
x_right = min(gt_bbx[2], anchor[2])
y_bottom = min(gt_bbx[3], anchor[3])
bb1_area = (gt_bbx[2]- gt_bbx[0])*(gt_bbx[3]- gt_bbx[1])
anchor_area = (anchor[2]- anchor[0])*(anchor[3]- anchor[1])
intersect_area = abs(max((x_right - x_left), 0) * max((y_bottom - y_top),0))
iou = intersect_area / float(bb1_area + anchor_area - intersect_area)
result.append(iou)
return result
iou_list = tf.stack(single_iou(anchor) for anchor in anchors)
return iou_list
I don't understand what is the problem?我不明白有什么问题? Thanks for replying
感谢回复
The problem might be in the dimensions of your input.问题可能出在您输入的维度上。 From
tf.map_fn
documentation :来自
tf.map_fn
文档:
If elems is a tuple (or nested structure) of tensors, then those tensors must all have the same outer-dimension size (num_elems);
如果 elems 是张量的元组(或嵌套结构),则这些张量必须都具有相同的外维大小 (num_elems); and fn is used to transform each tuple (or structure) of corresponding slices from elems.
fn 用于从 elems 转换相应切片的每个元组(或结构)。 Eg, if elems is a tuple (t1, t2, t3), then fn is used to transform each tuple of slices (t1[i], t2[i], t3[i]) (where 0 <= i < num_elems).
例如,如果 elems 是元组 (t1, t2, t3),则 fn 用于转换切片的每个元组 (t1[i], t2[i], t3[i])(其中 0 <= i < num_elems) .
So in your case if tf_rois
and tf_anchors
have different dimension (like 3 and 10), fn_map
will fail because there are not enough elements in the smaller tensor to pair with the bigger tensor elements.因此,在您的情况下,如果
tf_rois
和tf_anchors
具有不同的维度(例如 3 和 10),则fn_map
将失败,因为较小的张量中没有足够的元素与较大的张量元素配对。
If you need to run your functions against all permutations of the two tensors, you need to find those permutations and feed two equal-sized tensors as elems
in fn_map
.如果您需要针对两个张量的所有排列运行您的函数,则需要找到这些排列并在
fn_map
两个大小相等的张量作为elems
fn_map
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.