我有一段代码使用 tf.nn.softmax 来预测图像是否属于 0、1、2 等类别。
但是,我想编辑代码以使用 sigmoid 作为激活函数并输出所有概率,并将概率 >0.5 的概率设置为图像中标识的类之一。
这是我试图运行的代码:https ://github.com/satyenrajpal/Concrete-Crack-Detection
我相信这是我应该进行编辑的代码片段:
for counter,image in enumerate(test_images):
#break up images into 128*128
broken_image,h,w,h_no,w_no = break_image(image,128)
output_image = np.zeros((h_no*128,w_no*128,3),dtype = np.uint8)
feed_dict = {x: broken_image}
batch_predictions = sess.run(predictions, feed_dict = feed_dict)
print("here is one loop")
print(batch_predictions)
# file = open("test.txt","w")
# bpred_str = batch_predictions.astype('str')
# file.write(bpred_str)
# file.write(" ")
# file.close()
results=np.concatenate((results, batch_predictions))
matrix_pred = batch_predictions.reshape((h_no,w_no))
#Concentrate after this for post processing
for i in range(0,h_no):
for j in range(0,w_no):
a = matrix_pred[i,j]
output_image[128*i:128*(i+1),128*j:128*(j+1),:] = 1-a
cropped_image = image[0:h_no*128,0:w_no*128,:]
pred_image = np.multiply(output_image,cropped_image)
我试图打印batch_predictions,但它打印出如下内容:
[1 1 0 1 1 0]
另一个片段是:
#Predict the class
y_pred = tf.nn.sigmoid(layer_fc2)
print("a: %s",y_pred)
#print("Class Probability: %s"%(sess.run(y_pred)))
self.y_pred_cls = tf.argmax(y_pred, dimension=1,name="predictions")
#Cost Function
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=layer_fc2, labels=self.y_true)
print("b: %s",cross_entropy)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
#Predict
correct_prediction = tf.equal(self.y_pred_cls, self.y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
probabilities=tf.nn.sigmoid(y_pred)
print("Class Probabilities: %s", probabilities)
return optimizer, accuracy
我还尝试打印 tf.nn.sigmoid(y_pred) 的输出,但它给出了:
Class Probabilities: %s Tensor("Sigmoid_1:0", shape=(?, 2), dtype=float32)
当我在未标记的数据上运行模型时,我需要有关如何打印出类的各个概率的帮助。先感谢您!