{"record":{"id":"efb84277ed61b765","repo":"huggingface/pytorch-image-models","slug":"invalid-class-map-file-expected-a-dict-class-ma","errorCode":null,"errorMessage":"Invalid class map file, expected a dict ({class_map_path}).","messagePattern":"Invalid class map file, expected a dict \\((.+?)\\)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"timm/data/readers/class_map.py","lineNumber":33,"sourceCode":"\n\ndef load_class_map(map_or_filename, root=''):\n    if isinstance(map_or_filename, dict):\n        assert dict, 'class_map dict must be non-empty'\n        return map_or_filename\n    class_map_path = map_or_filename\n    if not os.path.exists(class_map_path):\n        class_map_path = os.path.join(root, class_map_path)\n        assert os.path.exists(class_map_path), 'Cannot locate specified class map file (%s)' % map_or_filename\n    class_map_ext = os.path.splitext(map_or_filename)[-1].lower()\n    if class_map_ext == '.txt':\n        with open(class_map_path) as f:\n            class_to_idx = {v.strip(): k for k, v in enumerate(f)}\n    elif class_map_ext == '.pkl':\n        with open(class_map_path, 'rb') as f:\n            class_to_idx = _ClassMapUnpickler(f).load()\n        if not isinstance(class_to_idx, dict):\n            raise ValueError(f'Invalid class map file, expected a dict ({class_map_path}).')\n    else:\n        assert False, f'Unsupported class map file extension ({class_map_ext}).'\n    return class_to_idx\n\n","sourceCodeStart":15,"sourceCodeEnd":38,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/data/readers/class_map.py#L15-L38","documentation":"load_class_map unpickles .pkl class-map files with a restricted unpickler and requires the result to be a dict mapping class name to index. If the pickle contains anything else (list, string, arbitrary object), this ValueError is raised, protecting downstream indexing from a malformed class map.","triggerScenarios":"Calling load_class_map('classes.pkl') (directly or via a dataset reader's class_map argument) where the pickle holds a non-dict object, e.g. a pickled list of names, a numpy array, or a plain text file renamed to .pkl.","commonSituations":"User-created class map saved with pickle.dump(['a','b',...]) instead of a dict; a .txt class map mistakenly saved with a .pkl extension; class-map files generated by a different training framework with an incompatible schema.","solutions":["Regenerate the class map as a dict: {class_name: int_index} and pickle it.","If your classes are plain text, save the file as .txt (one class name per line) — load_class_map supports that natively.","Verify with python -c \"import pickle; print(type(pickle.load(open('map.pkl','rb'))))\" and reformat accordingly."],"exampleFix":"# before\nimport pickle\npickle.dump(['cat','dog'], open('map.pkl','wb'))  # list -> raises\n\n# after\nimport pickle\npickle.dump({'cat':0,'dog':1}, open('map.pkl','wb'))  # dict -> ok","handlingStrategy":"validation","validationCode":"import pickle\nm = pickle.load(open('map.pkl','rb'))\nassert isinstance(m, dict) and all(isinstance(k,str) and isinstance(v,int) for k,v in m.items()), 'bad class map'","typeGuard":null,"tryCatchPattern":"try:\n    class_to_idx = load_class_map(path)\nexcept ValueError as e:\n    log.error(f'class map {path} invalid: {e}'); raise","preventionTips":["Always save class maps as {'name': idx} dicts.","Prefer the plain .txt one-class-per-line format for portability.","Validate pickled files after generating them."],"tags":["timm","class-map","pickle","dataset"],"backgroundTag":"invalid-pickle-payload","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}