tensorflow_二分类之tensorboard可视化
import dataset
import tensorflow as tf
import time
from datetime import timedelta
import math
import random
import numpy as np
import os

#Adding Seed so that random initialization is consistent
from numpy.random import seed
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)


batch_size = 32

#Prepare input data
classes = os.listdir('training_data')
num_classes = len(classes)

# 20% of the data will automatically be used for validation
validation_size = 0.2
img_size =128 #=crop_size
num_channels = 3
train_path='training_data'

# We shall load all the training and validation images and labels into memory using openCV and use that during training
data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)


print("Complete reading input data. Will Now print a snippet of it")
print("Number of files in Training-set:\t\t{}".format(len(data.train.labels)))
print("Number of files in Validation-set:\t{}".format(len(data.valid.labels)))



session = tf.Session()


x = tf.placeholder(tf.float32, shape=[None, img_size,img_size,num_channels],name='x')

## labels
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)



##Network graph params
filter_size_conv1 = 3
num_filters_conv1 = 32

filter_size_conv2 = 3
num_filters_conv2 = 32

filter_size_conv3 = 3
num_filters_conv3 = 64
    
fc_layer_size = 128
tf.merged=None#tensorboard
def create_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05)) #weight_decay

def create_biases(size):
    return tf.Variable(tf.constant(0.05, shape=[size]))



def create_convolutional_layer(input,
               num_input_channels,
               conv_filter_size,        
               num_filters):  
   with tf.name_scope('input'):
    ## We shall define the weights that will be trained using create_weights function.
           weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])
           tf.summary.histogram('weights',weights)#tensorboard
       ## We create biases using the create_biases function. These are also trained.
           biases = create_biases(num_filters)
           tf.summary.histogram('biases',biases)#tensorboard
    ## Creating the convolutional layer
           layer = tf.nn.conv2d(input=input,
                     filter=weights,
                     strides=[1, 1, 1, 1],
                     padding='SAME')

           layer += biases

    ## We shall be using max-pooling.  
           layer = tf.nn.max_pool(value=layer,
                            ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1],
                            padding='SAME')
    ## Output of pooling is fed to Relu which is the activation function for us.
           layer = tf.nn.relu(layer)

   return layer

    

def create_flatten_layer(layer):
    #We know that the shape of the layer will be [batch_size img_size img_size num_channels]
    # But let's get it from the previous layer.
    layer_shape = layer.get_shape()

    ## Number of features will be img_height * img_width* num_channels. But we shall calculate it in place of hard-coding it.
    num_features = layer_shape[1:4].num_elements()

    ## Now, we Flatten the layer so we shall have to reshape to num_features
    layer = tf.reshape(layer, [-1, num_features])

    return layer


def create_fc_layer(input,          
             num_inputs,    
             num_outputs,
             use_relu='True'):
   with tf.name_scope('fc'):#tensorboard
    #Let's define trainable weights and biases.
            weights = create_weights(shape=[num_inputs, num_outputs])
            biases = create_biases(num_outputs)
            tf.summary.histogram('weights',weights)#tensorboard
            tf.summary.histogram('biases',biases)#tensorboard

    # Fully connected layer takes input x and produces wx+b.Since, these are matrices, we use matmul function in Tensorflow
            layer = tf.matmul(input, weights) + biases
            if use_relu:
                layer = tf.nn.relu(layer)

   return layer


layer_conv1 = create_convolutional_layer(input=x,
               num_input_channels=num_channels,
               conv_filter_size=filter_size_conv1,
               num_filters=num_filters_conv1)
layer_conv2 = create_convolutional_layer(input=layer_conv1,
               num_input_channels=num_filters_conv1,
               conv_filter_size=filter_size_conv2,
               num_filters=num_filters_conv2)

layer_conv3= create_convolutional_layer(input=layer_conv2,
               num_input_channels=num_filters_conv2,
               conv_filter_size=filter_size_conv3,
               num_filters=num_filters_conv3)
          
layer_flat = create_flatten_layer(layer_conv3)

layer_fc1 = create_fc_layer(input=layer_flat,
                     num_inputs=layer_flat.get_shape()[1:4].num_elements(),
                     num_outputs=fc_layer_size,
                     use_relu=True)

layer_fc2 = create_fc_layer(input=layer_fc1,
                     num_inputs=fc_layer_size,
                     num_outputs=num_classes,
                     use_relu=False)

y_pred = tf.nn.softmax(layer_fc2,name='y_pred')

y_pred_cls = tf.argmax(y_pred, dimension=1)
session.run(tf.global_variables_initializer())
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,labels=y_true)
cost = tf.reduce_mean(cross_entropy)
tf.summary.scalar('cost',cost)#tensorboard
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)#base_lr
correct_prediction = tf.equal(y_pred_cls, y_true_cls)

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('accuracy',accuracy)
tf.merged = tf.summary.merge_all()#tensorboard zaichushihuasuoyoubianliangzhiqian
session.run(tf.global_variables_initializer())
writer=tf.summary.FileWriter('tensorboard/test2', session.graph)#tensorboard rizhiwenjian

def show_progress(epoch, feed_dict_train, feed_dict_validate, val_loss):
    with tf.name_scope('accuracy'):#tensorboard
        acc = session.run(accuracy, feed_dict=feed_dict_train)
        val_acc = session.run(accuracy, feed_dict=feed_dict_validate)
        msg = "Training Epoch {0} --- Training Accuracy: {1:>6.1%}, Validation Accuracy: {2:>6.1%},  Validation Loss: {3:.3f}"
        print(msg.format(epoch + 1, acc, val_acc, val_loss))

total_iterations = 0

saver = tf.train.Saver()
def train(num_iteration):
    global total_iterations
    
    
    for i in range(total_iterations,
                   total_iterations + num_iteration):

        x_batch, y_true_batch, _, cls_batch = data.train.next_batch(batch_size)
        x_valid_batch, y_valid_batch, _, valid_cls_batch = data.valid.next_batch(batch_size)

        
        feed_dict_tr = {x: x_batch,
                           y_true: y_true_batch}
        feed_dict_val = {x: x_valid_batch,
                              y_true: y_valid_batch}

        _, summary=session.run([optimizer,tf.merged ],feed_dict=feed_dict_tr)
        
        writer.add_summary(summary, i)
        if i % int(data.train.num_examples/batch_size) == 0:
               val_loss = session.run(cost, feed_dict=feed_dict_val)
               epoch = int(i / int(data.train.num_examples/batch_size))    
            
            
               show_progress(epoch, feed_dict_tr, feed_dict_val, val_loss)
               saver.save(session, 'dogs-cats-model')
        
            
        total_iterations += num_iteration
    writer.close()#tensorboard

train(num_iteration=3000)

2)解析

构建log日志文件,在session之后的合适位置writer=tf.summary.FileWriter('tensorboard/test2', session.graph);

运行代码之后,会在tensorboard/test2文件下生成一个日志文件,然后在命令行中输入tensorboard --logdir tensorboard/test2

点开命令行输出的网址,即可打开以下页面:

可以看到graph图。

2)可以精简图中节点的名称,可以把一个模块定一个总的名字。

具体做法是在模块的上面使用tf.name_scope('名字'):

如:def create_convolutional_layer(input,
               num_input_channels,
               conv_filter_size,        
               num_filters):  
   with tf.name_scope('input'):
    ## We shall define the weights that will be trained using create_weights function.
           weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])
           tf.summary.histogram('weights',weights)#tensorboard
       ## We create biases using the create_biases function. These are also trained.
           biases = create_biases(num_filters)
           tf.summary.histogram('biases',biases)#tensorboard
    ## Creating the convolutional layer
           layer = tf.nn.conv2d(input=input,
                     filter=weights,
                     strides=[1, 1, 1, 1],
                     padding='SAME')

           layer += biases

    ## We shall be using max-pooling.  
           layer = tf.nn.max_pool(value=layer,
                            ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1],
                            padding='SAME')
    ## Output of pooling is fed to Relu which is the activation function for us.
           layer = tf.nn.relu(layer)

   return layer

    

def create_flatten_layer(layer):
    #We know that the shape of the layer will be [batch_size img_size img_size num_channels]
    # But let's get it from the previous layer.
    layer_shape = layer.get_shape()

    ## Number of features will be img_height * img_width* num_channels. But we shall calculate it in place of hard-coding it.
    num_features = layer_shape[1:4].num_elements()

    ## Now, we Flatten the layer so we shall have to reshape to num_features
    layer = tf.reshape(layer, [-1, num_features])

    return layer


def create_fc_layer(input,          
             num_inputs,    
             num_outputs,
             use_relu='True'):
   with tf.name_scope('fc'):#tensorboard
    #Let's define trainable weights and biases.
            weights = create_weights(shape=[num_inputs, num_outputs])
            biases = create_biases(num_outputs)
            tf.summary.histogram('weights',weights)#tensorboard
            tf.summary.histogram('biases',biases)#tensorboard

    # Fully connected layer takes input x and produces wx+b.Since, these are matrices, we use matmul function in Tensorflow
            layer = tf.matmul(input, weights) + biases
            if use_relu:
                layer = tf.nn.relu(layer)

   return layer


layer_conv1 = create_convolutional_layer(input=x,
               num_input_channels=num_channels,
               conv_filter_size=filter_size_conv1,
               num_filters=num_filters_conv1)
layer_conv2 = create_convolutional_layer(input=layer_conv1,
               num_input_channels=num_filters_conv1,
               conv_filter_size=filter_size_conv2,
               num_filters=num_filters_conv2)

layer_conv3= create_convolutional_layer(input=layer_conv2,
               num_input_channels=num_filters_conv2,
               conv_filter_size=filter_size_conv3,
               num_filters=num_filters_conv3)
          
layer_flat = create_flatten_layer(layer_conv3)

layer_fc1 = create_fc_layer(input=layer_flat,
                     num_inputs=layer_flat.get_shape()[1:4].num_elements(),
                     num_outputs=fc_layer_size,
                     use_relu=True)

layer_fc2 = create_fc_layer(input=layer_fc1,
                     num_inputs=fc_layer_size,
                     num_outputs=num_classes,
                     use_relu=False)

y_pred = tf.nn.softmax(layer_fc2,name='y_pred')

y_pred_cls = tf.argmax(y_pred, dimension=1)
session.run(tf.global_variables_initializer())
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,labels=y_true)
cost = tf.reduce_mean(cross_entropy)
tf.summary.scalar('cost',cost)#tensorboard
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)#base_lr
correct_prediction = tf.equal(y_pred_cls, y_true_cls)

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('accuracy',accuracy)
tf.merged = tf.summary.merge_all()#tensorboard zaichushihuasuoyoubianliangzhiqian
session.run(tf.global_variables_initializer())
writer=tf.summary.FileWriter('tensorboard/test2', session.graph)#tensorboard rizhiwenjian

def show_progress(epoch, feed_dict_train, feed_dict_validate, val_loss):
    with tf.name_scope('accuracy'):#tensorboard
        acc = session.run(accuracy, feed_dict=feed_dict_train)
        val_acc = session.run(accuracy, feed_dict=feed_dict_validate)
        msg = "Training Epoch {0} --- Training Accuracy: {1:>6.1%}, Validation Accuracy: {2:>6.1%},  Validation Loss: {3:.3f}"
        print(msg.format(epoch + 1, acc, val_acc, val_loss))

total_iterations = 0

saver = tf.train.Saver()
def train(num_iteration):
    global total_iterations
    
    
    for i in range(total_iterations,
                   total_iterations + num_iteration):

        x_batch, y_true_batch, _, cls_batch = data.train.next_batch(batch_size)
        x_valid_batch, y_valid_batch, _, valid_cls_batch = data.valid.next_batch(batch_size)

        
        feed_dict_tr = {x: x_batch,
                           y_true: y_true_batch}
        feed_dict_val = {x: x_valid_batch,
                              y_true: y_valid_batch}

        _, summary=session.run([optimizer,tf.merged ],feed_dict=feed_dict_tr)
        
        writer.add_summary(summary, i)
        if i % int(data.train.num_examples/batch_size) == 0:
               val_loss = session.run(cost, feed_dict=feed_dict_val)
               epoch = int(i / int(data.train.num_examples/batch_size))    
            
            
               show_progress(epoch, feed_dict_tr, feed_dict_val, val_loss)
               saver.save(session, 'dogs-cats-model')
        
            
        total_iterations += num_iteration
    writer.close()#tensorboard

train(num_iteration=3000)

2)解析

构建log日志文件,在session之后的合适位置writer=tf.summary.FileWriter('tensorboard/test2', session.graph);

运行代码之后,会在tensorboard/test2文件下生成一个日志文件,然后在命令行中输入tensorboard --logdir tensorboard/test2

点开命令行输出的网址,即可打开以下页面:

可以看到graph图。

2)可以精简图中节点的名称,可以把一个模块定一个总的名字。

具体做法是在模块的上面使用tf.name_scope('名字'):

如:def create_fc_layer(input,          
             num_inputs,    
             num_outputs,
             use_relu='True'):
   with tf.name_scope('fc'):#tensorboard
    #Let's define trainable weights and biases.
            weights = create_weights(shape=[num_inputs, num_outputs])
            biases = create_biases(num_outputs)
            tf.summary.histogram('weights',weights)#tensorboard

            tf.summary.histogram('biases',biases)#tensorboard

重新训练之后在打开tensorboard会发现图简洁了很多。

3)观察weight,biases,cost,accuracy变量等参数的变化

对于直方图输入以下内容:weights = create_weights(shape=[num_inputs, num_outputs])
                            biases = create_biases(num_outputs)
                            tf.summary.histogram('weights',weights)#tensorboard

                            tf.summary.histogram('biases',biases)#tensorboard

对于变量输入以下内容:cost = tf.reduce_mean(cross_entropy)

                                            tf.summary.scalar('cost',cost)#tensorboard

在对以上需要可视化的参数的操作之后,session.run(tf.global_variables_initializer())之前,整合以上参数:

tf.merged = tf.summary.merge_all()#tensorboard zaichushihuasuoyoubianliangzhiqian

session.run(tf.global_variables_initializer())

然后在执行训练的代码中将以上参数的变化也写入到writer文件中。具体见下段代码中红色字体,尤其注意蓝色字体容易出错。训练完成之后,关闭writer文件,writer.close()

def train(num_iteration):

    global total_iterations
    
    
    for i in range(total_iterations,
                   total_iterations + num_iteration):

        x_batch, y_true_batch, _, cls_batch = data.train.next_batch(batch_size)
        x_valid_batch, y_valid_batch, _, valid_cls_batch = data.valid.next_batch(batch_size)

        
        feed_dict_tr = {x: x_batch,
                           y_true: y_true_batch}
        feed_dict_val = {x: x_valid_batch,
                              y_true: y_valid_batch}

        _, summary=session.run([optimizer,tf.merged ],feed_dict=feed_dict_tr)
         writer.add_summary(summary, i)




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/twinkle_star1314/article/details/80319945
个人分类: Tensorflow
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

tensorflow_二分类之tensorboard可视化

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭