{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Imports Here"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\nimport matplotlib.pyplot as plt\nimport PIL.Image as Image\nimport os\nimport tensorflow_addons as tfa","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Unzip and Sort out the data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!unzip /kaggle/input/cassava-disease/extraimages.zip -d  /kaggle/working/ \n!unzip /kaggle/input/cassava-disease/test.zip -d /kaggle/working/\n!unzip /kaggle/input/cassava-disease/train.zip -d /kaggle/working/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# See what we have"},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls /kaggle/working/train/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls /kaggle/working/test/0/ | wc -l","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Using this place as Helper Functions"},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_image(image):\n    plt.imshow(image)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_and_display_image(path):\n    image = tf.keras.preprocessing.image.load_img(path)\n    display_image(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"load_and_display_image('/kaggle/working/train/cbb/train-cbb-351.jpg')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Checking the Distributions for Training Images"},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls /kaggle/working/train/cbb/ | wc -l\n!ls /kaggle/working/train/cbsd/ | wc -l\n!ls /kaggle/working/train/cgm/ | wc -l\n!ls /kaggle/working/train/cmd/ | wc -l\n!ls /kaggle/working/train/healthy/ | wc -l","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Some Fixed Variables"},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_SHAPE = 96\nBATCH_SIZE = 70\nBUFFER_SIZE = 200\nPREFETCH = tf.data.experimental.AUTOTUNE","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating Data Augmentations and flow from directory"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=8,\n                                                                zoom_range=[0.9, 1.25],\n                                                                horizontal_flip=True,\n                                                                vertical_flip=True,\n                                                                rescale=1/255,\n                                                                dtype='tf.float32',\n                                                                validation_split=0.2,\n                                                               )\n\nvalid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255,\n                                                               dtype='tf.float32',\n                                                                validation_split=0.2,\n                                                               )\n\ntest_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255,\n                                                              dtype='tf.float32'\n                                                              )\ntrain_datagen = train_datagen.flow_from_directory('/kaggle/working/train/',\n                                                 target_size=(IMAGE_SHAPE,IMAGE_SHAPE),\n                                                 batch_size=BATCH_SIZE,\n                                                 class_mode='categorical',\n                                                 shuffle=True,\n                                                 seed=42,\n                                                 subset='training')\n\nvalid_datagen = valid_datagen.flow_from_directory('/kaggle/working/train/',\n                                                 target_size=(IMAGE_SHAPE,IMAGE_SHAPE),\n                                                 batch_size=BATCH_SIZE,\n                                                 class_mode='categorical',\n                                                 shuffle=True,\n                                                 seed=42,\n                                                 subset='validation')\n\ntest_datagen = test_datagen.flow_from_directory('/kaggle/working/',\n                                               target_size=(IMAGE_SHAPE,IMAGE_SHAPE),\n                                               batch_size=BATCH_SIZE,\n                                               classes=['test']\n                                               )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen.next()[1].shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Download Pretrained Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model = tf.keras.applications.MobileNetV2(include_top=False,weights='imagenet',input_shape=(IMAGE_SHAPE,IMAGE_SHAPE,3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.7,patience=3, verbose=2, mode='auto',min_lr=1e-6)\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss',patience=6, verbose=2, mode='auto',baseline=None, restore_best_weights=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"try:\n    with tf.device('/device:GPU:0'):\n        inputs = tf.keras.layers.Input(shape=(IMAGE_SHAPE,IMAGE_SHAPE,3))\n        x = base_model(inputs)\n        x = tf.keras.layers.Flatten()(x)\n        x = tf.keras.layers.Dense(512,activation='relu')(x)\n        outputs = tf.keras.layers.Dense(5,activation=\"softmax\")(x)\n\n        model = tf.keras.Model(inputs=[inputs],outputs=[outputs])\n\n        model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=0.001),\n                      #loss=tf.keras.losses.CategoricalCrossentropy(),\n                      loss=tfa.losses.SigmoidFocalCrossEntropy(),\n                      metrics=[tf.keras.metrics.Accuracy()])\n\n        model.fit(train_datagen,epochs=10,validation_data=valid_datagen,callbacks = [reduce_lr,early_stop])\nexcept RuntimeError as e:\n    print(e)    "},{"metadata":{"trusted":true},"cell_type":"code","source":"try:\n    with tf.device('/device:GPU:0'):\n        inputs = tf.keras.layers.Input(shape=(IMAGE_SHAPE,IMAGE_SHAPE,3))\n        x = base_model(inputs)\n        x = keras.layers.GaussianNoise(0.2)(x)\n        x = keras.layers.GlobalAveragePooling2D()(x)\n        outputs = tf.keras.layers.Dense(5,activation=\"softmax\")(x)\n\n        model = tf.keras.Model(inputs=[inputs],outputs=[outputs])\n\n        model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001),\n                      loss=tf.keras.losses.CategoricalCrossentropy(),\n                      #loss=tfa.losses.SigmoidFocalCrossEntropy(),\n                      metrics=['accuracy'])\n\n        model.fit(train_datagen,epochs=30,validation_data=valid_datagen,callbacks = [reduce_lr,early_stop])\nexcept RuntimeError as e:\n    print(e)    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.config.experimental.list_physical_devices()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"strategy = tf.distribute.experimental_set_strategy('')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"strategy.num_replicas_in_sync","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}