我利用自己的图片做了一个数据集训练神经网络,在feed数据的时候数据类型不合适,加了session.run()程序就卡在这里不动了,下面贴出代码,跪求大神指导。
程序卡在print(“begin4”)和print(“begin5”)之间
import tensorflow as tf
from encode_to_tfrecords import create_record, create_test_record, read_and_decode, get_batch, get_test_batch
n_input = 154587
n_classes = 3
dropout = 0.5
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.int32, [None, n_classes])
keep_drop = tf.placeholder(tf.float32)
class network(object):
def inference(self, images,keep_drop):
####################################################################################################################
# 向量转为矩阵
# images = tf.reshape(images, shape=[-1, 39,39, 3])
images = tf.reshape(images, shape=[-1, 227, 227, 3]) # [batch, in_height, in_width, in_channels]
images = (tf.cast(images, tf.float32) / 255. - 0.5) * 2 # 归一化处理
####################################################################################################################
# 第一层 定义卷积偏置和下采样
conv1 = tf.nn.bias_add(tf.nn.conv2d(images, self.weights['conv1'], strides=[1, 4, 4, 1], padding='VALID'),
self.biases['conv1'])
relu1 = tf.nn.relu(conv1)
pool1 = tf.nn.max_pool(relu1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
# 第二层
conv2 = tf.nn.bias_add(tf.nn.conv2d(pool1, self.weights['conv2'], strides=[1, 1, 1, 1], padding='SAME'),
self.biases['conv2'])
relu2 = tf.nn.relu(conv2)
pool2 = tf.nn.max_pool(relu2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
# 第三层
conv3 = tf.nn.bias_add(tf.nn.conv2d(pool2, self.weights['conv3'], strides=[1, 1, 1, 1], padding='SAME'),
self.biases['conv3'])
relu3 = tf.nn.relu(conv3)
# pool3=tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
conv4 = tf.nn.bias_add(tf.nn.conv2d(relu3, self.weights['conv4'], strides=[1, 1, 1, 1], padding='SAME'),
self.biases['conv4'])
relu4 = tf.nn.relu(conv4)
conv5 = tf.nn.bias_add(tf.nn.conv2d(relu4, self.weights['conv5'], strides=[1, 1, 1, 1], padding='SAME'),
self.biases['conv5'])
relu5 = tf.nn.relu(conv5)
pool5 = tf.nn.max_pool(relu5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
# 全连接层1,先把特征图转为向量
flatten = tf.reshape(pool5, [-1, self.weights['fc1'].get_shape().as_list()[0]])
# dropout比率选用0.5
drop1 = tf.nn.dropout(flatten, keep_drop)
fc1 = tf.matmul(drop1, self.weights['fc1']) + self.biases['fc1']
fc_relu1 = tf.nn.relu(fc1)
fc2 = tf.matmul(fc_relu1, self.weights['fc2']) + self.biases['fc2']
fc_relu2 = tf.nn.relu(fc2)
fc3 = tf.matmul(fc_relu2, self.weights['fc3']) + self.biases['fc3']
return fc3
def __init__(self):
# 初始化权值和偏置
with tf.variable_scope("weights"):
self.weights = {
# 39*39*3->36*36*20->18*18*20
'conv1': tf.get_variable('conv1', [11, 11, 3, 96],
initializer=tf.contrib.layers.xavier_initializer_conv2d()),
# 18*18*20->16*16*40->8*8*40
'conv2': tf.get_variable('conv2', [5, 5, 96, 256],
initializer=tf.contrib.layers.xavier_initializer_conv2d()),
# 8*8*40->6*6*60->3*3*60
'conv3': tf.get_variable('conv3', [3, 3, 256, 384],
initializer=tf.contrib.layers.xavier_initializer_conv2d()),
# 3*3*60->120
'conv4': tf.get_variable('conv4', [3, 3, 384, 384],
initializer=tf.contrib.layers.xavier_initializer_conv2d()),
'conv5': tf.get_variable('conv5', [3, 3, 384, 256],
initializer=tf.contrib.layers.xavier_initializer_conv2d()),
'fc1': tf.get_variable('fc1', [6 * 6 * 256, 4096], initializer=tf.contrib.layers.xavier_initializer()),
'fc2': tf.get_variable('fc2', [4096, 4096], initializer=tf.contrib.layers.xavier_initializer()),
'fc3': tf.get_variable('fc3', [4096, 1000], initializer=tf.contrib.layers.xavier_initializer()),
}
with tf.variable_scope("biases"):
self.biases = {
'conv1': tf.get_variable('conv1', [96, ],
initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)),
'conv2': tf.get_variable('conv2', [256, ],
initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)),
'conv3': tf.get_variable('conv3', [384, ],
initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)),
'conv4': tf.get_variable('conv4', [384, ],
initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)),
'conv5': tf.get_variable('conv5', [256, ],
initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)),
'fc1': tf.get_variable('fc1', [4096, ],
initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)),
'fc2': tf.get_variable('fc2', [4096, ],
initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)),
'fc3': tf.get_variable('fc3', [1000, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32))
}
# 计算softmax交叉熵损失函数
def sorfmax_loss(self, predicts, labels):
predicts = tf.nn.softmax(predicts)
labels = tf.one_hot(labels, self.weights['fc3'].get_shape().as_list()[1])
loss = tf.nn.softmax_cross_entropy_with_logits(logits=predicts, labels=labels)
# loss =-tf.reduce_mean(labels * tf.log(predicts))# tf.nn.softmax_cross_entropy_with_logits(predicts, labels)
self.cost = loss
return self.cost
# 梯度下降
def optimer(self, loss, lr=0.01):
train_optimizer = tf.train.GradientDescentOptimizer(lr).minimize(loss)
return train_optimizer
#定义训练
# def train(self):
create_record('/Users/hanjiarong/Documents/testdata/tfrtrain')
# image, label = read_and_decode('train.tfrecords')
# batch_image, batch_label = get_batch(image, label, 30)
#连接网络 网络训练
net = network()
inf = net.inference(x, dropout)
loss = net.sorfmax_loss(inf,y)
opti = net.optimer(loss)
correct_pred = tf.equal(tf.argmax(inf, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# #定义测试
create_test_record('/Users/hanjiarong/Documents/testdata/tfrtest')
# image_t, label_t = read_and_decode('test.tfrecords')
# batch_test_image, batch_test_label = get_test_batch(image_t, label_t, 50)
#
# #生成测试
init = tf.initialize_all_variables()
with tf.Session() as session:
session.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
max_iter = 100000
iter = 1
print("begin1")
while iter * 30 < max_iter:
print("begin2")
image, label = read_and_decode('train.tfrecords')
print("begin3")
batch_image, batch_label = get_batch(image, label, 1)
print("begin4")
batch_image = session.run(batch_image)
batch_label = session.run(batch_label)
print("begin5")
# loss_np, _, label_np, image_np, inf_np = session.run([loss, opti, batch_label, batch_image, inf])
session.run(opti, feed_dict={x: batch_image, y: batch_label, keep_drop: dropout})
print("begin6")
if iter % 10 == 0:
loss, acc = session.run([loss, accuracy], feed_dict={x: batch_image, y: batch_label, keep_drop: 1.})
print("Iter " + str(iter * 30) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc))
iter += 1
print("Optimization Finished!")
image, label = read_and_decode('test.tfrecords')
batch_test_image, batch_test_label = get_batch(image, label, 2)
img_test, lab_test = session.run([batch_test_image, batch_test_label])
test_accuracy = session.run(accuracy,
feed_dict={x: img_test, y: lab_test, keep_drop: 1.})
print("Testing Accuracy:", test_accuracy)
参考一下:https://blog.csdn.net/happyhorizion/article/details/77894055
Tensorflow中文社区测试版已经开放了,你可以去看,希望你能找到答案!
也许可以使用.pkl文件试试?
我原来做的,直接读取TFRecord文件里的数据image,label作为输入,也卡在那了,,解决的话就是把读出的文件在处理一遍,tf.train.slice_input_producer([image, label])然后生成batch,image_batch, label_batch = tf.train.batch([image, label],
batch_size= batch_size,然后就可以跑了