Skip to content

Commit feda912

Browse files
committed
Notebook format updates.
1 parent 0716c28 commit feda912

File tree

2 files changed

+31
-74
lines changed

2 files changed

+31
-74
lines changed

site/en/gemma/docs/paligemma/fine-tuning-paligemma.ipynb

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@
121121
"\n",
122122
"To generate a Kaggle API key, open your [**Settings** page in Kaggle](https://www.kaggle.com/settings) and click **Create New Token**. This triggers the download of a `kaggle.json` file containing your API credentials.\n",
123123
"\n",
124-
"Then, in Colab, select **Secrets** (🔑) in the left pane and add your Kaggle username and Kaggle API key. Store your username under the name `KAGGLE_USERNAME` and your API key under the name `KAGGLE_KEY`.\n",
125-
"\n"
124+
"Then, in Colab, select **Secrets** (🔑) in the left pane and add your Kaggle username and Kaggle API key. Store your username under the name `KAGGLE_USERNAME` and your API key under the name `KAGGLE_KEY`.\n"
126125
]
127126
},
128127
{
@@ -502,7 +501,7 @@
502501
"\n",
503502
" image = tf.constant(image)\n",
504503
" image = tf.image.resize(image, (size, size), method='bilinear', antialias=True)\n",
505-
" return image.numpy() / 127.5 - 1.0 # [0, 255]-\u003e[-1,1]\n",
504+
" return image.numpy() / 127.5 - 1.0 # [0, 255]->[-1,1]\n",
506505
"\n",
507506
"def preprocess_tokens(prefix, suffix=None, seqlen=None):\n",
508507
" # Model has been trained to handle tokenized text composed of a prefix with\n",
@@ -642,12 +641,12 @@
642641
" return f\"data:image/jpeg;base64,{image_b64}\"\n",
643642
"\n",
644643
"def render_example(image, caption):\n",
645-
" image = ((image + 1)/2 * 255).astype(np.uint8) # [-1,1] -\u003e [0, 255]\n",
644+
" image = ((image + 1)/2 * 255).astype(np.uint8) # [-1,1] -> [0, 255]\n",
646645
" return f\"\"\"\n",
647-
" \u003cdiv style=\"display: inline-flex; align-items: center; justify-content: center;\"\u003e\n",
648-
" \u003cimg style=\"width:128px; height:128px;\" src=\"{render_inline(image, resize=(64,64))}\" /\u003e\n",
649-
" \u003cp style=\"width:256px; margin:10px; font-size:small;\"\u003e{html.escape(caption)}\u003c/p\u003e\n",
650-
" \u003c/div\u003e\n",
646+
" <div style=\"display: inline-flex; align-items: center; justify-content: center;\">\n",
647+
" <img style=\"width:128px; height:128px;\" src=\"{render_inline(image, resize=(64,64))}\" />\n",
648+
" <p style=\"width:256px; margin:10px; font-size:small;\">{html.escape(caption)}</p>\n",
649+
" </div>\n",
651650
" \"\"\"\n",
652651
"\n",
653652
"html_out = \"\"\n",
@@ -764,7 +763,7 @@
764763
" # Append to html output.\n",
765764
" for example, response in zip(examples, responses):\n",
766765
" outputs.append((example[\"image\"], response))\n",
767-
" if num_examples and len(outputs) \u003e= num_examples:\n",
766+
" if num_examples and len(outputs) >= num_examples:\n",
768767
" return outputs"
769768
]
770769
},
@@ -872,36 +871,14 @@
872871
],
873872
"metadata": {
874873
"colab": {
875-
"gpuType": "T4",
876-
"last_runtime": {
877-
"build_target": "//learning/grp/tools/ml_python:ml_notebook",
878-
"kind": "private"
879-
},
880-
"private_outputs": true,
881-
"provenance": [
882-
{
883-
"file_id": "17AiK8gRY7oiquQGkBH0d08PFQo3Kyx1I",
884-
"timestamp": 1715287187925
885-
},
886-
{
887-
"file_id": "1qZlJfPyfKRrNcz2shxQ93HnnE5Ge1LLn",
888-
"timestamp": 1715019972450
889-
},
890-
{
891-
"file_id": "1JFnlD2kSiTNexdPw_NYRtuW6uuSTI0kD",
892-
"timestamp": 1714585741026
893-
}
894-
],
874+
"name": "fine-tuning-paligemma.ipynb",
895875
"toc_visible": true
896876
},
897877
"kernelspec": {
898878
"display_name": "Python 3",
899879
"name": "python3"
900-
},
901-
"language_info": {
902-
"name": "python"
903880
}
904881
},
905882
"nbformat": 4,
906883
"nbformat_minor": 0
907-
}
884+
}

site/en/gemma/docs/paligemma/inference-with-keras.ipynb

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@
4949
"source": [
5050
"# Generate PaliGemma output with Keras\n",
5151
"\n",
52-
"\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n",
53-
"\u003ctd\u003e\n",
54-
"\u003ca target=\"_blank\" href=\"https://ai.google.dev/gemma/docs/paligemma/fine-tuning-paligemma\"\u003e\u003cimg src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" /\u003eView on ai.google.dev\u003c/a\u003e\n",
55-
"\u003c/td\u003e\n",
56-
"\u003ctd\u003e\n",
57-
"\u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/gemma/docs/paligemma/inference-with-keras.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
58-
"\u003c/td\u003e\n",
59-
"\u003ctd\u003e\n",
60-
"\u003ca target=\"_blank\" href=\"https://github.com/google/generative-ai-docs/blob/main/site/en/gemma/docs/paligemma/inference-with-keras.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n",
61-
"\u003c/td\u003e\n",
62-
"\u003c/table\u003e"
52+
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
53+
"<td>\n",
54+
"<a target=\"_blank\" href=\"https://ai.google.dev/gemma/docs/paligemma/fine-tuning-paligemma\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />View on ai.google.dev</a>\n",
55+
"</td>\n",
56+
"<td>\n",
57+
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/gemma/docs/paligemma/inference-with-keras.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
58+
"</td>\n",
59+
"<td>\n",
60+
"<a target=\"_blank\" href=\"https://github.com/google/generative-ai-docs/blob/main/site/en/gemma/docs/paligemma/inference-with-keras.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
61+
"</td>\n",
62+
"</table>"
6363
]
6464
},
6565
{
@@ -278,8 +278,7 @@
278278
"* **`read_img`:** Helper method for `read_img_from_url`. This method is what actually opens the image, resizes it so that it fits in the model's constraints, and puts it into an array that can be interpreted by the model.\n",
279279
"* **`read_img_from_url`:** Takes in an image via a valid URL. You need this method to pass the image to the model.\n",
280280
"\n",
281-
"You'll use `read_img_from_url` in the next step of this notebook.\n",
282-
"\n"
281+
"You'll use `read_img_from_url` in the next step of this notebook.\n"
283282
]
284283
},
285284
{
@@ -310,8 +309,8 @@
310309
"\n",
311310
"def parse_bbox_and_labels(detokenized_output: str):\n",
312311
" matches = re.finditer(\n",
313-
" '\u003cloc(?P\u003cy0\u003e\\d\\d\\d\\d)\u003e\u003cloc(?P\u003cx0\u003e\\d\\d\\d\\d)\u003e\u003cloc(?P\u003cy1\u003e\\d\\d\\d\\d)\u003e\u003cloc(?P\u003cx1\u003e\\d\\d\\d\\d)\u003e'\n",
314-
" ' (?P\u003clabel\u003e.+?)( ;|$)',\n",
312+
" '<loc(?P<y0>\\d\\d\\d\\d)><loc(?P<x0>\\d\\d\\d\\d)><loc(?P<y1>\\d\\d\\d\\d)><loc(?P<x1>\\d\\d\\d\\d)>'\n",
313+
" ' (?P<label>.+?)( ;|$)',\n",
315314
" detokenized_output,\n",
316315
" )\n",
317316
" labels, boxes = [], []\n",
@@ -362,7 +361,7 @@
362361
" mask = Image.fromarray(mask)\n",
363362
" mask = mask.resize((x2 - x1, y2 - y1), resample=Image.NEAREST)\n",
364363
" mask = np.array(mask)\n",
365-
" binary_mask = (mask \u003e 0.5).astype(np.uint8)\n",
364+
" binary_mask = (mask > 0.5).astype(np.uint8)\n",
366365
"\n",
367366
"\n",
368367
" # Place the binary mask onto the full mask\n",
@@ -373,7 +372,7 @@
373372
" if isinstance(image, Image.Image):\n",
374373
" image = np.array(image)\n",
375374
" blended_image = image.copy()\n",
376-
" mask_indices = full_mask \u003e 0\n",
375+
" mask_indices = full_mask > 0\n",
377376
" alpha = 0.5\n",
378377
"\n",
379378
" for c in range(3):\n",
@@ -568,10 +567,10 @@
568567
"source": [
569568
"import big_vision.evaluators.proj.paligemma.transfers.segmentation as segeval\n",
570569
"reconstruct_masks = segeval.get_reconstruct_masks('oi')\n",
571-
"def parse_segments(detokenized_output: str) -\u003e tuple[np.ndarray, np.ndarray]:\n",
570+
"def parse_segments(detokenized_output: str) -> tuple[np.ndarray, np.ndarray]:\n",
572571
" matches = re.finditer(\n",
573-
" '\u003cloc(?P\u003cy0\u003e\\d\\d\\d\\d)\u003e\u003cloc(?P\u003cx0\u003e\\d\\d\\d\\d)\u003e\u003cloc(?P\u003cy1\u003e\\d\\d\\d\\d)\u003e\u003cloc(?P\u003cx1\u003e\\d\\d\\d\\d)\u003e'\n",
574-
" + ''.join(f'\u003cseg(?P\u003cs{i}\u003e\\d\\d\\d)\u003e' for i in range(16)),\n",
572+
" '<loc(?P<y0>\\d\\d\\d\\d)><loc(?P<x0>\\d\\d\\d\\d)><loc(?P<y1>\\d\\d\\d\\d)><loc(?P<x1>\\d\\d\\d\\d)>'\n",
573+
" + ''.join(f'<seg(?P<s{i}>\\d\\d\\d)>' for i in range(16)),\n",
575574
" detokenized_output,\n",
576575
" )\n",
577576
" boxes, segs = [], []\n",
@@ -673,33 +672,14 @@
673672
"metadata": {
674673
"accelerator": "GPU",
675674
"colab": {
676-
"gpuType": "T4",
677-
"machine_shape": "hm",
678-
"private_outputs": true,
679-
"provenance": [
680-
{
681-
"file_id": "1Mh7SHaP1cd5XuuHLmprKoBfr9ddSVu3a",
682-
"timestamp": 1715369116355
683-
},
684-
{
685-
"file_id": "1Xo8MY-GJsjjBKS1l_hQ8AImMNw9yxqmH",
686-
"timestamp": 1715208696058
687-
},
688-
{
689-
"file_id": "1bLQYoesGy8awiA0UT0askWIjIqcivfdY",
690-
"timestamp": 1715147134742
691-
}
692-
],
675+
"name": "inference-with-keras.ipynb",
693676
"toc_visible": true
694677
},
695678
"kernelspec": {
696679
"display_name": "Python 3",
697680
"name": "python3"
698-
},
699-
"language_info": {
700-
"name": "python"
701681
}
702682
},
703683
"nbformat": 4,
704684
"nbformat_minor": 0
705-
}
685+
}

0 commit comments

Comments
 (0)