gpt4 book ai didi

python - Tensorflow conv3d_transpose *** 'python' : free(): invalid pointer 中的错误

转载 作者:行者123 更新时间:2023-11-30 09:21:12 25 4
gpt4 key购买 nike

编辑打开的问题:https://github.com/tensorflow/tensorflow/issues/3128

我正在尝试使用新添加的 conv3d_transpose创建here

但我在标题中收到错误:*** Error in 'python': free(): invalid pointer

我的网络在 2D 模式下运行(使用相同的数据),但它无法在 3D 模式下运行。

我在两台不同的 Linux 机器上尝试过,都运行 Ubuntu 14.04。我目前正在尝试让它在我的 Mac 上运行,但我找不到带有 conv3d_transpose 的版本包括。我必须在我的 Linux 机器上安装夜间构建,但 OSX 的夜间构建不包括它。有谁知道在哪里可以找到它吗?

我输入 3D 数据的方式可能是原因。我的数据输入为 2D 图像,我基本上将它们连接成 3D 矩阵。出于测试目的,我将数据集保持在较小的范围。我的批量大小是 1,深度是 5。我拉入 n_depth * batch_size来 self 的数据类,然后将其 reshape 为 [batch_size, n_depth, x, y, n_classes]

网络本身会构建,并且不会引发任何维度错误。该错误发生在第一次训练时。

完整代码如下:

import tensorflow as tf
import pdb
import numpy as np
from numpy import genfromtxt
from PIL import Image
from tensorflow.contrib.learn.python.learn.datasets.scroll import scroll_data

# Parameters
learning_rate = 0.001
training_iters = 1000000
batch_size = 1
display_step = 1

# Network Parameters
n_input_x = 200 # Input image x-dimension
n_input_y = 200 # Input image y-dimension
n_depth = 5
n_classes = 2 # Binary classification -- on a surface or not

dropout = 0.75 # Dropout, probability to keep units

# tf Graph input
x = tf.placeholder(tf.float32, [None, n_depth, n_input_x, n_input_y])
y = tf.placeholder(tf.float32, [None, n_depth, n_input_x, n_input_y, n_classes], name="ground_truth")
keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)


# This function converts the ground truth data into a
# 2 channel classification -- n_input_x x n_input_y x 2
# one layer for 0's and the other for 1's
def convert_to_2_channel(x, size):
#assume input has dimension (batch_size,x,y)
#output will have dimension (batch_size,x,y,2)
output = np.empty((size, 200, 200, 2))

temp_temp_arr1 = np.empty((size, 200, 200))
temp_temp_arr2 = np.empty((size, 200, 200))

for i in xrange(size):
for j in xrange(n_input_x):
for k in xrange(n_input_y):
if x[i][j][k] == 1:
temp_temp_arr1[i][j][k] = 1
temp_temp_arr2[i][j][k] = 0
else:
temp_temp_arr1[i][j][k] = 0
temp_temp_arr2[i][j][k] = 1

for i in xrange(size):
for j in xrange(n_input_x):
for k in xrange(n_input_y):
for l in xrange(2):
try:
if l == 0:
output[i][j][k][l] = temp_temp_arr1[i][j][k]
else:
output[i][j][k][l] = temp_temp_arr2[i][j][k]
except IndexError:
print "Index error"
pdb.set_trace()
return output


# Create some wrappers for simplicity
def conv3d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
x = tf.nn.conv3d(x, W, strides=[1, strides, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)

def maxpool3d(x, k=2):
# MaxPool2D wrapper
return tf.nn.max_pool3d(x, ksize=[1, k, k, k, 1], strides=[1, k, k, k, 1],
padding='SAME')

def deconv3d(prev_layer, w, b, output_shape, strides):
# Deconv layer
deconv = tf.nn.conv3d_transpose(prev_layer, w, output_shape=output_shape, strides=strides, padding="VALID")
deconv = tf.nn.bias_add(deconv, b)
deconv = tf.nn.relu(deconv)
return deconv

# Create model
def conv_net(x, weights, biases, dropout):
# Reshape input picture
x = tf.reshape(x, shape=[-1, 5, 200, 200, 1])

with tf.name_scope("conv1") as scope:
# Convolution Layer
conv1 = conv3d(x, weights['wc1'], biases['bc1'])
# Max Pooling (down-sampling)
#conv1 = tf.nn.local_response_normalization(conv1)
conv1 = maxpool3d(conv1, k=2)

# Convolution Layer
with tf.name_scope("conv2") as scope:
conv2 = conv3d(conv1, weights['wc2'], biases['bc2'])
# Max Pooling (down-sampling)
# conv2 = tf.nn.local_response_normalization(conv2)
conv2 = maxpool3d(conv2, k=2)

# Convolution Layer
with tf.name_scope("conv3") as scope:
conv3 = conv3d(conv2, weights['wc3'], biases['bc3'])
# Max Pooling (down-sampling)
# conv3 = tf.nn.local_response_normalization(conv3)
conv3 = maxpool3d(conv3, k=2)

pdb.set_trace()

temp_batch_size = tf.shape(x)[0] #batch_size shape
with tf.name_scope("deconv1") as scope:
output_shape = [temp_batch_size, 2, 50, 50, 64]
strides = [1,1,2,2,1]
conv4 = deconv3d(conv3, weights['wdc1'], biases['bdc1'], output_shape, strides)
# conv4 = tf.nn.local_response_normalization(conv4)

with tf.name_scope("deconv2") as scope:
output_shape = [temp_batch_size, 3, 100, 100, 32]
strides = [1,1,2,2,1]
conv5 = deconv3d(conv4, weights['wdc2'], biases['bdc2'], output_shape, strides)
# conv5 = tf.nn.local_response_normalization(conv5)

with tf.name_scope("deconv3") as scope:
output_shape = [temp_batch_size, 5, 200, 200, 2]
#this time don't use ReLu -- since output layer
conv6 = tf.nn.conv3d_transpose(conv5, weights['wdc3'], output_shape=output_shape, strides=[1,1,2,2,1], padding="VALID")
conv6 = tf.nn.bias_add(conv6, biases['bdc3'])
# conv6 = tf.nn.relu(conv6)

# Include dropout
#conv6 = tf.nn.dropout(conv6, dropout)
return conv6

weights = {
# 5x5 conv, 1 input, 32 outputs
'wc1' : tf.Variable(tf.random_normal([5, 5, 5, 1, 32])),
# 5x5 conv, 32 inputs, 64 outputs
'wc2' : tf.Variable(tf.random_normal([3, 5, 5, 32, 64])),
# 5x5 conv, 32 inputs, 64 outputs
'wc3' : tf.Variable(tf.random_normal([2, 5, 5, 64, 128])),

'wdc1' : tf.Variable(tf.random_normal([2, 2, 2, 64, 128])),

'wdc2' : tf.Variable(tf.random_normal([2, 2, 2, 32, 64])),

'wdc3' : tf.Variable(tf.random_normal([3, 2, 2, 2, 32])),
}

biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bc3': tf.Variable(tf.random_normal([128])),
'bdc1': tf.Variable(tf.random_normal([64])),
'bdc2': tf.Variable(tf.random_normal([32])),
'bdc3': tf.Variable(tf.random_normal([2])),
}

# Construct model
# with tf.name_scope("net") as scope:
pred = conv_net(x, weights, biases, keep_prob)
pdb.set_trace()
pred = tf.reshape(pred, [-1, n_input_x, n_input_y, n_depth, n_classes]) #Reshape to shape-Y

# Define loss and optimizer
# Reshape for cost function
temp_pred = tf.reshape(pred, [-1, 2])
temp_y = tf.reshape(y, [-1, 2])
with tf.name_scope("loss") as scope:
# cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(pred, y))
cost = (tf.nn.softmax_cross_entropy_with_logits(temp_pred, temp_y))

with tf.name_scope("opt") as scope:
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
with tf.name_scope("acc") as scope:
# accuracy is the difference between prediction and ground truth matrices
correct_pred = tf.equal(0,tf.cast(tf.sub(tf.nn.softmax(temp_pred),temp_y), tf.int32))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.initialize_all_variables()
saver = tf.train.Saver()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
summary = tf.train.SummaryWriter('/tmp/logdir/', sess.graph) #initialize graph for tensorboard
step = 1
# Import data
data = scroll_data.read_data('/home/kendall/Desktop/')
# Keep training until reach max iterations
while step * batch_size < training_iters:
batch_x, batch_y = data.train.next_batch(n_depth)
# Run optimization op (backprop)
batch_x = batch_x.reshape((batch_size, n_depth, n_input_x, n_input_y))
batch_y = batch_y.reshape((n_depth, n_input_x, n_input_y))
batch_y = convert_to_2_channel(batch_y, n_depth) # Converts the 200x200 ground truth to a 200x200x2 classification
batch_y = batch_y.reshape(batch_size * n_input_x * n_input_y * n_depth, 2)
sess.run(optimizer, feed_dict={x: batch_x, temp_y: batch_y,
keep_prob: dropout})

pdb.set_trace()
if step % display_step == 0:
batch_y = batch_y.reshape(batch_size, n_input_x, n_input_y, 2)
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.0})
print "Accuracy = " + str(acc)
#print "Loss = " + str(loss)

# Save network and make prediction
if acc > 0.7:
# Save network
save_path = "model.ckpt"
saver.save(sess, save_path)

# Make prediction
im = Image.open('/home/kendall/Desktop/HA900_frames/frame0001.tif')
batch_x = np.array(im)
batch_x = batch_x.reshape((1, n_input_x, n_input_y))
batch_x = batch_x.astype(float)
prediction = sess.run(pred, feed_dict={x: batch_x, keep_prob: 1.0})
prediction = prediction.reshape((40000,2))
prediction = tf.nn.softmax(prediction)
prediction = prediction.eval()
prediction = prediction.reshape((n_input_x, n_input_y, 2))

# Temp arrays are to splice the prediction n_input_x x n_input_y x 2
# into 2 matrices n_input_x x n_input_y
temp_arr1 = np.empty((n_input_x, n_input_y))
temp_arr2 = np.empty((n_input_x, n_input_y))
for i in xrange(n_input_x):
for j in xrange(n_input_x):
for k in xrange(2):
if k == 0:
temp_arr1[i][j] = prediction[i][j][k]
else:
temp_arr2[i][j] = prediction[i][j][k]
# np.savetxt("small_dataset_1.csv", temp_arr1, delimiter=",")
# np.savetxt("small_dataset_2.csv", temp_arr2, delimiter=",")

if acc > 0.70 and acc < 0.73:
np.savetxt("run_1_step_1-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_1-2.csv", temp_arr2, delimiter=",")
if acc > 0.73 and acc < 0.78:
np.savetxt("run_1_step_2-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_2-2.csv", temp_arr2, delimiter=",")
if acc > 0.78 and acc < 0.81:
np.savetxt("run_1_step_3-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_3-2.csv", temp_arr2, delimiter=",")
if acc > 0.81 and acc < 0.84:
np.savetxt("run_1_step_4-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_4-2.csv", temp_arr2, delimiter=",")
if acc > 0.84 and acc < 0.87:
np.savetxt("run_1_step_5-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_5-2.csv", temp_arr2, delimiter=",")
if acc > 0.87 and acc < 0.9:
np.savetxt("run_1_step_6-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_6-2.csv", temp_arr2, delimiter=",")
if acc > 0.9 and acc < 0.95:
np.savetxt("run_1_step_7-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_7-2.csv", temp_arr2, delimiter=",")
if acc > 0.95:
np.savetxt("run_1_step_8-1.csv", temp_arr1, delimiter=",")
# np.savetxt("run_1_step_8-2.csv", temp_arr2, delimiter=",")


if acc > 0.98:
break

step += 1
print "Optimization Finished!"

# Measure accuracy on test set
print "Testing Accuracy:",
test_img = data.test.labels[0:].reshape((5, n_input_x, n_input_y))
test_img = convert_to_2_channel(test_img, 5)
acc = sess.run(accuracy, feed_dict={x: data.test.images[0:].reshape((5, n_input_x, n_input_y)),
y: test_img,
keep_prob: 1.})
print acc

最佳答案

您可能需要安装libtcmalloc-minimal4

 sudo apt-get install libtcmalloc-minimal4

并导出其路径:

 export LD_PRELOAD="/usr/lib/libtcmalloc_minimal.so.4"

关于python - Tensorflow conv3d_transpose *** 'python' : free(): invalid pointer 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com