Commit 88016011 authored by Perez Visaires, Jon's avatar Perez Visaires, Jon

Archivos de GPU

parent 1d122c70
# ------------------------------------------------------------------------------------------------------------------- #
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)