4
\$\begingroup\$

This code basically check whether a person is in the grid of another person.

The algorithm should check all possible 2-permutations of persons whether a person is in another person grid. In the first version. It was two for loops. The outer one iterates all persons while the inner one iterates all persons for the selected person in the outer for loop.

I changed the code by building and iterating on permutations of persons and using numpy array (original data is pytorch Variable). It got improved 10 times but I'm still looking for further improvements.

mnp = len(pedlist_seq)
#print(pedlist_seq)
#print(lookup_seq)


width, height = dimensions[0], dimensions[1]
#print("width: ",width,"height: ",height)

frame_mask = np.zeros((mnp, mnp, grid_size**2))
frame_np =  frame.data.numpy()

#print("frame: ", frame)

width_bound, height_bound = (neighborhood_size/(width*1.0)), (neighborhood_size/(height*1.0))
#width_bound, height_bound = (neighborhood_size/(width*1.0))*2, (neighborhood_size/(height*1.0))*2
#print("weight_bound: ", width_bound, "height_bound: ", height_bound)

list_indices = list(range(0, mnp))
for real_frame_index, other_real_frame_index in itertools.permutations(list_indices, 2):
    current_x, current_y = frame_np[real_frame_index, 0], frame_np[real_frame_index, 1]

    width_low, width_high = current_x - width_bound/2, current_x + width_bound/2
    height_low, height_high = current_y - height_bound/2, current_y + height_bound/2

    other_x, other_y = frame_np[other_real_frame_index, 0], frame_np[other_real_frame_index, 1]

    #if (other_x >= width_high).all() or (other_x < width_low).all() or (other_y >= height_high).all() or (other_y < height_low).all():
    if (other_x >= width_high) or (other_x < width_low) or (other_y >= height_high) or (other_y < height_low):
            # Ped not in surrounding, so binary mask should be zero
            #print("not surrounding")
            continue
    # If in surrounding, calculate the grid cell
    cell_x = int(np.floor(((other_x - width_low)/width_bound) * grid_size))
    cell_y = int(np.floor(((other_y - height_low)/height_bound) * grid_size))
    #print("cell_x: ", cell_x, "cell_y: ", cell_y)

    if cell_x >= grid_size or cell_x < 0 or cell_y >= grid_size or cell_y < 0:
            continue

    # Other ped is in the corresponding grid cell of current ped
    frame_mask[real_frame_index, other_real_frame_index, cell_x + cell_y*grid_size] = 1
    #print("frame mask shape %s"%str(frame_mask.shape))
\$\endgroup\$

put on hold as unclear what you're asking by Peter Taylor, VisualMelon, Toby Speight, dfhwze, pacmaninbw Aug 20 at 13:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Still no answer but I need your helps!!! \$\endgroup\$ – Baran Nama Jun 12 '18 at 11:05
  • 4
    \$\begingroup\$ I lost count of how many undefined variables this depends on, and none of them are explicitly described in the code or in the introduction. This needs a lot more context. \$\endgroup\$ – Peter Taylor Aug 20 at 9:42