{"record":{"id":"0fd862cae0752a98","repo":"keras-team/keras","slug":"if-using-weights-as-imagenet-with-include-t","errorCode":null,"errorMessage":"If using `weights` as `\"imagenet\"` with `include_top` as true, `classes` should be 1000","messagePattern":"If using `weights` as `\"imagenet\"` with `include_top` as true, `classes` should be 1000","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/densenet.py","lineNumber":200,"sourceCode":"    Returns:\n        A model instance.\n    \"\"\"\n    if backend.image_data_format() == \"channels_first\":\n        raise ValueError(\n            \"DenseNet does not support the `channels_first` image data \"\n            \"format. Switch to `channels_last` by editing your local \"\n            \"config file at ~/.keras/keras.json\"\n        )\n    if not (weights in {\"imagenet\", None} or file_utils.exists(weights)):\n        raise ValueError(\n            \"The `weights` argument should be either \"\n            \"`None` (random initialization), `imagenet` \"\n            \"(pre-training on ImageNet), \"\n            \"or the path to the weights file to be loaded.\"\n        )\n\n    if weights == \"imagenet\" and include_top and classes != 1000:\n        raise ValueError(\n            'If using `weights` as `\"imagenet\"` with `include_top`'\n            \" as true, `classes` should be 1000\"\n        )\n\n    # Determine proper input shape\n    input_shape = imagenet_utils.obtain_input_shape(\n        input_shape,\n        default_size=224,\n        min_size=32,\n        data_format=backend.image_data_format(),\n        require_flatten=include_top,\n        weights=weights,\n    )\n\n    if input_tensor is None:\n        img_input = layers.Input(shape=input_shape)\n    else:\n        if not backend.is_keras_tensor(input_tensor):","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/densenet.py#L182-L218","documentation":"Same eager guard as in extract_patches, on reconstruct_patches: size must be an int or a tuple/list, because reconstruction needs the patch extent to un-flatten each patch. Anything else (numpy array, string, None, dict) is a TypeError before any tensor work happens.","triggerScenarios":"reconstruct_patches(patches, size=np.int64(8)) or size=np.array([8,8]); size=None reaching the call from an optional config; passing a dict or string parsed from a config file.","commonSituations":"Round-tripping configs through JSON where lists become arrays via numpy; size stored in a dataclass with the wrong type annotation; glue code between extract and reconstruct that transforms size.","solutions":["Pass int or tuple/list of 2 or 3 ints: size=8 or size=(8, 8)","Coerce near the boundary: size = int(size) if isinstance(size, (int, np.integer)) else tuple(int(s) for s in size)","Share one validated size constant between the extract and reconstruct call sites"],"exampleFix":"before: reconstruct_patches(p, size=np.array([8, 8])) -> TypeError; after: reconstruct_patches(p, size=tuple(size.tolist()))","handlingStrategy":"type-guard","validationCode":"import numpy as np\nif isinstance(size, np.ndarray):\n    size = size.tolist()\nif isinstance(size, (np.integer,)):\n    size = int(size)\nassert isinstance(size, (int, tuple, list))","typeGuard":"def coerce_patch_size(size):\n    if isinstance(size, np.integer):\n        return int(size)\n    if isinstance(size, np.ndarray):\n        size = size.tolist()\n    if isinstance(size, list):\n        size = tuple(size)\n    return size","tryCatchPattern":"try:\n    recon = keras.ops.image.reconstruct_patches(patches, size=size)\nexcept TypeError as e:\n    raise ValueError(f\"invalid size {size!r} of {type(size).__name__}\") from e","preventionTips":["Coerce numpy scalars/arrays to int/tuple at API boundaries","Validate config-loaded sizes once at startup","Share one validated size between extract and reconstruct calls"],"tags":["keras","argument-validation","typeerror","image-ops"],"backgroundTag":"invalid-argument-type","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}