Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified handlers/svg-open-street-map/requirements.txt
Binary file not shown.
9 changes: 4 additions & 5 deletions preprocessors/ner/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
FROM ubuntu:20.04
FROM python:3.11-bookworm

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
ENV JAVA_HOME=/usr/lib/jvm/default-java

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y \
openjdk-8-jdk \
python3-pip \
gcc \
build-essential \
default-jre-headless \
git \
curl \
&& apt-get clean && \
Expand Down
25 changes: 14 additions & 11 deletions preprocessors/ner/clipscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
import clip
import torch
from PIL import Image
from sklearn.preprocessing import normalize
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize
import torch
import tqdm
import numpy as np
import sklearn.preprocessing
import collections
import os
import pathlib
Expand All @@ -35,6 +32,12 @@
from pycocoevalcap.spice.spice import Spice


def normalize_rows(values: np.ndarray) -> np.ndarray:
"""Normalize a 2D float array row-wise while leaving zero-norm rows as zeros."""
norms = np.linalg.norm(values, axis=1, keepdims=True)
return np.divide(values, norms, out=np.zeros_like(values), where=norms != 0)


def get_all_metrics(refs, cands, return_per_cap=False):
metrics = []
names = []
Expand Down Expand Up @@ -212,14 +215,14 @@ def get_clip_score(model, images, candidates, device, w=2.5):

#as of numpy 1.21, normalize doesn't work properly for float16
if version.parse(np.__version__) < version.parse('1.21'):
images = sklearn.preprocessing.normalize(images, axis=1)
candidates = sklearn.preprocessing.normalize(candidates, axis=1)
images = normalize_rows(images)
candidates = normalize_rows(candidates)
else:
warnings.warn(
'due to a numerical instability, new numpy normalization is slightly different than paper results. '
'to exactly replicate paper results, please use numpy version less than 1.21, e.g., 1.20.3.')
images = images / np.sqrt(np.sum(images**2, axis=1, keepdims=True))
candidates = candidates / np.sqrt(np.sum(candidates**2, axis=1, keepdims=True))
images = normalize_rows(images)
candidates = normalize_rows(candidates)

per = w*np.clip(np.sum(images * candidates, axis=1), 0, None)
return np.mean(per), per, candidates
Expand All @@ -241,15 +244,15 @@ def get_refonlyclipscore(model, references, candidates, device):
flattened_refs = extract_all_captions(flattened_refs, model, device)

if version.parse(np.__version__) < version.parse('1.21'):
candidates = sklearn.preprocessing.normalize(candidates, axis=1)
flattened_refs = sklearn.preprocessing.normalize(flattened_refs, axis=1)
candidates = normalize_rows(candidates)
flattened_refs = normalize_rows(flattened_refs)
else:
warnings.warn(
'due to a numerical instability, new numpy normalization is slightly different than paper results. '
'to exactly replicate paper results, please use numpy version less than 1.21, e.g., 1.20.3.')

candidates = candidates / np.sqrt(np.sum(candidates**2, axis=1, keepdims=True))
flattened_refs = flattened_refs / np.sqrt(np.sum(flattened_refs**2, axis=1, keepdims=True))
candidates = normalize_rows(candidates)
flattened_refs = normalize_rows(flattened_refs)

cand_idx2refs = collections.defaultdict(list)
for ref_feats, cand_idx in zip(flattened_refs, flattened_refs_idxs):
Expand Down
11 changes: 5 additions & 6 deletions preprocessors/ner/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ bs4==0.0.1
imgkit==1.2.2
nltk==3.7
gunicorn==23.0.0
pillow==12.1.1
torch==1.13.1
pillow==12.2.0
torch==2.6.0
tqdm~=4.66.3
torchvision==0.8.2
clip-by-openai==1.1
scikit-learn==1.1.2
torchvision==0.21.0
packaging==21.3
jsonschema==4.23.0
Flask==3.1.3
pycocoevalcap==1.2
git+https://github.com/openai/CLIP
# Pin the tested CLIP commit used for the Python 3.11-compatible NER build.
git+https://github.com/openai/CLIP@d05afc436d78f1c48dc0dbf8e5980a9d471f35f6
23 changes: 23 additions & 0 deletions preprocessors/ner/test_clipscore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

import numpy as np

from clipscore import normalize_rows


class NormalizeRowsTest(unittest.TestCase):
def test_normalizes_rows(self):
values = np.array([[3.0, 4.0], [5.0, 12.0]])
normalized = normalize_rows(values)
expected = np.array([[0.6, 0.8], [5.0 / 13.0, 12.0 / 13.0]])
np.testing.assert_allclose(normalized, expected)

def test_preserves_zero_rows(self):
values = np.array([[0.0, 0.0], [0.0, 2.0]])
normalized = normalize_rows(values)
expected = np.array([[0.0, 0.0], [0.0, 1.0]])
np.testing.assert_allclose(normalized, expected)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion services/espnet-tts-fr/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
espnet==202207
espnet_model_zoo==0.1.7
Flask==3.1.3
Flask==2.2.5
gunicorn==23.0.0
jsonschema==4.4.0
parallel_wavegan==0.5.5
Expand Down
2 changes: 1 addition & 1 deletion services/espnet-tts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
espnet==0.10.0
espnet_model_zoo==0.1.7
Flask==3.1.3
Flask==2.2.5
gunicorn==23.0.0
jsonschema==4.4.0
parallel_wavegan==0.5.4
Expand Down