URLError: <urlopen error [WinError 10054] An existing connection was forcibly closed by the remote host>

48 Views Asked by At

I'm currently following along the PyTorch Tutorial here. As I call fasterrcnn_resnet50_fpn(), I'm getting the following error:

URLError: <urlopen error [WinError 10054] An existing connection was forcibly closed by the remote host>

I first checked the websitetext where the call was trying to download the weights from and indeed there's a connection reset error. I ran windows diagnostics and found that the remote device won't accept the connection. I tried downloading the weights manually and loading the weight path directly but I couldn't get the call to stop downloading the weights from the website.

# load a model pre-trained on COCO
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights_path='./fasterrcnn_resnet50_fpn_coco-258fb6c6.pth', )

# replace the classifier with a new one, that has num classes which is 2 
num_classes = 2 # 1 class (person) + background

# get number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features

# replace the pre-trained head with a new one
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
1

There are 1 best solutions below

0
Qamil Mirza On

Answering my own question, I found a workaround involving some brute force where we try to connect repeatedly. This took a few tries, but it finally worked. I still do not know why this is happening, but for now, this temporary fix enables the model to be downloaded to the local PyTorch Cache. The code is as follows:

# DEFINE THE MODEL
# our model will predict the bounding boxes and class scores for each object in the image
# We will use a pre-trained Faster R-CNN model available from torchvision
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
import time

# load a model pre-trained on COCO
# Since I get a connection reset error, I have to implement the following workaround
def retry_open_connection():
    max_retries = 5
    retries = 0
    while retries < max_retries:
        try:
            model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
            return model
        except Exception as e:
            print(f"Error: {e}")
            print("Retrying...")
            retries += 1
            time.sleep(1)  # Wait for 1 second before retrying

model = retry_open_connection()