Mohamed
Mohamed

Reputation: 41

No Accuracy in my tensorflow network

I'm new in tensorflow and machine learning but I'm trying to build this network with tensorflow but there is no accuracy and loss not changed !

Here is the network

Init All Variables

import tensorflow as tf, data # get custom dataset

training_dir = 'Datasets/att_faces/Training'
testing_dir = 'Datasets/att_faces/Testing'


filterSize1 = 5
numFilters1 = 64
maxPooling1 = 2

filterSize2 = 5
numFilters2 = 32
maxPooling2 = 2

filterSize3 = 5
numFilters3 = 16
maxPooling3 = 2


fullyConn1 = 1024
fullyConn2 = 256

dropout = 0.75

imageSize = 92*112
imageWidth = 92
imageHeight = 112

NClasses = 40
BatchSize = 10

NEpochs = 50
learningRate = 0.001

NChannels = 1

x = tf.placeholder(tf.float32, [None, imageSize])
y = tf.placeholder(tf.float32, [None, NClasses])
keepRatio = tf.placeholder(tf.float32)

X, Y= data.LoadTrainingData(training_dir, (imageWidth, imageHeight))
data.TrainingData = X
data.TrainingLables = Y

XT, YT, NamesT, Classes, Paths = data.LoadTestingData(testing_dir, (imageWidth, imageHeight))
data.TestingData = XT
data.TestingLables = YT
print (len(X), len(Y), len(XT), len(YT), len(NamesT), len(Paths))

New weights and biases

def newWeight(shape):
    return tf.Variable(tf.truncated_normal(shape=shape, stddev=0.05))
    #return tf.Variable(tf.random_normal(shape=shape))

def newBiases(length):
    return tf.Variable(tf.constant(0.05, shape=[length]))
    #return tf.Variable(tf.random_normal([length]))

Conv2d and maxpooling

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

def maxpool2d(layer, filterSize):
    return tf.nn.max_pool(value=layer, ksize=[1, filterSize, filterSize, 1], strides=[1, filterSize, filterSize, 1], padding="SAME")

Define new convolution layer

def newConvLayer(input, numInputChannels, filterSize, numFilters, activation='relu', usePooling=True, poolingFilter = 3):

    shape = [filterSize, filterSize, numInputChannels, numFilters]

    Weights = newWeight(shape=shape)

    Biases = newBiases(length=numFilters)

    layer = conv2d(input, Weights)

    layer = tf.nn.bias_add(layer, Biases)

    if(activation=='relu'):
        layer = tf.nn.relu(layer)
    else:
        layer = tf.nn.tanh(layer)

    if usePooling:
        layer = maxpool2d(layer, poolingFilter)

    return layer

Reshape image to feed it to hidden layer

def flattenLayer(input):
    layerShape = input.get_shape() # [num_images, height, width, num_channels]

    num_features =  layerShape[1:4].num_elements()

    Layer = tf.reshape(input, [-1, num_features])

    print "Flatten Layer: " + str(num_features)

    return Layer, num_features

Define fully connected layer

def newFCLayer(input, numInput, numOutput, isOut = False, activation='tanh', dropout=0.75):

    Weights = newWeight(shape=[numInput, numOutput])
    Biases = newBiases(length=numOutput)

    layer = tf.matmul(input, Weights)
    layer = tf.nn.bias_add(layer, Biases)

    if(isOut):
        return layer

    if activation=='tanh':
        layer = tf.nn.tanh(layer)
    else:
        layer = tf.nn.relu(layer)

    layer = tf.nn.dropout(layer, dropout)

    return layer

Building the network

def CNN(input, keepratio):
    network = tf.reshape(input, [-1, imageWidth, imageHeight, NChannels])

    network = newConvLayer(input=network, numInputChannels=NChannels, filterSize=filterSize1,
                        numFilters=numFilters1,
                            poolingFilter=maxPooling1)

    network = newConvLayer(input=network, numInputChannels=numFilters1,
                        filterSize=filterSize2, numFilters=numFilters2,
                            poolingFilter=maxPooling2)

   # network = newConvLayer(input=network, numInputChannels=numFilters2,
    #                    filterSize=filterSize3, numFilters=numFilters3,
     #                       poolingFilter=maxPooling3)


    network, NumFeatures = flattenLayer(network)
    network = newFCLayer(network, NumFeatures, fullyConn1, activation='relu', dropout=keepratio)
    #network = newFCLayer(network, fullyConn1, fullyConn2, activation='tanh', dropout=keepratio)
    network = newFCLayer(network, fullyConn1, NClasses, isOut=True)

    return network

Main

def main():
    Prediction = CNN(x, keepRatio)

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Prediction, labels=y))

    optimizer = tf.train.AdamOptimizer(learningRate).minimize(cost)
    correct = tf.equal(tf.argmax(Prediction, 1), tf.argmax(y, 1))

    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
    init = tf.global_variables_initializer()
    init2 = tf.local_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)
        sess.run(init2)
        rang = int(len(X) / BatchSize)
        if len(X) % BatchSize != 0:
            rang += 1

        for epoch in range(NEpochs):
            for i in range(rang):
                epochX, epochY = data.nextBatch(BatchSize)

                epochX = epochX.reshape(BatchSize, imageSize)

                feeds = {x: epochX, y:epochY, keepRatio: 0.75}

                sess.run(optimizer, feed_dict=feeds)

                loss, acc = sess.run([cost, accuracy], feed_dict={x:epochX, y:epochY, keepRatio:1.})

                print("Epoch: %01d/%01d loss: %.4f Acc: %.4f" %(epoch, NEpochs, loss, acc))

            print "Epoch " + str(epoch) + " Finished !"

Output

(360, 360, 40, 40, 40, 40)
Flatten Layer: 20608

Epoch: 0/50 loss: 3.7418 Acc: 0.9000
Epoch: 0/50 loss: 4.3752 Acc: 0.0000
Epoch: 0/50 loss: 4.5419 Acc: 0.0000
Epoch: 0/50 loss: 2.3754 Acc: 0.0000
Epoch: 0/50 loss: 2.7341 Acc: 0.0000

Epoch: 16/50 loss: 3.7056 Acc: 0.0000
Epoch: 16/50 loss: 3.7036 Acc: 0.0000
Epoch: 16/50 loss: 3.7028 Acc: 0.0000
Epoch: 16/50 loss: 3.7009 Acc: 0.0000
Epoch: 16/50 loss: 3.7084 Acc: 0.0000

Epoch: 29/50 loss: 3.6965 Acc: 0.0000
Epoch: 29/50 loss: 3.6930 Acc: 0.0000
Epoch: 29/50 loss: 3.6932 Acc: 0.0000
Epoch: 29/50 loss: 3.6973 Acc: 0.0000
Epoch: 29/50 loss: 3.6910 Acc: 0.0000
Epoch: 29/50 loss: 3.6905 Acc: 0.0000
Epoch: 29/50 loss: 3.6862 Acc: 0.0000
Epoch: 29/50 loss: 3.6953 Acc: 0.0000

Epoch: 34/50 loss: 3.6851 Acc: 0.0000
Epoch: 34/50 loss: 3.6827 Acc: 0.0000
Epoch: 34/50 loss: 3.6839 Acc: 0.0000
Epoch: 34/50 loss: 3.6904 Acc: 0.0000
Epoch: 34/50 loss: 3.7003 Acc: 0.0000
Epoch: 34/50 loss: 3.6942 Acc: 0.0000
Epoch: 34/50 loss: 3.6979 Acc: 0.0000

Sorry about this long code but I'm trying on it for three days and still no change!

Thanks for helping :)

Upvotes: 1

Views: 515

Answers (1)

Ali
Ali

Reputation: 974

There are some points:

1) shuffle your data to prevent that each batch contain only one class,

2) increase your batch size, at least as same as classes number,

3) increase your model capacity(more layers, smaller filter sizes),

4) make sure, your labels are in one-hot vector format,

5) remove two-step of initializing, one of them is enough.

6) it seems you use "The ORL Database of Faces" which contains only 400 samples, it's very small for deep learning task, try to have data augmentation(create artificially images like original one, such as rotating, flipping, blur, add noise, zoom in, zoom out,...)

Upvotes: 1

Related Questions