Files
pyRBM/mnist_test_sub_image.ipynb
T
2026-06-02 08:55:16 +02:00

172 KiB

In [1]:
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from model.model import Model
from rbm.entity import Entity, EntityParams, TrainingParams
from rbm.matrix import Mat, np, rms_error_accu
from compat.torch import Optimizer
from image.sub_image import SubImageExtract
import math
In [2]:
transform = transforms.Compose([
    transforms.ToTensor()
])                              
In [3]:
train_data = torchvision.datasets.MNIST(root='./data', train=True, transform=transform, download=True)
test_data = torchvision.datasets.MNIST(root='./data', train=False, transform=transform, download=True)

train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True, num_workers=2)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=32, shuffle=True, num_workers=2)
In [4]:
print(len(train_data))
60000
In [5]:
print(list(train_data[1][0].size()[1:3]))
[28, 28]
In [6]:
N = 600
size = 784
n_side = 10
train_images = np.zeros(shape=(N,size))
for i in range(N):
    image = train_data[i][0]
    flat_array = torch.flatten(image)
    train_images[i, :] = np.asarray(flat_array.numpy())
In [7]:
print(train_images.shape)
image_indices = np.random.randint(0, N-1, n_side*n_side)
sub_generator = SubImageExtract(8,8,4,4)
train_subimages = sub_generator(np.reshape(train_images, (N, 28,28)), 1)
print(train_subimages.shape)
train_subimages = np.reshape(train_subimages, (train_subimages.shape[0], 8*8))
print(train_subimages.shape)

(600, 784)
(21600, 1, 8, 8)
(21600, 64)
In [8]:
fig, axes = plt.subplots(n_side, n_side, figsize=(30,30))
index = 0             
for x in range(n_side):
    for y in range(n_side):
        inp = train_subimages[image_indices[index]]
        img = np.reshape(inp, (8, 8))
        axes[x,y].imshow(np.asnumpy(img))
        axes[x,y].axis('off')
        index += 1

plt.show()
In [9]:
class TestModel(Model):
	def __init__(self, name: str, work_dir: str = '.'):
		super().__init__(name, work_dir)
		self.unit1 = Entity((8*8, 100), EntityParams(do_gaussian_visible=False, do_gaussian_hidden=False), TrainingParams(learning_rate=0.1, momentum=0.9, num_epochs=1000, mini_batch_size=1000, l1_lambda=0.04, do_rao_blackwell=True), True)
#		self.unit2 = Entity((500, 64), EntityParams(do_gaussian_visible=False, do_gaussian_hidden=False), TrainingParams(learning_rate=0.04, momentum=0.9, num_epochs=1000, mini_batch_size=100, l2_lambda=0.01, do_rao_blackwell=True), True)

	def forward(self, x: Mat):
		x = self.unit1.forward(x)
#		x = self.unit2.forward(x)
		return x

	def backward(self, x: Mat):
#		x = self.unit2.reconstruct(x)
		x = self.unit1.reconstruct(x)
		return x
In [10]:
work_dir = "results"
prj_name = "mnist_test"
prj_root = "/home/jens/work/repos/Rbm"

# Create model
model = TestModel(prj_name, "results")

# Init state
model.init(0.01)

# load state
model.load()

# Train
model.train(train_subimages)

# save state
model.save()
-------------------------------------------
Entity-64x100: progress              : 0%
Entity-64x100: err_rms              : 0.024593278436815722
Entity-64x100: l2_norm              : 2.1721602103616884
-------------------------------------------
Entity-64x100: progress              : 10%
Entity-64x100: err_rms              : 0.0024668303211456636
Entity-64x100: l2_norm              : 15.108631041174913
-------------------------------------------
Entity-64x100: progress              : 20%
Entity-64x100: err_rms              : 0.0019642582244151804
Entity-64x100: l2_norm              : 17.682007788367823
-------------------------------------------
Entity-64x100: progress              : 30%
Entity-64x100: err_rms              : 0.0018990980869732475
Entity-64x100: l2_norm              : 18.254273597749567
-------------------------------------------
Entity-64x100: progress              : 40%
Entity-64x100: err_rms              : 0.0018084020821888338
Entity-64x100: l2_norm              : 18.578546067444886
-------------------------------------------
Entity-64x100: progress              : 50%
Entity-64x100: err_rms              : 0.0017997921578530268
Entity-64x100: l2_norm              : 18.708539116146536
-------------------------------------------
Entity-64x100: progress              : 60%
Entity-64x100: err_rms              : 0.0017720193133826786
Entity-64x100: l2_norm              : 18.719519836176545
-------------------------------------------
Entity-64x100: progress              : 70%
Entity-64x100: err_rms              : 0.0018343015627015383
Entity-64x100: l2_norm              : 18.74884148274389
-------------------------------------------
Entity-64x100: progress              : 80%
Entity-64x100: err_rms              : 0.00177616642566865
Entity-64x100: l2_norm              : 18.800847250979384
-------------------------------------------
Entity-64x100: progress              : 90%
Entity-64x100: err_rms              : 0.001816204815664636
Entity-64x100: l2_norm              : 18.8517263479615
-------------------------------------------
Entity-64x100: progress              : 100%
Entity-64x100: err_rms              : 0.00179711377602289
Entity-64x100: l2_norm              : 18.90726806055687
-------------------------------------------
Entity-64x100: progress              : 100%
Entity-64x100: err_rms_total              : 0.001800342915790187
Entity-64x100: l2_norm              : 18.912132328950666
In [11]:
# Plot reconstructions
fig, axes = plt.subplots(n_side, n_side, figsize=(30,30))
index = 0             
for x in range(n_side):
    for y in range(n_side):
        inp = train_subimages[image_indices[index]]
        recon = model.backward(model.forward(inp))
        recon -= np.min(recon)
        recon = recon / np.max(recon)
        img = np.reshape(recon, (8, 8))
        axes[x,y].imshow(np.asnumpy(img))
        axes[x,y].axis('off')
        index += 1

plt.show()
In [12]:
# Plot weights
weights = model.unit1.state.w_hv
n, n_hid = weights.shape
weight_indices = np.random.randint(0, N-1, n_hid)
w = np.reshape(weights, (8, 8, n_hid))
print(w.shape)
fig, axes = plt.subplots(n_side, n_side, figsize=(30,30))

index = 0
for x in range(n_side):
    for y in range(n_side):
        inp = np.asnumpy(w[:,:, weight_indices[index]])
        axes[x,y].imshow(inp)
        axes[x,y].axis('off')
        index += 1

plt.show()
(8, 8, 100)
In [12]:
In [12]: