{"cells":[{"metadata":{},"cell_type":"markdown","source":"This is an example of **AutoML** usage and how **Autokeras** can be adapted for an internet disabled competition. Autokeras model is trained externally with multiple GPUs and 12 hours time. Overall, **44 different models** are tested to find the best model. Both pre-trained model weights and autokeras framework are added as external data in this kernel. \n\nTrained model got **65.58%** accuracy on train set, **63.45%** on validation set. \n\nHowever, this competition evaluates your kernel with a quadratic weighted kappa score. It got **0.4264** cohen kappa score on its own validation data. Besides, it got **0.114** kappa score on public test set, **0.385** kappa score on private test set. \n\nIt seems that autokeras is **promising** for this kind of competitions but you should let it to be trained for a much longer time.\n\nIf you are interested in AutoML and Autokeras, you should read the following blog posts.\n\n[1] https://sefiks.com/2019/04/08/a-gentle-introduction-to-auto-keras/\n\n[2] https://sefiks.com/2019/09/03/tips-for-building-automl-with-autokeras/"},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\npackage_dir = '../input/autokeras/autokeras'\nsys.path.insert(0, package_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import autokeras as ak\nfrom autokeras.utils import pickle_from_file","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"autokeras_model = pickle_from_file(\"../input/autokeras-model-v3/autokeras_model_v3.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\n# you can also convert the model to Keras but pre-trained weights disappear in this case\nautokeras_model.export_keras_model('keras_model.h5')\n\nfrom keras.models import load_model\nkeras_model = load_model('keras_model.h5')\nkeras_model.summary()\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 224","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#https://www.kaggle.com/taindow/pre-processing-train-and-test-images\n\ndef crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        mask = gray_img>tol        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0):\n            return img\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n            img = np.stack([img1,img2,img3],axis=-1)\n        return img\n\ndef circle_crop_v2(img):\n    img = cv2.imread(img)\n    img = crop_image_from_gray(img)\n\n    height, width, depth = img.shape\n    largest_side = np.max((height, width))\n    img = cv2.resize(img, (largest_side, largest_side))\n\n    height, width, depth = img.shape\n\n    x = int(width / 2)\n    y = int(height / 2)\n    r = np.amin((x, y))\n\n    circle_img = np.zeros((height, width), np.uint8)\n    cv2.circle(circle_img, (x, y), int(r), 1, thickness=-1)\n    img = cv2.bitwise_and(img, img, mask=circle_img)\n    img = crop_image_from_gray(img)\n\n    return img\n\ndef getImagePixelsNew(img_name):\n    \n    image = circle_crop_v2('../input/aptos2019-blindness-detection/test_images/%s.png' % img_name)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    \n    #normalize in scale of 0, +1\n    img_pixels = image / 255 #0 to 254\n    \n    return img_pixels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['pixels'] = df['id_code'].apply(getImagePixelsNew)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.iloc[0]['pixels'].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features = []\n\npbar = tqdm(range(0, df.shape[0]), desc='Processing')\nfor index in pbar:\n    features.append(df.iloc[index]['pixels'])\n\nprint(\"features variable created: \",len(features))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = []\n\npbar = tqdm(range(0, len(features)), desc='Processing')\n\nfor index in pbar:\n    prediction = autokeras_model.predict(np.expand_dims(features[index], axis = 0))\n    predictions.append(prediction[0])\n\n#predictions = autokeras_model.predict(features)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions[0:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['diagnosis'] = predictions\ndf = df.drop(columns=['pixels'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","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}