Esempio n. 1
0
def CVAE(input_shape, latent_dim=3, lr=0.001):
    image_size = input_shape[:-1]
    channels = input_shape[-1]
    conv_layers = 4
    feature_maps = [32, 64, 64, 32]
    filter_shapes = [(3, 3), (3, 3), (3, 3), (3, 3)]
    strides = [(1, 1), (2, 2), (1, 1), (1, 1)]
    dense_layers = 1
    dense_neurons = [128]
    dense_dropouts = [0.5]

    feature_maps = feature_maps[0:conv_layers]
    filter_shapes = filter_shapes[0:conv_layers]
    strides = strides[0:conv_layers]
    autoencoder = conv_variational_autoencoder(image_size,
                                               channels,
                                               conv_layers,
                                               feature_maps,
                                               filter_shapes,
                                               strides,
                                               dense_layers,
                                               dense_neurons,
                                               dense_dropouts,
                                               latent_dim,
                                               lr=lr)
    autoencoder.model.summary()
    return autoencoder
Esempio n. 2
0
    def compile(self):
        """
        Builds autoencoder.
        """
        print("Building convolutional variational autoencoder")

        # set up parameter
        self.feature_maps = self.feature_maps[0:self.conv_layers]
        self.filter_shapes = self.filter_shapes[0:self.conv_layers]
        self.strides = self.strides[0:self.conv_layers]
        self.autoencoder = conv_variational_autoencoder(
            self.image_size, self.channels, self.conv_layers,
            self.feature_maps, self.filter_shapes, self.strides,
            self.dense_layers, self.dense_neurons, self.dense_dropouts,
            self.latent_dim)
    tensor_shape = (1, image_size[0], image_size[1])
else:
    tensor_shape = (image_size[0], image_size[1], 1)
X_train = X_train.reshape((X_train.shape[0], ) + tensor_shape)
X_test = X_test.reshape((X_test.shape[0], ) + tensor_shape)
print "reshaped data:", "train:", np.shape(X_train), "test:", np.shape(X_test)

# build autoencoder;
print "building variational autoencoder"
# set up parameter;
feature_maps = feature_maps[0:conv_layers]
filter_shapes = filter_shapes[0:conv_layers]
strides = strides[0:conv_layers]
autoencoder = conv_variational_autoencoder(image_size, channels, conv_layers,
                                           feature_maps, filter_shapes,
                                           strides, dense_layers,
                                           dense_neurons, dense_dropouts,
                                           latent_dim)

# train, save & load;
for i in range(nb_start, nb_end):
    if i == 0:
        print("skipping - no previous saved file to load")
    # load model;
    else:
        autoencoder.load("./model/model_%i" % i)
    # train model;
    autoencoder.train(X_train[0:],
                      batch_size,
                      epochs=epochs,
                      validation_data=(X_test[0:], X_test[0:]),