Ejemplo n.º 1
0
 def __init__(self,
              model_container,
              generator,
              num_samples_stats,
              num_display_sentences,
              output_dir=None):
     self.model_container = model_container
     self.output_dir = output_dir
     self.generator = generator
     self.num_samples_stats = num_samples_stats
     self.num_display_sentences = num_display_sentences
     self.decoder = Decoder(beam_width=beam_width)
     print("output directory is ", output_dir)
     if output_dir is not None and not os.path.exists(self.output_dir):
         os.makedirs(self.output_dir)
Ejemplo n.º 2
0
 def __init__(self,
              env,
              other_players,
              n_seats,
              stacks=2500,
              encoding='norm',
              encoder=None,
              decoder=None,
              visualize=False,
              debug=False):
     assert len(other_players) == n_seats - 1
     self.env = env
     self.other_players = other_players
     self.n_seats = n_seats
     self._debug = debug
     self._visualize = visualize
     self._encoder = encoder if not encoder is None else Encoder(
         n_seats, ranking_encoding=encoding)
     self._decoder = decoder if not decoder is None else Decoder()
     self._add_players(n_seats, stacks)
Ejemplo n.º 3
0
        from decoders import gridrnn_Decoder as Decoder
    elif args.dec_type == 'hidden':
        from decoders import Hidden_Decoder as Decoder
    else:
        from decoders import gridrnn_Decoder as Decoder

    if args.d_type == 'dcgan':
        from discriminators import DCGAN_discriminator as Discriminator
    #elif args.d_type == 'hidden':
    else:
        from discriminators import Hidden_discriminator as Discriminator
    #else:
    #from discriminators import DCGAN_discriminator as Discriminator

    generator = Generator(args)
    decoder = Decoder(args)
    discriminator = Discriminator(args)

    if cuda:
        generator.cuda()
        discriminator.cuda()
        decoder.cuda()
        BCELoss.cuda()
        MSELoss.cuda()
    else:
        print('models', generator, discriminator, decoder)

    # Initialize weights
    generator.apply(weights_init_normal)
    discriminator.apply(weights_init_normal)
    decoder.apply(weights_init_normal)
Ejemplo n.º 4
0
                        shuffle=False,
                        collate_fn=dataset.collate_fn)

# iterations per epoch
setattr(args, 'iter_per_epoch',
        math.ceil(dataset.num_data_points[args.split] / args.batch_size))
print("{} iter per epoch.".format(args.iter_per_epoch))

# ----------------------------------------------------------------------------
# setup the model
# ----------------------------------------------------------------------------

encoder = Encoder(model_args)
encoder.load_state_dict(components['encoder'])

decoder = Decoder(model_args, encoder)
decoder.load_state_dict(components['decoder'])
print("Loaded model from {}".format(args.load_path))

if args.gpuid >= 0:
    encoder = encoder.cuda()
    decoder = decoder.cuda()

# ----------------------------------------------------------------------------
# evaluation
# ----------------------------------------------------------------------------

print("Evaluation start time: {}".format(
    datetime.datetime.strftime(datetime.datetime.utcnow(),
                               '%d-%b-%Y-%H:%M:%S')))
encoder.eval()
Ejemplo n.º 5
0
def train(run_name, speaker, start_epoch, stop_epoch, img_c, img_w, img_h,
          frames_n, absolute_max_string_len, minibatch_size):
    DATASET_DIR = os.path.join(CURRENT_PATH, speaker, 'datasets')
    OUTPUT_DIR = os.path.join(CURRENT_PATH, speaker, 'results')
    LOG_DIR = os.path.join(CURRENT_PATH, speaker, 'logs')

    curriculum = Curriculum(curriculum_rules)
    lip_gen = BasicGenerator(dataset_path=DATASET_DIR,
                             minibatch_size=minibatch_size,
                             img_c=img_c,
                             img_w=img_w,
                             img_h=img_h,
                             frames_n=frames_n,
                             absolute_max_string_len=absolute_max_string_len,
                             curriculum=curriculum,
                             start_epoch=start_epoch).build()

    lipnet = LipNet(img_c=img_c,
                    img_w=img_w,
                    img_h=img_h,
                    frames_n=frames_n,
                    absolute_max_string_len=absolute_max_string_len,
                    output_size=lip_gen.get_output_size())
    lipnet.summary()

    adam = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)

    # the loss calc occurs elsewhere, so use a dummy lambda func for the loss
    lipnet.model.compile(loss={
        'ctc': lambda y_true, y_pred: y_pred
    },
                         optimizer=adam)

    # load weight if necessary
    if start_epoch > 0:
        weight_file = os.path.join(
            OUTPUT_DIR,
            os.path.join(run_name, 'weights%02d.h5' % (start_epoch - 1)))
        lipnet.model.load_weights(weight_file)

    spell = Spell(path=PREDICT_DICTIONARY)
    decoder = Decoder(greedy=PREDICT_GREEDY,
                      beam_width=PREDICT_BEAM_WIDTH,
                      postprocessors=[labels_to_text, spell.sentence])

    # define callbacks
    statistics = Statistics(lipnet,
                            lip_gen.next_val(),
                            decoder,
                            256,
                            output_dir=os.path.join(OUTPUT_DIR, run_name))
    visualize = Visualize(os.path.join(OUTPUT_DIR, run_name),
                          lipnet,
                          lip_gen.next_val(),
                          decoder,
                          num_display_sentences=minibatch_size)
    tensorboard = TensorBoard(log_dir=os.path.join(LOG_DIR, run_name))
    csv_logger = CSVLogger(os.path.join(
        LOG_DIR, "{}-{}.csv".format('training', run_name)),
                           separator=',',
                           append=True)
    checkpoint = ModelCheckpoint(os.path.join(OUTPUT_DIR, run_name,
                                              "weights{epoch:02d}.h5"),
                                 monitor='val_loss',
                                 save_weights_only=True,
                                 mode='auto',
                                 period=1)

    lipnet.model.fit_generator(generator=lip_gen.next_train(),
                               steps_per_epoch=lip_gen.default_training_steps,
                               epochs=stop_epoch,
                               validation_data=lip_gen.next_val(),
                               validation_steps=2,
                               callbacks=[
                                   checkpoint, statistics, visualize, lip_gen,
                                   tensorboard, csv_logger
                               ],
                               initial_epoch=start_epoch,
                               verbose=1,
                               max_q_size=5,
                               workers=2,
                               pickle_safe=True)