Python影像辨識筆記(三十三):AGAM(Attributes-Guided and Pure-Visual Attention Alignment for Few-Shot Recognition)

Requirements

pip install torch==1.2.0 torchvision==0.4.0
pip install ordered_set
pip install tqdm
pip install pandas
pip install h5py
pip install scipy

Custom CUB attribute script

Train CUB with the custom attribute script

def get_semantic_size(args):    semantic_size_list = []    for semantic_type in args.semantic_type:        if semantic_type == 'class_attributes':
if args.train_data == 'cub':
semantic_size_list.append(156) #改成156
class CUBClassDataset(ClassDataset):
folder = 'cub'
# Google Drive ID from http://www.vision.caltech.edu/visipedia-data/CUB-200-2011/CUB_200_2011.tgz
gdrive_id = '1hbzc_P1FuxMkcabkgn9ZKinBwW683j45'
tgz_filename = 'CUB_200_2011.tgz'
tgz_md5 = '97eceeb196236b17998738112f37df78'
image_folder = 'CUB_200_2011/images'
filename = '{0}_data.hdf5'
filename_labels = '{0}_labels.json'
assets_dir = 'assets'
text_dir = 'text_c10'
attribute_dir = 'attributes'
class_attribute_filename_labels = 'class_attribute_labels_continuous_156.txt' #改成156
image_id_name_filename = 'images.txt'
image_attribute_filename_labels = 'image_attribute_labels_156.txt' #改成156
classes_filename = 'classes.txt'
attributes_dim = 156 #312改成156

How to run

# clone project
git clone https://github.com/bighuang624/AGAM.git
cd AGAM/models/agam_protonet
# download data and run on multiple GPUs with special settings
python train.py --train-data [train_data] --test-data [test_data] --backbone [backbone] --num-shots [num_shots] --train-tasks [train_tasks] --semantic-type [semantic_type] --multi-gpu --download
# Example: run on CUB dataset, Conv-4 backbone, 1 shot, single GPU (First time training with --download to get the dataset)
python train.py --train-data cub --test-data cub --backbone conv4 --num-shots 1 --train-tasks 50000 --semantic-type class_attributes --download
# Example: run on SUN dataset, ResNet-12 backbone, 5 shot, multiple GPUs (First time training with --download to get the dataset)
python train.py --train-data sun --test-data sun --backbone resnet12 --num-shots 5 --train-tasks 40000 --semantic-type image_attributes --multi-gpu --download
# If you have downloaded the dataset, use the command below:# Example: run on CUB dataset, Conv-4 backbone, 1 shot, single GPU
python train.py --train-data cub --test-data cub --backbone conv4 --num-shots 1 --train-tasks 50000 --semantic-type class_attributes
# Example: run on SUN dataset, ResNet-12 backbone, 5 shot, multiple GPUs
python train.py --train-data sun --test-data sun --backbone resnet12 --num-shots 5 --train-tasks 40000 --semantic-type image_attributes --multi-gpu
# 進行無attribute的訓練
python train_wo_attribute.py --train-data cub --test-data cub --backbone conv4 --num-shots 1 --train-tasks 50000 --semantic-type class_attributes

從中斷的model繼續進行訓練

python train.py --train-data cub --test-data cub --backbone conv4 --num-shots 1 --train-tasks 50000 --semantic-type class_attributes --resume --resume-folder checkpoint資料夾路徑# 例如:
python train.py --train-data cub --test-data cub --backbone conv4 --num-shots 1 --train-tasks 50000 --semantic-type class_attributes --resume --resume-folder no_attribute_cub_cub_protonet_agam_conv4_2021-05-27-17-59-38

Data Preparation

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store