{"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":"gpu","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":157253,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":133656,"modelId":156416}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras \nimport numpy as np \nimport re ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:31.173367Z","iopub.execute_input":"2024-11-05T18:23:31.174170Z","iopub.status.idle":"2024-11-05T18:23:31.178723Z","shell.execute_reply.started":"2024-11-05T18:23:31.174129Z","shell.execute_reply":"2024-11-05T18:23:31.177642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model= keras.models.load_model('/kaggle/input/cassava-xception/keras/default/1/best1.keras')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:31.180443Z","iopub.execute_input":"2024-11-05T18:23:31.180746Z","iopub.status.idle":"2024-11-05T18:23:39.365448Z","shell.execute_reply.started":"2024-11-05T18:23:31.180715Z","shell.execute_reply":"2024-11-05T18:23:39.364645Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:39.366792Z","iopub.execute_input":"2024-11-05T18:23:39.367102Z","iopub.status.idle":"2024-11-05T18:23:39.402038Z","shell.execute_reply.started":"2024-11-05T18:23:39.367068Z","shell.execute_reply":"2024-11-05T18:23:39.401123Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_filenames = tf.io.gfile.glob('/kaggle/input/cassava-leaf-disease-classification/test_tfrecords/*.tfrec')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:39.403918Z","iopub.execute_input":"2024-11-05T18:23:39.404223Z","iopub.status.idle":"2024-11-05T18:23:39.412775Z","shell.execute_reply.started":"2024-11-05T18:23:39.404191Z","shell.execute_reply":"2024-11-05T18:23:39.411848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def read_tfrec(example):\n    format = {\n        'image' : tf.io.FixedLenFeature([],tf.string),\n        'image_name' : tf.io.FixedLenFeature([],tf.string)\n    }\n    example = tf.io.parse_single_example(example,format)\n    image = tf.image.decode_jpeg(example['image'],channels=3)\n    image = tf.cast(image,tf.float32)/255.\n    image = tf.image.resize(image,(512,512))\n    image = tf.reshape(image,(512,512,3))\n    label = example['image_name']\n\n    return image,label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:39.413976Z","iopub.execute_input":"2024-11-05T18:23:39.414387Z","iopub.status.idle":"2024-11-05T18:23:39.422109Z","shell.execute_reply.started":"2024-11-05T18:23:39.414345Z","shell.execute_reply":"2024-11-05T18:23:39.421002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dset = tf.data.TFRecordDataset(test_filenames)\ndset = dset.map(read_tfrec)\ndset = dset.batch(32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:39.423576Z","iopub.execute_input":"2024-11-05T18:23:39.424293Z","iopub.status.idle":"2024-11-05T18:23:39.565095Z","shell.execute_reply.started":"2024-11-05T18:23:39.424260Z","shell.execute_reply":"2024-11-05T18:23:39.564085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = dset.map(lambda image,name : image)\nnames = dset.map(lambda image,name : name).unbatch()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:39.566218Z","iopub.execute_input":"2024-11-05T18:23:39.566506Z","iopub.status.idle":"2024-11-05T18:23:39.613088Z","shell.execute_reply.started":"2024-11-05T18:23:39.566476Z","shell.execute_reply":"2024-11-05T18:23:39.612135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prob = model.predict(image)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:39.614283Z","iopub.execute_input":"2024-11-05T18:23:39.614617Z","iopub.status.idle":"2024-11-05T18:23:42.031416Z","shell.execute_reply.started":"2024-11-05T18:23:39.614561Z","shell.execute_reply":"2024-11-05T18:23:42.030504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = np.argmax(prob,axis=-1).astype(np.int64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:42.034293Z","iopub.execute_input":"2024-11-05T18:23:42.034642Z","iopub.status.idle":"2024-11-05T18:23:42.039384Z","shell.execute_reply.started":"2024-11-05T18:23:42.034600Z","shell.execute_reply":"2024-11-05T18:23:42.038545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:42.040540Z","iopub.execute_input":"2024-11-05T18:23:42.040881Z","iopub.status.idle":"2024-11-05T18:23:42.050343Z","shell.execute_reply.started":"2024-11-05T18:23:42.040844Z","shell.execute_reply":"2024-11-05T18:23:42.049474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TEST_NUM = count_data_items(test_filenames)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:42.051495Z","iopub.execute_input":"2024-11-05T18:23:42.051843Z","iopub.status.idle":"2024-11-05T18:23:42.062403Z","shell.execute_reply.started":"2024-11-05T18:23:42.051811Z","shell.execute_reply":"2024-11-05T18:23:42.061441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ids = next(iter(names.batch(TEST_NUM))).numpy().astype('U') # all in one batch\nnp.savetxt('submission.csv', np.rec.fromarrays([test_ids, pred]), fmt=['%s', '%d'], delimiter=',', header='image_id,label', comments='')\n!head submission.csv","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T18:23:42.063507Z","iopub.execute_input":"2024-11-05T18:23:42.063830Z","iopub.status.idle":"2024-11-05T18:23:43.165284Z","shell.execute_reply.started":"2024-11-05T18:23:42.063797Z","shell.execute_reply":"2024-11-05T18:23:43.164089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}