{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport zipfile\nimport numpy as np\nimport sys\nimport pandas as pd\nimport tensorflow as tf\nfrom keras_preprocessing.image import load_img\nfrom keras_preprocessing.image import img_to_array\nfrom kaggle_datasets import KaggleDatasets\nfrom numpy import save\nfrom numpy import asarray\nfrom os import listdir\nimport matplotlib as mpl\nfrom numpy import load\nfrom tensorflow.keras.optimizers import RMSprop\nimport matplotlib.pyplot as plt\nimport random\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/aptos-prepare-train-and-validation-set/train.csv')\nvalidation_df=pd.read_csv('/kaggle/input/aptos-prepare-train-and-validation-set/validation.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fmap1=train_df['diagnosis'].value_counts()\nprint(fmap1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"validation_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fmap2=validation_df['diagnosis'].value_counts()\nprint(fmap2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_for_gcs=KaggleDatasets().get_gcs_path('aptos2019-blindness-detection')\nprint(path_for_gcs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_files_path=[path_for_gcs+'/train_images/'+ fname for fname in train_df['id_code']]\nvalidation_files_path=[path_for_gcs+'/train_images/'+ fname for fname in validation_df['id_code']]\ntrain_labels=list(train_df['diagnosis'])\nvalidation_labels=list(validation_df['diagnosis'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nprint(train_files_path[0])\nprint(validation_files_path[0])\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#TPU setup\ntry:\n    tpu=tf.distribute.cluster_resolver.TPUClusterResolver()\n    print(\"Running on TPU\")\nexcept ValueError:\n    tpu=None\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    tpu_strategy=tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    tpu_strategy=tf.distribute.get_strategy()\nprint(\"REPLICAS \",tpu_strategy.num_replicas_in_sync)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\n#predefined variable\nIMG_WIDTH=512\nIMG_HEIGHT=512\nBATCH_SIZE=16*tpu_strategy.num_replicas_in_sync#take thumb rule\nAUTOTUNE = tf.data.experimental.AUTOTUNE \nEPOCHS = 10\nSTEPS_PER_EPOCH=train_df.shape[0]//BATCH_SIZE\nprint(STEPS_PER_EPOCH)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\ndef parse_function_for_train(filename,label):\n    image_string=tf.io.read_file(filename)\n    image_decoded=tf.image.decode_png(image_string,channels=3)\n    #image_decoded=image_aug(image_decoded)\n    image_resized=tf.image.resize(image_decoded,[IMG_WIDTH,IMG_HEIGHT])\n    image_normalized=image_resized/255.0\n    label=tf.dtypes.cast(label,tf.int32)\n    label=tf.one_hot(label,5)\n    return image_normalized,label\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\ndef parse_function_for_validate(filename,label):\n    image_string=tf.io.read_file(filename)\n    image_decoded=tf.image.decode_png(image_string,channels=3)\n    image_resized=tf.image.resize(image_decoded,[IMG_WIDTH,IMG_HEIGHT])\n    image_normalized=image_resized/255.0\n    label=tf.dtypes.cast(label,tf.int32)\n    label=tf.one_hot(label,5)\n    return image_normalized,label\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\ndef image_aug(img):\n    img=tf.image.adjust_gamma(img,gamma=1, gain=1)\n    img=tf.image.adjust_contrast(img,1)\n    #img = tf.image.random_flip_left_right(img) horizontal flip\n    img=X = tf.image.random_flip_up_down(img) #vertical flip\n    img = tf.image.random_brightness(img, max_delta = 0.1)\n    img = tf.image.random_saturation(img, lower = 0.75, upper = 1.5)\n    img = tf.image.random_hue(img, max_delta = 0.15)\n    img = tf.image.random_contrast(img, lower = 0.75, upper = 1.5)\n    return img\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_dataset(filenames, labels, is_training=True):\n    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))\n    if is_training:\n        dataset = dataset.map(parse_function_for_train, num_parallel_calls=AUTOTUNE)\n    else:\n        dataset = dataset.map(parse_function_for_validate, num_parallel_calls=AUTOTUNE)\n    dataset = dataset.prefetch(buffer_size=AUTOTUNE)\n    dataset = dataset.batch(BATCH_SIZE)\n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dataset=create_dataset(train_files_path,train_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"validation_dataset=create_dataset(validation_files_path,validation_labels,is_training=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\ndef print_image_from_dataset(dataset,number):\n    images_ds=dataset.map(lambda image,label :image).unbatch()\n    labels_ds=dataset.map(lambda image,label :label).unbatch()\n    images=next(iter(images_ds.batch(validation_df.shape[0]))).numpy()\n    labels=next(iter(labels_ds.batch(validation_df.shape[0]))).numpy()\n    for i in range(number):\n        print(images[i].shape)\n        plt.imshow(images[i])\n        plt.title(labels[i])\n        plt.show()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_image_from_dataset(train_dataset,10) #we are checking if the augmentation worked properly","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -q efficientnet\nimport efficientnet.tfkeras as efn","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"metrices=[tf.keras.metrics.CategoricalAccuracy(name='acc')]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with tpu_strategy.scope():\n    enet = efn.EfficientNetB7(\n        input_shape=(IMG_WIDTH, IMG_HEIGHT, 3),\n        weights='imagenet',#'imagenet if training for first time'\n        include_top=False\n    )\n    enet.trainable = True\n    model = tf.keras.Sequential([\n        enet,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(5, activation='softmax')\n    ])\n    model.compile(\n        optimizer='adam',\n        loss = 'categorical_crossentropy',\n        metrics=metrices\n    )\n    model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights('/kaggle/input/aptos-blindness-detection-augmentation/efficientnetb7epochs36weightswithoversamplingnoimgaugpart0.hdf5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"histories=[]\nfor i in range(EPOCHS):\n    print(\"EPOCHS\",10+i+1)\n    history = model.fit(\n        train_dataset, \n        epochs=1,\n        steps_per_epoch=STEPS_PER_EPOCH,\n        validation_data=validation_dataset,validation_steps=2\n    )\n    histories.append(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate(validation_dataset)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save_weights('efficientnetb7epochs20weightswithoversamplingnoimgaugpart1.hdf5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"historiestoarray=[]\nfor x in histories:\n    historiestoarray.append(x.history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from numpy import save\nsave(\"history1to10.npy\",historiestoarray)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}