1

編輯打開的問題:https://github.com/tensorflow/tensorflow/issues/3128Tensorflow conv3d_transpose ***錯誤在 '蟒蛇':免費():無效的指針

我試圖使用新添加的conv3d_transpose創建here

但我m得到標題中的錯誤:*** Error in 'python': free(): invalid pointer

我有網絡工作在二維(使用相同的數據),但它不會在3D中運行。

我試過兩臺運行Ubuntu 14.04的不同Linux機器。我目前正試圖讓它在我的Mac上運行,但我無法找到包含conv3d_transpose的版本。我不得不在Linux機器上安裝每晚構建,但OSX的每晚構建不包括它。有誰知道我在哪裏可以找到它?

我輸入3D數據的方式可能是原因。我的數據輸入到二維圖像中,我基本上將它們連接成三維矩陣。出於測試目的,我保持我的數據集小。我的批量大小是1,深度是5.我從我的數據類中拉入n_depth * batch_size,然後將其重新整形爲[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 
+0

這聽起來像是在Tensorflow的錯誤,可能在(尚未提交)拉入請求。 github pull request是詢問這個問題的最佳地點。 –

+0

@PeterHawkins這是我的想法,我已經ping開發誰寫這 –

+0

我打開了一個問題https://github.com/tensorflow/tensorflow/issues/3128 –

回答

0

您可能需要安裝libtcmalloc-minimal4

sudo apt-get install libtcmalloc-minimal4 

和出口的路徑:

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