Python, GDAL and building raster attribute tables
The SetValueAsInt method is expecting a python int type, not a numpy uint16 type.
>>> print type(vals[0])
<type 'numpy.uint16'>
The following works:
rat.SetValueAsInt(i, 0, int(vals[i]))
If you use vals = numpy.unique(data).tolist()
instead it will automatically convert each value to a python int type.