Skip to content

Fresh Meat Classifier using fastai and PyTorch

This notebook demonstrates the process of building a Fresh Meat classifier using the fastai library, which is built on top of PyTorch.

Fastai is a high-level library that simplifies the process of training deep learning models. It provides easy-to-use APIs and pre-built architectures, making it accessible for both beginners and experienced practitioners.

In this notebook, we will explore the steps involved in training a Fresh Meat classifier using fastai and PyTorch. We will cover data preprocessing, model creation, training, and evaluation.

!pip install fastai
Hidden output

Extracting from 'Freshness Dataset.zip'

import zipfile
import os

def extract_zip_file(zip_file_path, destination_folder, extracted_folder_name):
    # Extract the zip file
    with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
        zip_ref.extractall(destination_folder)

    # Specify the paths of the train and valid folders
    extracted_folder_path = os.path.join(destination_folder, extracted_folder_name)
    train_folder_path = os.path.join(extracted_folder_path, 'train')
    valid_folder_path = os.path.join(extracted_folder_path, 'valid')

    # Check if the train and valid folders exist
    if os.path.exists(train_folder_path) and os.path.exists(valid_folder_path):
        return "Extraction successful."
    else:
        return "Extraction failed."

# Specify the path to the zip file
zip_file_path = 'Freshness Dataset.zip'

# Specify the destination folder to extract the files
destination_folder = '.'

# Specify the name of the extracted folder
extracted_folder_name = 'Freshness Dataset'

# Call the function
result = extract_zip_file(zip_file_path, destination_folder, extracted_folder_name)
result
Hidden output
!mv Meat\ Freshness.v1-new-dataset.multiclass FreshnessDataSet

Checking the lengths of train and valid folders

import os

# Specify the paths of the train and valid folders
train_folder_path = os.path.join('FreshnessDataSet', 'train')
valid_folder_path = os.path.join('FreshnessDataSet', 'valid')

# Get the number of files in the train folder
train_files = os.listdir(train_folder_path)
num_train_files = len(train_files)

# Get the number of files in the valid folder
valid_files = os.listdir(valid_folder_path)
num_valid_files = len(valid_files)

num_train_files, num_valid_files
!ls /work/files/workspace/FreshnessDataSet/
import os

# List the contents of the train directory
print("Train Directory Contents:")
print(os.listdir('FreshnessDataSet/train'))

# List the contents of the valid directory
print("\nValid Directory Contents:")
print(os.listdir('FreshnessDataSet/valid'))
Hidden output

Need to create subfolders Fresh/Half-Fresh/Spoiled in Train and Valid folders

import os
import shutil

base_dir = 'FreshnessDataSet'
classes = ['FRESH', 'HALF-FRESH', 'SPOILED']

# Function to create subdirectories and move files
def organize_dataset(folder_name):
    folder_path = os.path.join(base_dir, folder_name)
    if not os.path.exists(folder_path):
        print(f"Folder {folder_path} not found.")
        return

    # Create class subdirectories
    for cls in classes:
        cls_dir = os.path.join(folder_path, cls)
        os.makedirs(cls_dir, exist_ok=True)

    # Move files to their respective class subdirectories
    for file in os.listdir(folder_path):
        if file.endswith('.jpg'):
            for cls in classes:
                # Check if the class name is at the beginning of the filename
                if file.startswith(cls):
                    shutil.move(os.path.join(folder_path, file), os.path.join(folder_path, cls, file))
                    break

# Organize train and valid directories
organize_dataset('train')
organize_dataset('valid')

print("Dataset organization complete.")

Loading the Dataset and Fine-tuning a CNN Model

To load the dataset into dls and fine-tune a CNN model resnet18 for five iterations, you can use the following code:

from fastai.vision.all import *
data_path = 'FreshnessDataSet'
# Load the dataset
dls = ImageDataLoaders.from_folder(
    data_path, 
    train='train', 
    valid='valid',
    item_tfms=Resize(196),  # Ensure images are resized to 416x416
    batch_tfms=aug_transforms(max_zoom=1.2, max_lighting=0.2),  # Optional
    bs=16,  # Reduced batch size
)

# Create a CNN model
learn = cnn_learner(dls, resnet18, metrics=accuracy)

# Train the model
learn.fine_tune(10)  # Number of epochs, adjust as needed
# Generate confusion matrix from learned model
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
# Evaluate the model's performance
learn.validate()