{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":15803,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# since kaggle environment has been changed I need to apply some fixes\n!pip install imutils\n!unzip ../input/train.zip -d train\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-21T07:27:02.122991Z","iopub.execute_input":"2023-03-21T07:27:02.123453Z","iopub.status.idle":"2023-03-21T07:27:30.840725Z","shell.execute_reply.started":"2023-03-21T07:27:02.123414Z","shell.execute_reply":"2023-03-21T07:27:30.839527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n# You may need to install a custom package named *imutils* to run this notebook. \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import cross_val_score\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"train\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2023-03-21T07:27:30.843171Z","iopub.execute_input":"2023-03-21T07:27:30.843790Z","iopub.status.idle":"2023-03-21T07:27:31.388994Z","shell.execute_reply.started":"2023-03-21T07:27:30.843748Z","shell.execute_reply":"2023-03-21T07:27:31.387632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adopted (and modified) from https://www.pyimagesearch.com/2016/08/08/k-nn-classifier-for-image-classification/\nfrom sklearn.ensemble import AdaBoostClassifier\nfrom xgboost import XGBClassifier\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier\nfrom sklearn.feature_selection import RFE, SelectFromModel\nfrom sklearn.decomposition import PCA, FastICA, KernelPCA\nfrom sklearn.kernel_approximation import RBFSampler\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis\nimport matplotlib.pyplot as plt\nfrom imutils import paths\nimport numpy as np\nimport imutils # a simple image utility library\nimport cv2 #opencv library\nimport os","metadata":{"_uuid":"9548902205315f78f9d6ea596a7a45d3dd66db4e","execution":{"iopub.status.busy":"2023-03-21T07:27:31.390757Z","iopub.execute_input":"2023-03-21T07:27:31.391319Z","iopub.status.idle":"2023-03-21T07:27:32.122957Z","shell.execute_reply.started":"2023-03-21T07:27:31.391271Z","shell.execute_reply":"2023-03-21T07:27:32.121624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_to_feature_vector(image, size=(32, 32)):\n# resize the image to a fixed size, then flatten the image into\n# a list of raw pixel intensitieb\n    image = cv2.medianBlur(image, 5)\n    ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)\n    return cv2.resize(ycrcb, size, interpolation=cv2.INTER_AREA).flatten()","metadata":{"_uuid":"2ba6d6ea34bd8a0f36d2873b6b0960ee8a9b8775","execution":{"iopub.status.busy":"2023-03-21T07:31:36.225794Z","iopub.execute_input":"2023-03-21T07:31:36.226394Z","iopub.status.idle":"2023-03-21T07:31:36.234684Z","shell.execute_reply.started":"2023-03-21T07:31:36.226345Z","shell.execute_reply":"2023-03-21T07:31:36.232694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = \"train/train/\"","metadata":{"_uuid":"9510d8bc95e6f34446413b9a40471caa20c8f15c","execution":{"iopub.status.busy":"2023-03-21T07:31:36.238198Z","iopub.execute_input":"2023-03-21T07:31:36.238769Z","iopub.status.idle":"2023-03-21T07:31:36.251002Z","shell.execute_reply.started":"2023-03-21T07:31:36.238713Z","shell.execute_reply":"2023-03-21T07:31:36.249197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grab the list of images that we'll be describing\nprint(\"[INFO] describing images...\")\nimagePaths = list(paths.list_images(dataset))\nprint(len(imagePaths))\nprint(imagePaths[0])\n# initialize the raw pixel intensities matrix, the features matrix,\n# and labels list\nrawImages = []\nlabels = []","metadata":{"_uuid":"970c37bf80b54b5e186061d0fe39c5a34f4c1995","execution":{"iopub.status.busy":"2023-03-21T07:31:36.255285Z","iopub.execute_input":"2023-03-21T07:31:36.255964Z","iopub.status.idle":"2023-03-21T07:31:36.351761Z","shell.execute_reply.started":"2023-03-21T07:31:36.255914Z","shell.execute_reply":"2023-03-21T07:31:36.350136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loop over the input images\nfor (i, imagePath) in enumerate(imagePaths):\n\t# load the image and extract the class label (assuming that our\n\t# path as the format: /path/to/dataset/{class}.{image_num}.jpg\n\timage = cv2.imread(imagePath)\n\tlabel = 1 if imagePath.split(os.path.sep)[-1].split(\".\")[0] == \"dog\" else 0\n \n\t# extract raw pixel intensity \"features\", followed by a color\n\t# histogram to characterize the color distribution of the pixels\n\t# in the image\n\tpixels = image_to_feature_vector(image)\n\t\n \n\t# update the raw images, features, and labels matricies,\n\t# respectively\n\trawImages.append(pixels)\n\tlabels.append(label)\n \n\t# show an update every 1,000 images\n\tif i > 0 and i % 1000 == 0:\n\t\tprint(\"[INFO] processed {}/{}\".format(i, len(imagePaths)))","metadata":{"_uuid":"2c3f13675a86fd1a762649ef52114b5151be16e5","execution":{"iopub.status.busy":"2023-03-21T07:31:36.353623Z","iopub.execute_input":"2023-03-21T07:31:36.353992Z","iopub.status.idle":"2023-03-21T07:34:04.876341Z","shell.execute_reply.started":"2023-03-21T07:31:36.353958Z","shell.execute_reply":"2023-03-21T07:34:04.874571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show some information on the memory consumed by the raw images\n# matrix and features matrix\nrawImages = np.array(rawImages)\nlabels = np.array(labels)\nprint(\"[INFO] pixels matrix: {:.2f}MB\".format(\n\trawImages.nbytes / (1024 * 1000.0)))","metadata":{"_uuid":"e3b2adca6b9b7f83038f0a5d1c3a2118d1afdede","execution":{"iopub.status.busy":"2023-03-21T07:34:04.879821Z","iopub.execute_input":"2023-03-21T07:34:04.880449Z","iopub.status.idle":"2023-03-21T07:34:04.951606Z","shell.execute_reply.started":"2023-03-21T07:34:04.880382Z","shell.execute_reply":"2023-03-21T07:34:04.950535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# partition the data into training and testing splits, using 75%\n# of the data for training and the remaining 25% for testing\n(trainRI, trainRL) = (rawImages, labels)\n","metadata":{"_uuid":"5fab6cded70110b1fc3494799346a6c31793f9c9","execution":{"iopub.status.busy":"2023-03-21T07:34:04.953221Z","iopub.execute_input":"2023-03-21T07:34:04.953870Z","iopub.status.idle":"2023-03-21T07:34:04.959361Z","shell.execute_reply.started":"2023-03-21T07:34:04.953833Z","shell.execute_reply":"2023-03-21T07:34:04.957874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select a subset of the entire dataset \nrawImages_subset = rawImages[:2000]\nlabels_subset= labels[:2000]\n(trainRI, trainRL) = (rawImages_subset, labels_subset)\n","metadata":{"_uuid":"96f099b5712bfb068bd93211725ff11cfa70b3cf","execution":{"iopub.status.busy":"2023-03-21T07:34:04.961179Z","iopub.execute_input":"2023-03-21T07:34:04.961519Z","iopub.status.idle":"2023-03-21T07:34:04.972796Z","shell.execute_reply.started":"2023-03-21T07:34:04.961466Z","shell.execute_reply":"2023-03-21T07:34:04.971386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainRI = StandardScaler().fit_transform(trainRI)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:34:04.974441Z","iopub.execute_input":"2023-03-21T07:34:04.974829Z","iopub.status.idle":"2023-03-21T07:34:05.133218Z","shell.execute_reply.started":"2023-03-21T07:34:04.974795Z","shell.execute_reply":"2023-03-21T07:34:05.131770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=500, svd_solver='randomized').fit(trainRI)\nE = pca.explained_variance_ratio_\nprint(np.cumsum(E)[::-1][0])\npca_X = pca.transform(trainRI)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:37:50.232330Z","iopub.execute_input":"2023-03-21T07:37:50.232763Z","iopub.status.idle":"2023-03-21T07:37:53.613722Z","shell.execute_reply.started":"2023-03-21T07:37:50.232728Z","shell.execute_reply":"2023-03-21T07:37:53.611942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kpca = KernelPCA(n_components=500, kernel='rbf', gamma=20.0).fit(trainRI)\nprint(np.cumsum(E)[::-1][0])\nkpca_X = kpca.transform(trainRI)\nprint(kpca_X.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:37:04.250359Z","iopub.execute_input":"2023-03-21T07:37:04.250755Z","iopub.status.idle":"2023-03-21T07:37:06.040637Z","shell.execute_reply.started":"2023-03-21T07:37:04.250723Z","shell.execute_reply":"2023-03-21T07:37:06.039141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ica = FastICA(n_components=300, max_iter=300).fit(trainRI)\nprint(np.cumsum(E)[::-1][0])\nica_X = ica.transform(trainRI)\nprint(ica_X.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:34:13.117595Z","iopub.execute_input":"2023-03-21T07:34:13.118901Z","iopub.status.idle":"2023-03-21T07:34:25.418762Z","shell.execute_reply.started":"2023-03-21T07:34:13.118827Z","shell.execute_reply":"2023-03-21T07:34:25.416905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rbf = RBFSampler(gamma=1, random_state=114514).fit(trainRI)\nprint(np.cumsum(E)[::-1][0])\nrbf_X = rbf.transform(trainRI)\nprint(rbf_X.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:34:25.426267Z","iopub.execute_input":"2023-03-21T07:34:25.428657Z","iopub.status.idle":"2023-03-21T07:34:25.540821Z","shell.execute_reply.started":"2023-03-21T07:34:25.428543Z","shell.execute_reply":"2023-03-21T07:34:25.539084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = trainRI[2].reshape(32, 32, 3)\nplt.imshow(im)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:34:25.543848Z","iopub.execute_input":"2023-03-21T07:34:25.545195Z","iopub.status.idle":"2023-03-21T07:34:25.861777Z","shell.execute_reply.started":"2023-03-21T07:34:25.545121Z","shell.execute_reply":"2023-03-21T07:34:25.860089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rpca = pca.inverse_transform(pca_X[2])\nim = rpca.reshape(32, 32, 3)\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:37:58.245623Z","iopub.execute_input":"2023-03-21T07:37:58.246113Z","iopub.status.idle":"2023-03-21T07:37:58.515640Z","shell.execute_reply.started":"2023-03-21T07:37:58.246046Z","shell.execute_reply":"2023-03-21T07:37:58.514678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rica = ica.inverse_transform([ica_X[2]])\nim = rica[0].reshape(32, 32, 3)\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:34:26.165105Z","iopub.execute_input":"2023-03-21T07:34:26.166236Z","iopub.status.idle":"2023-03-21T07:34:26.441187Z","shell.execute_reply.started":"2023-03-21T07:34:26.166184Z","shell.execute_reply":"2023-03-21T07:34:26.440161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train and evaluate a AdaBoost classifer on the raw pixel intensities\nprint(\"[INFO] evaluating raw pixel accuracy...\")\n#neighbors = [1, 3, 5, 7, 13]\n#for k in neighbors:\nmodel = AdaBoostClassifier(n_estimators=300)\nacc = cross_val_score(model, pca_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(PCA): {}\".format(acc))\nacc = cross_val_score(model, kpca_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(KernelPCA): {}\".format(acc))\nacc = cross_val_score(model, ica_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(ICA): {}\".format(acc))\nacc = cross_val_score(model, rbf_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(RBFSampler): {}\".format(acc))","metadata":{"_uuid":"70ccd1c8cae68e8af1692316dbfb8fd62355a476","execution":{"iopub.status.busy":"2023-03-21T07:38:05.279594Z","iopub.execute_input":"2023-03-21T07:38:05.280077Z","iopub.status.idle":"2023-03-21T07:41:29.147494Z","shell.execute_reply.started":"2023-03-21T07:38:05.280024Z","shell.execute_reply":"2023-03-21T07:41:29.146165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train and evaluate a XGBoost classifer on the raw pixel intensities\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nmodel = XGBClassifier(learning_rate=0.1, n_estimators=600, max_depth=15, colsample_bytree=0.2, subsample=0.9, objective=\"binary:logistic\")\nacc = cross_val_score(model, pca_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(PCA): {}\".format(acc))\nacc = cross_val_score(model, kpca_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(KernelPCA): {}\".format(acc))\nacc = cross_val_score(model, ica_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(ICA): {}\".format(acc))\nacc = cross_val_score(model, rbf_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(RBFSampler): {}\".format(acc))","metadata":{"_uuid":"68b277b0331fdacd19be062e8539b6280222dae8","execution":{"iopub.status.busy":"2023-03-21T07:41:29.399384Z","iopub.execute_input":"2023-03-21T07:41:29.399777Z","iopub.status.idle":"2023-03-21T07:42:49.817025Z","shell.execute_reply.started":"2023-03-21T07:41:29.399740Z","shell.execute_reply":"2023-03-21T07:42:49.815994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train and evaluate a Logistic Regression classifer on the raw pixel intensities\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nmodel = LogisticRegression(solver='liblinear', max_iter=900)\nacc = cross_val_score(model, pca_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(PCA): {}\".format(acc))\nacc = cross_val_score(model, kpca_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(KernelPCA): {}\".format(acc))\nacc = cross_val_score(model, ica_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(ICA): {}\".format(acc))\nacc = cross_val_score(model, rbf_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy(RBFSampler): {}\".format(acc))","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:36:36.868709Z","iopub.status.idle":"2023-03-21T07:36:36.869177Z","shell.execute_reply.started":"2023-03-21T07:36:36.868937Z","shell.execute_reply":"2023-03-21T07:36:36.868959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(trainRI, trainRL) = (rawImages, labels)\npca_X = pca.fit_transform(trainRI)\n\nprint(\"[INFO] evaluating raw pixel accuracy...\")\nmodel = XGBClassifier(learning_rate=0.1, n_estimators=300, max_depth=6, colsample_bytree=0.2, subsample=0.9, objective=\"binary:logistic\")\nacc = cross_val_score(model, pca_X, trainRL, cv=3)\nprint(\"[INFO] raw pixel accuracy: {}\".format(acc))","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:36:36.872883Z","iopub.status.idle":"2023-03-21T07:36:36.873329Z","shell.execute_reply.started":"2023-03-21T07:36:36.873117Z","shell.execute_reply":"2023-03-21T07:36:36.873142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\nmodel.fit(pca_X, trainRL)\npickle.dump(pca, open(\"pca.pickle\", \"wb\"))\npickle.dump(model, open(\"model.pickle\", \"wb\"))\n","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:36:36.874689Z","iopub.status.idle":"2023-03-21T07:36:36.875133Z","shell.execute_reply.started":"2023-03-21T07:36:36.874907Z","shell.execute_reply":"2023-03-21T07:36:36.874930Z"},"trusted":true},"execution_count":null,"outputs":[]}]}