{"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":"markdown","source":"**This notebook uses the embeddings generated in https://www.kaggle.com/code/mohammedobeidat/product-embeddings-from-images-keras-vgg16/notebook (version 3) to find top 20 similar items.**\n\nData can be found in this public dataset https://www.kaggle.com/datasets/mohammedobeidat/hm-product-image-embeddings\n\n**Similar items are saved in a pickle file in https://www.kaggle.com/datasets/mohammedobeidat/hm-top-20-similar-items-using-image-embeddings**\n\n**article_id's are saved in another pcikle in the same path sorted in the same order as the embeddings.**","metadata":{}},{"cell_type":"code","source":"import pickle\nimport pandas as pd\nfrom sklearn.neighbors import KNeighborsClassifier as KNN\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport sklearn","metadata":{"execution":{"iopub.status.busy":"2022-05-29T17:20:13.747998Z","iopub.execute_input":"2022-05-29T17:20:13.748547Z","iopub.status.idle":"2022-05-29T17:20:15.302292Z","shell.execute_reply.started":"2022-05-29T17:20:13.748445Z","shell.execute_reply":"2022-05-29T17:20:15.300736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('../input/hm-product-image-embeddings/embeds.pickle', 'rb')\npaths = open('../input/hm-product-image-embeddings/paths.pickle', 'rb')\nlabels = open('../input/hm-product-image-embeddings/labels.pickle', 'rb')\npaths = pickle.load(paths)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-29T17:20:15.30506Z","iopub.execute_input":"2022-05-29T17:20:15.30551Z","iopub.status.idle":"2022-05-29T17:20:15.426361Z","shell.execute_reply.started":"2022-05-29T17:20:15.305472Z","shell.execute_reply":"2022-05-29T17:20:15.425341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = np.array([path[-14:-4] for path in paths])","metadata":{"execution":{"iopub.status.busy":"2022-05-29T17:20:15.427753Z","iopub.execute_input":"2022-05-29T17:20:15.428419Z","iopub.status.idle":"2022-05-29T17:20:15.49328Z","shell.execute_reply.started":"2022-05-29T17:20:15.42838Z","shell.execute_reply":"2022-05-29T17:20:15.492304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.array([pickle.load(labels) for i in range(105100)])","metadata":{"execution":{"iopub.status.busy":"2022-05-29T17:20:17.277522Z","iopub.execute_input":"2022-05-29T17:20:17.277983Z","iopub.status.idle":"2022-05-29T17:20:20.232455Z","shell.execute_reply.started":"2022-05-29T17:20:17.277948Z","shell.execute_reply":"2022-05-29T17:20:20.231353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeds = np.array([pickle.load(f)[0] for i, path in zip(range(105100), paths)])","metadata":{"execution":{"iopub.status.busy":"2022-05-29T17:20:20.234658Z","iopub.execute_input":"2022-05-29T17:20:20.235005Z","iopub.status.idle":"2022-05-29T17:20:30.717993Z","shell.execute_reply.started":"2022-05-29T17:20:20.234959Z","shell.execute_reply":"2022-05-29T17:20:30.717122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(embeds)\ndf['article_id'] = ids\ndf['label'] = labels","metadata":{"execution":{"iopub.status.busy":"2022-05-29T17:20:30.719426Z","iopub.execute_input":"2022-05-29T17:20:30.719851Z","iopub.status.idle":"2022-05-29T17:20:30.778451Z","shell.execute_reply.started":"2022-05-29T17:20:30.719817Z","shell.execute_reply":"2022-05-29T17:20:30.777225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[['article_id', 'label']].to_csv('cnn_labels.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T17:20:30.780574Z","iopub.execute_input":"2022-05-29T17:20:30.780877Z","iopub.status.idle":"2022-05-29T17:20:31.053799Z","shell.execute_reply.started":"2022-05-29T17:20:30.780839Z","shell.execute_reply":"2022-05-29T17:20:31.052561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df.drop(['article_id', 'label'], axis=1)\ny = df.article_id","metadata":{"execution":{"iopub.status.busy":"2022-05-29T12:18:57.060933Z","iopub.execute_input":"2022-05-29T12:18:57.06159Z","iopub.status.idle":"2022-05-29T12:18:57.314108Z","shell.execute_reply.started":"2022-05-29T12:18:57.061552Z","shell.execute_reply":"2022-05-29T12:18:57.313547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNN(20, metric='cosine')","metadata":{"execution":{"iopub.status.busy":"2022-05-29T12:18:57.31511Z","iopub.execute_input":"2022-05-29T12:18:57.316011Z","iopub.status.idle":"2022-05-29T12:18:57.319808Z","shell.execute_reply.started":"2022-05-29T12:18:57.315975Z","shell.execute_reply":"2022-05-29T12:18:57.318885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T12:18:57.321004Z","iopub.execute_input":"2022-05-29T12:18:57.321316Z","iopub.status.idle":"2022-05-29T12:18:57.618684Z","shell.execute_reply.started":"2022-05-29T12:18:57.321279Z","shell.execute_reply":"2022-05-29T12:18:57.617402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scores = knn.kneighbors()","metadata":{"execution":{"iopub.status.busy":"2022-05-29T12:18:57.621016Z","iopub.execute_input":"2022-05-29T12:18:57.621247Z","iopub.status.idle":"2022-05-29T12:18:57.625587Z","shell.execute_reply.started":"2022-05-29T12:18:57.62122Z","shell.execute_reply":"2022-05-29T12:18:57.624317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# similar = open('top_20.pickle', 'wb')\n# pickle.dump(scores, similar)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T12:18:57.62699Z","iopub.execute_input":"2022-05-29T12:18:57.627376Z","iopub.status.idle":"2022-05-29T12:18:57.636699Z","shell.execute_reply.started":"2022-05-29T12:18:57.627334Z","shell.execute_reply":"2022-05-29T12:18:57.636007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_items(items, scores, labels):\n    path = \"../input/h-and-m-personalized-fashion-recommendations/images\"\n\n    k = len(items)\n    fig = plt.figure(figsize=(3*k, 10))\n    for item, i, score, label in zip(items, range(1, k+1), scores, labels):\n        sub = item[:3]\n        image = path + \"/\"+ sub + \"/\"+ item +\".jpg\"\n        image = plt.imread(image)\n        fig.add_subplot(1, k, i)\n        plt.axis(False)\n        plt.title(str(score)+' - '+(label))\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T12:18:57.638246Z","iopub.execute_input":"2022-05-29T12:18:57.638553Z","iopub.status.idle":"2022-05-29T12:18:57.649996Z","shell.execute_reply.started":"2022-05-29T12:18:57.638495Z","shell.execute_reply":"2022-05-29T12:18:57.649048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nrand = x.sample(1)\nsample = knn.kneighbors(rand, 20)\nrcmnds = ids[sample[1][0]]\nscores = np.round(1- sample[0][0], 2)\nsample_labels = df[df.article_id.isin(rcmnds)].label\nprint(scores.mean())\nplot_items(rcmnds, scores, labels)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T12:18:57.651357Z","iopub.execute_input":"2022-05-29T12:18:57.651753Z","iopub.status.idle":"2022-05-29T12:19:04.539295Z","shell.execute_reply.started":"2022-05-29T12:18:57.651713Z","shell.execute_reply":"2022-05-29T12:19:04.538445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}