0

still learning Tensorflow and I'm trying to change a loss function in some code in Darkflow

The network outputs a given tensor with shape [49,3,2]. I would like to take the two elements in the last part of the tensor and process them with some code. I would then like to return the data back. So a bit like a map that would work with Tensorflow.

More Context - https://github.com/thtrieu/darkflow/blob/master/darkflow/net/yolo/train.py of the file I'm trying to change.

So not sure how to do this, please ask for more information If I've not been clear enough on the question. I still trying to get my head around what I want to do yet.

e.g

S = 7
SS = S * S 
C = 8 
B = 3

size1 = [None, SS, C]
size2 = [None, SS, B]


# Extract the coordinate prediction from net.out
coords = net_out[:, SS * (C + B):]
# Take flatten array and make it back into a tensor.
coords = tf.reshape(coords, [-1, SS, B, 4])
wh = tf.pow(coords[:,:,:,2:4], 2) * S # unit: grid cell
area_pred = wh[:,:,:,0] * wh[:,:,:,1] # unit: grid cell^2
centers = coords[:,:,:,0:2] # [batch, SS, B, 2]
floor = centers - (wh * .5) # [batch, SS, B, 2]
ceil  = centers + (wh * .5) # [batch, SS, B, 2]

# calculate the intersection areas 
# WHAT HAPPENS CURRENTLY 
intersect_upleft   = tf.maximum(floor, _upleft)
intersect_botright = tf.minimum(ceil , _botright)
intersect_wh = intersect_botright - intersect_upleft
intersect_wh = tf.maximum(intersect_wh, 0.0)
intersect = tf.multiply(intersect_wh[:,:,:,0], intersect_wh[:,:,:,1])

 # I WANT TO CALCULATE THE AREA OF INTERSECTION THE BOX DIFFERENTLY SO 
   I WOULD HAVE 
   MY OWN FUNCTION DOING SOMETHING. BUT I ONLY WANT IT DONE FOR CENTERS 
   AND THEN RETURN A BIT LIKE A MAP FUNCTION BUT I NEED IT TO WORK WITH 
   TENSORFLOW PLACEHOLDERS 

Any tips or advice would be good, thanks guy :D

1 Answers1

0

It seems the tf.map_fn function fits your needs. The documentation explains you can apply a Python callable to a tensor or a sequence of tensors.

An extract of the current documentation, about the main arguments of the function:

fn: The callable to be performed. It accepts one argument, which will have the same (possibly nested) structure as elems. Its output must have the same structure as dtype if one is provided, otherwise it must have the same structure as elems.

elems: A tensor or (possibly nested) sequence of tensors, each of which will be unpacked along their first dimension. The nested sequence of the resulting slices will be applied to fn.

This function is available from TensorFlow 0.8, so virtually always available.

Eric Platon
  • 9,819
  • 6
  • 41
  • 48