简体   繁体   中英

AttributeError: 'Compose' object has no attribute 'Compose' (in Pytorch 0.2.1)

This is the block of code where am getting this error:

train_transforms = transforms.Compose([transforms.RandomRotation(30),
                                       transforms.RandomResizedCrop(224),
                                       transforms.RandomHorizontalFlip(),
                                       transforms.ToTensor(),
                                       transforms.Normalize([0.5, 0.5, 0.5], 
                                                            [0.5, 0.5, 0.5])])

I've tried updating my torchvision but had no luck!

The problem is that you have a variable called transforms after from torchvision import transforms which has a compose of a certain type. This override the transform you import from the torchvison . Therefore when you run the above code it calls the transforms which is a variable not the one from torchvision module.

It is advisable to rename the variable or if you are using jupyter notebook run the cell where you import transforms before running the cell with the code above.

    train_transforms = torchvision.transforms.Compose([torchvision.transforms.RandomRotation(30), enter code here                                      torchvision.transforms.RandomResizedCrop(224),                                       torchvision.transforms.RandomHorizontalFlip(),
                                   torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize([0.5, 0.5, 0.5], 
                                                        [0.5, 0.5, 0.5])])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM