{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub\nimport pandas as pd\nimport numpy as np\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\n\nimport os\nimport datetime","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/dog-breed-identification/\"\nMODELS_PATH = \"/kaggle/working/models/\"\nLOGS_PATH = \"/kaggle/working/logs/\"\nOUTPUT_PATH = \"/kaggle/working/output/\"\n\nif not os.path.isdir(MODELS_PATH):\n    os.makedirs(MODELS_PATH)\nif not os.path.isdir(LOGS_PATH):\n    os.makedirs(LOGS_PATH)\nif not os.path.isdir(OUTPUT_PATH):\n    os.makedirs(OUTPUT_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels_csv = pd.read_csv(DATA_PATH + \"labels.csv\")\n\n#getting filenames\nfilenames = [DATA_PATH + f\"train/{fname}.jpg\" for fname in labels_csv[\"id\"]]\nfilenames[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = labels_csv.breed.values\nlabels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#unique breeds labels\nunique_breeds = np.unique(labels)\nprint(len(unique_breeds))\nprint(unique_breeds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# label into one-hot array\nprint(labels[0])\nlabels[0] == unique_breeds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"one_hot_labels = [label == unique_breeds for label in labels]\none_hot_labels[:2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = filenames\ny = one_hot_labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"NUM_IMAGES = 10222\nX_train, X_val, y_train, y_val = train_test_split(X[:NUM_IMAGES],\n                                                  y[:NUM_IMAGES],\n                                                  test_size=0.2,\n                                                  random_state=42)\n\nlen(X_train), len(X_val), len(y_train), len(y_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train[:2], y_train[:2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 224\n\n\n# Function for preprocessing images\ndef process_image(image_path, img_size=IMG_SIZE):\n    \"\"\"\n  Takes an image filepath and turns it into a Tensor\n  \"\"\"\n    # Read the image file\n    image = tf.io.read_file(image_path)\n    # Turn the jpeg image into numerical Tensor with 3 color channels (Red, Green, Blue)\n    image = tf.image.decode_jpeg(image, channels=3)\n    # Convert the color channels values range from 0-255 to 0-1\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    # Resize the image to our desired values (224, 224)\n    image = tf.image.resize(image, size=(img_size, img_size))\n    # Return the modified image\n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_image_label(image_path, label):\n    \"\"\"\n  Takes an image filepath name and the associated label, processes the image and return a tuple of (image, label)\n  \"\"\"\n    image = process_image(image_path)\n    return image, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 32\ndef create_data_batches(X,\n                        y=None,\n                        batch_size=BATCH_SIZE,\n                        valid_data=False,\n                        test_data=False):\n    \"\"\"\n  Creates batches of data out of image (X) and label (y) pairs. Shuffles the data if it's validation data.\n  Also accepts test data as input (no labels).\n  \"\"\"\n   \n    if test_data:\n        print(\"Creating test data batches...\")\n        data = tf.data.Dataset.from_tensor_slices(\n            (tf.constant(X)))  # only filepaths (no labels)\n        data_batch = data.map(process_image).batch(BATCH_SIZE)\n        return data_batch\n\n    \n    elif valid_data:\n        print(\"Creating validation data batches...\")\n        data = tf.data.Dataset.from_tensor_slices((\n            tf.constant(X),  # filepaths\n            tf.constant(y)))  # labels\n        data_batch = data.map(get_image_label).batch(BATCH_SIZE)\n        return data_batch\n\n    else:\n        print(\"Creating training data batches...\")\n        data = tf.data.Dataset.from_tensor_slices(\n            (tf.constant(X), tf.constant(y)))\n        \n        data = data.shuffle(buffer_size=len(X))\n\n       \n        data_batch = data.map(get_image_label).batch(BATCH_SIZE)\n\n        return data_batch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = create_data_batches(X_train, y_train)\nval_data = create_data_batches(X_val, y_val, valid_data=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.element_spec, val_data.element_spec","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_25_images(images, labels):\n    \"\"\"\n  Displays a plot of a 25 of images and their labels from a data batch.\n  \"\"\"\n   \n    plt.figure(figsize=(10, 10))\n    \n    for i in range(25):\n        ax = plt.subplot(5, 5, i + 1)\n        \n        plt.imshow(images[i])\n        \n        plt.title(unique_breeds[labels[i].argmax()])\n       \n        plt.axis(\"off\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images, train_labels = next(train_data.as_numpy_iterator())\nshow_25_images(train_images, train_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_images, val_labels = next(val_data.as_numpy_iterator())\n\nINPUT_SHAPE = [None, IMG_SIZE, IMG_SIZE,\n               3]  # batch, hieght, width, color channels\n\n# Setup output shape of our model\nOUTPUT_SHAPE = len(unique_breeds)\n\n# Setup the MobileNetV2 model URL from TensorFlow hub\nMODEL_URL = \"https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Function which builds a Keras model\ndef create_model(input_shape=INPUT_SHAPE,\n                 output_shape=OUTPUT_SHAPE,\n                 model_url=MODEL_URL):\n    print(\"Building model with:\", model_url)\n\n    # Setup the model layers\n    model = tf.keras.Sequential([\n        hub.KerasLayer(model_url),  # layer 1 (input layer)\n        tf.keras.layers.Dense(units=output_shape,\n                              activation=\"softmax\")  # layer 2 (output layer)\n    ])\n\n    # Compile the model\n    model.compile(loss=tf.keras.losses.CategoricalCrossentropy(),\n                  optimizer=tf.keras.optimizers.Adam(),\n                  metrics=[\"accuracy\"])\n\n    # Build the model\n    model.build(input_shape)\n\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%load_ext tensorboard","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_tensorboard_callback():\n    \n    logdir = os.path.join(\n        LOGS_PATH,  \n        datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n    return tf.keras.callbacks.TensorBoard(logdir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"early_stopping = tf.keras.callbacks.EarlyStopping(monitor=\"val_accuracy\",\n                                                  patience=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"NUM_EPOCHS = 100\n\n\n# Function to train and return a trained model\ndef train_model(num_epochs=NUM_EPOCHS):\n    \"\"\"\n  Trains a given model and return the trained version.\n  \"\"\"\n    # Create a model\n    model = create_model()\n\n    # Create a new TensorBoard session everytime we train a model\n    tensorboard = create_tensorboard_callback()\n\n    # Fit the model to the data passing it the callbacks we created\n    model.fit(x=train_data,\n              epochs=NUM_EPOCHS,\n              validation_data=val_data,\n              validation_freq=1,\n              callbacks=[tensorboard, early_stopping])\n\n    # Return the fitted model\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = train_model()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_path = save_model(model, suffix=\"1000_images_mobilenetv2_Adam\")\nfull_model_path=model_path\nloaded_full_model = load_model(model_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"loaded_full_model = load_model(full_model_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load test image filenames\ntest_path = DATA_PATH + \"test/\"\ntest_filenames = [test_path + fname for fname in os.listdir(test_path)]\ntest_filenames[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create test data batch\ntest_data = create_data_batches(test_filenames, test_data=True)\ntest_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntest_predictions = loaded_full_model.predict(test_data, verbose=1)\n\nnp.savetxt(OUTPUT_PATH + \"preds_array.csv\", test_predictions, delimiter=\",\")\n\ntest_predictions = np.loadtxt(OUTPUT_PATH + \"preds_array.csv\", delimiter=\",\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predictions.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\npreds_df = pd.DataFrame(columns=[\"id\"] + list(unique_breeds))\npreds_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntest_ids = [os.path.splitext(path)[0] for path in os.listdir(test_path)]\npreds_df[\"id\"] = test_ids\npreds_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds_df[list(unique_breeds)] = test_predictions\npreds_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Save  \npreds_df.to_csv(OUTPUT_PATH +\n                \"Coe6_101703122_Avani.csv\",\n                index=False)","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}