{"record":{"id":"6eda9c0d45c5be4f","repo":"keras-team/keras","slug":"the-weights-argument-should-be-either-none-ra-6eda9c","errorCode":null,"errorMessage":"The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.","messagePattern":"The `weights` argument should be either `None` \\(random initialization\\), `imagenet` \\(pre-training on ImageNet\\), or the path to the weights file to be loaded\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/densenet.py","lineNumber":192,"sourceCode":"        classifier_activation: A `str` or callable.\n            The activation function to use\n            on the \"top\" layer. Ignored unless `include_top=True`. Set\n            `classifier_activation=None` to return the logits of the \"top\"\n            layer. When loading pretrained weights, `classifier_activation`\n            can only be `None` or `\"softmax\"`.\n        name: The name of the model (string).\n\n    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(),","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/densenet.py#L174-L210","documentation":"With padding=same, reconstruct_patches tolerates output sizes smaller than grid*patch_size (down to just above (grid-1)*p, i.e. at most one patch worth of overlap trimmed), because same padding guarantees coverage. An output_size outside ((g-1)*p, g*p] per axis would leave pixels uncovered or over-cover them, so it is rejected.","triggerScenarios":"output_size=(24, 24) with grid=4, patch=8 (24 <= (4-1)*8=24, boundary excluded); output_size larger than grid*p (e.g. 40 with grid=4, p=8); axis-wise mistakes where height is fine but width falls below the floor.","commonSituations":"Reconstructing to the original padded input size after cropping; mixing per-axis conventions (one axis valid, one same); assuming output_size can be arbitrary when padding=same.","solutions":["Choose output_size per axis in the range ((grid-1)*p, grid*p]; the common correct choice is grid*p or the original pre-pad input size","Drop output_size to let Keras infer grid*p","If you truly need a smaller output, crop after reconstruction instead of requesting an invalid size"],"exampleFix":"before: reconstruct_patches(p, size=(8,8), padding=\"same\", output_size=(24,24)) -> ValueError; after: out = reconstruct_patches(p, size=(8,8), padding=\"same\"); out = out[:, :24, :24, :]","handlingStrategy":"validation","validationCode":"for g, p, o in zip(grid_dims, patch_dims, output_size):\n    assert g * p - p < o <= g * p, f\"same padding needs o in ({g*p-p}, {g*p}], got {o}\"","typeGuard":"def same_padding_ok(grid, patch, out) -> bool:\n    return all(g * p - p < o <= g * p for g, p, o in zip(grid, patch, out))","tryCatchPattern":null,"preventionTips":["Pick output_size = grid*p or the pre-pad input size","Crop after reconstruction instead of requesting sub-floor sizes","Check each axis independently; one bad axis fails the whole call"],"tags":["keras","image-ops","padding","argument-validation"],"backgroundTag":"padding-output-size-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}