{"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 code builds 1000 dimension product embeddings from images by passing each image through a pre-trained VGG16 CNN from Keras Applications API.\nEmbeddings are stored in a pickle file found here https://www.kaggle.com/datasets/pranithchowdary/image-embeddings\nIt took 11 hours to finish the process**","metadata":{"papermill":{"duration":0.006442,"end_time":"2022-05-18T11:45:56.993300","exception":false,"start_time":"2022-05-18T11:45:56.986858","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions\nimport numpy as np\nimport tensorflow as tf\nmodel = VGG16(weights='imagenet')","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":12.102773,"end_time":"2022-05-18T11:46:09.112152","exception":false,"start_time":"2022-05-18T11:45:57.009379","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-04T14:42:26.793086Z","iopub.execute_input":"2023-02-04T14:42:26.793546Z","iopub.status.idle":"2023-02-04T14:42:29.856196Z","shell.execute_reply.started":"2023-02-04T14:42:26.793510Z","shell.execute_reply":"2023-02-04T14:42:29.855169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\npaths = []\nids = []\nfor dirname, _, filenames in os.walk('../input/h-and-m-personalized-fashion-recommendations/images'):\n    for filename in filenames:\n        paths.append(os.path.join(dirname, filename))\n        ids.append(filename[:1])","metadata":{"papermill":{"duration":116.944179,"end_time":"2022-05-18T11:48:06.081091","exception":false,"start_time":"2022-05-18T11:46:09.136912","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-04T14:42:29.858252Z","iopub.execute_input":"2023-02-04T14:42:29.858800Z","iopub.status.idle":"2023-02-04T14:42:48.789666Z","shell.execute_reply.started":"2023-02-04T14:42:29.858756Z","shell.execute_reply":"2023-02-04T14:42:48.788508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\nwith open('paths.pickle', 'wb') as f:\n    pickle.dump(paths, f)\n    \nwith open('ids.pickle', 'wb') as f:\n    pickle.dump(ids, f)","metadata":{"papermill":{"duration":0.06915,"end_time":"2022-05-18T11:48:06.175074","exception":false,"start_time":"2022-05-18T11:48:06.105924","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-04T14:42:48.791383Z","iopub.execute_input":"2023-02-04T14:42:48.791728Z","iopub.status.idle":"2023-02-04T14:42:48.834684Z","shell.execute_reply.started":"2023-02-04T14:42:48.791696Z","shell.execute_reply":"2023-02-04T14:42:48.833340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# embeds = []\n# labels = []\nembeds = open('embeds.pickle', 'wb')\nlabels = open('labels.pickle', 'wb')\n\nfor path in paths:\n    try:\n        img = image.load_img(path, target_size=(224, 224))\n        x = image.img_to_array(img)\n        x = np.expand_dims(x, axis=0)\n        x = preprocess_input(x)\n        embed = model.predict(x)\n        label = decode_predictions(embed, top=1)[0][0][1]\n\n    #     embeds.append(embed)\n    #     labels.append(label)\n\n        pickle.dump(embed, embeds)\n        pickle.dump(label, labels)\n        \n    except e:\n        print(e)\n","metadata":{"papermill":{"duration":9292.495811,"end_time":"2022-05-18T14:22:58.695713","exception":false,"start_time":"2022-05-18T11:48:06.199902","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-04T14:42:48.836922Z","iopub.execute_input":"2023-02-04T14:42:48.837325Z"},"trusted":true},"execution_count":null,"outputs":[]}]}