2NQXRSC7CUW5IQ72H7AFIPQI3HONT2SOWGM37OGRU7ZYBD2GQ2ZQC
let results = (0..mnist_data.len())
.map(|idx| {
let item = mnist_data.get(idx).unwrap();
let handles: Vec<_> = (0..4)
.map(|i| {
let tx = tx.clone();
let inferred_count = inferred_count.clone();
let dataset = mnist_data
.iter()
.skip(i * data_len / 4)
.take(data_len / 4)
.collect::<Vec<_>>();
let device = device.clone();
let immediate_stop = immediate_stop.clone();
std::thread::spawn(move || {
for item in dataset {
if immediate_stop.load(Ordering::Acquire) {
break;
}
let dyn_img = image::DynamicImage::ImageLuma8(
GrayImage::from_raw(
28,
28,
item.image
.iter()
.flatten()
.map(|f| (255.0 * *f) as u8)
.collect(),
)
.unwrap(),
);
let image = picker.new_resize_protocol(dyn_img);
let dyn_img = GrayImage::from_raw(
28,
28,
item.image
.iter()
.flatten()
.map(|f| (255.0 * *f) as u8)
.collect(),
)
.unwrap();
// Resize the image to 150x150
let dyn_img =
imageops::resize(&dyn_img, 150, 150, imageops::FilterType::Nearest);
// Wrap as dynamic image
let dyn_img = image::DynamicImage::ImageLuma8(dyn_img);
let image = picker.new_resize_protocol(dyn_img);
let loading_layout = Layout::vertical([
Constraint::Length(1),
Constraint::Length(1),
Constraint::Min(1),
]);
while inferred_count.load(Ordering::Acquire) < data_len {
if event::poll(std::time::Duration::from_millis(16))? {
if let event::Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
immediate_stop.store(true, Ordering::Release);
break;
}
}
}
terminal.draw(|f| {
let [text_area, bar_area, _] = loading_layout.areas(f.size());
f.render_widget(Paragraph::new("Loading..."), text_area);
f.render_widget(
Gauge::default()
.gauge_style(
Style::default()
.fg(ratatui::style::Color::White)
.bg(ratatui::style::Color::Black)
.add_modifier(Modifier::ITALIC),
)
.use_unicode(true)
.ratio(inferred_count.load(Ordering::Acquire) as f64 / data_len as f64),
bar_area,
)
})?;
}
for handle in handles {
handle.join().unwrap();
}
let mut results = Vec::with_capacity(data_len);
drop(tx);
results.extend(rx);