MCPcopy Create free account
hub / github.com/OUCMachineLearning/OUCML / build_model

Function build_model

One_Day_One_GAN/day15/model.py:28–81  ·  view source on GitHub ↗

Builds/compiles the model and returns (encoder, decoder, vae).

()

Source from the content-addressed store, hash-verified

26 return z_mean + K.exp(0.5 * z_log_var) * epsilon
27
28def build_model():
29 """Builds/compiles the model and returns (encoder, decoder, vae)."""
30
31 # encoder
32 inputs = Input(shape=input_shape)
33 x = Reshape(image_shape)(inputs)
34 x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
35 x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
36 x = MaxPooling2D((2, 2))(x)
37 x = Dropout(0.25)(x)
38 x = Flatten()(x)
39 x = Dense(128, activation='relu')(x)
40 z_mean = Dense(latent_dim, name='z_mean')(x)
41 z_log_var = Dense(latent_dim, name='z_log_var')(x)
42 z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])
43
44 encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
45 encoder.summary()
46 plot_model(encoder, to_file='vae_cnn_encoder.png', show_shapes=True)
47
48 # decoder
49 latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
50 label_inputs = Input(shape=(num_classes,), name='label')
51 x = Concatenate()([latent_inputs, label_inputs])
52 x = Dense(128, activation='relu')(x)
53 x = Dense(14 * 14 * 32, activation='relu')(x)
54 x = Reshape((14, 14, 32))(x)
55 x = Dropout(0.25)(x)
56 x = UpSampling2D((2, 2))(x)
57 x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
58 x = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
59 outputs = Reshape(input_shape)(x)
60
61 decoder = Model([latent_inputs, label_inputs], outputs, name='decoder')
62 decoder.summary()
63 plot_model(decoder, to_file='vae_cnn_decoder.png', show_shapes=True)
64
65 # variational autoencoder
66 outputs = decoder([encoder(inputs)[2], label_inputs])
67 vae = Model([inputs, label_inputs], outputs, name='vae_mlp')
68 vae.summary()
69 plot_model(vae, to_file='vae_cnn.png', show_shapes=True)
70
71 # loss function
72 reconstruction_loss = mse(inputs, outputs)
73 reconstruction_loss *= original_dim
74 kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
75 kl_loss = K.sum(kl_loss, axis=-1)
76 kl_loss *= -0.5
77 vae_loss = K.mean(reconstruction_loss + kl_loss)
78 vae.add_loss(vae_loss)
79 vae.compile(optimizer='adam')
80
81 return encoder, decoder, vae
82
83def from_weights(weights_file):
84 """Build a model and load its pretrained weights from `weights_file`."""

Callers 1

from_weightsFunction · 0.85

Calls 1

ModelClass · 0.85

Tested by

no test coverage detected