def build(self, height, width, depth, filters=(32, 64), latentDim=16): inputShape = (height, width, depth) inputs = Input(shape=inputShape) # loading existing model print("Loading existing autoencoders...") file_path = '.\models\convnetDenoisingAEgaussian.h5' if os.path.isfile(file_path): cae = ConvAutoencoder() autoencoder_gaussian = cae.load_model(file_path) file_path = '.\models\convnetDenoisingAEspeckle.h5' if os.path.isfile(file_path): cae = ConvAutoencoder() autoencoder_speckle = cae.load_model(file_path) file_path = '.\models\convnetDenoisingAEsaltAndPepper.h5' if os.path.isfile(file_path): cae = ConvAutoencoder() autoencoder_saltAndPepper = cae.load_model(file_path) file_path = '.\models\convnetDenoisingAEblock.h5' if os.path.isfile(file_path): cae = ConvAutoencoder() autoencoder_block = cae.load_model(file_path) file_path = '.\models\convnetDenoisingAEborder.h5' if os.path.isfile(file_path): cae = ConvAutoencoder() autoencoder_border = cae.load_model(file_path) file_path = '.\models\convnetDenoisingAEnoNoise.h5' if os.path.isfile(file_path): cae = ConvAutoencoder() autoencoder_no_noise = cae.load_model(file_path) # x_gaussian = autoencoder_gaussian(inputs) x_speckle = autoencoder_speckle(inputs) x_saltAndPepper = autoencoder_saltAndPepper(inputs) x_block = autoencoder_block(inputs) x_border = autoencoder_border(inputs) x_noNoise = autoencoder_no_noise(inputs) # concatinating autoencoders combined = concatenate([ x_gaussian, x_speckle, x_saltAndPepper, x_block, x_border, x_noNoise ]) # reshaping to fit input/output combined = Dense(1, input_shape=(6, ))(combined) combined = Reshape((28, 28, 1))(combined) # apply RELU => LINEAR (Don't know if this is needed) x = Activation("relu")(combined) x = Activation("linear")(x) # out models is the combined autoencoders self.model = Model(inputs=inputs, outputs=x) return self.model
5:] trainXNoisy = np.concatenate( (trainXNoisyGaussian, trainXNoisySpeckle, trainXNoisySaltAndPepper, trainXNoisyBlock, trainXNoisyBorder, trainXNoisyNoNoise)) testXNoisy = np.concatenate( (testXNoisyGaussian, testXNoisySpeckle, testXNoisySaltAndPepper, testXNoisyBlock, testXNoisyBorder, testXNoisyNoNoise)) # initialize convolutional autoencoder cae = ConvAutoencoder() file_path = '.\models\convnetDenoisingAE' + args["noise"] + '.h5' if os.path.isfile(file_path): # loading existing model print("Loading existing model...") autoencoder = cae.load_model(file_path) else: # construct our convolutional autoencoder print("[INFO] building autoencoder...") autoencoder = cae.build(28, 28, 1, args["noise"]) opt = Adam(lr=1e-3) autoencoder.compile(loss="mse", optimizer=opt) # train the convolutional autoencoder H = autoencoder.fit(trainXNoisy, trainX, validation_data=(testXNoisy, testX), epochs=EPOCHS, batch_size=BS) # save model cae.save_model(file_path)