Jacobellis Dan (dgj335) commited on
Commit
de00cee
·
1 Parent(s): 1a0f97c
README.ipynb CHANGED
@@ -53,7 +53,7 @@
53
  "from PIL import Image\n",
54
  "from IPython.display import display\n",
55
  "from torchvision.transforms import ToPILImage, PILToTensor\n",
56
- "from walloc.walloc import Walloc\n",
57
  "class Args: pass"
58
  ]
59
  },
@@ -77,7 +77,7 @@
77
  "device = \"cpu\"\n",
78
  "checkpoint = torch.load(\"v0.6.3_ext.pth\",map_location=\"cpu\")\n",
79
  "args = checkpoint['args']\n",
80
- "codec = Walloc(\n",
81
  " channels = args.channels,\n",
82
  " J = args.J,\n",
83
  " N = args.N,\n",
@@ -258,65 +258,6 @@
258
  {
259
  "cell_type": "code",
260
  "execution_count": 8,
261
- "id": "563fb76e-404f-4a99-b99a-b2f2496fe614",
262
- "metadata": {},
263
- "outputs": [],
264
- "source": [
265
- "def concatenate_channels(x):\n",
266
- " batch_size, N, h, w = x.shape\n",
267
- " n = int(N**0.5)\n",
268
- " if n*n != N:\n",
269
- " raise ValueError(\"Number of channels must be a perfect square.\")\n",
270
- " \n",
271
- " x = x.view(batch_size, n, n, h, w)\n",
272
- " x = x.permute(0, 1, 3, 2, 4).contiguous()\n",
273
- " x = x.view(batch_size, 1, n*h, n*w)\n",
274
- " return x\n",
275
- "\n",
276
- "def split_channels(x, N):\n",
277
- " batch_size, _, H, W = x.shape\n",
278
- " n = int(N**0.5)\n",
279
- " h = H // n\n",
280
- " w = W // n\n",
281
- " \n",
282
- " x = x.view(batch_size, n, h, n, w)\n",
283
- " x = x.permute(0, 1, 3, 2, 4).contiguous()\n",
284
- " x = x.view(batch_size, N, h, w)\n",
285
- " return x\n",
286
- "\n",
287
- "def to_bytes(x, n_bits):\n",
288
- " max_value = 2**(n_bits - 1) - 1\n",
289
- " min_value = -max_value - 1\n",
290
- " if x.min() < min_value or x.max() > max_value:\n",
291
- " raise ValueError(f\"Tensor values should be in the range [{min_value}, {max_value}].\")\n",
292
- " return (x + (max_value + 1)).to(torch.uint8)\n",
293
- "\n",
294
- "def from_bytes(x, n_bits):\n",
295
- " max_value = 2**(n_bits - 1) - 1\n",
296
- " return (x.to(torch.float32) - (max_value + 1))\n",
297
- "\n",
298
- "def latent_to_pil(latent, n_bits):\n",
299
- " latent_bytes = to_bytes(latent, n_bits)\n",
300
- " concatenated_latent = concatenate_channels(latent_bytes)\n",
301
- " \n",
302
- " pil_images = []\n",
303
- " for i in range(concatenated_latent.shape[0]):\n",
304
- " pil_image = Image.fromarray(concatenated_latent[i][0].numpy(), mode='L')\n",
305
- " pil_images.append(pil_image)\n",
306
- " \n",
307
- " return pil_images\n",
308
- "\n",
309
- "def pil_to_latent(pil_images, N, n_bits):\n",
310
- " tensor_images = [PILToTensor()(img).unsqueeze(0) for img in pil_images]\n",
311
- " tensor_images = torch.cat(tensor_images, dim=0)\n",
312
- " split_latent = split_channels(tensor_images, N)\n",
313
- " latent = from_bytes(split_latent, n_bits)\n",
314
- " return latent"
315
- ]
316
- },
317
- {
318
- "cell_type": "code",
319
- "execution_count": 9,
320
  "id": "60fc51ff-cb67-45ec-929d-d35af77dc3fb",
321
  "metadata": {},
322
  "outputs": [
@@ -328,19 +269,19 @@
328
  "<PIL.Image.Image image mode=L size=384x256>"
329
  ]
330
  },
331
- "execution_count": 9,
332
  "metadata": {},
333
  "output_type": "execute_result"
334
  }
335
  ],
336
  "source": [
337
- "Y_pil = latent_to_pil(Y,5)\n",
338
  "Y_pil[0]"
339
  ]
340
  },
341
  {
342
  "cell_type": "code",
343
- "execution_count": 10,
344
  "id": "b027bdc2-f843-40a4-b20f-3c30fa13a3d8",
345
  "metadata": {},
346
  "outputs": [
@@ -359,7 +300,7 @@
359
  },
360
  {
361
  "cell_type": "code",
362
- "execution_count": 11,
363
  "id": "ed1fadec-ba42-4cc0-9a4c-2161b23de5a9",
364
  "metadata": {},
365
  "outputs": [
@@ -369,36 +310,35 @@
369
  "tensor(1.)"
370
  ]
371
  },
372
- "execution_count": 11,
373
  "metadata": {},
374
  "output_type": "execute_result"
375
  }
376
  ],
377
  "source": [
378
- "Y2 = pil_to_latent(Y_pil, 16, 5)\n",
379
  "(Y == Y2).sum()/Y.numel()"
380
  ]
381
  },
382
  {
383
  "cell_type": "code",
384
- "execution_count": 13,
385
  "id": "0ae9e5bb-d616-4d22-a9c9-6743e4216802",
386
  "metadata": {},
387
- "outputs": [
388
- {
389
- "name": "stdout",
390
- "output_type": "stream",
391
- "text": [
392
- "[NbConvertApp] Converting notebook README.ipynb to markdown\n",
393
- "[NbConvertApp] Support files will be in README_files/\n",
394
- "[NbConvertApp] Making directory README_files\n",
395
- "[NbConvertApp] Writing 5751 bytes to README.md\n"
396
- ]
397
- }
398
- ],
399
  "source": [
400
  "!jupyter nbconvert --to markdown README.ipynb"
401
  ]
 
 
 
 
 
 
 
 
 
 
402
  }
403
  ],
404
  "metadata": {
 
53
  "from PIL import Image\n",
54
  "from IPython.display import display\n",
55
  "from torchvision.transforms import ToPILImage, PILToTensor\n",
56
+ "from walloc import walloc\n",
57
  "class Args: pass"
58
  ]
59
  },
 
77
  "device = \"cpu\"\n",
78
  "checkpoint = torch.load(\"v0.6.3_ext.pth\",map_location=\"cpu\")\n",
79
  "args = checkpoint['args']\n",
80
+ "codec = walloc.Walloc(\n",
81
  " channels = args.channels,\n",
82
  " J = args.J,\n",
83
  " N = args.N,\n",
 
258
  {
259
  "cell_type": "code",
260
  "execution_count": 8,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  "id": "60fc51ff-cb67-45ec-929d-d35af77dc3fb",
262
  "metadata": {},
263
  "outputs": [
 
269
  "<PIL.Image.Image image mode=L size=384x256>"
270
  ]
271
  },
272
+ "execution_count": 8,
273
  "metadata": {},
274
  "output_type": "execute_result"
275
  }
276
  ],
277
  "source": [
278
+ "Y_pil = walloc.latent_to_pil(Y,5)\n",
279
  "Y_pil[0]"
280
  ]
281
  },
282
  {
283
  "cell_type": "code",
284
+ "execution_count": 9,
285
  "id": "b027bdc2-f843-40a4-b20f-3c30fa13a3d8",
286
  "metadata": {},
287
  "outputs": [
 
300
  },
301
  {
302
  "cell_type": "code",
303
+ "execution_count": 10,
304
  "id": "ed1fadec-ba42-4cc0-9a4c-2161b23de5a9",
305
  "metadata": {},
306
  "outputs": [
 
310
  "tensor(1.)"
311
  ]
312
  },
313
+ "execution_count": 10,
314
  "metadata": {},
315
  "output_type": "execute_result"
316
  }
317
  ],
318
  "source": [
319
+ "Y2 = walloc.pil_to_latent(Y_pil, 16, 5)\n",
320
  "(Y == Y2).sum()/Y.numel()"
321
  ]
322
  },
323
  {
324
  "cell_type": "code",
325
+ "execution_count": null,
326
  "id": "0ae9e5bb-d616-4d22-a9c9-6743e4216802",
327
  "metadata": {},
328
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
329
  "source": [
330
  "!jupyter nbconvert --to markdown README.ipynb"
331
  ]
332
+ },
333
+ {
334
+ "cell_type": "code",
335
+ "execution_count": null,
336
+ "id": "3d4794d5-a26a-4789-804e-7aa543db89d6",
337
+ "metadata": {},
338
+ "outputs": [],
339
+ "source": [
340
+ "!sed -i 's|!\\[png](README_files/\\(README_[0-9]*_[0-9]*\\.png\\))|![png](https://huggingface.co/danjacobellis/walloc/resolve/main/README_files/\\1)|g' README.md"
341
+ ]
342
  }
343
  ],
344
  "metadata": {
README.md CHANGED
@@ -33,7 +33,7 @@ import numpy as np
33
  from PIL import Image
34
  from IPython.display import display
35
  from torchvision.transforms import ToPILImage, PILToTensor
36
- from walloc.walloc import Walloc
37
  class Args: pass
38
  ```
39
 
@@ -46,7 +46,7 @@ class Args: pass
46
  device = "cpu"
47
  checkpoint = torch.load("v0.6.3_ext.pth",map_location="cpu")
48
  args = checkpoint['args']
49
- codec = Walloc(
50
  channels = args.channels,
51
  J = args.J,
52
  N = args.N,
@@ -71,7 +71,7 @@ img
71
 
72
 
73
 
74
- ![png](README_files/README_6_0.png)
75
 
76
 
77
 
@@ -96,7 +96,7 @@ ToPILImage()(x_hat[0]+0.5)
96
 
97
 
98
 
99
- ![png](README_files/README_8_0.png)
100
 
101
 
102
 
@@ -147,7 +147,7 @@ plt.xticks(range(-15,16,5));
147
 
148
 
149
 
150
- ![png](README_files/README_12_0.png)
151
 
152
 
153
 
@@ -155,61 +155,7 @@ plt.xticks(range(-15,16,5));
155
 
156
 
157
  ```python
158
- def concatenate_channels(x):
159
- batch_size, N, h, w = x.shape
160
- n = int(N**0.5)
161
- if n*n != N:
162
- raise ValueError("Number of channels must be a perfect square.")
163
-
164
- x = x.view(batch_size, n, n, h, w)
165
- x = x.permute(0, 1, 3, 2, 4).contiguous()
166
- x = x.view(batch_size, 1, n*h, n*w)
167
- return x
168
-
169
- def split_channels(x, N):
170
- batch_size, _, H, W = x.shape
171
- n = int(N**0.5)
172
- h = H // n
173
- w = W // n
174
-
175
- x = x.view(batch_size, n, h, n, w)
176
- x = x.permute(0, 1, 3, 2, 4).contiguous()
177
- x = x.view(batch_size, N, h, w)
178
- return x
179
-
180
- def to_bytes(x, n_bits):
181
- max_value = 2**(n_bits - 1) - 1
182
- min_value = -max_value - 1
183
- if x.min() < min_value or x.max() > max_value:
184
- raise ValueError(f"Tensor values should be in the range [{min_value}, {max_value}].")
185
- return (x + (max_value + 1)).to(torch.uint8)
186
-
187
- def from_bytes(x, n_bits):
188
- max_value = 2**(n_bits - 1) - 1
189
- return (x.to(torch.float32) - (max_value + 1))
190
-
191
- def latent_to_pil(latent, n_bits):
192
- latent_bytes = to_bytes(latent, n_bits)
193
- concatenated_latent = concatenate_channels(latent_bytes)
194
-
195
- pil_images = []
196
- for i in range(concatenated_latent.shape[0]):
197
- pil_image = Image.fromarray(concatenated_latent[i][0].numpy(), mode='L')
198
- pil_images.append(pil_image)
199
-
200
- return pil_images
201
-
202
- def pil_to_latent(pil_images, N, n_bits):
203
- tensor_images = [PILToTensor()(img).unsqueeze(0) for img in pil_images]
204
- tensor_images = torch.cat(tensor_images, dim=0)
205
- split_latent = split_channels(tensor_images, N)
206
- latent = from_bytes(split_latent, n_bits)
207
- return latent
208
- ```
209
-
210
-
211
- ```python
212
- Y_pil = latent_to_pil(Y,5)
213
  Y_pil[0]
214
  ```
215
 
@@ -217,7 +163,7 @@ Y_pil[0]
217
 
218
 
219
 
220
- ![png](README_files/README_15_0.png)
221
 
222
 
223
 
@@ -233,7 +179,7 @@ print("compression_ratio: ", x.numel()/os.path.getsize("latent.png"))
233
 
234
 
235
  ```python
236
- Y2 = pil_to_latent(Y_pil, 16, 5)
237
  (Y == Y2).sum()/Y.numel()
238
  ```
239
 
@@ -249,7 +195,7 @@ Y2 = pil_to_latent(Y_pil, 16, 5)
249
  !jupyter nbconvert --to markdown README.ipynb
250
  ```
251
 
252
- [NbConvertApp] Converting notebook README.ipynb to markdown
253
- [NbConvertApp] Support files will be in README_files/
254
- [NbConvertApp] Writing 5751 bytes to README.md
255
 
 
 
 
 
33
  from PIL import Image
34
  from IPython.display import display
35
  from torchvision.transforms import ToPILImage, PILToTensor
36
+ from walloc import walloc
37
  class Args: pass
38
  ```
39
 
 
46
  device = "cpu"
47
  checkpoint = torch.load("v0.6.3_ext.pth",map_location="cpu")
48
  args = checkpoint['args']
49
+ codec = walloc.Walloc(
50
  channels = args.channels,
51
  J = args.J,
52
  N = args.N,
 
71
 
72
 
73
 
74
+ ![png](https://huggingface.co/danjacobellis/walloc/resolve/main/README_files/README_6_0.png)
75
 
76
 
77
 
 
96
 
97
 
98
 
99
+ ![png](https://huggingface.co/danjacobellis/walloc/resolve/main/README_files/README_8_0.png)
100
 
101
 
102
 
 
147
 
148
 
149
 
150
+ ![png](https://huggingface.co/danjacobellis/walloc/resolve/main/README_files/README_12_0.png)
151
 
152
 
153
 
 
155
 
156
 
157
  ```python
158
+ Y_pil = walloc.latent_to_pil(Y,5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  Y_pil[0]
160
  ```
161
 
 
163
 
164
 
165
 
166
+ ![png](https://huggingface.co/danjacobellis/walloc/resolve/main/README_files/README_14_0.png)
167
 
168
 
169
 
 
179
 
180
 
181
  ```python
182
+ Y2 = walloc.pil_to_latent(Y_pil, 16, 5)
183
  (Y == Y2).sum()/Y.numel()
184
  ```
185
 
 
195
  !jupyter nbconvert --to markdown README.ipynb
196
  ```
197
 
 
 
 
198
 
199
+ ```python
200
+ !sed -i 's|!\[png](README_files/\(README_[0-9]*_[0-9]*\.png\))|![png](https://huggingface.co/danjacobellis/walloc/resolve/main/README_files/\1)|g' README.md
201
+ ```
README_files/README_14_0.jpg ADDED
README_files/README_14_0.png ADDED