WIP: TF2
Here we'll port some models to TF2 and add some training script examples
Merge request reports
Activity
- cnn_training/centerloss.py 0 → 100644
48 49 # WEIGHTS BEWTWEEN the two losses 50 LOSS_WEIGHTS = {"cross_entropy": 1.0, "center_loss": 0.01} 51 52 53 class CenterLossModel(tf.keras.Model): 54 def compile( 55 self, 56 cross_entropy, 57 center_loss, 58 loss_weights, 59 train_loss, 60 train_cross_entropy, 61 train_center_loss, 62 test_acc, 63 global_batch_size, changed this line in version 2 of the diff
- cnn_training/centerloss.py 0 → 100644
121 inputs=model.input, outputs=[logits, prelogits], name=model.name 122 ) 123 return model 124 125 126 def build_and_compile_model(n_classes, learning_rate, global_batch_size): 127 model = create_model(n_classes) 128 129 cross_entropy = tf.keras.losses.SparseCategoricalCrossentropy( 130 from_logits=True, name="cross_entropy", reduction=tf.keras.losses.Reduction.NONE 131 ) 132 center_loss = CenterLoss( 133 centers_layer=model.get_layer("centers"), 134 alpha=0.9, 135 name="center_loss", 136 reduction=tf.keras.losses.Reduction.NONE, changed this line in version 2 of the diff
I am wondering if this is the correct package to implement functionality from deep networks. Actually, I am currently proposing a Master's project to build a generic package
bob.bio.dnn
that includes interfaces for several deep learning frameworks as well as pre-trained networks for feature extraction, and face detectors. @sebastien.marcel can tell you more about this. I know that some packages for feature extraction with CNNs already exist, but it would be nice to have a package that provides generic implementations for many frameworks.Maybe that new package might be a good place the have scripts to train some networks, too.
Hi Manuel,
We have
bob.bio.face_ongoing
(https://gitlab.idiap.ch/bob/bob.bio.face_ongoing) andbob.bio.htface
(https://gitlab.idiap.ch/bob/bob.bio.htface) that contains some scripts to train CNNs for respectively face and Heterogenous face recognition. Then we have thebob.ip.tensorflow_extractor
that contains some feature extractors based on TensorFlow.Furthermore, we have
bob.learn.tensorflow
(https://gitlab.idiap.ch/bob/bob.learn.tensorflow) andbob.learn.pytorch
(https://gitlab.idiap.ch/bob/bob.learn.pytorch) that contains generic functionalities (some CNN backbones, specific ways to preprocess data using tensors, some specific losses,...) for respectively, TF and PyTorch.As you can see, we have a lot of packages to maintain and a lot of stuff for a newcomer to look at (and here I'm mentioning only FR). I would like to keep all FR stuff in one place, so newcomers can i- Arrive, ii-) git clone, iii- Create a conda env and iv- work right away. This will also reduce our load of packages to maintain and would make the job a newcomer less stressful.
With this new iteration of our development, which I would be happy to brief you via Zoom if you would like, we are trying to reduce the number of packages. Hence, adding these scripts here it's just a move in this direction.
Thanks
Edited by Tiago de Freitas PereiraThanks, Tiago, I am aware of all of these packages. The idea of the proposed package is to consolidate all the efforts for face processing with DNNs in one package. For example, there are pre-trained face recognition network that are available in MxNet: https://github.com/deepinsight/insightface for which there exists no package in Bob yet -- and as you mentioned it might not be a good idea to add another package particularly for MxNet. There is a generic interface of OpenCV, which is able to use pre-trained networks from several types of frameworks (including the outdated Caffe framework, for which we still have some networks), which would be a good starting point for a generic interface.
But maybe you are right,
bob.bio.face
might actually be the right package to put all these things into since all of the methods are related to face recognition (at least as of now).W.r.t. the Zoom call, I will send you an email.
For example, there are pre-trained face recognition network that are available in MxNet: https://github.com/deepinsight/insightface
We incorporated this in
bob.bio.face
too. It's in a branchWe also have a tutorial to convert mxnext weights to tensorflow in https://gitlab.idiap.ch/biometric/mxnet-to-tensorflow but I guess you cannot see it.
To add to this conversation, I think FR is deep learning now so it doesn't make sense to have bob.bio.face and not have deep learning code in here. Although, the dependencies might become too big at some point if we start depending on several deep learning frameworks. So I think the best option is to keep these dependencies optional.
@tiago.pereira You have pushed a random empty file in here, pls remove that.
- cnn_training/centerloss.py 0 → 100644
168 @click.option( 169 "-b", 170 "--batch-size", 171 default=90, 172 help="Batch size. Be aware that we are using single precision. Batch size should be high.", 173 ) 174 @click.option( 175 "-e", "--epochs", default=35, help="Number of epochs", 176 ) 177 def train_and_evaluate(tf_record_paths, checkpoint_path, n_classes, batch_size, epochs): 178 # number of training steps to do before validating a model. This also defines an epoch 179 # for keras which is not really true. We want to evaluate every 180000 (90 * 2000) 180 # samples 181 STEPS_PER_EPOCH = 180000 // batch_size 182 learning_rate = 0.1 183 KERAS_EPOCH_MULTIPLIER = 6 - cnn_training/centerloss_mixed_precision.py 0 → 100644
181 182 183 @click.command() 184 @click.argument("tf-record-paths") 185 @click.argument("checkpoint-path") 186 @click.option( 187 "-n", 188 "--n-classes", 189 default=87662, 190 help="Number of classes in the classification problem. Default to `87662`, which is the number of identities in our pruned MSCeleb", 191 ) 192 @click.option( 193 "-b", 194 "--batch-size", 195 default=90 * 2, 196 help="Batch size. Be aware that we are using single precision. Batch size should be high.", - cnn_training/centerloss_mixed_precision.py 0 → 100644
116 images, labels = data 117 logits, prelogits = self(images, training=False) 118 self.test_acc(accuracy_from_embeddings(labels, prelogits)) 119 return {m.name: m.result() for m in [self.test_acc]} 120 121 122 def create_model(n_classes): 123 124 model = BACKBONE( 125 include_top=True, 126 classes=n_classes, 127 bottleneck=True, 128 input_shape=OUTPUT_SHAPE + (3,), 129 kernel_regularizer=tf.keras.regularizers.L2(5e-5) 130 ) 131 float32_layer = layers.Activation("linear", dtype="float32") I have also been working with converting these networks, but this is cumbersome and might not work in all circumstances for all source and target frameworks and models. I was using
mmdnn
(https://github.com/microsoft/MMdnn) for the conversion. A generic interface like the one from OpenCV.DNN (https://github.com/opencv/opencv/wiki/Deep-Learning-in-OpenCV) might be a good starting point for any network from any framework, including ONNX.I totally agree that we should keep all dependencies optional. AFAIR we have some infrastructure available to make tests optional, too, depending on the availability of frameworks.