错误描述
pytorch在自定义dataset CELEBA类的时候,用到了以下表达:
class CELEBA(DatasetFolder):
def __init__(
self,
root: str,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
loader: Callable[[str], Any] = default_loader,
is_valid_file: Optional[Callable[[str], bool]] = None,
):
super(CELEBA, self).__init__(root, loader, IMG_EXTENSIONS if is_valid_file is None else None,
transform=transform,
target_transform=target_transform,
is_valid_file=is_valid_file)
self.imgs = self.samples
self.onehot_targets = encode_onehot(self.targets, 10177) # 50 10177
错误信息:
~/SageMaker/dsdh/data/celeba.py in CELEBA()
63 target_transform: Optional[Callable] = None,
64 loader: Callable[[str], Any] = default_loader,
—> 65 is_valid_file: Optional[Callable[[str], bool]] = None,
66 ):
67 super(CELEBA, self).init(root, loader, IMG_EXTENSIONS if is_valid_file is None else None,
NameError: name ‘Optional’ is not defined
解决方法
from typing import Optional, Callable, Any, Tuple