def main(batch_size): if batch_size is None: batch_size = 1 x, vocabulary, reverse_vocab, sentence_lengths = read_sampleFile() if batch_size > len(x): batch_size = len(x) start_token = vocabulary['START'] end_token = vocabulary['END'] pad_token = vocabulary['PAD'] ignored_tokens = [start_token, end_token, pad_token] vocab_size = len(vocabulary) generator = pretrain_generator(x, start_token=start_token, end_token=end_token,ignored_tokens=ignored_tokens, sentence_lengths=sentence_lengths,batch_size=batch_size, vocab_size=vocab_size) x_gen = generator.generate(start_token=start_token, ignored_tokens=ignored_tokens, batch_size=len(x)) discriminator = train_discriminator_wrapper(x, x_gen, batch_size, vocab_size) rollout = Rollout(generator, r_update_rate=0.8) rollout.to(DEVICE) for total_batch in range(TOTAL_BATCH): print('batch: {}'.format(total_batch)) for it in range(1): samples = generator.generate(start_token=start_token, ignored_tokens=ignored_tokens, batch_size=batch_size) # Take average of ROLLOUT_ITER times of rewards. # The more times a [0,1] class (positive, real data) # is returned, the higher the reward. rewards = getReward(samples, rollout, discriminator) (generator, y_prob_all, y_output_all) = train_generator(model=generator, x=samples, reward=rewards, iter_n_gen=1, batch_size=batch_size, sentence_lengths=sentence_lengths) rollout.update_params(generator) for iter_n_dis in range(DIS_NUM_EPOCH): print('iter_n_dis: {}'.format(iter_n_dis)) x_gen = generator.generate(start_token=start_token, ignored_tokens=ignored_tokens, batch_size=len(x)) discriminator = train_discriminator_wrapper(x, x_gen, batch_size,vocab_size) log = openLog('genTxt.txt') num = generator.generate(batch_size=batch_size) words_all = decode(num, reverse_vocab, log) log.close() print(words_all)
def fit(self, lr, epochs, n_fold, batch_size=8, opt="Adam", batches=400): if opt == "Adam": optimizer = Adam(lr=lr) elif opt == "SGD": optimizer = SGD(lr=lr, momentum=0.9, decay=1e-6, nesterov=True) else: raise Exception(f"Unknown optimizer: {opt}") train_gen = train_generator(n_fold=n_fold, batch_size=batch_size) valid_gen = valid_generator(n_fold=n_fold, batch_size=batch_size) self.unet.compile(optimizer=optimizer, loss=dice_bce_loss, metrics=[dice_coef]) self.unet.fit_generator(train_gen, steps_per_epoch=batches, nb_epoch=epochs, validation_data=valid_gen, validation_steps=batches, callbacks=self.callbacks) return self._get_losses()
def train(model_name): model = load_model(model_name) train_gen = train_generator(model.output_shape[3], (['wnp'], ['fill']), (height, width), 64) check_pointer = ModelCheckpoint(filepath='models/' + model_name + '.hdf5', verbose=1, save_best_only=True, monitor='loss') reduce = ReduceLROnPlateau(monitor='loss', factor=0.3, verbose=1, cooldown=10) tensor_board = TensorBoard( log_dir='models/' + model_name + '/', #write_images=True, #write_grads=True, #write_graph=True, #histogram_freq=1 ) tensor_board.validation_data = input_samples epoch = 0 epochs = 20 #while running: try: for i, o in train_gen: if not running: break check_memory() calc_sample_results(model) model.fit(i, o, epochs=epoch + epochs, initial_epoch=epoch, callbacks=[check_pointer, tensor_board], batch_size=6) #model.fit_generator( # train_gen, steps_per_epoch=30, # epochs=epoch + epochs, initial_epoch=epoch, callbacks=[check_pointer]) epoch += epochs except MemoryError: return
def main(batch_size, num=None): if batch_size is None: batch_size = 1 x, vocabulary, reverse_vocab, sentence_lengths = read_sampleFile(num=num) if batch_size > len(x): batch_size = len(x) start_token = vocabulary['START'] end_token = vocabulary['END'] pad_token = vocabulary['PAD'] ignored_tokens = [start_token, end_token, pad_token] vocab_size = len(vocabulary) log = openLog() log.write("###### start to pretrain generator: {}\n".format( datetime.now())) log.close() generator = pretrain_generator(x, start_token=start_token, end_token=end_token, ignored_tokens=ignored_tokens, sentence_lengths=torch.tensor( sentence_lengths, device=DEVICE).long(), batch_size=batch_size, vocab_size=vocab_size) x_gen = generator.generate(start_token=start_token, ignored_tokens=ignored_tokens, batch_size=len(x)) log = openLog() log.write("###### start to pretrain discriminator: {}\n".format( datetime.now())) log.close() discriminator = train_discriminator_wrapper(x, x_gen, batch_size, vocab_size) rollout = Rollout(generator, r_update_rate=0.8) rollout = torch.nn.DataParallel(rollout) #, device_ids=[0]) rollout.to(DEVICE) log = openLog() log.write("###### start to train adversarial net: {}\n".format( datetime.now())) log.close() for total_batch in range(TOTAL_BATCH): log = openLog() log.write('batch: {} : {}\n'.format(total_batch, datetime.now())) print('batch: {} : {}\n'.format(total_batch, datetime.now())) log.close() for it in range(1): samples = generator.generate(start_token=start_token, ignored_tokens=ignored_tokens, batch_size=batch_size) # Take average of ROLLOUT_ITER times of rewards. # The more times a [0,1] class (positive, real data) # is returned, the higher the reward. rewards = getReward(samples, rollout, discriminator) (generator, y_prob_all, y_output_all) = train_generator(model=generator, x=samples, reward=rewards, iter_n_gen=1, batch_size=batch_size, sentence_lengths=sentence_lengths) rollout.module.update_params(generator) for iter_n_dis in range(DIS_NUM_EPOCH): log = openLog() log.write(' iter_n_dis: {} : {}\n'.format(iter_n_dis, datetime.now())) log.close() x_gen = generator.generate(start_token=start_token, ignored_tokens=ignored_tokens, batch_size=len(x)) discriminator = train_discriminator_wrapper( x, x_gen, batch_size, vocab_size) log = openLog() log.write('###### training done: {}\n'.format(datetime.now())) log.close() torch.save(reverse_vocab, PATH + 'reverse_vocab.pkl') try: torch.save(generator, PATH + 'generator.pkl') print('successfully saved generator model.') except: print('error: model saving failed!!!!!!') log = openLog('genTxt.txt') num = generator.generate(batch_size=batch_size) log.close()
default=64, help='Height and width of results') parser.add_argument('-i', type=str, default='input', help='Input class list') parser.add_argument('-o', type=str, default='output', help='Output class list') args = parser.parse_args() folder = abspath(args.name) mkd(folder) input_folder = join(folder, 'input') output_folder = join(folder, 'output') mkd(input_folder) mkd(output_folder) input_classes = args.i.split(',') output_classes = args.o.split(',') n = 1 for i, o in train_generator(1, (input_classes, output_classes), (args.s, args.s), root='../'): while True: name = "%04d.png" % (n, ) if isfile(join(input_folder, name)): n += 1 else: break if n > args.count: break imsave(join(input_folder, name), i) imsave(join(output_folder, name), o[:, :, 0]) n += 1 print('Terminating...') shutdown_generator()
import numpy as np from skimage.io import imread from matplotlib import pyplot as plt from generator import train_generator from pympler import summary, muppy size = (64, 64) im = plt.imshow(np.zeros((size[0] * 2, size[1] * 2))) plt.ion() plt.show() train_gen = train_generator(1, (['mrsk', 'nsd', 'sam'], ['mrsk', 'nsd', 'sam']), size, 2, root='..', dump_mem=True) ctr = 0 def out_to_3(o): return np.concatenate((o, o, o), axis=2) def in_to_3(i): return i[:, :, :3] def stack(i, o):
type=int, default=512, help='Height and width of results') parser.add_argument('-i', type=str, default='wnp', help='Input class list') parser.add_argument('-o', type=str, default='fill', help='Output class list') args = parser.parse_args() folder = abspath(args.name) mkd(folder) input_folder = join(folder, 'input') output_folder = join(folder, 'output') mkd(input_folder) mkd(output_folder) input_classes = args.i.split(',') output_classes = args.o.split(',') n = 1 for i, o in train_generator(4, (input_classes, output_classes), (args.s, args.s)): while True: name = "%04d.png" % (n, ) if isfile(join(input_folder, name)): n += 1 else: break if n > args.count: break imsave(join(input_folder, name), i) imsave(join(output_folder, name), inverse_channel(o, 3)) n += 1 print('Terminating...') shutdown_generator()
model.summary() # Compile adam = Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-10, decay=0.0) loss_function = {"prob_output": loss_function_1, "pos_output": loss_function_2} model.compile(optimizer=adam, loss=loss_function, metrics=None) # Train epochs = 10 batch_size = 256 train_set_size = 25090 valid_set_size = 1317 training_steps_per_epoch = ceil(train_set_size / batch_size) validation_steps_per_epoch = ceil(valid_set_size / batch_size) train_gen = train_generator(batch_size=batch_size) val_gen = valid_generator(batch_size=batch_size) checkpoints = ModelCheckpoint('weights/weights_{epoch:03d}.h5', save_weights_only=True, save_freq=1) history = model.fit(train_gen, steps_per_epoch=training_steps_per_epoch, epochs=epochs, verbose=1, validation_data=val_gen, validation_steps=validation_steps_per_epoch, callbacks=[checkpoints], shuffle=True, max_queue_size=512)
import numpy as np from skimage.io import imread from matplotlib import pyplot as plt from generator import train_generator from pympler import summary, muppy size = (512, 512) im = plt.imshow(np.zeros((size[0] * 2, size[1] * 2))) plt.ion() plt.show() train_gen = train_generator(4, ['wnp'], size, 2) ctr = 0 for i, o in train_gen: i1 = np.hstack((i[0], o[0][:, :, :3])) i2 = np.hstack((i[1], o[1][:, :, :3])) im.set_data(np.vstack((i1, i2))) plt.draw() plt.pause(0.3) ctr += 1 if ctr > 100: ctr = 0 sum1 = summary.summarize(muppy.get_objects()) summary.print_(sum1)
# Compile adam = Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-10, decay=0.0) loss_function = { "probabilistic_output": loss_function_1, "positional_output": loss_function_2 } metrics = { "probabilistic_output": loss_function_1, "positional_output": loss_function_2 } model.compile(optimizer=adam, loss=loss_function, metrics=metrics) # Train epochs = 100 train_gen = train_generator(sample_per_batch=8, batch_number=3136) val_gen = valid_generator(sample_per_batch=64, batch_number=20) checkpoints = ModelCheckpoint('weights/performance{epoch:03d}.h5', save_weights_only=True, period=1) history = model.fit_generator(train_gen, steps_per_epoch=3136, epochs=epochs, verbose=1, validation_data=val_gen, validation_steps=20, shuffle=True, callbacks=[checkpoints], max_queue_size=100)
loss = tf.reduce_mean(cross_entropy) return loss # create model model = hand_model() model.summary() # compile adam = Adam(lr=1e-5) metrics = {'output': loss_function} model.compile(optimizer=adam, loss=loss_function, metrics=metrics) # train epochs = 10 train_gen = train_generator(steps_per_epoch=845, sample_per_batch=4) val_gen = valid_generator(steps_per_epoch=50, sample_per_batch=10) checkpoints = ModelCheckpoint('weights/hand_weights{epoch:03d}.h5', save_weights_only=True, period=1) history = model.fit_generator(train_gen, steps_per_epoch=845, epochs=epochs, verbose=1, validation_data=val_gen, validation_steps=25, shuffle=True, callbacks=[checkpoints], max_queue_size=50) with open('history.txt', 'a+') as f:
elif args.stage == "planner": extractor = get_extractor(parser, cnn=args.use_cnn) planner = get_planner(parser, cnn=args.use_cnn) eval_planner(extractor, planner, test=corpus) elif args.stage == "generator": extractor = get_extractor(parser, cnn=args.use_cnn) planner = get_planner(parser, cnn=args.use_cnn) generator = get_generator(parser, cnn=args.use_cnn) eval_generator(extractor, planner, generator, test=corpus, planner=args.use_planner) elif args.command == "generate": extractor = get_extractor(parser, cnn=args.use_cnn) planner = get_planner(parser, cnn=args.use_cnn) generator = get_generator(parser, cnn=args.use_cnn) genMdFiles(extractor, planner, generator, args.corpus, planner=args.use_planner) elif args.command == "train": if args.stage == "extractor": train_extractor(cnn=args.use_cnn) elif args.stage == "planner": extractor = get_extractor(parser, cnn=args.use_cnn) train_planner(extractor) elif args.stage == "generator": extractor = get_extractor(parser, cnn=args.use_cnn) planner = get_planner(parser, cnn=args.use_cnn) train_generator(extractor, planner) elif args.stage == "pipeline": extractor = train_extractor(cnn=args.use_cnn) planner = train_planner(extractor) train_generator(extractor, planner)