Simple producer-consumer problem causing 100% CPU usage

The below code is causing 100% CPU usage.
There’s no GPU.

import random
from queue import Queue
from threading import Thread

import torch
from torchvision.models import resnet18
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

model = resnet18(pretrained=True)
model.fc = torch.nn.Identity()

#list of 50 image tensors of varying sizes
ims = [torch.randn(1,3,random.randint(224,512),random.randint(224,512)) for i in range(50)]

def producer(out_q):

    for im_tensor in ims:
        with torch.no_grad():
            feat_vector = model(im_tensor).detach().numpy()
        out_q.put(feat_vector)

def consumer(in_q):
    feat_vector1 = in_q.get()
    feat_vector2 = np.random.randn(1,512)
    sim_score = cosine_similarity(feat_vector1,feat_vector2)[0][0]
    in_q.task_done()
    return sim_score

for i in range(50):
    q = Queue()
    t1 = Thread(target = consumer, args =(q, ))
    t2 = Thread(target = producer, args =(q, ))
    t1.start()
    t2.start()
    q.join()

How to avoid the CPU cranking up?

You are not using the GPU, so a high CPU workload is expected.
This tutorial explains how to use the GPU by pushing the model as well as the tensors to the device.