-
Notifications
You must be signed in to change notification settings - Fork 16
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
How to use the dataset wrappers #98
Comments
Are you sure you're in the same python environment that you installed pytorch-adapt in? If you're using conda, you can do Maybe there were errors when you ran |
It was my mistake! Thank you for your reply. |
May I ask you another question? I tried to run MCD with my own dataset but I am stuck with an error. Could you please tell me where I am doing wrong? I constructed dataset as follows: train_names = ["source_train_dataset", "target_train_dataset"] Here, getitem() in source_train_dataset and source_val_dataset returns an image and a corresponding label. and getitem() in target_train_dataset and target_val_dataset returns only an image. And I constructed hook as follows: generator = timm.create_model("tf_efficientnet_b4", num_classes=0) G_opt = torch.optim.Adam(generator.parameters()) hook = MCDHook(g_opts=[G_opt], c_opts=[C_opt_]) models = {"G": generator, "C": clf_models} _, losses = hook({**models, **dataloaders}) When I ran the last line, the following error appeared. /opt/conda/envs/skn_dev/lib/python3.9/site-packages/pytorch_adapt/hooks/base.py in call(self, inputs, losses) /opt/conda/envs/skn_dev/lib/python3.9/site-packages/pytorch_adapt/hooks/base.py in call(self, *args, **kwargs) /opt/conda/envs/skn_dev/lib/python3.9/site-packages/pytorch_adapt/hooks/base.py in call(self, inputs, losses) I would be so grateful if you help me. Thank you. |
In your code you are passing from tqdm import tqdm
from pytorch_adapt.utils.common_functions import batch_to_device
for data in tqdm(dataloaders["train"]):
data = batch_to_device(data, device)
_, loss = hook({**models, **data}) |
Also you need to use from pytorch_adapt.datasets import CombinedSourceAndTargetDataset
# I'm assuming the original "unwrapped" datasets are
# source_train_dataset, source_val_dataset, target_train_dataset, target_val_dataset.
# The derived datasets are src_train, src_val, etc.
src_train = SourceDataset(source_train_dataset)
src_val = SourceDataset(source_val_dataset)
target_train = TargetDataset(target_train_dataset)
target_val = TargetDataset(target_val_dataset)
train_dataset = CombinedSourceAndTargetDataset(src_train, target_train)
dc = DataloaderCreator(batch_size=32, num_workers=8)
dataloaders= dc(train=train_dataset,
src_train=src_train,
target_train=target_train,
src_val=src_val,
target_val=target_val,
) This way The other keys are just for validation, and they will return the specific datasets with random sampling turned off. For example, Maybe during validation you just want to compute accuracy on the target validation set. In that case, you don't need to pass in so many datasets: src_train = SourceDataset(source_train_dataset)
target_train = TargetDataset(target_train_dataset)
train_dataset = CombinedSourceAndTargetDataset(src_train, target_train)
# During validation you typically don't want randomness in your dataset.
# So the input dataset here should use a non-random transform (e.g. no random cropping, or random flips).
# (I've made the variable name indicate that.)
# The supervised=True argument means that target_val will return tuples of (data, label).
# This only works if the original target_val_dataset also returns tuples of (data, label).
target_val = TargetDataset(target_val_dataset_without_random_transform, supervised=True)
dc = DataloaderCreator(batch_size=32, num_workers=8)
dataloaders = dc(train=train_dataset, target_val=target_val)
# During training use "train"
for data in tqdm(dataloaders["train"]):
data = batch_to_device(data, device)
_, loss = hook({**models, **data})
# During validation use "target_val"
for data in tqdm(dataloaders["target_val"]):
# validation code here |
Could you give me some more days to follow up your code? I want to close this comment after I understand it :D |
Sure, I'll leave this open |
Hello! Thanks to your help, I was able to understand how I can utilize dataset wrappers. I want to train and evaluate MCD, and I want to plot x_loss, y_loss and z_loss during both training and evaluation. I guess that belowing code will help me to evaluate losses during evaluation while not updating model parameters. But I am not sure whether it is right. So, it would be extremely helpful if you let me know if it is right. dc = DataloaderCreator(batch_size=BATCH_SIZE, num_workers=NUM_WORKERS) validation code heregenerator.eval() Thank you for your help! |
Unfortunately there's no easy way to use a hook with optimization turned off. The best you can do right now is create a separate identical hook for eval, using no-op optimizers: from pytorch_adapt.layers import DoNothingOptimizer
eval_hook = MCDHook(g_opts=[DoNothingOptimizer()], c_opts=[DoNothingOptimizer()])
generator.eval()
clf1.eval()
clf2.eval()
outputs, losses = eval_hook({**models, **data}) |
Hi I installed pytorch-adapt with pip.
But when I tried
from pytorch_adapt.datasets import (
CombinedSourceAndTargetDataset,
SourceDataset,
TargetDataset,
)
"No module named 'pytorch_adapt'" occured.
My python version is 3.9.5.
Thank you.
The text was updated successfully, but these errors were encountered: