Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INaturalist Hard version #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configs/datamodule/inaturalist_emb_contexts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ num_workers: 4

spurious_setting: ${spurious_setting} # Use the global value of 'spurious_setting'
sp_token_generation_mode: ${sp_token_generation_mode} # Use the global value of 'sp_token_generation_mode'
select_classes_from_supercategories: False

use_context_as_intermediate_queries: ${use_context_as_intermediate_queries}

Expand Down
58 changes: 54 additions & 4 deletions src/datamodules/datasets/inaturalist_emb_contexts_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self,
query_minority_group_proportion: float,
spurious_setting: str,
sp_token_generation_mode: str,
select_classes_from_supercategories: bool = False,
use_context_as_intermediate_queries: bool = False,
reverse_task: bool = False,
rotate_encodings: bool = False,
Expand All @@ -65,6 +66,7 @@ def __init__(self,
spurious_setting (str): Determines the handling mode of spurious tokens in the dataset instances.
sp_token_generation_mode (str): Specifies whether the representations of two spurious labels should be
'opposite' or 'random'.
select_classes_from_supercategories (bool): Whether to select classes from the same supercategories.
use_context_as_intermediate_queries (bool): Whether intermediate queries should be the context examples.
reverse_task (bool): Whether to predict the artificially created spurious variable instead of the object.
rotate_encodings (bool): Determines if image encodings are rotated. True enables rotation
Expand Down Expand Up @@ -120,10 +122,50 @@ def __init__(self,
self._class1_split = class1_split
self._class2_split = class2_split
if class1_split == class2_split:
self._categories = self._dataframe["category"].unique()
if select_classes_from_supercategories:
self._supercategories = [
supercategory for supercategory in self._dataframe["supercategory"].unique()
if len(self._dataframe[self._dataframe['supercategory'] == supercategory]['category'].unique()) >= 2]

self._categories = {
supercategory: self._dataframe[self._dataframe['supercategory'] == supercategory]["category"].unique()
for supercategory in self._supercategories
}
else:
self._supercategories = None

self._categories = self._dataframe["category"].unique()
else:
self._categories = (self._dataframe.loc[self._dataframe['split'] == class1_split, "category"].unique(),
if select_classes_from_supercategories:
self._supercategories = [
supercategory for supercategory in self._dataframe["supercategory"].unique()
if len(self._dataframe[(self._dataframe['supercategory'] == supercategory)
& (self._dataframe['split'] == class1_split)]['category'].unique()) > 0
and len(self._dataframe[(self._dataframe['supercategory'] == supercategory)
& (self._dataframe['split'] == class2_split)]['category'].unique()) > 0]

self._categories = [{
supercategory: self._dataframe[(self._dataframe['supercategory'] == supercategory)
& (self._dataframe['split'] == class_split)]["category"].unique()
for supercategory in self._supercategories}
for class_split in [class1_split, class2_split]]
else:
self._supercategories = None

self._categories = (self._dataframe.loc[self._dataframe['split'] == class1_split, "category"].unique(),
self._dataframe.loc[self._dataframe['split'] == class2_split, "category"].unique())

if self._supercategories:
if isinstance(self._categories, list):
self._supercategories_prop = [
len(self._categories[0][supercategory])
+ len(self._categories[1][supercategory])
for supercategory in self._supercategories
]
else:
self._supercategories_prop = [len(self._categories[supercategory]) for supercategory in self._supercategories]

self._supercategories_prop = np.array(self._supercategories_prop) / np.sum(self._supercategories_prop)

self._context_minority_group_proportion = context_minority_group_proportion
self._query_minority_group_proportion = query_minority_group_proportion
Expand All @@ -142,9 +184,17 @@ def _generate_context_and_queries(
"""
# Randomly selecting two categories
if self._class1_split != self._class2_split:
category1, category2 = (np.random.choice(x, size=1)[0] for x in self._categories)
if self._supercategories:
supercategory = np.random.choice(self._supercategories, p=self._supercategories_prop, size=1)[0]
category1, category2 = (np.random.choice(x[supercategory], size=1)[0] for x in self._categories)
else:
category1, category2 = (np.random.choice(x, size=1)[0] for x in self._categories)
else:
category1, category2 = np.random.choice(self._categories, size=2, replace=False)
if self._supercategories:
supercategory = np.random.choice(self._supercategories, p=self._supercategories_prop, size=1)[0]
category1, category2 = np.random.choice(self._categories[supercategory], size=2, replace=False)
else:
category1, category2 = np.random.choice(self._categories, size=2, replace=False)

# sampling context and query examples for each class
context_cat1_size = num_context_examples // 2
Expand Down
2 changes: 2 additions & 0 deletions src/datamodules/inaturalist_emb_contexts_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self,
query_minority_group_proportion: float,
spurious_setting: str,
sp_token_generation_mode: str,
select_classes_from_supercategories: bool,
use_context_as_intermediate_queries: bool,
reverse_task: bool,
rotate_encodings: bool,
Expand All @@ -49,6 +50,7 @@ def __init__(self,
query_minority_group_proportion=query_minority_group_proportion,
spurious_setting=spurious_setting,
sp_token_generation_mode=sp_token_generation_mode,
select_classes_from_supercategories=select_classes_from_supercategories,
use_context_as_intermediate_queries=use_context_as_intermediate_queries,
reverse_task=reverse_task,
swapping_minority_proportion_context=swapping_minority_proportion_context,
Expand Down