ó
¾÷Xc           @@ sŽ  d  Z  d d l m Z d d l m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 d d	 l m
 Z
 d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d Z d Z d d d f e d „ Z  e! d e e e d d „ Z" d „  Z# d S(    s]  Inception V3 model for Keras.

Note that the input image format for this model is different than for
the VGG16 and ResNet models (299x299 instead of 224x224),
and that the input preprocessing function is also different (same as Xception).

# Reference

- [Rethinking the Inception Architecture for Computer Vision](http://arxiv.org/abs/1512.00567)

i    (   t   print_function(   t   absolute_importNi   (   t   Model(   t   layers(   t
   Activation(   t   Dense(   t   Input(   t   BatchNormalization(   t   Conv2D(   t   MaxPooling2D(   t   AveragePooling2D(   t   GlobalAveragePooling2D(   t   GlobalMaxPooling2D(   t   get_source_inputs(   t   convert_all_kernels_in_model(   t   get_file(   t   backendi   (   t   decode_predictions(   t   _obtain_input_shapesz   https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5s€   https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5t   samec   
      C@ sÀ   | d k	 r# | d } | d } n d } d } t j ƒ  d k rJ d }	 n d }	 t | | | f d | d | d t d	 | ƒ|  ƒ }  t d
 |	 d t d	 | ƒ |  ƒ }  t d d	 | ƒ|  ƒ }  |  S(   s  Utility function to apply conv + BN.

    # Arguments
        x: input tensor.
        filters: filters in `Conv2D`.
        num_row: height of the convolution kernel.
        num_col: width of the convolution kernel.
        padding: padding mode in `Conv2D`.
        strides: strides in `Conv2D`.
        name: name of the ops; will become `name + '_conv'`
            for the convolution and `name + '_bn'` for the
            batch norm layer.

    # Returns
        Output tensor after applying `Conv2D` and `BatchNormalization`.
    t   _bnt   _convt   channels_firsti   i   t   stridest   paddingt   use_biast   namet   axist   scalet   reluN(   t   Nonet   Kt   image_data_formatR   t   FalseR   R   (
   t   xt   filterst   num_rowt   num_colR   R   R   t   bn_namet	   conv_namet   bn_axis(    (    s>   /tmp/pip-build-isqEY4/keras/keras/applications/inception_v3.pyt	   conv2d_bn)   s"    
	!t   imagenetiè  c         C@ s¿  | d d? h k r! t d ƒ ‚ n  | d k rN |  rN | d k rN t d ƒ ‚ n  t | d d d d d	 t j ƒ  d
 |  ƒ} | d? k r– t d | ƒ } n t d | d | ƒ } t j ƒ  d k rÆ d } n d } t | d d d d d@ d d ƒ} t | d d d d d ƒ} t | d d d ƒ } t dA d dB ƒ| ƒ } t | d d d d d ƒ} t | d d d d d ƒ} t dC d dD ƒ| ƒ } t | d d d ƒ }	 t | d d d ƒ }
 t |
 d d d ƒ }
 t | d d d ƒ } t | d d d ƒ } t | d d d ƒ } t dE d dF d d ƒ| ƒ } t | d d d ƒ } t	 j
 |	 |
 | | g d | d d ƒ} t | d d d ƒ }	 t | d d d ƒ }
 t |
 d d d ƒ }
 t | d d d ƒ } t | d d d ƒ } t | d d d ƒ } t dG d dH d d ƒ| ƒ } t | d d d ƒ } t	 j
 |	 |
 | | g d | d d ƒ} t | d d d ƒ }	 t | d d d ƒ }
 t |
 d d d ƒ }
 t | d d d ƒ } t | d d d ƒ } t | d d d ƒ } t dI d dJ d d ƒ| ƒ } t | d d d ƒ } t	 j
 |	 |
 | | g d | d d  ƒ} t | d! d d d dK d d ƒ} t | d d d ƒ } t | d d d ƒ } t | d d d d dL d d ƒ} t dM d dN ƒ| ƒ } t	 j
 | | | g d | d d" ƒ} t | d d d ƒ }	 t | d# d d ƒ } t | d# d d$ ƒ } t | d d$ d ƒ } t | d# d d ƒ } t | d# d$ d ƒ } t | d# d d$ ƒ } t | d# d$ d ƒ } t | d d d$ ƒ } t dO d dP d d ƒ| ƒ } t | d d d ƒ } t	 j
 |	 | | | g d | d d% ƒ} x9t d ƒ D]+} t | d d d ƒ }	 t | d& d d ƒ } t | d& d d$ ƒ } t | d d$ d ƒ } t | d& d d ƒ } t | d& d$ d ƒ } t | d& d d$ ƒ } t | d& d$ d ƒ } t | d d d$ ƒ } t dQ d dR d d ƒ| ƒ } t | d d d ƒ } t	 j
 |	 | | | g d | d d' t d | ƒ ƒ} q×Wt | d d d ƒ }	 t | d d d ƒ } t | d d d$ ƒ } t | d d$ d ƒ } t | d d d ƒ } t | d d$ d ƒ } t | d d d$ ƒ } t | d d$ d ƒ } t | d d d$ ƒ } t dS d dT d d ƒ| ƒ } t | d d d ƒ } t	 j
 |	 | | | g d | d d( ƒ} t | d d d ƒ } t | d) d d d dU d d ƒ} t | d d d ƒ } t | d d d$ ƒ } t | d d$ d ƒ } t | d d d d dV d d ƒ} t dW d dX ƒ| ƒ } t	 j
 | | | g d | d d* ƒ} xjt d ƒ D]\} t | d) d d ƒ }	 t | d! d d ƒ } t | d! d d ƒ } t | d! d d ƒ } t	 j
 | | g d | d d+ t | ƒ ƒ} t | d, d d ƒ } t | d! d d ƒ } t | d! d d ƒ } t | d! d d ƒ } t	 j
 | | g d | ƒ} t dY d dZ d d ƒ| ƒ } t | d d d ƒ } t	 j
 |	 | | | g d | d d' t d- | ƒ ƒ} qüW|  r˜
t d d. ƒ | ƒ } t | d/ d0 d d1 ƒ| ƒ } n< | d2 k r¶
t ƒ  | ƒ } n | d3 k rÔ
t ƒ  | ƒ } n  | d? k	 rï
t | ƒ } n | } t | | d d4 ƒ} | d k r»t j ƒ  d k rMt j ƒ  d5 k rMt j d6 ƒ qMn  |  rqt d7 t d8 d9 d: d; ƒ} n t d< t d8 d9 d: d= ƒ} | j | ƒ t j ƒ  d> k r»t | ƒ q»n  | S([   s³  Instantiates the Inception v3 architecture.

    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format="channels_last"` in your Keras config
    at ~/.keras/keras.json.
    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.
    Note that the default input image size for this model is 299x299.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(299, 299, 3)` (with `channels_last` data format)
            or `(3, 299, 299)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 139.
            E.g. `(150, 150, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    R*   sp   The `weights` argument should be either `None` (random initialization) or `imagenet` (pre-training on ImageNet).iè  sS   If using `weights` as imagenet with `include_top` as true, `classes` should be 1000t   default_sizei+  t   min_sizei‹   t   data_formatt   include_topt   shapet   tensorR   i   i   i    R   i   R   t   validi@   iP   iÀ   i0   i   i`   R   R   R   t   mixed0t   mixed1t   mixed2i€  t   mixed3i€   i   t   mixed4i    t   mixedt   mixed7i@  t   mixed8t   mixed9_iÀ  i	   t   avg_poolt
   activationt   softmaxt   predictionst   avgt   maxt   inception_v3t
   tensorflowsò   You are using the TensorFlow backend, yet you are using the Theano image data format convention (`image_data_format="channels_first"`). For best performance, set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json.s2   inception_v3_weights_tf_dim_ordering_tf_kernels.h5t   cache_subdirt   modelst   md5_hasht    9a0d58056eeedaa3f26cb7ebd46da564s8   inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5t    bcbd6486424b2319ff4ef7d526e38f63t   theanoN(   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   R   t
   ValueErrorR   R   R    R   R)   R	   R
   R   t   concatenatet   ranget   strR   R   R   R   R   R   t   warningst   warnR   t   WEIGHTS_PATHt   WEIGHTS_PATH_NO_TOPt   load_weightsR   (   R.   t   weightst   input_tensort   input_shapet   poolingt   classest	   img_inputt   channel_axisR"   t	   branch1x1t	   branch5x5t   branch3x3dblt   branch_poolt	   branch3x3t	   branch7x7t   branch7x7dblt   it   branch7x7x3t   branch3x3_1t   branch3x3_2t   branch3x3dbl_1t   branch3x3dbl_2t   inputst   modelt   weights_path(    (    s>   /tmp/pip-build-isqEY4/keras/keras/applications/inception_v3.pyt   InceptionV3U   s^   7		!			!		%!	c         C@ s"   |  d :}  |  d 8}  |  d 9}  |  S(   Ng     ào@g      à?g       @(    (   R"   (    (    s>   /tmp/pip-build-isqEY4/keras/keras/applications/inception_v3.pyt   preprocess_input…  s    


($   t   __doc__t
   __future__R    R   RM   RD   R   t    R   R   R   R   R   R   R	   R
   R   R   t   engine.topologyR   t   utils.layer_utilsR   t   utils.data_utilsR   R   R   t   imagenet_utilsR   R   RO   RP   R   R)   t   TrueRi   Rj   (    (    (    s>   /tmp/pip-build-isqEY4/keras/keras/applications/inception_v3.pyt   <module>   sB   	&ÿ ,