{"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":"!pip install -q efficientnet\n!pip install openpyxl\n# !pip install wandb==0.11.0","metadata":{"papermill":{"duration":9.779329,"end_time":"2021-04-23T01:30:05.572068","exception":false,"start_time":"2021-04-23T01:29:55.792739","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:03:21.433574Z","iopub.execute_input":"2021-07-24T18:03:21.433999Z","iopub.status.idle":"2021-07-24T18:03:49.513725Z","shell.execute_reply.started":"2021-07-24T18:03:21.433915Z","shell.execute_reply":"2021-07-24T18:03:49.512441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# targets = ['color', 'material', 'shoe_upper', 'shoe_heel', 'shoe_toe','shoe_style']\ntarget = \"shoe_style\"","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:03:49.516136Z","iopub.execute_input":"2021-07-24T18:03:49.516514Z","iopub.status.idle":"2021-07-24T18:03:49.524102Z","shell.execute_reply.started":"2021-07-24T18:03:49.516475Z","shell.execute_reply":"2021-07-24T18:03:49.523016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nfrom wandb.keras import WandbCallback\nfrom kaggle_secrets import UserSecretsClient\n\nuser_secrets = UserSecretsClient()\nwandb_api = user_secrets.get_secret(\"wandb-key\")\n\nwandb.login(key=wandb_api)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:03:49.527534Z","iopub.execute_input":"2021-07-24T18:03:49.527875Z","iopub.status.idle":"2021-07-24T18:03:56.557206Z","shell.execute_reply.started":"2021-07-24T18:03:49.527843Z","shell.execute_reply":"2021-07-24T18:03:56.55645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport tensorflow as tf\nimport keras\nimport efficientnet.tfkeras as efn\nfrom kaggle_datasets import KaggleDatasets\nfrom tensorflow.keras.models import Sequential\nimport tensorflow.keras.layers as L\nfrom tensorflow.keras.applications import ResNet152V2, InceptionResNetV2, InceptionV3, Xception, VGG19\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":7.995631,"end_time":"2021-04-23T01:30:13.737308","exception":false,"start_time":"2021-04-23T01:30:05.741677","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:03:56.558748Z","iopub.execute_input":"2021-07-24T18:03:56.559259Z","iopub.status.idle":"2021-07-24T18:03:57.744677Z","shell.execute_reply.started":"2021-07-24T18:03:56.559226Z","shell.execute_reply":"2021-07-24T18:03:57.743465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TPU Config","metadata":{"papermill":{"duration":0.030426,"end_time":"2021-04-23T01:30:13.802261","exception":false,"start_time":"2021-04-23T01:30:13.771835","status":"completed"},"tags":[]}},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\n\n# Create strategy from tpu\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\nstrategy = tf.distribute.experimental.TPUStrategy(tpu)\n\n# Data access\n# GCS_DS_PATH = KaggleDatasets().get_gcs_path(\"fashion\")\nGCS_DS_PATH = KaggleDatasets().get_gcs_path(\"best-shoes-dataset\")\n# Configuration\nEPOCHS = 30\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync","metadata":{"papermill":{"duration":6.113544,"end_time":"2021-04-23T01:30:19.94761","exception":false,"start_time":"2021-04-23T01:30:13.834066","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:03:57.746043Z","iopub.execute_input":"2021-07-24T18:03:57.746352Z","iopub.status.idle":"2021-07-24T18:04:04.003822Z","shell.execute_reply.started":"2021-07-24T18:03:57.74632Z","shell.execute_reply":"2021-07-24T18:04:04.00245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # Load Labels","metadata":{"papermill":{"duration":0.032783,"end_time":"2021-04-23T01:30:20.012548","exception":false,"start_time":"2021-04-23T01:30:19.979765","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import pandas as pd\ncolumns = [ 'color', 'material', 'shoe_upper' , 'shoe_heel', 'shoe_toe', 'shoe_style', 'Photo_URL']\n\ndata = pd.read_excel('../input/fahion-dataset/Database_ecommerce_intl_bubble.xlsx',  engine='openpyxl') \n\ndata = data[columns]\ndata.shape\nimages = data[\"Photo_URL\"].tolist()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:04.005105Z","iopub.execute_input":"2021-07-24T18:04:04.005453Z","iopub.status.idle":"2021-07-24T18:04:23.469698Z","shell.execute_reply.started":"2021-07-24T18:04:04.005411Z","shell.execute_reply":"2021-07-24T18:04:23.46878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.47101Z","iopub.execute_input":"2021-07-24T18:04:23.471293Z","iopub.status.idle":"2021-07-24T18:04:23.491347Z","shell.execute_reply.started":"2021-07-24T18:04:23.471266Z","shell.execute_reply":"2021-07-24T18:04:23.490349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndata = data[[target,\"Photo_URL\"]]\ndata = data.dropna() \n\ndef clean(s):\n    return s.split(\"(\")[0]#.replace('',\"\")\n\ndata[target] = data[target].apply(clean)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.494115Z","iopub.execute_input":"2021-07-24T18:04:23.494405Z","iopub.status.idle":"2021-07-24T18:04:23.576147Z","shell.execute_reply.started":"2021-07-24T18:04:23.494377Z","shell.execute_reply":"2021-07-24T18:04:23.575165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts = data[target].value_counts()\nprint(counts[:40])","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.578308Z","iopub.execute_input":"2021-07-24T18:04:23.57859Z","iopub.status.idle":"2021-07-24T18:04:23.593438Z","shell.execute_reply.started":"2021-07-24T18:04:23.578562Z","shell.execute_reply":"2021-07-24T18:04:23.59225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"super_labels = list(set(data[target].values))\n# super_labels\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.594519Z","iopub.execute_input":"2021-07-24T18:04:23.594825Z","iopub.status.idle":"2021-07-24T18:04:23.610741Z","shell.execute_reply.started":"2021-07-24T18:04:23.594795Z","shell.execute_reply":"2021-07-24T18:04:23.609719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"L = []\nfor k in data[target].values:\n    try:\n        for u in k.split(\"/\"):\n            L.append(u.split(\"(\")[0].replace(\"monochromatic-\",\"\").replace(\" \",\"\"))\n    except:\n        pass\n#             print(\"check this\",k)\n#             break\nn = list(set(L))\n\n# try:print(\" & \".join(n))\n# except:pass#print(\" & \".join(n[1:]))\nlabels = n\n\nprint(len(labels))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.612993Z","iopub.execute_input":"2021-07-24T18:04:23.613448Z","iopub.status.idle":"2021-07-24T18:04:23.650764Z","shell.execute_reply.started":"2021-07-24T18:04:23.613402Z","shell.execute_reply":"2021-07-24T18:04:23.649598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if \"\" in labels:\n    labels.remove(\"\")\nlen(labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.651951Z","iopub.execute_input":"2021-07-24T18:04:23.652241Z","iopub.status.idle":"2021-07-24T18:04:23.660134Z","shell.execute_reply.started":"2021-07-24T18:04:23.65221Z","shell.execute_reply":"2021-07-24T18:04:23.658575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.661899Z","iopub.execute_input":"2021-07-24T18:04:23.662236Z","iopub.status.idle":"2021-07-24T18:04:23.669402Z","shell.execute_reply.started":"2021-07-24T18:04:23.662189Z","shell.execute_reply":"2021-07-24T18:04:23.668397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\ndef ss2(i):\n#     try:\n    jj = \"\".join(re.findall(\"[a-zA-Z0-9]+\", str(i)))+\".jpg\"\n    jj = \"images/\"+jj\n    return jj\n#     except:return False\n\ndata2 = data.copy()\ndata2[\"path\"] =  data[\"Photo_URL\"].apply(ss2 )","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.671129Z","iopub.execute_input":"2021-07-24T18:04:23.671891Z","iopub.status.idle":"2021-07-24T18:04:23.857997Z","shell.execute_reply.started":"2021-07-24T18:04:23.671847Z","shell.execute_reply":"2021-07-24T18:04:23.857029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_data = data2[[target,\"path\"]]\ntarget_data","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.859575Z","iopub.execute_input":"2021-07-24T18:04:23.860011Z","iopub.status.idle":"2021-07-24T18:04:23.879255Z","shell.execute_reply.started":"2021-07-24T18:04:23.859968Z","shell.execute_reply":"2021-07-24T18:04:23.878303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\nimages = data2.path.tolist()\nfrom tqdm import tqdm\nOK = []\nP1 = \"../input/best-shoes-dataset/kaggle/working/\"\nformats = set()\nPack2 = []\nfor ii in tqdm(images):\n#      e = PIL.Image.open(i)\n    try:\n        i = P1+ii.replace(\"images/\",\"images/0_\")\n#         print(i)\n#         print(i)\n        e = PIL.Image.open(i)\n        formats.add(e.format)\n        if e.format == 'PNG':e.save(i, 'JPEG')\n        OK.append(True)\n    except:OK.append(False)\n    try:\n        i = P1+ii.replace(\"images/\",\"images/1_\")\n#         print(i)\n#         print(i)\n        e = PIL.Image.open(i)\n        formats.add(e.format)\n        if e.format == 'PNG':e.save(i, 'JPEG')\n#         OK.append(True)\n        Pack2.append(i)\n    except:pass#OK.append(False)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:04:23.880449Z","iopub.execute_input":"2021-07-24T18:04:23.880769Z","iopub.status.idle":"2021-07-24T18:06:26.564716Z","shell.execute_reply.started":"2021-07-24T18:04:23.88074Z","shell.execute_reply":"2021-07-24T18:06:26.563808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:26.56598Z","iopub.execute_input":"2021-07-24T18:06:26.56626Z","iopub.status.idle":"2021-07-24T18:06:26.576257Z","shell.execute_reply.started":"2021-07-24T18:06:26.566232Z","shell.execute_reply":"2021-07-24T18:06:26.575188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[\"OK\"] = OK\nprint(data2.shape)\ndata2 = data2[data2.OK]\ndata2.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:26.577262Z","iopub.execute_input":"2021-07-24T18:06:26.577549Z","iopub.status.idle":"2021-07-24T18:06:26.599744Z","shell.execute_reply.started":"2021-07-24T18:06:26.577521Z","shell.execute_reply":"2021-07-24T18:06:26.598419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pack2[-1]","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:26.600799Z","iopub.execute_input":"2021-07-24T18:06:26.60109Z","iopub.status.idle":"2021-07-24T18:06:26.606897Z","shell.execute_reply.started":"2021-07-24T18:06:26.60106Z","shell.execute_reply":"2021-07-24T18:06:26.605945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data3 = data2.copy()\ndef fi(path):\n    path = path.split(\"/\")[1]\n    for i in Pack2:\n        if path in i:\n            return True\n    return False\ndata3[\"OK\"]= data3.path.apply(fi)\n# data3","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:26.608214Z","iopub.execute_input":"2021-07-24T18:06:26.608491Z","iopub.status.idle":"2021-07-24T18:06:30.16557Z","shell.execute_reply.started":"2021-07-24T18:06:26.608462Z","shell.execute_reply":"2021-07-24T18:06:30.164625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data3 = data3[data3.OK]\ndata3.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:30.166803Z","iopub.execute_input":"2021-07-24T18:06:30.167103Z","iopub.status.idle":"2021-07-24T18:06:30.181316Z","shell.execute_reply.started":"2021-07-24T18:06:30.167074Z","shell.execute_reply":"2021-07-24T18:06:30.180294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data3[\"path\"] = data3[\"path\"].apply(lambda e:e.replace(\"images/\",\"kaggle/working/images/1_\")) \ndata3.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:30.182611Z","iopub.execute_input":"2021-07-24T18:06:30.183055Z","iopub.status.idle":"2021-07-24T18:06:30.204618Z","shell.execute_reply.started":"2021-07-24T18:06:30.183022Z","shell.execute_reply":"2021-07-24T18:06:30.203378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[\"path\"] = data2[\"path\"].apply(lambda e:e.replace(\"images/\",\"kaggle/working/images/0_\")) \ndata2.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:30.2101Z","iopub.execute_input":"2021-07-24T18:06:30.210456Z","iopub.status.idle":"2021-07-24T18:06:30.240737Z","shell.execute_reply.started":"2021-07-24T18:06:30.210423Z","shell.execute_reply":"2021-07-24T18:06:30.239645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_data = pd.concat([data2,data3])","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:30.242626Z","iopub.execute_input":"2021-07-24T18:06:30.242951Z","iopub.status.idle":"2021-07-24T18:06:30.249242Z","shell.execute_reply.started":"2021-07-24T18:06:30.24291Z","shell.execute_reply":"2021-07-24T18:06:30.248346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_data","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:30.250282Z","iopub.execute_input":"2021-07-24T18:06:30.250669Z","iopub.status.idle":"2021-07-24T18:06:30.275489Z","shell.execute_reply.started":"2021-07-24T18:06:30.25064Z","shell.execute_reply":"2021-07-24T18:06:30.274496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in labels:\n    co = target_data[target].apply(lambda e:(i in e)*1)\n    target_data[i]=pd.to_numeric(co, downcast=\"float\")","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:30.27667Z","iopub.execute_input":"2021-07-24T18:06:30.277084Z","iopub.status.idle":"2021-07-24T18:06:32.568671Z","shell.execute_reply.started":"2021-07-24T18:06:30.277047Z","shell.execute_reply":"2021-07-24T18:06:32.567553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_data = target_data[[\"path\"]+labels]\ntarget_data","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.570218Z","iopub.execute_input":"2021-07-24T18:06:32.570726Z","iopub.status.idle":"2021-07-24T18:06:32.6549Z","shell.execute_reply.started":"2021-07-24T18:06:32.570609Z","shell.execute_reply":"2021-07-24T18:06:32.65377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_data.isnull().values.any()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.657357Z","iopub.execute_input":"2021-07-24T18:06:32.657663Z","iopub.status.idle":"2021-07-24T18:06:32.679743Z","shell.execute_reply.started":"2021-07-24T18:06:32.657633Z","shell.execute_reply":"2021-07-24T18:06:32.677096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labelslist = labels\n_labels = target_data[labels].values\n_paths = (GCS_DS_PATH+\"/\"+target_data.path).values","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.681422Z","iopub.execute_input":"2021-07-24T18:06:32.68176Z","iopub.status.idle":"2021-07-24T18:06:32.699468Z","shell.execute_reply.started":"2021-07-24T18:06:32.681726Z","shell.execute_reply":"2021-07-24T18:06:32.698693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# _paths[19580]\n# train_labels[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.700601Z","iopub.execute_input":"2021-07-24T18:06:32.70107Z","iopub.status.idle":"2021-07-24T18:06:32.715175Z","shell.execute_reply.started":"2021-07-24T18:06:32.701031Z","shell.execute_reply":"2021-07-24T18:06:32.714129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Resizing","metadata":{"papermill":{"duration":0.03253,"end_time":"2021-04-23T01:30:20.63738","exception":false,"start_time":"2021-04-23T01:30:20.60485","status":"completed"},"tags":[]}},{"cell_type":"code","source":"image_size = 400\n\ndef decode_image(filename, label=None, image_size=(image_size, image_size)):\n    bits = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(bits, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0\n\n#     h, w = 2672, 4000# image.shape[-3], image.shape[-2]\n#     e= 500\n#     image = tf.image.crop_to_bounding_box(image, e+0, e+(w - h) // 2, h-e, h-e)\n    image = tf.image.resize(image, image_size)\n    if label is None:\n        return image\n    else:\n        return image, label\n\ndef data_augment(image, label=None):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    \n    if label is None:\n        return image\n    else:\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.716365Z","iopub.execute_input":"2021-07-24T18:06:32.716727Z","iopub.status.idle":"2021-07-24T18:06:32.733518Z","shell.execute_reply.started":"2021-07-24T18:06:32.716671Z","shell.execute_reply":"2021-07-24T18:06:32.732261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# _paths = _paths.tolist()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.734961Z","iopub.execute_input":"2021-07-24T18:06:32.735245Z","iopub.status.idle":"2021-07-24T18:06:32.747383Z","shell.execute_reply.started":"2021-07-24T18:06:32.735218Z","shell.execute_reply":"2021-07-24T18:06:32.746366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_paths, valid_paths, train_labels, valid_labels = train_test_split(_paths,_labels, test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.749114Z","iopub.execute_input":"2021-07-24T18:06:32.749538Z","iopub.status.idle":"2021-07-24T18:06:32.782062Z","shell.execute_reply.started":"2021-07-24T18:06:32.749496Z","shell.execute_reply":"2021-07-24T18:06:32.78094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_paths),len(valid_paths)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:32.783701Z","iopub.execute_input":"2021-07-24T18:06:32.784105Z","iopub.status.idle":"2021-07-24T18:06:32.790864Z","shell.execute_reply.started":"2021-07-24T18:06:32.784062Z","shell.execute_reply":"2021-07-24T18:06:32.789873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset = (\n# tf.data.Dataset\n#     .from_tensor_slices((X_train, y_train))\n#     .map(decode_image, num_parallel_calls=AUTO)\n#     .cache()\n#     .map(data_augment, num_parallel_calls=AUTO)\n#     .repeat()\n#     .shuffle(512)\n#     .batch(BATCH_SIZE)\n#     .prefetch(AUTO)\n# )\ntrain_dataset_1 = (\ntf.data.Dataset\n    .from_tensor_slices((train_paths, train_labels))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .cache()\n    .map(data_augment, num_parallel_calls=AUTO)\n    .repeat()\n    .shuffle(512)\n    .batch(64)\n    .prefetch(AUTO)\n)\n\nvalid_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices((valid_paths, valid_labels))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .cache()\n    .prefetch(AUTO)\n)\n\n# test_dataset = (\n#     tf.data.Dataset\n#     .from_tensor_slices(test_paths)\n#     .map(decode_image, num_parallel_calls=AUTO)\n#     .batch(BATCH_SIZE)\n# )","metadata":{"papermill":{"duration":0.304292,"end_time":"2021-04-23T01:30:21.123536","exception":false,"start_time":"2021-04-23T01:30:20.819244","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:06:32.792313Z","iopub.execute_input":"2021-07-24T18:06:32.792888Z","iopub.status.idle":"2021-07-24T18:06:33.070194Z","shell.execute_reply.started":"2021-07-24T18:06:32.792844Z","shell.execute_reply":"2021-07-24T18:06:33.069127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LR_START = 0.0001\nLR_MAX = 0.00005 * strategy.num_replicas_in_sync\nLR_MIN = 0.0001\nLR_RAMPUP_EPOCHS = 4\nLR_SUSTAIN_EPOCHS = 6\nLR_EXP_DECAY = .8\n\ndef lrfn(epoch):\n    if epoch < LR_RAMPUP_EPOCHS:\n        lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START\n    elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:\n        lr = LR_MAX\n    else:\n        lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN\n    return lr\n    \nlr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)\n\nrng = [i for i in range(EPOCHS)]\ny = [lrfn(x) for x in rng]\nplt.plot(rng, y)\nprint(\"Learning rate schedule: {:.3g} to {:.3g} to {:.3g}\".format(y[0], max(y), y[-1]))","metadata":{"papermill":{"duration":0.239139,"end_time":"2021-04-23T01:30:21.395111","exception":false,"start_time":"2021-04-23T01:30:21.155972","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:06:33.071503Z","iopub.execute_input":"2021-07-24T18:06:33.071853Z","iopub.status.idle":"2021-07-24T18:06:33.258634Z","shell.execute_reply.started":"2021-07-24T18:06:33.071819Z","shell.execute_reply":"2021-07-24T18:06:33.257975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Weight & Bias","metadata":{"papermill":{"duration":0.033514,"end_time":"2021-04-23T01:30:21.542385","exception":false,"start_time":"2021-04-23T01:30:21.508871","status":"completed"},"tags":[]}},{"cell_type":"code","source":"num_classes = len(labels)\nnum_classes","metadata":{"papermill":{"duration":0.040593,"end_time":"2021-04-23T01:30:21.616656","exception":false,"start_time":"2021-04-23T01:30:21.576063","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:06:33.259727Z","iopub.execute_input":"2021-07-24T18:06:33.260161Z","iopub.status.idle":"2021-07-24T18:06:33.265638Z","shell.execute_reply.started":"2021-07-24T18:06:33.260118Z","shell.execute_reply":"2021-07-24T18:06:33.264802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Architecture","metadata":{"papermill":{"duration":0.033858,"end_time":"2021-04-23T01:30:21.684604","exception":false,"start_time":"2021-04-23T01:30:21.650746","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# with strategy.scope():\n#     model = tf.keras.Sequential([\n#         rmodel,\n#         InceptionResNetV2(\n#             input_shape=(image_size, image_size, 3),\n#             weights='imagenet',\n#             include_top=False\n#         ),\n#         L.GlobalAveragePooling2D(),\n#         L.Dense(6, activation='softmax'), \n#         SoftProbField()\n#     ])\n \n#     model.compile(\n#         optimizer = 'adam',\n#         loss = 'categorical_crossentropy',\n#         metrics=['categorical_accuracy']\n#     )\n#     model.summary()","metadata":{"papermill":{"duration":43.817183,"end_time":"2021-04-23T01:31:05.535768","exception":false,"start_time":"2021-04-23T01:30:21.718585","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:06:33.266986Z","iopub.execute_input":"2021-07-24T18:06:33.267461Z","iopub.status.idle":"2021-07-24T18:06:33.275039Z","shell.execute_reply.started":"2021-07-24T18:06:33.267418Z","shell.execute_reply":"2021-07-24T18:06:33.274178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# STEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE\n\n# history = model.fit(\n#     train_dataset, \n#     epochs= EPOCHS, \n#     callbacks=[lr_callback],\n#     steps_per_epoch=STEPS_PER_EPOCH,\n#     #validation_data=valid_dataset\n# )\n\n# model.save('my_model1.h5') ","metadata":{"papermill":{"duration":1638.006957,"end_time":"2021-04-23T01:58:23.588782","exception":false,"start_time":"2021-04-23T01:31:05.581825","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:06:33.276276Z","iopub.execute_input":"2021-07-24T18:06:33.276731Z","iopub.status.idle":"2021-07-24T18:06:33.289293Z","shell.execute_reply.started":"2021-07-24T18:06:33.276676Z","shell.execute_reply":"2021-07-24T18:06:33.288453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\n\ndef recall_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef precision_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef f1_m(y_true, y_pred):\n    precision = precision_m(y_true, y_pred)\n    recall = recall_m(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:33.290701Z","iopub.execute_input":"2021-07-24T18:06:33.291136Z","iopub.status.idle":"2021-07-24T18:06:33.302169Z","shell.execute_reply.started":"2021-07-24T18:06:33.291094Z","shell.execute_reply":"2021-07-24T18:06:33.301104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import RMSprop, Adam, SGD\nfrom keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Input, GlobalAveragePooling2D, ReLU, Flatten, Dense, Dropout, BatchNormalization, MaxPooling2D, GlobalMaxPooling2D\nimport tensorflow_addons\nfrom tensorflow.keras.applications import ResNet152V2,EfficientNetB7, InceptionResNetV2, InceptionV3, Xception, VGG19,EfficientNetB4\nimport tensorflow.keras.layers as L\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:33.303441Z","iopub.execute_input":"2021-07-24T18:06:33.303969Z","iopub.status.idle":"2021-07-24T18:06:33.458671Z","shell.execute_reply.started":"2021-07-24T18:06:33.303932Z","shell.execute_reply":"2021-07-24T18:06:33.457808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import Input,Lambda,merge\nfrom keras.layers import Conv2D,Cropping2D,Average\nfrom keras.layers import MaxPooling2D,add\nfrom keras.utils import plot_model\nfrom keras import backend as K\nimport keras.layers as layers\n# data_augmentation = keras.Sequential(\n#     [\n#         layers.experimental.preprocessing.RandomFlip(\"horizontal\"),\n#         layers.experimental.preprocessing.RandomRotation(0.1),\n#     ]\n# )","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:33.459798Z","iopub.execute_input":"2021-07-24T18:06:33.46022Z","iopub.status.idle":"2021-07-24T18:06:33.465231Z","shell.execute_reply.started":"2021-07-24T18:06:33.46018Z","shell.execute_reply":"2021-07-24T18:06:33.464303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# https://keras.io/api/applications/\ndef make_model(input_shape, num_classes):\n    model = tf.keras.Sequential([\n#        keras.applications.VGG16(input_shape = input_shape, weights = 'imagenet', include_top = False),\n          EfficientNetB7(\n            input_shape=input_shape, weights='imagenet', include_top=False),\n        L.GlobalAveragePooling2D(),\n        L.Dense(num_classes, activation='sigmoid')])\n    return model\n        \nwith strategy.scope():\n    model = make_model(input_shape=(image_size,image_size,3), num_classes=len(labels))\n#     opt = keras.optimizers.Adam(learning_rate=0.01)\n    model.compile(loss='binary_crossentropy', optimizer=\"adam\", \n                  metrics= ['categorical_accuracy','acc',f1_m,precision_m, recall_m])\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:06:33.466444Z","iopub.execute_input":"2021-07-24T18:06:33.466824Z","iopub.status.idle":"2021-07-24T18:07:15.980571Z","shell.execute_reply.started":"2021-07-24T18:06:33.466792Z","shell.execute_reply":"2021-07-24T18:07:15.97963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.init(entity='aaiit', project='visualize-models-fashion', name=target )","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:07:15.981656Z","iopub.execute_input":"2021-07-24T18:07:15.981986Z","iopub.status.idle":"2021-07-24T18:07:22.387984Z","shell.execute_reply.started":"2021-07-24T18:07:15.981958Z","shell.execute_reply":"2021-07-24T18:07:22.387113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEPS_PER_EPOCH = train_labels.shape[0] // 32\n# model2.load_weights('../input/tpu-tf-there-are-only-12-classes/my_model2.h5') \n\n\n\n# history = model.fit(\n#     train_dataset_1, \n#     epochs= 1, \n#     callbacks=[lr_callback],\n#     steps_per_epoch=10,\n#     validation_data=valid_dataset\n# )","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:07:22.389353Z","iopub.execute_input":"2021-07-24T18:07:22.389817Z","iopub.status.idle":"2021-07-24T18:13:26.777725Z","shell.execute_reply.started":"2021-07-24T18:07:22.389773Z","shell.execute_reply":"2021-07-24T18:13:26.776659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# WandbCallback()\n\nhistory = model.fit(\n    train_dataset_1, \n    epochs= EPOCHS, \n    callbacks=[lr_callback],\n    steps_per_epoch=STEPS_PER_EPOCH,\n    validation_data=valid_dataset\n)","metadata":{"papermill":{"duration":1995.226586,"end_time":"2021-04-23T02:32:29.638289","exception":false,"start_time":"2021-04-23T01:59:14.411703","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:13:26.779412Z","iopub.execute_input":"2021-07-24T18:13:26.779868Z","iopub.status.idle":"2021-07-24T18:13:36.822525Z","shell.execute_reply.started":"2021-07-24T18:13:26.779817Z","shell.execute_reply":"2021-07-24T18:13:36.820211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# N = 56\n# path,label = valid_paths[N].replace(GCS_DS_PATH,\"../input/fashion\"), valid_labels[N]\n# path,label","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:13:36.823822Z","iopub.status.idle":"2021-07-24T18:13:36.824264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.preprocessing.image import load_img,img_to_array\n\n# image = path\n# img = img_to_array(load_img(image,target_size=(image_size,image_size,3)))\n# plt.imshow(img/255.)\n# img.shape\n","metadata":{"papermill":{"duration":45.856491,"end_time":"2021-04-23T02:33:24.559523","exception":false,"start_time":"2021-04-23T02:32:38.703032","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:13:36.825938Z","iopub.status.idle":"2021-07-24T18:13:36.826759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def make_prediction(model,image,shape):\n#     img = img_to_array(load_img(image,target_size=shape))\n#     img = np.expand_dims(img,axis=0)/255.\n#     pred = model.predict(img)[0]\n#     pred =( pred>0.5)*1\n#     s = []\n#     for i in range(len(labels)):\n#         if pred[i]==1:\n#             s.append(labels[i])\n#     print(\"\\\\\".join(s))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:13:36.82838Z","iopub.status.idle":"2021-07-24T18:13:36.829094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label\n# s = []\n# for i in range(len(labels)):\n#     if label[i]==1:\n#         s.append(labels[i])\n# print(\"Real style label: \"+\"\\\\\".join(s))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:13:36.830714Z","iopub.status.idle":"2021-07-24T18:13:36.831396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# pred = make_prediction(model,image,(image_size,image_size,3))\n# pred[16]","metadata":{"papermill":{"duration":4.547928,"end_time":"2021-04-23T02:55:15.497639","exception":false,"start_time":"2021-04-23T02:55:10.949711","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:13:36.833067Z","iopub.status.idle":"2021-07-24T18:13:36.833745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-24T18:13:36.835532Z","iopub.status.idle":"2021-07-24T18:13:36.836081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#     model5 = tf.keras.Sequential([\n#            InceptionV3(\n#             input_shape=(image_size, image_size, 3), weights='imagenet', include_top=False),\n#         L.GlobalAveragePooling2D(),\n#         L.Dense(12, activation='softmax')\n","metadata":{"papermill":{"duration":24.091824,"end_time":"2021-04-23T02:55:53.561598","exception":false,"start_time":"2021-04-23T02:55:29.469774","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:13:36.836979Z","iopub.status.idle":"2021-07-24T18:13:36.837387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#     model6 = tf.keras.Sequential([\n#            Xception(\n#             input_shape=(image_size, image_size, 3), weights='imagenet', include_top=False),\n#         L.GlobalAveragePooling2D(),\n#         L.Dense(12, activation='softmax'), \n","metadata":{"papermill":{"duration":17.906206,"end_time":"2021-04-23T03:07:55.65334","exception":false,"start_time":"2021-04-23T03:07:37.747134","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-24T18:13:36.838407Z","iopub.status.idle":"2021-07-24T18:13:36.838895Z"},"trusted":true},"execution_count":null,"outputs":[]}]}