Skip to content

Commit

Permalink
reformat files
Browse files Browse the repository at this point in the history
  • Loading branch information
YaelBenShalom committed Oct 26, 2021
1 parent b859b35 commit 01c8dc1
Show file tree
Hide file tree
Showing 18 changed files with 447 additions and 337 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
# Objects Recognition and Classification

## ME499 - Independent Project, Winter 2021

Yael Ben Shalom, Northwestern University.

## Table of Contents

Table of Contents
-----------------
* [Project Overview](#project-overview)
* [Traffic sign detection and classification](#traffic-sign-detection-and-classification)
* [Trash detection, classification, and segmentation](#trash-detection-classification-and-segmentation)
- [Project Overview](#project-overview)
- [Traffic sign detection and classification](#traffic-sign-detection-and-classification)
- [Trash detection, classification, and segmentation](#trash-detection-classification-and-segmentation)

## Project Overview
This project contains 2 modules:

This project contains 2 modules:

### [**Traffic sign detection and classification**](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection)

In this module I built and trained a neural network to detect and classify different traffic signs using PyTorch, OpenCV and YoloV3.<br>
This module contained 2 parts:
* [traffic sign classification](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_classification) - Were I build and trained a neural network to classify different traffic-signs images.
* [traffic sign detection](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_recognition) - Were I trained neural network to detect different traffic-signs in an image/video.
_ [traffic sign classification](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_classification) - Were I build and trained a neural network to classify different traffic-signs images.
_ [traffic sign detection](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_recognition) - Were I trained neural network to detect different traffic-signs in an image/video.

An example of traffic detection program output:<br>
![Traffic-Sign Recognition](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_recognition/images/traffic-sign.gif)


### [**Trash detection, classification, and segmentation**](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/trash_detection)

In this module I built and trained a neural network to detect and classify different traffic signs using PyTorch, OpenCV and YoloV5.<br>
This module contained 2 parts:
* [trash classification](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_classification) - Were I build and trained a neural network to classify different recyclable objects' images.
* [trash detection](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_recognition) - Were I trained neural network to detect different recyclable objects in an image/video.
_ [trash classification](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_classification) - Were I build and trained a neural network to classify different recyclable objects' images.
_ [trash detection](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_recognition) - Were I trained neural network to detect different recyclable objects in an image/video.

An example of trash detection program output:<br>
![Trash Recognition](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/trash_detection/trash_recognition/images/real-time%20detection2.gif)
Expand Down
68 changes: 35 additions & 33 deletions traffic_signs_detection/traffic_signs_classification/README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
# Traffic-Sign Classification

## ME499 - Independent Project, Winter 2021

Yael Ben Shalom, Northwestern University.<br>
This module is a part of a [Objects Recognition and Classification](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification) project.

## Table of Contents

Table of Contents
-----------------
* [Module Overview](#module-overview)
* [User Guide](#user-guide)
* [Program Installation](#program-installation)
* [Quickstart Guide](#quickstart-guide)
* [Dataset](#dataset)

- [Module Overview](#module-overview)
- [User Guide](#user-guide)
- [Program Installation](#program-installation)
- [Quickstart Guide](#quickstart-guide)
- [Dataset](#dataset)

## Module Overview

In this module I built and trained a neural network to classify different traffic signs using PyTorch.<br>
I based my program on the German Traffic Sign Recognition Benchmark ([GTSRB](https://benchmark.ini.rub.de/gtsrb_news.html)) dataset.


## User Guide

### Program Installation

1. Clone the repository, using the following commands:
```
git clone https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_classification
```

2. Download the dataset and extract it into `./data` directory. The dataset can be found on the [INI Benchmark website](https://benchmark.ini.rub.de/?section=gtsrb&subsection=news), or downloaded directly through [here](https://s3-us-west-1.amazonaws.com/udacity-selfdrivingcar/traffic-signs-data.zip).
```
git clone https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/tree/master/traffic_signs_detection/traffic_signs_classification
```

2. Download the dataset and extract it into `./data` directory. The dataset can be found on the [INI Benchmark website](https://benchmark.ini.rub.de/?section=gtsrb&subsection=news), or downloaded directly through [here](https://s3-us-west-1.amazonaws.com/udacity-selfdrivingcar/traffic-signs-data.zip).

### Quickstart Guide

Run the classification program:

1. To train and test the program on the dataset, run the following command from the root directory:
```
python code/classification.py
```

```
python code/classification.py
```

2. To train the program on the dataset and test it on a specific image, copy the image to the root directory and run the following command from the root directory:
```
python code/classification.py --image <image-name>
```
Where `<image-name>` is the name of the image (including image type).<br>
The trained model will be saved in the root directory as `/model`.

3. To to use an existing model and test it on a specific image, copy the image to the root directory and run the following command from the root directory:
```
python code/classification.py --image <image-name> --model <model-name>
```
Where `<model-name>` is the name of the trained model.
```
python code/classification.py --image <image-name>
```

Where `<image-name>` is the name of the image (including image type).<br>
The trained model will be saved in the root directory as `/model`.

3. To to use an existing model and test it on a specific image, copy the image to the root directory and run the following command from the root directory:
```
python code/classification.py --image <image-name> --model <model-name>
```
Where `<model-name>` is the name of the trained model.

<br>The program output when running it on the example image:

The loss plot:<br>
![Loss Graph](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/Losses%20(100%20Epochs).png)
![Loss Graph](<https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/Losses%20(100%20Epochs).png>)

The accuracy plot:<br>
![Accuracy Graph](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/Accuracy%20(100%20Epochs).png)
![Accuracy Graph](<https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/Accuracy%20(100%20Epochs).png>)

The output image (with the correct prediction):<br>
![Accuracy Graph](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/Image_Classification.png)
![Accuracy Graph](https://github.com/YaelBenShalom/Objects-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/Image_Classification.png)

## Dataset

The German Traffic Sign Recognition Benchmark ([GTSRB](https://benchmark.ini.rub.de/gtsrb_news.html)) is a large multi-category classification benchmark. It was used in a competition at the International Joint Conference on Neural Networks (IJCNN) 2011.

The benchmark has the following properties:

1. Single-image, multi-class classification problem.
2. 43 classes.
3. More than 50,000 images in total (~35,000 training images, ~4000 validation images, and ~13,000 testing images).
4. Large, lifelike database.

The dataset can be found on the [INI Benchmark website](https://benchmark.ini.rub.de/?section=gtsrb&subsection=news), or downloaded directly through [here](https://s3-us-west-1.amazonaws.com/udacity-selfdrivingcar/traffic-signs-data.zip).

![The German Traffic Sign Recognition Benchmark](https://github.com/YaelBenShalom/Traffic-Sign-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/dataset.png)
![The German Traffic Sign Recognition Benchmark](https://github.com/YaelBenShalom/Traffic-Sign-Recognition-and-Classification/blob/master/traffic_signs_detection/traffic_signs_classification/images/dataset.png)
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ def dataset_properties(trainset_name, validset_name, testset_name, class_names,
Output: None
"""
train_features, train_labels = load_dataset(trainset_name, base_folder=data_dir)
valid_features, valid_labels = load_dataset(validset_name, base_folder=data_dir)
test_features, test_labels = load_dataset(testset_name, base_folder=data_dir)
train_features, train_labels = load_dataset(
trainset_name, base_folder=data_dir)
valid_features, valid_labels = load_dataset(
validset_name, base_folder=data_dir)
test_features, test_labels = load_dataset(
testset_name, base_folder=data_dir)

# print(f"train_features shape: {train_features.shape}")
# print(f"train_labels shape: {train_labels.shape}")
Expand Down Expand Up @@ -85,7 +88,8 @@ def dataset_properties(trainset_name, validset_name, testset_name, class_names,
for i in range(40):
feature_index = random.randint(0, train_labels.shape[0])
plt.subplot(6, 8, i + 1)
plt.subplots_adjust(left=0.1, bottom=0.03, right=0.9, top=0.92, wspace=0.2, hspace=0.2)
plt.subplots_adjust(left=0.1, bottom=0.03, right=0.9,
top=0.92, wspace=0.2, hspace=0.2)
plt.axis('off')
plt.imshow(train_features[feature_index])
plt.suptitle('Random Training Images', fontsize=20)
Expand All @@ -97,7 +101,8 @@ def dataset_properties(trainset_name, validset_name, testset_name, class_names,
for i in range(classes_num):
feature_index = random.choice(np.where(train_labels == i)[0])
plt.subplot(6, 8, i + 1)
plt.subplots_adjust(left=0.1, bottom=0.03, right=0.9, top=0.92, wspace=0.2, hspace=0.2)
plt.subplots_adjust(left=0.1, bottom=0.03, right=0.9,
top=0.92, wspace=0.2, hspace=0.2)
plt.axis('off')
plt.title(class_names[i], fontsize=10)
plt.imshow(train_features[feature_index])
Expand All @@ -124,7 +129,7 @@ def class_names_fun(data_dir):
for row in class_names_rows:
label, label_name = row.strip().split(",")
class_names[int(label)] = label_name

return class_names


Expand All @@ -136,14 +141,17 @@ def plot_training_results(train_loss_list, valid_loss_list, valid_accuracy_list,
train_loss_list: list of loss value on the entire training dataset.
valid_loss_list: list of loss value on the entire validation dataset.
valid_accuracy_list: list of accuracy on the entire validation dataset.
Output: None
"""
# Plotting training and validation loss vs. epoch number
plt.figure()
plt.plot(range(len(train_loss_list)), train_loss_list, label='Training Loss')
plt.plot(range(len(valid_loss_list)), valid_loss_list, label='Validation Loss')
plt.title(f'Training and Validation Loss Vs. Epoch Number ({epoch_num} Epochs)')
plt.plot(range(len(train_loss_list)),
train_loss_list, label='Training Loss')
plt.plot(range(len(valid_loss_list)),
valid_loss_list, label='Validation Loss')
plt.title(
f'Training and Validation Loss Vs. Epoch Number ({epoch_num} Epochs)')
plt.xlabel('Epoch Number')
plt.ylabel('Loss')
plt.legend(loc="best")
Expand All @@ -152,7 +160,8 @@ def plot_training_results(train_loss_list, valid_loss_list, valid_accuracy_list,

# Plotting validation accuracy vs. epoch number
plt.figure()
plt.plot(range(len(valid_accuracy_list)), valid_accuracy_list, label='Validation Accuracy')
plt.plot(range(len(valid_accuracy_list)),
valid_accuracy_list, label='Validation Accuracy')
plt.title(f'Validation Accuracy Vs. Epoch Number ({epoch_num} Epochs)')
plt.xlabel('Epoch Number')
plt.ylabel('Accuracy')
Expand All @@ -172,7 +181,7 @@ def main(args):
"""

# Define dataset directory
data_dir = "data"
data_dir = "data"

# Define dataset files
trainset_name = "train.p"
Expand All @@ -183,7 +192,8 @@ def main(args):
class_names = class_names_fun(data_dir)

# Visualizing the dataset
dataset_properties(trainset_name, validset_name, testset_name, class_names, data_dir)
dataset_properties(trainset_name, validset_name,
testset_name, class_names, data_dir)

# Define the device parameters
torch.manual_seed(1)
Expand All @@ -201,7 +211,8 @@ def main(args):
stop_threshold = 1e-4

# Computing data transformation to normalize data
mean = (0.485, 0.456, 0.406) # from https://pytorch.org/docs/stable/torchvision/transforms.html
# from https://pytorch.org/docs/stable/torchvision/transforms.html
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225) # -"-
transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize((32, 32)),
Expand All @@ -219,15 +230,16 @@ def main(args):

# Train the network
model, train_loss_list, valid_loss_list, valid_accuracy_list = run_model(model, running_mode='train',
train_set=train_dataset,
valid_set=valid_dataset,
test_set=test_dataset,
batch_size=batch_size, epoch_num=epoch_num,
learning_rate=learning_rate,
stop_thr=stop_threshold,
criterion=criterion, device=device)
train_set=train_dataset,
valid_set=valid_dataset,
test_set=test_dataset,
batch_size=batch_size, epoch_num=epoch_num,
learning_rate=learning_rate,
stop_thr=stop_threshold,
criterion=criterion, device=device)
# Plot the results of training the network
plot_training_results(train_loss_list, valid_loss_list, valid_accuracy_list, epoch_num)
plot_training_results(train_loss_list, valid_loss_list,
valid_accuracy_list, epoch_num)

# Save the trained model
torch.save(model.state_dict(), model_path)
Expand All @@ -236,7 +248,7 @@ def main(args):
else:
# Defining the model
model_path = os.path.abspath(args["model"])

# Load the trained model
model.load_state_dict(torch.load(model_path, map_location=device))

Expand All @@ -263,7 +275,8 @@ def main(args):

# Predict the class of the tested image
prediction = int(predict(model, test_image_transform4d)[0])
print(f"Test prediction: {prediction} -> Class: {class_names[prediction]}")
print(
f"Test prediction: {prediction} -> Class: {class_names[prediction]}")

# Plot the image with the predicted class
plt.figure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def __init__(self):
# Replace last layer
num_ftrs = self.network.fc.in_features
self.network.fc = nn.Linear(num_ftrs, output_layer)

def forward(self, xb):
x = torch.sigmoid(self.network(xb))

return x
return x
Loading

0 comments on commit 01c8dc1

Please sign in to comment.