Перейти к содержимому

Как tensor сохранить в jpeg

  • автор:

Как преобразовать каталог изображений jpeg в файл TFRecords в tensorflow?

У меня есть учебные данные, которые являются каталогом изображений jpeg и соответствующим текстовым файлом, содержащим имя файла и связанную метку категории. Я пытаюсь преобразовать эти учебные данные в файл tfrecords, как описано в документации tensorflow. Я потратил достаточно времени, пытаясь заставить это работать, но в tensorflow нет примеров, которые демонстрируют, как использовать любой из читателей для чтения в файлах jpeg и добавления их в tfrecord с помощью tfrecordwriter

Поделиться Источник 21 ноября 2015 в 22:57

8 ответов

Я надеюсь, это поможет:

filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) # list of files to read reader = tf.WholeFileReader() key, value = reader.read(filename_queue) my_img = tf.image.decode_png(value) # use decode_png or decode_jpeg decoder based on your files. init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) # Start populating the filename queue. coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for i in range(1): #length of your filename list image = my_img.eval() #here is your image Tensor :) print(image.shape) Image.show(Image.fromarray(np.asarray(image))) coord.request_stop() coord.join(threads) 

Для получения всех изображений в виде массива тензоров используйте следующий пример кода.

В предыдущем ответе я только что рассказал, как прочитать изображение в формате TF, но не сохраняя его в TFRecords. Для этого вы должны использовать:

def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) # images and labels array as input def convert_to(images, labels, name): num_examples = labels.shape[0] if images.shape[0] != num_examples: raise ValueError("Images size %d does not match label size %d." % (images.shape[0], num_examples)) rows = images.shape[1] cols = images.shape[2] depth = images.shape[3] filename = os.path.join(FLAGS.directory, name + '.tfrecords') print('Writing', filename) writer = tf.python_io.TFRecordWriter(filename) for index in range(num_examples): image_raw = images[index].tostring() example = tf.train.Example(features=tf.train.Features(feature=< 'height': _int64_feature(rows), 'width': _int64_feature(cols), 'depth': _int64_feature(depth), 'label': _int64_feature(int(labels[index])), 'image_raw': _bytes_feature(image_raw)>)) writer.write(example.SerializeToString()) 

И вы читаете данные так:

# Remember to generate a file name queue of you 'train.TFRecord' file path def read_and_decode(filename_queue): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, dense_keys=['image_raw', 'label'], # Defaults are not specified since both keys are required. dense_types=[tf.string, tf.int64]) # Convert from a scalar string tensor (whose single string has image = tf.decode_raw(features['image_raw'], tf.uint8) image = tf.reshape(image, [my_cifar.n_input]) image.set_shape([my_cifar.n_input]) # OPTIONAL: Could reshape into a 28x28 image and apply distortions # here. Since we are not applying any distortions in this # example, and the next step expects the image to be flattened # into a vector, we don't bother. # Convert from [0, 255] -> [-0.5, 0.5] floats. image = tf.cast(image, tf.float32) image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 # Convert label from a scalar uint8 tensor to an int32 scalar. label = tf.cast(features['label'], tf.int32) return image, label 

Поделиться 23 ноября 2015 в 01:43

Модель начала Tensorflow имеет файл build_image_data.py, который может выполнить то же самое, предполагая, что каждый подкаталог представляет метку.

Поделиться 09 июня 2016 в 20:42

Обратите внимание, что изображения будут сохранены в TFRecord как несжатые тензоры, возможно, увеличивая размер на коэффициент около 5. Это тратит место на хранение и, вероятно, будет довольно медленным из-за количества данных, которые нужно прочитать.

Лучше просто сохранить имя файла в TFRecord и прочитать файл по требованию. Новый API Dataset работает хорошо, и документация имеет следующий пример:

# Reads an image from a file, decodes it into a dense tensor, and resizes it # to a fixed shape. def _parse_function(filename, label): image_string = tf.read_file(filename) image_decoded = tf.image.decode_jpeg(image_string) image_resized = tf.image.resize_images(image_decoded, [28, 28]) return image_resized, label # A vector of filenames. filenames = tf.constant(["/var/data/image1.jpg", "/var/data/image2.jpg", . ]) # `labels[i]` is the label for the image in `filenames[i]. labels = tf.constant([0, 37, . ]) dataset = tf.data.Dataset.from_tensor_slices((filenames, labels)) dataset = dataset.map(_parse_function) 

Поделиться 13 июля 2018 в 05:15

У меня тоже есть та же проблема.

Вот как я получаю файлы tfrecords моих собственных jpeg файлов

Редактировать: добавить sol 1 — лучший и более быстрый способ обновления: 5 января 2020 года

(Рекомендуется) Решение 1: TFRecordWriter

Решение 2:

С tensorflow официального github: Как создать новый набор данных для переобучения , используйте официальный скрипт на Python build_image_data.py напрямую и bazel — это лучшая идея.

Чтобы запустить build_image_data.py ,, вы можете запустить следующую командную строку:

# location to where to save the TFRecord data. OUTPUT_DIRECTORY=$HOME/my-custom-data/ # build the preprocessing script. bazel build inception/build_image_data # convert the data. bazel-bin/inception/build_image_data \ --train_directory="$" \ --validation_directory="$" \ --output_directory="$" \ --labels_file="$" \ --train_shards=128 \ --validation_shards=24 \ --num_threads=8 

где $OUTPUT_DIRECTORY — это местоположение зашифрованных TFRecords . $LABELS_FILE будет текстовым файлом, который читается скриптом, который предоставляет список всех меток.

затем это должно сработать.

п.с. bazel, созданный Google, преобразует код в makefile.

Решение 3:

Сначала я ссылаюсь на инструкцию от @capitalistpug и проверяю файл скрипта оболочки от Google: download_and_precess_flowers.sh )

Во-вторых, я также узнал о мини-учебнике по началу-v3 от NVIDIA

Будьте осторожны , следующие шаги должны быть выполнены в среде BAZEL WORKSAPCE

таким образом, файл сборки Bazel может быть успешно запущен

Первый шаг , я закомментирую часть загрузки набора данных imagemet, который я уже скачал

и остальную часть, которая мне не нужна download_and_preprocess_flowers.sh

Второй шаг , измените каталог на tensorflow/models/inception

где это среда Bazel и она была построена Bazel до

$ cd tensorflow/models/inception 

Необязательно: Если она не была построена до этого, введите следующий код в cmd

$ bazel build inception/download_and_preprocess_flowers 

Вам нужно найти содержимое на следующем изображении

enter image description here

И последний шаг , введите следующий код:

$ bazel-bin/inception/download_and_preprocess_flowers $Your/own/image/data/path 

Затем он начнет вызывать build_image_data.py и создавать файл tfrecords

Поделиться 13 февраля 2017 в 09:59

Попробуйте этот скрипт: (используется с набором данных сегментации VOC:http://host.robots.ox.ac.uk/pascal/VOC/voc2012/)

import numpy as np import tensorflow as tf import scipy.io # to read .mat files from PIL import Image # to read image files def get_image(path): jpg = Image.open(path).convert('RGB') return np.array(jpg) def get_label_png(path): png = Image.open(path) # image is saved as palettised png. arr = np.array(png) return arr[. None] def get_example(image, label): feature = < 'height': tf.train.Feature(int64_list=tf.train.Int64List(value=[image.shape[0]])), 'width': tf.train.Feature(int64_list=tf.train.Int64List(value=[image.shape[1]])), 'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image.tobytes()])), 'label': tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tobytes()])) >return tf.train.Example(features=tf.train.Features(feature=feature)) ## Paths ====================================== images_folder = 'data/images/' #images folder labels_folder = 'data/labels/' #label folder train_file = 'data/train.txt' val_file = 'data/val.txt' TRAIN = 'data/train.tfrecords' VAL = 'data/val.tfrecords' ## write train dataset with tf.io.TFRecordWriter(TRAIN) as writer: with open(train_file) as file: filenames = [s.rstrip('\n') for s in file.readlines()] for name in filenames: image = utils.get_image(images_folder+name+'.jpg') label = utils.get_label_png(labels_folder+name+'.png') writer.write(utils.get_example(image, label).SerializeToString()) ## write validation dataset with tf.io.TFRecordWriter(VAL) as writer: with open(val_file) as file: filenames = [s.rstrip('\n') for s in file.readlines()] for name in filenames: image = utils.get_image(images_folder+name+'.jpg') label = utils.get_label_png(labels_folder+name+'.png') writer.write(utils.get_example(image, label).SerializeToString()) 

Поделиться 18 января 2022 в 14:52

Упоминание кода в ссылке, указанной Камилем, чтобы код был доступен, даже если ссылка не будет выполнена.

"""Converts image data to TFRecords file format with Example protos. If your data set involves bounding boxes, please look at build_imagenet_data.py. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from datetime import datetime import os import random import sys import threading import numpy as np import tensorflow as tf tf.app.flags.DEFINE_string('train_directory', '/tmp/', 'Training data directory') tf.app.flags.DEFINE_string('validation_directory', '/tmp/', 'Validation data directory') tf.app.flags.DEFINE_string('output_directory', '/tmp/', 'Output data directory') tf.app.flags.DEFINE_integer('train_shards', 2, 'Number of shards in training TFRecord files.') tf.app.flags.DEFINE_integer('validation_shards', 2, 'Number of shards in validation TFRecord files.') tf.app.flags.DEFINE_integer('num_threads', 2, 'Number of threads to preprocess the images.') # The labels file contains a list of valid labels are held in this file. # Assumes that the file contains entries as such: # dog # cat # flower # where each line corresponds to a label. We map each label contained in # the file to an integer corresponding to the line number starting from 0. tf.app.flags.DEFINE_string('labels_file', '', 'Labels file') FLAGS = tf.app.flags.FLAGS def _int64_feature(value): """Wrapper for inserting int64 features into Example proto.""" if not isinstance(value, list): value = [value] return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) def _bytes_feature(value): """Wrapper for inserting bytes features into Example proto.""" return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def _convert_to_example(filename, image_buffer, label, text, height, width): """Build an Example proto for an example. Args: filename: string, path to an image file, e.g., '/path/to/example.JPG' image_buffer: string, JPEG encoding of RGB image label: integer, identifier for the ground truth for the network text: string, unique human-readable, e.g. 'dog' height: integer, image height in pixels width: integer, image width in pixels Returns: Example proto """ colorspace = 'RGB' channels = 3 image_format = 'JPEG' example = tf.train.Example(features=tf.train.Features(feature=< 'image/height': _int64_feature(height), 'image/width': _int64_feature(width), 'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)), 'image/channels': _int64_feature(channels), 'image/class/label': _int64_feature(label), 'image/class/text': _bytes_feature(tf.compat.as_bytes(text)), 'image/format': _bytes_feature(tf.compat.as_bytes(image_format)), 'image/filename': _bytes_feature(tf.compat.as_bytes(os.path.basename(filename))), 'image/encoded': _bytes_feature(tf.compat.as_bytes(image_buffer))>)) return example class ImageCoder(object): """Helper class that provides TensorFlow image coding utilities.""" def __init__(self): # Create a single Session to run all image coding calls. self._sess = tf.Session() # Initializes function that converts PNG to JPEG data. self._png_data = tf.placeholder(dtype=tf.string) image = tf.image.decode_png(self._png_data, channels=3) self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100) # Initializes function that decodes RGB JPEG data. self._decode_jpeg_data = tf.placeholder(dtype=tf.string) self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3) def png_to_jpeg(self, image_data): return self._sess.run(self._png_to_jpeg, feed_dict=) def decode_jpeg(self, image_data): image = self._sess.run(self._decode_jpeg, feed_dict=) assert len(image.shape) == 3 assert image.shape[2] == 3 return image def _is_png(filename): """Determine if a file contains a PNG format image. Args: filename: string, path of the image file. Returns: boolean indicating if the image is a PNG. """ return '.png' in filename def _process_image(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. 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) # Decode the RGB JPEG. image = coder.decode_jpeg(image_data) # Check that image converted to RGB assert len(image.shape) == 3 height = image.shape[0] width = image.shape[1] assert image.shape[2] == 3 return image_data, height, width def _process_image_files_batch(coder, thread_index, ranges, name, filenames, texts, labels, num_shards): """Processes and saves list of images as TFRecord in 1 thread. Args: coder: instance of ImageCoder to provide TensorFlow image coding utils. thread_index: integer, unique batch to run index is within [0, len(ranges)). ranges: list of pairs of integers specifying ranges of each batches to analyze in parallel. name: string, unique identifier specifying the data set filenames: list of strings; each string is a path to an image file texts: list of strings; each string is human readable, e.g. 'dog' labels: list of integer; each integer identifies the ground truth num_shards: integer number of shards for this data set. """ # Each thread produces N shards where N = int(num_shards / num_threads). # For instance, if num_shards = 128, and the num_threads = 2, then the first # thread would produce shards [0, 64). num_threads = len(ranges) assert not num_shards % num_threads num_shards_per_batch = int(num_shards / num_threads) shard_ranges = np.linspace(ranges[thread_index][0], ranges[thread_index][1], num_shards_per_batch + 1).astype(int) num_files_in_thread = ranges[thread_index][1] - ranges[thread_index][0] counter = 0 for s in range(num_shards_per_batch): # Generate a sharded version of the file name, e.g. 'train-00002-of-00010' shard = thread_index * num_shards_per_batch + s output_filename = '%s-%.5d-of-%.5d' % (name, shard, num_shards) output_file = os.path.join(FLAGS.output_directory, output_filename) writer = tf.python_io.TFRecordWriter(output_file) shard_counter = 0 files_in_shard = np.arange(shard_ranges[s], shard_ranges[s + 1], dtype=int) for i in files_in_shard: filename = filenames[i] label = labels[i] text = texts[i] try: image_buffer, height, width = _process_image(filename, coder) except Exception as e: print(e) print('SKIPPED: Unexpected eror while decoding %s.' % filename) continue example = _convert_to_example(filename, image_buffer, label, text, height, width) writer.write(example.SerializeToString()) shard_counter += 1 counter += 1 if not counter % 1000: print('%s [thread %d]: Processed %d of %d images in thread batch.' % (datetime.now(), thread_index, counter, num_files_in_thread)) sys.stdout.flush() writer.close() print('%s [thread %d]: Wrote %d images to %s' % (datetime.now(), thread_index, shard_counter, output_file)) sys.stdout.flush() shard_counter = 0 print('%s [thread %d]: Wrote %d images to %d shards.' % (datetime.now(), thread_index, counter, num_files_in_thread)) sys.stdout.flush() def _process_image_files(name, filenames, texts, labels, num_shards): """Process and save list of images as TFRecord of Example protos. Args: name: string, unique identifier specifying the data set filenames: list of strings; each string is a path to an image file texts: list of strings; each string is human readable, e.g. 'dog' labels: list of integer; each integer identifies the ground truth num_shards: integer number of shards for this data set. """ assert len(filenames) == len(texts) assert len(filenames) == len(labels) # Break all images into batches with a [ranges[i][0], ranges[i][1]]. spacing = np.linspace(0, len(filenames), FLAGS.num_threads + 1).astype(np.int) ranges = [] for i in range(len(spacing) - 1): ranges.append([spacing[i], spacing[i + 1]]) # Launch a thread for each batch. print('Launching %d threads for spacings: %s' % (FLAGS.num_threads, ranges)) sys.stdout.flush() # Create a mechanism for monitoring when all threads are finished. coord = tf.train.Coordinator() # Create a generic TensorFlow-based utility for converting all image codings. coder = ImageCoder() threads = [] for thread_index in range(len(ranges)): args = (coder, thread_index, ranges, name, filenames, texts, labels, num_shards) t = threading.Thread(target=_process_image_files_batch, args=args) t.start() threads.append(t) # Wait for all the threads to terminate. coord.join(threads) print('%s: Finished writing all %d images in data set.' % (datetime.now(), len(filenames))) sys.stdout.flush() def _find_image_files(data_dir, labels_file): """Build a list of all images files and labels in the data set. Args: data_dir: string, path to the root directory of images. Assumes that the image data set resides in JPEG files located in the following directory structure. data_dir/dog/another-image.JPEG data_dir/dog/my-image.jpg where 'dog' is the label associated with these images. labels_file: string, path to the labels file. The list of valid labels are held in this file. Assumes that the file contains entries as such: dog cat flower where each line corresponds to a label. We map each label contained in the file to an integer starting with the integer 0 corresponding to the label contained in the first line. Returns: filenames: list of strings; each string is a path to an image file. texts: list of strings; each string is the class, e.g. 'dog' labels: list of integer; each integer identifies the ground truth. """ print('Determining list of input files and labels from %s.' % data_dir) unique_labels = [l.strip() for l in tf.gfile.FastGFile( labels_file, 'r').readlines()] labels = [] filenames = [] texts = [] # Leave label index 0 empty as a background class. label_index = 1 # Construct the list of JPEG files and labels. for text in unique_labels: jpeg_file_path = '%s/%s/*' % (data_dir, text) matching_files = tf.gfile.Glob(jpeg_file_path) labels.extend([label_index] * len(matching_files)) texts.extend([text] * len(matching_files)) filenames.extend(matching_files) if not label_index % 100: print('Finished finding files in %d of %d classes.' % ( label_index, len(labels))) label_index += 1 # Shuffle the ordering of all image files in order to guarantee # random ordering of the images with respect to label in the # saved TFRecord files. Make the randomization repeatable. shuffled_index = list(range(len(filenames))) random.seed(12345) random.shuffle(shuffled_index) filenames = [filenames[i] for i in shuffled_index] texts = [texts[i] for i in shuffled_index] labels = [labels[i] for i in shuffled_index] print('Found %d JPEG files across %d labels inside %s.' % (len(filenames), len(unique_labels), data_dir)) return filenames, texts, labels def _process_dataset(name, directory, num_shards, labels_file): """Process a complete data set and save it as a TFRecord. Args: name: string, unique identifier specifying the data set. directory: string, root path to the data set. num_shards: integer number of shards for this data set. labels_file: string, path to the labels file. """ filenames, texts, labels = _find_image_files(directory, labels_file) _process_image_files(name, filenames, texts, labels, num_shards) def main(unused_argv): assert not FLAGS.train_shards % FLAGS.num_threads, ( 'Please make the FLAGS.num_threads commensurate with FLAGS.train_shards') assert not FLAGS.validation_shards % FLAGS.num_threads, ( 'Please make the FLAGS.num_threads commensurate with ' 'FLAGS.validation_shards') print('Saving results to %s' % FLAGS.output_directory) # Run it! _process_dataset('validation', FLAGS.validation_directory, FLAGS.validation_shards, FLAGS.labels_file) _process_dataset('train', FLAGS.train_directory, FLAGS.train_shards, FLAGS.labels_file) if __name__ == '__main__': tf.app.run() 

Поделиться 22 января 2020 в 11:32

В случае слишком большого размера в файлах tfrecord вы используете байты для непосредственного чтения.

вы используете эту функцию для непосредственного чтения байтов.

write_jpeg¶

Takes an input tensor in CHW layout and saves it in a JPEG file.

  • input (Tensor[channels,image_height,image_width]) – int8 image tensor of c channels, where c must be 1 or 3.
  • filename (str) – Path to save the image.
  • quality (int) – Quality of the resulting JPEG file, it must be a number between 1 and 100. Default: 75

© Copyright 2017-present, Torch Contributors.

Built with Sphinx using a theme provided by Read the Docs.

Docs

Access comprehensive developer documentation for PyTorch

Tutorials

Get in-depth tutorials for beginners and advanced developers

Resources

Find development resources and get your questions answered

© Copyright The Linux Foundation. The PyTorch Foundation is a project of The Linux Foundation. For web site terms of use, trademark policy and other policies applicable to The PyTorch Foundation please see www.linuxfoundation.org/policies/. The PyTorch Foundation supports the PyTorch open source project, which has been established as PyTorch Project a Series of LF Projects, LLC. For policies applicable to the PyTorch Project a Series of LF Projects, LLC, please see www.lfprojects.org/policies/.

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.

Сохранение изображения с помощью cv2.imwrite() в OpenCV Python

Чтобы сохранить изображение в локальное хранилище с помощью Python, используйте функцию cv2.imwrite() в библиотеке OpenCV.

Синтаксис

cv2.imwrite(path, image)

Где, path – это полный путь к выходному файлу, в который вы хотите записать массив numpy изображений.

cv2.imwrite() возвращает логическое значение. true, если изображение успешно записано, и false, если изображение не было успешно записано по указанному локальному пути.

Пример 1

В этом примере мы прочитаем изображение, преобразуем его, а затем сохраним изображение в постоянном файловом хранилище с помощью метода imwrite().

import cv2 #read image as grey scale img = cv2.imread('D:/image-1.png') #do some transformations on img #save matrix/array as image file isWritten = cv2.imwrite('D:/image-2.png', img) if isWritten: print('Image is successfully saved as file.')
Image is successfully saved as file.

Пример 2: со случайными значениями

В этом примере мы напишем массив numpy как изображение, используя функцию cv2.imwrite(). Для этого мы создадим массив с тремя каналами для красного, зеленого и синего, содержащий случайные значения. В общих случаях мы читаем изображение, применяем некоторые преобразования к массиву и затем записываем изображение в локальное хранилище. Но в этом примере мы будем придерживаться массива со случайными значениями.

import cv2 import numpy as np img = np.random.randint(255, size=(300, 600, 3)) isWritten = cv2.imwrite('D:/image-2.png', img) if isWritten: print('The image is successfully saved.')
The image is successfully saved.

Ниже приведено изображение, созданное со случайными значениями.

Пример функции imwrite()

В этом руководстве на примерах Python мы узнали, как использовать cv2.imwrite() для сохранения массива numpy в виде изображения.

Загрузка и преобразование изображений в PyTorch

После импорта всех необходимых библиотек и добавления VGG-19 на наше устройство, мы должны загрузить в память изображения, которые мы хотим преобразовать для переноса стиля в PyTorch.

У нас есть изображение контента, изображение стиля и целевое изображение, которые будут комбинацией этих изображений. Не все изображения должны иметь одинаковый размер или пиксели. Чтобы сделать изображения одинаковыми, мы применим процесс их преобразования.

Преобразование изображений для передачи стилей в PyTorch

Загрузка изображения

Мы должны загрузить изображение содержимого и изображение стиля в память, чтобы мы могли выполнить над ним операцию. Процесс загрузки играет важную роль в процессе передачи стиля. Нам нужны изображения в памяти, и процесс передачи стиля будет невозможен до процесса загрузки.

#defining a method with three parameters i.e. image location, maximum size and shape def load_image(img_path,max_size=400,shape=None): # Open the image, convert it into RGB and store in a variable image=Image.open(img_path).convert('RGB') # comparing image size with the maximum size if max(image.size)>max_size: size=max_size else: size=max(image.size) # checking for the image shape if shape is not None: size=shape #Applying appropriate transformation to our image such as Resize, ToTensor and Normalization in_transform=transforms.Compose([ transforms.Resize(size), transforms.ToTensor(), transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))]) #Calling in_transform with our image image=in_transform(image).unsqueeze(0) #unsqueeze(0) is used to add extra layer of dimensionality to the image #Returning image return image #Calling load_image() with our image and add it to our device content=load_image('ab.jpg').to(device) style=load_image('abc.jpg',shape=content.shape[-2:]).to(device)

Преобразование изображения

Перед импортом изображений нам нужно их преобразовать из тензора в изображения numpy, чтобы обеспечить совместимость с пакетом plot. Мы делали это раньше с помощью уже знакомой вспомогательной функции image_converts, которую мы использовали в преобразованиях изображений для распознавания.

def im_convert(tensor): image=tensor.cpu().clone().detach().numpy() image=image.transpose(1,2,0) image=image*np.array((0.5,0.5,0.5))+np.array((0.5,0.5,0.5)) image=image.clip(0,1) return image

Если мы запустим этот вспомогательный метод, он сгенерирует ошибку. Мы должны удалить одномерные записи из формы изображения и из формы массива. Таким образом, мы сожмем наше изображение перед методом транспонирования.

image=image.squeeze()

Построение изображений

fig,(ax1,ax2)=plt.subplots(1,2,figsize=(20,10)) ax1.imshow(im_convert(content)) ax1.axis('off') ax2.imshow(im_convert(style)) ax2.axis('off')

Когда мы запустим его в блокноте Google Colab, он даст нам ожидаемый результат:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *