def make_multi_crop_batch(image_data, coder): """Process a single image file. Args: filename: string, path to an image file e.g., '/path/to/example.JPG'. coder: instance of ImageCoder to provide TensorFlow image coding utils. Returns: image_buffer: string, JPEG encoding of RGB image. """ image_data[:, :, 2] = np.ones([image_data.shape[0], image_data.shape[1] ]) * 50 / 255.0 image = image_data crops = [] print('Running multi-cropped image') h = image.shape[0] w = image.shape[1] hl = h - RESIZE_FINAL wl = w - RESIZE_FINAL crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) crops.append(standardize_image(crop)) crops.append(standardize_image(tf.image.flip_left_right(crop))) corners = [(0, 0), (0, wl), (hl, 0), (hl, wl), (int(hl / 2), int(wl / 2))] for corner in corners: ch, cw = corner cropped = tf.image.crop_to_bounding_box(image, ch, cw, RESIZE_FINAL, RESIZE_FINAL) crops.append(standardize_image(cropped)) flipped = standardize_image(tf.image.flip_left_right(cropped)) crops.append(standardize_image(flipped)) image_batch = tf.stack(crops) return image_batch
def make_batch(filename, coder, multicrop): """Process a single image file. Args: filename: string, path to an image file e.g., '/path/to/example.JPG'. coder: instance of ImageCoder to provide TensorFlow image coding utils. Returns: image_buffer: string, JPEG encoding of RGB image. height: integer, image height in pixels. width: integer, image width in pixels. """ # Read the image file. with tf.gfile.FastGFile(filename, 'rb') as f: image_data = f.read() # Convert any PNG to JPEG's for consistency. if _is_png(filename): print('Converting PNG to JPEG for %s' % filename) image_data = coder.png_to_jpeg(image_data) image = coder.decode_jpeg(image_data) crops = [] if multicrop is False: print('Running a single image') crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) image = standardize_image(crop) crops.append(image) else: print('Running multi-cropped image') h = image.shape[0] w = image.shape[1] hl = h - RESIZE_FINAL wl = w - RESIZE_FINAL crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) crops.append(standardize_image(crop)) crops.append(tf.image.flip_left_right(crop)) corners = [(0, 0), (0, wl), (hl, 0), (hl, wl), (int(hl / 2), int(wl / 2))] for corner in corners: ch, cw = corner cropped = tf.image.crop_to_bounding_box(image, ch, cw, RESIZE_FINAL, RESIZE_FINAL) crops.append(standardize_image(cropped)) flipped = tf.image.flip_left_right(cropped) crops.append(standardize_image(flipped)) image_batch = tf.stack(crops) print("一共多少张图片", image_batch.shape) return image_batch
def make_batch(filename, coder, multicrop): """Process a single image file. Args: filename: string, path to an image file e.g., '/path/to/example.JPG'. coder: instance of ImageCoder to provide TensorFlow image coding utils. Returns: image_buffer: string, JPEG encoding of RGB image. height: integer, image height in pixels. width: integer, image width in pixels. """ # Read the image file. with tf.gfile.FastGFile(filename, 'r') as f: image_data = f.read() # Convert any PNG to JPEG's for consistency. if _is_png(filename): print('Converting PNG to JPEG for %s' % filename) image_data = coder.png_to_jpeg(image_data) image = coder.decode_jpeg(image_data) crops = [] if multicrop is False: print('Running a single image') crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) image = standardize_image(crop) crops.append(image) else: print('Running multi-cropped image') h = image.shape[0] w = image.shape[1] hl = h - RESIZE_FINAL wl = w - RESIZE_FINAL crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) crops.append(standardize_image(crop)) crops.append(tf.image.flip_left_right(crop)) corners = [ (0, 0), (0, wl), (hl, 0), (hl, wl), (int(hl/2), int(wl/2))] for corner in corners: ch, cw = corner cropped = tf.image.crop_to_bounding_box(image, ch, cw, RESIZE_FINAL, RESIZE_FINAL) crops.append(standardize_image(cropped)) flipped = tf.image.flip_left_right(cropped) crops.append(standardize_image(flipped)) image_batch = tf.stack(crops) return image_batch
def make_multi_image_batch(filenames, coder): """Process a multi-image batch, each with a single-look Args: filenames: list of paths coder: instance of ImageCoder to provide TensorFlow image coding utils. Returns: image_buffer: string, JPEG encoding of RGB image. """ images = [] for filename in filenames: with tf.gfile.FastGFile(filename, 'rb') as f: image_data = f.read() # Convert any PNG to JPEG's for consistency. if _is_png(filename): print('Converting PNG to JPEG for %s' % filename) image_data = coder.png_to_jpeg(image_data) image = coder.decode_jpeg(image_data) crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) image = standardize_image(crop) images.append(image) image_batch = tf.stack(images) return image_batch
def make_multi_crop_batch(filename, coder): """Process a single image file. Args: filename: string, path to an image file e.g., '/path/to/example.JPG'. coder: instance of ImageCoder to provide TensorFlow image coding utils. Returns: image_buffer: string, JPEG encoding of RGB image. """ # Read the image file. with tf.gfile.FastGFile(filename, 'rb') as f: image_data = f.read() # Convert any PNG to JPEG's for consistency. if _is_png(filename): print('Converting PNG to JPEG for %s' % filename) image_data = coder.png_to_jpeg(image_data) image = coder.decode_jpeg(image_data) # (256,256,3) crops = [] print('Running multi-cropped image') h = image.shape[0] w = image.shape[1] hl = h - RESIZE_FINAL # RESIZE_FINAL=227,h1=29 wl = w - RESIZE_FINAL crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) # 三维矩阵中的数字均值变为0,方差变为1 crops.append(standardize_image(crop)) # 左右翻转 crops.append(standardize_image(tf.image.flip_left_right(crop))) corners = [(0, 0), (0, wl), (hl, 0), (hl, wl), (int(hl / 2), int(wl / 2))] for corner in corners: ch, cw = corner cropped = tf.image.crop_to_bounding_box(image, ch, cw, RESIZE_FINAL, RESIZE_FINAL) crops.append(standardize_image(cropped)) flipped = standardize_image(tf.image.flip_left_right(cropped)) crops.append(standardize_image(flipped)) image_batch = tf.stack(crops) return image_batch
def decode(self, raw_bytes): dec = tf.image.decode_jpeg(raw_bytes, channels=3) res = tf.image.resize_images(dec, (RESIZE_FINAL, RESIZE_FINAL)) stand = standardize_image(res) return stand