GPU out of memory when using Tensorflow Slim
I have used Tensorflow / Slim to implement ResNext 50 framework. When I run the program with a cardinality (the number of groups used in ResNext, see detailed explanations below) of 32, the GPU is always out of memory, even if I set the batch size to 1. When I use cardinality of 4, I can successfully run the program with a batch size of 16.
------------Update---------------- I find the reason is due to the update of start_dim (see labeled below) does not work. I change the codes locations and the program now works. It's somewhat weird to me. ------------Update End-----------
My system information: Tensorflow: 1.10rc1 OS: Windows 10 Cuda: 8.0 Cudnn: 5.1 Python: 3.5 GPU: one Quadro M4000 with 8GB memory CPU: 16 cores Memory: 64GB
When running Resnet_v1 50, there is no problem on my machine. Therefore, I guess the crash comes from the cardinality which make the framework wider. My codes are copied below.
(For people not familiar with ResNext bottleneck, here is the simple explanation. If input depth is 256, then it first use 1x1 conv to convert it to depth of 128. Then the 128 depths were divided into "cardinality" (32 here) groups, and 3x3 convs were performed within each group with same output depth as input (4 here). All results from the 32 groups are concatenated to form a 128 depth tensor again. Finally a 1x1 conv is performed to get the 256 depth output)
Take input = 256 dim and cardinality = 32 as an example. In my opinion, the division of each sub tensor only has a depth of 4. Even all of them are allocated in the GPU, the union of these tensors only cost two times of the original memory (WxHx128 instead of WxHx64 as in Resnet). All other tensors share the same size. Therefore, the maximum memory should be no more than 2 times of ResNet, which should be simply achieved by setting half of the original batch size (32-->16). However, even if I set batch size to 1, the program still crashes. Could this be a bug in Tensorflow/Slim or some issues in my codes? Thank you
My bottleneck function is defined as:
@slim.add_arg_scope
def bottleneck(inputs, depth, depth_bottleneck, cardinality, stride, rate=1,
outputs_collections=None, scope=None):
with tf.variable_scope(scope, 'bottleneck_vc', [inputs]) as sc:
depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = resnext_utils.subsample(inputs, stride, 'shortcut')
else:
shortcut = slim.conv2d(inputs, depth, [1, 1], stride=stride,
activation_fn=None, scope='shortcut')
residual = slim.conv2d(inputs, depth_bottleneck, [1, 1], stride=1,
scope='conv1')
residual_merge = []
step = depth_bottleneck / cardinality
start_dim = 0
for i in range(cardinality):
with tf.variable_scope('conv2_%d' % (i + 1)) as local_sc:
end_dim = round(step * (i + 1))
dim_num = end_dim - start_dim
part_residual = tf.slice(residual,
[0,0,0,start_dim], [-1,-1,-1,dim_num])
part_out = resnext_utils.conv2d_same(part_residual, dim_num, 3, stride, rate=rate, scope=local_sc)
if i == 0:
residual_merge = part_out
else:
residual_merge = tf.concat([residual_merge, part_out], 3)
star_dim = end_dim #------------Update: this line does not work-------------
residual = slim.conv2d(residual_merge, depth, [1, 1], stride=1,
activation_fn=None, scope='conv3')
output = tf.nn.relu(shortcut + residual)
return slim.utils.collect_named_outputs(outputs_collections, sc.original_name_scope, output)
The net definition is exact the same as ResNext-50 mentioned in the paper. Therefore, theoretically, the number of parameters is even smaller than ResNet-50. However, ResNet-50 can run without problem with batch size 32, while ResNext-50 cannot run even with batch size 1.
My network definition is as below, where the "32" is the cardinality.
def resnext_vc_50(inputs,
num_classes=None,
is_training=True,
global_pool=True,
output_stride=None,
reuse=None,
scope='resnext_vc_50'):
blocks = [
resnext_utils.Block(
'block1', bottleneck, [(256, 128, 32, 1)] * 2 + [(256, 128, 32, 2)]),
resnext_utils.Block(
'block2', bottleneck, [(512, 256, 32, 1)] * 3 + [(512, 256, 32, 2)]),
resnext_utils.Block(
'block3', bottleneck, [(1024, 512, 32, 1)] * 5 + [(1024, 512, 32, 2)]),
resnext_utils.Block(
'block4', bottleneck, [(2048, 1024, 32, 1)] * 3)
]
return resnext_vc(inputs, blocks, num_classes, is_training,
global_pool=global_pool, output_stride=output_stride,
include_root_block=True, reuse=reuse, scope=scope)