{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 style=\"background-color:#00008B; font-family:'Brush Script MT',cursive;color:#DC143C;font-size:200%; text-align:center;border-radius: 50% 20% / 10% 40%\">Long-tailed Learning</h1>\n\n\"Long-tailed learning, one of the most challenging problems in visual recognition, aims to train well-performing models from a large number of images that follow a long-tailed class distribution.\"\n\nhttps://paperswithcode.com/task/long-tail-learning","metadata":{}},{"cell_type":"markdown","source":"#当前位置：网站首页>Yan Shuicheng and Feng Jiashi's team released the first review of deep long tail learning!\n\n![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSlB8opuohbo0z2YKKvhy1Ir_lFYVFkrf4ugQ&usqp=CAU)chowdera.com\n\nhttps://chowdera.com/2021/11/20211128120204616d.html\n\nhttps://arxiv.org/pdf/2110.04596.pdf","metadata":{}},{"cell_type":"markdown","source":"I will try to develop that subject on another Kaggle Notebook, since it's so compelling.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pathlib import Path\nimport os.path\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport seaborn as sns\n\nimport imageio\nimport matplotlib as mpl\nimport matplotlib.image as mpimg\nfrom PIL import Image, ImageOps\nimport scipy.ndimage as ndi","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-20T18:08:10.005065Z","iopub.execute_input":"2022-02-20T18:08:10.00636Z","iopub.status.idle":"2022-02-20T18:08:10.066319Z","shell.execute_reply.started":"2022-02-20T18:08:10.006285Z","shell.execute_reply":"2022-02-20T18:08:10.065469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ventakumar R https://www.kaggle.com/venkatkumar001/hfp-2-eda-tensorflow/notebook\n\nimport json, codecs\n\nwith codecs.open(\"../input/herbarium-2022-fgvc9/train_metadata.json\", 'r',\n                 encoding='utf-8', errors='ignore') as f:\n    train_meta = json.load(f)\n    \nwith codecs.open(\"../input/herbarium-2022-fgvc9/test_metadata.json\", 'r',\n                 encoding='utf-8', errors='ignore') as f:\n    test_meta = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:02:51.088884Z","iopub.execute_input":"2022-02-20T18:02:51.089391Z","iopub.status.idle":"2022-02-20T18:03:04.66169Z","shell.execute_reply.started":"2022-02-20T18:02:51.089342Z","shell.execute_reply":"2022-02-20T18:03:04.660674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ventakumar R https://www.kaggle.com/venkatkumar001/hfp-2-eda-tensorflow/notebook\n\ndisplay(train_meta.keys())","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:03:18.441299Z","iopub.execute_input":"2022-02-20T18:03:18.441624Z","iopub.status.idle":"2022-02-20T18:03:18.449591Z","shell.execute_reply.started":"2022-02-20T18:03:18.441588Z","shell.execute_reply":"2022-02-20T18:03:18.448681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ventakumar R https://www.kaggle.com/venkatkumar001/hfp-2-eda-tensorflow/notebook\n\nLtailed = pd.DataFrame(train_meta['annotations'])\n#train_cat.columns = [ 'category_id', 'scientificName','family', 'genus']\ndisplay(Ltailed)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:05:25.623893Z","iopub.execute_input":"2022-02-20T18:05:25.624601Z","iopub.status.idle":"2022-02-20T18:05:27.257067Z","shell.execute_reply.started":"2022-02-20T18:05:25.624548Z","shell.execute_reply":"2022-02-20T18:05:27.256112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After having another idea. Let's go to Plan B. ","metadata":{}},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\n#here we need to get to the data\ndirname = '/kaggle/input/herbarium-2022-fgvc9/train_images'\ndir_004 = os.path.join(dirname, '004')\ndir_004_13 = os.path.join(dir_004, '13')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:23:13.196413Z","iopub.execute_input":"2022-02-20T18:23:13.197043Z","iopub.status.idle":"2022-02-20T18:23:13.202399Z","shell.execute_reply.started":"2022-02-20T18:23:13.196992Z","shell.execute_reply":"2022-02-20T18:23:13.201773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nos.listdir(dir_004)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:16:06.968361Z","iopub.execute_input":"2022-02-20T18:16:06.968699Z","iopub.status.idle":"2022-02-20T18:16:06.994604Z","shell.execute_reply.started":"2022-02-20T18:16:06.968668Z","shell.execute_reply":"2022-02-20T18:16:06.993722Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nos.listdir(dir_004_13)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:23:27.99779Z","iopub.execute_input":"2022-02-20T18:23:27.998624Z","iopub.status.idle":"2022-02-20T18:23:28.012518Z","shell.execute_reply.started":"2022-02-20T18:23:27.998565Z","shell.execute_reply":"2022-02-20T18:23:28.011717Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\ndef plot_imgs_one_dir(item_dir, num_imgs=25):\n    all_item_dirs = os.listdir(item_dir)\n    item_files = [os.path.join(item_dir, file) for file in all_item_dirs][:num_imgs]\n\n    plt.figure(figsize=(10, 10))\n    for idx, img_path in enumerate(item_files):\n        plt.subplot(5, 5, idx+1)\n\n        img = plt.imread(img_path)\n        plt.imshow(img)\n\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:28:31.634108Z","iopub.execute_input":"2022-02-20T18:28:31.6348Z","iopub.status.idle":"2022-02-20T18:28:31.64166Z","shell.execute_reply.started":"2022-02-20T18:28:31.634744Z","shell.execute_reply":"2022-02-20T18:28:31.641053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nplot_imgs_one_dir(dir_004_13)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:28:57.945941Z","iopub.execute_input":"2022-02-20T18:28:57.946587Z","iopub.status.idle":"2022-02-20T18:29:03.524146Z","shell.execute_reply.started":"2022-02-20T18:28:57.946536Z","shell.execute_reply":"2022-02-20T18:29:03.523116Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\ndef plot_img_hist_3channel(item_dir, num_img=6):\n    all_item_dirs = os.listdir(item_dir)\n    item_files = [os.path.join(item_dir, file) for file in all_item_dirs][:num_img]\n\n    #plt.figure(figsize=(10, 10))\n    for idx, img_path in enumerate(item_files):\n        fig1 = plt.figure(idx,figsize=(10, 10))\n        fig1.add_subplot(2, 2, 1)\n        img = mpimg.imread(img_path, )\n        plt.imshow(img)\n        fig1.add_subplot(2, 2, 2)\n        plt.hist(img.ravel(), bins = 256, color = 'orange')\n        plt.hist(img[:, :, 0].ravel(), bins = 256, color = 'red')\n        plt.hist(img[:, :, 1].ravel(), bins = 256, color = 'green')\n        plt.hist(img[:, :, 2].ravel(), bins = 256, color = 'blue')\n        plt.xlabel('Intensity')\n        plt.ylabel('Count')\n        plt.legend(['Total', 'Red Channel', 'Green Channel', 'Blue Channel'])\n        plt.show()\n    \n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:30:12.274243Z","iopub.execute_input":"2022-02-20T18:30:12.27506Z","iopub.status.idle":"2022-02-20T18:30:12.285212Z","shell.execute_reply.started":"2022-02-20T18:30:12.27501Z","shell.execute_reply":"2022-02-20T18:30:12.284379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nplot_img_hist_3channel(dir_004_13)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:30:36.833427Z","iopub.execute_input":"2022-02-20T18:30:36.834032Z","iopub.status.idle":"2022-02-20T18:30:55.273164Z","shell.execute_reply.started":"2022-02-20T18:30:36.83398Z","shell.execute_reply":"2022-02-20T18:30:55.272158Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nos.mkdir('/kaggle/working/work_imgs')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:31:35.4384Z","iopub.execute_input":"2022-02-20T18:31:35.438704Z","iopub.status.idle":"2022-02-20T18:31:35.44348Z","shell.execute_reply.started":"2022-02-20T18:31:35.438675Z","shell.execute_reply":"2022-02-20T18:31:35.442687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\ndir_output = os.path.join('/kaggle/working/','work_imgs');","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:31:53.570106Z","iopub.execute_input":"2022-02-20T18:31:53.570893Z","iopub.status.idle":"2022-02-20T18:31:53.575885Z","shell.execute_reply.started":"2022-02-20T18:31:53.570838Z","shell.execute_reply":"2022-02-20T18:31:53.575053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\ndef  hist_equal(path_from, path_to, stop=4):\n    i=1\n    files = os.listdir(path_from)\n    \n    for file in files[:stop]: \n        try:\n            file_dir = os.path.join(path_from, file)\n            file_dir_save = os.path.join(path_to, file)\n            img = Image.open(file_dir)\n            img = ImageOps.equalize(img)\n            #img = img.convert(\"RGB\") #konwersja z RGBA do RGB, usuniecie kanału alfa zeby zapisać do jpg\n            img.save(file_dir_save) \n            i=i+1\n        except:\n            continue","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:32:13.842235Z","iopub.execute_input":"2022-02-20T18:32:13.842806Z","iopub.status.idle":"2022-02-20T18:32:13.850625Z","shell.execute_reply.started":"2022-02-20T18:32:13.842756Z","shell.execute_reply":"2022-02-20T18:32:13.849794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nhist_equal(dir_004_13, dir_output)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:32:38.483888Z","iopub.execute_input":"2022-02-20T18:32:38.48462Z","iopub.status.idle":"2022-02-20T18:32:38.63673Z","shell.execute_reply.started":"2022-02-20T18:32:38.484573Z","shell.execute_reply":"2022-02-20T18:32:38.635771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nplot_img_hist_3channel(dir_output, 4)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:32:56.055356Z","iopub.execute_input":"2022-02-20T18:32:56.056093Z","iopub.status.idle":"2022-02-20T18:33:07.889866Z","shell.execute_reply.started":"2022-02-20T18:32:56.056046Z","shell.execute_reply":"2022-02-20T18:33:07.889004Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\ndef plot_cdf_comparison(item_dir_before,item_dir_after, num_img=1):\n    all_item_dirs = os.listdir(item_dir_before)\n    item_files_before = [os.path.join(item_dir_before, file) for file in all_item_dirs][:num_img]\n    item_files_after = [os.path.join(item_dir_after, file) for file in all_item_dirs][:num_img]\n  \n  #plt.figure(figsize=(10, 10))\n    for idx, img_path in enumerate(item_files_before):\n        im_b = imageio.imread(img_path)\n        hist_b = ndi.histogram(im_b, min=0, max=255, bins=256)\n        cdf_b = hist_b.cumsum() / hist_b.sum()\n        \n        img_path_a = item_files_after[idx]\n        im_a = imageio.imread(img_path_a)\n        hist_a = ndi.histogram(im_a, min=0, max=255, bins=256)\n        cdf_a = hist_a.cumsum() / hist_a.sum()\n\n        fig1 = plt.figure(idx,figsize=(10, 10))\n        fig1.add_subplot(2, 4, 1)\n        img_b = mpimg.imread(img_path, )\n        plt.title(\"Before. {}\".format(idx))\n        plt.imshow(img_b, cmap='gray')\n        fig1.add_subplot(2, 4, 4)\n        plt.title(\"CDF comparison\")\n        plt.plot(cdf_b)\n        \n        fig2 = plt.figure(idx,figsize=(10, 10))\n        fig2.add_subplot(2, 4, 2)\n        img_a = mpimg.imread(img_path_a, )\n        plt.title(\"Before. {}\".format(idx))\n        plt.imshow(img_a)\n        fig1.add_subplot(2, 4, 4)\n        plt.plot(cdf_a)\n        plt.legend(['CDF before', 'CDF after'])\n\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:34:12.993227Z","iopub.execute_input":"2022-02-20T18:34:12.993547Z","iopub.status.idle":"2022-02-20T18:34:13.006354Z","shell.execute_reply.started":"2022-02-20T18:34:12.993516Z","shell.execute_reply":"2022-02-20T18:34:13.005639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Wojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nplot_cdf_comparison(dir_004_13, dir_output, 4)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T18:34:38.033811Z","iopub.execute_input":"2022-02-20T18:34:38.034077Z","iopub.status.idle":"2022-02-20T18:34:41.727354Z","shell.execute_reply.started":"2022-02-20T18:34:38.034049Z","shell.execute_reply":"2022-02-20T18:34:41.726431Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgements:\n\nWojciech Łabuñski https://www.kaggle.com/wojciech1103/herbarium-2020-a-little-bit-fun-with-images/notebook\n\nVentakumar R https://www.kaggle.com/venkatkumar001/hfp-2-eda-tensorflow/notebook","metadata":{}},{"cell_type":"markdown","source":"![](http://bair.berkeley.edu/static/blog/oltr/teaser.png)bair.berkeley.edu","metadata":{}}]}