Files
pyRBM/cifar_test.ipynb
T

440 KiB

In [308]:
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 rbm.model import Model
from rbm.entity import Entity, EntityParams, TrainingParams
from rbm.matrix import Mat, np, rms_error_accu
from rbm.torch import Optimizer
import math
In [309]:
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))                            
])                              
In [310]:
train_data = torchvision.datasets.CIFAR10(root='./data', train=True, transform=transform, download=True)
test_data = torchvision.datasets.CIFAR10(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 [311]:
image, label = train_data[0]
In [312]:
print(label)
6
In [313]:
print(train_data)
Dataset CIFAR10
    Number of datapoints: 50000
    Root location: ./data
    Split: Train
    StandardTransform
Transform: Compose(
               ToTensor()
               Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
           )
In [314]:
dim = image.size()
print(dim)
torch.Size([3, 32, 32])
In [ ]:
In [456]:
N = 64
image, label = train_data[0]
size = len(torch.flatten(image))
train_images = np.zeros(shape=(N, size))
train_images_tensor = torch.Tensor(N, size)
#train_labels = np.zeros(shape=(N, size))
for i in range(N):
    image, label = train_data[i]
    flat_array = torch.flatten(image)
    train_images[i, :] = np.asarray(flat_array.numpy())
    train_images_tensor[i, :] = flat_array
#    train_labels[i, :] = torch.flatten(label).numpy()
In [457]:
print(len(train_images))
64
In [458]:
n_side = int(math.sqrt(len(train_images)))
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_images[index]
        img = (1+np.reshape(inp, (3, 32, 32)))/2
        img = img.transpose((1,2,0))
        axes[x,y].imshow(np.asnumpy(img))
        axes[x,y].axis('off')
        index += 1

plt.show()
In [466]:
class TestModel(Model):
	def __init__(self, name: str, work_dir: str = '.'):
		super().__init__(name, work_dir)
#		self.unit1 = Entity((3072, 333), EntityParams(do_gaussian_visible=True, do_gaussian_hidden=True), TrainingParams(learning_rate=0.00001, momentum=0.9, num_epochs=10000, mini_batch_size=100))
		self.unit1 = Entity((3072, 333), EntityParams(do_gaussian_visible=True, do_gaussian_hidden=False), TrainingParams(learning_rate=0.00001, momentum=0.9, num_epochs=10000, mini_batch_size=100))

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

	def backward(self, x: Mat):
		x = self.unit1.reconstruct(x)
		return x
In [467]:
work_dir = "results"
prj_name = "cifar_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_images)
# save state
model.save()
-------------------------------------------
progress              : 0%
err_rms              : 0.7598373690272209
-------------------------------------------
progress              : 10%
err_rms              : 0.2550089071186767
-------------------------------------------
progress              : 20%
err_rms              : 0.23073051327845565
-------------------------------------------
progress              : 30%
err_rms              : 0.20837876799476085
-------------------------------------------
progress              : 40%
err_rms              : 0.18892075853670467
-------------------------------------------
progress              : 50%
err_rms              : 0.1714640103265428
-------------------------------------------
progress              : 60%
err_rms              : 0.1555011448082522
-------------------------------------------
progress              : 70%
err_rms              : 0.14181307545217142
-------------------------------------------
progress              : 80%
err_rms              : 0.1351610598648988
-------------------------------------------
progress              : 90%
err_rms              : 0.15933470100682925
-------------------------------------------
progress              : 100%
err_rms              : 0.22653114964742918
-------------------------------------------
progress              : 100%
err_rms_total              : 0.22644332971112477
results/cifar_test-0-state.npz saved successfully!
In [463]:
n_side = int(math.sqrt(len(train_images)))
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_images[index]
        recon = model.backward(model.forward(inp))
        recon -= np.min(recon)
        recon = recon / np.max(recon) - 0.5
        img = (0.5 + np.reshape(recon, (3, 32, 32)))
        img = img.transpose((1,2,0))
        axes[x,y].imshow(np.asnumpy(img))
        axes[x,y].axis('off')
        index += 1

plt.show()
In [171]:
# torch style
In [172]:
optimizer = Optimizer(model.unit1)
report_interval = 1000
next_ep = report_interval
for epoch in range(6000):
    running_loss = 0.0

    optimizer.zero_grad()
    optimizer.step(train_images)

    if epoch >= next_ep:
        print(f'Training epoch {epoch}...')
        next_ep += report_interval
        # Update final status
        err_rms = rms_error_accu(train_images - model.unit1.reconstruct(model.unit1.forward(train_images)))
        print(err_rms)

model.save()
Training epoch 1000...
0.01986007065482907
Training epoch 2000...
0.019140598876222572
Training epoch 3000...
0.01836005860460779
Training epoch 4000...
0.016391635241554094
Training epoch 5000...
0.016382646045662693
results/cifar_test-0-state.npz saved successfully!
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: