{"cells":[{"metadata":{},"cell_type":"markdown","source":"The baseline model provided by organizers was really tricky to work with. In particular, it was hard even to re-save the model, since the variables were not recorded.","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"model = tf.saved_model.load('../input/baseline-landmark-retrieval-model/baseline_landmark_retrieval_model')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(model.variables)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It turned out that getting to model's variables (which then can be used, for example, for loading them into Keras reimplementation, using Keras `set_weights()` function from `Model` API) is tricky. Below I present the way to load the weights into numpy arrays, in order to be able to use them later (for example in Keras).","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Firstly, this magic one-liner shows us list of variables that are hidden in model's graph.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model.graph.get_collection('variables')[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"var_names = [var.name for var in model.graph.get_collection('variables')]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now that we have variable names, we can use the following trick, which I discovered by reading TF source code and examining model in Tensorboard, to load the weights into numpy arrays.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"test_in = tf.constant(np.uint8(np.random.randn(300, 300, 3)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"var_names_to_fetch = [\n    var_name[:-2] + '/read' + var_name[-2:] for var_name in var_names]\n\nweight_fetcher = model.prune(\n    feeds=[\"input_image:0\"],\n    fetches=var_names_to_fetch\n)\n\nweights = weight_fetcher(test_in)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now, all numpy arrays with model parameters are saved into eager Tensors in the `weights` list.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"len(weights) == len(model.graph.get_collection('variables'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weights[0].numpy().shape, weights[0].numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}