Skip to content

Commit e3caeea

Browse files
authored
Merge pull request #172 from jeffin07/master
implementaion of VGG16
2 parents a36b931 + 76e2aa2 commit e3caeea

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

Machine_Learning/src/VGG16/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This is the implemetaion of the VGG architecture(16 layers).I've used mnist dataset to train the model for simplicity<br/>
2+
3+
The paper can be found in [VGG](https://arxiv.org/pdf/1409.1556.pdf) <br/>
4+
5+
And the model architecture for VGG is given below the implemetation is for the configuration D(16 layers)<br/>
6+
7+
![](imgs/vggnet_table1.png)
Loading

Machine_Learning/src/VGG16/vgg_tf2.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import tensorflow as tf
2+
from tensorflow.keras.models import Model
3+
from tensorflow.keras.losses import MSE
4+
from tensorflow.keras.optimizers import Adam
5+
import numpy as np
6+
7+
class Vgg16(tf.keras.Model):
8+
def __init__(self,output_nodes):
9+
super(Vgg16, self).__init__()
10+
# layers needed
11+
self.conv1_1 = tf.keras.layers.Conv2D(
12+
input_shape=[None,28,28,1],filters=64, kernel_size=3,
13+
padding="same", activation="relu")
14+
self.conv1_2 = tf.keras.layers.Conv2D(
15+
filters=64, kernel_size=3,
16+
padding="same", activation="relu")
17+
self.conv2_1 = tf.keras.layers.Conv2D(
18+
filters=128, kernel_size=3,
19+
padding="same", activation="relu")
20+
self.conv2_2 = tf.keras.layers.Conv2D(
21+
filters=128, kernel_size=3,
22+
padding="same", activation="relu")
23+
self.conv3_1 = tf.keras.layers.Conv2D(
24+
filters=256,kernel_size=3,
25+
padding="same", activation="relu")
26+
self.conv3_2 = tf.keras.layers.Conv2D(
27+
filters=256,kernel_size=3,
28+
padding="same", activation="relu")
29+
self.conv3_3 = tf.keras.layers.Conv2D(
30+
filters=256,kernel_size=3,
31+
padding="same", activation="relu")
32+
33+
self.conv4_1 = tf.keras.layers.Conv2D(
34+
filters=512, kernel_size=3,
35+
padding="same", activation="relu")
36+
self.conv4_2 = tf.keras.layers.Conv2D(
37+
filters=512, kernel_size=3,
38+
padding="same", activation="relu")
39+
self.conv4_3 = tf.keras.layers.Conv2D(
40+
filters=512, kernel_size=3,
41+
padding="same", activation="relu")
42+
self.conv5_1 = tf.keras.layers.Conv2D(
43+
filters=512, kernel_size=3,
44+
padding="same", activation="relu")
45+
self.conv5_2 = tf.keras.layers.Conv2D(
46+
filters=512, kernel_size=3,
47+
padding="same", activation="relu")
48+
self.conv5_3 = tf.keras.layers.Conv2D(
49+
filters=512, kernel_size=3,
50+
padding="same", activation="relu")
51+
self.dense1_1 = tf.keras.layers.Dense(
52+
units=4096, activation="relu")
53+
self.dense1_2 = tf.keras.layers.Dense(
54+
units=4096, activation="relu")
55+
self.dense2 = tf.keras.layers.Dense(
56+
units=output_nodes, activation="softmax")
57+
self.maxPool = tf.keras.layers.MaxPool2D(
58+
pool_size=2, strides=2, padding="same")
59+
self.flatten = tf.keras.layers.Flatten()
60+
61+
def call(self,input):
62+
# ops
63+
x = self.conv1_1(input)
64+
x = self.conv1_2(x)
65+
x = self.maxPool(x)
66+
x = self.conv2_1(x)
67+
x = self.conv2_2(x)
68+
x = self.maxPool(x)
69+
x = self.conv3_1(x)
70+
x = self.conv3_2(x)
71+
x = self.conv3_3(x)
72+
x = self.maxPool(x)
73+
x = self.conv4_1(x)
74+
x = self.conv4_2(x)
75+
x = self.conv4_3(x)
76+
x = self.maxPool(x)
77+
x = self.conv5_1(x)
78+
x = self.conv5_2(x)
79+
x = self.conv5_3(x)
80+
x = self.maxPool(x)
81+
x = self.flatten(x)
82+
x = self.dense1_1(x)
83+
x = self.dense1_2(x)
84+
x = self.dense2(x)
85+
return x
86+
87+
88+
network = Vgg16(10)
89+
# network.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
90+
91+
mnist = tf.keras.datasets.mnist
92+
93+
(x_train, y_train),(x_test, y_test) = mnist.load_data()
94+
x_train = (x_train.reshape(-1, 28, 28, 1) / 255).astype(np.float32)
95+
# x_train, x_test = x_train / 255.0, x_test / 255.0
96+
y_train = np.eye(10)[y_train].astype(np.float32)
97+
x_train = x_train[:100]
98+
y_train = y_train[:100]
99+
100+
101+
102+
input_layer = tf.keras.layers.Input(shape=(28,28,1))
103+
output_layer = network(input_layer)
104+
training_model = Model(inputs=input_layer,outputs=output_layer)
105+
optim = Adam()
106+
107+
for i in range(100):
108+
with tf.GradientTape(watch_accessed_variables=False) as tape:
109+
tape.watch(training_model.trainable_variables)
110+
preds = training_model(x_train)
111+
loss = MSE(preds, y_train)
112+
cost = tf.reduce_mean(loss)
113+
grads = tape.gradient(loss, training_model.trainable_variables)
114+
optim.apply_gradients(zip(grads, training_model.trainable_variables))
115+
print(cost)
116+
117+
118+
119+

0 commit comments

Comments
 (0)