{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":7463464,"sourceType":"datasetVersion","datasetId":4344114}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd \nimport seaborn as sns\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nimport tensorflow as tf\nfrom tensorflow.keras import models\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPool2D, BatchNormalization\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.losses import BinaryCrossentropy\nfrom keras.metrics import BinaryAccuracy\nfrom keras.optimizers import RMSprop\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-23T12:12:03.434003Z","iopub.execute_input":"2024-01-23T12:12:03.434301Z","iopub.status.idle":"2024-01-23T12:12:18.439840Z","shell.execute_reply.started":"2024-01-23T12:12:03.434262Z","shell.execute_reply":"2024-01-23T12:12:18.439014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q '/kaggle/input/dogs-vs-cats/train.zip'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_PATH = \"/kaggle/working/train/\"\nimage_files = os.listdir(INPUT_PATH)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_files = np.array(image_files)\nnp.random.shuffle(image_files)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(image_files)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 250\nn = 0\n\nX_train = []\nlabels = []\n\nfor idx, img_name in enumerate(image_files) :\n    if(idx%batch_size==0) and (idx!=0):\n        #save file\n        np.save(\"img_processed\"+str(n+1)+\".npy\", np.array(X_train))\n        np.save(\"labels\"+str(n+1)+\".npy\", np.array(labels))\n        n+=1\n        X_train = []\n        labels = []\n        print(f\"batch {n+1} done!\")\n    img = np.array(plt.imread(INPUT_PATH + image_files[idx]))\n    img = tf.image.resize(img , (224 , 224))\n    img = img/255.0\n    \n    ani = img_name.split('.')[0]\n    if (ani=='dog'):\n        label = [1.0, 0.0]\n    else:\n        label = [0.0, 1.0]\n    \n    X_train.append(img)\n    labels.append(label)\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b1 = np.load(\"img_processed1.npy\")\nbl1 = np.load(\"labels1.npy\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bl1[45]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(b1[45])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(Conv2D(6, (3, 3), activation='relu', padding = 'same', input_shape=(224, 224, 3)))\nmodel.add(MaxPool2D((2, 2)))\nmodel.add(Conv2D(16, (3, 3), activation='relu', padding = 'same'))\nmodel.add(MaxPool2D((2, 2)))\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding = 'same'))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(2, activation='softmax'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"100 batches (75 train, 25 test)","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate= 0.002),\n              loss=tf.keras.losses.CategoricalCrossentropy(),\n              metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoches =10\n\nfor i in range(epoches):\n    for j in range(75):\n        img_pro = np.load(\"img_processed\"+str(j+1)+\".npy\")\n        labels_ = np.load(\"labels\"+str(j+1)+\".npy\")\n        model.fit(img_pro, labels_)\n    print(\"epoch\", i+1, \"done!\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"dogVScat\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#evaluation\nacc = []\n\nn_test = 24\n\nfor i in range(n_test):\n    X_test = np.load(\"img_processed\"+str(i+76)+\".npy\")\n    Y_test = np.load(\"labels\"+str(i+76)+\".npy\")\n    \n    test_loss, test_accuracy = model.evaluate(X_test, Y_test)\n    acc.append(test_accuracy)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = np.load(\"img_processed80.npy\")\npred = model.predict(sample[1])\npred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample[1].shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(sample[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = np.array(acc)\nacc_mean = acc.mean()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_mean","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test submission","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q '/kaggle/input/dogs-vs-cats/test1.zip'","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:13:10.726485Z","iopub.execute_input":"2024-01-23T12:13:10.727465Z","iopub.status.idle":"2024-01-23T12:13:16.882227Z","shell.execute_reply.started":"2024-01-23T12:13:10.727426Z","shell.execute_reply":"2024-01-23T12:13:16.880913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_PATH = \"/kaggle/working/test1/\"\ntest_files = os.listdir(INPUT_PATH)","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:17:35.274356Z","iopub.execute_input":"2024-01-23T12:17:35.275148Z","iopub.status.idle":"2024-01-23T12:17:35.287298Z","shell.execute_reply.started":"2024-01-23T12:17:35.275117Z","shell.execute_reply":"2024-01-23T12:17:35.286526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"/kaggle/input/model-ja/model.keras\")","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:16:47.528068Z","iopub.execute_input":"2024-01-23T12:16:47.528909Z","iopub.status.idle":"2024-01-23T12:16:54.340431Z","shell.execute_reply.started":"2024-01-23T12:16:47.528876Z","shell.execute_reply":"2024-01-23T12:16:54.339409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = np.array(test_files)","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:17:36.672857Z","iopub.execute_input":"2024-01-23T12:17:36.673204Z","iopub.status.idle":"2024-01-23T12:17:36.682178Z","shell.execute_reply.started":"2024-01-23T12:17:36.673175Z","shell.execute_reply":"2024-01-23T12:17:36.681241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_files)","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:17:44.906019Z","iopub.execute_input":"2024-01-23T12:17:44.906399Z","iopub.status.idle":"2024-01-23T12:17:44.913242Z","shell.execute_reply.started":"2024-01-23T12:17:44.906364Z","shell.execute_reply":"2024-01-23T12:17:44.912263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files.sort()","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:48:44.695104Z","iopub.execute_input":"2024-01-23T12:48:44.695806Z","iopub.status.idle":"2024-01-23T12:48:44.701602Z","shell.execute_reply.started":"2024-01-23T12:48:44.695772Z","shell.execute_reply":"2024-01-23T12:48:44.700686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:48:49.173118Z","iopub.execute_input":"2024-01-23T12:48:49.173955Z","iopub.status.idle":"2024-01-23T12:48:49.179756Z","shell.execute_reply.started":"2024-01-23T12:48:49.173922Z","shell.execute_reply":"2024-01-23T12:48:49.178818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = np.array(plt.imread(INPUT_PATH + test_files[1]))\nsample.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:21:28.378268Z","iopub.execute_input":"2024-01-23T12:21:28.378721Z","iopub.status.idle":"2024-01-23T12:21:28.388472Z","shell.execute_reply.started":"2024-01-23T12:21:28.378690Z","shell.execute_reply":"2024-01-23T12:21:28.387540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = np.array(plt.imread(INPUT_PATH + test_files[1]))\nsample = tf.image.resize(sample , (224 , 224))\nsample = sample/255.0\nsample.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:22:46.480677Z","iopub.execute_input":"2024-01-23T12:22:46.481303Z","iopub.status.idle":"2024-01-23T12:22:46.494085Z","shell.execute_reply.started":"2024-01-23T12:22:46.481256Z","shell.execute_reply":"2024-01-23T12:22:46.493152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(np.array([sample]))\npred.argmax()","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:23:27.915874Z","iopub.execute_input":"2024-01-23T12:23:27.916812Z","iopub.status.idle":"2024-01-23T12:23:27.990531Z","shell.execute_reply.started":"2024-01-23T12:23:27.916770Z","shell.execute_reply":"2024-01-23T12:23:27.989492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = []\nlabelsss = []\n\nfor idx, img_name in enumerate(test_files) :\n    \n    sample = np.array(plt.imread(INPUT_PATH + test_files[idx]))\n    sample = tf.image.resize(sample , (224 , 224))\n    sample = sample/255.0\n    pred = model.predict(np.array([sample]))\n    idx_pred = pred.argmax()\n    \n    name_id = img_name.split('.')[0]\n    id_.append(name_id)\n    \n    if(idx_pred==1): \n        labelsss.append(0)\n    else:\n        labelsss.append(1)","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(id_)","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:41:25.426343Z","iopub.execute_input":"2024-01-23T12:41:25.426717Z","iopub.status.idle":"2024-01-23T12:41:25.432904Z","shell.execute_reply.started":"2024-01-23T12:41:25.426687Z","shell.execute_reply":"2024-01-23T12:41:25.431963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = [int(i) for i in id_]","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:50:17.857742Z","iopub.execute_input":"2024-01-23T12:50:17.858153Z","iopub.status.idle":"2024-01-23T12:50:17.869217Z","shell.execute_reply.started":"2024-01-23T12:50:17.858124Z","shell.execute_reply":"2024-01-23T12:50:17.868339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':id_, \"label\":labelsss})","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:50:20.048226Z","iopub.execute_input":"2024-01-23T12:50:20.048616Z","iopub.status.idle":"2024-01-23T12:50:20.065068Z","shell.execute_reply.started":"2024-01-23T12:50:20.048585Z","shell.execute_reply":"2024-01-23T12:50:20.064264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.set_index('id')","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:50:40.348021Z","iopub.execute_input":"2024-01-23T12:50:40.348669Z","iopub.status.idle":"2024-01-23T12:50:40.360836Z","shell.execute_reply.started":"2024-01-23T12:50:40.348636Z","shell.execute_reply":"2024-01-23T12:50:40.359713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for r in range(12):\n    print(submission.iloc[r])","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-23T12:48:12.363768Z","iopub.execute_input":"2024-01-23T12:48:12.364444Z","iopub.status.idle":"2024-01-23T12:48:12.373859Z","shell.execute_reply.started":"2024-01-23T12:48:12.364411Z","shell.execute_reply":"2024-01-23T12:48:12.372944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.sort_values(by=['id'], ascending=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:50:30.304880Z","iopub.execute_input":"2024-01-23T12:50:30.305590Z","iopub.status.idle":"2024-01-23T12:50:30.311615Z","shell.execute_reply.started":"2024-01-23T12:50:30.305555Z","shell.execute_reply":"2024-01-23T12:50:30.310705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:50:35.658975Z","iopub.execute_input":"2024-01-23T12:50:35.659725Z","iopub.status.idle":"2024-01-23T12:50:35.670375Z","shell.execute_reply.started":"2024-01-23T12:50:35.659690Z","shell.execute_reply":"2024-01-23T12:50:35.669326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-23T12:51:36.908510Z","iopub.execute_input":"2024-01-23T12:51:36.908903Z","iopub.status.idle":"2024-01-23T12:51:36.948722Z","shell.execute_reply.started":"2024-01-23T12:51:36.908873Z","shell.execute_reply":"2024-01-23T12:51:36.947818Z"},"trusted":true},"execution_count":null,"outputs":[]}]}