[PYTHON] How to change the generated image of GAN to a high quality one to your liking

1.First of all

Normally, ** trained GAN model generated images ** are a mixture of ** favorite images, disliked images, and broken images **, and cannot be viewed as they are. ** Therefore, this time, I will introduce how to easily change the generated image of GAN ** to a high quality one to your liking **.

The trained model of GAN used this time is ** TF-Hub Progressive GAN **. The code was created with ** Goole colab ** and posted on ** Github ** (** link here ** )) So, if you like, please play with it while having a drink.

2. How do you do it?

The trained GAN model cannot be modified because ** the input vector and the output image are already determined on a one-to-one basis **. Then what should we do. Change the input vector.

When a random vector is input to the GAN model and an image is generated, if there is a ** broken image or an image that you dislike **, the corresponding vector is accumulated in the ** minus vector ** and ** your preference. If there is an image ** of, the corresponding vector is stored in ** plus vector **.

Then, the next vector to be input to the GAN model is ** random vector + mean of plus vector-mean of minus vector **. By repeating this, mysteriously, the generated image of GAN will be ** transformed into a high quality one to your liking **.

3. Implement

First, install and import the library, define the function, and download the model. See google colab for details.

First, initialize it.

#Initialization of learning
tf.random.set_seed(80)  
vectors = tf.zeros([5,512])
plus_vector = tf.zeros([1,512])
minus_vector = tf.zeros([1,512])

Since the ** Form function ** of google colab is used, the code cannot be seen in the initial state, but you can see the code by clicking the UI. The initial state is easier to operate.

#@title please, you change the selected action if you need. 
vec_0 = 'nothing' #@param ['plus', 'nothing', 'minus']
vec_1 = 'nothing' #@param ['plus', 'nothing', 'minus']
vec_2 = 'nothing' #@param ['plus', 'nothing', 'minus']
vec_3 = 'nothing' #@param ['plus', 'nothing', 'minus']
vec_4 = 'nothing' #@param ['plus', 'nothing', 'minus']
vec = [vec_0, vec_1, vec_2, vec_3, vec_4]

for i in range(len(vectors)):
    if vec[i] == 'plus':
      plus_vector = tf.concat([plus_vector, tf.reshape(vectors[i],[1,512])],axis=0)

    if vec[i] == 'minus':
      minus_vector = tf.concat([minus_vector, tf.reshape(vectors[i],[1,512])], axis=0)

print('number of plus_vector = ', len(plus_vector)-1)
print('number of minus_vector = ', len(minus_vector)-1)

plus_vector_mean = tf.reduce_mean(plus_vector, axis=0) # plus_Take the average of the vector
minus_vector_mean = tf.reduce_mean(minus_vector, axis=0)  # minus_Take the average of the vector

vectors = tf.random.normal([5, 512])  #Random vector acquisition
vectors = vectors + plus_vector_mean - minus_vector_mean  #Vector correction
display_images(vectors)

Due to the ** Form function **, the variables ** vec_0 to vec_4 ** will contain the string ** ('plus','nothing','minus') selected in the UI. If the string is **'plus' **, the corresponding vector is stored in ** plus_vector **, and if it is **'minus' **, the corresponding vector is stored in ** minus_vector **. All you have to do now is take the average with tf.reduce_mean () and add or subtract from the random vector.

** The first time you run the code without changing any settings **. ** From the second time onward **, look at the generated 5 images, and at your own discretion ** whether to put in the minus vector ** (minus), ** whether to put in the plus vector ** (plus), ** what Decide if there is ** (nothing), ** change the settings **, and then ** execute the code **. Repeat this many times.

スクリーンショット 2020-06-23 18.29.57.png

What is displayed is the result of trying about a dozen times. The UI is running in its initial state. This is the result of selecting 5 positive vectors (female of your choice) and 8 negative vectors (broken image). Certainly, there are only women who like it (laughs).

Recommended Posts

How to change the generated image of GAN to a high quality one to your liking
[Go] Create a CLI command to change the extension of the image
How to calculate the volatility of a brand
How to check in Python if one of the elements of a list is in another list
[Ubuntu] How to delete the entire contents of a directory
[python] Change the image file name to a serial number
(Note) How to pass the path of your own module
How to find the scaling factor of a biorthogonal wavelet
How to connect the contents of a list into a string
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to change the color of just the button pressed in Tkinter
How to check the memory size of a variable in Python
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
I summarized how to change the boot parameters of GRUB and GRUB2
[NNabla] How to remove the middle tier of a pre-built network
[EC2] How to take a screen capture of your smartphone with selenium
How to crop the lower right part of the image with Python OpenCV
How to send a visualization image of data created in Python to Typetalk
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Image recognition] How to read the result of automatic annotation with VoTT
[NNabla] How to add a quantization layer to the middle layer of a trained model
How to create a wrapper that preserves the signature of the function to wrap
How to play a video while watching the number of frames (Mac)
Script to change the description of fasta
How to check the version of Django
How to save the feature point information of an image in a file and use it for matching
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
How to count the number of elements in Django and output to a template
[python] How to sort by the Nth Mth element of a multidimensional array
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
Change the data frame of pandas purchase data (id x product) to a dictionary
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to make a Raspberry Pi that speaks the tweets of the specified user
A story of a high school graduate technician trying to predict the survival of the Titanic
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to find the area of the Voronoi diagram
How to code a drone using image recognition
I tried to correct the keystone of the image
Change the decimal point of logging from, to.
How to identify the element with the smallest number of characters in a Python list?
[Ruby] How to replace only a part of the string matched by the regular expression?
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
[NNabla] How to add a new layer between the middle layers of a pre-built network
A memo on how to overcome the difficult problem of capturing FX with AI
How to find the first element that matches your criteria in a Python list
Read the Python-Markdown source: How to create a parser
How to know the port number of the xinetd service
A memo of how to use AIST supercomputer ABCI
A memo to visually understand the axis of pandas.Panel
How to create a submenu with the [Blender] plugin
How to visualize the decision tree model of scikit-learn