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.
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 **.
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.
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