Home

Awesome

tensorflow-cnn-finetune

This repo is about finetuning some famous convolutional neural nets using TensorFlow.

ConvNets:

Requirements:

Dataset file

You need to setup two dataset files for training and validation. The format must be like following:

/absolute/path/to/image1.jpg class_index
/absolute/path/to/image2.jpg class_index
...

class_index must start from 0.

Sample dataset files can be found at data/train.txt and data/val.txt.

Do not forget to pass --num_classes flag when running finetune.py script.

AlexNet

Go into alexnet folder

cd alexnet

Finetuning

Download the weights if you hadn't before.

./download_weights.sh

Run the finetune.py script with your options.

python finetune.py \
    --training_file=../data/train.txt \
    --val_file=../data/val.txt \
    --num_classes 26
OptionDefaultDescription
--training_file../data/train.txtTraining dataset file
--val_file../data/val.txtValidation dataset file
--num_classes26Number of classes
--train_layersfc8,fc7Layers to be finetuned, seperated by commas. Avaliable layers: fc8, fc7, fc6, conv5, conv4, conv3, conv2, conv1
--num_epochs10How many epochs to run training
--learning_rate0.0001Learning rate for ADAM optimizer
--dropout_keep_prob0.5Dropout keep probability
--batch_size128Batch size
--multi_scaleAs a preprocessing step, it scalse the image randomly between 2 numbers and crop randomly at network's input size. For example if you set it 228,256: - Select a random number between 228 and 256 -- S - Scale input image to S x S pixels - Crop it 227x227 randomly
--tensorboard_root_dir../trainingRoot directory to put the training logs and weights
--log_step10Logging period in terms of a batch run

You can observe finetuning with the tensorboard.

tensorboard --logdir ../training

Testing a dataset file

At the end of each epoch while finetuning, the current state of the weights are saved into ../training folder (or any folder you specified with --tensorboard_root_dir option). Go to that folder and locate the model and epoch you want to test.

You must have your test dataset file as mentinoned before.

python test.py \
    --ckpt ../training/alexnet_XXXXX_XXXX/checkpoint/model_epoch1.ckpt \
    --num_classes 26 \
    --test_file ../data/test.txt
OptionDefaultDescription
--ckptCheckpoint path; it must end with ".ckpt"
--num_classes26Number of classes
--test_file../data/val.txtTest dataset file
--batch_size128Batch size

Predicting a single image

python predict.py \
    --ckpt ../training/alexnet_XXXXX_XXXX/checkpoint/model_epoch1.ckpt \
    --input_image=/some/path/to/image.jpg
OptionDefaultDescription
--ckptCheckpoint path; it must end with ".ckpt"
--num_classes26Number of classes
--input_imageThe path of input image

VGGNet

Go into vggnet folder

cd vggnet

Finetuning

Download the weights if you hadn't before.

./download_weights.sh

Run the finetune.py script with your options.

python finetune.py \
    --training_file=../data/train.txt \
    --val_file=../data/val.txt \
    --num_classes 26
OptionDefaultDescription
--training_file../data/train.txtTraining dataset file
--val_file../data/val.txtValidation dataset file
--num_classes26Number of classes
--train_layersfc8,fc7Layers to be finetuned, seperated by commas. Avaliable layers: fc8, fc7, fc6, conv5_1, conv5_2, conv5_3, conv4_1, conv4_2, conv4_3, conv3_1, conv3_2, conv3_3, conv2_1, conv2_2, conv1_1, conv1_2
--num_epochs10How many epochs to run training
--learning_rate0.0001Learning rate for ADAM optimizer
--dropout_keep_prob0.5Dropout keep probability
--batch_size128Batch size
--multi_scaleAs a preprocessing step, it scalse the image randomly between 2 numbers and crop randomly at network's input size. For example if you set it 228,256: - Select a random number between 228 and 256 -- S - Scale input image to S x S pixels - Crop it 224x224 randomly
--tensorboard_root_dir../trainingRoot directory to put the training logs and weights
--log_step10Logging period in terms of a batch run

You can observe finetuning with the tensorboard.

tensorboard --logdir ../training

Testing a dataset file

At the end of each epoch while finetuning, the current state of the weights are saved into ../training folder (or any folder you specified with --tensorboard_root_dir option). Go to that folder and locate the model and epoch you want to test.

You must have your test dataset file as mentinoned before.

python test.py \
    --ckpt ../training/vggnet_XXXXX_XXXX/checkpoint/model_epoch1.ckpt \
    --num_classes 26 \
    --test_file ../data/test.txt
OptionDefaultDescription
--ckptCheckpoint path; it must end with ".ckpt"
--num_classes26Number of classes
--test_file../data/val.txtTest dataset file
--batch_size128Batch size

Predicting a single image

python predict.py \
    --ckpt ../training/vggnet_XXXXX_XXXX/checkpoint/model_epoch1.ckpt \
    --input_image=/some/path/to/image.jpg
OptionDefaultDescription
--ckptCheckpoint path; it must end with ".ckpt"
--num_classes26Number of classes
--input_imageThe path of input image

ResNet

Go into resnet folder

cd resnet

Finetuning

Download the weights if you hadn't before.

./download_weights.sh

Run the finetune.py script with your options.

python finetune.py \
    --training_file=../data/train.txt \
    --val_file=../data/val.txt \
    --num_classes 26
OptionDefaultDescription
--resnet_depth50ResNet architecture to be used: 50, 101 or 152
--training_file../data/train.txtTraining dataset file
--val_file../data/val.txtValidation dataset file
--num_classes26Number of classes
--train_layersfcLayers to be finetuned, seperated by commas. Fully-connected last layer: fc, tho whole 5th layer: scale5, or some blocks of a layer: scale4/block6,scale4/block5
--num_epochs10How many epochs to run training
--learning_rate0.0001Learning rate for ADAM optimizer
--dropout_keep_prob0.5Dropout keep probability
--batch_size128Batch size
--multi_scaleAs a preprocessing step, it scalse the image randomly between 2 numbers and crop randomly at network's input size. For example if you set it 228,256: - Select a random number between 228 and 256 -- S - Scale input image to S x S pixels - Crop it 224x224 randomly
--tensorboard_root_dir../trainingRoot directory to put the training logs and weights
--log_step10Logging period in terms of a batch run

You can observe finetuning with the tensorboard.

tensorboard --logdir ../training

Testing a dataset file

At the end of each epoch while finetuning, the current state of the weights are saved into ../training folder (or any folder you specified with --tensorboard_root_dir option). Go to that folder and locate the model and epoch you want to test.

You must have your test dataset file as mentinoned before.

python test.py \
    --ckpt ../training/resnet_XXXXX_XXXX/checkpoint/model_epoch1.ckpt \
    --num_classes 26 \
    --test_file ../data/test.txt
OptionDefaultDescription
--ckptCheckpoint path; it must end with ".ckpt"
--resnet_depth50ResNet architecture to be used: 50, 101 or 152
--num_classes26Number of classes
--test_file../data/val.txtTest dataset file
--batch_size128Batch size

Predicting a single image

python predict.py \
    --ckpt ../training/resnet_XXXXX_XXXX/checkpoint/model_epoch1.ckpt \
    --input_image=/some/path/to/image.jpg
OptionDefaultDescription
--ckptCheckpoint path; it must end with ".ckpt"
--resnet_depth50ResNet architecture to be used: 50, 101 or 152
--num_classes26Number of classes
--input_imageThe path of input image