def train(self): #Train Alternating if random() < mixed_prob: style = mixedList(BATCH_SIZE) else: style = noiseList(BATCH_SIZE) a, b, c = self.train_step( self.im.get_batch(BATCH_SIZE).astype('float32'), style, nImage(BATCH_SIZE), self.gp_weight) if self.GAN.steps % 10 == 0: self.GAN.EMA() if self.GAN.steps <= 10000 and self.GAN.steps % 1000 == 2: self.GAN.MAinit() new_weight = 5 / (np.array(c) + 1e-7) self.gp_weight = self.gp_weight[0] * 0.9 + 0.1 * new_weight self.gp_weight = np.clip([self.gp_weight] * BATCH_SIZE, 0.01, 10000.0).astype('float32') #Print info if self.GAN.steps % 100 == 0 and not self.silent: print("\n\nRound " + str(self.GAN.steps) + ":") print("D:", np.array(a)) print("G:", np.array(b)) print("GP:", self.gp_weight[0]) s = round((time.clock() - self.lastblip), 4) self.lastblip = time.clock() steps_per_second = 100 / s steps_per_minute = steps_per_second * 60 steps_per_hour = steps_per_minute * 60 print("Steps/Second: " + str(round(steps_per_second, 2))) print("Steps/Hour: " + str(round(steps_per_hour))) min1k = floor(1000 / steps_per_minute) sec1k = floor(1000 / steps_per_second) % 60 print("1k Steps: " + str(min1k) + ":" + str(sec1k)) steps_left = 200000 - self.GAN.steps + 1e-7 hours_left = steps_left // steps_per_hour minutes_left = (steps_left // steps_per_minute) % 60 print("Til Completion: " + str(int(hours_left)) + "h" + str(int(minutes_left)) + "m") print() #Save Model if self.GAN.steps % 500 == 0: self.save(floor(self.GAN.steps / 10000)) if self.GAN.steps % 1000 == 0 or (self.GAN.steps % 100 == 0 and self.GAN.steps < 1000): self.evaluate(floor(self.GAN.steps / 1000)) printProgressBar(self.GAN.steps % 100, 99, decimals=0) self.GAN.steps = self.GAN.steps + 1
def train(self, directory=''): if self.im is None: # Data generator (my own code, not from TF 2.0) self.im = dataGenerator(directory, im_size, flip=True) # Train Alternating if random() < mixed_prob: style = mixedList(BATCH_SIZE) else: style = noiseList(BATCH_SIZE) # Apply penalties every 16 steps apply_gradient_penalty = self.GAN.steps % 2 == 0 or self.GAN.steps < 10000 # noqa apply_path_penalty = self.GAN.steps % 16 == 0 a, b, c, d = self.train_step( self.im.get_batch(BATCH_SIZE).astype('float32'), style, nImage(BATCH_SIZE), apply_gradient_penalty, apply_path_penalty) # Adjust path length penalty mean # d = pl_mean when no penalty is applied if self.pl_mean == 0: self.pl_mean = np.mean(d) self.pl_mean = 0.99 * self.pl_mean + 0.01 * np.mean(d) if self.GAN.steps % 10 == 0 and self.GAN.steps > 20000: self.GAN.EMA() if self.GAN.steps <= 25000 and self.GAN.steps % 1000 == 2: self.GAN.MAinit() if np.isnan(a): print("NaN Value Error.") exit() # Print info if self.GAN.steps % 100 == 0 and not self.silent: print("\n\nRound " + str(self.GAN.steps) + ":") print("D:", np.array(a)) print("G:", np.array(b)) print("PL:", self.pl_mean) s = round((time.clock() - self.lastblip), 4) self.lastblip = time.clock() steps_per_second = 100 / s steps_per_minute = steps_per_second * 60 steps_per_hour = steps_per_minute * 60 print("Steps/Second: " + str(round(steps_per_second, 2))) print("Steps/Hour: " + str(round(steps_per_hour))) min1k = floor(1000 / steps_per_minute) sec1k = floor(1000 / steps_per_second) % 60 print("1k Steps: " + str(min1k) + ":" + str(sec1k)) steps_left = 200000 - self.GAN.steps + 1e-7 hours_left = steps_left // steps_per_hour minutes_left = (steps_left // steps_per_minute) % 60 print("Til Completion: " + str(int(hours_left)) + "h" + str(int(minutes_left)) + "m") print() # Save Model if self.GAN.steps % 500 == 0: self.save(floor(self.GAN.steps / 10000)) if self.GAN.steps % 1000 == 0 or (self.GAN.steps % 100 == 0 and self.GAN.steps < 2500): self.evaluate(floor(self.GAN.steps / 1000)) printProgressBar(self.GAN.steps % 100, 99, decimals=0) self.GAN.steps = self.GAN.steps + 1
def train(self): source_class = 0 target_class = 1 source_images = self.im[source_class].get_batch(BATCH_SIZE).astype('float32') target_images = self.im[target_class].get_batch(BATCH_SIZE).astype('float32') apply_gradient_penalty = self.GAN.steps % 4 == 0 a, b, c = self.train_step(source_images, target_images, apply_gradient_penalty) #Adjust path length penalty mean #d = pl_mean when no penalty is applied if self.GAN.steps % 10 == 0 and self.GAN.steps > 20000: self.GAN.EMA() if self.GAN.steps <= 25000 and self.GAN.steps % 1000 == 2: self.GAN.MAinit() if np.isnan(a): print("NaN Value Error.") exit() #Print info if self.GAN.steps % 100 == 0 or self.GAN.steps < 10 and not self.silent: print("\n\nRound " + str(self.GAN.steps) + ":") print("D:", np.array(a)) print("G:", np.array(b)) print("R:", np.array(c)) s = round((time.clock() - self.lastblip), 4) self.lastblip = time.clock() steps_per_second = 100 / s steps_per_minute = steps_per_second * 60 steps_per_hour = steps_per_minute * 60 print("Steps/Second: " + str(round(steps_per_second, 2))) print("Steps/Hour: " + str(round(steps_per_hour))) min1k = floor(1000/steps_per_minute) sec1k = floor(1000/steps_per_second) % 60 print("1k Steps: " + str(min1k) + ":" + str(sec1k)) steps_left = 200000 - self.GAN.steps + 1e-7 hours_left = steps_left // steps_per_hour minutes_left = (steps_left // steps_per_minute) % 60 print("Til Completion: " + str(int(hours_left)) + "h" + str(int(minutes_left)) + "m") print() #Save Model if self.GAN.steps % 500 == 0: self.save(floor(self.GAN.steps / 10000)) if self.GAN.steps % 1000 == 0 or (self.GAN.steps % 100 == 0 and self.GAN.steps < 2500): self.evaluate(floor(self.GAN.steps / 1000)) printProgressBar(self.GAN.steps % 100, 99, decimals = 0) self.GAN.steps = self.GAN.steps + 1