{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"from multiprocessing import Pool, cpu_count\nimport glob, zipfile, os, itertools\nfrom PIL import Image, ImageStat\nfrom sklearn import *\nimport pandas as pd\nimport numpy as np\n\n# Statistics\ndef get_features(path):\n    try:\n        st = []\n        # Image pixel obtained\n        img = Image.open(path)\n        # Start the statistical result of RGB by image data\n        im_stats_ = ImageStat.Stat(img)\n        # total\n        st += im_stats_.sum\n        # Average value\n        st += im_stats_.mean\n        # Root mean square\n        st += im_stats_.rms\n        # dispersion\n        st += im_stats_.var\n        # standard deviation\n        st += im_stats_.stddev\n    except:\n        print(path)\n    return [path, st]\n\n# Parallel processing\ndef normalize_img(paths):\n    imf_d = {}\n    p = Pool(cpu_count())\n    # get_features(Parallel processing of functions)\n    ret = p.map(get_features, paths)\n    # Arrange the result of parallel processing\n    for i in range(len(ret)):\n        imf_d[ret[i][0]] = ret[i][1]\n    ret = []\n    fdata = [imf_d[f] for f in paths]\n    return pd.DataFrame(fdata)\n\n# Load image data path\ndog_bytes = pd.DataFrame(glob.glob('../input/all-dogs/all-dogs/**'), columns=['Path'])\n# Get statistical data of pixcel data for each image\ndog_bytes = pd.concat((dog_bytes, normalize_img(dog_bytes.Path.values)), axis=1)\ndog_bytes.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Divide image data into 100 classifications by KMeans method\ndog_bytes['Group'] = cluster.KMeans(n_clusters=100, random_state=3, n_jobs=-1).fit_predict(dog_bytes[list(range(15))])\n# Get 5 large categories from 100 categories (displayed)\ndog_bytes['Group'].value_counts()[:5]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Inspiration\n==========="},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n# Create a window to display the image\n# Unit is in inches\nfig = plt.figure(figsize=(8, 80))\nsamples = []\n# Get 5 samples from image data of a specific category\nfor i in range(100):\n    # Acquire image data of a specific classification\n    g = dog_bytes[dog_bytes['Group'] == i]\n    if len(g) >= 5:\n        # Get 5 samples from image data of specific classification\n        samples += list(g['Path'].values[:5])\n\n# Display images for each category\nfor i in range(len(samples))[:50]:\n    # Get one of the 5 rows and 5 columns of windows\n    ax = fig.add_subplot(len(samples)/5, 5, i+1, xticks=[], yticks=[])\n    # Get image data\n    img = Image.open(samples[i])\n    # Resize image data\n    # Unit is pixel\n    # Resolution (dpi) = pixel / inch\n    img = img.resize((100,int(img.size[1]/(img.size[0]/100))), Image.ANTIALIAS)\n    img = img.crop((0, 0, 64, 64))\n    plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Motivation\n=============="},{"metadata":{"trusted":true},"cell_type":"code","source":"def sim_img(path):\n    img = Image.open(path).convert('RGB')\n    img = img.resize((100,int(img.size[1]/(img.size[0]/100))), Image.ANTIALIAS)\n    img = img.crop((0, 0, 64, 64))\n    return img\n\nsamples = []\nfor i in range(100):\n    g = dog_bytes[dog_bytes['Group'] == i]\n    if len(g) >= 23:\n        s = g['Path'].values[:23]\n        # Create a complete set of two sets of image data in the same classification\n        s = list([p for p,_ in itertools.groupby(sorted([sorted(p) for p in list(itertools.permutations(s, 2))]))])\n        samples += s\nprint(len(samples))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Submission\n============="},{"metadata":{"trusted":true},"cell_type":"code","source":"z = zipfile.PyZipFile('images.zip', mode='w')\nfor i in range(10000):\n    p1, p2 = samples[i]\n    try:\n        # Mix two images in the same classification to create a new image\n        # out = p1 * (1 - 0.4) + p2 * 0.4\n        im = Image.blend(sim_img(p1), sim_img(p2), alpha=0.4)\n        f = str(i)+'.png'\n        im.save(f,'PNG'); z.write(f); os.remove(f)\n        if i % 1000==0:\n            print(i)\n    except:\n        print(p1, p2)\n\nprint (len(z.namelist()))\nz.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"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":1}