TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('uint8') with casting rule 's

【背景】keras中ImageDataGenerator使用image crop来进行数据增强,参考:https://blog.csdn.net/u012348774/article/details/78612869

【报错】TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'

【原因】crop后的图片要使用numpy转为浮点类型才能在ImageDataGenerator进行fit

 

【代码】


def random_crop_image(image):
      height, width = image.shape[:2] # image.shape[:2] like (466, 700)
      random_array = np.random.random(size=4)
      w = int((width*0.5)*(1+random_array[0]*0.5))
      h = int((height*0.5)*(1+random_array[1]*0.5))
      x = int(random_array[2]*(width-w))
      y = int(random_array[3]*(height-h))

      image_crop = image[y:h+y,x:w+x,0:3]
      image_crop = misc.imresize(image_crop,image.shape)
      return image_crop.astype('float64') # 这一句解决了bug

train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=5,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=5,
    channel_shift_range=80,
    preprocessing_function=random_crop_image,
    zoom_range=0.1,
    horizontal_flip=True,
    fill_mode='nearest')

 

【致谢】感谢https://github.com/keras-team/keras/issues/3765@keskarnitish  

你可能感兴趣的:(Python,#,DL-报错,#,DL-数据预处理)