{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":9631568,"sourceType":"datasetVersion","datasetId":5880049},{"sourceId":9714147,"sourceType":"datasetVersion","datasetId":5942467},{"sourceId":9806248,"sourceType":"datasetVersion","datasetId":6010828},{"sourceId":9806649,"sourceType":"datasetVersion","datasetId":6011136},{"sourceId":9822621,"sourceType":"datasetVersion","datasetId":6023107},{"sourceId":9822844,"sourceType":"datasetVersion","datasetId":6023276},{"sourceId":779,"sourceType":"modelInstanceVersion","modelInstanceId":646,"modelId":55}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport glob\nimport math\nimport numpy as np \nimport pandas as pd \nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nimport matplotlib.pyplot as plt\nimport tensorflow_hub as hub\nIMAGE_SIZE = [512, 512]","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:51.588770Z","iopub.execute_input":"2024-11-07T22:19:51.589345Z","iopub.status.idle":"2024-11-07T22:19:51.598174Z","shell.execute_reply.started":"2024-11-07T22:19:51.589293Z","shell.execute_reply":"2024-11-07T22:19:51.596731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv', index_col=0)","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:51.600782Z","iopub.execute_input":"2024-11-07T22:19:51.601294Z","iopub.status.idle":"2024-11-07T22:19:51.642068Z","shell.execute_reply.started":"2024-11-07T22:19:51.601243Z","shell.execute_reply":"2024-11-07T22:19:51.640194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fs = sub.index","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:51.643737Z","iopub.execute_input":"2024-11-07T22:19:51.644819Z","iopub.status.idle":"2024-11-07T22:19:51.649633Z","shell.execute_reply.started":"2024-11-07T22:19:51.644755Z","shell.execute_reply":"2024-11-07T22:19:51.648560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_tfrecord(filename):\n    filename = tf.strings.join([\"/kaggle/input/cassava-leaf-disease-classification/test_images/\", filename])\n    b = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(b)\n    image = tf.image.resize(image, [512, 512])\n    image = tf.cast(image, tf.float32) / 255.0\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:51.653306Z","iopub.execute_input":"2024-11-07T22:19:51.653946Z","iopub.status.idle":"2024-11-07T22:19:51.663153Z","shell.execute_reply.started":"2024-11-07T22:19:51.653889Z","shell.execute_reply":"2024-11-07T22:19:51.661761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\n\ndataset = tf.data.Dataset.from_tensor_slices(fs)\ndataset = dataset.map(read_tfrecord, num_parallel_calls=AUTO)\ndataset = dataset.prefetch(AUTO)\ndataset = dataset.batch(1)","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:51.665161Z","iopub.execute_input":"2024-11-07T22:19:51.666113Z","iopub.status.idle":"2024-11-07T22:19:51.730485Z","shell.execute_reply.started":"2024-11-07T22:19:51.666068Z","shell.execute_reply":"2024-11-07T22:19:51.729156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import kagglehub\npath = kagglehub.model_download(\"google/cropnet/tensorFlow2/classifier-cassava-disease-v1\")\n","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:51.732328Z","iopub.execute_input":"2024-11-07T22:19:51.732736Z","iopub.status.idle":"2024-11-07T22:19:52.055569Z","shell.execute_reply.started":"2024-11-07T22:19:51.732687Z","shell.execute_reply":"2024-11-07T22:19:52.054229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_hub as hub\nimport tf_keras as keras\ncassava = hub.KerasLayer(path)\nmodel = keras.Sequential([keras.Input(shape=(224,224,3)),\n                             cassava])","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:52.056974Z","iopub.execute_input":"2024-11-07T22:19:52.057336Z","iopub.status.idle":"2024-11-07T22:19:55.361933Z","shell.execute_reply.started":"2024-11-07T22:19:52.057301Z","shell.execute_reply":"2024-11-07T22:19:55.360602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor i, image in enumerate(dataset.as_numpy_iterator()):\n#     plt.imshow(image.reshape(512,512,3))\n#     plt.show()\n    image = tf.image.central_crop(image, central_fraction = 0.7)\n    image = tf.image.adjust_contrast(image, 1.6) #8.4530491e-01\n#     print(\"shape after crop\",image.shape)\n    image_crop = tf.image.resize(image, [224, 224])\n#     plt.imshow(image_crop.numpy().squeeze())\n    \n    pred = model.predict(image_crop)[0]\n#     print(pred)\n    sub.iloc[i, 0] = np.argmax(pred[0:5])","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:25:36.061534Z","iopub.execute_input":"2024-11-07T22:25:36.062583Z","iopub.status.idle":"2024-11-07T22:25:36.878529Z","shell.execute_reply.started":"2024-11-07T22:25:36.062528Z","shell.execute_reply":"2024-11-07T22:25:36.877164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:57.211350Z","iopub.execute_input":"2024-11-07T22:19:57.211954Z","iopub.status.idle":"2024-11-07T22:19:57.220558Z","shell.execute_reply.started":"2024-11-07T22:19:57.211899Z","shell.execute_reply":"2024-11-07T22:19:57.218895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-11-07T22:19:57.223566Z","iopub.execute_input":"2024-11-07T22:19:57.224453Z","iopub.status.idle":"2024-11-07T22:19:57.240562Z","shell.execute_reply.started":"2024-11-07T22:19:57.224396Z","shell.execute_reply":"2024-11-07T22:19:57.239211Z"},"trusted":true},"execution_count":null,"outputs":[]}]}