🏙 Thebe Experiments with Live Python Coding 03
✒️ Cell #1
!pip install imageio tensorflow tensorflow_hub -q
import os,urllib,pylab as pl,numpy as np,imageio
import tensorflow as tf,tensorflow_hub as hub
from IPython.display import display,HTML
from IPython.core.magic import register_line_magic
os.environ['TFHUB_MODEL_LOAD_FORMAT']='COMPRESSED'
file_path='https://raw.githubusercontent.com/OlgaBelitskaya/data/main/'
folder_path=['humans/','paintings/','decors/','symbols/']
original,style,original_img,style_img='','',[],[]
original_dict={'selfie 1':folder_path[0]+'00_01_001.png',
'selfie 2':folder_path[0]+'00_01_002.png'}
style_sources=['Ivan Aivazovsky','Traditional Pattern','Symbol']
style_paintings=['The Bay of Naples','Gzel','Letter']
style_dict={style_sources[0]+' || '+style_paintings[0]:folder_path[1]+'01_00_001.png',
style_sources[1]+' || '+style_paintings[1]:folder_path[2]+'00_00_011.png',
style_sources[2]+' || '+style_paintings[2]:folder_path[3]+'00_01_00_00_001.png'}
def load_img(img_path,rotate=True,max_dim=int(512)):
img=tf.io.read_file(img_path)
img=tf.image.decode_image(img,channels=3)
img=tf.image.convert_image_dtype(img,tf.float32)
shape=tf.cast(tf.shape(img)[:-int(1)],tf.float32)
new_shape=tf.cast(shape*max_dim/max(shape),tf.int32)
img=tf.image.resize(img,new_shape)
if rotate: img=tf.image.rot90(img)
return img[tf.newaxis,:]
✒️ Cell #2
original_image='selfie 1'
style_source='Ivan Aivazovsky'
style_painting='The Bay of Naples'
max_image_size=512
rotate_original_image=True
rotate_style_image=False
original=original_dict[original_image]
style_exp=style_source+' || '+style_painting
style=style_dict[style_exp]
for file_name in [original,style]:
input_file=urllib.request.urlopen(file_path+file_name)
output_file=open(file_name[file_name.find('/')+int(1):],'wb');
output_file.write(input_file.read())
output_file.close(); input_file.close()
original_img=load_img(original[original.find('/')+int(1):],
rotate=rotate_original_image,max_dim=int(max_image_size))
style_img=load_img(style[style.find('/')+int(1):],
rotate=rotate_style_image,max_dim=int(max_image_size))
pl.figure(figsize=(8,3))
print('shape of the original image: %s'%str(original_img.shape))
pl.subplot(1,2,1); pl.imshow(original_img[int(0)])
print('shape of the style image: %s'%str(style_img.shape))
pl.subplot(1,2,2); pl.imshow(style_img[int(0)]);
✒️ Cell #3
model_path='https://tfhub.dev/google/'
model_dict={'magenta':model_path+'magenta/arbitrary-image-stylization-v1-256/2'}
original_img=load_img(original[original.find('/')+int(1):],
rotate=rotate_original_image,max_dim=int(max_image_size))
style_img=load_img(style[style.find('/')+int(1):],
rotate=rotate_style_image,max_dim=int(max_image_size))
hub_model=hub.load(model_dict['magenta'])
stylized_img=hub_model(
tf.constant(original_img),tf.constant(style_img))[int(0)]
pl.figure(figsize=(5,7))
pl.subplot(1,1,1); pl.imshow(stylized_img.numpy()[int(0)]);
✒️ Cell #4
steps, vectors,imgs=60,[],[]
def interpolate_hypersphere(original_img,stylized_img,steps):
original_img_norm=tf.norm(original_img)
stylized_img_norm=tf.norm(stylized_img)
stylized_img_normalized=stylized_img*(original_img_norm/stylized_img_norm)
for step in range(steps):
interpolated=original_img+(stylized_img_normalized-original_img)*step/steps
interpolated_norm=tf.norm(interpolated)
interpolated_normalized=interpolated*(original_img_norm/interpolated_norm)
vectors.append(interpolated_normalized)
return tf.squeeze(tf.stack(vectors))
imgs=interpolate_hypersphere(original_img,stylized_img,steps).numpy()
imgs=np.array(imgs*255,dtype=np.uint8)
imageio.mimsave('pic.gif',imgs)
from IPython.display import Image
Image(open('pic.gif','rb').read())