项目作者: ryanfwy

项目描述 :
Bilinear CNN implementation in keras.
高级语言: Python
项目地址: git://github.com/ryanfwy/BCNN-keras-clean.git
创建时间: 2019-01-29T03:17:06Z
项目社区:https://github.com/ryanfwy/BCNN-keras-clean

开源协议:MIT License

下载


BCNN Keras Clean

This repository is a clean version of BCNN (Bilinear CNN) implementation with keras.

The notebook is totally translated to the structured codes, which would be much more convenient to run with cloud servers, such as AWS, in the background.

Most of the codes are kept as same as the notebook, while some codes are modified for better structure. Also know that some training parameters are changed too.

Bilinear CNN

Bilinear CNN [1] is simple yet powerful model for Fine-Grained Visual Classification task. Here, B-CNN D-D model is implemented with keras.

Bilinear-CNN architecture

Usages

For practical usage, we just need to invoke two functions transfer_learning() and fine_tuning() from main.py. In most cases, we should first invoke transfer_learning() to transfer the model to our target classes, then invoke fine_tuning() to fine tune all the layers.

Here’s a demo, we try to train a 4-class BCNN model. At the beginning, ensure all the requirements are poperly installed in the environment.

Firstly, the dataset should be prepared as follow.

  1. ./train
  2. |____ class1
  3. |____ class2
  4. |____ class3
  5. |____ class4
  6. ./validation
  7. |____ class1
  8. |____ class2
  9. |____ class3
  10. |____ class4

After that, rewrite main.py or just import it.

  1. import os
  2. TRAIN_DIR = './train'
  3. VALID_DIR = './validation'
  4. # Start transfer learning
  5. TENSORBOARD_DIR = './logs_tl'
  6. CHECKPOINT_DIR = './checkpoints'
  7. if not os.path.exists(TENSORBOARD_DIR):
  8. os.makedirs(TENSORBOARD_DIR)
  9. if not os.path.exists(CHECKPOINT_DIR):
  10. os.makedirs(CHECKPOINT_DIR)
  11. # Transfer the model to 4 classes, images and labels of
  12. # each class should be well prepared in train and validataion directory.
  13. transfer_learning(
  14. TRAIN_DIR,
  15. VALID_DIR,
  16. no_class=4,
  17. batch_size=64)
  18. # Start fine-tuning
  19. MODEL_WEIGHTS_PATH = './model_weights.h5'
  20. TENSORBOARD_DIR = './logs_ft'
  21. CHECKPOINT_DIR = './checkpoints'
  22. if not os.path.exists(TENSORBOARD_DIR):
  23. os.makedirs(TENSORBOARD_DIR)
  24. if not os.path.exists(CHECKPOINT_DIR):
  25. os.makedirs(CHECKPOINT_DIR)
  26. # Load the generated weights from transer learning,
  27. # then fine tune all layers.
  28. fine_tuning(
  29. TRAIN_DIR,
  30. VALID_DIR,
  31. model_weights_path=MODEL_WEIGHTS_PATH,
  32. no_class=4,
  33. batch_size=32,
  34. tensorboard_dir=TENSORBOARD_DIR,
  35. checkpoint_dir=CHECKPOINT_DIR)

Requirements

  • tensorflow-gpu=1.10.0: any version that match you GPU, or simply the latest version of tensorflow for CPU.
  • numpy=1.14.5: for scientific computing.
  • opencv-python=4.0.0.21: for image resizing.
  • h5py=2.6.0: for model weights saving.

NOTE: Keras has its own methods for data augmentation, but the notebook has rewritten the preprocessing functions with center cropping and random cropping. Therefore, OpenCV is required in order to follow the source code. BTW, I haven’t test whether the rewritten functions are necessary or not.

Reference

[1] Lin T Y, RoyChowdhury A, Maji S. Bilinear cnn models for fine-grained visual recognition[C]//Proceedings of the IEEE International Conference on Computer Vision. 2015: 1449-1457. [pdf]

Thanks

Deeply thanks BCNN_keras by tkhs3.

NOTE: Click README and BCNN_keras for details of the original souce codes.