TypeError: 'int' object is not iterable while training a neural network

Issue

I am trying to train NeRF (https://github.com/bmild/nerf), below is the error I am getting. I have tried to print the datatype of the of the variables. I do not know how to proceed next.

Here is a code snippet:

In run_nerf_helpers.py:

def init_nerf_model(D=8, W=256, input_ch=3, input_ch_views=3, output_ch=4, skips=[4], use_viewdirs=False):

    relu = tf.keras.layers.ReLU()
    def dense(W, act=relu): return tf.keras.layers.Dense(W, activation=act)

    print('MODEL', input_ch, input_ch_views, type(
        input_ch), type(input_ch_views), use_viewdirs)
    input_ch = int(input_ch)
    input_ch_views = int(input_ch_views)

    inputs = tf.keras.Input(shape=(input_ch + input_ch_views))
    inputs_pts, inputs_views = tf.split(inputs, [input_ch, input_ch_views], -1)
    inputs_pts.set_shape([None, input_ch])
    inputs_views.set_shape([None, input_ch_views])

    print(inputs.shape, inputs_pts.shape, inputs_views.shape)
    outputs = inputs_pts
    for i in range(D):
        outputs = dense(W)(outputs)
        if i in skips:
            outputs = tf.concat([inputs_pts, outputs], -1)

In run_nerf.py

def create_nerf(args):
    """Instantiate NeRF's MLP model."""

    embed_fn, input_ch = get_embedder(args.multires, args.i_embed)

    input_ch_views = 0
    embeddirs_fn = None
    if args.use_viewdirs:
        embeddirs_fn, input_ch_views = get_embedder(
            args.multires_views, args.i_embed)
    output_ch = 4
    skips = [4]
    model = init_nerf_model(
        D=args.netdepth, W=args.netwidth,
        input_ch=input_ch, output_ch=output_ch, skips=skips,
        input_ch_views=input_ch_views, use_viewdirs=args.use_viewdirs)
    grad_vars = model.trainable_variables
    models = {'model': model}

def create_nerf(args):
    """Instantiate NeRF's MLP model."""

    embed_fn, input_ch = get_embedder(args.multires, args.i_embed)

    input_ch_views = 0
    embeddirs_fn = None
    if args.use_viewdirs:
        embeddirs_fn, input_ch_views = get_embedder(
            args.multires_views, args.i_embed)
    output_ch = 4
    skips = [4]
    model = init_nerf_model(
        D=args.netdepth, W=args.netwidth,
        input_ch=input_ch, output_ch=output_ch, skips=skips,
        input_ch_views=input_ch_views, use_viewdirs=args.use_viewdirs)
    grad_vars = model.trainable_variables
    models = {'model': model}

Error:

('input_ch_views:', <type 'int'>)
('output_ch:', <type 'int'>)
('use_viewdirs:', <type 'bool'>)
('MODEL', 63, 27, <type 'int'>, <type 'int'>, True)

Traceback (most recent call last):
  File "run_nerf.py", line 938, in <module>
    train()
  File "run_nerf.py", line 682, in train
    args)
  File "run_nerf.py", line 397, in create_nerf
    input_ch_views=input_ch_views, use_viewdirs=args.use_viewdirs)
  File "/home/nerf-master/run_nerf_helpers.py", line 90, in init_nerf_model
    inputs = tf.keras.Input(shape=(input_ch + input_ch_views))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 231, in Input
    input_tensor=tensor)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 91, in __init__
    batch_input_shape = (batch_size,) + tuple(input_shape)
TypeError: 'int' object is not iterable

I am new at this. Any help would be really appreciated. Thank you 🙂

Solution

In scientific libraries, the effort is made on algorithm, and less on type checking. So passed arguments aren’t checked as well as in base packages, the error is most of the time in the user code, but it crashes in the depths of the library code itself, which makes it harder to follow.

So you have to follow the traceback up to your code where the error lies. The lower stackframe of your code is:

inputs = tf.keras.Input(shape=(input_ch + input_ch_views))

Input shape parameter is: A shape tuple (integers), not including the batch size.

(input_ch + input_ch_views) doesn’t define a tuple. To define a tuple with only one element inside, you need to add a trailing ,

inputs = tf.keras.Input(shape=(input_ch + input_ch_views,))

reference: How to create a tuple with only one element

Answered By – Jean-François Fabre

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.