[PYTHON] I didn't understand the Resize of TensorFlow so I tried to summarize it visually.

Introduction

TensorFlow has a number of functions for resizing.

When I looked it up, there are 6 functions for resize in TensorFlow.

When I read the document, the explanation is written, but it doesn't come out very well.

So I would like to try it out and visually understand how it works.

In conclusion, tf.image.resize_images included the following four features:

So, I will try only the following two.

The images used are Lena, who has a size of 256x256, and a cat, which has a size of 256x170. スクリーンショット 2016-09-06 11.17.15.png

Let's try them one by one.

tf.image.resize_images(images, new_height, new_width, method=0, align_corners=False) resize_images is a function that resizes images to new_height x new_width by the specified method.

A 4D tensor [batch, height, width, channels] or a 3D tensor [height, width, channels] can be given as the input image. If given in 4D, batch conversion of images is possible.

A 4D tensor [batch, new_height, new_width, channels] or a 3D tensor [new_height, new_width, channels] is returned as the return value. This changes according to the input tensor.

First try to reduce

Try reducing the image to 128x128 with the following settings.

tf.image.resize_images(image, 128, 128)

The result is as follows. スクリーンショット 2016-09-06 11.21.07.png

Lena has been able to shrink without problems, but the cat is distorted. In this way, ** If the original aspect ratio is not the same as new_width and new_height, the resized image will be distorted. ** To avoid this, use resize_image_with_crop_or_pad. I'll try this later.

Try changing the method

The resize_images function can take four methods:

The results of trying one by one are as follows: スクリーンショット 2016-09-06 12.08.59.png

It can be seen that the degree of blurring is slightly different from the original image when enlarged. It seems that each has its own uses.

align_corners Below are the images when align_corners is False and True: スクリーンショット 2016-09-06 13.20.00.png

To be honest, I don't really understand the difference, but it seems that True scales the input to (new_height -1) / (height -1), and False scales it to new_height / height. By setting it to True, it seems that the positions of all four corners of the input and output are accurately aligned. I'm not sure what to do for it.

tf.image.resize_image_with_crop_or_pad(image, target_height, target_width) The resize_image_with_crop_or_pad function is a function that trims and / or pad the image to the specified size (target_height x target_width).

Resize the image to target_width and target_height by cropping the center of the image or padding the black image.

Crop the center of the image if the width or height is greater than the specified target_width or target_height, respectively. The following is a case where a 256x170 cat image is resized by specifying 128x128 for target_height and target_width. You can see that the center of the image is cropped. スクリーンショット 2016-09-06 13.53.39.png

Embeds a black image if the width or height is less than target_width or target_height. The following is a case where a 256x170 cat image is resized by specifying 196x196 for target_height and target_width. The center of the image is cropped while being padded up and down. スクリーンショット 2016-09-06 13.57.54.png

bonus

By combining resize_image_with_crop_or_pad and resize_images, you can reduce the image with only padding without cropping. test.jpg

manner

  1. Get the size of the image
  2. Specify the size of the long side in target_height and target_width of the resize_image_with_crop_or_pad function.
  3. Shrink the padded image with resize_images

reference

TensorFlow/image

Recommended Posts

I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
I didn't understand the behavior of numpy's argsort, so I will summarize it.
I tried to summarize the string operations of Python
I tried to find the average of the sequence with TensorFlow
[Machine learning] I tried to summarize the theory of Adaboost
The tree.plot_tree of scikit-learn was very easy and convenient, so I tried to summarize how to use it easily.
[Linux] I tried to summarize the command of resource confirmation system
I tried to summarize the umask command
I tried to summarize the graphical modeling.
I tried to summarize the frequently used implementation method of pytest-mock
I tried to summarize the logical way of thinking about object orientation.
I tried to touch the API of ebay
I tried to correct the keystone of the image
LeetCode I tried to summarize the simple ones
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
I tried to rescue the data of the laptop by booting it on Ubuntu
I tried to summarize how to use matplotlib of python
I tried the MNIST tutorial for beginners of tensorflow.
I want to fully understand the basics of Bokeh
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to classify the voices of voice actors
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
[Linux] I learned LPIC lv1 in 10 days and tried to understand the mechanism of Linux.
I tried to scrape YouTube, but I can use the API, so don't do it.
[Rails] v1.0 came out on google-cloud-vision of gem, so I tried to support it
A Python beginner made a chat bot, so I tried to summarize how to make it.
[Introduction] I tried to implement it by myself while explaining to understand the binary tree
I tried to make it easy to change the setting of authenticated Proxy on Jupyter
I tried to use deep learning to extract the part where the plant is shown from the photo of the balcony, but it didn't work, so I will summarize the contents of trial and error. Part 2
Don't read the official docs until I understand Django's static files
I didn't understand the behavior of numpy's argsort, so I will summarize it.
I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
What is Django? .. ..
I thought "What is Linux?", So I looked it up.
What is the true identity of Python's sort method "sort"? ??
I tried to find the entropy of the image with python
[Horse Racing] I tried to quantify the strength of racehorses
[First COTOHA API] I tried to summarize the old story
I tried refactoring the CNN model of TensorFlow using TF-Slim
I tried to summarize the code often used in Pandas
[Python] I tried to visualize the follow relationship of Twitter
I wrote it in Go to understand the SOLID principle
PyTorch's book was difficult to understand, so I supplemented it
I tried to summarize the commands often used in business
I tried to fight the Local Minimum of Goldstein-Price Function
I want to know the legend of the IT technology world
I tried to summarize how to use the EPEL repository again
I examined Yuki Kashiwagi's facial features to understand TensorFlow [Part 2]
I examined Yuki Kashiwagi's facial features to understand TensorFlow [Part 1]
I want to understand systemd roughly
I tried to implement Autoencoder with TensorFlow
I tried to visualize AutoEncoder with TensorFlow
I want to handle the rhyme part1
I want to handle the rhyme part3
I tried to classify text using TensorFlow
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
Implemented DQN in TensorFlow (I wanted to ...)
I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
I made a function to crop the image of python openCV, so please use it.
I tried to understand it carefully while implementing the algorithm Adaboost in machine learning (+ I deepened my understanding of array calculation)
I tried the TensorFlow tutorial 1st
I tried the TensorFlow tutorial 2nd
I tried to move the ball
I tried to estimate the interval.
I tried to get the index of the list using the enumerate function
I tried to automate the watering of the planter with Raspberry Pi
I tried to build the SD boot image of LicheePi Nano
I looked at the meta information of BigQuery & tried using it
I tried to summarize the commands used by beginner engineers today
[Super basics of Python] I learned the basics of the basics, so I summarized it briefly.
I tried to expand the size of the logical volume with LVM
I tried to improve the efficiency of daily work with Python