Ejemplo n.º 1
0
def main():
    pygame.init()
 
    size = [SCREEN_WIDTH, SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size)
 
    pygame.display.set_caption("Donkey Kong")
    
    
    score=0
                
    player = Player()
    player.score = score             
    current_level = Level_num(player)

    don = Donkey()
    don.level = current_level
    current_level.donkey.add(don)

    k = 845
    for i in range(1):
        for j in range(10):
            fireball = Fireball()
            fireball.rect.x = random.randint(300,800)
            fireball.rect.y = SCREEN_HEIGHT - k
            fireball.level = current_level
            fireball.player = player
            fireball.donkey = don
            current_level.fireball_list.add(fireball)
            k-=80

    k=845
    for i in range(10):
        for j in range(2):
            coin = Coins()
            coin.rect.x = random.randint(300,800)
            coin.rect.y = SCREEN_HEIGHT - random.randint(k,k+50)
            current_level.coins_list.add(coin)
        k-=80

    q = Queen()
    current_level.queen.add(q)

    active_sprite_list = pygame.sprite.Group()

    player.level = current_level


    player.rect.x = 340
    player.rect.y = SCREEN_HEIGHT - 2*player.rect.height
    active_sprite_list.add(player)


    done = False

    clock = pygame.time.Clock()

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    player.go_left()
                if event.key == pygame.K_d:
                    player.go_right()
                if event.key == pygame.K_w:
                    player.go_down()
                if event.key == pygame.K_s:
                    player.go_up()
                if event.key == pygame.K_SPACE:
                    player.jump()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a and player.change_x < 0:
                    player.stop()
                if event.key == pygame.K_d and player.change_x > 0:
                    player.stop()
                if event.key == pygame.K_w and player.change_y < 0:
                    player.stop()
                if event.key == pygame.K_s and player.change_y > 0:
                    player.stop()
        
        
            # if not player.chances:
            #     game = False
            #     break 
        active_sprite_list.update()

        current_level.update()

        #fireball_group.update()

        if player.rect.right > SCREEN_WIDTH:
            player.rect.right = SCREEN_WIDTH

        if player.rect.left < 0:
            player.rect.left = 0
        
        current_level.draw(screen)
        active_sprite_list.draw(screen)
        clock.tick(60)

        pygame.display.flip()
Ejemplo n.º 2
0
    def __call__(self):
        import msvcrt
        return msvcrt.getch()

screen=set_screen()

player = Player(28,1)
player.arr = screen.arr
fireballs = Fireball()
fireballs.arr = player.arr
fireballs.pposx = player.posx
fireballs.pposy = player.posy
fireballs.life = player.life

don = Donkey()
don.arr = player.arr
don.set_pos(7,1)
quen = Queen()
quen.arr = player.arr
quen.set_pos(2,17)
for i in range(30):
	for j in range(45):
		sys.stdout.write(screen.arr[i][j])
	print ''	

getch =_Getch()
while(1):
	player.m = getch()
	if player.m=='Q':
		print "You Quit!!"	
Ejemplo n.º 3
0
    def evaluate(self, path_to_checkpoint, path_to_tfrecords_file,
                 num_examples, global_step, defend_layer, attacker_type):
        batch_size = 32
        num_batches = num_examples // batch_size
        needs_include_length = False

        with tf.Graph().as_default():
            image_batch, length_batch, digits_batch = Donkey.build_batch(
                path_to_tfrecords_file,
                num_examples=num_examples,
                batch_size=batch_size,
                shuffled=False)
            with tf.variable_scope('model'):
                length_logits, digits_logits, hidden_out = Model.inference(
                    image_batch,
                    drop_rate=0.0,
                    is_training=False,
                    defend_layer=defend_layer)
            with tf.variable_scope('defender'):
                recovered = Attacker.recover_hidden(attacker_type,
                                                    hidden_out,
                                                    is_training=False,
                                                    defend_layer=defend_layer)
            ssim = tf.reduce_mean(
                tf.abs(tf.image.ssim(image_batch, recovered, max_val=2)))
            length_predictions = tf.argmax(length_logits, axis=1)
            digits_predictions = tf.argmax(digits_logits, axis=2)

            if needs_include_length:
                labels = tf.concat(
                    [tf.reshape(length_batch, [-1, 1]), digits_batch], axis=1)
                predictions = tf.concat([
                    tf.reshape(length_predictions, [-1, 1]), digits_predictions
                ],
                                        axis=1)
            else:
                labels = digits_batch
                predictions = digits_predictions

            labels_string = tf.reduce_join(tf.as_string(labels), axis=1)
            predictions_string = tf.reduce_join(tf.as_string(predictions),
                                                axis=1)

            accuracy, update_accuracy = tf.metrics.accuracy(
                labels=labels_string, predictions=predictions_string)

            tf.summary.image('image', image_batch, max_outputs=20)
            tf.summary.image('recovered', recovered, max_outputs=20)
            tf.summary.scalar('ssim', ssim)
            tf.summary.scalar('accuracy', accuracy)
            tf.summary.histogram(
                'variables',
                tf.concat([
                    tf.reshape(var, [-1]) for var in tf.trainable_variables()
                ],
                          axis=0))
            summary = tf.summary.merge_all()

            with tf.Session() as sess:
                sess.run([
                    tf.global_variables_initializer(),
                    tf.local_variables_initializer()
                ])
                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(sess=sess, coord=coord)

                restorer = tf.train.Saver()
                restorer.restore(sess, path_to_checkpoint)

                for _ in range(num_batches):
                    sess.run(update_accuracy)

                accuracy_val, summary_val = sess.run([accuracy, summary])
                self.summary_writer.add_summary(summary_val,
                                                global_step=global_step)

                coord.request_stop()
                coord.join(threads)

        return accuracy_val
Ejemplo n.º 4
0
def _train(path_to_train_tfrecords_file, num_train_examples,
           path_to_val_tfrecords_file, num_val_examples, path_to_train_log_dir,
           path_to_restore_checkpoint_file, training_options):
    batch_size = training_options['batch_size']
    initial_patience = training_options['patience']
    num_steps_to_show_loss = 100
    num_steps_to_check = 1000

    with tf.Graph().as_default():
        image_batch, length_batch, digits_batch = Donkey.build_batch(
            path_to_train_tfrecords_file,
            num_examples=num_train_examples,
            batch_size=batch_size,
            shuffled=True)
        # length_logtis, digits_logits = Model.inference(image_batch, drop_rate=0.2)
        length_logtis, digits_logits = Model.forward(image_batch, 0.8)

        loss = Model.loss(length_logtis, digits_logits, length_batch,
                          digits_batch)

        global_step = tf.Variable(0, name='global_step', trainable=False)
        learning_rate = tf.train.exponential_decay(
            training_options['learning_rate'],
            global_step=global_step,
            decay_steps=training_options['decay_steps'],
            decay_rate=training_options['decay_rate'],
            staircase=True)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train_op = optimizer.minimize(loss, global_step=global_step)

        tf.summary.image('image', image_batch)
        tf.summary.scalar('loss', loss)
        tf.summary.scalar('learning_rate', learning_rate)
        summary = tf.summary.merge_all()

        with tf.Session() as sess:
            summary_writer = tf.summary.FileWriter(path_to_train_log_dir)
            summary_writer.add_graph(graph=sess.graph)
            evaluator = Evaluator(
                os.path.join(path_to_train_log_dir, 'eval/val'))

            sess.run(tf.global_variables_initializer())
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            saver = tf.train.Saver()
            if path_to_restore_checkpoint_file is not None:
                assert tf.train.checkpoint_exists(path_to_restore_checkpoint_file), \
                    '%s not found' % path_to_restore_checkpoint_file
                saver.restore(sess, path_to_restore_checkpoint_file)
                print('Model restored from file: %s' %
                      path_to_restore_checkpoint_file)

            print('Start training')
            patience = initial_patience
            best_accuracy = 0.0
            duration = 0.0

            while True:
                start_time = time.time()
                _, loss_val, summary_val, global_step_val, learning_rate_val = sess.run(
                    [train_op, loss, summary, global_step, learning_rate])
                duration += time.time() - start_time

                if global_step_val % num_steps_to_show_loss == 0:
                    examples_per_sec = batch_size * num_steps_to_show_loss / duration
                    duration = 0.0
                    print('=> %s: step %d, loss = %f (%.1f examples/sec)' %
                          (datetime.now(), global_step_val, loss_val,
                           examples_per_sec))

                if global_step_val % num_steps_to_check != 0:
                    continue

                summary_writer.add_summary(summary_val,
                                           global_step=global_step_val)

                print('=> Evaluating on validation dataset...')
                path_to_latest_checkpoint_file = saver.save(
                    sess, os.path.join(path_to_train_log_dir, 'latest.ckpt'))
                accuracy = evaluator.evaluate(path_to_latest_checkpoint_file,
                                              path_to_val_tfrecords_file,
                                              num_val_examples,
                                              global_step_val)
                print('==> accuracy = %f, best accuracy %f' %
                      (accuracy, best_accuracy))

                if accuracy > best_accuracy:
                    path_to_checkpoint_file = saver.save(
                        sess,
                        os.path.join(path_to_train_log_dir, 'model.ckpt'),
                        global_step=global_step_val)
                    print('=> Model saved to file: %s' %
                          path_to_checkpoint_file)
                    patience = initial_patience
                    best_accuracy = accuracy
                else:
                    patience -= 1

                print('=> patience = %d' % patience)
                if patience == 0:
                    break

            coord.request_stop()
            coord.join(threads)
            print('Finished')

        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess, sess.graph_def, 'output')
        with tf.gfile.FastGFile('./model/NumberCamera.pb', 'wb') as f:
            f.write(output_graph_def.SerializeToString())
Ejemplo n.º 5
0
def main():
    pygame.init()

    screen = pygame.display.set_mode((WIDTH,HEIGHT))
    pygame.display.set_caption("DonkeyKong")
    
    # Initialize level
    
    lifes = 3
    score = 0
    COINS = 20
    times = 0

    done = False
    donkey = Donkey()
    donkey_group = pygame.sprite.GroupSingle(donkey.makeDonkey())

    hero = Player()
    hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes))

    fireball_group =  pygame.sprite.OrderedUpdates()
    Fireballs = []
    
    fireballS = Fireball()
    fireSource = pygame.sprite.GroupSingle(fireballS.makeFireSource())
    
    coinGroup = comLevels.genCoins(COINS)

    pygame.key.set_repeat(8,10)
    FIRETIME = pygame.USEREVENT + 1
    pygame.time.set_timer(pygame.USEREVENT, 150)
    pygame.time.set_timer(FIRETIME, 2000)

    while done == False :

        for event in pygame.event.get() :
            if event.type == pygame.QUIT :
                done = True
            if event.type == pygame.KEYDOWN :
                if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
                    done = True
                elif event.key == pygame.K_SPACE :
                    hero.setState("JUMPING")
                elif event.key == pygame.K_w or event.key == pygame.K_UP :
                    hero.moveUP(screen,hero_group,level.getStairGroup())
                elif event.key == pygame.K_s or event.key == pygame.K_DOWN :
                    hero.moveDown(screen,hero_group,level.getStairGroup())
                elif event.key == pygame.K_a or event.key == pygame.K_LEFT :
                    if hero.getState == ("JUMPING") :
                        hero.allowLeft(True)
                    hero.moveLeft(screen)
                elif event.key == pygame.K_d or event.key == pygame.K_RIGHT :
                    if hero.getState() == "JUMPING" :
                        hero.allowRight(True)
                    hero.moveRigth(screen)
                elif event.key == pygame.K_f :
                    for ball in Fireballs :
                        ball.setSpeedP()
                elif event.key == pygame.K_b :
                    for ball in Fireballs :
                        ball.setSpeedN()
            if event.type == pygame.USEREVENT :
                donkey.move()
                donkey.chgSpeed()
            if event.type == FIRETIME :
                fireball = Fireball()
                fireball_group.add(fireball.makeFireball())            
                Fireballs.append(fireball)
            for ball in Fireballs :
                ball.moveBalls()
                ball.gravity()
                ball.onBarless()
                ball.killFireball()

        collDon = pygame.sprite.groupcollide(donkey_group,hero_group,False,True)
        collFire = pygame.sprite.groupcollide(fireball_group,hero_group,False,True)
        if len(collDon) > 0 or len(collFire) > 0 :
            if score >= 25 :
                score -= 25
            else :
                score = 0
            print "player: D " + str(hero.getPosition("D"))
            print "player: L " + str(hero.getPosition("L"))
            print "donkey: D " + str(donkey.getPosition("D"))
            print "donkey: L " + str(donkey.getPosition("L"))
            lifes = hero.playerDied(screen)
            pygame.display.update()
            hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes))
            try:
                time.sleep(3)
            except :
                print "key"
        
        collCoin = pygame.sprite.groupcollide(coinGroup,hero_group,True,False)
        if len(collCoin) > 0 :
            score += 20
        
        if hero.getState() == "JUMPING":
            hero.printPos()
            print "--------"
        times = hero.jump(times)
        if times == 4:
            hero.setState("STANDING")
            hero.allowLeft(False)
            hero.allowRight(False)
            times = 0
        level.selectLevel(screen,1)
        coinGroup.draw(screen)
        hero_group.draw(screen)
        fireSource.draw(screen)
        fireball_group.draw(screen)
        donkey_group.draw(screen)
        comLevels.updateLife(lifes,screen)
        comLevels.updateScore(score,screen)
        pygame.display.update()
        
        if lifes == 0:
            break


    pygame.quit()
    sys.exit()
def _train(path_to_train_tfrecords_file, num_train_examples,
           path_to_val_tfrecords_file, num_val_examples, path_to_train_log_dir,
           path_to_restore_checkpoint_file, training_options):
    batch_size = training_options['batch_size']
    num_steps_to_show_loss = 100
    num_steps_to_check = 2000

    with tf.Graph().as_default():
        image_batch, length_batch, digits_batch = Donkey.build_batch(
            path_to_train_tfrecords_file,
            num_example=num_train_examples,
            batch_size=batch_size,
            shuffled=True)
        length_logits, digits_logits = Model.inference(image_batch,
                                                       drop_rate=0.2)
        loss = Model.loss(length_logits, digits_logits, length_batch,
                          digits_batch)

        global_step = tf.Variable(0, name='global_step', trainable=False)
        learning_rate = tf.train.exponential_decay(
            training_options['learning_rate'],
            global_step=global_step,
            decay_steps=training_options['decay_steps'],
            decay_rate=training_options['decay_rate'])
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train_op = optimizer.minimize(loss, global_step=global_step)

        tf.summary.image('image', image_batch)
        tf.summary.scalar('learning_rate', learning_rate)
        tf.summary.scalar('loss', loss)
        summary = tf.summary.merge_all()

        with tf.Session() as sess:
            summary_writer = tf.summary.FileWriter(path_to_train_log_dir,
                                                   sess.graph)
            evaluator = Evaluator(
                os.path.join(path_to_train_log_dir, 'eval/val'))

            sess.run(tf.global_variables_initializer())
            coord = tf.train.Coordinator()
            thread = tf.train.start_queue_runners(sess=sess, coord=coord)

            saver = tf.train.Saver()

            if path_to_restore_checkpoint_file is not None:
                assert tf.train.checkpoint_exists(
                    path_to_restore_checkpoint_file
                ), '%s not found' % path_to_restore_checkpoint_file
                saver.restore(sess, path_to_restore_checkpoint_file)
                print('Model restord from file: %s' %
                      path_to_restore_checkpoint_file)

            print('Start training')
            best_accuracy = 0.0
            duration = 0
            num_epoches = training_options['epoches']

            # for epoch in range(training_options['epoches']):
            while True:
                start_time = time.time()
                _, loss_val, summary_val, global_step_val, learning_rate_val = sess.run(
                    [train_op, loss, summary, global_step, learning_rate])
                duration += time.time() - start_time

                if global_step_val % num_steps_to_show_loss == 0:
                    example_per_sec = batch_size * num_steps_to_show_loss / duration
                    duration = 0.0
                    print('%s: step %d, loss = %f (%.1f examples/sec)' %
                          (datetime.now(), global_step_val, loss_val,
                           example_per_sec))

                if global_step_val % num_steps_to_check != 0:
                    continue

                summary_writer.add_summary(summary_val,
                                           global_step=global_step_val)

                print('Evaluating on validation dataset...')
                path_to_latest_checkpoint_file = saver.save(
                    sess, os.path.join(path_to_train_log_dir, 'latest.ckpt'))
                accuracy = evaluator.evaluate(path_to_latest_checkpoint_file,
                                              path_to_val_tfrecords_file,
                                              num_val_examples,
                                              global_step_val)
                print('Accuracy = %f, best accuracy = %f' %
                      (accuracy, best_accuracy))

                if accuracy > best_accuracy:
                    path_to_checkpoint_file = saver.save(
                        sess,
                        os.path.join(path_to_train_log_dir, 'model.ckpt'),
                        global_step=global_step_val)
                    print('Model saved to file: %s' %
                          (path_to_checkpoint_file))
                    best_accuracy = accuracy
                    num_epoches = training_options['epoches']
                else:
                    num_epoches -= 1

                print('epoch = %d' % num_epoches)
                if num_epoches == 0:
                    break

            coord.request_stop()
            coord.join(thread)
            print('Finished.')
Ejemplo n.º 7
0
def main():
    pygame.init()

    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("DonkeyKong")

    # Initialize level

    lifes = 3
    score = 0
    COINS = 20
    times = 0

    done = False
    donkey = Donkey()
    donkey_group = pygame.sprite.GroupSingle(donkey.makeDonkey())

    hero = Player()
    hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes))

    fireball_group = pygame.sprite.OrderedUpdates()
    Fireballs = []

    fireballS = Fireball()
    fireSource = pygame.sprite.GroupSingle(fireballS.makeFireSource())

    coinGroup = comLevels.genCoins(COINS)

    pygame.key.set_repeat(8, 10)
    FIRETIME = pygame.USEREVENT + 1
    pygame.time.set_timer(pygame.USEREVENT, 150)
    pygame.time.set_timer(FIRETIME, 2000)

    while done == False:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
                    done = True
                elif event.key == pygame.K_SPACE:
                    hero.setState("JUMPING")
                elif event.key == pygame.K_w or event.key == pygame.K_UP:
                    hero.moveUP(screen, hero_group, level.getStairGroup())
                elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    hero.moveDown(screen, hero_group, level.getStairGroup())
                elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    if hero.getState == ("JUMPING"):
                        hero.allowLeft(True)
                    hero.moveLeft(screen)
                elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    if hero.getState() == "JUMPING":
                        hero.allowRight(True)
                    hero.moveRigth(screen)
                elif event.key == pygame.K_f:
                    for ball in Fireballs:
                        ball.setSpeedP()
                elif event.key == pygame.K_b:
                    for ball in Fireballs:
                        ball.setSpeedN()
            if event.type == pygame.USEREVENT:
                donkey.move()
                donkey.chgSpeed()
            if event.type == FIRETIME:
                fireball = Fireball()
                fireball_group.add(fireball.makeFireball())
                Fireballs.append(fireball)
            for ball in Fireballs:
                ball.moveBalls()
                ball.gravity()
                ball.onBarless()
                ball.killFireball()

        collDon = pygame.sprite.groupcollide(donkey_group, hero_group, False,
                                             True)
        collFire = pygame.sprite.groupcollide(fireball_group, hero_group,
                                              False, True)
        if len(collDon) > 0 or len(collFire) > 0:
            if score >= 25:
                score -= 25
            else:
                score = 0
            print "player: D " + str(hero.getPosition("D"))
            print "player: L " + str(hero.getPosition("L"))
            print "donkey: D " + str(donkey.getPosition("D"))
            print "donkey: L " + str(donkey.getPosition("L"))
            lifes = hero.playerDied(screen)
            pygame.display.update()
            hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes))
            try:
                time.sleep(3)
            except:
                print "key"

        collCoin = pygame.sprite.groupcollide(coinGroup, hero_group, True,
                                              False)
        if len(collCoin) > 0:
            score += 20

        if hero.getState() == "JUMPING":
            hero.printPos()
            print "--------"
        times = hero.jump(times)
        if times == 4:
            hero.setState("STANDING")
            hero.allowLeft(False)
            hero.allowRight(False)
            times = 0
        level.selectLevel(screen, 1)
        coinGroup.draw(screen)
        hero_group.draw(screen)
        fireSource.draw(screen)
        fireball_group.draw(screen)
        donkey_group.draw(screen)
        comLevels.updateLife(lifes, screen)
        comLevels.updateScore(score, screen)
        pygame.display.update()

        if lifes == 0:
            break

    pygame.quit()
    sys.exit()
Ejemplo n.º 8
0
def _train(path_to_train_tfrecords_file, num_train_examples, path_to_val_tfrecords_file, num_val_examples,
           path_to_train_log_dir, path_to_restore_checkpoint_file, training_options):
    batch_size = training_options['batch_size']
    initial_patience = training_options['patience']
    num_steps_to_show_loss = 100
    num_steps_to_check = 1000

    with tf.Graph().as_default():
        image_batch, length_batch, digits_batch = Donkey.build_batch(path_to_train_tfrecords_file,
                                                                     num_examples=num_train_examples,
                                                                     batch_size=batch_size,
                                                                     shuffled=True)
        length_logtis, digits_logits = Model.inference(image_batch, drop_rate=0.2)
        loss = Model.loss(length_logtis, digits_logits, length_batch, digits_batch)

        global_step = tf.Variable(0, name='global_step', trainable=False)
        learning_rate = tf.train.exponential_decay(training_options['learning_rate'], global_step=global_step,
                                                   decay_steps=training_options['decay_steps'], decay_rate=training_options['decay_rate'], staircase=True)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train_op = optimizer.minimize(loss, global_step=global_step)

        tf.summary.image('image', image_batch)
        tf.summary.scalar('loss', loss)
        tf.summary.scalar('learning_rate', learning_rate)
        summary = tf.summary.merge_all()

        with tf.Session() as sess:
            summary_writer = tf.summary.FileWriter(path_to_train_log_dir, sess.graph)
            evaluator = Evaluator(os.path.join(path_to_train_log_dir, 'eval/val'))

            sess.run(tf.global_variables_initializer())
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            saver = tf.train.Saver()
            if path_to_restore_checkpoint_file is not None:
                assert tf.train.checkpoint_exists(path_to_restore_checkpoint_file), \
                    '%s not found' % path_to_restore_checkpoint_file
                saver.restore(sess, path_to_restore_checkpoint_file)
                print 'Model restored from file: %s' % path_to_restore_checkpoint_file

            print 'Start training'
            patience = initial_patience
            best_accuracy = 0.0
            duration = 0.0

            while True:
                start_time = time.time()
                _, loss_val, summary_val, global_step_val, learning_rate_val = sess.run([train_op, loss, summary, global_step, learning_rate])
                duration += time.time() - start_time

                if global_step_val % num_steps_to_show_loss == 0:
                    examples_per_sec = batch_size * num_steps_to_show_loss / duration
                    duration = 0.0
                    print '=> %s: step %d, loss = %f (%.1f examples/sec)' % (
                        datetime.now(), global_step_val, loss_val, examples_per_sec)

                if global_step_val % num_steps_to_check != 0:
                    continue

                summary_writer.add_summary(summary_val, global_step=global_step_val)

                print '=> Evaluating on validation dataset...'
                path_to_latest_checkpoint_file = saver.save(sess, os.path.join(path_to_train_log_dir, 'latest.ckpt'))
                accuracy = evaluator.evaluate(path_to_latest_checkpoint_file, path_to_val_tfrecords_file,
                                              num_val_examples,
                                              global_step_val)
                print '==> accuracy = %f, best accuracy %f' % (accuracy, best_accuracy)

                if accuracy > best_accuracy:
                    path_to_checkpoint_file = saver.save(sess, os.path.join(path_to_train_log_dir, 'model.ckpt'),
                                                         global_step=global_step_val)
                    print '=> Model saved to file: %s' % path_to_checkpoint_file
                    patience = initial_patience
                    best_accuracy = accuracy
                else:
                    patience -= 1

                print '=> patience = %d' % patience
                if patience == 0:
                    break

            coord.request_stop()
            coord.join(threads)
            print 'Finished'
class Game:
    def __init__(self):
        WIDTH = 192
        HEIGHT = 250
        CAPTION = "Mario Game Beta 5.0"

        self.mario = Mario()
        self.kong = Donkey()
        self.win_condition = False

        self.barrels = {}
        #######################
        self.platforms = {}
        for i in range(1, 24):
            if i < 3:
                name = 'platform_' + str(i)
                x = 16 * (i - 1)
                y = 242
                obj = statics.Platform(x, y)
                self.platforms[name] = obj
            elif i < 14:
                for j in range(2):
                    if j == 0:
                        name = 'platform_' + str(i) + '.' + str(j + 1)
                        x = 16 * (i - 1)
                        y = 242 - (i - 2)
                        obj = statics.Platform(x, y)
                        self.platforms[name] = obj
                    elif j == 1:
                        name = 'platform_' + str(i) + '.' + str(j + 1)
                        x = 16 * (i - 1)
                        y = 140 - (i - 2)
                        obj = statics.Platform(x, y)
                        self.platforms[name] = obj
            elif i > 13:
                if i == 16:
                    name = 'platform_' + str(i)
                    x = 16 * (i - 15)
                    y = 180 + 1
                    obj = statics.Platform(x, y)
                    self.platforms[name] = obj
                else:
                    name = 'platform_' + str(i)
                    x = 16 * (i - 15)
                    y = 180 + (i - 15)
                    obj = statics.Platform(x, y)
                    self.platforms[name] = obj

        for i in range(10):
            if i < 3:
                name = 'platform' + str(100 + i)
                x = 16 * i
                y = 70
                obj = statics.Platform(x, y)
                self.platforms[name] = obj
            else:
                name = 'platform' + str(100 + i)
                x = 16 * i
                y = 70 + (i - 2)
                obj = statics.Platform(x, y)
                self.platforms[name] = obj

        for i in range(6):
            name = 'platform' + str(200 + i)
            x = 100 + 16 * i
            y = 40
            obj = statics.Platform(x, y)
            self.platforms[name] = obj
        ################
        self.invisiplats = {}
        name = 'inv_1'
        x = 145
        y = 188
        obj = statics.InvisiPlat(x, y)
        self.invisiplats[name] = obj
        name = 'inv_2'
        x = 160
        y = 77
        obj = statics.InvisiPlat(x, y)
        self.invisiplats[name] = obj
        name = 'inv_3'
        x = 32
        y = 139
        obj = statics.InvisiPlat(x, y)
        self.invisiplats[name] = obj
        ################
        self.ladders = {}

        name = 'ladder_1'
        x = 118
        yl = 229
        yh = 187
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        name = 'ladder_2'
        x = 50
        yl = 176
        yh = 138
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        name = 'ladder_3'
        x = 130
        yl = 126
        yh = 76
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        name = 'ladder_4'
        x = 101
        yl = 67
        yh = 41
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        ##################
        self.scores_path = 'highscores.txt'
        try:
            with open(self.scores_path, "r") as file:
                self.highscores = eval(file.readline())

        except:
            self.highscores = []
            for i in range(5):
                self.highscores.append(0)

            with open(self.scores_path, "w") as file:
                file.write(str(self.highscores))
        ##################
        """
        We thought at first that ladders could be iterably generated, but soon
        changed our mind. 
        self.ladders = {}
        for i in range(1,9):
            name = 'ladder_' + str(i)
            x = 117
            y = 236-6*i
            obj = classes.Ladder(x,y)
            self.ladders[name] = obj
        """
        pyxel.init(WIDTH, HEIGHT, caption=CAPTION)
        pyxel.load("opjects.pyxres")
        pyxel.playm(0, loop=True)

        pyxel.run(self.update, self.draw)

    def update(self):
        if not self.mario.dead and not self.mario.win:
            self.win_condition = False
            self.mario.onplat = False
            self.mario.onladder = False
            self.mario.falling = False
            barrels_clear = False
            for j in self.barrels:
                self.barrels[j].onPlat = False
                self.barrels[j].ladder = False

            del_barrel = False

            b = random.randint(1, 3)
            if pyxel.frame_count % (30 * b) == 0:
                if len(self.barrels) < 10:
                    self.kong.throwing = True
                    self.kong.waiting = False
                    self.kong.grabbing_barrel = False
                    l = self.kong.throwBarrel()
                    self.barrels[l[0]] = l[1]

            #Mario WALK ON NON-HORIZONTAL platforms
            if self.mario.posY > 45:
                for i in self.platforms:
                    if self.mario.posY < 100:
                        if self.mario.posX + 5 == self.platforms[
                                i].posX and self.mario.posY + 16 == self.platforms[
                                    i].posY:  #fixes a bug when climbing the 3rd ladder
                            self.mario.posY -= 1
                    if self.mario.posY > 200 or (self.mario.posY < 160
                                                 and self.mario.posY > 100):

                        if self.mario.posX + 12 == self.platforms[
                                i].posX and self.mario.posY + 15 == self.platforms[
                                    i].posY and self.mario.direct == 'R':
                            self.mario.posY -= 1
                            self.mario.posX += 1

                        if self.mario.posX + 12 == self.platforms[
                                i].posX and self.mario.posY + 16 == self.platforms[
                                    i].posY and self.mario.direct == 'L':
                            self.mario.posY += 1
                            self.mario.posX -= 1

                        if self.mario.posX + 13 == self.platforms[
                                i].posX and self.mario.posY + 16 == self.platforms[
                                    i].posY and self.mario.direct == 'L' and i != 1:
                            self.mario.posY += 1
                            self.mario.posX -= 1
                    #BUG FIX
                    if (self.mario.posX + 6 == self.platforms[i].posX) and (
                            self.mario.posY + 16
                            or self.mario.posY + 17) == self.platforms[i].posY:
                        self.mario.posY -= 1

                    #BUG FIX
                    if (self.mario.posX + 6 == self.platforms[i].posX
                        ) and self.mario.posY + 14 == self.platforms[i].posY:
                        self.mario.posY -= 1

                    #BUG FIX
                    if (self.mario.posX + 2 == self.platforms[i].posX
                            or self.mario.posX + 1 == self.platforms[i].posX
                            or self.mario.posX == self.platforms[i].posX
                            or self.mario.posX - 1 == self.platforms[i].posX
                        ) and (
                            self.mario.posY + 17 == self.platforms[i].posY
                            or self.mario.posY + 18 == self.platforms[i].posY
                            or self.mario.posY + 19 == self.platforms[i].posY
                        ) and self.mario.onladder == False:
                        self.mario.posY += 1

                    if (self.mario.posY < 200 and self.mario.posY > 160) or (
                            self.mario.posY < 100 and self.mario.posY > 0):
                        if self.mario.posX + 3 == self.platforms[
                                i].posX and self.mario.posY + 16 == self.platforms[
                                    i].posY and self.mario.direct == 'L':
                            self.mario.posY -= 1
                            self.mario.posX -= 1
                        if self.mario.posX + 3 == self.platforms[
                                i].posX and self.mario.posY + 16 == self.platforms[
                                    i].posY and self.mario.direct == 'L':
                            self.mario.posY -= 1
                            self.mario.posX -= 1
                        if self.mario.posX + 4 == self.platforms[
                                i].posX and self.mario.posY + 17 == self.platforms[
                                    i].posY and self.mario.direct == 'R':
                            self.mario.posY += 1
                            self.mario.posX += 1

            else:
                #BUG FIX
                for i in self.platforms:
                    if (self.mario.posX + 6 == self.platforms[i].posX) and (
                            self.mario.posY + 17 == self.platforms[i].posY):
                        self.mario.posY += 1

            #BARRELS ROLLING DOWN THE self.platforms
            for i in self.platforms:
                for j in self.barrels:
                    if self.platforms[i].posY - self.barrels[
                            j].posY < 12 and self.platforms[
                                i].posY - self.barrels[j].posY > 9 and abs(
                                    self.barrels[j].posX -
                                    self.platforms[i].posX) < 16:
                        self.barrels[j].onPlat = True
                        self.barrels[j].falling = False
                if self.platforms[
                        i].posY - self.mario.posY < 18 and self.platforms[
                            i].posY - self.mario.posY > 15 and abs(
                                self.mario.posX - self.platforms[i].posX) < 18:
                    self.mario.onplat = True
                    self.mario.jumping = False

                    if not self.mario.dying:
                        self.mario.platMove()
                        self.mario.jump()
                        for l in self.invisiplats:
                            if 0 < self.invisiplats[
                                    l].posX - self.mario.posX < 17 and 15 < self.invisiplats[
                                        l].posY - self.mario.posY < 18 and (
                                            (self.mario.posY < 200
                                             and self.mario.posY > 160) or
                                            (self.mario.posY < 100
                                             and self.mario.posY > 50)):
                                self.mario.platMove()
                            else:
                                if -16 < self.mario.posX - self.invisiplats[
                                        l].posX < 6 and 15 < self.invisiplats[
                                            l].posY - self.mario.posY < 18:
                                    self.mario.platMove()

                #PREVENTS self.mario FROM SLOWING DOWN AFTER JUMPING
                if self.platforms[
                        i].posY - self.mario.posY < 20 and self.platforms[
                            i].posY - self.mario.posY > 17 and abs(
                                self.mario.posX - self.platforms[i].posX) < 18:
                    if not self.mario.dying:
                        self.mario.platMove()
                        self.mario.jump()

            for j in self.barrels:
                for k in self.ladders:
                    if self.barrels[j].posX + 4 - self.ladders[
                            k].posX < 3 and self.barrels[
                                j].posX + 4 - self.ladders[
                                    k].posX > -5 and self.barrels[
                                        j].posY + 8 < self.ladders[
                                            k].posYlow and self.barrels[
                                                j].posY >= self.ladders[
                                                    k].posYhigh - 17:
                        a = random.randint(1, 4)
                        if a == 1:
                            self.barrels[j].ladder = True
                            self.barrels[j].onplat = False

            #self.mario ON LADDER DETECTION
            for i in self.ladders:
                if self.mario.posX + 2 - self.ladders[
                        i].posX < 4 and self.mario.posX + 2 - self.ladders[
                            i].posX > -4 and self.mario.posY + 8 < self.ladders[
                                i].posYlow and self.mario.posY >= self.ladders[
                                    i].posYhigh - 17:
                    self.mario.onladder = True

            #self.mario ON LADDER MOVEMENT
            for i in self.ladders:
                if self.mario.onladder:
                    self.mario.jumping = False
                    if (self.mario.posY + 10 > self.ladders[i].posYlow
                            or self.mario.posY + 8 < self.ladders[i].posYlow):
                        if not self.mario.dying:
                            self.mario.ladderUp()
                    if self.mario.posY > self.ladders[
                            i].posYhigh - 18 and self.mario.posY + 10 < self.ladders[
                                i].posYlow:
                        if not self.mario.dying:
                            self.mario.ladderDown()

            if self.mario.onplat == False and self.mario.onladder == False:
                self.mario.fall()

            for j in self.barrels:
                if self.barrels[j].ladder == True:
                    self.barrels[j].ladderMove()
                    self.barrels[j].ladderMove()
                    self.barrels[j].falling = True

                elif self.barrels[j].onPlat == False:
                    self.barrels[j].fall()
                else:
                    self.barrels[j].platMove()

            # print(self.mario.jumping)
            for j in self.barrels:
                if math.sqrt((self.mario.posX - self.barrels[j].posX)**2 +
                             (self.mario.posY - self.barrels[j].posY)**2) < 15:
                    self.mario.dying = True

                if self.barrels[j].posX == 0 and self.barrels[j].posY > 200:
                    barrel_n = j
                    del_barrel = True
                if -20 < self.mario.posX - self.barrels[j].posX < 20 and 15 < (
                        self.barrels[j].posY - self.mario.posY) < 40:
                    if self.mario.jumping or self.mario.falling:
                        if self.barrels[j].scoreable:
                            self.mario.inRange = True
                            self.mario.score += 100
                            self.barrels[j].scoreable = False

            if del_barrel == True:
                del (self.barrels[barrel_n])
                del_barrel = False

            if barrels_clear or self.mario.dying:
                self.kong.grabbing_barrel = False
                self.kong.throwing = False
                self.barrels.clear()

            if self.mario.posX > 130 and self.mario.posY < 30:
                self.barrels.clear()
                self.mario.win = True

        else:
            if self.mario.score != 0:
                self.highscores.append(self.mario.score)
                self.bubbleSort(self.highscores)
                self.mario.score = 0
                self.highscores.pop()

        if pyxel.btnp(pyxel.KEY_Q):
            with open(self.scores_path, "w") as file:
                file.write(str(self.highscores))
            pyxel.quit()
        if pyxel.btnp(pyxel.KEY_R):
            with open(self.scores_path, "w") as file:
                file.write(str(self.highscores))
            self.reset()

    def draw(self):
        if not self.mario.dead and not self.mario.win:
            pyxel.cls(0)
            # draw PAULINE
            if self.mario.dying:
                if self.mario.lives == 1:
                    pyxel.blt(150, 19, 2, 122, 196, 16, 12,
                              colkey=0)  # Broken heart
                    pyxel.blt(170, 18, 0, 0, 49, 16, 22, colkey=0)
                else:
                    if (pyxel.frame_count // 3) % 2 == 0:
                        pyxel.blt(170, 18, 0, 16, 49, 16, 22, colkey=0)
                        pyxel.blt(145, 19, 2, 2, 198, 22, 7, colkey=0)  # HELP!
                    else:
                        pyxel.blt(170, 18, 0, 0, 49, 16, 22, colkey=0)
                        pyxel.blt(145, 19, 2, 34, 198, 22, 7,
                                  colkey=0)  # HELP!
            else:
                pyxel.blt(170, 18, 0, 0, 49, 16, 22, colkey=0)

            #draw platforms
            for i in self.platforms:
                pyxel.blt(self.platforms[i].posX,
                          self.platforms[i].posY,
                          0,
                          0,
                          180,
                          16,
                          8,
                          colkey=0)

            #draw ladders
            for i in range(1, 9):
                x = 118
                y = 236 - 6 * i
                pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)
            for i in range(1, 9):
                x = 50
                y = 183 - 6 * i
                pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)
            for i in range(1, 10):
                x = 130
                y = 133 - 6 * i
                pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)
            for i in range(1, 7):
                x = 101
                y = 74 - 6 * i
                pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)

            pyxel.blt(116, 230, 2, 0, 16, 1, 1, colkey=0)

            # draw donkey
            pyxel.blt(0, 60, 0, 2, 3, 12, 10, colkey=0)  # Side barrel
            if self.kong.throwing:
                change = pyxel.frame_count % 7
                pyxel.blt(self.kong.posX,
                          self.kong.posY,
                          0,
                          40,
                          98,
                          42,
                          32,
                          colkey=0)
                if change == 6:
                    self.kong.throwing = False
                    self.kong.grabbing_barrel = True

            elif self.kong.grabbing_barrel:
                change = pyxel.frame_count % 15
                pyxel.blt(self.kong.posX,
                          self.kong.posY,
                          0,
                          84,
                          98,
                          42,
                          32,
                          colkey=0)
                if change == 0:
                    self.kong.grabbing_barrel = False
                    self.kong.waiting = True

            elif self.kong.waiting:
                pyxel.blt(self.kong.posX,
                          self.kong.posY,
                          0,
                          0,
                          146,
                          38,
                          32,
                          colkey=0)

            else:
                pyxel.blt(self.kong.posX,
                          self.kong.posY,
                          0,
                          0,
                          98,
                          38,
                          32,
                          colkey=0)

            #draw mario

            if self.mario.dying:
                pyxel.text(self.mario.posX + 10, self.mario.posY - 7, "-200",
                           8)
                change = (pyxel.frame_count // 6) % 7
                if change == 0 or self.mario.condition:
                    self.mario.condition = True
                    pyxel.blt(self.mario.posX,
                              self.mario.posY,
                              1,
                              16 * change,
                              48,
                              16,
                              16,
                              colkey=0)
                    if change > 5:
                        self.mario.condition = False
                        self.barrels.clear()
                        self.mario.Death()
                else:
                    pyxel.blt(self.mario.posX,
                              self.mario.posY,
                              1,
                              0,
                              48,
                              16,
                              16,
                              colkey=0)

            elif self.mario.onladder:
                change = (pyxel.frame_count // 5) % 2
                if pyxel.btn(pyxel.KEY_DOWN) or pyxel.btn(pyxel.KEY_UP):

                    for i in range(2):
                        pyxel.blt(self.mario.posX,
                                  self.mario.posY,
                                  1,
                                  16 * change,
                                  32,
                                  16,
                                  16,
                                  colkey=0)
                else:
                    pyxel.blt(self.mario.posX,
                              self.mario.posY,
                              1,
                              32,
                              32,
                              16,
                              16,
                              colkey=0)

            elif self.mario.direct == 'R':
                if self.mario.jumping or self.mario.falling:
                    pyxel.blt(self.mario.posX,
                              self.mario.posY,
                              1,
                              31,
                              0,
                              16,
                              16,
                              colkey=0)
                elif pyxel.btn(pyxel.KEY_RIGHT):
                    change = (pyxel.frame_count // 5) % 2
                    for i in range(2):
                        pyxel.blt(self.mario.posX,
                                  self.mario.posY,
                                  1,
                                  16 * change,
                                  0,
                                  15,
                                  16,
                                  colkey=0)
                else:
                    pyxel.blt(self.mario.posX,
                              self.mario.posY,
                              1,
                              0,
                              0,
                              16,
                              16,
                              colkey=0)

            elif self.mario.direct == 'L':
                if self.mario.jumping or self.mario.falling:
                    pyxel.blt(self.mario.posX,
                              self.mario.posY,
                              1,
                              48,
                              16,
                              17,
                              16,
                              colkey=0)
                elif pyxel.btn(pyxel.KEY_LEFT):
                    change = (pyxel.frame_count // 5) % 2
                    for i in range(2):
                        pyxel.blt(self.mario.posX,
                                  self.mario.posY,
                                  1,
                                  16 * change,
                                  16,
                                  16,
                                  16,
                                  colkey=0)
                else:
                    pyxel.blt(self.mario.posX,
                              self.mario.posY,
                              1,
                              0,
                              16,
                              16,
                              16,
                              colkey=0)

            if self.mario.inRange:
                pyxel.text(self.mario.posX + 10, self.mario.posY - 7, "+100",
                           11)
                if self.mario.onplat or self.mario.dying or self.mario.onladder:
                    self.mario.inRange = False

            #draw barrel

            for j in self.barrels:
                if self.barrels[j].ladder or self.barrels[j].falling:
                    change2 = (pyxel.frame_count // 2) % 2
                    for i in range(2):
                        pyxel.blt(self.barrels[j].posX,
                                  self.barrels[j].posY,
                                  0,
                                  16 * change2,
                                  16,
                                  16,
                                  16,
                                  colkey=0)
                else:
                    change2 = (pyxel.frame_count // 3) % 4
                    if self.barrels[j].direct == "R":
                        for i in range(4):
                            pyxel.blt(self.barrels[j].posX,
                                      self.barrels[j].posY,
                                      0,
                                      16 * change2,
                                      3,
                                      16,
                                      10,
                                      colkey=0)
                    elif self.barrels[j].direct == "L":
                        for i in range(4):
                            pyxel.blt(self.barrels[j].posX,
                                      self.barrels[j].posY,
                                      0,
                                      16 * change2,
                                      131,
                                      16,
                                      10,
                                      colkey=0)

            # Draw Lives
            for i in range(self.mario.lives):
                pyxel.blt(i * 10, 0, 1, 52, 36, 8, 8, colkey=0)

            # Draw Score
            color = 7
            if self.mario.dying:
                color = 8
            pyxel.text(60, 0, "Score: %i" % self.mario.score, color)

            # Draw Christmas Tree
            color_change = pyxel.frame_count // 15 % 3
            for i in range(3):
                pyxel.blt(0, 214, 0, color_change * 24, 195, 21, 28, colkey=0)
            if self.mario.inRange:
                pyxel.text(self.mario.posX + 10, self.mario.posY - 7, "+100",
                           11)
                if self.mario.onplat or self.mario.dying or self.mario.onladder:
                    self.mario.inRange = False

        elif self.mario.win:
            timer = pyxel.frame_count // 30 % 8
            if timer == 0 or self.win_condition:
                self.win_condition = True
                if timer <= 2:
                    self.draw_win()
                else:
                    self.draw_death()
                    if timer == 7:
                        self.win_condition = False
            else:
                self.draw_win()

        else:
            self.draw_death()

    def draw_win(self):
        change2 = (pyxel.frame_count // 3) % 15
        pyxel.cls(change2)
        # draw self.platforms
        for i in self.platforms:
            pyxel.blt(self.platforms[i].posX,
                      self.platforms[i].posY,
                      0,
                      0,
                      180,
                      16,
                      8,
                      colkey=0)

        # draw self.ladders
        for i in range(1, 9):
            x = 118
            y = 236 - 6 * i
            pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)
        for i in range(1, 9):
            x = 50
            y = 183 - 6 * i
            pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)
        for i in range(1, 10):
            x = 130
            y = 133 - 6 * i
            pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)
        for i in range(1, 7):
            x = 101
            y = 74 - 6 * i
            pyxel.blt(x, y, 0, 80, 0, 8, 6, colkey=0)
        pyxel.blt(116, 230, 2, 0, 16, 1, 1, colkey=0)

        # draw donkey

        pyxel.blt(self.kong.posX, self.kong.posY, 0, 0, 98, 38, 32, colkey=0)

        # draw mario

        pyxel.blt(self.mario.posX, self.mario.posY, 1, 0, 0, 16, 16, colkey=0)

        # draw PAULINE

        pyxel.blt(170, 18, 0, 0, 49, 16, 22, colkey=0)

        # Draw Heart

        pyxel.blt(150, 19, 2, 99, 196, 16, 12, colkey=0)

        # Draw Christmas Tree

        color_change = pyxel.frame_count // 15 % 3
        for i in range(3):
            pyxel.blt(0, 214, 0, color_change * 24, 195, 21, 28, colkey=0)

    def draw_death(self):
        pyxel.cls(0)
        pyxel.text(pyxel.width // 2 - 30, pyxel.height // 2 - 8,
                   "RESET? PRESS R", 11)
        pyxel.text(pyxel.width // 2 - 28, pyxel.height // 2 + 10 - 8,
                   "QUIT? PRESS Q", 8)
        change2 = (pyxel.frame_count // 2) % 2
        y = pyxel.frame_count % (pyxel.height)
        for i in range(2):
            pyxel.blt(pyxel.width // 2 - 60, y, 0, 16 * change2, 16, 16, 16)
            pyxel.blt(pyxel.width // 2 + 40, y, 0, 16 * change2, 16, 16, 16)
        change2 = (pyxel.frame_count // 3) % 15
        if change2 != 0:
            pyxel.text(pyxel.width // 2 - 23, 55, "HIGHSCORES:", change2)
        else:
            pyxel.text(pyxel.width // 2 - 23, 55, "HIGHSCORES:", 15)
        for i in range(len(self.highscores)):
            if i == 0:
                pyxel.text(80, 65 + 7 * i,
                           str(i + 1) + '.' + '%i' % (self.highscores[i]), 10)
            elif i == 1:
                pyxel.text(80, 65 + 7 * i,
                           str(i + 1) + '.' + '%i' % (self.highscores[i]), 6)
            elif i == 2:
                pyxel.text(80, 65 + 7 * i,
                           str(i + 1) + '.' + '%i' % (self.highscores[i]), 4)
            else:
                pyxel.text(80, 65 + 7 * i,
                           str(i + 1) + '.' + '%i' % (self.highscores[i]), 7)

    def reset(self):
        self.mario.Reset()
        self.barrels.clear()

    def bubbleSort(self, aList):

        swapping = True
        num = len(aList) - 1

        while num > 0 and swapping:
            swapping = False
            for i in range(num):
                if aList[i] < aList[i + 1]:
                    swapping = True
                    aux = aList[i]
                    aList[i] = aList[i + 1]
                    aList[i + 1] = aux
            num -= 1
    def __init__(self):
        WIDTH = 192
        HEIGHT = 250
        CAPTION = "Mario Game Beta 5.0"

        self.mario = Mario()
        self.kong = Donkey()
        self.win_condition = False

        self.barrels = {}
        #######################
        self.platforms = {}
        for i in range(1, 24):
            if i < 3:
                name = 'platform_' + str(i)
                x = 16 * (i - 1)
                y = 242
                obj = statics.Platform(x, y)
                self.platforms[name] = obj
            elif i < 14:
                for j in range(2):
                    if j == 0:
                        name = 'platform_' + str(i) + '.' + str(j + 1)
                        x = 16 * (i - 1)
                        y = 242 - (i - 2)
                        obj = statics.Platform(x, y)
                        self.platforms[name] = obj
                    elif j == 1:
                        name = 'platform_' + str(i) + '.' + str(j + 1)
                        x = 16 * (i - 1)
                        y = 140 - (i - 2)
                        obj = statics.Platform(x, y)
                        self.platforms[name] = obj
            elif i > 13:
                if i == 16:
                    name = 'platform_' + str(i)
                    x = 16 * (i - 15)
                    y = 180 + 1
                    obj = statics.Platform(x, y)
                    self.platforms[name] = obj
                else:
                    name = 'platform_' + str(i)
                    x = 16 * (i - 15)
                    y = 180 + (i - 15)
                    obj = statics.Platform(x, y)
                    self.platforms[name] = obj

        for i in range(10):
            if i < 3:
                name = 'platform' + str(100 + i)
                x = 16 * i
                y = 70
                obj = statics.Platform(x, y)
                self.platforms[name] = obj
            else:
                name = 'platform' + str(100 + i)
                x = 16 * i
                y = 70 + (i - 2)
                obj = statics.Platform(x, y)
                self.platforms[name] = obj

        for i in range(6):
            name = 'platform' + str(200 + i)
            x = 100 + 16 * i
            y = 40
            obj = statics.Platform(x, y)
            self.platforms[name] = obj
        ################
        self.invisiplats = {}
        name = 'inv_1'
        x = 145
        y = 188
        obj = statics.InvisiPlat(x, y)
        self.invisiplats[name] = obj
        name = 'inv_2'
        x = 160
        y = 77
        obj = statics.InvisiPlat(x, y)
        self.invisiplats[name] = obj
        name = 'inv_3'
        x = 32
        y = 139
        obj = statics.InvisiPlat(x, y)
        self.invisiplats[name] = obj
        ################
        self.ladders = {}

        name = 'ladder_1'
        x = 118
        yl = 229
        yh = 187
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        name = 'ladder_2'
        x = 50
        yl = 176
        yh = 138
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        name = 'ladder_3'
        x = 130
        yl = 126
        yh = 76
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        name = 'ladder_4'
        x = 101
        yl = 67
        yh = 41
        obj = statics.Ladder(x, yh, yl)
        self.ladders[name] = obj

        ##################
        self.scores_path = 'highscores.txt'
        try:
            with open(self.scores_path, "r") as file:
                self.highscores = eval(file.readline())

        except:
            self.highscores = []
            for i in range(5):
                self.highscores.append(0)

            with open(self.scores_path, "w") as file:
                file.write(str(self.highscores))
        ##################
        """
        We thought at first that ladders could be iterably generated, but soon
        changed our mind. 
        self.ladders = {}
        for i in range(1,9):
            name = 'ladder_' + str(i)
            x = 117
            y = 236-6*i
            obj = classes.Ladder(x,y)
            self.ladders[name] = obj
        """
        pyxel.init(WIDTH, HEIGHT, caption=CAPTION)
        pyxel.load("opjects.pyxres")
        pyxel.playm(0, loop=True)

        pyxel.run(self.update, self.draw)
Ejemplo n.º 11
0
    def evaluate(self, path_to_checkpoint, path_to_tfrecords_file,
                 num_examples, global_step):
        batch_size = 128
        num_batches = num_examples / batch_size
        needs_include_length = False

        with tf.Graph().as_default():
            image_batch, length_batch, digits_batch = Donkey.build_batch(
                path_to_tfrecords_file,
                num_examples=num_examples,
                batch_size=batch_size,
                shuffled=False)
            length_logits, digits_logits = Model.inference(image_batch,
                                                           drop_rate=0.0)
            length_predictions = tf.argmax(length_logits, axis=1)
            digits_predictions = tf.argmax(digits_logits, axis=2)

            if needs_include_length:
                labels = tf.concat(
                    [tf.reshape(length_batch, [-1, 1]), digits_batch], axis=1)
                predictions = tf.concat([
                    tf.reshape(length_predictions, [-1, 1]), digits_predictions
                ],
                                        axis=1)
            else:
                labels = digits_batch
                predictions = digits_predictions

            labels_string = tf.reduce_join(tf.as_string(labels), axis=1)
            predictions_string = tf.reduce_join(tf.as_string(predictions),
                                                axis=1)

            accuracy, update_accuracy = tf.metrics.accuracy(
                labels=labels_string, predictions=predictions_string)

            tf.summary.image('image', image_batch)
            tf.summary.scalar('accuracy', accuracy)
            tf.summary.histogram(
                'variables',
                tf.concat([
                    tf.reshape(var, [-1]) for var in tf.trainable_variables()
                ],
                          axis=0))
            summary = tf.summary.merge_all()

            with tf.Session() as sess:
                sess.run([
                    tf.global_variables_initializer(),
                    tf.local_variables_initializer()
                ])
                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(sess=sess, coord=coord)

                restorer = tf.train.Saver()
                restorer.restore(sess, path_to_checkpoint)

                for _ in range(round(num_batches)):
                    sess.run(update_accuracy)

                accuracy_val, summary_val = sess.run([accuracy, summary])
                self.summary_writer.add_summary(summary_val,
                                                global_step=global_step)

                coord.request_stop()
                coord.join(threads)

        return accuracy_val
Ejemplo n.º 12
0
def _train(path_to_train_tfrecords_file, num_train_examples,
           path_to_val_tfrecords_file, num_val_examples, path_to_train_log_dir,
           path_to_restore_checkpoint_file, training_options):
    batch_size = training_options['batch_size']
    initial_patience = training_options['patience']
    num_steps_to_show_loss = 100
    num_steps_to_check = 1000

    with tf.Graph().as_default():
        image_batch, length_batch, digits_batch = Donkey.build_batch(
            path_to_train_tfrecords_file,
            num_examples=num_train_examples,
            batch_size=batch_size,
            shuffled=True)
        with tf.variable_scope('model'):
            length_logtis, digits_logits, hidden_out = Model.inference(
                image_batch,
                drop_rate=0.2,
                is_training=True,
                defend_layer=FLAGS.defend_layer)
        with tf.variable_scope('defender'):
            recovered = Attacker.recover_hidden(FLAGS.attacker_type,
                                                hidden_out, True,
                                                FLAGS.defend_layer)
        ssim = tf.reduce_mean(
            tf.abs(tf.image.ssim(image_batch, recovered, max_val=2)))
        model_loss = Model.loss(length_logtis, digits_logits, length_batch,
                                digits_batch)
        loss = model_loss + FLAGS.ssim_weight * ssim
        defender_loss = -ssim

        global_step = tf.Variable(0, name='global_step', trainable=False)
        learning_rate = tf.train.exponential_decay(
            training_options['learning_rate'],
            global_step=global_step,
            decay_steps=training_options['decay_steps'],
            decay_rate=training_options['decay_rate'],
            staircase=True)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)

        model_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS,
                                             scope='model')
        with tf.control_dependencies(model_update_ops):
            train_op = optimizer.minimize(loss,
                                          var_list=tf.get_collection(
                                              tf.GraphKeys.TRAINABLE_VARIABLES,
                                              scope='model'),
                                          global_step=global_step)

        defender_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS,
                                                scope='defender')
        with tf.control_dependencies(defender_update_ops):
            defender_op = optimizer.minimize(
                defender_loss,
                var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                           scope='defender'),
                global_step=global_step)

        tf.summary.image('image', image_batch, max_outputs=20)
        tf.summary.image('recovered', recovered, max_outputs=20)
        tf.summary.scalar('model_loss', model_loss)
        tf.summary.scalar('ssim', ssim)
        tf.summary.scalar('learning_rate', learning_rate)
        summary = tf.summary.merge_all()

        with tf.Session() as sess:
            summary_writer = tf.summary.FileWriter(path_to_train_log_dir,
                                                   sess.graph)
            evaluator = Evaluator(
                os.path.join(path_to_train_log_dir, 'eval/val'))

            sess.run(tf.global_variables_initializer())
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            saver = tf.train.Saver()
            model_saver = tf.train.Saver(var_list=tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES, scope='model'))
            defender_saver = tf.train.Saver(var_list=tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES, scope='defender'))
            # if path_to_restore_checkpoint_file is not None:
            #     assert tf.train.checkpoint_exists(path_to_restore_checkpoint_file), \
            #         '%s not found' % path_to_restore_checkpoint_file
            #     saver.restore(sess, path_to_restore_checkpoint_file)
            #     print('Model restored from file: %s' % path_to_restore_checkpoint_file)

            print('Start training')
            patience = initial_patience
            best_accuracy = 0.0
            duration = 0.0

            while True:
                start_time = time.time()
                _, _, loss_val, summary_val, global_step_val, learning_rate_val = sess.run(
                    [
                        train_op, defender_op, loss, summary, global_step,
                        learning_rate
                    ])
                duration += time.time() - start_time

                # print("image: {} - {}".format(image_batch_val.min(), image_batch_val.max()))

                if global_step_val % num_steps_to_show_loss == 0:
                    examples_per_sec = batch_size * num_steps_to_show_loss / duration
                    duration = 0.0
                    print('=> %s: step %d, loss = %f (%.1f examples/sec)' %
                          (datetime.now(), global_step_val, loss_val,
                           examples_per_sec))

                if global_step_val % num_steps_to_check != 0:
                    continue

                summary_writer.add_summary(summary_val,
                                           global_step=global_step_val)

                print('=> Evaluating on validation dataset...')
                path_to_latest_checkpoint_file = saver.save(
                    sess,
                    os.path.join(path_to_train_log_dir, 'model_defender.ckpt'))
                model_saver.save(
                    sess, os.path.join(path_to_train_log_dir, 'model.ckpt'))
                defender_saver.save(
                    sess, os.path.join(path_to_train_log_dir, 'defender.ckpt'))
                accuracy = evaluator.evaluate(path_to_latest_checkpoint_file,
                                              path_to_val_tfrecords_file,
                                              num_val_examples,
                                              global_step_val,
                                              FLAGS.defend_layer,
                                              FLAGS.attacker_type)
                print('==> accuracy = %f, best accuracy %f' %
                      (accuracy, best_accuracy))

                if accuracy > best_accuracy:
                    model_saver.save(
                        sess,
                        os.path.join(path_to_train_log_dir, 'model_best.ckpt'))
                    defender_saver.save(
                        sess,
                        os.path.join(path_to_train_log_dir,
                                     'defender_best.ckpt'))
                    patience = initial_patience
                    best_accuracy = accuracy
                else:
                    patience -= 1

                print('=> patience = %d' % patience)
                # if patience == 0:
                #     break
                if global_step_val > FLAGS.max_steps:
                    break

            coord.request_stop()
            coord.join(threads)
            print('Finished')
def train(path_to_train_h5py_file, path_to_val_h5py_file,
           path_to_train_log_dir, path_to_restore_checkpoint_file, training_options):
    image_train,length_train,digits_train=read_h5py_file(path_to_train_h5py_file,Flag=1)
    image_val,length_val,digits_val=read_h5py_file(path_to_val_h5py_file,Flag=2)
    batch_size = training_options['batch_size']
    initial_patience = training_options['patience']
    num_steps_to_show_loss = 100
    num_steps_to_check = 1000
    image_size=54
    num_channels=3
    digits_nums=5
    with tf.Graph().as_default():
        image_batch=tf.placeholder(
        tf.float32, shape=[None, image_size, image_size, num_channels])
        length_batch=tf.placeholder(tf.int32,shape=[None])
        digits_batch=tf.placeholder(tf.int32,shape=[None,digits_nums])
        # length_logtis, digits_logits = Model.inference(image_batch, drop_rate=0.2)
        length_logtis, digits_logits = Model.forward(image_batch, 0.8)

        loss = Model.loss(length_logtis, digits_logits, length_batch, digits_batch)

        global_step = tf.Variable(0, name='global_step', trainable=False)
        learning_rate = tf.train.exponential_decay(training_options['learning_rate'], global_step=global_step,
                                                   decay_steps=training_options['decay_steps'], decay_rate=training_options['decay_rate'], staircase=True)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train_op = optimizer.minimize(loss, global_step=global_step)

        tf.summary.image('image', image_batch)
        tf.summary.scalar('loss', loss)
        tf.summary.scalar('learning_rate', learning_rate)
        summary = tf.summary.merge_all()

        with tf.Session() as sess:
            summary_writer = tf.summary.FileWriter(path_to_train_log_dir, sess.graph)
            evaluator = Evaluator(os.path.join(path_to_train_log_dir, 'eval/val'))

            sess.run(tf.global_variables_initializer())
            saver = tf.train.Saver()
            if path_to_restore_checkpoint_file is not None:
                assert tf.train.checkpoint_exists(path_to_restore_checkpoint_file), \
                    '%s not found' % path_to_restore_checkpoint_file
                saver.restore(sess, path_to_restore_checkpoint_file)
                print('Model restored from file: %s' % path_to_restore_checkpoint_file)

            print('Start training')
            patience = initial_patience
            best_accuracy = 0.0
            duration = 0.0

            while True:
                start_time = time.time()
                image_batch_input, length_batch_input, digits_batch_input= Donkey.build_batch(image_train,length_train,digits_train,
                                                                     batch_size=batch_size)
                feed_dict={image_batch:image_batch_input,length_batch:length_batch_input,digits_batch:digits_batch_input}
                _, loss_val, summary_val, global_step_val, learning_rate_val = sess.run([train_op, loss, summary, global_step, learning_rate],feed_dict=feed_dict)
                duration += time.time() - start_time

                if global_step_val % num_steps_to_show_loss == 0:
                    examples_per_sec = batch_size * num_steps_to_show_loss / duration
                    duration = 0.0
                    print ('=> %s: step %d, loss = %f (%.1f examples/sec)' % (
                        datetime.now(), global_step_val, loss_val, examples_per_sec))

                if global_step_val % num_steps_to_check != 0:
                    continue

                summary_writer.add_summary(summary_val, global_step=global_step_val)

                print ('=> Evaluating on validation dataset...')
                path_to_latest_checkpoint_file = saver.save(sess, os.path.join(path_to_train_log_dir, 'latest.ckpt'))
                accuracy = evaluator.evaluate(path_to_latest_checkpoint_file, image_val,length_val,digits_val,
                                              global_step_val)
                print ('==> accuracy = %f, best accuracy %f' % (accuracy, best_accuracy))

                if accuracy > best_accuracy:
                    path_to_checkpoint_file = saver.save(sess, os.path.join(path_to_train_log_dir, 'model.ckpt'),
                                                         global_step=global_step_val)
                    print ('=> Model saved to file: %s' % path_to_checkpoint_file)
                    patience = initial_patience
                    best_accuracy = accuracy
                else:
                    patience -= 1

                print ('=> patience = %d' % patience)
                if patience == 0:
                    break
            print ('Finished')