Skip to content

Add hparams and job status support to SummaryWriter #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tensorboard-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ tensorboard-proto = { version = "0.5.7" }

gethostname = "0.2.1"

image = "0.23.4"
image = "0.25.6"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
hex = "0.4.3"
sha2 = "0.10"

[dev-dependencies]
#protobuf-codegen = "2.14"
Expand Down
10 changes: 5 additions & 5 deletions tensorboard-rs/examples/draw_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use tensorboard_rs::summary_writer::SummaryWriter;
//use tensorboard_proto::event::{Event, TaggedRunMetadata};
//use tensorboard_proto::summary::{Summary};
//use tensorboard_proto::graph::{GraphDef, };
use tensorboard_proto::node_def::{NodeDef, };
use tensorboard_proto::node_def::NodeDef;
//use tensorboard_proto::versions::{VersionDef, };
use tensorboard_proto::attr_value::{AttrValue, };
use tensorboard_proto::attr_value::AttrValue;
//use tensorboard_proto::tensor_shape::{TensorShapeProto, };
//use tensorboard_proto::step_stats::{RunMetadata, };
use protobuf::RepeatedField;
Expand All @@ -16,17 +16,17 @@ pub fn main() {
let mut node1 = NodeDef::new();
node1.set_name("node1".to_string());
node1.set_op("op1".to_string());

let inputs = RepeatedField::from(vec![]);
node1.set_input(inputs);

let mut attrs = HashMap::new();
let mut v1 = AttrValue::new();
v1.set_i(16);
attrs.insert("attr1".to_string(), v1);
node1.set_attr(attrs);

writer.add_graph(&[node1]);

writer.flush();
}
66 changes: 44 additions & 22 deletions tensorboard-rs/examples/draw_histo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,61 @@ use tensorboard_rs::summary_writer::SummaryWriter;
//use image::{open, };

pub fn main() {

let mut writer = SummaryWriter::new(&("./logdir".to_string()));

let min = 1.001;
let max = 29.001;
let num = 435.;
let sum = 8555.435;
let sum_squares = 189242.110435;
let bucket_limits = [3.8009999999999997, 6.600999999999999, 9.400999999999998, 12.200999999999999, 15.001, 17.801, 20.601, 23.401, 26.201, 29.001];
let bucket_counts = [ 6., 15., 24., 33., 27., 48., 57., 66., 75., 84.];

writer.add_histogram_raw("run1/histo1",
min, max,
num,
sum, sum_squares,
&bucket_limits, &bucket_counts,
1
let bucket_limits = [
3.8009999999999997,
6.600999999999999,
9.400999999999998,
12.200999999999999,
15.001,
17.801,
20.601,
23.401,
26.201,
29.001,
];
let bucket_counts = [6., 15., 24., 33., 27., 48., 57., 66., 75., 84.];

writer.add_histogram_raw(
"run1/histo1",
min,
max,
num,
sum,
sum_squares,
&bucket_limits,
&bucket_counts,
1,
);

writer.add_histogram_raw("run1/histo1",
min, max,
num,
sum, sum_squares,
&bucket_limits, &bucket_counts,
2
writer.add_histogram_raw(
"run1/histo1",
min,
max,
num,
sum,
sum_squares,
&bucket_limits,
&bucket_counts,
2,
);

writer.add_histogram_raw("run1/histo1",
min, max,
num,
sum, sum_squares,
&bucket_limits, &bucket_counts,
3
writer.add_histogram_raw(
"run1/histo1",
min,
max,
num,
sum,
sum_squares,
&bucket_limits,
&bucket_counts,
3,
);
writer.flush();
}
43 changes: 43 additions & 0 deletions tensorboard-rs/examples/draw_hparams.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::collections::HashMap;
use tensorboard_rs::hparams::{GenericValue, HyperParameter, Metric};
use tensorboard_rs::status::JobStatus;
use tensorboard_rs::summary_writer::SummaryWriter;

pub fn main() {
for i in 0..10 {
let path = format!("./logdir/test-{:}", i);
let mut writer = SummaryWriter::new(path);
let options = ["A", "B", "C", "D"];
let hparams = vec![
HyperParameter::new("M1"),
HyperParameter::with_string("M2", "A Metric"),
HyperParameter::with_bool("M3", true),
HyperParameter::with_f64s("M4", &[1f64, 2f64]),
HyperParameter::with_strings("M5", &options),
];
let metrics = vec![Metric::new("Test Metric")];
writer.add_hparams_config(&hparams, &metrics);

let mut data = HashMap::new();
data.insert("M1".to_string(), GenericValue::Number(i as f64));
data.insert(
"M2".to_string(),
GenericValue::String("A Metric".to_string()),
);
data.insert("M3".to_string(), GenericValue::Bool(true));
data.insert("M4".to_string(), GenericValue::Number((i % 2) as f64));
data.insert(
"M5".to_string(),
GenericValue::String(options[i % 4].to_string()),
);
writer.add_hparams(data, Some(format!("test-{:}", i).to_string()), Some(0));

writer.add_scalar("Test Metric", 0f32, 0);
writer.add_scalar("Test Metric", 1f32, 1);
writer.add_scalar("Test Metric", 2f32, 2);

writer.add_job_status(&JobStatus::Success, None);

writer.flush();
}
}
11 changes: 7 additions & 4 deletions tensorboard-rs/examples/draw_image.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use image::open;
use tensorboard_rs::summary_writer::SummaryWriter;
use image::{open, };

pub fn main() {

let mut writer = SummaryWriter::new(&("./logdir".to_string()));

let stop_image = "./examples/stop.jpg";
let img = open(stop_image).expect("");
let img = img.into_rgb8();
let (width, height) = img.dimensions();


writer.add_image(&"test_image".to_string(), &img.into_raw()[..], &vec![3, width as usize, height as usize][..], 12);
writer.add_image(
&"test_image".to_string(),
&img.into_raw()[..],
&vec![3, width as usize, height as usize][..],
12,
);
writer.flush();
}
6 changes: 3 additions & 3 deletions tensorboard-rs/examples/draw_scalar.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use tensorboard_rs::summary_writer::SummaryWriter;
use std::collections::HashMap;
use tensorboard_rs::summary_writer::SummaryWriter;

pub fn main() {
let mut writer = SummaryWriter::new(&("./logdir".to_string()));

let name = "run1";
let mut scalar = 2.3;
let mut step = 12;
let mut step = 12;
for i in 0..2 {
println!("{}", i);
scalar += (i as f32)*0.1;
scalar += (i as f32) * 0.1;
step += i;

writer.add_scalar(name, scalar, step);
Expand Down
41 changes: 24 additions & 17 deletions tensorboard-rs/src/event_file_writer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::path::{PathBuf, Path};
use std::fs;
use std::time::SystemTime;
use gethostname::gethostname;
use std::process::id;
use std::fs::File;
use protobuf::Message;
use std::thread::{spawn, JoinHandle};
use std::fs;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::id;
use std::sync::mpsc::{channel, Sender};
use std::thread::{spawn, JoinHandle};
use std::time::SystemTime;

use tensorboard_proto::event::Event;
use crate::record_writer::RecordWriter;
use tensorboard_proto::event::Event;

enum EventSignal {
Data(Vec<u8>),
Expand Down Expand Up @@ -37,8 +37,11 @@ impl EventFileWriter {
}
let hostname = gethostname().into_string().expect("");
let pid = id();

let file_name = format!("events.out.tfevents.{:010}.{}.{}.{}", time, hostname, pid, 0);

let file_name = format!(
"events.out.tfevents.{:010}.{}.{}.{}",
time, hostname, pid, 0
);
//let file_writer = File::create(logdir.join(file_name)).expect("");
//let writer = RecordWriter::new(file_writer);

Expand All @@ -47,20 +50,24 @@ impl EventFileWriter {
let child = spawn(move || {
let file_writer = File::create(logdir_move.join(file_name)).expect("");
let mut writer = RecordWriter::new(file_writer);

loop {
let result: EventSignal = rx.recv().unwrap();
match result {
EventSignal::Data(d) => {
writer.write(&d).expect("write error");
},
EventSignal::Flush => {writer.flush().expect("flush error");},
EventSignal::Stop => {break;},
}
EventSignal::Flush => {
writer.flush().expect("flush error");
}
EventSignal::Stop => {
break;
}
}
};
}
writer.flush().expect("flush error");
});

let mut ret = EventFileWriter {
logdir,
writer: tx,
Expand All @@ -81,13 +88,13 @@ impl EventFileWriter {
pub fn get_logdir(&self) -> PathBuf {
self.logdir.to_path_buf()
}

pub fn add_event(&mut self, event: &Event) {
let mut data: Vec<u8> = Vec::new();
event.write_to_vec(&mut data).expect("");
self.writer.send(EventSignal::Data(data)).expect("");
}

pub fn flush(&mut self) {
self.writer.send(EventSignal::Flush).expect("");
}
Expand Down
Loading