第七课 Tensorflow Cifar10 Eval

Cifar10的评估

# coding:utf-8
"""
评估准确率

步骤就是:
1. 载入测试数据集
2. 使用先前构建的模型, 对输入的数据产出预测结果
3. 计算预测结果的正确比率
4. 载入模型计算的参数,运行模型
"""

from abc import ABCMeta
from abc import abstractmethod
import tensorflow as tf
import math
import numpy as np
from datetime import datetime


class IEval(object):

    __metaclass__ = ABCMeta

    def __init__(self, eval_dir, checkpoint_path, test_data_num, batch_size):
        self._eval_dir = eval_dir
        self._checkpoing_path = checkpoint_path
        self._batch_size = batch_size
        self._test_data_num = test_data_num

    def eval(self):

        with tf.Graph().as_default() as g:
            test_data_batch, label_batch = self.read_test_data_set()

            predict_results = self.predict(test_data_batch)
            acuuracy = self.accuracy(predict_results=predict_results, labels=label_batch)

            # 读取EMA变量
            # 这个decay 可以作为全局的配置 都使用
            variable_averages = tf.train.ExponentialMovingAverage(0.9999)

            # resotre variables
            variables_to_restore = variable_averages.variables_to_restore()


            saver = tf.train.Saver(variables_to_restore)

            summary_op = tf.summary.merge_all()

            summary_writer = tf.summary.FileWriter(self._eval_dir, g)

            with tf.Session() as sess:
                # 获取check point
                ckpt = tf.train.get_checkpoint_state(self._checkpoing_path)

                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)

                    # 从checkpoing的文件名的后缀中拿到step
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                else:
                    print('No checkpoit file found')
                    return

                coord = tf.train.Coordinator()

                threads = list()
                try:
                    for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
                        print 'qr ok'
                        threads.extend(qr.create_threads(sess,
                                                         coord=coord,
                                                         daemon=True,
                                                         start=True))
                    num_iter = int(math.ceil(10000 / 128))
                    true_count = 0

                    total_sample_count = num_iter * 128
                    step = 0

                    while step < num_iter and not coord.should_stop():

                        predictions = sess.run([acuuracy])
                        true_count += np.sum(predictions)
                        print 'step:', step, 'true_count:', true_count
                        step += 1

                    precision = true_count*1.0 / total_sample_count
                    print('%s: precision @ 1 = %.3f' % (datetime.now(), precision))

                    summary = tf.Summary()
                    summary.ParseFromString(sess.run(summary_op))
                    summary.value.add(tag='Precision @ 1', simple_value=precision)
                    summary_writer.add_summary(summary, global_step)

                except Exception as e:
                    coord.request_stop(e)

                coord.request_stop()
                coord.join(threads, stop_grace_period_secs=10)

    @abstractmethod
    def read_test_data_set(self):
        pass

    @abstractmethod
    def predict(self, test_data_batch):
        pass

    @abstractmethod
    def accuracy(self, predict_results, labels):
        pass
# coding:utf-8
"""
CIFAR10 eval
"""

from eval import IEval
from cifar10_data_input import CIFAR10DataInput
from cifar10_inference import CIFAR10Inference
import tensorflow as tf


class CIFAR10Eval(IEval):
    """
    Cifar10 eval 使用checkpoint eval
    """

    def __init__(self, eval_dir, checkpoit_dir, test_data_set_file_path, test_date_num, batch_size):
        super(CIFAR10Eval, self).__init__(eval_dir, checkpoit_dir, test_date_num, batch_size)
        self._test_data_set_file_path = test_data_set_file_path
        self._image_channel = 3
        self._image_label_num = 10

    def read_test_data_set(self):

        cifar10_data_input = CIFAR10DataInput(self._test_data_set_file_path,
                                              batch_size=self._batch_size,
                                              example_per_epoch_num=self._test_data_num)

        image_batch, label_batch = cifar10_data_input.read_data()

        tf.summary.image('test_images', image_batch)

        return image_batch, label_batch

    def predict(self, image_batch):
        cifar10_inference = CIFAR10Inference(self._image_channel, self._batch_size, self._image_label_num)
        return cifar10_inference.inference(image_batch)

    def accuracy(self, predict_results, labels):
        return tf.nn.in_top_k(predictions=predict_results, targets=labels, k=1)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值