{"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":"print(\"\\n... IMPORTS STARTING ...\\n\")\n\nprint(\"\\n\\tVERSION INFORMATION\")\n\n# Machine Learning and Data Science Imports\nimport tensorflow as tf; print(f\"\\t\\t– TENSORFLOW VERSION: {tf.__version__}\");\nimport tensorflow_hub as tfhub; print(f\"\\t\\t– TENSORFLOW HUB VERSION: {tfhub.__version__}\");\nimport tensorflow_addons as tfa; print(f\"\\t\\t– TENSORFLOW ADDONS VERSION: {tfa.__version__}\");\nimport pandas as pd; pd.options.mode.chained_assignment = None;\nimport numpy as np; print(f\"\\t\\t– NUMPY VERSION: {np.__version__}\");\nimport sklearn; print(f\"\\t\\t– SKLEARN VERSION: {sklearn.__version__}\");\nfrom sklearn.preprocessing import RobustScaler, PolynomialFeatures\nfrom pandarallel import pandarallel; pandarallel.initialize();\nfrom sklearn.model_selection import GroupKFold, StratifiedKFold\nfrom scipy.spatial import cKDTree\n\n# # RAPIDS\n# import cudf, cupy, cuml\n# from cuml.neighbors import NearestNeighbors\n# from cuml.manifold import TSNE, UMAP\n# from cuml import PCA\n\n# Built In Imports\nfrom kaggle_datasets import KaggleDatasets\nfrom collections import Counter\nfrom datetime import datetime\nfrom zipfile import ZipFile\nfrom glob import glob\nimport openslide\nimport warnings\nimport requests\nimport hashlib\nimport imageio\nimport IPython\nimport sklearn\nimport urllib\nimport zipfile\nimport pickle\nimport random\nimport shutil\nimport string\nimport json\nimport math\nimport time\nimport gzip\nimport ast\nimport sys\nimport io\nimport os\nimport gc\nimport re\n\n# Visualization Imports\nfrom matplotlib.colors import ListedColormap\nfrom matplotlib.patches import Rectangle\nimport matplotlib.patches as patches\nimport plotly.graph_objects as go\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm; tqdm.pandas();\nimport plotly.express as px\nimport tifffile as tif\nimport seaborn as sns\nfrom PIL import Image, ImageEnhance; Image.MAX_IMAGE_PIXELS = 5_000_000_000;\nimport matplotlib; print(f\"\\t\\t– MATPLOTLIB VERSION: {matplotlib.__version__}\");\nfrom matplotlib import animation, rc; rc('animation', html='jshtml')\nimport plotly\nimport PIL\nimport cv2\n\nimport plotly.io as pio\nprint(pio.renderers)\n\ndef seed_it_all(seed=7):\n    \"\"\" Attempt to be Reproducible \"\"\"\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\nseed_it_all()\n    \nprint(\"\\n\\n... IMPORTS COMPLETE ...\\n\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(_bb, model_dim=512):\n    \n    _inputs = tf.keras.layers.Input(shape=(None,None, 3), dtype='uint8')\n    \n    # Main part\n    x = tf.keras.layers.Resizing(model_dim, model_dim, crop_to_aspect_ratio=True)(tf.cast(_inputs, tf.float32))\n    x = _bb(x, training=False)\n    \n    _output_1 = tf.keras.layers.Reshape((-1,), name=\"embedding\")(x)    \n    \n    # embedding normalization\n    _output_2 = tf.keras.layers.Lambda(lambda x: tf.nn.l2_normalize(x), name=\"embedding_norm\")(_output_1)    \n    \n    # Return model\n    return tf.keras.Model(inputs=_inputs, outputs=[_output_1, _output_2])\n                          \n_model = get_model(tf.keras.models.load_model(\"/kaggle/input/part-2-tf-keras-train-custom-clip-like/vision_encoder_512\"))\n_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T21:12:51.228827Z","iopub.execute_input":"2022-07-27T21:12:51.22925Z","iopub.status.idle":"2022-07-27T21:13:53.276363Z","shell.execute_reply.started":"2022-07-27T21:12:51.229207Z","shell.execute_reply":"2022-07-27T21:13:53.274456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_model(tf.ones((1,111,111,3), dtype=tf.uint8))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T21:13:53.27836Z","iopub.execute_input":"2022-07-27T21:13:53.280017Z","iopub.status.idle":"2022-07-27T21:14:00.431255Z","shell.execute_reply.started":"2022-07-27T21:13:53.279929Z","shell.execute_reply":"2022-07-27T21:14:00.430109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_submission_zip(model_dir, output_dir=\".\"):\n    with ZipFile(os.path.join(output_dir, 'submission.zip'),'w') as zip:           \n        zip.write(os.path.join(model_dir, 'saved_model.pb'), arcname='saved_model.pb') \n        zip.write(os.path.join(model_dir, 'variables', 'variables.data-00000-of-00001'), arcname='variables/variables.data-00000-of-00001') \n        zip.write(os.path.join(model_dir, 'variables', 'variables.index'), arcname='variables/variables.index') \n\n# Save fresh model to directory\n!rm -rf ./models\nos.makedirs(\"./models\", exist_ok=True)\n_model.save(\"./models\")\n\n# Show unzipped contents\nprint(os.listdir(\"./models\"))\n\nmake_submission_zip(\"./models\", output_dir=\".\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T21:14:00.434019Z","iopub.execute_input":"2022-07-27T21:14:00.43505Z","iopub.status.idle":"2022-07-27T21:14:26.495626Z","shell.execute_reply.started":"2022-07-27T21:14:00.434995Z","shell.execute_reply":"2022-07-27T21:14:26.492693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf ./model*\n!rm -rf ./tmp*\n!ls .","metadata":{"execution":{"iopub.status.busy":"2022-07-27T21:14:26.496733Z","iopub.status.idle":"2022-07-27T21:14:26.49821Z","shell.execute_reply.started":"2022-07-27T21:14:26.497911Z","shell.execute_reply":"2022-07-27T21:14:26.497938Z"},"trusted":true},"execution_count":null,"outputs":[]}]}