要学会看源码啊...
最近google 上不去,就用bing来举例好了,一般来讲,看了各种example代码以后,发现transformer 里面有一个 totensor,所以直接搜索torchvision.transforms,看里面的文档,在文档里面搜索to_tensor,里面有对这个函数的解释,然后查看源码 ,源码如下:
def to_tensor(pic):
"""Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
See ``ToTensor`` for more details.
Args:
pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
Returns:
Tensor: Converted image.
"""
if not(_is_pil_image(pic) or _is_numpy_image(pic)):
raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
if isinstance(pic, np.ndarray):
# handle numpy array
if pic.ndim == 2:
pic = pic[:, :, None]
img = torch.from_numpy(pic.transp