def extract_patches(features, size, stride):
    """
    Arguments:
        features: a float tensor with shape [c, h, w].
        size: an integer, size of the patch.
        stride: an integer.
    Returns:
        a float tensor with shape [N, c * size * size],
        where N = n * m, n = 1 + floor((h - size)/stride),
        and m = 1 + floor((w - size)/stride).
    """
    c, h, w = features.size()
    patches = features.unfold(1, size, stride).unfold(2, size, stride)
    # it has shape [c, n, m, size, size]

    # get the number of patches
    n, m = patches.size()[1:3]
    N = n * m

    patches = patches.permute(1, 2, 0, 3, 4).contiguous()
    patches = patches.view(N, c * size * size)
    return patches