【发布时间】:2019-02-21 19:30:23
【问题描述】:
我收到提到的错误。我想加载单个图像作为输入 并在给定的蒙版图像上训练它以进行图像二进制 分类。
import tensorflow as tf
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
images = []
file_names = [os.path.join('../', f)
for f in os.listdir('../')
if f.endswith(".jpg")]
for f in file_names:
images.append(cv2.cvtColor(cv2.imread(f,1), cv2.COLOR_BGR2GRAY))
img_mask = images[0];
retval,mask_img = cv2.threshold(img_mask, 50, 255, cv2.THRESH_BINARY)
mask_img = mask_img/255
maskk = np.concatenate(mask_img)
x = tf.placeholder(dtype = tf.float32, shape = [None, 637, 1162])
y = tf.placeholder(dtype = tf.int32, shape = [None])
展平输入数据
images_flat = tf.contrib.layers.flatten(x)
全连接层
logits = tf.contrib.layers.fully_connected(images_flat, 2, tf.nn.relu)
定义损失函数
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y, logits = logits))
定义优化器
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
将 logits 转换为标签索引
correct_pred = tf.argmax(logits, 1)
定义准确度指标
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)
tf.set_random_seed(1234)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(201):
print('EPOCH', i)
_, accuracy_val = sess.run([train_op, accuracy], feed_dict={x:
images[1], y: maskk})
if i % 10 == 0:
print("Loss: ", loss)
print('DONE WITH EPOCH')
【问题讨论】:
-
你能告诉我们你是从哪里得到这个错误的吗?
标签: python tensorflow machine-learning deep-learning