{
  "id": 527028,
  "title": "Can someone help me with my model",
  "url": "/competitions/rsna-2024-lumbar-spine-degenerative-classification/discussion/527028",
  "author_name": "",
  "post_date": "2024-08-09T15:32:40.433746600Z",
  "votes": null,
  "comment_count": 3,
  "views": 0,
  "content": "<p>My model is stuck at 75% accuracy can someone fix the issue for me </p>\n<p>X_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)<br>\nnum_classes = len(label_encoder.classes_)<br>\ny_train = to_categorical(y_train, num_classes)<br>\ny_val = to_categorical(y_val, num_classes)</p>\n<p>from tensorflow.keras.models import Sequential<br>\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout<br>\nfrom tensorflow.keras.optimizers import Adam<br>\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping</p>\n<p>model = Sequential([<br>\n    Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),<br>\n    MaxPooling2D(pool_size=(2, 2)),<br>\n    Conv2D(64, (3, 3), activation='relu'),<br>\n    MaxPooling2D(pool_size=(2, 2)),<br>\n    Conv2D(128, (3, 3), activation='relu'),<br>\n    MaxPooling2D(pool_size=(2, 2)),<br>\n    Flatten(),<br>\n    Dense(128, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(64, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(1, activation='sigmoid')  # For binary classification<br>\n])</p>\n<h1>Compile with Adam optimizer and reduced learning rate</h1>\n<p>model.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])</p>\n<p>model.fit(X_train, y_train, epochs=20, batch_size=16, validation_split=0.2)</p>",
  "messages": [
    {
      "id": "2954329",
      "postDate": "08/09/2024 15:32:40",
      "content": "<p>My model is stuck at 75% accuracy can someone fix the issue for me </p>\n<p>X_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)<br>\nnum_classes = len(label_encoder.classes_)<br>\ny_train = to_categorical(y_train, num_classes)<br>\ny_val = to_categorical(y_val, num_classes)</p>\n<p>from tensorflow.keras.models import Sequential<br>\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout<br>\nfrom tensorflow.keras.optimizers import Adam<br>\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping</p>\n<p>model = Sequential([<br>\n    Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),<br>\n    MaxPooling2D(pool_size=(2, 2)),<br>\n    Conv2D(64, (3, 3), activation='relu'),<br>\n    MaxPooling2D(pool_size=(2, 2)),<br>\n    Conv2D(128, (3, 3), activation='relu'),<br>\n    MaxPooling2D(pool_size=(2, 2)),<br>\n    Flatten(),<br>\n    Dense(128, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(64, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(1, activation='sigmoid')  # For binary classification<br>\n])</p>\n<h1>Compile with Adam optimizer and reduced learning rate</h1>\n<p>model.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])</p>\n<p>model.fit(X_train, y_train, epochs=20, batch_size=16, validation_split=0.2)</p>",
      "rawMarkdown": "My model is stuck at 75% accuracy can someone fix the issue for me \n\n\n\nX_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)\nnum_classes = len(label_encoder.classes_)\ny_train = to_categorical(y_train, num_classes)\ny_val = to_categorical(y_val, num_classes)\n\n\n\n\n\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')  # For binary classification\n])\n\n# Compile with Adam optimizer and reduced learning rate\nmodel.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])\n\nmodel.fit(X_train, y_train, epochs=20, batch_size=16, validation_split=0.2)",
      "votes": null
    },
    {
      "id": "2954910",
      "postDate": "08/10/2024 10:18:33",
      "content": "<p>try using the pretrained or fine-tune model. you can use it by importing from \"from keras.application … import …\" here's <a href=\"https://keras.io/api/applications/\" target=\"_blank\">doc</a> or you can install / upgrade keras_cv</p>",
      "rawMarkdown": "try using the pretrained or fine-tune model. you can use it by importing from \"from keras.application ... import ...\" here's [doc](https://keras.io/api/applications/) or you can install / upgrade keras_cv",
      "votes": null
    },
    {
      "id": "2954943",
      "postDate": "08/10/2024 12:06:55",
      "content": "<p>Getting 75% accuracy simply means model has learned that  'Normal/Mild' is majority class and it is predicting 'Normal/Mild' for everything.( 100 % 'Normal/Mild' prediction with ~ 75% actual 'Normal/Mild' labels give ~ 75% accuracy.</p>\n<p>For this competition, you should monitor other metrics like actual crossentropy loss, or competition metric which will better reflect model performance and not accuracy.</p>\n<p>you can see how your model performance is increasing with these metrics , accuracy will suddenly jump when crossentropy loss is sufficiently small.</p>",
      "rawMarkdown": "Getting 75% accuracy simply means model has learned that  'Normal/Mild' is majority class and it is predicting 'Normal/Mild' for everything.( 100 % 'Normal/Mild' prediction with ~ 75% actual 'Normal/Mild' labels give ~ 75% accuracy.\n\nFor this competition, you should monitor other metrics like actual crossentropy loss, or competition metric which will better reflect model performance and not accuracy.\n\nyou can see how your model performance is increasing with these metrics , accuracy will suddenly jump when crossentropy loss is sufficiently small.",
      "votes": null
    },
    {
      "id": "2963539",
      "postDate": "08/18/2024 19:30:31",
      "content": "<p>I wouldn't advice you to create your own model, try using resnet50</p>\n<p>`import tensorflow as tf<br>\nfrom tensorflow.keras.applications import ResNet50, ResNet18, ConvNeXt, DenseNet121, EfficientNetB0<br>\nfrom tensorflow.keras.models import Sequential<br>\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, GlobalAveragePooling2D<br>\nfrom tensorflow.keras.optimizers import Adam<br>\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping<br>\nfrom tensorflow.keras.utils import to_categorical<br>\nfrom sklearn.model_selection import train_test_split</p>\n<h1>Train-test split</h1>\n<p>X_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)</p>\n<h1>Number of classes (assumed to be binary classification)</h1>\n<p>num_classes = len(label_encoder.classes_)<br>\ny_train = to_categorical(y_train, num_classes)<br>\ny_val = to_categorical(y_val, num_classes)</p>\n<h1>Set the base model - choose one</h1>\n<p>base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))  # Or use any of the others</p>\n<h1>base_model = ResNet18(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>base_model = ConvNeXt(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>Build the model</h1>\n<p>model = Sequential([<br>\n    base_model,<br>\n    GlobalAveragePooling2D(),<br>\n    Dense(128, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(64, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(1, activation='sigmoid')  # For binary classification<br>\n])</p>\n<h1>Compile the model</h1>\n<p>model.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])</p>\n<h1>Check for multiple GPUs</h1>\n<p>strategy = tf.distribute.MirroredStrategy()<br>\nwith strategy.scope():<br>\n    model = tf.keras.models.clone_model(model)</p>\n<h1>Training callbacks</h1>\n<p>reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6, verbose=1)<br>\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)</p>\n<h1>Train the model</h1>\n<p>model.fit(<br>\n    X_train, y_train,<br>\n    epochs=20,<br>\n    batch_size=16,<br>\n    validation_data=(X_val, y_val),<br>\n    callbacks=[reduce_lr, early_stopping]<br>\n)<br>\n`<br>\nand that is all</p>",
      "rawMarkdown": "I wouldn't advice you to create your own model, try using resnet50\n\n`import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50, ResNet18, ConvNeXt, DenseNet121, EfficientNetB0\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\n# Train-test split\nX_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)\n\n# Number of classes (assumed to be binary classification)\nnum_classes = len(label_encoder.classes_)\ny_train = to_categorical(y_train, num_classes)\ny_val = to_categorical(y_val, num_classes)\n\n# Set the base model - choose one\nbase_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))  # Or use any of the others\n# base_model = ResNet18(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n# base_model = ConvNeXt(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n# base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n# base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Build the model\nmodel = Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')  # For binary classification\n])\n\n# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])\n\n# Check for multiple GPUs\nstrategy = tf.distribute.MirroredStrategy()\nwith strategy.scope():\n    model = tf.keras.models.clone_model(model)\n\n# Training callbacks\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6, verbose=1)\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\n\n# Train the model\nmodel.fit(\n    X_train, y_train,\n    epochs=20,\n    batch_size=16,\n    validation_data=(X_val, y_val),\n    callbacks=[reduce_lr, early_stopping]\n)\n`\nand that is all",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2954910,
      "author_name": "naufalhafishj",
      "author_url": "",
      "post_date": "08/10/2024 10:18:33",
      "content": "<p>try using the pretrained or fine-tune model. you can use it by importing from \"from keras.application … import …\" here's <a href=\"https://keras.io/api/applications/\" target=\"_blank\">doc</a> or you can install / upgrade keras_cv</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 2954943,
      "author_name": "rohitchaudhari25",
      "author_url": "",
      "post_date": "08/10/2024 12:06:55",
      "content": "<p>Getting 75% accuracy simply means model has learned that  'Normal/Mild' is majority class and it is predicting 'Normal/Mild' for everything.( 100 % 'Normal/Mild' prediction with ~ 75% actual 'Normal/Mild' labels give ~ 75% accuracy.</p>\n<p>For this competition, you should monitor other metrics like actual crossentropy loss, or competition metric which will better reflect model performance and not accuracy.</p>\n<p>you can see how your model performance is increasing with these metrics , accuracy will suddenly jump when crossentropy loss is sufficiently small.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 2963539,
      "author_name": "theexaltedone",
      "author_url": "",
      "post_date": "08/18/2024 19:30:31",
      "content": "<p>I wouldn't advice you to create your own model, try using resnet50</p>\n<p>`import tensorflow as tf<br>\nfrom tensorflow.keras.applications import ResNet50, ResNet18, ConvNeXt, DenseNet121, EfficientNetB0<br>\nfrom tensorflow.keras.models import Sequential<br>\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, GlobalAveragePooling2D<br>\nfrom tensorflow.keras.optimizers import Adam<br>\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping<br>\nfrom tensorflow.keras.utils import to_categorical<br>\nfrom sklearn.model_selection import train_test_split</p>\n<h1>Train-test split</h1>\n<p>X_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)</p>\n<h1>Number of classes (assumed to be binary classification)</h1>\n<p>num_classes = len(label_encoder.classes_)<br>\ny_train = to_categorical(y_train, num_classes)<br>\ny_val = to_categorical(y_val, num_classes)</p>\n<h1>Set the base model - choose one</h1>\n<p>base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))  # Or use any of the others</p>\n<h1>base_model = ResNet18(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>base_model = ConvNeXt(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))</h1>\n<h1>Build the model</h1>\n<p>model = Sequential([<br>\n    base_model,<br>\n    GlobalAveragePooling2D(),<br>\n    Dense(128, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(64, activation='relu'),<br>\n    Dropout(0.5),<br>\n    Dense(1, activation='sigmoid')  # For binary classification<br>\n])</p>\n<h1>Compile the model</h1>\n<p>model.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])</p>\n<h1>Check for multiple GPUs</h1>\n<p>strategy = tf.distribute.MirroredStrategy()<br>\nwith strategy.scope():<br>\n    model = tf.keras.models.clone_model(model)</p>\n<h1>Training callbacks</h1>\n<p>reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6, verbose=1)<br>\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)</p>\n<h1>Train the model</h1>\n<p>model.fit(<br>\n    X_train, y_train,<br>\n    epochs=20,<br>\n    batch_size=16,<br>\n    validation_data=(X_val, y_val),<br>\n    callbacks=[reduce_lr, early_stopping]<br>\n)<br>\n`<br>\nand that is all</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2954329": "My model is stuck at 75% accuracy can someone fix the issue for me \n\n\n\nX_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)\nnum_classes = len(label_encoder.classes_)\ny_train = to_categorical(y_train, num_classes)\ny_val = to_categorical(y_val, num_classes)\n\n\n\n\n\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')  # For binary classification\n])\n\n# Compile with Adam optimizer and reduced learning rate\nmodel.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])\n\nmodel.fit(X_train, y_train, epochs=20, batch_size=16, validation_split=0.2)",
    "2954910": "try using the pretrained or fine-tune model. you can use it by importing from \"from keras.application ... import ...\" here's [doc](https://keras.io/api/applications/) or you can install / upgrade keras_cv",
    "2954943": "Getting 75% accuracy simply means model has learned that  'Normal/Mild' is majority class and it is predicting 'Normal/Mild' for everything.( 100 % 'Normal/Mild' prediction with ~ 75% actual 'Normal/Mild' labels give ~ 75% accuracy.\n\nFor this competition, you should monitor other metrics like actual crossentropy loss, or competition metric which will better reflect model performance and not accuracy.\n\nyou can see how your model performance is increasing with these metrics , accuracy will suddenly jump when crossentropy loss is sufficiently small.",
    "2963539": "I wouldn't advice you to create your own model, try using resnet50\n\n`import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50, ResNet18, ConvNeXt, DenseNet121, EfficientNetB0\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\n# Train-test split\nX_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.2, random_state=42)\n\n# Number of classes (assumed to be binary classification)\nnum_classes = len(label_encoder.classes_)\ny_train = to_categorical(y_train, num_classes)\ny_val = to_categorical(y_val, num_classes)\n\n# Set the base model - choose one\nbase_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))  # Or use any of the others\n# base_model = ResNet18(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n# base_model = ConvNeXt(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n# base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n# base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Build the model\nmodel = Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')  # For binary classification\n])\n\n# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])\n\n# Check for multiple GPUs\nstrategy = tf.distribute.MirroredStrategy()\nwith strategy.scope():\n    model = tf.keras.models.clone_model(model)\n\n# Training callbacks\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6, verbose=1)\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\n\n# Train the model\nmodel.fit(\n    X_train, y_train,\n    epochs=20,\n    batch_size=16,\n    validation_data=(X_val, y_val),\n    callbacks=[reduce_lr, early_stopping]\n)\n`\nand that is all"
  },
  "source": "meta"
}