{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport re,glob\nAUTO = tf.data.experimental.AUTOTUNE\nDIM = 600 \nIMAGE_SIZE=[DIM,DIM]\n\nBATCH_SIZE = 16  \nDATA_PATH = \"../input/ranzcr-clip-catheter-line-classification/\"\nOUTPUT_PATH = \"./\"\n\nimport sys\n\n!pip install ../input/kerasapplications/keras-applications-master/ \npackage_path = '../input/effici/efficientnet-master/'\nsys.path.append(package_path)\n\n#test\nfrom efficientnet import tfkeras as efn\n\nTEST_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":{"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":{"trusted":true},"cell_type":"code","source":"models1=[]  \n\n#for filename in glob.glob('../input/eff7trained/*s.h5'): #assuming gif  \nmodel = tf.keras.models.load_model('../input/efficetmodels/model_1s.h5/model_eff7-fold_1-best.h5', custom_objects = None )  \nmodels1.append(model)\nmodel = tf.keras.models.load_model('../input/efficetmodels/model_2s.h5/model_eff7-fold_2-best.h5', custom_objects = None )  \nmodels1.append(model)\nmodel = tf.keras.models.load_model('../input/efficetmodels/model_3s.h5/model_eff7-fold_3-best.h5', custom_objects = None )  \nmodels1.append(model)\nmodel = tf.keras.models.load_model('../input/efficetmodels/model_4s.h5/model_eff7-fold_4-best.h5', custom_objects = None )  \nmodels1.append(model)\n","execution_count":null,"outputs":[]},{"metadata":{"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))/4.0","execution_count":null,"outputs":[]},{"metadata":{"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') \n\nsubmission = 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)\n\nprint(\"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}