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()
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
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()