2
$\begingroup$

I'm trying to retrain the final layer of a pretrained model with a new image dataset using TensorFlow-Slim.

Lets say I want to fine-tuning inception-v3 on flowers dataset. Inception_v3 was trained on ImageNet with 1000 class labels, but the flowers dataset only have 5 classes. Since the dataset is quite small we will only train the new layers.

An example at official tf github page shows how to do it:

$ DATASET_DIR=/tmp/flowers
$ TRAIN_DIR=/tmp/flowers-models/inception_v3
$ CHECKPOINT_PATH=/tmp/my_checkpoints/inception_v3.ckpt
$ python train_image_classifier.py \
    --train_dir=${TRAIN_DIR} \
    --dataset_dir=${DATASET_DIR} \
    --dataset_name=flowers \
    --dataset_split_name=train \
    --model_name=inception_v3 \
    --checkpoint_path=${CHECKPOINT_PATH} \
    --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits/Logits \
    --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits/Logits

I couldn't fully understand all the parameters in the above code:

train_dir = ?

dataset_dir = new dataset directory location

dataset_name = name of the dataset (but why ? )

dataset_split_name = ?

model_name = name of the model on which we want to train

checkpoint_path = path to the model checkpoint

checkpoint_exclude_scopes = ?

trainable_scopes = ?


Help me figure out what these parameter means and correct me if I'm wrong about any of the parameter?

Note: I'm aware that we can retrain inception_v3 with a method mentioned at official tensorflow website but I want to do the same with tensorflow-slim.

$\endgroup$

2 Answers 2

2
$\begingroup$

You can get all information you need from the source code.

  • train_dir: Directory where checkpoints and event logs are written to. (default: /tmp/tfmodel/)

  • dataset_dir: The directory where the dataset files are stored.

  • dataset_name: The name of the dataset to load. (default: imagenet)

  • dataset_split_name: The name of the train/test split. (default: train)

  • model_name: The name of the architecture to train. (default: inception_v3)

  • checkpoint_path: The path to a checkpoint from which to fine-tune.

  • checkpoint_exclude_scopes: Comma-separated list of scopes of variables to exclude when restoring from a checkpoint.

  • trainable_scopes: Comma-separated list of scopes to filter the set of variables to train. By default, None would train all the variables.

$\endgroup$
7
  • $\begingroup$ If I'm using a custom dataset, why should I give dataset_name? Is that train_dir works just like:file_writer = tf.summary.FileWriter('/path/to/logs', sess.graph) collecting event logs. How do you find these list of scopes of variables to be exclude or list of scopes to filter the set of variables to train without interrupting the model? $\endgroup$ Commented Mar 12, 2017 at 11:01
  • $\begingroup$ After reading the source code, I found out train_image_classifier.py script didn't provides custom dataset support. Dataset is restricted here and here. If you insist fine-tuning with tf-slim, maybe you need to implement the dataset part yourself. $\endgroup$
    – Icyblade
    Commented Mar 12, 2017 at 11:13
  • $\begingroup$ But the example in the question shows how to fine tune pretrained inception model on imagenet, with custom dataset flower. $\endgroup$ Commented Mar 12, 2017 at 11:22
  • 1
    $\begingroup$ flowers is actually a built-in dataset. Checkout github.com/tensorflow/models/blob/master/slim/datasets/… $\endgroup$
    – Icyblade
    Commented Mar 12, 2017 at 11:32
  • $\begingroup$ Do you have any tips that will help me implement the dataset part myself? $\endgroup$ Commented Mar 12, 2017 at 11:51
1
$\begingroup$

In addition to what @Icyblade suggested by citing the source code, I'd like to add a few things.

  • dataset_name is an option that tells slim how to read the TFRecords located in the dataset_dir folder.
    The default value imagenet means that the TFRecords have to have this format: train-00146-of-00168.tfrecord or validation-00003-of-00019.tfrecord.
    The value flowers means that slim expects the TFRecords to have the form flowers_train_00146-of-00168.tfrecord or flowers_validation_00003-of-00019.tfrecord. This peculiar way of formatting the tfrecords is defined here in the source code.
    Different sample datasets have different formatting rules, e.g. here you can see cifar10.
  • Moreover, dataset_split_name tells the converter how to name the TFRecords files. For the flowers dataset they are train and validation, for cifar10 they are train and test, etc.
  • About checkpoint_exclude_scopes and trainable_scopes, to make a long story short, these commands tell the training algorithm to remove the final two layers of the network from the last checkpoint available (either passed by the checkpoint_path option or read from the folder specified in train_dir), and make it retrain the CNN on them, but using the images you provide inside the TFRecords file. Thus, using these options means to perform a retrain -or, as the slim devs call it in the README, a fine-tuning from an existing checkpoint (link in the comments)- of a CNN on another set of images.

To give you an hint about how to build your own TFRecord converter/creator, I'd suggest to base it on the download_and_convert_flowers.py script, since the input dataset is regular, with the images divided in categories, where each category is a single folder.
Moreover, I'd extend it by adding the option to read input png images (the flowers pictures are only jpg, and thus the script doesn't need this conversion, but your future dataset may). Start by de-assembling it piece by piece and remove the hard-coded information related to the flowers dataset, to make it less specific.

$\endgroup$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.