Wasim
Wasim

Reputation: 31

What does it mean to assert an object in python?

We are getting an assertion error in this line "assert dataset". We are printing the dataset object and the value we got was this '<datasets.TextDataset object at 0x000002531F10E408>'. We are using 'python 3.7' in this code. Why are we getting assertion error on dataset object?

We are basically trying to run the code of AttnGAN (https://github.com/taoxugit/AttnGAN). The error is happening on Line: 130 in 'code/main.py'.

Code

dataset = TextDataset(cfg.DATA_DIR, split_dir, base_size=cfg.TREE.BASE_SIZE, transform=image_transform)

print(dataset)

assert dataset

dataloader = torch.utils.data.DataLoader(dataset, batch_size=cfg.TRAIN.BATCH_SIZE, drop_last=True, shuffle=bshuffle, num_workers=int(cfg.WORKERS))

Output

Load from: C:\Users\admin\Desktop\TextToImage\AttnGAN-master (1)\AttnGAN-master\code/../data/birds/captions.pickle

<datasets.TextDataset object at 0x000002531F10E408>

Traceback (most recent call last): File "./code/main.py", line 131, in assert dataset

AssertionError

PS C:\Users\admin\Desktop\TextToImage\AttnGAN-master (1)\AttnGAN-master>

Upvotes: 0

Views: 784

Answers (1)

Alex I
Alex I

Reputation: 20317

In this case, assert dataset is a not-very-clear way of checking if the dataset is empty. assert throws an exception if the expression (in this case the dataset object) evaluates to false.

https://docs.python.org/3/library/stdtypes.html "Truth Value Testing" says

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero

Loking at the github repo, TextDataset does define __len__(). The logical conclusion is that the returned length of the dataset in your case (after it is loaded) is zero.

Try to look at where it is loading data from, try to make sure the data is there, and print the length before the assertion. Bonus: try to figure out why the original loading doesn't throw an exception but succeeds and produces an empty dataset.

Upvotes: 1

Related Questions