コード例 #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, 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
コード例 #3
0
ファイル: model.py プロジェクト: vincentvigon/neural
    def __init__(self, encoder: Encoder, Y_last_dim: int, keep_prob: float,
                 favoritism: tuple, depth0: int, depth1: int,
                 either_sig_softmax: bool):
        """"""
        """ 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

        if either_sig_softmax: self.Y_proba = tf.nn.sigmoid(self.Y_logits)
        else: self.Y_proba = tf.nn.softmax(self.Y_logits)

        self.Y_cat_sum = tf.reduce_sum(self.Y_proba, axis=3)
コード例 #4
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
コード例 #5
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)