Hone logo
Hone
Problems

Image Classification with a Convolutional Neural Network (CNN)

This challenge asks you to design and implement a basic Convolutional Neural Network (CNN) architecture in Python using a deep learning framework like TensorFlow or PyTorch. CNNs are powerful tools for image recognition and classification, and this exercise will help you understand their fundamental building blocks and how to assemble them. The goal is to create a model capable of classifying images from a simplified dataset.

Problem Description

You are tasked with building a CNN model that can classify images from a dataset containing two classes: "cats" and "dogs". The dataset will be represented as NumPy arrays, where each array represents a grayscale image. Your CNN should consist of at least two convolutional layers, a pooling layer, and a fully connected layer that outputs a probability for each class.

What needs to be achieved:

  • Create a CNN model with the specified architecture.
  • The model should take grayscale images as input.
  • The model should output a probability score for each class ("cats" and "dogs").

Key Requirements:

  • Convolutional Layers: At least two convolutional layers with ReLU activation. Experiment with different filter sizes and numbers of filters.
  • Pooling Layer: A max pooling layer to reduce the spatial dimensions of the feature maps.
  • Fully Connected Layer: A fully connected layer with a softmax activation function to produce the final classification probabilities.
  • Framework: Use either TensorFlow or PyTorch. Specify which framework you are using in your code.
  • Input Shape: The input images should be 28x28 pixels.

Expected Behavior:

The model should accept a 28x28 grayscale image as input and return a NumPy array of shape (2,) containing the predicted probabilities for "cats" and "dogs", respectively. Higher probability indicates a stronger prediction.

Edge Cases to Consider:

  • Invalid Input Shape: While not strictly required for this challenge, consider how your model would handle input images that are not 28x28. (You can assume all inputs will be 28x28 for this challenge).
  • Zero Filters: Ensure you don't accidentally set the number of filters to zero in any layer, as this would prevent learning.

Examples

Example 1:

Input: A 28x28 grayscale image of a cat.
Output: [0.1, 0.9]
Explanation: The model predicts a 10% probability of the image being a dog and a 90% probability of it being a cat.

Example 2:

Input: A 28x28 grayscale image of a dog.
Output: [0.8, 0.2]
Explanation: The model predicts an 80% probability of the image being a dog and a 20% probability of it being a cat.

Example 3: (Edge Case - Low Contrast Image)

Input: A 28x28 grayscale image of a cat with very low contrast.
Output: [0.4, 0.6]
Explanation: The model might struggle with low contrast images and produce a less confident prediction, but should still classify it as a cat with a higher probability than a dog.

Constraints

  • Image Size: Input images must be 28x28 pixels.
  • Framework: Must use either TensorFlow or PyTorch.
  • Layers: The CNN must contain at least two convolutional layers, one pooling layer, and one fully connected layer.
  • Training: No training is required for this challenge. You only need to define the architecture. You can use dummy data to test the output shape.
  • Output: The output must be a NumPy array of shape (2,) representing the probabilities for "cats" and "dogs".

Notes

  • Consider using a sequential model for simplicity.
  • Experiment with different filter sizes (e.g., 3x3, 5x5) and numbers of filters in the convolutional layers.
  • The choice of activation functions (ReLU, sigmoid, etc.) can impact performance. ReLU is generally a good starting point.
  • Focus on creating a valid CNN architecture; training and optimization are not part of this challenge.
  • Remember to clearly document which deep learning framework you are using.
Loading editor...
python