def preprocess_image(image, label, is_training): '''数据预处理''' if is_training: image, label = preprocessing.random_rescale_image_and_label( image, label, _MIN_SCALE, _MAX_SCALE) image, label = preprocessing.random_crop_or_pad_image_and_label( image, label, _HEIGHT, _WIDTH, _IGNORE_LABEL) image, label = preprocessing.random_filp_left_right_image_and_label( image, label) image.set_shape([_HEIGHT, _WIDTH, 3]) label.set_shape([_HEIGHT, _WIDTH, 1]) image = preprocessing.mean_image_subtraction(image) return image, label
def preprocess_image(image, label, is_training): """Preprocess a single image of layout [height, width, depth].""" if is_training: # Randomly scale the image and label. image, label = preprocessing.random_rescale_image_and_label( image, label, _MIN_SCALE, _MAX_SCALE) # Randomly crop or pad a [_HEIGHT, _WIDTH] section of the image and label. image, label = preprocessing.random_crop_or_pad_image_and_label( image, label, _HEIGHT, _WIDTH, _IGNORE_LABEL) # Randomly flip the image and label horizontally. image, label = preprocessing.random_flip_left_right_image_and_label( image, label) image.set_shape([_HEIGHT, _WIDTH, 3]) label.set_shape([_HEIGHT, _WIDTH, 1]) if 'B2' in FLAGS.bands: image = preprocessing.mean_image_subtraction(image, S2=True) elif 'R' in FLAGS.bands: image = preprocessing.mean_image_subtraction(image, S2=False) return image, label
def preprocess_image(image, sem, disp, is_training): """Preprocess a single image of layout [height, width, depth].""" disp_ori = disp disp_ori.set_shape([_EVAL_HEIGHT, _EVAL_WIDTH, 1]) if is_training: # Randomly scale the image and label. image, sem, disp = preprocessing.random_rescale_image_and_label( image, sem, disp, _HEIGHT, _WIDTH, _EVAL_WIDTH / _WIDTH, _MIN_SCALE, _MAX_SCALE) # Randomly crop or pad a [_HEIGHT, _WIDTH] section of the image and label. image, sem, disp = preprocessing.random_crop_or_pad_image_and_label( image, sem, disp, _HEIGHT, _WIDTH, _IGNORE_LABEL) # Randomly flip the image and label horizontally. image, sem, disp = preprocessing.random_flip_left_right_image_and_label( image, sem, disp) image.set_shape([_HEIGHT, _WIDTH, 3]) sem.set_shape([_HEIGHT, _WIDTH, 1]) disp.set_shape([_HEIGHT, _WIDTH, 1]) else: image.set_shape([None, None, 3]) image = tf.image.resize_images(image, (_HEIGHT, _WIDTH), method=tf.image.ResizeMethod.BILINEAR) sem.set_shape([None, None, 1]) sem = tf.image.resize_images(sem, (_HEIGHT, _WIDTH), method=tf.image.ResizeMethod.BILINEAR) sem = tf.to_int32(sem) disp.set_shape([None, None, 1]) disp = tf.image.resize_images(disp, (_HEIGHT, _WIDTH), method=tf.image.ResizeMethod.BILINEAR) disp = tf.to_int32(disp) image = preprocessing.mean_image_subtraction(image) disp = preprocessing.normalization(disp, dataset=FLAGS.dataset) return image, sem, disp
def decode_powerline_func(x): features = tf.io.parse_single_example(x, feature) jpg_array_img = tf.io.decode_jpeg(features['image'], channels=3) jpg_array_gt = tf.io.decode_jpeg(features['gt'], channels=1) initial_width = tf.cast(tf.shape(jpg_array_img)[0], tf.float32) initial_height = tf.cast(tf.shape(jpg_array_img)[1], tf.float32) if Mode_128: # If the shape of image is 128x128, uncomment this: img = jpg_array_img gt = jpg_array_gt else: # If the shape of image IS NOT 128x128, the following is not required. lo_dim = 130.0 # Take the greater value, and use it for the ratio min_ = tf.minimum(initial_width, initial_height) ratio = min_ / lo_dim new_width = tf.cast(initial_width / ratio, tf.int32) new_height = tf.cast(initial_height / ratio, tf.int32) img = tf.image.resize_images(jpg_array_img, (new_width, new_height), preserve_aspect_ratio=False) img = tf.cast(img, tf.uint8) img = tf.image.random_crop(img, (128, 128, 3), 1) gt = tf.image.resize_images(jpg_array_gt, (new_width, new_height), preserve_aspect_ratio=False) gt = tf.cast(gt, tf.uint8) gt = tf.image.random_crop(gt, (128, 128, 1), 1) if Random_Size: exp = tf.random.uniform(()) img = tf.image.resize_images(img, (128 + tf.cast(256 * exp, tf.int32), 128 + tf.cast(256 * exp, tf.int32)), preserve_aspect_ratio=False) img = tf.image.random_crop(img, (128, 128, 3)) if Hue_Distort and not Gray_Mode: img = tf.image.random_hue(img, 0.5) img, gt = tf.cond(tf.random.uniform(()) > 0.5, lambda: (tf.image.flip_left_right(img), tf.image.flip_left_right(gt)), lambda: (img, gt)) gt = tf.where(gt > 128, tf.constant(1, tf.uint8, (128, 128, 1)), tf.constant(0, tf.uint8, (128, 128, 1)), name='binary_img') img = tf.cast(img, tf.float32) features['image'] = preprocessing.mean_image_subtraction(img) features['gt'] = gt return features
def _parse_function(filename, is_label): if not is_label: image_filename, label_filename = filename, None else: image_filename, label_filename = filename image_string = tf.read_file(image_filename) image = tf.image.decode_image(image_string) image = tf.to_float(tf.image.convert_image_dtype(image, dtype=tf.uint8)) image.set_shape([None, None, 4]) image = preprocessing.mean_image_subtraction(image) if not is_label: return image else: label_string = tf.read_file(label_filename) label = tf.image.decode_image(label_string) label = tf.to_int32( tf.image.convert_image_dtype(label, dtype=tf.uint8)) label.set_shape([None, None, 1]) return image, label
image, label = preprocessing.random_rescale_image_and_label( image, label, _MIN_SCALE, _MAX_SCALE) # Randomly crop or pad a [_HEIGHT, _WIDTH] section of the image and label. image, label = preprocessing.random_crop_or_pad_image_and_label( image, label, _HEIGHT, _WIDTH, _IGNORE_LABEL) # Randomly flip the image and label horizontally. image, label = preprocessing.random_flip_left_right_image_and_label( image, label) ''' image.set_shape([_HEIGHT, _WIDTH, 3]) label.set_shape([_HEIGHT, _WIDTH, 1]) image = preprocessing.mean_image_subtraction(image) return image, label def input_fn(is_training, data_dir, batch_size, num_epochs=1): """Input_fn using the tf.data input pipeline for CIFAR-10 dataset. Args: is_training: A boolean denoting whether the input is for training. data_dir: The directory containing the input data. batch_size: The number of samples per batch. num_epochs: The number of epochs to repeat the dataset. Returns: A tuple of images and labels.
def main(): image = tf.placeholder(tf.float32, [None, None, 3]) inputs = preprocessing.mean_image_subtraction(image) inputs = tf.expand_dims(inputs, axis=0) model = deeplab_model.model_generator( FLAGS.num_classes, FLAGS.output_stride, FLAGS.base_architecture, FLAGS.pre_trained_model, None, ) logits = model(inputs, False) #预测类别shape[batch,h,w,1] pred_classes = tf.expand_dims(tf.argmax(logits, axis=3, output_type=tf.int32), axis=3) #图片上色形式shape[batch,h,w,3] pred_decoded_labels = tf.py_func(preprocessing.decode_labels, [pred_classes, 1, FLAGS.num_classes], tf.uint8) pred_decoded_labels = tf.squeeze(pred_decoded_labels) saver = tf.train.Saver() sess = tf.Session() model_file = tf.train.latest_checkpoint(FLAGS.model_dir) saver.restore(sess, model_file) if FLAGS.test_mode == '1': for filename in os.listdir(FLAGS.pictue): x = cv2.imread(FLAGS.pictue + filename) x1 = cv2.cvtColor(x, cv2.COLOR_BGR2RGB) out = sess.run(pred_decoded_labels, feed_dict={image: x1}) out = cv2.cvtColor(out, cv2.COLOR_RGB2BGR) out = np.concatenate([x, out], axis=1) cv2.imshow('im', out) k = cv2.waitKey(0) & 0xFF if k == 27: cv2.imwrite(FLAGS.output + filename, out) cv2.destroyAllWindows() if FLAGS.test_mode == '2': cap = cv2.VideoCapture(0) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(FLAGS.output + 'out.mp4', fourcc, 10, (1280, 480)) while True: ret, frame = cap.read() if ret == True: frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) result = sess.run(pred_decoded_labels, feed_dict={image: frame1}) result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR) result1 = np.concatenate([frame, result], axis=1) a = out.write(result1) cv2.imshow("result", result1) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows() sess.close()