Python影像辨識筆記(三十):Zero-Shot Classification — Latent Embedding Feedback and Discriminative Features for Zero-Shot Classification

Yanwei Liu
2 min readMar 6, 2021

介紹

這篇論文是目前Zero-Shot Classification的state-of-the-art。

可以參考:https://paperswithcode.com/task/zero-shot-learninghttps://paperswithcode.com/sota/zero-shot-learning-on-cub-200-2011

在各個不同Dataset上的實驗結果

2021/03/10更新

也可以使用我所建立的conda環境,直接安裝即可:
conda env create -f tfvaegan.yml

使用方式

# 需特別注意,這份程式使用PyTorch 0.3.1版本,且repo裡面提供的安裝方式在我的環境當中會有錯誤產生,因此使用以下的方式來進行安裝及訓練conda update condaconda create --name tfvaegan python=3.6conda activate tfvaegan# 安裝0.3.1的PyTorch
# 參考https://www.ptorch.com/news/145.html
pip install http://download.pytorch.org/whl/cu90/torch-0.3.1-cp36-cp36m-linux_x86_64.whl
pip install h5py sklearn# Clone程式碼
git clone https://github.com/akshitac8/tfvaegan
cd tfvaegan/zero-shot-images/data/
# dataset的檔案來源
wget http://datasets.d2.mpi-inf.mpg.de/xian/cvpr18xian.zip
unzip cvpr18xian.zip && rm cvpr18xian.zip
wget http://datasets.d2.mpi-inf.mpg.de/xian/xlsa17.zip
unzip xlsa17.zip && rm xlsa17.zip
# 上面兩個檔案下載完成解壓縮後,將各別資料夾中data資料夾內的資料夾檔案{cvpr18xian/data/*}和{xlsa17/data/*},移動到tfvaegan/zero-shot-images/data/ 裡面

Zero-Shot Image Classification

cd zero-shot-images# 如果使用多個Terminal的話,請記得進入先前使用conda建立的tfvaegan環境,否則會有訓練錯誤的問題(conda activate tfvaegan)# 依照不同的dataset,使用不同的訓練指令
# 記得修改run_dataset名稱_tfvaegan.py裡面的CUDA_VISIBLE_DEVICES=6,改成CUDA_VISIBLE_DEVICES=0。因為我們沒有那麼多顆的GPU,使用0代表用第1顆GPU
python image-scripts/run_cub_tfvaegan.py
python image_scripts/run_awa_tfvaegan.py
python image_scripts/run_flo_tfvaegan.py
python image_scripts/run_sun_tfvaegan.py

Zero-Shot Action Classification

取得hmdb51和ucf101的dataset

# 來源
The I3D features (concatenated RGB and Flow) for HMDB51 and UCF101 are provided here. Place the unzipped sub-folders under data folder before running the experiments.
cd zero-shot-actions
HMDB51 : python action_scripts/run_hmdb51_tfvaegan.py
UCF101 : python action_scripts/run_ucf101_tfvaegan.py

如何建立Custom Dataset?

建立custom dataset,主要分成res101.mat和att_splits.mat兩種

其中res101.mat保存圖片經過特徵萃取器萃取出的Tensor資料;att_splits.mat則包含label的att, original_att, test_seen, test_unseen, train_val, tain, val......等構成資料集的欄位

res101.mat
視覺化的特徵透過ResNet101在ImageNet的Model進行萃取

https://gist.githubusercontent.com/e96031413/2f4cb8f730e39eafec6cde6323f096ca/raw/4d516c9ac76b822a087e6173299d3d7820734de8/mat_feature_extract_with_pytorch_gpu_resnet101.py

att_splits.mat
稍微複雜一點,我們必須

# 參考資料
# How do i convert a Python Pandas Dataframe to Matlab types?
# 錯誤raise ValueError('Unknown mat file type, version %s, %s' % ret)
保存版本過新造成,使用-v7可存成舊版
1. 先透過pandas先自定義attribute(例如one-hot encoding, label encoding)
2. 將dataframe資料輸出成csv檔案
3. 使用Octave或Matlab讀取CSV檔案檢查資料是否正常
4. 轉存成*.mat檔案
以Octave為例# 透過octave讀取csv檔案再次處理
csvread('fileName.csv')
# 保留前n個row或column
ans(:,2:end) % all but first column
ans(2:end,:) % all but first row
# 將ans變數更名成att# 保存matrix成mat檔案
att = [ 1:3; 4:6; 7:9 ];
save myfile.mat att -v7
這個myfile.mat檔案可作為訓練時載入的attribute檔案

--

--