Error in torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train) -- urllib.error.URLError: <urlopen error [Errno -3]

This code runs with no problem in local machine with RTX 3080. When I run it in Azure Cluster using MLOps Pipeline Templates, I get this error:

import time
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms

start_time = time.time()
# Define the transforms for the dataset
transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))

transform_test = transforms.Compose([
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))

# Load the CIFAR-10 dataset
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
trainloader =, batch_size=128, shuffle=True, num_workers=4)

testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader =, batch_size=128, shuffle=False, num_workers=4)

# Define the ResNet50 model
model = torchvision.models.resnet50(pretrained=True)
num_features = model.fc.in_features
model.fc = nn.Linear(num_features, 10)

# Move the model to the GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Train the model for 100 epochs
for epoch in range(2):
# for epoch in range(100):
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data
        inputs, labels =,


        outputs = model(inputs)
        loss = criterion(outputs, labels)

        running_loss += loss.item()

    print('[Epoch %d] loss: %.3f' % (epoch + 1, running_loss / len(trainloader)))

# Evaluate the model on the test set
correct = 0
total = 0
with torch.no_grad():
    for data in testloader:
        inputs, labels = data
        inputs, labels =,

        outputs = model(inputs)
        _, predicted = torch.max(, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Test accuracy: %.2f %%' % (100 * correct / total))

end_time = time.time()

elapsed_time = start_time - end_time

print('elapsed time: ', elapsed_time)


Traceback (most recent call last):
  File "/opt/conda/lib/python3.8/urllib/", line 1354, in do_open
    h.request(req.get_method(), req.selector,, headers,
  File "/opt/conda/lib/python3.8/http/", line 1256, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/opt/conda/lib/python3.8/http/", line 1302, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/opt/conda/lib/python3.8/http/", line 1251, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/opt/conda/lib/python3.8/http/", line 1011, in _send_output
  File "/opt/conda/lib/python3.8/http/", line 951, in send
  File "/opt/conda/lib/python3.8/http/", line 1418, in connect
  File "/opt/conda/lib/python3.8/http/", line 922, in connect
    self.sock = self._create_connection(
  File "/opt/conda/lib/python3.8/", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/opt/conda/lib/python3.8/", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 23, in <module>
    trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/datasets/", line 65, in __init__
  File "/opt/conda/lib/python3.8/site-packages/torchvision/datasets/", line 141, in download
    download_and_extract_archive(self.url, self.root, filename=self.filename, md5=self.tgz_md5)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/datasets/", line 430, in download_and_extract_archive
    download_url(url, download_root, filename, md5)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/datasets/", line 131, in download_url
    url = _get_redirect_url(url, max_hops=max_redirect_hops)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/datasets/", line 79, in _get_redirect_url
    with urllib.request.urlopen(urllib.request.Request(url, headers=headers)) as response:
  File "/opt/conda/lib/python3.8/urllib/", line 222, in urlopen
    return, data, timeout)
  File "/opt/conda/lib/python3.8/urllib/", line 525, in open
    response = self._open(req, data)
  File "/opt/conda/lib/python3.8/urllib/", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/opt/conda/lib/python3.8/urllib/", line 502, in _call_chain
    result = func(*args)
  File "/opt/conda/lib/python3.8/urllib/", line 1397, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/opt/conda/lib/python3.8/urllib/", line 1357, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>

fixed by adding these lines:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context