{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom keras import layers\nfrom keras import models\nfrom tensorflow.keras import optimizers\nfrom keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\nfrom tensorflow.keras import applications\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn import metrics\n\nimport glob\nimport os\nprint(\"Cats&Dogs Dataset Folder Contain:\",os.listdir(\"../input\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"id":"-ba3ROo0Lhel","outputId":"f8cf3725-5436-4ea5-c7d5-bcf1934aaacc","execution":{"iopub.status.busy":"2022-10-09T19:29:08.814316Z","iopub.execute_input":"2022-10-09T19:29:08.814676Z","iopub.status.idle":"2022-10-09T19:29:08.823072Z","shell.execute_reply.started":"2022-10-09T19:29:08.814644Z","shell.execute_reply":"2022-10-09T19:29:08.821957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\n\nzip_files = ['test1', 'train']\n# Will unzip the files so that you can see them..\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"id":"uT7HTCBsLhen","outputId":"21ae8149-2ead-4eea-a004-fb8d4af403f2","execution":{"iopub.status.busy":"2022-10-09T19:24:43.610276Z","iopub.execute_input":"2022-10-09T19:24:43.612337Z","iopub.status.idle":"2022-10-09T19:25:01.953503Z","shell.execute_reply.started":"2022-10-09T19:24:43.612260Z","shell.execute_reply":"2022-10-09T19:25:01.951336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../input'))","metadata":{"id":"cqMQNVpRLhen","execution":{"iopub.status.busy":"2022-10-09T19:25:07.383678Z","iopub.execute_input":"2022-10-09T19:25:07.384527Z","iopub.status.idle":"2022-10-09T19:25:07.392272Z","shell.execute_reply.started":"2022-10-09T19:25:07.384495Z","shell.execute_reply":"2022-10-09T19:25:07.390694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FOLDER_PATH=\"../working/train\"\nFILE_NAMES=os.listdir(IMAGE_FOLDER_PATH)\nWIDTH=150\nHEIGHT=150","metadata":{"_kg_hide-input":true,"id":"ZtwOeijeLhen","execution":{"iopub.status.busy":"2022-10-09T19:25:07.395583Z","iopub.execute_input":"2022-10-09T19:25:07.396252Z","iopub.status.idle":"2022-10-09T19:25:07.415669Z","shell.execute_reply.started":"2022-10-09T19:25:07.396216Z","shell.execute_reply":"2022-10-09T19:25:07.414840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets=list()\nfull_paths=list()\nfor file_name in FILE_NAMES:\n    target=file_name.split(\".\")[0]\n    full_path=os.path.join(IMAGE_FOLDER_PATH, file_name)\n    full_paths.append(full_path)\n    targets.append(target)\n\ndataset=pd.DataFrame()\ndataset['image_path']=full_paths\ndataset['target']=targets","metadata":{"_kg_hide-input":true,"id":"D5DoHI7DLhen","execution":{"iopub.status.busy":"2022-10-09T19:25:07.417725Z","iopub.execute_input":"2022-10-09T19:25:07.418092Z","iopub.status.idle":"2022-10-09T19:25:07.488611Z","shell.execute_reply.started":"2022-10-09T19:25:07.418055Z","shell.execute_reply":"2022-10-09T19:25:07.487770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head(10)","metadata":{"_kg_hide-output":true,"id":"UimV2YjALheo","execution":{"iopub.status.busy":"2022-10-09T19:25:07.490317Z","iopub.execute_input":"2022-10-09T19:25:07.490565Z","iopub.status.idle":"2022-10-09T19:25:07.507649Z","shell.execute_reply.started":"2022-10-09T19:25:07.490541Z","shell.execute_reply":"2022-10-09T19:25:07.506765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_counts=dataset['target'].value_counts()\nprint(\"Number of dogs in the dataset:{}\".format(target_counts['dog']))\nprint(\"Number of cats in the dataset:{}\".format(target_counts['cat']))","metadata":{"_kg_hide-input":true,"id":"BEGUSXvqLheo","execution":{"iopub.status.busy":"2022-10-09T19:25:07.509001Z","iopub.execute_input":"2022-10-09T19:25:07.509507Z","iopub.status.idle":"2022-10-09T19:25:07.519489Z","shell.execute_reply.started":"2022-10-09T19:25:07.509473Z","shell.execute_reply":"2022-10-09T19:25:07.518542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_side(img, side_type, side_size=5):\n    height, width, channel=img.shape\n    if side_type==\"horizontal\":\n        return np.ones((height,side_size,  channel), dtype=np.float32)*255\n        \n    return np.ones((side_size, width,  channel), dtype=np.float32)*255\n\ndef show_gallery(show=\"both\"):\n    n=100\n    counter=0\n    images=list()\n    vertical_images=[]\n    rng_state = np.random.get_state()\n    np.random.shuffle(full_paths)\n    np.random.set_state(rng_state)\n    np.random.shuffle(targets)\n    for path, target in zip(full_paths, targets):\n        if target!=show and show!=\"both\":\n            continue\n        counter=counter+1\n        if counter%100==0:\n            break\n        #Image loading from disk as JpegImageFile file format\n        img=load_img(path, target_size=(WIDTH,HEIGHT))\n        #Converting JpegImageFile to numpy array\n        img=img_to_array(img)\n        \n        hside=get_side(img, side_type=\"horizontal\")\n        images.append(img)\n        images.append(hside)\n\n        if counter%10==0:\n            himage=np.hstack((images))\n            vside=get_side(himage, side_type=\"vertical\")\n            vertical_images.append(himage)\n            vertical_images.append(vside)\n            \n            images=list()\n\n    gallery=np.vstack((vertical_images)) \n    plt.figure(figsize=(12,12))\n    plt.xticks([])\n    plt.yticks([])\n    title={\"both\":\"Dogs and Cats\",\n          \"cat\": \"Cats\",\n          \"dog\": \"Dogs\"}\n    plt.title(\"100 samples of {} of the dataset\".format(title[show]))\n    plt.imshow(gallery.astype(np.uint8))","metadata":{"_kg_hide-input":true,"id":"FX8362OuLheo","execution":{"iopub.status.busy":"2022-10-09T19:25:07.520943Z","iopub.execute_input":"2022-10-09T19:25:07.521552Z","iopub.status.idle":"2022-10-09T19:25:07.536514Z","shell.execute_reply.started":"2022-10-09T19:25:07.521512Z","shell.execute_reply":"2022-10-09T19:25:07.535538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_gallery(show=\"cat\")\nshow_gallery(show=\"dog\")\nshow_gallery(show=\"both\")","metadata":{"id":"t5vh38vtLheo","execution":{"iopub.status.busy":"2022-10-09T19:25:07.537932Z","iopub.execute_input":"2022-10-09T19:25:07.538459Z","iopub.status.idle":"2022-10-09T19:25:10.456867Z","shell.execute_reply.started":"2022-10-09T19:25:07.538424Z","shell.execute_reply":"2022-10-09T19:25:10.456025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=applications.VGG16(weights=\"imagenet\", include_top=False, input_shape=(WIDTH, HEIGHT, 3))\nmodel.summary()","metadata":{"_kg_hide-output":true,"id":"xftKmk2oLher","execution":{"iopub.status.busy":"2022-10-09T19:29:18.725122Z","iopub.execute_input":"2022-10-09T19:29:18.725493Z","iopub.status.idle":"2022-10-09T19:29:19.637048Z","shell.execute_reply.started":"2022-10-09T19:29:18.725460Z","shell.execute_reply":"2022-10-09T19:29:19.636121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counter=0\nfeatures=list()\nfor path, target in zip(full_paths, targets):\n    img=load_img(path, target_size=(WIDTH, HEIGHT))\n    img=img_to_array(img)\n    img=np.expand_dims(img, axis=0)\n    feature=model.predict(img)\n    features.append(feature)\n    counter+=1\n    if counter%2500==0:\n        print(\"[INFO]:{} images loaded\".format(counter))\n\n","metadata":{"id":"bNfNnLdCLher","execution":{"iopub.status.busy":"2022-10-09T19:29:22.391110Z","iopub.execute_input":"2022-10-09T19:29:22.391469Z","iopub.status.idle":"2022-10-09T19:48:48.956084Z","shell.execute_reply.started":"2022-10-09T19:29:22.391436Z","shell.execute_reply":"2022-10-09T19:48:48.955145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=np.array(features)\nprint(\"Before reshape,features.shape:\",features.shape)\nfeatures=features.reshape(features.shape[0], 4*4*512)\nprint(\"After reshape, features.shape:\",features.shape)","metadata":{"id":"SunqE48_Lher","execution":{"iopub.status.busy":"2022-10-09T19:48:48.957982Z","iopub.execute_input":"2022-10-09T19:48:48.958594Z","iopub.status.idle":"2022-10-09T19:48:49.242554Z","shell.execute_reply.started":"2022-10-09T19:48:48.958557Z","shell.execute_reply":"2022-10-09T19:48:49.241471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le=LabelEncoder()\ntargets=le.fit_transform(targets)","metadata":{"id":"ekoFxd8LLher","execution":{"iopub.status.busy":"2022-10-09T19:48:49.244170Z","iopub.execute_input":"2022-10-09T19:48:49.244539Z","iopub.status.idle":"2022-10-09T19:48:49.257475Z","shell.execute_reply.started":"2022-10-09T19:48:49.244503Z","shell.execute_reply":"2022-10-09T19:48:49.256321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"features.shape:\",features.shape)\nprint(\"targets.shape:\",targets.shape)","metadata":{"id":"Q9BUUr4kLher","execution":{"iopub.status.busy":"2022-10-09T19:48:49.268574Z","iopub.execute_input":"2022-10-09T19:48:49.268954Z","iopub.status.idle":"2022-10-09T19:48:49.275454Z","shell.execute_reply.started":"2022-10-09T19:48:49.268920Z","shell.execute_reply":"2022-10-09T19:48:49.274297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test=train_test_split(features, targets, test_size=0.2, random_state=42)","metadata":{"id":"LGViq4eWLher","execution":{"iopub.status.busy":"2022-10-09T19:48:49.277094Z","iopub.execute_input":"2022-10-09T19:48:49.277454Z","iopub.status.idle":"2022-10-09T19:48:49.520156Z","shell.execute_reply.started":"2022-10-09T19:48:49.277420Z","shell.execute_reply":"2022-10-09T19:48:49.519166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score","metadata":{"id":"xxpHg_ERLher","execution":{"iopub.status.busy":"2022-10-09T19:48:49.521820Z","iopub.execute_input":"2022-10-09T19:48:49.522206Z","iopub.status.idle":"2022-10-09T19:48:49.527739Z","shell.execute_reply.started":"2022-10-09T19:48:49.522162Z","shell.execute_reply":"2022-10-09T19:48:49.526707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf=LogisticRegression(solver=\"lbfgs\")\nprint(\"{} training...\".format(clf.__class__.__name__))\nclf.fit(X_train, y_train)\ny_pred=clf.predict(X_test)\nprint(\"The model trained and used to predict the test data...\")","metadata":{"id":"RBXeEaXBLhes","execution":{"iopub.status.busy":"2022-10-09T19:48:50.472348Z","iopub.execute_input":"2022-10-09T19:48:50.472784Z","iopub.status.idle":"2022-10-09T19:49:14.447664Z","shell.execute_reply.started":"2022-10-09T19:48:50.472744Z","shell.execute_reply":"2022-10-09T19:49:14.443712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy:\",metrics.accuracy_score(y_test, y_pred))\nprint(\"Confusion Matrix:\\n\",metrics.confusion_matrix(y_test, y_pred))\nprint(\"Classification Report:\\n\",metrics.classification_report(y_test, y_pred, target_names=[\"cat\", \"dog\"]))","metadata":{"id":"gc5f69I_Lhes","execution":{"iopub.status.busy":"2022-10-09T19:49:14.449610Z","iopub.execute_input":"2022-10-09T19:49:14.450051Z","iopub.status.idle":"2022-10-09T19:49:14.486904Z","shell.execute_reply.started":"2022-10-09T19:49:14.450012Z","shell.execute_reply":"2022-10-09T19:49:14.484275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_scores=cross_val_score(LogisticRegression(solver=\"lbfgs\"), features, targets, cv=3 )\nprint(\"Cross validation scores obtained...\")","metadata":{"_kg_hide-output":true,"id":"i42X4u4TLhes","execution":{"iopub.status.busy":"2022-10-09T19:49:14.488285Z","iopub.execute_input":"2022-10-09T19:49:14.488751Z","iopub.status.idle":"2022-10-09T19:50:17.418764Z","shell.execute_reply.started":"2022-10-09T19:49:14.488717Z","shell.execute_reply":"2022-10-09T19:50:17.415521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Cross validated scores:{}\".format(cv_scores))\nprint(\"Mean of cross validated scores:{:.3f}\".format(cv_scores.mean()))","metadata":{"id":"KCXvWOcuLhes","execution":{"iopub.status.busy":"2022-10-09T19:50:17.420634Z","iopub.execute_input":"2022-10-09T19:50:17.421317Z","iopub.status.idle":"2022-10-09T19:50:17.431144Z","shell.execute_reply.started":"2022-10-09T19:50:17.421279Z","shell.execute_reply":"2022-10-09T19:50:17.429105Z"},"trusted":true},"execution_count":null,"outputs":[]}]}