Blog

Learn About Our Meetup

5000+ Members

MEETUPS

LEARN, CONNECT, SHARE

Join our meetup, learn, connect, share, and get to know your Toronto AI community. 

JOB POSTINGS

INDEED POSTINGS

Browse through the latest deep learning, ai, machine learning postings from Indeed for the GTA.

CONTACT

CONNECT WITH US

Are you looking to sponsor space, be a speaker, or volunteer, feel free to give us a shout.

[R] Replace VAE dense layer with 1x1x1 Convolutional layer

Hi!

I have a Variational autoencoder model created in Keras.Encoder is built with three 3D Convolutional layers + Flatten + Dense layer. Decoder is built with three 3D Transposed Convolutional layers to reconstruct the input 3D images.

My goal is to replace Flatten and Dense layer in Encoder with 1x1x1 Convolutional layer. Any ideas how to do that?

My model structure looks like this:

original_dim = np.prod(np.array(original_shape)) input_shape = ( original_shape[0], original_shape[1], original_shape[2], 1) latent_dim = 100 def sampling(args): """Reparameterization trick by sampling fr an isotropic unit Gaussian. Arguments: args {tensor} -- mean and log of variance of Q(z|X) Returns: z {tensor} -- sampled latent vector """ z_mean, z_log_var = args batch = K.shape(z_mean)[0] dim = K.int_shape(z_mean)[1] epsilon = K.random_normal(shape=(batch, dim)) return z_mean + K.exp(0.5 * z_log_var) * epsilon # Encoder inputs = Input(shape=input_shape, name='encoder_input') x = inputs x = Conv3D(filters=32, kernel_size=3, activation='relu', strides=1, padding='same')(x) x = Conv3D(filters=64, kernel_size=3, activation='relu', strides=2, padding='same')(x) x = Conv3D(filters=128, kernel_size=3, activation='relu', strides=2, padding='same')(x) shape = K.int_shape(x) x = Flatten()(x) x = Dense(latent_dim, activation='relu')(x) z_mean = Dense(latent_dim, name='z_mean')(x) z_log_var = Dense(latent_dim, name='z_log_var')(x) z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var]) encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') # Decoder latent_inputs = Input(shape=(latent_dim,), name='z_sampling') x = Dense(shape[1] * shape[2] * shape[3] * shape[4], activation='relu')(latent_inputs) x = Reshape((shape[1], shape[2], shape[3], shape[4]))(x) x = Conv3DTranspose(filters=128, kernel_size=3, activation='relu', strides=2, padding='same')(x) x = Conv3DTranspose(filters=64, kernel_size=3, activation='relu', strides=2, padding='same')(x) x = Conv3DTranspose(filters=32, kernel_size=3, activation='relu', strides=1, padding='same')(x) outputs = Conv3DTranspose(filters=1, kernel_size=3, strides=1, activation='sigmoid', padding='same', name='decoder_output')(x) decoder = Model(latent_inputs, outputs, name='decoder') decoder.summary() outputs = decoder(encoder(inputs)[2]) vae = Model(inputs, outputs, name='vae') reconstruction_loss = mse(K.flatten(inputs), K.flatten(outputs)) reconstruction_loss *= original_dim kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5 vae_loss = K.mean(reconstruction_loss + kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer='rmsprop') 

Thank you for any help

submitted by /u/Pubec
[link] [comments]

Next Meetup

 

Days
:
Hours
:
Minutes
:
Seconds

 

Plug yourself into AI and don't miss a beat

 


Toronto AI is a social and collaborative hub to unite AI innovators of Toronto and surrounding areas. We explore AI technologies in digital art and music, healthcare, marketing, fintech, vr, robotics and more. Toronto AI was founded by Dave MacDonald and Patrick O'Mara.