def displayOutput(self, display): display.fill((34, 177, 76)) display.blit(data.getResource("credits_player.png"), (data.config.WIDTH * 0.4 - 380, data.config.HEIGHT * 0.55 - 264)) display.blit(data.getResource("credits_dog.png"), (data.config.WIDTH * 0.7 - 200, data.config.HEIGHT * 0.6)) graphics.drawText(display, data.translate("credits"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.1, size=50, formatting="center") graphics.drawText(display, data.translate("students"), data.config.WIDTH * 0.7 - 400, data.config.HEIGHT * 0.25, size=30) graphics.drawText(display, "Victor Andrade", data.config.WIDTH * 0.7 - 100, data.config.HEIGHT * 0.25, size=20) graphics.drawText(display, "Marianne Linhares", data.config.WIDTH * 0.7 - 100, data.config.HEIGHT * 0.29, size=20) graphics.drawText(display, data.translate("professors"), data.config.WIDTH * 0.7 - 400, data.config.HEIGHT * 0.36 + 20, size=30) graphics.drawText(display, "Dalton Serey", data.config.WIDTH * 0.7 - 25, data.config.HEIGHT * 0.36 + 20, size=20) graphics.drawText(display, "Jorge Figueiredo", data.config.WIDTH * 0.7 - 25, data.config.HEIGHT * 0.40 + 20, size=20) graphics.drawText(display, data.translate("monitor"), data.config.WIDTH * 0.7 - 400 , data.config.HEIGHT * 0.47 + 40, size=30) graphics.drawText(display, "Kl�udio Medeiros".decode("cp437"), data.config.WIDTH * 0.7 - 120, data.config.HEIGHT * 0.47 + 40, size=20) self.credits_list.draw(display) if self.fadin > 0: blackness = pygame.Surface((data.config.WIDTH, data.config.HEIGHT)) blackness.set_alpha(self.fadin, pygame.RLEACCEL) display.blit(blackness, (0, 0)) self.fadin -= 4
def test_translate_np(): # Simulate a piece matrix of length 100 timesteps and test if translate # shifts the matrix down and up correctly across the note axis rand_matrix = np.random.rand(100 * 78 * 2).reshape(100, 78, 2) translated_matrix = data.translate(rand_matrix, direction="up") assert translated_matrix[0][1:] == rand_matrix[0][:-1] translated_matrix_d = data.translate(rand_matrix, direction="down") assert translated_matrix_d[0][:-1] == rand_matrix[0][1:]
def displayOutput(self, display): display.blit(self.shadow, (0, 0)) self.menu_list.draw(display) graphics.drawText(display, data.translate("return"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.3, size=20 , formatting="center") graphics.drawText(display, data.translate("help"), data.config.WIDTH * 0.3, data.config.HEIGHT * 0.5, size=20 , formatting="center") graphics.drawText(display, data.translate("configurations"), data.config.WIDTH * 0.7, data.config.HEIGHT * 0.5, size=20 , formatting="center") graphics.drawText(display, data.translate("exit"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.7, size=20 , formatting="center") graphics.drawText(display, data.translate("pause"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.5, size=40 , color=0xFFFFFF, formatting="center")
def displayOutput(self, display): self.buff.fill((64 - self.transitionTimer / 2, 0, 0)) self.buff.set_alpha(self.transitionTimer * 2 + 1, pygame.RLEACCEL) self.menu_list.draw(self.buff) graphics.drawText(self.buff, "Game Over", data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4, size=40 , color=0xFFFFFF, formatting="center") graphics.drawText(self.buff, data.translate("menu"), data.config.WIDTH * 0.3, data.config.HEIGHT * 0.7, size=20 , formatting="center") graphics.drawText(self.buff, data.translate("submit"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.7 - 15, size=20, formatting="center") graphics.drawText(self.buff, data.translate("score"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.7 + 15, size=20, formatting="center") graphics.drawText(self.buff, data.translate("restart"), data.config.WIDTH * 0.7, data.config.HEIGHT * 0.7, size=20 , formatting="center") display.blit(self.buff, (0, 0)) self.transitionTimer = min(self.transitionTimer + 1, 128)
def write_predictions(netG, inv_charmap, batches_per_file, num_files, batch_size=100000): print( f"Generating {batch_size * batches_per_file * num_files} passwords...") t_total = datetime.datetime.now() for i in range(1, num_files + 1): t = datetime.datetime.now() print(f"File {i}") print("\tGenerating output...") preds = [] for _ in range(batches_per_file): preds.append( netG(torch.randn(batch_size, 128).to(device=device)).argmax( dim=2)) #max 100k fits in memory list_of_preds = torch.stack(preds).reshape((-1, 10)).cpu().tolist() del preds print("\tTranslating output...") translated_preds = translate(list_of_preds, inv_charmap) del list_of_preds print("\tWriting output...") with open( f"Predictions/predfile_{i}_{batch_size*batches_per_file}.txt", 'w+') as f: for pred in translated_preds: f.write(pred + "\n") print(f"\t{datetime.datetime.now() - t}") del translated_preds print(f"\tTotal: {datetime.datetime.now() - t_total}")
def displayOutput(self, display): textColor = 0xF0F0F0 buf = pygame.Surface((data.config.WIDTH, data.config.HEIGHT)) if self.transitionTimer < 1080: buf.set_alpha(5 * abs((self.transitionTimer + 180) % 360 - 180), pygame.RLEACCEL) if self.transitionTimer < 360: buf.blit(data.getResource("bob.png")[28], (data.config.WIDTH * 0.4 - 48, data.config.HEIGHT * 0.6 - 48)) buf.blit(data.getResource("dog.png")[3], (data.config.WIDTH * 0.6 - 26, data.config.HEIGHT * 0.6 - 24)) graphics.drawText(buf, data.translate("intro1"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4, color=textColor, size=20, formatting="center") graphics.drawText(buf, data.translate("intro2"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4 + 50, color=textColor, size=20, formatting="center") elif self.transitionTimer < 720: buf.blit(data.getResource("bob.png")[24], (data.config.WIDTH * 0.4 - 48, data.config.HEIGHT * 0.7 - 48)) buf.blit(data.getResource("cage.png")[0], (data.config.WIDTH * 0.6 + 37, data.config.HEIGHT * 0.7 - 6)) buf.blit(data.getResource("neighbour.png")[1], (data.config.WIDTH * 0.6 - 40, data.config.HEIGHT * 0.7 - 92)) graphics.drawText(buf, data.translate("intro3"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4, color=textColor, size=20, formatting="center") graphics.drawText(buf, data.translate("intro4"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4 + 50, color=textColor, size=20, formatting="center") graphics.drawText(buf, data.translate("intro5"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4 + 100, color=textColor, size=20, formatting="center") elif self.transitionTimer < 1080: buf.blit(data.getResource("bob.png")[0], (data.config.WIDTH * 0.5 - 48, data.config.HEIGHT * 0.45 - 48)) graphics.drawText(buf, data.translate("intro6"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.6, color=textColor, size=20, formatting="center") elif ((self.transitionTimer - 1002) / 16) % 4: graphics.drawText(buf, data.translate("press"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.5, color=textColor, size=20, formatting="center") display.blit(buf, (0, 0))
def displayOutput(self, display, offset=(0, 0)): if self.transitionTimer < 128: buf = pygame.Surface((200 + data.config.WIDTH * 0.3, 100), pygame.SRCALPHA) self.winButtons.draw(buf, (data.config.WIDTH * 0.35 - 100, data.config.HEIGHT * 0.4 - 50)) graphics.drawText(buf, data.translate("submit"), 100, 50 - 15, size=20, formatting="center") graphics.drawText(buf, data.translate("score"), 100, 50 + 15, size=20, formatting="center") graphics.drawText(buf, data.translate("exit"), 100 + data.config.WIDTH * 0.3, 50, size=20, formatting="center") buf.fill((255, 255, 255, 255 - self.transitionTimer * 2), special_flags=pygame.BLEND_RGBA_MULT) display.blit(buf, (data.config.WIDTH * 0.35 - 100, data.config.HEIGHT * 0.4 - 50)) self.transitionTimer += 2 elif self.transitionTimer < 256: buf = display.copy() self.menu_list.draw(buf) graphics.drawText(buf, data.translate("name"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4, size=40, color=self.color, formatting="center") graphics.drawText(buf, data.translate("submit"), data.config.WIDTH * 0.5 + 225, data.config.HEIGHT * 0.55, size=20 , formatting="center") buf.set_alpha(2 * self.transitionTimer - 256, pygame.RLEACCEL) display.blit(buf, (0, 0)) self.transitionTimer += 2 else: self.menu_list.draw(display) graphics.drawText(display, data.translate("name"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.4, size=40, color=self.color, formatting="center") graphics.drawText(display, data.translate("submit"), data.config.WIDTH * 0.5 + 225, data.config.HEIGHT * 0.55, size=20 , formatting="center")
def displayOutput(self, display): display.fill(0x101010) display.blit(data.getResource("ranking.png"), (data.config.WIDTH * 0.5 - 350 - 8, data.config.HEIGHT * 0.55 - 272)) self.ranking_list.draw(display) purple = 0x7018cb buff = display.copy() if len(self.list) == 0: graphics.drawText(buff, data.translate("empty"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.55, size=30, color=0x303030, formatting="center") for i in range(0, len(self.list)): y = data.config.HEIGHT * 0.55 - 218 + i * 110 - self.ranking_list.getSliderValue(1) * max(0, len(self.list) - 5) * 110 / 1000 buff.blit(data.getResource("ranking_pos.png"), (data.config.WIDTH * 0.5 - 350, y - 46)) buff.blit(data.getResource("ranking_slot.png"), (data.config.WIDTH * 0.5 - 240, y - 46)) buff.blit(data.getResource("ranking_score.png"), (data.config.WIDTH * 0.5 + 160, y - 46)) if data.config.HEIGHT * 0.55 - 276 < y < data.config.HEIGHT * 0.55 + 272 + 15: graphics.drawText(buff, str(i + 1), data.config.WIDTH * 0.5 - 300, y, size=30, color=purple, formatting="center") graphics.drawText(buff, self.list[i][0], data.config.WIDTH * 0.5 - 228, y, size=20, color=purple) graphics.drawText(buff, "%4d" % self.list[i][1], data.config.WIDTH * 0.5 + 160 + 100, y, size=20, color=purple, formatting="center") display.blit(buff, (data.config.WIDTH * 0.5 - 350, data.config.HEIGHT * 0.55 - 268), ((data.config.WIDTH * 0.5 - 350, data.config.HEIGHT * 0.55 - 268), (710, 540))) display.blit(data.getResource("separator.png"), (data.config.WIDTH * 0.5 - 350 - 8, data.config.HEIGHT * 0.55 - 276)) display.blit(data.getResource("separator.png"), (data.config.WIDTH * 0.5 - 350 - 8, data.config.HEIGHT * 0.55 + 272)) if self.fadin > 0: blackness = pygame.Surface((data.config.WIDTH, data.config.HEIGHT)) blackness.set_alpha(self.fadin, pygame.RLEACCEL) display.blit(blackness, (0, 0)) self.fadin -= 4 graphics.drawText(display, data.translate("ranking"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.1, size=40, color=0xffffff, formatting="center")
def displayOutput(self, display): display.blit(data.getResource("backyard.png"), (data.config.WIDTH * 0.5 - 960, data.config.HEIGHT * 0.85 - 1080 + 162)) display.blit(data.getResource("logo.png"), (data.config.WIDTH * 0.5 - 461, data.config.HEIGHT * 0.05)) self.menu_list.draw(display) graphics.drawText(display, data.translate("start"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.6, size=20 , formatting="center") graphics.drawText(display, data.translate("help"), data.config.WIDTH * 0.1, data.config.HEIGHT * 0.9, size=20 , formatting="center") graphics.drawText(display, data.translate("ranking"), data.config.WIDTH * 0.3, data.config.HEIGHT * 0.9, size=20 , formatting="center") graphics.drawText(display, data.translate("configurations"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.9, size=20 , formatting="center") graphics.drawText(display, data.translate("credits"), data.config.WIDTH * 0.7, data.config.HEIGHT * 0.9, size=20 , formatting="center") graphics.drawText(display, data.translate("exit"), data.config.WIDTH * 0.9, data.config.HEIGHT * 0.9, size=20 , formatting="center") if self.fadin > 0: blackness = pygame.Surface((data.config.WIDTH, data.config.HEIGHT)) blackness.set_alpha(self.fadin, pygame.RLEACCEL) display.blit(blackness, (0, 0)) self.fadin -= 4
def displayOutput(self, display): if self.transitionTimer >= 128: self.buttons.draw(display) graphics.drawText(display, data.translate("submit"), data.config.WIDTH * 0.35, data.config.HEIGHT * 0.4 - 15, size=20, formatting="center") graphics.drawText(display, data.translate("score"), data.config.WIDTH * 0.35, data.config.HEIGHT * 0.4 + 15, size=20, formatting="center") graphics.drawText(display, data.translate("exit"), data.config.WIDTH * 0.65, data.config.HEIGHT * 0.4, size=20, formatting="center") else: buf = pygame.Surface((200 + data.config.WIDTH * 0.3, 100), pygame.SRCALPHA) self.buttons.draw(buf, (data.config.WIDTH * 0.35 - 100, data.config.HEIGHT * 0.4 - 50)) graphics.drawText(buf, data.translate("submit"), 100, 50 - 15, size=20, formatting="center") graphics.drawText(buf, data.translate("score"), 100, 50 + 15, size=20, formatting="center") graphics.drawText(buf, data.translate("exit"), 100 + data.config.WIDTH * 0.3, 50, size=20, formatting="center") buf.fill((255, 255, 255, self.transitionTimer * 2), special_flags=pygame.BLEND_RGBA_MULT) display.blit(buf, (data.config.WIDTH * 0.35 - 100, data.config.HEIGHT * 0.4 - 50)) self.transitionTimer += 2
import pygame import screen import keyboard import graphics import data pygame.mixer.pre_init(buffer=1024) pygame.init() pygame.mixer.set_num_channels(16) data.loadConfig() data.loadLanguage() pygame.display.set_icon(pygame.image.load("icon.png")) pygame.display.set_caption(data.translate("caption")) resolution = data.config.WIDTH, data.config.HEIGHT if data.config.FULLSCREEN: display = pygame.display.set_mode(resolution, pygame.FULLSCREEN) else: display = pygame.display.set_mode(resolution) data.loadResources() graphics.userInterface.cursor.init() clock = pygame.time.Clock() screen = screen.Introduction() def repaint(): display.fill(0x000000) screen.displayOutput(display)
def eval_dataset(config, valid_data, vocab, model, sess): model_preds = [] ground_truths = [] memories = [] questions = [] commonsense = [] total_eval_loss = [] batch_obj = enumerate(valid_data.get_batches(config.batch_size, shuffle=False, pad_to_full_batch=True)) if config.show_eval_progress: batch_obj = tqdm(batch_obj) for i,eval_batches in batch_obj: is_training = False if config.sample != -1 and i > config.sample: break ground_truths.extend(map( lambda item: [item['answer1'], item['answer2']], eval_batches)) memories.extend(map(lambda item: item['summary'], eval_batches)) questions.extend(map(lambda item: item['ques'], eval_batches)) if config.load_commonsense: commonsense.extend(map(lambda item: item['commonsense'], eval_batches)) fd = model.encode(eval_batches, is_training) oovs = model.get_batch_oov() eval_loss, preds = model.eval(sess, fd) model_preds.extend(map(lambda p: translate(p, vocab, oovs), preds)) total_eval_loss.append(eval_loss) model_preds = model_preds[:valid_data.num_examples] ground_truths = ground_truths[:valid_data.num_examples] eval_loss = avg(total_eval_loss) bleu1, bleu4, meteor, rouge, cider,\ bleu1_scores, bleu4_scores, meteor_scores, rouge_scores, cider_scores =\ eval_set(model_preds, ground_truths) if config.to_print_nums == -1: to_print_indices = range(len(model_preds)) else: to_print_indices = random.sample(range(len(model_preds)), config.to_print_nums) for idx in to_print_indices: print("Data %d" % idx) print("Summary: ", " ".join(memories[idx])) if config.load_commonsense: print("Commonsense: ") for path in commonsense[idx]: for concept in path: print (concept, "->", ) print ("") print("Question: ", " ".join(questions[idx])) print("Answer1: ", " ".join(ground_truths[idx][0])) print("Answer2: ", " ".join(ground_truths[idx][1])) print("Predicted: ", " ".join(model_preds[idx])) print("Bleu1: %.3f, Bleu4: %.3f, Rouge-L: %.3f, Meteor: %.3f, CIDEr: %.3f" % (bleu1_scores[idx], bleu4_scores[idx], rouge_scores[idx], meteor_scores[idx], cider_scores[idx])) print ("") print("=" * 80) print ("") return bleu1, bleu4, meteor, rouge, cider, eval_loss, model_preds
def displayOutput(self, display): textColor = 0x161616 menuNum = 3 if self.origin is not screen.Menu: display.blit(self.shadow, (0, 0)) textColor = 0xE0E0E0 menuNum = 2 else: display.fill((0, 90, 150)) if self.image % menuNum == 0: display.blit(data.getResource("help_esc.png"), (data.config.WIDTH * 0.4 - 200, data.config.HEIGHT * 0.25)) display.blit(data.getResource("help_Z.png"), (data.config.WIDTH * 0.4 - 200, data.config.HEIGHT * 0.25 + 80)) display.blit(data.getResource("help_X.png"), (data.config.WIDTH * 0.4 - 200, data.config.HEIGHT * 0.25 + 80 * 2)) display.blit(data.getResource("help_C.png"), (data.config.WIDTH * 0.4 - 200, data.config.HEIGHT * 0.25 + 80 * 3)) display.blit(data.getResource("help_up.png"), (data.config.WIDTH * 0.6 - 100, data.config.HEIGHT * 0.25)) display.blit(data.getResource("help_down.png"), (data.config.WIDTH * 0.6 - 100, data.config.HEIGHT * 0.25 + 80)) display.blit(data.getResource("help_left.png"), (data.config.WIDTH * 0.6 - 100, data.config.HEIGHT * 0.25 + 80 * 2)) display.blit(data.getResource("help_right.png"), (data.config.WIDTH * 0.6 - 20, data.config.HEIGHT * 0.25 + 80 * 2)) graphics.drawText(display, data.translate("controls"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.1, color=textColor, size=40, formatting="center") graphics.drawText(display, data.translate("pause"), data.config.WIDTH * 0.4 - 120, data.config.HEIGHT * 0.25 + 20, color=textColor, size=20) graphics.drawText(display, data.translate("jump"), data.config.WIDTH * 0.4 - 120, data.config.HEIGHT * 0.25 + 80 + 25, color=textColor, size=20) graphics.drawText(display, data.translate("attack"), data.config.WIDTH * 0.4 - 120, data.config.HEIGHT * 0.25 + 80 * 2 + 25, color=textColor, size=20) graphics.drawText(display, data.translate("use item"), data.config.WIDTH * 0.4 - 120, data.config.HEIGHT * 0.25 + 80 * 3 + 25, color=textColor, size=20) graphics.drawText(display, data.translate("open"), data.config.WIDTH * 0.6 - 20, data.config.HEIGHT * 0.25 + 25, color=textColor, size=20) graphics.drawText(display, data.translate("crouch"), data.config.WIDTH * 0.6 - 20, data.config.HEIGHT * 0.25 + 80 + 25, color=textColor, size=20) graphics.drawText(display, data.translate("move"), data.config.WIDTH * 0.6 + 60, data.config.HEIGHT * 0.25 + 80 * 2 + 25, color=textColor, size=20) graphics.drawText(display, data.translate("jump higher"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.65 + 60, color=textColor, size=18, formatting="center") elif self.image % menuNum == 1: display.blit(data.getResource("items.png")[0], (data.config.WIDTH * 0.6 + 150, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[1], (data.config.WIDTH * 0.6 + 150 - 48, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[2], (data.config.WIDTH * 0.6 + 150 - 48 * 2, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[3], (data.config.WIDTH * 0.6 + 150 - 48 * 3, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[4], (data.config.WIDTH * 0.6 + 150 - 48 * 4, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[5], (data.config.WIDTH * 0.6 + 150 - 48 * 5, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[6], (data.config.WIDTH * 0.6 + 150 - 48 * 6, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[7], (data.config.WIDTH * 0.6 + 150 - 48 * 7, data.config.HEIGHT * 0.25)) display.blit(data.getResource("items.png")[8], (data.config.WIDTH * 0.6 + 150, data.config.HEIGHT * 0.25 + 80)) display.blit(data.getResource("items.png")[9], (data.config.WIDTH * 0.6 + 150 - 48, data.config.HEIGHT * 0.25 + 80)) display.blit(data.getResource("items.png")[16], (data.config.WIDTH * 0.6 + 150, data.config.HEIGHT * 0.25 + 80 * 2)) display.blit(data.getResource("items.png")[17], (data.config.WIDTH * 0.6 + 150 - 48, data.config.HEIGHT * 0.25 + 80 * 2)) display.blit(data.getResource("items.png")[18], (data.config.WIDTH * 0.6 + 150 - 48 * 2, data.config.HEIGHT * 0.25 + 80 * 2)) display.blit(data.getResource("items.png")[11], (data.config.WIDTH * 0.6 + 150, data.config.HEIGHT * 0.25 + 80 * 3)) display.blit(data.getResource("chest.png")[0], (data.config.WIDTH * 0.3 - 200, data.config.HEIGHT * 0.55 - 120)) graphics.drawText(display, data.translate("items"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.1, size=40, color=textColor, formatting="center") graphics.drawText(display, data.translate("chest1"), data.config.WIDTH * 0.3 - 250, data.config.HEIGHT * 0.55, color=textColor, size=20) graphics.drawText(display, data.translate("chest2"), data.config.WIDTH * 0.3 - 250, data.config.HEIGHT * 0.55 + 50, color=textColor, size=20) graphics.drawText(display, data.translate("chest3"), data.config.WIDTH * 0.3 - 250, data.config.HEIGHT * 0.55 + 100, color=textColor, size=20) graphics.drawText(display, data.translate("chest4"), data.config.WIDTH * 0.3 - 250, data.config.HEIGHT * 0.55 + 150, color=textColor, size=15) graphics.drawText(display, data.translate("food"), data.config.WIDTH * 0.6 + 150 + 80, data.config.HEIGHT * 0.25 + 24, color=textColor, size=20) graphics.drawText(display, data.translate("potions"), data.config.WIDTH * 0.6 + 150 + 80, data.config.HEIGHT * 0.25 + 80 + 24, color=textColor, size=20) graphics.drawText(display, data.translate("weapons"), data.config.WIDTH * 0.6 + 150 + 80, data.config.HEIGHT * 0.25 + 80 * 2 + 24, color=textColor, size=20) graphics.drawText(display, data.translate("bomb"), data.config.WIDTH * 0.6 + 150 + 80, data.config.HEIGHT * 0.25 + 80 * 3 + 24, color=textColor, size=20) elif self.image % menuNum == 2: graphics.drawText(display, data.translate("test"), data.config.WIDTH * 0.5, data.config.HEIGHT * 0.1, size=40, color=textColor, formatting="center") graphics.drawText(display, data.translate("test1"), data.config.WIDTH * 0.5 - 320, data.config.HEIGHT * 0.62 - 75, color=textColor, size=20) graphics.drawText(display, data.translate("test2"), data.config.WIDTH * 0.5 - 320, data.config.HEIGHT * 0.62 - 25, color=textColor, size=20) graphics.drawText(display, data.translate("test3"), data.config.WIDTH * 0.5 - 320, data.config.HEIGHT * 0.62 + 25, color=textColor, size=20) graphics.drawText(display, data.translate("test4"), data.config.WIDTH * 0.5 - 320, data.config.HEIGHT * 0.62 + 75, color=textColor, size=20) self.help_list.draw(display)
def predict_many(netGs, inv_charmap, num_samples=5): latent_noise = torch.randn(num_samples, 128).to(device=device) return [ translate(netG(latent_noise).argmax(dim=2), inv_charmap) for netG in netGs ]
def predict_one(netG, inv_charmap, num_samples): latent_noise = torch.randn(num_samples, 128).to(device=device) return translate(netG(latent_noise).argmax(dim=2), inv_charmap)
def training_loop(lines, charmap, inv_charmap, dataloader, args): lambda_ = args['lambda_'] n_critic_iters_per_generator_iter = args[ 'n_critic_iters_per_generator_iter'] batch_size = args['batch_size'] lr = args['lr'] adam_beta1 = args['adam_beta1'] adam_beta2 = args['adam_beta2'] iterations = args['iterations'] continue_training = args['continue_training'] netG_checkpoint = args['netG_checkpoint'] netD_checkpoint = args['netD_checkpoint'] netG = Generator(charmap).to(device=device) netD = Discriminator(charmap).to(device=device) train = dataloader(lines, batch_size) if continue_training: #if continuing from checkpoint netG.load_state_dict(torch.load(netG_checkpoint)) netD.load_state_dict(torch.load(netD_checkpoint)) start_iter = int(netG_checkpoint.split(":")[0].split("-")[-1][:-2]) for _ in range(start_iter): next(train) pass print(f"Model loaded, starting at {start_iter}...") else: start_iter = 1 optimG = optim.Adam(netG.parameters(), lr=lr, betas=(adam_beta1, adam_beta2)) optimD = optim.Adam(netD.parameters(), lr=lr, betas=(adam_beta1, adam_beta2)) # start actual training loop for iteration in range(start_iter, iterations + 1): for p in netD.parameters(): p.requires_grad = True for i in range(n_critic_iters_per_generator_iter ): #discrim trains 10 times for gen's 1 real_inputs_discrete = next(train) real_data = F.one_hot(real_inputs_discrete, num_classes=len(charmap)).float() #x latent_variable = torch.randn(batch_size, 128).to(device=device) #z alpha = torch.rand(batch_size, 1, 1).to(device=device) #epsilon fake_data = netG(latent_variable) #x_tilde interpolates = alpha * real_data + ( (1 - alpha) * fake_data) #x_hat interpolates = interpolates.clone().detach().requires_grad_( True) #x_hat discrim_interpolates = netD(interpolates) #D_w(x_hat) gradients = autograd.grad( outputs=discrim_interpolates, inputs=interpolates, #grad D_w(x_hat) grad_outputs=torch.ones( discrim_interpolates.size()).to(device=device), create_graph=True, retain_graph=True, only_inputs=True)[0] #doesn't populate grad attributes gradient_penalty = ( (gradients.norm(2, dim=1) - 1)**2).mean() * lambda_ disc_real = netD(real_data).mean() #D_w(x) disc_fake = netD(fake_data).mean() #D_w(x_tilde) loss = disc_fake - disc_real + gradient_penalty #L loss.backward() optimD.step() netD.zero_grad() for p in netD.parameters(): p.requires_grad = False # to avoid computation netG.zero_grad() latent_variable = torch.randn(batch_size, 128).to(device=device) #z fake_data = netG(latent_variable) G = -netD(fake_data).mean() G.backward() optimG.step() if iteration % 1000 == 0 or iteration == 1: print(f"iterations {iteration}") real_translation = translate( real_inputs_discrete[:10].cpu().numpy(), inv_charmap) fake_translation = translate( fake_data[:10].detach().cpu().numpy().argmax(axis=2), inv_charmap) print(f"\tFake: {fake_translation}\n\tReal: {real_translation}") time = datetime.datetime.now( tz=datetime.timezone(datetime.timedelta( hours=-5))).strftime("%I:%M:%S%p_%m-%d-%y") torch.save( netG.state_dict(), f"/home/nvijayakumar/gcp-gan/Checkpoints/netG-{iteration}{time}" ) torch.save( netD.state_dict(), f"/home/nvijayakumar/gcp-gan/Checkpoints/netD-{iteration}{time}" )