Commit a233abf7 authored by Rudolf Chrispens's avatar Rudolf Chrispens
Browse files

complete eval upload

parent bcfcc1dc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
[{"caption": "A man riding a surfboard on a wave on the beach at the ocean . <END>", "image_id": 532481, "image_name": "000000532481.jpg"}, {"caption": "A man riding a surfboard on a wave on the beach at the ocean . <END>", "image_id": 532481, "image_name": "000000532481.jpg"}, {"caption": "A man riding a surfboard on a wave on the beach at the ocean . <END>", "image_id": 532481, "image_name": "000000532481.jpg"}, {"caption": "A man riding a surfboard on a wave on the beach at the ocean . <END>", "image_id": 532481, "image_name": "000000532481.jpg"}, {"caption": "A man riding a surfboard on a wave on the beach at the ocean . <END>", "image_id": 532481, "image_name": "000000532481.jpg"}, {"caption": "A woman holding a child in her hand and a man holding a baby in a glass and a frisbee", "image_id": 458755, "image_name": "000000458755.jpg"}, {"caption": "A woman holding a child in her hand and a man holding a baby in a glass and a frisbee", "image_id": 458755, "image_name": "000000458755.jpg"}, {"caption": "A woman holding a child in her hand and a man holding a baby in a glass and a frisbee", "image_id": 458755, "image_name": "000000458755.jpg"}, {"caption": "A woman holding a child in her hand and a man holding a baby in a glass and a frisbee", "image_id": 458755, "image_name": "000000458755.jpg"}, {"caption": "A woman holding a child in her hand and a man holding a baby in a glass and a frisbee", "image_id": 458755, "image_name": "000000458755.jpg"}, {"caption": "A table with a candle , glass of wine , and magazine . <END>", "image_id": 385029, "image_name": "000000385029.jpg"}, {"caption": "A table with a candle , glass of wine , and magazine . <END>", "image_id": 385029, "image_name": "000000385029.jpg"}, {"caption": "A table with a candle , glass of wine , and magazine . <END>", "image_id": 385029, "image_name": "000000385029.jpg"}, {"caption": "A table with a candle , glass of wine , and magazine . <END>", "image_id": 385029, "image_name": "000000385029.jpg"}, {"caption": "A table with a candle , glass of wine , and magazine . <END>", "image_id": 385029, "image_name": "000000385029.jpg"}, {"caption": "A hot dog with sauce on it and a mug sitting on a bench on a table . <END>", "image_id": 311303, "image_name": "000000311303.jpg"}, {"caption": "A hot dog with sauce on it and a mug sitting on a bench on a table . <END>", "image_id": 311303, "image_name": "000000311303.jpg"}, {"caption": "A hot dog with sauce on it and a mug sitting on a bench on a table . <END>", "image_id": 311303, "image_name": "000000311303.jpg"}, {"caption": "A hot dog with sauce on it and a mug sitting on a bench on a table . <END>", "image_id": 311303, "image_name": "000000311303.jpg"}, {"caption": "A hot dog with sauce on it and a mug sitting on a bench on a table . <END>", "image_id": 311303, "image_name": "000000311303.jpg"}]
 No newline at end of file
+1 −0

File added.

Preview size limit exceeded, changes collapsed.

caption_lib/cnn/cnn.py

deleted100644 → 0
+0 −284
Original line number Diff line number Diff line
#ACHTUNG das ist aus dem python notebookk kopiert!
#ihr könnt alles komplett ansehen indem ihr einfach das "ipython notebook" startet und nachseht!

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import math

# Import data
fashion_mnist = input_data.read_data_sets('data/fashion', one_hot=True)

# Target classes 
label_dict = {
 0: 'T-shirt/top',
 1: 'Trouser',
 2: 'Pullover',
 3: 'Dress',
 4: 'Coat',
 5: 'Sandal',
 6: 'Shirt',
 7: 'Sneaker',
 8: 'Bag',
 9: 'Ankle boot'
}

# Shapes of training set
print("Training set (images) shape: {shape}".format(shape=fashion_mnist.train.images.shape))
print("Training set (labels) shape: {shape}".format(shape=fashion_mnist.train.labels.shape))

# Shapes of validation set
print("Validation set (images) shape: {shape}".format(shape=fashion_mnist.validation.images.shape))
print("Validation set (labels) shape: {shape}".format(shape=fashion_mnist.validation.labels.shape))

# Shapes of test set
print("Test set (images) shape: {shape}".format(shape=fashion_mnist.test.images.shape))
print("Test set (labels) shape: {shape}".format(shape=fashion_mnist.test.labels.shape))

# Visualize random sample
sample_no = np.random.randint(0, fashion_mnist.train.num_examples)
# Get 28x28 image
sample_1 = fashion_mnist.train.images[sample_no].reshape(28,28)
# Get corresponding integer label from one-hot encoded data
sample_label_1 = np.where(fashion_mnist.train.labels[sample_no] == 1)[0][0]
# Plot sample
print("Random sample {}: y = {} ({})".format(sample_no, sample_label_1, label_dict[sample_label_1]))
plt.imshow(sample_1, cmap='Greys')
plt.show()

def accuracy(true_labels, predicted_labels):
    """ 
    Compute the classification accuracy for given labels and predictions
    :param true_labels: list of gold labels
    :param predicted_labels: list of predicted labels of same length as true_labels
    :return: a scalar between 0 and 1 describing how accurate the predicted labels are
    
    True Positives (TP): number of positive examples, labeled as such.
    False Positives (FP): number of negative examples, labeled as positive.
    True Negatives (TN): number of negative examples, labeled as such.
    False Negatives (FN): number of positive examples, labeled as negative.
    
    Definition of accuracy: (without precision and recall)
    accuracy = (TP + TN)/(TP + TN + FP + FN)
    """
    accuracy = 0.0
    
    TP = 0;
    TN = 0;
    FP = 0;
    FN = 0;
    
    for i in range(0, len(true_labels)):
        if(true_labels[i] == predicted_labels[i] ):
            TP += 1
        #elif(true_labels[i] == False and predicted_labels[i] == False):
         #   TN += 1
        elif(true_labels[i] != predicted_labels[i]):
            FP += 1
        #elif(true_labels[i] == True and predicted_labels[i] == False):
         #   FN += 1

    accuracy = (TP + TN)/(TP + TN + FP + FN)
    return accuracy


# Specify the network hyperparameters
n_input = 784  # Fashion MNIST data input (img shape: 28*28)
n_pictureSize = 28
pixelAfterDoublePooling = 7
n_classes = len(label_dict)  # Fashion MNIST total classes (0–9 digits)
n_samples = fashion_mnist.train.num_examples  # Number of examples in training set 
batch_size = 10
n_epochs = 1
summary_freq_batches = 20
learning_rate = 0.012

#convolution
n_cPatchSize = 5
n_cFeatureSize1 = 32
n_cFeatureSize2 = 64

#neurons
neurons = 1024

def weight_variable(shape, name):
  initial = tf.truncated_normal(shape, stddev=0.1, name=name + '_trunc')
  return tf.Variable(initial, name=name)

def bias_variable(shape, name):
  initial = tf.constant(0.1, shape=shape, name=name + '_const')
  return tf.Variable(initial, name=name)

def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x, name):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding='SAME', name=name)

sess = tf.InteractiveSession()

#4d tensor -1 ??? ,28x28 image size, 1 number of color channels
x = tf.placeholder(tf.float32, shape=[None, n_input], name='var_image') #input image
x_image = tf.reshape(x, [-1, n_pictureSize, n_pictureSize, 1], name='var_28Image')
y_ = tf.placeholder(tf.float32, shape=[None, n_classes], name='var_targetOutput') #target output

#weights 10 because e have 10 outputs
W = tf.Variable(tf.zeros([n_input,n_classes]), name='var_weight') #weights

#bias is 10 because we have 10 classes
b = tf.Variable(tf.zeros([n_classes]), name='var_bias') #biases

#5,5 patch size 5x5
#..,1 is input channel size
#..,..,32 is output channel size features
W_conv1 = weight_variable([n_cPatchSize, n_cPatchSize, 1, n_cFeatureSize1], 'var_weightConv1')
b_conv1 = bias_variable([n_cFeatureSize1], 'var_biasConv1')

#5,5 patch size 5x5
#..,1 is input channel size
#..,..,64 is output channel size features
W_conv2 = weight_variable([n_cPatchSize, n_cPatchSize, n_cFeatureSize1, n_cFeatureSize2], 'var_weightConv2')
b_conv2 = bias_variable([n_cFeatureSize2], 'var_biasConv2')

#first layer
#32 features 5x5 patch size
#convolve image with weight and bias and apply EeLu
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1, name='var_h_conv1Relu')
#apply max pool to reduce the images to 14x14
h_pool1 = max_pool_2x2(h_conv1, name='var_h_pool1')

#second layer
#64 features and 5x5 patch size
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2, name='var_h_conv2Relu')
#apply max pool to reduce the images to 7x7
h_pool2 = max_pool_2x2(h_conv2, name='var_h_pool2')

#1024 neurons
W_fc1 = weight_variable([pixelAfterDoublePooling * pixelAfterDoublePooling * n_cFeatureSize2, neurons], 'var_weightFc1')
b_fc1 = bias_variable([neurons], 'var_biasFc1')

#batches reshaping
h_pool2_flat = tf.reshape(h_pool2, [-1, pixelAfterDoublePooling*pixelAfterDoublePooling*n_cFeatureSize2], name='var_h_pool2')
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1, name='var_h_fc1')

#dropout setting to turn on and off
#reduces overfitting while training
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob, name='dropout')

#implement regression model
with tf.variable_scope("regression", reuse=None):
    y = tf.matmul(x,W) + b

    #read out layer (final output)
    W_fc2 = weight_variable([neurons, n_classes], 'var_weightFc2')
    b_fc2 = bias_variable([n_classes], 'var_biasFc2')

    y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1), name="func_predictionStatus")
predicted_label = tf.argmax(y_conv, 1, name="func_predictionLabel")
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32), name="func_accuracy")

assert prediction is not None

#loss function
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv), name="loss_function")

assert loss is not None

#Definiere dann Optimierer (hier: SGD mit konstanter Lernrate learning_rate) und Update-Operation.
optimizer = tf.train.GradientDescentOptimizer(learning_rate, name="optimizer")
update_op = optimizer.minimize(loss, name="update_operation")

assert optimizer is not None
assert update_op is not None

#implementiere summeries
#setup time to get different logs
#import datetime
#current_Time = datetime.datetime.utcnow().strftime("%Y-%m-%d-%I:%M%p")

tf.summary.scalar("loss", loss)
#loss_writer.add_graph(sess.graph)
merged = tf.summary.merge_all()
#loss_writer = tf.summary.FileWriter('summaries') #/loss/l_' + current_Time)

#trainiere das Modell
# Start a session
with tf.Session() as sess:
    # Specify where summaries are written
    train_writer = tf.summary.FileWriter('summaries/train', sess.graph)
    valid_writer = tf.summary.FileWriter('summaries/valid')
    
    # YOUR CODE HERE
    sess.run(tf.global_variables_initializer())
    
    # Training
    total_count = 0
    for epoch_no in range(n_epochs):
        for batch_no in range(int(n_samples/batch_size)):
            total_count += batch_size
            batch_inputs, batch_labels = fashion_mnist.train.next_batch(batch_size=batch_size)
            # TODO: feed inputs to the graph, fetch the update operation, the summary, the loss
            # YOUR CODE HERE
            
            summaries, _, loss_val = sess.run([merged, update_op, loss], feed_dict={x: batch_inputs, y_: batch_labels, keep_prob: 0.5})
            #train_accuracy = accuracy.eval(feed_dict={x: batch_inputs, y_: batch_labels, keep_prob: 1.0})
            #update_op.run(feed_dict={x: batch_inputs, y_: batch_labels, keep_prob: 0.5})
        
            if batch_no % summary_freq_batches==0:
                # TODO: add the loss of the training batch to the training summary
                # YOUR CODE HERE
                train_writer.add_summary(summaries, batch_no)
                
        # Once per epoch, we're validating the current model on the validation set
        validation_inputs = fashion_mnist.validation.images
        validation_labels = fashion_mnist.validation.labels

        # TODO: feed validation inputs, fetch predictions and validation loss summary
        val_summaries, validation_predictions, validation_loss = sess.run( [merged, predicted_label, loss], feed_dict={x: validation_inputs, y_: validation_labels, keep_prob: 1.0})
        # YOUR CODE HERE

        print('valid accuracy %g' % accuracy.eval(feed_dict={ x: validation_inputs, y_: validation_labels, keep_prob: 1.0}))
    
        #print('test accuracy %g' % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
    
        # TODO: add the validation loss to the validation summary
        # YOUR CODE HERE
        valid_writer.add_summary(val_summaries, epoch_no)
        true_validation_labels = np.argmax(validation_labels, axis=1)

        # Compute the accuracy of the validation predictions
        #print("Validation accuracy at iteration {}: {:.2f}%".format(total_count, accuracy(true_validation_labels, validation_predictions)*100))
    
    # Test on the test set
    test_inputs = fashion_mnist.test.images
    test_labels = fashion_mnist.test.labels
    
    # TODO: feed test inputs, fetch predictions
    # YOUR CODE HERE
    test_summaries, test_predictions, test_loss = sess.run( [merged, predicted_label, loss], feed_dict={x: test_inputs, y_: test_labels, keep_prob: 1.0})
    
    true_test_labels = np.argmax(test_labels, axis=1)

    # Compute the accuracy of the validation predictions
    print('test accuracy %g' % accuracy.eval(feed_dict={ x: test_inputs, y_: test_labels, keep_prob: 1.0}))
    #print("Test accuracy after training of {} iterations: {:.2f}%".format(total_count, accuracy(true_test_labels, test_predictions)*100))
    
#I got this sample:
#valid accuracy 0.9132
#test accuracy 0.898

# Random test sample
sample_no = np.random.randint(0, fashion_mnist.test.num_examples)
print("Test prediction: {}".format(label_dict[test_predictions[sample_no]])) 
plt.show()
plt.imshow(test_inputs[sample_no].reshape(28,28), cmap='Greys')
plt.show()

#ACHTUNG das ist aus dem python notebookk kopiert!
#ihr könnt alles komplett ansehen indem ihr einfach das "ipython notebook" startet und nachseht!
 No newline at end of file

caption_lib/cnn/session00.pdf

deleted100644 → 0
−1.8 MiB

File deleted.

caption_lib/cnn/session02.pdf

deleted100644 → 0
−471 KiB

File deleted.

Loading