{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Networks Using Blocks (VGG)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:21:55.309728Z",
"start_time": "2019-07-03T22:21:51.994279Z"
}
},
"outputs": [],
"source": [
"import d2l\n",
"from mxnet import gluon, np, npx\n",
"from mxnet.gluon import nn\n",
"npx.set_np()\n",
"\n",
"train_iter, test_iter = d2l.load_data_fashion_mnist(\n",
" batch_size=128, resize=224)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"VGG block"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:21:55.320094Z",
"start_time": "2019-07-03T22:21:55.314113Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "1"
}
},
"outputs": [],
"source": [
"def vgg_block(num_convs, num_channels):\n",
" blk = nn.Sequential()\n",
" for _ in range(num_convs):\n",
" blk.add(nn.Conv2D(num_channels, kernel_size=3,\n",
" padding=1, activation='relu'))\n",
" blk.add(nn.MaxPool2D(pool_size=2, strides=2))\n",
" return blk"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"The model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:21:55.336383Z",
"start_time": "2019-07-03T22:21:55.322439Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "3"
}
},
"outputs": [],
"source": [
"conv_arch = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512))\n",
"\n",
"def vgg(conv_arch):\n",
" net = nn.Sequential()\n",
" # The convolutional layer part\n",
" for (num_convs, num_channels) in conv_arch:\n",
" net.add(vgg_block(num_convs, num_channels))\n",
" # The fully connected layer part\n",
" net.add(nn.Dense(4096, activation='relu'), nn.Dropout(0.5),\n",
" nn.Dense(4096, activation='relu'), nn.Dropout(0.5),\n",
" nn.Dense(10))\n",
" return net\n",
"\n",
"net = vgg(conv_arch)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Training with narrow version"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:28:14.142303Z",
"start_time": "2019-07-03T22:21:55.338438Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "5"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss 0.177, train acc 0.935, test acc 0.925\n",
"9350.4 exampes/sec on gpu(0)\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"ratio = 4\n",
"small_conv_arch = [(pair[0], pair[1] // ratio) for pair in conv_arch]\n",
"net = vgg(small_conv_arch)\n",
"d2l.train_ch5(net, train_iter, test_iter, num_epochs=10, lr=0.05)"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}