def train_VAE(): model = VAE() model.summary() history = [] x_train = import_image(folder='train') x_test = import_image(folder='test') #x_train = import_image('test') arbitrary = np.zeros([x_train.shape[0], 1024 * 2]) model.summary() history = model.fit(x_train, [x_train, arbitrary], epochs=200, batch_size=128, shuffle=True, verbose=1, validation_data=(x_test, [x_test, arbitrary[:x_test.shape[0]]])) print("history =", history.history.keys()) output_loss1 = history.history['recons_loss'] output_loss2 = history.history['KLD_loss'] np.save('VAE2_recons_loss', output_loss1) np.save('VAE2_KLD_loss', output_loss2) model.save_weights('saved_model/VAE2_epochs60_weights.h5')
def problem3(model): # plot test image and calculate MSE x_test = import_image(path=sys.argv[1]) decoded_imgs = model.predict(x_test)[0] #output(decoded_imgs[:10], 'test') pred = np.concatenate([x_test[:10], decoded_imgs], axis=0) output_20(pred, sys.argv[2] + "fig1_3.jpg", 80) get_MSE(x_test, decoded_imgs)
def problem5(): np.random.seed(0) path = sys.argv[1] x_test = import_image(sys.argv[1]) input_img = Input(shape=(64, 64, 3)) encoder_output = VAE_encoder(input_img) encoder = Model(input_img, encoder_output) weights_path = 'saved_model/VAE_weights.h5' encoder.load_weights(weights_path, by_name=True) h = encoder.predict(x_test) print("h =", h.shape) np.save('report/VAE_problem5_h', h)
def train_ACGAN(self): self.x_train = import_image('train') self.features = import_image_features('train') #self.features_cat = to_categorical(self.features, 2) print("feat =", self.features.shape, self.features[:5]) self.x_train = self.x_train * 2 - 1 print("x_train =", self.x_train.shape) self.half_batch = int(self.batch_size / 2) noise = np.random.normal(0, 1, (10, 128)) history = [] self.train_D() self.train_G() fout = open('process_ACGAN', 'w') for epoch in range(self.epochs): if self.d_loss[3] > 0.8: #g_loss[1] > 3: print("%d" % 1, end=' ') fout.write("%d " % 1) for i in range(15): self.train_G() elif self.d_loss[3] > 0.65: #g_loss[1] > 1.5: print("%d" % 2, end=' ') fout.write("%d " % 2) for i in range(5): self.train_G() elif self.d_loss[3] < 0.45: #[1] > 1.8: print("%d" % 4, end=' ') fout.write("%d " % 4) for i in range(2): self.train_D() else: print("%d" % 3, end=' ') fout.write("%d " % 3) self.train_D() self.train_G() ''' if self.g_loss[1] > 2: #g_loss[1] > 3: print("%d" %1, end = ' ') fout.write("%d " %1) self.train_G() elif self.g_loss[1] > 1.5: #g_loss[1] > 1.5: print("%d" %2, end = ' ') fout.write("%d " %2) for i in range(5): self.train_G() self.train_D() elif self.d_loss[1] > 2: #g_loss[1] > 1.5: print("%d" %4, end = ' ') fout.write("%d " %4) self.train_D() else: print("%d" %3, end = ' ') fout.write("%d " %3) self.train_D() self.train_G() ''' # Plot the progress if epoch % 10 == 0: print("[acc1.: %.2f%%, acc2.: %.2f%%]" % (100 * self.d_loss[3], 100 * self.d_loss[4])) # 3, 4 print("%d [D loss1: %.4f, D loss2.: %.4f] [G loss1: %.4f, G loss2: %.4f]" % \ (epoch, self.d_loss[1], self.d_loss[2], self.g_loss[1], self.g_loss[2])), # 1, 2 fout.write("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]\n" % (epoch, self.d_loss[0], 100 * self.d_loss[3], self.g_loss[0])) if epoch % self.loss_interval == 0: history.append([self.d_loss[1], self.d_loss[2], self.g_loss[1], \ self.g_loss[2], 100*self.d_loss[3], 100*self.d_loss[4]]) # If at save interval => save generated image samples if epoch % self.sample_interval == 0: self.sample_images(epoch, noise) if epoch % 1000 == 0: self.generator.save_weights('saved_model/ACGAN_G_epochs' + str(epoch) + '_weights.h5') self.discriminator.save_weights('saved_model/ACGAN_D_epochs' + str(epoch) + '_weights.h5') self.combined.save_weights('saved_model/ACGAN_C_epochs' + str(epoch) + '_weights.h5') np.save("ACGAN_history", np.array(history)) #history.append([self.d_loss[0], 100*self.d_loss[1], self.g_loss]) self.generator.save_weights('saved_model/GAN_G_epochs' + str(epoch) + '_weights.h5') self.discriminator.save_weights('saved_model/GAN_D_epochs' + str(epoch) + '_weights.h5') self.combined.save_weights('saved_model/GAN_C_epochs' + str(epoch) + '_weights.h5') np.save("ACGAN_history", np.array(history)) self.sample_images(epoch, noise)
def train_GAN(self): #self.x_train = import_image('test') self.x_train = import_image('train') #x_test = import_image('test') self.x_train = self.x_train * 2 - 1 print("x_train =", self.x_train.shape) self.half_batch = int(self.batch_size / 2) print('construct noise') noise = np.random.normal(0, 1, (32, 128)) np.save('GAN_noise.npy', noise) history = [] self.train_D() self.train_G() fout = open('process2', 'a') for epoch in range(9000, 9000 + self.epochs): if self.d_loss[1] > 0.8: print("%d" % 1, end=' ') fout.write("%d " % 1) for i in range(15): self.train_G() elif self.d_loss[1] > 0.65: print("%d" % 2, end=' ') fout.write("%d " % 2) for i in range(5): self.train_G() elif self.d_loss[1] < 0.55: print("%d" % 4, end=' ') fout.write("%d " % 4) for i in range(2): self.train_D() elif self.d_loss[1] < 0.4: print("%d" % 5, end=' ') fout.write("%d " % 5) for i in range(7): self.train_D() else: print("%d" % 3, end=' ') fout.write("%d " % 3) self.train_D() self.train_G() # Plot the progress if epoch % 10 == 0: print( "%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, self.d_loss[0], 100 * self.d_loss[1], self.g_loss)) fout.write( "%d [D loss: %f, acc.: %.2f%%] [G loss: %f]\n" % (epoch, self.d_loss[0], 100 * self.d_loss[1], self.g_loss)) if epoch % self.loss_interval == 0: history.append( [self.d_loss[0], 100 * self.d_loss[1], self.g_loss]) # If at save interval => save generated image samples if epoch % self.sample_interval == 0: self.sample_images(epoch, noise) if epoch % 500 == 0: self.generator.save_weights('saved_model/GAN_G_epochs' + str(epoch) + '_weights.h5') self.discriminator.save_weights('saved_model/GAN_D_epochs' + str(epoch) + '_weights.h5') self.combined.save_weights('saved_model/GAN_C_epochs' + str(epoch) + '_weights.h5') np.save("GAN_history2", np.array(history)) history.append([self.d_loss[0], 100 * self.d_loss[1], self.g_loss]) self.generator.save_weights('saved_model/GAN_G_epochs' + str(epoch) + '_weights.h5') self.discriminator.save_weights('saved_model/GAN_D_epochs' + str(epoch) + '_weights.h5') self.combined.save_weights('saved_model/GAN_C_epochs' + str(epoch) + '_weights.h5') np.save("GAN_history2", np.array(history)) self.sample_images(20000, noise)