diff options
Diffstat (limited to 'megapixels/notebooks/datasets/megaface/megaface_prepare_flickr_api.ipynb')
| -rw-r--r-- | megapixels/notebooks/datasets/megaface/megaface_prepare_flickr_api.ipynb | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/megapixels/notebooks/datasets/megaface/megaface_prepare_flickr_api.ipynb b/megapixels/notebooks/datasets/megaface/megaface_prepare_flickr_api.ipynb new file mode 100644 index 00000000..48133228 --- /dev/null +++ b/megapixels/notebooks/datasets/megaface/megaface_prepare_flickr_api.ipynb @@ -0,0 +1,315 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Prepare Flickr API Batch CSV" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "%reload_ext autoreload\n", + "%autoreload 2\n", + "\n", + "import os\n", + "from os.path import join\n", + "from glob import glob, iglob\n", + "from pathlib import Path\n", + "from tqdm import tqdm_notebook as tqdm\n", + "\n", + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create CSV for API" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "| filepath | query |\n", + "|:---|:---|\n", + "| 12234 | 12234@123|" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "fp_in_dir_ids = '/data_store_ssd_perrier/datasets/people/megaface/downloads/MegafaceIdentities_VGG_META/'\n", + "fp_out_queries = '/data_store_hdd/datasets/people/megaface/research/megaface_flickr_api_queries.csv'\n", + "fp_out_queries_full = '/data_store_hdd/datasets/people/megaface/research/megaface_flickr_api_queries_full.csv'" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "nsid_paths = glob(join(fp_in_dir_ids, '*'))" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "1ab58c3dd0934591b0ea3f2da77003f5", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(IntProgress(value=0, max=672057), HTML(value='')))" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "items = []\n", + "for nsid_path in tqdm(nsid_paths):\n", + " nsid_full = Path(nsid_path).name\n", + " nsid = nsid_full.split('_')[0]\n", + " json_files = glob(join(fp_in_dir_ids, nsid_path, '*.json'))\n", + " for json_file in json_files:\n", + " nsid_id_json = file_utils.load_json(json_file)\n", + " full_image_url = nsid_id_json.get('full_img_url')\n", + " obj = {'nsid': nsid, 'nsid_full': nsid_full, 'full_image_url': full_image_url}\n", + " items.append(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.DataFrame.from_dict(items)\n", + "df.to_csv(fp_out_queries_full, index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [], + "source": [ + "count_lookup = {}\n", + "for item in items:\n", + " nsid = item['nsid']\n", + " if not nsid in count_lookup.keys():\n", + " count_lookup[nsid] = 0\n", + " count_lookup[nsid] += 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Combine MegaFace Flickr API Meta for User/NSID" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [], + "source": [ + "fp_in_dir = '/media/adam/ah8tb/data_store/datasets/people/megaface/research/flickr_api_dump'\n", + "fp_files = glob(join(fp_in_dir, '*.json'))\n", + "fp_files_err = [f for f in fp_files if '.txt' in f]\n", + "fp_files = [f for f in fp_files if '.txt' not in f]" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "46906\n", + "0\n" + ] + } + ], + "source": [ + "print(len(fp_files))\n", + "print(len(fp_files_err))" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "# combine files into single CSV" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "{\n", + " \"stat\": \"ok\",\n", + " \"user\": {\n", + " \"nsid\": \"7122726@N03\",\n", + " \"url\": \"https://www.flickr.com/people/tdbsca/\"\n", + " }\n", + "}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "c304096902584115be0c1a86accf2f69", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(IntProgress(value=0, max=46906), HTML(value='')))" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "meta_records = []\n", + "for fp_file in tqdm(fp_files):\n", + " data = file_utils.load_json(fp_file)\n", + " user = data.get('user')\n", + " nsid = user.get('nsid')\n", + " path_alias = Path(user.get('url')).stem\n", + " count = count_lookup.get(nsid)\n", + " obj = {'path_alias': path_alias, 'nsid': nsid, 'count': count}\n", + " meta_records.append(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "46906\n" + ] + } + ], + "source": [ + "df_meta_records = pd.DataFrame.from_dict(meta_records)\n", + "print(len(df_meta_records))" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [], + "source": [ + "df_meta_records.drop_duplicates(subset='nsid', keep='last', inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "46906\n" + ] + } + ], + "source": [ + "print(len(df_meta_records))" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [], + "source": [ + "fp_out_combined = '/data_store_hdd/datasets/people/megaface/research/megaface_flickr_meta_count.csv'\n", + "df_meta_records.to_csv(fp_out_combined, index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4627187\n" + ] + } + ], + "source": [ + "print(df_meta_records['count'].sum())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "megapixels", + "language": "python", + "name": "megapixels" + }, + "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.6.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} |
