In [1]:
import os
import tensorflow as tf
import numpy as np
from PIL import Image
import keras

from keras.applications.inception_v3 import InceptionV3


Using TensorFlow backend.

In [4]:
from keras.applications.resnet50 import ResNet50
from keras.applications.xception import Xception
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
from keras.applications import imagenet_utils
from keras import backend as K
from scipy.ndimage.filters import gaussian_filter
from IPython.display import clear_output, HTML
from IPython.display import display, Image
from functools import partial
from IPython.core.debugger import Tracer
import pprint
import time
from scipy.misc import imsave

%matplotlib inline

In [5]:
# Output image size
image_size = (300, 300)

In [7]:
def random_image(size,):
    '''Generate a random noise image and return it as np array. 
        Args:
            size: The desired dimensions of the output image.
            random_noise: Whether to generate a random noise 
             image or to load a picture.
            filename: The fullpath to the image to load.
    '''
    width, height = size
    # Generate a random noise image
    # 3 is the number of channels (RGB) in the image.
    img = np.random.random(size=(width, height, 3))
    img_array = img * 256
    return img_array


def load_image(size, filename=None):
    '''Load image from disc and return it as np array. 
        Args:
            size: The desired dimensions of the output image.
            filename: The fullpath to the image to load.
    '''
    width, height = size

    if not filename:
        raise ValueError(
            'If not generating random noise image, image filename is required.')
    # Load a picture
    img = PIL.Image.open(filename)
    img = img.resize((width, height))
    img_array = np.asarray(img.copy())
    return img_array

# Get and displaying image
image = random_image(size=image_size)

In [8]:
from matplotlib.pyplot import imshow
from io import BytesIO
from IPython import display 
import matplotlib.pyplot as plt
import PIL.Image
import numpy as np

def plot(img, scale=1, dpi=80):
    plt.figure(figsize=(img.shape[0]*scale/dpi, img.shape[1]*scale/dpi), dpi=dpi)
    imshow(img)
    
# Prepare a tensor to be displayed as image
def normalize(x):
    x = x.copy().astype(float)
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    # clip to [0, 1]
    x += 0.5
    x = np.clip(x, 0, 1)

    # convert to RGB array
    x *= 255
    return x.astype('uint8')

def show(a, fmt='jpeg'):
    f = BytesIO()
    PIL.Image.fromarray(a.astype('uint8')).save(f, fmt)
    img = display.Image(data=f.getvalue())
    display.display(img)

def resize_array(array, size):
    '''Resizes an image (formatted as np array) to give size.
    Args:
        array: np array representing the image.
        size: The desired size.
    Returns: The resized image as a float np array.
    '''
    image = PIL.Image.fromarray(array.astype('uint8').copy())
    image_resized = image.resize(size, PIL.Image.ANTIALIAS)
    return np.asarray(image_resized).astype(float)

In [9]:
show(image)



In [14]:
!export HTTPS_PROXY=https://gate-zrh.swissre.com:9443

In [15]:
graph = tf.Graph()
config = tf.ConfigProto()
# Uncomment this if you want to use two iPython scripts side by side,
# and you dont want tensorflow to take up all the GPU memory on initialization
# config.gpu_options.allow_growth=True

# Interactive session is better suited for notebooks
sess = tf.InteractiveSession(graph=graph, config=config)
K.set_session(sess)

# Keras flag - we are not training, just testing now
K.set_learning_phase(0)


def setup_model(initial_value, model_name='Inception5h'):
    '''Load the model. Use a TF tensor as input just for fun.
    Args: 
        initial_value: The initial value of the input tensor. Mainly used for size.
        model_name: Whether to normalize the input image.
    Returns:
        Tuple of (model, # The loaded keras model
            input_tensor, # The tensor that feeds the model
            content_layers, # The content layers of this model as tensors
            style_layers, # The style layers of this model as tensors
            preprocess_func, # Preprocesses an image for the model
            deprocess_func # Returns preprocessed image back to normal.
    '''
    # Prepare tensor for input image
    image_tensor = tf.Variable(initial_value)

    if model_name == 'VGG16' or model == 'VGG19':
        # These two models share a lot, so define them together
        if model_name == 'VGG16':
            # VGG 16 model
            model = VGG16(include_top=False, weights='imagenet',
                          input_tensor=image_tensor)
        elif model_name == 'VGG19':
            model = VGG19(include_top=False, weights='imagenet',
                          input_tensor=image_tensor)

        # Preprocesses an image for the model
        def preprocess_func(x):
            x = x.copy().astype(float)
            rank = len(x.shape)
            if (rank == 3):
                # Add extra batch dimension
                x = np.expand_dims(x, axis=0)
            x[:, :, :, 2] -= 103.939
            x[:, :, :, 1] -= 116.779
            x[:, :, :, 0] -= 123.68

            # Flip the channels from RGB to BGR
            x = x[:, :, :, ::-1]
            return x

        # Returns preprocessed image back to normal.
        def deprocess_func(x):
            x = np.asarray(x).copy()
            rank = len(x.shape)
            if (rank == 4):
                # Remove extra batch dimension
                x = np.squeeze(x, axis=0)

            # Flip the channels from BGR to RGB
            x = x[:, :, ::-1]

            # Remove zero-center by mean pixel
            x[:, :, 2] += 103.939
            x[:, :, 1] += 116.779
            x[:, :, 0] += 123.68

            x = np.clip(x, 0, 255).astype('uint8')
            return x

        # Define the style layers
        style_layers = [model.get_layer('block1_conv1').output,
                        model.get_layer('block2_conv1').output,
                        model.get_layer('block3_conv1').output,
                        model.get_layer('block4_conv1').output,
                        model.get_layer('block5_conv1').output]

        # Define the content layers
        content_layers = model.get_layer('block4_conv2').output

    # TODO: Add other models

    return model, image_tensor, content_layers, style_layers, preprocess_func, deprocess_func

with graph.as_default():
    with graph.name_scope("model") as scope:
        # Use the image size (with a batch dimension) to feed the model input.
        # We do so that the input has size and the convolutional layers
        # will be sized as well.
        initial = np.expand_dims(image, axis=0).astype('float32')

        # Setup the model
        (model, input_tensor, content_layers, style_layers,
         preprocess, deprocess) = setup_model(
            initial_value=initial,
            model_name='VGG16')


Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5
---------------------------------------------------------------------------
SSLError                                  Traceback (most recent call last)
/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1317                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error

/home/s4szqx/anaconda3/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 

/home/s4szqx/anaconda3/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 

/home/s4szqx/anaconda3/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 

/home/s4szqx/anaconda3/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

/home/s4szqx/anaconda3/lib/python3.6/http/client.py in send(self, data)
    963             if self.auto_open:
--> 964                 self.connect()
    965             else:

/home/s4szqx/anaconda3/lib/python3.6/http/client.py in connect(self)
   1399             self.sock = self._context.wrap_socket(self.sock,
-> 1400                                                   server_hostname=server_hostname)
   1401             if not self._context.check_hostname and self._check_hostname:

/home/s4szqx/anaconda3/lib/python3.6/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
    400                          server_hostname=server_hostname,
--> 401                          _context=self, _session=session)
    402 

/home/s4szqx/anaconda3/lib/python3.6/ssl.py in __init__(self, sock, keyfile, certfile, server_side, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, family, type, proto, fileno, suppress_ragged_eofs, npn_protocols, ciphers, server_hostname, _context, _session)
    807                         raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
--> 808                     self.do_handshake()
    809 

/home/s4szqx/anaconda3/lib/python3.6/ssl.py in do_handshake(self, block)
   1060                 self.settimeout(None)
-> 1061             self._sslobj.do_handshake()
   1062         finally:

/home/s4szqx/anaconda3/lib/python3.6/ssl.py in do_handshake(self)
    682         """Start the SSL/TLS handshake."""
--> 683         self._sslobj.do_handshake()
    684         if self.context.check_hostname:

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
/home/s4szqx/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py in get_file(fname, origin, untar, md5_hash, cache_subdir)
    112                 urlretrieve(origin, fpath,
--> 113                             functools.partial(dl_progress, progbar=progbar))
    114             except URLError as e:

/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in urlretrieve(url, filename, reporthook, data)
    247 
--> 248     with contextlib.closing(urlopen(url, data)) as fp:
    249         headers = fp.info()

/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 

/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    525 
--> 526         response = self._open(req, data)
    527 

/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in _open(self, req, data)
    543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
    545         if result:

/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:

/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in https_open(self, req)
   1360             return self.do_open(http.client.HTTPSConnection, req,
-> 1361                 context=self._context, check_hostname=self._check_hostname)
   1362 

/home/s4szqx/anaconda3/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1319             except OSError as err: # timeout error
-> 1320                 raise URLError(err)
   1321             r = h.getresponse()

URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-15-86dcea0ab0fa> in <module>()
     98          preprocess, deprocess) = setup_model(
     99             initial_value=initial,
--> 100             model_name='VGG16')

<ipython-input-15-86dcea0ab0fa> in setup_model(initial_value, model_name)
     34             # VGG 16 model
     35             model = VGG16(include_top=False, weights='imagenet',
---> 36                           input_tensor=image_tensor)
     37         elif model_name == 'VGG19':
     38             model = VGG19(include_top=False, weights='imagenet',

/home/s4szqx/anaconda3/lib/python3.6/site-packages/keras/applications/vgg16.py in VGG16(include_top, weights, input_tensor, input_shape, pooling, classes)
    166             weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
    167                                     WEIGHTS_PATH_NO_TOP,
--> 168                                     cache_subdir='models')
    169         model.load_weights(weights_path)
    170         if K.backend() == 'theano':

/home/s4szqx/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py in get_file(fname, origin, untar, md5_hash, cache_subdir)
    113                             functools.partial(dl_progress, progbar=progbar))
    114             except URLError as e:
--> 115                 raise Exception(error_msg.format(origin, e.errno, e.reason))
    116             except HTTPError as e:
    117                 raise Exception(error_msg.format(origin, e.code, e.msg))

Exception: URL fetch failure on https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

In [ ]: