# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:neon]
#     language: python
#     name: conda-env-neon-py
# ---

import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

dat = np.load("tiny_data.npz")

arr = dat.items()[0][1][:200, :]

plt.imshow(arr[2].reshape((25, 25, 25))[:, 12, :])

import os
from datetime import datetime
from neon.callbacks.callbacks import Callbacks, GANCostCallback, LossCallback
#from neon.callbacks.plotting_callbacks import GANPlotCallback
from neon.initializers import Gaussian
from neon.layers import GeneralizedGANCost, Affine, Sequential, Conv, Deconv, Dropout, Pooling, BatchNorm
from neon.layers.layer import Linear, Reshape
from neon.layers.container import GenerativeAdversarial
from neon.models.model import GAN, Model
from neon.transforms import Rectlin, Logistic, GANCost, Tanh
from neon.util.argparser import NeonArgparser
from neon.util.persist import ensure_dirs_exist
from neon.layers.layer import Dropout
from neon.data.dataiterator import ArrayIterator
from neon.optimizers import GradientDescentMomentum, RMSProp
from neon.backends import gen_backend
import numpy as np
from sklearn.cross_validation import train_test_split

X = arr.copy()
y = np.ones(arr.shape[0])


mean = np.mean(X, axis=0, keepdims=True) 
X-=mean
max_elem = np.max(np.abs(X))

X = (X)/max_elem

np.max(X), np.min(X)

plt.plot(X[20].reshape((25, 25, 25))[12, 12, :])

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.9, random_state=42)
print(X_train.shape, 'X train shape')
print(y_train.shape, 'y train shape')

gen_backend(backend='cpu', batch_size=10)
train_set = ArrayIterator(X=X_train, y=y_train, nclass=2, lshape=(1, 25, 25, 25))
valid_set = ArrayIterator(X=X_test, y=y_test, nclass=2)

# +
# setup weight initialization function
init = Gaussian(scale=0.01)

# discriminiator using convolution layers
lrelu = Rectlin(slope=0.1)  # leaky relu for discriminator
# sigmoid = Logistic() # sigmoid activation function
conv1 = dict(init=init, batch_norm=False, activation=lrelu, padding=2, bias=init) # what's about BatchNorm Layer and batch_norm parameter?
conv2 = dict(init=init, batch_norm=False, activation=lrelu, padding=2, bias=init)
conv3 = dict(init=init, batch_norm=False, activation=lrelu, padding=2, bias=init)
D_layers = [
            Conv((5, 5, 5, 16), **conv1),
            Conv((5, 5, 5, 16), **conv2),
            Pooling((2, 2, 2), strides=2),
            Conv((5, 5, 5, 16), **conv1),
            Conv((5, 5, 5, 16), **conv2),
            Pooling((2, 2, 2), strides=2),
            Affine(1024, init=init, activation=lrelu),
            Affine(1, init=init, activation=Logistic())
            ]

# generator using convolution layers
init_gen = Gaussian(scale=0.01)
relu = Rectlin(slope=0)  # relu for generator
pad1 = dict(pad_h=0, pad_w=0, pad_d=0)
str1 = dict(str_h=1, str_w=1, str_d=1)
conv1 = dict(init=init_gen, batch_norm=False, activation=relu, padding=pad1, strides=str1, bias=init_gen)
pad2 = dict(pad_h=0, pad_w=0, pad_d=0)
str2 = dict(str_h=2, str_w=2, str_d=2)
conv2 = dict(init=init_gen, batch_norm=False, activation=relu, padding=pad2, strides=str2, bias=init_gen)
pad3 = dict(pad_h=0, pad_w=0, pad_d=0)
str3 = dict(str_h=1, str_w=1, str_d=1)
conv3 = dict(init=init_gen, batch_norm=False, activation=Tanh(), padding=pad3, strides=str3, bias=init_gen)
G_layers = [
            Affine(1024, init=init_gen, bias=init_gen, activation=relu),
            BatchNorm(),
            Affine(8 * 9 * 9 * 9, init=init_gen, bias=init_gen, activation=relu),
            BatchNorm(),
            Reshape((8, 9, 9, 9)),
            Deconv((5, 5, 5, 16), **conv1), #14x14x14
            BatchNorm(),
            Deconv((5, 5, 5, 16), **conv1), #14x14x14
            BatchNorm(),
            Deconv((5, 5, 5, 16), **conv1), #14x14x14
            BatchNorm(),
            Deconv((5, 5, 5, 1), **conv3), #27x27x27
            ]

layers = GenerativeAdversarial(generator=Sequential(G_layers, name="Generator"),
                               discriminator=Sequential(D_layers, name="Discriminator"))

# setup optimizer
optimizer = RMSProp(learning_rate=1e-2, decay_rate=0.9, epsilon=1e-8)
#optimizer = GradientDescentMomentum(learning_rate=1e-3, momentum_coef = 0.9)

# setup cost function as Binary CrossEntropy
cost = GeneralizedGANCost(costfunc=GANCost(func="wasserstein"))

nb_epochs = 13
latent_size = 200
inb_classes = 2
nb_test = 100

# initialize model
noise_dim = (latent_size)
gan = GAN(layers=layers, noise_dim=noise_dim, k=5)

# configure callbacks
callbacks = Callbacks(gan, eval_set=valid_set)
callbacks.add_callback(GANCostCallback())
#callbacks.add_save_best_state_callback("./best_state.pkl")

# run fit
gan.fit(train_set, num_epochs=nb_epochs, optimizer=optimizer,
        cost=cost, callbacks=callbacks)
# -

x_new = np.random.standard_normal((64, latent_size))
inference_set = ArrayIterator(x_new, None, nclass=2, lshape=(latent_size))
my_generator = Model(gan.layers.generator)

# +
test = my_generator.get_outputs(inference_set)

test = test * max_elem 
test += mean
test =  test.reshape((64, 25, 25, 25))
# -

plt.imshow(test[0, 12, :, :])

plt.imshow(test[12, :, 12, :])

test[0, :, 12, :]

for i in range(10):
    plt.plot(test[i, 12, :, 12])

for i in range(20):
    plt.plot(arr[i].reshape((25, 25, 25))[12, :, 12])



x_new = np.random.randn(100, latent_size)
inference_set = ArrayIterator(x_new, None, nclass=2, lshape=(latent_size))
my_generator = Model(gan.layers.generator)

test1 = my_generator.get_outputs(inference_set)
test1 = test1 * max_elem +  mean
test1 =  test1.reshape((100, 25, 25, 25))

# Last activation function - Logistic() -test1

plt.imshow(test1[0, :, 12, :])

test1[0, :, 12, :]

np.max(test1)

plt.plot(test1[10, 12, 12, :])

# Last activation function - Tanh() -test

plt.imshow(test[0, :, 12, :])

test[0, :, 12, :]

plt.plot(test[20, 13, 12, :])

plt.imshow(test[20, :, :, 12])

# Last activation function - lrelu()

plt.imshow(test[0, :, 12, :])

test[0, :, 12, :]



StarcoderdataJupyterScriptsDedupFiltered