def train(x_train, learning_rate, batch_size, epochs): autoencoder = VAE(input_shape=(28, 28, 1), conv_filters=(32, 64, 64, 64), conv_kernels=(3, 3, 3, 3), conv_strides=(1, 2, 2, 1), latent_space_dim=100) autoencoder.summary() autoencoder.compile(learning_rate) autoencoder.train(x_train, batch_size, epochs) return autoencoder
def train(x_train, learning_rate, batch_size, epochs): autoencoder = VAE( input_shape=(256, 388, 1), conv_filters=(512, 256), #(512, 256, 128, 64, 32) conv_kernels=(3, 3), # (3, 3, 3, 3, 3) conv_strides=(2, 2), # (2, 2, 2, 2, (2, 1)) latent_space_dim=128) autoencoder.summary() autoencoder.compile(learning_rate) autoencoder.train(x_train, batch_size, epochs) return autoencoder
def train(x_train, x_test, y_size, x_size, num_channels, latent_space_dim, learning_rate): autoencoder, encoder, decoder, encoder_mu, encoder_log_variance = VAE( y_size, x_size, num_channels, latent_space_dim) autoencoder.summary() autoencoder.compile(optimizer=Adam(lr=learning_rate), loss=loss_func(encoder_mu, encoder_log_variance)) return autoencoder, encoder, decoder
# -*- coding: utf-8 -*- """ Created on Fri Apr 1 13:11:00 2022 @author: nbrow """ import numpy as np import random import sys import matplotlib.pyplot as plt import copy from autoencoder import VAE _, VAE_Encoder, _, _, _ = VAE(20, 60, 1, 16) VAE_Encoder.load_weights("../VAE/VAE_encoder_Best.h5") UC_samp = np.load('../ML_Input_Noise_Files/UC_Design_1.npy')[0:20, 0:60] New_Count = 92_568 for i in range(1, New_Count + 1): sys.stdout.write('\rCurrently working on Iteration {}/{}...'.format( i, New_Count)) sys.stdout.flush() UC_Num = random.randint(1, 7432) True_UC = np.load( '../ML_Input_Noise_Files/UC_Design_{}.npy'.format(UC_Num)) Popt_Hold = np.load( '../ML_Output_Noise_Files/UC_Design_C_{}.npy'.format(UC_Num)) Noise_Plot = copy.deepcopy(True_UC) X_loc = [random.randint(0, 119) for i in range(120)] Y_loc = [random.randint(0, 39) for i in range(120)]
if __name__ == '__main__': if torch.cuda.is_available(): cuda = True else: cuda = False device = torch.device('cuda' if cuda else 'cpu') datas = pickle.load(open('./SavedWeights/bdd100k_inform.pkl', "rb")) segmentation = FastSCNN(classes=3).to(device) checkpoint = torch.load('./SavedWeights/model_8.pth', map_location=device) segmentation.load_state_dict(checkpoint['model']) autoencoder = VAE().to(device) trained_VAE = torch.load("./SavedWeights/last_VAE.pt", map_location=device) autoencoder.load_state_dict(trained_VAE.state_dict()) agent = torch.load('./SavedWeights/dqn.pt', map_location=device) trans = T.ToPILImage(mode='L') last_actions = np.zeros(4) last_action = np.zeros(2) x_coord, y_coord, x1_coord, y1_coord = get_window_coords('Grand Theft Auto V') while True: start = time.time() img = pyautogui.screenshot(region=(x_coord, y_coord, x1_coord, y1_coord))
def plot_images_encoded_in_latent_space(latent_representations, sample_labels): plt.figure(figsize=(10, 10)) plt.scatter(latent_representations[:, 0], latent_representations[:, 1], cmap="rainbow", c=sample_labels, alpha=0.5, s=2) plt.colorbar() plt.show() if __name__ == "__main__": autoencoder = VAE.load("model") x_train, y_train, x_test, y_test = load_mnist() num_sample_images_to_show = 8 sample_images, _ = select_images(x_test, y_test, num_sample_images_to_show) reconstructed_images, _ = autoencoder.reconstruct(sample_images) plot_reconstructed_images(sample_images, reconstructed_images) num_images = 6000 sample_images, sample_labels = select_images(x_test, y_test, num_images) _, latent_representations = autoencoder.reconstruct(sample_images) plot_images_encoded_in_latent_space(latent_representations, sample_labels)
load_name = "UC_Surrogate_Model_Weights_4-4-22" load_path = "checkpoints/" + load_name + "/cp.ckpt" checkpoint_path = "checkpoints/" + save_name + "/cp.ckpt" Norm_Y = np.load('../Result_Files/Normalizing_Compression_Y_Value.npy') Norm_X = np.load('../Result_Files/Normalizing_Compression_X_Value.npy') # save model after each epoch cp_callback = ModelCheckpoint(filepath=checkpoint_path, verbose=1) #Segment Datasets file_count = 100_000 UC_samp = np.load('../ML_Input_Noise_Files/UC_Design_1.npy')[0:20, 0:60] #Load in the VAE _, encoder, _, _, _ = VAE(np.shape(UC_samp)[0], np.shape(UC_samp)[1], num_channels=1, latent_space_dim=16) encoder.load_weights("../VAE/VAE_encoder_Best.h5") #------------------------------Comment Below------------------------- ''' X_samp= encoder.predict(np.reshape(UC_samp,(1,20,60,1))) Y_samp=np.load('../ML_Output_Noise_Files/UC_Design_C_1.npy') X_All=np.zeros((file_count,X_samp.shape[0],X_samp.shape[1])) Y_All=np.zeros((file_count,Y_samp.shape[0])) for i in range(0,file_count): sys.stdout.write('\rCurrently working on Iteration {}/{}...'.format(i,file_count)) sys.stdout.flush() Coef=np.load('../ML_Output_Noise_Files/UC_Design_C_{}.npy'.format(i+1)) UC_Train=np.load('../ML_Input_Noise_Files/UC_Design_{}.npy'.format(i+1))[0:20,0:60]
min_max_values[file_path] for file_path in file_paths ] print(file_paths) print(sampled_min_max_values) return sampled_spectrogrmas, sampled_min_max_values def save_signals(signals, save_dir, sample_rate=22050): for i, signal in enumerate(signals): save_path = os.path.join(save_dir, str(i) + ".wav") sf.write(save_path, signal, sample_rate) if __name__ == "__main__": # initialise sound generator vae = VAE.load("model") sound_generator = SoundGenerator(vae, HOP_LENGTH) # load spectrograms + min max values with open(MIN_MAX_VALUES_PATH, "rb") as f: min_max_values = pickle.load(f) specs, file_paths = load_fsdd(SPECTROGRAMS_PATH) # sample spectrograms + min max values sampled_specs, sampled_min_max_values = select_spectrograms( specs, file_paths, min_max_values, 5) # generate audio for sampled spectrograms signals, _ = sound_generator.generate(sampled_specs, sampled_min_max_values)
from autoencoder import AutoEncoder, VAE, generate_dataloader, train_model from torch import save from torchsummary import summary # creating the autoencoder #autoencoder = AutoEncoder() autoencoder = VAE() # visualising it summary(autoencoder, input_size=(1, 28, 28)) # generating the train data train_loader = generate_dataloader(batch_size=64, nb_train=200) # training the model trained_autoencoder = train_model(autoencoder, epochs=300, train_loader=train_loader, lr=0.01) # saving the model save(trained_autoencoder.state_dict(), 'my_autoencoder.pth')
epochs=EPOCHS, batch_size=BATCH_SIZE, shuffle=True, validation_data=(x_test, x_test)) plt.plot(history.history['loss'], label='Training Loss') plt.plot(history.history['val_loss'], label='Validation Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend(loc='best') plt.savefig(save_name + '_LossPlot.png') autoencoder.save("model") encoder.save("VAE_encoder.h5") decoder.save("VAE_decoder.h5") elif Method == 'Test': x_test = np.load('VAE_Testing_Set.npy') autoencoder, encoder, decoder, _, _ = VAE(y_size, x_size, num_channels, latent_space_dim) encoder.load_weights("VAE_encoder_Best.h5") decoder.load_weights("VAE_decoder_Best.h5") encoded_data = encoder.predict(x_test) decoded_data = decoder.predict(encoded_data) hold_dd = copy.deepcopy(decoded_data) decoded_data[decoded_data > 0.4] = 1 decoded_data[decoded_data <= 0.4] = 0 for i in range(0, 3): x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] Num = x[i] fig_1, axs_1 = plt.subplots() fig_2, axs_2 = plt.subplots() fig_3, axs_3 = plt.subplots() fig_4, axs_4 = plt.subplots()