import json import datasets import os from PIL import Image from numpy import asarray _CITATION = """\ @inproceedings{gao-etal-2018-action, title = "What Action Causes This? Towards Naive Physical Action-Effect Prediction", author = "Gao, Qiaozi and Yang, Shaohua and Chai, Joyce and Vanderwende, Lucy", booktitle = "Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)", month = jul, year = "2018", address = "Melbourne, Australia", publisher = "Association for Computational Linguistics", url = "https://aclanthology.org/P18-1086", doi = "10.18653/v1/P18-1086", pages = "934--945", } """ _DESCRIPTION = """\ Despite recent advances in knowledge representation, automated reasoning, and machine learning, artificial agents still lack the ability to understand basic action-effect relations regarding the physical world, for example, the action of cutting a cucumber most likely leads to the state where the cucumber is broken apart into smaller pieces. If artificial agents (e.g., robots) ever become our partners in joint tasks, it is critical to empower them with such action-effect understanding so that they can reason about the state of the world and plan for actions. Towards this goal, this paper introduces a new task on naive physical action-effect prediction, which addresses the relations between concrete actions (expressed in the form of verb-noun pairs) and their effects on the state of the physical world as depicted by images. We collected a dataset for this task and developed an approach that harnesses web image data through distant supervision to facilitate learning for action-effect prediction. Our empirical results have shown that web data can be used to complement a small number of seed examples (e.g., three examples for each action) for model learning. This opens up possibilities for agents to learn physical action-effect relations for tasks at hand through communication with humans with a few examples. """ _URL = "https://huggingface.co/datasets/sled-umich/Action-Effect" JSON_URL = "http://162.212.153.129/action_effect_info.json" IMGS_URL = "action_effect_image_rs.tar.gz" class Action_Effect(datasets.GeneratorBasedBuilder): """ What Action Causes This? Towards Naive Physical Action-Effect Prediction """ VERSION = datasets.Version("1.0.0") def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "verb noun": datasets.Value("string"), "effect_sentence_list": datasets.features.Sequence(datasets.Value("string")), "effect_phrase_list": datasets.features.Sequence(datasets.features.Sequence(datasets.Value("string"))), "positvie_image_list": datasets.features.Sequence(datasets.Image()), "negative_image_list": datasets.features.Sequence(datasets.Image()), } ), homepage=_URL, citation=_CITATION, ) def _split_generators(self, dl_manager: datasets.DownloadManager): [action_effect_info_path, img_zip_path] = dl_manager.download_and_extract([JSON_URL, IMGS_URL]) # print(dl_dir) # action_effect_info_path = dl_dir[0] + "/action_effect_info.json" # img_zip_path = dl_dir[1] + "/action_effect_image_rs.tar.gz" return [ datasets.SplitGenerator( name= "ActionEffect", gen_kwargs={ "action_effect_info_path": action_effect_info_path, "img_zip_path": img_zip_path }, ) ] def _generate_examples(self, action_effect_info_path, img_zip_path): with open(action_effect_info_path) as f: action_effect_info = json.load(f) # img_zip = tarfile.open(img_zip_path) img_root = img_zip_path+ "/action_effect_image_rs/" for idx, this_ae_info in enumerate(action_effect_info): this_ae_info['positive_image_list'] = [] this_ae_info['negative_image_list'] = [] vn = this_ae_info["verb noun"] this_image_root_positive = img_root + vn.replace(" ", "+") + "/positive/" this_image_root_negative = img_root + vn.replace(" ", "+") + "/positive/" for img_name in os.listdir(this_image_root_positive): img_pil = Image.open(this_image_root_positive + img_name) img_np = asarray(img_pil) this_image_root_positive.append(img_np) for img_name in os.listdir(this_image_root_negative): img_pil = Image.open(this_image_root_negative + img_name) img_np = asarray(img_pil) this_image_root_negative.append(img_np) yield idx, this_ae_info