def make_config(self, **options: Any) -> DatasetConfig: if not self._valid_options and options: raise ValueError( f"Dataset {self.name} does not take any options, " f"but got {sequence_to_str(list(options), separate_last=' and')}." ) for name, arg in options.items(): if name not in self._valid_options: raise ValueError( add_suggestion( f"Unknown option '{name}' of dataset {self.name}.", word=name, possibilities=sorted(self._valid_options.keys()), )) valid_args = self._valid_options[name] if arg not in valid_args: raise ValueError( add_suggestion( f"Invalid argument '{arg}' for option '{name}' of dataset {self.name}.", word=arg, possibilities=valid_args, )) return DatasetConfig(self.default_config, **options)
def __new__(cls, data, *, dtype=None, device=None, like=None, **kwargs): unknown_meta_attrs = kwargs.keys() - cls._META_ATTRS if unknown_meta_attrs: unknown_meta_attr = sorted(unknown_meta_attrs)[0] raise TypeError( add_suggestion( f"{cls.__name__}() got unexpected keyword '{unknown_meta_attr}'.", word=unknown_meta_attr, possibilities=sorted(cls._META_ATTRS), ) ) if like is not None: dtype = dtype or like.dtype device = device or like.device data = cls._to_tensor(data, dtype=dtype, device=device) requires_grad = False self = torch.Tensor._make_subclass(cast(_TensorBase, cls), data, requires_grad) meta_data = dict() for attr, (explicit, fallback) in cls._parse_meta_data(**kwargs).items(): if explicit is not DEFAULT: value = explicit elif like is not None: value = getattr(like, attr) else: value = fallback(data) if callable(fallback) else fallback meta_data[attr] = value self._meta_data = meta_data return self
def make_config(self, **options: Any) -> DatasetConfig: for name, arg in options.items(): if name not in self._valid_options: raise ValueError( add_suggestion( f"Unknown option '{name}' of dataset {self.name}.", word=name, possibilities=sorted(self._valid_options.keys()), )) valid_args = self._valid_options[name] if arg not in valid_args: raise ValueError( add_suggestion( f"Invalid argument '{arg}' for option '{name}' of dataset {self.name}.", word=arg, possibilities=valid_args, )) return DatasetConfig(self.default_config, **options)
def find(dct: Dict[str, T], name: str) -> T: name = name.lower() try: return dct[name] except KeyError as error: raise ValueError( add_suggestion( f"Unknown dataset '{name}'.", word=name, possibilities=dct.keys(), alternative_hint=lambda _: ("You can use torchvision.datasets.list_datasets() to get a list of all available datasets." ), )) from error