DEV Community

Cover image for AI Image Classification with MobileNet(Tensorflow)
Aksh Thakkar
Aksh Thakkar

Posted on

AI Image Classification with MobileNet(Tensorflow)

So recently, I've been wondering how apps like Google Lens or Instagram can just look at a photo and identify it as a "dog" or "sushi".This sparked my interest, so I decided to explore behind the scenes!

It's quite simple, actually. I used a pre-trained AI model called MobileNetV2—thanks to TensorFlow—and added a Streamlit user interface on top of it. You can upload any image, and it will identify what it contains and display top 3 matches for that image. It's not always accurate, but it works surprisingly well.

What It Does (in Simple Terms):

  1. You upload a random image.
  2. The AI analyzes the image and makes some guesses about what’s in it.
  3. It shows you the top three guesses along with its confidence levels for each guess.

It's quite straightforward! The best part? I didn’t train the model myself, I utilized a pre-trained model that has already learned from millions of photos, making it highly knowledgeable.

The AI follows a specific format to understand any image you give it. That’s what this function does:

def preprocess_image(image):
    img = np.array(image)
    img = cv2.resize(img, (224, 224))         # Resize to MobileNet's expected size
    img = preprocess_input(img)               # Normalize pixel values
    img = np.expand_dims(img, axis=0)         # Add batch dimension
    return img
Enter fullscreen mode Exit fullscreen mode

MobileNetV2 uses a 224x224 image size. Consider,

  • Your 224x224 image = 224 rows and 224 columns = 50,176 tiny squares
  • Each square has 3 color values = R, G, B
  • The model looks at all these values and tries to figure out patterns — like edges, shapes, colors, textures.

This part is where the AI does its actual guessing:

def classify_image(model, image):
    processed_img = preprocess_image(image)
    preds = model.predict(processed_img)
    return decode_predictions(preds, top=3)[0]  # Returns top 3 predictions with labels and %

Enter fullscreen mode Exit fullscreen mode
  • The image goes through the AI model with .predict()
  • The model gives back a bunch of numbers (raw predictions)
  • decode_predictions turns those numbers into real labels like “sneaker” or “toaster”
  • Then, it grabs the top 3 guesses — the stuff the model is most confident about

You're welcome to explore the project at AI Image Classifier. Simply upload any image to see how the AI interprets it. I hope you find this experience both interesting and useful! Additionally, here’s a link to the project code on Github.

Top comments (0)