{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[],"gpuType":"T4"},"accelerator":"GPU","kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":7806033,"sourceType":"datasetVersion","datasetId":4456930}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import sys\nsys.path.append('/kaggle/input/d2l-module')","metadata":{"id":"pq2IXjyNydhJ","outputId":"360a5e72-42e9-4de8-f612-db2122b3c432","execution":{"iopub.status.busy":"2024-04-29T09:42:16.438002Z","iopub.execute_input":"2024-04-29T09:42:16.438317Z","iopub.status.idle":"2024-04-29T09:42:16.443279Z","shell.execute_reply.started":"2024-04-29T09:42:16.438292Z","shell.execute_reply":"2024-04-29T09:42:16.442378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom d2l import torch as d2l\n\n\ndef vgg_block(num_convs, in_channels, out_channels):\n    layers = []\n    for _ in range(num_convs):\n        layers.append(nn.Conv2d(in_channels, out_channels,\n                                kernel_size=3, padding=1))\n        layers.append(nn.ReLU())\n        in_channels = out_channels\n    layers.append(nn.MaxPool2d(kernel_size=2,stride=2))\n    return nn.Sequential(*layers)\n\nsource_dir='/kaggle/input/dog-breed-identification'\ndata_dir='/kaggle/working/'","metadata":{"id":"1xyUiOwIyCYX","execution":{"iopub.status.busy":"2024-04-29T09:43:48.472518Z","iopub.execute_input":"2024-04-29T09:43:48.473503Z","iopub.status.idle":"2024-04-29T09:43:48.479754Z","shell.execute_reply.started":"2024-04-29T09:43:48.473469Z","shell.execute_reply":"2024-04-29T09:43:48.478719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_arch = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512))","metadata":{"id":"wmbJsBD9yCYY","execution":{"iopub.status.busy":"2024-04-29T09:43:55.890477Z","iopub.execute_input":"2024-04-29T09:43:55.890850Z","iopub.status.idle":"2024-04-29T09:43:55.895604Z","shell.execute_reply.started":"2024-04-29T09:43:55.890823Z","shell.execute_reply":"2024-04-29T09:43:55.894669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vgg(conv_arch):\n    conv_blks = []\n    in_channels = 1\n    # 卷积层部分\n    for (num_convs, out_channels) in conv_arch:\n        conv_blks.append(vgg_block(num_convs, in_channels, out_channels))\n        in_channels = out_channels\n\n    return nn.Sequential(\n        *conv_blks, nn.Flatten(),\n        # 全连接层部分\n        nn.Linear(out_channels * 7 * 7, 4096), nn.ReLU(), nn.Dropout(0.5),\n        nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(0.5),\n        nn.Linear(4096, 10))\n\nnet = vgg(conv_arch)","metadata":{"id":"_B5p2ImLyCYY","execution":{"iopub.status.busy":"2024-04-29T09:44:01.996547Z","iopub.execute_input":"2024-04-29T09:44:01.996889Z","iopub.status.idle":"2024-04-29T09:44:03.251758Z","shell.execute_reply.started":"2024-04-29T09:44:01.996864Z","shell.execute_reply":"2024-04-29T09:44:03.250924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = torch.randn(size=(1, 1, 224, 224))\nfor blk in net:\n    X = blk(X)\n    print(blk.__class__.__name__,'output shape:\\t',X.shape)","metadata":{"id":"wth0tgg3yCYY","execution":{"iopub.status.busy":"2024-04-29T09:44:09.545377Z","iopub.execute_input":"2024-04-29T09:44:09.545964Z","iopub.status.idle":"2024-04-29T09:44:09.835248Z","shell.execute_reply.started":"2024-04-29T09:44:09.545935Z","shell.execute_reply":"2024-04-29T09:44:09.834310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratio = 4\nsmall_conv_arch = [(pair[0], pair[1] // ratio) for pair in conv_arch]\nnet = vgg(small_conv_arch)","metadata":{"id":"XXOIVOUXyCYZ","outputId":"7eb63796-aa26-4d66-9850-bcb3d78a1013","execution":{"iopub.status.busy":"2024-04-29T09:44:15.441289Z","iopub.execute_input":"2024-04-29T09:44:15.442048Z","iopub.status.idle":"2024-04-29T09:44:15.859457Z","shell.execute_reply.started":"2024-04-29T09:44:15.442015Z","shell.execute_reply":"2024-04-29T09:44:15.858422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr, num_epochs, batch_size = 0.05, 10, 128\ntrain_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)\nd2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())","metadata":{"id":"zG30NZ3vyCYa","outputId":"e08e243f-b09d-4890-9240-8e7deeb931ab","execution":{"iopub.status.busy":"2024-04-29T09:44:21.812611Z","iopub.execute_input":"2024-04-29T09:44:21.812989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}