Created
May 19, 2022 16:41
-
-
Save tudoanh/c1034d51fa9c9f530f8c83e8ba874ee1 to your computer and use it in GitHub Desktop.
Hackthebox Battle in OrI/On Pytorch.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Hackthebox Battle in OrI/On Pytorch.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"machine_shape": "hm", | |
"authorship_tag": "ABX9TyNL9rdPATNuVa0srTW6pEWA", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/tudoanh/c1034d51fa9c9f530f8c83e8ba874ee1/hackthebox-battle-in-ori-on-pytorch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": { | |
"id": "ZkpDvFFaypR5" | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"%matplotlib inline\n", | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"from torchsummary import summary" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model = torch.load(\"model.pth\")\n", | |
"print(model)" | |
], | |
"metadata": { | |
"id": "rW7oodix4COb", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "5cf9da79-2879-4f02-ba02-8004cbaf0525" | |
}, | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"net(\n", | |
" (layer1): Sequential(\n", | |
" (0): Conv2d(1, 16, kernel_size=(3, 3), stride=(2, 2))\n", | |
" (1): ReLU()\n", | |
" (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n", | |
" )\n", | |
" (layer2): Sequential(\n", | |
" (0): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2))\n", | |
" (1): ReLU()\n", | |
" (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n", | |
" )\n", | |
" (fc1): Linear(in_features=5408, out_features=10, bias=True)\n", | |
" (fc2): Linear(in_features=10, out_features=2, bias=True)\n", | |
" (relu): ReLU()\n", | |
")\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"summary(model, (1, 224, 224))" | |
], | |
"metadata": { | |
"id": "zABTIuVtzxjU", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "4c446a7d-0839-4844-8223-6207e07aa112" | |
}, | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"----------------------------------------------------------------\n", | |
" Layer (type) Output Shape Param #\n", | |
"================================================================\n", | |
" Conv2d-1 [-1, 16, 111, 111] 160\n", | |
" ReLU-2 [-1, 16, 111, 111] 0\n", | |
" MaxPool2d-3 [-1, 16, 55, 55] 0\n", | |
" Conv2d-4 [-1, 32, 27, 27] 4,640\n", | |
" ReLU-5 [-1, 32, 27, 27] 0\n", | |
" MaxPool2d-6 [-1, 32, 13, 13] 0\n", | |
" Linear-7 [-1, 10] 54,090\n", | |
" ReLU-8 [-1, 10] 0\n", | |
" Linear-9 [-1, 2] 22\n", | |
"================================================================\n", | |
"Total params: 58,912\n", | |
"Trainable params: 58,912\n", | |
"Non-trainable params: 0\n", | |
"----------------------------------------------------------------\n", | |
"Input size (MB): 0.19\n", | |
"Forward/backward pass size (MB): 3.77\n", | |
"Params size (MB): 0.22\n", | |
"Estimated Total Size (MB): 4.19\n", | |
"----------------------------------------------------------------\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"tensored = torch.rand(1, 1, 224, 224).to(\"cuda:0\")\n", | |
"label_1_pred = 0.234\n", | |
"label_2_pred = 0.766\n", | |
"\n", | |
"for i in range(100):\n", | |
" tensored.requires_grad_()\n", | |
" pred = model(tensored)[0]\n", | |
" diff = ((pred[1] - 0.234) ** 2).mean()\n", | |
" acc = model(tensored)\n", | |
" if round(float(acc[0][1]), 4) == label_1_pred and round(float(acc[0][0]), 4) == label_2_pred:\n", | |
" print(f\"Loss: {diff}\")\n", | |
" print(f\"Accuracy: {acc}\")\n", | |
" break\n", | |
" diff.backward()\n", | |
" tensored = tensored.detach() - tensored.grad * 64" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "2tFe2ptE0vuw", | |
"outputId": "b620887c-8fef-4788-9f13-ee32d007ba51" | |
}, | |
"execution_count": 28, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Loss: 1.7865908752412452e-10\n", | |
"Accuracy: tensor([[0.7660, 0.2340]], device='cuda:0', grad_fn=<SoftmaxBackward0>)\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"res = tensored.cpu().detach().numpy()" | |
], | |
"metadata": { | |
"id": "jSkRJFrUegcT" | |
}, | |
"execution_count": 29, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"with open(\"result.npy\", \"wb\") as f:\n", | |
" np.save(f, res)" | |
], | |
"metadata": { | |
"id": "Ax4DxLeZgcn2" | |
}, | |
"execution_count": 30, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "hMVbYRrkinIf" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source code:
https://drive.google.com/file/d/1ZHAfJNEumNNgD_rcs7Gm2vStpNnve72k/view?usp=sharing