{ "cells": [ { "cell_type": "markdown", "id": "a25ac442", "metadata": {}, "source": [ "## Performance HuggingFace Dataset vs Dataset Loader" ] }, { "cell_type": "markdown", "id": "3da127dc", "metadata": {}, "source": [ "## Hugging Face Dataset" ] }, { "cell_type": "code", "execution_count": 34, "id": "aef315bf", "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset\n", "import torch\n", "import glob" ] }, { "cell_type": "code", "execution_count": 63, "id": "c0ed6498", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Found cached dataset ava (/home/william/.cache/huggingface/datasets/will33am___ava/default/1.0.0/723cc8bd5959ef1cd88b7d51648a8bc7fd98c9d8ddb768cb8c8ebaade1b82306)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 211 ms, sys: 11.8 ms, total: 223 ms\n", "Wall time: 852 ms\n" ] } ], "source": [ "%%time\n", "ds = load_dataset(\"will33am/AVA\",split = 'train')\n", "ds = ds.remove_columns([\"rating_counts\",\"text_tag_0\",\"text_tag_1\"])" ] }, { "cell_type": "code", "execution_count": 66, "id": "c51e48dd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 7.3 ms, sys: 644 µs, total: 7.94 ms\n", "Wall time: 6.24 ms\n" ] } ], "source": [ "%%time\n", "image = ds[0]['image']" ] }, { "cell_type": "markdown", "id": "25b4bd86", "metadata": {}, "source": [ "## Pytorch Dataset" ] }, { "cell_type": "code", "execution_count": 30, "id": "07480450", "metadata": {}, "outputs": [], "source": [ "from PIL import Image\n", "class Datasets(torch.utils.data.Dataset):\n", " def __init__(self,files):\n", " self.files = files\n", " def __getitem__(self,idx):\n", " return Image.open(self.files[idx])\n", " \n", " def __len__(self):\n", " return len(self.files)" ] }, { "cell_type": "code", "execution_count": 31, "id": "7dcb29a3", "metadata": {}, "outputs": [], "source": [ "files = glob.glob(\"../../AVA_src/images/images/*.jpg\")\n", "dataset = Datasets(files)" ] }, { "cell_type": "code", "execution_count": 53, "id": "43c33afc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.21 ms, sys: 0 ns, total: 1.21 ms\n", "Wall time: 656 µs\n" ] } ], "source": [ "%%time\n", "image = dataset[0]" ] }, { "cell_type": "code", "execution_count": 58, "id": "771fe113", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.731707317073171" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 10 veces mas rapido acceder a un elemento\n", "(7.04e-3)/(656 * 1e-6 )" ] } ], "metadata": { "kernelspec": { "display_name": "huggingface", "language": "python", "name": "huggingface" }, "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.8.15" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }