In this example, we finetune a pretrained resnet18 for classification of images with two categries: Ant and Bee. This example is a PyMIC implementation of pytorch's "transfer learning for computer vision tutorial". The orginal tutorial can be found here. In PyMIC's implementation, we only need to edit the configure file to run the code.
- The dataset contains about 120 training images each for ants and bees. There are 75 validation images for each class. Download the data from here and extract it to
PyMIC_data
. Then the path for training and validation set should bePyMIC_data/hymenoptera_data/train
andPyMIC_data/hymenoptera_data/val
, respectively. - Run
python write_csv_files.py
to create two csv files storing the paths and labels of training and validation images. They aretrain_data.csv
andvalid_data.csv
and saved in./config
.
- Here we use resnet18 for finetuning, and update all the layers. Open the configure file
config/train_test_ce1.cfg
. In thenetwork
section we can find details for the network. Hereupdate_mode = all
means updating all the layers.
# type of network
net_type = resnet18
pretrain = True
input_chns = 3
# finetune all the layers
update_mode = all
Then start to train by running:
pymic_train config/train_test_ce1.cfg
- During training or after training, run
tensorboard --logdir model/resnet18_ce1
and you will see a link in the output, such ashttp://your-computer:6006
. Open the link in the browser and you can observe the average loss and accuracy during the training stage, such as shown in the following images, where blue and red curves are for training set and validation set respectively. The iteration number obtained the highest accuracy on the validation set was 400, and may be different based on the hardware environment. After training, you can find the trained models in./model/resnet18_ce1
.
- Run the following command to obtain classification results of testing images. By default we use the best performing checkpoint based on the validation set. You can set
ckpt_mode
to 0 inconfig/train_test_ce1.cfg
to use the latest checkpoint.
mkdir result
pymic_test config/train_test_ce1.cfg
- Then run the following command to obtain quantitative evaluation results in terms of accuracy.
pymic_eval_cls -cfg config/evaluation.cfg
The obtained accuracy by default setting should be around 0.9477, and the AUC will be around 0.9745.
- Run
python show_roc.py
to show the receiver operating characteristic curve.
Similarly to the above example, we further try to only finetune the last layer of resnet18 for the same classification task. Use a different configure file config/train_test_ce2.cfg
for training and testing, where update_mode = last
in the network
section means updating the last layer only:
net_type = resnet18
pretrain = True
input_chns = 3
# finetune the last layer only
update_mode = last
Edit config/evaluation.cfg
accordinly for evaluation. The corresponding accuracy and AUC would be around 0.9477 and 0.9778, respectively.