{"cells":[{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T15:54:50.488798Z","iopub.status.busy":"2021-03-01T15:54:50.478847Z","iopub.status.idle":"2021-03-01T15:55:01.965367Z","shell.execute_reply":"2021-03-01T15:55:01.964195Z"},"papermill":{"duration":11.519035,"end_time":"2021-03-01T15:55:01.965494","exception":false,"start_time":"2021-03-01T15:54:50.446459","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Sun Jul  5 18:07:41 2020\n\n@author: Vicky\n\"\"\"\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport re,glob\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T15:55:01.996925Z","iopub.status.busy":"2021-03-01T15:55:01.996079Z","iopub.status.idle":"2021-03-01T15:55:01.998954Z","shell.execute_reply":"2021-03-01T15:55:01.998438Z"},"papermill":{"duration":0.020946,"end_time":"2021-03-01T15:55:01.999050","exception":false,"start_time":"2021-03-01T15:55:01.978104","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\nDIM = 600 \nIMAGE_SIZE=[DIM,DIM]\n\nBATCH_SIZE = 8  \nDATA_PATH = \"../input/ranzcr-clip-catheter-line-classification/\"\nOUTPUT_PATH = \"./\"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T15:55:02.029964Z","iopub.status.busy":"2021-03-01T15:55:02.029028Z","iopub.status.idle":"2021-03-01T15:55:32.919317Z","shell.execute_reply":"2021-03-01T15:55:32.917670Z"},"papermill":{"duration":30.908676,"end_time":"2021-03-01T15:55:32.919446","exception":false,"start_time":"2021-03-01T15:55:02.010770","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import sys\n\n!pip install ../input/kerasapplications/keras-applications-master/ \npackage_path = '../input/efficientnetmaster/efficientnet-master/'\nsys.path.append(package_path)\n\n#test\nfrom efficientnet import tfkeras as efn","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T15:55:32.956160Z","iopub.status.busy":"2021-03-01T15:55:32.955171Z","iopub.status.idle":"2021-03-01T15:55:32.968397Z","shell.execute_reply":"2021-03-01T15:55:32.967506Z"},"papermill":{"duration":0.032779,"end_time":"2021-03-01T15:55:32.968504","exception":false,"start_time":"2021-03-01T15:55:32.935725","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"TEST_FILENAMES = tf.io.gfile.glob('../input/ranzcr-clip-catheter-line-classification/test_tfrecords/*.tfrec') # predictions on this dataset should be submitted for the competition\nprint(TEST_FILENAMES) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T15:55:33.060195Z","iopub.status.busy":"2021-03-01T15:55:33.058154Z","iopub.status.idle":"2021-03-01T15:55:33.061140Z","shell.execute_reply":"2021-03-01T15:55:33.061662Z"},"papermill":{"duration":0.039243,"end_time":"2021-03-01T15:55:33.061785","exception":false,"start_time":"2021-03-01T15:55:33.022542","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0  # convert image to floats in [0, 1] range\n    image = tf.image.resize(image, [DIM, DIM])\n    image = tf.reshape(image, [*IMAGE_SIZE, 3]) # explicit size needed for TPU\n    return image\n\ndef read_unlabeled_tfrecord(example):\n\n    UNLABELED_TFREC_FORMAT = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string)\n    }\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    idnum = example['StudyInstanceUID']\n    return image, idnum # returns a dataset of image(s)\n\ndef load_dataset(filenames, labeled = True, ordered = False):\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n        \n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads = AUTO) # automatically interleaves reads from multiple files\n    dataset = dataset.with_options(ignore_order) # use data as soon as it streams in, rather than in its original order\n    dataset = dataset.map(read_unlabeled_tfrecord, num_parallel_calls = AUTO) # returns a dataset of (image, label) pairs if labeled = True or (image, id) pair if labeld = False\n    return dataset\n\ndef get_test_dataset(ordered=False):\n    dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\n\ndef 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)\n\nNUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T15:55:33.099033Z","iopub.status.busy":"2021-03-01T15:55:33.098283Z","iopub.status.idle":"2021-03-01T15:56:56.096521Z","shell.execute_reply":"2021-03-01T15:56:56.095335Z"},"papermill":{"duration":83.019232,"end_time":"2021-03-01T15:56:56.096663","exception":false,"start_time":"2021-03-01T15:55:33.077431","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"models1=[]  \n\nfor filename in glob.glob('../input/eff7trained/*best.h5'): #assuming gif  \n    model = tf.keras.models.load_model(filename, custom_objects = None )  \n    models1.append(model)\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T15:56:56.183567Z","iopub.status.busy":"2021-03-01T15:56:56.182753Z","iopub.status.idle":"2021-03-01T16:05:27.558446Z","shell.execute_reply":"2021-03-01T16:05:27.557287Z"},"papermill":{"duration":511.402841,"end_time":"2021-03-01T16:05:27.558604","exception":false,"start_time":"2021-03-01T15:56:56.155763","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"test_ds = get_test_dataset(ordered=True) \ntest_images_ds = test_ds.map(lambda image, idnum: image)\n\nlabels = ['ETT - Abnormal', 'ETT - Borderline',\n       'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n       'NGT - Incompletely Imaged', 'NGT - Normal', 'CVC - Abnormal',\n       'CVC - Borderline', 'CVC - Normal', 'Swan Ganz Catheter Present']\nmean =(models1[0].predict(test_images_ds) \n       +models1[1].predict(test_images_ds) \n       +models1[2].predict(test_images_ds) \n       +models1[3].predict(test_images_ds) \n       +models1[4].predict(test_images_ds))/5.0","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T16:05:27.672290Z","iopub.status.busy":"2021-03-01T16:05:27.671335Z","iopub.status.idle":"2021-03-01T16:06:02.504964Z","shell.execute_reply":"2021-03-01T16:06:02.504247Z"},"papermill":{"duration":34.855889,"end_time":"2021-03-01T16:06:02.505098","exception":false,"start_time":"2021-03-01T16:05:27.649209","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"test_ids_ds = test_ds.map(lambda image, idnum: idnum).unbatch()\ntest_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES))).numpy().astype('U') ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T16:06:02.544684Z","iopub.status.busy":"2021-03-01T16:06:02.543848Z","iopub.status.idle":"2021-03-01T16:06:02.790998Z","shell.execute_reply":"2021-03-01T16:06:02.790398Z"},"papermill":{"duration":0.269822,"end_time":"2021-03-01T16:06:02.791111","exception":false,"start_time":"2021-03-01T16:06:02.521289","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame(mean, columns = labels)\n\nsubmission.insert(0, \"StudyInstanceUID\", test_ids, False) \nsubmission['StudyInstanceUID'] = submission['StudyInstanceUID'].apply(lambda x: x.rstrip(\".jpg\"))\nsubmission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-01T16:06:02.827636Z","iopub.status.busy":"2021-03-01T16:06:02.826783Z","iopub.status.idle":"2021-03-01T16:06:02.831537Z","shell.execute_reply":"2021-03-01T16:06:02.830831Z"},"papermill":{"duration":0.024583,"end_time":"2021-03-01T16:06:02.831670","exception":false,"start_time":"2021-03-01T16:06:02.807087","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"print(\"Done\")","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}