Compare commits

...

3 commits

Author SHA1 Message Date
Andrew Straw 1dcc323471 source for exercie 13 2025-01-31 09:53:37 +01:00
Andrew Straw 137b3ce34a lecture 13 as finished 2025-01-31 09:53:06 +01:00
Andrew Straw e99689f614 exercise 14 2025-01-31 09:52:52 +01:00
6 changed files with 1631 additions and 390 deletions

View file

@ -0,0 +1,395 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fac96be8-dc47-4cac-9591-1d4dddd45580",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "56281969fd314d8abd60bb730bc6733c",
"grade": false,
"grade_id": "cell-0ec3e572e00b624c",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# Deep Learning"
]
},
{
"cell_type": "markdown",
"id": "c33c9c87-b7a4-4f26-9865-34d06d9f3067",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "1a85ab040d5086ce0062ee76e1160425",
"grade": false,
"grade_id": "cell-1be808b348d19ef2",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## YOLO (version 8) video detection\n",
"\n",
"We will use YOLO (You Only Look Once), a very popular *object detector* trained using deep learning. Read this [good article about what is YOLOv8](https://blog.roboflow.com/whats-new-in-yolov8/).\n",
"\n",
"As our first step"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90a07dd6-6ab8-4014-a29a-82358b1176c9",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "e19967d111f53da29bb3e1c9d523874a",
"grade": false,
"grade_id": "cell-411e1ce5e3e3171d",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"!pip install ultralytics"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c95ea0b3-75fc-4612-80cb-113ba7bba00f",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "d0d6bd700b391a4bb2bfde9dbc668c53",
"grade": false,
"grade_id": "cell-bec97cac599022a5",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"import cv2\n",
"from ultralytics import YOLO\n",
"from ultralytics.utils.plotting import Annotator, colors\n",
"import os"
]
},
{
"cell_type": "markdown",
"id": "dae71ee3-ae2a-444a-80e9-e344edfd9bea",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "d9750a219862774bb13834baaa125ce4",
"grade": false,
"grade_id": "cell-6bf6aaac22f685db",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"In this exercise, we use a pre-trained model. Ultralytics has trained this model on the [COCO 2017 dataset by Microsoft](https://cocodataset.org). They have trained 80 classes of objects, from `apple` to `wine glass` including `person`, `bicycle`, and `car`. They make several versions of the object detection model available (and they have other models for tasks beyond object detection). We will use the smallest (and fastest) of these, the \"nano\" version, `yolov8n`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9cbfaf12-f167-46e4-bd3f-3024542929cd",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b20451e51a59ca2637f81e32ebd387b5",
"grade": false,
"grade_id": "cell-c78347e3794f28a3",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"model = YOLO(\"yolov8n.pt\")"
]
},
{
"cell_type": "markdown",
"id": "eb7956c2-c064-40dd-aafb-442685ee3cbf",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "f81e3dd44ac6d1483c8d3ef5076a5155",
"grade": false,
"grade_id": "cell-95c4d55bfbcc11d4",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Q1\n",
"\n",
"Upload a video which you would like to run the YOLO object detector on. If you want to use a video of ours, you can try this video: https://strawlab-cdn.com/assets/samples-for-object-detection/BiologieI-bike.mp4 . Upload the video to the folder where this notebook lives by dragging and dropping it into the file browser in JupyterLab.\n",
"\n",
"Put the name of your video in the variable `movie_filename`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "698ab95e-58c0-49d0-bcb4-0ffed2a4d877",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "e1d2a866b43bbe45344f69c635afcb37",
"grade": false,
"grade_id": "cell-f4654a9b8a23da31",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
},
"tags": []
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cfbdadd-cde2-4db0-bb5d-69a21b64d0c9",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "75e0fe2c0779b190f089891e63fe4530",
"grade": true,
"grade_id": "cell-ef12e91efc25735d",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"outputs": [],
"source": [
"assert os.path.exists(movie_filename), 'No file with the name in `movie_filename`'\n",
"assert os.path.getsize(movie_filename) >= 10, 'Not video file: no more than 10 bytes'"
]
},
{
"cell_type": "markdown",
"id": "de630ada-5901-4142-a917-13320e79dd82",
"metadata": {},
"source": [
"Now we run YOLO and create an output video where each object detected by YOLO is drawn with a bounding box."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ac2b138-6a81-4a6a-8e70-85982d0215c1",
"metadata": {},
"outputs": [],
"source": [
"output_movie_filename = os.path.splitext(movie_filename)[0] + \"-output.mp4\"\n",
"\n",
"print(f'\"{movie_filename}\" -> \"{output_movie_filename}\"')\n",
"\n",
"cap = cv2.VideoCapture(movie_filename)\n",
"w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))\n",
"\n",
"out = cv2.VideoWriter(output_movie_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))\n",
"\n",
"for _ in range(100):\n",
" ret, im0 = cap.read()\n",
" if not ret:\n",
" print(\"Video frame is empty or video processing has been successfully completed.\")\n",
" break\n",
"\n",
" results = model.predict(im0, verbose=False)\n",
" annotator = Annotator(im0, line_width=2)\n",
"\n",
" for box in results[0].boxes:\n",
" cls = int(box.cls)\n",
" annotator.box_label(box.xyxy[0], model.names[cls], color=colors(cls))\n",
" out.write(im0)\n",
"\n",
"out.release()\n",
"cap.release()\n",
"print(\"done\")"
]
},
{
"cell_type": "markdown",
"id": "4c56534d-b32a-4c9f-82de-341e13c77261",
"metadata": {},
"source": [
"In practice, one may improve performance of a pre-trained model by *fine-tuning* for a particular task by using labelled data from your task. To limit the computational resources (and time) required, we did not perform any training in this exercise. Even the modest amount of tuning"
]
},
{
"cell_type": "markdown",
"id": "fc0d505e-df76-4cea-97b5-2b521b5a6f16",
"metadata": {},
"source": [
"## Q2\n",
"\n",
"Now describe some aspects of the object detection that are functioned but also describe some aspects which did not work well. (Two or three sentences.)"
]
},
{
"cell_type": "markdown",
"id": "b796689b-edc8-43de-b7e7-15202706e455",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "747796a397c411b96072c68d84e6ddf6",
"grade": true,
"grade_id": "cell-0ee9df636020a250",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"source": [
"YOUR ANSWER HERE"
]
},
{
"cell_type": "markdown",
"id": "2a4336ab-8261-42a6-8fd3-7c51a6348f89",
"metadata": {},
"source": [
"## LLMs\n",
"\n",
"For the next questions, sign up on ILIAS for \"AI Chat\" as shown in the movie during lecture. Answer the questions to enable \"Pilotierung: AI Chat für das Studium\".\n",
"\n",
"Now, go to \"AI Chat (Modell: llama3.3:70b, Betrieb: RZ)\" as shown in the movie during lecture."
]
},
{
"cell_type": "markdown",
"id": "0d74188e-482d-4486-a415-e48617bc3dc3",
"metadata": {},
"source": [
"## Q3\n",
"\n",
"With the AI Chat, ask the model to explain something to you which you have learned about in one of your biology courses. Copy and paste your prompt and the answer in the next cell:"
]
},
{
"cell_type": "markdown",
"id": "b93d2c66-e0da-43ec-9764-7701d75ee657",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "25edd3bbcd007a31d1f0b9fd7942da17",
"grade": true,
"grade_id": "cell-63f10d82a4d5ebd9",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"source": [
"YOUR ANSWER HERE"
]
},
{
"cell_type": "markdown",
"id": "960b6e5a-47ca-408d-ad2f-37845ad808bb",
"metadata": {},
"source": [
"## Q4\n",
"\n",
"Now, comment on the the quality of the response from the AI Chat: (two or three sentences)."
]
},
{
"cell_type": "markdown",
"id": "e1b9ea17-47f2-461a-99d5-f9f025de6380",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "ace265dc87dc26191ee49a7b9fcad653",
"grade": true,
"grade_id": "cell-9dcc354a187c8738",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"source": [
"YOUR ANSWER HERE"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,356 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# You must run this cell, but you can ignore its contents.\n",
"\n",
"import hashlib\n",
"\n",
"def ads_hash(ty):\n",
" \"\"\"Return a unique string for input\"\"\"\n",
" ty_str = str(ty).encode()\n",
" m = hashlib.sha256()\n",
" m.update(ty_str)\n",
" return m.hexdigest()[:10]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The questions here are in reference to the notebook from the lecture named `BEST.ipynb`. You will need to run that notebook to answer the following questions. You will need PyMC installed to run that notebook. You may be able to install it within Jupyter with `!pip install pymc`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problems\n",
"\n",
"### Q1 Run the notebook above with the following samples. What is the mean value of the posterior estimate of the difference of means? Put this in the variable `posterior_diff_means`.\n",
"\n",
"```\n",
"drug = (101,100,102,104,102,97,105,105,98,101,100,123,105,103,100,95,102,106,\n",
" 109,102,82,102,100,102,102,101,102,102,103,103,97,97,103,101,97,104,\n",
" 96,103,124,101,101,100,101,101,104,100,101)\n",
"placebo = (99,101,100,101,102,100,97,101,104,101,102,102,100,105,88,101,100,\n",
" 104,100,100,100,101,102,103,97,101,101,100,101,99,101,100,100,\n",
" 101,100,99,101,100,102,99,100,99)\n",
"```\n",
"\n",
"Note: you may also try running this at [the online version of the BEST test](http://www.sumsar.net/best_online/)."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-4583b093ba19414e",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'4a44dc1536'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Type your answer here and then run this and the following cell.\n",
"posterior_diff_means = 1.017\n",
"ads_hash(int(np.round(posterior_diff_means*10)))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-16393e9287358f37",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# This is a test of the above, do not change this code.\n",
"assert(ads_hash(int(np.round(posterior_diff_means*10)))=='4a44dc1536')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q2 Note the bounds of the 94% highest posterior density interval (HDI) for the difference of means between group 1 and group 2. Read these values from the summary table of the trace looking at the \"difference of means\" variable.\n",
"\n",
"Put these into the variables `diff_means_hdi_lower` and `diff_means_hdi_upper`\n",
"\n",
"(94% was chosen by the authors of the software as a friendly reminder that this value is an arbitrary choice - rather than perhaps 95% which my not remind you of this choice.)\n",
"\n",
"Also, due to the use of (pseudo) random numbers, your results may not exactly meet the auto-test check here. If that's the case, rerun the example with the BEST model and try again."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-f10804d9e06925c6",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"('6b86b273ff', '4e07408562')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Type your answer here and then run this and the following cell.\n",
"diff_means_hdi_lower, diff_means_hdi_upper = 0.220, 1.915\n",
"diff_means_hdi_lower, diff_means_hdi_upper = 0.125, 1.827\n",
"ads_hash(int(np.round(diff_means_hdi_lower*10))), ads_hash(int(np.round(diff_means_hdi_upper+1.0)))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-c13982956b19759a",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"assert ads_hash(int(np.round(diff_means_hdi_lower*10)))=='6b86b273ff'\n",
"assert ads_hash(int(np.round(diff_means_hdi_upper+1.0))) == '4e07408562'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q3 Re-run the notebook with the following samples. What is the mean value for the posterior estimate of the difference of means for this smaller sample size? Put this in the variable `posterior_diff_means_small_samples`.¶\n",
"\n",
"```\n",
"drug = (103,99,103,105,102,101)\n",
"placebo = (101,100,102,105,102,96)\n",
"```\n",
"\n",
"Again, due to the use of (pseudo) random numbers, your results may not exactly meet the auto-test check here. If that's the case, rerun the example with the BEST model and try again."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-e349ae3da9999c30",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'4a44dc1536'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Type your answer here and then run this and the following cell.\n",
"posterior_diff_means_small_samples = 1.043\n",
"ads_hash(int(np.round(posterior_diff_means_small_samples*10)))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-25d34717f657c572",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# This is a test of the above, do not change this code.\n",
"assert(ads_hash(int(np.round(posterior_diff_means_small_samples*10)))=='4a44dc1536')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q4 Note the bounds of the 94% highest posterior density interval (HDI) for the difference of means between group 1 and group 2 for this smaller data set. Read these values from the summary table of the trace looking at the \"difference of means\" variable.\n",
"\n",
"Put these into the variables `diff_means_hdi_lower_small_samples` and `diff_means_hdi_upper_small_samples`\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-6cb965cd2f3f109a",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"('37aa1ccf80', 'e7f6c01177')"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Type your answer here and then run this and the following cell.\n",
"(diff_means_hdi_lower_small_samples,diff_means_hdi_upper_small_samples)=-2.600, 4.521\n",
"ads_hash(int(np.round(diff_means_hdi_lower_small_samples*2))), ads_hash(int(np.round(diff_means_hdi_upper_small_samples+1)))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-b0b09eb0a16dfb2b",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# This is a test of the above, do not change this code.\n",
"assert ads_hash(int(np.round(diff_means_hdi_lower_small_samples*2))) == '37aa1ccf80'\n",
"assert ads_hash(int(np.round(diff_means_hdi_upper_small_samples+1))) == 'e7f6c01177'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q5 The posterior estimate of the difference in means between the two datasets is similar. But one of the datasets has much more data than the other. Explain why the 94% HDI exclude a zero difference result in one dataset but not the other."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-cde0c771165a691c",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"source": [
"### (Your answer here)\n",
"\n",
"The width of the HDI is much smaller in the dataset with more data -- this reflects that we are more certain about our estimates because we have more data. With the narrower posterior we can confidently exclude zero effect."
]
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Create Assignment",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.10"
},
"latex_envs": {
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 0
}
},
"nbformat": 4,
"nbformat_minor": 4
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long