我正在训练神经网络。这是一个二进制分类器。它将平原和森林图像分类。我已将所有4K图像转换为124×93×3。一切都还好。但是tensorflow无法识别图像标签。模型是训练,损失在减少,但我得到了相同的精度。
data_y=[]
for x in range(len(data_x)):
data_y.append(np.array(random.randint(0,1)))
data_y=np.array(data_y).astype(np.float32)
此代码块创建np.float32格式的随机标签。张量流可以识别哪种格式?
这里都是代码。
from google.colab import drive
drive.mount('/content/gdrive')
%cd content/gdrive/My Drive/Image Dataset
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import cv2
import random
data_x=np.load("/content/gdrive/My Drive/Image Dataset/foo0.npy")
tmp=[]
for i_tmp in data_x:
gray = cv2.resize(i_tmp,(124, 93))
tmp.append(gray)
del data_x
data_x=np.array(tmp).astype(np.float32)
del tmp
data_y=[]
for x in range(len(data_x)):
data_y.append(np.array(random.randint(0,1)))
data_y=np.array(data_y).astype(np.float32)
X=tf.placeholder(tf.float32,[None,93, 124, 3])
Y=tf.placeholder(tf.float32)
w1=tf.Variable(tf.zeros([34596,1000]))
b1=tf.Variable(tf.zeros(1000))
w2=tf.Variable(tf.zeros([1000,1]))
b2=tf.Variable(tf.zeros([1]))
y1=tf.nn.tanh(tf.linalg.matmul(tf.reshape(X,(-1,34596)),w1)+b1)
y_pred=tf.nn.sigmoid(tf.linalg.matmul(y1,w2)+b2)
xent=-tf.reduce_sum(tf.math.square(Y-y_pred))
correct_pred=tf.equal(Y,tf.round(y_pred))
accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))
optimizer=tf.train.AdamOptimizer(1e-3).minimize(xent)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(10000):
sess.run(optimizer,feed_dict={X:data_x,Y:data_y})
if i%10==0:
acc,loss=sess.run([accuracy,xent],feed_dict={X:data_x,Y:data_y})
print("Iteration",i,"Accuracy="+str(acc),"Loss="+str(loss))