Awesome-panel DETR App not working - pn.serve with num_procs > 0 and shared state - how to fix?

At i’m running the Facebook DE:TR: app.

After starting to pn.serve with num_procs=4 my app has started crashing when I or a user start and navigate to the DE:TR: app.

The first time I go to the DE:TR: it loads fast, second time slower and third or fourth time it does not respond.

My hypothesis is that it is because I have some code I run when the DE:TR: model module is imported . I.e. it’s run once before I pn.serve my app.

# Load model

print("Loading DETR model")
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
detr = torch.hub.load("facebookresearch/detr", "detr_resnet50", pretrained=True)

# standard PyTorch mean-std input image normalization
transform = T.Compose(
    [T.Resize(500), T.ToTensor(), T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]
print("Loading DETR model...DONE")

The same detr, DEVICE and transform is then available to and shared across all sessions.

I do this because the code is slow and I would like to avoid having the user wait each time he/ she navigates to the page.

HOW DO I FIX THIS? DO I NEED TO LOAD THESE FOR EACH USER/ SESSION? Or can I safely load or share these in some way? How?

Additional Context

The DE:TR: app can be found here The python code above is in the file.

The file can be found here

I’ve refactored so that detr, DEVICEand transform is loaded every time a user loads the DETR page for the first time.

This makes the server more stable (i.e. not crashing). But if I open 4 tabs and load the DETR page in each them (in serial) it gets slower and slower.

But if “refresh” the pages and start from the home page it seems I get the speed back again.