σ
Ύχ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 d l m Z d d l m Z d d l m Z d Z  d Z! d   Z" d" d  Z# e$ d d d d d  d!  Z& d S(#   s¨   ResNet50 model for Keras.

# Reference:

- [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)

Adapted from code contributed by BigMoyan.
i    (   t   print_function(   t   absolute_importNi   (   t   Input(   t   layers(   t   Dense(   t
   Activation(   t   Flatten(   t   Conv2D(   t   MaxPooling2D(   t   ZeroPadding2D(   t   AveragePooling2D(   t   GlobalAveragePooling2D(   t   GlobalMaxPooling2D(   t   BatchNormalization(   t   Model(   t   backend(   t   get_source_inputs(   t   layer_utils(   t   get_filei   (   t   decode_predictions(   t   preprocess_input(   t   _obtain_input_shapesv   https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5s|   https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5c         C@ so  | \ } } } t  j   d k r* d } n d } d t |  | d }	 d t |  | d }
 t | d d |	 d |   } t d	 | d |
 d  |  } t d
  |  } t | | d d d |	 d |  } t d	 | d |
 d  |  } t d
  |  } t | d d |	 d |  } t d	 | d |
 d  |  } t j | |  g  } t d
  |  } | S(   sμ  The identity block is the block that has no conv layer at shortcut.

    # Arguments
        input_tensor: input tensor
        kernel_size: defualt 3, the kernel size of middle conv layer at main path
        filters: list of integers, the filterss of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names

    # Returns
        Output tensor for the block.
    t   channels_lasti   i   t   rest   _brancht   bnt   namet   2at   axist   relut   paddingt   samet   2bt   2c(   i   i   (   i   i   (   t   Kt   image_data_formatt   strR   R   R   R   t   add(   t   input_tensort   kernel_sizet   filterst   staget   blockt   filters1t   filters2t   filters3t   bn_axist   conv_name_baset   bn_name_baset   x(    (    s:   /tmp/pip-build-isqEY4/keras/keras/applications/resnet50.pyt   identity_block)   s$    	c         C@ sΉ  | \ } } } t  j   d k r* d }	 n d }	 d t |  | d }
 d t |  | d } t | d d | d |
 d	 |   } t d
 |	 d | d	  |  } t d  |  } t | | d d d |
 d |  } t d
 |	 d | d  |  } t d  |  } t | d d |
 d |  } t d
 |	 d | d  |  } t | d d | d |
 d |   } t d
 |	 d | d  |  } t j | | g  } t d  |  } | S(   sn  conv_block is the block that has a conv layer at shortcut

    # Arguments
        input_tensor: input tensor
        kernel_size: defualt 3, the kernel size of middle conv layer at main path
        filters: list of integers, the filterss of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names

    # Returns
        Output tensor for the block.

    Note that from stage 3, the first conv layer at main path is with strides=(2,2)
    And the shortcut should have strides=(2,2) as well
    R   i   i   R   R   R   t   stridesR   R   R   R   R   R   R    R!   t   1(   i   i   (   i   i   (   i   i   (   R"   R#   R$   R   R   R   R   R%   (   R&   R'   R(   R)   R*   R3   R+   R,   R-   R.   R/   R0   R1   t   shortcut(    (    s:   /tmp/pip-build-isqEY4/keras/keras/applications/resnet50.pyt
   conv_blockO   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 j |  s½ t d | d |  } n | } t j   d k rή d } n d } t d;  |  } t d d< d d= d d |  } t	 d | d d  |  } t
 d  |  } t d> d d? |  } t | d d d d g d d d d d d@ } t | d d d d g d d d d } t | d d d d g d d d d } t | d d d d  g d d d d } t | d d d d  g d d d d } t | d d d d  g d d d d } t | d d d d  g d d d d! } t | d d d d" g d d# d d } t | d d d d" g d d# d d } t | d d d d" g d d# d d } t | d d d d" g d d# d d! } t | d d d d" g d d# d d$ } t | d d d d" g d d# d d% } t | d d  d  d& g d d' d d } t | d d  d  d& g d d' d d } t | d d  d  d& g d d' d d } t dA d d( |  } |  r t   |  } t | d) d* d d+ |  } n< | d, k r>t   |  } n | d- k r\t   |  } n  | d: k	 rwt |  }	 n | }	 t |	 | d d. }
 | d k r|  rΒt d/ t d0 d1 d2 d3 } n t d4 t d0 d1 d2 d5 } |
 j |  t j   d6 k rt j |
  n  t j   d7 k r|  rk|
 j d d(  } | j d } |
 j d d+  } t j | | d7  n  t j   d8 k rt j  d9  qqn  |
 S(B   sj  Instantiates the ResNet50 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.

    # 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 `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 244)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 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.
    R7   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@   i   R3   i   R   t   conv1R   t   bn_conv1R   i   R)   R*   t   at   bt   ci   i   t   di   i   t   et   fi   i   t   avg_poolt
   activationt   softmaxt   fc1000t   avgt   maxt   resnet50s.   resnet50_weights_tf_dim_ordering_tf_kernels.h5t   cache_subdirt   modelst   md5_hasht    a7b3fe01876f51b976af0dea6bc144ebs4   resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5t    a268eb855778b3df3c7506639542a6aft   theanot   channels_firstt
   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.N(   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (!   t   Nonet
   ValueErrorR   R"   R#   R   t   is_keras_tensorR	   R   R   R   R   R6   R2   R
   R   R   R   R   R   R   R   t   WEIGHTS_PATHt   WEIGHTS_PATH_NO_TOPt   load_weightsR   R   t   convert_all_kernels_in_modelt	   get_layert   output_shapet!   convert_dense_weights_data_formatt   warningst   warn(   R;   t   weightsR&   t   input_shapet   poolingt   classest	   img_inputR.   R1   t   inputst   modelt   weights_patht   maxpoolR<   t   dense(    (    s:   /tmp/pip-build-isqEY4/keras/keras/applications/resnet50.pyt   ResNet50}   s    5			!-'''''''''''''''!	(   i   i   ('   t   __doc__t
   __future__R    R   R_   R   R   t    R   R   R   R   R   R	   R
   R   R   R   RN   R   R   R"   t   engine.topologyR   t   utilsR   t   utils.data_utilsR   t   imagenet_utilsR   R   R   RX   RY   R2   R6   t   TrueRU   Rk   (    (    (    s:   /tmp/pip-build-isqEY4/keras/keras/applications/resnet50.pyt   <module>	   s>   	&.