wyctorfogos commited on
Commit
889e8f5
·
1 Parent(s): 46451dd

fix: Ajuste no resiza da imagem do heatmap e do app.py

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. app.py +2 -3
  3. dockerfile +14 -9
  4. requirements.txt +2 -0
  5. src/models/inference.py +18 -3
.gitignore CHANGED
@@ -16,3 +16,4 @@ env/
16
  .venv/
17
  pip-log.txt
18
  pip-delete-this-directory.txt
 
 
16
  .venv/
17
  pip-log.txt
18
  pip-delete-this-directory.txt
19
+ .codex
app.py CHANGED
@@ -14,11 +14,10 @@ from main import demo
14
 
15
  if __name__ == "__main__":
16
  port = int(os.environ.get("PORT", 7860))
17
-
18
- logging.info(f"Starting app on port {port}")
19
 
20
  demo.launch(
21
  server_name="0.0.0.0",
22
  server_port=port,
23
- share=True
24
  )
 
14
 
15
  if __name__ == "__main__":
16
  port = int(os.environ.get("PORT", 7860))
17
+ logging.info("Starting app on port %s", port)
 
18
 
19
  demo.launch(
20
  server_name="0.0.0.0",
21
  server_port=port,
22
+ share=False,
23
  )
dockerfile CHANGED
@@ -3,12 +3,16 @@ FROM python:3.10-slim
3
  ENV PYTHONDONTWRITEBYTECODE=1 \
4
  PYTHONUNBUFFERED=1 \
5
  PIP_NO_CACHE_DIR=1 \
6
- HF_HOME=/data/.huggingface \
7
- PORT=7860
 
 
 
 
8
 
9
  RUN useradd -m -u 1000 user
10
 
11
- WORKDIR /app
12
 
13
  RUN apt-get update && apt-get install -y --no-install-recommends \
14
  build-essential \
@@ -22,15 +26,16 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
22
  libgomp1 \
23
  && rm -rf /var/lib/apt/lists/*
24
 
25
- COPY --chown=user requirements.txt /app/requirements.txt
26
- RUN pip install --upgrade pip setuptools wheel && \
27
- pip install -r /app/requirements.txt
28
 
29
- COPY --chown=user . /app
30
 
31
  USER user
32
- ENV HOME=/home/user \
33
- PATH=/home/user/.local/bin:$PATH
 
 
 
34
 
35
  EXPOSE 7860
36
 
 
3
  ENV PYTHONDONTWRITEBYTECODE=1 \
4
  PYTHONUNBUFFERED=1 \
5
  PIP_NO_CACHE_DIR=1 \
6
+ PORT=7860 \
7
+ HOME=/home/user \
8
+ PATH=/home/user/.local/bin:$PATH \
9
+ HF_HOME=/tmp/.huggingface \
10
+ TRANSFORMERS_CACHE=/tmp/.cache/huggingface/transformers \
11
+ HUGGINGFACE_HUB_CACHE=/tmp/.cache/huggingface/hub
12
 
13
  RUN useradd -m -u 1000 user
14
 
15
+ WORKDIR $HOME/app
16
 
17
  RUN apt-get update && apt-get install -y --no-install-recommends \
18
  build-essential \
 
26
  libgomp1 \
27
  && rm -rf /var/lib/apt/lists/*
28
 
29
+ RUN mkdir -p /tmp/.huggingface /tmp/.cache/huggingface/transformers /tmp/.cache/huggingface/hub
 
 
30
 
31
+ COPY --chown=user requirements.txt $HOME/app/requirements.txt
32
 
33
  USER user
34
+
35
+ RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
36
+ pip install --no-cache-dir -r $HOME/app/requirements.txt
37
+
38
+ COPY --chown=user . $HOME/app
39
 
40
  EXPOSE 7860
41
 
requirements.txt CHANGED
@@ -1,3 +1,5 @@
 
 
1
  torch==2.4.1
2
  torchvision==0.19.1
3
  optuna==4.2.1
 
1
+ fastapi
2
+ uvicorn[standard]
3
  torch==2.4.1
4
  torchvision==0.19.1
5
  optuna==4.2.1
src/models/inference.py CHANGED
@@ -315,9 +315,24 @@ def run_inference(image_pil, metadata_text, model_key=None):
315
  pred_class
316
  )
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  fig, ax = plt.subplots(figsize=(6, 6))
319
- ax.imshow(image_pil)
320
- ax.imshow(heatmap, cmap="jet", alpha=0.4)
321
  ax.axis("off")
322
 
323
  title = f"{CLASS_LIST[pred_class]} | conf={confidence:.3f}"
@@ -328,4 +343,4 @@ def run_inference(image_pil, metadata_text, model_key=None):
328
  plt.close(fig)
329
 
330
  print(f"[inference] inference complete: {title}")
331
- return result, title
 
315
  pred_class
316
  )
317
 
318
+ image_np = np.array(image_pil.convert("RGB"))
319
+ heatmap = np.asarray(heatmap, dtype=np.float32).squeeze()
320
+
321
+ if heatmap.shape != image_np.shape[:2]:
322
+ heatmap_tensor = torch.from_numpy(heatmap).unsqueeze(0).unsqueeze(0)
323
+ heatmap = torch.nn.functional.interpolate(
324
+ heatmap_tensor,
325
+ size=image_np.shape[:2],
326
+ mode="bilinear",
327
+ align_corners=False,
328
+ ).squeeze().cpu().numpy()
329
+
330
+ heatmap = np.clip(heatmap, 0.0, 1.0)
331
+ alpha_map = np.clip(heatmap * 0.6, 0.0, 0.6)
332
+
333
  fig, ax = plt.subplots(figsize=(6, 6))
334
+ ax.imshow(image_np)
335
+ ax.imshow(heatmap, cmap="jet", alpha=alpha_map)
336
  ax.axis("off")
337
 
338
  title = f"{CLASS_LIST[pred_class]} | conf={confidence:.3f}"
 
343
  plt.close(fig)
344
 
345
  print(f"[inference] inference complete: {title}")
346
+ return result, title