{"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 libraries\nimport os\nimport zipfile\nimport glob\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport matplotlib.cm as cm\nimport keras\nimport cv2\n%matplotlib inline\n\n#import K-Means\nfrom sklearn.cluster import KMeans\n# important metrics\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-23T10:46:29.460970Z","iopub.execute_input":"2021-06-23T10:46:29.461583Z","iopub.status.idle":"2021-06-23T10:46:37.312945Z","shell.execute_reply.started":"2021-06-23T10:46:29.461551Z","shell.execute_reply":"2021-06-23T10:46:37.311820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:58:16.137138Z","iopub.execute_input":"2021-06-23T10:58:16.137510Z","iopub.status.idle":"2021-06-23T10:58:16.142171Z","shell.execute_reply.started":"2021-06-23T10:58:16.137470Z","shell.execute_reply":"2021-06-23T10:58:16.140648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Processing","metadata":{}},{"cell_type":"code","source":"zip_files = glob.glob('/kaggle/input/dogs-vs-cats/*.zip')\n\nprint('{} files found in the input directory'.format(str(len(zip_files))) +'\\n')\nfor file in zip_files:\n    with zipfile.ZipFile(file, 'r') as Z:\n        Z.extractall('data')\n    print ('{} is extracted'.format(file.split('/')[-1]) + '\\n')\n      \nprint('Extraction is completed' + '\\n')","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:46:37.314581Z","iopub.execute_input":"2021-06-23T10:46:37.314995Z","iopub.status.idle":"2021-06-23T10:46:58.007725Z","shell.execute_reply.started":"2021-06-23T10:46:37.314950Z","shell.execute_reply":"2021-06-23T10:46:58.006728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir(\"./data/train\")","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:46:58.009642Z","iopub.execute_input":"2021-06-23T10:46:58.009930Z","iopub.status.idle":"2021-06-23T10:46:58.014101Z","shell.execute_reply.started":"2021-06-23T10:46:58.009902Z","shell.execute_reply":"2021-06-23T10:46:58.012954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = []\nlabel = []\npath = \"./data/train/\"\nc = 0\nd = 0\nIMG_SIZE = 32\n\nfor file in os.listdir(path):\n    img=cv2.imread(path+file)\n    img=cv2.resize(img,(IMG_SIZE,IMG_SIZE))\n    img=img.astype('float32')\n    \n    if file[:3]=='cat':\n        if c==200: continue\n        c+=1\n        label.append(\"cat\")\n    else:\n        if d==200: continue\n        d+=1\n        label.append(\"dog\")\n    \n    data.append(img)\ndata = np.array(data)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:58:18.568142Z","iopub.execute_input":"2021-06-23T10:58:18.568750Z","iopub.status.idle":"2021-06-23T10:59:10.847057Z","shell.execute_reply.started":"2021-06-23T10:58:18.568705Z","shell.execute_reply":"2021-06-23T10:59:10.845983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converting label cat to 0 and dog to 1\ndata_label = []\nfor i in label:\n    if i==\"cat\": data_label.append(0)\n    else: data_label.append(1)\ndata_label = np.array(data_label)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:19.675560Z","iopub.execute_input":"2021-06-23T10:59:19.675923Z","iopub.status.idle":"2021-06-23T10:59:19.681413Z","shell.execute_reply.started":"2021-06-23T10:59:19.675891Z","shell.execute_reply":"2021-06-23T10:59:19.680574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Normalization\n\n# Normalization\ndata = data/255.0\n    \n# k-means accept data with less than 3 dimensions\nreshaped_data = data.reshape(len(data),-1)\nreshaped_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:20.933473Z","iopub.execute_input":"2021-06-23T10:59:20.933970Z","iopub.status.idle":"2021-06-23T10:59:20.943140Z","shell.execute_reply.started":"2021-06-23T10:59:20.933930Z","shell.execute_reply":"2021-06-23T10:59:20.942092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standard K-Means for image classification","metadata":{}},{"cell_type":"code","source":"# Applying K-Means Clustering\nkmeans = KMeans(n_clusters=2, random_state=0)\nclusters = kmeans.fit_predict(reshaped_data)\nkmeans.cluster_centers_.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:22.194208Z","iopub.execute_input":"2021-06-23T10:59:22.194700Z","iopub.status.idle":"2021-06-23T10:59:22.849793Z","shell.execute_reply.started":"2021-06-23T10:59:22.194668Z","shell.execute_reply":"2021-06-23T10:59:22.848695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This is how our centroids look like\nkmeans.cluster_centers_ = kmeans.cluster_centers_*255\nplt.figure(figsize = (10,9))\nbottom = 0.35\nfor i in range(2):\n    plt.subplots_adjust(bottom)\n    plt.subplot(4,4,i+1)\n    plt.imshow(kmeans.cluster_centers_[i].astype(int).reshape(32,32,3))","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:47:51.212043Z","iopub.execute_input":"2021-06-23T10:47:51.212398Z","iopub.status.idle":"2021-06-23T10:47:51.434810Z","shell.execute_reply.started":"2021-06-23T10:47:51.212365Z","shell.execute_reply":"2021-06-23T10:47:51.433805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scatter Plot for both the centroids\nx_data = [i for i in range(3072)]\nplt.scatter(x_data,kmeans.cluster_centers_[0], color = 'red',alpha=0.2,s=70)\nplt.scatter(x_data,kmeans.cluster_centers_[1] , color = 'blue',alpha=0.2,s=50)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:24.807366Z","iopub.execute_input":"2021-06-23T10:59:24.807755Z","iopub.status.idle":"2021-06-23T10:59:25.087702Z","shell.execute_reply.started":"2021-06-23T10:59:24.807724Z","shell.execute_reply":"2021-06-23T10:59:25.086640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Since K-Means Does not know about our labels we have to change labels of k-means according to our usage\n\n# mapping labels from cluster to original labels\ndef get_reference_dict(clusters,data_label):\n    reference_label = {}\n    # For loop to run through each label of cluster label\n    for i in range(len(np.unique(clusters))):\n        index = np.where(clusters == i,1,0)\n        num = np.bincount(data_label[index==1]).argmax()\n        reference_label[i] = num\n    return reference_label\n\n# Mapping predictions to original labels\ndef get_labels(clusters,refernce_labels):\n    temp_labels = np.random.rand(len(clusters))\n    for i in range(len(clusters)):\n        temp_labels[i] = reference_labels[clusters[i]]\n    return temp_labels","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:26.286361Z","iopub.execute_input":"2021-06-23T10:59:26.286737Z","iopub.status.idle":"2021-06-23T10:59:26.295689Z","shell.execute_reply.started":"2021-06-23T10:59:26.286707Z","shell.execute_reply":"2021-06-23T10:59:26.294306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reference_labels = get_reference_dict(clusters,data_label)\npredicted_labels = get_labels(clusters,reference_labels)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:27.791080Z","iopub.execute_input":"2021-06-23T10:59:27.791488Z","iopub.status.idle":"2021-06-23T10:59:27.798818Z","shell.execute_reply.started":"2021-06-23T10:59:27.791448Z","shell.execute_reply":"2021-06-23T10:59:27.797513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy Score for right predictions\nprint(accuracy_score(predicted_labels,data_label))","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:28.783910Z","iopub.execute_input":"2021-06-23T10:59:28.784267Z","iopub.status.idle":"2021-06-23T10:59:28.790640Z","shell.execute_reply.started":"2021-06-23T10:59:28.784238Z","shell.execute_reply":"2021-06-23T10:59:28.789520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mat = confusion_matrix(data_label,predicted_labels)\nsns.heatmap(mat, annot=True)\nplt.ylabel('true label')\nplt.xlabel('predicted label')","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:47:51.727629Z","iopub.execute_input":"2021-06-23T10:47:51.727917Z","iopub.status.idle":"2021-06-23T10:47:51.990520Z","shell.execute_reply.started":"2021-06-23T10:47:51.727881Z","shell.execute_reply":"2021-06-23T10:47:51.989571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Elbow Method: Find good value of K","metadata":{}},{"cell_type":"code","source":"# elbow method\n# squared distance\nsse = []\nlist_k = [2,16,64,100,256]\n\nfor k in list_k:\n    km = KMeans(n_clusters=k)\n    clusters = km.fit_predict(reshaped_data)\n    sse.append(km.inertia_)\n    \n    reference_labels = get_reference_dict(clusters,data_label)\n    predicted_labels = get_labels(clusters,reference_labels)\n    \n    print(f\"Accuracy for k = {k}: \", accuracy_score(predicted_labels,data_label))\n\n# Plot sse against k\nplt.figure(figsize=(6, 6))\nplt.plot(list_k, sse, '-o')\nplt.xlabel(r'Number of clusters *k*')\nplt.ylabel('Sum of squared distance');","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:47:51.992116Z","iopub.execute_input":"2021-06-23T10:47:51.992568Z","iopub.status.idle":"2021-06-23T10:49:39.262680Z","shell.execute_reply.started":"2021-06-23T10:47:51.992522Z","shell.execute_reply":"2021-06-23T10:49:39.261960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning","metadata":{}},{"cell_type":"code","source":"# Libraries\nfrom keras.applications.resnet50 import preprocess_input\nfrom keras.preprocessing import image","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:49:39.263815Z","iopub.execute_input":"2021-06-23T10:49:39.264257Z","iopub.status.idle":"2021-06-23T10:49:39.268480Z","shell.execute_reply.started":"2021-06-23T10:49:39.264226Z","shell.execute_reply":"2021-06-23T10:49:39.267519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features from resnet50\ndef extract_vector(data_holder):\n    resnet_feature_list = []\n\n    for im in data_holder:\n        im = image.img_to_array(im) \n#         print(\"dimensions of im: \", im.shape)\n        img = preprocess_input(np.expand_dims(im.copy(), axis=0))\n#         print(\"dimensions of img: \", img.shape)\n        resnet_feature = model.predict(img)\n        resnet_feature_np = np.array(resnet_feature)\n#         print(\"dimensions of resnet_feature_np: \", resnet_feature_np.shape)\n        resnet_feature_list.append(resnet_feature_np.flatten())\n\n    return np.array(resnet_feature_list)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:54:30.414084Z","iopub.execute_input":"2021-06-23T10:54:30.414571Z","iopub.status.idle":"2021-06-23T10:54:30.420072Z","shell.execute_reply.started":"2021-06-23T10:54:30.414540Z","shell.execute_reply":"2021-06-23T10:54:30.419295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Output <br>\ndimensions of im:  (32, 32, 3)<br>\ndimensions of img:  (1, 32, 32, 3)<br>\ndimensions of resnet_feature_np:  (1, 1, 1, 2048)<br>","metadata":{}},{"cell_type":"code","source":"# IMG_SIZE = 32\nmodel =   keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE,3))","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:54:32.789241Z","iopub.execute_input":"2021-06-23T10:54:32.789805Z","iopub.status.idle":"2021-06-23T10:54:34.888386Z","shell.execute_reply.started":"2021-06-23T10:54:32.789756Z","shell.execute_reply":"2021-06-23T10:54:34.887030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract vectors and apply kmeans\narray = extract_vector(data)\nkmeans = KMeans(n_clusters=2, random_state=0)\nclusters = kmeans.fit_predict(array)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:59:35.619602Z","iopub.execute_input":"2021-06-23T10:59:35.619953Z","iopub.status.idle":"2021-06-23T11:00:00.401306Z","shell.execute_reply.started":"2021-06-23T10:59:35.619925Z","shell.execute_reply":"2021-06-23T11:00:00.399905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reference_labels = get_reference_dict(clusters,data_label)\npredicted_labels = get_labels(clusters,reference_labels)","metadata":{"execution":{"iopub.status.busy":"2021-06-23T11:00:00.403861Z","iopub.execute_input":"2021-06-23T11:00:00.404349Z","iopub.status.idle":"2021-06-23T11:00:00.419396Z","shell.execute_reply.started":"2021-06-23T11:00:00.404305Z","shell.execute_reply":"2021-06-23T11:00:00.418021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint(accuracy_score(predicted_labels,data_label))","metadata":{"execution":{"iopub.status.busy":"2021-06-23T11:00:00.423143Z","iopub.execute_input":"2021-06-23T11:00:00.424601Z","iopub.status.idle":"2021-06-23T11:00:00.433885Z","shell.execute_reply.started":"2021-06-23T11:00:00.424548Z","shell.execute_reply":"2021-06-23T11:00:00.432628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mat = confusion_matrix(data_label,predicted_labels )\nsns.heatmap(mat, annot=True)\nplt.ylabel('true label')\nplt.xlabel('predicted label')","metadata":{"execution":{"iopub.status.busy":"2021-06-23T10:50:44.990468Z","iopub.execute_input":"2021-06-23T10:50:44.990760Z","iopub.status.idle":"2021-06-23T10:50:45.239799Z","shell.execute_reply.started":"2021-06-23T10:50:44.990734Z","shell.execute_reply":"2021-06-23T10:50:45.238851Z"},"trusted":true},"execution_count":null,"outputs":[]}]}