-
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 do I change a few things in the implementation? #81
Comments
I've created this Google Colab notebook that you can run: https://colab.research.google.com/drive/109TaIdhLGu7LLFvm5gZ401tYYsKJrnNP?usp=sharing It's kind of like the existing example notebooks, but with fewer helper functions. That is, there's more boilerplate, but you get to see more clearly what's going on. The "Datasets" and "Validators" sections might be most relevant to you. Let me know if you need help with any specific parts. And if you want less boilerplate, I can show you which pytorch-adapt functions to use. |
Thanks a ton for your reply. I was able to understand this one much better. But however, using the code in https://colab.research.google.com/drive/109TaIdhLGu7LLFvm5gZ401tYYsKJrnNP?usp=sharing, I found that there was an error which says " object of type 'type' has no len()" when I copy/pasted this code : from torchvision.datasets import MNIST num_classes = 10 src_dataset = SourceDataset(src_dataset) #THE ISSUE IS IN THE SECOND LINE OF THIS BLOCK OF CODE into the "Datasets" cell of the same colab. Can you shed some more light as to how I customize the datasets (source, target)? I can not thank you enough for your time, but it is very helpful for me since I am working on my masters thesis in UDA, and plan to use this repository. |
I think it's because you haven't initialized the datasets. Change this: src_dataset = MNIST
target_dataset = SVHN to this: src_dataset = MNIST(root="mnist", download=True)
target_dataset = SVHN(root="svhn", download=True) |
Great! I am finally able to load datasets. But however in this case, this error comes up in the "Traning+Evaluation" code block : "TypeError: cannot convert dictionary update sequence element #0 to a sequence". |
I request you to give an implementation of an algorithm with any two datasets as template (say MNIST and SVHN in my case) so that we can better appreciate and understand the code. Thank you again and eagerly waiting! |
Good idea! I've updated the notebook to run on MNIST and SVHN: https://colab.research.google.com/drive/109TaIdhLGu7LLFvm5gZ401tYYsKJrnNP?usp=sharing You might want to start with a model that is already trained on the source dataset, instead of a randomly initialized model. If you're using MNIST as the source dataset, you can use these pretrained models: from pytorch_adapt.models import mnistC, mnistG
generator = mnistG(pretrained=True).to(device)
classifier = mnistC(pretrained=True).to(device) |
Thank you! That was indeed very helpful. |
You have to pass in a separate list of optimizers for the discriminator and generator steps. Every optimizer in each list gets called at each iteration. Here's how to use GANHook (with MCCHook): from pytorch_adapt.hooks import GANHook
discriminator = torch.nn.Linear(1200, 1).to(device)
model_dict = {"G": generator, "C": classifier, "D": discriminator}
d_optimizer = torch.optim.SGD(discriminator.parameters(), lr=0.001)
d_opts = [d_optimizer]
g_opts = [optimizer]
hook = GANHook(d_opts=d_opts, g_opts=g_opts, post_g=[MCCHook()]) |
This notebook might also help: https://github.com/KevinMusgrave/pytorch-adapt/blob/main/examples/getting_started/PaperImplementationsAsHooks.ipynb It shows how a bunch of different hooks are initialized. |
I hope you can give me a few days to figure the hooks out before closing this issue. Right now I'm studying your datasets and validators library.
worked out almost fine but had an error in the training loop which said: |
Ah I forgot to flatten the output of the discriminator: discriminator = torch.nn.Sequential(torch.nn.Linear(1200, 1), torch.nn.Flatten(start_dim=0)).to(device) |
Hi, I am trying to run the code in your colab notebook on my local GPU. But for some reason, .to_device() does not seem to work and it always gives me "Torch not compiled with CUDA enabled error". I removed the .to_device() from MNISTFeatures().to_device and the generator.to_device() and it seemed to fix the problem for that block of code. |
Do you have an Nvidia GPU? How did you install PyTorch? |
Yes. GTX1050… Installed the latest pytorch using pip |
I haven't run into this issue before. Maybe this will help: pytorch/pytorch#30664 (comment) |
So when you copy that notebook (PA Issue 81) into a py file, and run it on your GPU, it runs without any errors? |
I haven't run that specific notebook, but I'm using pytorch everyday. Using You can test pytorch alone by looking at some of the tutorials. For example, just test the following (without the if statement) and see if it works: https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#tensor-operations |
Hi @KevinMusgrave, excellent work maintaining this repository! I want to run some of these algorithms on a custom dataset as well, but the PaperImplementationsAsHooks notebook is quite confusing to me. I don’t know how |
@coder-chica97 Here's a copy of the "PA Issue 81" notebook, but using a ResNet model and a suitable dataset: https://colab.research.google.com/drive/1MU9uSxZBU483B1_KQlO8_xP165oyj_nB?usp=sharing The only changes are in the "Datasets" and "Model + Optimizer" section. |
@coder-chica97 The "PaperImplementationsAsHooks" is meant to show the data and model requirements for each algorithm. For example, at each iteration, the DANN algorithm requires:
These need to be passed to the hook in a single dictionary. In the notebook, this is done by unpacking the model dictionary and data dictionary: A similar thing is done in the "PA Issue 81 ResNet" notebook, in the "Training + Evaluation" section: for data in tqdm(dataloaders["train"]):
data = batch_to_device(data, device)
_, loss = hook({**model_dict, **data}) The reason why # pytorch-adapt wrappers
src_dataset = SourceDataset(real_dataset)
target_dataset = TargetDataset(clipart_dataset)
target_dataset_with_labels = TargetDataset(clipart_dataset, supervised=True)
dataset = CombinedSourceAndTargetDataset(src_dataset, target_dataset)
# create dataloaders
val_names = ["src_train", "target_train", "target_train_with_labels"]
dc = DataloaderCreator(batch_size=16, num_workers=2, val_names=val_names)
dataloaders = dc(train=dataset, src_train=src_dataset, target_train=target_dataset, target_train_with_labels=target_dataset_with_labels) There are ways to simplify some of the code by using more of this library's helper functions. But if you want to be closer to "raw" pytorch, then the "PA Issue 81 ResNet" notebook is probably best. |
I would like to run on my own dataset, and also, print the accuracy on the source and target domains. In the paper implementations, I don't see any accuracy metric. Please guide the amateur learner looking at this to change the dataset to their own, and to implement accuracy terms. Thanks
The text was updated successfully, but these errors were encountered: