AI Image Recognition: Build Image Classification Models with Python

Hello everyone! Today I want to introduce an amazing AI tool—TensorFlow. How easy is it to build an image classification model with TensorFlow? Let me give you a few very practical examples:

Scenario 1: Quickly Build a Simple Image Classification Model

Suppose you have some images and you want the model to recognize which category they belong to.

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
from tensorflow.keras.datasets import cifar10  # Use CIFAR-10 dataset

# Load dataset
(x_train, y_train),(x_test, y_test)= cifar10.load_data()

# Data preprocessing
x_train, x_test = x_train /255.0, x_test /255.0# Normalization

# Build model
model = Sequential([
    Conv2D(32,(3,3), activation='relu', input_shape=(32,32,3)),# Convolutional layer
    MaxPooling2D((2,2)),# Pooling layer
    Flatten(),# Flatten layer
    Dense(64, activation='relu'),# Fully connected layer
    Dense(10, activation='softmax')# Output layer
])

# Compile model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train model
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# Evaluate model
loss, accuracy = model.evaluate(x_test, y_test)
print(f"Test set accuracy: {accuracy:.2f}")

This code means:

1. Load the CIFAR-10 dataset (which contains images of 10 categories). 2. Normalize the data. 3. Build a simple Convolutional Neural Network (CNN) model. 4. Compile the model and train it. 5. Evaluate the model’s accuracy.

Isn’t it super simple? With just a few lines of code, you can build an image classification model!

Scenario 2: Use Pre-trained Models to Quickly Achieve Results

If you don’t want to train a model from scratch, TensorFlow also provides many pre-trained models that can be loaded and used directly.

import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

# Load the pre-trained ResNet50 model
model = ResNet50(weights='imagenet')

# Load an image and preprocess it
img_path ='your_image.jpg'# Replace with your image path
img = image.load_img(img_path, target_size=(224,224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# Make predictions using the model
predictions = model.predict(x)
decoded_predictions = decode_predictions(predictions, top=3)[0]
for i,(imagenet_id, label, score) in enumerate(decoded_predictions):
    print(f"{i + 1}: {label} ({score:.2f})")

This code means:

1. Load the pre-trained ResNet50 model. 2. Load an image and preprocess it. 3. Use the model to make predictions and output the top 3 most likely categories.

Isn’t it super fast? Just load the pre-trained model, and you can directly classify images!

Scenario 3: Fine-tune Pre-trained Models to Fit Your Own Data

If you have your own dataset, you can also fine-tune the pre-trained model to better adapt to your task.

import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Load the pre-trained ResNet50 model, removing the last layer
base_model = ResNet50(weights='imagenet', include_top=False)

# Add custom fully connected layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)# Assuming your data has 10 categories

# Build the new model
model = Model(inputs=base_model.input, outputs=predictions)

# Freeze the weights of the pre-trained model
for layer in base_model.layers:
    layer.trainable = False

# Compile model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Data preprocessing
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory('your_data_folder', target_size=(224,224), batch_size=32)

# Train model
model.fit(train_generator, epochs=10)

# Evaluate model
# ...

This code means:

1. Load the pre-trained ResNet50 model, removing the last layer. 2. Add custom fully connected layers. 3. Freeze the weights of the pre-trained model and only train the newly added layers. 4. Train using your own dataset.

Leave a Comment