{"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":34426,"databundleVersionId":3218632,"sourceType":"competition"},{"sourceId":1003830,"sourceType":"datasetVersion","datasetId":550917}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing the packages","metadata":{"_uuid":"91b7296f-1c39-4d66-848b-da54d0fc554e","_cell_guid":"ce00088b-67b2-48fb-a911-973dc2066239","trusted":true}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport cv2\nimport keras.models as M\nimport keras.layers as L\nimport os\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom IPython.display import clear_output\nimport keras","metadata":{"_uuid":"cd0d11b7-51fe-4e50-a1a3-df4d202670d5","_cell_guid":"1f8b5310-65a9-4615-90ac-8bb0d9fda1b6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:09:31.114109Z","iopub.execute_input":"2023-12-23T20:09:31.114484Z","iopub.status.idle":"2023-12-23T20:09:46.398039Z","shell.execute_reply.started":"2023-12-23T20:09:31.114455Z","shell.execute_reply":"2023-12-23T20:09:46.397179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Having a look at the data","metadata":{"_uuid":"b4f79da5-8a38-4c66-9735-74cb68786ea7","_cell_guid":"2baaadfd-89ed-4dd5-929d-5b4d3032d55c","trusted":true}},{"cell_type":"code","source":"image=Image.open('/kaggle/input/dog-vs-cat-classification/train/train/cats/cat.0.jpg')\nimage=image.convert('L')\nimage=np.array(image)\nplt.imshow(image)","metadata":{"_uuid":"cde8cc0c-11b5-41a5-a3fd-1e1b27175919","_cell_guid":"1cf806a2-158e-40aa-a22a-c674fe7f7e40","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:09:46.400116Z","iopub.execute_input":"2023-12-23T20:09:46.401174Z","iopub.status.idle":"2023-12-23T20:09:46.862613Z","shell.execute_reply.started":"2023-12-23T20:09:46.401112Z","shell.execute_reply":"2023-12-23T20:09:46.861678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Having a look at the histogram to get the threshold of the image","metadata":{"_uuid":"33492487-33b6-4846-b4ba-4bdc67d53076","_cell_guid":"62d80269-836f-45dd-8413-43f25333f49e","trusted":true}},{"cell_type":"code","source":"a,b,x=plt.hist(image.flatten())","metadata":{"_uuid":"247d9bbe-4524-4f1b-9e69-e8178ca82d5c","_cell_guid":"e5d6d6e5-be01-410c-8153-32a5a0e22f7c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:09:46.863814Z","iopub.execute_input":"2023-12-23T20:09:46.864147Z","iopub.status.idle":"2023-12-23T20:09:47.133097Z","shell.execute_reply.started":"2023-12-23T20:09:46.864101Z","shell.execute_reply":"2023-12-23T20:09:47.132298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Threshold looks like it lies between 135-160","metadata":{"_uuid":"84381fdf-e862-4548-9c33-87bc8fe968b3","_cell_guid":"acc841d7-c71f-48b7-9336-37e29efe84c9","trusted":true}},{"cell_type":"markdown","source":"# Let's Make a Function to get the threshold and make masked images","metadata":{"_uuid":"9383a20c-9312-408e-85f8-f5db525035ee","_cell_guid":"9c6be782-9533-471e-828e-6774886dd376","trusted":true}},{"cell_type":"code","source":"def get_threshold(image) :\n    a,b,x=plt.hist(image.flatten())\n    arg=np.argmax(a)\n    threshold=[b[arg],b[arg+1]]\n    plt.clf()\n    return threshold\ndef make_mask(path,color='blue',print_plot=False):\n    image=Image.open(path)\n    image=image.resize((150,150))\n    image=image.convert('L')\n    image=np.array(image)\n    threshold=get_threshold(image)\n    if color=='blue' :\n        n=[0,0,255]\n    masked_image=[]\n    for i in image.flatten() :\n        if i >= threshold[0] and i<=threshold[1] :\n            masked_image.append(255)\n        else:\n            masked_image.append(0)\n    masked_image=np.array(masked_image)\n    masked_image=masked_image.reshape(image.shape)\n    if print_plot==True :\n        plt.imshow(masked_image)\n    return masked_image","metadata":{"_uuid":"d83ff9c4-37c1-425b-aebd-6ea3fffdb7e5","_cell_guid":"7a67bb31-dd96-4e24-8e16-503b15d2ba37","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:09:47.135032Z","iopub.execute_input":"2023-12-23T20:09:47.135335Z","iopub.status.idle":"2023-12-23T20:09:47.143683Z","shell.execute_reply.started":"2023-12-23T20:09:47.135311Z","shell.execute_reply":"2023-12-23T20:09:47.142806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr=make_mask('/kaggle/input/dog-vs-cat-classification/train/train/cats/cat.1.jpg',print_plot=True)","metadata":{"_uuid":"1e1b666b-a991-4561-b9ef-79211d706de2","_cell_guid":"038217ba-0eae-4ad5-bcb6-64f874f3f2e6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:09:47.145057Z","iopub.execute_input":"2023-12-23T20:09:47.145447Z","iopub.status.idle":"2023-12-23T20:09:47.541389Z","shell.execute_reply.started":"2023-12-23T20:09:47.145418Z","shell.execute_reply":"2023-12-23T20:09:47.540483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making Masked Data (Only 1000 for each class since it takes some time)","metadata":{"_uuid":"5a209ab8-3fba-4651-87b2-11022b1044e7","_cell_guid":"3cdfdcf5-11b8-44ea-8e78-3d6c4d4d951c","trusted":true}},{"cell_type":"code","source":"# Adding the paths\ndog_path='/kaggle/input/dog-vs-cat-classification/train/train/dogs'\ncats_path= '/kaggle/input/dog-vs-cat-classification/train/train/cats'","metadata":{"_uuid":"1761ece4-3eaa-4341-85c6-69cf70fd3edd","_cell_guid":"d3b4d7a7-abc9-42dc-8a85-43173e01e78a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:09:47.542556Z","iopub.execute_input":"2023-12-23T20:09:47.542892Z","iopub.status.idle":"2023-12-23T20:09:47.547318Z","shell.execute_reply.started":"2023-12-23T20:09:47.54286Z","shell.execute_reply":"2023-12-23T20:09:47.54645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making the data\nX=[]\ny=[]\ncount=0\nfor i in os.listdir(dog_path)[:1000]:\n    print(count*100/1000)\n    clear_output(wait=True)\n    count+=1\n    X.append(make_mask(dog_path+'/'+i))\n\ny=[1]*1000","metadata":{"_uuid":"926017b1-4181-40f6-8a00-310e8af7d620","_cell_guid":"007fd73b-6981-4b73-b331-64d3a925c808","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:09:47.548505Z","iopub.execute_input":"2023-12-23T20:09:47.548782Z","iopub.status.idle":"2023-12-23T20:11:19.976343Z","shell.execute_reply.started":"2023-12-23T20:09:47.548759Z","shell.execute_reply":"2023-12-23T20:11:19.975467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making mask image data for cats\nx2=[]\ny2=[]\ncount=0\nfor i in os.listdir(cats_path)[:1000]:\n    print(count*100/1000)\n    clear_output(wait=True)\n    count+=1\n    x2.append(make_mask(cats_path+'/'+i))\ny2=[0]*1000","metadata":{"_uuid":"40cbcdd4-acdf-404c-9f8c-9d2ca1d9c2c2","_cell_guid":"0dfb736d-9b02-45fd-a6c5-d5882a7296a7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:11:19.977554Z","iopub.execute_input":"2023-12-23T20:11:19.978196Z","iopub.status.idle":"2023-12-23T20:12:50.816903Z","shell.execute_reply.started":"2023-12-23T20:11:19.978161Z","shell.execute_reply":"2023-12-23T20:12:50.815924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting all the data together\nX.extend(x2)\ny.extend(y2)\nX=np.array(X)\ny=np.array(y)\nX2=X/255","metadata":{"_uuid":"11aff020-4909-4553-9959-49965c458a15","_cell_guid":"639869fc-fed3-42f5-a4f5-ac4de6d0d2e3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:12:50.818035Z","iopub.execute_input":"2023-12-23T20:12:50.818346Z","iopub.status.idle":"2023-12-23T20:12:51.070339Z","shell.execute_reply.started":"2023-12-23T20:12:50.818319Z","shell.execute_reply":"2023-12-23T20:12:51.069435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Doing Train Test Split","metadata":{"_uuid":"05c18d17-9d17-4f80-9d9c-dde79c31ab02","_cell_guid":"2ff45e23-cf1e-4f03-bbf2-dda97865175c","trusted":true}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split( X2, y, test_size=0.15, random_state=42 )","metadata":{"_uuid":"787fb414-5476-49a8-ac52-b6d213a63611","_cell_guid":"f7aa1204-250e-40f8-9a5b-292ff2d5c79e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:12:51.073097Z","iopub.execute_input":"2023-12-23T20:12:51.073426Z","iopub.status.idle":"2023-12-23T20:12:51.179234Z","shell.execute_reply.started":"2023-12-23T20:12:51.073398Z","shell.execute_reply":"2023-12-23T20:12:51.178178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making the model CNN DIY","metadata":{"_uuid":"63bc5dac-7cb1-4e62-bfd6-b5a9f7ce3026","_cell_guid":"3a381fc6-57ae-44a0-81a1-fa1787396c04","trusted":true}},{"cell_type":"code","source":"model=M.Sequential()\nmodel.add(L.Reshape((150,150,1)))\nmodel.add(L.Conv2D(kernel_size=(3,3),filters=32,input_shape=(150,150),activation='relu'))\nmodel.add(L.MaxPool2D(pool_size=(2,2)))\nmodel.add(L.Conv2D(kernel_size=(3,3),filters=64,activation='relu'))\nmodel.add(L.MaxPool2D(pool_size=(2,2)))\nmodel.add(L.Flatten())\nmodel.add(L.Dense(1000,activation='tanh'))\nmodel.add(L.Dense(100,'relu'))\nmodel.add(L.Dense(2,activation='softmax'))","metadata":{"_uuid":"50591293-5caf-421c-b297-f4586df1180d","_cell_guid":"d88b62a4-3be0-46e8-8ac3-652c104c72fa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:12:51.180536Z","iopub.execute_input":"2023-12-23T20:12:51.180839Z","iopub.status.idle":"2023-12-23T20:12:57.20792Z","shell.execute_reply.started":"2023-12-23T20:12:51.180813Z","shell.execute_reply":"2023-12-23T20:12:57.207131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compiling the model","metadata":{"_uuid":"1deb97b1-82ae-4ea0-8122-a89280925d5d","_cell_guid":"d988aa7d-c24a-412a-957b-e7d71be57b44","trusted":true}},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"_uuid":"83304fc0-e6ab-4d8b-8686-f40cac8ac4da","_cell_guid":"02272bf6-c15a-4360-8ce9-af3d0027bdd7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:12:57.208998Z","iopub.execute_input":"2023-12-23T20:12:57.209283Z","iopub.status.idle":"2023-12-23T20:12:57.233739Z","shell.execute_reply.started":"2023-12-23T20:12:57.209258Z","shell.execute_reply":"2023-12-23T20:12:57.232856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training The model","metadata":{"_uuid":"e4dca1dc-34d2-44ad-b978-8e4abce3d895","_cell_guid":"1f9d6fff-0f0c-4a46-b259-795dc43bb74f","trusted":true}},{"cell_type":"code","source":"model.fit(X_train,y_train,epochs=20,validation_data=(X_test,y_test))","metadata":{"_uuid":"23f26028-0172-42b4-84df-47fe07306484","_cell_guid":"ca0ff982-7ff4-4088-8157-715a0806d05e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:19:40.800278Z","iopub.execute_input":"2023-12-23T20:19:40.800672Z","iopub.status.idle":"2023-12-23T20:21:04.536817Z","shell.execute_reply.started":"2023-12-23T20:19:40.800642Z","shell.execute_reply":"2023-12-23T20:21:04.535763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving the model","metadata":{"_uuid":"d20ac820-5a20-4df4-8af2-7af9d1f2e5de","_cell_guid":"b62a18a6-8479-4853-a293-821bf0ba796e","trusted":true}},{"cell_type":"code","source":"keras.models.save_model(model,'M_model.h5')","metadata":{"_uuid":"260ce26c-bee8-4d8b-976c-763d1119af7d","_cell_guid":"7e0a808b-909d-49a2-ac8b-16caf329dadc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-12-23T20:21:04.5384Z","iopub.execute_input":"2023-12-23T20:21:04.538721Z","iopub.status.idle":"2023-12-23T20:21:06.487869Z","shell.execute_reply.started":"2023-12-23T20:21:04.538693Z","shell.execute_reply":"2023-12-23T20:21:06.487023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making a function to do some predicitons for us","metadata":{"_uuid":"a3c34783-fb6a-4df7-b29b-cfc5a8025e2f","_cell_guid":"4744add8-5270-47f0-9a17-db72c77b839d","trusted":true}},{"cell_type":"code","source":"def make_predictions(path) :\n    l=['Cats','Dogs']\n    print('The masked image is :')\n    arr=make_mask(path,print_plot=True)\n    arr=np.array(arr)\n    arr=arr.reshape(1,150,150)\n    clas=np.argmax(model.predict(arr),axis=-1)\n    print(l[clas[0]])","metadata":{"_uuid":"8894e9f5-144f-4ff1-8147-40e2a97fb380","_cell_guid":"3002e41c-e863-4d84-9ef4-bea999802ab8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-14T20:28:58.145353Z","iopub.execute_input":"2023-11-14T20:28:58.146271Z","iopub.status.idle":"2023-11-14T20:28:58.1544Z","shell.execute_reply.started":"2023-11-14T20:28:58.146234Z","shell.execute_reply":"2023-11-14T20:28:58.152897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Use Pretrained Model","metadata":{}},{"cell_type":"markdown","source":"Using mobilenet V2","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras import layers, models\n\n# Load MobileNetV2 base model (excluding the top)\nbase_model = MobileNetV2(input_shape=(150, 150, 3), include_top=False, weights='imagenet')\n\n# Freeze the convolutional layers of MobileNetV2\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# Create your custom model on top of MobileNetV2\nmodel = models.Sequential()\nmodel.add(layers.Reshape((150, 150, 1), input_shape=(150, 150, 1)))\nmodel.add(layers.Conv2D(3, (3, 3), activation='relu'))  # Convert grayscale to RGB\nmodel.add(base_model)\nmodel.add(layers.GlobalAveragePooling2D())\nmodel.add(layers.Dense(1000, activation='tanh'))\nmodel.add(layers.Dense(100, activation='relu'))\nmodel.add(layers.Dense(2, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-12T09:59:13.588647Z","iopub.execute_input":"2023-12-12T09:59:13.589067Z","iopub.status.idle":"2023-12-12T09:59:15.588608Z","shell.execute_reply.started":"2023-12-12T09:59:13.589039Z","shell.execute_reply":"2023-12-12T09:59:15.587426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-12T09:47:16.360383Z","iopub.execute_input":"2023-12-12T09:47:16.360917Z","iopub.status.idle":"2023-12-12T09:47:16.411761Z","shell.execute_reply.started":"2023-12-12T09:47:16.360879Z","shell.execute_reply":"2023-12-12T09:47:16.408556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"_uuid":"4aa53caf-359c-463e-a1f6-7402e475ad91","_cell_guid":"c46a1acb-812e-4562-acac-482f7ae5378f","trusted":true}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-12-12T10:02:04.924159Z","iopub.execute_input":"2023-12-12T10:02:04.924594Z","iopub.status.idle":"2023-12-12T10:02:04.931262Z","shell.execute_reply.started":"2023-12-12T10:02:04.92456Z","shell.execute_reply":"2023-12-12T10:02:04.930129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-12T10:02:26.779552Z","iopub.execute_input":"2023-12-12T10:02:26.779957Z","iopub.status.idle":"2023-12-12T10:02:26.786425Z","shell.execute_reply.started":"2023-12-12T10:02:26.779929Z","shell.execute_reply":"2023-12-12T10:02:26.785199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nepochs = 10","metadata":{"execution":{"iopub.status.busy":"2023-12-12T10:02:39.754604Z","iopub.execute_input":"2023-12-12T10:02:39.755038Z","iopub.status.idle":"2023-12-12T10:02:39.759614Z","shell.execute_reply.started":"2023-12-12T10:02:39.755006Z","shell.execute_reply":"2023-12-12T10:02:39.758675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n# Convert labels to one-hot encoding\ny_train_encoded = to_categorical(y_train, num_classes=2)\ny_test_encoded = to_categorical(y_test, num_classes=2)\n\n# Fit the model\nbatch_size = 32\nepochs = 10\n\n# Use the ImageDataGenerator for data augmentation during training\ndatagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\ntrain_generator = datagen.flow(X_train, y_train_encoded, batch_size=batch_size)\n\nhistory = model.fit(train_generator, epochs=epochs, validation_data=(X_test, y_test_encoded))\n\n# Make predictions on test data\nX_test_predictions = model.predict(X_test)\n\n# Convert predictions to class labels\npredicted_labels = np.argmax(X_test_predictions, axis=1)\n\n# Evaluate accuracy\naccuracy = np.mean(predicted_labels == y_test)\nprint(f\"Test Accuracy: {accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-12T10:19:14.135549Z","iopub.execute_input":"2023-12-12T10:19:14.136075Z","iopub.status.idle":"2023-12-12T10:19:14.308703Z","shell.execute_reply.started":"2023-12-12T10:19:14.136033Z","shell.execute_reply":"2023-12-12T10:19:14.306847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}