@torch.no_grad()
def run_model(batch: List[torch.Tensor], model, hook, classes: Tuple[(str, ...)], labels_IO: np.ndarray, labels_attribute: List[str], W_attribute: np.ndarray, num_cat: int, num_attr: int, weight_softmax: Optional[np.ndarray]=None) -> List[Dict]:
    (listing_ids, photo_ids, input_img) = batch
    logit = model.forward(input_img.cuda())
    h_x = F.softmax(logit, 1)
    detections = []
    for (i, p) in enumerate(h_x):
        listing_id = int(listing_ids[i])
        photo_id = int(photo_ids[i])
        key = get_key(listing_id, photo_id)
        (probs, idx) = p.sort(0, True)
        probs = probs.detach().cpu().numpy()
        idx = idx.detach().cpu().numpy()
        category = [(probs[j], classes[idx[j]]) for j in range(0, num_cat)]
        ft = [np.squeeze(f[i]) for f in hook.features]
        responses_attribute = softmax(W_attribute.dot(ft[1]))
        idx_a = np.argsort(responses_attribute)
        attributes = [(responses_attribute[idx_a[j]], labels_attribute[idx_a[j]]) for j in range((- 1), (- num_attr), (- 1))]
        detections.append({'listing_id': listing_id, 'photo_id': photo_id, 'category': category, 'attributes': attributes, 'is_indoor': is_indoor(idx, labels_IO)})
        if (weight_softmax is not None):
            ca_map = class_activation_map(ft[0], weight_softmax, [idx[0]])[0]
            img = NormalizeInverse()(input_img[i])
            (height, width, _) = img.shape
            heatmap = cv2.applyColorMap(cv2.resize(ca_map, (width, height)), cv2.COLORMAP_JET)
            result = ((heatmap * 0.4) + (img * 0.5))
            cv2.imwrite(f'examples/{key}-heatmap.jpg', result)
            cv2.imwrite(f'examples/{key}-image.jpg', img[:, :, ::(- 1)])
    hook.reset()
    return detections
@torch.no_grad()
def run_model(batch: List[torch.Tensor], model, hook, classes: Tuple[(str, ...)], labels_IO: np.ndarray, labels_attribute: List[str], W_attribute: np.ndarray, num_cat: int, num_attr: int, weight_softmax: Optional[np.ndarray]=None) -> List[Dict]:
    (listing_ids, photo_ids, input_img) = batch
    logit = model.forward(input_img.cuda())
    h_x = F.softmax(logit, 1)
    detections = []
    for (i, p) in enumerate(h_x):
        listing_id = int(listing_ids[i])
        photo_id = int(photo_ids[i])
        key = get_key(listing_id, photo_id)
        (probs, idx) = p.sort(0, True)
        probs = probs.detach().cpu().numpy()
        idx = idx.detach().cpu().numpy()
        category = [(probs[j], classes[idx[j]]) for j in range(0, num_cat)]
        ft = [np.squeeze(f[i]) for f in hook.features]
        responses_attribute = softmax(W_attribute.dot(ft[1]))
        idx_a = np.argsort(responses_attribute)
        attributes = [(responses_attribute[idx_a[j]], labels_attribute[idx_a[j]]) for j in range((- 1), (- num_attr), (- 1))]
        detections.append({'listing_id': listing_id, 'photo_id': photo_id, 'category': category, 'attributes': attributes, 'is_indoor': is_indoor(idx, labels_IO)})
        if (weight_softmax is not None):
            ca_map = class_activation_map(ft[0], weight_softmax, [idx[0]])[0]
            img = NormalizeInverse()(input_img[i])
            (height, width, _) = img.shape
            heatmap = cv2.applyColorMap(cv2.resize(ca_map, (width, height)), cv2.COLORMAP_JET)
            result = ((heatmap * 0.4) + (img * 0.5))
            cv2.imwrite(f'examples/{key}-heatmap.jpg', result)
            cv2.imwrite(f'examples/{key}-image.jpg', img[:, :, ::(- 1)])
    hook.reset()
    return detections