Une mise-à-jour de Gitlab vers la version 14.0 est prévue le 24 juin entre 14:00 et 15:00. Le service sera inaccessible ou instable pendant cette période. Merci de votre compréhension.

create_model_maggiori17_fullyconv.py 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# -*- coding: utf-8 -*-
#==========================================================================
#
#   Copyright Remi Cresson (IRSTEA)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#          http://www.apache.org/licenses/LICENSE-2.0.txt
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#==========================================================================*/
remi cresson's avatar
remi cresson committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Imports
import numpy as np
import tensorflow as tf
import numpy as np
import random
import os
import shutil
import sys
import time
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import f1_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix

from tricks import *

tf.logging.set_verbosity(tf.logging.INFO)

def do_eval2(sess,
            testPrediction,
            xs_placeholder,
            labels_placeholder,
            istraining_placeholder,
            ds_data_xs,
            ds_labels,
            batch_size):
  """Runs one evaluation against the full epoch of data.
  """
  # total predictions
  tot_pred = np.array([])
  
  # And run one epoch of eval.
  n_data = ds_data_xs.shape[0]
  n_steps = int(n_data / batch_size)
  for step in range(0, n_steps):
    start_idx = batch_size * step
    end_idx = start_idx + batch_size
    if step == n_steps - 1:
        end_idx = n_data
        print(end_idx)

    feed_dict = {
        xs_placeholder: ds_data_xs[start_idx:end_idx,:],
        labels_placeholder: ds_labels[start_idx:end_idx],
        istraining_placeholder: False,
    }
    ( predicted_y) = sess.run(testPrediction, feed_dict=feed_dict)
    
    if tot_pred.size == 0:
      tot_pred = predicted_y
    else:
      tot_pred = np.concatenate( (tot_pred, predicted_y), axis=0)

  print(ds_labels.shape)
  print(tot_pred.shape)
    
  tot_pred = tot_pred.reshape((tot_pred.size))
  ds_labels = ds_labels.reshape((ds_labels.size))
  print(tot_pred.shape)

  print("F-Measure: "+str(f1_score(ds_labels, tot_pred, average='weighted')))
  print("F1 score: " + str(f1_score(ds_labels, tot_pred, average=None)))
  print("Accuracy: "+str(accuracy_score(ds_labels, tot_pred)))

def main(unused_argv):
  """ Main function
  In this function we do:
    1. Import a dataset
    2. Build a model implementing a CNN
    3. Perform training of the CNN
    4. Export the model
  """

  # Training params
  n_epoch = 100
  batch_size = 32
  learning_rate = 0.0001

  ############################################################
  #                     import a dataset 
  ############################################################


  # check number of arguments
  if len(sys.argv) != 4:
110
    print("Usage : <patches> <labels> <export_dir>")
remi cresson's avatar
remi cresson committed
111 112 113
    sys.exit(1)

  # Export dir
114
  export_dir = sys.argv[3]
remi cresson's avatar
remi cresson committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

  print("loading dataset")

  # Create a dataset of size imp_size
  imp_ds_patches = read_samples(sys.argv[1])
  imp_ds_labels  = read_samples(sys.argv[2])
  
  # Shuffle the dataset
  imp_ds_patches,imp_ds_labels = shuffle(imp_ds_patches,imp_ds_labels, random_state=0)
  
  print("ok")
  
   # Number of samples
  if (imp_ds_patches.shape[0] != imp_ds_labels.shape[0]):
    print("Number of samples should be the same as number of patches!")
    sys.exit(1)

  # Number of samples for training  
  n_data_train = int(imp_ds_patches.shape[0] / 2)

  # Size of patches  
  nb_bands = 4
  patch_size_xs = 80
  patch_size_label = 16
  
  # Divide the dataset in two subdatasets : training and validation
  ds_data_train = imp_ds_patches[0:n_data_train,:]
  ds_data_valid = imp_ds_patches[n_data_train:imp_ds_patches.shape[0],:]
  ds_labels_train = imp_ds_labels[0:n_data_train,:]
  ds_labels_valid = imp_ds_labels[n_data_train:imp_ds_patches.shape[0],:]

  ############################################################
  #                    Build the graph
  ############################################################
  
  with tf.Graph().as_default():

    # placeholder for images and labels
    istraining_placeholder = tf.placeholder(tf.bool, shape=(), name="istraining") # Used only for dropout...
    xs_placeholder = tf.placeholder(tf.float32, shape=(None, patch_size_xs, patch_size_xs, nb_bands), name="x1")
    labels_placeholder = tf.placeholder(tf.int32, shape=(None, patch_size_label, patch_size_label, 1), name="y1")
 
    print_tensor_info("xs_placeholder",xs_placeholder)
 
    # Convolutional Layer #1 
    conv1 = tf.layers.conv2d(
      inputs=xs_placeholder,
      filters=32,
      kernel_size=[12, 12],
      padding="valid",
      activation=tf.nn.crelu)
    print_tensor_info("conv1",conv1)
  
    # Normalization of output of layer 1
    norm1 = tf.layers.batch_normalization(conv1)
    print_tensor_info("norm1",norm1)
  
    # pooling layer #1
    pool1 = tf.layers.max_pooling2d(inputs=norm1, pool_size=[4, 4], strides=4)
    print_tensor_info("pool1",pool1)

    # Convolutional Layer #2
    conv2 = tf.layers.conv2d(
      inputs=pool1,
      filters=56,
      kernel_size=[4, 4],
      padding="valid",
      activation=tf.nn.crelu)
    print_tensor_info("conv2",conv2)

     # Normalization of output of layer 2
    norm2 = tf.layers.batch_normalization(conv2)
    print_tensor_info("norm2",norm2)
   
    # Convolutional Layer #3 
    conv3 = tf.layers.conv2d(
      inputs=norm2,
      filters=40,
      kernel_size=[3, 3],
      padding="valid",
      activation=tf.nn.crelu)
    print_tensor_info("conv3",conv3)
 
     # Normalization of output of layer 3
    norm3 = tf.layers.batch_normalization(conv3)
    print_tensor_info("norm3",norm3)
  
    # Convolutional Layer #4
    conv4 = tf.layers.conv2d(
      inputs=norm3,
      filters=1,
      kernel_size=[8, 8],
      padding="valid",
      activation=tf.nn.crelu)
    print_tensor_info("conv4",conv4)
    
    # Deconv = conv on the padded/strided input, that is an (5+1)*4
    deconv1 = tf.layers.conv2d_transpose(
      inputs=conv4,
      filters=1,
      strides=(4,4),
      kernel_size=[8, 8],
      padding="valid",
      activation=tf.nn.sigmoid)
    print_tensor_info("deconv1",deconv1)
 
    numbatch = tf.shape(deconv1)[0]
    szx =      tf.shape(deconv1)[1]
    szy =      tf.shape(deconv1)[2]
    estimated = tf.slice(deconv1, [0, 4, 4, 0], [numbatch, szx - 8, szy - 8, 1], "estimated")
    print_tensor_info("estimated", estimated)
   
    ##################### Loss calculation ###################

    estimated_resized = tf.reshape(estimated, [-1, patch_size_label*patch_size_label])
    labels_resized = tf.reshape(labels_placeholder, [-1, patch_size_label*patch_size_label])
    labels_resized = tf.cast(labels_resized, tf.float32)
    
    testPrediction = tf.to_int32(estimated > 0.5)

    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=labels_resized, logits=estimated_resized))
    
    ###################### Gradient descent ##################
    
    train_op = tf.train.AdamOptimizer(learning_rate, name="optimizer").minimize(loss)
    
    ############### Variable initializer Op ##################

    init = tf.global_variables_initializer()
    
    ######################### Saver ##########################

    saver = tf.train.Saver()
    
    #################### Create a session ####################
    
    sess = tf.Session()
    sess.run(init)

    ############## Here we start the training ################
    
    for curr_epoch in range(n_epoch):

      print("Epoch #" + str(curr_epoch))
      
      ds_data_train,ds_labels_train = shuffle(ds_data_train,ds_labels_train, random_state=0)
  
      # Start the training loop.
      n_steps = int(n_data_train / batch_size)
      for step in range(n_steps):
  
        start_time = time.time()
        
        # Fill a feed dictionary with the actual set of images and labels
        # for this particular training step.
        # Load training and eval data
        start_idx = batch_size * step
        end_idx = start_idx + batch_size 
        feed_dict = {
          xs_placeholder: ds_data_train[start_idx:end_idx,:],
          labels_placeholder: ds_labels_train[start_idx:end_idx,:],
          istraining_placeholder: True,
        }
  
        # Run one step of the model.  The return values are the activations
        # from the `train_op` (which is discarded) and the `loss` Op.  To
        # inspect the values of your Ops or variables, you may include them
        # in the list passed to sess.run() and the value tensors will be
        # returned in the tuple from the call.
        _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

        duration = time.time() - start_time
  
        # Print an overview fairly often.
        if step % 10 == 0:
          # Print status to stdout.
          print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration))

      # Save a checkpoint and evaluate the model periodically.
      if (curr_epoch + 1) % 1 == 0:
        # Evaluate against the training set.
        print('Training Data Eval:')
        do_eval2(sess,
                testPrediction,
                xs_placeholder,
                labels_placeholder,
                istraining_placeholder,
                ds_data_train,
                ds_labels_train,
                batch_size)
        # Evaluate against the validation set.
        print('Validation Data Eval:')
        do_eval2(sess,
                testPrediction,
                xs_placeholder,
                labels_placeholder,
                istraining_placeholder,
                ds_data_valid,
                ds_labels_valid,
                batch_size)
                
        # Let's export a SavedModel
317
        CreateSavedModel(sess, ["x1:0"], ["prediction:0"], export_dir)
remi cresson's avatar
remi cresson committed
318 319 320 321 322 323 324

  quit()
  
if __name__ == "__main__":
  
  tf.add_check_numerics_ops()
  tf.app.run(main)