MarkusEssl commited on
Commit
99d0358
·
1 Parent(s): 7bacc7f
Files changed (3) hide show
  1. nbs/02_utils.ipynb +77 -10
  2. signify/_modidx.py +6 -21
  3. signify/utils.py +17 -6
nbs/02_utils.ipynb CHANGED
@@ -39,13 +39,34 @@
39
  },
40
  {
41
  "cell_type": "code",
42
- "execution_count": null,
43
  "metadata": {},
44
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
45
  "source": [
46
  "#| export\n",
47
  "import torch \n",
48
  "import torch.nn as nn\n",
 
 
 
 
 
 
 
 
 
49
  "class SiameseNetwork(nn.Module):\n",
50
  " def __init__(self, model, embedding_size=2000):\n",
51
  " super(SiameseNetwork, self).__init__()\n",
@@ -53,8 +74,10 @@
53
  " self.backbone = model\n",
54
  " self.rgb_grayscale = nn.Conv2d(1,3,kernel_size=3,stride=1,padding=1)\n",
55
  " self.a = nn.Sigmoid()\n",
56
- " self.fc1 = nn.Linear(in_features=1000, out_features=embedding_size) \n",
57
  " self.fc2 = nn.Linear(in_features=embedding_size,out_features=1) \n",
 
 
58
  "\n",
59
  " def forward_once(self, x):\n",
60
  " \n",
@@ -67,10 +90,46 @@
67
  " \n",
68
  " x1 = self.forward_once(x1)\n",
69
  " x2 = self.forward_once(x2)\n",
70
- " out1, out2 = self.fc1(x1), self.fc1(x2)\n",
71
- " dis = torch.abs(torch.sub(out1, out2))\n",
72
- " \n",
73
- " return self.a(self.fc2(dis)).squeeze()"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  ]
75
  },
76
  {
@@ -86,18 +145,26 @@
86
  ],
87
  "metadata": {
88
  "kernelspec": {
89
- "display_name": "Python 3.11.3 ('siglit')",
90
  "language": "python",
91
  "name": "python3"
92
  },
93
  "language_info": {
 
 
 
 
 
 
94
  "name": "python",
95
- "version": "3.11.3"
 
 
96
  },
97
  "orig_nbformat": 4,
98
  "vscode": {
99
  "interpreter": {
100
- "hash": "58fbb3b322f4fd165dbe2df418fd689197a633c5b2065049c07cf881fd363779"
101
  }
102
  }
103
  },
 
39
  },
40
  {
41
  "cell_type": "code",
42
+ "execution_count": 15,
43
  "metadata": {},
44
+ "outputs": [
45
+ {
46
+ "ename": "ModuleNotFoundError",
47
+ "evalue": "No module named 'torch'",
48
+ "output_type": "error",
49
+ "traceback": [
50
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
51
+ "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
52
+ "Cell \u001b[0;32mIn[15], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#| export\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m \n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mnn\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnn\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mIdentity\u001b[39;00m(nn\u001b[38;5;241m.\u001b[39mModule):\n",
53
+ "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'torch'"
54
+ ]
55
+ }
56
+ ],
57
  "source": [
58
  "#| export\n",
59
  "import torch \n",
60
  "import torch.nn as nn\n",
61
+ "\n",
62
+ "class Identity(nn.Module):\n",
63
+ " def __init__(self):\n",
64
+ " super(Identity, self).__init__()\n",
65
+ " \n",
66
+ " def forward(self, x):\n",
67
+ " return x\n",
68
+ " \n",
69
+ "\n",
70
  "class SiameseNetwork(nn.Module):\n",
71
  " def __init__(self, model, embedding_size=2000):\n",
72
  " super(SiameseNetwork, self).__init__()\n",
 
74
  " self.backbone = model\n",
75
  " self.rgb_grayscale = nn.Conv2d(1,3,kernel_size=3,stride=1,padding=1)\n",
76
  " self.a = nn.Sigmoid()\n",
77
+ " self.fc1 = nn.Linear(in_features=1280, out_features=embedding_size) \n",
78
  " self.fc2 = nn.Linear(in_features=embedding_size,out_features=1) \n",
79
+ " self.relu = nn.ReLU()\n",
80
+ " self.backbone.classifier = Identity()\n",
81
  "\n",
82
  " def forward_once(self, x):\n",
83
  " \n",
 
90
  " \n",
91
  " x1 = self.forward_once(x1)\n",
92
  " x2 = self.forward_once(x2)\n",
93
+ " out1, out2 = self.relu(self.fc1(x1)), self.relu(self.fc1(x2))\n",
94
+ " dis = torch.abs((out1 - out2))\n",
95
+ " pred = self.a(self.fc2(dis)).squeeze()\n",
96
+ " return pred"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "execution_count": 14,
102
+ "metadata": {},
103
+ "outputs": [
104
+ {
105
+ "name": "stdout",
106
+ "output_type": "stream",
107
+ "text": [
108
+ "255\n",
109
+ "0\n"
110
+ ]
111
+ },
112
+ {
113
+ "data": {
114
+ "text/plain": [
115
+ "True"
116
+ ]
117
+ },
118
+ "execution_count": 14,
119
+ "metadata": {},
120
+ "output_type": "execute_result"
121
+ }
122
+ ],
123
+ "source": [
124
+ "import cv2\n",
125
+ "import numpy as np\n",
126
+ "p = \"/Users/markus/code/explore/signify/results/media/documents/ajy01c00.png\"\n",
127
+ "pnew = \"/Users/markus/code/explore/signify/results/media/documents/3.png\"\n",
128
+ "\n",
129
+ "img = 255 - cv2.imread(p) \n",
130
+ "print(img.max())\n",
131
+ "print(img.min())\n",
132
+ "cv2.imwrite(pnew, img)"
133
  ]
134
  },
135
  {
 
145
  ],
146
  "metadata": {
147
  "kernelspec": {
148
+ "display_name": "Python 3.10.8 ('base')",
149
  "language": "python",
150
  "name": "python3"
151
  },
152
  "language_info": {
153
+ "codemirror_mode": {
154
+ "name": "ipython",
155
+ "version": 3
156
+ },
157
+ "file_extension": ".py",
158
+ "mimetype": "text/x-python",
159
  "name": "python",
160
+ "nbconvert_exporter": "python",
161
+ "pygments_lexer": "ipython3",
162
+ "version": "3.10.8"
163
  },
164
  "orig_nbformat": 4,
165
  "vscode": {
166
  "interpreter": {
167
+ "hash": "a2b499d73ef2075ded5123ca15bce54816687eb05fa98ad819942f2bd35191e9"
168
  }
169
  }
170
  },
signify/_modidx.py CHANGED
@@ -298,26 +298,11 @@ d = { 'settings': { 'branch': 'main',
298
  'signify.siamese.with_cbs': ('siamese.html#with_cbs', 'signify/siamese.py'),
299
  'signify.siamese.with_cbs.__call__': ('siamese.html#with_cbs.__call__', 'signify/siamese.py'),
300
  'signify.siamese.with_cbs.__init__': ('siamese.html#with_cbs.__init__', 'signify/siamese.py')},
301
- 'signify.utils': { 'signify.utils.SiameseNetwork': ('utils.html#siamesenetwork', 'signify/utils.py'),
 
 
 
302
  'signify.utils.SiameseNetwork.__init__': ('utils.html#siamesenetwork.__init__', 'signify/utils.py'),
303
  'signify.utils.SiameseNetwork.forward': ('utils.html#siamesenetwork.forward', 'signify/utils.py'),
304
- 'signify.utils.SiameseNetwork.forward_once': ('utils.html#siamesenetwork.forward_once', 'signify/utils.py')},
305
- 'signify.yolo.detect': {},
306
- 'signify.yolo.models.common': {},
307
- 'signify.yolo.models.experimental': {},
308
- 'signify.yolo.models.export': {},
309
- 'signify.yolo.models.yolo': {},
310
- 'signify.yolo.utils.activations': {},
311
- 'signify.yolo.utils.autoanchor': {},
312
- 'signify.yolo.utils.aws.resume': {},
313
- 'signify.yolo.utils.datasets': {},
314
- 'signify.yolo.utils.flask_rest_api.example_request': {},
315
- 'signify.yolo.utils.flask_rest_api.restapi': {},
316
- 'signify.yolo.utils.general': {},
317
- 'signify.yolo.utils.google_utils': {},
318
- 'signify.yolo.utils.loss': {},
319
- 'signify.yolo.utils.metrics': {},
320
- 'signify.yolo.utils.plots': {},
321
- 'signify.yolo.utils.torch_utils': {},
322
- 'signify.yolo.utils.wandb_logging.log_dataset': {},
323
- 'signify.yolo.utils.wandb_logging.wandb_utils': {}}}
 
298
  'signify.siamese.with_cbs': ('siamese.html#with_cbs', 'signify/siamese.py'),
299
  'signify.siamese.with_cbs.__call__': ('siamese.html#with_cbs.__call__', 'signify/siamese.py'),
300
  'signify.siamese.with_cbs.__init__': ('siamese.html#with_cbs.__init__', 'signify/siamese.py')},
301
+ 'signify.utils': { 'signify.utils.Identity': ('utils.html#identity', 'signify/utils.py'),
302
+ 'signify.utils.Identity.__init__': ('utils.html#identity.__init__', 'signify/utils.py'),
303
+ 'signify.utils.Identity.forward': ('utils.html#identity.forward', 'signify/utils.py'),
304
+ 'signify.utils.SiameseNetwork': ('utils.html#siamesenetwork', 'signify/utils.py'),
305
  'signify.utils.SiameseNetwork.__init__': ('utils.html#siamesenetwork.__init__', 'signify/utils.py'),
306
  'signify.utils.SiameseNetwork.forward': ('utils.html#siamesenetwork.forward', 'signify/utils.py'),
307
+ 'signify.utils.SiameseNetwork.forward_once': ( 'utils.html#siamesenetwork.forward_once',
308
+ 'signify/utils.py')}}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
signify/utils.py CHANGED
@@ -1,11 +1,20 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_utils.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['SiameseNetwork']
5
 
6
  # %% ../nbs/02_utils.ipynb 1
7
  import torch
8
  import torch.nn as nn
 
 
 
 
 
 
 
 
 
9
  class SiameseNetwork(nn.Module):
10
  def __init__(self, model, embedding_size=2000):
11
  super(SiameseNetwork, self).__init__()
@@ -13,8 +22,10 @@ class SiameseNetwork(nn.Module):
13
  self.backbone = model
14
  self.rgb_grayscale = nn.Conv2d(1,3,kernel_size=3,stride=1,padding=1)
15
  self.a = nn.Sigmoid()
16
- self.fc1 = nn.Linear(in_features=1000, out_features=embedding_size)
17
  self.fc2 = nn.Linear(in_features=embedding_size,out_features=1)
 
 
18
 
19
  def forward_once(self, x):
20
 
@@ -27,7 +38,7 @@ class SiameseNetwork(nn.Module):
27
 
28
  x1 = self.forward_once(x1)
29
  x2 = self.forward_once(x2)
30
- out1, out2 = self.fc1(x1), self.fc1(x2)
31
- dis = torch.abs(torch.sub(out1, out2))
32
-
33
- return self.a(self.fc2(dis)).squeeze()
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_utils.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['Identity', 'SiameseNetwork']
5
 
6
  # %% ../nbs/02_utils.ipynb 1
7
  import torch
8
  import torch.nn as nn
9
+
10
+ class Identity(nn.Module):
11
+ def __init__(self):
12
+ super(Identity, self).__init__()
13
+
14
+ def forward(self, x):
15
+ return x
16
+
17
+
18
  class SiameseNetwork(nn.Module):
19
  def __init__(self, model, embedding_size=2000):
20
  super(SiameseNetwork, self).__init__()
 
22
  self.backbone = model
23
  self.rgb_grayscale = nn.Conv2d(1,3,kernel_size=3,stride=1,padding=1)
24
  self.a = nn.Sigmoid()
25
+ self.fc1 = nn.Linear(in_features=1280, out_features=embedding_size)
26
  self.fc2 = nn.Linear(in_features=embedding_size,out_features=1)
27
+ self.relu = nn.ReLU()
28
+ self.backbone.classifier = Identity()
29
 
30
  def forward_once(self, x):
31
 
 
38
 
39
  x1 = self.forward_once(x1)
40
  x2 = self.forward_once(x2)
41
+ out1, out2 = self.relu(self.fc1(x1)), self.relu(self.fc1(x2))
42
+ dis = torch.abs((out1 - out2))
43
+ pred = self.a(self.fc2(dis)).squeeze()
44
+ return pred