def preprocess(x, y): # x: 图片的路径,y:图片的数字编码 x = tf.io.read_file(x) x = tf.image.decode_jpeg(x, channels=3) # RGBA # 图片缩放 # x = tf.image.resize(x, [244, 244]) # 图片旋转 # x = tf.image.rot90(x,2) # 随机水平翻转 x = tf.image.random_flip_left_right(x) # 随机竖直翻转 # x = tf.image.random_flip_up_down(x) # 图片先缩放到稍大尺寸 x = tf.image.resize(x, [244, 244]) # 再随机裁剪到合适尺寸 x = tf.image.random_crop(x, [224, 224, 3]) # x: [0,255]=> -1~1 x = tf.cast(x, dtype=tf.float32) / 255. x = normalize(x) y = tf.convert_to_tensor(y) y = tf.one_hot(y, depth=5) return x, y
def preprocess(x, y): # x: 图片的路径,y:图片的数字编码 x = tf.io.read_file(x) x = tf.image.decode_jpeg(x, channels=3) # RGBA x = tf.image.resize(x, [256, 256]) x = tf.image.random_flip_left_right(x) # x = tf.image.random_flip_up_down(x) x = tf.image.random_brightness(x, max_delta=0.5) # 在某范围随机调整图片亮度 x = tf.image.random_contrast(x, 0.1, 0.6) # 在某范围随机调整图片对比度 x = tf.image.random_crop(x, [224, 224, 3]) # x: [0,255]=> 0~1 x = tf.cast(x, dtype=tf.float32) / 255. x = normalize(x) y = tf.convert_to_tensor(y) y = tf.one_hot(y, depth=5) return x, y