def testTFSummaryTensor(self): """Verify processing of tf.summary.tensor.""" event_sink = _EventGenerator(self, zero_out_timestamps=True) writer = tf.summary.FileWriter(self.get_temp_dir()) writer.event_writer = event_sink with self.test_session() as sess: tf.summary.tensor_summary('scalar', tf.constant(1.0)) tf.summary.tensor_summary('vector', tf.constant([1.0, 2.0, 3.0])) tf.summary.tensor_summary('string', tf.constant(six.b('foobar'))) merged = tf.summary.merge_all() summ = sess.run(merged) writer.add_summary(summ, 0) accumulator = ea.EventAccumulator(event_sink) accumulator.Reload() self.assertTagsEqual(accumulator.Tags(), { ea.TENSORS: ['scalar', 'vector', 'string'], }) scalar_proto = accumulator.Tensors('scalar')[0].tensor_proto scalar = tf.make_ndarray(scalar_proto) vector_proto = accumulator.Tensors('vector')[0].tensor_proto vector = tf.make_ndarray(vector_proto) string_proto = accumulator.Tensors('string')[0].tensor_proto string = tf.make_ndarray(string_proto) self.assertTrue(np.array_equal(scalar, 1.0)) self.assertTrue(np.array_equal(vector, [1.0, 2.0, 3.0])) self.assertTrue(np.array_equal(string, six.b('foobar')))
def _make_grpc_request(examples): """Builds and sends request to TensorFlow model server.""" request = predict_pb2.PredictRequest() request.model_spec.name = servable_name request.inputs["input"].CopyFrom( tf.contrib.util.make_tensor_proto( [ex.SerializeToString() for ex in examples], shape=[len(examples)])) response = stub.Predict(request, timeout_secs) outputs = tf.make_ndarray(response.outputs["outputs"]) scores = tf.make_ndarray(response.outputs["scores"]) assert len(outputs) == len(scores) return [{ "outputs": outputs[i], "scores": scores[i] } for i in range(len(outputs))]
def test_audio_count_when_more_than_max(self): max_outputs = len(self.stereo) + 2 pb = self.compute_and_check_summary_pb('k488', self.stereo, max_outputs=max_outputs) self.assertEqual(1, len(pb.value)) results = tf.make_ndarray(pb.value[0].tensor) self.assertEqual(results.shape, (len(self.stereo), 2))
def compute_and_check_summary_pb(self, name, audio, max_outputs=3, display_name=None, description=None, audio_tensor=None, feed_dict=None): """Use both `op` and `pb` to get a summary, asserting validity. "Validity" means that the `op` and `pb` functions must return the same protobufs, and also that each encoded audio value appears to be a valid WAV file. If either of these conditions fails, the test will immediately fail. Otherwise, the valid protobuf will be returned. Returns: A `Summary` protocol buffer. """ if audio_tensor is None: audio_tensor = tf.constant(audio) op = summary.op(name, audio_tensor, self.samples_per_second, max_outputs=max_outputs, display_name=display_name, description=description) pb = summary.pb(name, audio, self.samples_per_second, max_outputs=max_outputs, display_name=display_name, description=description) pb_via_op = self.pb_via_op(op, feed_dict=feed_dict) self.assertProtoEquals(pb, pb_via_op) audios = tf.make_ndarray(pb.value[0].tensor)[:, 0].tolist() invalid_audios = [x for x in audios if not x.startswith(b'RIFF')] self.assertFalse(invalid_audios) return pb
def _audio_response_for_run(self, tensor_events, run, tag, sample): """Builds a JSON-serializable object with information about audio. Args: tensor_events: A list of image event_accumulator.TensorEvent objects. run: The name of the run. tag: The name of the tag the audio entries all belong to. sample: The zero-indexed sample of the audio sample for which to retrieve information. For instance, setting `sample` to `2` will fetch information about only the third audio clip of each batch, and steps with fewer than three audio clips will be omitted from the results. Returns: A list of dictionaries containing the wall time, step, URL, width, and height for each audio entry. """ response = [] index = 0 filtered_events = self._filter_by_sample(tensor_events, sample) content_type = self._get_mime_type(run, tag) for (index, tensor_event) in enumerate(filtered_events): data = tf.make_ndarray(tensor_event.tensor_proto) label = data[sample, 1] response.append({ 'wall_time': tensor_event.wall_time, 'step': tensor_event.step, 'label': plugin_util.markdown_to_safe_html(label), 'contentType': content_type, 'query': self._query_for_individual_audio(run, tag, sample, index) }) return response
def _ProcessHealthPillSummary(self, value, event): """Process summaries containing health pills. These summaries are distinguished by the fact that they have a Tensor field and have a special tag value. This method emits ERROR-level messages to the logs if it encounters Tensor summaries that it cannot process. Args: value: A tf.Summary.Value with a Tensor field. event: The tf.Event containing that value. """ elements = tf.make_ndarray(value.tensor) # The node_name property of the value object is actually a watch key: a # combination of node name, output slot, and a suffix. We capture the # actual node name and the output slot with a regular expression. match = re.match(r'^(.*):(\d+):DebugNumericSummary$', value.node_name) if not match: tf.logging.log_first_n( tf.logging.ERROR, 'Unsupported watch key %s for health pills; skipping this sequence.', 1, value.node_name) return node_name = match.group(1) output_slot = int(match.group(2)) device_name = value.tag[len(HEALTH_PILL_EVENT_TAG_PREFIX):] self._ProcessHealthPill(event.wall_time, event.step, device_name, node_name, output_slot, elements)
def post(self, model, version=None): request_key = self.settings['request_key'] request_data = tornado.escape.json_decode(self.request.body) instances = request_data.get(request_key) if not instances: self.send_error('Request json object have to use the key: %s'%request_key) if len(instances) < 1 or not isinstance(instances, (list, tuple)): self.send_error('Request instances object have to use be a list') instances = decode_b64_if_needed(instances) input_columns = instances[0].keys() request = predict_pb2.PredictRequest() request.model_spec.name = model if version is not None: request.model_spec.version = version for input_column in input_columns: values = [instance[input_column] for instance in instances] request.inputs[input_column].CopyFrom(tf.make_tensor_proto(values, shape=[len(values)])) stub = self.settings['stub'] result = yield fwrap(stub.Predict.future(request, self.settings['rpc_timeout'])) output_keys = result.outputs.keys() predictions = zip(*[tf.make_ndarray(result.outputs[output_key]).tolist() for output_key in output_keys]) predictions = [dict(zip(*t)) for t in zip(repeat(output_keys), predictions)] self.write(dict(predictions=predictions))
def test_correctly_handles_no_audio(self): shape = (0, self.audio_length, 2) audio = np.array([]).reshape(shape).astype(np.float32) pb = self.compute_and_check_summary_pb('k488', audio, max_outputs=3) self.assertEqual(1, len(pb.value)) results = tf.make_ndarray(pb.value[0].tensor) self.assertEqual(results.shape, (0, 2))
def last_metric_eval(multiplexer, session_name, metric_name): """Returns the last evaluations of the given metric at the given session. Args: multiplexer: The EventMultiplexer instance allowing access to the exported summary data. session_name: String. The session name for which to get the metric evaluations. metric_name: api_pb2.MetricName proto. The name of the metric to use. Returns: A 3-tuples, of the form [wall-time, step, value], denoting the last evaluation of the metric, where wall-time denotes the wall time in seconds since UNIX epoch of the time of the evaluation, step denotes the training step at which the model is evaluated, and value denotes the (scalar real) value of the metric. Raises: KeyError if the given session does not have the metric. """ assert isinstance(session_name, str) assert isinstance(metric_name, api_pb2.MetricName) run = session_name + metric_name.group tag = metric_name.tag try: tensor_events = multiplexer.Tensors(run=run, tag=tag) except KeyError as e: raise KeyError( 'Can\'t find metric %s for session: %s. Underlying error message: %s' % (metric_name, session_name, e)) last_event = tensor_events[-1] # TODO(erez): Raise HParamsError if the tensor is not a 0-D real scalar. return (last_event.wall_time, last_event.step, tf.make_ndarray(last_event.tensor_proto).item())
def _verify_event_lists_have_same_tensor_values(self, expected, gotten): """Checks that two lists of events have the same tensor values. Args: expected: The expected list of events. gotten: The list of events we actually got. """ self.assertEqual(len(expected), len(gotten)) # Compare the events one at a time. for expected_event, gotten_event in zip(expected, gotten): self.assertEqual(expected_event.summary.value[0].node_name, gotten_event.summary.value[0].node_name) self.assertAllClose( tf.make_ndarray(expected_event.summary.value[0].tensor), tf.make_ndarray(gotten_event.summary.value[0].tensor)) self.assertEqual(expected_event.summary.value[0].tag, gotten_event.summary.value[0].tag)
def process_string_tensor_event(event): """Convert a TensorEvent into a JSON-compatible response.""" string_arr = tf.make_ndarray(event.tensor_proto) html = text_array_to_html(string_arr) return { 'wall_time': event.wall_time, 'step': event.step, 'text': html, }
def on_value_event(self, event): """Records the summary values based on an updated message from the debugger. Logs an error message if writing the event to disk fails. Args: event: The Event proto to be processed. """ if not event.summary.value: tf.logging.warn("The summary of the event lacks a value.") return # The node name property is actually a watch key, which is a concatenation # of several pieces of data. watch_key = event.summary.value[0].node_name if not watch_key.endswith(constants.DEBUG_NUMERIC_SUMMARY_SUFFIX): # Ignore events that lack a DebugNumericSummary. # NOTE(@chihuahua): We may later handle other types of debug ops. return # We remove the constants.DEBUG_NUMERIC_SUMMARY_SUFFIX from the end of the # watch name because it is not distinguishing: every health pill entry ends # with it. node_name_and_output_slot = watch_key[ :-len(constants.DEBUG_NUMERIC_SUMMARY_SUFFIX)] shape = tf.make_ndarray(event.summary.value[0].tensor).shape if (len(shape) != 1 or shape[0] < constants.MIN_DEBUG_NUMERIC_SUMMARY_TENSOR_LENGTH): tf.logging.warning("Health-pill tensor either lacks a dimension or is " "shaped incorrectly: %s" % shape) return match = re.match(r"^(.*):(\d+)$", node_name_and_output_slot) if not match: tf.logging.warning( ("A event with a health pill has an invalid node name and output " "slot combination, (i.e., an unexpected debug op): %r"), node_name_and_output_slot) return if self._session_run_index >= 0: event.step = self._session_run_index else: # Data from parameter servers (or any graphs without a master) do not # contain core metadata. So the session run count is missing. Set its # value to a microsecond epoch timestamp. event.step = int(time.time() * 1e6) # Write this event to the events file designated for data from the # debugger. self._events_writer_manager.write_event(event) alert = numerics_alert.extract_numerics_alert(event) if self._numerics_alert_callback and alert: self._numerics_alert_callback(alert)
def parse_translation_result(result, sentence_processor): """Parses a translation result. Args: result: A `PredictResponse` proto. sentence_processor: A `sentencepiece.SentenceProcessor` Returns: A result string """ lengths = tf.make_ndarray(result.outputs["length"])[0] hypotheses = tf.make_ndarray(result.outputs["tokens"])[0] # Only consider the first hypothesis (the best one). best_hypothesis = hypotheses[0] best_length = lengths[0] model_out = best_hypothesis[0:best_length - 1] # Ignore </s> pieces = sentence_processor.DecodePieces(list(model_out)) return codecs.decode(pieces)
def _serve_individual_audio(self, request): """Serve encoded audio data.""" tag = request.args.get('tag') run = request.args.get('run') index = int(request.args.get('index')) sample = int(request.args.get('sample', 0)) events = self._filter_by_sample(self._multiplexer.Tensors(run, tag), sample) data = tf.make_ndarray(events[index].tensor_proto)[sample, 0] mime_type = self._get_mime_type(run, tag) return http_util.Respond(request, data, mime_type)
def read_tensor_summary(path): with tf.gfile.Open(path, 'rb') as summary_file: summary_string = summary_file.read() if not summary_string: raise message.DecodeError('Empty summary.') summary_proto = tf.Summary() summary_proto.ParseFromString(summary_string) tensor_proto = summary_proto.value[0].tensor array = tf.make_ndarray(tensor_proto) return array
def test_np_array_unicode_value(self): pb = self.compute_and_check_summary_pb( 'fa', np.array( [[u'A', u'long', u'long'], [u'way', u'to', u'run \u203C']])) values = tf.make_ndarray(pb.value[0].tensor).tolist() self.assertEqual( [[b'A', b'long', b'long'], [b'way', b'to', b'run \xe2\x80\xbc']], values) # Check that all entries are byte strings. for vectors in values: for value in vectors: self.assertIsInstance(value, six.binary_type)
def scalars_impl(self, tag, run, output_format): """Result of the form `(body, mime_type)`.""" tensor_events = self._multiplexer.Tensors(run, tag) values = [[tensor_event.wall_time, tensor_event.step, tf.make_ndarray(tensor_event.tensor_proto).item()] for tensor_event in tensor_events] if output_format == OutputFormat.CSV: string_io = StringIO() writer = csv.writer(string_io) writer.writerow(['Wall time', 'Step', 'Value']) writer.writerows(values) return (string_io.getvalue(), 'text/csv') else: return (values, 'application/json')
def ddpg(episode, breaking_step, reward_name): env = gym.make('AntPyBulletEnv-v0') cumulus_steps = 0 episode_steps = 0 # randomly initialize critics and actor with weights and biases q1 = CriticNN() q1.compile(optimizer=Adam(learning_rate=0.001), loss='mse') q2 = CriticNN() q2.compile(optimizer=Adam(learning_rate=0.001), loss='mse') mu = ActorNN(env.action_space.shape[0]) mu.compile(optimizer=Adam(learning_rate=0.001), loss='mse') # initialize target networks q1_target = CriticNN() q1_target.compile(optimizer=Adam(learning_rate=0.001), loss='mse') q2_target = CriticNN() q2_target.compile(optimizer=Adam(learning_rate=0.001), loss='mse') mu_target = ActorNN(env.action_space.shape[0]) mu_target.compile(optimizer=Adam(learning_rate=0.001), loss='mse') q1_target, q2_target, mu_target = update_network_parameters( q1, q1_target, q2, q2_target, mu, mu_target, 0.005) # initialize replay buffer (actor critic train only after batch is full 64!) replay_buffer = ReplayBuffer(1000000, env.observation_space.shape[0], env.action_space.shape[0]) performance = [] avg_return = [] time_step_reward = [] avg_time_step_reward = [] a_c = 0 b_c = 0 c_c = 0 d_c = 0 e_c = 0 f_c = 0 for e in range(episode): # receive initial observation state s1 (observation = s1) # env.render() observation = env.reset() state = tf.convert_to_tensor([observation], dtype=tf.float32) max_steps = 1000 min_action = env.action_space.low[0] max_action = env.action_space.high[0] update_frequency = 2 learn_count = 0 score = 0 for i in range(max_steps): # select an action a_t = mu(state) + noise noise = NormalActionNoise(0, 0.1) if cumulus_steps < 900: action = env.action_space.sample() else: action = mu(state) + np.random.normal(noise.mean, noise.sigma) proto_tensor = tf.make_tensor_proto(action) action = tf.make_ndarray(proto_tensor) action = action[0] # execute action a_t and observe reward, and next state action[2] = 0 action[3] = 0 next_state, reward, done, _ = env.step(action) reward_list = env.env.rewards z_pos = env.env.robot.body_xyz[2] fwp = reward_list[1] if fwp > 0: reward = reward + fwp * z_pos # store transition in replay buffer replay_buffer.store_transition(state, action, reward, next_state, done) # if there are enough transitions in the replay buffer batch_size = 100 if replay_buffer.mem_cntr >= batch_size: # sample a random mini batch of n=64 transitions buff_state, buff_action, buff_reward, buff_next_state, buff_done = replay_buffer.sample_buffer( batch_size) states = tf.convert_to_tensor(buff_state, dtype=tf.float32) next_states = tf.convert_to_tensor(buff_next_state, dtype=tf.float32) rewards = tf.convert_to_tensor(buff_reward, dtype=tf.float32) actions = tf.convert_to_tensor(buff_action, dtype=tf.float32) # train critics with tf.GradientTape(persistent=True) as tape: # calculate which actions target_actor chooses and add noise target_actions = mu_target(next_states) + tf.clip_by_value( np.random.normal(scale=0.2), -0.5, 0.5) target_actions = tf.clip_by_value(target_actions, min_action, max_action) # calculate next_q_values of the critic by feeding the next state and from actor chosen actions next_critic_value1 = tf.squeeze( q1_target(next_states, target_actions), 1) next_critic_value2 = tf.squeeze( q2_target(next_states, target_actions), 1) # calculate q values of critic actual state critic_value1 = tf.squeeze(q1(states, actions), 1) critic_value2 = tf.squeeze(q2(states, actions), 1) # use smaller q value from the 2 critics next_critic_value = tf.math.minimum( next_critic_value1, next_critic_value2) # calculate target values: yt = rt + gamma * q_target(s_t+1, mu_target(s_t+1)); with t = time step y = rewards + 0.99 * next_critic_value * (1 - buff_done) # calculate the loss between critic and target_critic critic1_loss = keras.losses.MSE(y, critic_value1) critic2_loss = keras.losses.MSE(y, critic_value2) # update critics by minimized the loss (critic_loss) and using Adam optimizer critic1_network_gradient = tape.gradient( critic1_loss, q1.trainable_variables) critic2_network_gradient = tape.gradient( critic2_loss, q2.trainable_variables) q1.optimizer.apply_gradients( zip(critic1_network_gradient, q1.trainable_variables)) q2.optimizer.apply_gradients( zip(critic2_network_gradient, q2.trainable_variables)) learn_count += 1 # train actor if learn_count % update_frequency == 0: with tf.GradientTape() as tape: new_policy_actions = mu(states) # check if - or + (descent or ascent) not sure yet actor_loss = -q1(states, new_policy_actions) actor_loss = tf.math.reduce_mean(actor_loss) # update the actor policy using the sampled policy gradient actor_network_gradient = tape.gradient( actor_loss, mu.trainable_variables) mu.optimizer.apply_gradients( zip(actor_network_gradient, mu.trainable_variables)) # update the target networks update_network_parameters(q1, q1_target, q2, q2_target, mu, mu_target, 0.005) time_step_reward.append(reward) avg_time_step_reward_short = np.mean(time_step_reward[-50:]) avg_time_step_reward.append(avg_time_step_reward_short) if done: performance.append(score) avg_reward = np.mean(performance[-50:]) avg_return.append(avg_reward) cumulus_steps += i print( "episode: {}/{}, score: {}, avg_score: {}, ep_steps: {}, cumulus_steps: {}" .format(e, episode, score, avg_reward, i, cumulus_steps)) if 10000 < cumulus_steps < 11000 and a_c == 0: a_c = 1 if not os.path.exists( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)): os.mkdir( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)) mu.save_weights( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/mu{}.h5" .format(reward_name, cumulus_steps)) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_return{}" .format(reward_name, cumulus_steps), avg_return) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/time_step_reward{}" .format(reward_name, cumulus_steps), time_step_reward) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/performance{}" .format(reward_name, cumulus_steps), performance) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_time_step_reward{}" .format(reward_name, cumulus_steps), avg_time_step_reward) if 150000 < cumulus_steps < 151000 and b_c == 0: b_c = 1 if not os.path.exists( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)): os.mkdir( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)) mu.save_weights( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/mu{}.h5" .format(reward_name, cumulus_steps)) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_return{}" .format(reward_name, cumulus_steps), avg_return) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/time_step_reward{}" .format(reward_name, cumulus_steps), time_step_reward) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/performance{}" .format(reward_name, cumulus_steps), performance) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_time_step_reward{}" .format(reward_name, cumulus_steps), avg_time_step_reward) # if 350000 < cumulus_steps < 351000 and c_c == 0: # c_c = 1 # if not os.path.exists("/home/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}".format(reward_name)): # os.mkdir("/home/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}".format(reward_name)) # mu.save_weights("/home/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/mu{}.h5".format(reward_name, cumulus_steps)) # np.save("/home/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_return{}".format(reward_name, cumulus_steps), avg_return) # np.save("/home/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/time_step_reward{}".format(reward_name, cumulus_steps), time_step_reward) # np.save("/home/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/performance{}".format(reward_name, cumulus_steps), performance) # np.save("/home/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_time_step_reward{}".format(reward_name, cumulus_steps), avg_time_step_reward) if 550000 < cumulus_steps < 551000 and d_c == 0: d_c = 1 if not os.path.exists( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)): os.mkdir( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)) mu.save_weights( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/mu{}.h5" .format(reward_name, cumulus_steps)) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_return{}" .format(reward_name, cumulus_steps), avg_return) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/time_step_reward{}" .format(reward_name, cumulus_steps), time_step_reward) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/performance{}" .format(reward_name, cumulus_steps), performance) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_time_step_reward{}" .format(reward_name, cumulus_steps), avg_time_step_reward) if 750000 < cumulus_steps < 751000 and e_c == 0: e_c = 1 if not os.path.exists( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)): os.mkdir( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}" .format(reward_name)) mu.save_weights( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/mu{}.h5" .format(reward_name, cumulus_steps)) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_return{}" .format(reward_name, cumulus_steps), avg_return) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/time_step_reward{}" .format(reward_name, cumulus_steps), time_step_reward) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/performance{}" .format(reward_name, cumulus_steps), performance) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_time_step_reward{}" .format(reward_name, cumulus_steps), avg_time_step_reward) if 1000000 < cumulus_steps < 1001000 and f_c == 0: f_c = 1 mu.save_weights( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/mu{}.h5" .format(reward_name, cumulus_steps)) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_return{}" .format(reward_name, cumulus_steps), avg_return) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/time_step_reward{}" .format(reward_name, cumulus_steps), time_step_reward) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/performance{}" .format(reward_name, cumulus_steps), performance) np.save( "/var/tmp/ga53cov/Bachelor_Arbeit/BA/Models/Ant_v2/{}/avg_time_step_reward{}" .format(reward_name, cumulus_steps), avg_time_step_reward) break score += reward state = tf.convert_to_tensor([next_state], dtype=tf.float32) # stop learning after certain time steps if cumulus_steps > breaking_step: break return avg_return, mu, performance, time_step_reward, avg_time_step_reward
import tensorflow as tf import numpy as np inputs = tf.convert_to_tensor(np.random.rand(32, 32), dtype=tf.float32) test = tf.keras.activations.softmax(inputs) ndtest = tf.make_ndarray(tf.make_tensor_proto(test)) print(ndtest)
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse: _NAME = 'image' event_id = context.invocation_id logging.info( f"Python humanpose function start process.\nID:{event_id}\nback server host:{_HOST}:{_PORT}" ) method = req.method url = req.url header = req.headers if method != 'POST': logging.warning( f'ID:{event_id},the method was {files.content_type}.refused.') return func.HttpResponse(f'only accept POST method', status_code=400) try: files = req.files[_NAME] if files: if files.content_type != 'image/jpeg': logging.warning( f'ID:{event_id},the file type was {files.content_type}.refused.' ) return func.HttpResponse(f'only accept jpeg images', status_code=400) # pre processing img_bin = files.read() # get image_bin form request img = prep.to_pil_image(img_bin) img_cv_copied = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) img = prep.resize(img) # w,h = 456,256 img_np = np.array(img) img_np = prep.transpose(img_np) # hwc > bchw [1,3,256,456] # print(img_np.shape) request = predict_pb2.PredictRequest() request.model_spec.name = 'human-pose-estimation' request.inputs["data"].CopyFrom( make_tensor_proto(img_np, shape=img_np.shape)) # send to infer model by grpc start = time() channel = grpc.insecure_channel("{}:{}".format(_HOST, _PORT)) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) result = stub.Predict(request, 10.0) pafs = make_ndarray(result.outputs["Mconv7_stage2_L1"])[0] heatmaps = make_ndarray(result.outputs["Mconv7_stage2_L2"])[0] # logging.info(f"PAF{pafs.shape},\nheatmap{heatmaps.shape}") timecost = time() - start logging.info(f"Inference complete,Takes{timecost}") # post processing c = posp.estimate_pose(heatmaps, pafs) response_image = posp.draw_to_image(img_cv_copied, c) cv2.imshow('test', response_image) cv2.waitKey(0) cv2.destroyAllWindows() imgbytes = cv2.imencode('.jpg', response_image)[1].tobytes() MIMETYPE = 'image/jpeg' return func.HttpResponse(body=imgbytes, status_code=200, mimetype=MIMETYPE, charset='utf-8') else: logging.warning(f'ID:{event_id},Failed to get image,down.') return func.HttpResponse(f'no image files', status_code=400) except grpc.RpcError as e: status_code = e.code() if "DEADLINE_EXCEEDED" in status_code.name: logging.error(e) return func.HttpResponse(f'the grpc request timeout', status_code=408) else: logging.error(f"grpcError:{e}") return func.HttpResponse(f'Failed to get grpcResponse', status_code=500) except Exception as e: logging.error(f"Error:{e}\n\ url:{url}\n\ method:{method}\n") return func.HttpResponse(f'Service Error.check the log.', status_code=500)
seq.append(pred[0][len(pred[0]) - 1]) tmpSeq = [] if len(seq) < 30: seq_data = tf.data.Dataset.from_tensor_slices(np.array(seq)).batch( len(seq)).batch(1) else: for i in range(seqIndex, seqIndex + 30): tmpSeq.append(seq[seqIndex]) seqIndex += 1 seq_data = tf.data.Dataset.from_tensor_slices( np.array(tmpSeq)).batch(len(tmpSeq)).batch(1) outArray = [] seq_data = tf.data.Dataset.from_tensor_slices(np.array(seq)).batch( len(seq)).batch(1) for input in seq_data.take(1): outArray = tf.make_ndarray(tf.make_tensor_proto(input)) outArray = outArray[0] x = [] y = [] xt = [] yt = [] for point in outArray: x.append(point[0]) y.append(point[1]) for point in dataset_raw[randomSample]: xt.append(point[0]) yt.append(point[1]) plt.plot(x, y, 'o') #plt.plot(xt, yt, 'ro') plt.axis([-1, 1, -1, 1]) plt.show()
def tensor(self, name) -> List[Event]: name = name.replace('.', '/') events = self.event_acc.Tensors(name) return [Event(e.step, tf.make_ndarray(e.tensor_proto)) for e in events]
cnt=0 with open('/home/carlton/Documents/warranty_500.csv',encoding="latin1") as csvfile: readCSV = csv.reader(csvfile, delimiter=',' ) next(readCSV, None) i=1 # skip the headers for row in readCSV: #print(row[0], row[5]) doc_id = i; i=i+1 outline = row[0]; cause = row[1] measure = row[2] data=outline+cause+measure outline_vec = tf.make_ndarray(tf.make_tensor_proto(embed([data]))).tolist()[0] cause_vec = tf.make_ndarray(tf.make_tensor_proto(embed([cause]))).tolist()[0] measure_vec = tf.make_ndarray(tf.make_tensor_proto(embed([measure]))).tolist()[0] b = {"outline":outline, "outline_vector":outline_vec, "cause":cause, "cause_vector":cause_vec, "measure":measure, "measure_vector":measure_vec, } res = es.index(index="warranty-index", id=doc_id, body=b)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--server_address', default='localhost:8500', help='tensorflow-model-server-neuron grpc address') parser.add_argument('--model_name', default='default', help='Serving model name') parser.add_argument('--val2017', required=True, help='Path to COCO 2017 validation dataset') parser.add_argument('--instances_val2017_json', required=True, help='Json file that contains labeling information') parser.add_argument('--num_threads', type=int, default=4, help='Number of threads') parser.add_argument('--throughput_interval', type=int, default=10, help='Interval for counting throughput') parser.add_argument('--save_results', default=None) args = parser.parse_args() if not args.disable_version_check: try: tfsn_info = subprocess.check_output(['apt', 'list', 'tensorflow-model-server-neuron']) start_marker = b',now ' start = tfsn_info.find(start_marker) end = tfsn_info.find(b' all') tfn_version = tfsn_info[start+len(start_marker):end].decode() except FileNotFoundError: tfsn_info = subprocess.check_output(['yum', 'info', 'tensorflow-model-server-neuron']) for line in tfsn_info.split(b'\n'): if line.startswith(b'Version'): start_marker = b': ' start = line.find(start_marker) tfn_version = line[start+len(start_marker):].decode() break if LooseVersion(tfn_version) < LooseVersion('1.15.0.1.0.1333.0'): raise RuntimeError( 'tensorflow-model-server-neuron version {} is too low for this demo. Please upgrade ' 'by "sudo apt-get install tensorflow-model-server-neuron"'.format(tfn_version)) channel = grpc.insecure_channel(args.server_address) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) val_dataset = get_val_dataset(args.instances_val2017_json, args.val2017) inv_map = {v: k for k, v in val_dataset.label_map.items()} request_list = [] for img_id in val_dataset.img_keys: img_path = os.path.join(args.val2017, val_dataset.images[img_id][0]) with open(img_path, 'rb') as f: img_jpg_bytes = f.read() data = np.array([img_jpg_bytes], dtype=object) data = tf.contrib.util.make_tensor_proto(data, shape=data.shape) request = predict_pb2.PredictRequest() request.model_spec.name = args.model_name request.inputs['batch_image'].CopyFrom(data) request_list.append(request) latency_list = [] throughput_list = [] def predict(request): start = time.time() result = stub.Predict(request).outputs latency_list.append(time.time() - start) return result def performance(): last_num_infer = len(latency_list) while len(latency_list) < len(request_list): current_num_infer = len(latency_list) throughput = (current_num_infer - last_num_infer) / args.throughput_interval throughput_list.append(throughput) p50 = 0.0 p90 = 0.0 if latency_list: p50 = np.percentile(latency_list, 50) p90 = np.percentile(latency_list, 90) print('pid {}: current throughput {}, latency p50={:.3f} p90={:.3f}'.format(os.getpid(), throughput, p50, p90)) last_num_infer = current_num_infer time.sleep(args.throughput_interval) executor = futures.ThreadPoolExecutor(max_workers=args.num_threads+1) performance_future = executor.submit(performance) eval_futures = [] for idx, request in enumerate(request_list): eval_fut = executor.submit(predict, request) eval_futures.append(eval_fut) waited_results = [] for idx, eval_fut in enumerate(eval_futures): if idx % 100 == 0: print('evaluating image {}/{}'.format(idx, len(eval_futures))) waited_results.append(eval_fut.result()) eval_results = [] for idx, (img_id, results) in enumerate(zip(val_dataset.img_keys, waited_results)): results = {key: tf.make_ndarray(value) for key, value in results.items()} boxes = results['boxes'] for box, label, prob in zip(results['boxes'][0], results['classes'][0], results['scores'][0]): res = [img_id, box[0], box[1], box[2], box[3], prob, inv_map[label+1]] # +1 to account for background eval_results.append(res) performance_future.result() coco_gt = COCO(annotation_file=args.instances_val2017_json) coco_dt = coco_gt.loadRes(np.array(eval_results).astype(np.float32)) coco_eval = COCOeval(coco_gt, coco_dt, iouType='bbox') coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize() if args.save_results is not None: np.save(args.save_results, coco_eval.stats)
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4") # CONSTANTS NUM_QUESTIONS_INDEXED = 200000 # Col-Names: Id,OwnerUserId,CreationDate,ClosedDate,Score,Title,Body cnt = 0 with open('./data/Questions.csv', encoding="latin1") as csvfile: readCSV = csv.reader(csvfile, delimiter=',') next(readCSV, None) # skip the headers for row in readCSV: # print(row[0], row[5]) doc_id = row[0] title = row[5] vec = tf.make_ndarray(tf.make_tensor_proto(embed([title]))).tolist()[0] b = { "title": title, "title_vector": vec, } # print(json.dumps(tmp,indent=4)) res = es.index(index="questions-index", id=doc_id, body=b) # print(res) # keep count of # rows processed cnt += 1 if cnt % 100 == 0: print(cnt)
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse: _NAME = 'image' event_id = context.invocation_id logging.info( f"Python human segmentation function start process.\nID:{event_id}\nBack-end server host: {_HOST}:{_PORT}" ) try: method = req.method url = req.url files = req.files[_NAME] if method != 'POST': logging.warning( f'ID:{event_id},the method was {files.content_type}.refused.') return func.HttpResponse(f'only accept POST method', status_code=400) if files: if files.content_type != 'image/jpeg': logging.warning( f'ID:{event_id},the file type was {files.content_type}.refused.' ) return func.HttpResponse(f'only accept jpeg images', status_code=400) # pre processing # get image_bin form request img_bin = files.read() img = prep.to_pil_image(img_bin) # rotate image with orientation value(for iOS, iPadOS) img = prep.rotate_image(img) # get width and height value of img w, h = img.size # resize image to [2048, 2048] img_np = prep.resize(img, w=2048, h=1024) img_np = np.array(img_np) img_np = img_np.astype(np.float32) # hwc > bchw [1,3,2048,2048] img_np = prep.transpose(img_np) # semantic segmentation request = predict_pb2.PredictRequest() request.model_spec.name = 'semantic-segmentation-adas' request.inputs["data"].CopyFrom(make_tensor_proto(img_np)) # send to infer model by grpc start = time() options = [('grpc.max_receive_message_length', 8388653)] channel = grpc.insecure_channel("{}:{}".format(_HOST, _PORT), options=options) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) result = stub.Predict(request, timeout=10.0) # logging.warning(f'Output:{result}') logging.warning(f'OutputType:{type(result)}') output = make_ndarray(result.outputs['4656.1']) #----------------------------------------------------------- # img's gray scale image gray = img.convert('L') # human segmentation mask image mask = postp.segmentation(output, w, h) # masking 'gray' and 'mask' images image = Image.composite(gray, img, mask) image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) #----------------------------------------------------------- timecost = time() - start logging.info(f"Inference complete,Takes{timecost}") imgbytes = cv2.imencode('.jpg', image)[1].tobytes() # imgbytes = prep.encode(image) MIMETYPE = 'image/jpeg' return func.HttpResponse(body=imgbytes, status_code=200, mimetype=MIMETYPE, charset='utf-8') else: logging.warning(f'ID:{event_id},Failed to get image,down.') return func.HttpResponse(f'no image files', status_code=400) except grpc.RpcError as e: status_code = e.code() if "DEADLINE_EXCEEDED" in status_code.name: logging.error(e) return func.HttpResponse(f'the grpc request timeout', status_code=408) else: logging.error(f"grpcError:{e}") return func.HttpResponse(f'Failed to get grpcResponse', status_code=500) except Exception as e: logging.error(f"Error:{e}\n\ url:{url}\n\ method:{method}\n") return func.HttpResponse(f'Service Error.check the log.', status_code=500)
def predict_from_tf_serving(self, line): from grpc.beta import implementations from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2_grpc # def pretty_print(line, preds): # words = line.strip().split() # lengths = [max(len(w), len(p)) for w, p in zip(words, preds)] # padded_words = [w + (l - len(w)) * ' ' for w, l in zip(words, lengths)] # padded_preds = [p.decode() + (l - len(p)) * ' ' for p, l in zip(preds, lengths)] # # print('words: {}'.format(' '.join(padded_words))) # # print('preds: {}'.format(' '.join(padded_preds))) # # res1 = 'words: {}'.format(' '.join(padded_words)) # # res2 = 'preds: {}'.format(' '.join(padded_preds)) # res1 = ' '.join(padded_words) # res2 = ' '.join(padded_preds) # return res1, res2 # # def predict_input_fn(line): # # Words # words = [w.encode() for w in line.strip().split()] # nwords = len(words) # # # Wrapping in Tensors # # words = tf.constant([words], dtype=tf.string) # # nwords = tf.constant([nwords], dtype=tf.int32) # # # return (words, nwords), None # return words, nwords def pretty_print(line, preds): line = repr(line).replace('\\', '/')[1:-1] line = list(line.replace(' ', '')) words = [w for w in line] lengths = [max(len(w), len(p)) for w, p in zip(words, preds)] padded_words = [w + (l - len(w)) * ' ' for w, l in zip(words, lengths)] padded_preds = [p + (l - len(p)) * ' ' for p, l in zip(preds, lengths)] print('words: {}'.format('\t'.join(padded_words))) print('preds: {}'.format('\t'.join(padded_preds))) def predict_input_fn(line): # Words # line = ''.join([' ' + c + ' ' if len(c.encode()) > 1 else c for c in line]).split() line = repr(line).replace('\\', '/')[1:-1] line = list(line.replace(' ', '')) words = [w.encode() for w in line] nwords = len(words) # Wrapping in Tensors # words = tf.constant(words, shape=(1, nwords), dtype = tf.string) # nwords = tf.constant([nwords], shape=(1,), dtype=tf.int32) # return (words, nwords), None return words, nwords def build_sentence(data, tags): res = '' sub_res = [] for word, tag in zip(data.strip().split(), tags.strip().split()): if tag == 'B': if sub_res: sub_str = ''.join(sub_res) res = res + sub_str + ' ' # idx = i sub_res = [] else: # idx = i sub_res.append(word) elif tag == 'S': # res[i] = word # ' '.join(res, word) res = res + word + ' ' # idx = -1 elif tag == 'M': sub_res.append(word) # idx = -1 elif tag == 'E': if len(sub_res) > 0: sub_res.append(word) sub_str = ''.join(sub_res) res = res + sub_str + ' ' sub_res = [] else: # res[idx] = word res = res + word + ' ' return res.strip() # @timecost # def predict(testStr): host = '43.247.185.201' # port='8031' port = '8036' # port='30000' channel = implementations.insecure_channel(host, int(port)) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel._channel) request = predict_pb2.PredictRequest() request.model_spec.name = "saved_model" words, nwords = predict_input_fn(line) request.inputs['words'].CopyFrom( tf.contrib.util.make_tensor_proto(words, shape=(1, nwords), dtype=tf.string)) # tf.contrib.util.make_tensor_proto(words, shape=words.shape, dtype=tf.string)) request.inputs['nwords'].CopyFrom( tf.contrib.util.make_tensor_proto([nwords], shape=(1,), dtype=tf.int32)) future = stub.Predict.future(request, 10.0) result = future.result() result_list = tf.make_ndarray(result.outputs["pred_ids"]).tolist() tags_id = [self.tags_from_table[id] for id in result_list[0]] pretty_print(line, tags_id)
def main(): # 1. Load image # For code run on server, this should be image send from front end img = Image.open( './test_input_img1.jpg') # TODO change this to your imaeg read in # 2. Image Preprocess # 2.1 Resize # 2.2 Convert to numpy float 32 (1, H, W, 3) representation # For code run on server # you may need to find a way to resize image # numpy image representation is MANDATORY, this is due to the input formate of tf.make_tensor_proto def img_resize(img): ''' Input: img (PIL.Image) : <PIL.Image> class objecy represent image ''' h, w = img.size IMG_LONG_SIZE = 500. if h > w: # h is the long side h_new = int(IMG_LONG_SIZE) w_new = int(IMG_LONG_SIZE * (w * (1.0)) / (h * (1.0))) else: # w is the long side w_new = int(IMG_LONG_SIZE) h_new = int(IMG_LONG_SIZE * (h * (1.0)) / (w * (1.0))) img = img.resize((h_new, w_new), resample=Image.BILINEAR) return img img = img_resize(img) img = np.expand_dims(np.array(img).astype(np.float32), axis=0) # float32, (1, h, w, 3) representaiton # 3. Prepare & Send Request #ip_port = "0.0.0.0:8500" # TODO change this to your ip:port ip_port = "35.232.203.191:8500" # if you run docker run -t -p 0000:8500 -p 0001:8501 xiaosong99/servable:latest-skeleton # then the port should be "0000" # For more information, see `QuickStart_GeneralModel.md` print('prepare to send request') start_time = time.time() channel = grpc.insecure_channel(ip_port) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) request = predict_pb2.PredictRequest() request.model_spec.name = "van-gogh" # TODO change this to the model you're using request.model_spec.signature_name = "predict_images" request.inputs["input_img"].CopyFrom( tf.make_tensor_proto(img, shape=list(img.shape))) response = stub.Predict( request, 100.0) # TODO change the request timeout, default is 10s # 4. Image Postprocess output_img = tf.make_ndarray( response.outputs['output_img']) # numpy array (1, H, W, 3) print('process using {}s'.format(time.time() - start_time)) def post_process(img): ''' Input: img (np.array) : value range : [-1, 1], dtype : float32 Return: img (np.array) : value range : [0, 255], dtype : uint8 # TODO change the return image having the same image range as the image you received ''' img = (img + 1.) * 127.5 img = img.astype(np.uint8) return img output_img = post_process(output_img) # 5. Save Image / Send image back to frontend output_img_pil = Image.fromarray(output_img[0]) output_img_pil.save('test_output_{}.jpg'.format(request.model_spec.name))
for epoch in tf.range(1, args.epochs + 1): ### Train ### epoch_preds, epoch_trues = [], [] with tqdm(total=len(list(train_batches))) as progress_bar: for batch_seq, batch_tag in train_batches: preds, text_lens = train_fn(model, optimizer, train_loss, batch_seq, batch_tag) # [batch_size, seq_len] # Unpad preds/tags to the real lengths (for metrics) for pred, text_len in zip(preds, text_lens): # logit: [seq_len, num_tags] pred_cut = tf.make_ndarray( tf.make_tensor_proto( pred[:text_len])).tolist() # convert tensor to list epoch_preds.append(pred_cut) for tag, text_len in zip( batch_tag, text_lens): # batch_tag: [seq_len, num_tags] tag_cut = tf.make_ndarray(tf.make_tensor_proto( tag[:text_len])).tolist() # convert tensor to list epoch_trues.append(tag_cut) progress_bar.update(1) # Convert epoch_idxs to epoch_tags epoch_tag_preds = utils.epoch_idx2tag(epoch_preds, idx2tag) epoch_tag_trues = utils.epoch_idx2tag(epoch_trues, idx2tag) # Calculate metrics for whole epoch
self._model = tf.keras.models.load_model(model_path) @staticmethod def _create_conv2d_layer(num_filters, kernel_size, strides): return tf.keras.layers.Conv2D( filters=num_filters, strides=(strides, strides), kernel_size=kernel_size, activation=tf.nn.relu, kernel_initializer='random_normal', ) @staticmethod def _create_dense_layer(num_nodes, act_fn=tf.nn.relu): return tf.keras.layers.Dense(units=num_nodes, activation=act_fn) if __name__ == '__main__': # create new instance model = A2CModel() action, values = \ model.get_action_value(np.zeros((5, 8, 8, 1), dtype=np.float32), []) print('action:', action) print('action:', tf.make_ndarray(tf.make_tensor_proto(action))) print('values:', values) print('values:', tf.make_ndarray(tf.make_tensor_proto(values))) model.summary()
argparse.add_argument('--output_file_path', type=str, required=True) argparse.add_argument('-l', '--log_files', nargs='+', help='<Required> Set flag', required=True) argparse.add_argument('-r', '--plot_names', nargs='+', help='<Required> Set flag', required=True) args = argparse.parse_args() assert len(args.plot_names) == len( args.log_files ), "There should be an identiacal number of plot names and log files" log_dict = {} from tensorflow.core.util import event_pb2 for log_file, plot_name in zip(args.log_files, args.plot_names): serialized_examples = tf.data.TFRecordDataset(log_file) for serialized_example in serialized_examples: event = event_pb2.Event.FromString(serialized_example.numpy()) for value in event.summary.value: if plot_name not in log_dict: log_dict[plot_name] = [] t = tf.make_ndarray(value.tensor) log_dict[plot_name].append(t) process_dict(log_dict, args.output_file_path)
cur = conn.cursor() application_id = str(get_application_id(app_addr)) binary_data = reqstore.APIHelper.subsample(reqstore_addr, application_id) records = reqstore.BinaryHelper.decode_records(binary_data) random.shuffle(records) imgs, labels = list(), list() for timestamp in records[:int(len(records) * 1)]: for entry in timestamp.entries: cur.execute("SELECT * FROM requests WHERE timestamp=? AND uid=?", (timestamp.ts, entry.uid)) db_record = cur.fetchone() if not db_record: continue request_image = make_ndarray(entry.request.inputs["imgs"]).reshape( 28 * 28) imgs.append(request_image) labels.append(db_record[2]) imgs, labels = np.array(imgs), np.array(labels) train_imgs, train_labels = imgs[:int(len(imgs) * 0.75 )], labels[:int(len(labels) * 0.75)] test_imgs, test_labels = imgs[int(len(imgs) * 0.75):], labels[int(len(labels) * 0.75):] os.makedirs(data_path, exist_ok=True) print(f"New train subsample size: {len(train_imgs)}", flush=True) print(f"New test subsample size: {len(test_imgs)}", flush=True) np.savez_compressed(os.path.join(data_path, "subsample-train"), imgs=train_imgs,
def convert_graph_precision(source_graph_def, target_type='FP16'): def rewrite_batch_norm_node_v2(node, graph_def, target_type='FP16'): """ Rewrite FusedBatchNorm with FusedBatchNormV2 for reserve_space_1 and reserve_space_2 in FusedBatchNorm require float32 for gradient calculation (See here: https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/fused-batch-norm) """ if target_type == 'BFLOAT16': dtype = types_pb2.DT_BFLOAT16 elif target_type == 'FP16': dtype = types_pb2.DT_HALF elif target_type == 'FP64': dtype = types_pb2.DT_DOUBLE else: dtype = types_pb2.DT_FLOAT new_node = graph_def.node.add() new_node.op = 'FusedBatchNormV2' new_node.name = node.name new_node.input.extend(node.input) new_node.attr['U'].CopyFrom( attr_value_pb2.AttrValue(type=types_pb2.DT_FLOAT)) for attr in list(node.attr.keys()): if attr == 'T': node.attr[attr].type = dtype new_node.attr[attr].CopyFrom(node.attr[attr]) if target_type == 'BFLOAT16': dtype = types_pb2.DT_BFLOAT16 elif target_type == 'FP16': dtype = types_pb2.DT_HALF elif target_type == 'FP64': dtype = types_pb2.DT_DOUBLE else: dtype = types_pb2.DT_FLOAT for node in source_graph_def.node: if node.op == 'FusedBatchNorm': rewrite_batch_norm_node_v2(node, target_graph_def, target_type=target_type) continue if ('BatchNorm' in node.name) or ('batch_normalization' in node.name): continue attrs = list(node.attr.keys()) if node.op == 'convert_gradient_to_tensor_HBc3xYw22Mw': node.op = 'Identity' node.attr.setdefault('T') node.attr['T'].type = dtype del node.attr['_disable_call_shape_inference'] for attr in attrs: if node.attr[attr].type == types_pb2.DT_FLOAT: node.attr[attr].type = dtype if attr == 'value': tensor = node.attr[attr].tensor if tensor.dtype == types_pb2.DT_FLOAT: if tensor.float_val: float_val = tf.make_ndarray(node.attr[attr].tensor) node.attr[attr].tensor.CopyFrom( tf.compat.v1.make_tensor_proto(float_val, dtype=dtype)) continue if tensor.tensor_content: tensor_shape = [ x.size for x in tensor.tensor_shape.dim ] tensor_weights = tf.make_ndarray(tensor) tensor_weights = np.reshape(tensor_weights, tensor_shape) tensor_proto = tf.compat.v1.make_tensor_proto( tensor_weights, dtype=dtype) node.attr[attr].tensor.CopyFrom(tensor_proto) continue return source_graph_def
# TensorFlow serving stuff to send messages from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2_grpc from tensorflow.contrib.util import make_tensor_proto from os import listdir from os.path import isfile, join timeout = 60.0 channel = grpc.insecure_channel('localhost:8501') stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) input_data = np.array([[2061, 318, 428, 30]], dtype='int32') # Boiler-plate request = predict_pb2.PredictRequest() # Set request objects using the tf-serving `CopyFrom` setter method request.model_spec.name = '0' request.model_spec.signature_name = 'serving_default' # This is correct (default constant). request.inputs['input'].CopyFrom(make_tensor_proto(input_data, shape=input_data.shape)) # Boiler-Plate response = stub.Predict(request, timeout) result = response.outputs['output'] print(tf.make_ndarray(result))
def batch_embed_evaluate(input_path,output_path): print("input_path",input_path) docMap = {} classDocStrings = {} with open(input_path,'rb') as data: docStringObjects = ijson.items(data, 'item') for docString in docStringObjects: if 'klass' in docString: if 'class_docstring' in docString: if 'class_docstring' != None: classDocStrings[docString['klass']] = docString['class_docstring'] docMap = {} with open(input_path,'rb') as data: docStringObjects = ijson.items(data, 'item') for docString in docStringObjects: if docString['module'] != None: totalLabel = docString['module'] else: totalLabel = 'noModule' className = 'noClass' if 'klass' in docString: if docString['klass'] != None: className = docString['klass'] totalLabel = totalLabel + ' ' + className totalText = '' if className != 'noClass': totalText = totalText + className classDocString = '' if className in classDocStrings: totalText = totalText + ' ' + classDocStrings[className] docMap[totalLabel] = totalText docItems = [] for label, text in docMap.items(): for thing in text: if thing == None: print(label) docItems.append((label, text)) docStringCompiled= docItems docItems=[] docMap={} #@title Load the Universal Sentence Encoder's TF Hub module def embed(input): module_url = "https://tfhub.dev/google/universal-sentence-encoder/4" #@param ["https://tfhub.dev/google/universal-sentence-encoder/4", "https://tfhub.dev/google/universal-sentence-encoder-large/5"] model = hub.load(module_url) return model(input) def randomize(x, y): ##Randomizes the order of data samples and their corresponding labels import numpy as np permutation = np.random.permutation(len(y)) y=np.asarray(y) shuffled_x = x[permutation] shuffled_y = y[permutation] return shuffled_x, shuffled_y def get_next_batch(x, y, start, end): ##get the specified batch x_batch = x[start:end] y_batch = y[start:end] return x_batch, y_batch size_to_train=len(docStringCompiled) ##ive run for a max of 130000 till now batch_size=100 embeded_docnames=[] index = faiss.IndexFlatL2(512) shuffled_docnames,shuffled_docmessages=randomize(np.asarray([seq[0] for seq in docStringCompiled]),np.asarray([seq[1] for seq in docStringCompiled])) for i in range(int(size_to_train/batch_size)): print("iteration",i) # Reduce logging output. docStringsset1,docStringsset2=get_next_batch(shuffled_docnames,shuffled_docmessages,i*batch_size,(i+1)*batch_size) message_embeddings = embed(docStringsset2) message_embeddings=tf.make_tensor_proto(message_embeddings) message_embeddings=tf.make_ndarray(message_embeddings) embeded_docnames.append(docStringsset1) index.add(message_embeddings) pickle.dump(docItems,open('docString_embededIndices', 'wb')) print("embedding complete") # k=10 # embeded_distance_index_info=[] # embeded_distance_info=[] # for i in range(len(embeded_docmessages)): # # we want to see 6 nearest neighborS # D, I = index.search(embeded_docmessages[i], k) # embeded_distance_index_info.append(I) # embeded_distance_info.append(D) # sys.stdout = open(output_path, "w") # for i in range(len(embeded_docmessages)): # for j in range(len(embeded_docmessages[i])): # print("-------------------------------------------------------------") # print("document name : \n"+str(embeded_docnames[i][j])+"\n") # for k in range(len(embeded_distance_index_info[i][j])): # print("\n close to document :",shuffled_docnames[embeded_distance_index_info[i][j][k]]) # print("\n with a distance :",embeded_distance_info[i][j][k]) sys.stdout.close()
def compute_score(predictions, references, vocab=None, beam_size=4): """Computes the bleu score. Args: predictions: a numpy arrary in the shape of [batch_size, max_phrase_length] references: a numpy array in the shape of [batch_size, num_of_ref, ref_len] vocab: the vocabulary file. beam_size: the beam_size. Returns: a scalar value for the corpus level bleu score. """ assert np.rank(predictions) == 3 assert predictions.shape[0] == references.shape[0] batch_size = predictions.shape[0] predictions = tf.make_ndarray(tf.make_tensor_proto(predictions)).tolist() references = tf.make_ndarray(tf.make_tensor_proto(references)).tolist() hypotheses_list = [] references_list = [] for index in range(batch_size): # All predictions in beam search are used for validation scores. for b_index in range(beam_size): h = predictions[index][b_index] try: eos_index = h.index(input_utils.EOS) except ValueError: eos_index = len(h) hypotheses_list.append(h[:eos_index]) ref = references[index][0].decode().split('|') ref_list = [r.strip().split(' ') for r in ref if r.strip()] references_list.append(ref_list) all_scores = collections.defaultdict(list) for hypothesis, references in zip(hypotheses_list, references_list): if vocab is not None and len(vocab): # Skip PADDING, UNK, EOS, START (0-3). hypothesis = [ vocab[word_id].numpy().decode() for word_id in hypothesis if word_id > 3 ] h_str = ' '.join(str(e) for e in hypothesis) r_str = [' '.join(str(e) for e in ref) for ref in references] print('hypothesis: ', str(h_str)) print('references: ', str(r_str)) print('\n') scores = screen2words_eval.coco_evaluate(r_str, h_str) for key, score in scores.items(): all_scores[key].append(score) score_names = [ 'BLEU-1', 'BLEU-2', 'BLEU-3', 'BLEU-4', 'ROUGE-1-f1-mean', 'ROUGE-1-f1-min', 'ROUGE-1-f1-max', 'ROUGE-2-f1-mean', 'ROUGE-2-f1-min', 'ROUGE-2-f1-max', 'ROUGE-L-f1-mean', 'ROUGE-L-f1-min', 'ROUGE-L-f1-max' ] return [ np.array(all_scores[name], dtype=np.float32) for name in score_names ]
def aggregate(path): keys = [ # 'Train/Tot_Reward', # 'Eval/Length', # 'Eval/MCTS_Confidence', # 'Train/AvgLoss' # 'Eval/MCTS_Confidence' 'Train/Last_Fish_Population' ] agg_runs = defaultdict(lambda: defaultdict(list)) agg_keys = {} path = glob.glob(os.path.join(path, "events.out.tfevents.*")) if not path: return None, None path = path[0] data =[] for event in my_summary_iterator(path): if not event.summary.value: continue tag = event.summary.value[0].tag for key in keys: # if tag.startswith(key): # run = tag[tag.rfind('/')+1:] # val = tf.make_ndarray(event.summary.value[0].tensor) # agg_runs[key][run].append(val) if key == tag: val = float(tf.make_ndarray(event.summary.value[0].tensor)) # print(val) data.append(val) # data = smooth(data, 20)[10:-9] # import pickle # print(data) # print(len(data)) # with open('t800_5fish_last_fish_pop.pickle', 'wb+') as f: # pickle.dump(data, f) # import matplotlib.pyplot as plt # plt.plot(data) # plt.show() for key in agg_runs: aggregated = [] for run in agg_runs[key]: aggregated.append(agg_runs[key][run]) max_len = max(len(x) for x in aggregated) aggregated_ = [] for i, x in enumerate(aggregated): aggregated_.append( np.pad( x, (0, max_len - len(x)), mode='constant', constant_values=(0, x[-1]) ) ) agg_keys[key] = np.array(aggregated_) return agg_runs, agg_keys
def test_float_value(self): pb = self.scalar('a', 1.13) value = tf.make_ndarray(pb.value[0].tensor).item() self.assertEqual(float, type(value)) self.assertNear(1.13, value, 1e-6)
def extract_values(reader, tag): events = reader.Tensors('run', tag) steps = [event.step for event in events] times = [event.wall_time for event in events] values = [tf.make_ndarray(event.tensor_proto) for event in events] return steps, times, values
original.append(data) changed.append(p_data) writeCSV.writerow((doc_id, p_data)) #Preprocessing the corpus and storing in new file. savePreproccessedData() #loading the universal sentence encoder model use_embeddings = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4") embeddings = [] for c in tqdm_notebook(changed): ques_list = [c] sent_tfTensor = use_embeddings(ques_list) array_embeddings = tf.make_ndarray(tf.make_tensor_proto(sent_tfTensor)) list_embeddings = array_embeddings.tolist()[0] embeddings.append(np.array(list_embeddings)) embeddings = np.array(embeddings) joblib.dump(embeddings,"/content/drive/MyDrive/IR project/title_embeddings_full" ) #function to create the sentence embeddings and store in a csv file. def createEmbeddings(): with open('/content/drive/MyDrive/IR project/archive/finalOutput_soumam_testing_with_title_only.csv', encoding="latin1") as csvfile, open('/content/drive/MyDrive/IR project/sentenceEmbeddings_soumam_testing_with_title_only.csv' ,'w' ,newline='') as myfile: readCsv = csv.reader(csvfile, delimiter=',' ) writeCSV = csv.writer(myfile , delimiter = ',') #count =0 for row in tqdm_notebook(readCsv): '''
import functools import pandas as pd import tensorflow as tf from tensorboard.backend.event_processing.event_accumulator import ( DEFAULT_SIZE_GUIDANCE, STORE_EVERYTHING_SIZE_GUIDANCE, EventAccumulator) SCALAR_RESERVOIR = { "tag_category": "scalars", "event_list_fn": EventAccumulator.Scalars, "value_decoder": lambda x: x.value, } TENSOR_RESERVOIR = { "tag_category": "tensors", "event_list_fn": EventAccumulator.Tensors, "value_decoder": lambda x: tf.make_ndarray(x.tensor_proto), } def frames(*directories, **kwargs): frames = [dataframe(d, **kwargs) for d in directories] return [f for f in frames if not f.empty] def dataframe(path, block=[], reservoirs=[SCALAR_RESERVOIR, TENSOR_RESERVOIR], everything=False, metric=False): if everything: size_guidance = STORE_EVERYTHING_SIZE_GUIDANCE
def fill_tensor(row, summary): if summary.HasField('tensor'): import tensorflow as tf row.tensor = tf.make_ndarray(summary.tensor)
def get_config(self): config = super().get_config().copy() config.update({'v': tf.make_ndarray(self.v)}) return config
def plot(logging_dir, tag, xlabel, ylabel): from collections import defaultdict import glob import os import pandas as pd import tensorflow as tf import tqdm import seaborn as sns import matplotlib import matplotlib.pyplot as plt import json plt.rcParams['text.usetex'] = False matplotlib.rc('font', family='serif', serif='cm10') matplotlib.rc('mathtext', fontset='cm') color_palette = [ '#EE7733', '#0077BB', '#33BBEE', '#009988', '#CC3311', '#EE3377', '#BBBBBB', '#000000' ] palette = sns.color_palette(color_palette) sns.palplot(palette) sns.set_palette(palette) def pretty(s): return s.replace('_', ' ').title() # get the experiment folders dirs = glob.glob(os.path.join(logging_dir, "trial-*")) # get the hyper parameters for each experiment params = [] for d in dirs: with open(os.path.join(d, 'params.json'), 'r') as f: params.append(json.load(f)) # concatenate all params along axis 1 all_params = defaultdict(list) for p in params: for key, val in p.items(): if val not in all_params[key]: all_params[key].append(val) # locate the params of variation in this experiment params_of_variation = [] for key, val in all_params.items(): if len(val) > 1 and (not isinstance(val[0], dict) or 'seed' not in val[0]): params_of_variation.append(key) # get the task and algorithm name task_name = params[0]['eval_env'] if len(params_of_variation) == 0: params_of_variation.append('eval_env') # read data from tensor board data = pd.DataFrame(columns=[xlabel, ylabel] + params_of_variation) for d, p in tqdm.tqdm(zip(dirs, params)): for f in glob.glob(os.path.join(d, '*/events.out*')): for e in tf.compat.v1.train.summary_iterator(f): for v in e.summary.value: if v.tag == tag: row = { ylabel: tf.make_ndarray(v.tensor).tolist(), xlabel: e.step } for key in params_of_variation: row[key] = f'{pretty(key)} = {p[key]}' data = data.append(row, ignore_index=True) # save a separate plot for every hyper parameter for key in params_of_variation: plt.clf() g = sns.relplot(x=xlabel, y=ylabel, hue=key, data=data, kind="line", height=5, aspect=1.5, facet_kws={"legend_out": True}) g.set(title=f'{task_name}') plt.savefig(f'{task_name}_{key}_{tag.replace("/", "_")}.png', bbox_inches='tight')
request.model_spec.name = args.get('model_name') request.inputs[args['input_name']].CopyFrom( make_tensor_proto(img, shape=(img.shape))) start_time = datetime.datetime.now() result = stub.Predict( request, 10.0) # result includes a dictionary with all model outputs end_time = datetime.datetime.now() if args['output_name'] not in result.outputs: print("Invalid output name", args['output_name']) print("Available outputs:") for Y in result.outputs: print(Y) exit(1) duration = (end_time - start_time).total_seconds() * 1000 processing_times = np.append(processing_times, np.array([int(duration)])) output = make_ndarray(result.outputs[args['output_name']]) nu = np.array(output) # for object classification models show imagenet class print( 'Processing time: {:.2f} ms; speed {:.2f} fps'.format( round(duration), 2), round(1000 / duration, 2)) ma = np.argmax(nu) if int(label) == ma: matched += 1 i += 1 print("Detected:", ma, " Should be:", label) latency = np.average(processing_times) accuracy = matched / i print("Overall accuracy=", accuracy * 100, "%")
import tensorflow as tf import tensorflow_hub as hub embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4") embeddings = tf.make_ndarray( tf.make_tensor_proto( embed(["The quick brown fox jumps over the lazy dog."]))).tolist()[0] print(type(embeddings)) print(len(embeddings)) print(embeddings)
def test(mu_render, e, train_bool, weight_string): env = gym.make('AntPyBulletEnv-v0') if not train_bool: mu_render.load_weights(weight_string) final_x_list = [] forward_return_list = [] electricity_list = [] joint_lim_list = [] z_pos_list = [] cumulus_steps_list = [] for i in range(e): done = 0 ep_reward = 0 step = 0 env.render() observation = env.reset() state = tf.convert_to_tensor([observation], dtype=tf.float32) final_x_pos = 0 episode_electricity_costs = 0 episode_jointlim_costs = 0 episode_alive = 0 z_ep_list = [] while not done: z_ep_list = [] action = mu_render(state) proto_tensor = tf.make_tensor_proto(action) action = tf.make_ndarray(proto_tensor) action = action[0] # action[2] = 0 # action[3] = 0 # print(action) next_state, reward, done, _ = env.step(action) # print(next_state[24], next_state[27], next_state[25], next_state[26]) reward_list = env.env.rewards reward = reward_list[1] # print(next_state[6], next_state[7]) z_pos = env.env.robot.body_xyz[2] state = tf.convert_to_tensor([next_state], dtype=tf.float32) ep_reward += reward step += 1 final_x_pos = env.env.robot.body_xyz[0] episode_electricity_costs += reward_list[2] episode_jointlim_costs += reward_list[3] episode_alive += reward_list[0] z_ep_list.append(z_pos) print("Final x position: {}".format(final_x_pos)) final_x_list.append(final_x_pos) print("Episode forward return: {}".format(ep_reward)) forward_return_list.append(ep_reward) print("Electricity costs: {}".format(episode_electricity_costs)) electricity_list.append(episode_electricity_costs) print("Joints at limits costs: {}".format(episode_jointlim_costs)) joint_lim_list.append(episode_jointlim_costs) print("Average z position: {}".format(np.mean(z_ep_list))) z_pos_list.append(np.mean(z_ep_list)) print("cumulus steps: {}".format(step)) cumulus_steps_list.append(step) print("Mean final x position: {}".format(np.mean(final_x_list))) print("Mean episode forward return: {}".format( np.mean(forward_return_list))) print("Mean episode electricity costs: {}".format( np.mean(electricity_list))) print("Mean joint limit costs: {}".format(np.mean(joint_lim_list))) print("Mean episodes mean z pos: {}".format(np.mean(z_pos_list))) print("Mean cumulus steps: {}".format(np.mean(cumulus_steps_list)))