Tensorflow 培训准确度和测试准确度不变

Tensorflow 培训准确度和测试准确度不变,tensorflow,machine-learning,deep-learning,computer-vision,cnn,Tensorflow,Machine Learning,Deep Learning,Computer Vision,Cnn,我的CNN模型的训练精度和测试精度没有改变。我尝试过不同的方法,但结果还是一样的。我使用的是tensorflow==1.5 仅供参考:如果我对相同的数据使用Keras,它可以很好地工作,并为我的模型提供99%的准确性,但由于我运行模型的机器不支持Keras,我必须在tensorflow==1.5中创建一个模型 数据维度: 打印(序列X.shape、序列y.shape、测试X.shape、测试y.shape) (16481281281) 下面是我的代码和代码输出: train_X = np.loa

我的CNN模型的训练精度和测试精度没有改变。我尝试过不同的方法,但结果还是一样的。我使用的是tensorflow==1.5 仅供参考:如果我对相同的数据使用Keras,它可以很好地工作,并为我的模型提供99%的准确性,但由于我运行模型的机器不支持Keras,我必须在tensorflow==1.5中创建一个模型

数据维度: 打印(序列X.shape、序列y.shape、测试X.shape、测试y.shape) (16481281281)

下面是我的代码和代码输出:

train_X = np.load('{}x_train_.npy'.format(INPUT_DIR))
train_y = np.load('{}y_train_.npy'.format(INPUT_DIR))
test_X = np.load('{}x_test_.npy'.format(INPUT_DIR))
test_y = np.load('{}y_test_.npy'.format(INPUT_DIR))

tf.reset_default_graph()
X_input = tf.placeholder(dtype=tf.float32, shape=[None, IMG_SIZE, IMG_SIZE, 1], name="Input")
Y_true = tf.placeholder(dtype=tf.float32,shape= [None, output_classes], name="TrueLabels")
lr = tf.placeholder(tf.float32, shape = [], name = 'lr')

conv_1=tf.layers.Conv2D( filters=32,kernel_size=3,strides=1,data_format='channels_last', name='layer_conv1')(X_input)
conv_relu_1=tf.nn.relu(conv_1)
print(conv_relu_1)
maxpool_1=tf.nn.max_pool(conv_relu_1, ksize= [1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID')
print(maxpool_1)

conv_2=tf.layers.Conv2D( filters=64,kernel_size=3,strides=1,data_format='channels_last',name='layer_conv2')(maxpool_1)
conv_relu_2=tf.nn.relu(conv_2)
print(conv_relu_2)
maxpool_2=tf.nn.max_pool(conv_relu_2, ksize= [1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID')
print(maxpool_2)

conv_3=tf.layers.Conv2D( filters=128,kernel_size=3,strides=1,data_format='channels_last',name='layer_conv2')(maxpool_2)
conv_relu_3=tf.nn.relu(conv_3)
print(conv_relu_3)
maxpool_3=tf.nn.max_pool(conv_relu_3, ksize= [1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID')
print(maxpool_3)

conv_4=tf.layers.Conv2D( filters=256,kernel_size=3,strides=1,data_format='channels_last',name='layer_conv2')(maxpool_3)
conv_relu_4=tf.nn.relu(conv_4)
print(conv_relu_4)
maxpool_4=tf.nn.max_pool(conv_relu_4, ksize= [1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID')
print(maxpool_4)

flatten=tf.layers.flatten(maxpool_4)
print(flatten)

flatten_2=tf.layers.dense(flatten,256,activation=tf.nn.relu,name='layer_fc') 
print(flatten_2)
output=tf.layers.dense(flatten_2,1,activation=tf.nn.sigmoid,name='out') 
print(output)

correct_prediction = tf.equal(tf.round(output), Y_true)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=Y_true, logits=output))
opt= tf.train.AdamOptimizer(lr).minimize(loss)

batch_size=32
init = tf.global_variables_initializer()
training_iters=50

with tf.Session() as sess:
    sess.run(init)
    train_loss = []
    test_loss = []
    train_accuracy = []
    test_accuracy = []
    #summary_writer = tf.summary.FileWriter('./Output', sess.graph)
    for i in range(training_iters):
        for batch in range(len(train_X)//batch_size):
            batch_x = train_X[batch*batch_size:min((batch+1)*batch_size,len(train_X))]
            batch_y = train_y[batch*batch_size:min((batch+1)*batch_size,len(train_y))]
            #print(batch*batch_size, min((batch+1)*batch_size,len(train_y)))
            #print(batch_x.shape, batch_y.shape)
            # Run optimization op (backprop).
                # Calculate batch loss and accuracy
            opt1 = sess.run(opt, feed_dict={X_input: batch_x,Y_true: batch_y, lr: 0.01})
            loss_i, acc = sess.run([loss, accuracy], feed_dict={X_input: batch_x, Y_true: batch_y})
        print("Iter " + str(i) + ", Loss= " + \
                      "{:.6f}".format(loss_i) + ", Training Accuracy= " + \
                      "{:.5f}".format(acc))
        print("Optimization Finished!")

        # Calculate accuracy for all 10000 mnist test images
        test_acc,valid_loss = sess.run([accuracy,loss], feed_dict={X_input: test_X,Y_true : test_y})
        train_loss.append(loss)
        test_loss.append(valid_loss)
        train_accuracy.append(acc)
        test_accuracy.append(test_acc)
        print("Testing Accuracy:","{:.5f}".format(test_acc))
    #summary_writer.close()

IMG_大小=128,输出_类=1
Iter 0, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407
Iter 1, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407
Iter 2, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407
Iter 3, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407
Iter 4, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407
Iter 5, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407
Iter 6, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407
Iter 7, Loss= 0.693147, Training Accuracy= 0.78125
Optimization Finished!
Testing Accuracy: 0.64407