Python loops are slow. If u can’t find a way to use some Pytorch methods to replace these loops, u can write them in C++ or use TouchScript. I would suggest first try to replace these loops with other methods. I think the following code should achieve ur goal
h_max, w_max = image.max(0)[0], image.max(1)[0]
h_vec, w_vec=h_max>180, w_max>180
# these 2 tensors are bool tensors, u can cast them to other types