Giter VIP home page Giter VIP logo

cbam-tensorflow-slim's Introduction

CBAM-TensorFlow-Slim

This is a Tensorflow implementation of "CBAM: Convolutional Block Attention Module" aiming to be compatible on the TensorFlow-Slim image classification model library. This repository includes the implementation of "SENet-tensorflow-slim".

If you want to use simpler implementation, check the repository "CBAM-TensorFlow" which includes simple tensorflow implementation of ResNext, Inception-V4, and Inception-ResNet-V2 on Cifar10 dataset.

CBAM: Convolutional Block Attention Module

CBAM proposes an architectural unit called "Convolutional Block Attention Module" (CBAM) block to improve representation power by using attention mechanism: focusing on important features and supressing unnecessary ones. This research can be considered as a descendant and an improvement of "Squeeze-and-Excitation Networks".

Diagram of a CBAM_block

Diagram of each attention sub-module

Classification results on ImageNet-1K

Prerequisites

Prepare Data set

You should prepare your own dataset or open dataset (Cifar10, flowers, MNIST, ImageNet). For preparing dataset, you can follow the 'preparing the datasets' part in TF-Slim image models README.

CBAM_block and SE_block Supportive Models

This project is based on TensorFlow-Slim image classification model library. Every image classification model in TensorFlow-Slim can be run the same. And, you can run CBAM_block or SE_block added models in the below list by adding one argument --attention_module=cbam_block or --attention_module=se_block when you train or evaluate a model.

  • Inception V4 + CBAM / + SE
  • Inception-ResNet-v2 + CBAM / + SE
  • ResNet V1 50 + CBAM / + SE
  • ResNet V1 101 + CBAM / + SE
  • ResNet V1 152 + CBAM / + SE
  • ResNet V1 200 + CBAM / + SE
  • ResNet V2 50 + CBAM / + SE
  • ResNet V2 101 + CBAM / + SE
  • ResNet V2 152 + CBAM / + SE
  • ResNet V2 200 + CBAM / + SE

Change Reduction ratio

To change reduction ratio, you have to manually set the ratio on def cbam_block(input_feature, name, ratio=16) method for cbam_block or def se_block(residual, name, ratio=8) method for se_block in CBAM-tensorflow-slim/nets/attention_module.py.

Train a Model

You can find example of training script in CBAM-tensorflow-slim/scripts/.

Train a model with CBAM_block

Below script gives you an example of training a model with CBAM_block.

DATASET_DIR=/DIRECTORY/TO/DATASET
TRAIN_DIR=/DIRECTORY/TO/TRAIN
CUDA_VISIBLE_DEVICES=0 python train_image_classifier.py \
    --train_dir=${TRAIN_DIR} \
    --dataset_name=imagenet \
    --dataset_split_name=train \
    --dataset_dir=${DATASET_DIR} \
    --model_name=resnet_v1_50 \
    --batch_size=100 \
    --attention_module=cbam_block

Train a model with SE_block

Below script gives you an example of training a model with SE_block.

DATASET_DIR=/DIRECTORY/TO/DATASET
TRAIN_DIR=/DIRECTORY/TO/TRAIN
CUDA_VISIBLE_DEVICES=0 python train_image_classifier.py \
    --train_dir=${TRAIN_DIR} \
    --dataset_name=imagenet \
    --dataset_split_name=train \
    --dataset_dir=${DATASET_DIR} \
    --model_name=resnet_v1_50 \
    --batch_size=100 \
    --attention_module=se_block

Train a model without attention module

Below script gives you an example of training a model without attention module.

DATASET_DIR=/DIRECTORY/TO/DATASET
TRAIN_DIR=/DIRECTORY/TO/TRAIN
CUDA_VISIBLE_DEVICES=0 python train_image_classifier.py \
    --train_dir=${TRAIN_DIR} \
    --dataset_name=imagenet \
    --dataset_split_name=train \
    --dataset_dir=${DATASET_DIR} \
    --model_name=resnet_v1_50 \
    --batch_size=100

Evaluate a Model

You can find example of evaluation script in CBAM-tensorflow-slim/scripts/. To keep track of validation accuracy while training, you can use eval_image_classifier_loop.py which evaluate the performance at multiple checkpoints during training. If you want to just evaluate a model once, you can use eval_image_classifier.py.

Evaluate a model with CBAM_block

Below script gives you an example of evaluating a model with CBAM_block during training.

DATASET_DIR=/DIRECTORY/TO/DATASET
CHECKPOINT_FILE=/DIRECTORY/TO/CHECKPOINT
EVAL_DIR=/DIRECTORY/TO/EVAL
CUDA_VISIBLE_DEVICES=0 python eval_image_classifier_loop.py \
    --alsologtostderr \
    --checkpoint_path=${CHECKPOINT_FILE} \
    --dataset_dir=${DATASET_DIR} \
    --eval_dir=${EVAL_DIR} \
    --dataset_name=imagenet \
    --dataset_split_name=validation \
    --model_name=resnet_v1_50 \
    --batch_size=100 \
    --attention_module=cbam_block

Evaluate a model with SE-block

Below script gives you an example of evaluating a model with SE_block during training.

DATASET_DIR=/DIRECTORY/TO/DATASET
CHECKPOINT_FILE=/DIRECTORY/TO/CHECKPOINT
EVAL_DIR=/DIRECTORY/TO/EVAL
CUDA_VISIBLE_DEVICES=0 python eval_image_classifier_loop.py \
    --alsologtostderr \
    --checkpoint_path=${CHECKPOINT_FILE} \
    --dataset_dir=${DATASET_DIR} \
    --eval_dir=${EVAL_DIR} \
    --dataset_name=imagenet \
    --dataset_split_name=validation \
    --model_name=resnet_v1_50 \
    --batch_size=100 \
    --attention_module=se_block

Evaluate a model without attention module

Below script gives you an example of evaluating a model without attention module during training.

DATASET_DIR=/DIRECTORY/TO/DATASET
CHECKPOINT_FILE=/DIRECTORY/TO/CHECKPOINT
EVAL_DIR=/DIRECTORY/TO/EVAL
CUDA_VISIBLE_DEVICES=0 python eval_image_classifier_loop.py \
    --alsologtostderr \
    --checkpoint_path=${CHECKPOINT_FILE} \
    --dataset_dir=${DATASET_DIR} \
    --eval_dir=${EVAL_DIR} \
    --dataset_name=imagenet \
    --dataset_split_name=validation \
    --model_name=resnet_v1_50 \
    --batch_size=100 

Related Works

Reference

Author

Byung Soo Ko / [email protected]

cbam-tensorflow-slim's People

Contributors

kobiso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cbam-tensorflow-slim's Issues

loss during training

Hello!I have a question: should I train my model until the loss becomes the end learning rate(0.0001)? If I stop training my model when the loss is 0.027, then my model will perform badly?

The position to add attention modules

Dear kobiso:

Thanks for your re-implementations of cbam-tensorflow-slim and SENet-tensorflow-slim.

However, I find that the position you add cbam block is different from the paper "CBAM: Convolutional Block Attention Module" for the inception-resnet-v2. The same situation also happens for the inception-resnet-v2 in SENet-tensorflow-slim.

From my point of view, in both papers, i.e., "CBAM: Convolutional Block Attention Module" and "Squeeze-and-Excitation Networks", the attention modules should be placed between the original blocks of the inception-resnet-v2. So for the inception-resnet-v2, code of adding cbam and SENet modules would be as:

net += scaled_up
if activation_fn:
net = activation_fn(net)
if cbam_block:
net=cbam_block(net, 'cbam_block')
if se_block:
net = se_block(net, 'se_block')
which means that the attention modules would be added after "net += scaled_up".

Also in the inception-resnet-v2 of cbam-tensorflow-slim, you add the attention module in the scaled_up branch as " scaled_up= cbam_block(scaled_up, 'cbam_block')" before "net += scaled_up", while in the the inception-resnet-v2 of SENet-tensorflow-slim, you add the attention module in the identity branch as " net = se_block(net, 'se_block')" before "net += scaled_up".

I don't know if you make these modifications because these modifications show better performance in your own re-implementations. Or, do I make mistakes in understanding the way of adding these two attention modules to the inception-resnet-v2?

Thank you again for your hard work.

All the best.

Any pretrained model?

Hello,
thank you for your code.
I am wondering if you can provide the pretrained model of the resnet_v2 with cbam attention model?
Thanks!!!

visualization

Can you share some code related to visualization。thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.