import context import numpy as np import matplotlib.pyplot as plt from src.networks.cnn import CNN from src.networks.autoencoder import AutoEncoder if __name__ == '__main__': # Variables dataset = 'datatest2' model = 'Model2_test15' version_model = 0 # load an already trained cnn model ae1 = AutoEncoder(load_models=('encoder-' + 'Model1_test15'), version=0) cnn1 = CNN(load_models=('cnn-' + model), version=version_model) # predict cnn, 20 predict ts_names = sorted(glob.glob(dataset + '/train_TS/*.npy')) bathy_names = sorted(glob.glob(dataset + '/train_GT/*.npy')) x = np.arange(0, 480, 1) # cross-shore shift = 40 for i in range(20): plt.subplot(4, 5, i + 1) ts_origi = np.load(ts_names[i + shift])[200:720, :] # croping width, height = ts_origi.shape ts_origi = np.array([ts_origi])
def browse_m(self, ae=False): """Action of the Choose model button. Description: browse the selected encoder model and the selected cnn model and then update the predicted bathymetry in consequence. """ # Get the encoder filename encoder_filename = QFileDialog.getOpenFileName(None, 'Find trained encoder', 'saves/weights/', '(*.h5)')[0] # Get the encoder model name and version encoder_path = encoder_filename.split('.')[0] encoder_name = encoder_path.split('/')[-1] encoder_version = int(encoder_filename.split('.')[1]) # Get the cnn model filename model_filename = QFileDialog.getOpenFileName(None, 'Find trained model', 'saves/weights/', '(*.h5)')[0] # Get the cnn model name and version model_path = model_filename.split('.')[0] model_name = model_path.split('/')[-1] model_version = int(model_filename.split('.')[1]) if encoder_filename and model_filename: # Create the encoder and the cnn ae1 = AutoEncoder(load_models=encoder_name, version=encoder_version) cnn1 = CNN(load_models=model_name, version=model_version) # Adjust the timestack shape ts_origi = self.b_canvas.ts #[200:] # croping width, height = ts_origi.shape ts_origi = np.array([ts_origi]) # Predict the encoded the timestack (= encode the timestack) ts_enc = ae1.predict(ts_origi.reshape(len(ts_origi), width, height, 1), batch_size=1) a, width, height = ts_enc.shape ts_enc = np.array([ts_enc]) # Predict the bathymetry self.b_canvas.bath_pred = cnn1.predict(ts_enc.reshape( len(ts_enc), width, height, 1), batch_size=1, smooth=True).flatten() # Update the display self.b_canvas.pred = True self.b_canvas.plot(bath_path=self.bath) max_height_error = max( abs(self.b_canvas.bath - self.b_canvas.bath_pred)) self.text1.selectAll() self.text1.textCursor().clearSelection() self.text1.textCursor().insertText( 'max height error : ' + str(round(max_height_error, 2)) + ' m') mean_height_error = np.mean( abs(self.b_canvas.bath - self.b_canvas.bath_pred)) self.text2.selectAll() self.text2.textCursor().clearSelection() self.text2.textCursor().insertText( 'mean height error : ' + str(round(mean_height_error, 2)) + ' m') corr_coef = np.corrcoef(self.b_canvas.bath, self.b_canvas.bath_pred) self.text3.selectAll() self.text3.textCursor().clearSelection() self.text3.textCursor().insertText('correlation coef : ' + str(round(corr_coef[0, 1], 5)))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ###################################################################### # Train a new Auto-Encoder and save the encoder # # # ###################################################################### import context import os from src.models import autoencoder from src.networks.autoencoder import AutoEncoder if __name__ == '__main__': dataset_path = '../GREGOIRE/dataset_new' # '.' name = 'Model1_test17' # creation and training of an auto-encoder, to pre-process the data ae1 = AutoEncoder(model=autoencoder.Model1((520, 480, 1)), batch_size=64, dataset_path=dataset_path) # Adapt to your path ae1.compile() hae1 = ae1.fit(epochs=2, repeat=1, fname=('autoencoder-'+name), fname_enc=('encoder-'+name)) ae1.save_losses(hae1, 'encoder-'+name) # saving the losses # Create the encoded dataset with the encoder #ae1 = AutoEncoder(load_models='encoder-Model1_test5', version=0) if not os.path.exists(dataset_path + '/train_encoded_TS/'): os.mkdir(dataset_path + '/train_encoded_TS/') ae1.encode_dataset(dataset_path, dataset_path)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import context from src.models import autoencoder from src.networks.autoencoder import AutoEncoder if __name__ == '__main__': # creation and training of an auto-encoder, to pre-process the data ae1 = AutoEncoder(model=autoencoder.Model1((400, 200, 1)), batch_size=4, dataset_path='.') # Adapt to your path ae1.compile() hae1 = ae1.fit(epochs=10, repeat=1, fname='autoencoder-Model1', fname_enc='encoder-Model1') ae1.save_losses(hae1, 'encoder-Model1') # saving the losses # Create the encoded dataset with the encoder ae1.encode_dataset('.', '.')
x = np.arange(-99, 600 - 99, 1) # cross-shore y = np.arange(0, 100, 1) # long-shore X, Y = np.meshgrid(x, y) # bathy_nc = netCDFFile('dataset_2D/dep.nc') # bathy = bathy_nc.variables['depth'] # bathy = -np.array(bathy) bathy = np.load('../../') print(bathy.shape) ts_nc = netCDFFile('dataset_2D/eta.nc') ts = ts_nc.variables['eta'] ts = np.array(ts) print(ts.shape) ae1 = AutoEncoder(load_models='encoder-Model1_test13', version=0) cnn1 = CNN(load_models='cnn-Model2_test14', version=0) bathy_pred = np.zeros([100, 480]) for i in range(bathy.shape[0]): bath1D = bathy[i, 21:501] ts1D = ts[200:720, i, 21:501] width, height = ts1D.shape ts1D = np.array([ts1D]) ts1D_enc = ae1.predict(ts1D.reshape(len(ts1D), width, height, 1), batch_size=1) a, width, height = ts1D_enc.shape ts1D_enc = np.array([ts1D_enc]) bathy_pred[i, :] = cnn1.predict(ts1D_enc.reshape(