def flatten_grid(x):
    """ collapses spatial dimensions of pytorch tensor 'x' and transposes
        Inputs:
            x -- 1xCxHxW pytorch tensor
        Outputs:
            y -- (H*W)xC pytorch tensor
    """
    assert x.size(0) == 1, "undefined behavior for batched input"
    y = x.contiguous().view(x.size(1), -1).clone().transpose(1, 0)
    return y