{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\nGCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')\nprint(GCS_DS_PATH)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-08T11:32:25.908366Z","iopub.execute_input":"2022-02-08T11:32:25.90863Z","iopub.status.idle":"2022-02-08T11:32:26.311514Z","shell.execute_reply.started":"2022-02-08T11:32:25.908603Z","shell.execute_reply":"2022-02-08T11:32:26.31047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom functools import partial\nimport matplotlib.pyplot as plt\n\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n    print(\"Device:\", tpu.master())\n    strategy = tf.distribute.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint(\"Number of replicas:\", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:32:27.763566Z","iopub.execute_input":"2022-02-08T11:32:27.763888Z","iopub.status.idle":"2022-02-08T11:32:35.875231Z","shell.execute_reply.started":"2022-02-08T11:32:27.76385Z","shell.execute_reply":"2022-02-08T11:32:35.874446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\nBATCH_SIZE = 64\nIMAGE_SIZE = [224, 224]","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:32:39.772096Z","iopub.execute_input":"2022-02-08T11:32:39.772357Z","iopub.status.idle":"2022-02-08T11:32:39.777165Z","shell.execute_reply.started":"2022-02-08T11:32:39.77233Z","shell.execute_reply":"2022-02-08T11:32:39.77616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAINING_FILENAMES = tf.io.gfile.glob(GCS_DS_PATH + \"/tfrecords-jpeg-224x224/train/*.tfrec\")\nVALIDATION_FILENAMES = tf.io.gfile.glob(GCS_DS_PATH + \"/tfrecords-jpeg-224x224/val/*.tfrec\")\nprint(\"Train TFRecord Files:\", len(TRAINING_FILENAMES))\nprint(\"val TFRecord Files:\", len(VALIDATION_FILENAMES))","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:32:40.820317Z","iopub.execute_input":"2022-02-08T11:32:40.820832Z","iopub.status.idle":"2022-02-08T11:32:40.961588Z","shell.execute_reply.started":"2022-02-08T11:32:40.820789Z","shell.execute_reply":"2022-02-08T11:32:40.960661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(image):\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.cast(image, tf.float32)\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:32:43.293289Z","iopub.execute_input":"2022-02-08T11:32:43.293894Z","iopub.status.idle":"2022-02-08T11:32:43.298654Z","shell.execute_reply.started":"2022-02-08T11:32:43.293846Z","shell.execute_reply":"2022-02-08T11:32:43.2979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_tfrecord(example, train):\n    tfrecord_format = (\n        {\n            \"image\": tf.io.FixedLenFeature([], tf.string),\n            \"class\": tf.io.FixedLenFeature([], tf.int64),\n        }\n        if train\n        else {\"image\": tf.io.FixedLenFeature([], tf.string),}\n    )\n    example = tf.io.parse_single_example(example, tfrecord_format)\n    image = decode_image(example[\"image\"])\n    if train:\n        label = tf.cast(example[\"class\"], tf.int32)\n        return image, label\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:32:43.999304Z","iopub.execute_input":"2022-02-08T11:32:44.000028Z","iopub.status.idle":"2022-02-08T11:32:44.006892Z","shell.execute_reply.started":"2022-02-08T11:32:43.999982Z","shell.execute_reply":"2022-02-08T11:32:44.006072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset(filenames, train=True):\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False  # disable order, increase speed\n    dataset = tf.data.TFRecordDataset(\n        filenames\n    )  # automatically interleaves reads from multiple files\n    dataset = dataset.with_options(\n        ignore_order\n    )  # uses data as soon as it streams in, rather than in its original order\n    dataset = dataset.map(\n        partial(read_tfrecord, train=train), num_parallel_calls=AUTOTUNE\n    )\n    # returns a dataset of (image, label) pairs if labeled=True or just images if labeled=False\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:32:47.313985Z","iopub.execute_input":"2022-02-08T11:32:47.314257Z","iopub.status.idle":"2022-02-08T11:32:47.320028Z","shell.execute_reply.started":"2022-02-08T11:32:47.314229Z","shell.execute_reply":"2022-02-08T11:32:47.319182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dataset(filenames, train=True):\n    dataset = load_dataset(filenames, train=train)\n    dataset = dataset.shuffle(2048)\n    dataset = dataset.prefetch(buffer_size=AUTOTUNE)\n    dataset = dataset.batch(BATCH_SIZE)\n    return dataset fail or down ايه الحل ؟\nفانا بجاوب اننا نستخدم","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:33:58.631338Z","iopub.execute_input":"2022-02-08T11:33:58.631844Z","iopub.status.idle":"2022-02-08T11:33:58.63693Z","shell.execute_reply.started":"2022-02-08T11:33:58.631799Z","shell.execute_reply":"2022-02-08T11:33:58.636204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = get_dataset(TRAINING_FILENAMES)\nvalidation_dataset = get_dataset(VALIDATION_FILENAMES)\n\nimage_batch, label_batch = next(iter(train_dataset))\n\n\ndef show_batch(image_batch, label_batch):\n    plt.figure(figsize=(10, 10))\n    for n in range(25):\n        ax = plt.subplot(5, 5, n + 1)\n        plt.imshow(image_batch[n] / 255.0)\n        if label_batch[n]:\n            plt.title(\"MALIGNANT\")\n        else:\n            plt.title(\"BENIGN\")\n        plt.axis(\"off\")\n\n\nshow_batch(image_batch.numpy(), label_batch.numpy())","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:34:00.34241Z","iopub.execute_input":"2022-02-08T11:34:00.342692Z","iopub.status.idle":"2022-02-08T11:34:02.51147Z","shell.execute_reply.started":"2022-02-08T11:34:00.342664Z","shell.execute_reply":"2022-02-08T11:34:02.510793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_learning_rate = 0.01\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate, decay_steps=20, decay_rate=0.96, staircase=True\n)\n\ncheckpoint_cb = tf.keras.callbacks.ModelCheckpoint(\n    \"melanoma_model.h5\", save_best_only=True\n)\n\nearly_stopping_cb = tf.keras.callbacks.EarlyStopping(\n    patience=10, restore_best_weights=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:34:03.060865Z","iopub.execute_input":"2022-02-08T11:34:03.06164Z","iopub.status.idle":"2022-02-08T11:34:03.066997Z","shell.execute_reply.started":"2022-02-08T11:34:03.0616Z","shell.execute_reply":"2022-02-08T11:34:03.066142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_model():\n    base_model = tf.keras.applications.ResNet50(\n        input_shape=(*IMAGE_SIZE, 3), include_top=False\n    )\n\n    base_model.trainable = False\n\n    inputs = tf.keras.layers.Input([*IMAGE_SIZE, 3])\n    x = tf.keras.applications.resnet.preprocess_input(inputs)\n    x = base_model(x)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dense(512, activation=\"relu\")(x)\n    x = tf.keras.layers.Dropout(0.2)(x)\n    outputs = tf.keras.layers.Dense(104, activation=\"softmax\")(x)\n\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n        loss=\"sparse_categorical_crossentropy\",\n        metrics=['accuracy'],\n    )\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:34:06.003958Z","iopub.execute_input":"2022-02-08T11:34:06.004349Z","iopub.status.idle":"2022-02-08T11:34:06.0126Z","shell.execute_reply.started":"2022-02-08T11:34:06.004321Z","shell.execute_reply":"2022-02-08T11:34:06.01172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    model = make_model()\n\nhistory = model.fit(\n    train_dataset,\n    epochs=20,\n    validation_data=validation_dataset,\n    callbacks=[checkpoint_cb, early_stopping_cb],\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:34:11.231351Z","iopub.execute_input":"2022-02-08T11:34:11.231661Z","iopub.status.idle":"2022-02-08T11:38:49.230146Z","shell.execute_reply.started":"2022-02-08T11:34:11.231627Z","shell.execute_reply":"2022-02-08T11:38:49.229201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.save(GCS_DS_PATH + \"/model_1\")","metadata":{"execution":{"iopub.status.busy":"2022-01-11T14:50:08.95415Z","iopub.execute_input":"2022-01-11T14:50:08.954421Z","iopub.status.idle":"2022-01-11T14:50:08.958842Z","shell.execute_reply.started":"2022-01-11T14:50:08.95439Z","shell.execute_reply":"2022-01-11T14:50:08.957847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_model2():\n    base_model = tf.keras.applications.Xception(\n        input_shape=(*IMAGE_SIZE, 3), include_top=False\n    )\n\n    base_model.trainable = False\n\n    inputs = tf.keras.layers.Input([*IMAGE_SIZE, 3])\n    x = tf.keras.applications.xception.preprocess_input(inputs)\n    x = base_model(x)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dense(512, activation=\"relu\")(x)\n    x = tf.keras.layers.Dropout(0.2)(x)\n    outputs = tf.keras.layers.Dense(104, activation=\"softmax\")(x)\n\n    model2 = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n    model2.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n        loss=\"sparse_categorical_crossentropy\",\n        metrics=['accuracy'],\n    )\n\n    return model2","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:42:08.06222Z","iopub.execute_input":"2022-02-08T11:42:08.062642Z","iopub.status.idle":"2022-02-08T11:42:08.072647Z","shell.execute_reply.started":"2022-02-08T11:42:08.062606Z","shell.execute_reply":"2022-02-08T11:42:08.071659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    model2 = make_model()\n\nhistory2 = model2.fit(\n    train_dataset,\n    epochs=20,\n    validation_data=validation_dataset,\n    callbacks=[checkpoint_cb, early_stopping_cb],\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:42:10.378028Z","iopub.execute_input":"2022-02-08T11:42:10.378348Z","iopub.status.idle":"2022-02-08T11:46:22.598495Z","shell.execute_reply.started":"2022-02-08T11:42:10.378314Z","shell.execute_reply":"2022-02-08T11:46:22.597362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\ndef show_batch_predictions(image_batch):\n    plt.figure(figsize=(10, 10))\n    for n in range(25):\n        ax = plt.subplot(5, 5, n + 1)\n        plt.imshow(image_batch[n] / 255.0)\n        img_array = tf.expand_dims(image_batch[n], axis=0)\n        plt.title(np.argmax(model.predict(img_array)[0]))\n        plt.axis(\"off\")\n    plt.show()\n\ndef test(image_batch):\n    predictions = []\n    for i in range(len(image_batch)):\n        img_array = tf.expand_dims(image_batch[i], axis=0)\n        predict = model.predict(img_array)[0].tolist()\n        predictions.append(predict.index(max(predict)))\n    return predictions\n\nlabels = []\nprediction_labels = []\nfor batch in validation_dataset:\n    image_batch, labels_batch = batch\n    prediction_labels_batch = test(image_batch)\n    labels.append(labels_batch)\n    prediction_labels.append(prediction_labels_batch)\n#     show_batch_predictions(image_batch)\n\nlabels = list(np.concatenate(labels).flat)\nprediction_labels = list(np.concatenate(prediction_labels).flat)\nconfusion_matrix(labels,prediction_labels)\n\nprint(sum((np.array(prediction_labels) == np.array(labels)).tolist())/len(labels))","metadata":{"execution":{"iopub.status.busy":"2022-02-08T11:46:22.600426Z","iopub.execute_input":"2022-02-08T11:46:22.600978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_batch_predictions(image_batch):\n    plt.figure(figsize=(10, 10))\n    for n in range(25):\n        ax = plt.subplot(5, 5, n + 1)\n        plt.imshow(image_batch[n] / 255.0)\n        img_array = tf.expand_dims(image_batch[n], axis=0)\n        plt.title(np.argmax(model2.predict(img_array)[0]))\n        plt.axis(\"off\")\n    plt.show()\n\ndef test(image_batch):\n    predictions = []\n    for i in range(len(image_batch)):\n        img_array = tf.expand_dims(image_batch[i], axis=0)\n        predict = model2.predict(img_array)[0].tolist()\n        predictions.append(np.argmax(predict))\n    return predictions\n\nlabels = []\nprediction_labels = []\nfor batch in validation_dataset:\n    image_batch, labels_batch = batch\n    prediction_labels_batch = test(image_batch)\n    labels.append(labels_batch)\n    prediction_labels.append(prediction_labels_batch)\n#     show_batch_predictions(image_batch)\n\nlabels = list(np.concatenate(labels).flat)\nprediction_labels = list(np.concatenate(prediction_labels).flat)\nconfusion_matrix(labels,prediction_labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = [model, model2]\nmodel_input = tf.keras.Input(shape=(224, 224, 3))\nmodel_outputs = [model(model_input) for model in models]\nensemble_output = tf.keras.layers.Average()(model_outputs)\nensemble_model = tf.keras.Model(inputs=model_input, outputs=ensemble_output)\n\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_batch_predictions(image_batch):\n    plt.figure(figsize=(10, 10))\n    for n in range(25):\n        ax = plt.subplot(5, 5, n + 1)\n        plt.imshow(image_batch[n] / 255.0)\n        img_array = tf.expand_dims(image_batch[n], axis=0)\n        plt.title(np.argmax(ensemble_model.predict(img_array)[0]))\n        plt.axis(\"off\")\n    plt.show()\n\ndef test(image_batch):\n    predictions = []\n    for i in range(len(image_batch)):\n        img_array = tf.expand_dims(image_batch[i], axis=0)\n        predict = ensemble_model.predict(img_array)[0].tolist()\n        predictions.append(predict.index(max(predict)))\n    return predictions\n\n\nlabels = []\nprediction_labels = []\nfor batch in validation_dataset:\n    image_batch, labels_batch = batch\n    prediction_labels_batch = test(image_batch)\n    labels.append(labels_batch)\n    prediction_labels.append(prediction_labels_batch)\n#     show_batch_predictions(image_batch)\n\nlabels = list(np.concatenate(labels).flat)\nprediction_labels = list(np.concatenate(prediction_labels).flat)\nconfusion_matrix(labels,prediction_labels)\n\nprint(sum((np.array(prediction_labels) == np.array(labels)).tolist())/len(labels))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import plot_confusion_matrix\nfrom sklearn.metrics import classification_report\nimport seaborn as sns\n\nclasses = ['pink primrose',    'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea',     'wild geranium',     'tiger lily',           'moon orchid',              'bird of paradise', 'monkshood',        'globe thistle',         # 00 - 09\n           'snapdragon',       \"colt's foot\",               'king protea',      'spear thistle', 'yellow iris',       'globe-flower',         'purple coneflower',        'peruvian lily',    'balloon flower',   'giant white arum lily', # 10 - 19\n           'fire lily',        'pincushion flower',         'fritillary',       'red ginger',    'grape hyacinth',    'corn poppy',           'prince of wales feathers', 'stemless gentian', 'artichoke',        'sweet william',         # 20 - 29\n           'carnation',        'garden phlox',              'love in the mist', 'cosmos',        'alpine sea holly',  'ruby-lipped cattleya', 'cape flower',              'great masterwort', 'siam tulip',       'lenten rose',           # 30 - 39\n           'barberton daisy',  'daffodil',                  'sword lily',       'poinsettia',    'bolero deep blue',  'wallflower',           'marigold',                 'buttercup',        'daisy',            'common dandelion',      # 40 - 49\n           'petunia',          'wild pansy',                'primula',          'sunflower',     'lilac hibiscus',    'bishop of llandaff',   'gaura',                    'geranium',         'orange dahlia',    'pink-yellow dahlia',    # 50 - 59\n           'cautleya spicata', 'japanese anemone',          'black-eyed susan', 'silverbush',    'californian poppy', 'osteospermum',         'spring crocus',            'iris',             'windflower',       'tree poppy',            # 60 - 69\n           'gazania',          'azalea',                    'water lily',       'rose',          'thorn apple',       'morning glory',        'passion flower',           'lotus',            'toad lily',        'anthurium',             # 70 - 79\n           'frangipani',       'clematis',                  'hibiscus',         'columbine',     'desert-rose',       'tree mallow',          'magnolia',                 'cyclamen ',        'watercress',       'canna lily',            # 80 - 89\n           'hippeastrum ',     'bee balm',                  'pink quill',       'foxglove',      'bougainvillea',     'camellia',             'mallow',                   'mexican petunia',  'bromelia',         'blanket flower',        # 90 - 99\n           'trumpet creeper',  'blackberry lily',           'common tulip',     'wild rose']\ndef confusion_matrix(true_labels,prediction_labels):\n    plt.figure(figsize=(10, 10))\n    cf = tf.math.confusion_matrix(true_labels,prediction_labels)\n    ax = sns.heatmap(cf, annot=False, cmap='Blues')\n    ax.set_title('Seaborn Confusion Matrix with labels\\n\\n');\n    ax.set_xlabel('\\nPredicted Values')\n    ax.set_ylabel('Actual Values ');\n\n    plt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot model\ntf.keras.utils.plot_model(\n    model,\n    to_file=\"model.png\",\n    show_shapes=False,\n    show_dtype=False,\n    show_layer_names=True,\n    rankdir=\"TB\",\n    expand_nested=False,\n    dpi=96,\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sum((np.array(prediction_labels) == np.array(labels)).tolist())/len(labels))","metadata":{"execution":{"iopub.status.busy":"2022-01-11T14:50:22.697766Z","iopub.status.idle":"2022-01-11T14:50:22.698084Z","shell.execute_reply.started":"2022-01-11T14:50:22.697916Z","shell.execute_reply":"2022-01-11T14:50:22.697937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}