{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport os\nimport pandas as pd\nimport cv2\nimport numpy as np\n!pip install -q ../input/keras-efficientnet-whl/Keras_Applications-1.0.8-py3-none-any.whl\n!pip install -q ../input/keras-efficientnet-whl/efficientnet-1.1.1-py3-none-any.whl\nimport efficientnet.keras\nimport efficientnet\nimport time\nimport gc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_img_dir='../input/cassava-leaf-disease-classification/test_images/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_root_path = '../input/cassava-gpu-preprocessed-trainer/'\nmodel_name = 'effnet5'\nmodel_0 = tf.keras.models.load_model(model_root_path +'fold_0_'+model_name+'_aug.hdf5')\nmodel_1 = tf.keras.models.load_model(model_root_path +'fold_1_'+model_name+'_aug.hdf5')\nmodel_2 = tf.keras.models.load_model(model_root_path +'fold_2_'+model_name+'_aug.hdf5')\nmodel_3 = tf.keras.models.load_model(model_root_path +'fold_3_'+model_name+'_aug.hdf5')\nmodel_4 = tf.keras.models.load_model(model_root_path + 'fold_4_'+model_name+'_aug.hdf5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# pipeline:\n#  image_name -> predict ->store to csv \n#  predict = (image_generator  -> make_prediction ) \n#  image_generator = ( -> read_img -> augment )\n\n\ndef read_img(image_name):\n    filepath = '../input/cassava-leaf-disease-classification/test_images/'+image_name\n    image = tf.io.decode_jpeg(tf.io.read_file(filepath))\n    return image\n\n\ndef get_augment_list():\n    return np.array(list(map(lambda x:x<1,np.random.randint(2, size=6))),dtype='bool')\n\n\n@tf.function\ndef resize_image(image):\n    return tf.cast(tf.reshape(tf.image.resize(image,[299,299]),(1,299,299,3)),tf.float32)\n\n    \n    \n@tf.function\ndef augment_img_randomly(img):\n    '''\n    Augmentaions to be used: (use stateless versions of these)\n    \n    Random hue (0.2)\n    Random brightness (0.3)\n    Random saturation (0.7,1.3)\n    Random contrast  (0.8,1.2)\n    ''' \n    augment_list = get_augment_list()\n    image = resize_image(img)\n     #(32,512,512,3)\n    \n    if augment_list[0]:\n        image = tf.image.random_saturation(image,0.7,1.3)\n    if augment_list[1]:\n        image = tf.image.random_contrast(image,0.8,1.2)\n    if augment_list[2]:\n        image = tf.image.random_brightness(image,0.3)\n    if augment_list[3]:\n        image = tf.image.random_hue(image,0.2)\n    if augment_list[4]:\n        image = tf.image.random_flip_left_right(image)\n    if augment_list[5]:\n        image = tf.image.random_flip_up_down(image)\n    \n    \n    \n    image = tf.math.divide(image,255)\n    del augment_list,img\n    gc.collect()\n        \n    return image\n\ndef image_generator(image_name):\n    img = read_img(image_name)\n    img = augment_img_randomly(img)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\ndef make_image(image_name):\n    img = tf.image.decode_jpeg(tf.io.read_file('../input/cassava-leaf-disease-classification/test_images/'+image_name))\n    img = tf.image.resize(img,[299,299])\n    img = tf.cast(img,tf.float32)\n    img = tf.math.divide(img,tf.constant(255.0))\n    img = tf.reshape(img,[1,299,299,3])\n    return img\n\n\n#@tf.function\ndef make_prediction(img):\n    \n     \n    \n    label_arr_0 = model_0.predict(img)\n    \n    \n    label_arr_1 = model_1.predict(img)\n    \n    \n    label_arr_2 = model_2.predict(img)\n    \n    \n    label_arr_3 = model_3.predict(img)\n    \n    \n    label_arr_4 = model_4.predict(img)\n    \n    \n    label_arr = label_arr_0 + label_arr_1 + label_arr_2 + label_arr_3 + label_arr_4 \n    return tf.math.argmax(label_arr,axis=-1)\n\ndef predict(img_name):\n    #imgpath = tf.constant(img_path)\n    return make_prediction(image_generator(img_name)).numpy()[0] ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images = []\nlabels = []\nfor i in os.listdir(test_img_dir):\n    image_name = i\n    #label = predict('2216849948.jpg')\n    label = make_prediction(make_image(i)).numpy()[0]\n    images.append(image_name)\n    labels.append(label)\n\n\ndf=pd.DataFrame(data={'image_id':images,'label':labels})\n\ndf.to_csv('submission.csv',index=False)    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# start = time.time()\n# print(make_prediction(make_image(tf.constant('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg'))))\n# mid = time.time()\n# print(\"Elapsed: \",mid-start)\n\n# print(make_prediction(make_image(tf.constant('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg'))))\n# endd = time.time()\n# print(\"Elapsed: \",endd-mid)","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":4}