<jupyter_start><jupyter_text>******inception_v1*** file is given by tensorflow for using inception model.we are using inceptionv1 model for this projectinceptionv1.py defines base architecture for the inception modelinception_v1_base() is the function which contains the defined base architecture for the inception model. This can be easilyread as we see this function.inception_v1 is the actual function to call the inception_v1_base().inception_v1_base() has architecture that distinguishes the googlenet.inception_v1() gets the output from the inception_v1_base() and then it is fruther passed for average pooling and so.This means we can define our own architecture in inception_v1() after getting result from inception_v1_base()*** ***We also have another important library provided by google that is ***inception_preprocessing***.This takes care of the data augmentation, and changes that needed to be made in the input tensor.***<jupyter_code>def prep_image(im):
        
    # Resize so smallest dim = 224, preserving aspect ratio
    h, w, _ = im.shape
    if h < w:
        im = scipy.misc.imresize(im, (224, int(w*224/h)))
    else:
        im = scipy.misc.imresize(im, (int(h*224/w), 224))

    # Central crop to 224x224
    h, w, _ = im.shape
    im = im[h//2-112:h//2+112, w//2-112:w//2+112]
    
    rawim = np.copy(im).astype('uint8')
    
    # Now rescale it to [-1,+1].float32 from [0..255].unit8
    im = ( im.astype('float32')/255.0 - 0.5 ) * 2.0
    return rawim, im


photos = "/home/n/projectdl/content2.jpg"
photo_i=-1 # will be incremented in next cell (i.e. to start at [0])


photo_i += 1
photo = plt.imread(photos)
photo_rawim, photo = prep_image(photo)
photo = photo.reshape((1,224,224,3))
styles = "/home/n/projectdl/color4.jpg"
style_i=-1 # will be incremented in next cell (i.e. to start at [0])

style_i += 1

style = plt.imread(styles)
style_rawim, style = prep_image(style)
style = style.reshape((1,224,224,3))


tf.reset_default_graph()

# This creates an image 'placeholder' - image inputs should be (224,224,3).float32 each [-1.0,1.0]
# input_image_float = tf.placeholder(tf.float32, shape=[1,224, 224, 3], name='input_image_float')
with tf.variable_scope('input') as scope:
            input_image_float = tf.get_variable('in_img', 
                                        shape=([1, 224, 224, 3]),
                                        dtype=tf.float32,
                                        initializer=tf.zeros_initializer())
# Here is the actual model zoo model being instantiated :
with slim.arg_scope(inception.inception_v1_arg_scope()):
    _, end_points = inception.inception_v1(input_image_float, num_classes=1001, is_training=False)

# Create an operation that loads the pre-trained model from the checkpoint
init_fn = slim.assign_from_checkpoint_fn(
    os.path.join('home/n/models/inception_/', '/home/n/data/inception_v1.ckpt'),
    slim.get_model_variables('InceptionV1')
)




with tf.Session() as sess:
    # This is the loader 'op' we defined above
    init_fn(sess)  
    sess.run(input_image_float.assign(photo))
    # This run grabs all the layer constants for the original photo image input
    photo_layers_np = sess.run([ end_points[content_layer]])
    

    # This run grabs all the layer constants for the style image input
with tf.Session() as sess:
    init_fn(sess)
    sess.run(input_image_float.assign(style))
    style_layers_np = sess.run([ end_points[k] for k in style_layers])
    
def gram_matrix(tensor,ch):
    

    # Reshape the tensor so it is a 2-dim matrix. This essentially
    # flattens the contents of each feature-channel.
    matrix = tf.reshape(tensor, shape=[-1, ch])
    
    # Calculate the Gram-matrix as the matrix-product of
    # the 2-dim matrix with itself. This calculates the
    # dot-products of all combinations of the feature-channels.
    gram = tf.matmul(tf.transpose(matrix), matrix)
    return gram

def content_loss(P, X, layer):
    p = tf.constant( P)
    x = X[layer]
    
    loss = 1./4. * tf.reduce_mean(tf.square(x - p))
    return loss

def style_loss(S, X, layer):
    s = S
    x = X[layer]
    layer_shape = x.get_shape()
    N = layer_shape[1]
    M = layer_shape[2] * layer_shape[3]
    
    S_gram = gram_matrix(s,layer_shape[3])
    X_gram = gram_matrix(x,layer_shape[3])
    
    
    
    loss = tf.reduce_mean(tf.square(X_gram - S_gram)) / (4. * tf.cast( tf.square(N) * tf.square(M), tf.float32))
    return loss


sl = style_loss(style_layers_np[0], end_points, 'Conv2d_1a_7x7') + 5*style_loss(style_layers_np[1], end_points, 'Conv2d_2c_3x3') + style_loss(style_layers_np[2], end_points, 'Mixed_3b') + style_loss(style_layers_np[3], end_points, 'Mixed_4d')
cl = content_loss(photo_layers_np[0],end_points,content_layer)
total_loss = sl*15 + cl*10
# # And here are some more TF nodes, to compute the losses using the layer values 'saved off' earlier
# losses = []

# # content loss
# cl = 10.
# losses.append(cl *1.     * content_loss(photo_features, art_features, 'Mixed_4b'))

# # style loss
# sl = 2. *1000. *1000.
# losses.append(sl *1.     * style_loss(style_features, art_features, 'Conv2d_1a_7x7'))
# losses.append(sl *1.     * style_loss(style_features, art_features, 'Conv2d_2c_3x3'))
# losses.append(sl *10.    * style_loss(style_features, art_features, 'Mixed_3b'))
# losses.append(sl *10.    * style_loss(style_features, art_features, 'Mixed_4d'))

# # total variation penalty
# vp = 10. /1000. /1000.
# losses.append(vp *1.     * total_variation_loss_lX(input_image_float))
# #losses.append(vp *1.     * total_variation_loss_l1(input_image_float))


# # ['193.694946', '5.038591', '1.713539', '8.238111', '0.034608', '9.986152']
# # ['0.473700', '0.034096', '0.010799', '0.021023', '0.164272', '0.539243']
# # ['2.659750', '0.238304', '0.073061', '0.190739', '0.806217', '3.915816']
# # ['1.098473', '0.169444', '0.245660', '0.109285', '0.938582', '0.028973']
# # ['0.603620', '1.707279', '0.498789', '0.181227', '0.060200', '0.002774']
# # ['0.788231', '0.920096', '0.358549', '0.806517', '0.256121', '0.002777']

# total_loss = tf.reduce_sum(losses)

# # And define the overall symbolic gradient operation
total_grad = tf.gradients(total_loss, [input_image_float])[0]

j = photo.reshape((224,224,3))
art_image = j
#art_image = np.random.uniform(-1.0, +1.0, (image_size, image_size, 3))

x0 = art_image.flatten().astype('float64')
iteration=0
x0.shape
photo.shape
t0 = time.time()

with tf.Session() as sess:
    init_fn(sess)
#     sess.run(input_image_float.assign(photo))
        # This helper function (to interface with scipy.optimize) must close over sess
    def eval_loss_and_grad(x):  # x0 is a 3*image_size*image_size float64 vector
        x_image = x.reshape(1,224,224,3).astype('float32')
#         print(x_image.shape)
        sess.run(input_image_float.assign(x_image))
        x_loss, x_grad = sess.run( [total_loss, total_grad])
        print("\nEval Loss @ ", [ "%.6f" % l for l in x[100:106]], " = ", x_loss)
            #print("Eval Grad = ", [ "%.6f" % l for l in x_grad.flatten()[100:106]] )

#         losses_ = sess.run( losses, feed_dict={input_image_float: x_image} )
#         print("Eval loss components = ", [ "%.6f" % l for l in losses_])

#         return x_loss.astype('float64'), x_grad.flatten().astype('float64')
        return x_loss.astype('float64'), x_grad.flatten().astype('float64')

    x0, x0_loss, state = scipy.optimize.fmin_l_bfgs_b( eval_loss_and_grad,x0, maxfun=500,maxiter=500) 
#         iteration += 1

#     print("Iteration %d, in %.1fsec, Current loss : %.4f" % (iteration, float(time.time() - t0), x0_loss))

#     art_raw = np.clip( ((x0*0.5 + 0.5) * 255.0), a_min=0.0, a_max=255.0 )
#     plot_layout( art_raw.reshape(image_size,image_size,3).astype('uint8') )
art_raw = np.clip( ((x0*0.8 + 0.8) * 255.0), a_min=0.0, a_max=255.0 )
kk = art_raw.reshape((224,224,3))
kk = kk.astype('uint8')
plt.imsave("hima.jpg",kk)
plt.imshow(kk)
result = Image.fromarray(kk)
result.save('hima.jpg')
from PIL import Image
def save_image(path, image):
    image = image[0]
    image = np.clip(image, 0, 255).astype('uint8')
    scipy.misc.imsave(path, image)

save_image("hima.jpg",x0)
kk.shape
x02 = gen.flatten()
art_raw2 = np.clip( ((x02*0.5 + 0.5) * 255.0), a_min=0.0, a_max=255.0 )
kk2 = art_raw2.reshape((224,224,3))
kk2 = kk2.astype('uint8')
plt.imshow(kk2)
np.sqrt(np.sum(kk2-photo[0,:,:,:]))
photo.shape
plt.imshow(j[0,:,:,0],cmap="gray")

with tf.Session() as sess:
            
            ###############################
            ## TO DO: 
            ## 1. initialize your variables
            ## 2. create writer to write your graph
    init_fn(sess)
    
    sess.run(tf.global_variables_initializer())
    sess.run(input_image_float.assign(photo))

# For this kind of use case, the limited memory BFGS performs the best
    optimizer = tf.contrib.opt.ScipyOptimizerInterface(total_loss, method='L-BFGS-B',
                                                   options={'maxiter': 30})
#     x_loss, x_grad = sess.run( [total_loss, total_grad])
    global step
    step = 0

    def update(l):
        # Function to print loss
        global step
        if step % 10 == 0:
            print('Step {}; loss {}'.format(step, l))
        step += 1

    optimizer.minimize(session=sess,fetches=[total_loss],loss_callback=update)
    gen = sess.run(input_image_float)
#     o = sess.run(photo)

### Define loss function and minimise
with tf.Session() as sess:
    # loss function
    L_total  = cl + 5 * sl 
    
    # instantiate optimiser
    optimizer = tf.contrib.opt.ScipyOptimizerInterface(
      L_total, method='L-BFGS-B',
      options={'maxiter': 10})
    
    init_op = tf.initialize_all_variables()
    sess.run(init_op)
    sess.run(input_image_float.assign(photo))
    for i in range(1,5):
        # run optimisation
        optimizer.minimize(sess)
        img_output = sess.run(input_image_float)
v = gen
img_output.shape
x0 = img_output.flatten()
art_raw = np.clip( ((x0*0.5 + 0.5) * 255.0), a_min=0.0, a_max=255.0 )
kk = art_raw.reshape((224,224,3))
kk = kk.astype('uint8')
plt.imshow(kk)
plt.imshow(f)
t0 = time.time()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    init_fn(sess)
    
        # This helper function (to interface with scipy.optimize) must close over sess
#     def eval_loss_and_grad(x):  # x0 is a 3*image_size*image_size float64 vector
#     x_image = x.reshape(1,224,224,3).astype('float32')
#         print(x_image.shape)
    i = 0
    while (i<30):   
        sess.run( [train_step3], feed_dict={input_image_float: j} )
#         x_loss = sess.run(total_loss,feed_dict={input_image_float: j} )
        k = sess.run(input_image_float)
        print(i,x_loss)
        i = i+1
#     f = sess.run()    
#     print("\nEval Loss @ ", [ "%.6f" % l for l in x[100:106]], " = ", x_loss)
            #print("Eval Grad = ", [ "%.6f" % l for l in x_grad.flatten()[100:106]] )

#         losses_ = sess.run( losses, feed_dict={input_image_float: x_image} )
#         print("Eval loss components = ", [ "%.6f" % l for l in losses_])

#         return x_loss.astype('float64'), x_grad.flatten().astype('float64')

#     x0, x0_loss, state = scipy.optimize.fmin_l_bfgs_b( eval_loss_and_grad, photo, maxfun=50,maxiter=500) 
#         iteration += 1

#     print("Iteration %d, in %.1fsec, Current loss : %.4f" % (iteration, float(time.time() - t0), x0_loss))

#     art_raw = np.clip( ((x0*0.5 + 0.5) * 255.0), a_min=0.0, a_max=255.0 )
#     plot_layout( art_raw.reshape(image_size,image_size,3).astype('uint8') )
# define optimizer (1 line)
optimizer3 = tf.train.AdamOptimizer(1)

# define train_step (1 line)
train_step3 = optimizer3.minimize(total_loss)

x_loss


plt.imshow(photo[0,:,:,1])
def generate_noise_image(content_image,noise_ratio=0.6):
    noise_image = np.random.uniform(-20, 20, (1, 224, 224, 3)).astype(np.float32)
    return noise_image * noise_ratio + content_image * (1 - noise_ratio)

j = generate_noise_image(photo)
plt.imshow(j[0,:,:,0],cmap="gray")

def optimize(optimizer, iterations=100, new_image=True):
    ''' Define and run the optimization.
    Args:
        optimizer: The optimizer to use.
        iterations: Number of times to run optimizer.
        new_image: Whether to start with a new image, or continue with the 
            previous one.
    Return: A tuple: 
        A list of losses during the optimization like so: [(time elapsed, loss)]
        The image
    '''
    with graph.as_default():
        # Compute the gradients for a list of variables.
        grads_and_vars = optimizer.compute_gradients(total_grad, [input_tensor])
        # Op that ask the optimizer to apply the gradients.
        train_step = optimizer.apply_gradients(grads_and_vars)

        initialize_variables()
        if new_image:
            init_input()
        losses = run_optimization(
            train_step, iterations=iterations, print_n_times=5)
        result_image = input_tensor.eval()
        result_image = np.clip(deprocess(result_image), 0, 255)
        show(result_image)
        return losses, result_image




optimizer = tf.train.AdamOptimizer(learning_rate)
loss, result_image = optimize(optimizer)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1431163529144931, 'fraction_numerical': 0.05715581764572465, 'mean_word_length': 3.372892561983471, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '610776', 'license': 'MIT', 'n_tokens_mistral': 5221, 'n_tokens_neox': 4785, 'n_words': 1094, 'path': 'style_transfer_using_inception (3rd copy).ipynb', 'repo_name': 'nile649/Style-Transfer', 'size': '13227'}