Hvass-Labs / TensorFlow-Tutorials

TensorFlow Tutorials with YouTube Videos
MIT License
9.28k stars 4.19k forks source link

Trying to use Conv nets with the knifey dataset #40

Closed maziello closed 7 years ago

maziello commented 7 years ago

I'm trying to train the neural network in tutorial 02 from @Hvass-Labs using the knifey dataset. All is fine until I try to run a modified version of the print_test_accuracy() function. In the while loop, images and labels are loaded correctly but when it tries to run the session to predict the result, everything crashes and the python kernel has to be restarted. I modified placeholder x to have the following shape: x = tf.placeholder(tf.float32, shape=[None, img_height, img_width, num_channels], name='x')

Could you please help me understand what am I doing wrong? I pasted the modified print_test_accuracy() function below. Thanks a lot for you help,

`# Split the test-set into smaller batches of this size. test_batch_size = 64

def print_test_accuracy(show_example_errors=False, show_confusion_matrix=False):

# Number of images in the test-set.
num_test = len(image_paths_test)

# Allocate an array for the predicted classes which
# will be calculated in batches and filled into this array.
cls_pred = np.zeros(shape=num_test, dtype=np.int)

# Now calculate the predicted classes for the batches.
# We will just iterate through all the batches.
# There might be a more clever and Pythonic way of doing this.

# The starting index for the next batch is denoted i.
i = 0

while i < num_test:
    # The ending index for the next batch is denoted j.
    j = min(i + test_batch_size, num_test)

    # Get the images from the test-set between index i and j.
    images = load_images(image_paths=image_paths_test[i:j])

    # Get the associated labels.
    labels = labels_test[i:j]

    # Create a feed-dict with these images and labels.
    feed_dict = {x: images, y_true: labels}

    # Calculate the predicted class using TensorFlow.
    cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

    # Set the start-index for the next batch to the
    # end-index of the current batch.
    i = j

# Convenience variable for the true class-numbers of the test-set.
cls_true = test_classes

# Create a boolean array whether each image is correctly classified.
correct = (cls_true == cls_pred)

# Calculate the number of correctly classified images.
# When summing a boolean array, False means 0 and True means 1.
correct_sum = correct.sum()

# Classification accuracy is the number of correctly classified
# images divided by the total number of images in the test-set.
acc = float(correct_sum) / num_test

# Print the accuracy.
msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
print(msg.format(acc, correct_sum, num_test))

# Plot some examples of mis-classifications, if desired.
if show_example_errors:
    print("Example errors:")
    plot_example_errors(cls_pred=cls_pred, correct=correct)

# Plot the confusion matrix, if desired.
if show_confusion_matrix:
    print("Confusion Matrix:")
    plot_confusion_matrix(cls_pred=cls_pred) `
Hvass-Labs commented 7 years ago

In general I cannot answer questions like this, because it would simply take too much of my time if I had to help everyone who tried to mod my code and got an error somewhere. People expect too much of me. You also don't even print the error message here so it is impossible to say what might be the problem. I would suggest that you first google search for the error-codes, then try adding lots of print statements until you find the problem. Then please complete this thread you opened by writing the full error-message and the solution you found, so it may help others in the future. When you're done please close the thread.

maziello commented 7 years ago

Excellent. Thanks