
Xc           @` s  d  Z  d d l m Z d d l m Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 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 e j j j Z e j j j d
 d d  e j j j d d d  d Z d Z d Z d Z d Z d Z d Z d Z d Z  d Z! d   Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d    Z) d!   Z* d"   Z+ d#   Z, d S($   s  Builds the CIFAR-10 network.

Summary of available functions:

 # Compute input images and labels for training. If you would like to run
 # evaluations, use input() instead.
 inputs, labels = distorted_inputs()

 # Compute inference on the model inputs to make a prediction.
 predictions = inference(inputs)

 # Compute the total loss of the prediction with respect to the labels.
 loss = loss(predictions, labels)

 # Create a graph to run one step of training with respect to the loss.
 train_op = train(loss, global_step)
i    (   t   absolute_import(   t   division(   t   print_functionN(   t   urllib(   t   xrange(   t   cifar10_input(   t   gfilet
   batch_sizei   s'   Number of images to process in a batch.t   data_dirs   /tmp/cifar10_datas$   Path to the CIFAR-10 data directory.i   i
   iP  i'  gH.?g     u@g?t   towers6   http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gzc         C` sW   t  j d t d |  j j  } t j | d |   t j | d t j j	 |    d S(   s   Helper to create summaries for activations.

  Creates a summary that provides a histogram of activations.
  Creates a summary that measure the sparsity of activations.

  Args:
    x: Tensor
  Returns:
    nothing
  s
   %s_[0-9]*/t    s   /activationss	   /sparsityN(
   t   ret   subt
   TOWER_NAMEt   opt   namet   tft   histogram_summaryt   scalar_summaryt   nnt   zero_fraction(   t   xt   tensor_name(    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   _activation_summaryT   s    c      	   C` s2   t  j d   t  j |  | d | } Wd QX| S(   s   Helper to create a Variable stored on CPU memory.

  Args:
    name: name of the variable
    shape: list of ints
    initializer: initializer for Variable

  Returns:
    Variable Tensor
  s   /cpu:0t   initializerN(   R   t   devicet   get_variable(   R   t   shapeR   t   var(    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   _variable_on_cpuf   s    c         C` s_   t  |  | t j d |   } | r[ t j t j j |  | d d } t j d |  n  | S(   s  Helper to create an initialized Variable with weight decay.

  Note that the Variable is initialized with a truncated normal distribution.
  A weight decay is added only if one is specified.

  Args:
    name: name of the variable
    shape: list of ints
    stddev: standard deviation of a truncated Gaussian
    wd: add L2Loss weight decay multiplied by this float. If None, weight
        decay is not added for this Variable.

  Returns:
    Variable Tensor
  t   stddevR   t   weight_losst   losses(   R   R   t   truncated_normal_initializert   mulR   t   l2_losst   add_to_collection(   R   R   R   t   wdR   t   weight_decay(    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   _variable_with_weight_decayv   s    	$c      
   C` sv   d } t  j j |  | g d t j d | d | d t j d | \ } } t  j d |  | t  j | t j g  f S(   s  Construct a queued batch of images and labels.

  Args:
    image: 3-D Tensor of [IMAGE_SIZE, IMAGE_SIZE, 3] of type.float32.
    label: 1-D Tensor of type.int32
    min_queue_examples: int32, minimum number of samples to retain
      in the queue that provides of batches of examples.

  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  i   R   t   num_threadst   capacityi   t   min_after_dequeuet   images(   R   t   traint   shuffle_batcht   FLAGSR   t   image_summaryt   reshape(   t   imaget   labelt   min_queue_examplest   num_preprocess_threadsR+   t   label_batch(    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   _generate_image_and_label_batch   s    		c          C` s_  g  t  d d  D]% }  t j j t j d d |   ^ q } x0 | D]( } t j |  sB t d |   qB qB Wt	 j
 j |  } t j |  } t	 j | j t	 j  } t } t } t	 j j | | | g  } t	 j j |  } t	 j j | d d } t	 j j | d d	 d
 d } t	 j j |  }	 d }
 t t |
  } t d |  t |	 | j |  S(   s   Construct distorted input for CIFAR training using the Reader ops.

  Raises:
    ValueError: if no data_dir

  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  i   i   s   cifar-10-batches-bins   data_batch_%d.bins   Failed to find file: t	   max_deltai?   t   lowerg?t   upperg?g?sZ   Filling queue with %d CIFAR images before starting to train. This will take a few minutes.(   R   t   ost   patht   joinR.   R   R   t   Existst
   ValueErrorR   R,   t   string_input_producerR   t   read_cifar10t   castt
   uint8imaget   float32t
   IMAGE_SIZER1   t   random_cropt   random_flip_left_rightt   random_brightnesst   random_contrastt   per_image_whiteningt   intt    NUM_EXAMPLES_PER_EPOCH_FOR_TRAINt   printR6   R2   (   t   it	   filenamest   ft   filename_queuet
   read_inputt   reshaped_imaget   heightt   widtht   distorted_imaget   float_imaget!   min_fraction_of_examples_in_queueR3   (    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   distorted_inputs   s0    
8	
c         C` sQ  t  j s t d   n  |  sb g  t d d  D]% } t j j t  j d d |  ^ q. } t } n$ t j j t  j d d  g } t } x0 | D]( } t	 j
 |  s t d |   q q Wt j j |  } t j |  } t j | j t j  } t } t }	 t j j | |	 |  }
 t j j |
  } d } t | |  } t | | j |  S(	   sM  Construct input for CIFAR evaluation using the Reader ops.

  Args:
    eval_data: bool, indicating if one should use the train or eval data set.

  Raises:
    ValueError: if no data_dir

  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  s   Please supply a data_diri   i   s   cifar-10-batches-bins   data_batch_%d.bins   test_batch.bins   Failed to find file: g?(   R.   R   R>   R   R:   R;   R<   RK   t   NUM_EXAMPLES_PER_EPOCH_FOR_EVALR   R=   R   R,   R?   R   R@   RA   RB   RC   RD   R1   t   resize_image_with_crop_or_padRI   RJ   R6   R2   (   t	   eval_dataRM   RN   t   num_examples_per_epochRO   RP   RQ   RR   RS   RT   t   resized_imageRV   RW   R3   (    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   inputs   s2    	8	
c         C` sB  t  j d   } t d d d d d d g d d d	 d
 } t  j j |  | d d d d g d d } t d d g t  j d
   } t  j j | |  } t  j j | d | j	 } t
 |  Wd QXt  j j | d d d d d g d d d d d g d d d d } t  j j | d d d d d* d d d d } t  j d   } t d d d d d d g d d d	 d
 } t  j j | | d d d d g d d } t d d g t  j d   } t  j j | |  } t  j j | d | j	 }	 t
 |	  Wd QXt  j j |	 d d d d d+ d d d d  }
 t  j j |
 d d d d d g d d d d d g d d d d! } t  j d"   } d } x( | j   d j   D] } | | 9} qWt  j | t j | g  } t d d | d# g d d$ d	 d% } t d d# g t  j d   } t  j j | | | d | j	 } t
 |  Wd QXt  j d&  t } t d d d# d' g d d$ d	 d% } t d d' g t  j d   } t  j j | | | d | j	 } t
 |  Wd QXt  j d(  q } t d d' t g d d, d	 d
 } t d t g t  j d
   } t  j j | | | d | j	 } t
 |  Wd QX| S(-   s~   Build the CIFAR-10 model.

  Args:
    images: Images returned from distorted_inputs() or inputs().

  Returns:
    Logits.
  t   conv1t   weightsR   i   i   i@   R   g-C6?R%   g        i   t   paddingt   SAMEt   biasesR   Nt   ksizet   stridesi   t   pool1i   t   biasg      ?t   alphagMbP?g      "@t   betag      ?t   norm1t   conv2g?t   norm2t   pool2t   local3i  g{Gz?gMbp?t   local4i   t   softmax_linearg      h@g2.Z ?g2.Z ?gUUUUUUu?(   R   t   variable_scopeR'   R   t   conv2dR   t   constant_initializert   bias_addt   reluR   R   t   max_poolt   lrnt	   get_shapet   as_listR0   R.   R   t
   relu_layert   NUM_CLASSESt	   xw_plus_b(   R+   t   scopet   kernelt   convRc   Rg   R_   Rf   Rj   Rk   Rl   Rm   t   dimt   dR0   R`   Rn   Ro   Rp   (    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt	   inference  s`    *3$	*$	!!!!!c         C` s   t  j | t j d g  } t  j t  j t j  t j d g  } t  j d | | g  } t  j | t j t g d d  } t  j j	 |  | d d } t  j
 | d d } t  j d |  t  j t  j d  d d S(	   s  Add L2Loss to all the trainable variables.

  Add summary for for "Loss" and "Loss/avg".
  Args:
    logits: Logits from inference().
    labels: Labels from distorted_inputs or inputs(). 1-D tensor
            of shape [batch_size]

  Returns:
    Loss tensor of type float.
  i   g      ?g        R   t   cross_entropy_per_examplet   cross_entropyR    t
   total_loss(   R   R0   R.   R   t   ranget   concatt   sparse_to_denseR{   R   t!   softmax_cross_entropy_with_logitst   reduce_meanR$   t   add_nt   get_collection(   t   logitst   labelst   sparse_labelst   indicest   concatedt   dense_labelsR   t   cross_entropy_mean(    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   lossq  s    '		c         C` s   t  j j d d d } t  j d  } | j | |  g  } xN | |  g D]? } t  j | j j d |  t  j | j j | j |   qK W| S(   s#  Add summaries for losses in CIFAR-10 model.

  Generates moving average for all losses and associated summaries for
  visualizing the performance of the network.

  Args:
    total_loss: Total loss from loss().
  Returns:
    loss_averages_op: op for generating moving averages of losses.
  g?R   t   avgR    s    (raw)(	   R   R,   t   ExponentialMovingAverageR   t   applyR   R   R   t   average(   R   t   loss_averagesR    t   loss_averages_opt   l(    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   _add_loss_summaries  s    #c         C` sl  t  t j } t | t  } t j j t | | t	 d t
 } t j d |  t |   } t j | g  & t j j |  } | j |   } Wd QX| j | d | } x* t j   D] }	 t j |	 j j |	  q Wx7 | D]/ \ }
 }	 |
 r t j |	 j j d |
  q q Wt j j t |  } | j t j    } t j | | g   t j d d  } Wd QX| S(   s;  Train CIFAR-10 model.

  Create an optimizer and apply to all trainable variables. Add moving
  average for all trainable variables.

  Args:
    total_loss: Total loss from loss().
    global_step: Integer Variable counting the number of training steps
      processed.
  Returns:
    train_op: op for training.
  t	   staircaset   learning_rateNt   global_steps
   /gradientsR   R,   (   RK   R.   R   RJ   t   NUM_EPOCHS_PER_DECAYR   R,   t   exponential_decayt   INITIAL_LEARNING_RATEt   LEARNING_RATE_DECAY_FACTORt   TrueR   R   t   control_dependenciest   GradientDescentOptimizert   compute_gradientst   apply_gradientst   trainable_variablesR   R   R   R   t   MOVING_AVERAGE_DECAYR   t   no_op(   R   R   t   num_batches_per_epocht   decay_stepst   lrR   t   optt   gradst   apply_gradient_opR   t   gradt   variable_averagest   variables_averages_opt   train_op(    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyR,     s0    	!	c          ` s   t  j }  t j j |   s+ t j |   n  t j d  d   t j j |     } t j j |  s   f d   } t	 j
 j t | d | \ } } t   t j |  } t d   | j d  t j | d  j |   n  d S(	   s5   Download and extract the tarball from Alex's website.t   /ic         ` sC   t  j j d   t |  |  t |  d f  t  j j   d  S(   Ns   >> Downloading %s %.1f%%g      Y@(   t   syst   stdoutt   writet   floatt   flush(   t   countt
   block_sizet
   total_size(   t   filename(    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt	   _progress  s    #t
   reporthooks   Succesfully downloadeds   bytes.s   r:gzN(   R.   R   R:   R;   t   existst   makedirst   DATA_URLt   splitR<   R   t   requestt   urlretrieveRL   t   statt   st_sizet   tarfilet   opent
   extractall(   t   dest_directoryt   filepathR   t   _t   statinfo(    (   R   si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   maybe_download_and_extract  s    	(-   t   __doc__t
   __future__R    R   R   t   gzipR:   R   R   R   t   tensorflow.python.platformt
   tensorflowt	   six.movesR   R   R   t   tensorflow.models.image.cifar10R   R   t   appt   flagsR.   t   DEFINE_integert   DEFINE_stringRD   R{   RK   RY   R   R   R   R   R   R   R   R   R'   R6   RX   R^   R   R   R   R,   R   (    (    (    si   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/cifar10/cifar10.pyt   <module>    sP   					:	:	R	 		8