Beispiel #1
0
def test_predict():
    with tf.Graph().as_default():

        boardsize = 19
        num_channels = 4

        board = Board(boardsize)
        #seq = ['b B3', 'b C4', 'b C2', 'B D3',
        #               'W D4', 'w E3', 'w D2']
        seq = ['b Q16']
        board.play_seq(seq)
        print(board)

        #move='Q16'
        # get input to the neural network
        c, p = utils.c_cd2cp(seq[0], boardsize)
        board_reg_str = str(board.create_board_register(utils.Color.WHITE, 4))
        #data = data.reshape(num_images, NUM_CHANNELS, IMAGE_SIZE, IMAGE_SIZE)
        #data = data.transpose(0, 2, 3, 1)

        data = np.frombuffer(board_reg_str, dtype=np.uint8)
        data = data.astype(np.float32)
        data = data.reshape(1, num_channels, boardsize, boardsize)
        print(data)
        data = data.transpose(0, 2, 3, 1)
        print(data)

        # images=tf.placeholder(tf.float64,shape=[None,num_channels,boardsize,boardsize])
        logits = cnn.inference_layer(data, boardsize, num_channels, 11)
        sm_output = tf.nn.softmax(logits)

        init = tf.initialize_all_variables()

        saver = tf.train.Saver()
        with tf.Session() as sess:
            # Restore variables from disk.
            saver.restore(sess, "KGS_train_l13/model.ckpt-12000")
            print("Model restored.")
            output = sess.run(sm_output)
            a = np.argmax(output)
            print(output, utils.p2cd(utils.a2p(a, boardsize), boardsize))
            #get top 5 prediction
            top_5 = output[0].argsort()[::-1][:5]
            print(top_5)
            for e in top_5:
                print(utils.p2cd(utils.a2p(e, boardsize), boardsize))

            print(board)
Beispiel #2
0
def evaluate():
    """Eval for a number of steps."""
    with tf.Graph().as_default() as g:
        # Get images and labels
        eval_data = FLAGS.eval_data == 'test'
        images, labels = cnn.inputs(eval_data, num_train_files,
                                    train_num_examples, test_num_examples,
                                    boardsize, num_channels)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        #logits = cnn.inference(images, boardsize, num_channels)
        logits = cnn.inference_layer(images, boardsize, num_channels, 0)

        # Calculate predictions.
        top_k_op = tf.nn.in_top_k(logits, labels, 1)

        #print(top_k_op)
        #with tf.Session() as sess:
        #   sess.

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            cnn.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_all_summaries()

        summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, g)

        for step in range(0, 21000, 1000):
            dir_name = '/home/mario/Dropbox/PycharmProjects/ndsgo/cnn_models/{}'.format(
                FLAGS.checkpoint_dir)
            fn = "model.ckpt-{}".format(step)
            full_fn = os.path.join(dir_name, fn)

            #print('dirname:{}'.format(dir_name))
            #print('fn:{}'.format(fn))
            print('full_fn:{}'.format(full_fn))
            print('global_step:{}'.format(step))
            eval_once(saver, summary_writer, top_k_op, summary_op,
                      test_num_examples, full_fn)
            #eval_once(saver, summary_writer, top_k_op, summary_op, 1000)
            if FLAGS.run_once:
                break
Beispiel #3
0
    def __init__(self, N, verbose=False):
        Player.__init__(self, N)

        dir_name = '/home/mario/Dropbox/PycharmProjects/ndsgo'
        self.player_file = {
            9: os.path.join(dir_name, 'model_9x9_l13_26000'),
            #9: os.path.join(dir_name, 'model_9x9'),
            #19: os.path.join(dir_name,'model_19x19_k128')}
            19: os.path.join(dir_name, 'model_19x19_l13')
        }
        #19: os.path.join(dir_name,'model_19x19')}

        self.verbose = verbose
        boardsize = N

        #with open(self.player_file[boardsize]+'.prop','r') as f:
        #self.num_channels=int(f.readline().split(',')[1])

        self.num_channels = 4

        self.data = tf.placeholder(
            tf.float32, [1, boardsize, boardsize, self.num_channels])

        self.logits = cnn.inference_layer(self.data, boardsize,
                                          self.num_channels, 11)
        #self.logits = cnn.inference(self.data, boardsize, self.num_channels)
        self.sm_output = tf.nn.softmax(self.logits)

        # init = tf.initialize_all_variables()
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)

        saver = tf.train.Saver()
        self.sess = tf.Session()
        #self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        #self.sess = tf.Session(config=tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options))
        try:
            file_name = self.player_file[N]
        except:
            eprint('There is no file for boardsize {}'.format(N))
            exit()
        saver.restore(self.sess, file_name + '.ckpt')
Beispiel #4
0
def evaluate():
    """Eval for a number of steps."""
    with tf.Graph().as_default() as g:
        # Get images and labels
        eval_data = FLAGS.eval_data == 'test'
        images, labels = cnn.inputs(eval_data, num_train_files,
                                    train_num_examples, test_num_examples,
                                    boardsize, num_channels)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        #logits = cnn.inference(images, boardsize, num_channels)
        logits = cnn.inference_layer(images, boardsize, num_channels, 11)

        # Calculate predictions.
        top_k_op = tf.nn.in_top_k(logits, labels, 1)

        #print(top_k_op)
        #with tf.Session() as sess:
        #   sess.

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            cnn.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_all_summaries()

        summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, g)

        while True:
            eval_once(saver, summary_writer, top_k_op, summary_op,
                      test_num_examples)
            #eval_once(saver, summary_writer, top_k_op, summary_op, 1000)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
Beispiel #5
0
def train():
    """Train for a number of steps."""
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)

        # Get images and labels
        images, labels = cnn.distorted_inputs(num_train_files,
                                              train_num_examples, boardsize,
                                              num_channels)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        #logits = cnn.inference_l2(images, boardsize, num_channels)
        #13->11, 3->1,2->0
        logits = cnn.inference_layer(images, boardsize, num_channels, 0)
        #logits=cnn.inference(images,boardsize,num_channels)

        # Calculate loss.
        loss = cnn.loss(logits, labels)

        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = cnn.train(loss, global_step, train_num_examples)

        # Create a saver.
        saver = tf.train.Saver(tf.all_variables(), max_to_keep=50)

        saver_exp = tf.train.Saver(tf.all_variables())

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_all_summaries()

        # Build an initialization operation to run below.
        init = tf.initialize_all_variables()

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)

        # Start running operations on the Graph.
        sess = tf.Session(config=tf.ConfigProto(
            log_device_placement=FLAGS.log_device_placement,
            gpu_options=gpu_options))
        sess.run(init)

        stored_performace = []

        # Start the queue runners.
        tf.train.start_queue_runners(sess=sess)

        summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)

        for step in xrange(FLAGS.max_steps):
            start_time = time.time()
            _, loss_value = sess.run([train_op, loss])
            #images = sess.run(images)
            duration = time.time() - start_time

            # np.set_printoptions(threshold="nan")
            # images=np.array(images)
            # images=images.transpose(0, 3, 1, 2)
            # image=images[0]
            # reg=bytearray([1,0])+bytearray(image.reshape([19*19*4]))
            # print(image)
            # board, a = Board.get_board_and_move_from_register_str(19,reg,Color.BLACK)
            # print(board)
            # p=utils.a2p(a,19)
            # cd=utils.p2cd(p,19)
            # print('a:{} p:{} cd:{}'.format(a,p,cd))
            #
            # print(images[0])

            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

            if step % 10 == 0:
                num_examples_per_step = FLAGS.batch_size
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)

                format_str = (
                    '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                    'sec/batch)')
                print(format_str % (datetime.now(), step, loss_value,
                                    examples_per_sec, sec_per_batch))

                #print(stored_performace)

            if step % 100 == 0:
                summary_str = sess.run(summary_op)
                summary_writer.add_summary(summary_str, step)

            # Save the model checkpoint periodically.
            if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

            if step == 10 or step == 100 or step == 1000 or step == 10000 or step == 100000 or step == 1000000:
                checkpoint_path = os.path.join(FLAGS.train_dir,
                                               'model_exp.ckpt')
                saver_exp.save(sess, checkpoint_path, global_step=step)