拍图识菜单
作者:互联网
Inverse Cooking: Recipe Generation from Food Images
代码:https://github.com/facebookresearch/inversecooking.git
论文:Inverse Cooking: Recipe Generation from Food Images.
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import numpy as np
import os
from args import get_parser
import pickle
from model import get_model
from torchvision import transforms
from utils.output_utils import prepare_output
from PIL import Image
import time
Set data_dir
to the path including vocabularies and model checkpoint
data_dir = '../data'
# code will run in gpu if available and if the flag is set to True, else it will run on cpu
use_gpu = False
device = torch.device('cuda' if torch.cuda.is_available() and use_gpu else 'cpu')
map_loc = None if torch.cuda.is_available() and use_gpu else 'cpu'
# code below was used to save vocab files so that they can be loaded without Vocabulary class
#ingrs_vocab = pickle.load(open(os.path.join(data_dir, 'final_recipe1m_vocab_ingrs.pkl'), 'rb'))
#ingrs_vocab = [min(w, key=len) if not isinstance(w, str) else w for w in ingrs_vocab.idx2word.values()]
#vocab = pickle.load(open(os.path.join(data_dir, 'final_recipe1m_vocab_toks.pkl'), 'rb')).idx2word
#pickle.dump(ingrs_vocab, open('../demo/ingr_vocab.pkl', 'wb'))
#pickle.dump(vocab, open('../demo/instr_vocab.pkl', 'wb'))
ingrs_vocab = pickle.load(open(os.path.join(data_dir, 'ingr_vocab.pkl'), 'rb'))
vocab = pickle.load(open(os.path.join(data_dir, 'instr_vocab.pkl'), 'rb'))
ingr_vocab_size = len(ingrs_vocab)
instrs_vocab_size = len(vocab)
output_dim = instrs_vocab_size
print (instrs_vocab_size, ingr_vocab_size)
23231 1488
t = time.time()
import sys; sys.argv=['']; del sys
args = get_parser()
args.maxseqlen = 15
args.ingrs_only=False
model = get_model(args, ingr_vocab_size, instrs_vocab_size)
# Load the trained model parameters
model_path = os.path.join(data_dir, 'modelbest.ckpt')
model.load_state_dict(torch.load(model_path, map_location=map_loc))
model.to(device)
model.eval()
model.ingrs_only = False
model.recipe_only = False
print ('loaded model')
print ("Elapsed time:", time.time() -t)
Downloading: "https://download.pytorch.org/models/resnet50-19c8e357.pth" to /Users/xuehuiping/.torch/models/resnet50-19c8e357.pth
100%|██████████| 102502400/102502400 [00:03<00:00, 26545967.56it/s]
/Users/xuehuiping/anaconda3/envs/inversecooking/lib/python3.6/site-packages/torch/nn/_reduction.py:49: UserWarning: size_average and reduce args will be deprecated, please use reduction='none' instead.
warnings.warn(warning.format(ret))
loaded model
Elapsed time: 7.817010879516602
transf_list_batch = []
transf_list_batch.append(transforms.ToTensor())
transf_list_batch.append(transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)))
to_input_transf = transforms.Compose(transf_list_batch)
greedy = [True, False, False, False]
beam = [-1, -1, -1, -1]
temperature = 1.0
numgens = len(greedy)
Set use_urls = True
to get recipes for images in demo_urls
.
You can also set use_urls = False
and get recipes for images in the path in data_dir/test_imgs
.
import requests
from io import BytesIO
import random
from collections import Counter
use_urls = False # set to true to load images from demo_urls instead of those in test_imgs folder
show_anyways = False #if True, it will show the recipe even if it's not valid
image_folder = os.path.join(data_dir, 'demo_imgs')
if not use_urls:
demo_imgs = os.listdir(image_folder)
random.shuffle(demo_imgs)
demo_urls = ['https://food.fnr.sndimg.com/content/dam/images/food/fullset/2013/12/9/0/FNK_Cheesecake_s4x3.jpg.rend.hgtvcom.826.620.suffix/1387411272847.jpeg',
'https://www.196flavors.com/wp-content/uploads/2014/10/california-roll-3-FP.jpg']
demo_files = demo_urls if use_urls else demo_imgs
for img_file in demo_files:
if use_urls:
response = requests.get(img_file)
image = Image.open(BytesIO(response.content))
else:
image_path = os.path.join(image_folder, img_file)
image = Image.open(image_path).convert('RGB')
transf_list = []
transf_list.append(transforms.Resize(256))
transf_list.append(transforms.CenterCrop(224))
transform = transforms.Compose(transf_list)
image_transf = transform(image)
image_tensor = to_input_transf(image_transf).unsqueeze(0).to(device)
plt.imshow(image_transf)
plt.axis('off')
plt.show()
plt.close()
num_valid = 1
for i in range(numgens):
with torch.no_grad():
outputs = model.sample(image_tensor, greedy=greedy[i],
temperature=temperature, beam=beam[i], true_ingrs=None)
ingr_ids = outputs['ingr_ids'].cpu().numpy()
recipe_ids = outputs['recipe_ids'].cpu().numpy()
outs, valid = prepare_output(recipe_ids[0], ingr_ids[0], ingrs_vocab, vocab)
if valid['is_valid'] or show_anyways:
print ('RECIPE', num_valid)
num_valid+=1
#print ("greedy:", greedy[i], "beam:", beam[i])
BOLD = '\033[1m'
END = '\033[0m'
print (BOLD + '\nTitle:' + END,outs['title'])
print (BOLD + '\nIngredients:'+ END)
print (', '.join(outs['ingrs']))
print (BOLD + '\nInstructions:'+END)
print ('-'+'\n-'.join(outs['recipe']))
print ('='*20)
else:
pass
print ("Not a valid recipe!")
print ("Reason: ", valid['reason'])
RECIPE 1
[1m
Title:[0m Penne with caramelized onions 【焦糖洋葱通心粉】
[1m
Ingredients:[0m
cheese, pasta, pepper, onion, oil
[1m
Instructions:[0m
-Heat oil in large skillet over medium-high heat.
-Add onions; cook until golden brown, stirring occasionally, about 15 minutes.
-Add pasta and cook until heated through, stirring occasionally, about 10 minutes.
-Season with salt and pepper.
-Sprinkle with cheese.
====================
RECIPE 2
[1m
Title:[0m Mostaccioli and cheese
[1m
Ingredients:[0m
cheese, pasta, pepper, onion, oil
[1m
Instructions:[0m
-Bring a large pot of lightly salted water to a boil.
-Add pasta and cook for 8 to 10 minutes or until al dente; drain.
-Heat oil in a large skillet over medium heat; cook and stir onion until lightly browned.
-Mix in pasta and cheese; season with salt and pepper.
-Toss in pasta, and continue cooking until cheese has melted.
====================
RECIPE 3
[1m
Title:[0m Pasta with caramelized onions
[1m
Ingredients:[0m
cheese, pasta, pepper, onion, oil
[1m
Instructions:[0m
-Preheat the oven to 350 f. put the onions into a skillet.
-Add the olive oil.
-Season with salt and pepper and cook over medium heat, stirring occasionally, until they are golden brown, about 15 minutes.
-Meanwhile, cook the pasta in a large pot of boiling water for 8 minutes.
-Drain the pasta into a colander.
-Add the onions back to the pot, along with the ragu sauce.
-Stir to combine.
-Transfer the onions to a baking dish, cover with foil and bake for 45 minutes.
-Sprinkle the cheeses on top, return to the oven and bake for another 15 minutes, until the cheese is melted.
====================
RECIPE 4
[1m
Title:[0m Easy baked ziti
[1m
Ingredients:[0m
cheese, pasta, pepper, onion, oil
[1m
Instructions:[0m
-Preheat oven to 375f
-Cook the ziti according to package instructions.
-Drain, rinse with cool water, and set aside.
-Heat the oil in a medium sized pan over medium heat.
-Add the onion, and season with salt and pepper.
-Cook until the onion is translucent.
-Add the ziti pasta and toss to coat with oil and onions.
-Stir in the ricotta cheese.
-Pour the ziti mixture into an ungreased baking dish.
-Toss in the parmesan cheese and mozzarella cheeses.
-Spread the ziti mix over the ziti.
-Bake in the preheated oven for 20 minutes, or until the mixture is bubbling.
====================
RECIPE 1
[1m
Title:[0m Grilled eggplant and zucchini 【烤茄子和西葫芦】
[1m
Ingredients:[0m
zucchini, oil, salt, squash, pepper, eggplant
[1m
Instructions:[0m
-Preheat grill to medium-high.
-Brush eggplant slices with oil and season with salt and pepper.
-Grill, turning once, until tender and lightly charred, about 10 minutes.
-Transfer to a platter and let cool.
-Cut into 1/2-inch slices.
-Serve with grilled zucchini.
====================
RECIPE 2
[1m
Title:[0m Grilled foil-wrapped awesome eggplant & zucchini
[1m
Ingredients:[0m
zucchini, oil, salt, squash, pepper, eggplant
[1m
Instructions:[0m
-Heat grill to medium.
-Brush the eggplant and zucchini slices with olive oil.
-Grill for 4 to 6 minutes, flipping over halfway through.
-Season with salt and pepper.
-Grill until tender, about 8 minutes per side.
====================
RECIPE 3
[1m
Title:[0m Grilled vegetables
[1m
Ingredients:[0m
zucchini, oil, salt, squash, pepper, eggplant
[1m
Instructions:[0m
-Cut the vegetables in half lengthwise and then crosswise.
-Put the vegetable pieces, zucchini and squash halves in a colander or colander and sprinkle with salt, then toss.
-Allow to drain for 30 minutes to 2 hours or up to 1 day.
-Preheat the grill.
-Put the eggplant slices on the hottest part of the grill, then rotate the pieces so that they are flat on the grill.
-Grill for 5 to 7 minutes on each side or until the flesh is very soft.
-Serve hot.
====================
RECIPE 4
[1m
Title:[0m Grilled eggplant & zucchini
[1m
Ingredients:[0m
zucchini, oil, salt, squash, pepper, eggplant
[1m
Instructions:[0m
-Heat barbecue to medium-high heat.
-Brush grill with oil.
-Place eggplant and zucchini on grate of grill and grill 8 to 10 min.
-Or until tender, turning after 4 min.
-Brush eggplant with oil; place on grill grate, cut sides down, over medium-high grill.
-Grill 3 min., or until lightly grill-marked.
-Transfer eggplant and zucchini to serving platter.
-Add salt, pepper and remaining ingredients to bowl; toss well to coat.
====================
RECIPE 1
[1m
Title:[0m Easy pancakes 【简单的煎饼】
[1m
Ingredients:[0m
sugar, flour, egg, salt, butter, baking_powder, milk, oil
[1m
Instructions:[0m
-Mix all ingredients together.
-Heat skillet and pour 1/4 cup of batter onto skillet.
-Cook until bubbles form on top.
-Flip and cook until golden brown.
====================
RECIPE 2
[1m
Title:[0m Fluffy pancakes
[1m
Ingredients:[0m
sugar, flour, egg, salt, butter, baking_powder, milk, oil
[1m
Instructions:[0m
-In a large mixing bowl, combine the flour, baking powder, sugar and salt.
-Make a well in the center.
-In the well, in a separate bowl, combine the milk, eggs and oil.
-Add the wet ingredients into the well and stir well.
-Pour 1/2 cup portions over a hot griddle.
-When bubbles appear on the surface and the pancakes are dry, flip them over and continue frying until the other side is golden.
-Serve hot or cold with syrup.
-Makes 12 pancakes.
-Note: i made these with a batch of batter for this recipe.
====================
RECIPE 3
[1m
Title:[0m Whole wheat peanut butter pancakes
[1m
Ingredients:[0m
sugar, flour, egg, salt, butter, baking_powder, milk, oil
[1m
Instructions:[0m
-Whisk together the flour, sugar, salt, and baking powder.
-In a separate bowl, beat the egg, milk, and peanut butter.
-Stir the egg mixture into the dry ingredients just until moistened.
-Heat a large skillet over medium high heat.
-Grease with butter or olive oil if using, and pour the batter 1/4 cup at a time into the skillet.
-Let the batter cook until bubbles begin to form on the top, and then flip.
-Cook until browned on the other side, about 2 minutes.
-Repeat with the remaining batter.
====================
RECIPE 4
[1m
Title:[0m Easy whole wheat pancakes
[1m
Ingredients:[0m
sugar, flour, egg, salt, butter, baking_powder, milk, oil
[1m
Instructions:[0m
-Sift dry ingredients together.
-Mix eggs, milk, and oil together.
-Add to dry ingredients.
-Cook as per pancakes.
====================
RECIPE 1
[1m
Title:[0m Chicken enchiladas 【辣子鸡丁】
[1m
Ingredients:[0m
cheese, tortilla, onion, cream, enchilada_sauce, chicken, chili, beans, pepper, tomato, cumin
[1m
Instructions:[0m
-Preheat oven to 350 degrees.
-In a large bowl, combine chicken, beans, tomatoes, onion, green chilies, cumin, and chili powder.
-Spoon about 1/4 cup of the chicken mixture down the center of each tortilla.
-Roll up and place seam side down in a greased 13x9 baking dish.
-Pour enchilada sauce over enchiladas.
-Sprinkle with cheese.
-Bake uncovered for 20 minutes.
====================
RECIPE 2
[1m
Title:[0m Chicken enchilada bake
[1m
Ingredients:[0m
cheese, tortilla, onion, cream, enchilada_sauce, chicken, chili, beans, pepper, tomato, cumin
[1m
Instructions:[0m
-Preheat oven to 350f.
-Cook onion and green pepper in a little oil in a saucepan.
-Add green chile peppers and cook 2 minutes.
-Add tomatoes, and beans, and stir.
-Bring to a boil, and reduce heat to med.
-Simmer 15 minutes.
-Place one tortilla in a greased 13x9 inch baking dish.
-Pour 1/3 of chicken mixture over tortilla.
-Top with 1/4 cup salsa, and 1/3 cup cheese.
-Repeat twice.
-Bake at 350f for 20 minutes, or until cheese is melted.
-Serve with sour cream, guacamole, and chopped green onions.
====================
RECIPE 3
[1m
Title:[0m Chicken enchiladas
[1m
Ingredients:[0m
cheese, tortilla, onion, cream, enchilada_sauce, chicken, chili, beans, pepper, tomato, cumin
[1m
Instructions:[0m
-Boil chicken and beans.
-Combine chicken, beans, and seasonings.
-Spread 1/3 c enchilada sauce onto each tortilla.
-Place 1 tablespoon chicken mixture in center and roll tortilla.
-Place seam-side down in 13 x 9 inch pan.
-Pour remaining enchilada sauce over all.
-Top with remaining cheese.
-Bake at 350 degrees for 30 minutes.
-Garnish with sour cream and chopped green onions.
====================
RECIPE 4
[1m
Title:[0m Enchilada chicken enchiladas
[1m
Ingredients:[0m
cheese, tortilla, onion, cream, enchilada_sauce, chicken, chili, beans, pepper, tomato, cumin
[1m
Instructions:[0m
-Preheat oven to 350f.
-Place chicken breasts in a baking pan and season with salt and pepper; cook in the preheated oven for 20-25 minutes, until chicken is cooked through.
-While chicken is cooking, mix together refried beans, green chiles, onion, tomato sauce, 1 cup of shredded jack cheese, and cumin.
-When chicken is done, place about 2 tablespoons of sauce on a tortilla and wrap up.
-Top with sour cream and remaining monterey jack cheese.
====================
RECIPE 1
[1m
Title:[0m Garlic shrimp scampi 【大蒜虾虾】
[1m
Ingredients:[0m
shrimp, pepper, butter, clove, oil, salt, pasta, parsley
[1m
Instructions:[0m
-Heat olive oil in a large skillet over medium heat.
-Cook and stir garlic in hot oil until fragrant, about 1 minute.
-Stir shrimp into garlic; cook and stir until shrimp are pink and opaque, about 3 minutes.
-Season with salt and pepper.
-Stir butter into shrimp mixture until melted and sauce is heated through, about 2 minutes.
-Stir parsley into shrimp mixture; cook and stir until heated through, about 1 minute.
====================
RECIPE 2
[1m
Title:[0m Angel hair with garlic and shrimp 【天使毛配大蒜和虾】
[1m
Ingredients:[0m
shrimp, pepper, butter, clove, oil, salt, pasta, parsley
[1m
Instructions:[0m
-Cook angel hair pasta al dente and drain.
-In lg pan over medium heat, add olive oil.
-Saute garlic for one minute then add shrimp saute for 2 minutes.
-Add butter and heat until melted.
-Season with salt, pepper, and parsley.
-Toss with angel hair pasta.
-Serve immediately.
====================
RECIPE 3
[1m
Title:[0m Simple garlic shrimp 【简单的大蒜虾】
[1m
Ingredients:[0m
shrimp, pepper, butter, clove, oil, salt, pasta, parsley
[1m
Instructions:[0m
-Bring a large pot of lightly salted water to a boil.
-Add pasta and cook for 8 to 10 minutes or until al dente; drain.
-Melt butter in a skillet over medium heat.
-Add garlic and cook until lightly browned.
-Stir in cooked and drained shrimp and cook 3 to 5 minutes on each side, until heated through.
-Sprinkle with parsley and serve.
====================
RECIPE 4
[1m
Title:[0m Fettuccine with garlic butter 【蒜蓉黄油宽面条】
[1m
Ingredients:[0m
shrimp, pepper, butter, clove, oil, salt, pasta, parsley
[1m
Instructions:[0m
-Cook pasta according to package directions.
-While pasta is cooking, heat the olive oil in a large saute pan over medium heat.
-Add the garlic and cook, stirring, until fragrant, about 30 seconds.
-Add the shrimp and season with salt and pepper.
-Cook, stirring often, until shrimp are pink on the outside and just cooked through, 2 to 3 minutes.
-Remove from the pan and stir in the butter, parsley, and salt.
-Toss well to coat.
-Serve immediately.
====================
RECIPE 1
[1m
Title:[0m Avocado egg salad sandwich
[1m
Ingredients:[0m
avocado, bread, tomato, mayonnaise, pepper, egg, salt
[1m
Instructions:[0m
-Toast bread.
-Spread mayonnaise on one side of each slice.
-Place avocado slices on one slice of bread.
-Top with tomato slices and salt and pepper.
-Top with other slice of bread.
====================
RECIPE 2
[1m
Title:[0m Egg salad sandwich
[1m
Ingredients:[0m
avocado, bread, tomato, mayonnaise, pepper, egg, salt
[1m
Instructions:[0m
-Toast bread
-Smash avocado into a chunky paste.
-Season with salt and pepper.
-Toast bread.
-Spread egg on toast and top with tomato, avocado and a sprinkle of cayenne pepper.
-Eat with a fork or a fork.
====================
RECIPE 3
[1m
Title:[0m Easy avocado egg salad sandwich
[1m
Ingredients:[0m
avocado, bread, tomato, mayonnaise, pepper, egg, salt
[1m
Instructions:[0m
-Mash avocado in a bowl with a fork.
-Add mayonnaise and mix well.
-Add salt and pepper to taste.
-Add tomatoes.
-Divide egg salad on 2 bread slices.
-Top with avocado and tomatoes.
-Sprinkle with additional salt and pepper.
-Add other 2 bread slices.
====================
RECIPE 4
[1m
Title:[0m Avocado egg sandwich
[1m
Ingredients:[0m
avocado, bread, tomato, mayonnaise, pepper, egg, salt
[1m
Instructions:[0m
-Toast the bread.
-Slice tomato.
-Salt and pepper the egg, and toast.
-Place tomato on toasted bread, and mayonnaise.
-Top with avocado and sprinkle with salt (optional).
-Cut a small hole in the middle of the bread and enjoy.
====================
标签:pepper,菜单,1m,拍图识,oil,salt,until,0m 来源: https://www.cnblogs.com/xuehuiping/p/16467801.html