Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Red #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions models/ecbsr1d/demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 一、F.conv2d() 与 forward的等价性\n",
"\n",
"常用的卷积参数为 inp,oup,kernel_size,stride,padding\n",
"F.conv2d(inp, weight, bias, stride)\n",
"padding部分需要自己手动填充\n",
"2"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([64, 64, 3, 1])\n",
"torch.Size([64, 28, 28])\n"
]
}
],
"source": [
"conv0 = torch.nn.Conv2d(64, 64, kernel_size=(3,1), stride=(1,0), padding=(1,0))\n",
"k0 = conv0.weight\n",
"b0 = conv.bias\n",
"inp = torch.randn(64, 28, 28)\n",
"out1 = conv1(inp)\n",
"# out2 = F.conv2d(input=inp, weight=k0, bias=b0, stride=1)\n",
"print(k0.shape)\n",
"print(out1.shape)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'y0' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/user3/code/SimpleIR/models/ecbsr copy/demo.ipynb Cell 4'\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> <a href='vscode-notebook-cell://ssh-remote%2Bred_t4/home/user3/code/SimpleIR/models/ecbsr%20copy/demo.ipynb#ch0000008vscode-remote?line=0'>1</a>\u001b[0m y0 \u001b[39m=\u001b[39m F\u001b[39m.\u001b[39mpad(y0, (\u001b[39m1\u001b[39m, \u001b[39m1\u001b[39m, \u001b[39m1\u001b[39m, \u001b[39m1\u001b[39m), \u001b[39m'\u001b[39m\u001b[39mconstant\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m0\u001b[39m)\n",
"\u001b[0;31mNameError\u001b[0m: name 'y0' is not defined"
]
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([64, 28, 28])\n",
"torch.Size([64, 64, 3, 3])\n",
"torch.Size([64, 28, 30])\n"
]
}
],
"source": [
"conv0= torch.nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)\n",
"k0 = conv0.weight\n",
"b0 = conv0.bias\n",
"inp = torch.randn(64, 28, 28)\n",
"out = conv0(inp)\n",
"# out2 = F.conv2d(input=inp, weight=k0, bias=b0, stride=1)\n",
"print(out.shape)\n",
"print(k0.shape)\n",
"out = F.pad(out, (1, 1, 0, 0), 'constant', 0)\n",
"print(out.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"conv0 = torch.nn.Conv2d(self.inp_planes, self.out_planes, kernel_size=1, padding=0)\n",
"self.k0 = conv0.weight\n",
"self.b0 = conv0.bias\n",
"\n",
"# init scale & bias\n",
"scale = torch.randn(size=(self.out_planes, 1, 1, 1)) * 1e-3\n",
"self.scale = nn.Parameter(scale)\n",
"# bias = 0.0\n",
"# bias = [bias for c in range(self.out_planes)]\n",
"# bias = torch.FloatTensor(bias)\n",
"bias = torch.randn(self.out_planes) * 1e-3\n",
"bias = torch.reshape(bias, (self.out_planes,))\n",
"self.bias = nn.Parameter(bias)\n",
"# init mask\n",
"self.mask = torch.zeros((self.out_planes, 1, 3, 3), dtype=torch.float32)\n",
"for i in range(self.out_planes):\n",
" self.mask[i, 0, 0, 0] = 1.0\n",
" self.mask[i, 0, 1, 0] = 2.0\n",
" self.mask[i, 0, 2, 0] = 1.0\n",
" self.mask[i, 0, 0, 2] = -1.0\n",
" self.mask[i, 0, 1, 2] = -2.0\n",
" self.mask[i, 0, 2, 2] = -1.0\n",
"self.mask = nn.Parameter(data=self.mask, requires_grad=False)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y0 = F.conv2d(input=x, weight=k0, bias=b0, stride=1)\n",
"# explicitly padding with bias\n",
"y0 = F.pad(y0, (1, 1, 1, 1), 'constant', 0)\n",
"b0_pad = self.b0.view(1, -1, 1, 1)\n",
"y0[:, :, 0:1, :] = b0_pad\n",
"y0[:, :, -1:, :] = b0_pad\n",
"y0[:, :, :, 0:1] = b0_pad\n",
"y0[:, :, :, -1:] = b0_pad\n",
"# conv-3x3\n",
"y1 = F.conv2d(input=y0, weight=self.scale * self.mask, bias=self.bias, stride=1, groups=self.out_planes)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.13 ('py38': conda)",
"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.8.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "798585ee0e69c52f6919ecef47b9e35918308944029e7c22636f87b86ab713f5"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading