Skip to content
Project: Building an E-Commerce Clothing Classifier Model with Keras
  • AI Chat
  • Code
  • Report
  • Fashion Forward is a new AI-based e-commerce clothing retailer. They want to use image classification to automatically categorize new product listings, making it easier for customers to find what they're looking for. It will also assist in inventory management by quickly sorting items.

    As a data scientist tasked with implementing a garment classifier, your primary objective is to develop a machine learning model capable of accurately categorizing images of clothing items into distinct garment types such as shirts, trousers, shoes, etc.

    # Run the cells below first
    from tensorflow.keras import datasets, layers, models, Sequential
    from keras.layers import Dense, Conv2D, Flatten
    from tensorflow.keras.utils import to_categorical
    (train_images, train_labels), (test_images, test_labels) = datasets.fashion_mnist.load_data()
    import matplotlib.pyplot as plt
    plt.figure(figsize=(5,5))
    for i in range(25):
        plt.subplot(5,5,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(train_images[i]/255.0, cmap='gray')
    plt.show()
    # Start coding here
    # Use as many cells as you need
    
    num_classes = len(set(train_labels))
    img_size = train_images[0].shape[0]
    
    train_labels_1h = to_categorical(train_labels)
    test_labels_1h = to_categorical(test_labels)
    
    model = Sequential()
    
    model.add(Conv2D(32, kernel_size=3, input_shape=(img_size, img_size, 1), activation = 'relu'))
    
    model.add(Conv2D(16, kernel_size=3, activation='relu'))
    
    model.add(Flatten())
    
    model.add(Dense(num_classes, activation='softmax'))
    
    model.compile(optimizer='adam',
                 loss='categorical_crossentropy',
                 metrics=['accuracy'])
    
    model.summary()
    
    
    model.fit(train_images, train_labels_1h, 
              validation_split=0.2, 
              epochs=1, batch_size=100)
    
    scores = model.evaluate(test_images, test_labels_1h, batch_size=100)
    test_accuracy = scores[-1]
    print(f'Test accuracy: {test_accuracy:.2f}')