Caffe: TypeError: 1.0 has type <class 'float'>, but expected one of: (<class 'int'>, <class 'int'>)

Created on 22 Mar 2018  路  3Comments  路  Source: weiliu89/caffe

Hi everyone,

I need help in the following matter:

executing python3 ssd_coco.py resulted in python2 vs python3 specific errors, as seen in #754 .
I had the same issues but fixed them.
However, after executing again, I ran into following issue:
TypeError: 1.0 has type <class 'float'>, but expected one of: (<class 'int'>, <class 'int'>)

full stack trace:

Traceback (most recent call last):
  File "ssd_coco.py", line 454, in <module>
    print(net.to_proto(), file=f)
  File "/home/frauke/python/caffe/net_spec.py", line 209, in to_proto
    top._to_proto(layers, names, autonames)
  File "/home/frauke/python/caffe/net_spec.py", line 100, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/home/frauke/python/caffe/net_spec.py", line 162, in _to_proto
    _param_names[self.type_name] + '_param'), k, v)
  File "/home/frauke/python/caffe/net_spec.py", line 74, in assign_proto
    getattr(proto, name).extend(val)
  File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/containers.py", line 274, in extend
    new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
  File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/containers.py", line 274, in <listcomp>
    new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
  File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/type_checkers.py", line 123, in CheckValue
    raise TypeError(message)
TypeError: 1.0 has type <class 'float'>, but expected one of: (<class 'int'>, <class 'int'>)

It seems to me that ssd_coco.py doesn't support Python 3. However, that doesn't explain above error ...

Apparently the value checker is expecting an int value (or a tuple of int values?) and got a float type. But which value is meant? Where can I edit the float value to an integer value?

PS: If it helps, I'm executing this on my own trained model. Still, I think the same error occurs when using a supplied model when you are also using python3. I might try to recompile caffe for python 2 in order to see if the error persists, but I don't really want to go down that road.

EDIT: I tried the same on the VOC dataset, as suggested in this README. Still, I have the same error. So it's not the fault of my model, but a Python 2/3 confusion problem.

Any suggestions?
Cheers.

Most helpful comment

Problem solved.
FYI: The result of operation / is float type in Python3 not int type.
change pad = int((3 + (dilation - 1) * 2) - 1) / 2 (any line with /2) in model_libs.py to pad = int((3 + (dilation - 1) * 2) - 1) // 2.

All 3 comments

python examples/ssd/ssd_pascal.py returns same error, dun know how to fix it.

Traceback (most recent call last):
File "examples/ssd/ssd_pascal.py", line 455, in
print(net.to_proto(), file=f)
File "/home/hailing/caffeWeiliu89/caffe/python/caffe/net_spec.py", line 209, in to_proto
top._to_proto(layers, names, autonames)
File "/home/hailing/caffeWeiliu89/caffe/python/caffe/net_spec.py", line 100, in _to_proto
return self.fn._to_proto(layers, names, autonames)
File "/home/hailing/caffeWeiliu89/caffe/python/caffe/net_spec.py", line 162, in _to_proto
_param_names[self.type_name] + '_param'), k, v)
File "/home/hailing/caffeWeiliu89/caffe/python/caffe/net_spec.py", line 74, in assign_proto
getattr(proto, name).extend(val)
TypeError: 1.0 has type float, but expected one of: int, long

Problem solved.
FYI: The result of operation / is float type in Python3 not int type.
change pad = int((3 + (dilation - 1) * 2) - 1) / 2 (any line with /2) in model_libs.py to pad = int((3 + (dilation - 1) * 2) - 1) // 2.

@WUhailing Thanks! I also needed to change some /'s to //'s in ssd_coco.py. I opened a pull request for both issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stoneyang picture stoneyang  路  3Comments

zhenxing9968 picture zhenxing9968  路  7Comments

kyuusaku picture kyuusaku  路  6Comments

dakatapetrov picture dakatapetrov  路  5Comments

tumitx picture tumitx  路  5Comments