我是张力流和机器学习的新手,但我正在尝试使用张量流建立这个网络,但没有准确性和损失没有改变!
这是网络
初始化所有变量
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 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
主要
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
对于这段长代码感到抱歉,但我正在尝试三天但仍然没有变化!
感谢您的帮助:)
答案 0 :(得分:1)
有一些观点:
1)随机播放您的数据以防止每个批次只包含一个类
2)增加批量大小,至少与类号相同,
3)增加模型容量(更多层,更小的过滤器尺寸),
4)确保您的标签采用单热矢量格式
5)删除两步初始化,其中一步就足够了。
6)你似乎使用"面部的ORL数据库"它只包含400个样本,对于深度学习任务来说非常小,尝试进行数据增强(人工创建像原始图像一样的图像,如旋转,翻转,模糊,添加噪点,放大,缩小,... 。)