简体   繁体   中英

CNN model with Tensorflow

I am doing character recognition by building a Convolutional Neural Network model in Tensorflow. My model has 2 Conv layer followed by 2 fully connected layer. I have around 78K images from training and 13K images for testing. When I execute the model, I got accuracy around 92.xx% on test set. When I visualized my accuracy and loss curve on Tensorboard. I got a vertical line , but I did not know why I am getting this? I got curve like this Accuracy and Cross Entropy curve when viewed on tensorboard .

Also distribution curve of weights and biases shows a vertical line Left side shows testing parameters (weights and bias) and right side shows training parameters on first conv layer

Any help in this regard is highly appreciated. !!

def conv_layer(input, size_in, size_out, name="conv"):
with tf.name_scope(name):
w = tf.Variable(tf.random_normal([5, 5, size_in, size_out], stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1],padding="VALID")
act = tf.nn.relu(conv + b)
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")`

def fc_layer(input, size_in, size_out, name="fc"):
with tf.name_scope(name):
w = tf.Variable(tf.random_normal([size_in, size_out], stddev=0.1), name="W")  # Truncated_normal
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
act = tf.matmul(input, w) + b
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return act

def model(use_two_conv, use_two_fc):
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[None, 1024], name="x")
x_image = tf.reshape(x, [-1, 32, 32, 1])
tf.summary.image('input', x_image, 3)
y = tf.placeholder(tf.float32, shape=[None,46], name="labels")

if use_two_conv:
  conv1 = conv_layer(x_image, 1, 4, "conv1")
  conv_out = conv_layer(conv1,4,16,"conv2")    
else:
  conv1 = conv_layer(x_image, 1, 16, "conv1")
  conv_out = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

flattened = tf.reshape(conv_out, [-1, 5 * 5 * 16])

if use_two_fc:
  fc1 = fc_layer(flattened, 5 * 5 * 16, 200, "fc1")
  relu = tf.nn.relu(fc1)
  tf.summary.histogram("fc1/relu", relu)
  logits = fc_layer(fc1, 200, 46, "fc2")        

else:
  logits = fc_layer(flattened, 5*5*16, 46, "fc")  

When I've encountered this problem in the past, it's been a result of using

writer.add_summary(current_summary)

instead of

writer.add_summary(current_summary, epoch)

(using generic variable names because the relevant part of the asker's code isn't posted.) For instance,

summary_op = tf.summary.merge_all()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    writer = tf.summary.FileWriter("/Whatever/Path", sess.graph)
    for iteration in range(1001):
        if epoch % 100 == 0:
            _, current_summary = sess.run([training_op, summary_op])
            writer.add_summary(current_summary, iteration)
        else:
            _ = sess.run(training_op)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM