在使用numpy中的np.linspace时,出现TypeError: ‘float’ object cannot be interpreted as an integer的错误,出错代码如下:
xx,yy,zz = np.meshgrid(np.linspace(-0.5,0.5,imgs.shape[1]/self.stride),
np.linspace(-0.5,0.5,imgs.shape[2]/self.stride),
np.linspace(-0.5,0.5,imgs.shape[3]/self.stride),indexing ='ij')
通过查资料发现,原来python3中“/”符号是保留小数的,返回的是float类型,而在python2中/”是取整,是int类型。
解决方案:将上述代码中“/”改为“//” 即可,如下:
xx,yy,zz = np.meshgrid(np.linspace(-0.5,0.5,imgs.shape[1]//self.stride),
np.linspace(-0.5,0.5,imgs.shape[2]//self.stride),
np.linspace(-0.5,0.5,imgs.shape[3]//self.stride),indexing ='ij')
就可以解决上面那个报错。