[refactor] SubImage → SubImageExtract, add SubImageCombine
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
"from rbm.entity import Entity, EntityParams, TrainingParams\n",
|
||||
"from rbm.matrix import Mat, np, rms_error_accu, sample_gaussian\n",
|
||||
"from rbm.torch import Optimizer\n",
|
||||
"from image.sub_image import SubImage, normalize\n",
|
||||
"from image.sub_image import SubImageExtract, SubImageExtractCombine, normalize\n",
|
||||
"import math\n",
|
||||
"import random"
|
||||
],
|
||||
@@ -216,7 +216,7 @@
|
||||
"mean_training_batch = np.reshape(np.repeat(np.mean(train_images, axis=1), 3072, axis=0), train_images.shape)\n",
|
||||
"var_training_batch = np.reshape(np.repeat(np.std(train_images, axis=1), 3072, axis=0), train_images.shape)\n",
|
||||
"train_images_norm = (train_images - mean_training_batch) / var_training_batch\n",
|
||||
"sub_generator = SubImage(PATCH, PATCH, STRIDE, STRIDE)\n",
|
||||
"sub_generator = SubImageExtract(PATCH, PATCH, STRIDE, STRIDE)\n",
|
||||
"train_subimages = sub_generator(np.reshape(train_images, (N, 3, 32, 32)), 3)\n",
|
||||
"print(train_subimages.shape)\n",
|
||||
"train_subimages = np.reshape(train_subimages, (train_subimages.shape[0], N_VIS))\n",
|
||||
@@ -387,12 +387,10 @@
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"import numpy\n",
|
||||
"\n",
|
||||
"# Reconstruct full 32x32 images by assembling reconstructed sub-images (overlap-averaged)\n",
|
||||
"combiner = SubImageExtractCombine(PATCH, PATCH, STRIDE, STRIDE, 32, 32, n_ch=3)\n",
|
||||
"n_images = n_side\n",
|
||||
"nx_steps = (32 - PATCH) // STRIDE + 1\n",
|
||||
"ny_steps = (32 - PATCH) // STRIDE + 1\n",
|
||||
"n_images = n_side\n",
|
||||
"\n",
|
||||
"fig, axes = plt.subplots(n_images, 2, figsize=(6, n_images * 3))\n",
|
||||
"\n",
|
||||
@@ -400,21 +398,10 @@
|
||||
" orig = np.reshape(train_images[img_idx], (3, 32, 32))\n",
|
||||
" orig_display = np.asnumpy((orig - np.min(orig)) / (np.max(orig) - np.min(orig)))\n",
|
||||
"\n",
|
||||
" accum = numpy.zeros((3, 32, 32), dtype=numpy.float64)\n",
|
||||
" count = numpy.zeros((1, 32, 32), dtype=numpy.float64)\n",
|
||||
" for ix in range(nx_steps):\n",
|
||||
" for iy in range(ny_steps):\n",
|
||||
" sub_idx = img_idx * nx_steps * ny_steps + ix * ny_steps + iy\n",
|
||||
" inp = train_subimages[sub_idx]\n",
|
||||
" recon = model.backward(model.forward(inp))\n",
|
||||
" arr = np.asnumpy(np.reshape(recon, (3, PATCH, PATCH)))\n",
|
||||
" x0, y0 = ix * STRIDE, iy * STRIDE\n",
|
||||
" accum[:, x0:x0+PATCH, y0:y0+PATCH] += arr\n",
|
||||
" count[0, x0:x0+PATCH, y0:y0+PATCH] += 1.0\n",
|
||||
"\n",
|
||||
" recon_full = (accum / numpy.maximum(count, 1)).transpose(1, 2, 0)\n",
|
||||
" lo, hi = recon_full.min(), recon_full.max()\n",
|
||||
" recon_full = numpy.clip((recon_full - lo) / (hi - lo + 1e-8), 0, 1)\n",
|
||||
" start = img_idx * nx_steps * ny_steps\n",
|
||||
" patches = train_subimages[start : start + nx_steps * ny_steps]\n",
|
||||
" recon_patches = np.array([model.backward(model.forward(p)) for p in patches])\n",
|
||||
" recon_full = combiner(recon_patches)\n",
|
||||
"\n",
|
||||
" axes[img_idx, 0].imshow(orig_display.transpose(1, 2, 0))\n",
|
||||
" axes[img_idx, 0].axis('off')\n",
|
||||
|
||||
Reference in New Issue
Block a user