{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-07T22:07:31.410734Z","iopub.execute_input":"2021-11-07T22:07:31.411647Z","iopub.status.idle":"2021-11-07T22:07:31.420750Z","shell.execute_reply.started":"2021-11-07T22:07:31.411599Z","shell.execute_reply":"2021-11-07T22:07:31.419931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport tqdm\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:31.423644Z","iopub.execute_input":"2021-11-07T22:07:31.424410Z","iopub.status.idle":"2021-11-07T22:07:37.088123Z","shell.execute_reply.started":"2021-11-07T22:07:31.424372Z","shell.execute_reply":"2021-11-07T22:07:37.087370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To Extract the Zip Files ( Unpack )","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/content'\n# Extract files\nimport zipfile\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zipf:\n    zipf.extractall(path)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:37.089421Z","iopub.execute_input":"2021-11-07T22:07:37.089666Z","iopub.status.idle":"2021-11-07T22:07:49.547495Z","shell.execute_reply.started":"2021-11-07T22:07:37.089633Z","shell.execute_reply":"2021-11-07T22:07:49.546680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ntrain_path = path + '/train'\ntrain_dog_data = train_path + '/dog'\ntrain_cat_data = train_path + '/cat'\n\nvalid_path = path + '/valid'\nvalid_dog_data = valid_path + '/dog'\nvalid_cat_data = valid_path + '/cat'","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:49.549703Z","iopub.execute_input":"2021-11-07T22:07:49.549971Z","iopub.status.idle":"2021-11-07T22:07:49.557356Z","shell.execute_reply.started":"2021-11-07T22:07:49.549936Z","shell.execute_reply":"2021-11-07T22:07:49.556657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir(train_path)))\n#print(len(os.listdir(valid_path)))         ## as we see , there is an error because there is not valid .","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:49.558266Z","iopub.execute_input":"2021-11-07T22:07:49.558497Z","iopub.status.idle":"2021-11-07T22:07:49.595369Z","shell.execute_reply.started":"2021-11-07T22:07:49.558462Z","shell.execute_reply":"2021-11-07T22:07:49.594668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(os.listdir(train_path))   # it has all the images named like ( 'cat.2914.jpg',)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:49.596474Z","iopub.execute_input":"2021-11-07T22:07:49.597088Z","iopub.status.idle":"2021-11-07T22:07:49.601302Z","shell.execute_reply.started":"2021-11-07T22:07:49.597050Z","shell.execute_reply":"2021-11-07T22:07:49.600452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split cats and dogs images to ( train and valid datasets) ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n# Split cats and dogs images to train and valid datasets\nimg_filenames = os.listdir(train_path)\nprint('Num of images:', len(img_filenames))\n\ndog_filenames = [fn for fn in img_filenames if fn.startswith('dog')]\ncat_filenames = [fn for fn in img_filenames if fn.startswith('cat')]\n\ndataset_filenames = train_test_split(\n    dog_filenames, cat_filenames, test_size=0.1, shuffle=True, random_state=42\n)\n\ntrain_dog_total, valid_dog_total, train_cat_total, valid_cat_total = [len(fns) for fns in dataset_filenames]\ntrain_total = train_dog_total + train_cat_total\nvalid_total = valid_dog_total + valid_cat_total\nprint('Train: {}, test: {}'.format(train_total, valid_total))","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:49.602628Z","iopub.execute_input":"2021-11-07T22:07:49.603246Z","iopub.status.idle":"2021-11-07T22:07:49.645381Z","shell.execute_reply.started":"2021-11-07T22:07:49.603190Z","shell.execute_reply":"2021-11-07T22:07:49.644650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## After Dividing the Data , we need to put it in folders ","metadata":{}},{"cell_type":"code","source":"# Move images\nmake_dirs = [train_dog_data, valid_dog_data, train_cat_data, valid_cat_data]\nfor dir, fns in zip(make_dirs, dataset_filenames):\n    os.makedirs(dir, exist_ok=True)\n    for fn in tqdm.tqdm(fns):\n        shutil.move(os.path.join(train_path, fn), dir)\n    print('elements in {}: {}'.format(dir, len(os.listdir(dir))))","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:49.646560Z","iopub.execute_input":"2021-11-07T22:07:49.646877Z","iopub.status.idle":"2021-11-07T22:07:50.879261Z","shell.execute_reply.started":"2021-11-07T22:07:49.646841Z","shell.execute_reply":"2021-11-07T22:07:50.878473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = 224\nbatch_size= 64","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:50.880658Z","iopub.execute_input":"2021-11-07T22:07:50.881120Z","iopub.status.idle":"2021-11-07T22:07:50.890441Z","shell.execute_reply.started":"2021-11-07T22:07:50.881078Z","shell.execute_reply":"2021-11-07T22:07:50.888062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Preprocessing¶**\ndecode images \\\nrescale image layers from [0..255] to [0,1]","metadata":{}},{"cell_type":"code","source":"train_gen = ImageDataGenerator(                       # For data augmantention + loading  \n        rescale=1./255,\n        rotation_range=40,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True,\n        shear_range=0.2,\n        fill_mode=\"nearest\")            # repeating the last pixel (row) ","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:50.895042Z","iopub.execute_input":"2021-11-07T22:07:50.898129Z","iopub.status.idle":"2021-11-07T22:07:50.943248Z","shell.execute_reply.started":"2021-11-07T22:07:50.898088Z","shell.execute_reply":"2021-11-07T22:07:50.942368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_gen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:50.945106Z","iopub.execute_input":"2021-11-07T22:07:50.945863Z","iopub.status.idle":"2021-11-07T22:07:50.951894Z","shell.execute_reply.started":"2021-11-07T22:07:50.945823Z","shell.execute_reply":"2021-11-07T22:07:50.951162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = train_gen.flow_from_directory(\n        directory = train_path ,            # Training Data path \n        batch_size=batch_size,\n        target_size=(img_size,img_size),\n        class_mode=\"binary\")\n\nvalid_set = valid_gen.flow_from_directory(\n            directory=valid_path,\n             batch_size=batch_size,\n             target_size=(img_size,img_size),\n            class_mode=\"binary\")","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:50.953366Z","iopub.execute_input":"2021-11-07T22:07:50.953749Z","iopub.status.idle":"2021-11-07T22:07:51.631336Z","shell.execute_reply.started":"2021-11-07T22:07:50.953710Z","shell.execute_reply":"2021-11-07T22:07:51.630528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visulize some images to make sure that you read the data correctly ","metadata":{}},{"cell_type":"code","source":"# show 15 images\nsome_pets = next(train_set)[0][:15]        # next  : means the next Batch \nfig, axes = plt.subplots(3, 5, figsize=(20, 20))\nfor img, ax in zip(some_pets, axes.flatten()):\n    ax.imshow(img)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:51.632662Z","iopub.execute_input":"2021-11-07T22:07:51.633094Z","iopub.status.idle":"2021-11-07T22:07:54.892536Z","shell.execute_reply.started":"2021-11-07T22:07:51.633055Z","shell.execute_reply":"2021-11-07T22:07:54.891715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize Data Augmantation :","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef show_img(image):\n    fig,axes = plt.subplots( 1,5 , figsize=(20,20) )\n    axes = axes.flatten()\n    for img ,ax in zip(image,axes):\n        ax.imshow(img)\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:54.893655Z","iopub.execute_input":"2021-11-07T22:07:54.893929Z","iopub.status.idle":"2021-11-07T22:07:54.900396Z","shell.execute_reply.started":"2021-11-07T22:07:54.893890Z","shell.execute_reply":"2021-11-07T22:07:54.899676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Do augmantation on the first picture only , 5 times randomly .\naug_img = [train_set[0][0][0] for i in range(5)]\nshow_img(aug_img)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:54.902485Z","iopub.execute_input":"2021-11-07T22:07:54.902923Z","iopub.status.idle":"2021-11-07T22:07:59.606970Z","shell.execute_reply.started":"2021-11-07T22:07:54.902888Z","shell.execute_reply":"2021-11-07T22:07:59.606336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create the Model : ","metadata":{}},{"cell_type":"code","source":"# Import Feature Vector from Tensorflow hub\nimport tensorflow as tf \nimport tensorflow_hub as hub\nimg_size = 224\nmodel = tf.keras.Sequential([\n    hub.KerasLayer(\"https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/5\",\n                   trainable=False,output_shape=[1280]), \n    #outputshape : zay el flatten el b3mlha fe a5er el model\n    # trainable= false : means that im using the hyperparameters of the model.\n# Note : im training the last layer only , because the hyperparameters in the previous layers are fixed.\n    tf.keras.layers.Dense(2, activation='softmax')\n])\nmodel.build([None, 224, 224, 3])  # Batch input shape.\n","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:07:59.608254Z","iopub.execute_input":"2021-11-07T22:07:59.608661Z","iopub.status.idle":"2021-11-07T22:08:05.084663Z","shell.execute_reply.started":"2021-11-07T22:07:59.608618Z","shell.execute_reply":"2021-11-07T22:08:05.083949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(0.00115),\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:08:05.086014Z","iopub.execute_input":"2021-11-07T22:08:05.086257Z","iopub.status.idle":"2021-11-07T22:08:05.099213Z","shell.execute_reply.started":"2021-11-07T22:08:05.086224Z","shell.execute_reply":"2021-11-07T22:08:05.098495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# as we see here, the Trainable Params : 2,562  parameter Only \nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:08:05.100602Z","iopub.execute_input":"2021-11-07T22:08:05.100862Z","iopub.status.idle":"2021-11-07T22:08:05.115613Z","shell.execute_reply.started":"2021-11-07T22:08:05.100824Z","shell.execute_reply":"2021-11-07T22:08:05.114679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(\n    train_set,\n    validation_data=valid_set,\n    epochs=4\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:08:05.117138Z","iopub.execute_input":"2021-11-07T22:08:05.117612Z","iopub.status.idle":"2021-11-07T22:28:04.195386Z","shell.execute_reply.started":"2021-11-07T22:08:05.117402Z","shell.execute_reply":"2021-11-07T22:28:04.194607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_graphs(hist):\n    plt.figure(figsize=(12, 8))\n\n    plt.subplot(1, 2, 1)\n    plt.plot(hist.history['accuracy'], label='train')\n    plt.plot(hist.history['val_accuracy'], label='valid')\n    plt.legend(loc='lower right')\n    plt.title('Accuracy')\n\n    plt.subplot(1, 2, 2)\n    plt.plot(hist.history['loss'], label='train')\n    plt.plot(hist.history['val_loss'], label='valid')\n    plt.legend(loc='upper left')\n    plt.title('Loss (sparse_categorical_crossentropy)')\n\n    plt.show()\n    \nshow_graphs(hist)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:35:41.162535Z","iopub.execute_input":"2021-11-07T22:35:41.163116Z","iopub.status.idle":"2021-11-07T22:35:41.496285Z","shell.execute_reply.started":"2021-11-07T22:35:41.163078Z","shell.execute_reply":"2021-11-07T22:35:41.495572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = path + '/test'\n\n# Extract files\nimport zipfile\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as zipf:\n    zipf.extractall(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:37:29.751736Z","iopub.execute_input":"2021-11-07T22:37:29.752600Z","iopub.status.idle":"2021-11-07T22:37:37.082480Z","shell.execute_reply.started":"2021-11-07T22:37:29.752545Z","shell.execute_reply":"2021-11-07T22:37:37.081728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = ImageDataGenerator(rescale=1./255)\n\ntest_set = test_generator.flow_from_directory(\n    directory=test_data,\n    target_size=(img_size, img_size),\n    batch_size=1,\n    class_mode='binary',\n    shuffle=False\n)\ntest_set.reset()\n\ntest_total = len(test_set.filenames)\npredict = model.predict_generator(test_set, steps=test_total, verbose=1)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:43:48.480727Z","iopub.execute_input":"2021-11-07T22:43:48.481481Z","iopub.status.idle":"2021-11-07T22:45:33.589008Z","shell.execute_reply.started":"2021-11-07T22:43:48.481447Z","shell.execute_reply":"2021-11-07T22:45:33.587761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dogs-vs-cats/sampleSubmission.csv', index_col='id')\nsubmission['label'] = np.argmax(predict,axis=1)\nsubmission.to_csv('D:\\\\TekoMoro\\Cats_Dogs\\Cat_dogs_MahmoudOsama_sub.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:48:28.872876Z","iopub.execute_input":"2021-11-07T22:48:28.873635Z","iopub.status.idle":"2021-11-07T22:48:28.911880Z","shell.execute_reply.started":"2021-11-07T22:48:28.873597Z","shell.execute_reply":"2021-11-07T22:48:28.911201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}