<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Load Images2&nbsp;&nbsp;Identity Transform3&nbsp;&nbsp;Rotation<jupyter_code>import numpy as np
from PIL import Image
import tensorflow as tf

from utils import img2array, array2img
from stn import spatial_transformer_network as transformer<jupyter_output><empty_output><jupyter_text>Load Images<jupyter_code>DIMS = (600, 600)
data_dir = './data/'

# load 4 cat images
img1 = img2array(data_dir + 'cat1.jpg', DIMS, expand=True)#, view=True)
img2 = img2array(data_dir + 'cat2.jpg', DIMS, expand=True)
img3 = img2array(data_dir + 'cat3.jpg', DIMS, expand=True)
img4 = img2array(data_dir + 'cat4.jpg', DIMS, expand=True)
input_img = np.concatenate([img1, img2, img3, img4], axis=0)
B, H, W, C = input_img.shape
print("Input Img Shape: {}".format(input_img.shape))<jupyter_output>Input Img Shape: (4, 600, 600, 3)<jupyter_text>Identity TransformWe'll be using an identity transform as a sanity check. This means the output image should look like the input image.<jupyter_code>tf.compat.v1.disable_eager_execution()
# identity transform
theta = np.array([[1., 0, 0], [0, 1., 0]])
x = tf.compat.v1.placeholder(tf.float32, [None, H, W, C])

with tf.compat.v1.variable_scope('spatial_transformer'):
    theta = theta.astype('float32')
    theta = theta.flatten()

    # define loc net weight and bias
    loc_in = H*W*C
    loc_out = 6
    W_loc = tf.Variable(tf.zeros([loc_in, loc_out]), name='W_loc')
    b_loc = tf.Variable(initial_value=theta, name='b_loc')
    
    # tie everything together
    fc_loc = tf.matmul(tf.zeros([B, loc_in]), W_loc) + b_loc
    h_trans = transformer(x, fc_loc)
# run session
sess = tf.compat.v1.Session()
sess.run(tf.compat.v1.global_variables_initializer())
y = sess.run(h_trans, feed_dict={x: input_img})
print("y: {}".format(y.shape))
array2img(y[0]).show()<jupyter_output>y: (4, 600, 600, 3)<jupyter_text>RotationLet's try rotating the picture by 45 degrees.<jupyter_code>from utils import deg2rad
# initialize affine transform tensor `theta`
degree = 45
theta = np.array([
    [np.cos(deg2rad(degree)), -np.sin(deg2rad(degree)), 0], 
    [np.sin(deg2rad(degree)), np.cos(deg2rad(degree)), 0]
])
x = tf.compat.v1.placeholder(tf.float32, [None, H, W, C])

with tf.compat.v1.variable_scope('spatial_transformer'):
    theta = theta.astype('float32')
    theta = theta.flatten()

    # define loc net weight and bias
    loc_in = H*W*C
    loc_out = 6
    W_loc = tf.Variable(tf.zeros([loc_in, loc_out]), name='W_loc')
    b_loc = tf.Variable(initial_value=theta, name='b_loc')
    
    # tie everything together
    fc_loc = tf.matmul(tf.zeros([B, loc_in]), W_loc) + b_loc
    h_trans = transformer(x, fc_loc)
# run session
sess = tf.compat.v1.Session()
sess.run(tf.compat.v1.global_variables_initializer())
y = sess.run(h_trans, feed_dict={x: input_img})
print("y: {}".format(y.shape))
array2img(y[0]).show()<jupyter_output>y: (4, 600, 600, 3)
{'fraction_non_alphanumeric': 0.15182531559194815, 'fraction_numerical': 0.03172978505629478, 'mean_word_length': 5.172631578947368, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '60644', 'license': 'MIT', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1117, 'n_words': 245, 'path': 'Sanity Check.ipynb', 'repo_name': 'NewLuminous/spatial-transformer-network', 'size': '2931'}