{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%matplotlib inline\nfrom IPython.display import SVG\nfrom keras.utils.vis_utils import model_to_dot","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os\nimport shutil\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e4d7b577d13e3e0df8153b05cc3deb75c50e59e2"},"cell_type":"code","source":"import os.path\nimport itertools\nfrom itertools import chain\n\nimport numpy as np\nimport pandas as pd\nfrom sklearn import datasets\nfrom sklearn import preprocessing\nfrom sklearn.decomposition import PCA\nfrom sklearn import cluster, datasets, mixture\nfrom sklearn.datasets import load_digits\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.metrics import f1_score, classification_report, confusion_matrix\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import ListedColormap\nimport seaborn as sns\n\nfrom keras.layers import Input, Embedding, LSTM, GRU, Dense, Dropout, Lambda, \\\n    Conv1D, Conv2D, Conv3D, \\\n    Conv2DTranspose, \\\n    AveragePooling1D, AveragePooling2D, \\\n    MaxPooling1D, MaxPooling2D, MaxPooling3D, \\\n    GlobalAveragePooling1D, \\\n    GlobalMaxPooling1D, GlobalMaxPooling2D, GlobalMaxPooling3D, \\\n    LocallyConnected1D, LocallyConnected2D, \\\n    concatenate, Flatten, Average, Activation, \\\n    RepeatVector, Permute, Reshape, Dot, \\\n    multiply, dot, add, \\\n    PReLU, \\\n    Bidirectional, TimeDistributed, \\\n    SpatialDropout1D, \\\n    BatchNormalization\nfrom keras.models import Model, Sequential\nfrom keras import losses\nfrom keras.callbacks import BaseLogger, ProgbarLogger, Callback, History\nfrom keras.wrappers.scikit_learn import KerasClassifier\nfrom keras import regularizers\nfrom keras import initializers\nfrom keras.metrics import categorical_accuracy\nfrom keras.constraints import maxnorm, non_neg\nfrom keras.optimizers import RMSprop\nfrom keras.utils import to_categorical, plot_model\nfrom keras import backend as K","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f571224b68622dd08a7630b7c85563683e2acaa4"},"cell_type":"code","source":"from PIL import Image\nfrom zipfile import ZipFile\nimport h5py\nimport cv2\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b2420237bc57ac2c8ba50d00ed1019750220399c"},"cell_type":"code","source":"src_dir = '../input/human-protein-atlas-image-classification'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e70e5c48c66b130a86319c92e59f075dbd794d1"},"cell_type":"code","source":"train_labels = pd.read_csv(os.path.join(src_dir, \"train.csv\"))\nprint(train_labels.shape)\ntrain_labels.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd0c472d882c2acfa7379b6330c1dd068ea7ecf6"},"cell_type":"code","source":"test_labels = pd.read_csv(os.path.join(src_dir, \"sample_submission.csv\"))\nprint(test_labels.shape)\ntest_labels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"976f242fbe72d1b9a06c97e75237594fe138b190"},"cell_type":"code","source":"def show_arr(arr, nrows = 1, ncols = 4, figsize=(15, 5)):\n    fig, subs = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize)\n    for ii in range(ncols):\n        iplt = subs[ii]\n        img_array = arr[:,:,ii]\n        if ii == 0:\n            cp = 'Greens'\n        elif ii == 1:\n            cp = 'Blues'\n        elif ii == 2:\n            cp = 'Reds'\n        else:\n            cp = 'Oranges'\n        iplt.imshow(img_array, cmap=cp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae6dbf33e56318c938fef430bf2d3e1d42eb750c"},"cell_type":"code","source":"def get_arr0(Id, test=False):\n    def fn(Id, color, test=False):\n        if test:\n            tgt = 'test'\n        else:\n            tgt = 'train'\n        with open(os.path.join(src_dir, tgt, Id+'_{}.png'.format(color)), 'rb') as fp:\n            img = Image.open(fp)\n            arr = (np.asarray(img) / 255.)\n        return arr\n    res = []\n    for icolor in ['green', 'blue', 'red', 'yellow']:\n        arr0 = fn(Id, icolor, test)\n        res.append(arr0)\n    arr = np.stack(res, axis=-1)\n    return arr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"245de4fadd989996c292c7dbb517f761abf03b4c"},"cell_type":"code","source":"arr = get_arr0('00008af0-bad0-11e8-b2b8-ac1f6b6435d0', test=True)\nprint(arr.shape)\nshow_arr(arr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"641c153972972ad9a4a93649aa62952897cdc267"},"cell_type":"code","source":"arr = get_arr0('00070df0-bbc3-11e8-b2bc-ac1f6b6435d0')\nprint(arr.shape)\nshow_arr(arr)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a3c2a19084228fb2ba788405bbd3d8dcfbf167ab"},"cell_type":"markdown","source":"### cache train data"},{"metadata":{"trusted":true,"_uuid":"0e06748aed3ea4d547824fad5512b8cd67a6a63c"},"cell_type":"code","source":"SH = (140, 140)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5ffa5775c45435e5bfafe93e7a56f1314693cdb"},"cell_type":"code","source":"### CACHE\nimg_cache_train = np.zeros((train_labels.shape[0], 140, 140, 4), dtype=np.float16)\nID_LIST_TRAIN = train_labels.Id.tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2cbbd9584e72af83c60b0a34397e0cee885c24a"},"cell_type":"code","source":"for ii, id0 in enumerate(tqdm(ID_LIST_TRAIN)):\n    arr = get_arr0(id0)\n    img_cache_train[ii] = cv2.resize(arr[:], SH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"835fef201f53a516106f07c60a555e0d08da1041"},"cell_type":"code","source":"img_cache_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b23e978f31669257cbedac0d2d943676e61fe83e"},"cell_type":"code","source":"np.savez_compressed('img_cache_train_resize_140x140', x=img_cache_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"adc77588d22ee13bf3569f1c3cd79efb5b169118"},"cell_type":"code","source":"ls -la","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85741575ccf5e7718be9a139edab2fcb8f742f95"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3c74863e70039207f511502ec743042e26b7e29"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30d777b0958a0cee5da0b0d0130171e0b8247bcc"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}