Skip to content
Snippets Groups Projects
Commit 88016011 authored by Perez Visaires, Jon's avatar Perez Visaires, Jon
Browse files

Archivos de GPU

parent 1d122c70
Branches
No related tags found
No related merge requests found
Showing
with 2140 additions and 0 deletions
File added
File added
File added
File added
File added
File added
# ------------------------------------------------------------------------------------------------------------------- #
from tensorflow.keras.layers import Input, Dropout, Conv2D, Conv2DTranspose, BatchNormalization, Flatten, Activation, Reshape, LeakyReLU
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
# ------------------------------------------------------------------------------------------------------------------- #
def crea_autoencoder_capas(input_shape, feature_multiplier, surface_kernel_size, kernel_size, dropout, init_func):
layer_conv = []
### Conv 1 ###
# Input #
conv1_input_shape = input_shape
conv1_input = Input(shape = conv1_input_shape)
x = conv1_input
# Layer 0 #
x = Conv2D(filters = feature_multiplier * 1,
kernel_size = surface_kernel_size,
strides = 1,
padding = "same",
kernel_initializer = init_func)(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2D(filters = feature_multiplier * 1,
kernel_size = surface_kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 2 #
x = Conv2D(filters = feature_multiplier * 1,
kernel_size = surface_kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
conv1_output = x
convolution_1 = Model(conv1_input, conv1_output)
layer_conv.append(convolution_1)
conv1_output_shape = (convolution_1.output_shape[1],
convolution_1.output_shape[2],
convolution_1.output_shape[3])
### Conv 2 ###
# Input #
conv2_input_shape = conv1_output_shape
conv2_input = Input(shape = conv2_input_shape)
x = conv2_input
# Layer 0 #
x = Conv2D(filters = feature_multiplier * 2,
kernel_size = kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2D(filters = feature_multiplier * 2,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
conv2_output = x
convolution_2 = Model(conv2_input, conv2_output)
layer_conv.append(convolution_2)
conv2_output_shape = (convolution_2.output_shape[1],
convolution_2.output_shape[2],
convolution_2.output_shape[3])
### Conv 3 ###
# Input #
conv3_input_shape = conv2_output_shape
conv3_input = Input(shape = conv3_input_shape)
x = conv3_input
# Layer 0 #
x = Conv2D(filters = feature_multiplier * 4,
kernel_size = kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2D(filters = feature_multiplier * 4,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
conv3_output = x
convolution_3 = Model(conv3_input, conv3_output)
layer_conv.append(convolution_3)
conv3_output_shape = (convolution_3.output_shape[1],
convolution_3.output_shape[2],
convolution_3.output_shape[3])
### Conv 4 ###
# Input #
conv4_input_shape = conv3_output_shape
conv4_input = Input(shape = conv4_input_shape)
x = conv4_input
# Layer 0 #
x = Conv2D(filters = feature_multiplier * 8,
kernel_size = kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2D(filters = feature_multiplier * 8,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
conv4_output = x
convolution_4 = Model(conv4_input, conv4_output)
layer_conv.append(convolution_4)
conv4_output_shape = (convolution_4.output_shape[1],
convolution_4.output_shape[2],
convolution_4.output_shape[3])
### Conv 5 ###
# Input #
conv5_input_shape = conv4_output_shape
conv5_input = Input(shape = conv5_input_shape)
x = conv5_input
# Layer 0 #
x = Conv2D(filters = feature_multiplier * 16,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
conv5_output = x
convolution_5 = Model(conv5_input, conv5_output)
layer_conv.append(convolution_5)
conv5_output_shape = (convolution_5.output_shape[1],
convolution_5.output_shape[2],
convolution_5.output_shape[3])
### Conv 6 ###
# Input #
conv6_input_shape = conv5_output_shape
conv6_input = Input(shape = conv6_input_shape)
x = conv6_input
# Layer 0 #
x = Conv2D(filters = feature_multiplier * 32,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
conv6_output = x
convolution_6 = Model(conv6_input, conv6_output)
layer_conv.append(convolution_6)
conv6_output_shape = (convolution_6.output_shape[1],
convolution_6.output_shape[2],
convolution_6.output_shape[3])
### ------------------------------------------------ ###
layer_deconv = []
### Deconv 6 ###
# Input #
deconv6_input_shape = conv6_output_shape
deconv6_input = Input(shape = deconv6_input_shape)
x = deconv6_input
# Layer 0 #
x = Conv2DTranspose(filters = feature_multiplier * 16,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
deconv6_output = x
deconvolution_6 = Model(deconv6_input, deconv6_output)
layer_deconv.append(deconvolution_6)
### Deconv 5 ###
# Input #
deconv5_input_shape = conv5_output_shape
deconv5_input = Input(shape = deconv5_input_shape)
x = deconv5_input
# Layer 0 #
x = Conv2DTranspose(filters = feature_multiplier * 8,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2DTranspose(filters = feature_multiplier * 8,
kernel_size = kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
deconv5_output = x
deconvolution_5 = Model(deconv5_input, deconv5_output)
layer_deconv.append(deconvolution_5)
### Deconv 4 ###
# Input #
deconv4_input_shape = conv4_output_shape
deconv4_input = Input(shape = deconv4_input_shape)
x = deconv4_input
# Layer 0 #
x = Conv2DTranspose(filters = feature_multiplier * 4,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2DTranspose(filters = feature_multiplier * 4,
kernel_size = kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
deconv4_output = x
deconvolution_4 = Model(deconv4_input, deconv4_output)
layer_deconv.append(deconvolution_4)
### Deconv 3 ###
# Input #
deconv3_input_shape = conv3_output_shape
deconv3_input = Input(shape = deconv3_input_shape)
x = deconv3_input
# Layer 0 #
x = Conv2DTranspose(filters = feature_multiplier * 2,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2DTranspose(filters = feature_multiplier * 2,
kernel_size = kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
deconv3_output = x
deconvolution_3 = Model(deconv3_input, deconv3_output)
layer_deconv.append(deconvolution_3)
### Deconv 2 ###
# Input #
deconv2_input_shape = conv2_output_shape
deconv2_input = Input(shape = deconv2_input_shape)
x = deconv2_input
# Layer 0 #
x = Conv2DTranspose(filters = feature_multiplier * 1,
kernel_size = kernel_size,
strides = 2,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
# Layer 1 #
x = Conv2DTranspose(filters = feature_multiplier * 1,
kernel_size = kernel_size,
strides = 1,
kernel_initializer = init_func,
padding = "same")(x)
x = LeakyReLU(alpha = 0.2)(x)
x = BatchNormalization()(x)
x = Dropout(dropout)(x) if dropout > 0.0 else x
# Output #
deconv2_output = x
deconvolution_2 = Model(deconv2_input, deconv2_output)
layer_deconv.append(deconvolution_2)
### Deconv 1 ###
# Input #
deconv1_input_shape = conv1_output_shape
deconv1_input = Input(shape = deconv1_input_shape)
x = deconv1_input
# Layer 0 #
x = Conv2DTranspose(input_shape[-1],
kernel_size = surface_kernel_size,
strides = 2,
padding = "same",
kernel_initializer = init_func)(x)
x = Activation("linear")(x)
# Output #
deconv1_output = x
deconvolution_1 = Model(deconv1_input, deconv1_output)
layer_deconv.append(deconvolution_1)
layer_deconv.reverse()
return layer_conv, layer_deconv
# ------------------------------------------------------------------------------------------------------------------- #
def crea_adam(adam_learning_rate = 0.00015, adam_epsilon = 1e-08, adam_lr_decay = 1e-05):
optimizer = Adam(lr = adam_learning_rate,
epsilon = adam_epsilon,
decay = adam_lr_decay)
return optimizer
# ------------------------------------------------------------------------------------------------------------------- #
def crea_autoencoder_etapas(input_shape, layer_conv, layer_deconv, optimizer):
stages = []
### Etapa 1 ###
stage_input = Input(shape = input_shape)
x = stage_input
x = layer_conv[0](x)
x = layer_deconv[0](x)
stage_output = x
stage_1 = Model(inputs = stage_input, outputs = stage_output)
stage_1.compile(optimizer = optimizer, loss = "mse", metrics = ["mae"])
stages.append(stage_1)
### Etapa 2 ###
stage_input = Input(shape = input_shape)
x = stage_input
x = layer_conv[0](x)
x = layer_conv[1](x)
x = layer_deconv[1](x)
x = layer_deconv[0](x)
stage_output = x
stage_2 = Model(inputs = stage_input, outputs = stage_output)
stage_2.compile(optimizer = optimizer, loss = "mse", metrics = ["mae"])
stages.append(stage_2)
### Etapa 3 ###
stage_input = Input(shape = input_shape)
x = stage_input
x = layer_conv[0](x)
x = layer_conv[1](x)
x = layer_conv[2](x)
x = layer_deconv[2](x)
x = layer_deconv[1](x)
x = layer_deconv[0](x)
stage_output = x
stage_3 = Model(inputs = stage_input, outputs = stage_output)
stage_3.compile(optimizer = optimizer, loss = "mse", metrics = ["mae"])
stages.append(stage_3)
### Etapa 4 ###
stage_input = Input(shape = input_shape)
x = stage_input
x = layer_conv[0](x)
x = layer_conv[1](x)
x = layer_conv[2](x)
x = layer_conv[3](x)
x = layer_deconv[3](x)
x = layer_deconv[2](x)
x = layer_deconv[1](x)
x = layer_deconv[0](x)
stage_output = x
stage_4 = Model(inputs = stage_input, outputs = stage_output)
stage_4.compile(optimizer = optimizer, loss = "mse", metrics = ["mae"])
stages.append(stage_4)
### Etapa 5 ###
stage_input = Input(shape = input_shape)
x = stage_input
x = layer_conv[0](x)
x = layer_conv[1](x)
x = layer_conv[2](x)
x = layer_conv[3](x)
x = layer_conv[4](x)
x = layer_deconv[4](x)
x = layer_deconv[3](x)
x = layer_deconv[2](x)
x = layer_deconv[1](x)
x = layer_deconv[0](x)
stage_output = x
stage_5 = Model(inputs = stage_input, outputs = stage_output)
stage_5.compile(optimizer = optimizer, loss = "mse", metrics = ["mae"])
stages.append(stage_5)
### Etapa 6 ###
stage_input = Input(shape = input_shape)
x = stage_input
x = layer_conv[0](x)
x = layer_conv[1](x)
x = layer_conv[2](x)
x = layer_conv[3](x)
x = layer_conv[4](x)
x = layer_conv[5](x)
x = layer_deconv[5](x)
x = layer_deconv[4](x)
x = layer_deconv[3](x)
x = layer_deconv[2](x)
x = layer_deconv[1](x)
x = layer_deconv[0](x)
stage_output = x
stage_6 = Model(inputs = stage_input, outputs = stage_output)
stage_6.compile(optimizer = optimizer, loss = "mse", metrics = ["mae"])
stages.append(stage_6)
return stages
# ------------------------------------------------------------------------------------------------------------------- #
def guarda_encoder(layer_conv, input_shape):
encoder_input = Input(shape = input_shape)
x = encoder_input
x = layer_conv[0](x)
x = layer_conv[1](x)
x = layer_conv[2](x)
x = layer_conv[3](x)
x = layer_conv[4](x)
x = layer_conv[5](x)
encoder_output = x
encoder = Model(inputs = encoder_input, outputs = encoder_output)
encoder.save("../modelos/encoder_true.h5")
def guarda_decoder(layer_conv, layer_deconv):
decoder_target_shape = (layer_conv[5].output_shape[1],
layer_conv[5].output_shape[2],
layer_conv[5].output_shape[3])
decoder_input = Input(shape = (256,))
x = decoder_input
x = Reshape(target_shape = decoder_target_shape)(x)
x = layer_deconv[-1](x)
x = layer_deconv[-2](x)
x = layer_deconv[-3](x)
x = layer_deconv[-4](x)
x = layer_deconv[-5](x)
x = layer_deconv[-6](x)
decoder_output = x
decoder = Model(inputs = decoder_input, outputs = decoder_output)
decoder.save("../modelos/decoder_true.h5")
# ------------------------------------------------------------------------------------------------------------------- #
##### -------------------------------------------------- Librerías -------------------------------------------------- #####
import tensorflow as tf
import numpy as np
import h5py
import os
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.callbacks import ModelCheckpoint
from datos_funciones import carga_datos, crea_sets
from plots_funciones import training_plot
from autoencoder_funciones import crea_autoencoder_capas, crea_adam, crea_autoencoder_etapas, guarda_encoder, guarda_decoder
##### -------------------------------------------------- Selección GPU ---------------------------------------------- #####
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
##### -------------------------------------------------- Hiperparámetros -------------------------------------------- #####
NUM_SIMS = 1900 # Índice máximo escenas.
NUM_INICIO = 1000
NUM_SCENES = NUM_SIMS - 1000 # Número de escenas.
NUM_FRAMES = 200 # Frames por escena.
AE_EPOCHS = 100 # Epochs para entrenamiento normal.
AE_EPOCHS_LIST = 5 # Epochs para cada batch size de la lista.
PRE_EPOCHS = 1 # Epochs de preentrenamiento.
AE_BATCH_MULTIPLE = False # Probar distintos batch sizes, comparar diferencias.
PRE_TRAINING = True # Realizar preentrenamiento.
ae_batch_list = [1024, 512, 256, 128, 64, 32] # Posibles batch sizes de prueba.
AE_BATCH_SIZE = 128 # Batch size oficial.
PRE_BATCH_SIZE = 128 # Bacth size para preentrenamiento.
##### -------------------------------------------------- Carga de datos --------------------------------------------- #####
densities = carga_datos(num_inicio = NUM_INICIO, num_sims = NUM_SIMS, frames = NUM_FRAMES)
train_data, vali_data = crea_sets(densities)
##### -------------------------------------------------- Autoencoder 2D --------------------------------------------- #####
FEATURE_MULTIPLIER = 8 # Controla la cantidad de filtros de convolución utilizados por el autoencoder, y la dimension del espacio latente de la red.
SURFACE_KERNEL_SIZE = 4 # Matriz 4x4
KERNEL_SIZE = 2 # Matriz 2x2
DROPOUT = 0.0 # Porcentaje de nodos que apagar mediante dropout.
INIT_FUNCTION = "glorot_normal" # Inicialización de los pesos de la red neuronal.
input_shape = (train_data.shape[1],
train_data.shape[2],
train_data.shape[3])
layer_conv, layer_deconv = crea_autoencoder_capas(input_shape = input_shape, feature_multiplier = FEATURE_MULTIPLIER, surface_kernel_size = SURFACE_KERNEL_SIZE, kernel_size = KERNEL_SIZE, dropout = DROPOUT, init_func = INIT_FUNCTION)
optimizer = crea_adam()
stages = crea_autoencoder_etapas(input_shape = input_shape, layer_conv = layer_conv, layer_deconv = layer_deconv, optimizer = optimizer)
##### -------------------------------------------------- Autoencoder Entrenamiento ---------------------------------- #####
autoencoder = stages [-1]
autoencoder_clean_weights = autoencoder.get_weights()
if PRE_TRAINING:
for stage in stages:
autoencoder_pre_train = stage.fit(train_data, train_data,
epochs = PRE_EPOCHS,
batch_size = PRE_BATCH_SIZE,
validation_data = (vali_data, vali_data),
shuffle = True)
autoencoder.save("../modelos/autoencoder_true_pretraining.h5")
autoencoder_pre_weights = autoencoder.get_weights()
# ------------------------------------------------------------------------------------------------------------------- #
mc = ModelCheckpoint(filepath = "../modelos/autoencoder_true.h5",
monitor = "val_loss",
mode = "min",
save_best_only = True,
verbose = 1)
if AE_BATCH_MULTIPLE:
for BATCH_SIZE in ae_batch_list:
if PRE_TRAINING:
autoencoder.set_weights(autoencoder_pre_weights)
autoencoder_train = autoencoder.fit(train_data, train_data,
epochs = AE_EPOCHS_LIST,
batch_size = BATCH_SIZE,
verbose = 1,
validation_data = (vali_data, vali_data),
shuffle = True,
callbacks = [mc])
training_plot(network_train = autoencoder_train, epochs = AE_EPOCHS_LIST, batch_size = BATCH_SIZE, dropout = DROPOUT, loss = "mse", metric = "mae", identification = "ae_true_list")
else:
if PRE_TRAINING:
autoencoder.set_weights(autoencoder_pre_weights)
autoencoder_train = autoencoder.fit(train_data, train_data,
epochs = AE_EPOCHS,
batch_size = AE_BATCH_SIZE,
verbose = 1,
validation_data = (vali_data, vali_data),
shuffle = True,
callbacks = [mc])
training_plot(network_train = autoencoder_train, epochs = AE_EPOCHS, batch_size = AE_BATCH_SIZE, dropout = DROPOUT, loss = "mse", metric = "mae", identification = "ae_true_single")
# ------------------------------------------------------------------------------------------------------------------- #
guarda_encoder(layer_conv, input_shape)
guarda_decoder(layer_conv, layer_deconv)
This diff is collapsed.
# ------------------------------------------------------------------------------------------------------------------- #
import os
import sys
import numpy as np
sys.path.append("../tools") # Herramientas propias de MantaFlow.
import uniio # Lectura de ficheros .uni
def carga_datos(num_inicio, num_sims, frames):
base_path = "../data"
densities = []
for sim in range(num_inicio, num_sims):
if os.path.exists("%s/simSimple_%04d" % (base_path, sim)): # Comprueba la existencia de las carpetas (cada una 200 frames de datos).
for i in range(0, frames):
filename = "%s/simSimple_%04d/density_%04d.uni" # Nombre de cada frame (densidad).
uni_path = filename % (base_path, sim, i) # 200 frames por sim, rellena parametros de la ruta.
header, content = uniio.readUni(uni_path) # Devuelve un array Numpy [Z, Y, X, C].
h = header["dimX"]
w = header["dimY"]
arr = content[:, ::-1, :, :] # Cambia el orden del eje Y.
arr = np.reshape(arr, [w, h, 1]) # Deshecha el eje Z.
densities.append(arr)
load_num = len(densities)
if load_num < 2 * frames:
print("Error - Usa al menos dos simulaciones completas")
exit(True)
densities = np.reshape(densities, (len(densities), 64, 64, 1)) # Reconvierte la lista en array de Numpy.
print("Forma del array: {}".format(densities.shape))
print("Dimensiones del array: {}".format(densities.ndim))
print("Número de pixels en total: {}".format(densities.size))
return densities
# ------------------------------------------------------------------------------------------------------------------- #
def carga_datos_velocity(num_inicio, num_sims, frames):
base_path = "../data"
velocities = []
for sim in range(num_inicio, num_sims):
if os.path.exists("%s/simSimple_%04d" % (base_path, sim)): # Comprueba la existencia de las carpetas (cada una 200 frames de datos).
for i in range(0, frames):
filename = "%s/simSimple_%04d/vel_%04d.uni" # Nombre de cada frame (velocidad).
uni_path = filename % (base_path, sim, i) # 200 frames por sim, rellena parametros de la ruta.
header, content = uniio.readUni(uni_path) # Devuelve un array Numpy [Z, Y, X, C].
h = header["dimX"]
w = header["dimY"]
arr = content[:, ::-1, :, :] # Cambia el orden del eje Y.
arr = np.reshape(arr, [w, h, 1]) # Deshecha el eje Z.
velocities.append(arr)
load_num = len(velocities)
if load_num < 2 * frames:
print("Error - Usa al menos dos simulaciones completas")
exit(True)
velocities = np.reshape(velocities, (len(velocities), 64, 64, 1)) # Reconvierte la lista en array de Numpy
print("Forma del array: {}".format(velocities.shape))
print("Dimensiones del array: {}".format(velocities.ndim))
print("Número de pixels en total: {}".format(velocities.size))
return densities
# ------------------------------------------------------------------------------------------------------------------- #
def crea_sets(densities):
load_num = len(densities)
vali_set_size = max(200, int(load_num * 0.1)) # Al menos una sim completa o el 10% de los datos.
vali_data = densities[load_num - vali_set_size : load_num, :] # "load_num" datos del final de "densities".
train_data = densities[0 : load_num - vali_set_size, :] # El resto de datos de "densities".
print("Separamos en {} frames de entrenamiento y {} frames de validación.".format(train_data.shape[0], vali_data.shape[0]))
train_data = np.reshape(train_data, (len(train_data), 64, 64, 1)) # Reconvertimos a arrays de Numpy.
vali_data = np.reshape(vali_data, (len(vali_data), 64, 64, 1))
print("Forma del set de entrenamiento: {}".format(train_data.shape))
print("Forma del set de validación: {}".format(vali_data.shape))
return train_data, vali_data
# ------------------------------------------------------------------------------------------------------------------- #
import imageio
import PIL
import numpy as np
FRAMES = 200
TIME_STEPS = 6
images_in = []
images_out = []
images_pred = []
images_combi = []
for number in range(TIME_STEPS, FRAMES):
images_in.append(imageio.imread("../imagenes/in_" + str(number) + ".png"))
images_out.append(imageio.imread("../imagenes/out_" + str(number) + ".png"))
imageio.mimsave("../gifs/in.gif", images_in)
imageio.mimsave("../gifs/out.gif", images_out)
for number in range(TIME_STEPS, FRAMES):
images_pred.append(imageio.imread("../imagenes/pred_" + str(number) + ".png"))
imageio.mimsave("../gifs/pred.gif", images_pred)
for number in range(TIME_STEPS, FRAMES):
list_im = ["../imagenes/in_" + str(number) + ".png", "../imagenes/out_" + str(number) + ".png", "../imagenes/pred_" + str(number) + ".png"]
imgs = [PIL.Image.open(i) for i in list_im]
min_shape = sorted([(np.sum(i.size), i.size) for i in imgs])[0][1]
imgs_comb = np.hstack((np.asarray(i.resize(min_shape)) for i in imgs))
imgs_comb = PIL.Image.fromarray(imgs_comb)
imgs_comb.save("../imagenes/combi_" + str(number) + ".png")
for number in range(TIME_STEPS, FRAMES):
images_combi.append(imageio.imread("../imagenes/combi_" + str(number) + ".png"))
imageio.mimsave("../gifs/combi.gif", images_combi)
import tensorflow as tf
def loss_fluid_velocity(y_true, y_pred):
# ------------------------------------------------------------------------------------------------------------------- #
import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import RepeatVector, LSTM, Conv1D, Reshape, Input, Flatten
from tensorflow.keras.losses import mean_absolute_error, mean_squared_error
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.models import Model
from math import floor
# ------------------------------------------------------------------------------------------------------------------- #
def prepara_datos_lstm(encoder, train_data, vali_data, batch_size, time_steps, out_time_steps, frames):
encoded_train = encoder.predict(train_data)
encoded_vali = encoder.predict(vali_data)
def generator_count(encoded_data, batch_size, time_steps, out_time_steps, frames):
scene_count = len(encoded_data) // frames
sample_count = frames
scene_iteration_count = floor((sample_count + 1 - (time_steps + out_time_steps)) / batch_size)
return scene_count, sample_count, scene_iteration_count
def generator_batch_samples(encoded_data, batch_size, time_steps, out_time_steps, frames):
scene_count, sample_count, scene_iteration_count = generator_count(encoded_data, batch_size, time_steps, out_time_steps, frames)
batch_samples = scene_count * scene_iteration_count
return batch_samples
def shuffle_in_unison(*np_arrays):
rng = np.random.get_state()
for array in np_arrays:
np.random.set_state(rng)
np.random.shuffle(array)
def restructure_encoded_data(encoded_data, time_steps, out_time_steps, batch_size):
content_shape = encoded_data[0].shape # (256,)
final_sample_count = encoded_data.shape[0] - time_steps - out_time_steps # frames, frames - batch_size, frames - 2 * batch_size, ...
final_sample_count = min(batch_size, final_sample_count) # 8
X_data = np.zeros((final_sample_count, time_steps) + content_shape) # (8, 6, 256)
y_data = np.zeros((final_sample_count, out_time_steps) + content_shape) # (8, 1, 256)
curTS = 0
for z in range(time_steps, final_sample_count + time_steps):
X_data[curTS] = np.array(encoded_data[curTS:z])
y_data[curTS] = np.array(encoded_data[z:z+out_time_steps])
curTS += 1
return X_data, y_data
def generator_scene(encoded_data, batch_size, time_steps, out_time_steps, frames):
scene_count, sample_count, scene_iteration_count = generator_count(encoded_data, batch_size, time_steps, out_time_steps, frames)
while True:
for i in range(scene_count):
scene = encoded_train[(i * frames):((i + 1) * frames)] # Selecciona escenas individualmente.
for j in range(scene_iteration_count): # Número de batches que entran en una escena individual.
start = j * batch_size
end = sample_count
data = scene[start:end]
X, Y = restructure_encoded_data(data, time_steps, out_time_steps, batch_size)
X = X.reshape(*X.shape[0:2], -1)
Y = np.squeeze(Y.reshape(Y.shape[0], out_time_steps, -1))
shuffle_in_unison(X, Y)
yield X, Y
train_gen_samples = generator_batch_samples(encoded_train, batch_size, time_steps, out_time_steps, frames)
print ("Number of train batch samples per epoch: {}".format(train_gen_samples))
train_generator = generator_scene(encoded_train, batch_size, time_steps, out_time_steps, frames)
vali_gen_samples = generator_batch_samples(encoded_vali, batch_size, time_steps, out_time_steps, frames)
print ("Number of validation batch samples per epoch: {}".format(vali_gen_samples))
vali_generator = generator_scene(encoded_vali, batch_size, time_steps, out_time_steps, frames)
return train_gen_samples, train_generator, vali_gen_samples, vali_generator
def crea_lstm(time_steps, out_time_steps, latent_dimension, encoder_lstm_neurons, decoder_lstm_neurons, activation, use_bias, dropout, recurrent_dropout, stateful, lstm_optimizer, loss):
lstm_input = Input(shape = (time_steps, latent_dimension))
x = lstm_input
x = LSTM(units = encoder_lstm_neurons,
activation = activation,
use_bias = use_bias,
recurrent_activation = "hard_sigmoid",
kernel_initializer = "glorot_uniform",
recurrent_initializer = "orthogonal",
bias_initializer = "zeros",
unit_forget_bias = True,
dropout = dropout,
recurrent_dropout = recurrent_dropout,
return_sequences = False,
go_backwards = True,
stateful = stateful)(x)
x = RepeatVector(out_time_steps)(x)
x = LSTM(units = decoder_lstm_neurons,
activation = activation,
use_bias = use_bias,
recurrent_activation = "hard_sigmoid",
kernel_initializer = "glorot_uniform",
recurrent_initializer = "orthogonal",
bias_initializer = "zeros",
unit_forget_bias = True,
dropout = dropout,
recurrent_dropout = recurrent_dropout,
return_sequences = True,
go_backwards = False,
stateful = stateful)(x)
x = Conv1D(filters = latent_dimension, kernel_size = 1)(x)
x = Flatten()(x) if out_time_steps == 1 else x
lstm_output = x
lstm = Model(inputs = lstm_input, outputs = lstm_output)
lstm.compile(loss = loss,
optimizer = lstm_optimizer,
metrics = ["mse"])
return lstm
# ------------------------------------------------------------------------------------------------------------------- #
def crea_optimizador_lstm():
optimizer = RMSprop(lr = 0.000126,
rho = 0.9,
epsilon = 1e-08,
decay = 0.000334)
return optimizer
# ------------------------------------------------------------------------------------------------------------------- #
##### -------------------------------------------------- Librerías -------------------------------------------------- #####
import tensorflow as tf
import numpy as np
import h5py
import os
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.callbacks import ModelCheckpoint
from datos_funciones import carga_datos, crea_sets
from lstm_funciones import crea_lstm, crea_optimizador_lstm, prepara_datos_lstm
from plots_funciones import training_plot
##### -------------------------------------------------- Selección GPU ---------------------------------------------- #####
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
# ------------------------------------------------------------------------------------------------------------------- #
TIME_STEPS = 6 # 6 frames para alimentar al LSTM.
OUT_TIME_STEPS = 1 # Predicción de 1 frame por el LSTM.
LATENT_DIMENSION = 256 # 256 features en el codificado.
NUM_FRAMES = 200
NUM_SIMS = 1900
ENCODER_NEURONS = 256
DECODER_NEURONS = 512
ACTIVATION = "tanh"
LOSS = "mae"
LSTM_BATCH_SIZE = 16
LSTM_EPOCHS = 100
DROPOUT = 0.0132
RECURRENT_DROPOUT = 0.385
USE_BIAS = True
STATEFUL = False
# ------------------------------------------------------------------------------------------------------------------- #
densities = carga_datos(num_sims = NUM_SIMS, frames = NUM_FRAMES)
train_data, vali_data = crea_sets(densities)
encoder = load_model("../modelos/encoder_true.h5")
train_gen_samples, train_generator, vali_gen_samples, vali_generator = prepara_datos_lstm(encoder = encoder, train_data = train_data, vali_data = vali_data, batch_size = LSTM_BATCH_SIZE, time_steps = TIME_STEPS, out_time_steps = OUT_TIME_STEPS, frames = NUM_FRAMES)
optimizador = crea_optimizador_lstm()
lstm = crea_lstm(time_steps = TIME_STEPS, out_time_steps = OUT_TIME_STEPS, latent_dimension = LATENT_DIMENSION, encoder_lstm_neurons = ENCODER_NEURONS, decoder_lstm_neurons = DECODER_NEURONS, activation = ACTIVATION, use_bias = USE_BIAS, dropout = DROPOUT, recurrent_dropout = RECURRENT_DROPOUT, stateful = STATEFUL, lstm_optimizer = optimizador, loss = LOSS)
lstm_train = lstm.fit_generator(generator = train_generator,
steps_per_epoch = train_gen_samples,
epochs = LSTM_EPOCHS,
verbose = 1,
callbacks = None,
validation_data = vali_generator,
validation_steps = vali_gen_samples,
class_weight = None,
workers = 1)
lstm.save("../modelos/lstm_true.h5")
training_plot(network_train = lstm_train, epochs = LSTM_EPOCHS, batch_size = LSTM_BATCH_SIZE, dropout = DROPOUT, identification = "lstm_true_single", loss = LOSS, metric = "mse")
# ------------------------------------------------------------------------------------------------------------------- #
import matplotlib.pyplot as plt
def training_plot(network_train, epochs, batch_size, dropout, identification, loss, metric):
plot_epochs = range(epochs)
plot_loss = network_train.history["loss"]
plot_val_loss = network_train.history["val_loss"]
plot_metric = network_train.history[metric]
plot_val_metric = network_train.history["val_" + metric]
plt.figure(figsize = (15, 5))
ax = plt.subplot(1, 2, 1)
plt.plot(plot_epochs, plot_loss, label = loss.upper())
plt.plot(plot_epochs, plot_val_loss, label = "Validation " + loss.upper())
plt.legend()
plt.xlabel("Epoch")
plt.ylabel(loss.upper())
ax = plt.subplot(1, 2, 2)
plt.plot(plot_epochs, plot_metric, label = metric.upper())
plt.plot(plot_epochs, plot_val_metric, label = "Validation " + metric.upper())
plt.legend()
plt.xlabel("Epoch")
plt.ylabel(metric.upper())
if dropout > 0.0:
plt.savefig("../plots/model_" + identification + "_DO-" + str(dropout * 100) + "_BS-" + str(batch_size) + ".png")
else:
plt.savefig("../plots/model_" + identification + "_BS-" + str(batch_size) + ".png")
# ------------------------------------------------------------------------------------------------------------------- #
import os
import tensorflow as tf
import numpy as np
import scipy.misc
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
from random import randrange
from datos_funciones import carga_datos
NUM_INICIO = 1900
NUM_SIMS = 2000
FRAMES = 200
TIME_STEPS = 6
OUT_TIME_STEPS = 1
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
densities = carga_datos(num_inicio = NUM_INICIO, num_sims = NUM_SIMS, frames = FRAMES)
autoencoder = load_model("../modelos/autoencoder_24e-5.h5")
encoder = load_model("../modelos/encoder_24e-5.h5")
decoder = load_model("../modelos/decoder_24e-5.h5")
lstm = load_model("../modelos/lstm_true.h5")
NUM_SCENES = densities.shape[0] // FRAMES
RAND_SCENE = randrange(0, NUM_SCENES)
scene = densities[RAND_SCENE*FRAMES : RAND_SCENE*FRAMES + FRAMES, :, :, :]
autoencoder_scene = autoencoder.predict(scene)
encoded_scene = encoder.predict(scene)
latent_dim = encoded_scene.shape[-1]
lstm_scene = []
for i in range(FRAMES - 5):
time_frames = encoded_scene[i : i + 6]
time_frames = time_frames.reshape(1, 6, latent_dim)
lstm_prediction = lstm.predict(time_frames, batch_size = 1)
decoded_frame = decoder.predict(lstm_prediction)
lstm_scene.append(decoded_frame)
lstm_scene = np.reshape(lstm_scene, (len(lstm_scene), 64, 64, 1))
n = 10
plt.figure(figsize = (10, 3))
for i in range(n):
ax = plt.subplot(3, n, i + 1)
plt.imshow(scene[i].reshape(64, 64))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(3, n, i + 1 + n)
plt.imshow(autoencoder_scene[i].reshape(64, 64))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(3, n, i + 1 + n + n)
plt.imshow(lstm_scene[i].reshape(64, 64))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.savefig("../plots/comparativa.png")
out_dir = "../imagenes"
if not os.path.exists(out_dir): os.makedirs(out_dir)
for i in range(TIME_STEPS, FRAMES):
scipy.misc.toimage(np.reshape(scene[i - TIME_STEPS], [64, 64])).save("{}/in_{}.png".format(out_dir, i))
scipy.misc.toimage(np.reshape(autoencoder_scene[i - TIME_STEPS], [64, 64])).save("{}/out_{}.png".format(out_dir, i))
scipy.misc.toimage(np.reshape(lstm_scene[i - TIME_STEPS], [64, 64])).save("{}/pred_{}.png".format(out_dir, i))
import numpy as np
import tensorflow as tf
import os
import matplotlib.pyplot as plt
from datos_funciones import carga_datos_velocity
##### -------------------------------------------------- Selección GPU ---------------------------------------------- #####
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
##### -------------------------------------------------- Hiperparámetros -------------------------------------------- #####
NUM_SIMS = 1900 # Índice máximo escenas.
NUM_INICIO = 1000
NUM_SCENES = NUM_SIMS - 1000 # Número de escenas.
NUM_FRAMES = 200 # Frames por escena.
AE_EPOCHS = 100 # Epochs para entrenamiento normal.
AE_EPOCHS_LIST = 5 # Epochs para cada batch size de la lista.
PRE_EPOCHS = 1 # Epochs de preentrenamiento.
AE_BATCH_MULTIPLE = False # Probar distintos batch sizes, comparar diferencias.
PRE_TRAINING = True # Realizar preentrenamiento.
ae_batch_list = [1024, 512, 256, 128, 64, 32] # Posibles batch sizes de prueba.
AE_BATCH_SIZE = 128 # Batch size oficial.
PRE_BATCH_SIZE = 128 # Bacth size para preentrenamiento.
##### -------------------------------------------------- Carga de datos --------------------------------------------- #####
velocities = carga_datos_velocity(num_inicio = NUM_INICIO, num_sims = NUM_SIMS, frames = NUM_FRAMES)
from os import system as sys
sys("python3 autoencoder_train.py")
sys("python3 lstm_train.py")
File added
gifs/combi.gif

1.89 MiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment