Skip to content

Commit

Permalink
avoid random lib usage (#2344)
Browse files Browse the repository at this point in the history
  • Loading branch information
eaidova authored Sep 2, 2024
1 parent 593da6b commit c81e765
Show file tree
Hide file tree
Showing 17 changed files with 527 additions and 86 deletions.
510 changes: 458 additions & 52 deletions notebooks/ct-segmentation-quantize/ct-segmentation-quantize-nncf.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -62,6 +63,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -93,6 +95,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -107,7 +110,6 @@
"outputs": [],
"source": [
"import os\n",
"import random\n",
"import zipfile\n",
"from pathlib import Path\n",
"\n",
Expand All @@ -131,6 +133,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -139,6 +142,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -211,12 +215,13 @@
"print(f\"Number of images to get explanations: {len(img_files)}\")\n",
"\n",
"# Get a fewer subset for fast execution\n",
"random.seed(42)\n",
"img_files = random.sample(img_files, 1)\n",
"np.random.seed(42)\n",
"img_files = np.random.choice(img_files, 1)\n",
"print(f\"Run explanations on fewer number of images: {len(img_files)}\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -257,6 +262,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -265,6 +271,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -302,6 +309,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -322,6 +330,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -330,6 +339,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -377,6 +387,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -385,6 +396,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -393,6 +405,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -432,6 +445,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -440,6 +454,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -490,6 +505,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -498,6 +514,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -527,6 +544,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -554,6 +572,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -562,6 +581,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -668,6 +688,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -796,6 +817,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -820,6 +842,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -828,6 +851,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -871,6 +895,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -879,6 +904,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -892,6 +918,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -900,6 +927,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -928,6 +956,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -939,6 +968,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -967,13 +997,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"`True positive low confidence` basically means that key features are not well available or are transformed. From the saliency maps, we see that the model is paying attention to the whole object, trying to make a decision mostly based on high-level features."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -1002,6 +1034,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -1021,6 +1054,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -1049,6 +1083,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down
13 changes: 9 additions & 4 deletions notebooks/florence2/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import io
import copy
import random
import requests
from pathlib import Path

Expand Down Expand Up @@ -68,8 +67,13 @@ def draw_polygons(image, prediction, fill_mask=False):
draw = ImageDraw.Draw(image)
scale = 1
for polygons, label in zip(prediction["polygons"], prediction["labels"]):
color = random.choice(colormap)
fill_color = random.choice(colormap) if fill_mask else None
color_id = np.random.choice(len(colormap))
color = colormap[color_id]
if fill_mask:
fill_color_id = np.random.choice(len(colormap))
fill_color = colormap[fill_color_id]
else:
fill_color = None
for _polygon in polygons:
_polygon = np.array(_polygon).reshape(-1, 2)
if len(_polygon) < 3:
Expand All @@ -96,7 +100,8 @@ def draw_ocr_bboxes(image, prediction):
draw = ImageDraw.Draw(image)
bboxes, labels = prediction["quad_boxes"], prediction["labels"]
for box, label in zip(bboxes, labels):
color = random.choice(colormap)
color_id = np.random.choice(len(colormap))
color = colormap[color_id]
new_box = (np.array(box) * scale).tolist()
draw.polygon(new_box, width=3, outline=color)
draw.text((new_box[0] + 8, new_box[1] + 2), "{}".format(label), align="right", fill=color)
Expand Down
3 changes: 1 addition & 2 deletions notebooks/flux.1-image-generation/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import gradio as gr
import numpy as np
import random
import torch

MAX_SEED = np.iinfo(np.int32).max
Expand All @@ -23,7 +22,7 @@
def make_demo(ov_pipe):
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidance_scale=0, progress=gr.Progress(track_tqdm=True)):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
seed = np.random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
image = ov_pipe(
prompt=prompt, width=width, height=height, num_inference_steps=num_inference_steps, generator=generator, guidance_scale=guidance_scale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,13 +966,12 @@
"outputs": [],
"source": [
"def draw_mask(mask, draw, random_color=False):\n",
" import random\n",
"\n",
" if random_color:\n",
" color = (\n",
" random.randint(0, 255),\n",
" random.randint(0, 255),\n",
" random.randint(0, 255),\n",
" np.random.randint(0, 255),\n",
" np.random.randint(0, 255),\n",
" np.random.randint(0, 255),\n",
" 153,\n",
" )\n",
" else:\n",
Expand Down
3 changes: 1 addition & 2 deletions notebooks/instant-id/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pathlib import Path
from typing import Callable
import gradio as gr
import random
import numpy as np
from diffusers.utils import load_image
from style_template import styles
Expand Down Expand Up @@ -32,7 +31,7 @@

def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED)
seed = np.random.randint(0, MAX_SEED)
return seed


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@
" batch_size = input_ids.shape[0]\n",
" if not self.stateful:\n",
" for input_name in self.key_value_input_names:\n",
" model_inputs = self.modeget_anyres_image_grid_shapel.input(input_name)\n",
" model_inputs = self.model.input(input_name)\n",
" shape = model_inputs.get_partial_shape()\n",
" shape[0] = batch_size\n",
" if shape[2].is_dynamic:\n",
Expand Down
Loading

0 comments on commit c81e765

Please sign in to comment.