self.x = profile_pngs_objs self.y = midcurve_pngs_objs self.autoencoder.fit(self.x, self.y, epochs=self.epochs, batch_size=5, shuffle=True) # Save models self.autoencoder.save(self.autoencoder_model_pkl) self.encoder.save(self.encoder_model_pkl) self.decoder.save(self.decoder_model_pkl) else: # Save models self.autoencoder = load_model(self.autoencoder_model_pkl) self.encoder= load_model(self.encoder_model_pkl) self.decoder = load_model(self.decoder_model_pkl) def predict(self, test_profile_images): png_profile_images = self.process_images(test_profile_images) encoded_imgs = self.encoder.predict(png_profile_images) decoded_imgs = self.decoder.predict(encoded_imgs) return test_profile_images,decoded_imgs if __name__ == "__main__": profile_gray_objs, midcurve_gray_objs = get_training_data() endec = simple_encoderdecoder() endec.train(profile_gray_objs, midcurve_gray_objs) test_gray_images = random.sample(profile_gray_objs,5) original_profile_imgs,predicted_midcurve_imgs = endec.predict(test_gray_images) plot_results(original_profile_imgs,predicted_midcurve_imgs)
profile_pngs_objs = np.asarray(profile_pngs_objs) midcurve_pngs_objs = np.asarray(midcurve_pngs_objs) profile_pngs_objs = np.expand_dims(profile_pngs_objs, axis=-1) midcurve_pngs_objs = np.expand_dims(midcurve_pngs_objs, axis=-1) profile_pngs_objs = (profile_pngs_objs - 127.5) / 127.5 #Normalize [-1, 1] midcurve_pngs_objs = (midcurve_pngs_objs - 127.5) / 127.5 #Normalize [-1, 1] x_coord = np.zeros(shape=(128, 128, 1)) y_coord = np.zeros(shape=(128, 128, 1)) for i in range(0, 128): x_coord[:, i, 0] = i y_coord[i, :, 0] = i coords = np.append(x_coord, y_coord, axis=-1) coords = (coords - 63.5) / 63.5 #Normalize [-1, 1] #coords = np.expand_dims(coords, axis=0) profile_pngs = [] for i in range(len(profile_pngs_objs)): profile_pngs.append(np.append(profile_pngs_objs[i], coords, axis=-1)) profile_pngs = np.asarray(profile_pngs) original_imgs, decoded_imgs = build_cnn_encoderdecoder_model( profile_pngs, midcurve_pngs_objs) plot_results(original_imgs, decoded_imgs)
self.denoiser_autoencoder.save(self.denoiser_autoencoder_model_pkl) else: # Save models self.denoiser_autoencoder = load_model( self.denoiser_autoencoder_model_pkl) def predict(self, test_noisy_images): png_profile_images = self.process_images(test_noisy_images) encoded_imgs = self.denoiser_autoencoder.predict(png_profile_images) decoded_imgs = self.denoiser_autoencoder.predict(encoded_imgs) return test_noisy_images, decoded_imgs if __name__ == "__main__": profile_gray_objs, midcurve_gray_objs = get_training_data() endec = simple_encoderdecoder() endec.train(profile_gray_objs, midcurve_gray_objs) original_profile_images, noisy_predicted_midcurve_images = endec.predict( profile_gray_objs) plot_results(original_profile_images[:5], noisy_predicted_midcurve_images[:5]) denoiser = denoiser_encoderdecoder() denoiser.train(noisy_predicted_midcurve_images, midcurve_gray_objs) sample_noisy_midcurve_images = random.sample( noisy_predicted_midcurve_images, 5) original_noisy_midcurve_images, clean_predicted_midcurve_images = denoiser.predict( sample_noisy_midcurve_images) plot_results(original_noisy_midcurve_images, clean_predicted_midcurve_images)