> For the complete documentation index, see [llms.txt](https://ykkim.gitbook.io/dlip/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ykkim.gitbook.io/dlip/deep-learning-for-perception/example-code/vgg.md).

# VGG Tutorial

## Introduction

[Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556) (ICLR 2015)

Read about VGG: click here

### VGG-16

![](/files/-MR96j282hPorJd0WzJI)

![](/files/-MR96gBvxDpa_0jBRTXb)

### VGG-19

![](/files/-MR96j27UtE6hoPLQgQR)

## Keras

### Pretrained model

Using [Keras application of VGG 16, 19](https://keras.io/api/applications/vgg/#vgg16-function) with ImageNet pretrained

* Check the index of imagenet 1000 classes labels: [click here](https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a)

[My example colab code: click here](https://colab.research.google.com/drive/1yjiFt1BiTE7H8BduxJU-8hWSumH-KDW7#scrollTo=zHK9wFdofwor)

```python
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
import numpy as np

# Open VGG model
model = VGG16(weights='imagenet')

img_path = 'cat2.jpg'
img = image.load_img(img_path, target_size=(224, 224))
plt.imshow(img)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# Run Classfication
preds = model.predict(x)

# Display the score
display(preds.shape)
idx=np.argmax(preds)
score=preds[0][idx]
display(idx, score)
```

### Building from scratch

[VGG-16: My Keras code](https://colab.research.google.com/drive/1TUI3WX639yajO0Hf6KW-GsQ8VCZxPFod?usp=sharing), [VGG-16 weight file](https://drive.google.com/u/1/uc?id=0Bz7KyqmuGsilT0J5dmRCM0ROVHc\&export=download)

[Read this blog for step by step tutorial](https://towardsdatascience.com/step-by-step-vgg16-implementation-in-keras-for-beginners-a833c686ae6c)

![](/files/-MR96gBxqsNVvkSKgcjG)

```python
#Importing library
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
import numpy as np

np.random.seed(1000)


model = Sequential()
model.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding="same", activation="relu"))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

model.add(Flatten())
model.add(Dense(units=4096,activation="relu"))
model.add(Dense(units=4096,activation="relu"))
model.add(Dense(units=2, activation="softmax"))

#Model Summary
model.summary()

#weights_path='vgg16_weights.h5'
#model.load_weights(weights_path)
```

## PyTorch

### Pretrained model:

### Building from scratch:

Implementation by PyTorch: [Vgg.py](https://github.com/pytorch/vision/blob/master/torchvision/models/vgg.py)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ykkim.gitbook.io/dlip/deep-learning-for-perception/example-code/vgg.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
