# A simple MNIST classifier (linear regression) with tensorflow
#

#
# Usage:
# python2.7 -m pip install tensorflow
# python3 board.py
# python3.5 board.py
#
# tensorboard --logdir=summary
# 
# JSCH 2017-04-23

from __future__ import print_function

import os
import sys
import numpy as np

import tensorflow as tf
from tensorflow.contrib.data import Dataset, Iterator

this_folder =  os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, this_folder+ '/../dataPreprocessing/OnTabeasData/Step4CollectTogether/')
import analyse_Fingerset
sys.path.insert(0,this_folder+"/../helperfunctions/")
import heinzAPI as hAPI
import parserClassFingers as pC

import cv2
import matplotlib.pyplot as plt

#get default-Hyperparameters, btw. Parameters from shell-script:
parser_object = pC.make_parser()

name                            = parser_object.modelname
print(name)
batchSize                       = parser_object.batch_Size
learning_rate                   = parser_object.learning_rate
num_threads                     = parser_object.num_Threads
buffer_size                     = parser_object.buffer_Size
origin_path                     = parser_object.origin_Path
nr_of_epochs                    = parser_object.nr_of_epochs
nr_of_epochs_until_save_model   = parser_object.nr_of_epochs_until_save_model
dropout                         = parser_object.dropout_bool
batchnorm                       = parser_object.batchnorm_bool
test                            = parser_object.test_bool



def traindata_preprocessor(picname,label):
    content = tf.read_file(origin_path + "trainDataset/" + picname)
    image = tf.image.decode_png(content,channels=1)
    image = tf.image.convert_image_dtype(image,tf.float32)
    #here could be done a random crop, but attention to the labels!!
    return image,label
def validdata_preprocessor(picname,label):
    content = tf.read_file(origin_path +"validDataset/"+ picname)
    image = tf.image.decode_png(content,channels=1)
    image = tf.image.convert_image_dtype(image,tf.float32)
    #here could be done a random crop, but attention to the labels!!
    return image,label
    
def main():
    print("TensorFlow version ", tf.__version__)
    with tf.name_scope("Data") as scope:
        print("read in all Train Picture-Names & Labels and shuffle them")
        ReadData        = analyse_Fingerset.Dataset_Heinz()
        
        train_data      = ReadData.get_train_data(origin_path = origin_path)
        train_picnames  = [row[0] for row in train_data]
        train_labels    = np.float32([row[1] for row in train_data])#puts a 4 Dimensional Vector to train_labels
        train_data      = Dataset.from_tensor_slices((train_picnames,train_labels))
        train_data      = train_data.repeat()
        train_data      = train_data.shuffle(buffer_size=buffer_size)
        train_data      = train_data.map(map_func=traindata_preprocessor,
                                         num_threads=num_threads,
                                         output_buffer_size=10000)
        train_data      = train_data.batch(batchSize)

        print("read in all Valid Picture-Names & Labels and shuffle them")
        valid_data      = ReadData.get_valid_data(origin_path=origin_path)
        valid_picnames  = [row[0] for row in valid_data]
        valid_labels    = np.float32([row[1] for row in valid_data])
        valid_data      = Dataset.from_tensor_slices((valid_picnames,valid_labels))
        valid_data      = valid_data.repeat()
        valid_data      = valid_data.shuffle(buffer_size=buffer_size)
        valid_data      = valid_data.map(map_func=validdata_preprocessor,
                                         num_threads=num_threads,
                                         output_buffer_size=10000)
        valid_data      = valid_data.batch(batchSize)

        print("read in all Test Picture-Names & Labels and shuffle them")  
        test_data       = ReadData.get_valid_data(origin_path=origin_path)
        test_picnames   = [row[0] for row in test_data]
        test_labels    = np.float32([row[1] for row in test_data])
        test_data      = Dataset.from_tensor_slices((test_picnames,test_labels))
        test_data      = test_data.map(map_func=validdata_preprocessor,
                                         num_threads=num_threads,
                                         output_buffer_size=10000)
        #No Shuffler and no Repeat for Testdata, because every Testpicture
        #shall be used exactly once
        test_data       = test_data.batch(batchSize)

    print("finished read all Pictures and Labels, start Data-iterator")
    
    with tf.name_scope("Data-Iterator") as scope:        
        iterator        = Iterator.from_structure(train_data.output_types, train_data.output_shapes)
        images_unnormalized, labels  = iterator.get_next()
        
        training_init_op    = iterator.make_initializer(train_data)
        validation_init_op  = iterator.make_initializer(valid_data)
        testing_init_op     = iterator.make_initializer(test_data)
        images_unnormalized = tf.image.resize_images(images_unnormalized,[448,448])
        images_unnormalized = tf.cast(images_unnormalized,tf.float32)
        
        #Puts the a few of the first Pictures to tensorboard
        tf.summary.image('images_after_crop',tensor = images_unnormalized , max_outputs=20)
            
    #is true,if the model is training right now, and is False, if the model is in validation.
    training = tf.placeholder(tf.bool, name='training')
    learnrate = tf.placeholder(tf.float32, name='learnrate')
        
    with tf.name_scope("normalize_pictures") as scope:                            
        images = hAPI.normalize_pictures(tensor=images_unnormalized)
#==============================================================================                                                 
# Graph                                           
#==============================================================================
    #Conv. Layer 7x7x64-s-2                                                  
    output_1 = hAPI.convLayerPretrained(tensor=images,layerNr=1,batchSize=batchSize, filterwidth=7, inputdepth=1, outputdepth=64, strides=2, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("2_Maxpool_Layer") as scope:
        output_2 = tf.nn.max_pool(output_1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME', name="maxpool_2")
    #Conv Layer 3x3x192
    output_3 = hAPI.convLayerPretrained(tensor=output_2,layerNr=3,batchSize=batchSize, filterwidth=3, inputdepth=64, outputdepth=192, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("4_Maxpool_Layer") as scope:
        output_4 = tf.nn.max_pool(output_3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME', name="maxpool_4")
    #Conv Layer 1x1x128
    output_5 = hAPI.convLayerPretrained(tensor=output_4,layerNr=5,batchSize=batchSize, filterwidth=1, inputdepth=192, outputdepth=128, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x256 
    output_6 = hAPI.convLayerPretrained(tensor=output_5,layerNr=6,batchSize=batchSize, filterwidth=3, inputdepth=128, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_7 = hAPI.convLayerPretrained(tensor=output_6,layerNr=7,batchSize=batchSize, filterwidth=1, inputdepth=256, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_8 = hAPI.convLayerPretrained(tensor=output_7,layerNr=8,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("9_Maxpool_Layer") as scope:
        output_9 = tf.nn.max_pool(output_8,ksize=[1,2,2,1],strides=[1,2,2,1], padding="SAME", name="maxpool_9")
    #Conv Layer 1x1x256
    output_10 = hAPI.convLayerPretrained(tensor=output_9,layerNr=10,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_11 = hAPI.convLayerPretrained(tensor=output_10,layerNr=11,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_12 = hAPI.convLayerPretrained(tensor=output_11,layerNr=12,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_13 = hAPI.convLayerPretrained(tensor=output_12,layerNr=13,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_14 = hAPI.convLayerPretrained(tensor=output_13,layerNr=14,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_15 = hAPI.convLayerPretrained(tensor=output_14,layerNr=15,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_16 = hAPI.convLayerPretrained(tensor=output_15,layerNr=16,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_17 = hAPI.convLayerPretrained(tensor=output_16,layerNr=17,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x512
    output_18 = hAPI.convLayerPretrained(tensor=output_17,layerNr=18,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_19 = hAPI.convLayerPretrained(tensor=output_18,layerNr=19,batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("20_Maxpool_Layer") as scope:
        output_20 = tf.nn.max_pool(output_19,ksize=[1,2,2,1],strides=[1,2,2,1], padding="SAME", name="maxpool_20")
    #Conv Layer 1x1x512
    output_21 = hAPI.convLayerPretrained(tensor=output_20,layerNr=21,batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_22 = hAPI.convLayerPretrained(tensor=output_21,layerNr=22,batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x512
    output_23 = hAPI.convLayerPretrained(tensor=output_22,layerNr=23,batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_24 = hAPI.convLayerPretrained(tensor=output_23,layerNr=24,batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_25 = hAPI.convLayer(tensor=output_24,layerNr=25,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training)
    #Conv Layer 3x3x1024-s-2
    output_26 = hAPI.convLayer(tensor=output_25,layerNr=26,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=2, batchnorm_=batchnorm, dropout_=False,training=training)
    #Conv Layer 3x3x1024
    output_27 = hAPI.convLayer(tensor=output_26,layerNr=27,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training)
    #Conv Layer 3x3x1024
    output_28 = hAPI.convLayer(tensor=output_27,layerNr=28,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training)
    #Fully-Connected Layer ==> make vector
    with tf.name_scope("Layer31_full") as scope:
        input_31 = tf.reshape(tensor=output_28,shape=[batchSize,7*7*1024])
        if dropout == True:
            with tf.name_scope("dropout"):        
                #dropout only over all the feature-maps and batches.
                input_31 = tf.cond(training,
                                      lambda:tf.nn.dropout(x=input_31, keep_prob=0.8,noise_shape=[batchSize,7*7*1024]),
                                      lambda:input_31)
        W31 = tf.Variable(tf.truncated_normal(shape=[7*7*1024,4096],stddev=0.01,dtype=tf.float32),name="W31")
        b31 = tf.Variable(tf.truncated_normal(shape=[4096],stddev=0.01,dtype=tf.float32),name="b31")
        preactivate_31 = tf.add(tf.matmul(input_31,W31),b31)
        with tf.name_scope("batch_norm"):
            input_depth_31 = preactivate_31.get_shape().as_list()[-1]#takes the last element which is in this case 64
            #make new weights and new bias
            with tf.name_scope("beta"):
                beta31 = tf.Variable(tf.constant(0.0,shape=[input_depth_31],dtype=tf.float32), name="beta",trainable=True)
            with tf.name_scope("gamma"):
                gamma31 = tf.Variable(tf.constant(1.0,shape=[input_depth_31],dtype=tf.float32),name="gamma",trainable=True)
            batch_mean31, batch_variance31 = tf.nn.moments(x=preactivate_31,axes=[0,1])
            
            preactivate_31 = tf.nn.batch_normalization(x=preactivate_31,mean=batch_mean31,variance=batch_variance31,offset=beta31,scale=gamma31,variance_epsilon=1e-4,name=None) 
        output_31 = tf.nn.relu(preactivate_31)
        with tf.name_scope("summary"):
            hAPI.variable_summaries(variable=W31,name="W31")
            hAPI.variable_summaries(variable=b31,name="b31")
            hAPI.variable_summaries(variable=preactivate_31, name="preactivate31")
            hAPI.variable_summaries(variable=output_31, name="output31")
    #Fully-Connected Layer ==> make tensor again.
    with tf.name_scope("Layer32_full") as scope:
        if dropout == True:
            with tf.name_scope("dropout"):        
                #dropout only over all the feature-maps and batches.
                output_31 = tf.cond(training,
                                      lambda:tf.nn.dropout(x=output_31, keep_prob=0.8,noise_shape=[batchSize,4096]),
                                      lambda:output_31)
        W32 = tf.Variable(tf.truncated_normal(shape=[4096,7*7*6],stddev=0.01,dtype=tf.float32),name="W32")
        b32 = tf.Variable(tf.truncated_normal(shape=[7*7*6],stddev=0.01,dtype=tf.float32),name="b32")
        preactivate_32 = tf.add(tf.matmul(output_31,W32),b32)
        with tf.name_scope("batch_norm"):
            input_depth_32 = preactivate_32.get_shape().as_list()[-1]#takes the last element which is in this case 64
            #make new weights and new bias
            with tf.name_scope("beta"):
                beta32 = tf.Variable(tf.constant(0.0,shape=[input_depth_32],dtype=tf.float32), name="beta",trainable=True)
            with tf.name_scope("gamma"):
                gamma32 = tf.Variable(tf.constant(1.0,shape=[input_depth_32],dtype=tf.float32),name="gamma",trainable=True)
            batch_mean32, batch_variance32 = tf.nn.moments(x=preactivate_32,axes=[0,1])
            preactivate_32 = tf.nn.batch_normalization(x=preactivate_32,mean=batch_mean32,variance=batch_variance32,offset=beta32,scale=gamma32,variance_epsilon=1e-4,name=None)   
        fully_32 = preactivate_32#tf.nn.relu(preactivate_32)
        output_32 = tf.sigmoid(tf.reshape(tensor=fully_32, shape=[batchSize,7,7,6]))
        output_32 = tf.cast(output_32,tf.float32)
        with tf.name_scope("summary"):                        
            hAPI.variable_summaries(variable=W32,name="W32")
            hAPI.variable_summaries(variable=b32,name="b32")
            hAPI.variable_summaries(variable=preactivate_32, name="preactivate32")
            hAPI.variable_summaries(variable=output_32, name="output32")
            
        
    with tf.name_scope("cost_function") as scope:
        x1_output = tf.squeeze(output_32[:,:,:,0])
        x_label   = tf.squeeze(labels[:,:,:,0])

        y1_output = tf.squeeze(output_32[:,:,:,1])
        y_label   = tf.squeeze(labels[:,:,:,1])

        h1_output = tf.squeeze(output_32[:,:,:,2])
        h_label  = tf.squeeze(labels[:,:,:,2])
        
        w1_output = tf.squeeze(output_32[:,:,:,3])
        w_label   = tf.squeeze(labels[:,:,:,3])

        c1_output = tf.squeeze(output_32[:,:,:,4])
        
        p_output  = tf.squeeze(output_32[:,:,:,5])
        p_label   = tf.squeeze(labels[:,:,:,4])
        
        with tf.name_scope("x-costs"):
            x1_difference = tf.subtract(x1_output,x_label)           
            x1_squared = tf.square(x1_difference)
            x1_costs = tf.multiply(p_label,x1_squared)
            x1_costs = tf.reduce_sum(x1_costs)
            x1_costs = tf.multiply(x1_costs,5)#like in the yolo-paper...
        with tf.name_scope("y-costs"):
            y1_difference = tf.subtract(y1_output,y_label)           
            y1_squared = tf.square(y1_difference)
            y1_costs = tf.multiply(p_label,y1_squared)
            y1_costs = tf.reduce_sum(y1_costs)
            y1_costs = tf.multiply(y1_costs,5)#like in the yolo-paper...
        with tf.name_scope("h-costs"):
            h1_output_root = tf.sqrt(h1_output)
            h_label_root = tf.sqrt(h_label)
            h1_difference = tf.subtract(h1_output_root,h_label_root)
            h1_squared = tf.square(h1_difference)
            h1_costs = tf.multiply(p_label,h1_squared)
            h1_costs = tf.reduce_sum(h1_costs)
            h1_costs = tf.multiply(h1_costs,5)#like in the yolo-paper...
        with tf.name_scope("w-costs"):
            w1_output_root = tf.sqrt(w1_output)
            w_label_root = tf.sqrt(w_label)
            w1_difference = tf.subtract(w1_output_root,w_label_root)
            w1_squared = tf.square(w1_difference)
            w1_costs = tf.multiply(p_label,w1_squared)
            w1_costs = tf.reduce_sum(w1_costs)
            w1_costs = tf.multiply(w1_costs,5)
        with tf.name_scope("C-costs"):
            w_label_half = tf.div(w_label,2)
            h_label_half = tf.div(h_label,2)

            w1_output_half= tf.div(w1_output,2)
            h1_output_half= tf.div(h1_output,2)
            
            label_left = tf.subtract(x_label,w_label_half)
            label_right= tf.add(x_label,w_label_half)
            label_top = tf.subtract(y_label,h_label_half)
            label_bottom=tf.add(y_label,h_label_half)
            
            output1_left = tf.subtract(x1_output,w1_output_half)
            output1_right= tf.add(x1_output,w1_output_half)
            output1_top = tf.subtract(y1_output,h1_output_half)
            output1_bottom= tf.add(y1_output,h1_output_half)

            overlap1_left = tf.maximum(label_left,output1_left)
            overlap1_right= tf.minimum(label_right,output1_right)
            overlap1_top = tf.maximum(label_top,output1_top)
            overlap1_bottom= tf.minimum(label_bottom,output1_bottom)
            
            overlap1_width = tf.subtract(overlap1_right,overlap1_left)
            overlap1_width = tf.maximum(np.float32(0),overlap1_width)
            overlap1_height = tf.subtract(overlap1_bottom,overlap1_top)
            overlap1_height = tf.maximum(np.float32(0),overlap1_height)
            area_of_overlap1 = tf.multiply(overlap1_width, overlap1_height)
            
            output1_area = tf.multiply(h1_output,w1_output)
            label_area = tf.multiply(h_label,w_label)
            
            area_of_union1 = tf.subtract(tf.add(output1_area,label_area),area_of_overlap1)
            
            IoU1 = tf.div(area_of_overlap1,area_of_union1)
            IoU1_scalar = tf.reduce_mean(IoU1)            
            
            c1_label = tf.multiply(IoU1,p_label)#with more than one finger use here p_label=max(p_label1,p_label2,...)
                                                
            c1_difference = tf.subtract(c1_output,c1_label)
            c1_squared = tf.square(c1_difference)
            
            with tf.name_scope("obj_present"):
                c1_obj = tf.multiply(c1_squared, p_label)
                c1_obj_costs = tf.reduce_sum(c1_obj)
            with tf.name_scope("noobj_present"):
                p_label_invers = tf.subtract(np.float32(1),p_label)# y=1-x   x=0|y=1  & x=1|y=0
                c1_noobj = tf.multiply(c1_squared, p_label_invers)
                c1_noobj_costs = tf.reduce_sum(c1_noobj)
                c1_noobj_costs = tf.multiply(c1_noobj_costs,0.5)#like in the yolo paper...
        with tf.name_scope("p_costs"):
            p_difference = tf.subtract(p_output,p_label)
            p_squared = tf.square(p_difference)
            p_costs = tf.multiply(p_label,p_squared)
            p_costs = tf.reduce_sum(p_costs)
        
        center_costs = tf.add(x1_costs,y1_costs)        
        box_costs = tf.add(h1_costs,w1_costs)
        conf_costs = tf.add(c1_obj_costs,c1_noobj_costs)
        spatial_costs = tf.add(center_costs,box_costs)
        prob_costs = tf.add(conf_costs,p_costs)
        costs = tf.add(spatial_costs,prob_costs)
        
        tf.summary.scalar("Costx",x1_costs)
        tf.summary.scalar("Costy",y1_costs)
        tf.summary.scalar("Costh",h1_costs)
        tf.summary.scalar("Costw",w1_costs)
        tf.summary.scalar("IoU_mean",IoU1_scalar)
        tf.summary.scalar("CostcObj",c1_obj_costs)
        tf.summary.scalar("CostcNoObj",c1_noobj_costs)
        tf.summary.scalar("Costp",p_costs)
        
        tf.summary.scalar("CostCenter",center_costs)
        tf.summary.scalar("CostBox",box_costs)
        tf.summary.scalar("CostConf",conf_costs)
        
        tf.summary.scalar("CostSpatial",spatial_costs)
        tf.summary.scalar("CostProb",prob_costs)
        
        tf.summary.scalar("Costs",costs)

        
    with tf.name_scope("optimizer") as scope:

        optimizer = tf.train.AdamOptimizer(learning_rate=learnrate,epsilon=1e-04)

        grads_and_vars = optimizer.compute_gradients(costs)
        capped_gvs = [(tf.clip_by_value(grad, -5., 5.), var) for grad, var in grads_and_vars]
        
        # Ask the optimizer to apply the capped gradients.
        train_step = optimizer.apply_gradients(capped_gvs)

    #Save Gradients in Tensorboard
    for grad, var in capped_gvs:
        if grad is not None:
            tf.summary.histogram(var.op.name +"capped_gradients",grad)
        
    with tf.name_scope("Test") as scope:        
        with tf.name_scope("IOU"):
            nr_of_fingers_in_batch = tf.reduce_sum(p_label)
            
            y_label_index = tf.argmax(tf.reduce_max(p_label,axis=[2]),axis=1)
            x_label_index  = tf.argmax(tf.reduce_max(p_label,axis=[1]),axis=1)
            y_label_global = tf.add((tf.cast(y_label_index,tf.float32)/7) , (tf.reduce_max((y_label/7),axis=[1,2])))
            x_label_global = tf.add((tf.cast(x_label_index,tf.float32)/7) , (tf.reduce_max((x_label/7),axis=[1,2])))
            h_label_global = tf.reduce_max(h_label,axis=[1,2])
            w_label_global = tf.reduce_max(w_label,axis=[1,2])
            
            probconf_output = tf.multiply(p_output,c1_output)            
            y_probconf_index = tf.argmax(tf.reduce_max(probconf_output, axis=[2]),axis=1)
            x_probconf_index = tf.argmax(tf.reduce_max(probconf_output, axis=[1]),axis=1)
            
            
            #make tensor to extract the values for x,y,w & h at the point with highest prob/conf
            enumerator = tf.constant(range(batchSize),tf.int64)
            probconf_indices_tensor = tf.stack([enumerator,y_probconf_index,x_probconf_index],1)
            
            y_probconf_coarse = tf.cast(y_probconf_index,tf.float32)/7            
            y_probconf_fine = tf.squeeze(tf.gather_nd(y1_output,probconf_indices_tensor))/7
            y_probconf = tf.add(y_probconf_coarse, y_probconf_fine)
            x_probconf_coarse = tf.cast(x_probconf_index,tf.float32)/7
            x_probconf_fine = tf.squeeze(tf.gather_nd(x1_output,probconf_indices_tensor))/7
            x_probconf = tf.add(x_probconf_coarse, x_probconf_fine)
            h_probconf =  tf.squeeze(tf.gather_nd(h1_output,probconf_indices_tensor))
            w_probconf =  tf.squeeze(tf.gather_nd(w1_output,probconf_indices_tensor))            

            iou_max_prob_conf = hAPI.iou(x_label_global,y_label_global,h_label_global,w_label_global,y_probconf,x_probconf,h_probconf,w_probconf)
            
            iou_max_probconf_mean = tf.reduce_sum(iou_max_prob_conf)/nr_of_fingers_in_batch
            

            tf.summary.scalar("iou_max_probconf",iou_max_probconf_mean)
            
        with tf.name_scope("distance"):
            #only test the pictures with fingers on distance, those, which don't have a finger, mustn't be tested!!!
            p_labels = tf.reduce_sum(p_label,axis=[1,2])
            x_probconfs = tf.multiply(x_probconf,p_labels)
            y_probconfs = tf.multiply(y_probconf,p_labels)
            x_labels_global = tf.multiply(x_label_global,p_labels)
            y_labels_global = tf.multiply(y_label_global,p_labels)
            
            x_probconf_diff = tf.subtract(x_labels_global, x_probconfs)
            y_probconf_diff = tf.subtract(y_labels_global, y_probconfs)            
            
            distance_probconf = tf.sqrt(tf.add(tf.square(x_probconf_diff),tf.square(y_probconf_diff)))
            #Problem: when only testing distance on pictures with fingers, the pictures without fingers have distance 0, what makes a bias to the mean-distance.
            #Solution: Make new vectors, which doesn't include Pictures, where no finger exist.
            zeros_vector = tf.zeros(batchSize)
            boolean_vector = tf.squeeze(tf.not_equal(zeros_vector,p_labels))
            boolean_vector.set_shape([None])#to prevent problems with tf.boolean_mask()
            distance_probconfs = tf.boolean_mask(distance_probconf,boolean_vector)
            
            mean_distance_probconf, var_distance_probconf = tf.nn.moments(distance_probconfs, axes=[0])
            
            tf.summary.scalar("mean_distance_probconf",mean_distance_probconf)
            tf.summary.scalar("var_distance_probconf",var_distance_probconf)
                        
    
    init_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
    #initialize the Saver, which organizes the Saving of all Weights
    saver = tf.train.Saver(
        max_to_keep=5,
        keep_checkpoint_every_n_hours=4.0, 
        pad_step_number=True,
        save_relative_paths=True,)
    #Make a own directory for the weights of this Training.
    if not os.path.exists(origin_path + "../../../weights/"+name+"/"):
        os.makedirs(origin_path + "../../../weights/"+name+"/")

    
    merged_summary_op = tf.summary.merge_all()

    with tf.Session() as sess:
        #Initialize different Datasets
        sess.run(testing_init_op)
        sess.run(validation_init_op)
        sess.run(training_init_op)
        sess.run(init_op)
                
        if(test==False):
            #initialize three different Tensorflow-Summaries
            train_train_writer = tf.summary.FileWriter(origin_path + "../../../summarys/training/summary_" + name + "_traintrain")
            train_train_writer.add_graph(sess.graph)             
            train_writer=tf.summary.FileWriter(origin_path + "../../../summarys/training/summary_" + name + "_train")
            train_writer.add_graph(sess.graph) 
            valid_writer=tf.summary.FileWriter(origin_path + "../../../summarys/training/summary_" + name + "_valid")
            valid_writer.add_graph(sess.graph) 
            


            #saver.restore(sess=sess, save_path=origin_path + "../../../weights/7BnormBeforeRelu2.ckpt-00103000")
            print("start training....\n")
            for i in range(nr_of_epochs/nr_of_epochs_until_save_model):
                for j in range(nr_of_epochs_until_save_model):
                    _ = sess.run([train_step],feed_dict={training: True, learnrate : learning_rate})                        
                
                #testing in traindata while training
                numbers_of_iterations_until_now = i*nr_of_epochs_until_save_model+j+1 
                _,sumsum = sess.run([train_step,merged_summary_op],feed_dict={training: True, learnrate : learning_rate})                
                train_train_writer.add_summary(sumsum,(numbers_of_iterations_until_now))                
                
                #testing on traindata
                train_writer.add_summary(sess.run(merged_summary_op,feed_dict={training: False, learnrate : learning_rate}),(numbers_of_iterations_until_now))

                #testing on validationdata:
                sess.run(validation_init_op)
                valid_writer.add_summary(sess.run(merged_summary_op,feed_dict={training: False, learnrate : learning_rate}),(numbers_of_iterations_until_now))            
                sess.run(training_init_op)
                
                #save Model
                saver.save(sess=sess, save_path=origin_path + "../../../weights/"+name+"/"+name+".ckpt", global_step=(numbers_of_iterations_until_now))
                print("model updatet\n")

                #training:


        else:
            #Make directory to store Results in Pictures and Histogramms
            if not os.path.exists(origin_path +"picsRecognized/"):
                os.makedirs(origin_path +"picsRecognized/")             
            
            sess.run(testing_init_op)
            #Restore Weights for Testing
            #only needed, if Test is done without Training.
            print("Try to restore")
            saver.restore(sess,origin_path + "../../../weights/139_9000Set_newPipe/139_9000Set_newPipe.ckpt-00510000")                
            print("Restored")  
            
            #initialize running Variables
            nr_of_fingers = 0
            nr_of_coarse_recognized_fingers = 0
            nr_of_recognized_fingers = 0
            nr_of_fine_recognized_fingers = 0
            nr_of_extremefine_recognized_fingers = 0
            iou_array = []
            distance_array = []
            distance_x_array = []
            distance_y_array = []
            for i in range(len(test_picnames)/batchSize):
                #let one Batch flow through the Graph
                (testimages,
                 probconfs_y,
                 probconfs_x,
                 probconfs_h,
                 probconfs_w,
                 labels_y,
                 labels_x,
                 labels_h,
                 labels_w,
                 probconfs_iou,
                 probconfs_distance,
                 labels_p,
                 probconfs_x_diff, 
                 probconfs_y_diff)  = sess.run([images_unnormalized,
                                                y_probconf,
                                                x_probconf,
                                                h_probconf,
                                                w_probconf,
                                                y_label_global,
                                                x_label_global,
                                                h_label_global,
                                                w_label_global,
                                                iou_max_prob_conf,
                                                distance_probconf,
                                                p_labels,
                                                x_probconf_diff,
                                                y_probconf_diff],   feed_dict={training: False})
                                                
                #Analyze every Element of the Batch
                for b in range(batchSize):
                    print(str(i*batchSize+b))
                    testimage = testimages[b]*200
                    probconf_y  = probconfs_y[b] * 448
                    probconf_x  = probconfs_x[b] * 448
                    probconf_h  = probconfs_h[b] * 448
                    probconf_w  = probconfs_w[b] * 448
                    probconf_iou= probconfs_iou[b]
                    probconf_distance=probconfs_distance[b]
                    probconf_x_diff = probconfs_x_diff[b]
                    probconf_y_diff = probconfs_y_diff[b]
                    label_y     = labels_y[b] * 448
                    label_x     = labels_x[b] * 448
                    label_h     = labels_h[b] * 448
                    label_w     = labels_w[b] * 448
                    print("iou                                                      = "+str(probconf_iou))
                    print("distance                                                 = " + str(probconf_distance))
                    if(labels_p[b]!=0):
                            
                        #make array with all valid iou's and distances, to make later histograms, variances, etc.
                        iou_array = np.concatenate((iou_array,[probconf_iou]))
                        distance_array = np.concatenate((distance_array,[probconf_distance]))
                        distance_x_array = np.concatenate((distance_x_array,[probconf_x_diff]))
                        distance_y_array = np.concatenate((distance_y_array,[probconf_y_diff]))

                        nr_of_fingers += 1

                        print("mean_iou                                                 = " + str(np.mean(iou_array)))
                        print("mean_distance                                            = " + str(np.mean(distance_array)))
                        
                        if probconf_distance < 0.04:
                            nr_of_coarse_recognized_fingers += 1
                        if probconf_distance < 0.02:
                            nr_of_recognized_fingers += 1
                        if probconf_distance < 0.01:
                            nr_of_fine_recognized_fingers += 1
                        if probconf_distance < 0.001:
                            nr_of_extremefine_recognized_fingers += 1
                        
                        coarse_recognized_fingers_in_percent = (float(nr_of_coarse_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of coarse recognized fingers (<0.04)          = " + str(coarse_recognized_fingers_in_percent) + "%")
                        recognized_fingers_in_percent = (float(nr_of_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of recognized fingers (<0.02)                 = " + str(recognized_fingers_in_percent) + "%")
                        fine_recognized_fingers_in_percent = (float(nr_of_fine_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of fine recognized fingers (<0.01)            = " + str(fine_recognized_fingers_in_percent) + "%")
                        extremefine_recognized_fingers_in_percent = (float(nr_of_extremefine_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of extreme fine recognized fingers (>0.001)   = " + str(extremefine_recognized_fingers_in_percent)+"%")
                        
                        
   
                    #save picture in own folder for recognized fingers 
                    sess.run(tf.write_file(origin_path+"picsRecognized/pic" + str(batchSize*i+b)+".png",tf.image.encode_png(testimage)))
                    
                    #get picture back with cv2
                    img = cv2.imread(origin_path+"picsRecognized/pic" + str(batchSize*i+b)+".png")

                    cv2.rectangle(img,(int(probconf_x-(probconf_w/2)),int(probconf_y-(probconf_h/2))),(int(probconf_x+(probconf_w/2)),int(probconf_y+(probconf_h/2))),(0,0,255),1)     
                    cv2.putText(img,'Prediction',(int(probconf_x-(probconf_w/2)),int(probconf_y-(probconf_h/2))),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),1)
                    cv2.putText(img,'iou='+str(probconf_iou),(0,25),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),1)
                    cv2.putText(img,'dist='+str(probconf_distance),(0,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),1)
                    cv2.rectangle(img,(int(label_x-(label_w/2)),int(label_y-(label_h/2))),(int(label_x+(label_w/2)),int(label_y+(label_h/2))),(255,255,0),1)     
                    cv2.putText(img,'Label',(int(label_x-(label_w/2)),int(label_y-(label_h/2))),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),1)                    
                    cv2.imwrite(origin_path+"picsRecognized/pic" + str(batchSize*i+b)+".png",img)
                
            #At the End of the Test-Sequence:
            #Here shall the whole median, mean, variance and histograms of the iou and the distance be calculated and plottet to PDF!!
            plt.hist(iou_array,bins=np.arange(0.0,1.0,0.01),normed=1)
            plt.title("IOU Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("IOU (median="+str(round(np.median(iou_array),3))+", mean="+str(round(np.mean(iou_array),3))+", stdev="+str(round(np.std(iou_array),3))+")")
            plt.ylabel("Probability in % [1/100]")
            plt.plot([0.4,0.4],[0,10], 'orange')
            plt.text(0.3, 9, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(0.45,9, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path+"picsRecognized/IOUprobDensity.pdf")
            plt.close()
            
            plt.hist(distance_array,bins=np.arange(0.0,1.0,0.01),normed=1)
            plt.title("Label/Prediction-Center-Distance Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("Distance (median="+str(round(np.median(distance_array),3))+", mean="+str(round(np.mean(distance_array),3))+", stdev="+str(round(np.std(distance_array),3))+") \n max. possible distance = sqrt(2)")
            plt.ylabel("Probability in % [1/100]")
            plt.plot([0.02,0.02],[0,60], 'orange')
            plt.text(0.05, 55, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07,55, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path+"picsRecognized/distProbDensity.pdf")
            plt.close()  
            
            plt.hist(distance_array,bins=np.arange(0.0,1.0,0.01),normed=1)
            plt.title("Label/Prediction-Center-Distance Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("Distance (median="+str(round(np.median(distance_array),3))+", mean="+str(round(np.mean(distance_array),3))+", stdev="+str(round(np.std(distance_array),3))+") \n max. possible distance = sqrt(2)")
            plt.ylabel("Probability in % [1/100] (Log-Scale)")
            plt.plot([0.02,0.02],[0,100], 'orange')
            plt.text(0.05, 55, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07,55, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.yscale('log')
            plt.savefig(origin_path+"picsRecognized/logdistProbDensity.pdf")
            plt.close() 
            
            distance_array_without_outliers = []
            for dist in distance_array:
                if dist < 0.25:
                    distance_array_without_outliers = np.concatenate((distance_array_without_outliers,[dist]))
            plt.hist(distance_array_without_outliers,bins=np.arange(0.0,0.25,0.001),normed=1)
            plt.title("Label/Prediction-Center-Distance Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures) Without Outliers(Cut at 0.25)")
            plt.xlabel("Distance (median="+str(round(np.median(distance_array_without_outliers),3))+", mean="+str(round(np.mean(distance_array_without_outliers),3))+", stdev="+str(round(np.std(distance_array_without_outliers),3))+")")
            plt.ylabel("Probability in Promille [1/1000]")
            plt.plot([0.02,0.02],[0,100], 'orange')
            plt.text(0.025,90, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.002,90, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path+"picsRecognized/distProbDensity_improved.pdf")
            plt.close() 

            plt.hist(distance_x_array,bins=np.arange(-0.125,0.125,0.001),normed=1)
            plt.title("X-Distance \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("x-Distance (median="+str(round(np.median(distance_x_array),3))+", mean="+str(round(np.mean(distance_x_array),3))+", stdev="+str(round(np.std(distance_x_array),3))+")")
            plt.ylabel("Probability/Distance")
            plt.plot([0.0,0.0],[0,100], 'orange')
            plt.savefig(origin_path+"picsRecognized/xDistance.pdf")
            plt.close()    

            plt.hist(distance_y_array,bins=np.arange(-0.125,0.125,0.001),normed=1)
            plt.title("Y-Distance \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("y-Distance (median="+str(round(np.median(distance_y_array),3))+", mean="+str(round(np.mean(distance_y_array),3))+", stdev="+str(round(np.std(distance_y_array),3))+")")
            plt.ylabel("Probability/Distance")
            plt.plot([0.0,0.0],[0,100], 'orange')
            plt.savefig(origin_path+"picsRecognized/yDistance.pdf")
            plt.close()              

    print("finished")

    


if __name__ == "__main__":
   
    main()

190a9e00d1262f70f975717b7147f9b3db11ced4
<mask token>


def traindata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'trainDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def validdata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'validDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def main():
    print('TensorFlow version ', tf.__version__)
    with tf.name_scope('Data') as scope:
        print('read in all Train Picture-Names & Labels and shuffle them')
        ReadData = analyse_Fingerset.Dataset_Heinz()
        train_data = ReadData.get_train_data(origin_path=origin_path)
        train_picnames = [row[0] for row in train_data]
        train_labels = np.float32([row[1] for row in train_data])
        train_data = Dataset.from_tensor_slices((train_picnames, train_labels))
        train_data = train_data.repeat()
        train_data = train_data.shuffle(buffer_size=buffer_size)
        train_data = train_data.map(map_func=traindata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        train_data = train_data.batch(batchSize)
        print('read in all Valid Picture-Names & Labels and shuffle them')
        valid_data = ReadData.get_valid_data(origin_path=origin_path)
        valid_picnames = [row[0] for row in valid_data]
        valid_labels = np.float32([row[1] for row in valid_data])
        valid_data = Dataset.from_tensor_slices((valid_picnames, valid_labels))
        valid_data = valid_data.repeat()
        valid_data = valid_data.shuffle(buffer_size=buffer_size)
        valid_data = valid_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        valid_data = valid_data.batch(batchSize)
        print('read in all Test Picture-Names & Labels and shuffle them')
        test_data = ReadData.get_valid_data(origin_path=origin_path)
        test_picnames = [row[0] for row in test_data]
        test_labels = np.float32([row[1] for row in test_data])
        test_data = Dataset.from_tensor_slices((test_picnames, test_labels))
        test_data = test_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        test_data = test_data.batch(batchSize)
    print('finished read all Pictures and Labels, start Data-iterator')
    with tf.name_scope('Data-Iterator') as scope:
        iterator = Iterator.from_structure(train_data.output_types,
            train_data.output_shapes)
        images_unnormalized, labels = iterator.get_next()
        training_init_op = iterator.make_initializer(train_data)
        validation_init_op = iterator.make_initializer(valid_data)
        testing_init_op = iterator.make_initializer(test_data)
        images_unnormalized = tf.image.resize_images(images_unnormalized, [
            448, 448])
        images_unnormalized = tf.cast(images_unnormalized, tf.float32)
        tf.summary.image('images_after_crop', tensor=images_unnormalized,
            max_outputs=20)
    training = tf.placeholder(tf.bool, name='training')
    learnrate = tf.placeholder(tf.float32, name='learnrate')
    with tf.name_scope('normalize_pictures') as scope:
        images = hAPI.normalize_pictures(tensor=images_unnormalized)
    output_1 = hAPI.convLayerPretrained(tensor=images, layerNr=1, batchSize
        =batchSize, filterwidth=7, inputdepth=1, outputdepth=64, strides=2,
        batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('2_Maxpool_Layer') as scope:
        output_2 = tf.nn.max_pool(output_1, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_2')
    output_3 = hAPI.convLayerPretrained(tensor=output_2, layerNr=3,
        batchSize=batchSize, filterwidth=3, inputdepth=64, outputdepth=192,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('4_Maxpool_Layer') as scope:
        output_4 = tf.nn.max_pool(output_3, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_4')
    output_5 = hAPI.convLayerPretrained(tensor=output_4, layerNr=5,
        batchSize=batchSize, filterwidth=1, inputdepth=192, outputdepth=128,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_6 = hAPI.convLayerPretrained(tensor=output_5, layerNr=6,
        batchSize=batchSize, filterwidth=3, inputdepth=128, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_7 = hAPI.convLayerPretrained(tensor=output_6, layerNr=7,
        batchSize=batchSize, filterwidth=1, inputdepth=256, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_8 = hAPI.convLayerPretrained(tensor=output_7, layerNr=8,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('9_Maxpool_Layer') as scope:
        output_9 = tf.nn.max_pool(output_8, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_9')
    output_10 = hAPI.convLayerPretrained(tensor=output_9, layerNr=10,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_11 = hAPI.convLayerPretrained(tensor=output_10, layerNr=11,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_12 = hAPI.convLayerPretrained(tensor=output_11, layerNr=12,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_13 = hAPI.convLayerPretrained(tensor=output_12, layerNr=13,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_14 = hAPI.convLayerPretrained(tensor=output_13, layerNr=14,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_15 = hAPI.convLayerPretrained(tensor=output_14, layerNr=15,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_16 = hAPI.convLayerPretrained(tensor=output_15, layerNr=16,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_17 = hAPI.convLayerPretrained(tensor=output_16, layerNr=17,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_18 = hAPI.convLayerPretrained(tensor=output_17, layerNr=18,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_19 = hAPI.convLayerPretrained(tensor=output_18, layerNr=19,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    with tf.name_scope('20_Maxpool_Layer') as scope:
        output_20 = tf.nn.max_pool(output_19, ksize=[1, 2, 2, 1], strides=[
            1, 2, 2, 1], padding='SAME', name='maxpool_20')
    output_21 = hAPI.convLayerPretrained(tensor=output_20, layerNr=21,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_22 = hAPI.convLayerPretrained(tensor=output_21, layerNr=22,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_23 = hAPI.convLayerPretrained(tensor=output_22, layerNr=23,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_24 = hAPI.convLayerPretrained(tensor=output_23, layerNr=24,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_25 = hAPI.convLayer(tensor=output_24, layerNr=25, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_26 = hAPI.convLayer(tensor=output_25, layerNr=26, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=2, batchnorm_=batchnorm, dropout_=False, training=training)
    output_27 = hAPI.convLayer(tensor=output_26, layerNr=27, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_28 = hAPI.convLayer(tensor=output_27, layerNr=28, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    with tf.name_scope('Layer31_full') as scope:
        input_31 = tf.reshape(tensor=output_28, shape=[batchSize, 7 * 7 * 1024]
            )
        if dropout == True:
            with tf.name_scope('dropout'):
                input_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    input_31, keep_prob=0.8, noise_shape=[batchSize, 7 * 7 *
                    1024]), lambda : input_31)
        W31 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 1024, 4096],
            stddev=0.01, dtype=tf.float32), name='W31')
        b31 = tf.Variable(tf.truncated_normal(shape=[4096], stddev=0.01,
            dtype=tf.float32), name='b31')
        preactivate_31 = tf.add(tf.matmul(input_31, W31), b31)
        with tf.name_scope('batch_norm'):
            input_depth_31 = preactivate_31.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta31 = tf.Variable(tf.constant(0.0, shape=[input_depth_31
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma31 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_31], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean31, batch_variance31 = tf.nn.moments(x=preactivate_31,
                axes=[0, 1])
            preactivate_31 = tf.nn.batch_normalization(x=preactivate_31,
                mean=batch_mean31, variance=batch_variance31, offset=beta31,
                scale=gamma31, variance_epsilon=0.0001, name=None)
        output_31 = tf.nn.relu(preactivate_31)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W31, name='W31')
            hAPI.variable_summaries(variable=b31, name='b31')
            hAPI.variable_summaries(variable=preactivate_31, name=
                'preactivate31')
            hAPI.variable_summaries(variable=output_31, name='output31')
    with tf.name_scope('Layer32_full') as scope:
        if dropout == True:
            with tf.name_scope('dropout'):
                output_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    output_31, keep_prob=0.8, noise_shape=[batchSize, 4096]
                    ), lambda : output_31)
        W32 = tf.Variable(tf.truncated_normal(shape=[4096, 7 * 7 * 6],
            stddev=0.01, dtype=tf.float32), name='W32')
        b32 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 6], stddev=
            0.01, dtype=tf.float32), name='b32')
        preactivate_32 = tf.add(tf.matmul(output_31, W32), b32)
        with tf.name_scope('batch_norm'):
            input_depth_32 = preactivate_32.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta32 = tf.Variable(tf.constant(0.0, shape=[input_depth_32
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma32 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_32], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean32, batch_variance32 = tf.nn.moments(x=preactivate_32,
                axes=[0, 1])
            preactivate_32 = tf.nn.batch_normalization(x=preactivate_32,
                mean=batch_mean32, variance=batch_variance32, offset=beta32,
                scale=gamma32, variance_epsilon=0.0001, name=None)
        fully_32 = preactivate_32
        output_32 = tf.sigmoid(tf.reshape(tensor=fully_32, shape=[batchSize,
            7, 7, 6]))
        output_32 = tf.cast(output_32, tf.float32)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W32, name='W32')
            hAPI.variable_summaries(variable=b32, name='b32')
            hAPI.variable_summaries(variable=preactivate_32, name=
                'preactivate32')
            hAPI.variable_summaries(variable=output_32, name='output32')
    with tf.name_scope('cost_function') as scope:
        x1_output = tf.squeeze(output_32[:, :, :, 0])
        x_label = tf.squeeze(labels[:, :, :, 0])
        y1_output = tf.squeeze(output_32[:, :, :, 1])
        y_label = tf.squeeze(labels[:, :, :, 1])
        h1_output = tf.squeeze(output_32[:, :, :, 2])
        h_label = tf.squeeze(labels[:, :, :, 2])
        w1_output = tf.squeeze(output_32[:, :, :, 3])
        w_label = tf.squeeze(labels[:, :, :, 3])
        c1_output = tf.squeeze(output_32[:, :, :, 4])
        p_output = tf.squeeze(output_32[:, :, :, 5])
        p_label = tf.squeeze(labels[:, :, :, 4])
        with tf.name_scope('x-costs'):
            x1_difference = tf.subtract(x1_output, x_label)
            x1_squared = tf.square(x1_difference)
            x1_costs = tf.multiply(p_label, x1_squared)
            x1_costs = tf.reduce_sum(x1_costs)
            x1_costs = tf.multiply(x1_costs, 5)
        with tf.name_scope('y-costs'):
            y1_difference = tf.subtract(y1_output, y_label)
            y1_squared = tf.square(y1_difference)
            y1_costs = tf.multiply(p_label, y1_squared)
            y1_costs = tf.reduce_sum(y1_costs)
            y1_costs = tf.multiply(y1_costs, 5)
        with tf.name_scope('h-costs'):
            h1_output_root = tf.sqrt(h1_output)
            h_label_root = tf.sqrt(h_label)
            h1_difference = tf.subtract(h1_output_root, h_label_root)
            h1_squared = tf.square(h1_difference)
            h1_costs = tf.multiply(p_label, h1_squared)
            h1_costs = tf.reduce_sum(h1_costs)
            h1_costs = tf.multiply(h1_costs, 5)
        with tf.name_scope('w-costs'):
            w1_output_root = tf.sqrt(w1_output)
            w_label_root = tf.sqrt(w_label)
            w1_difference = tf.subtract(w1_output_root, w_label_root)
            w1_squared = tf.square(w1_difference)
            w1_costs = tf.multiply(p_label, w1_squared)
            w1_costs = tf.reduce_sum(w1_costs)
            w1_costs = tf.multiply(w1_costs, 5)
        with tf.name_scope('C-costs'):
            w_label_half = tf.div(w_label, 2)
            h_label_half = tf.div(h_label, 2)
            w1_output_half = tf.div(w1_output, 2)
            h1_output_half = tf.div(h1_output, 2)
            label_left = tf.subtract(x_label, w_label_half)
            label_right = tf.add(x_label, w_label_half)
            label_top = tf.subtract(y_label, h_label_half)
            label_bottom = tf.add(y_label, h_label_half)
            output1_left = tf.subtract(x1_output, w1_output_half)
            output1_right = tf.add(x1_output, w1_output_half)
            output1_top = tf.subtract(y1_output, h1_output_half)
            output1_bottom = tf.add(y1_output, h1_output_half)
            overlap1_left = tf.maximum(label_left, output1_left)
            overlap1_right = tf.minimum(label_right, output1_right)
            overlap1_top = tf.maximum(label_top, output1_top)
            overlap1_bottom = tf.minimum(label_bottom, output1_bottom)
            overlap1_width = tf.subtract(overlap1_right, overlap1_left)
            overlap1_width = tf.maximum(np.float32(0), overlap1_width)
            overlap1_height = tf.subtract(overlap1_bottom, overlap1_top)
            overlap1_height = tf.maximum(np.float32(0), overlap1_height)
            area_of_overlap1 = tf.multiply(overlap1_width, overlap1_height)
            output1_area = tf.multiply(h1_output, w1_output)
            label_area = tf.multiply(h_label, w_label)
            area_of_union1 = tf.subtract(tf.add(output1_area, label_area),
                area_of_overlap1)
            IoU1 = tf.div(area_of_overlap1, area_of_union1)
            IoU1_scalar = tf.reduce_mean(IoU1)
            c1_label = tf.multiply(IoU1, p_label)
            c1_difference = tf.subtract(c1_output, c1_label)
            c1_squared = tf.square(c1_difference)
            with tf.name_scope('obj_present'):
                c1_obj = tf.multiply(c1_squared, p_label)
                c1_obj_costs = tf.reduce_sum(c1_obj)
            with tf.name_scope('noobj_present'):
                p_label_invers = tf.subtract(np.float32(1), p_label)
                c1_noobj = tf.multiply(c1_squared, p_label_invers)
                c1_noobj_costs = tf.reduce_sum(c1_noobj)
                c1_noobj_costs = tf.multiply(c1_noobj_costs, 0.5)
        with tf.name_scope('p_costs'):
            p_difference = tf.subtract(p_output, p_label)
            p_squared = tf.square(p_difference)
            p_costs = tf.multiply(p_label, p_squared)
            p_costs = tf.reduce_sum(p_costs)
        center_costs = tf.add(x1_costs, y1_costs)
        box_costs = tf.add(h1_costs, w1_costs)
        conf_costs = tf.add(c1_obj_costs, c1_noobj_costs)
        spatial_costs = tf.add(center_costs, box_costs)
        prob_costs = tf.add(conf_costs, p_costs)
        costs = tf.add(spatial_costs, prob_costs)
        tf.summary.scalar('Costx', x1_costs)
        tf.summary.scalar('Costy', y1_costs)
        tf.summary.scalar('Costh', h1_costs)
        tf.summary.scalar('Costw', w1_costs)
        tf.summary.scalar('IoU_mean', IoU1_scalar)
        tf.summary.scalar('CostcObj', c1_obj_costs)
        tf.summary.scalar('CostcNoObj', c1_noobj_costs)
        tf.summary.scalar('Costp', p_costs)
        tf.summary.scalar('CostCenter', center_costs)
        tf.summary.scalar('CostBox', box_costs)
        tf.summary.scalar('CostConf', conf_costs)
        tf.summary.scalar('CostSpatial', spatial_costs)
        tf.summary.scalar('CostProb', prob_costs)
        tf.summary.scalar('Costs', costs)
    with tf.name_scope('optimizer') as scope:
        optimizer = tf.train.AdamOptimizer(learning_rate=learnrate, epsilon
            =0.0001)
        grads_and_vars = optimizer.compute_gradients(costs)
        capped_gvs = [(tf.clip_by_value(grad, -5.0, 5.0), var) for grad,
            var in grads_and_vars]
        train_step = optimizer.apply_gradients(capped_gvs)
    for grad, var in capped_gvs:
        if grad is not None:
            tf.summary.histogram(var.op.name + 'capped_gradients', grad)
    with tf.name_scope('Test') as scope:
        with tf.name_scope('IOU'):
            nr_of_fingers_in_batch = tf.reduce_sum(p_label)
            y_label_index = tf.argmax(tf.reduce_max(p_label, axis=[2]), axis=1)
            x_label_index = tf.argmax(tf.reduce_max(p_label, axis=[1]), axis=1)
            y_label_global = tf.add(tf.cast(y_label_index, tf.float32) / 7,
                tf.reduce_max(y_label / 7, axis=[1, 2]))
            x_label_global = tf.add(tf.cast(x_label_index, tf.float32) / 7,
                tf.reduce_max(x_label / 7, axis=[1, 2]))
            h_label_global = tf.reduce_max(h_label, axis=[1, 2])
            w_label_global = tf.reduce_max(w_label, axis=[1, 2])
            probconf_output = tf.multiply(p_output, c1_output)
            y_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[2]), axis=1)
            x_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[1]), axis=1)
            enumerator = tf.constant(range(batchSize), tf.int64)
            probconf_indices_tensor = tf.stack([enumerator,
                y_probconf_index, x_probconf_index], 1)
            y_probconf_coarse = tf.cast(y_probconf_index, tf.float32) / 7
            y_probconf_fine = tf.squeeze(tf.gather_nd(y1_output,
                probconf_indices_tensor)) / 7
            y_probconf = tf.add(y_probconf_coarse, y_probconf_fine)
            x_probconf_coarse = tf.cast(x_probconf_index, tf.float32) / 7
            x_probconf_fine = tf.squeeze(tf.gather_nd(x1_output,
                probconf_indices_tensor)) / 7
            x_probconf = tf.add(x_probconf_coarse, x_probconf_fine)
            h_probconf = tf.squeeze(tf.gather_nd(h1_output,
                probconf_indices_tensor))
            w_probconf = tf.squeeze(tf.gather_nd(w1_output,
                probconf_indices_tensor))
            iou_max_prob_conf = hAPI.iou(x_label_global, y_label_global,
                h_label_global, w_label_global, y_probconf, x_probconf,
                h_probconf, w_probconf)
            iou_max_probconf_mean = tf.reduce_sum(iou_max_prob_conf
                ) / nr_of_fingers_in_batch
            tf.summary.scalar('iou_max_probconf', iou_max_probconf_mean)
        with tf.name_scope('distance'):
            p_labels = tf.reduce_sum(p_label, axis=[1, 2])
            x_probconfs = tf.multiply(x_probconf, p_labels)
            y_probconfs = tf.multiply(y_probconf, p_labels)
            x_labels_global = tf.multiply(x_label_global, p_labels)
            y_labels_global = tf.multiply(y_label_global, p_labels)
            x_probconf_diff = tf.subtract(x_labels_global, x_probconfs)
            y_probconf_diff = tf.subtract(y_labels_global, y_probconfs)
            distance_probconf = tf.sqrt(tf.add(tf.square(x_probconf_diff),
                tf.square(y_probconf_diff)))
            zeros_vector = tf.zeros(batchSize)
            boolean_vector = tf.squeeze(tf.not_equal(zeros_vector, p_labels))
            boolean_vector.set_shape([None])
            distance_probconfs = tf.boolean_mask(distance_probconf,
                boolean_vector)
            mean_distance_probconf, var_distance_probconf = tf.nn.moments(
                distance_probconfs, axes=[0])
            tf.summary.scalar('mean_distance_probconf', mean_distance_probconf)
            tf.summary.scalar('var_distance_probconf', var_distance_probconf)
    init_op = tf.group(tf.global_variables_initializer(), tf.
        local_variables_initializer())
    saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=4.0,
        pad_step_number=True, save_relative_paths=True)
    if not os.path.exists(origin_path + '../../../weights/' + name + '/'):
        os.makedirs(origin_path + '../../../weights/' + name + '/')
    merged_summary_op = tf.summary.merge_all()
    with tf.Session() as sess:
        sess.run(testing_init_op)
        sess.run(validation_init_op)
        sess.run(training_init_op)
        sess.run(init_op)
        if test == False:
            train_train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_traintrain')
            train_train_writer.add_graph(sess.graph)
            train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_train')
            train_writer.add_graph(sess.graph)
            valid_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_valid')
            valid_writer.add_graph(sess.graph)
            print('start training....\n')
            for i in range(nr_of_epochs / nr_of_epochs_until_save_model):
                for j in range(nr_of_epochs_until_save_model):
                    _ = sess.run([train_step], feed_dict={training: True,
                        learnrate: learning_rate})
                numbers_of_iterations_until_now = (i *
                    nr_of_epochs_until_save_model + j + 1)
                _, sumsum = sess.run([train_step, merged_summary_op],
                    feed_dict={training: True, learnrate: learning_rate})
                train_train_writer.add_summary(sumsum,
                    numbers_of_iterations_until_now)
                train_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(validation_init_op)
                valid_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(training_init_op)
                saver.save(sess=sess, save_path=origin_path +
                    '../../../weights/' + name + '/' + name + '.ckpt',
                    global_step=numbers_of_iterations_until_now)
                print('model updatet\n')
        else:
            if not os.path.exists(origin_path + 'picsRecognized/'):
                os.makedirs(origin_path + 'picsRecognized/')
            sess.run(testing_init_op)
            print('Try to restore')
            saver.restore(sess, origin_path +
                '../../../weights/139_9000Set_newPipe/139_9000Set_newPipe.ckpt-00510000'
                )
            print('Restored')
            nr_of_fingers = 0
            nr_of_coarse_recognized_fingers = 0
            nr_of_recognized_fingers = 0
            nr_of_fine_recognized_fingers = 0
            nr_of_extremefine_recognized_fingers = 0
            iou_array = []
            distance_array = []
            distance_x_array = []
            distance_y_array = []
            for i in range(len(test_picnames) / batchSize):
                (testimages, probconfs_y, probconfs_x, probconfs_h,
                    probconfs_w, labels_y, labels_x, labels_h, labels_w,
                    probconfs_iou, probconfs_distance, labels_p,
                    probconfs_x_diff, probconfs_y_diff) = (sess.run([
                    images_unnormalized, y_probconf, x_probconf, h_probconf,
                    w_probconf, y_label_global, x_label_global,
                    h_label_global, w_label_global, iou_max_prob_conf,
                    distance_probconf, p_labels, x_probconf_diff,
                    y_probconf_diff], feed_dict={training: False}))
                for b in range(batchSize):
                    print(str(i * batchSize + b))
                    testimage = testimages[b] * 200
                    probconf_y = probconfs_y[b] * 448
                    probconf_x = probconfs_x[b] * 448
                    probconf_h = probconfs_h[b] * 448
                    probconf_w = probconfs_w[b] * 448
                    probconf_iou = probconfs_iou[b]
                    probconf_distance = probconfs_distance[b]
                    probconf_x_diff = probconfs_x_diff[b]
                    probconf_y_diff = probconfs_y_diff[b]
                    label_y = labels_y[b] * 448
                    label_x = labels_x[b] * 448
                    label_h = labels_h[b] * 448
                    label_w = labels_w[b] * 448
                    print(
                        'iou                                                      = '
                         + str(probconf_iou))
                    print(
                        'distance                                                 = '
                         + str(probconf_distance))
                    if labels_p[b] != 0:
                        iou_array = np.concatenate((iou_array, [probconf_iou]))
                        distance_array = np.concatenate((distance_array, [
                            probconf_distance]))
                        distance_x_array = np.concatenate((distance_x_array,
                            [probconf_x_diff]))
                        distance_y_array = np.concatenate((distance_y_array,
                            [probconf_y_diff]))
                        nr_of_fingers += 1
                        print(
                            'mean_iou                                                 = '
                             + str(np.mean(iou_array)))
                        print(
                            'mean_distance                                            = '
                             + str(np.mean(distance_array)))
                        if probconf_distance < 0.04:
                            nr_of_coarse_recognized_fingers += 1
                        if probconf_distance < 0.02:
                            nr_of_recognized_fingers += 1
                        if probconf_distance < 0.01:
                            nr_of_fine_recognized_fingers += 1
                        if probconf_distance < 0.001:
                            nr_of_extremefine_recognized_fingers += 1
                        coarse_recognized_fingers_in_percent = float(
                            nr_of_coarse_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of coarse recognized fingers (<0.04)          = '
                             + str(coarse_recognized_fingers_in_percent) + '%')
                        recognized_fingers_in_percent = float(
                            nr_of_recognized_fingers) / float(nr_of_fingers
                            ) * 100
                        print(
                            'Percentage of recognized fingers (<0.02)                 = '
                             + str(recognized_fingers_in_percent) + '%')
                        fine_recognized_fingers_in_percent = float(
                            nr_of_fine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of fine recognized fingers (<0.01)            = '
                             + str(fine_recognized_fingers_in_percent) + '%')
                        extremefine_recognized_fingers_in_percent = float(
                            nr_of_extremefine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of extreme fine recognized fingers (>0.001)   = '
                             + str(
                            extremefine_recognized_fingers_in_percent) + '%')
                    sess.run(tf.write_file(origin_path +
                        'picsRecognized/pic' + str(batchSize * i + b) +
                        '.png', tf.image.encode_png(testimage)))
                    img = cv2.imread(origin_path + 'picsRecognized/pic' +
                        str(batchSize * i + b) + '.png')
                    cv2.rectangle(img, (int(probconf_x - probconf_w / 2),
                        int(probconf_y - probconf_h / 2)), (int(probconf_x +
                        probconf_w / 2), int(probconf_y + probconf_h / 2)),
                        (0, 0, 255), 1)
                    cv2.putText(img, 'Prediction', (int(probconf_x - 
                        probconf_w / 2), int(probconf_y - probconf_h / 2)),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'iou=' + str(probconf_iou), (0, 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'dist=' + str(probconf_distance), (0, 
                        50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.rectangle(img, (int(label_x - label_w / 2), int(
                        label_y - label_h / 2)), (int(label_x + label_w / 2
                        ), int(label_y + label_h / 2)), (255, 255, 0), 1)
                    cv2.putText(img, 'Label', (int(label_x - label_w / 2),
                        int(label_y - label_h / 2)), cv2.
                        FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 1)
                    cv2.imwrite(origin_path + 'picsRecognized/pic' + str(
                        batchSize * i + b) + '.png', img)
            plt.hist(iou_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title('IOU Probability-Density \n on Testset (' + str(
                nr_of_fingers) + 'Pictures)')
            plt.xlabel('IOU (median=' + str(round(np.median(iou_array), 3)) +
                ', mean=' + str(round(np.mean(iou_array), 3)) + ', stdev=' +
                str(round(np.std(iou_array), 3)) + ')')
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.4, 0.4], [0, 10], 'orange')
            plt.text(0.3, 9, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(0.45, 9, 'good', bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path + 'picsRecognized/IOUprobDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.02, 0.02], [0, 60], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.savefig(origin_path + 'picsRecognized/distProbDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100] (Log-Scale)')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.yscale('log')
            plt.savefig(origin_path + 'picsRecognized/logdistProbDensity.pdf')
            plt.close()
            distance_array_without_outliers = []
            for dist in distance_array:
                if dist < 0.25:
                    distance_array_without_outliers = np.concatenate((
                        distance_array_without_outliers, [dist]))
            plt.hist(distance_array_without_outliers, bins=np.arange(0.0, 
                0.25, 0.001), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) +
                'Pictures) Without Outliers(Cut at 0.25)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array_without_outliers), 3)) + ', mean=' + str(
                round(np.mean(distance_array_without_outliers), 3)) +
                ', stdev=' + str(round(np.std(
                distance_array_without_outliers), 3)) + ')')
            plt.ylabel('Probability in Promille [1/1000]')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.025, 90, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.002, 90, 'good', bbox=dict(facecolor='green', alpha
                =0.5))
            plt.savefig(origin_path +
                'picsRecognized/distProbDensity_improved.pdf')
            plt.close()
            plt.hist(distance_x_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('X-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('x-Distance (median=' + str(round(np.median(
                distance_x_array), 3)) + ', mean=' + str(round(np.mean(
                distance_x_array), 3)) + ', stdev=' + str(round(np.std(
                distance_x_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/xDistance.pdf')
            plt.close()
            plt.hist(distance_y_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('Y-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('y-Distance (median=' + str(round(np.median(
                distance_y_array), 3)) + ', mean=' + str(round(np.mean(
                distance_y_array), 3)) + ', stdev=' + str(round(np.std(
                distance_y_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/yDistance.pdf')
            plt.close()
    print('finished')


<mask token>

<mask token>
sys.path.insert(0, this_folder +
    '/../dataPreprocessing/OnTabeasData/Step4CollectTogether/')
<mask token>
sys.path.insert(0, this_folder + '/../helperfunctions/')
<mask token>
print(name)
<mask token>


def traindata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'trainDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def validdata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'validDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def main():
    print('TensorFlow version ', tf.__version__)
    with tf.name_scope('Data') as scope:
        print('read in all Train Picture-Names & Labels and shuffle them')
        ReadData = analyse_Fingerset.Dataset_Heinz()
        train_data = ReadData.get_train_data(origin_path=origin_path)
        train_picnames = [row[0] for row in train_data]
        train_labels = np.float32([row[1] for row in train_data])
        train_data = Dataset.from_tensor_slices((train_picnames, train_labels))
        train_data = train_data.repeat()
        train_data = train_data.shuffle(buffer_size=buffer_size)
        train_data = train_data.map(map_func=traindata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        train_data = train_data.batch(batchSize)
        print('read in all Valid Picture-Names & Labels and shuffle them')
        valid_data = ReadData.get_valid_data(origin_path=origin_path)
        valid_picnames = [row[0] for row in valid_data]
        valid_labels = np.float32([row[1] for row in valid_data])
        valid_data = Dataset.from_tensor_slices((valid_picnames, valid_labels))
        valid_data = valid_data.repeat()
        valid_data = valid_data.shuffle(buffer_size=buffer_size)
        valid_data = valid_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        valid_data = valid_data.batch(batchSize)
        print('read in all Test Picture-Names & Labels and shuffle them')
        test_data = ReadData.get_valid_data(origin_path=origin_path)
        test_picnames = [row[0] for row in test_data]
        test_labels = np.float32([row[1] for row in test_data])
        test_data = Dataset.from_tensor_slices((test_picnames, test_labels))
        test_data = test_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        test_data = test_data.batch(batchSize)
    print('finished read all Pictures and Labels, start Data-iterator')
    with tf.name_scope('Data-Iterator') as scope:
        iterator = Iterator.from_structure(train_data.output_types,
            train_data.output_shapes)
        images_unnormalized, labels = iterator.get_next()
        training_init_op = iterator.make_initializer(train_data)
        validation_init_op = iterator.make_initializer(valid_data)
        testing_init_op = iterator.make_initializer(test_data)
        images_unnormalized = tf.image.resize_images(images_unnormalized, [
            448, 448])
        images_unnormalized = tf.cast(images_unnormalized, tf.float32)
        tf.summary.image('images_after_crop', tensor=images_unnormalized,
            max_outputs=20)
    training = tf.placeholder(tf.bool, name='training')
    learnrate = tf.placeholder(tf.float32, name='learnrate')
    with tf.name_scope('normalize_pictures') as scope:
        images = hAPI.normalize_pictures(tensor=images_unnormalized)
    output_1 = hAPI.convLayerPretrained(tensor=images, layerNr=1, batchSize
        =batchSize, filterwidth=7, inputdepth=1, outputdepth=64, strides=2,
        batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('2_Maxpool_Layer') as scope:
        output_2 = tf.nn.max_pool(output_1, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_2')
    output_3 = hAPI.convLayerPretrained(tensor=output_2, layerNr=3,
        batchSize=batchSize, filterwidth=3, inputdepth=64, outputdepth=192,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('4_Maxpool_Layer') as scope:
        output_4 = tf.nn.max_pool(output_3, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_4')
    output_5 = hAPI.convLayerPretrained(tensor=output_4, layerNr=5,
        batchSize=batchSize, filterwidth=1, inputdepth=192, outputdepth=128,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_6 = hAPI.convLayerPretrained(tensor=output_5, layerNr=6,
        batchSize=batchSize, filterwidth=3, inputdepth=128, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_7 = hAPI.convLayerPretrained(tensor=output_6, layerNr=7,
        batchSize=batchSize, filterwidth=1, inputdepth=256, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_8 = hAPI.convLayerPretrained(tensor=output_7, layerNr=8,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('9_Maxpool_Layer') as scope:
        output_9 = tf.nn.max_pool(output_8, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_9')
    output_10 = hAPI.convLayerPretrained(tensor=output_9, layerNr=10,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_11 = hAPI.convLayerPretrained(tensor=output_10, layerNr=11,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_12 = hAPI.convLayerPretrained(tensor=output_11, layerNr=12,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_13 = hAPI.convLayerPretrained(tensor=output_12, layerNr=13,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_14 = hAPI.convLayerPretrained(tensor=output_13, layerNr=14,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_15 = hAPI.convLayerPretrained(tensor=output_14, layerNr=15,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_16 = hAPI.convLayerPretrained(tensor=output_15, layerNr=16,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_17 = hAPI.convLayerPretrained(tensor=output_16, layerNr=17,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_18 = hAPI.convLayerPretrained(tensor=output_17, layerNr=18,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_19 = hAPI.convLayerPretrained(tensor=output_18, layerNr=19,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    with tf.name_scope('20_Maxpool_Layer') as scope:
        output_20 = tf.nn.max_pool(output_19, ksize=[1, 2, 2, 1], strides=[
            1, 2, 2, 1], padding='SAME', name='maxpool_20')
    output_21 = hAPI.convLayerPretrained(tensor=output_20, layerNr=21,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_22 = hAPI.convLayerPretrained(tensor=output_21, layerNr=22,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_23 = hAPI.convLayerPretrained(tensor=output_22, layerNr=23,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_24 = hAPI.convLayerPretrained(tensor=output_23, layerNr=24,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_25 = hAPI.convLayer(tensor=output_24, layerNr=25, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_26 = hAPI.convLayer(tensor=output_25, layerNr=26, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=2, batchnorm_=batchnorm, dropout_=False, training=training)
    output_27 = hAPI.convLayer(tensor=output_26, layerNr=27, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_28 = hAPI.convLayer(tensor=output_27, layerNr=28, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    with tf.name_scope('Layer31_full') as scope:
        input_31 = tf.reshape(tensor=output_28, shape=[batchSize, 7 * 7 * 1024]
            )
        if dropout == True:
            with tf.name_scope('dropout'):
                input_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    input_31, keep_prob=0.8, noise_shape=[batchSize, 7 * 7 *
                    1024]), lambda : input_31)
        W31 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 1024, 4096],
            stddev=0.01, dtype=tf.float32), name='W31')
        b31 = tf.Variable(tf.truncated_normal(shape=[4096], stddev=0.01,
            dtype=tf.float32), name='b31')
        preactivate_31 = tf.add(tf.matmul(input_31, W31), b31)
        with tf.name_scope('batch_norm'):
            input_depth_31 = preactivate_31.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta31 = tf.Variable(tf.constant(0.0, shape=[input_depth_31
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma31 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_31], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean31, batch_variance31 = tf.nn.moments(x=preactivate_31,
                axes=[0, 1])
            preactivate_31 = tf.nn.batch_normalization(x=preactivate_31,
                mean=batch_mean31, variance=batch_variance31, offset=beta31,
                scale=gamma31, variance_epsilon=0.0001, name=None)
        output_31 = tf.nn.relu(preactivate_31)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W31, name='W31')
            hAPI.variable_summaries(variable=b31, name='b31')
            hAPI.variable_summaries(variable=preactivate_31, name=
                'preactivate31')
            hAPI.variable_summaries(variable=output_31, name='output31')
    with tf.name_scope('Layer32_full') as scope:
        if dropout == True:
            with tf.name_scope('dropout'):
                output_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    output_31, keep_prob=0.8, noise_shape=[batchSize, 4096]
                    ), lambda : output_31)
        W32 = tf.Variable(tf.truncated_normal(shape=[4096, 7 * 7 * 6],
            stddev=0.01, dtype=tf.float32), name='W32')
        b32 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 6], stddev=
            0.01, dtype=tf.float32), name='b32')
        preactivate_32 = tf.add(tf.matmul(output_31, W32), b32)
        with tf.name_scope('batch_norm'):
            input_depth_32 = preactivate_32.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta32 = tf.Variable(tf.constant(0.0, shape=[input_depth_32
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma32 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_32], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean32, batch_variance32 = tf.nn.moments(x=preactivate_32,
                axes=[0, 1])
            preactivate_32 = tf.nn.batch_normalization(x=preactivate_32,
                mean=batch_mean32, variance=batch_variance32, offset=beta32,
                scale=gamma32, variance_epsilon=0.0001, name=None)
        fully_32 = preactivate_32
        output_32 = tf.sigmoid(tf.reshape(tensor=fully_32, shape=[batchSize,
            7, 7, 6]))
        output_32 = tf.cast(output_32, tf.float32)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W32, name='W32')
            hAPI.variable_summaries(variable=b32, name='b32')
            hAPI.variable_summaries(variable=preactivate_32, name=
                'preactivate32')
            hAPI.variable_summaries(variable=output_32, name='output32')
    with tf.name_scope('cost_function') as scope:
        x1_output = tf.squeeze(output_32[:, :, :, 0])
        x_label = tf.squeeze(labels[:, :, :, 0])
        y1_output = tf.squeeze(output_32[:, :, :, 1])
        y_label = tf.squeeze(labels[:, :, :, 1])
        h1_output = tf.squeeze(output_32[:, :, :, 2])
        h_label = tf.squeeze(labels[:, :, :, 2])
        w1_output = tf.squeeze(output_32[:, :, :, 3])
        w_label = tf.squeeze(labels[:, :, :, 3])
        c1_output = tf.squeeze(output_32[:, :, :, 4])
        p_output = tf.squeeze(output_32[:, :, :, 5])
        p_label = tf.squeeze(labels[:, :, :, 4])
        with tf.name_scope('x-costs'):
            x1_difference = tf.subtract(x1_output, x_label)
            x1_squared = tf.square(x1_difference)
            x1_costs = tf.multiply(p_label, x1_squared)
            x1_costs = tf.reduce_sum(x1_costs)
            x1_costs = tf.multiply(x1_costs, 5)
        with tf.name_scope('y-costs'):
            y1_difference = tf.subtract(y1_output, y_label)
            y1_squared = tf.square(y1_difference)
            y1_costs = tf.multiply(p_label, y1_squared)
            y1_costs = tf.reduce_sum(y1_costs)
            y1_costs = tf.multiply(y1_costs, 5)
        with tf.name_scope('h-costs'):
            h1_output_root = tf.sqrt(h1_output)
            h_label_root = tf.sqrt(h_label)
            h1_difference = tf.subtract(h1_output_root, h_label_root)
            h1_squared = tf.square(h1_difference)
            h1_costs = tf.multiply(p_label, h1_squared)
            h1_costs = tf.reduce_sum(h1_costs)
            h1_costs = tf.multiply(h1_costs, 5)
        with tf.name_scope('w-costs'):
            w1_output_root = tf.sqrt(w1_output)
            w_label_root = tf.sqrt(w_label)
            w1_difference = tf.subtract(w1_output_root, w_label_root)
            w1_squared = tf.square(w1_difference)
            w1_costs = tf.multiply(p_label, w1_squared)
            w1_costs = tf.reduce_sum(w1_costs)
            w1_costs = tf.multiply(w1_costs, 5)
        with tf.name_scope('C-costs'):
            w_label_half = tf.div(w_label, 2)
            h_label_half = tf.div(h_label, 2)
            w1_output_half = tf.div(w1_output, 2)
            h1_output_half = tf.div(h1_output, 2)
            label_left = tf.subtract(x_label, w_label_half)
            label_right = tf.add(x_label, w_label_half)
            label_top = tf.subtract(y_label, h_label_half)
            label_bottom = tf.add(y_label, h_label_half)
            output1_left = tf.subtract(x1_output, w1_output_half)
            output1_right = tf.add(x1_output, w1_output_half)
            output1_top = tf.subtract(y1_output, h1_output_half)
            output1_bottom = tf.add(y1_output, h1_output_half)
            overlap1_left = tf.maximum(label_left, output1_left)
            overlap1_right = tf.minimum(label_right, output1_right)
            overlap1_top = tf.maximum(label_top, output1_top)
            overlap1_bottom = tf.minimum(label_bottom, output1_bottom)
            overlap1_width = tf.subtract(overlap1_right, overlap1_left)
            overlap1_width = tf.maximum(np.float32(0), overlap1_width)
            overlap1_height = tf.subtract(overlap1_bottom, overlap1_top)
            overlap1_height = tf.maximum(np.float32(0), overlap1_height)
            area_of_overlap1 = tf.multiply(overlap1_width, overlap1_height)
            output1_area = tf.multiply(h1_output, w1_output)
            label_area = tf.multiply(h_label, w_label)
            area_of_union1 = tf.subtract(tf.add(output1_area, label_area),
                area_of_overlap1)
            IoU1 = tf.div(area_of_overlap1, area_of_union1)
            IoU1_scalar = tf.reduce_mean(IoU1)
            c1_label = tf.multiply(IoU1, p_label)
            c1_difference = tf.subtract(c1_output, c1_label)
            c1_squared = tf.square(c1_difference)
            with tf.name_scope('obj_present'):
                c1_obj = tf.multiply(c1_squared, p_label)
                c1_obj_costs = tf.reduce_sum(c1_obj)
            with tf.name_scope('noobj_present'):
                p_label_invers = tf.subtract(np.float32(1), p_label)
                c1_noobj = tf.multiply(c1_squared, p_label_invers)
                c1_noobj_costs = tf.reduce_sum(c1_noobj)
                c1_noobj_costs = tf.multiply(c1_noobj_costs, 0.5)
        with tf.name_scope('p_costs'):
            p_difference = tf.subtract(p_output, p_label)
            p_squared = tf.square(p_difference)
            p_costs = tf.multiply(p_label, p_squared)
            p_costs = tf.reduce_sum(p_costs)
        center_costs = tf.add(x1_costs, y1_costs)
        box_costs = tf.add(h1_costs, w1_costs)
        conf_costs = tf.add(c1_obj_costs, c1_noobj_costs)
        spatial_costs = tf.add(center_costs, box_costs)
        prob_costs = tf.add(conf_costs, p_costs)
        costs = tf.add(spatial_costs, prob_costs)
        tf.summary.scalar('Costx', x1_costs)
        tf.summary.scalar('Costy', y1_costs)
        tf.summary.scalar('Costh', h1_costs)
        tf.summary.scalar('Costw', w1_costs)
        tf.summary.scalar('IoU_mean', IoU1_scalar)
        tf.summary.scalar('CostcObj', c1_obj_costs)
        tf.summary.scalar('CostcNoObj', c1_noobj_costs)
        tf.summary.scalar('Costp', p_costs)
        tf.summary.scalar('CostCenter', center_costs)
        tf.summary.scalar('CostBox', box_costs)
        tf.summary.scalar('CostConf', conf_costs)
        tf.summary.scalar('CostSpatial', spatial_costs)
        tf.summary.scalar('CostProb', prob_costs)
        tf.summary.scalar('Costs', costs)
    with tf.name_scope('optimizer') as scope:
        optimizer = tf.train.AdamOptimizer(learning_rate=learnrate, epsilon
            =0.0001)
        grads_and_vars = optimizer.compute_gradients(costs)
        capped_gvs = [(tf.clip_by_value(grad, -5.0, 5.0), var) for grad,
            var in grads_and_vars]
        train_step = optimizer.apply_gradients(capped_gvs)
    for grad, var in capped_gvs:
        if grad is not None:
            tf.summary.histogram(var.op.name + 'capped_gradients', grad)
    with tf.name_scope('Test') as scope:
        with tf.name_scope('IOU'):
            nr_of_fingers_in_batch = tf.reduce_sum(p_label)
            y_label_index = tf.argmax(tf.reduce_max(p_label, axis=[2]), axis=1)
            x_label_index = tf.argmax(tf.reduce_max(p_label, axis=[1]), axis=1)
            y_label_global = tf.add(tf.cast(y_label_index, tf.float32) / 7,
                tf.reduce_max(y_label / 7, axis=[1, 2]))
            x_label_global = tf.add(tf.cast(x_label_index, tf.float32) / 7,
                tf.reduce_max(x_label / 7, axis=[1, 2]))
            h_label_global = tf.reduce_max(h_label, axis=[1, 2])
            w_label_global = tf.reduce_max(w_label, axis=[1, 2])
            probconf_output = tf.multiply(p_output, c1_output)
            y_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[2]), axis=1)
            x_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[1]), axis=1)
            enumerator = tf.constant(range(batchSize), tf.int64)
            probconf_indices_tensor = tf.stack([enumerator,
                y_probconf_index, x_probconf_index], 1)
            y_probconf_coarse = tf.cast(y_probconf_index, tf.float32) / 7
            y_probconf_fine = tf.squeeze(tf.gather_nd(y1_output,
                probconf_indices_tensor)) / 7
            y_probconf = tf.add(y_probconf_coarse, y_probconf_fine)
            x_probconf_coarse = tf.cast(x_probconf_index, tf.float32) / 7
            x_probconf_fine = tf.squeeze(tf.gather_nd(x1_output,
                probconf_indices_tensor)) / 7
            x_probconf = tf.add(x_probconf_coarse, x_probconf_fine)
            h_probconf = tf.squeeze(tf.gather_nd(h1_output,
                probconf_indices_tensor))
            w_probconf = tf.squeeze(tf.gather_nd(w1_output,
                probconf_indices_tensor))
            iou_max_prob_conf = hAPI.iou(x_label_global, y_label_global,
                h_label_global, w_label_global, y_probconf, x_probconf,
                h_probconf, w_probconf)
            iou_max_probconf_mean = tf.reduce_sum(iou_max_prob_conf
                ) / nr_of_fingers_in_batch
            tf.summary.scalar('iou_max_probconf', iou_max_probconf_mean)
        with tf.name_scope('distance'):
            p_labels = tf.reduce_sum(p_label, axis=[1, 2])
            x_probconfs = tf.multiply(x_probconf, p_labels)
            y_probconfs = tf.multiply(y_probconf, p_labels)
            x_labels_global = tf.multiply(x_label_global, p_labels)
            y_labels_global = tf.multiply(y_label_global, p_labels)
            x_probconf_diff = tf.subtract(x_labels_global, x_probconfs)
            y_probconf_diff = tf.subtract(y_labels_global, y_probconfs)
            distance_probconf = tf.sqrt(tf.add(tf.square(x_probconf_diff),
                tf.square(y_probconf_diff)))
            zeros_vector = tf.zeros(batchSize)
            boolean_vector = tf.squeeze(tf.not_equal(zeros_vector, p_labels))
            boolean_vector.set_shape([None])
            distance_probconfs = tf.boolean_mask(distance_probconf,
                boolean_vector)
            mean_distance_probconf, var_distance_probconf = tf.nn.moments(
                distance_probconfs, axes=[0])
            tf.summary.scalar('mean_distance_probconf', mean_distance_probconf)
            tf.summary.scalar('var_distance_probconf', var_distance_probconf)
    init_op = tf.group(tf.global_variables_initializer(), tf.
        local_variables_initializer())
    saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=4.0,
        pad_step_number=True, save_relative_paths=True)
    if not os.path.exists(origin_path + '../../../weights/' + name + '/'):
        os.makedirs(origin_path + '../../../weights/' + name + '/')
    merged_summary_op = tf.summary.merge_all()
    with tf.Session() as sess:
        sess.run(testing_init_op)
        sess.run(validation_init_op)
        sess.run(training_init_op)
        sess.run(init_op)
        if test == False:
            train_train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_traintrain')
            train_train_writer.add_graph(sess.graph)
            train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_train')
            train_writer.add_graph(sess.graph)
            valid_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_valid')
            valid_writer.add_graph(sess.graph)
            print('start training....\n')
            for i in range(nr_of_epochs / nr_of_epochs_until_save_model):
                for j in range(nr_of_epochs_until_save_model):
                    _ = sess.run([train_step], feed_dict={training: True,
                        learnrate: learning_rate})
                numbers_of_iterations_until_now = (i *
                    nr_of_epochs_until_save_model + j + 1)
                _, sumsum = sess.run([train_step, merged_summary_op],
                    feed_dict={training: True, learnrate: learning_rate})
                train_train_writer.add_summary(sumsum,
                    numbers_of_iterations_until_now)
                train_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(validation_init_op)
                valid_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(training_init_op)
                saver.save(sess=sess, save_path=origin_path +
                    '../../../weights/' + name + '/' + name + '.ckpt',
                    global_step=numbers_of_iterations_until_now)
                print('model updatet\n')
        else:
            if not os.path.exists(origin_path + 'picsRecognized/'):
                os.makedirs(origin_path + 'picsRecognized/')
            sess.run(testing_init_op)
            print('Try to restore')
            saver.restore(sess, origin_path +
                '../../../weights/139_9000Set_newPipe/139_9000Set_newPipe.ckpt-00510000'
                )
            print('Restored')
            nr_of_fingers = 0
            nr_of_coarse_recognized_fingers = 0
            nr_of_recognized_fingers = 0
            nr_of_fine_recognized_fingers = 0
            nr_of_extremefine_recognized_fingers = 0
            iou_array = []
            distance_array = []
            distance_x_array = []
            distance_y_array = []
            for i in range(len(test_picnames) / batchSize):
                (testimages, probconfs_y, probconfs_x, probconfs_h,
                    probconfs_w, labels_y, labels_x, labels_h, labels_w,
                    probconfs_iou, probconfs_distance, labels_p,
                    probconfs_x_diff, probconfs_y_diff) = (sess.run([
                    images_unnormalized, y_probconf, x_probconf, h_probconf,
                    w_probconf, y_label_global, x_label_global,
                    h_label_global, w_label_global, iou_max_prob_conf,
                    distance_probconf, p_labels, x_probconf_diff,
                    y_probconf_diff], feed_dict={training: False}))
                for b in range(batchSize):
                    print(str(i * batchSize + b))
                    testimage = testimages[b] * 200
                    probconf_y = probconfs_y[b] * 448
                    probconf_x = probconfs_x[b] * 448
                    probconf_h = probconfs_h[b] * 448
                    probconf_w = probconfs_w[b] * 448
                    probconf_iou = probconfs_iou[b]
                    probconf_distance = probconfs_distance[b]
                    probconf_x_diff = probconfs_x_diff[b]
                    probconf_y_diff = probconfs_y_diff[b]
                    label_y = labels_y[b] * 448
                    label_x = labels_x[b] * 448
                    label_h = labels_h[b] * 448
                    label_w = labels_w[b] * 448
                    print(
                        'iou                                                      = '
                         + str(probconf_iou))
                    print(
                        'distance                                                 = '
                         + str(probconf_distance))
                    if labels_p[b] != 0:
                        iou_array = np.concatenate((iou_array, [probconf_iou]))
                        distance_array = np.concatenate((distance_array, [
                            probconf_distance]))
                        distance_x_array = np.concatenate((distance_x_array,
                            [probconf_x_diff]))
                        distance_y_array = np.concatenate((distance_y_array,
                            [probconf_y_diff]))
                        nr_of_fingers += 1
                        print(
                            'mean_iou                                                 = '
                             + str(np.mean(iou_array)))
                        print(
                            'mean_distance                                            = '
                             + str(np.mean(distance_array)))
                        if probconf_distance < 0.04:
                            nr_of_coarse_recognized_fingers += 1
                        if probconf_distance < 0.02:
                            nr_of_recognized_fingers += 1
                        if probconf_distance < 0.01:
                            nr_of_fine_recognized_fingers += 1
                        if probconf_distance < 0.001:
                            nr_of_extremefine_recognized_fingers += 1
                        coarse_recognized_fingers_in_percent = float(
                            nr_of_coarse_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of coarse recognized fingers (<0.04)          = '
                             + str(coarse_recognized_fingers_in_percent) + '%')
                        recognized_fingers_in_percent = float(
                            nr_of_recognized_fingers) / float(nr_of_fingers
                            ) * 100
                        print(
                            'Percentage of recognized fingers (<0.02)                 = '
                             + str(recognized_fingers_in_percent) + '%')
                        fine_recognized_fingers_in_percent = float(
                            nr_of_fine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of fine recognized fingers (<0.01)            = '
                             + str(fine_recognized_fingers_in_percent) + '%')
                        extremefine_recognized_fingers_in_percent = float(
                            nr_of_extremefine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of extreme fine recognized fingers (>0.001)   = '
                             + str(
                            extremefine_recognized_fingers_in_percent) + '%')
                    sess.run(tf.write_file(origin_path +
                        'picsRecognized/pic' + str(batchSize * i + b) +
                        '.png', tf.image.encode_png(testimage)))
                    img = cv2.imread(origin_path + 'picsRecognized/pic' +
                        str(batchSize * i + b) + '.png')
                    cv2.rectangle(img, (int(probconf_x - probconf_w / 2),
                        int(probconf_y - probconf_h / 2)), (int(probconf_x +
                        probconf_w / 2), int(probconf_y + probconf_h / 2)),
                        (0, 0, 255), 1)
                    cv2.putText(img, 'Prediction', (int(probconf_x - 
                        probconf_w / 2), int(probconf_y - probconf_h / 2)),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'iou=' + str(probconf_iou), (0, 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'dist=' + str(probconf_distance), (0, 
                        50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.rectangle(img, (int(label_x - label_w / 2), int(
                        label_y - label_h / 2)), (int(label_x + label_w / 2
                        ), int(label_y + label_h / 2)), (255, 255, 0), 1)
                    cv2.putText(img, 'Label', (int(label_x - label_w / 2),
                        int(label_y - label_h / 2)), cv2.
                        FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 1)
                    cv2.imwrite(origin_path + 'picsRecognized/pic' + str(
                        batchSize * i + b) + '.png', img)
            plt.hist(iou_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title('IOU Probability-Density \n on Testset (' + str(
                nr_of_fingers) + 'Pictures)')
            plt.xlabel('IOU (median=' + str(round(np.median(iou_array), 3)) +
                ', mean=' + str(round(np.mean(iou_array), 3)) + ', stdev=' +
                str(round(np.std(iou_array), 3)) + ')')
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.4, 0.4], [0, 10], 'orange')
            plt.text(0.3, 9, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(0.45, 9, 'good', bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path + 'picsRecognized/IOUprobDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.02, 0.02], [0, 60], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.savefig(origin_path + 'picsRecognized/distProbDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100] (Log-Scale)')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.yscale('log')
            plt.savefig(origin_path + 'picsRecognized/logdistProbDensity.pdf')
            plt.close()
            distance_array_without_outliers = []
            for dist in distance_array:
                if dist < 0.25:
                    distance_array_without_outliers = np.concatenate((
                        distance_array_without_outliers, [dist]))
            plt.hist(distance_array_without_outliers, bins=np.arange(0.0, 
                0.25, 0.001), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) +
                'Pictures) Without Outliers(Cut at 0.25)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array_without_outliers), 3)) + ', mean=' + str(
                round(np.mean(distance_array_without_outliers), 3)) +
                ', stdev=' + str(round(np.std(
                distance_array_without_outliers), 3)) + ')')
            plt.ylabel('Probability in Promille [1/1000]')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.025, 90, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.002, 90, 'good', bbox=dict(facecolor='green', alpha
                =0.5))
            plt.savefig(origin_path +
                'picsRecognized/distProbDensity_improved.pdf')
            plt.close()
            plt.hist(distance_x_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('X-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('x-Distance (median=' + str(round(np.median(
                distance_x_array), 3)) + ', mean=' + str(round(np.mean(
                distance_x_array), 3)) + ', stdev=' + str(round(np.std(
                distance_x_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/xDistance.pdf')
            plt.close()
            plt.hist(distance_y_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('Y-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('y-Distance (median=' + str(round(np.median(
                distance_y_array), 3)) + ', mean=' + str(round(np.mean(
                distance_y_array), 3)) + ', stdev=' + str(round(np.std(
                distance_y_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/yDistance.pdf')
            plt.close()
    print('finished')


if __name__ == '__main__':
    main()

<mask token>
this_folder = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, this_folder +
    '/../dataPreprocessing/OnTabeasData/Step4CollectTogether/')
<mask token>
sys.path.insert(0, this_folder + '/../helperfunctions/')
<mask token>
parser_object = pC.make_parser()
name = parser_object.modelname
print(name)
batchSize = parser_object.batch_Size
learning_rate = parser_object.learning_rate
num_threads = parser_object.num_Threads
buffer_size = parser_object.buffer_Size
origin_path = parser_object.origin_Path
nr_of_epochs = parser_object.nr_of_epochs
nr_of_epochs_until_save_model = parser_object.nr_of_epochs_until_save_model
dropout = parser_object.dropout_bool
batchnorm = parser_object.batchnorm_bool
test = parser_object.test_bool


def traindata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'trainDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def validdata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'validDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def main():
    print('TensorFlow version ', tf.__version__)
    with tf.name_scope('Data') as scope:
        print('read in all Train Picture-Names & Labels and shuffle them')
        ReadData = analyse_Fingerset.Dataset_Heinz()
        train_data = ReadData.get_train_data(origin_path=origin_path)
        train_picnames = [row[0] for row in train_data]
        train_labels = np.float32([row[1] for row in train_data])
        train_data = Dataset.from_tensor_slices((train_picnames, train_labels))
        train_data = train_data.repeat()
        train_data = train_data.shuffle(buffer_size=buffer_size)
        train_data = train_data.map(map_func=traindata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        train_data = train_data.batch(batchSize)
        print('read in all Valid Picture-Names & Labels and shuffle them')
        valid_data = ReadData.get_valid_data(origin_path=origin_path)
        valid_picnames = [row[0] for row in valid_data]
        valid_labels = np.float32([row[1] for row in valid_data])
        valid_data = Dataset.from_tensor_slices((valid_picnames, valid_labels))
        valid_data = valid_data.repeat()
        valid_data = valid_data.shuffle(buffer_size=buffer_size)
        valid_data = valid_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        valid_data = valid_data.batch(batchSize)
        print('read in all Test Picture-Names & Labels and shuffle them')
        test_data = ReadData.get_valid_data(origin_path=origin_path)
        test_picnames = [row[0] for row in test_data]
        test_labels = np.float32([row[1] for row in test_data])
        test_data = Dataset.from_tensor_slices((test_picnames, test_labels))
        test_data = test_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        test_data = test_data.batch(batchSize)
    print('finished read all Pictures and Labels, start Data-iterator')
    with tf.name_scope('Data-Iterator') as scope:
        iterator = Iterator.from_structure(train_data.output_types,
            train_data.output_shapes)
        images_unnormalized, labels = iterator.get_next()
        training_init_op = iterator.make_initializer(train_data)
        validation_init_op = iterator.make_initializer(valid_data)
        testing_init_op = iterator.make_initializer(test_data)
        images_unnormalized = tf.image.resize_images(images_unnormalized, [
            448, 448])
        images_unnormalized = tf.cast(images_unnormalized, tf.float32)
        tf.summary.image('images_after_crop', tensor=images_unnormalized,
            max_outputs=20)
    training = tf.placeholder(tf.bool, name='training')
    learnrate = tf.placeholder(tf.float32, name='learnrate')
    with tf.name_scope('normalize_pictures') as scope:
        images = hAPI.normalize_pictures(tensor=images_unnormalized)
    output_1 = hAPI.convLayerPretrained(tensor=images, layerNr=1, batchSize
        =batchSize, filterwidth=7, inputdepth=1, outputdepth=64, strides=2,
        batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('2_Maxpool_Layer') as scope:
        output_2 = tf.nn.max_pool(output_1, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_2')
    output_3 = hAPI.convLayerPretrained(tensor=output_2, layerNr=3,
        batchSize=batchSize, filterwidth=3, inputdepth=64, outputdepth=192,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('4_Maxpool_Layer') as scope:
        output_4 = tf.nn.max_pool(output_3, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_4')
    output_5 = hAPI.convLayerPretrained(tensor=output_4, layerNr=5,
        batchSize=batchSize, filterwidth=1, inputdepth=192, outputdepth=128,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_6 = hAPI.convLayerPretrained(tensor=output_5, layerNr=6,
        batchSize=batchSize, filterwidth=3, inputdepth=128, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_7 = hAPI.convLayerPretrained(tensor=output_6, layerNr=7,
        batchSize=batchSize, filterwidth=1, inputdepth=256, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_8 = hAPI.convLayerPretrained(tensor=output_7, layerNr=8,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('9_Maxpool_Layer') as scope:
        output_9 = tf.nn.max_pool(output_8, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_9')
    output_10 = hAPI.convLayerPretrained(tensor=output_9, layerNr=10,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_11 = hAPI.convLayerPretrained(tensor=output_10, layerNr=11,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_12 = hAPI.convLayerPretrained(tensor=output_11, layerNr=12,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_13 = hAPI.convLayerPretrained(tensor=output_12, layerNr=13,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_14 = hAPI.convLayerPretrained(tensor=output_13, layerNr=14,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_15 = hAPI.convLayerPretrained(tensor=output_14, layerNr=15,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_16 = hAPI.convLayerPretrained(tensor=output_15, layerNr=16,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_17 = hAPI.convLayerPretrained(tensor=output_16, layerNr=17,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_18 = hAPI.convLayerPretrained(tensor=output_17, layerNr=18,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_19 = hAPI.convLayerPretrained(tensor=output_18, layerNr=19,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    with tf.name_scope('20_Maxpool_Layer') as scope:
        output_20 = tf.nn.max_pool(output_19, ksize=[1, 2, 2, 1], strides=[
            1, 2, 2, 1], padding='SAME', name='maxpool_20')
    output_21 = hAPI.convLayerPretrained(tensor=output_20, layerNr=21,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_22 = hAPI.convLayerPretrained(tensor=output_21, layerNr=22,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_23 = hAPI.convLayerPretrained(tensor=output_22, layerNr=23,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_24 = hAPI.convLayerPretrained(tensor=output_23, layerNr=24,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_25 = hAPI.convLayer(tensor=output_24, layerNr=25, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_26 = hAPI.convLayer(tensor=output_25, layerNr=26, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=2, batchnorm_=batchnorm, dropout_=False, training=training)
    output_27 = hAPI.convLayer(tensor=output_26, layerNr=27, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_28 = hAPI.convLayer(tensor=output_27, layerNr=28, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    with tf.name_scope('Layer31_full') as scope:
        input_31 = tf.reshape(tensor=output_28, shape=[batchSize, 7 * 7 * 1024]
            )
        if dropout == True:
            with tf.name_scope('dropout'):
                input_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    input_31, keep_prob=0.8, noise_shape=[batchSize, 7 * 7 *
                    1024]), lambda : input_31)
        W31 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 1024, 4096],
            stddev=0.01, dtype=tf.float32), name='W31')
        b31 = tf.Variable(tf.truncated_normal(shape=[4096], stddev=0.01,
            dtype=tf.float32), name='b31')
        preactivate_31 = tf.add(tf.matmul(input_31, W31), b31)
        with tf.name_scope('batch_norm'):
            input_depth_31 = preactivate_31.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta31 = tf.Variable(tf.constant(0.0, shape=[input_depth_31
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma31 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_31], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean31, batch_variance31 = tf.nn.moments(x=preactivate_31,
                axes=[0, 1])
            preactivate_31 = tf.nn.batch_normalization(x=preactivate_31,
                mean=batch_mean31, variance=batch_variance31, offset=beta31,
                scale=gamma31, variance_epsilon=0.0001, name=None)
        output_31 = tf.nn.relu(preactivate_31)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W31, name='W31')
            hAPI.variable_summaries(variable=b31, name='b31')
            hAPI.variable_summaries(variable=preactivate_31, name=
                'preactivate31')
            hAPI.variable_summaries(variable=output_31, name='output31')
    with tf.name_scope('Layer32_full') as scope:
        if dropout == True:
            with tf.name_scope('dropout'):
                output_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    output_31, keep_prob=0.8, noise_shape=[batchSize, 4096]
                    ), lambda : output_31)
        W32 = tf.Variable(tf.truncated_normal(shape=[4096, 7 * 7 * 6],
            stddev=0.01, dtype=tf.float32), name='W32')
        b32 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 6], stddev=
            0.01, dtype=tf.float32), name='b32')
        preactivate_32 = tf.add(tf.matmul(output_31, W32), b32)
        with tf.name_scope('batch_norm'):
            input_depth_32 = preactivate_32.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta32 = tf.Variable(tf.constant(0.0, shape=[input_depth_32
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma32 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_32], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean32, batch_variance32 = tf.nn.moments(x=preactivate_32,
                axes=[0, 1])
            preactivate_32 = tf.nn.batch_normalization(x=preactivate_32,
                mean=batch_mean32, variance=batch_variance32, offset=beta32,
                scale=gamma32, variance_epsilon=0.0001, name=None)
        fully_32 = preactivate_32
        output_32 = tf.sigmoid(tf.reshape(tensor=fully_32, shape=[batchSize,
            7, 7, 6]))
        output_32 = tf.cast(output_32, tf.float32)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W32, name='W32')
            hAPI.variable_summaries(variable=b32, name='b32')
            hAPI.variable_summaries(variable=preactivate_32, name=
                'preactivate32')
            hAPI.variable_summaries(variable=output_32, name='output32')
    with tf.name_scope('cost_function') as scope:
        x1_output = tf.squeeze(output_32[:, :, :, 0])
        x_label = tf.squeeze(labels[:, :, :, 0])
        y1_output = tf.squeeze(output_32[:, :, :, 1])
        y_label = tf.squeeze(labels[:, :, :, 1])
        h1_output = tf.squeeze(output_32[:, :, :, 2])
        h_label = tf.squeeze(labels[:, :, :, 2])
        w1_output = tf.squeeze(output_32[:, :, :, 3])
        w_label = tf.squeeze(labels[:, :, :, 3])
        c1_output = tf.squeeze(output_32[:, :, :, 4])
        p_output = tf.squeeze(output_32[:, :, :, 5])
        p_label = tf.squeeze(labels[:, :, :, 4])
        with tf.name_scope('x-costs'):
            x1_difference = tf.subtract(x1_output, x_label)
            x1_squared = tf.square(x1_difference)
            x1_costs = tf.multiply(p_label, x1_squared)
            x1_costs = tf.reduce_sum(x1_costs)
            x1_costs = tf.multiply(x1_costs, 5)
        with tf.name_scope('y-costs'):
            y1_difference = tf.subtract(y1_output, y_label)
            y1_squared = tf.square(y1_difference)
            y1_costs = tf.multiply(p_label, y1_squared)
            y1_costs = tf.reduce_sum(y1_costs)
            y1_costs = tf.multiply(y1_costs, 5)
        with tf.name_scope('h-costs'):
            h1_output_root = tf.sqrt(h1_output)
            h_label_root = tf.sqrt(h_label)
            h1_difference = tf.subtract(h1_output_root, h_label_root)
            h1_squared = tf.square(h1_difference)
            h1_costs = tf.multiply(p_label, h1_squared)
            h1_costs = tf.reduce_sum(h1_costs)
            h1_costs = tf.multiply(h1_costs, 5)
        with tf.name_scope('w-costs'):
            w1_output_root = tf.sqrt(w1_output)
            w_label_root = tf.sqrt(w_label)
            w1_difference = tf.subtract(w1_output_root, w_label_root)
            w1_squared = tf.square(w1_difference)
            w1_costs = tf.multiply(p_label, w1_squared)
            w1_costs = tf.reduce_sum(w1_costs)
            w1_costs = tf.multiply(w1_costs, 5)
        with tf.name_scope('C-costs'):
            w_label_half = tf.div(w_label, 2)
            h_label_half = tf.div(h_label, 2)
            w1_output_half = tf.div(w1_output, 2)
            h1_output_half = tf.div(h1_output, 2)
            label_left = tf.subtract(x_label, w_label_half)
            label_right = tf.add(x_label, w_label_half)
            label_top = tf.subtract(y_label, h_label_half)
            label_bottom = tf.add(y_label, h_label_half)
            output1_left = tf.subtract(x1_output, w1_output_half)
            output1_right = tf.add(x1_output, w1_output_half)
            output1_top = tf.subtract(y1_output, h1_output_half)
            output1_bottom = tf.add(y1_output, h1_output_half)
            overlap1_left = tf.maximum(label_left, output1_left)
            overlap1_right = tf.minimum(label_right, output1_right)
            overlap1_top = tf.maximum(label_top, output1_top)
            overlap1_bottom = tf.minimum(label_bottom, output1_bottom)
            overlap1_width = tf.subtract(overlap1_right, overlap1_left)
            overlap1_width = tf.maximum(np.float32(0), overlap1_width)
            overlap1_height = tf.subtract(overlap1_bottom, overlap1_top)
            overlap1_height = tf.maximum(np.float32(0), overlap1_height)
            area_of_overlap1 = tf.multiply(overlap1_width, overlap1_height)
            output1_area = tf.multiply(h1_output, w1_output)
            label_area = tf.multiply(h_label, w_label)
            area_of_union1 = tf.subtract(tf.add(output1_area, label_area),
                area_of_overlap1)
            IoU1 = tf.div(area_of_overlap1, area_of_union1)
            IoU1_scalar = tf.reduce_mean(IoU1)
            c1_label = tf.multiply(IoU1, p_label)
            c1_difference = tf.subtract(c1_output, c1_label)
            c1_squared = tf.square(c1_difference)
            with tf.name_scope('obj_present'):
                c1_obj = tf.multiply(c1_squared, p_label)
                c1_obj_costs = tf.reduce_sum(c1_obj)
            with tf.name_scope('noobj_present'):
                p_label_invers = tf.subtract(np.float32(1), p_label)
                c1_noobj = tf.multiply(c1_squared, p_label_invers)
                c1_noobj_costs = tf.reduce_sum(c1_noobj)
                c1_noobj_costs = tf.multiply(c1_noobj_costs, 0.5)
        with tf.name_scope('p_costs'):
            p_difference = tf.subtract(p_output, p_label)
            p_squared = tf.square(p_difference)
            p_costs = tf.multiply(p_label, p_squared)
            p_costs = tf.reduce_sum(p_costs)
        center_costs = tf.add(x1_costs, y1_costs)
        box_costs = tf.add(h1_costs, w1_costs)
        conf_costs = tf.add(c1_obj_costs, c1_noobj_costs)
        spatial_costs = tf.add(center_costs, box_costs)
        prob_costs = tf.add(conf_costs, p_costs)
        costs = tf.add(spatial_costs, prob_costs)
        tf.summary.scalar('Costx', x1_costs)
        tf.summary.scalar('Costy', y1_costs)
        tf.summary.scalar('Costh', h1_costs)
        tf.summary.scalar('Costw', w1_costs)
        tf.summary.scalar('IoU_mean', IoU1_scalar)
        tf.summary.scalar('CostcObj', c1_obj_costs)
        tf.summary.scalar('CostcNoObj', c1_noobj_costs)
        tf.summary.scalar('Costp', p_costs)
        tf.summary.scalar('CostCenter', center_costs)
        tf.summary.scalar('CostBox', box_costs)
        tf.summary.scalar('CostConf', conf_costs)
        tf.summary.scalar('CostSpatial', spatial_costs)
        tf.summary.scalar('CostProb', prob_costs)
        tf.summary.scalar('Costs', costs)
    with tf.name_scope('optimizer') as scope:
        optimizer = tf.train.AdamOptimizer(learning_rate=learnrate, epsilon
            =0.0001)
        grads_and_vars = optimizer.compute_gradients(costs)
        capped_gvs = [(tf.clip_by_value(grad, -5.0, 5.0), var) for grad,
            var in grads_and_vars]
        train_step = optimizer.apply_gradients(capped_gvs)
    for grad, var in capped_gvs:
        if grad is not None:
            tf.summary.histogram(var.op.name + 'capped_gradients', grad)
    with tf.name_scope('Test') as scope:
        with tf.name_scope('IOU'):
            nr_of_fingers_in_batch = tf.reduce_sum(p_label)
            y_label_index = tf.argmax(tf.reduce_max(p_label, axis=[2]), axis=1)
            x_label_index = tf.argmax(tf.reduce_max(p_label, axis=[1]), axis=1)
            y_label_global = tf.add(tf.cast(y_label_index, tf.float32) / 7,
                tf.reduce_max(y_label / 7, axis=[1, 2]))
            x_label_global = tf.add(tf.cast(x_label_index, tf.float32) / 7,
                tf.reduce_max(x_label / 7, axis=[1, 2]))
            h_label_global = tf.reduce_max(h_label, axis=[1, 2])
            w_label_global = tf.reduce_max(w_label, axis=[1, 2])
            probconf_output = tf.multiply(p_output, c1_output)
            y_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[2]), axis=1)
            x_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[1]), axis=1)
            enumerator = tf.constant(range(batchSize), tf.int64)
            probconf_indices_tensor = tf.stack([enumerator,
                y_probconf_index, x_probconf_index], 1)
            y_probconf_coarse = tf.cast(y_probconf_index, tf.float32) / 7
            y_probconf_fine = tf.squeeze(tf.gather_nd(y1_output,
                probconf_indices_tensor)) / 7
            y_probconf = tf.add(y_probconf_coarse, y_probconf_fine)
            x_probconf_coarse = tf.cast(x_probconf_index, tf.float32) / 7
            x_probconf_fine = tf.squeeze(tf.gather_nd(x1_output,
                probconf_indices_tensor)) / 7
            x_probconf = tf.add(x_probconf_coarse, x_probconf_fine)
            h_probconf = tf.squeeze(tf.gather_nd(h1_output,
                probconf_indices_tensor))
            w_probconf = tf.squeeze(tf.gather_nd(w1_output,
                probconf_indices_tensor))
            iou_max_prob_conf = hAPI.iou(x_label_global, y_label_global,
                h_label_global, w_label_global, y_probconf, x_probconf,
                h_probconf, w_probconf)
            iou_max_probconf_mean = tf.reduce_sum(iou_max_prob_conf
                ) / nr_of_fingers_in_batch
            tf.summary.scalar('iou_max_probconf', iou_max_probconf_mean)
        with tf.name_scope('distance'):
            p_labels = tf.reduce_sum(p_label, axis=[1, 2])
            x_probconfs = tf.multiply(x_probconf, p_labels)
            y_probconfs = tf.multiply(y_probconf, p_labels)
            x_labels_global = tf.multiply(x_label_global, p_labels)
            y_labels_global = tf.multiply(y_label_global, p_labels)
            x_probconf_diff = tf.subtract(x_labels_global, x_probconfs)
            y_probconf_diff = tf.subtract(y_labels_global, y_probconfs)
            distance_probconf = tf.sqrt(tf.add(tf.square(x_probconf_diff),
                tf.square(y_probconf_diff)))
            zeros_vector = tf.zeros(batchSize)
            boolean_vector = tf.squeeze(tf.not_equal(zeros_vector, p_labels))
            boolean_vector.set_shape([None])
            distance_probconfs = tf.boolean_mask(distance_probconf,
                boolean_vector)
            mean_distance_probconf, var_distance_probconf = tf.nn.moments(
                distance_probconfs, axes=[0])
            tf.summary.scalar('mean_distance_probconf', mean_distance_probconf)
            tf.summary.scalar('var_distance_probconf', var_distance_probconf)
    init_op = tf.group(tf.global_variables_initializer(), tf.
        local_variables_initializer())
    saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=4.0,
        pad_step_number=True, save_relative_paths=True)
    if not os.path.exists(origin_path + '../../../weights/' + name + '/'):
        os.makedirs(origin_path + '../../../weights/' + name + '/')
    merged_summary_op = tf.summary.merge_all()
    with tf.Session() as sess:
        sess.run(testing_init_op)
        sess.run(validation_init_op)
        sess.run(training_init_op)
        sess.run(init_op)
        if test == False:
            train_train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_traintrain')
            train_train_writer.add_graph(sess.graph)
            train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_train')
            train_writer.add_graph(sess.graph)
            valid_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_valid')
            valid_writer.add_graph(sess.graph)
            print('start training....\n')
            for i in range(nr_of_epochs / nr_of_epochs_until_save_model):
                for j in range(nr_of_epochs_until_save_model):
                    _ = sess.run([train_step], feed_dict={training: True,
                        learnrate: learning_rate})
                numbers_of_iterations_until_now = (i *
                    nr_of_epochs_until_save_model + j + 1)
                _, sumsum = sess.run([train_step, merged_summary_op],
                    feed_dict={training: True, learnrate: learning_rate})
                train_train_writer.add_summary(sumsum,
                    numbers_of_iterations_until_now)
                train_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(validation_init_op)
                valid_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(training_init_op)
                saver.save(sess=sess, save_path=origin_path +
                    '../../../weights/' + name + '/' + name + '.ckpt',
                    global_step=numbers_of_iterations_until_now)
                print('model updatet\n')
        else:
            if not os.path.exists(origin_path + 'picsRecognized/'):
                os.makedirs(origin_path + 'picsRecognized/')
            sess.run(testing_init_op)
            print('Try to restore')
            saver.restore(sess, origin_path +
                '../../../weights/139_9000Set_newPipe/139_9000Set_newPipe.ckpt-00510000'
                )
            print('Restored')
            nr_of_fingers = 0
            nr_of_coarse_recognized_fingers = 0
            nr_of_recognized_fingers = 0
            nr_of_fine_recognized_fingers = 0
            nr_of_extremefine_recognized_fingers = 0
            iou_array = []
            distance_array = []
            distance_x_array = []
            distance_y_array = []
            for i in range(len(test_picnames) / batchSize):
                (testimages, probconfs_y, probconfs_x, probconfs_h,
                    probconfs_w, labels_y, labels_x, labels_h, labels_w,
                    probconfs_iou, probconfs_distance, labels_p,
                    probconfs_x_diff, probconfs_y_diff) = (sess.run([
                    images_unnormalized, y_probconf, x_probconf, h_probconf,
                    w_probconf, y_label_global, x_label_global,
                    h_label_global, w_label_global, iou_max_prob_conf,
                    distance_probconf, p_labels, x_probconf_diff,
                    y_probconf_diff], feed_dict={training: False}))
                for b in range(batchSize):
                    print(str(i * batchSize + b))
                    testimage = testimages[b] * 200
                    probconf_y = probconfs_y[b] * 448
                    probconf_x = probconfs_x[b] * 448
                    probconf_h = probconfs_h[b] * 448
                    probconf_w = probconfs_w[b] * 448
                    probconf_iou = probconfs_iou[b]
                    probconf_distance = probconfs_distance[b]
                    probconf_x_diff = probconfs_x_diff[b]
                    probconf_y_diff = probconfs_y_diff[b]
                    label_y = labels_y[b] * 448
                    label_x = labels_x[b] * 448
                    label_h = labels_h[b] * 448
                    label_w = labels_w[b] * 448
                    print(
                        'iou                                                      = '
                         + str(probconf_iou))
                    print(
                        'distance                                                 = '
                         + str(probconf_distance))
                    if labels_p[b] != 0:
                        iou_array = np.concatenate((iou_array, [probconf_iou]))
                        distance_array = np.concatenate((distance_array, [
                            probconf_distance]))
                        distance_x_array = np.concatenate((distance_x_array,
                            [probconf_x_diff]))
                        distance_y_array = np.concatenate((distance_y_array,
                            [probconf_y_diff]))
                        nr_of_fingers += 1
                        print(
                            'mean_iou                                                 = '
                             + str(np.mean(iou_array)))
                        print(
                            'mean_distance                                            = '
                             + str(np.mean(distance_array)))
                        if probconf_distance < 0.04:
                            nr_of_coarse_recognized_fingers += 1
                        if probconf_distance < 0.02:
                            nr_of_recognized_fingers += 1
                        if probconf_distance < 0.01:
                            nr_of_fine_recognized_fingers += 1
                        if probconf_distance < 0.001:
                            nr_of_extremefine_recognized_fingers += 1
                        coarse_recognized_fingers_in_percent = float(
                            nr_of_coarse_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of coarse recognized fingers (<0.04)          = '
                             + str(coarse_recognized_fingers_in_percent) + '%')
                        recognized_fingers_in_percent = float(
                            nr_of_recognized_fingers) / float(nr_of_fingers
                            ) * 100
                        print(
                            'Percentage of recognized fingers (<0.02)                 = '
                             + str(recognized_fingers_in_percent) + '%')
                        fine_recognized_fingers_in_percent = float(
                            nr_of_fine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of fine recognized fingers (<0.01)            = '
                             + str(fine_recognized_fingers_in_percent) + '%')
                        extremefine_recognized_fingers_in_percent = float(
                            nr_of_extremefine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of extreme fine recognized fingers (>0.001)   = '
                             + str(
                            extremefine_recognized_fingers_in_percent) + '%')
                    sess.run(tf.write_file(origin_path +
                        'picsRecognized/pic' + str(batchSize * i + b) +
                        '.png', tf.image.encode_png(testimage)))
                    img = cv2.imread(origin_path + 'picsRecognized/pic' +
                        str(batchSize * i + b) + '.png')
                    cv2.rectangle(img, (int(probconf_x - probconf_w / 2),
                        int(probconf_y - probconf_h / 2)), (int(probconf_x +
                        probconf_w / 2), int(probconf_y + probconf_h / 2)),
                        (0, 0, 255), 1)
                    cv2.putText(img, 'Prediction', (int(probconf_x - 
                        probconf_w / 2), int(probconf_y - probconf_h / 2)),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'iou=' + str(probconf_iou), (0, 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'dist=' + str(probconf_distance), (0, 
                        50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.rectangle(img, (int(label_x - label_w / 2), int(
                        label_y - label_h / 2)), (int(label_x + label_w / 2
                        ), int(label_y + label_h / 2)), (255, 255, 0), 1)
                    cv2.putText(img, 'Label', (int(label_x - label_w / 2),
                        int(label_y - label_h / 2)), cv2.
                        FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 1)
                    cv2.imwrite(origin_path + 'picsRecognized/pic' + str(
                        batchSize * i + b) + '.png', img)
            plt.hist(iou_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title('IOU Probability-Density \n on Testset (' + str(
                nr_of_fingers) + 'Pictures)')
            plt.xlabel('IOU (median=' + str(round(np.median(iou_array), 3)) +
                ', mean=' + str(round(np.mean(iou_array), 3)) + ', stdev=' +
                str(round(np.std(iou_array), 3)) + ')')
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.4, 0.4], [0, 10], 'orange')
            plt.text(0.3, 9, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(0.45, 9, 'good', bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path + 'picsRecognized/IOUprobDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.02, 0.02], [0, 60], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.savefig(origin_path + 'picsRecognized/distProbDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100] (Log-Scale)')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.yscale('log')
            plt.savefig(origin_path + 'picsRecognized/logdistProbDensity.pdf')
            plt.close()
            distance_array_without_outliers = []
            for dist in distance_array:
                if dist < 0.25:
                    distance_array_without_outliers = np.concatenate((
                        distance_array_without_outliers, [dist]))
            plt.hist(distance_array_without_outliers, bins=np.arange(0.0, 
                0.25, 0.001), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) +
                'Pictures) Without Outliers(Cut at 0.25)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array_without_outliers), 3)) + ', mean=' + str(
                round(np.mean(distance_array_without_outliers), 3)) +
                ', stdev=' + str(round(np.std(
                distance_array_without_outliers), 3)) + ')')
            plt.ylabel('Probability in Promille [1/1000]')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.025, 90, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.002, 90, 'good', bbox=dict(facecolor='green', alpha
                =0.5))
            plt.savefig(origin_path +
                'picsRecognized/distProbDensity_improved.pdf')
            plt.close()
            plt.hist(distance_x_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('X-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('x-Distance (median=' + str(round(np.median(
                distance_x_array), 3)) + ', mean=' + str(round(np.mean(
                distance_x_array), 3)) + ', stdev=' + str(round(np.std(
                distance_x_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/xDistance.pdf')
            plt.close()
            plt.hist(distance_y_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('Y-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('y-Distance (median=' + str(round(np.median(
                distance_y_array), 3)) + ', mean=' + str(round(np.mean(
                distance_y_array), 3)) + ', stdev=' + str(round(np.std(
                distance_y_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/yDistance.pdf')
            plt.close()
    print('finished')


if __name__ == '__main__':
    main()

from __future__ import print_function
import os
import sys
import numpy as np
import tensorflow as tf
from tensorflow.contrib.data import Dataset, Iterator
this_folder = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, this_folder +
    '/../dataPreprocessing/OnTabeasData/Step4CollectTogether/')
import analyse_Fingerset
sys.path.insert(0, this_folder + '/../helperfunctions/')
import heinzAPI as hAPI
import parserClassFingers as pC
import cv2
import matplotlib.pyplot as plt
parser_object = pC.make_parser()
name = parser_object.modelname
print(name)
batchSize = parser_object.batch_Size
learning_rate = parser_object.learning_rate
num_threads = parser_object.num_Threads
buffer_size = parser_object.buffer_Size
origin_path = parser_object.origin_Path
nr_of_epochs = parser_object.nr_of_epochs
nr_of_epochs_until_save_model = parser_object.nr_of_epochs_until_save_model
dropout = parser_object.dropout_bool
batchnorm = parser_object.batchnorm_bool
test = parser_object.test_bool


def traindata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'trainDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def validdata_preprocessor(picname, label):
    content = tf.read_file(origin_path + 'validDataset/' + picname)
    image = tf.image.decode_png(content, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    return image, label


def main():
    print('TensorFlow version ', tf.__version__)
    with tf.name_scope('Data') as scope:
        print('read in all Train Picture-Names & Labels and shuffle them')
        ReadData = analyse_Fingerset.Dataset_Heinz()
        train_data = ReadData.get_train_data(origin_path=origin_path)
        train_picnames = [row[0] for row in train_data]
        train_labels = np.float32([row[1] for row in train_data])
        train_data = Dataset.from_tensor_slices((train_picnames, train_labels))
        train_data = train_data.repeat()
        train_data = train_data.shuffle(buffer_size=buffer_size)
        train_data = train_data.map(map_func=traindata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        train_data = train_data.batch(batchSize)
        print('read in all Valid Picture-Names & Labels and shuffle them')
        valid_data = ReadData.get_valid_data(origin_path=origin_path)
        valid_picnames = [row[0] for row in valid_data]
        valid_labels = np.float32([row[1] for row in valid_data])
        valid_data = Dataset.from_tensor_slices((valid_picnames, valid_labels))
        valid_data = valid_data.repeat()
        valid_data = valid_data.shuffle(buffer_size=buffer_size)
        valid_data = valid_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        valid_data = valid_data.batch(batchSize)
        print('read in all Test Picture-Names & Labels and shuffle them')
        test_data = ReadData.get_valid_data(origin_path=origin_path)
        test_picnames = [row[0] for row in test_data]
        test_labels = np.float32([row[1] for row in test_data])
        test_data = Dataset.from_tensor_slices((test_picnames, test_labels))
        test_data = test_data.map(map_func=validdata_preprocessor,
            num_threads=num_threads, output_buffer_size=10000)
        test_data = test_data.batch(batchSize)
    print('finished read all Pictures and Labels, start Data-iterator')
    with tf.name_scope('Data-Iterator') as scope:
        iterator = Iterator.from_structure(train_data.output_types,
            train_data.output_shapes)
        images_unnormalized, labels = iterator.get_next()
        training_init_op = iterator.make_initializer(train_data)
        validation_init_op = iterator.make_initializer(valid_data)
        testing_init_op = iterator.make_initializer(test_data)
        images_unnormalized = tf.image.resize_images(images_unnormalized, [
            448, 448])
        images_unnormalized = tf.cast(images_unnormalized, tf.float32)
        tf.summary.image('images_after_crop', tensor=images_unnormalized,
            max_outputs=20)
    training = tf.placeholder(tf.bool, name='training')
    learnrate = tf.placeholder(tf.float32, name='learnrate')
    with tf.name_scope('normalize_pictures') as scope:
        images = hAPI.normalize_pictures(tensor=images_unnormalized)
    output_1 = hAPI.convLayerPretrained(tensor=images, layerNr=1, batchSize
        =batchSize, filterwidth=7, inputdepth=1, outputdepth=64, strides=2,
        batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('2_Maxpool_Layer') as scope:
        output_2 = tf.nn.max_pool(output_1, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_2')
    output_3 = hAPI.convLayerPretrained(tensor=output_2, layerNr=3,
        batchSize=batchSize, filterwidth=3, inputdepth=64, outputdepth=192,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('4_Maxpool_Layer') as scope:
        output_4 = tf.nn.max_pool(output_3, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_4')
    output_5 = hAPI.convLayerPretrained(tensor=output_4, layerNr=5,
        batchSize=batchSize, filterwidth=1, inputdepth=192, outputdepth=128,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_6 = hAPI.convLayerPretrained(tensor=output_5, layerNr=6,
        batchSize=batchSize, filterwidth=3, inputdepth=128, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_7 = hAPI.convLayerPretrained(tensor=output_6, layerNr=7,
        batchSize=batchSize, filterwidth=1, inputdepth=256, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_8 = hAPI.convLayerPretrained(tensor=output_7, layerNr=8,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    with tf.name_scope('9_Maxpool_Layer') as scope:
        output_9 = tf.nn.max_pool(output_8, ksize=[1, 2, 2, 1], strides=[1,
            2, 2, 1], padding='SAME', name='maxpool_9')
    output_10 = hAPI.convLayerPretrained(tensor=output_9, layerNr=10,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_11 = hAPI.convLayerPretrained(tensor=output_10, layerNr=11,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_12 = hAPI.convLayerPretrained(tensor=output_11, layerNr=12,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_13 = hAPI.convLayerPretrained(tensor=output_12, layerNr=13,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_14 = hAPI.convLayerPretrained(tensor=output_13, layerNr=14,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_15 = hAPI.convLayerPretrained(tensor=output_14, layerNr=15,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_16 = hAPI.convLayerPretrained(tensor=output_15, layerNr=16,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_17 = hAPI.convLayerPretrained(tensor=output_16, layerNr=17,
        batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_18 = hAPI.convLayerPretrained(tensor=output_17, layerNr=18,
        batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=512,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training,
        origin_path=origin_path)
    output_19 = hAPI.convLayerPretrained(tensor=output_18, layerNr=19,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    with tf.name_scope('20_Maxpool_Layer') as scope:
        output_20 = tf.nn.max_pool(output_19, ksize=[1, 2, 2, 1], strides=[
            1, 2, 2, 1], padding='SAME', name='maxpool_20')
    output_21 = hAPI.convLayerPretrained(tensor=output_20, layerNr=21,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_22 = hAPI.convLayerPretrained(tensor=output_21, layerNr=22,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_23 = hAPI.convLayerPretrained(tensor=output_22, layerNr=23,
        batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=
        512, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_24 = hAPI.convLayerPretrained(tensor=output_23, layerNr=24,
        batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=
        1024, strides=1, batchnorm_=batchnorm, dropout_=False, training=
        training, origin_path=origin_path)
    output_25 = hAPI.convLayer(tensor=output_24, layerNr=25, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_26 = hAPI.convLayer(tensor=output_25, layerNr=26, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=2, batchnorm_=batchnorm, dropout_=False, training=training)
    output_27 = hAPI.convLayer(tensor=output_26, layerNr=27, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    output_28 = hAPI.convLayer(tensor=output_27, layerNr=28, batchSize=
        batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024,
        strides=1, batchnorm_=batchnorm, dropout_=False, training=training)
    with tf.name_scope('Layer31_full') as scope:
        input_31 = tf.reshape(tensor=output_28, shape=[batchSize, 7 * 7 * 1024]
            )
        if dropout == True:
            with tf.name_scope('dropout'):
                input_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    input_31, keep_prob=0.8, noise_shape=[batchSize, 7 * 7 *
                    1024]), lambda : input_31)
        W31 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 1024, 4096],
            stddev=0.01, dtype=tf.float32), name='W31')
        b31 = tf.Variable(tf.truncated_normal(shape=[4096], stddev=0.01,
            dtype=tf.float32), name='b31')
        preactivate_31 = tf.add(tf.matmul(input_31, W31), b31)
        with tf.name_scope('batch_norm'):
            input_depth_31 = preactivate_31.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta31 = tf.Variable(tf.constant(0.0, shape=[input_depth_31
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma31 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_31], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean31, batch_variance31 = tf.nn.moments(x=preactivate_31,
                axes=[0, 1])
            preactivate_31 = tf.nn.batch_normalization(x=preactivate_31,
                mean=batch_mean31, variance=batch_variance31, offset=beta31,
                scale=gamma31, variance_epsilon=0.0001, name=None)
        output_31 = tf.nn.relu(preactivate_31)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W31, name='W31')
            hAPI.variable_summaries(variable=b31, name='b31')
            hAPI.variable_summaries(variable=preactivate_31, name=
                'preactivate31')
            hAPI.variable_summaries(variable=output_31, name='output31')
    with tf.name_scope('Layer32_full') as scope:
        if dropout == True:
            with tf.name_scope('dropout'):
                output_31 = tf.cond(training, lambda : tf.nn.dropout(x=
                    output_31, keep_prob=0.8, noise_shape=[batchSize, 4096]
                    ), lambda : output_31)
        W32 = tf.Variable(tf.truncated_normal(shape=[4096, 7 * 7 * 6],
            stddev=0.01, dtype=tf.float32), name='W32')
        b32 = tf.Variable(tf.truncated_normal(shape=[7 * 7 * 6], stddev=
            0.01, dtype=tf.float32), name='b32')
        preactivate_32 = tf.add(tf.matmul(output_31, W32), b32)
        with tf.name_scope('batch_norm'):
            input_depth_32 = preactivate_32.get_shape().as_list()[-1]
            with tf.name_scope('beta'):
                beta32 = tf.Variable(tf.constant(0.0, shape=[input_depth_32
                    ], dtype=tf.float32), name='beta', trainable=True)
            with tf.name_scope('gamma'):
                gamma32 = tf.Variable(tf.constant(1.0, shape=[
                    input_depth_32], dtype=tf.float32), name='gamma',
                    trainable=True)
            batch_mean32, batch_variance32 = tf.nn.moments(x=preactivate_32,
                axes=[0, 1])
            preactivate_32 = tf.nn.batch_normalization(x=preactivate_32,
                mean=batch_mean32, variance=batch_variance32, offset=beta32,
                scale=gamma32, variance_epsilon=0.0001, name=None)
        fully_32 = preactivate_32
        output_32 = tf.sigmoid(tf.reshape(tensor=fully_32, shape=[batchSize,
            7, 7, 6]))
        output_32 = tf.cast(output_32, tf.float32)
        with tf.name_scope('summary'):
            hAPI.variable_summaries(variable=W32, name='W32')
            hAPI.variable_summaries(variable=b32, name='b32')
            hAPI.variable_summaries(variable=preactivate_32, name=
                'preactivate32')
            hAPI.variable_summaries(variable=output_32, name='output32')
    with tf.name_scope('cost_function') as scope:
        x1_output = tf.squeeze(output_32[:, :, :, 0])
        x_label = tf.squeeze(labels[:, :, :, 0])
        y1_output = tf.squeeze(output_32[:, :, :, 1])
        y_label = tf.squeeze(labels[:, :, :, 1])
        h1_output = tf.squeeze(output_32[:, :, :, 2])
        h_label = tf.squeeze(labels[:, :, :, 2])
        w1_output = tf.squeeze(output_32[:, :, :, 3])
        w_label = tf.squeeze(labels[:, :, :, 3])
        c1_output = tf.squeeze(output_32[:, :, :, 4])
        p_output = tf.squeeze(output_32[:, :, :, 5])
        p_label = tf.squeeze(labels[:, :, :, 4])
        with tf.name_scope('x-costs'):
            x1_difference = tf.subtract(x1_output, x_label)
            x1_squared = tf.square(x1_difference)
            x1_costs = tf.multiply(p_label, x1_squared)
            x1_costs = tf.reduce_sum(x1_costs)
            x1_costs = tf.multiply(x1_costs, 5)
        with tf.name_scope('y-costs'):
            y1_difference = tf.subtract(y1_output, y_label)
            y1_squared = tf.square(y1_difference)
            y1_costs = tf.multiply(p_label, y1_squared)
            y1_costs = tf.reduce_sum(y1_costs)
            y1_costs = tf.multiply(y1_costs, 5)
        with tf.name_scope('h-costs'):
            h1_output_root = tf.sqrt(h1_output)
            h_label_root = tf.sqrt(h_label)
            h1_difference = tf.subtract(h1_output_root, h_label_root)
            h1_squared = tf.square(h1_difference)
            h1_costs = tf.multiply(p_label, h1_squared)
            h1_costs = tf.reduce_sum(h1_costs)
            h1_costs = tf.multiply(h1_costs, 5)
        with tf.name_scope('w-costs'):
            w1_output_root = tf.sqrt(w1_output)
            w_label_root = tf.sqrt(w_label)
            w1_difference = tf.subtract(w1_output_root, w_label_root)
            w1_squared = tf.square(w1_difference)
            w1_costs = tf.multiply(p_label, w1_squared)
            w1_costs = tf.reduce_sum(w1_costs)
            w1_costs = tf.multiply(w1_costs, 5)
        with tf.name_scope('C-costs'):
            w_label_half = tf.div(w_label, 2)
            h_label_half = tf.div(h_label, 2)
            w1_output_half = tf.div(w1_output, 2)
            h1_output_half = tf.div(h1_output, 2)
            label_left = tf.subtract(x_label, w_label_half)
            label_right = tf.add(x_label, w_label_half)
            label_top = tf.subtract(y_label, h_label_half)
            label_bottom = tf.add(y_label, h_label_half)
            output1_left = tf.subtract(x1_output, w1_output_half)
            output1_right = tf.add(x1_output, w1_output_half)
            output1_top = tf.subtract(y1_output, h1_output_half)
            output1_bottom = tf.add(y1_output, h1_output_half)
            overlap1_left = tf.maximum(label_left, output1_left)
            overlap1_right = tf.minimum(label_right, output1_right)
            overlap1_top = tf.maximum(label_top, output1_top)
            overlap1_bottom = tf.minimum(label_bottom, output1_bottom)
            overlap1_width = tf.subtract(overlap1_right, overlap1_left)
            overlap1_width = tf.maximum(np.float32(0), overlap1_width)
            overlap1_height = tf.subtract(overlap1_bottom, overlap1_top)
            overlap1_height = tf.maximum(np.float32(0), overlap1_height)
            area_of_overlap1 = tf.multiply(overlap1_width, overlap1_height)
            output1_area = tf.multiply(h1_output, w1_output)
            label_area = tf.multiply(h_label, w_label)
            area_of_union1 = tf.subtract(tf.add(output1_area, label_area),
                area_of_overlap1)
            IoU1 = tf.div(area_of_overlap1, area_of_union1)
            IoU1_scalar = tf.reduce_mean(IoU1)
            c1_label = tf.multiply(IoU1, p_label)
            c1_difference = tf.subtract(c1_output, c1_label)
            c1_squared = tf.square(c1_difference)
            with tf.name_scope('obj_present'):
                c1_obj = tf.multiply(c1_squared, p_label)
                c1_obj_costs = tf.reduce_sum(c1_obj)
            with tf.name_scope('noobj_present'):
                p_label_invers = tf.subtract(np.float32(1), p_label)
                c1_noobj = tf.multiply(c1_squared, p_label_invers)
                c1_noobj_costs = tf.reduce_sum(c1_noobj)
                c1_noobj_costs = tf.multiply(c1_noobj_costs, 0.5)
        with tf.name_scope('p_costs'):
            p_difference = tf.subtract(p_output, p_label)
            p_squared = tf.square(p_difference)
            p_costs = tf.multiply(p_label, p_squared)
            p_costs = tf.reduce_sum(p_costs)
        center_costs = tf.add(x1_costs, y1_costs)
        box_costs = tf.add(h1_costs, w1_costs)
        conf_costs = tf.add(c1_obj_costs, c1_noobj_costs)
        spatial_costs = tf.add(center_costs, box_costs)
        prob_costs = tf.add(conf_costs, p_costs)
        costs = tf.add(spatial_costs, prob_costs)
        tf.summary.scalar('Costx', x1_costs)
        tf.summary.scalar('Costy', y1_costs)
        tf.summary.scalar('Costh', h1_costs)
        tf.summary.scalar('Costw', w1_costs)
        tf.summary.scalar('IoU_mean', IoU1_scalar)
        tf.summary.scalar('CostcObj', c1_obj_costs)
        tf.summary.scalar('CostcNoObj', c1_noobj_costs)
        tf.summary.scalar('Costp', p_costs)
        tf.summary.scalar('CostCenter', center_costs)
        tf.summary.scalar('CostBox', box_costs)
        tf.summary.scalar('CostConf', conf_costs)
        tf.summary.scalar('CostSpatial', spatial_costs)
        tf.summary.scalar('CostProb', prob_costs)
        tf.summary.scalar('Costs', costs)
    with tf.name_scope('optimizer') as scope:
        optimizer = tf.train.AdamOptimizer(learning_rate=learnrate, epsilon
            =0.0001)
        grads_and_vars = optimizer.compute_gradients(costs)
        capped_gvs = [(tf.clip_by_value(grad, -5.0, 5.0), var) for grad,
            var in grads_and_vars]
        train_step = optimizer.apply_gradients(capped_gvs)
    for grad, var in capped_gvs:
        if grad is not None:
            tf.summary.histogram(var.op.name + 'capped_gradients', grad)
    with tf.name_scope('Test') as scope:
        with tf.name_scope('IOU'):
            nr_of_fingers_in_batch = tf.reduce_sum(p_label)
            y_label_index = tf.argmax(tf.reduce_max(p_label, axis=[2]), axis=1)
            x_label_index = tf.argmax(tf.reduce_max(p_label, axis=[1]), axis=1)
            y_label_global = tf.add(tf.cast(y_label_index, tf.float32) / 7,
                tf.reduce_max(y_label / 7, axis=[1, 2]))
            x_label_global = tf.add(tf.cast(x_label_index, tf.float32) / 7,
                tf.reduce_max(x_label / 7, axis=[1, 2]))
            h_label_global = tf.reduce_max(h_label, axis=[1, 2])
            w_label_global = tf.reduce_max(w_label, axis=[1, 2])
            probconf_output = tf.multiply(p_output, c1_output)
            y_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[2]), axis=1)
            x_probconf_index = tf.argmax(tf.reduce_max(probconf_output,
                axis=[1]), axis=1)
            enumerator = tf.constant(range(batchSize), tf.int64)
            probconf_indices_tensor = tf.stack([enumerator,
                y_probconf_index, x_probconf_index], 1)
            y_probconf_coarse = tf.cast(y_probconf_index, tf.float32) / 7
            y_probconf_fine = tf.squeeze(tf.gather_nd(y1_output,
                probconf_indices_tensor)) / 7
            y_probconf = tf.add(y_probconf_coarse, y_probconf_fine)
            x_probconf_coarse = tf.cast(x_probconf_index, tf.float32) / 7
            x_probconf_fine = tf.squeeze(tf.gather_nd(x1_output,
                probconf_indices_tensor)) / 7
            x_probconf = tf.add(x_probconf_coarse, x_probconf_fine)
            h_probconf = tf.squeeze(tf.gather_nd(h1_output,
                probconf_indices_tensor))
            w_probconf = tf.squeeze(tf.gather_nd(w1_output,
                probconf_indices_tensor))
            iou_max_prob_conf = hAPI.iou(x_label_global, y_label_global,
                h_label_global, w_label_global, y_probconf, x_probconf,
                h_probconf, w_probconf)
            iou_max_probconf_mean = tf.reduce_sum(iou_max_prob_conf
                ) / nr_of_fingers_in_batch
            tf.summary.scalar('iou_max_probconf', iou_max_probconf_mean)
        with tf.name_scope('distance'):
            p_labels = tf.reduce_sum(p_label, axis=[1, 2])
            x_probconfs = tf.multiply(x_probconf, p_labels)
            y_probconfs = tf.multiply(y_probconf, p_labels)
            x_labels_global = tf.multiply(x_label_global, p_labels)
            y_labels_global = tf.multiply(y_label_global, p_labels)
            x_probconf_diff = tf.subtract(x_labels_global, x_probconfs)
            y_probconf_diff = tf.subtract(y_labels_global, y_probconfs)
            distance_probconf = tf.sqrt(tf.add(tf.square(x_probconf_diff),
                tf.square(y_probconf_diff)))
            zeros_vector = tf.zeros(batchSize)
            boolean_vector = tf.squeeze(tf.not_equal(zeros_vector, p_labels))
            boolean_vector.set_shape([None])
            distance_probconfs = tf.boolean_mask(distance_probconf,
                boolean_vector)
            mean_distance_probconf, var_distance_probconf = tf.nn.moments(
                distance_probconfs, axes=[0])
            tf.summary.scalar('mean_distance_probconf', mean_distance_probconf)
            tf.summary.scalar('var_distance_probconf', var_distance_probconf)
    init_op = tf.group(tf.global_variables_initializer(), tf.
        local_variables_initializer())
    saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=4.0,
        pad_step_number=True, save_relative_paths=True)
    if not os.path.exists(origin_path + '../../../weights/' + name + '/'):
        os.makedirs(origin_path + '../../../weights/' + name + '/')
    merged_summary_op = tf.summary.merge_all()
    with tf.Session() as sess:
        sess.run(testing_init_op)
        sess.run(validation_init_op)
        sess.run(training_init_op)
        sess.run(init_op)
        if test == False:
            train_train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_traintrain')
            train_train_writer.add_graph(sess.graph)
            train_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_train')
            train_writer.add_graph(sess.graph)
            valid_writer = tf.summary.FileWriter(origin_path +
                '../../../summarys/training/summary_' + name + '_valid')
            valid_writer.add_graph(sess.graph)
            print('start training....\n')
            for i in range(nr_of_epochs / nr_of_epochs_until_save_model):
                for j in range(nr_of_epochs_until_save_model):
                    _ = sess.run([train_step], feed_dict={training: True,
                        learnrate: learning_rate})
                numbers_of_iterations_until_now = (i *
                    nr_of_epochs_until_save_model + j + 1)
                _, sumsum = sess.run([train_step, merged_summary_op],
                    feed_dict={training: True, learnrate: learning_rate})
                train_train_writer.add_summary(sumsum,
                    numbers_of_iterations_until_now)
                train_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(validation_init_op)
                valid_writer.add_summary(sess.run(merged_summary_op,
                    feed_dict={training: False, learnrate: learning_rate}),
                    numbers_of_iterations_until_now)
                sess.run(training_init_op)
                saver.save(sess=sess, save_path=origin_path +
                    '../../../weights/' + name + '/' + name + '.ckpt',
                    global_step=numbers_of_iterations_until_now)
                print('model updatet\n')
        else:
            if not os.path.exists(origin_path + 'picsRecognized/'):
                os.makedirs(origin_path + 'picsRecognized/')
            sess.run(testing_init_op)
            print('Try to restore')
            saver.restore(sess, origin_path +
                '../../../weights/139_9000Set_newPipe/139_9000Set_newPipe.ckpt-00510000'
                )
            print('Restored')
            nr_of_fingers = 0
            nr_of_coarse_recognized_fingers = 0
            nr_of_recognized_fingers = 0
            nr_of_fine_recognized_fingers = 0
            nr_of_extremefine_recognized_fingers = 0
            iou_array = []
            distance_array = []
            distance_x_array = []
            distance_y_array = []
            for i in range(len(test_picnames) / batchSize):
                (testimages, probconfs_y, probconfs_x, probconfs_h,
                    probconfs_w, labels_y, labels_x, labels_h, labels_w,
                    probconfs_iou, probconfs_distance, labels_p,
                    probconfs_x_diff, probconfs_y_diff) = (sess.run([
                    images_unnormalized, y_probconf, x_probconf, h_probconf,
                    w_probconf, y_label_global, x_label_global,
                    h_label_global, w_label_global, iou_max_prob_conf,
                    distance_probconf, p_labels, x_probconf_diff,
                    y_probconf_diff], feed_dict={training: False}))
                for b in range(batchSize):
                    print(str(i * batchSize + b))
                    testimage = testimages[b] * 200
                    probconf_y = probconfs_y[b] * 448
                    probconf_x = probconfs_x[b] * 448
                    probconf_h = probconfs_h[b] * 448
                    probconf_w = probconfs_w[b] * 448
                    probconf_iou = probconfs_iou[b]
                    probconf_distance = probconfs_distance[b]
                    probconf_x_diff = probconfs_x_diff[b]
                    probconf_y_diff = probconfs_y_diff[b]
                    label_y = labels_y[b] * 448
                    label_x = labels_x[b] * 448
                    label_h = labels_h[b] * 448
                    label_w = labels_w[b] * 448
                    print(
                        'iou                                                      = '
                         + str(probconf_iou))
                    print(
                        'distance                                                 = '
                         + str(probconf_distance))
                    if labels_p[b] != 0:
                        iou_array = np.concatenate((iou_array, [probconf_iou]))
                        distance_array = np.concatenate((distance_array, [
                            probconf_distance]))
                        distance_x_array = np.concatenate((distance_x_array,
                            [probconf_x_diff]))
                        distance_y_array = np.concatenate((distance_y_array,
                            [probconf_y_diff]))
                        nr_of_fingers += 1
                        print(
                            'mean_iou                                                 = '
                             + str(np.mean(iou_array)))
                        print(
                            'mean_distance                                            = '
                             + str(np.mean(distance_array)))
                        if probconf_distance < 0.04:
                            nr_of_coarse_recognized_fingers += 1
                        if probconf_distance < 0.02:
                            nr_of_recognized_fingers += 1
                        if probconf_distance < 0.01:
                            nr_of_fine_recognized_fingers += 1
                        if probconf_distance < 0.001:
                            nr_of_extremefine_recognized_fingers += 1
                        coarse_recognized_fingers_in_percent = float(
                            nr_of_coarse_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of coarse recognized fingers (<0.04)          = '
                             + str(coarse_recognized_fingers_in_percent) + '%')
                        recognized_fingers_in_percent = float(
                            nr_of_recognized_fingers) / float(nr_of_fingers
                            ) * 100
                        print(
                            'Percentage of recognized fingers (<0.02)                 = '
                             + str(recognized_fingers_in_percent) + '%')
                        fine_recognized_fingers_in_percent = float(
                            nr_of_fine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of fine recognized fingers (<0.01)            = '
                             + str(fine_recognized_fingers_in_percent) + '%')
                        extremefine_recognized_fingers_in_percent = float(
                            nr_of_extremefine_recognized_fingers) / float(
                            nr_of_fingers) * 100
                        print(
                            'Percentage of extreme fine recognized fingers (>0.001)   = '
                             + str(
                            extremefine_recognized_fingers_in_percent) + '%')
                    sess.run(tf.write_file(origin_path +
                        'picsRecognized/pic' + str(batchSize * i + b) +
                        '.png', tf.image.encode_png(testimage)))
                    img = cv2.imread(origin_path + 'picsRecognized/pic' +
                        str(batchSize * i + b) + '.png')
                    cv2.rectangle(img, (int(probconf_x - probconf_w / 2),
                        int(probconf_y - probconf_h / 2)), (int(probconf_x +
                        probconf_w / 2), int(probconf_y + probconf_h / 2)),
                        (0, 0, 255), 1)
                    cv2.putText(img, 'Prediction', (int(probconf_x - 
                        probconf_w / 2), int(probconf_y - probconf_h / 2)),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'iou=' + str(probconf_iou), (0, 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.putText(img, 'dist=' + str(probconf_distance), (0, 
                        50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                    cv2.rectangle(img, (int(label_x - label_w / 2), int(
                        label_y - label_h / 2)), (int(label_x + label_w / 2
                        ), int(label_y + label_h / 2)), (255, 255, 0), 1)
                    cv2.putText(img, 'Label', (int(label_x - label_w / 2),
                        int(label_y - label_h / 2)), cv2.
                        FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 1)
                    cv2.imwrite(origin_path + 'picsRecognized/pic' + str(
                        batchSize * i + b) + '.png', img)
            plt.hist(iou_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title('IOU Probability-Density \n on Testset (' + str(
                nr_of_fingers) + 'Pictures)')
            plt.xlabel('IOU (median=' + str(round(np.median(iou_array), 3)) +
                ', mean=' + str(round(np.mean(iou_array), 3)) + ', stdev=' +
                str(round(np.std(iou_array), 3)) + ')')
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.4, 0.4], [0, 10], 'orange')
            plt.text(0.3, 9, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(0.45, 9, 'good', bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path + 'picsRecognized/IOUprobDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100]')
            plt.plot([0.02, 0.02], [0, 60], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.savefig(origin_path + 'picsRecognized/distProbDensity.pdf')
            plt.close()
            plt.hist(distance_array, bins=np.arange(0.0, 1.0, 0.01), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) + 'Pictures)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array), 3)) + ', mean=' + str(round(np.mean(
                distance_array), 3)) + ', stdev=' + str(round(np.std(
                distance_array), 3)) +
                """) 
 max. possible distance = sqrt(2)""")
            plt.ylabel('Probability in % [1/100] (Log-Scale)')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.05, 55, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07, 55, 'good', bbox=dict(facecolor='green', alpha=0.5)
                )
            plt.yscale('log')
            plt.savefig(origin_path + 'picsRecognized/logdistProbDensity.pdf')
            plt.close()
            distance_array_without_outliers = []
            for dist in distance_array:
                if dist < 0.25:
                    distance_array_without_outliers = np.concatenate((
                        distance_array_without_outliers, [dist]))
            plt.hist(distance_array_without_outliers, bins=np.arange(0.0, 
                0.25, 0.001), normed=1)
            plt.title(
                'Label/Prediction-Center-Distance Probability-Density \n on Testset ('
                 + str(nr_of_fingers) +
                'Pictures) Without Outliers(Cut at 0.25)')
            plt.xlabel('Distance (median=' + str(round(np.median(
                distance_array_without_outliers), 3)) + ', mean=' + str(
                round(np.mean(distance_array_without_outliers), 3)) +
                ', stdev=' + str(round(np.std(
                distance_array_without_outliers), 3)) + ')')
            plt.ylabel('Probability in Promille [1/1000]')
            plt.plot([0.02, 0.02], [0, 100], 'orange')
            plt.text(0.025, 90, 'bad', bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.002, 90, 'good', bbox=dict(facecolor='green', alpha
                =0.5))
            plt.savefig(origin_path +
                'picsRecognized/distProbDensity_improved.pdf')
            plt.close()
            plt.hist(distance_x_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('X-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('x-Distance (median=' + str(round(np.median(
                distance_x_array), 3)) + ', mean=' + str(round(np.mean(
                distance_x_array), 3)) + ', stdev=' + str(round(np.std(
                distance_x_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/xDistance.pdf')
            plt.close()
            plt.hist(distance_y_array, bins=np.arange(-0.125, 0.125, 0.001),
                normed=1)
            plt.title('Y-Distance \n on Testset (' + str(nr_of_fingers) +
                'Pictures)')
            plt.xlabel('y-Distance (median=' + str(round(np.median(
                distance_y_array), 3)) + ', mean=' + str(round(np.mean(
                distance_y_array), 3)) + ', stdev=' + str(round(np.std(
                distance_y_array), 3)) + ')')
            plt.ylabel('Probability/Distance')
            plt.plot([0.0, 0.0], [0, 100], 'orange')
            plt.savefig(origin_path + 'picsRecognized/yDistance.pdf')
            plt.close()
    print('finished')


if __name__ == '__main__':
    main()

# A simple MNIST classifier (linear regression) with tensorflow
#

#
# Usage:
# python2.7 -m pip install tensorflow
# python3 board.py
# python3.5 board.py
#
# tensorboard --logdir=summary
# 
# JSCH 2017-04-23

from __future__ import print_function

import os
import sys
import numpy as np

import tensorflow as tf
from tensorflow.contrib.data import Dataset, Iterator

this_folder =  os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, this_folder+ '/../dataPreprocessing/OnTabeasData/Step4CollectTogether/')
import analyse_Fingerset
sys.path.insert(0,this_folder+"/../helperfunctions/")
import heinzAPI as hAPI
import parserClassFingers as pC

import cv2
import matplotlib.pyplot as plt

#get default-Hyperparameters, btw. Parameters from shell-script:
parser_object = pC.make_parser()

name                            = parser_object.modelname
print(name)
batchSize                       = parser_object.batch_Size
learning_rate                   = parser_object.learning_rate
num_threads                     = parser_object.num_Threads
buffer_size                     = parser_object.buffer_Size
origin_path                     = parser_object.origin_Path
nr_of_epochs                    = parser_object.nr_of_epochs
nr_of_epochs_until_save_model   = parser_object.nr_of_epochs_until_save_model
dropout                         = parser_object.dropout_bool
batchnorm                       = parser_object.batchnorm_bool
test                            = parser_object.test_bool



def traindata_preprocessor(picname,label):
    content = tf.read_file(origin_path + "trainDataset/" + picname)
    image = tf.image.decode_png(content,channels=1)
    image = tf.image.convert_image_dtype(image,tf.float32)
    #here could be done a random crop, but attention to the labels!!
    return image,label
def validdata_preprocessor(picname,label):
    content = tf.read_file(origin_path +"validDataset/"+ picname)
    image = tf.image.decode_png(content,channels=1)
    image = tf.image.convert_image_dtype(image,tf.float32)
    #here could be done a random crop, but attention to the labels!!
    return image,label
    
def main():
    print("TensorFlow version ", tf.__version__)
    with tf.name_scope("Data") as scope:
        print("read in all Train Picture-Names & Labels and shuffle them")
        ReadData        = analyse_Fingerset.Dataset_Heinz()
        
        train_data      = ReadData.get_train_data(origin_path = origin_path)
        train_picnames  = [row[0] for row in train_data]
        train_labels    = np.float32([row[1] for row in train_data])#puts a 4 Dimensional Vector to train_labels
        train_data      = Dataset.from_tensor_slices((train_picnames,train_labels))
        train_data      = train_data.repeat()
        train_data      = train_data.shuffle(buffer_size=buffer_size)
        train_data      = train_data.map(map_func=traindata_preprocessor,
                                         num_threads=num_threads,
                                         output_buffer_size=10000)
        train_data      = train_data.batch(batchSize)

        print("read in all Valid Picture-Names & Labels and shuffle them")
        valid_data      = ReadData.get_valid_data(origin_path=origin_path)
        valid_picnames  = [row[0] for row in valid_data]
        valid_labels    = np.float32([row[1] for row in valid_data])
        valid_data      = Dataset.from_tensor_slices((valid_picnames,valid_labels))
        valid_data      = valid_data.repeat()
        valid_data      = valid_data.shuffle(buffer_size=buffer_size)
        valid_data      = valid_data.map(map_func=validdata_preprocessor,
                                         num_threads=num_threads,
                                         output_buffer_size=10000)
        valid_data      = valid_data.batch(batchSize)

        print("read in all Test Picture-Names & Labels and shuffle them")  
        test_data       = ReadData.get_valid_data(origin_path=origin_path)
        test_picnames   = [row[0] for row in test_data]
        test_labels    = np.float32([row[1] for row in test_data])
        test_data      = Dataset.from_tensor_slices((test_picnames,test_labels))
        test_data      = test_data.map(map_func=validdata_preprocessor,
                                         num_threads=num_threads,
                                         output_buffer_size=10000)
        #No Shuffler and no Repeat for Testdata, because every Testpicture
        #shall be used exactly once
        test_data       = test_data.batch(batchSize)

    print("finished read all Pictures and Labels, start Data-iterator")
    
    with tf.name_scope("Data-Iterator") as scope:        
        iterator        = Iterator.from_structure(train_data.output_types, train_data.output_shapes)
        images_unnormalized, labels  = iterator.get_next()
        
        training_init_op    = iterator.make_initializer(train_data)
        validation_init_op  = iterator.make_initializer(valid_data)
        testing_init_op     = iterator.make_initializer(test_data)
        images_unnormalized = tf.image.resize_images(images_unnormalized,[448,448])
        images_unnormalized = tf.cast(images_unnormalized,tf.float32)
        
        #Puts the a few of the first Pictures to tensorboard
        tf.summary.image('images_after_crop',tensor = images_unnormalized , max_outputs=20)
            
    #is true,if the model is training right now, and is False, if the model is in validation.
    training = tf.placeholder(tf.bool, name='training')
    learnrate = tf.placeholder(tf.float32, name='learnrate')
        
    with tf.name_scope("normalize_pictures") as scope:                            
        images = hAPI.normalize_pictures(tensor=images_unnormalized)
#==============================================================================                                                 
# Graph                                           
#==============================================================================
    #Conv. Layer 7x7x64-s-2                                                  
    output_1 = hAPI.convLayerPretrained(tensor=images,layerNr=1,batchSize=batchSize, filterwidth=7, inputdepth=1, outputdepth=64, strides=2, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("2_Maxpool_Layer") as scope:
        output_2 = tf.nn.max_pool(output_1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME', name="maxpool_2")
    #Conv Layer 3x3x192
    output_3 = hAPI.convLayerPretrained(tensor=output_2,layerNr=3,batchSize=batchSize, filterwidth=3, inputdepth=64, outputdepth=192, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("4_Maxpool_Layer") as scope:
        output_4 = tf.nn.max_pool(output_3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME', name="maxpool_4")
    #Conv Layer 1x1x128
    output_5 = hAPI.convLayerPretrained(tensor=output_4,layerNr=5,batchSize=batchSize, filterwidth=1, inputdepth=192, outputdepth=128, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x256 
    output_6 = hAPI.convLayerPretrained(tensor=output_5,layerNr=6,batchSize=batchSize, filterwidth=3, inputdepth=128, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_7 = hAPI.convLayerPretrained(tensor=output_6,layerNr=7,batchSize=batchSize, filterwidth=1, inputdepth=256, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_8 = hAPI.convLayerPretrained(tensor=output_7,layerNr=8,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("9_Maxpool_Layer") as scope:
        output_9 = tf.nn.max_pool(output_8,ksize=[1,2,2,1],strides=[1,2,2,1], padding="SAME", name="maxpool_9")
    #Conv Layer 1x1x256
    output_10 = hAPI.convLayerPretrained(tensor=output_9,layerNr=10,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_11 = hAPI.convLayerPretrained(tensor=output_10,layerNr=11,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_12 = hAPI.convLayerPretrained(tensor=output_11,layerNr=12,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_13 = hAPI.convLayerPretrained(tensor=output_12,layerNr=13,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_14 = hAPI.convLayerPretrained(tensor=output_13,layerNr=14,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_15 = hAPI.convLayerPretrained(tensor=output_14,layerNr=15,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x256
    output_16 = hAPI.convLayerPretrained(tensor=output_15,layerNr=16,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=256, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x512
    output_17 = hAPI.convLayerPretrained(tensor=output_16,layerNr=17,batchSize=batchSize, filterwidth=3, inputdepth=256, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x512
    output_18 = hAPI.convLayerPretrained(tensor=output_17,layerNr=18,batchSize=batchSize, filterwidth=1, inputdepth=512, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_19 = hAPI.convLayerPretrained(tensor=output_18,layerNr=19,batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Maxpool Layer 2x2 -s-2
    with tf.name_scope("20_Maxpool_Layer") as scope:
        output_20 = tf.nn.max_pool(output_19,ksize=[1,2,2,1],strides=[1,2,2,1], padding="SAME", name="maxpool_20")
    #Conv Layer 1x1x512
    output_21 = hAPI.convLayerPretrained(tensor=output_20,layerNr=21,batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_22 = hAPI.convLayerPretrained(tensor=output_21,layerNr=22,batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 1x1x512
    output_23 = hAPI.convLayerPretrained(tensor=output_22,layerNr=23,batchSize=batchSize, filterwidth=1, inputdepth=1024, outputdepth=512, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_24 = hAPI.convLayerPretrained(tensor=output_23,layerNr=24,batchSize=batchSize, filterwidth=3, inputdepth=512, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training,origin_path=origin_path)
    #Conv Layer 3x3x1024
    output_25 = hAPI.convLayer(tensor=output_24,layerNr=25,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training)
    #Conv Layer 3x3x1024-s-2
    output_26 = hAPI.convLayer(tensor=output_25,layerNr=26,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=2, batchnorm_=batchnorm, dropout_=False,training=training)
    #Conv Layer 3x3x1024
    output_27 = hAPI.convLayer(tensor=output_26,layerNr=27,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training)
    #Conv Layer 3x3x1024
    output_28 = hAPI.convLayer(tensor=output_27,layerNr=28,batchSize=batchSize, filterwidth=3, inputdepth=1024, outputdepth=1024, strides=1, batchnorm_=batchnorm, dropout_=False,training=training)
    #Fully-Connected Layer ==> make vector
    with tf.name_scope("Layer31_full") as scope:
        input_31 = tf.reshape(tensor=output_28,shape=[batchSize,7*7*1024])
        if dropout == True:
            with tf.name_scope("dropout"):        
                #dropout only over all the feature-maps and batches.
                input_31 = tf.cond(training,
                                      lambda:tf.nn.dropout(x=input_31, keep_prob=0.8,noise_shape=[batchSize,7*7*1024]),
                                      lambda:input_31)
        W31 = tf.Variable(tf.truncated_normal(shape=[7*7*1024,4096],stddev=0.01,dtype=tf.float32),name="W31")
        b31 = tf.Variable(tf.truncated_normal(shape=[4096],stddev=0.01,dtype=tf.float32),name="b31")
        preactivate_31 = tf.add(tf.matmul(input_31,W31),b31)
        with tf.name_scope("batch_norm"):
            input_depth_31 = preactivate_31.get_shape().as_list()[-1]#takes the last element which is in this case 64
            #make new weights and new bias
            with tf.name_scope("beta"):
                beta31 = tf.Variable(tf.constant(0.0,shape=[input_depth_31],dtype=tf.float32), name="beta",trainable=True)
            with tf.name_scope("gamma"):
                gamma31 = tf.Variable(tf.constant(1.0,shape=[input_depth_31],dtype=tf.float32),name="gamma",trainable=True)
            batch_mean31, batch_variance31 = tf.nn.moments(x=preactivate_31,axes=[0,1])
            
            preactivate_31 = tf.nn.batch_normalization(x=preactivate_31,mean=batch_mean31,variance=batch_variance31,offset=beta31,scale=gamma31,variance_epsilon=1e-4,name=None) 
        output_31 = tf.nn.relu(preactivate_31)
        with tf.name_scope("summary"):
            hAPI.variable_summaries(variable=W31,name="W31")
            hAPI.variable_summaries(variable=b31,name="b31")
            hAPI.variable_summaries(variable=preactivate_31, name="preactivate31")
            hAPI.variable_summaries(variable=output_31, name="output31")
    #Fully-Connected Layer ==> make tensor again.
    with tf.name_scope("Layer32_full") as scope:
        if dropout == True:
            with tf.name_scope("dropout"):        
                #dropout only over all the feature-maps and batches.
                output_31 = tf.cond(training,
                                      lambda:tf.nn.dropout(x=output_31, keep_prob=0.8,noise_shape=[batchSize,4096]),
                                      lambda:output_31)
        W32 = tf.Variable(tf.truncated_normal(shape=[4096,7*7*6],stddev=0.01,dtype=tf.float32),name="W32")
        b32 = tf.Variable(tf.truncated_normal(shape=[7*7*6],stddev=0.01,dtype=tf.float32),name="b32")
        preactivate_32 = tf.add(tf.matmul(output_31,W32),b32)
        with tf.name_scope("batch_norm"):
            input_depth_32 = preactivate_32.get_shape().as_list()[-1]#takes the last element which is in this case 64
            #make new weights and new bias
            with tf.name_scope("beta"):
                beta32 = tf.Variable(tf.constant(0.0,shape=[input_depth_32],dtype=tf.float32), name="beta",trainable=True)
            with tf.name_scope("gamma"):
                gamma32 = tf.Variable(tf.constant(1.0,shape=[input_depth_32],dtype=tf.float32),name="gamma",trainable=True)
            batch_mean32, batch_variance32 = tf.nn.moments(x=preactivate_32,axes=[0,1])
            preactivate_32 = tf.nn.batch_normalization(x=preactivate_32,mean=batch_mean32,variance=batch_variance32,offset=beta32,scale=gamma32,variance_epsilon=1e-4,name=None)   
        fully_32 = preactivate_32#tf.nn.relu(preactivate_32)
        output_32 = tf.sigmoid(tf.reshape(tensor=fully_32, shape=[batchSize,7,7,6]))
        output_32 = tf.cast(output_32,tf.float32)
        with tf.name_scope("summary"):                        
            hAPI.variable_summaries(variable=W32,name="W32")
            hAPI.variable_summaries(variable=b32,name="b32")
            hAPI.variable_summaries(variable=preactivate_32, name="preactivate32")
            hAPI.variable_summaries(variable=output_32, name="output32")
            
        
    with tf.name_scope("cost_function") as scope:
        x1_output = tf.squeeze(output_32[:,:,:,0])
        x_label   = tf.squeeze(labels[:,:,:,0])

        y1_output = tf.squeeze(output_32[:,:,:,1])
        y_label   = tf.squeeze(labels[:,:,:,1])

        h1_output = tf.squeeze(output_32[:,:,:,2])
        h_label  = tf.squeeze(labels[:,:,:,2])
        
        w1_output = tf.squeeze(output_32[:,:,:,3])
        w_label   = tf.squeeze(labels[:,:,:,3])

        c1_output = tf.squeeze(output_32[:,:,:,4])
        
        p_output  = tf.squeeze(output_32[:,:,:,5])
        p_label   = tf.squeeze(labels[:,:,:,4])
        
        with tf.name_scope("x-costs"):
            x1_difference = tf.subtract(x1_output,x_label)           
            x1_squared = tf.square(x1_difference)
            x1_costs = tf.multiply(p_label,x1_squared)
            x1_costs = tf.reduce_sum(x1_costs)
            x1_costs = tf.multiply(x1_costs,5)#like in the yolo-paper...
        with tf.name_scope("y-costs"):
            y1_difference = tf.subtract(y1_output,y_label)           
            y1_squared = tf.square(y1_difference)
            y1_costs = tf.multiply(p_label,y1_squared)
            y1_costs = tf.reduce_sum(y1_costs)
            y1_costs = tf.multiply(y1_costs,5)#like in the yolo-paper...
        with tf.name_scope("h-costs"):
            h1_output_root = tf.sqrt(h1_output)
            h_label_root = tf.sqrt(h_label)
            h1_difference = tf.subtract(h1_output_root,h_label_root)
            h1_squared = tf.square(h1_difference)
            h1_costs = tf.multiply(p_label,h1_squared)
            h1_costs = tf.reduce_sum(h1_costs)
            h1_costs = tf.multiply(h1_costs,5)#like in the yolo-paper...
        with tf.name_scope("w-costs"):
            w1_output_root = tf.sqrt(w1_output)
            w_label_root = tf.sqrt(w_label)
            w1_difference = tf.subtract(w1_output_root,w_label_root)
            w1_squared = tf.square(w1_difference)
            w1_costs = tf.multiply(p_label,w1_squared)
            w1_costs = tf.reduce_sum(w1_costs)
            w1_costs = tf.multiply(w1_costs,5)
        with tf.name_scope("C-costs"):
            w_label_half = tf.div(w_label,2)
            h_label_half = tf.div(h_label,2)

            w1_output_half= tf.div(w1_output,2)
            h1_output_half= tf.div(h1_output,2)
            
            label_left = tf.subtract(x_label,w_label_half)
            label_right= tf.add(x_label,w_label_half)
            label_top = tf.subtract(y_label,h_label_half)
            label_bottom=tf.add(y_label,h_label_half)
            
            output1_left = tf.subtract(x1_output,w1_output_half)
            output1_right= tf.add(x1_output,w1_output_half)
            output1_top = tf.subtract(y1_output,h1_output_half)
            output1_bottom= tf.add(y1_output,h1_output_half)

            overlap1_left = tf.maximum(label_left,output1_left)
            overlap1_right= tf.minimum(label_right,output1_right)
            overlap1_top = tf.maximum(label_top,output1_top)
            overlap1_bottom= tf.minimum(label_bottom,output1_bottom)
            
            overlap1_width = tf.subtract(overlap1_right,overlap1_left)
            overlap1_width = tf.maximum(np.float32(0),overlap1_width)
            overlap1_height = tf.subtract(overlap1_bottom,overlap1_top)
            overlap1_height = tf.maximum(np.float32(0),overlap1_height)
            area_of_overlap1 = tf.multiply(overlap1_width, overlap1_height)
            
            output1_area = tf.multiply(h1_output,w1_output)
            label_area = tf.multiply(h_label,w_label)
            
            area_of_union1 = tf.subtract(tf.add(output1_area,label_area),area_of_overlap1)
            
            IoU1 = tf.div(area_of_overlap1,area_of_union1)
            IoU1_scalar = tf.reduce_mean(IoU1)            
            
            c1_label = tf.multiply(IoU1,p_label)#with more than one finger use here p_label=max(p_label1,p_label2,...)
                                                
            c1_difference = tf.subtract(c1_output,c1_label)
            c1_squared = tf.square(c1_difference)
            
            with tf.name_scope("obj_present"):
                c1_obj = tf.multiply(c1_squared, p_label)
                c1_obj_costs = tf.reduce_sum(c1_obj)
            with tf.name_scope("noobj_present"):
                p_label_invers = tf.subtract(np.float32(1),p_label)# y=1-x   x=0|y=1  & x=1|y=0
                c1_noobj = tf.multiply(c1_squared, p_label_invers)
                c1_noobj_costs = tf.reduce_sum(c1_noobj)
                c1_noobj_costs = tf.multiply(c1_noobj_costs,0.5)#like in the yolo paper...
        with tf.name_scope("p_costs"):
            p_difference = tf.subtract(p_output,p_label)
            p_squared = tf.square(p_difference)
            p_costs = tf.multiply(p_label,p_squared)
            p_costs = tf.reduce_sum(p_costs)
        
        center_costs = tf.add(x1_costs,y1_costs)        
        box_costs = tf.add(h1_costs,w1_costs)
        conf_costs = tf.add(c1_obj_costs,c1_noobj_costs)
        spatial_costs = tf.add(center_costs,box_costs)
        prob_costs = tf.add(conf_costs,p_costs)
        costs = tf.add(spatial_costs,prob_costs)
        
        tf.summary.scalar("Costx",x1_costs)
        tf.summary.scalar("Costy",y1_costs)
        tf.summary.scalar("Costh",h1_costs)
        tf.summary.scalar("Costw",w1_costs)
        tf.summary.scalar("IoU_mean",IoU1_scalar)
        tf.summary.scalar("CostcObj",c1_obj_costs)
        tf.summary.scalar("CostcNoObj",c1_noobj_costs)
        tf.summary.scalar("Costp",p_costs)
        
        tf.summary.scalar("CostCenter",center_costs)
        tf.summary.scalar("CostBox",box_costs)
        tf.summary.scalar("CostConf",conf_costs)
        
        tf.summary.scalar("CostSpatial",spatial_costs)
        tf.summary.scalar("CostProb",prob_costs)
        
        tf.summary.scalar("Costs",costs)

        
    with tf.name_scope("optimizer") as scope:

        optimizer = tf.train.AdamOptimizer(learning_rate=learnrate,epsilon=1e-04)

        grads_and_vars = optimizer.compute_gradients(costs)
        capped_gvs = [(tf.clip_by_value(grad, -5., 5.), var) for grad, var in grads_and_vars]
        
        # Ask the optimizer to apply the capped gradients.
        train_step = optimizer.apply_gradients(capped_gvs)

    #Save Gradients in Tensorboard
    for grad, var in capped_gvs:
        if grad is not None:
            tf.summary.histogram(var.op.name +"capped_gradients",grad)
        
    with tf.name_scope("Test") as scope:        
        with tf.name_scope("IOU"):
            nr_of_fingers_in_batch = tf.reduce_sum(p_label)
            
            y_label_index = tf.argmax(tf.reduce_max(p_label,axis=[2]),axis=1)
            x_label_index  = tf.argmax(tf.reduce_max(p_label,axis=[1]),axis=1)
            y_label_global = tf.add((tf.cast(y_label_index,tf.float32)/7) , (tf.reduce_max((y_label/7),axis=[1,2])))
            x_label_global = tf.add((tf.cast(x_label_index,tf.float32)/7) , (tf.reduce_max((x_label/7),axis=[1,2])))
            h_label_global = tf.reduce_max(h_label,axis=[1,2])
            w_label_global = tf.reduce_max(w_label,axis=[1,2])
            
            probconf_output = tf.multiply(p_output,c1_output)            
            y_probconf_index = tf.argmax(tf.reduce_max(probconf_output, axis=[2]),axis=1)
            x_probconf_index = tf.argmax(tf.reduce_max(probconf_output, axis=[1]),axis=1)
            
            
            #make tensor to extract the values for x,y,w & h at the point with highest prob/conf
            enumerator = tf.constant(range(batchSize),tf.int64)
            probconf_indices_tensor = tf.stack([enumerator,y_probconf_index,x_probconf_index],1)
            
            y_probconf_coarse = tf.cast(y_probconf_index,tf.float32)/7            
            y_probconf_fine = tf.squeeze(tf.gather_nd(y1_output,probconf_indices_tensor))/7
            y_probconf = tf.add(y_probconf_coarse, y_probconf_fine)
            x_probconf_coarse = tf.cast(x_probconf_index,tf.float32)/7
            x_probconf_fine = tf.squeeze(tf.gather_nd(x1_output,probconf_indices_tensor))/7
            x_probconf = tf.add(x_probconf_coarse, x_probconf_fine)
            h_probconf =  tf.squeeze(tf.gather_nd(h1_output,probconf_indices_tensor))
            w_probconf =  tf.squeeze(tf.gather_nd(w1_output,probconf_indices_tensor))            

            iou_max_prob_conf = hAPI.iou(x_label_global,y_label_global,h_label_global,w_label_global,y_probconf,x_probconf,h_probconf,w_probconf)
            
            iou_max_probconf_mean = tf.reduce_sum(iou_max_prob_conf)/nr_of_fingers_in_batch
            

            tf.summary.scalar("iou_max_probconf",iou_max_probconf_mean)
            
        with tf.name_scope("distance"):
            #only test the pictures with fingers on distance, those, which don't have a finger, mustn't be tested!!!
            p_labels = tf.reduce_sum(p_label,axis=[1,2])
            x_probconfs = tf.multiply(x_probconf,p_labels)
            y_probconfs = tf.multiply(y_probconf,p_labels)
            x_labels_global = tf.multiply(x_label_global,p_labels)
            y_labels_global = tf.multiply(y_label_global,p_labels)
            
            x_probconf_diff = tf.subtract(x_labels_global, x_probconfs)
            y_probconf_diff = tf.subtract(y_labels_global, y_probconfs)            
            
            distance_probconf = tf.sqrt(tf.add(tf.square(x_probconf_diff),tf.square(y_probconf_diff)))
            #Problem: when only testing distance on pictures with fingers, the pictures without fingers have distance 0, what makes a bias to the mean-distance.
            #Solution: Make new vectors, which doesn't include Pictures, where no finger exist.
            zeros_vector = tf.zeros(batchSize)
            boolean_vector = tf.squeeze(tf.not_equal(zeros_vector,p_labels))
            boolean_vector.set_shape([None])#to prevent problems with tf.boolean_mask()
            distance_probconfs = tf.boolean_mask(distance_probconf,boolean_vector)
            
            mean_distance_probconf, var_distance_probconf = tf.nn.moments(distance_probconfs, axes=[0])
            
            tf.summary.scalar("mean_distance_probconf",mean_distance_probconf)
            tf.summary.scalar("var_distance_probconf",var_distance_probconf)
                        
    
    init_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
    #initialize the Saver, which organizes the Saving of all Weights
    saver = tf.train.Saver(
        max_to_keep=5,
        keep_checkpoint_every_n_hours=4.0, 
        pad_step_number=True,
        save_relative_paths=True,)
    #Make a own directory for the weights of this Training.
    if not os.path.exists(origin_path + "../../../weights/"+name+"/"):
        os.makedirs(origin_path + "../../../weights/"+name+"/")

    
    merged_summary_op = tf.summary.merge_all()

    with tf.Session() as sess:
        #Initialize different Datasets
        sess.run(testing_init_op)
        sess.run(validation_init_op)
        sess.run(training_init_op)
        sess.run(init_op)
                
        if(test==False):
            #initialize three different Tensorflow-Summaries
            train_train_writer = tf.summary.FileWriter(origin_path + "../../../summarys/training/summary_" + name + "_traintrain")
            train_train_writer.add_graph(sess.graph)             
            train_writer=tf.summary.FileWriter(origin_path + "../../../summarys/training/summary_" + name + "_train")
            train_writer.add_graph(sess.graph) 
            valid_writer=tf.summary.FileWriter(origin_path + "../../../summarys/training/summary_" + name + "_valid")
            valid_writer.add_graph(sess.graph) 
            


            #saver.restore(sess=sess, save_path=origin_path + "../../../weights/7BnormBeforeRelu2.ckpt-00103000")
            print("start training....\n")
            for i in range(nr_of_epochs/nr_of_epochs_until_save_model):
                for j in range(nr_of_epochs_until_save_model):
                    _ = sess.run([train_step],feed_dict={training: True, learnrate : learning_rate})                        
                
                #testing in traindata while training
                numbers_of_iterations_until_now = i*nr_of_epochs_until_save_model+j+1 
                _,sumsum = sess.run([train_step,merged_summary_op],feed_dict={training: True, learnrate : learning_rate})                
                train_train_writer.add_summary(sumsum,(numbers_of_iterations_until_now))                
                
                #testing on traindata
                train_writer.add_summary(sess.run(merged_summary_op,feed_dict={training: False, learnrate : learning_rate}),(numbers_of_iterations_until_now))

                #testing on validationdata:
                sess.run(validation_init_op)
                valid_writer.add_summary(sess.run(merged_summary_op,feed_dict={training: False, learnrate : learning_rate}),(numbers_of_iterations_until_now))            
                sess.run(training_init_op)
                
                #save Model
                saver.save(sess=sess, save_path=origin_path + "../../../weights/"+name+"/"+name+".ckpt", global_step=(numbers_of_iterations_until_now))
                print("model updatet\n")

                #training:


        else:
            #Make directory to store Results in Pictures and Histogramms
            if not os.path.exists(origin_path +"picsRecognized/"):
                os.makedirs(origin_path +"picsRecognized/")             
            
            sess.run(testing_init_op)
            #Restore Weights for Testing
            #only needed, if Test is done without Training.
            print("Try to restore")
            saver.restore(sess,origin_path + "../../../weights/139_9000Set_newPipe/139_9000Set_newPipe.ckpt-00510000")                
            print("Restored")  
            
            #initialize running Variables
            nr_of_fingers = 0
            nr_of_coarse_recognized_fingers = 0
            nr_of_recognized_fingers = 0
            nr_of_fine_recognized_fingers = 0
            nr_of_extremefine_recognized_fingers = 0
            iou_array = []
            distance_array = []
            distance_x_array = []
            distance_y_array = []
            for i in range(len(test_picnames)/batchSize):
                #let one Batch flow through the Graph
                (testimages,
                 probconfs_y,
                 probconfs_x,
                 probconfs_h,
                 probconfs_w,
                 labels_y,
                 labels_x,
                 labels_h,
                 labels_w,
                 probconfs_iou,
                 probconfs_distance,
                 labels_p,
                 probconfs_x_diff, 
                 probconfs_y_diff)  = sess.run([images_unnormalized,
                                                y_probconf,
                                                x_probconf,
                                                h_probconf,
                                                w_probconf,
                                                y_label_global,
                                                x_label_global,
                                                h_label_global,
                                                w_label_global,
                                                iou_max_prob_conf,
                                                distance_probconf,
                                                p_labels,
                                                x_probconf_diff,
                                                y_probconf_diff],   feed_dict={training: False})
                                                
                #Analyze every Element of the Batch
                for b in range(batchSize):
                    print(str(i*batchSize+b))
                    testimage = testimages[b]*200
                    probconf_y  = probconfs_y[b] * 448
                    probconf_x  = probconfs_x[b] * 448
                    probconf_h  = probconfs_h[b] * 448
                    probconf_w  = probconfs_w[b] * 448
                    probconf_iou= probconfs_iou[b]
                    probconf_distance=probconfs_distance[b]
                    probconf_x_diff = probconfs_x_diff[b]
                    probconf_y_diff = probconfs_y_diff[b]
                    label_y     = labels_y[b] * 448
                    label_x     = labels_x[b] * 448
                    label_h     = labels_h[b] * 448
                    label_w     = labels_w[b] * 448
                    print("iou                                                      = "+str(probconf_iou))
                    print("distance                                                 = " + str(probconf_distance))
                    if(labels_p[b]!=0):
                            
                        #make array with all valid iou's and distances, to make later histograms, variances, etc.
                        iou_array = np.concatenate((iou_array,[probconf_iou]))
                        distance_array = np.concatenate((distance_array,[probconf_distance]))
                        distance_x_array = np.concatenate((distance_x_array,[probconf_x_diff]))
                        distance_y_array = np.concatenate((distance_y_array,[probconf_y_diff]))

                        nr_of_fingers += 1

                        print("mean_iou                                                 = " + str(np.mean(iou_array)))
                        print("mean_distance                                            = " + str(np.mean(distance_array)))
                        
                        if probconf_distance < 0.04:
                            nr_of_coarse_recognized_fingers += 1
                        if probconf_distance < 0.02:
                            nr_of_recognized_fingers += 1
                        if probconf_distance < 0.01:
                            nr_of_fine_recognized_fingers += 1
                        if probconf_distance < 0.001:
                            nr_of_extremefine_recognized_fingers += 1
                        
                        coarse_recognized_fingers_in_percent = (float(nr_of_coarse_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of coarse recognized fingers (<0.04)          = " + str(coarse_recognized_fingers_in_percent) + "%")
                        recognized_fingers_in_percent = (float(nr_of_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of recognized fingers (<0.02)                 = " + str(recognized_fingers_in_percent) + "%")
                        fine_recognized_fingers_in_percent = (float(nr_of_fine_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of fine recognized fingers (<0.01)            = " + str(fine_recognized_fingers_in_percent) + "%")
                        extremefine_recognized_fingers_in_percent = (float(nr_of_extremefine_recognized_fingers)/float(nr_of_fingers))*100
                        print("Percentage of extreme fine recognized fingers (>0.001)   = " + str(extremefine_recognized_fingers_in_percent)+"%")
                        
                        
   
                    #save picture in own folder for recognized fingers 
                    sess.run(tf.write_file(origin_path+"picsRecognized/pic" + str(batchSize*i+b)+".png",tf.image.encode_png(testimage)))
                    
                    #get picture back with cv2
                    img = cv2.imread(origin_path+"picsRecognized/pic" + str(batchSize*i+b)+".png")

                    cv2.rectangle(img,(int(probconf_x-(probconf_w/2)),int(probconf_y-(probconf_h/2))),(int(probconf_x+(probconf_w/2)),int(probconf_y+(probconf_h/2))),(0,0,255),1)     
                    cv2.putText(img,'Prediction',(int(probconf_x-(probconf_w/2)),int(probconf_y-(probconf_h/2))),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),1)
                    cv2.putText(img,'iou='+str(probconf_iou),(0,25),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),1)
                    cv2.putText(img,'dist='+str(probconf_distance),(0,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),1)
                    cv2.rectangle(img,(int(label_x-(label_w/2)),int(label_y-(label_h/2))),(int(label_x+(label_w/2)),int(label_y+(label_h/2))),(255,255,0),1)     
                    cv2.putText(img,'Label',(int(label_x-(label_w/2)),int(label_y-(label_h/2))),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),1)                    
                    cv2.imwrite(origin_path+"picsRecognized/pic" + str(batchSize*i+b)+".png",img)
                
            #At the End of the Test-Sequence:
            #Here shall the whole median, mean, variance and histograms of the iou and the distance be calculated and plottet to PDF!!
            plt.hist(iou_array,bins=np.arange(0.0,1.0,0.01),normed=1)
            plt.title("IOU Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("IOU (median="+str(round(np.median(iou_array),3))+", mean="+str(round(np.mean(iou_array),3))+", stdev="+str(round(np.std(iou_array),3))+")")
            plt.ylabel("Probability in % [1/100]")
            plt.plot([0.4,0.4],[0,10], 'orange')
            plt.text(0.3, 9, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(0.45,9, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path+"picsRecognized/IOUprobDensity.pdf")
            plt.close()
            
            plt.hist(distance_array,bins=np.arange(0.0,1.0,0.01),normed=1)
            plt.title("Label/Prediction-Center-Distance Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("Distance (median="+str(round(np.median(distance_array),3))+", mean="+str(round(np.mean(distance_array),3))+", stdev="+str(round(np.std(distance_array),3))+") \n max. possible distance = sqrt(2)")
            plt.ylabel("Probability in % [1/100]")
            plt.plot([0.02,0.02],[0,60], 'orange')
            plt.text(0.05, 55, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07,55, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path+"picsRecognized/distProbDensity.pdf")
            plt.close()  
            
            plt.hist(distance_array,bins=np.arange(0.0,1.0,0.01),normed=1)
            plt.title("Label/Prediction-Center-Distance Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("Distance (median="+str(round(np.median(distance_array),3))+", mean="+str(round(np.mean(distance_array),3))+", stdev="+str(round(np.std(distance_array),3))+") \n max. possible distance = sqrt(2)")
            plt.ylabel("Probability in % [1/100] (Log-Scale)")
            plt.plot([0.02,0.02],[0,100], 'orange')
            plt.text(0.05, 55, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.07,55, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.yscale('log')
            plt.savefig(origin_path+"picsRecognized/logdistProbDensity.pdf")
            plt.close() 
            
            distance_array_without_outliers = []
            for dist in distance_array:
                if dist < 0.25:
                    distance_array_without_outliers = np.concatenate((distance_array_without_outliers,[dist]))
            plt.hist(distance_array_without_outliers,bins=np.arange(0.0,0.25,0.001),normed=1)
            plt.title("Label/Prediction-Center-Distance Probability-Density \n on Testset ("+str(nr_of_fingers)+"Pictures) Without Outliers(Cut at 0.25)")
            plt.xlabel("Distance (median="+str(round(np.median(distance_array_without_outliers),3))+", mean="+str(round(np.mean(distance_array_without_outliers),3))+", stdev="+str(round(np.std(distance_array_without_outliers),3))+")")
            plt.ylabel("Probability in Promille [1/1000]")
            plt.plot([0.02,0.02],[0,100], 'orange')
            plt.text(0.025,90, "bad", bbox=dict(facecolor='red', alpha=0.5))
            plt.text(-0.002,90, "good", bbox=dict(facecolor='green', alpha=0.5))
            plt.savefig(origin_path+"picsRecognized/distProbDensity_improved.pdf")
            plt.close() 

            plt.hist(distance_x_array,bins=np.arange(-0.125,0.125,0.001),normed=1)
            plt.title("X-Distance \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("x-Distance (median="+str(round(np.median(distance_x_array),3))+", mean="+str(round(np.mean(distance_x_array),3))+", stdev="+str(round(np.std(distance_x_array),3))+")")
            plt.ylabel("Probability/Distance")
            plt.plot([0.0,0.0],[0,100], 'orange')
            plt.savefig(origin_path+"picsRecognized/xDistance.pdf")
            plt.close()    

            plt.hist(distance_y_array,bins=np.arange(-0.125,0.125,0.001),normed=1)
            plt.title("Y-Distance \n on Testset ("+str(nr_of_fingers)+"Pictures)")
            plt.xlabel("y-Distance (median="+str(round(np.median(distance_y_array),3))+", mean="+str(round(np.mean(distance_y_array),3))+", stdev="+str(round(np.std(distance_y_array),3))+")")
            plt.ylabel("Probability/Distance")
            plt.plot([0.0,0.0],[0,100], 'orange')
            plt.savefig(origin_path+"picsRecognized/yDistance.pdf")
            plt.close()              

    print("finished")

    


if __name__ == "__main__":
   
    main()
