{"cells":[{"metadata":{},"cell_type":"markdown","source":"Current notebook is empiric improvment current [top decision](https://www.kaggle.com/naivelamb/alaska2-srnet-baseline-inference) by [Xuan Cao](https://www.kaggle.com/naivelamb)."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom skimage.io import imread\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = '../input/alaska2-image-steganalysis'\nsub = pd.read_csv(os.path.join(PATH, 'sample_submission.csv'))\nsub","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub2 = pd.read_csv(os.path.join(PATH, 'sample_submission.csv'))\nsub2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub3 = pd.read_csv(os.path.join(PATH, 'sample_submission.csv'))\nsub3","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Compression Rate\nMay we have an RGB image with height H and width W. The theoretical number bytes B for this image is $$B = H * W * 3$$\nTherefor if the real size of image is S, then compression rate C is $$ C = (B - S) / B$$"},{"metadata":{"trusted":true},"cell_type":"code","source":"class JPEGImageCompressionRateDeterminer:\n    def __call__(self, image_path):\n        image = imread(image_path)\n        w, h, c = image.shape\n        \n        # theoretical image size\n        b = w*h*3\n        \n        # real image file size in bytes\n        s = os.stat(image_path).st_size\n        return (b - s) / b ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"compression_rate_determiner = JPEGImageCompressionRateDeterminer()\n\ncompressions = {}\n\ndir_path = os.path.join(PATH, 'Test')\nfor impath in tqdm(sub.Id.values):\n    c = compression_rate_determiner(os.path.join(dir_path, impath))\n    compressions[impath] = c\n    sub.loc[sub.Id == impath, 'Label'] = c","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"compression_rate_determiner = JPEGImageCompressionRateDeterminer()\n\ncompressions = {}\n\ndir_path = os.path.join(PATH, 'Test')\nfor impath in tqdm(sub.Id.values):\n    c = compression_rate_determiner(os.path.join(dir_path, impath))\n    compressions[impath] = c\n    sub2.loc[sub.Id == impath, 'Label'] = c**2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"compression_rate_determiner = JPEGImageCompressionRateDeterminer()\n\ncompressions = {}\n\ndir_path = os.path.join(PATH, 'Test')\nfor impath in tqdm(sub.Id.values):\n    c = compression_rate_determiner(os.path.join(dir_path, impath))\n    compressions[impath] = c\n    if c < 0.75:\n        sub3.loc[sub.Id == impath, 'Label'] = 0.000001\n    elif c >= 0.75 and c < 0.90:\n        sub3.loc[sub.Id == impath, 'Label'] = 1. - 1e-3 - (c - 0.75)\n    elif c >= 0.90 and c < 0.95:\n        sub3.loc[sub.Id == impath, 'Label'] = 1. - 1e-3 - (c - 0.90)\n    else:\n        sub3.loc[sub.Id == impath, 'Label'] = 1. - 1e-3 - (c - 0.95)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Empiric improvement\nEmpiric improvement based on punct 4 in data description.\n> \"4.The images are all compressed with one of the three following JPEG quality factors: 95, 90 or 75.\"\n\nSo images with compression rate more then 0.95 are prohibited."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\n\nplt.axvline(0.75, color='orange')\nplt.axvline(0.90, color='orange')\nplt.axvline(0.95, color='orange')\nplt.axvspan(0., 0.95, color='green', alpha=0.25)\nplt.axvspan(0.95, 1.0, color='red', alpha=0.25)\nsns.distplot(list(compressions.values()));","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv('submission.csv', index=None)\nsub2.to_csv('submission2.csv', index=None)\nsub3.to_csv('submission3.csv', index=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Reference\n* [ALASKA2: SRNet baseline inference](https://www.kaggle.com/naivelamb/alaska2-srnet-baseline-inference)"}],"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":4,"nbformat_minor":4}