{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.9","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":9058,"sourceType":"datasetVersion","datasetId":6156},{"sourceId":28903,"sourceType":"datasetVersion","datasetId":22535},{"sourceId":30378,"sourceType":"datasetVersion","datasetId":23777},{"sourceId":269359,"sourceType":"datasetVersion","datasetId":111880},{"sourceId":791828,"sourceType":"datasetVersion","datasetId":119698},{"sourceId":1003830,"sourceType":"datasetVersion","datasetId":550917}],"dockerImageVersionId":30060,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Masking And Trying CNN On It","metadata":{}},{"cell_type":"markdown","source":"In this file i tried something new for me . I have created masks for the dogs and cats images using threshold and then trained the model on it and done some predictions . ","metadata":{}},{"cell_type":"markdown","source":"# Importing the packages","metadata":{}},{"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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-08T16:51:54.861648Z","iopub.execute_input":"2024-08-08T16:51:54.862075Z","iopub.status.idle":"2024-08-08T16:52:00.779405Z","shell.execute_reply.started":"2024-08-08T16:51:54.861991Z","shell.execute_reply":"2024-08-08T16:52:00.778623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Having a look at the data","metadata":{}},{"cell_type":"code","source":"image=Image.open('../input/dogs-cats-images/dataset/training_set/dogs/dog.2.jpg')\nimage=image.convert('L')\nimage=np.array(image)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:52:00.781180Z","iopub.execute_input":"2024-08-08T16:52:00.781452Z","iopub.status.idle":"2024-08-08T16:52:01.024644Z","shell.execute_reply.started":"2024-08-08T16:52:00.781426Z","shell.execute_reply":"2024-08-08T16:52:01.023885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Having a look at the histogram to get the threshold of the image ","metadata":{}},{"cell_type":"code","source":"a,b,x=plt.hist(image.flatten())\n","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:52:01.025946Z","iopub.execute_input":"2024-08-08T16:52:01.026235Z","iopub.status.idle":"2024-08-08T16:52:01.184052Z","shell.execute_reply.started":"2024-08-08T16:52:01.026207Z","shell.execute_reply":"2024-08-08T16:52:01.183260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Threshold looks like it lies between 135-160","metadata":{}},{"cell_type":"markdown","source":"# Let's Make a Function to get the threshold and make masked images","metadata":{}},{"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\n        \n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:52:01.185013Z","iopub.execute_input":"2024-08-08T16:52:01.185280Z","iopub.status.idle":"2024-08-08T16:52:01.193662Z","shell.execute_reply.started":"2024-08-08T16:52:01.185247Z","shell.execute_reply":"2024-08-08T16:52:01.192878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr=make_mask('../input/dogs-cats-images/dataset/training_set/dogs/dog.1001.jpg',print_plot=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:52:01.196470Z","iopub.execute_input":"2024-08-08T16:52:01.196808Z","iopub.status.idle":"2024-08-08T16:52:01.553324Z","shell.execute_reply.started":"2024-08-08T16:52:01.196780Z","shell.execute_reply":"2024-08-08T16:52:01.552374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making Masked Data (Only 1000 for each class since it takes some time)","metadata":{}},{"cell_type":"code","source":"# Adding the paths\ndog_path='../input/dogs-cats-images/dataset/training_set/dogs'\ncats_path='../input/dogs-cats-images/dataset/training_set/cats'","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:52:01.555074Z","iopub.execute_input":"2024-08-08T16:52:01.555448Z","iopub.status.idle":"2024-08-08T16:52:01.559332Z","shell.execute_reply.started":"2024-08-08T16:52:01.555411Z","shell.execute_reply":"2024-08-08T16:52:01.558415Z"},"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":{"execution":{"iopub.status.busy":"2024-08-08T16:52:01.560683Z","iopub.execute_input":"2024-08-08T16:52:01.561078Z","iopub.status.idle":"2024-08-08T16:55:15.905428Z","shell.execute_reply.started":"2024-08-08T16:52:01.561042Z","shell.execute_reply":"2024-08-08T16:55:15.904691Z"},"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":{"execution":{"iopub.status.busy":"2024-08-08T16:55:15.906530Z","iopub.execute_input":"2024-08-08T16:55:15.906859Z","iopub.status.idle":"2024-08-08T16:58:30.053244Z","shell.execute_reply.started":"2024-08-08T16:55:15.906830Z","shell.execute_reply":"2024-08-08T16:58:30.052357Z"},"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":{"execution":{"iopub.status.busy":"2024-08-08T16:58:30.054475Z","iopub.execute_input":"2024-08-08T16:58:30.054882Z","iopub.status.idle":"2024-08-08T16:58:30.297972Z","shell.execute_reply.started":"2024-08-08T16:58:30.054851Z","shell.execute_reply":"2024-08-08T16:58:30.297031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Doing Train Test Split","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-08-08T16:58:30.299431Z","iopub.execute_input":"2024-08-08T16:58:30.299773Z","iopub.status.idle":"2024-08-08T16:58:30.404539Z","shell.execute_reply.started":"2024-08-08T16:58:30.299742Z","shell.execute_reply":"2024-08-08T16:58:30.403674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making the model","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-08-08T16:58:30.405889Z","iopub.execute_input":"2024-08-08T16:58:30.406200Z","iopub.status.idle":"2024-08-08T16:58:33.710703Z","shell.execute_reply.started":"2024-08-08T16:58:30.406169Z","shell.execute_reply":"2024-08-08T16:58:33.709935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Having a look at the data","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compiling the model","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:59:04.795531Z","iopub.execute_input":"2024-08-08T16:59:04.795923Z","iopub.status.idle":"2024-08-08T16:59:04.813441Z","shell.execute_reply.started":"2024-08-08T16:59:04.795890Z","shell.execute_reply":"2024-08-08T16:59:04.812537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training The model","metadata":{}},{"cell_type":"code","source":"model.fit(X_train,y_train,epochs=20,validation_data=(X_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:59:06.566168Z","iopub.execute_input":"2024-08-08T16:59:06.566495Z","iopub.status.idle":"2024-08-08T16:59:51.807206Z","shell.execute_reply.started":"2024-08-08T16:59:06.566466Z","shell.execute_reply":"2024-08-08T16:59:51.806094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving the model","metadata":{}},{"cell_type":"code","source":"keras.models.save_model(model,'mask_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:59:54.037745Z","iopub.execute_input":"2024-08-08T16:59:54.038097Z","iopub.status.idle":"2024-08-08T16:59:56.044672Z","shell.execute_reply.started":"2024-08-08T16:59:54.038063Z","shell.execute_reply":"2024-08-08T16:59:56.043909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making a function to do some predicitons for us","metadata":{}},{"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]])\n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:59:56.048448Z","iopub.execute_input":"2024-08-08T16:59:56.048808Z","iopub.status.idle":"2024-08-08T16:59:56.055376Z","shell.execute_reply.started":"2024-08-08T16:59:56.048775Z","shell.execute_reply":"2024-08-08T16:59:56.054564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Doing some predictions","metadata":{}},{"cell_type":"code","source":"\nmake_predictions('../input/dogs-cats-images/dataset/test_set/dogs/dog.4005.jpg')","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:59:58.296012Z","iopub.execute_input":"2024-08-08T16:59:58.296368Z","iopub.status.idle":"2024-08-08T16:59:58.861034Z","shell.execute_reply.started":"2024-08-08T16:59:58.296338Z","shell.execute_reply":"2024-08-08T16:59:58.860077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_predictions('../input/dogs-cats-images/dataset/test_set/cats/cat.4008.jpg')","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:59:58.862760Z","iopub.execute_input":"2024-08-08T16:59:58.863037Z","iopub.status.idle":"2024-08-08T16:59:59.305087Z","shell.execute_reply.started":"2024-08-08T16:59:58.863010Z","shell.execute_reply":"2024-08-08T16:59:59.304126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# I can't understand the second image at all but our model predicted it right :)","metadata":{}},{"cell_type":"markdown","source":"# Thank you :)","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}