def testSaveModelSucceedsForNestedKerasModel(self): inner_model = keras.Sequential([ keras.layers.Dense(4, input_shape=[3], activation='relu'), keras.layers.Dense(3, activation='tanh') ]) outer_model = keras.Sequential() outer_model.add(inner_model) outer_model.add(keras.layers.Dense(1, activation='sigmoid')) conversion.save_keras_model(outer_model, self._tmp_dir) # Verify the content of the artifacts output directory. self.assertTrue( os.path.isfile(os.path.join(self._tmp_dir, 'group1-shard1of1.bin'))) model_json = json.load( open(os.path.join(self._tmp_dir, 'model.json'), 'rt')) topology_json = model_json['modelTopology'] self.assertIn('keras_version', topology_json) self.assertIn('backend', topology_json) self.assertIn('model_config', topology_json) # Verify that all the layers' weights are present. weights_manifest = model_json['weightsManifest'] self.assertIsInstance(weights_manifest, list) weight_entries = [] for group in weights_manifest: weight_entries.extend(group['weights']) self.assertEqual(6, len(weight_entries))
def _init_recurrent_model(self): x = 16 self.frame_enc = tfk.Sequential([ tfkl.Conv2D(2 * x, 3, 2, input_shape=(64, 64, 3)), tfkl.LeakyReLU(), tfkl.Conv2D(4 * x, 3, 2), tfkl.LeakyReLU(), tfkl.Conv2D(1 * x, 3, 2), tfkl.LeakyReLU(), tfkl.Flatten(), tfkl.Dense(1 * x), ]) if self.is_discrete_action: action_space_size = self._action_space.n else: action_space_size = self._action_space.shape[0] self.action_enc = tfk.Sequential([ tfkl.Dense(4 * x, input_shape=(self.output_length, action_space_size)), tfkl.LeakyReLU(), tfkl.Dense(2 * x), tfkl.LeakyReLU(), tfkl.Dense(1 * x), tfkl.LeakyReLU(), ]) self.reward_pred = tfk.Sequential([ tfkl.LSTM(256, return_sequences=True, input_shape=(self.output_length, 2 * x)), tfkl.LayerNormalization(), tfkl.LeakyReLU(), tfkl.LSTM(128, return_sequences=True), tfkl.LayerNormalization(), tfkl.LeakyReLU(), tfkl.LSTM(64, return_sequences=True), tfkl.LayerNormalization(), tfkl.LeakyReLU(), tfkl.Dense(8 * x), tfkl.LeakyReLU(), tfkl.Dropout(0.2), tfkl.Dense(2 * x), tfkl.LeakyReLU(), tfkl.Dropout(0.2), tfkl.Dense(1) ])
def _init_model(self): x = 16 self.frame_enc = tfk.Sequential([ tfkl.Conv2D(2 * x, 3, 2, input_shape=(64, 64, 3)), tfkl.LeakyReLU(), tfkl.Conv2D(4 * x, 3, 2), tfkl.LeakyReLU(), tfkl.Conv2D(1 * x, 3, 2), tfkl.LeakyReLU(), tfkl.Flatten(), tfkl.Dense(1 * x), ]) if self.is_discrete_action: action_space_size = self._action_space.n else: action_space_size = self._action_space.shape[0] self.action_enc = tfk.Sequential([ tfkl.Flatten(input_shape=(self.output_length, action_space_size)), tfkl.Dense(4 * x), tfkl.LeakyReLU(), tfkl.Dense(2 * x), tfkl.LeakyReLU(), tfkl.Dense(1 * x), tfkl.LeakyReLU(), ]) self.reward_pred = tfk.Sequential([ tfkl.Flatten(input_shape=(2 * x, )), tfkl.Dense(8 * x), tfkl.LeakyReLU(), tfkl.Dropout(0.2), tfkl.Dense(2 * x), tfkl.LeakyReLU(), tfkl.Dropout(0.2), tfkl.Dense(self.output_length) ])
def testConvertModelWithNestedLayerNames(self): model = keras.Sequential() # Add a layer with a nested layer name, i.e., a layer name with slash(es) # in it. model.add(keras.layers.Dense(2, input_shape=[12], name='dense')) model.add(keras.layers.Dense(8, name='foo/dense')) model.add(keras.layers.Dense(4, name='foo/bar/dense')) tfjs_path = os.path.join(self._tmp_dir, 'nested_layer_names_model') conversion.save_keras_model(model, tfjs_path) # Check model.json and weights manifest. with open(os.path.join(tfjs_path, 'model.json'), 'rt') as f: model_json = json.load(f) # Check meta-data in the artifact JSON. self.assertEqual(model_json['format'], 'layers-model') self.assertEqual(model_json['generatedBy'], 'keras v%s' % keras.__version__) self.assertEqual(model_json['convertedBy'], 'TensorFlow.js Converter v%s' % version.version) self.assertTrue(model_json['modelTopology']) weights_manifest = model_json['weightsManifest'] weight_shapes = dict() for group in weights_manifest: for weight in group['weights']: weight_shapes[weight['name']] = weight['shape'] self.assertEqual( sorted([ 'dense/kernel', 'dense/bias', 'foo/dense/kernel', 'foo/dense/bias', 'foo/bar/dense/kernel', 'foo/bar/dense/bias' ]), sorted(list(weight_shapes.keys()))) self.assertEqual([12, 2], weight_shapes['dense/kernel']) self.assertEqual([2], weight_shapes['dense/bias']) self.assertEqual([2, 8], weight_shapes['foo/dense/kernel']) self.assertEqual([8], weight_shapes['foo/dense/bias']) self.assertEqual([8, 4], weight_shapes['foo/bar/dense/kernel']) self.assertEqual([4], weight_shapes['foo/bar/dense/bias'])
def testSavedModelSucceedsForExistingDirAndSequential(self): artifacts_dir = os.path.join(self._tmp_dir, 'artifacts') os.makedirs(artifacts_dir) model = keras.Sequential() model.add(keras.layers.Dense(3, input_shape=[2])) conversion.save_keras_model(model, artifacts_dir) # Verify the content of the artifacts output directory. self.assertTrue( os.path.isfile(os.path.join(artifacts_dir, 'group1-shard1of1.bin'))) model_json = json.load( open(os.path.join(artifacts_dir, 'model.json'), 'rt')) topology_json = model_json['modelTopology'] self.assertIn('keras_version', topology_json) self.assertIn('backend', topology_json) self.assertIn('model_config', topology_json) weights_manifest = model_json['weightsManifest'] self.assertIsInstance(weights_manifest, list) self.assertEqual(1, len(weights_manifest)) self.assertIn('paths', weights_manifest[0])
def testSaveModelSucceedsForTfKerasSequentialModel(self): model = keras.Sequential([keras.layers.Dense(1, input_shape=[2])]) # `keras.Model`s must be compiled before they can be saved. model.compile(loss='mean_squared_error', optimizer='sgd') conversion.save_keras_model(model, self._tmp_dir) # Verify the content of the artifacts output directory. self.assertTrue( os.path.isfile(os.path.join(self._tmp_dir, 'group1-shard1of1.bin'))) model_json = json.load( open(os.path.join(self._tmp_dir, 'model.json'), 'rt')) topology_json = model_json['modelTopology'] self.assertIn('keras_version', topology_json) self.assertIn('backend', topology_json) self.assertIn('model_config', topology_json) weights_manifest = model_json['weightsManifest'] self.assertIsInstance(weights_manifest, list) self.assertEqual(1, len(weights_manifest)) self.assertIn('paths', weights_manifest[0])