Open abhisheks008 opened 6 months ago
Full name : Jahnvi sahni GitHub Profile Link : https://github.com/jahnvisahni31 Email ID : jahnvisahni98@gmail.com Approach for this Project : model implementation using dl What is your participant role? GSSOC'24
I want to contribute to this
Need some clarification on the approach @jahnvisahni31
we can use cnn, vgg16, resnet50, inceptionv3 and then do the comparative analysis that which is better version
Assigned to you @jahnvisahni31
thankyou will get this done at earliest
It's okay, no need to hurry. It's a long event, take your time and showcase the best result.
In this we have to make the test and train repo because as in the dataset they have given 9 folders which have mixed images of fire and non fire so by using imagenet we can automatically categorize the dataset but i dont know how to approach other models can you guide me a little? @abhisheks008
Certainly! Here’s a step-by-step guide on how to approach the task of organizing your dataset into train and test sets and categorizing the images using models beyond ImageNet:
Organize the Dataset: You mentioned having 9 folders with mixed images of fire and non-fire. First, you need to create two main directories: train
and test
.
Split the Data:
Here’s a simple way to do this using Python:
import os
import shutil
from sklearn.model_selection import train_test_split
# Paths
dataset_path = 'path_to_your_dataset'
train_path = 'path_to_train'
test_path = 'path_to_test'
# Create train and test directories
os.makedirs(train_path, exist_ok=True)
os.makedirs(test_path, exist_ok=True)
# Gather all image paths
images = []
for folder_name in os.listdir(dataset_path):
folder_path = os.path.join(dataset_path, folder_name)
for img_name in os.listdir(folder_path):
images.append(os.path.join(folder_path, img_name))
# Split the data
train_images, test_images = train_test_split(images, test_size=0.2, random_state=42)
# Move images to respective directories
for img_path in train_images:
shutil.copy(img_path, train_path)
for img_path in test_images:
shutil.copy(img_path, test_path)
To categorize the images, you can use a pre-trained model or train a new model. Here’s an outline of both approaches:
Load a Pre-trained Model:
import torch
from torchvision import models, transforms
from PIL import Image
# Load pre-trained model
model = models.resnet50(pretrained=True)
model.eval()
# Define image transformations
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Function to classify an image
def classify_image(image_path):
input_image = Image.open(image_path)
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0)
with torch.no_grad():
output = model(input_batch)
# Output predictions
_, predicted = torch.max(output, 1)
return predicted.item()
Categorize the Images:
fire_images = []
non_fire_images = []
for img_path in os.listdir(test_path):
category = classify_image(os.path.join(test_path, img_path))
if category == fire_label: # Assuming fire_label is defined
fire_images.append(img_path)
else:
non_fire_images.append(img_path)
Create a Dataset Class:
from torch.utils.data import Dataset
from torchvision.io import read_image
class FireDataset(Dataset):
def __init__(self, img_dir, transform=None):
self.img_dir = img_dir
self.transform = transform
self.img_labels = [img.split('_')[0] for img in os.listdir(img_dir)]
self.img_paths = [os.path.join(img_dir, img) for img in os.listdir(img_dir)]
def __len__(self):
return len(self.img_labels)
def __getitem__(self, idx):
image = read_image(self.img_paths[idx])
label = 1 if 'fire' in self.img_labels[idx] else 0
if self.transform:
image = self.transform(image)
return image, label
Training the Model:
import torch.nn as nn
import torch.optim as optim
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 3, 1)
self.conv2 = nn.Conv2d(16, 32, 3, 1)
self.fc1 = nn.Linear(32 * 6 * 6, 128)
self.fc2 = nn.Linear(128, 2)
def forward(self, x):
x = nn.functional.relu(self.conv1(x))
x = nn.functional.max_pool2d(x, 2, 2)
x = nn.functional.relu(self.conv2(x))
x = nn.functional.max_pool2d(x, 2, 2)
x = x.view(-1, 32 * 6 * 6)
x = nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
model = SimpleCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
Training Loop:
from torch.utils.data import DataLoader
# Load the dataset
transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
])
train_dataset = FireDataset(train_path, transform=transform)
test_dataset = FireDataset(test_path, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
# Training function
def train_model(model, train_loader, criterion, optimizer, num_epochs=10):
for epoch in range(num_epochs):
model.train()
running_loss = 0.0
for inputs, labels in train_loader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
epoch_loss = running_loss / len(train_loader.dataset)
print(f'Epoch {epoch+1}/{num_epochs}, Loss: {epoch_loss:.4f}')
# Train the model
train_model(model, train_loader, criterion, optimizer)
Evaluate on the Test Set:
from sklearn.metrics import accuracy_score
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
all_preds.extend(preds.numpy())
all_labels.extend(labels.numpy())
accuracy = accuracy_score(all_labels, all_preds)
print(f'Accuracy: {accuracy:.4f}')
By following these steps, you should be able to organize your dataset, categorize the images using a pre-trained model, or train a custom model for classifying fire and non-fire images.
i have done please check the pull request @abhisheks008
Deep Learning Simplified Repository (Proposing new issue)
:red_circle: Project Title : Tuav Fire Detection using DL :red_circle: Aim : The aim is to detect fire from the given images in the dataset using image processing and deep learning methods. :red_circle: Dataset : https://www.kaggle.com/datasets/enesyurt/tuav-fire-detection :red_circle: Approach : Try to use 3-4 algorithms to implement the models and compare all the algorithms to find out the best fitted algorithm for the model by checking the accuracy scores. Also do not forget to do a exploratory data analysis before creating any model.
📍 Follow the Guidelines to Contribute in the Project :
requirements.txt
- This file will contain the required packages/libraries to run the project in other machines.Model
folder, theREADME.md
file must be filled up properly, with proper visualizations and conclusions.:red_circle::yellow_circle: Points to Note :
:white_check_mark: To be Mentioned while taking the issue :
Happy Contributing 🚀
All the best. Enjoy your open source journey ahead. 😎