<jupyter_start><jupyter_text>a = torch.zeros([64, 100])b = torch.zeros([64, 10])torch.cat([a,b], dim=-1).shape<jupyter_code>train_err, test_err = model.train(train_loader, epochs=epochs, test_data=test_loader, loss_threshold=loss_threshold)
plt.plot(train_err)
plt.plot(test_err)
plt.show()
ex = model.encode(test_loader)
# ex = ex[:10,:, :1,:]
ex[0].shape, ex[1].shape<jupyter_output><empty_output><jupyter_text>latent_vecs = []model.model.eval()with torch.no_grad():    for data, _ in (test_loader):        data = data.to(device)        mu, logvar = model.model.encode(data)        latent = torch.stack([mu, logvar]).permute(1,0,2)        latent_vecs.append(latent)ex = torch.cat(latent_vecs)<jupyter_code>dx = model.decode(ex[0], ex[1]).cpu().numpy()
dx.shape
for data, _ in test_loader:
    test_images = data.cpu().numpy()
    break
test_images.shape
rows = 5
cols = 4
fig = plt.figure(figsize=[20,20])
for n in range(rows):
    fig.add_subplot(rows, cols, cols*n+1)
    plt.axis('off')
    plt.imshow(test_images[n, 0], cmap='gray')
    fig.add_subplot(rows, cols, cols*n+2)
    plt.axis('off')
    img = dx[n, 0]
    plt.imshow(img, cmap='gray')
    
    fig.add_subplot(rows, cols, cols*n+3)
    plt.axis('off')
    
    t, img = cv2.threshold(img,0.4,1,cv2.THRESH_BINARY)
        
    surr = 0.07
    slf = 0.44
    kernel = np.array([[surr,surr,surr], [surr,slf,surr], [surr,surr,surr]])
    img = cv2.filter2D(img, -1, kernel)
    
#     kernel = np.ones((1, 1), np.uint8)
#     img = cv2.erode(img, kernel, iterations=10)
#     img = cv2.dilate(img, kernel, iterations=1)

    plt.imshow(img, cmap='gray')
    
    img = dx[n, 0]
    fig.add_subplot(rows, cols, cols*n+4)
    plt.axis('off')
    
    surr = -0.1
    slf = 1.8
    kernel = np.array([[surr,surr,surr], [surr,slf,surr], [surr,surr,surr]])
    img = cv2.filter2D(img, -1, kernel)
    plt.imshow(img, cmap='gray')

plt.show()
for name, param in model.model.named_parameters():
#     print(name)
    if name == "lastconv.weight" or name == "lastconv.bias":
        print(param)
lb = torch.zeros([25, 10])
for i in range(25):
    label = i%10
    lb[i, label] = 1
lb = lb.to(device)
z = torch.randn([25, 2, 100])
dr = model.decode(z, lb).cpu()
w=10
h=10
columns = 5
rows = 5

print("generated:")
fig=plt.figure(figsize=(8, 8))
for i in range(1, columns*rows +1):
    img = dr[i-1, 0].reshape([28,28])
    fig.add_subplot(rows, columns, i)
    plt.imshow(img, cmap='gray')
    plt.axis('off')
# plt.savefig("test.png", bbox_inches='tight')
plt.show()

print("test dataset:")
fig=plt.figure(figsize=(8, 8))
for i in range(1, columns*rows +1):
    img = test_images[i, 0]
    fig.add_subplot(rows, columns, i)
    plt.imshow(img, cmap='gray')
    plt.axis('off')
# plt.savefig("test.png", bbox_inches='tight')
plt.show()

print("generated and corrected:")
fig=plt.figure(figsize=(8, 8))
for i in range(1, columns*rows +1):
    img = dr[i-1, 0].cpu().numpy().reshape([28,28])
    t, img = cv2.threshold(img,0.5,1,cv2.THRESH_BINARY)
    surr = 0.07
    slf = 0.44
    kernel = np.array([[surr,surr,surr], [surr,slf,surr], [surr,surr,surr]])
    img = cv2.filter2D(img, -1, kernel)
    fig.add_subplot(rows, columns, i)
    plt.imshow(img, cmap='gray')
    plt.axis('off')
# plt.savefig("test.png", bbox_inches='tight')
plt.show()
from torchvision.utils import save_image

save_image(dr[:25], "vae.png", nrow=5, normalize=True)
def sharpen(pixel, slope=1.05):
    assert slope >= 1
    c = (1 - slope) / 2
    return min(max(slope*pixel + c, 0), 1)
print("generated:")
fig=plt.figure(figsize=(8, 8))
for i in range(1, columns*rows +1):
    img = dr[i-1, 0].reshape([28,28])
    img = img.cpu().numpy()
    fig.add_subplot(rows, columns, i)
    plt.imshow(img, cmap='gray')
    plt.axis('off')
# plt.savefig("test.png", bbox_inches='tight')
plt.show()

print("generated and corrected:")
fig=plt.figure(figsize=(8, 8))
for i in range(1, columns*rows +1):
    img = dr[i-1, 0].cpu().numpy().reshape([28,28])
    img = np.vectorize(sharpen)(img)
    fig.add_subplot(rows, columns, i)
    plt.imshow(img, cmap='gray')
    plt.axis('off')
# plt.savefig("test.png", bbox_inches='tight')
plt.show()<jupyter_output>generated:
{'fraction_non_alphanumeric': 0.19507530480516375, 'fraction_numerical': 0.03657661965096821, 'mean_word_length': 3.9223529411764706, 'pattern_counts': {'":': 1, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '85847', 'license': 'MIT', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1755, 'n_words': 321, 'path': 'notebooks/vae.ipynb', 'repo_name': 'parthjshah95/mnist-generator', 'size': '4183'}