title: Tensorflow conv3d_transpose *** Error in 'python': free(): invalid pointer
body: <p><strong>EDIT</strong> opened issue: <a href="https://github.com/tensorflow/tensorflow/issues/3128" rel="nofollow">https://github.com/tensorflow/tensorflow/issues/3128</a></p>
  
  <p>I'm trying to use the newly added <code>conv3d_transpose</code> created <a href="https://github.com/tensorflow/tensorflow/pull/3049" rel="nofollow">here</a></p>
  
  <p>But I'm getting the error in the title: <code>*** Error in 'python': free(): invalid pointer</code></p>
  
  <p>I have the network working in 2D (using the same data), but it won't run in 3D. </p>
  
  <p>I have tried on two different linux machines both running Ubuntu 14.04. I am currently trying to get it to run on my Mac, but I can't find a build with with the <code>conv3d_transpose</code> included. I had to install the nightly build on my linux machine, but the nightly build for OSX doesn't include it. Does anyone know where I could find it?</p>
  
  <p>The way I input the 3D data may be the cause. My data inputs in 2D images, and I essentially concatenate them into a 3D matrix. For testing purposes, I'm keeping my data set small. My batch size is 1 and my depth is 5. I pull in <code>n_depth * batch_size</code> from my data class, and then reshape it into <code>[batch_size, n_depth, x, y, n_classes]</code></p>
  
  <p>The network itself builds, and it doesn't throw any dimension errors. The error occurs on the first training session. </p>
  
  <p>Full code is below:</p>
  
  <pre><code>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 &lt; 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 &gt; 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 &gt; 0.70 and acc &lt; 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 &gt; 0.73 and acc &lt; 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 &gt; 0.78 and acc &lt; 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 &gt; 0.81 and acc &lt; 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 &gt; 0.84 and acc &lt; 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 &gt; 0.87 and acc &lt; 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 &gt; 0.9 and acc &lt; 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 &gt; 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 &gt; 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
  </code></pre>