Python 我的tensorflow网络不准确

Python 我的tensorflow网络不准确,python,machine-learning,tensorflow,deep-learning,Python,Machine Learning,Tensorflow,Deep Learning,我是tensorflow和机器学习方面的新手,但我正在尝试用tensorflow构建这个网络,但没有准确性和损失没有改变 这里是网络 def CNN(input, keepratio): network = tf.reshape(input, [-1, imageWidth, imageHeight, NChannels]) network = newConvLayer(input=network, numInputChannels=NChannels, filterSize=

我是tensorflow和机器学习方面的新手,但我正在尝试用tensorflow构建这个网络,但没有准确性和损失没有改变

这里是网络

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
初始化所有变量

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))
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]))
新权重和偏差

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))
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和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")
定义新的卷积层

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
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
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
重塑图像以将其送入隐藏层

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
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
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
定义完全连接的层

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
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
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
构建网络

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 !"
输出

(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
很抱歉,这段代码太长了,但我试用了三天,仍然没有任何变化


谢谢你的帮助:)这里有几点:

1) 洗牌您的数据,以防止每个批次只包含一个类

2) 增加批处理大小,至少与类数相同

3) 增加模型容量(更多层,更小的过滤器尺寸)

4) 确保标签采用一种热矢量格式

5) 删除初始化的两个步骤,其中一个就足够了


6) 似乎您使用的是“ORL人脸数据库”,它只包含400个样本,对于深度学习任务来说非常小,尝试进行数据扩充(创建与原始图像类似的人工图像,例如旋转、翻转、模糊、添加噪声、放大、缩小等)

不要期望有人会阅读您的完整代码。只需提供您认为与您的问题相关的代码。