Skip to content

Commit 64bfa70

Browse files
committedDec 31, 2022
VGG Models for Image Classification
1 parent bece39b commit 64bfa70

26 files changed

+95
-36
lines changed
 

‎README.md

+41-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ai
22

3-
_Artificial Intelligence (AI) - Contains Code for Several YouTube Series._
3+
From-scratch impls of AI models, approaches, tricks, and more!
44

55
## Contents
66

@@ -49,6 +49,16 @@ _Artificial Intelligence (AI) - Contains Code for Several YouTube Series._
4949
- [:chart_with_upwards_trend: Plot][tanh_plot]
5050
- [:tv: YouTube Video: Discussing and Implementing Tanh and Its Derivative Using PyTorch][tanh_youtube]
5151

52+
### Deep Learning
53+
54+
- Deep Learning Project Setup (+ CNN for MNIST)
55+
- [:orange_book: The Reproducible MNIST][the_reproducible_mnist]
56+
- [:rocket: MNIST CNN Model][mnist_cnn]
57+
- [:tv: YouTube Video: Deep Learning Project Setup (+ CNN for MNIST)][deep_learning_setup_youtube]
58+
- VGG Models for Image Classification
59+
- [:rocket: VGG Model][vgg]
60+
- [:tv: YouTube Video: VGG Models for Image Classification][vgg_youtube]
61+
5262
### Functions
5363

5464
- Distance Functions
@@ -67,30 +77,23 @@ _Artificial Intelligence (AI) - Contains Code for Several YouTube Series._
6777
- [:chart_with_upwards_trend: Convex and Non-Convex Functions][gradient_descent_plot]
6878
- [:tv: YouTube Video: Discussing Batch, Stochastic, and Mini-Batch Gradient Descent][gradient_descent_youtube]
6979

70-
### Deep Learning
71-
72-
- Deep Learning Project Setup (+ CNN for MNIST)
73-
- [:orange_book: The Reproducible MNIST][the_reproducible_mnist]
74-
- [:rocket: MNIST CNN Model][mnist_cnn]
75-
- [:tv: YouTube Video: Deep Learning Project Setup (+ CNN for MNIST)][deep_learning_setup]
76-
7780
### Machine Learning Models from Scratch Using NumPy
7881

7982
- Gaussian Naive Bayes
8083
- [:rocket: Implementation][gaussian_naive_bayes]
8184
- [:orange_book: Theory][gaussian_naive_bayes_theory]
8285
- [:tv: YouTube Video: Implementing Gaussian Naive Bayes from Scratch][gaussian_naive_bayes_youtube]
83-
- [:tv: YouTube Video: AI/ML Model API Design and Numerical Stability (follow-up)][api_design_and_numerical_stability]
86+
- [:tv: YouTube Video: AI/ML Model API Design and Numerical Stability (follow-up)][api_design_and_numerical_stability_youtube]
8487
- K-Nearest Neighbors (k-NN)
8588
- [:rocket: Implementation][k_nearest_neighbors]
8689
- [:orange_book: Theory][k_nearest_neighbors_theory]
8790
- [:tv: YouTube Video: Implementing K-Nearest Neighbors from Scratch][k_nearest_neighbors_youtube]
88-
- [:tv: YouTube Video: AI/ML Model API Design and Numerical Stability (follow-up)][api_design_and_numerical_stability]
91+
- [:tv: YouTube Video: AI/ML Model API Design and Numerical Stability (follow-up)][api_design_and_numerical_stability_youtube]
8992
- Linear Regression
9093
- [:rocket: Implementation][linear_regression]
9194
- [:orange_book: Theory][linear_regression_theory]
9295
- [:tv: YouTube Video: Implementing Linear Regression from Scratch][linear_regression_youtube]
93-
- [:tv: YouTube Video: AI/ML Model API Design and Numerical Stability (follow-up)][api_design_and_numerical_stability]
96+
- [:tv: YouTube Video: AI/ML Model API Design and Numerical Stability (follow-up)][api_design_and_numerical_stability_youtube]
9497
- Logistic Regression
9598
- [:rocket: Implementation][logistic_regression]
9699
- [:orange_book: Theory][logistic_regression_theory]
@@ -106,62 +109,64 @@ _Artificial Intelligence (AI) - Contains Code for Several YouTube Series._
106109
[MIT License][license]
107110

108111
[license]: LICENSE
109-
[sigmoid]: activations/sigmoid.py
112+
[sigmoid]: activation/sigmoid.py
110113
[sigmoid_theory]: https://en.wikipedia.org/wiki/Sigmoid_function
111-
[sigmoid_plot]: activations/plots/sigmoid.png
114+
[sigmoid_plot]: activation/plots/sigmoid.png
112115
[sigmoid_youtube]: https://www.youtube.com/watch?v=oxC3T_-_Amw
113-
[relu]: activations/relu.py
116+
[relu]: activation/relu.py
114117
[relu_theory]: https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
115-
[relu_plot]: activations/plots/relu.png
118+
[relu_plot]: activation/plots/relu.png
116119
[relu_youtube]: https://www.youtube.com/watch?v=93qjwrP7PfE
117-
[leaky_relu]: activations/leaky_relu.py
120+
[leaky_relu]: activation/leaky_relu.py
118121
[leaky_relu_theory]: https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Leaky_ReLU
119-
[leaky_relu_plot]: activations/plots/leaky_relu.png
122+
[leaky_relu_plot]: activation/plots/leaky_relu.png
120123
[leaky_relu_youtube]: https://www.youtube.com/watch?v=1HLKeWG0qnE
121-
[gelu]: activations/gelu.py
124+
[gelu]: activation/gelu.py
122125
[gelu_theory]: https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Gaussian-error_linear_unit_(GELU)
123-
[gelu_plot]: activations/plots/gelu.png
126+
[gelu_plot]: activation/plots/gelu.png
124127
[gelu_youtube]: https://www.youtube.com/watch?v=1HLKeWG0qnE
125-
[swish]: activations/swish.py
128+
[swish]: activation/swish.py
126129
[swish_theory]: https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#SiLU
127-
[swish_plot]: activations/plots/swish.png
130+
[swish_plot]: activation/plots/swish.png
128131
[swish_youtube]: https://www.youtube.com/watch?v=1HLKeWG0qnE
129-
[serf]: activations/serf.py
132+
[serf]: activation/serf.py
130133
[serf_theory]: https://arxiv.org/abs/2108.09598
131-
[serf_plot]: activations/plots/serf.png
134+
[serf_plot]: activation/plots/serf.png
132135
[serf_youtube]: https://www.youtube.com/watch?v=CLjmEuCxuT4
133136
[serf_reddit]: https://www.reddit.com/r/MachineLearning/comments/uhgupq/r_d_serf_activation_function_improving_swish/
134-
[tanh]: activations/tanh.py
137+
[tanh]: activation/tanh.py
135138
[tanh_theory]: https://en.wikipedia.org/wiki/Hyperbolic_functions#Exponential_definitions
136-
[tanh_plot]: activations/plots/tanh.png
139+
[tanh_plot]: activation/plots/tanh.png
137140
[tanh_youtube]: https://www.youtube.com/watch?v=MSi1tobj-jg
138-
[distance_functions]: functions/distance.py
141+
[distance_functions]: function/distance.py
139142
[distance_functions_theory]: https://en.wikipedia.org/wiki/Similarity_measure
140143
[distance_functions_youtube]: https://www.youtube.com/watch?v=50G47n42-9o
141-
[convolution]: functions/convolution.py
144+
[convolution]: function/convolution.py
142145
[convolution_theory]: https://en.wikipedia.org/wiki/Convolution
143146
[convolution_youtube]: https://www.youtube.com/watch?v=pmyulQwV62k
144147
[gradient_descent_theory]: theory/gradient_descent/gradient_descent.pdf
145148
[gradient_descent_plot]: theory/gradient_descent/convex_and_non_convex_plot.png
146149
[gradient_descent_youtube]: https://www.youtube.com/watch?v=mV247Fe1DJc
147-
[gaussian_naive_bayes]: models/gaussian_naive_bayes.py
150+
[gaussian_naive_bayes]: model/ml/gaussian_naive_bayes.py
148151
[gaussian_naive_bayes_theory]: https://en.wikipedia.org/wiki/Naive_Bayes_classifier#Gaussian_naive_Bayes
149152
[gaussian_naive_bayes_youtube]: https://www.youtube.com/watch?v=maJIRFeQBVI
150-
[k_nearest_neighbors]: models/k_nearest_neighbors.py
153+
[k_nearest_neighbors]: model/ml/k_nearest_neighbors.py
151154
[k_nearest_neighbors_theory]: https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm
152155
[k_nearest_neighbors_youtube]: https://www.youtube.com/watch?v=8SFTAcZb9i4
153-
[linear_regression]: models/linear_regression.py
156+
[linear_regression]: model/ml/linear_regression.py
154157
[linear_regression_theory]: https://en.wikipedia.org/wiki/Linear_regression
155158
[linear_regression_youtube]: https://www.youtube.com/watch?v=7FdQZ9r41LU
156-
[logistic_regression]: models/logistic_regression.py
159+
[logistic_regression]: model/ml/logistic_regression.py
157160
[logistic_regression_theory]: https://en.wikipedia.org/wiki/Logistic_regression
158161
[logistic_regression_computing_gradients]: theory/gradients/logistic_regression/logistic_regression.pdf
159162
[logistic_regression_youtube]: https://www.youtube.com/watch?v=YDa3rX9yLCE
160-
[k_means_clustering]: models/k_means_clustering.py
163+
[k_means_clustering]: model/ml/k_means_clustering.py
161164
[k_means_clustering_theory]: https://en.wikipedia.org/wiki/K-means_clustering
162165
[k_means_clustering_youtube]: https://www.youtube.com/watch?v=NfPGFSUM-nI
163166
[implement]: https://www.youtube.com/watch?v=maJIRFeQBVI&list=PLG8XxYPkVOUvVzz1ZKcGAJpIBK7GRrFYR
164-
[api_design_and_numerical_stability]: https://www.youtube.com/watch?v=BOoTX0hkO6k
165-
[the_reproducible_mnist]: https://github.com/oniani/mnist
166-
[mnist_cnn]: models/mnist_cnn.py
167-
[deep_learning_setup]: https://www.youtube.com/watch?v=2JkJZQP9dHg
167+
[api_design_and_numerical_stability_youtube]: https://www.youtube.com/watch?v=BOoTX0hkO6k
168+
[the_reproducible_mnist]: https://github.com/davidoniani/mnist
169+
[mnist_cnn]: model/dl/mnist_cnn.py
170+
[deep_learning_setup_youtube]: https://www.youtube.com/watch?v=2JkJZQP9dHg
171+
[vgg]: model/dl/vgg.py
172+
[vgg_youtube]: https://www.youtube.com/watch?v=0Ak4i2j_diM
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎model/dl/vgg.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import torch
2+
import torch.nn as nn
3+
4+
5+
VGG_VARIANT: dict[int, list[int | str]] = {
6+
11: [64, "M", 128, "M", 256, 256, "M", 512, 512, "M", 512, 512, "M"],
7+
13: [64, 64, "M", 128, 128, "M", 256, 256, "M", 512, 512, "M", 512, 512, "M"],
8+
16: [64, 64, "M", 128, 128, "M", 256, 256, 256, "M", 512, 512, 512, "M", 512, 512, 512, "M"],
9+
# 19: [64, 64, "M", 128, 128, "M", 256, 256, 256, 256, "M", 512, 512, 512, 512, "M", 512, 512, 512, 512, "M"],
10+
}
11+
12+
13+
class VGG(nn.Module):
14+
"""A generic VGG model representation."""
15+
16+
def __init__(self, variant: int) -> None:
17+
"""Performs inheritance and defines model blocks."""
18+
19+
super().__init__()
20+
21+
self.features = self.make_layers(VGG_VARIANT[variant])
22+
self.classifier = nn.Linear(512, 10)
23+
24+
def forward(self, data: torch.Tensor) -> torch.Tensor:
25+
"""Performs a forward pass."""
26+
27+
out = self.features(data)
28+
out = out.flatten(1)
29+
out = self.classifier(out)
30+
31+
return out
32+
33+
def make_layers(self, model_config: list[int | str]) -> nn.Sequential:
34+
"""Uses the configuration and builds up a VGG model."""
35+
36+
layers = []
37+
in_channels = 3
38+
for val in model_config:
39+
if isinstance(val, int):
40+
layer = [
41+
nn.Conv2d(in_channels=in_channels, out_channels=val, kernel_size=3, padding=1),
42+
nn.BatchNorm2d(num_features=val),
43+
nn.ReLU(),
44+
]
45+
layers.extend(layer)
46+
in_channels = val
47+
elif val == "M":
48+
layers.append(nn.MaxPool2d(kernel_size=2, stride=2))
49+
else:
50+
raise ValueError(f"Unknown value: {val}")
51+
52+
layers.append(nn.AvgPool2d(kernel_size=1, stride=1))
53+
54+
return nn.Sequential(*layers)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.