PyTorch can easily switch between cpu and gpu modes for tensors with `` hoge.to (device)
`, etc., but I often don't know if this dataset or model is cpu or gpu, so check it. I will write down how to do it.
As a prerequisite, data set and model preparation
IMAGE_SIZE=224
BATCH_SIZE=20
TRAIN = 'train'
DATA_DIR = 'dataset/predata/'
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
data_transforms = {
TRAIN: transforms.Compose([
transforms.Resize(IMAGE_SIZE),
transforms.ToTensor(),
])
}
#Data preprocessing
image_datasets = {x: datasets.ImageFolder(os.path.join(DATA_DIR, x), data_transforms[x]) for x in [TRAIN]}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x],
batch_size=BATCH_SIZE, shuffle=True, num_workers=4) for x in [TRAIN]}
is_If you use cuda, it will be returned as a bool type.[here](https://discuss.pytorch.org/t/how-to-check-if-model-is-on-cuda/180/2?u=kazuki_hamaguchi)(1)With reference to
```python
data, label = iter(dataloaders[TRAIN]).next()
data = data.to(DEVICE)
label = label.to(DEVICE)
print(data.is_cuda)
print(label.is_cuda)
model = model.to(DEVICE)
print(next(model.parameters()).is_cuda)
You can check it with this.
I wonder what happens to the mode before and after exiting the local variable. It might be interesting to check
(1) How to check if Model is on cuda
Recommended Posts