{"cells":[{"metadata":{},"cell_type":"markdown","source":"<a id=\"toc\"></a>\n# Table of Contents\n1. [Configure parameters](#configure_parameters)\n1. [Import modules](#import_modules)\n1. [Define helper functions](#define_helper_functions)\n1. [Start the calculation](#start_the_calculation)"},{"metadata":{},"cell_type":"markdown","source":"<a id=\"configure_parameters\"></a>\n# Configure parameters\n[Back to Table of Contents](#toc)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# DATASET_DIR = '/kaggle/input/understanding-clouds-from-satellite-images-384x576/'\nDATASET_DIR = '/kaggle/input/understanding_cloud_organization/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"import_modules\"></a>\n# Import modules\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport cv2\nimport os\nfrom tqdm import tqdm_notebook","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"define_helper_functions\"></a>\n# Define helper functions\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"def compute_sample_mean(im_dir, image_files):\n    \"\"\"\n    Parameters:\n        im_dir: The directory that contains images.\n        image_files: List of image files you need.\n\n    Returns:\n        sample-mean\n    \"\"\"\n\n    if len(image_files) > 0:\n        image_sum = cv2.imread(os.path.join(im_dir, image_files[0]))\n        image_sum = cv2.cvtColor(image_sum, cv2.COLOR_BGR2RGB).astype(np.float64)\n\n        for image_file in tqdm_notebook(image_files[1:]):\n            img = cv2.imread(os.path.join(im_dir, image_file))\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n            image_sum += img\n\n        return image_sum/(255*len(image_files))\n\n    return None\n\n\ndef compute_pixel_mean(im_dir, image_files, sample_mean=None):\n    \"\"\"\n    Parameters:\n        im_dir: The directory that contains images.\n        image_files: List of image files you need.\n        sample_mean: sample-mean value.\n\n    Returns:\n        pixel-mean\n    \"\"\"\n\n    if sample_mean is None:\n        sample_mean = compute_sample_mean(im_dir, image_files)\n\n    return np.mean(sample_mean, axis=(0, 1))\n\n\ndef compute_sample_std(im_dir, image_files, sample_mean=None):\n    \"\"\"\n    Parameters:\n        im_dir: The directory that contains images.\n        image_files: List of image files you need.\n        sample_mean: sample-mean value.\n\n    Returns:\n        sample-std\n    \"\"\"\n\n    if len(image_files) > 0:\n        if sample_mean is None:\n            sample_mean = compute_sample_mean(im_dir, image_files)\n\n        square_diff_sum = 0\n        for image_file in tqdm_notebook(image_files):\n            img = cv2.imread(os.path.join(im_dir, image_file))\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float64)\n            img /= 255.\n            square_diff_sum += np.square(np.abs(img - sample_mean))\n\n        return np.sqrt(square_diff_sum/len(image_files))\n\n    return 0\n\n\ndef compute_pixel_std(im_dir, image_files, sample_mean=None, pixel_mean=None):\n    \"\"\"\n    Parameters:\n        im_dir: The directory that contains images.\n        image_files: List of image files you need.\n        sample_mean: sample-mean value.\n        pixel_mean: pixel-mean value.\n\n    Returns:\n        pixel-std\n    \"\"\"\n\n    if len(image_files) > 0:\n        if pixel_mean is None:\n            if sample_mean is not None:\n                pixel_mean = compute_pixel_mean(im_dir, image_files, sample_mean)\n            else:\n                pixel_mean = compute_pixel_mean(im_dir, image_files)\n\n        square_diff_sum = 0\n        for image_file in tqdm_notebook(image_files):\n            img = cv2.imread(os.path.join(im_dir, image_file))\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float64)\n            img /= 255.\n            square_diff_sum += np.sum(np.square(np.abs(img - pixel_mean)), axis=(0, 1))\n\n        return np.sqrt(square_diff_sum/(len(image_files)*img.shape[0]*img.shape[1]))\n\n    return 0","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"start_the_calculation\"></a>\n# Start the calculation\n[Back to Table of Contents](#toc)"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_dir = os.path.join(DATASET_DIR, 'train_images')\nimage_files = os.listdir(train_images_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_mean = compute_sample_mean(train_images_dir, image_files)\nsample_std = compute_sample_std(train_images_dir, image_files, sample_mean=sample_mean)\npixel_mean = compute_pixel_mean(train_images_dir, image_files, sample_mean=sample_mean)\npixel_std = compute_pixel_std(train_images_dir, image_files, pixel_mean=pixel_mean)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Sample mean:', sample_mean)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Sample std:', sample_std)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Pixel mean:', pixel_mean)\nprint('Pixel std:', pixel_std)","execution_count":null,"outputs":[]}],"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":1}