-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmain.rs
More file actions
83 lines (71 loc) · 1.94 KB
/
Copy pathmain.rs
File metadata and controls
83 lines (71 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use anyhow::Result;
use clap::{Parser, Subcommand};
use usls::{
models::{Swin2SR, APISR},
Config, DataLoader, Model, Source,
};
mod apisr;
mod swin2sr;
#[path = "../utils/mod.rs"]
mod utils;
#[derive(Parser)]
#[command(author, version, about = "Super-Resolution Examples")]
#[command(propagate_version = true)]
struct Cli {
/// Source: images
#[arg(long, global = true, default_value = "images/ekko.jpg")]
pub source: Source,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Apisr(apisr::APISRArgs),
Swin2sr(swin2sr::Swin2SRArgs),
}
fn main() -> Result<()> {
utils::init_logging();
let cli = Cli::parse();
match &cli.command {
Commands::Apisr(args) => {
let config = apisr::config(args)?.commit()?;
run::<APISR>(config, &cli.source)
}
Commands::Swin2sr(args) => {
let config = swin2sr::config(args)?.commit()?;
run::<Swin2SR>(config, &cli.source)
}
}?;
usls::perf_chart();
Ok(())
}
fn run<M>(config: Config, source: &Source) -> Result<()>
where
for<'a> M: Model<Input<'a> = &'a [usls::Image]>,
{
let mut model = M::new(config)?;
// Build dataloader
let dl = DataLoader::new(source)?
.with_batch(model.batch() as _)
.with_progress_bar(true)
.stream()?;
// Run & Save
for xs in &dl {
let ys = model.run(&xs)?;
for y in ys {
let images = y.images();
if !images.is_empty() {
for image in images.iter() {
image.save(format!(
"{}.png",
usls::Dir::Current
.base_dir_with_subs(&["runs/super-resolution", model.spec()])?
.join(usls::timestamp(None))
.display(),
))?;
}
}
}
}
Ok(())
}