Friday, July 28, 2023

Image classification CNN using PyTorch for the given e-commerce product categorization task

 Simplified example of how you can implement an image classification CNN using PyTorch for the given e-commerce product categorization task:


Step 1: Import the required libraries.

```python

import torch

import torch.nn as nn

import torch.optim as optim

import torchvision.transforms as transforms

from torchvision.datasets import ImageFolder

from torch.utils.data import DataLoader

```


Step 2: Preprocess the data and create data loaders.

```python

# Define the data transformations

transform = transforms.Compose([

    transforms.Resize((64, 64)),   # Resize the images to a fixed size

    transforms.ToTensor(),          # Convert images to tensors

    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))  # Normalize image data

])


# Load the training dataset

train_dataset = ImageFolder('path_to_train_data_folder', transform=transform)


# Create data loaders

batch_size = 64

train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)

```


Step 3: Define the CNN architecture.

```python

class CNNClassifier(nn.Module):

    def __init__(self):

        super(CNNClassifier, self).__init__()

        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)

        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)

        self.fc1 = nn.Linear(64 * 16 * 16, 128)

        self.fc2 = nn.Linear(128, 3)  # Assuming 3 categories: "clothing," "electronics," "home appliances"


    def forward(self, x):

        x = nn.functional.relu(self.conv1(x))

        x = nn.functional.max_pool2d(x, 2)

        x = nn.functional.relu(self.conv2(x))

        x = nn.functional.max_pool2d(x, 2)

        x = x.view(-1, 64 * 16 * 16)  # Flatten the output

        x = nn.functional.relu(self.fc1(x))

        x = self.fc2(x)

        return x

```


Step 4: Train the CNN.

```python

# Instantiate the model

model = CNNClassifier()


# Define the loss function and optimizer

criterion = nn.CrossEntropyLoss()

optimizer = optim.Adam(model.parameters(), lr=0.001)


# Training loop

num_epochs = 10

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model.to(device)


for epoch in range(num_epochs):

    for images, labels in train_loader:

        images, labels = images.to(device), labels.to(device)


        optimizer.zero_grad()

        outputs = model(images)

        loss = criterion(outputs, labels)

        loss.backward()

        optimizer.step()


    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')


print("Training completed.")

```


Step 5: Deploy the model for inference (Assuming you have a separate test dataset).

```python

# Load the test dataset

test_dataset = ImageFolder('path_to_test_data_folder', transform=transform)

test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)


# Evaluate the model on the test data

model.eval()

correct = 0

total = 0


with torch.no_grad():

    for images, labels in test_loader:

        images, labels = images.to(device), labels.to(device)

        outputs = model(images)

        _, predicted = torch.max(outputs.data, 1)

        total += labels.size(0)

        correct += (predicted == labels).sum().item()


accuracy = 100 * correct / total

print(f'Test Accuracy: {accuracy:.2f}%')

```


This is a basic example to demonstrate the process. In a real-world scenario, you would further fine-tune the model, perform hyperparameter tuning, and optimize the deployment process for production use. Additionally, you might need to implement data augmentation techniques and deal with class imbalances, depending on the characteristics of your dataset.

No comments:

Post a Comment

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...