def random_flip_tf(img, out_np=True): # -img: np array or Tensor, (H,W,C) or (N,H,W,C) # -out_np: whether output is np array or Tensor if tf.is_tensor(img) == False: img = tf.convert_to_tensor(img) img = tfimage.random_flip_left_right(img) img = tfimage.random_flip_up_down(img) if out_np == True: img = img.numpy() return img
def random_img_preproc(x, bright=True, contrast=True, lr_flip=True, hue=True, saturn=True): if x is not None: rv = None num = x.shape[0] counter = 1 blue("Kreprocessing: started to process " + str(num) + ' images') config = tf.ConfigProto( device_count={ "CPU": 6, "GPU": 0 }, # limit to num_cpu_core CPU usage inter_op_parallelism_threads=1, intra_op_parallelism_threads=12, log_device_placement=True) sess = tf.Session(config=config) for i in x: img = kimg.array_to_img(i) if bright: brighten = tfimg.random_brightness(img, 0.2) img = sess.run(brighten) if contrast: contr = tfimg.random_contrast(img, 0.5, 1.5) img = sess.run(contr) if lr_flip: flip = tfimg.random_flip_left_right(img) img = sess.run(flip) if hue: adj_hue = tfimg.random_hue(img, 0.2) img = sess.run(adj_hue) if saturn: sat = tfimg.random_saturation(img, 0.3, 1.7) img = sess.run(sat) raw = np.asarray(img).reshape(1, 64, 64, 3) ''' if rv is None: rv = raw.copy() else: rv = np.concatenate((rv, raw), axis=0)''' if rv is None: rv = [raw.copy()] else: rv.append(raw) flush(counter, ' / ', num) counter += 1 print() sess.close() return np.concatenate(tuple(rv), axis=0)
def random_img_preproc_gpu(x, bright=True, contrast=True, lr_flip=True, hue=True, saturn=True): if x is not None: rv = None num = x.shape[0] counter = 1 blue("Kreprocessing: started to process " + str(num) + ' images') sess = tf.Session() for i in x: img = kimg.array_to_img(i) if bright: brighten = tfimg.random_brightness(img, 0.2) img = sess.run(brighten) if contrast: contr = tfimg.random_contrast(img, 0.5, 1.5) img = sess.run(contr) if lr_flip: flip = tfimg.random_flip_left_right(img) img = sess.run(flip) if hue: adj_hue = tfimg.random_hue(img, 0.2) img = sess.run(adj_hue) if saturn: sat = tfimg.random_saturation(img, 0.3, 1.7) img = sess.run(sat) raw = np.asarray(img).reshape(1, 64, 64, 3) ''' if rv is None: rv = raw.copy() else: rv = np.concatenate((rv, raw), axis=0)''' if rv is None: rv = [raw.copy()] else: rv.append(raw) flush(counter, ' / ', num) counter += 1 print() sess.close() return np.concatenate(tuple(rv), axis=0)
def preprocess_train_image(img: Tensor, label: Any) -> Tensor: img = random_flip_left_right(img) img = resize(img, [*ORIG_IMG_SIZE]) img = random_crop(img, [*INPUT_IMG_SIZE]) return normalize_img(img)