예제 #1
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'
    image_dir = 'tf_files/landing_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)
예제 #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 labelImages(img_dir, model_file, label_file):
    input_height = 224
    input_width = 224
    input_mean = 128
    input_std = 128
    input_layer = "input"
    output_layer = "final_result"
    graph = load_graph(model_file)
    input_name = "import/" + input_layer
    output_name = "import/" + output_layer
    input_operation = graph.get_operation_by_name(input_name)
    output_operation = graph.get_operation_by_name(output_name)

    with tf.Session(graph=graph) as sess:
        results_dict = defaultdict(list)
        for dirname, subdirs, files in os.walk(img_dir):
            for file in files:
                if not file.endswith('jpg'):
                    continue
                file_name = os.path.join(dirname, file)
                results_dict['file_name'].append(file_name)
                results_dict['true_label'].append(dirname.split('/')[-1])
                t = read_tensor_from_image_file(file_name,
                                                input_height=input_height,
                                                input_width=input_width,
                                                input_mean=input_mean,
                                                input_std=input_std)
                start = time.time()
                results = sess.run(output_operation.outputs[0],
                                   {input_operation.outputs[0]: t})
                end = time.time()
                results_dict['eval_time'].append(end - start)
                print(
                    '\nEvaluation time (1-image): {:.3f}s\n'.format(end - start))
                results = np.squeeze(results)
                top_k = results.argsort()[-5:][::-1]
                labels = load_labels(label_file)
                template = "{} (score={:0.5f})"
                for i in top_k:
                    results_dict[labels[i]].append(results[i])
                    print(template.format(labels[i], results[i]))
    return results_dict
def evaluate_graph(graph_file_name):

    with load_graph(graph_file_name).as_default() as graph:

        ground_truth_input = tf.placeholder(tf.float32, [None, 2],
                                            name='GroundTruthInput')

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

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

    image_dir = 'tf_files/images/validation'
    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())

    ground_truths = []
    filenames = []
    y_true = []

    print(image_lists.keys())

    data = {}
    data['label_outros'] = int(image_lists.keys()[0] == 'melanoma')
    data['label_melanoma'] = int(image_lists.keys()[1] == 'melanoma')

    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)
            y_true.append(label_index)
            filenames.append(image_name)

    accuracies = []
    xents = []
    y = []
    y_scores = []

    with tf.Session(graph=graph) as sess:

        #print('=== MISCLASSIFIED TEST IMAGES ===')

        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 - 127) / 127.0

            feed_dict = {
                image_buffer_input: image,
                ground_truth_input: ground_truth
            }

            eval_accuracy, eval_xent = sess.run([accuracy, xent], feed_dict)
            eval_prediction = sess.run([prediction], feed_dict)
            eval_softmax = sess.run([final_tensor], feed_dict)

            #if eval_prediction != ground_truth.argmax():
            #    print('%s' %
            #            (filename))

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

            y.append(eval_prediction[0][0])
            y_scores.append(eval_softmax[0][0][1])

    data['y_true'] = y_true
    data['y_pred'] = y

    print(data)

    return data