Skip to content

Commit

Permalink
add attribute file
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhankai committed Nov 17, 2018
1 parent dc6c69b commit c960c19
Show file tree
Hide file tree
Showing 8 changed files with 11,858 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#忽略build文件夹
# ignore folder
build
log

# 忽略所有生成的备份文件
# backup files
*.*~

#忽略所有.o 和 .a文件.
#.o and .a files
*.[oa]

*.DS_Store
Expand Down
10 changes: 5 additions & 5 deletions CUB.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def load_data(data_folder, target_size=(224, 224), bounding_box=True):
attribute_name_file = data_folder+'/attributes.txt'
processed_attribute_file = data_folder+'/processed_attributes.txt'
# train test split
split_rf = open(split_file,'rb')
split_rf = open(split_file,'r')
train_test_list = []
train_idx = []
test_idx = []
Expand All @@ -47,7 +47,7 @@ def load_data(data_folder, target_size=(224, 224), bounding_box=True):
i+=1
split_rf.close()
# bb
bb_rf = open(bb_file,'rb')
bb_rf = open(bb_file,'r')
bb_list = []
for line in bb_rf.readlines():
strs = line.strip().split(' ')
Expand All @@ -56,7 +56,7 @@ def load_data(data_folder, target_size=(224, 224), bounding_box=True):
bb_rf.close()
# images
i = 0
images_rf = open(images_file,'rb')
images_rf = open(images_file,'r')
for line in images_rf.readlines():
strs = line.strip().split(' ')
img = image.load_img(data_folder+'/images/'+strs[1])
Expand All @@ -73,7 +73,7 @@ def load_data(data_folder, target_size=(224, 224), bounding_box=True):
print(i,' images load.')
images_rf.close()
# label
label_rf = open(label_file,'rb')
label_rf = open(label_file,'r')
for line in label_rf.readlines():
strs = line.strip().split(' ')
if(train_test_list[int(strs[0])-1]=='1'):
Expand All @@ -87,7 +87,7 @@ def load_data(data_folder, target_size=(224, 224), bounding_box=True):
A_test = A_all[test_idx]
# class attributes
C_A = np.zeros((200,312))
class_attr_rf = open(class_attributes_file,'rb')
class_attr_rf = open(class_attributes_file,'r')
i = 0
for line in class_attr_rf.readlines():
strs = line.strip().split(' ')
Expand Down
13 changes: 7 additions & 6 deletions cub_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
from keras.utils import np_utils
from keras import backend as K
from keras.models import load_model
from keras.utils.visualize_util import plot
#from deep_learning_models.vgg16 import VGG16
#from keras.utils.visualize_util import plot
from keras.applications.vgg16 import VGG16
from keras.applications.resnet50 import ResNet50
import CUB
import scipy.misc
from sklearn import preprocessing
import CUB

# model config
# args
net = sys.argv[1]
data_folder = sys.argv[2]

# model config
flag_test = False
batch_size = 10
nb_epoch = 10
Expand All @@ -44,12 +46,11 @@

# dataset config
dataset = 'CUB'
data_folder = '/home/hankai/data/CUB_200_2011'
nb_classes = 200
nb_attributes = [10, 16, 16, 16, 5, 16, 7, 16, 12, 16, 16, 15, 4, 16, 16, 16, 16, 6, 6, 15, 5, 5, 5, 16, 16, 16, 16, 5]
img_rows, img_cols = 448, 448
L = 14*14
lr_list = [0.001,0.001,0.001,0.001,0.001,0.001,0.0001]
lr_list = [0.001,0.003,0.001,0.001,0.001,0.001,0.001,0.0001]

def init_classification(input_fea_map, dim_channel, nb_class, name=None):
# conv
Expand Down
22 changes: 12 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ Code for paper: Attribute-Aware Attention Model for Fine-grained Representation
![](./fig/a3m.png)

### Usage
Requires: Keras 1.2.1 with Theano backend
Requires: Keras 1.2.1 ("image_data_format": "channels_first")

1. Download CUB-200-2011 dataset [here](http://www.vision.caltech.edu/visipedia/CUB-200-2011.html) and unzip it to `$CUB`; Change `data_dir` in `cub_demo.py` to `$CUB`.
1. Download CUB-200-2011 dataset [here](http://www.vision.caltech.edu/visipedia/CUB-200-2011.html) and unzip it to `$CUB`; Copy file `tools/processed_attributes.txt` in `$CUB`.

2. `sh cub_run.sh`
2. Change `data_dir` in `run.sh` to `$CUB`, run the scprit `sh run.sh` to obtain the result.

Result on CUB dataset
- Result on CUB dataset
![](./fig/result.png)

### Citation

Please use the following bibtex to cite our work:
```
@inproceedings{a3m_2018_kai,
author = {Kai Han and Jianyuan Guo and Chao Zhang and Mingjian Zhu},
title = {Attribute-Aware Attention Model for Fine-grained Representation Learning},
year = {2018},
booktitle = {ACM Multimedia Conference}
}
@inproceedings{han2018attribute,
title={Attribute-Aware Attention Model for Fine-grained Representation Learning},
author={Han, Kai and Guo, Jianyuan and Zhang, Chao and Zhu, Mingjian},
booktitle={2018 ACM Multimedia Conference on Multimedia Conference},
pages={2040--2048},
year={2018},
organization={ACM}
}
```
3 changes: 2 additions & 1 deletion cub_run.sh → run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ datetime=`date -d @$timestamp +"%Y-%m-%d-%H:%M:%S"`
#net=VGG16
#net=InceptionV3
net=ResNet50
data_dir=/home/hankai/data/CUB_200_2011
gpu_id=0
THEANO_FLAGS='device=gpu'$gpu_id',floatX=float32,lib.cnmem=0.6' python cub_demo.py $net | tee "./log/"$net"-"$datetime".log.txt"
THEANO_FLAGS='device=gpu'$gpu_id',floatX=float32,lib.cnmem=0.6' python cub_demo.py $net $data_dir | tee "./log/"$net"-"$datetime".log.txt"


40 changes: 40 additions & 0 deletions tools/attributes_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#coding=utf-8
import re
import numpy as np

# get attribute cluster idx
attribute_name_file = 'attributes.txt'
f1 = open(attribute_name_file, 'rb')
start_idxs = []
last_attr = ''
for line in f1.readlines():
strs = re.split(' |::', line)
if(strs[1]!=last_attr):
start_idxs.append(int(strs[0]))
last_attr = strs[1]
start_idxs.append(int(strs[0])+1)
print(start_idxs)
a = np.array(start_idxs)
nums = a[1:]-a[:-1]+1
print(np.sum(nums))
print(nums.tolist())

# transform binary attribute to clustered attribute
nb_attr = len(start_idxs)-1
A_all = np.zeros((11788,nb_attr))
image_attribute_file = 'attributes/image_attribute_labels.txt'
f2 = open(image_attribute_file,'rb')
for line in f2.readlines():
strs = re.split(' ', line)
img_id = int(strs[0])-1
attr_id = int(strs[1])
is_present = int(strs[2])
if(is_present>0):
for i in range(len(start_idxs)):
if(attr_id<start_idxs[i]):
break
A_all[img_id][i-1] = attr_id-start_idxs[i-1]+1 # 0 mean no attr
print(A_all[1])

new_attr_file = 'processed_attributes.txt'
np.savetxt(new_attr_file,A_all,fmt='%d')
Loading

0 comments on commit c960c19

Please sign in to comment.