示例#1
0
 def testAddEvaluationStep(self):
     with tf.Graph().as_default():
         with tf.Session() as sess:
             tf.placeholder(tf.float32, [1], name='final')
             tf.placeholder(tf.float32, [1], name='gt')
             self.assertIsNotNone(
                 retrain.add_evaluation_step(sess.graph, 'final', 'gt'))
示例#2
0
def evaluate_graph(graph_file_name):
    with load_graph(graph_file_name).as_default() as graph:
        ground_truth_input = tf.placeholder(tf.float32, [None, 5],
                                            name='GroundTruthInput')

        image_buffer_input = graph.get_tensor_by_name('input:0')
        final_tensor = graph.get_tensor_by_name('final_result:0')
        accuracy, _ = retrain.add_evaluation_step(final_tensor,
                                                  ground_truth_input)

        logits = graph.get_tensor_by_name("final_training_ops/Wx_plus_b/add:0")
        xent = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=ground_truth_input,
                                                    logits=logits))

    image_dir = 'tf_files/flower_photos'
    testing_percentage = 10
    validation_percentage = 10
    validation_batch_size = 100
    category = 'testing'

    image_lists = retrain.create_image_lists(image_dir, testing_percentage,
                                             validation_percentage)
    class_count = len(image_lists.keys())

    ground_truths = []
    filenames = []

    for label_index, label_name in enumerate(image_lists.keys()):
        for image_index, image_name in enumerate(
                image_lists[label_name][category]):
            image_name = retrain.get_image_path(image_lists, label_name,
                                                image_index, image_dir,
                                                category)
            ground_truth = np.zeros([1, class_count], dtype=np.float32)
            ground_truth[0, label_index] = 1.0
            ground_truths.append(ground_truth)
            filenames.append(image_name)

    accuracies = []
    xents = []
    with tf.Session(graph=graph) as sess:
        for filename, ground_truth in zip(filenames, ground_truths):
            image = Image.open(filename).resize((224, 224), Image.ANTIALIAS)
            image = np.array(image, dtype=np.float32)[None, ...]
            image = (image - 128) / 128.0

            feed_dict = {
                image_buffer_input: image,
                ground_truth_input: ground_truth
            }

            eval_accuracy, eval_xent = sess.run([accuracy, xent], feed_dict)

            accuracies.append(eval_accuracy)
            xents.append(eval_xent)

    return np.mean(accuracies), np.mean(xents)
示例#3
0
 def testAddEvaluationStep(self):
     with tf.Graph().as_default():
         final = tf.placeholder(tf.float32, [1], name='final')
         gt = tf.placeholder(tf.int64, [1], name='gt')
         self.assertIsNotNone(retrain.add_evaluation_step(final, gt))
示例#4
0
    workbook = xlsxwriter.Workbook("ensemble_accuracy.xlsx")
    worksheet = workbook.add_worksheet()

    with graphs.as_default() as gs:
        dataset = dataset_factory.get_dataset(FLAGS.dataset_name,
                                              FLAGS.dataset_split_name,
                                              FLAGS.dataset_dir)

        ground_truth_input = tf.placeholder(tf.float32,
                                            [None, dataset.num_classes],
                                            name='GroundTruthInput')
        predictions_average = tf.placeholder(tf.float32,
                                             [None, dataset.num_classes],
                                             name='predictions_average')

        accuracy_eval, _ = retrain.add_evaluation_step(predictions_average,
                                                       ground_truth_input)

        logger.info('')

        if pre_ensemble_num < 50:
            pre_ensemble_num = 50

        random_times = 100
        ensemble_num = int(pre_ensemble_num * 0.2)
        logger.info("random ensemble num is : %d , random time is: %d" %
                    (ensemble_num, random_times))
        #sameple will can do the shuffle func

        if FLAGS.base_mode == True:
            pre_ensemble_df = all_files_exclude_base_df[0:ensemble_num]
            ensemble_df = pre_ensemble_df[:].sample(n=ensemble_num)
def extract():

    #sess=tf.Session()
    #先加载图和参数变量
    #    for op in graph.get_operations():
    #      print(str(op.name))
    #    var = tf.global_variables()#全部调用
    #    for i in var:
    #        print(i)

    input_layer = "input"
    #nput_layer = "MobilenetV2/input"
    output_layer = "MobilenetV2/Predictions/Reshape_1"
    #output_layer= "MobilenetV2/Logits/output"
    #graph = load_graph('./frozen_pb/frozen_0-0-0.pb')
    #with graph.as_default() as g:
    #        image_buffer_input = g.get_tensor_by_name('input:0')
    #        final_tensor = g.get_tensor_by_name('MobilenetV2/Logits/AvgPool:0')

    #image_dir = '/home/xxxx/PHICOMM/ai-share/dataset/imagenet/raw-data/imagenet-data/validation'
    image_dir = "/home/deepl/PHICOMM/dataset/cifar10_tf/cifar-10/test"
    testing_percentage = 100
    validation_percentage = 0
    category = 'testing'

    image_lists = retrain.create_image_lists(image_dir, testing_percentage,
                                             validation_percentage)
    class_count = len(image_lists.keys())
    print(class_count)

    total_start = time.time()

    ground_truths = []
    filenames = []

    all_files_df = pd.DataFrame(
        columns=['image_name', 'ground_truth', "predecit_label"])

    for label_index, label_name in enumerate(image_lists.keys()):
        for image_index, image_name in enumerate(
                image_lists[label_name][category]):
            image_name = retrain.get_image_path(image_lists, label_name,
                                                image_index, image_dir,
                                                category)
            ground_truth = np.zeros([1, class_count], dtype=np.float32)
            ground_truth[0, label_index] = 1.0
            ground_truths.append(ground_truth)
            filenames.append(image_name)

            ground_truth_argmax = np.argmax(ground_truth, axis=1)
            ground_truth_argmax = np.squeeze(ground_truth_argmax)

            all_files_df = all_files_df.append(
                [{
                    'image_name': image_name,
                    'ground_truth': ground_truth_argmax
                }],
                ignore_index=True)

    #all_files_df.to_csv("ground_truth1.csv")
    #print(filenames)

    if os.path.exists("./data"):
        print("data is exist, please delete it!")
        exit()
        #shutil.rmtree("./data")
    #os.makedirs("./data")

    #sio.savemat('./data/truth.mat',{"truth": ground_truths})

    cf = 0.875
    predictions = []
    i = 0
    start = time.time()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        #with tf.Session(graph=graph) as sess:
        network_fn = nets_factory.get_network_fn("mobilenet_v2",
                                                 num_classes=10,
                                                 is_training=False)
        image_size = 224
        placeholder = tf.placeholder(name='input',
                                     dtype=tf.float32,
                                     shape=[128, image_size, image_size, 3])
        logits, _ = network_fn(placeholder)
        graph = tf.get_default_graph()
        saver = tf.train.Saver()
        #raph_def = graph.as_graph_def()
        #aver = tf.train.import_meta_graph(graph_def)

        #saver = tf.train.import_meta_graph('./mobilenetv2_on_cifar10_check_point/0/model_0/model.ckpt-20000.meta')
        saver.restore(
            sess,
            './mobilenetv2_on_cifar10_check_point/0/model_0/model.ckpt-20000')
        # Initalize the variables
        #sess.run(tf.global_variables_initializer())

        output_operation = graph.get_operation_by_name(output_layer)
        input_operation = graph.get_operation_by_name(input_layer)
        read_tensor_from_jpg_image_file_op = read_tensor_from_jpg_image_file(
            input_height=224, input_width=224)
        file_num = len(filenames)
        batch_size = 128
        count = file_num // batch_size
        print("need %d batch, every batch is %d" % (count, batch_size))

        for i in range(count):
            print("this is %d batch" % i)
            print("jpg order get from %d to %d" %
                  (batch_size * i, batch_size * i + batch_size - 1))
            for j in range(batch_size):
                if j == 0:
                    t_batch = sess.run(read_tensor_from_jpg_image_file_op,
                                       feed_dict={
                                           "fnamejpg:0":
                                           filenames[batch_size * i + j]
                                       })
                    t_batch = np.expand_dims(t_batch, axis=0)
                else:
                    t = sess.run(read_tensor_from_jpg_image_file_op,
                                 feed_dict={
                                     "fnamejpg:0":
                                     filenames[batch_size * i + j]
                                 })
                    t = np.expand_dims(t, axis=0)
                    t_batch = np.concatenate((t_batch, t), axis=0)
            print(t_batch.shape)
            #            feed_dict={image_buffer_input: t}
            #            ft = final_tensor.eval(feed_dict, sess)
            #           pre = sess.run(output_operation.outputs[0],
            #                      {input_operation.outputs[0]: t_batch})
            pre = sess.run(logits, {input_operation.outputs[0]: t_batch})

            #print(pre.shape)

            for k in range(batch_size):
                predictions.append(pre[k, :])

        if file_num % batch_size != 0:
            print("this is %d batch" % count)
            print("jpg order get from %d to %d" %
                  (batch_size * count, file_num - 1))

            extra_num = file_num - count * batch_size
            for j in range(extra_num):
                if j == 0:
                    t_batch = sess.run(read_tensor_from_jpg_image_file_op,
                                       feed_dict={
                                           "fnamejpg:0":
                                           filenames[batch_size * count + j]
                                       })
                    t_batch = np.expand_dims(t_batch, axis=0)
                else:
                    t = sess.run(read_tensor_from_jpg_image_file_op,
                                 feed_dict={
                                     "fnamejpg:0":
                                     filenames[batch_size * count + j]
                                 })
                    t = np.expand_dims(t, axis=0)
                    t_batch = np.concatenate((t_batch, t), axis=0)
            print(t_batch.shape)

            for j in range(extra_num, batch_size):
                t_batch = np.concatenate((t_batch, t), axis=0)
            print(t_batch.shape)

            #           pre = sess.run(output_operation.outputs[0],
            #                        {input_operation.outputs[0]: t_batch})
            pre = sess.run(logits, {input_operation.outputs[0]: t_batch})
            for k in range(extra_num):
                predictions.append(pre[k, :])

            #i = i + 1
            #print(i)
    predictions = np.array(predictions)
    #print(predictions.shape)
    #predictions = np.squeeze(predictions)
    ground_truths = np.array(ground_truths)
    print(ground_truths.shape)
    ground_truths = np.squeeze(ground_truths)

    print(predictions.shape)
    print(ground_truths.shape)

    with tf.Session(config=config) as sess:
        # with tf.Session(graph=graph) as sess:
        ground_truth_input = tf.placeholder(tf.float32, [None, 10],
                                            name='GroundTruthInput')
        fts = tf.placeholder(tf.float32, [None, 10], name='fts')
        accuracy, _ = retrain.add_evaluation_step(fts, ground_truth_input)
        feed_dict = {fts: predictions, ground_truth_input: ground_truths}
        #accuracies.append(accuracy.eval(feed_dict, sess))
        ret = accuracy.eval(feed_dict, sess)

    for index, row in all_files_df.iterrows():
        row['predecit_label'] = np.squeeze(
            np.argmax(predictions[index, :], axis=0))

    all_files_df.to_csv("ground_truth.csv")
    print('Ensemble Accuracy: %g' % ret)

    stop = time.time()
    #print(str((stop-start)/len(ftg))+' seconds.')
    #sio.savemat('./data/feature.mat',{"feature": ftg})
    total_stop = time.time()
    print("total time is " + str((total_stop - total_start)) + ' seconds.')
示例#6
0
def extract():

    input_layer = "input"
    output_layer = "MobilenetV2/Predictions/Reshape_1"
    graph = load_graph('./frozen_pb/frozen_0-0-0.pb')
    #with graph.as_default() as g:
    #        image_buffer_input = g.get_tensor_by_name('input:0')
    #        final_tensor = g.get_tensor_by_name('MobilenetV2/Logits/AvgPool:0')
    input_operation = graph.get_operation_by_name(input_layer)
    output_operation = graph.get_operation_by_name(output_layer)

    #image_dir = '/home/xxxx/PHICOMM/ai-share/dataset/imagenet/raw-data/imagenet-data/validation'
    image_dir = "/home/deepl/PHICOMM/dataset/cifar10_tf/cifar-10/test"
    testing_percentage = 100
    validation_percentage = 0
    category = 'testing'

    image_lists = retrain.create_image_lists(image_dir, testing_percentage,
                                             validation_percentage)
    class_count = len(image_lists.keys())
    print(class_count)

    total_start = time.time()

    ground_truths = []
    filenames = []

    all_files_df = pd.DataFrame(
        columns=['image_name', 'ground_truth', "predecit_label"])

    for label_index, label_name in enumerate(image_lists.keys()):
        for image_index, image_name in enumerate(
                image_lists[label_name][category]):
            image_name = retrain.get_image_path(image_lists, label_name,
                                                image_index, image_dir,
                                                category)
            ground_truth = np.zeros([1, class_count], dtype=np.float32)
            ground_truth[0, label_index] = 1.0
            ground_truths.append(ground_truth)
            filenames.append(image_name)

            ground_truth_argmax = np.argmax(ground_truth, axis=1)
            ground_truth_argmax = np.squeeze(ground_truth_argmax)

            all_files_df = all_files_df.append(
                [{
                    'image_name': image_name,
                    'ground_truth': ground_truth_argmax
                }],
                ignore_index=True)

    #all_files_df.to_csv("ground_truth.csv")

    if os.path.exists("./data"):
        print("data is exist, please delete it!")
        exit()
        #shutil.rmtree("./data")
    #os.makedirs("./data")

    #sio.savemat('./data/truth.mat',{"truth": ground_truths})

    cf = 0.875
    predictions = []
    i = 0
    start = time.time()
    with tf.Session(graph=graph) as sess:
        read_tensor_from_jpg_image_file_op = read_tensor_from_jpg_image_file(
            input_height=224, input_width=224)
        for filename in filenames:
            t = sess.run(read_tensor_from_jpg_image_file_op,
                         feed_dict={"fnamejpg:0": filename})
            t = np.expand_dims(t, axis=0)
            #print(t.shape)
            #            feed_dict={image_buffer_input: t}
            #            ft = final_tensor.eval(feed_dict, sess)
            pre = sess.run(output_operation.outputs[0],
                           {input_operation.outputs[0]: t})
            predictions.append(pre)
            #i = i + 1
            #print(i)
    predictions = np.array(predictions)
    predictions = np.squeeze(predictions)
    ground_truths = np.array(ground_truths)
    ground_truths = np.squeeze(ground_truths)

    print(predictions.shape)
    print(ground_truths.shape)

    with tf.Session(graph=graph) as sess:
        ground_truth_input = tf.placeholder(tf.float32, [None, 10],
                                            name='GroundTruthInput')
        fts = tf.placeholder(tf.float32, [None, 10], name='fts')
        accuracy, _ = retrain.add_evaluation_step(fts, ground_truth_input)
        feed_dict = {fts: predictions, ground_truth_input: ground_truths}
        #accuracies.append(accuracy.eval(feed_dict, sess))
        ret = accuracy.eval(feed_dict, sess)

    for index, row in all_files_df.iterrows():
        row['predecit_label'] = np.squeeze(
            np.argmax(predictions[index, :], axis=0))

    all_files_df.to_csv("ground_truth.csv")

    print('Ensemble Accuracy: %g' % ret)

    stop = time.time()
    #print(str((stop-start)/len(ftg))+' seconds.')
    #sio.savemat('./data/feature.mat',{"feature": ftg})
    total_stop = time.time()
    print("total time is " + str((total_stop - total_start)) + ' seconds.')
示例#7
0
            num_classes=(dataset.num_classes - FLAGS.labels_offset),
            is_training=False)
        placeholder = tf.placeholder(name='input', dtype=tf.float32,
                                     shape=[None, FLAGS.eval_image_size,
                                            FLAGS.eval_image_size, 3])

        logits, _ = network_fn(placeholder)
        graph = tf.get_default_graph()
        saver = tf.train.Saver()
        output_operation = graph.get_operation_by_name(FLAGS.output_layer);
        input_operation = graph.get_operation_by_name(FLAGS.input_layer);

        ground_truth_input = tf.placeholder(
                    tf.float32, [None, dataset.num_classes], name='GroundTruthInput')
        predicts = tf.placeholder(tf.float32, [None, dataset.num_classes], name='predicts')
        accuracy, _ = retrain.add_evaluation_step(predicts, ground_truth_input)

        index = 0

        for i,checkpoint in enumerate(all_checkpoints):
            checkpoint_prefix = checkpoint.replace('.data-00000-of-00001', '')
            saver.restore(sess,checkpoint_prefix)

            predictions = sess.run(output_operation.outputs[0],
                 {input_operation.outputs[0]: ftg})

            #print(predictions.shape)

            feed_dict={predicts: predictions, ground_truth_input: ground_truths}
            ret = accuracy.eval(feed_dict, sess)
示例#8
0
def extract():

    input_layer= "input"
    #nput_layer = "MobilenetV2/input"
    output_layer= "MobilenetV2/Predictions/Reshape_1"

    total_start = time.time()
#    if os.path.exists("./data"):
#        print("data is exist, please delete it!")
#        exit()
        #shutil.rmtree("./data")
    #os.makedirs("./data")


    #sio.savemat('./data/truth.mat',{"truth": ground_truths})

    cf = 0.875
    predictions = []
    ground_truths = []
    i = 0
    start = time.time()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth=True

    with tf.Session(config=config) as sess:
        dataset = dataset_factory.get_dataset(
            FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir)

        network_fn = nets_factory.get_network_fn(
            FLAGS.model_name,
            num_classes=(dataset.num_classes - FLAGS.labels_offset),
            is_training=False)

        provider = slim.dataset_data_provider.DatasetDataProvider(
            dataset,
            num_epochs=1,
            shuffle=False,
            common_queue_capacity=2 * FLAGS.batch_size,
            common_queue_min=FLAGS.batch_size)
        [image, label] = provider.get(['image', 'label'])
        label -= FLAGS.labels_offset

        preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
        image_preprocessing_fn = preprocessing_factory.get_preprocessing(
            preprocessing_name,
            is_training=False)
        eval_image_size = FLAGS.eval_image_size or network_fn.default_image_size

        image = image_preprocessing_fn(image, eval_image_size, FLAGS.eval_image_size)

#        filename_queue = tf.train.string_input_producer(
#            [FLAGS.dataset_dir], num_epochs=1)
#        image, label = read_and_decode(filename_queue,FLAGS.eval_image_size)
#        print(image.shape)
#        print(image)
#        print(label.shape)

        images_batch, labels_batch = tf.train.batch(
            [image, label],
            batch_size=FLAGS.batch_size,
            num_threads=FLAGS.num_preprocessing_threads,
            capacity=5 * FLAGS.batch_size,
            allow_smaller_final_batch=True)

        labels_batch_one_hot = tf.one_hot(labels_batch,dataset.num_classes)


        placeholder = tf.placeholder(name='input', dtype=tf.float32,
                                     shape=[None, FLAGS.eval_image_size,
                                            FLAGS.eval_image_size, 3])
        logits, _ = network_fn(placeholder)
        graph = tf.get_default_graph()
        saver = tf.train.Saver()

        output_operation = graph.get_operation_by_name(output_layer);
        input_operation = graph.get_operation_by_name(input_layer);

        batch_size = FLAGS.batch_size
        print("every batch is %d"%(batch_size))

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        saver.restore(sess,FLAGS.checkpoint_path)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess, coord)
        count = 0
        try:
            while not coord.should_stop():
                image_batch_v, label_batch_v = sess.run([images_batch, labels_batch_one_hot])
                #print(image_batch_v.shape, label_batch_v.shape)
                print("this is %d batch"%count)

                ground_truths.extend(label_batch_v)
                count += 1
                pre = sess.run(logits,
                       {input_operation.outputs[0]: image_batch_v})

                predictions.extend(pre)

        except tf.errors.OutOfRangeError:
            print("done")
        finally:
            coord.request_stop()
        coord.join(threads)

            #i = i + 1
            #print(i)
    predictions = np.array(predictions)
    ground_truths = np.array(ground_truths)


    print(predictions.shape)
    print(ground_truths.shape)

    with tf.Session(config=config) as sess:
   # with tf.Session(graph=graph) as sess:
        ground_truth_input = tf.placeholder(
            tf.float32, [None, 1001], name='GroundTruthInput')
        predicts = tf.placeholder(tf.float32, [None, 1001], name='predicts')
        accuracy, _ = retrain.add_evaluation_step(predicts, ground_truth_input)
        feed_dict={predicts: predictions, ground_truth_input: ground_truths}
        #accuracies.append(accuracy.eval(feed_dict, sess))
        ret = accuracy.eval(feed_dict, sess)

    print('Ensemble Accuracy: %g' % ret)

    stop = time.time()
    #print(str((stop-start)/len(ftg))+' seconds.')
    #sio.savemat('./data/feature.mat',{"feature": ftg})
    total_stop = time.time()
    print("total time is "+str((total_stop-total_start))+' seconds.')