Skip to content

Commit

Permalink
1. add efficiency_analysis
Browse files Browse the repository at this point in the history
2. modified random_placement/below_other -> is_support
  • Loading branch information
sanghongrui committed Apr 25, 2023
1 parent fde4b74 commit a271f09
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 32 deletions.
20 changes: 15 additions & 5 deletions augmentation/envs/augmented_interactive_indoor_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,12 @@ def build_graph_without_editing_trav_map(self, floor):
def is_object_alone_on_floor(self, floor, obj):
"""
@description:An object is considered to be alone on floor, when its VerticalAdjacency
only contain floor whose body_id is 3
only contain floor whose body_id is 3. And is considered to be a support,
when positive_contacts has non-fixed objects.
---------
@param obj: obj instance
-------
@Returns below_other: object is below others ,
@Returns is_support: object is a support for others ,
@Returns is_alone: object is not below or above others
-------
"""
Expand All @@ -243,11 +244,20 @@ def is_object_alone_on_floor(self, floor, obj):

va = obj.states[object_states.VerticalAdjacency].get_value()
vertical_adjacency_list = va.negative_neighbors + va.positive_neighbors

below_others = len(va.positive_neighbors) != 0
is_alone = vertical_adjacency_list == scene_floor_id

is_support = False
if len(va.positive_neighbors) != 0:
contacts = obj.states[object_states.ContactBodies].get_value()
contacts = [c.bodyUniqueIdB for c in contacts]
positive_contacts = set(va.positive_neighbors) & set(contacts)
for id in positive_contacts:
o = self.objects_by_id[id]
if not hasattr(o, 'fixed_base') or not o.fixed_base:
is_support = True
break

return below_others, is_alone
return is_support, is_alone

def get_obj_aabb_map(self, obj):
"""
Expand Down
7 changes: 7 additions & 0 deletions augmentation/envs/augmented_interactive_nav_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from igibson.reward_functions.point_goal_reward import PointGoalReward
from igibson.reward_functions.potential_reward import PotentialReward

import time
from efficiency_analysis import writer


class AugmentedInteractiveNavTask(PointNavRandomTask):
"""
Expand Down Expand Up @@ -58,7 +61,11 @@ def reset_scene(self, env):
#load init_scene
env.scene.load_task_specified_scene(env, self.task_specified_scene_config, self.floor_num)

timer_start = time.time()
env.scene.augmentation.run(env)
timer_end = time.time()
per_time = (timer_end - timer_start)*1000 # in millisecond format
writer(env.scene.augmentation._name + "_efficiency.log", per_time)

# add aabb in env.scene.object_states
for obj_name in env.scene.object_states:
Expand Down
2 changes: 1 addition & 1 deletion augmentation/methods/random_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Contact : jasonsang.tongji.edu.cn
'''
# here put the import lib
from common.baseline_registry import baseline_registry
from baseline_registry import baseline_registry
from augmentation.methods.random_base import Augmentation_Base

@baseline_registry.register_augmentation_method(name="random_none")
Expand Down
2 changes: 1 addition & 1 deletion augmentation/methods/random_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'''
# here put the import lib
from typing import List
from common.baseline_registry import baseline_registry
from baseline_registry import baseline_registry
from augmentation.methods.random_base import Augmentation_Base

@baseline_registry.register_augmentation_method(name="random_open")
Expand Down
12 changes: 6 additions & 6 deletions augmentation/methods/random_placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from abc import abstractmethod
# from igibson import object_states
from igibson.utils.utils import restoreState
from common.baseline_registry import baseline_registry
from baseline_registry import baseline_registry
from augmentation.methods.random_base import Augmentation_Base
from augmentation.envs.augmented_env import AugmentedEnv

Expand Down Expand Up @@ -59,7 +59,7 @@ def random_objects_by_category(self, env:AugmentedEnv):
objs = self.scene.objects_by_category[c]
self.random_objects(env, objs)

print("Finished randomizing %s category" % c)
# print("Finished randomizing %s category" % c)

def random_objects(self, env:AugmentedEnv, room_random_objects):
"""
Expand All @@ -78,12 +78,12 @@ def random_objects(self, env:AugmentedEnv, room_random_objects):
for obj in room_random_objects:
is_exclude = self._is_in_exclude_dict(obj.name)
if is_exclude:
print("WARNING: skip randomlizing %s, it is in exclude dict." %obj.name)
# print("WARNING: skip randomlizing %s, it is in exclude dict." %obj.name)
continue

below_others, is_alone = env.scene.is_object_alone_on_floor(floor, obj)
if below_others:
print("WARNING: skip randomlizing %s, it is below others." %obj.name)
is_support, is_alone = env.scene.is_object_alone_on_floor(floor, obj)
if is_support:
# print("WARNING: skip randomlizing %s, it is below others." %obj.name)
continue

# TODO: p.saveState takes a few seconds, need to speed up
Expand Down
2 changes: 1 addition & 1 deletion augmentation/methods/random_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import random
import math
import numpy as np
from common.baseline_registry import baseline_registry
from baseline_registry import baseline_registry
from augmentation.methods.random_base import Augmentation_Base


Expand Down
2 changes: 1 addition & 1 deletion configs/augmentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: random_none

base:
trav_map_erosion: 3
prob: 0.5
prob: 1.0

scene_random:
# erosion_factor: 1.2
Expand Down
105 changes: 105 additions & 0 deletions efficiency_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File : efficiency_analysis.py
@Time : 2023/01/29 21:53:22
@Author : jasonsang
@Version : 1.0
@Contact : [email protected]
'''
import logging
import time

from igibson.render.mesh_renderer.mesh_renderer_settings import MeshRendererSettings
from igibson.scenes.igibson_indoor_scene import InteractiveIndoorScene
from igibson.simulator import Simulator

from augmentation.envs.augmented_interactive_indoor_scene import AugInteractiveIndoorScene

num_resets = 100
num_steps_per_reset = 1

def igibson_object_randomization(headless=True):
"""
Example of randomization of the texture in a scene
Loads Rs_int (interactive) and randomizes the texture of the objects
"""

settings = MeshRendererSettings(enable_shadow=True, msaa=False)

s = Simulator(
mode="gui_interactive" if not headless else "headless",
image_width=512,
image_height=512,
rendering_settings=settings,
)
load_object_categories = None # Use the list to load a partial house

for i in range(num_resets):
print("Randomizing objects ", i)

timer_start = time.time()
scene = InteractiveIndoorScene(
"Rs_int",
texture_randomization=False,
load_object_categories=load_object_categories,
object_randomization=True,
object_randomization_idx=i,
)
s.import_scene(scene)
for _ in range(num_steps_per_reset):
s.step()
s.reload()
timer_end = time.time()

per_time = (timer_end - timer_start)*1000 # in millisecond format
print('time: ', per_time)
writer("./igibson_object_randomization.log", per_time)

s.disconnect()


def igibson_texture_randomization(headless=True):
"""
Example of randomization of the texture in a scene
Loads Rs_int (interactive) and randomizes the texture of the objects
"""
settings = MeshRendererSettings(enable_shadow=False, msaa=False)

s = Simulator(
mode="gui_interactive" if not headless else "headless",
image_width=512,
image_height=512,
rendering_settings=settings,
)
scene = InteractiveIndoorScene(
"Rs_int",
# load_object_categories=[], # To load only the building. Fast
texture_randomization=True,
object_randomization=False,
)
s.import_scene(scene)

for i in range(num_resets):
print("Randomize texture", i)

timer_start = time.time()
scene.randomize_texture()
for _ in range(num_steps_per_reset):
s.step()
timer_end = time.time()

per_time = (timer_end - timer_start)*1000 # in millisecond format
print('time: ', per_time)
writer("./igibson_texture_randomization.log", per_time)
s.disconnect()

def writer(file_path, per_time):
with open(file_path, 'a') as fp:
fp.write('\n' + str(per_time))


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
# igibson_object_randomization(headless=True)
igibson_texture_randomization(headless=True)
34 changes: 17 additions & 17 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def color_map(floor_map, rect=None):

args = parser.parse_args()
args.config = "configs/fetch_interactive_nav.yaml"
args.mode = "gui_interactive" #"gui_interactive"
args.scene_id = "Benevolence_1_int"
args.mode = "headless" #"gui_interactive"
args.scene_id = "Rs_int"
args.scene_aug_type = "random_open"
args.scene_id = args.scene_id if args.scene_id != "" else None

igibson_config = parse_config(args.config)
Expand Down Expand Up @@ -103,26 +104,25 @@ def color_map(floor_map, rect=None):
viewer.initial_view_direction = [0.0, 0.0, -1.0]

for episode in range(100):
start = time.time()

env.reset()
cv2.imshow("floor_map", cv2.flip(cv2.resize(env.scene.floor_map[0], (512,512)), 0))
highlight_categories(env.scene, ["bottom_cabinet_no_top", "door", "fridge", "chest", "bottom_cabinet"])
# cv2.imshow("floor_map", cv2.flip(cv2.resize(env.scene.floor_map[0], (512,512)), 0))
# highlight_categories(env.scene, ["bottom_cabinet_no_top", "door", "fridge", "chest", "bottom_cabinet"])

print("Episode: {}".format(episode))
print("-" * 80)
for i in range(10): # 10 seconds
# print("-" * 80)
# for i in range(10): # 10 seconds

action = env.action_space.sample()
state, reward, done, _ = env.step(action)
env.simulator.sync(force_sync=True)
# action = env.action_space.sample()
# state, reward, done, _ = env.step(action)
# env.simulator.sync(force_sync=True)

cv2.imshow("map", cv2.flip(cv2.resize(env.scene.floor_map[0],(512,512)), 0))
print("\nStep: ", i)
print("Reward: ", reward)
if done:
break
print("Episode finished after {} timesteps, took {} seconds.".format(env.current_step, time.time() - start))
print("-" * 80 + "\n")
# cv2.imshow("map", cv2.flip(cv2.resize(env.scene.floor_map[0],(512,512)), 0))
# print("\nStep: ", i)
# print("Reward: ", reward)
# if done:
# break
# print("Episode finished after {} timesteps, took {} seconds.".format(env.current_step, time.time() - start))
# print("-" * 80 + "\n")

env.close()

0 comments on commit a271f09

Please sign in to comment.