{"record":{"id":"c811fd1cd407d01e","repo":"keras-team/keras","slug":"if-using-weights-imagenet-with-include-top-a","errorCode":null,"errorMessage":"If using `weights=\"imagenet\"` with `include_top` as true, `classes` should be 1000. Received `classes={classes}`","messagePattern":"If using `weights=\"imagenet\"` with `include_top` as true, `classes` should be 1000\\. Received `classes=(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/mobilenet_v2.py","lineNumber":122,"sourceCode":"            Set `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: String, the name of the model.\n\n    Returns:\n        A model instance.\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            f\"Received `weights={weights}`\"\n        )\n\n    if weights == \"imagenet\" and include_top and classes != 1000:\n        raise ValueError(\n            'If using `weights=\"imagenet\"` with `include_top` '\n            f\"as true, `classes` should be 1000. Received `classes={classes}`\"\n        )\n\n    # Determine proper input shape and default size.\n    # If both input_shape and input_tensor are used, they should match\n    if input_shape is not None and input_tensor is not None:\n        try:\n            is_input_t_tensor = backend.is_keras_tensor(input_tensor)\n        except ValueError:\n            try:\n                is_input_t_tensor = backend.is_keras_tensor(\n                    operation_utils.get_source_inputs(input_tensor)\n                )\n            except ValueError:\n                raise ValueError(\n                    f\"input_tensor: {input_tensor}\"\n                    \"is not type input_tensor. \"","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/mobilenet_v2.py#L104-L140","documentation":"MobileNetV2 constraint in keras/src/applications/mobilenet_v2.py: the ImageNet checkpoint's top is a 1000-class softmax, so weights=\"imagenet\" with include_top=True requires classes=1000; other class counts cannot load the stored classifier weights.","triggerScenarios":"Thrown at keras/src/applications/mobilenet_v2.py:122 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use classes=1000 with imagenet weights and include_top=True","Set include_top=False and attach a custom head for your class count","Use weights=None and train the whole model for a custom task"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}