{"record":{"id":"87ca8b104d235b29","repo":"keras-team/keras","slug":"densenet-does-not-support-the-channels-first-ima","errorCode":null,"errorMessage":"DenseNet does not support the `channels_first` image data format. Switch to `channels_last` by editing your local config file at ~/.keras/keras.json","messagePattern":"DenseNet does not support the `channels_first` image data format\\. Switch to `channels_last` by editing your local config file at ~/\\.keras/keras\\.json","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/densenet.py","lineNumber":186,"sourceCode":"                the output of the model will be a 2D tensor.\n            - `max` means that global max pooling will\n                be applied.\n        classes: optional number of classes to classify images\n            into, only to be specified if `include_top` is `True`, and\n            if no `weights` argument is specified. Defaults to `1000`.\n        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","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/densenet.py#L168-L204","documentation":"With padding=valid, reconstruct_patches assumes no padding existed at extraction, so each output spatial dim must equal patch_size * grid_count exactly. If output_size disagrees with grid*p for any axis, reconstructing would need to invent or drop pixels, so compute_output_spec rejects it.","triggerScenarios":"reconstruct_patches(patches, size, output_size=(33, 33)) where grid=4 and p=8 (grid*p=32); extracting with strides < size (overlapping patches) but claiming padding=valid; output_size copied from the padded input shape instead of the valid-extraction shape.","commonSituations":"Using stride 1 overlapping patches and assuming full-size reconstruction; feeding the original image dims as output_size after a valid-mode extraction trimmed the border; porting from frameworks whose padding semantics differ.","solutions":["Set output_size per axis to exactly grid * patch_size (e.g. omit output_size and let it be inferred, or compute it from the grid dims of patches)","If patches overlapped or the input was padded, use padding=same with an output_size in the valid overlap range","Re-extract with strides=size (non-overlapping) if you want lossless valid reconstruction"],"exampleFix":"before: reconstruct_patches(p, size=(8,8), padding=\"valid\", output_size=(33,33)) -> ValueError; after: reconstruct_patches(p, size=(8,8), padding=\"valid\") # output_size inferred as (32,32)","handlingStrategy":"validation","validationCode":"for g, p, o in zip(grid_dims, patch_dims, output_size):\n    assert g * p == o, f\"valid padding needs o == g*p, got {o} != {g*p}\"","typeGuard":"def valid_padding_ok(grid, patch, out) -> bool:\n    return all(g * p == o for g, p, o in zip(grid, patch, out))","tryCatchPattern":null,"preventionTips":["Omit output_size to get the inferred grid*p shape","Use non-overlapping strides for lossless valid reconstruction","Remember valid extraction trims borders; do not pass original padded dims"],"tags":["keras","image-ops","padding","shape-mismatch"],"backgroundTag":"padding-output-size-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}