TensorFlow - GraphDef cannot be larger than 2GB

529
September 06, 2017, at 9:13 PM

I am trying to save and restore a tensorflow model and I get the infamous ValueError: GraphDef cannot be larger than 2GB.

I was under the belief that my problem has something to do with unnecessary data (I only need the variables), so I tried fixing the issue by handing a dict of variables to the saver.

Unfortunately I still get the 2G error. the overlay.meta file is ~325 MB.

Any ideas?

code for model definition, session running and saving:

batch_size = 16
patch_size = 5
depth = 16
num_hidden = 64
num_channels = 1
num_labels = 2
image_size = 64
lim_valid_test = 1000 
# Input data.
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset[:lim_valid_test])
tf_test_dataset = tf.constant(test_dataset)
# Variables.
layer1_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, depth], stddev=0.1), name='layer1_weights')
layer1_biases = tf.Variable(tf.zeros([depth]), name='layer1_biases')
layer2_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth, depth], stddev=0.1), name='layer2_weights')
layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]), name='layer2_biases')
layer3_weights = tf.Variable(tf.truncated_normal([image_size // 4 * image_size // 4 * depth, num_hidden], stddev=0.1), name='layer3_weights')
layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]), name='layer3_biases')
layer4_weights = tf.Variable(tf.truncated_normal([num_hidden, num_labels], stddev=0.1), name='layer4_weights')
layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]), name='layer4_biases')
# Model.
def model(data):
    conv = tf.nn.conv2d(data, layer1_weights, [1, 2, 2, 1], padding='SAME')
    hidden = tf.nn.relu(conv + layer1_biases)
    conv = tf.nn.conv2d(hidden, layer2_weights, [1, 2, 2, 1], padding='SAME')
    hidden = tf.nn.relu(conv + layer2_biases)
    shape = hidden.get_shape().as_list()
    reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
    hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)
    return tf.matmul(hidden, layer4_weights) + layer4_biases
# Training computation.
logits = model(tf_train_dataset)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels))
# Optimizer.
optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)
# Predictions for the training, validation, and test data.
train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(model(tf_valid_dataset))
test_prediction = tf.nn.softmax(model(tf_test_dataset))
# init saver
g_vars = {'layer1_weights':layer1_weights, 'layer1_biases':layer1_biases,
          'layer2_weights':layer2_weights, 'layer2_biases':layer2_biases,
          'layer3_weights':layer3_weights, 'layer3_biases':layer3_biases, 
          'layer4_weights':layer4_weights, 'layer4_biases':layer4_biases
          }
saver = tf.train.Saver(g_vars) 
num_steps = 10000


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print('variables initialized')
    for step in range(num_steps):
        offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
        batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
        batch_labels = train_labels[offset:(offset + batch_size), :]
        feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
        _, l, predictions = sess.run([optimizer, loss, train_prediction], feed_dict=feed_dict)
        if (step % 50 == 0):
            print('Minibatch loss at step %d: %f' % (step, l))
            print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
            print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels[:lim_valid_test]))
    print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))
    print('saving trained model')
    savePath = saver.save(sess, 'overlay', global_step=1000)
    print ('saved')
num_steps = 10000
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print('variables initialized')
    for step in range(num_steps):
        offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
        batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
        batch_labels = train_labels[offset:(offset + batch_size), :]
        feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
        _, l, predictions = sess.run([optimizer, loss, train_prediction], feed_dict=feed_dict)
        if (step % 50 == 0):
            print('Minibatch loss at step %d: %f' % (step, l))
            print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
            print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels[:lim_valid_test]))
    print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))
    print('saving trained model')
    savePath = saver.save(sess, 'overlay', global_step=1000)
    print ('saved')
READ ALSO
customising tooltips in holoview

customising tooltips in holoview

does anyone know how to change the hover tooltips when using holoviews with a bokeh backend?

223
Not able to update variable in Pyspark

Not able to update variable in Pyspark

I am trying to update a variable in pyspark and want to use the same in another methodI am using @property in class, when i tested it in python it is working as expected but when i am trying to implement it in pyspark it is not updating the variable

154
How to run 3 scripts in a specific order in a website and how to modify html code with Javascript/Jquery

How to run 3 scripts in a specific order in a website and how to modify html code with Javascript/Jquery

I want to make a simple website with 9 "boxes" (3x3), just like an Instagram profile, and distrubute the 9 most recent posts of a user throughout those boxesI decided to do the scripts that get the posts with Python and Javascript, and the website with, of course,...

211