Skip to content

Commit

Permalink
Merge pull request aistairc#19 from aistairc/fix_state_value
Browse files Browse the repository at this point in the history
Fix state value
  • Loading branch information
Ease112 authored Jul 5, 2023
2 parents f5e8aff + 396f290 commit 5f4b8e2
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ For more information about original VirtualHome v2.2, please see [here](https://

## What is New
* We added [actions](./simulation/README.md#supported-in-unity-simulator) that can be executed in the Unity simulator. That is, many motions not supported by the original VirtualHome can now be executed.
* Several [new camera modes](https://github.com/aistairc/virtualhome_unity_aist#addition-of-new-four-cameras) have been added.
* Several [new cameras](https://github.com/aistairc/virtualhome_unity_aist#addition-of-new-four-cameras) and three [new camera modes](https://github.com/aistairc/virtualhome_aist/tree/main/simulation/unity_simulator#modification-of-render_script-recorded-on-20230421) have been added.
* It is now possible to output JSON data in frame-by-frame.
* The 2D bounding boxes of objects can now be output.
* A convenient jupyter notebook is provided to run a large number of simulations based on the script data and save the results.
Expand Down
170 changes: 170 additions & 0 deletions demo/change_state_value_after_action.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"import re\n",
"import json\n",
"import glob\n",
"import copy\n",
"import time\n",
"import random\n",
"import shutil\n",
"from pathlib import Path\n",
"\n",
"sys.path.append('../simulation/')\n",
"sys.path.append('../dataset_utils/')\n",
"\n",
"import augmentation_utils"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def get_program_file(file_path):\n",
" program_name = ''\n",
" list_of_steps = []\n",
" i = 1\n",
" with open(file_path, \"r\") as f:\n",
" lines = f.readlines()\n",
" program_name = lines[0].replace(\"\\n\", \"\")\n",
" for line in lines[2:]:\n",
" line = line.replace(\"\\n\", \"\")\n",
" if line.startswith(\"<\"):\n",
" line = line.replace(\"<char0> \",\"\")\n",
" i+=1\n",
" temp_dict = {}\n",
" temp_dict['step']=i\n",
" action, objs, indexs = augmentation_utils.parseStrBlock(line)\n",
" temp_dict['action']=action\n",
" if len(objs) > 0:\n",
" temp_dict['obj1']=objs[0]\n",
" temp_dict['index1']=indexs[0]\n",
" if len(objs) > 1:\n",
" temp_dict['obj2']=objs[1]\n",
" temp_dict['index2']=indexs[1]\n",
" list_of_steps.append(temp_dict)\n",
" else:\n",
" pass\n",
"\n",
" return program_name, list_of_steps"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def read_and_write(script_dict,path,scene,program_name,item,start_step_index=None,put_id=None):\n",
" if start_step_index is None:\n",
" json_file = \"/activityList-graph-state-{:03d}.json\".format(script_dict['step'])\n",
" else:\n",
" json_file = \"/activityList-graph-state-{:03d}.json\".format(start_step_index)\n",
" json_path = path+\"/\"+scene+\"/graph states/\"+program_name+json_file\n",
" new_data={}\n",
" with open(json_path, \"r\") as f:\n",
" json_data = json.load(f)\n",
" for node in json_data['nodes']:\n",
" if start_step_index is None:\n",
" if node['id']==int(script_dict['index1']) and node['class_name']==script_dict['obj1']:\n",
" if 'GRABED' in node['states']:\n",
" node['states'].remove('GRABED')\n",
" else:\n",
" if node['id']==int(put_id):\n",
" if 'GRABED' in node['states']:\n",
" node['states'].remove('GRABED')\n",
" new_data=json_data\n",
" output_path = \"../simulation/Output/edited jsons/\"+item+\"/\"+scene+\"/graph states/\"+program_name\n",
" if os.path.exists(output_path) is False:\n",
" os.makedirs(output_path)\n",
" output_json_path = output_path+json_file\n",
" with open(output_json_path, \"w\") as out_file:\n",
" json.dump(new_data, out_file, indent=2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"data_path = \"E:/件名:納品物/4(3)の生成データ 一式/データセット2(部屋から移動あり)/\"\n",
"for item in os.listdir(data_path):\n",
" category_path = data_path+item\n",
" for scene in os.listdir(category_path+\"/executed_program/\"):\n",
" program_path = category_path+\"/executed_program/\"+scene+\"/\"\n",
" for program in os.listdir(program_path):\n",
" path=program_path+program\n",
" program_name, script_info = get_program_file(path)\n",
" keep_change_list=[]\n",
" for script_dict in script_info:\n",
" if script_dict['action'] in ['GRAB']:\n",
" for dict in keep_change_list:\n",
" if script_dict['index1'] == dict['put_id']:\n",
" keep_change_list.remove(dict)\n",
" # we need to change JSON file until program end or agent pick same obj again\n",
" if len(keep_change_list) > 0:\n",
" existing_path=category_path\n",
" for dict in keep_change_list:\n",
" dict['start_index'] = dict['start_index']+1\n",
" read_and_write(script_dict,existing_path,scene,program_name,item, dict['start_index'],dict['put_id'])\n",
" existing_path=\"../simulation/Output/edited jsons/\"+item\n",
" json_file = \"/activityList-graph-state-{:03d}.json\".format(dict['start_index'])\n",
" if os.path.exists(existing_path+\"/\"+scene+\"/graph states/\"+program_name+json_file) is False:\n",
" print('edit new',program_name+json_file)\n",
" existing_path=category_path\n",
" if script_dict['action'] in ['PUTBACK','PUTOBJBACK','PUTIN']:\n",
" keep_change_dict={}\n",
" keep_change_dict['start_index']=script_dict['step']\n",
" keep_change_dict['put_id']=script_dict['index1']\n",
" keep_change_list.append(keep_change_dict)\n",
" if len(keep_change_list) > 1:\n",
" existing_path=\"../simulation/Output/edited jsons/\"+item\n",
" json_file = \"/activityList-graph-state-{:03d}.json\".format(script_dict['step'])\n",
" if os.path.exists(existing_path+\"/\"+scene+\"/graph states/\"+program_name+json_file):\n",
" read_and_write(script_dict,existing_path,scene,program_name,item)\n",
" else:\n",
" read_and_write(script_dict,category_path,scene,program_name,item)\n",
" else:\n",
" read_and_write(script_dict,category_path,scene,program_name,item)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
75 changes: 38 additions & 37 deletions demo/scenario_generate_graph_and_video_with_cameras_changes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
" '5':{'kitchen':'kitchentable', 'bathroom': 'bathroomcounter', 'bedroom': 'bed', 'livingroom':'tvstand'},\n",
" '6':{'kitchen':'kitchentable', 'bathroom': 'bathroomcounter', 'bedroom': 'coffeetable', 'livingroom':'desk'},\n",
" '7':{'kitchen':'kitchentable', 'bathroom': 'bathroomcounter', 'bedroom': 'desk', 'livingroom':'desk'}\n",
" }"
"}"
]
},
{
Expand Down Expand Up @@ -139,7 +139,7 @@
" is_first_switchoff = True\n",
" for line in script_lines:\n",
" action, objs, indexs = augmentation_utils.parseStrBlock(line)\n",
" if action == 'SWITCHOFF' and objs[0] in ['tv', 'television', 'computer', 'faucet', 'light', 'lightswitch']:\n",
" if action.upper() == 'SWITCHOFF' and objs[0] in ['tv', 'television', 'computer', 'faucet', 'light', 'lightswitch']:\n",
" if is_first_switchoff:\n",
" node = find_nodes(g, id=int(indexs[0]))[0]\n",
" states = node['states']\n",
Expand All @@ -152,7 +152,7 @@
" is_first_switchon = False\n",
" if objs[0] in ['tv', 'television', 'computer', 'faucet']:\n",
" comm.expand_scene(graph)\n",
" elif action == 'SWITCHON' and objs[0] in ['tv', 'television', 'computer', 'faucet', 'light', 'lightswitch']:\n",
" elif action.upper() == 'SWITCHON' and objs[0] in ['tv', 'television', 'computer', 'faucet', 'light', 'lightswitch']:\n",
" if is_first_switchon:\n",
" node = find_nodes(g, id=int(indexs[0]))[0]\n",
" states = node['states']\n",
Expand Down Expand Up @@ -245,8 +245,11 @@
" with open(ftaa_path, \"r\") as f:\n",
" ftaa_lines = f.readlines()\n",
" # sometimes FIND don't print out in ftaa file if the FIND object is near avatar\n",
" if 'FIND' in action_list and len(ftaa_lines) < len(action_list):\n",
" action_list.remove('FIND')\n",
" # assume FIND is no need to consider\n",
" if len(ftaa_lines) < len(action_list):\n",
" for action in action_list:\n",
" if action.upper() in ['FIND']:\n",
" action_list.remove(action)\n",
" i = 0\n",
" graph_state_index = 1\n",
" for action_index, action in zip(range(len(action_list)), action_list):\n",
Expand Down Expand Up @@ -349,7 +352,13 @@
" new_obj = equ_new_obj\n",
" break\n",
" else:\n",
" new_obj = equiv_class[current_obj][0]\n",
" # This can be a critical issue, program used the key of class_name_equi's dictionary as an object without specified ID.\n",
" # But item values can't be found in graph after alternative_temp_obj's transformation\n",
" # It will use the first item of dictionary's array.\n",
" if alternative_temp_obj.get(equiv_class[current_obj][0]) is not None:\n",
" new_obj = alternative_temp_obj[equiv_class[current_obj][0]]\n",
" else:\n",
" new_obj = equiv_class[current_obj][0]\n",
" return new_obj\n",
"\n",
"# graphにない場合\n",
Expand Down Expand Up @@ -403,11 +412,12 @@
" return str(current_room_id)\n",
" else:\n",
" found_in_program = False\n",
" if action not in ['WALK', 'GRAB', 'FIND']: # obj is in hand / same obj from the previous one\n",
" if action.upper() not in ['WALK', 'GRAB', 'FIND']: # obj is in hand / same obj from the previous one\n",
" for k, v in obj_id_map.items():\n",
" if obj in k:\n",
" found_in_program = True\n",
" return str(v)\n",
"\n",
" if found_in_program is False:\n",
" current_room_id = [node['id'] for node in graph['nodes'] if node['class_name'] == current_room][1 if (scene_id == 3 and current_room == 'bedroom') else 0]\n",
" ids_in_room = [edge['from_id'] for edge in graph['edges'] if edge['relation_type'] == 'INSIDE' and edge['to_id'] == current_room_id]\n",
Expand Down Expand Up @@ -469,21 +479,31 @@
" result_list.append(script_line)\n",
" continue\n",
" elif indexs[0].isdigit() and indexs[0] != '1': ## Only for accurate ID indication\n",
" if (objs[0] in change_execute_map):\n",
" change_obj_name = change_execute_map[objs[0]]\n",
" if equiv_class.get(objs[0]) is not None: # if program use the key value of class_name_equivalence's dictionary\n",
" change_obj_name = [node['class_name'] for node in graph['nodes'] if node['id'] == int(indexs[0])][0]\n",
" keyword = (change_obj_name, int(indexs[0]))\n",
" else:\n",
" keyword = (objs[0], int(indexs[0]))\n",
" if (objs[0] in change_execute_map):\n",
" change_obj_name = change_execute_map[objs[0]]\n",
" keyword = (change_obj_name, int(indexs[0]))\n",
" else:\n",
" keyword = (objs[0], int(indexs[0]))\n",
" program_obj_id_map[keyword+tuple(str(len(program_obj_id_map)))] = int(indexs[0])\n",
" if obj_id_map.get(keyword) is None:\n",
" obj_id_map[keyword] = int(indexs[0])\n",
" \n",
" graph_class_names = [node['class_name'] for node in graph['nodes']]\n",
" if objs[0] not in graph_class_names:\n",
" new_obj = get_obj_name(objs[0])\n",
" if equiv_class.get(objs[0]) is not None: # if program use the key value of class_name_equivalence's dictionary\n",
" new_obj = [node['class_name'] for node in graph['nodes'] if node['id'] == int(indexs[0])][0]\n",
" else:\n",
" new_obj = get_obj_name(objs[0])\n",
" script_line = script_line.replace('<'+objs[0]+'>', '<'+new_obj+'>')\n",
" if len(objs) > 1 and objs[1] not in graph_class_names:\n",
" new_obj = get_obj_name(objs[1])\n",
" if equiv_class.get(objs[1]) is not None: # if program use the key value of class_name_equivalence's dictionary\n",
" new_obj = [node['class_name'] for node in graph['nodes'] if node['id'] == int(indexs[1])][0]\n",
" else:\n",
" new_obj = get_obj_name(objs[1])\n",
" script_line = script_line.replace('<'+objs[1]+'>', '<'+new_obj+'>')\n",
" result_list.append(script_line)\n",
" if objs[0] in rooms:\n",
Expand Down Expand Up @@ -647,7 +667,7 @@
},
"outputs": [],
"source": [
"scene_id = 6\n",
"scene_id = 1\n",
"program_files = []\n",
"data_path = \"../dataset/Test/scene\"+str(scene_id)+\"/*.txt\"\n",
"for file_path in sorted(glob.glob(data_path)):\n",
Expand All @@ -666,7 +686,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
"scrolled": true
},
"outputs": [],
"source": [
Expand All @@ -682,9 +702,9 @@
" # *** the following is the object adding code to environment and it has to be used here not other places.\n",
" # *** if you need more than one object, you can use several times\n",
" # *** the purpose of this line is to add new object even the same object is existed in same room or any other different room\n",
"# add_object_out_of_script('book', 'livingroom', 'bookshelf', 'INSIDE', 0)\n",
"# add_object_out_of_script('remotecontrol', 'livingroom', 'sofa', 'ON', 0)\n",
"\n",
" initial_room = 'livingroom' # Initial Room : Need to change according to activity *******\n",
" initial_room = 'kitchen' # Initial Room : Need to change according to activity *******\n",
" current_room = initial_room\n",
" obj_id_map = {}\n",
" program_obj_id_map = {}\n",
Expand Down Expand Up @@ -755,18 +775,6 @@
},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"# import imagehash\n",
"\n",
"# function to calculate the amount of difference between two images\n",
"def mse(img1, img2):\n",
" h, w = img1.shape\n",
" diff = cv2.subtract(img1, img2)\n",
" err = np.sum(diff**2)\n",
" mse = err/(float(h*w))\n",
" return mse, diff\n",
"\n",
"#### complementing missing images\n",
"try:\n",
" for program in program_files:\n",
Expand Down Expand Up @@ -838,8 +846,8 @@
"outputs": [],
"source": [
"# Ignore this block, this is only for reason of searching and printing graph\n",
"sceneNo = 6\n",
"room_name = ''\n",
"sceneNo = 2\n",
"room_name = 'kitchen'\n",
"\n",
"# Search only nodes for each room or search nodes and edges for whole scene\n",
"def searchInGraph (sceneNo, test_room_name):\n",
Expand All @@ -859,13 +867,6 @@
"# out_file.close()\n",
"print(searchInGraph(sceneNo, room_name))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 5f4b8e2

Please sign in to comment.