Giter Site home page Giter Site logo

im2recipe-pytorch's Introduction

im2recipe: Learning Cross-modal Embeddings for Cooking Recipes and Food Images

This repository contains the code to train and evaluate models from the paper:
Learning Cross-modal Embeddings for Cooking Recipes and Food Images

Important note: In this repository the Skip-instructions has not been reimplemented in Pytorch, instead needed features are provided to train, validate and test the tri_joint model.

Clone it using:

git clone --recursive https://github.com/torralba-lab/im2recipe-Pytorch.git

If you find this code useful, please consider citing:

@article{marin2019learning,
  title = {Recipe1M+: A Dataset for Learning Cross-Modal Embeddings for Cooking Recipes and Food Images},
  author = {Marin, Javier and Biswas, Aritro and Ofli, Ferda and Hynes, Nicholas and 
  Salvador, Amaia and Aytar, Yusuf and Weber, Ingmar and Torralba, Antonio},
  journal = {{IEEE} Trans. Pattern Anal. Mach. Intell.},
  year = {2019}
}

@inproceedings{salvador2017learning,
  title={Learning Cross-modal Embeddings for Cooking Recipes and Food Images},
  author={Salvador, Amaia and Hynes, Nicholas and Aytar, Yusuf and Marin, Javier and 
          Ofli, Ferda and Weber, Ingmar and Torralba, Antonio},
  booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
  year={2017}
}

Contents

  1. Installation
  2. Recipe1M Dataset
  3. Vision models
  4. Out-of-the-box training
  5. Prepare training data
  6. Training
  7. Testing
  8. Pretrained model
  9. Recipes with nutritional info
  10. Contact

Installation

docker build -t im2recipe .
docker run -it im2recipe

You may use a volume to give snapshots, data, etc to the docker container.

If you are not using Docker, we do recommend to create a new environment with Python 3.7. Right after it, run pip install --upgrade cython and then install the dependencies with pip install -r requirements.txt. Notice that this will install the latest PyTorch version available. Once you finish, you will need to install torchwordemb. In order to do that (or at least the way we found it worked for us), we downloaded and installed it via python setup.py install. In case you get an error related to return {vocab, dest};, you just need to change the original code to return VocabAndTensor(vocab, dest);, and run python setup.py install again.

Recipe1M and Recipe1M+ Datasets

In order to get access to the dataset, please fill the following form here.

Vision models

This current version of the code uses a pre-trained ResNet-50.

Out-of-the-box training

To train the model, you will need to create following files:

  • data/train_lmdb: LMDB (training) containing skip-instructions vectors, ingredient ids and categories.
  • data/train_keys: pickle (training) file containing skip-instructions vectors, ingredient ids and categories.
  • data/val_lmdb: LMDB (validation) containing skip-instructions vectors, ingredient ids and categories.
  • data/val_keys: pickle (validation) file containing skip-instructions vectors, ingredient ids and categories.
  • data/test_lmdb: LMDB (testing) containing skip-instructions vectors, ingredient ids and categories.
  • data/test_keys: pickle (testing) file containing skip-instructions vectors, ingredient ids and categories.
  • data/text/vocab.txt: file containing all the vocabulary found within the recipes.

And download the following ones:

  • data/text/vocab.bin: ingredient Word2Vec vocabulary. Used during training to select word2vec vectors given ingredient ids.
  • data/food101_classes_renamed.txt: Food101 classes used to create the bigrams.
  • data/encs_train_1024.t7: Skip-instructions train partition.
  • data/encs_val_1024.t7: Skip-instructions val partition.
  • data/encs_test_1024.t7: Skip-instructions test partition.
  • data/recipe1M/layer2+.json: Recipe1M+ layer2.
  • data/images/Recipe1M+_{a..f}.tar: 6 Tar files containing part of the images available in Recipe1M+ (~210Gb each).
  • data/images/Recipe1M+_{0..9}.tar: 10 Tar files containing part of the images available in Recipe1M+ (~210Gb each).

To download these files, you first need to complete the following form. After submission, we will share the necessary download links via email. Access to the dataset is granted only for research purposes to universities and research institutions. Original Recipe1M LMDBs and pickle files can be found in train.tar, val.tar and test.tar.

It is worth mentioning that the code is expecting images to be located in a four-level folder structure, e.g. image named 0fa8309c13.jpg can be found in ./data/images/0/f/a/8/0fa8309c13.jpg. Each one of the Tar files contains the first folder level, 16 in total. If you do not have enough space after downloading the Tar files, you can try to mount them locally and access them. We did use ratarmount in our latest test experiments. In order to properly access the images with ratarmount, we temporarily changed our code. We basically tried up to three times to load an image within our default_loader.

Prepare training data

We also provide the steps to format and prepare Recipe1M/Recipe1M+ data for training the trijoint model. We hope these instructions will allow others to train similar models with other data sources as well.

Choosing semantic categories

We provide the script we used to extract semantic categories from bigrams in recipe titles:

  • Run python bigrams --crtbgrs. This will save to disk all bigrams in the corpus of all recipe titles in the training set, sorted by frequency. Note that you will need to create first vocab.txt running python get_vocab.py ../data/vocab.bin within ./scripts/.
  • Running the same script again with --nocrtbgrs will create class labels from those bigrams adding food101 categories.

These steps will create a file called classes1M.pkl in ./data/ that will be used later to create the LMDB file including categories.

Word2Vec

Training word2vec with recipe data:

  • Run python tokenize_instructions.py train to create a single file with all training recipe text.
  • Run the same python tokenize_instructions.py to generate the same file with data for all partitions (needed for skip-thoughts later).
  • Download and compile word2vec
  • Train with:
./word2vec -hs 1 -negative 0 -window 10 -cbow 0 -iter 10 -size 300 -binary 1 -min-count 10 -threads 20 -train tokenized_instructions_train.txt -output vocab.bin
  • Run python get_vocab.py vocab.bin to extract dictionary entries from the w2v binary file. This script will save vocab.txt, which will be used to create the dataset later.
  • Move vocab.bin and vocab.txt to ./data/text/.

Skip-instructions (Torch)

In this repository the Skip-instructions is not implemented in Pytorch, instead we provide the necessary files to train, validate and test tri_joint model.

Creating LMDB file

Navigate back to ./. Run the following from ./scripts:

python mk_dataset.py 
--vocab /path/to/w2v/vocab.txt 
--sthdir /path/to/skip-instr_files/

Notice, that layer2 within ./data/recipe1M/layer2.json will need to be replaced by layer2+.json in order to create our extended Recipe1M+ dataset.

Training

  • Train the model with:
python train.py 
--img_path /path/to/images/ 
--data_path /path/to/lmdbs/ 
--ingrW2V /path/to/w2v/vocab.bin
--snapshots snapshots/
--valfreq 10

Note: Again, this can be run without arguments with default parameters if files are in the default location.

  • You can set -batchSize to ~160. This is the default config, which will make the model converge in less than 3 days. Pytorch version requires less memory. You should be able to train the model using two TITAN X 12gb with same batch size. In this version we are using LMDBs to load the instructions and ingredients instead of a single HDF5 file.

Testing

  • Extract features from test set python test.py --model_path=snapshots/model*.tar. They will be saved in results.
  • After feature extraction, compute MedR and recall scores with python scripts/rank.py --path_results=results.

Pretrained model

Our best model trained with Recipe1M+ (journal extension) can be downloaded here.

You can test it with:

python test.py --model_path=snapshots/model_e500_v-8.950.pth.tar

Our best model trained with Recipe1M (CVPR paper) can be downloaded here.

Recipes with nutritional info

We also provide a subset of recipes with nutritional information. Below you can see an example:

{'fsa_lights_per100g': {'fat': 'green',
  'salt': 'green',
  'saturates': 'green',
  'sugars': 'orange'},
 'id': '000095fc1d',
 'ingredients': [{'text': 'yogurt, greek, plain, nonfat'},
  {'text': 'strawberries, raw'},
  {'text': 'cereals ready-to-eat, granola, homemade'}],
 'instructions': [{'text': 'Layer all ingredients in a serving dish.'}],
 'nutr_per_ingredient': [{'fat': 0.8845044000000001,
   'nrg': 133.80964,
   'pro': 23.110512399999998,
   'sat': 0.26535132,
   'sod': 81.64656,
   'sug': 7.348190400000001},
  {'fat': 0.46,
   'nrg': 49.0,
   'pro': 1.02,
   'sat': 0.023,
   'sod': 2.0,
   'sug': 7.43},
  {'fat': 7.415,
   'nrg': 149.25,
   'pro': 4.17,
   'sat': 1.207,
   'sod': 8.0,
   'sug': 6.04}],
 'nutr_values_per100g': {'energy': 81.12946131894766,
  'fat': 2.140139263515891,
  'protein': 6.914436593565536,
  'salt': 0.05597816738985967,
  'saturates': 0.36534716195613937,
  'sugars': 5.08634103436144},
 'partition': 'train',
 'quantity': [{'text': '8'}, {'text': '1'}, {'text': '1/4'}],
 'title': 'Yogurt Parfaits',
 'unit': [{'text': 'ounce'}, {'text': 'cup'}, {'text': 'cup'}],
 'url': 'http://tastykitchen.com/recipes/breakfastbrunch/yogurt-parfaits/',
 'weight_per_ingr': [226.796, 152.0, 30.5]}

Note that these recipes include the matched ingredients from USDA instead of the original ones. There are 35,867 recipes for training, 7,687 for validation and 7,681 for testing. In order to obtain the grams of salt, we multiplied the sodium by 2.5 and divided it by 1000. Total weight per ingredient, fat, proteins/pro, salt, saturates/sat and sugars/sug are expressed in grams. Sodium/sod is expressed in mg and energy/nrg in kcal. FSA traffic lights are also included per 100g.

Contact

For any questions or suggestions you can use the issues section or reach us at [email protected].

im2recipe-pytorch's People

Contributors

dependabot[bot] avatar jmarintur avatar jsiloto avatar k0kubun avatar macrochip avatar nhynes 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

im2recipe-pytorch's Issues

Question about the CCA protocol

Hi, guys. I am also using CCA algorithm to perform dimension reduction, i.e. using the api from sklearn. However, this operation may require more than a whole day to achieve that goal, from 2048-dims to 1048-dims. So I wonder if there is another solution to accelerate the procedure of this operation?

mk_dataset.py hasnot taken the images into lmdb

erialized_sample = pickle.dumps( {'ingrs':ingr_vec, 'intrs':st_vecs[partition]['encs'][beg:end],
    'classes':class_dict[entry['id']]+1, 'imgs':imgs[:maxNumImgs]} ) 

with env[partition].begin(write=True) as txn:
    txn.put('{}'.format(entry['id']), serialized_sample)

IndexError on attempt to run pertained model

Running

python test.py --model_path=snapshots/model_e500_v-8.950.pth.tar

results in

File "test.py", line 173, in
main()
File "test.py", line 34, in main
model = im2recipe()
File "/home/sws/im2recipe-Pytorch/trijoint.py", line 120, in init
self.ingRNN_ = ingRNN()
File "/home/sws/im2recipe-Pytorch/trijoint.py", line 65, in init
self.embs = nn.Embedding(vec.size(0), opts.ingrW2VDim, padding_idx=0) # not sure about the padding idx
File "/home/sws/.local/lib/python3.7/site-packages/torch/nn/modules/sparse.py", line 98, in init
self.reset_parameters()
File "/home/sws/.local/lib/python3.7/site-packages/torch/nn/modules/sparse.py", line 109, in reset_parameters
self.weight[self.padding_idx].fill_(0)
IndexError: index 0 is out of bounds for dimension 0 with size 0

I am in trouble to test it with this error.
What can you advise to do? Fix padding ?

Visualization

In the lua version, you have a section to see the visualization of the trained model, how this will be achieved with Pytorch once the model is trained?

Error installing torchwordemb

I'm trying to test the pretrained model but got the following error:

Traceback (most recent call last): File "test.py", line 13, in <module> from trijoint import im2recipe File "/project/recipe1m/im2recipe-Pytorch/trijoint.py", line 9, in <module> import torchwordemb ModuleNotFoundError: No module named 'torchwordemb'

but then I get the error when doing pip install torchwordemb

/project/conda_env/recipe1m/lib/python3.7/site-packages/torch/include/ATen/core/dispatch/Dispatcher.h: In member function โ€˜Return c10::Dispatcher::doCallUnboxed(const c10::Dispatc hTable&, const c10::LeftRight<ska::flat_hash_map<c10::TensorTypeId, c10::KernelFunction> >&, Args ...) const [with Return = bool; Args = {}]โ€™: /project/conda_env/recipe1m/lib/python3.7/site-packages/torch/include/ATen/core/dispatch/Dispatcher.h:191:1: warning: control reaches end of non-void function [-Wreturn-type] } ^
error: command 'gcc' failed with exit status 1

Has anyone had success running test.py and/or installing torchwordemb? 

im2recipe-Pytorch issue

I wanted to try to train your model using a new dataset. Following your instructions, I would like to launch the script "tokenize_instructions.py", which however requires the "det_ingrs.json" file. I can't figure out how you got that file, through a particular script or some other way? and if so, how can I get it for the new recipe dataset I would like to use.

The paper talks about a model " The initial ingredient name extraction task is solved by a bi-directional LSTM that performs logistic regression on each word in the ingredient text", but I couldn't find the code associated with this model, which I think could be the one used to then obtain the "det_ingrs.json" file, but i'm not sure.

If you could help me with these doubts I would be very grateful.

ONNX Version?

First, thank you for this model and this research. It's been a big help in my process for learning ML and PyTorch.

I was wondering if it would be possible to provide your pre-trained model in an ONNX format? My end goal here would be to use it on iOS and Android, and both platforms have hardware accelerated inference engines (CoreML/Tensorflow Lite respectively.) My initial reading has lead me to believe it's more compact and generally faster to use the native solutions rather PyTorch itself on mobile. Thoughts? It appears PyTorch can output to the ONNX format, but I'm not 100% sure I understand enough of the inputs and outputs of this model to do it correctly myself.

Problem of running test.py

It seems test.py will not extract recipe embedding features and image embedding features. The code is running for more than 4 hours, but nothing has been saved (still running). I have checked the test.py, it seems that no data fed into the model. I think there may be something wrong with the test function.
%----------------------------------------------------------------------
for i, (input, target) in enumerate(test_loader):
input_var = list()
for j in range(len(input)):
v = torch.autograd.Variable(input[j], volatile=True)
target_var = list()
for j in range(len(target)-2): # we do not consider the last two objects of the list
target[j] = target[j]
v = torch.autograd.Variable(target[j], volatile=True)
target_var.append(v.cuda() if not opts.no_cuda else v)

    # compute output

output = model(input_var[0],input_var[1], input_var[2], input_var[3], input_var[4])
%---------------------------------------------------------------------------------------------------
From the code, input_var is not updated and keeps empty. Could you help me address this?

I got Cuda Error when I run test.py ,And My soft environment is ubuntu16.04 python3.7 cpu

/home/pc/anaconda3/envs/py3.7/bin/python /home/pc/work/yq_work/im2recipe-Pytorch/test.py
test begin!
Traceback (most recent call last):
File "/home/pc/work/yq_work/im2recipe-Pytorch/test.py", line 176, in
main()
File "/home/pc/work/yq_work/im2recipe-Pytorch/test.py", line 36, in main
model = im2recipe()
File "/home/pc/work/yq_work/im2recipe-Pytorch/trijoint.py", line 102, in init
resnet = models.resnet50(pretrained=True)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torchvision/models/resnet.py", line 261, in resnet50
**kwargs)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torchvision/models/resnet.py", line 223, in _resnet
progress=progress)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torch/hub.py", line 506, in load_state_dict_from_url
return torch.load(cached_file, map_location=map_location)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torch/serialization.py", line 529, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torch/serialization.py", line 702, in _legacy_load
result = unpickler.load()
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torch/serialization.py", line 665, in persistent_load
deserialized_objects[root_key] = restore_location(obj, location)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torch/serialization.py", line 156, in default_restore_location
result = fn(storage, location)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torch/serialization.py", line 132, in _cuda_deserialize
device = validate_cuda_device(location)
File "/home/pc/anaconda3/envs/py3.7/lib/python3.7/site-packages/torch/serialization.py", line 116, in validate_cuda_device
raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

Process finished with exit code 1

Licence for using the Code (incl. model)

Your project is of great interest to me and i am very excited to try Your models!

You list Your code under the MIT licence and link Your parameter set (the model) in the read.me.

To actually being able to use Your model/code for inference, one would also need the vocabulary: vocab.bin .

Am i correct in the assumption, that You do not allow inference of Your model in commercial use (as part of a back-end service) ?
Otherwise, leave me a comment (which of course would make me very happy).

Recipe Visulization and new Prediction

I am using Ubuntu 18.04,Pytorch 0.4.1, Py27, no cuda
In test.py, i have deleted all line which format is ".cuda()" since my local computer is not suit the GPU way. Moreover , i haved modified the batch_size in agr.py = 1 instead of 160.
Then, i meet the problem in :
File "test.py", line 107, in test
output = model(input_var[0],input_var[1], input_var[2], input_var[3], input_var[4])
IndexError: list index out of range

This the whole process with the command : python test.py --model_path=snapshots/model_e500_v-8.950.pth.tar
=> loading checkpoint 'snapshots/model_e500_v-8.950.pth.tar'
=> loaded checkpoint 'snapshots/model_e500_v-8.950.pth.tar' (epoch 500)
/home/minh/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torchvision/transforms/transforms.py:188: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
"please use transforms.Resize instead.")
Test loader prepared.
Exception OSError: OSError(104, 'Connection reset by peer') in <bound method _DataLoaderIter.del of <torch.utils.data.dataloader._DataLoaderIter object at 0x7f1a29958810>> ignored
Traceback (most recent call last):
File "test.py", line 179, in
main()
File "test.py", line 81, in main
test(test_loader, model, criterion)
File "test.py", line 107, in test
output = model(input_var[0],input_var[1], input_var[2], input_var[3], input_var[4])
IndexError: list index out of range

Please give me some advices , Thanks anyway !

vocab size

I have tried the testing code with the pre-trained model, however, it reports the error "runtimeError: While copying the parameter named ingRNN_.embs.weight, whose dimensions in the model are torch.Size([30566, 300]) and whose dimensions in the checkpoint are torch.Size([30167, 300])."

test.py didn't run

I ran this command, then the below error occurred.
I suppose a reason why this error occurred,
input_var array is initialized, but not appended value in test function.
-- Command

python test.py --model_path=snapshots/model_e220_v-4.700.pth.tar

-- Error

Traceback (most recent call last):
  File "test.py", line 176, in <module>
    main()
  File "test.py", line 80, in main
...
    test(test_loader, model, criterion)
  File "test.py", line 104, in test
    output = model(input_var[0],input_var[1], input_var[2], input_var[3], input_var[4])

DB Download link not working

Dear authors,

I am trying to download your dataset "Recipe1M+ dataset" from the provided link:
http://im2recipe.csail.mit.edu/dataset/login/

When I tried to log in it mentioned the This account is inactive.

I have not received any email for confirming the registration.

I tried to create a new user with different emails but some errors raised, and now it says the account has been taken.

I would really appreciate it if you can kindly follow up and send me the dataset link.

Best,
Allen

test.py on existing model gives same results as random ranking

So, I initially got the same error as #12 and solved it using the solution there and also went through all the other issues to solve existing problems. After this, I run test.py and I get MedR and recall results comparable to random ranking results mentioned in the CVPR paper. So, maybe #12 is not solved after all. Also, maybe the model that I am loading (~220 epochs one) has random weights, but that is unlikely.

Another reason could be that I had to replace volatile=True in torch.autograd.Variable with torch.no_grad() wrapper. These are the changes I made to the code:

for i, (input, target) in enumerate(test_loader):
        input_var = list() 
        for j in range(len(input)):
            with torch.no_grad():
	    	v = torch.autograd.Variable(input[j])
	    input_var.append(v.cuda() if not opts.no_cuda else v)
        target_var = list()
        for j in range(len(target)-2): # we do not consider the last two objects of the list
            with torch.no_grad():
            	v = torch.autograd.Variable(target[j])
            target_var.append(v.cuda() if not opts.no_cuda else v)

If the @nhynes or anybody who was able to recreate the results could chip in, it would be extremely helpful. I am using a batch size of 60 for testing since that is what my GPU can handle.

cos_loss ~ 0.9
img_loss ~ 9.2
rec_loss ~ 2.2

expected scalar type Float but found Long

I get the following error when training the model

$ python train.py --batch_size 80                                                                                                                                                    [900/1419]
There are 2 parameter groups
Initial base params lr: 0.000100
Initial vision params lr: 0.000000
/home/foodvision/anaconda3/envs/py27/lib/python2.7/site-packages/torchvision-0.2.1-py2.7.egg/torchvision/transforms/transforms.py:188: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize inst$
ad.
Training loader prepared.
Validation loader prepared.
Traceback (most recent call last):
  File "train.py", line 372, in <module>
    main()
  File "train.py", line 115, in main
    train(train_loader, model, criterion, optimizer, epoch)
  File "train.py", line 181, in train
    cos_loss = criterion[0](output[0], output[1], target_var[0])
  File "/home/foodvision/anaconda3/envs/py27/lib/python2.7/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/foodvision/anaconda3/envs/py27/lib/python2.7/site-packages/torch/nn/modules/loss.py", line 950, in forward
    return F.cosine_embedding_loss(input1, input2, target, margin=self.margin, reduction=self.reduction)
  File "/home/foodvision/anaconda3/envs/py27/lib/python2.7/site-packages/torch/nn/functional.py", line 1794, in cosine_embedding_loss
    return torch.cosine_embedding_loss(input1, input2, target, margin, reduction)
RuntimeError: expected scalar type Float but found Long

Check this if its related: #11

Training model with batch size 40

I've executed the code python train.py --batch_size 40 with the batch size 40 (since my GPU was failing on 160 and 80).

image

I estimated 12 days since I have lowered the batch size about 3x time and again it's like 14 days now. Can you please advise, when will it be done?

Index Error while training

Hello

I am getting the following error while training :
File "/data/home/ameen.ali/food/im2recipe-Pytorch/data_loader.py", line 103, in __getitem__ path = os.path.join(self.imgPath, loader_path, imgs[imgIdx]['id']) IndexError: list index out of range


ANy idea what is the problem ?

Memory requirements

Can I know the memory requirements for running this, I keep running into an unexpected bus error even when I use (8GB RAM + 24GB Swap file), also the swapfile does not even get fully occupied when the error occurs. I am using a cpu version of torch to run this.

=> loading checkpoint 'model_e220_v-4.700.pth.tar'
=> loaded checkpoint 'model_e220_v-4.700.pth.tar' (epoch 220)
/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torchvision-0.2.1-py2.7.egg/torchvision/transforms/transforms.py:188: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
Test loader prepared.
321
i
0
321
test.py:110: UserWarning: volatile was removed and now has no effect. Use with torch.no_grad(): instead.
v = torch.autograd.Variable(input[j], volatile=True)
test.py:116: UserWarning: volatile was removed and now has no effect. Use with torch.no_grad(): instead.
v = torch.autograd.Variable(target[j], volatile=True)
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm).
Exception NameError: "global name 'FileNotFoundError' is not defined" in <bound method _DataLoaderIter.del of <torch.utils.data.dataloader._DataLoaderIter object at 0x7f97abe148d0>> ignored
Traceback (most recent call last):
File "test.py", line 199, in
main()
File "test.py", line 90, in main
test(test_loader, model, criterion)
File "test.py", line 127, in test
output = model(input_var[0],input_var[1], input_var[2], input_var[3], input_var[4])
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/akhil/Documents/im2recipe/im2recipe-Pytorch/trijoint.py", line 134, in forward
visual_emb = self.visionMLP(x)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py", line 109, in forward
return self.module(*inputs, **kwargs)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/container.py", line 91, in forward
input = module(input)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/container.py", line 91, in forward
input = module(input)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torchvision-0.2.1-py2.7.egg/torchvision/models/resnet.py", line 76, in forward
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/nn/modules/conv.py", line 301, in forward
self.padding, self.dilation, self.groups)
File "/home/akhil/anaconda2/envs/im2recipe/lib/python2.7/site-packages/torch/utils/data/dataloader.py", line 178, in handler
_error_if_any_worker_fails()
RuntimeError: DataLoader worker (pid 12487) is killed by signal: Bus error.

Invalid device id

I'm facing the following issue, when running the pretrained model

$ python test.py
Traceback (most recent call last):
  File "test.py", line 176, in <module>
    main()
  File "test.py", line 31, in main
    model.visionMLP = torch.nn.DataParallel(model.visionMLP, device_ids=[0,1,2,3])
  File "/home/foodvision/anaconda3/envs/py27/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py", line 111, in __init__
    _check_balance(self.device_ids)
  File "/home/foodvision/anaconda3/envs/py27/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py", line 17, in _check_balance
    dev_props = [torch.cuda.get_device_properties(i) for i in device_ids]
  File "/home/foodvision/anaconda3/envs/py27/lib/python2.7/site-packages/torch/cuda/__init__.py", line 292, in get_device_properties
    raise AssertionError("Invalid device id")
AssertionError: Invalid device id

CUDA device_count output is

$ torch.cuda.device_count()
1L

Output of nvidia-smi

$ nvidia-smi
Tue Nov 20 12:19:10 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 396.44                 Driver Version: 396.44                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K80           Off  | 000023D3:00:00.0 Off |                    0 |
| N/A   35C    P0    73W / 149W |      0MiB / 11441MiB |      1%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

Any help would be appreciated. Thanks

Training strategy doesn't lead to a decreasing loss

The training strategy, specifically the random creation of negative samples at each epoch just doesn't seem to work. The loss doesn't decrease even after 100+ epochs and the performance on validation set remains poor even after this much training. It would be such a huge help if you could post a guideline on how the loss function should behave or how the loss curves should look like for this implementation.

Thanks,
Avijit

Training data size

Hi I'm trying to process the data from scratch. But I don't know why the training data size is 238459 instead of 720639 as claimed in the CVPR paper. (I also ran "python tokenize_instructions.py train" and got the output file "tokenized_instructions_train.txt" of 720639 lines.)
I'd appreciate it if you could tell me why and how you chose the 238459 samples. Thank you.

Registration to access dataset results in error

Not really an issue with this repository, but I'm unable to register for access to the dataset. Upon submitting the registration form at

returns the following error:

Django Material
Server error

There's been an error. It's been reported to the site administrators via email and should be fixed shortly. Thanks for your patience.

Following the error, I'm unable to login with the account I tried to setup.

Asking the way to get recipe by querying img

Hi,
I want to test imge2recipe(querying image and response recipe).
But, I can't find imge2recipe script in git hub
How can I test imge2recipe in script??
Please guide us
Thank

How can I test pretrained model?

Hey, I downloaded pretrained model model_e500_v-8.950.pth.tar and test with command python test.py --model_path=model_e500_v-8.950.pth.tar but I got following error:

File "test.py", line 173, in
main()
File "test.py", line 34, in main
model = im2recipe()
File "/trijoint.py", line 120, in init
self.ingRNN_ = ingRNN()
File "/trijoint.py", line 65, in init
self.embs = nn.Embedding(vec.size(0), opts.ingrW2VDim, padding_idx=0) # not sure about the padding idx
File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/sparse.py", line 110, in init
self.reset_parameters()
File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/sparse.py", line 121, in reset_parameters
self.weight[self.padding_idx].fill_(0)
IndexError: index 0 is out of bounds for dimension 0 with size 0

can you provide me solution for this.
Thanks.

Index error when running test

Hi. I'm trying to run test with the pretrained model but I'm getting this error:

Traceback (most recent call last):
  File "test.py", line 173, in <module>
    main()
  File "test.py", line 34, in main
    model = im2recipe()
  File "/home/lea/im2recipe/trijoint.py", line 120, in __init__
    self.ingRNN_    = ingRNN()
  File "/home/lea/im2recipe/trijoint.py", line 65, in __init__
    self.embs = nn.Embedding(vec.size(0), opts.ingrW2VDim, padding_idx=0) # not sure about the padding idx 
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/sparse.py", line 98, in __init__
    self.reset_parameters()
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/sparse.py", line 109, in reset_parameters
    self.weight[self.padding_idx].fill_(0)
IndexError: index 0 is out of bounds for dimension 0 with size 0

"recipe1M" dataset

Hello,
How can I download the "recipe1M" dataset or just its two files "layer1.json" and "layer2.json".
Thanks in advance.

train error

@nhynes hi , I've executed the code python train.py --batch_size 32 with the batch size 32๏ผŒ
but I got an error:

screenshot from 2019-01-18 13-38-26

Can you please advise? thank you very much!

IndexError on attempt to run pertained model

Running

python test.py --model_path=snapshots/model_e500_v-8.950.pth.tar

results in

  File "/home/q/Projects/im2recipe-Pytorch/trijoint.py", line 65, in __init__
    self.embs = nn.Embedding(vec.size(0), opts.ingrW2VDim, padding_idx=0) # not sure about the padding idx
  File "/home/q/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/sparse.py", line 98, in __init__
    self.reset_parameters()
  File "/home/q/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/sparse.py", line 109, in reset_parameters
    self.weight[self.padding_idx].fill_(0)
IndexError: index 0 is out of bounds for dimension 0 with size 0

I'm using python3.7 fresh conda environment. From other issues I get that I should not have encountered such error. What can you advise to do? Fix padding ?

Very confused about the test.py

I tried the following code:
python test.py --model_path=model/model_e220_v-4.700.pth.tar --img_path=./data/Tomato-and-egg-stir-fry-ingredients.jpg --data_path=./data/, but it turns out the following error:

    print(..., file=sys.stderr)
          ^
SyntaxError: invalid syntax

What is the right way to load data?
What if I would like to test a single image or a group of images?

test error

Hi, what do they mean from line 93~ 104 in test.py? I got an error:
File "test.py", line 104, in test
output = model(input_var[0],input_var[1], input_var[2], input_var[3], input_var[4])
IndexError: list index out of range

the imput_var seams empty.

`TypeError: file must have 'read' and 'readline' attributes` when loading training data

First of all, thanks a lot for the great public code!

I am currently trying to reproduce the paper using the recipe1M dataset. I want to first reproduce it here, as downloading the other recipe1M+ dataset takes considerable time.

When I run the training algorithm, I get the following error:

Getitem from  195360 train
Transaction is:  <Transaction object at 0x7fb85a311810>
Reading from : b'c7d39222ec'
Serialized samples are:  25150
    data = self._next_data()
  File "/home/david/venv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1085, in _next_data
    return self._process_data(data)
  File "/home/david/venv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1111, in _process_data
    data.reraise()
  File "/home/david/venv/lib/python3.7/site-packages/torch/_utils.py", line 428, in reraise
    raise self.exc_type(msg)
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/david/venv/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/david/venv/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/david/venv/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/david/src/importer/image_loader.py", line 165, in __getitem__
    sample = pickle.load(serialized_sample, encoding='latin1')
TypeError: file must have 'read' and 'readline' attributes

Specifically, the image loaders __getitem__ function does not seem to work properly with the provided lmdb files. Notice that I renamed some variables names to make the code easier to understand. I even ran mk_dataset.py again to re-create the lmdb files, but this doesn't seem to help. Am I missing something?

    def __getitem__(self, index):

        print("Getitem from ", index, self.partition_name)

        assert self.ids, ("Self ids is not defined!", self.ids)

        if self.partition_name == "train":
            match_bool = np.random.uniform() > self.mismatch_ratio
        elif self.partition_name in ("val", "test"):
            match_bool = True
        else:
            assert False, ("Partition name not well defined", self.partition_name)

        target = match_bool and 1 or -1

        with self.env.begin(write=False) as transaction:
            print("Transaction is: ", transaction)
            print("Reading from :", self.ids[index].encode('latin1'))
            serialized_sample = transaction.get(self.ids[index].encode('latin1'))
        print("Serialized samples are: ", len(serialized_sample))

        sample = pickle.loads(serialized_sample, encoding='latin1')

Am I missing something? Any help is greately appreciated!

Dataset Inaccessible, error mentions missing email?

Files needed to run the pretrained model (vocab.bin if I'm not mistaken) that are in the dataset behind the .edu gate can't be accessed. I dug up my college email just to get an error saying that the im2recipe@csail whatever email couldn't be found or it was inaccessible, something along those lines. I can't register again to get the error to copy it exactly as it believes I've already registered, despite the registration info apparently having gone nowhere.

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.