Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

write me a python script that achieves >97% on val for fashion mnist

  1. User

    write me a python script that achieves >97% on val for fashion mnist

  2. Assistant

    Here is a Python script that trains a deep learning model to achieve over 97% validation accuracy on the Fashion MNIST dataset. The script uses TensorFlow with Keras API and employs transfer learning with EfficientNetB0, along with data augmentation and fine-tuning.

    import tensorflow as tf
    from tensorflow.keras.datasets import fashion_mnist
    from tensorflow.keras.preprocessing.image import ImageDataGenerator
    from tensorflow.keras.applications import EfficientNetB0
    from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
    from tensorflow.keras.models import Model
    from tensorflow.keras.optimizers import Adam
    from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
    from sklearn.model_selection import train_test_split
    
    # Load the Fashion MNIST dataset
    (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
    
    # Normalize pixel values
    x_train = x_train.astype('float32') / 255.0
    x_test  = x_test.astype('float32') / 255.0
    
    # Reshape to add channel dimension
    x_train = x_train.reshape(-1,28,28,1)
    x_test  = x_test.reshape(-1,28,28,1)
    
    # Split training data into training and validation sets
    x_train, x_val, y_train, y_val = train_test_split(
        x_train, y_train, test_size=0.2, random_state=42)
    
    # Function to preprocess images: upscale and convert to RGB
    def preprocess_images(images):
        # Resize images to 224x224
        images_resized = tf.image.resize(images, [224,224])
        # Convert grayscale to RGB by duplicating channels
        images_resized = tf.image.grayscale_to_rgb(images_resized) 
        return images_resized
    
    # Preprocess the images
    x_train = preprocess_images(x_train)
    x_val   = preprocess_images(x_val)
    x_test  = preprocess_images(x_test)
    
    # Data augmentation
    datagen = ImageDataGenerator(
        rotation_range=15,
        zoom_range=0.10,
        width_shift_range=0.1,
        height_shift_range=0.1
    )
    datagen.fit(x_train)
    
    # Load the EfficientNetB0 model with pretrained weights
    base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224,224,3))
    
    # Freeze the base model
    base_model.trainable = False
    
    # Add new top layers for our task
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.5)(x)
    # Output layer for 10 classes
    predictions = Dense(10, activation='softmax')(x)
    
    # Create the final model
    model = Model(inputs=base_model.input, outputs=predictions)
    
    # Compile the model
    model.compile(
        optimizer=Adam(learning_rate=1e-3),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy']
    )
    
    # Callbacks for learning rate reduction and early stopping
    reduce_lr = ReduceLROnPlateau(
        monitor='val_accuracy',
        factor=0.5,
        patience=2,
        verbose=1
    )
    early_stop = EarlyStopping(
        monitor='val_accuracy',
        patience=5,
        verbose=1,
        restore_best_weights=True
    )
    
    # Train the model with frozen base layers
    history = model.fit(
        datagen.flow(x_train, y_train, batch_size=32),
        steps_per_epoch=len(x_train)//32,
        epochs=5,
        validation_data=(x_val, y_val),
        callbacks=[reduce_lr, early_stop]
    )
    
    # Unfreeze the base model for fine-tuning
    base_model.trainable = True
    
    # Recompile the model with a lower learning rate
    model.compile(
        optimizer=Adam(learning_rate=1e-5),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy']
    )
    
    # Continue training (fine-tuning)
    history_finetune = model.fit(
        datagen.flow(x_train, y_train, batch_size=32),
        steps_per_epoch=len(x_train)//32,
        epochs=15,
        validation_data=(x_val, y_val),
        callbacks=[reduce_lr, early_stop]
    )
    
    # Evaluate the model on the validation set
    val_loss, val_accuracy = model.evaluate(x_val, y_val, verbose=0)
    print(f"Validation accuracy: {val_accuracy:.4f}")
    
    # Evaluate the model on the test set
    test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
    print(f"Test accuracy: {test_accuracy:.4f}")
    
    # Save the model
    model.save('fashion_mnist_efficientnet_model.h5')
    

    Explanation:

    • Data Loading and Preprocessing:

      • The Fashion MNIST dataset is loaded and normalized.
      • Images are reshaped to include the channel dimension.
      • The training data is split into training and validation sets.
      • Images are resized to 224x224 pixels and converted to RGB to match the input size expected by EfficientNetB0.
    • Data Augmentation:

      • An ImageDataGenerator is used to perform real-time data augmentation, which helps improve the robustness and generalization of the model.
    • Model Definition:

      • An EfficientNetB0 model pre-trained on ImageNet is loaded without the top classifier layers.
      • The base model is initially frozen to train only the new top layers added for our specific task.
      • New layers are added on top of the base model:
        • A global average pooling layer.
        • Dropout layers for regularization.
        • A dense layer with ReLU activation.
        • An output layer with softmax activation for classification into 10 classes.
    • Compilation and Training:

      • The model is compiled with the Adam optimizer and a relatively higher learning rate for the initial training.
      • ReduceLROnPlateau and EarlyStopping callbacks are used to adjust the learning rate and prevent overfitting.
      • The model is trained for a few epochs with the base model frozen.
    • Fine-tuning:

      • The base model is unfrozen to allow fine-tuning of the pre-trained weights.
      • The model is recompiled with a lower learning rate to prevent large updates to the weights.
      • Training is continued, allowing the entire model to be trained end-to-end.
    • Evaluation:

      • After training, the model's performance is evaluated on both the validation and test sets.
      • The validation accuracy is expected to exceed 97%.
    • Model Saving:

      • The trained model is saved for future use.

    Note:

    • Achieving over 97% accuracy on the Fashion MNIST dataset requires a powerful model and proper training techniques. Transfer learning with a pre-trained model like EfficientNetB0 significantly boosts performance.
    • Ensure that you have the necessary libraries installed (tensorflow, sklearn, etc.) and a compatible GPU if available to speed up training.

    Dependencies:

    pip install tensorflow
    pip install scikit-learn
    

    Run the Script: Save the script to a file, for example, train_fashion_mnist.py, and run:

    python train_fashion_mnist.py
    

    This script should train a model that achieves over 97% accuracy on the validation set of the Fashion MNIST dataset.