{
  "id": "pytorch/torchvision-transforms-pil-error",
  "signature": "TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>",
  "signature_zh": "类型错误：pic应为PIL图像或ndarray。得到<class 'torch.Tensor'>",
  "regex": "TypeError: pic should be PIL Image or ndarray\\. Got <class 'torch\\.Tensor'>",
  "domain": "pytorch",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A torchvision transform expecting PIL Image or numpy array received a raw torch.Tensor, often due to missing ToTensor() call or wrong transform order.",
  "root_cause_type": "generic",
  "root_cause_zh": "期望PIL图像或numpy数组的torchvision变换接收到了原始torch.Tensor，通常是由于缺少ToTensor()调用或变换顺序错误。",
  "versions": [
    {
      "version": "torch>=1.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torchvision>=0.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "transforms.Compose([transforms.Resize(256), transforms.ToTensor(), transforms.CenterCrop(224)])",
      "why_fails": "Adding ToTensor() after the transform that expects PIL will still pass a tensor, causing same error.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "from torchvision.transforms import ToPILImage; img = ToPILImage()(tensor)",
      "why_fails": "Converting tensor to PIL manually with ToPILImage() but forgetting to import often leads to NameError.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure ToTensor() is the last transform: transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor()]). This converts PIL to tensor after all PIL-based transforms.",
      "success_rate": 0.95,
      "how": "Ensure ToTensor() is the last transform: transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor()]). This converts PIL to tensor after all PIL-based transforms.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If using custom dataset, return PIL images: from PIL import Image; img = Image.open(path).convert('RGB')",
      "success_rate": 0.85,
      "how": "If using custom dataset, return PIL images: from PIL import Image; img = Image.open(path).convert('RGB')",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "确保ToTensor()是最后一个变换：transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor()])。这会在所有基于PIL的变换之后将PIL转换为张量。",
    "如果使用自定义数据集，返回PIL图像：from PIL import Image; img = Image.open(path).convert('RGB')"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/vision/stable/transforms.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-01-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}