コード例 #1
0
    def __init__(self, X, multi_label: bool, nbChannels: int,
                 nbCategories: int, _keep_proba):

        self.leNet_bottom = bricks.LeNet_bottom(X, nbChannels)
        """"""
        """on récupère la sortie de leNet_bottom"""
        h_pool2 = self.leNet_bottom.pool2
        '''
          On connecte les 7*7*64 neurones à une nouvelle couche de 1024 neurons
          fc signifie : fully connected.
          '''
        W_fc1 = ing.weight_variable([7 * 7 * 64, 1024])
        b_fc1 = ing.bias_variable([1024])

        h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
        """dropout pour éviter le sur-apprentissage"""
        h_fc1_drop = tf.nn.dropout(h_fc1, _keep_proba)
        ''' on connecte les 1024 neurons avec nbCategories neurones de sorties'''
        W_fc2 = ing.weight_variable([1024, nbCategories])
        b_fc2 = ing.bias_variable([nbCategories])

        if multi_label:
            ''' sigmoid produit un vecteur dont chaque composante est une proba'''
            self.Y = tf.nn.sigmoid(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
        else:
            '''softmax produit un vecteur de probabilité (la somme des composantes fait 1)'''
            self.Y = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
コード例 #2
0
    def __init__(self, multi_label, nbChannels, nbCategories):

        self._X = tf.placeholder(name="X", dtype=tf.float32)
        self._Y = tf.placeholder(name="Y", dtype=tf.float32)

        self.learning_rate = tf.get_variable("learning_rate",
                                             initializer=1e-4,
                                             trainable=False)
        self.keep_proba = tf.get_variable("keep_proba",
                                          initializer=1.,
                                          trainable=False)
        self.threshold = tf.get_variable("threshold",
                                         initializer=0.5,
                                         trainable=False)

        self.hat = Hat_multi_vs_mono(self._X, multi_label, nbChannels,
                                     nbCategories, self.keep_proba)
        """la loss et l'accuracy sont calculée de manière très différentes quand c'est du multi-label"""
        if multi_label:
            self._loss = ing.crossEntropy_multiLabel(self._Y, self.hat.Y)
            Y_hat_binary = tf.cast(
                tf.greater(self.hat.Y, self.threshold), tf.float32
            )  #ing.make_binary_with_threshold(self._Y_hat,self.threshold)
            """ l'accuracy dépend d'un threshold. Attention, une accuracy proche de 1 n'est pas forcément bonne: 
              Ex:  Y_hat_binary = [ 0,0,0,0,0,0,0,0,0,1]
                    Y_hat       = [ 1,0,0,0,0,0,0,0,0,0]
                    ---> accuracy = 80 % 
                    alors que le modèle n'a rien compris.
              """
            self._accuracy = tf.reduce_mean(
                tf.cast(tf.equal(Y_hat_binary, self._Y), tf.float32))
        else:
            self._loss = ing.crossEntropy(self._Y, self.hat.Y)
            Y_cat = tf.argmax(self._Y, dimension=1)
            Y_cat_hat = tf.argmax(self.hat.Y, dimension=1)
            self._accuracy = tf.reduce_mean(
                tf.cast(tf.equal(Y_cat, Y_cat_hat), tf.float32))

        self._minimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(
            self._loss)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        self.verbose = True
コード例 #3
0
    def __init__(self, nbChannels: int, nbCategories: int, nbRegressor: int):

        h_img, w_img = 28, 28

        self.inputSize = (h_img, w_img, nbChannels)
        self.nbRegressor = nbRegressor
        self.nbCategories = nbCategories

        self._X = tf.placeholder(name="X",
                                 dtype=tf.float32,
                                 shape=(None, h_img, w_img, nbChannels))

        tf.summary.image("X", self._X, max_outputs=12)

        self._Y_class = tf.placeholder(name="Y_class",
                                       dtype=tf.float32,
                                       shape=(None, nbCategories))
        self._Y_reg = tf.placeholder(name="Y_reg",
                                     dtype=tf.float32,
                                     shape=(None, nbRegressor))

        self.learning_rate = tf.get_variable("learning_rate",
                                             initializer=1e-2,
                                             trainable=False)
        self.keep_proba = tf.get_variable("keep_proba",
                                          initializer=1.,
                                          trainable=False)

        self.hat = Hat_bounding(self._X, self.keep_proba, nbChannels,
                                nbCategories, nbRegressor)

        #self._Y_class_hat, self._Y_reg_hat = xToY.getYclass_Yreg(self._X, self.keep_proba)

        self._loss_class = 20. * ing.crossEntropy(self._Y_class,
                                                  self.hat.Y_prob, True)
        self._loss_reg = tf.reduce_mean((self._Y_reg - self.hat.Y_bound)**2)

        self._accuracy = tf.reduce_mean(
            tf.cast(tf.equal(self.hat.Y_cat,
                             tf.argmax(self._Y_class, dimension=1)),
                    dtype=tf.float32))

        self._minimizer = tf.train.AdamOptimizer(1e-4).minimize(
            self._loss_class + self._loss_reg)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        self.verbose = True

        tf.summary.scalar("loss_class", self._loss_class)
        tf.summary.scalar("loss_reg", self._loss_reg)
        tf.summary.scalar("accuracy", self._accuracy)

        self._summary = tf.summary.merge_all()
コード例 #4
0
    def __init__(self, X, _keep_proba, nbChannels: int, nbCategories: int,
                 nbRegressor: int):

        leNet_bottom = bricks.LeNet_bottom(X, nbChannels)
        """on récupère la sortie de leNet_bottom"""
        h_pool2 = leNet_bottom.pool2

        W_fc1 = ing.weight_variable([7 * 7 * 64, 1024])
        b_fc1 = ing.bias_variable([1024])

        h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

        h_fc1_drop = tf.nn.dropout(h_fc1, _keep_proba)
        ''' on connecte les 1024 neurons avec nbCategories neurones de sorties'''
        W_fc2 = ing.weight_variable([1024, nbCategories])
        b_fc2 = ing.bias_variable([nbCategories])

        self.Y_prob = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
        self.Y_cat = tf.arg_max(self.Y_prob, dimension=1)
        ''' on connecte les 1024 neurons avec nbRegressor neurones de sorties'''
        W_fc_reg = ing.weight_variable([1024, nbRegressor])
        b_fc_reg = ing.bias_variable([nbRegressor])

        tf.summary.histogram("b_fc2", b_fc2)
        tf.summary.histogram("b_fc1", b_fc1)
        tf.summary.histogram("W_fc2", W_fc2)
        tf.summary.histogram("W_fc1", W_fc1)

        self.Y_bound = tf.matmul(h_fc1_drop, W_fc_reg) + b_fc_reg
コード例 #5
0
    def __init__(self, h_img: int, w_img: int, nbChannels: int, nbCategories,
                 favoritism):

        (self.batch_size, self.h_img, self.w_img,
         self.nbChannels) = (None, h_img, w_img, nbChannels)

        self.nbCategories = nbCategories

        self._X = tf.placeholder(name="X",
                                 dtype=tf.float32,
                                 shape=(None, h_img, w_img, nbChannels))
        """les annotations : une image d'entier, chaque entier correspond à une catégorie"""
        self._Y_cat = tf.placeholder(
            dtype=tf.int32,
            shape=[None, h_img, w_img],
            name="Y",
        )

        self.keep_proba = tf.get_variable("keep_proba",
                                          initializer=1.,
                                          trainable=False)
        self.learning_rate = tf.get_variable("learning_rate",
                                             initializer=1e-3,
                                             trainable=False)

        self.hat = Hat_fullyConv(self._X, nbChannels, nbCategories,
                                 self.keep_proba, favoritism)
        """ une version 'déjà' prête de la loss """
        #self._loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.hat.Y_logits,labels=self._Y_cat)))
        """ la version 'à la main'  """
        self._loss = ing.crossEntropy(
            tf.one_hot(self._Y_cat, self.nbCategories), self.hat.Y_proba)

        self._accuracy = tf.reduce_mean(
            tf.cast(tf.equal(self._Y_cat, self.hat.Y_cat), tf.float32))
        """la cat 0 est la plus présente (c'est le fond de l'image). 
        le classement trivial revient à classer tous les pixels en 0"""
        self._accuracy_trivial = tf.reduce_mean(
            tf.cast(tf.equal(0, self.hat.Y_cat), tf.float32))
        """ optimizer, monitoring des gradients """
        self._optimizer = tf.train.AdamOptimizer(self.learning_rate)
        self._grads_vars = self._optimizer.compute_gradients(self._loss)
        for index, grad in enumerate(self._grads_vars):
            tf.summary.histogram(
                "{}-grad".format(self._grads_vars[index][1].name),
                self._grads_vars[index][0])
            tf.summary.histogram(
                "{}-var".format(self._grads_vars[index][1].name),
                self._grads_vars[index][1])
        """ la minimisation est faite via cette op:  """
        self._minimizer = self._optimizer.apply_gradients(self._grads_vars)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        self.verbose = True

        tf.summary.scalar("loss", self._loss)
        tf.summary.scalar("accuracy", self._accuracy)

        max_outputs = 5
        tf.summary.image("input_image", self._X, max_outputs=max_outputs)
        tf.summary.image("ground_truth",
                         tf.expand_dims(tf.cast(self._Y_cat, tf.float32), 3),
                         max_outputs=max_outputs)
        for cat in range(1, self.nbCategories):
            tf.summary.image("hat_proba cat 1",
                             tf.expand_dims(self.hat.Y_proba[:, :, :, cat], 3),
                             max_outputs=max_outputs)

        self._summary = tf.summary.merge_all()
コード例 #6
0
    def __init__(self, X, nbChannels: int, nbCategories: int, keep_prob,
                 favoritism):
        """"""
        """on récupère le réseau très simple: leNet_bottom"""
        leNet = bricks.LeNet_bottom(X, nbChannels)
        """la sorties est un volume 7*7*64.  """
        """ DANS LA SUITE: on recopie leNet, mais en remplaçant les fully-connected par des convolutions 1*1  """
        """ on transforme le layer d'avant en un volume 7*7*1024 par des conv 1*1"""
        with tf.variable_scope("smallConv0"):
            W = ing.weight_variable([1, 1, 64, 1024], name="W")
            b = ing.bias_variable([1024], name="b")

            conv = tf.nn.conv2d(
                leNet.Y, W, strides=[1, 1, 1, 1], padding="SAME") + b
            relu = tf.nn.relu(conv, name="relu")
            relu_dropout = tf.nn.dropout(relu,
                                         keep_prob=keep_prob,
                                         name="dropout")
        """ on transforme le layer d'avant en un volume 7*7*nbCategories par des conv 1*1"""
        with tf.variable_scope("smallConv1"):
            W = ing.weight_variable([1, 1, 1024, nbCategories], name="W")

            b = ing.bias_variable([nbCategories], name="b")

            conv = tf.nn.conv2d(
                relu_dropout, W, strides=[1, 1, 1, 1], padding="SAME") + b
            relu = tf.nn.relu(conv, name="relu")
            relu_dropout = tf.nn.dropout(relu,
                                         keep_prob=keep_prob,
                                         name="dropout")
        """  DANS LA SUITE : on dilate les images 7*7 pour revenir à la résolution initiale 28*28  """
        """ 7*7*nbCategories ---> 14*14*32 """
        with tf.variable_scope("dilate0"):
            """  [height, width, output_channels, in_channels=nbCategories] """
            W = tf.Variable(initial_value=ing.get_bilinear_initial_tensor(
                [4, 4, 32, nbCategories], 2),
                            name='W')
            b = ing.bias_variable([32], name="b")
            upConv0 = ing.up_convolution(relu_dropout, W, 2, 2) + b
            """on y ajoute le milieu de leNet (14*14*32 aussi)"""
            fuse_1 = upConv0 + leNet.pool1

            ing.summarizeW_asImage(W)
        """on dilate maintenant fuse_1 pour atteindre la résolution des images d'origine
           14*14*32 ----> 28*28*nbCategories
        """
        with tf.variable_scope("dilate1"):
            W = tf.Variable(initial_value=ing.get_bilinear_initial_tensor(
                [4, 4, nbCategories, 32], 2),
                            name='W')
            b = ing.bias_variable([nbCategories], name="b")

            ing.summarizeW_asImage(W)
        """ les logits (on y applique pas le softmax car plus loin on utilisera la loss tf.nn.sparse_softmax_cross_entropy_with_logits) """
        self.Y_logits = ing.up_convolution(fuse_1, W, 2, 2) + b

        self.Y_proba = tf.nn.softmax(self.Y_logits)
        """ chaque pixel reçoit la catégorie qui a la plus forte probabilité, en tenant compte du favoritisme."""
        self.Y_cat = tf.cast(
            tf.argmax(self.Y_proba * favoritism,
                      dimension=3,
                      name="prediction"), tf.int32)
コード例 #7
0
ファイル: model.py プロジェクト: vincentvigon/neural
    def __init__(self, X: tf.Tensor, nbChannels: int):

        self.nbChannels = nbChannels
        nbSummaryOutput = 4
        """"""
        ''' couche de convolution 1'''
        with tf.variable_scope("conv1"):
            W_conv1 = ing.weight_variable([5, 5, self.nbChannels, 32],
                                          name="W")
            b_conv1 = ing.bias_variable([32], name="b")

            self.filtred1 = tf.nn.relu(ing.conv2d_basic(X, W_conv1, b_conv1))
            """ shape=(?,14*14,nbChannels)  """
            self.pool1 = ing.max_pool_2x2(self.filtred1)

            ing.summarizeW_asImage(W_conv1)
            tf.summary.image("filtred",
                             self.filtred1[:, :, :, 0:1],
                             max_outputs=nbSummaryOutput)
        ''' couche de convolution 2'''
        with tf.variable_scope("conv2"):

            W_conv2 = ing.weight_variable([5, 5, 32, 64], name="W")
            b_conv2 = ing.bias_variable([64], name="b")

            self.filtred2 = tf.nn.relu(
                ing.conv2d_basic(self.pool1, W_conv2, b_conv2))
            """ shape=(?,7*7,nbChannels)  """
            self.pool2 = ing.max_pool_2x2(self.filtred2)

            ing.summarizeW_asImage(W_conv2)
            tf.summary.image("filtred",
                             self.filtred2[:, :, :, 0:1],
                             max_outputs=12)
        """un alias pour la sortie"""
        self.Y = self.pool2
コード例 #8
0
ファイル: model.py プロジェクト: vincentvigon/neural
    def __init__(self, h_img: int, w_img: int, nbChannels: int, nbCategories,
                 favoritism, depth0, depth1):

        self.nbConsecutiveOptForOneFit = 1
        self.summaryEither_cat_proba = 0

        (self.batch_size, self.h_img, self.w_img,
         self.nbChannels) = (None, h_img, w_img, nbChannels)

        self.nbCategories = nbCategories
        """ PLACEHOLDER """
        self._X = tf.placeholder(name="X",
                                 dtype=tf.float32,
                                 shape=(None, h_img, w_img, nbChannels))
        """les annotations : une image d'entier, chaque entier correspond à une catégorie"""
        self._Y_proba = tf.placeholder(
            dtype=tf.float32,
            shape=[None, h_img, w_img, nbCategories],
            name="Y")
        self._itr = tf.placeholder(name="itr", dtype=tf.float32)

        self.keep_proba = tf.get_variable("keep_proba",
                                          initializer=1.,
                                          trainable=False)
        self.learning_rate = tf.get_variable("learning_rate",
                                             initializer=1e-2,
                                             trainable=False)

        self.hat = Hat_fullyConv(self._X, nbChannels, nbCategories,
                                 self.keep_proba, favoritism, depth0, depth1)
        """ les loss qu'on suivra sur le long terme. Le *10 c'est juste pour mieux interpréter  """
        self._loss_instances = -10 * matching_IoU_batch(
            self._Y_proba[:, :, :, 1:], self.hat.Y_proba[:, :, :, 1:])
        self._loss_background = -10 * just_IoU_batch(
            self._Y_proba[:, :, :, 0], self.hat.Y_proba[:, :, :, 0])
        self._penalty = 10 * sobel_penalty(self.hat.Y_proba, self.nbCategories)
        """ si le coef devant la _loss_background est trop grand, la loss_instance reste bloquée à 0.
            mais s'il est trop petit le background se transforme en damier !"""

        self._loss = self._loss_instances + tf.nn.sigmoid(
            self._itr - 5) * self._loss_background + 5. * self._penalty

        tf.summary.scalar("loss", self._loss)
        tf.summary.scalar("loss instances", self._loss_instances)
        tf.summary.scalar("loss background", self._loss_background)
        tf.summary.scalar("penalty", self._penalty)
        """ optimizer, monitoring des gradients """
        adam_opt = tf.train.AdamOptimizer(self.learning_rate)
        _grads_vars = adam_opt.compute_gradients(self._loss)
        for index, grad in enumerate(_grads_vars):
            tf.summary.histogram("{}-grad".format(_grads_vars[index][0].name),
                                 _grads_vars[index][0])
            tf.summary.histogram("{}-var".format(_grads_vars[index][1].name),
                                 _grads_vars[index][1])
            if len(_grads_vars[index][0].get_shape().as_list()) == 4:
                ing.summarizeW_asImage(_grads_vars[index][0])

        self._summary = tf.summary.merge_all()
        """ la minimisation est faite via cette op:  """
        self.step_op = adam_opt.apply_gradients(_grads_vars)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        self.verbose = True

        max_outputs = 4
        tf.summary.image("input_image", self._X, max_outputs=max_outputs)

        if self.summaryEither_cat_proba == 0:
            output = tf.expand_dims(tf.cast(self.hat.Y_cat, dtype=tf.float32),
                                    3)
            output_color = ing.colorize(
                output, vmin=0.0, vmax=self.nbCategories,
                cmap='plasma')  #'viridis', 'plasma', 'inferno', 'magma'
            tf.summary.image("Y_hat", output_color)
        else:
            for cat in range(0, self.nbCategories):
                tf.summary.image("hat_proba cat" + str(cat),
                                 tf.expand_dims(self.hat.Y_proba[:, :, :, cat],
                                                3),
                                 max_outputs=max_outputs)

        self._summary = tf.summary.merge_all()
コード例 #9
0
ファイル: model.py プロジェクト: vincentvigon/neural
    def __init__(self, encoder:Encoder, Y_last_dim:int, keep_prob:float, favoritism:tuple, depth0:int, depth1:int):
        """"""


        """ on transforme le layer d'avant en un volume 7*7*depth0 par des conv 1*1"""
        with tf.variable_scope("smallConv0"):
            W = ing.weight_variable([1, 1, 64, depth0], name="W")
            b= ing.bias_variable([depth0], name="b")

            conv = tf.nn.conv2d(encoder.Y, W, strides=[1, 1, 1, 1], padding="SAME") + b
            relu = tf.nn.relu(conv, name="relu")
            relu_dropout = tf.nn.dropout(relu, keep_prob=keep_prob,name="dropout")


        """ on transforme le layer d'avant en un volume 7*7*nbCategories par des conv 1*1"""
        with tf.variable_scope("smallConv1"):
            W = ing.weight_variable([1, 1, depth0,depth1], name="W")
            b = ing.bias_variable([depth1], name="b")

            conv = tf.nn.conv2d(relu_dropout, W, strides=[1, 1, 1, 1], padding="SAME") + b
            relu = tf.nn.relu(conv, name="relu")
            relu_dropout = tf.nn.dropout(relu, keep_prob=keep_prob, name="dropout")



        """  DANS LA SUITE : on dilate les images 7*7 pour revenir à la résolution initiale 28*28  """


        """ 7*7*depth1 ---> 14*14*32 """
        with tf.variable_scope("dilate0"):

            """  [height, width, output_channels, in_channels=nbCategories] """
            W = tf.Variable(initial_value=ing.get_bilinear_initial_tensor([4, 4, 32, depth1],2),name='W')
            b = ing.bias_variable([32], name="b")
            upConv0 = ing.up_convolution(relu_dropout, W, 2, 2) + b
            """on y ajoute le milieu de leNet (14*14*32 aussi)"""
            fuse_1 = upConv0 + encoder.pool1

            ing.summarizeW_asImage(W)



        """on dilate maintenant fuse_1 pour atteindre la résolution des images d'origine
           14*14*32 ----> 28*28*nbCategories
        """
        with tf.variable_scope("dilate1"):
            W = tf.Variable(initial_value=ing.get_bilinear_initial_tensor([4, 4, Y_last_dim, 32], 2), name='W')
            b = ing.bias_variable([Y_last_dim], name="b")

            ing.summarizeW_asImage(W)

        """ les logits (on y applique pas le softmax car plus loin on peut éventuellement utiliser tf.nn.sparse_softmax_cross_entropy_with_logits) """
        self.Y_logits = ing.up_convolution(fuse_1,W,2,2) +b





        self.Y_proba= tf.nn.softmax(self.Y_logits)

        if favoritism is None: favoritism=1
        """ chaque pixel reçoit la catégorie qui a la plus forte probabilité, en tenant compte du favoritisme."""
        self.Y_cat = tf.cast(tf.argmax(self.Y_proba*favoritism, dimension=3, name="prediction"),tf.int32)
コード例 #10
0
ファイル: model.py プロジェクト: vincentvigon/neural
    def __init__(self,h_img:int,w_img:int,nbChannels:int,nbCategories,nbRegressor,favoritism,depth0,depth1):

        self.nbConsecutiveOptForOneFit=1
        self.summaryEither_cat_proba=0
        self.nbRegressor=nbRegressor

        (self.batch_size,self.h_img, self.w_img, self.nbChannels)=(None,h_img,w_img,nbChannels)

        self.nbCategories=nbCategories

        """ PLACEHOLDER """
        self._X = tf.placeholder(name="X", dtype=tf.float32,shape=(None,h_img,w_img,nbChannels))
        """les annotations : une image d'entier, chaque entier correspond à une catégorie"""
        self._Y_cat = tf.placeholder(dtype=tf.int32, shape=[None, h_img, w_img], name="Y_cat" )
        self._Y_reg = tf.placeholder(dtype=tf.float32, shape=[None, h_img, w_img,nbRegressor], name="Y_cat" )

        self._itr = tf.placeholder(name="itr", dtype=tf.float32)


        self.keep_proba=tf.get_variable("keep_proba",initializer=1.,trainable=False)
        self.learning_rate=tf.get_variable("learning_rate",initializer=1e-2,trainable=False)



        """la sorties est un volume 7*7*64.  """
        encoder = Encoder(self._X, nbChannels)

        self.hat=Decoder(encoder,  nbCategories, self.keep_proba, favoritism, depth0, depth1)
        self.hat_reg=Decoder(encoder, self.nbRegressor, self.keep_proba, favoritism, depth0, depth1)



        """ les loss qu'on suivra sur le long terme. Les coef, c'est juste pour avoir des grandeurs faciles à lire  """
        self.where=tf.cast((self._Y_cat!=0),dtype=tf.float32)
        self._loss_reg = 0.1 * tf.reduce_mean(self.where*(self._Y_reg-self.hat_reg.Y_logits)**2)

        self._loss_cat =tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.hat.Y_logits, labels=self._Y_cat)))

        self._penalty=10*sobel_penalty(self.hat.Y_proba,self.nbCategories)

        """ si le coef devant la _loss_background est trop grand, la loss_instance reste bloquée à 0.
            mais s'il est trop petit le background se transforme en damier !"""


        self._loss=self._loss_cat + self._loss_reg


        tf.summary.scalar("log loss", tf.log(self._loss))
        tf.summary.scalar("log loss reg", tf.log(self._loss_reg))
        tf.summary.scalar("log loss cat", tf.log(self._loss_cat))
        tf.summary.scalar("log penalty", tf.log(self._penalty))


        """ optimizer, monitoring des gradients """
        adam_opt = tf.train.AdamOptimizer(self.learning_rate)
        _grads_vars = adam_opt.compute_gradients(self._loss)
        # for index, grad in enumerate(_grads_vars):
        #     tf.summary.histogram("{}-grad".format(_grads_vars[index][0].name), _grads_vars[index][0])
        #     tf.summary.histogram("{}-var".format(_grads_vars[index][1].name), _grads_vars[index][1])
        #     if len(_grads_vars[index][0].get_shape().as_list())==4:
        #         ing.summarizeW_asImage(_grads_vars[index][0])


        self._summary = tf.summary.merge_all()

        """ la minimisation est faite via cette op:  """
        self.step_op = adam_opt.apply_gradients(_grads_vars)




        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        self.verbose=True


        max_outputs=8
        tf.summary.image("input_image", self._X, max_outputs=max_outputs)


        if self.summaryEither_cat_proba==0:
            output = tf.expand_dims(tf.cast(self.hat.Y_cat,dtype=tf.float32),3)
            output_color = ing.colorize(output, vmin=0.0, vmax=self.nbCategories, cmap='plasma') #'viridis', 'plasma', 'inferno', 'magma'
            tf.summary.image("Y_hat",output_color)


            for i in range(self.nbRegressor):
                output2 = tf.expand_dims(tf.cast(self.hat_reg.Y_logits[:,:,:,i], dtype=tf.float32), 3)
                """   maxNbStrate depend of the size of cells. """
                output_color2 = ing.colorize(output2, vmin=None, vmax=None,cmap='plasma')  # 'viridis', 'plasma', 'inferno', 'magma'
                tf.summary.image("Y_reg_hat"+str(i), output_color2)

        else :
            for cat in range(0,self.nbCategories):
                tf.summary.image("hat_proba cat"+str(cat), tf.expand_dims(self.hat.Y_proba[:,:,:,cat],3), max_outputs=max_outputs)


        self._summary=tf.summary.merge_all()
コード例 #11
0
ファイル: model.py プロジェクト: vincentvigon/neural
    def __init__(self, h_img: int, w_img: int, nbChannels: int, nbCategories,
                 favoritism, depth0, depth1):

        self.nbConsecutiveOptForOneFit = 1
        self.summaryEither_cat_proba = 0

        (self.batch_size, self.h_img, self.w_img,
         self.nbChannels) = (None, h_img, w_img, nbChannels)

        self.nbCategories = nbCategories
        """ PLACEHOLDER """
        self._X = tf.placeholder(name="X",
                                 dtype=tf.float32,
                                 shape=(None, h_img, w_img, nbChannels))
        """les annotations : une image d'entier, chaque entier correspond à une catégorie"""
        self._Y_cat = tf.placeholder(dtype=tf.float32,
                                     shape=[None, h_img, w_img, nbCategories],
                                     name="Y_cat")
        self._Y_background = tf.placeholder(dtype=tf.float32,
                                            shape=[None, h_img, w_img, 2],
                                            name="Y_background")

        self._itr = tf.placeholder(name="itr", dtype=tf.int32)

        self.keep_proba = tf.get_variable("keep_proba",
                                          initializer=1.,
                                          trainable=False)
        self.learning_rate = tf.get_variable("learning_rate",
                                             initializer=1e-2,
                                             trainable=False)
        """la sorties est un volume 7*7*64.  """
        encoder = Encoder(self._X, nbChannels)

        self.hat = Decoder(encoder, nbCategories, self.keep_proba, favoritism,
                           depth0, depth1, True)

        self.hat_background = Decoder(encoder, 2, self.keep_proba, favoritism,
                                      depth0, depth1, False)
        """ les loss qu'on suivra sur le long terme. Les coef, c'est juste pour avoir des grandeurs faciles à lire  """

        where = tf.cast((self._Y_background[:, :, :, 1] == 1),
                        dtype=tf.float32)
        self._loss_background = -tf.reduce_mean(
            self._Y_background * tf.log(self.hat_background.Y_proba + 1e-10))
        self._loss_cat = ing.crossEntropy_multiLabel(self._Y_cat,
                                                     self.hat.Y_proba)

        self._penalty = 10 * sobel_penalty(self.hat.Y_proba, self.nbCategories)
        """ si le coef devant la _loss_background est trop grand, la loss_instance reste bloquée à 0.
            mais s'il est trop petit le background se transforme en damier !"""

        self._loss = self._loss_cat  #+self._loss_background

        tf.summary.scalar("loss", self._loss)
        tf.summary.scalar("loss cat", self._loss_cat)
        tf.summary.scalar("loss background", self._loss_background)
        tf.summary.scalar("penalty", self._penalty)

        tf.summary.histogram("hat_Y_cat", self.hat.Y_proba)
        shape = self.hat.Y_proba[0, :, :, :].get_shape().as_list()
        tf.summary.scalar(
            "zero of Y_hat_proba",
            tf.count_nonzero(self.hat.Y_proba[0, :, :, :]) -
            shape[0] * shape[1] * shape[2])
        """ optimizer, monitoring des gradients """
        adam_opt = tf.train.AdamOptimizer(self.learning_rate)
        _grads_vars = adam_opt.compute_gradients(self._loss)
        # for index, grad in enumerate(_grads_vars):
        #     tf.summary.histogram("{}-grad".format(_grads_vars[index][0].name), _grads_vars[index][0])
        #     tf.summary.histogram("{}-var".format(_grads_vars[index][1].name), _grads_vars[index][1])
        #     if len(_grads_vars[index][0].get_shape().as_list())==4:
        #         ing.summarizeW_asImage(_grads_vars[index][0])

        self._summary = tf.summary.merge_all()
        """ la minimisation est faite via cette op:  """
        self.step_op = adam_opt.apply_gradients(_grads_vars)

        self.rien = tf.ones(1)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        self.verbose = True

        max_outputs = 8
        tf.summary.image("input_image", self._X, max_outputs=max_outputs)

        self._Y_cat_sum = tf.reduce_sum(self._Y_cat, axis=3)

        if self.summaryEither_cat_proba == 0:

            output = tf.expand_dims(
                tf.cast(self.hat.Y_cat_sum, dtype=tf.float32), 3)
            output_color = ing.colorize(output,
                                        vmin=0.0,
                                        vmax=self.nbCategories,
                                        cmap='plasma')
            tf.summary.image("Y hat strates",
                             output_color,
                             max_outputs=max_outputs)

            # output = tf.expand_dims(tf.cast(self._Y_cat_sum,dtype=tf.float32),3)
            # output_color = ing.colorize(output, vmin=0.0, vmax=self.nbCategories, cmap='plasma') #'viridis', 'plasma', 'inferno', 'magma'
            # tf.summary.image("ground truth",output_color)
            #
            # output = tf.expand_dims(tf.cast(self.hat.Y_cat_sum, dtype=tf.float32), 3)
            # output_color = ing.colorize(output, vmin=None, vmax=None,
            #                             cmap='plasma')  # 'viridis', 'plasma', 'inferno', 'magma'
            # tf.summary.image("hat strates", output_color)
            #
            #
            # output = tf.expand_dims(tf.cast(self.hat_background.Y_proba[:,:,:,0], dtype=tf.float32), 3)
            # output_color = ing.colorize(output, vmin=0.0, vmax=self.nbCategories,
            #                             cmap='plasma')  # 'viridis', 'plasma', 'inferno', 'magma'
            # tf.summary.image("hat background", output_color)

        else:
            for cat in range(0, self.nbCategories):
                tf.summary.image("hat_proba cat" + str(cat),
                                 tf.expand_dims(self.hat.Y_proba[:, :, :, cat],
                                                3),
                                 max_outputs=max_outputs)

        self._summary = tf.summary.merge_all()