{"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":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Activation, MaxPooling2D, Conv2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:33:45.141637Z","iopub.execute_input":"2022-11-11T10:33:45.141996Z","iopub.status.idle":"2022-11-11T10:33:45.147972Z","shell.execute_reply.started":"2022-11-11T10:33:45.141967Z","shell.execute_reply":"2022-11-11T10:33:45.146800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\nIMG_SIZE = (224,224)\n\ntrain_data =tf.keras.preprocessing.image_dataset_from_directory(\"../input/dog-vs-cat-classification/train/train\",\n                                                          shuffle = True,\n                                                          image_size=IMG_SIZE,\n                                                          batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:09:21.759516Z","iopub.execute_input":"2022-11-11T10:09:21.760726Z","iopub.status.idle":"2022-11-11T10:09:26.384584Z","shell.execute_reply.started":"2022-11-11T10:09:21.760667Z","shell.execute_reply":"2022-11-11T10:09:26.383521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check out the class names of our dataset\ntrain_data.class_names","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:09:36.256577Z","iopub.execute_input":"2022-11-11T10:09:36.256946Z","iopub.status.idle":"2022-11-11T10:09:36.266103Z","shell.execute_reply.started":"2022-11-11T10:09:36.256916Z","shell.execute_reply":"2022-11-11T10:09:36.264988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train = train_data.prefetch(buffer_size=tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:09:53.587977Z","iopub.execute_input":"2022-11-11T10:09:53.588338Z","iopub.status.idle":"2022-11-11T10:09:53.594744Z","shell.execute_reply.started":"2022-11-11T10:09:53.588309Z","shell.execute_reply":"2022-11-11T10:09:53.593517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = tf.keras.applications.EfficientNetB0(include_top=False)\n\nbase_model.trainable = False\n\ninputs = tf.keras.layers.Input(shape=(224, 224, 3), name='InputLayer')\n\nx = base_model(inputs)\n\nx = tf.keras.layers.GlobalAveragePooling2D(name=\"GlobalAveragePoolingLayer\")(x)\n\noutputs = tf.keras.layers.Dense(10, activation='softmax', name=\"OutputLayer\")(x)\n\nmodel_0 = tf.keras.Model(inputs, outputs, name='ImageClassifierModel')\n\nmodel_0.compile(loss='sparse_categorical_crossentropy',\n                optimizer=tf.keras.optimizers.Adam(),\n                metrics=['accuracy'])\n\nhistory_0 = model_0.fit(Train,\n                        epochs=5,\n                        validation_data=Train,\n                        validation_steps=(0.25 * len(Train)))","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:11:49.901451Z","iopub.execute_input":"2022-11-11T10:11:49.901890Z","iopub.status.idle":"2022-11-11T10:19:02.409069Z","shell.execute_reply.started":"2022-11-11T10:11:49.901856Z","shell.execute_reply":"2022-11-11T10:19:02.407944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_0.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:19:02.411605Z","iopub.execute_input":"2022-11-11T10:19:02.412016Z","iopub.status.idle":"2022-11-11T10:19:02.428115Z","shell.execute_reply.started":"2022-11-11T10:19:02.411975Z","shell.execute_reply":"2022-11-11T10:19:02.427064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\n\nplot_model(model_0, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:19:02.429393Z","iopub.execute_input":"2022-11-11T10:19:02.430029Z","iopub.status.idle":"2022-11-11T10:19:03.731792Z","shell.execute_reply.started":"2022-11-11T10:19:02.429990Z","shell.execute_reply":"2022-11-11T10:19:03.730506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_0.history['accuracy'], label='accuracy')\nplt.plot(history_0.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right');","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:19:03.735731Z","iopub.execute_input":"2022-11-11T10:19:03.736750Z","iopub.status.idle":"2022-11-11T10:19:04.023470Z","shell.execute_reply.started":"2022-11-11T10:19:03.736705Z","shell.execute_reply":"2022-11-11T10:19:04.022576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_0.history['loss'], label='loss')\nplt.plot(history_0.history['val_loss'], label = 'val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.ylim([0.0, 1])\nplt.legend(loc='upper right');","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:19:04.024788Z","iopub.execute_input":"2022-11-11T10:19:04.025221Z","iopub.status.idle":"2022-11-11T10:19:04.264126Z","shell.execute_reply.started":"2022-11-11T10:19:04.025185Z","shell.execute_reply":"2022-11-11T10:19:04.263106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_batch, labels_batch in Train.take(1):\n    \n    first_img = img_batch[0].numpy().astype('uint8')\n    first_label = labels_batch[0].numpy()\n    \n    print('Image to predict ')\n    plt.imshow(first_img)\n    print('actual label: ',train_data.class_names[first_label])\n    \n    batch_prediction = model_0.predict(img_batch)\n    print('predicted batch: ',train_data.class_names[np.argmax(batch_prediction[0])])","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:19:44.609208Z","iopub.execute_input":"2022-11-11T10:19:44.609597Z","iopub.status.idle":"2022-11-11T10:19:46.714219Z","shell.execute_reply.started":"2022-11-11T10:19:44.609565Z","shell.execute_reply":"2022-11-11T10:19:46.713331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"../input/dog-vs-cat-classification/test/test\")\ntest_df = pd.DataFrame({\n    'id': test_filenames\n})","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:20:25.104499Z","iopub.execute_input":"2022-11-11T10:20:25.105174Z","iopub.status.idle":"2022-11-11T10:20:25.122208Z","shell.execute_reply.started":"2022-11-11T10:20:25.105137Z","shell.execute_reply":"2022-11-11T10:20:25.121096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator()\n\ntest_generator = test_datagen.flow_from_dataframe(\n    test_df,\n    \"../input/dog-vs-cat-classification/test/test\",\n    x_col='id',\n    y_col=None,\n    class_mode=None,\n    target_size=IMG_SIZE,\n    batch_size=BATCH_SIZE,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:22:42.687293Z","iopub.execute_input":"2022-11-11T10:22:42.687679Z","iopub.status.idle":"2022-11-11T10:22:45.794861Z","shell.execute_reply.started":"2022-11-11T10:22:42.687645Z","shell.execute_reply":"2022-11-11T10:22:45.793671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model_0.predict(test_generator)\ntest_df['labels'] = np.argmax(predict, axis=-1)\nprint(test_df)\ntest_df.set_index('id')\ntest_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T10:22:54.928111Z","iopub.execute_input":"2022-11-11T10:22:54.928500Z","iopub.status.idle":"2022-11-11T10:23:43.750299Z","shell.execute_reply.started":"2022-11-11T10:22:54.928443Z","shell.execute_reply":"2022-11-11T10:23:43.749290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}