Giter Site home page Giter Site logo

explainableml / czsl Goto Github PK

View Code? Open in Web Editor NEW
109.0 4.0 27.0 24.39 MB

PyTorch CZSL framework containing GQA, the open-world setting, and the CGE and CompCos methods.

License: GNU General Public License v3.0

Python 93.24% Jupyter Notebook 5.42% Shell 1.34%

czsl's Introduction

Compositional Zero-Shot Learning

This is the official PyTorch code of the CVPR 2021 works Learning Graph Embeddings for Compositional Zero-shot Learning and Open World Compositional Zero-Shot Learning. The code provides the implementation of the methods CGE, CompCos together with other baselines (e.g. SymNet, AoP, TMN, LabelEmbed+,RedWine). It also provides train and test for the Open World CZSL setting and the new C-GQA benchmark.

Important note: the C-GQA dataset has been updated (see this issue) and the code will automatically download the new version. The results of all models for the updated benchmark can be found in the Co-CGE and KG-SP papers.

Check also:

  • Co-CGE and its repo if you are interested in a stronger OW-CZSL model and a faster OW evaluation code.
  • KG-SP and its repo if you are interested in the partial CZSL setting and a simple but effective OW model.

Setup

  1. Clone the repo

  2. We recommend using Anaconda for environment setup. To create the environment and activate it, please run:

    conda env create --file environment.yml
    conda activate czsl
  1. Go to the cloned repo and open a terminal. Download the datasets and embeddings, specifying the desired path (e.g. DATA_ROOT in the example):
    bash ./utils/download_data.sh DATA_ROOT
    mkdir logs

Training

Closed World. To train a model, the command is simply:

    python train.py --config CONFIG_FILE

where CONFIG_FILE is the path to the configuration file of the model. The folder configs contains configuration files for all methods, i.e. CGE in configs/cge, CompCos in configs/compcos, and the other methods in configs/baselines.

To run CGE on MitStates, the command is just:

    python train.py --config configs/cge/mit.yml

On UT-Zappos, the command is:

    python train.py --config configs/cge/utzappos.yml

Open World. To train CompCos (in the open world scenario) on MitStates, run:

    python train.py --config configs/compcos/mit/compcos.yml

To run experiments in the open world setting for a non-open world method, just add --open_world after the command. E.g. for running SymNet in the open world scenario on MitStates, the command is:

    python train.py --config configs/baselines/mit/symnet.yml --open_world

Note: To create a new config, all the available arguments are indicated in flags.py.

Test

Closed World. To test a model, the code is simple:

    python test.py --logpath LOG_DIR

where LOG_DIR is the directory containing the logs of a model.

Open World. To test a model in the open world setting, run:

    python test.py --logpath LOG_DIR --open_world

To test a CompCos model in the open world setting with hard masking, run:

    python test.py --logpath LOG_DIR_COMPCOS --open_world --hard_masking

References

If you use this code, please cite

@inproceedings{naeem2021learning,
  title={Learning Graph Embeddings for Compositional Zero-shot Learning},
  author={Naeem, MF and Xian, Y and Tombari, F and Akata, Zeynep},
  booktitle={34th IEEE Conference on Computer Vision and Pattern Recognition},
  year={2021},
  organization={IEEE}
}

and

@inproceedings{mancini2021open,
  title={Open World Compositional Zero-Shot Learning},
  author={Mancini, M and Naeem, MF and Xian, Y and Akata, Zeynep},
  booktitle={34th IEEE Conference on Computer Vision and Pattern Recognition},
  year={2021},
  organization={IEEE}
}

Note: Some of the scripts are adapted from AttributeasOperators repository. GCN and GCNII implementations are imported from their respective repositories. If you find those parts useful, please consider citing:

@inproceedings{nagarajan2018attributes,
  title={Attributes as operators: factorizing unseen attribute-object compositions},
  author={Nagarajan, Tushar and Grauman, Kristen},
  booktitle={Proceedings of the European Conference on Computer Vision (ECCV)},
  pages={169--185},
  year={2018}
}

czsl's People

Contributors

frankruis avatar mancinimassimiliano 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

czsl's Issues

FileNotFoundError: [Errno 2] No such file or directory

when i run :

python train.py --config configs/compcos/mit/compcos_cw.yml 

in windows,
cannot find the house-design-exterior-ancient-greek-temple-views-portrait-with-characteristics-of-greek-architecture-designs-great-ancient-building-with-characteristics-of-greek-architecture-designs-600x480.jpg
**The image is clearly in the folder, but it cannot be found.**Could it be that the filename is too long?

Traceback (most recent call last):
File "C:\Users\rxx\Desktop\czsl-main\train.py", line 232, in
main()
File "C:\Users\rxx\Desktop\czsl-main\train.py", line 38, in main
trainset = dset.CompositionDataset(
File "C:\Users\rxx\Desktop\czsl-main\data\dataset.py", line 204, in init
self.generate_features(feat_file, model)
File "C:\Users\rxx\Desktop\czsl-main\data\dataset.py", line 404, in generate_features
imgs = list(map(self.loader, files))
File "C:\Users\rxx\Desktop\czsl-main\data\dataset.py", line 25, in call
img = Image.open(ospj(self.root_dir,img)).convert('RGB') #We don't want alpha
File "C:\Users\rxx.conda\envs\test\lib\site-packages\PIL\Image.py", line 3218, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'DataBase\mit-states\images\ancient_building\house-design-exterior-ancient-greek-temple-views-portrait-with-characteristics-of-greek-architecture-designs-great-ancient-building-with-characteristics-of-greek-architecture-designs-600x480.jpg'

Intuition of Eq. (8) in Open World Compositional Zero-Shot Learning

Really appreciate for your work.

I have a question about Eq_8.

  1. For seen pairs, we just use cos score
  2. For unseen pairs, we need to adjust:
    2.1 Feasible: fea_score will be closer to 1, then p(c) smaller, because minus fea_score
    2.2 Unfeabsiel: ea_score will be closer to -1, then p(c) bigger

It should add fea_score instead of minus?

Did I miss something.

Thank you.

Code for image retrieval

Hi there,
I see that the test code predicts the attr and obj for the given image which is one of the task, But, there is a second task of image retrieval. Given an object and attribute category, find the corresponding retrieval image. I was wondering where can I find the code for image retrieval?
Many thanks!!

Question about train_forward_bce function in ManifoldModel

Hi,

Thanks for the high quality open-resourced codebase.

I suspect that the Line 59 of czsl/models/manifold_methods.py should be

composed_clf = self.compose(sampled_attrs, sampled_objs)

instead of the current

composed_clf = self.compose(attrs, objs)

As I am a beginner in the CZSL field, I apologize for any misunderstandings that may exist. Looking forward to any response.

Random code crashes with multiple num_workers.

I get following error that is completely random. Doesn't make sense. num_workers=0 works just fine.

File "/opt/conda/lib/python3.8/multiprocessing/queues.py", line 108, in get raise Empty _queue.Empty The above exception was the direct cause of the following exception: Traceback (most recent call last): File "train.py", line 327, in <module> main() File "train.py", line 129, in main train(epoch, image_extractor, model, trainloader, optimizer, writer) File "train.py", line 159, in train_normal for idx, data in tqdm(enumerate(trainloader), total=len(trainloader), desc = 'Training'): File "/opt/conda/lib/python3.8/site-packages/tqdm/std.py", line 1166, in __iter__ for obj in iterable: File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 521, in __next__ data = self._next_data() File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1186, in _next_data idx, data = self._get_data() File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1152, in _get_data success, data = self._try_get_data() File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1003, in _try_get_data raise RuntimeError('DataLoader worker (pid(s) {}) exited unexpectedly'.format(pids_str)) from e RuntimeError: DataLoader worker (pid(s) 692128) exited unexpectedly

FileNotFoundError: Caught FileNotFoundError in DataLoader worker process 0.

when I run the vesicular_gru.py here is the error:

`/home/kumar/anaconda3/envs/pytorch/bin/python /home/kumar/SKKU/khanhlee/vesicular-gru/vesicular-gru-master/vesicular_gru.py
positive: 1771
negative: 6058
Length Train Dataset: 7829

positive: 443
negative: 1515
Length Validation Dataset: 1958

/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/nn/modules/rnn.py:62: UserWarning: dropout option adds dropout after all but last recurrent layer, so non-zero dropout expects num_layers greater than 1, but got dropout=0.01 and num_layers=1
warnings.warn("dropout option adds dropout after all but last "
Traceback (most recent call last):
File "/home/kumar/SKKU/khanhlee/vesicular-gru/vesicular-gru-master/vesicular_gru.py", line 457, in
train()
File "/home/kumar/SKKU/khanhlee/vesicular-gru/vesicular-gru-master/vesicular_gru.py", line 406, in train
epoch_loss_train_avg = train_one_epoch(learning_rate)
File "/home/kumar/SKKU/khanhlee/vesicular-gru/vesicular-gru-master/vesicular_gru.py", line 182, in train_one_epoch
for i, data in enumerate(train_loader, 0):
File "/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 521, in next
data = self._next_data()
File "/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 1203, in _next_data
return self._process_data(data)
File "/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 1229, in _process_data
data.reraise()
File "/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/_utils.py", line 434, in reraise
raise exception
FileNotFoundError: Caught FileNotFoundError in DataLoader worker process 1.
Original Traceback (most recent call last):
File "/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
data = fetcher.fetch(index)
File "/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/kumar/anaconda3/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 49, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/kumar/SKKU/khanhlee/vesicular-gru/vesicular-gru-master/vesicular_gru.py", line 74, in getitem
lines = load_text_file(self.lst_path[index])
File "/home/kumar/SKKU/khanhlee/vesicular-gru/vesicular-gru-master/vesicular_gru.py", line 43, in load_text_file
with open(file_text) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'data/pssm/pssm/membrane/cv/P33450.pssm'

Process finished with exit code 1
`
I have no idea how to fix this error. p33450.pssm is really an existing file in the directory: 'data/pssm/pssm/membrane/cv/P33450.pssm'. How can I fix that error

Role of closed_mask in evaluator vs dataset pairs

I find that the CGE and CompCos predict in all pairs (train pairs + val pairs + test pairs) in test time, but some pairs are masked for evaluation (cf. closed_mask in line 247-252, line 300-306, models/common.py). Is there any special meaning in it?

Code for generating adjacency matrix.

Hi! Thanks for your excellent work.
I tried to generate adjacency matrix for the graph model according to your paper. But I found there are some differences between my version and your given. Would you mind sharing the code for generating adjacency matrix ?
Thanks a lot!

Questions about datasets

What do the numbers in the names of the images in the dataset mean?for example: 1-2-orange-shirt.jpg

What does the cosine_scale mean?

Why multiply a self.scale in front of the predicted probability? Because before CE loss the probability will pass a softmax, i think the scale is meaningless, why is the result so much worse when I set the scale equal to 1?

Question about adj matrix

Could you tell me what are the first few rows of the adj matrix, attributes or objects? And what's the order?

Closed world seen acc of MIT-States in the paper is lower than what I have reimplemented

Dear author,
It is a great pleasure of reading your work. Open world compositional zero shot learning is surely an interesting topic worth to be further studied. To make myself more into the algorithm in this paper, I ran the codes several times. However I cannot get the reported results where seen acc was always 10%-20% higher than unseen acc. I found that the codes in evaluation section ran slowly somehow so I reimplemented all the evaluation codes. I used hyper-parameters precisely similar with that in the paper and freezed feature extractor ResNet18 pre-trained in ImageNet. When I ran the algorithm CompCos in closed world setting using MIT-STATES, I got 42.78% seen acc, 28.53% unseen acc in val set and 54.63% seen acc, 25.37% unseen acc in test set. 25.37% unseen acc in test set is the closest to the 24.6% unseen acc reported in the paper. Over-fitting happens both in val and test set. I wonder why seen acc in the paper of 25.3% is very close to unseen acc of 24.6% with only 0.7% difference. What is the test setting and how can I tune the results?
Thank you!
Great love for you

download_data.sh

This line“mv data/glove/* glove/”in download_data.sh,error,There's no glove under the data file

Cuda Running out of memory during evaluate_predictions

I am trying to replicate the results of Compcos using Mit-States dataset & i am getting the following error:

File "train.py", line 228, in
main()
File "train.py", line 108, in main
test(epoch, image_extractor, model, testloader, evaluator_val, writer, args, logpath)
File "train.py", line 193, in test
stats = evaluator.evaluate_predictions(results, all_attr_gt, all_obj_gt, all_pair_gt, all_pred_dict, topk=args.topk)
File "/nfs4/krissna/OWCZL/czsl-main/czsl-main/models/common.py", line 489, in evaluate_predictions
results = self.score_fast_model(scores, obj_truth, bias = bias, topk = topk)
File "/nfs4/krissna/OWCZL/czsl-main/czsl-main/models/common.py", line 363, in score_fast_model
scores[~mask] += bias # Add bias to test pairs
RuntimeError: CUDA out of memory. Tried to allocate 4.18 GiB (GPU 0; 10.92 GiB total capacity; 4.68 GiB already allocated; 2.44 GiB free; 7.85 GiB reserved in total by PyTorch)

(Currently using Cuda10.2 10Gis)
Please suggest some solution for the above error.
Thanks

Use of cosine similarity in CompCos

Hi,
Thank you for sharing your great work. I have some questions regarding the the paper "Open World Compositional Zero-Shot Learning", which introduces Compositional Cosine Logits (CompCos).

It explains that the image features and compositions are projected into a shared semantic space. The objective function is a cross entropy loss, where the logits of classification layer are replaced by the cosine similarity between the image features and compositional embeddings in the shared embedding space. However, in the code, the projected image features are just multiplied with the projected compositional embeddings, and cross entropy is applied directly.

pair_pred = torch.matmul(img_feats_normed, pair_embed)
loss_cos = F.cross_entropy(self.scale * pair_pred, pairs)

pair_pred = torch.matmul(img_feats_normed, pair_embed)

Is there something that I have misunderstood?
It would be great if you could point out where the cosine similarity is applied in the loss function. Thank you.

C-GQA dataset validation split stats on Co-CGE: seen and unseen switched

While consolidating and deduplicating dataset labels, I discovered some numerical issues. The problem arises in the Validation column of the statistical data for C-GQA in Table 1 of the Co-CGE paper. I would like to request the authors to confirm whether the positions of sp and up, i.e., seen and unseen, are mistakenly switched in this column.

Questions about the reported HM number (60.5) of CGE on UT-Zap50K?

Hi,

Thanks for the impressive paper and high quality open-resourced codebase.

I ran an experiment on zappos with this codebase (w/o any editing) and found that there is a big gap between the testset HM number(47.11) of CGE to the reported one(60.5) in the paper. The following is the eval log on test set

image

Furthermore, from this log, although the test auc number is close to the reported one(33.5), but the best unseen(66.05) also has a big gap to the reported one(71.5).

I am a little bit confused about these number gaps and could you please be kind to give some explainations. Thanks a lot.

FileNotFoundError: Caught FileNotFoundError in DataLoader worker process 0.

when i run the train.py with mit.yml ,here is the error:
Traceback (most recent call last):
File "train.py", line 233, in
main()
File "train.py", line 100, in main
train(epoch, image_extractor, model, trainloader, optimizer, writer)
File "train.py", line 122, in train_normal
for idx, data in tqdm(enumerate(trainloader), total=len(trainloader), desc = 'Training'):
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/tqdm/std.py", line 1166, in iter
for obj in iterable:
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 435, in next
data = self._next_data()
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1085, in _next_data
return self._process_data(data)
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1111, in _process_data
data.reraise()
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/torch/_utils.py", line 428, in reraise
raise self.exc_type(msg)
FileNotFoundError: Caught FileNotFoundError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
data = fetcher.fetch(index)
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/idea/zzy/czsl/data/dataset.py", line 405, in getitem
img = self.loader(image)
File "/home/idea/zzy/czsl/data/dataset.py", line 25, in call
img = Image.open(ospj(self.root_dir,img)).convert('RGB') #We don't want alpha
File "/home/idea/anaconda3/envs/czsl/lib/python3.8/site-packages/PIL/Image.py", line 2904, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'DATA_ROOT/mit-states/images/ripe_coffee/IMG_3474.jpg'

but when I run with the utzappos dataset, there are no errors!
I have not idea how to fix this error. IMG_3474.jpg is really an existing file in "DATA_ROOT/mit-states/images/ripe_coffee". How can I fix that error

Weird results on CGE@CGQA

Hi Authors,

Thank you for your great work! I tried to reproduce the results of CEG on the CGQA dataset by running "python train.py --config configs/cge/cgqa.yaml", however, the auc on validation after running 1k+ epochs is still very low (~3e-5, far from that reported in the paper.). I just cloned the repo and set the dataset as instructed.
May I know if there is anything that I should do to successfully reproduce the results?

logs.csv
Attached is the log

Pre-trained model

Hi,
Thank you for this code. I want to ask do you have pre-trained models? As training is very time consuming so if I want to test the model I have to train it first. So if you have pre-trained model that it would be very helpful. Thank you.

Eq. (8) in Open World Compositional Zero-Shot Learning

Hi,
I noticed that there used the cosine similarity minus feasibility score in Eq. 8 of your paper, while it seems that the cosine similarity plus the feasibility score in line 236 of compcos.py.
I don't know if I'm wrong.
Thank you for your wonderful work.

problem about reproduced results of LE+

very admirable work! But there is some confusing problems.
In the process of reproducing some methods such as Le+[1] based on your open source code, the results obtained are better than the results of Table 2 in article [2]. Is the published code somehow different from the setup in article [2]? Could you give some explanation?

[1] From redwine to red tomato: Composition with context
[2] Learning Graph Embeddings for Compositional Zero-shot Learning

CGQA data set is not available

Hi, Thank you for your work and dataset. I am trying to run the download_data.sh file. It downloads all the data sets except the cgqa. Till yesterday I can successfully download the cgqa dataset also but now it is has some issue. Can you please check it. I am getting the following error message:

Archive:  cgqa.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of cgqa.zip or
        cgqa.zip.zip, and cannot find cgqa.zip.ZIP, period.

I downloaded the cgqa.zip from the data that I downloaded through download_data.sh and found that it is not a valid file. And when I tried to download the dataset from following url it is saying that "The Connection has timed out".
https://s3.mlcloud.uni-tuebingen.de/czsl/cgqa.zip

use C-GQA datasets will make cuda memory be used up quickly

I found that when I use C-GQA datasets, I will get this problem "RuntimeError: CUDA out of memory" . The Navida gpu momery will be used up even though I use RTX 4090 (24GB) or V-100(32GB) . However , the problem won't happen when I use mit-states and ut-zappos datasets. I don't know if any processed steps exist before using C-GQA datasets。I can't solve it, Please help me.

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.