allenai / visprog

Official code for VisProg (CVPR 2023 Best Paper!)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in execution step -- "TypeError:can only concatenate str (not "int") to str"

simran-khanuja opened this issue · comments

Hi! Thanks for the great work :)

I was running the NLVR notebook and get the following error for the first two statements. Any idea why?

TypeError                                 Traceback (most recent call last)
Cell In[15], line 1
----> 1 result, prog_state, html_str = interpreter.execute(prog,init_state,inspect=True)

File ~/visprog/engine/utils.py:38, in ProgramInterpreter.execute(self, prog, init_state, inspect)
     36 for prog_step in prog_steps:
     37     if inspect:
---> 38         step_output, step_html = self.execute_step(prog_step,inspect)
     39         html_str += str(step_html) + '<hr>'
     40     else:

File ~/visprog/engine/utils.py:24, in ProgramInterpreter.execute_step(self, prog_step, inspect)
     22 step_name = parse_step(prog_step.prog_str,partial=True)['step_name']
     23 print(step_name)
---> 24 return self.step_interpreters[step_name].execute(prog_step,inspect)

File ~/visprog/engine/step_interpreters.py:103, in EvalInterpreter.execute(self, prog_step, inspect)
    100     step_input = step_input.replace('xor','!=')
    102 step_input = step_input.format(**prog_state)
--> 103 step_output = eval(step_input)
    104 prog_step.state[output_var] = step_output
    105 if inspect:

File <string>:1

TypeError: can only concatenate str (not "int") to str

The program generated is as follows:

ANSWER0=VQA(image=LEFT,question='How many camels are in the image?')
ANSWER1=VQA(image=RIGHT,question='How many camels are in the image?')
ANSWER2=VQA(image=LEFT,question='How many people are in the image?')
ANSWER3=VQA(image=RIGHT,question='How many people are in the image?')
ANSWER4=EVAL(expr='{ANSWER0} + {ANSWER1} > {ANSWER2} + {ANSWER3}')
FINAL_ANSWER=RESULT(var=ANSWER4)
commented

HI! I meet the same problem.:(
If you solve the question,pls tell me how to fix it.

commented

Ok! I have fixed this problem.
image

I find that the problem seems to be that the number is not correctly recognized, we just need to convert him into 'str'.
image

        converted_tokens = []
        for token in tokens:
            if token.isdigit():  
                word = num2words(int(token))  
                converted_tokens.append(f"'{word}'") 
            else:
                converted_tokens.append(token)
        step_input = ' '.join(converted_tokens)

After that, I found that adding single quotes to numbers would achieve the same effect :)
image

        converted_tokens = []
        for token in tokens:
            if token.isdigit():  
                converted_tokens.append(f"'{token}'") 
            else:
                converted_tokens.append(token)
        step_input = ' '.join(converted_tokens)

Have a nice day!

How do you fix the issue, and where the code is?

Yes, the above modifications can make the example nlvr output normally, but it will cause errors in the example gqa. In example gqa, it would result in '1'+'1'='11 'instead of 1+1=2.