<jupyter_start><jupyter_text>[Machine Learning with CoreML](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-core-ml)**By:** <NAME> (Author)  **Publisher:** [Packt Publishing](https://www.packtpub.com/)   Perceptual Losses for Real-Time Style Transfer and Super-ResolutionKeras implementation of the network described in Perceptual Losses for Real-Time Style Transfer and Super-Resolution by <NAME>, <NAME>, <NAME> in in March 2016. The implementation is based on what was described in the FastAI's course on Generative Models.<jupyter_code>%matplotlib inline
import importlib
import sys, os

from scipy.misc import imsave
import numpy as np
from PIL import Image # used to load images
import glob 

from keras.applications.vgg16 import preprocess_input, decode_predictions
TARGET_SIZE = (320,320)<jupyter_output><empty_output><jupyter_text>Training Data  Dataset from Fast.ai and can be downloaded from here.<jupyter_code>path = '/data/datasets/imagenet/samples/'
fnames = glob.glob(path+'**/*.JPEG', recursive=True)
n = len(fnames); n
fn = fnames[50]
fn
img=Image.open(fnames[50])
img<jupyter_output><empty_output><jupyter_text>Preprocessing  We'll be using this image with VGG16. Therefore, we need to subtract the mean of each channel of the imagenet data and reverse the order of RGB->BGR since those are the preprocessing steps that the VGG authors did - so their model won't work unless we do the same thing.<jupyter_code>def load_resize_image(img_path, target_size=TARGET_SIZE):
    """
    
    """
    # Load image 
    img = Image.open(img_path)
    if img.mode is not "RGB":
        img = img.convert('RGB')
    
    # Get dimensions
    img_width, img_height = img.size
    half_img_width, half_img_height = img_width/2.0, img_height/2.0

    # Crop
    target_width = min(img_width, img_height)
    target_height = min(img_width, img_height)

    left = half_img_width - target_width/2
    top = half_img_height - target_height/2
    right = half_img_width + target_width/2
    bottom = half_img_height + target_height/2

    img = img.crop((left, top, right, bottom))

    # Resize 
    img = img.resize(target_size)
    
    return img 
img=load_resize_image(fnames[50])
img
for fname in fnames:
    img = load_resize_image(fname)
    
    fname_parts = os.path.split(fname) 
    fname_parts = [part.replace("imagenet", "imagenet_small") for part in fname_parts]
    
    if not os.path.exists(fname_parts[0]):
        os.makedirs(fname_parts[0])
                
    img.save(os.path.join(fname_parts[0], fname_parts[1]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10015408320493066, 'fraction_numerical': 0.012326656394453005, 'mean_word_length': 4.2044088176352705, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '433504', 'license': 'MIT', 'n_tokens_mistral': 884, 'n_tokens_neox': 846, 'n_words': 230, 'path': 'Chapter06/Notebooks/Training/FastNeuralStyleTransfer_PreprocessingImageNetData.ipynb', 'repo_name': 'karant17/Machine-Learning-with-Core-ML', 'size': '2625'}