|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use super::{Image, HtmlLog, ReverseColorPopularity, ImageRotate90, ImageTryCreate, ExportARCTaskJson}; |
|
use rand::Rng; |
|
use rand::seq::SliceRandom; |
|
use rand::{rngs::StdRng, SeedableRng}; |
|
use serde::Serialize; |
|
use std::fs; |
|
use std::path::{PathBuf, Path}; |
|
|
|
#[allow(dead_code)] |
|
#[derive(Debug, Clone, Copy, Serialize)] |
|
enum TwoPixelBasicTransformation { |
|
LandscapeOrientationReverse, |
|
LandscapeOrientationRotateCW, |
|
LandscapeOrientationRotateCCW, |
|
PortraitOrientationReverse, |
|
PortraitOrientationRotateCW, |
|
PortraitOrientationRotateCCW, |
|
MixedOrientationReverse, |
|
MixedOrientationRotateCW, |
|
MixedOrientationRotateCCW, |
|
} |
|
|
|
#[allow(dead_code)] |
|
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)] |
|
enum TwoPixelSpecialTransformation { |
|
LandscapeOrientation, |
|
PortraitOrientation, |
|
MixedOrientation, |
|
} |
|
|
|
#[allow(dead_code)] |
|
#[derive(Debug, Clone, Copy, Serialize)] |
|
enum TwoPixelTransformation { |
|
Basic { basic: TwoPixelBasicTransformation }, |
|
Special { special: TwoPixelSpecialTransformation }, |
|
} |
|
|
|
#[allow(dead_code)] |
|
#[derive(Debug, Serialize)] |
|
struct DatasetItem { |
|
dirname: String, |
|
filename: String, |
|
json: String, |
|
} |
|
|
|
#[allow(dead_code)] |
|
pub struct GenerateDataset { |
|
dataset_items: Vec<DatasetItem>, |
|
} |
|
|
|
impl GenerateDataset { |
|
#[allow(dead_code)] |
|
fn new() -> Self { |
|
Self { |
|
dataset_items: vec!(), |
|
} |
|
} |
|
|
|
#[allow(dead_code)] |
|
fn populate(&mut self, number_of_items: u32, print_to_htmllog: bool) -> anyhow::Result<()> { |
|
let transformations: Vec<TwoPixelTransformation> = vec![ |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::LandscapeOrientationReverse }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::LandscapeOrientationRotateCW }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::LandscapeOrientationRotateCCW }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::PortraitOrientationReverse }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::PortraitOrientationRotateCW }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::PortraitOrientationRotateCCW }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::MixedOrientationReverse }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::MixedOrientationRotateCW }, |
|
TwoPixelTransformation::Basic { basic: TwoPixelBasicTransformation::MixedOrientationRotateCCW }, |
|
TwoPixelTransformation::Special { special: TwoPixelSpecialTransformation::LandscapeOrientation }, |
|
TwoPixelTransformation::Special { special: TwoPixelSpecialTransformation::PortraitOrientation }, |
|
TwoPixelTransformation::Special { special: TwoPixelSpecialTransformation::MixedOrientation }, |
|
]; |
|
|
|
for i in 0..number_of_items { |
|
if print_to_htmllog { |
|
HtmlLog::text(format!("iteration: {}", i)); |
|
} |
|
if i % 100000 == 0 { |
|
println!("iteration: {} number_of_items: {}", i, number_of_items); |
|
} |
|
let transform_index: usize = (i as usize) % transformations.len(); |
|
let transformation: TwoPixelTransformation = transformations[transform_index].clone(); |
|
|
|
let random_seed: u64 = ((i as usize / transformations.len()) + (transform_index * 1000000)) as u64; |
|
|
|
match transformation { |
|
TwoPixelTransformation::Basic { basic } => { |
|
let dataset_item: DatasetItem = Self::generate_twopixels_basic(basic.clone(), random_seed, print_to_htmllog)?; |
|
self.dataset_items.push(dataset_item); |
|
}, |
|
TwoPixelTransformation::Special { special } => { |
|
let dataset_item: DatasetItem = Self::generate_twopixels_special(special.clone(), random_seed, print_to_htmllog)?; |
|
self.dataset_items.push(dataset_item); |
|
}, |
|
} |
|
} |
|
|
|
Ok(()) |
|
} |
|
|
|
#[allow(dead_code)] |
|
fn save(&self, save_dir: &Path) -> anyhow::Result<()> { |
|
if !save_dir.is_dir() { |
|
fs::create_dir(save_dir)?; |
|
} |
|
assert!(save_dir.is_dir()); |
|
|
|
for dataset_item in &self.dataset_items { |
|
let task_type_dir: PathBuf = save_dir.join(&dataset_item.dirname); |
|
if !task_type_dir.is_dir() { |
|
fs::create_dir(&task_type_dir)?; |
|
} |
|
assert!(task_type_dir.is_dir()); |
|
|
|
let path: PathBuf = task_type_dir.join(&dataset_item.filename); |
|
fs::write(&path, &dataset_item.json)?; |
|
} |
|
Ok(()) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
fn five_unique_color_pairs(rng: &mut StdRng) -> Vec<(u8, u8)> { |
|
let mut colors: Vec<u8> = (0..=9).collect(); |
|
colors.shuffle(rng); |
|
let mut pairs = Vec::<(u8, u8)>::new(); |
|
while colors.len() >= 2 { |
|
let color0: u8 = colors.remove(0); |
|
let color1: u8 = colors.remove(0); |
|
pairs.push((color0, color1)); |
|
} |
|
assert!(pairs.len() == 5); |
|
pairs |
|
} |
|
|
|
fn alternate(count: usize, values: Vec<u8>) -> Vec<u8> { |
|
assert!(values.len() >= 2); |
|
let mut result = Vec::<u8>::new(); |
|
for i in 0..count { |
|
let index: usize = i % values.len(); |
|
result.push(values[index]); |
|
} |
|
result |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
fn round_robin_shuffled(rng: &mut StdRng, count: usize, values: &Vec<u8>) -> Vec<u8> { |
|
assert!(values.len() >= 2); |
|
|
|
|
|
|
|
let mut values: Vec<u8> = values.clone(); |
|
values.shuffle(rng); |
|
let mut items: Vec<u8> = Self::alternate(count, values); |
|
|
|
|
|
items.shuffle(rng); |
|
items |
|
} |
|
|
|
fn generate_twopixels_basic(transformation: TwoPixelBasicTransformation, random_seed: u64, print_to_htmllog: bool) -> anyhow::Result<DatasetItem> { |
|
let mut rng: StdRng = SeedableRng::seed_from_u64(random_seed); |
|
|
|
let insert_same_color_when_reaching_this_limit: u8 = 50; |
|
let insert_same_value: u8 = rng.gen_range(0..=100); |
|
|
|
let pair_count_values: Vec<(u8, u8)> = vec![ |
|
(2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (4, 3) |
|
]; |
|
let (train_count, test_count) = *pair_count_values.choose(&mut rng).unwrap(); |
|
let pair_count: u8 = train_count + test_count; |
|
|
|
let zero_one: Vec<u8> = vec![0, 1]; |
|
let mut mixed_orientation_vec = Vec::<u8>::new(); |
|
mixed_orientation_vec.extend(Self::round_robin_shuffled(&mut rng, train_count as usize, &zero_one)); |
|
mixed_orientation_vec.extend(Self::round_robin_shuffled(&mut rng, test_count as usize, &zero_one)); |
|
assert!(mixed_orientation_vec.len() == pair_count as usize); |
|
|
|
|
|
|
|
let mut color_pairs: Vec<(u8, u8)> = Self::five_unique_color_pairs(&mut rng); |
|
color_pairs.truncate(pair_count as usize); |
|
|
|
|
|
|
|
while color_pairs.len() < pair_count as usize { |
|
let color0: u8 = rng.gen_range(0..=9); |
|
let color1: u8 = rng.gen_range(0..=9); |
|
if color0 == color1 { |
|
continue; |
|
} |
|
if color_pairs.contains(&(color0, color1)) { |
|
continue; |
|
} |
|
color_pairs.push((color0, color1)); |
|
} |
|
assert!(color_pairs.len() == pair_count as usize); |
|
|
|
|
|
|
|
|
|
|
|
let allow_same_color: bool = match transformation { |
|
TwoPixelBasicTransformation::LandscapeOrientationReverse => false, |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCW => true, |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCCW => true, |
|
TwoPixelBasicTransformation::PortraitOrientationReverse => false, |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCW => true, |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCCW => true, |
|
TwoPixelBasicTransformation::MixedOrientationReverse => false, |
|
TwoPixelBasicTransformation::MixedOrientationRotateCW => true, |
|
TwoPixelBasicTransformation::MixedOrientationRotateCCW => true, |
|
}; |
|
if allow_same_color && train_count >= 2 && test_count >= 1 && insert_same_value >= insert_same_color_when_reaching_this_limit { |
|
|
|
let index: usize = rng.gen_range(train_count..pair_count) as usize; |
|
let color: u8 = rng.gen_range(0..=9); |
|
color_pairs[index] = (color, color); |
|
} |
|
|
|
if print_to_htmllog { |
|
HtmlLog::text(format!("pair_count: {}", pair_count)); |
|
} |
|
let mut export = ExportARCTaskJson::new(); |
|
let mut color_pair_strings = Vec::<String>::new(); |
|
for i in 0..pair_count { |
|
let is_train: bool = i < train_count; |
|
|
|
|
|
|
|
|
|
let (color0, color1) = color_pairs.remove(0); |
|
|
|
let input_landscape: Image = Image::try_create(2, 1, vec![color0, color1])?; |
|
let input_portrait: Image = input_landscape.rotate_cw()?; |
|
|
|
|
|
|
|
|
|
let input_mixed: Image = match mixed_orientation_vec[i as usize] { |
|
0 => input_landscape.clone(), |
|
1 => input_portrait.clone(), |
|
_ => unreachable!(), |
|
}; |
|
|
|
let input: &Image = match transformation { |
|
TwoPixelBasicTransformation::LandscapeOrientationReverse => &input_landscape, |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCW => &input_landscape, |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCCW => &input_landscape, |
|
TwoPixelBasicTransformation::PortraitOrientationReverse => &input_portrait, |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCW => &input_portrait, |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCCW => &input_portrait, |
|
TwoPixelBasicTransformation::MixedOrientationReverse => &input_mixed, |
|
TwoPixelBasicTransformation::MixedOrientationRotateCW => &input_mixed, |
|
TwoPixelBasicTransformation::MixedOrientationRotateCCW => &input_mixed, |
|
}; |
|
|
|
let output_reversed: Image = ReverseColorPopularity::apply_to_image(input)?; |
|
let output_rotate_ccw: Image = input.rotate_ccw()?; |
|
let output_rotate_cw: Image = input.rotate_cw()?; |
|
|
|
let output: &Image = match transformation { |
|
TwoPixelBasicTransformation::LandscapeOrientationReverse => &output_reversed, |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCW => &output_rotate_cw, |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCCW => &output_rotate_ccw, |
|
TwoPixelBasicTransformation::PortraitOrientationReverse => &output_reversed, |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCW => &output_rotate_cw, |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCCW => &output_rotate_ccw, |
|
TwoPixelBasicTransformation::MixedOrientationReverse => &output_reversed, |
|
TwoPixelBasicTransformation::MixedOrientationRotateCW => &output_rotate_cw, |
|
TwoPixelBasicTransformation::MixedOrientationRotateCCW => &output_rotate_ccw, |
|
}; |
|
|
|
if print_to_htmllog { |
|
HtmlLog::compare_images(vec![input.clone(), output.clone()]); |
|
} |
|
assert!(input != output, "input and output must be different"); |
|
if is_train { |
|
export.push_train(&input, &output); |
|
} else { |
|
export.push_test(&input, &output); |
|
} |
|
|
|
color_pair_strings.push(format!("{}{}", color0, color1)); |
|
} |
|
|
|
let transformation_name: &str = match transformation { |
|
TwoPixelBasicTransformation::LandscapeOrientationReverse => "land_rev", |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCW => "land_cw", |
|
TwoPixelBasicTransformation::LandscapeOrientationRotateCCW => "land_ccw", |
|
TwoPixelBasicTransformation::PortraitOrientationReverse => "port_rev", |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCW => "port_cw", |
|
TwoPixelBasicTransformation::PortraitOrientationRotateCCW => "port_ccw", |
|
TwoPixelBasicTransformation::MixedOrientationReverse => "landport_rev", |
|
TwoPixelBasicTransformation::MixedOrientationRotateCW => "landport_cw", |
|
TwoPixelBasicTransformation::MixedOrientationRotateCCW => "landport_ccw", |
|
}; |
|
|
|
let color_pair_strings_joined: String = color_pair_strings.join("_"); |
|
let filename: String = format!("{}_{}.json", transformation_name, color_pair_strings_joined); |
|
|
|
let dataset_item: DatasetItem = DatasetItem { |
|
json: export.to_string()?, |
|
dirname: transformation_name.to_string(), |
|
filename, |
|
}; |
|
Ok(dataset_item) |
|
} |
|
|
|
fn generate_twopixels_special(transformation: TwoPixelSpecialTransformation, random_seed: u64, print_to_htmllog: bool) -> anyhow::Result<DatasetItem> { |
|
let mut rng: StdRng = SeedableRng::seed_from_u64(random_seed); |
|
|
|
let pair_count_values: Vec<(u8, u8)> = match transformation { |
|
TwoPixelSpecialTransformation::LandscapeOrientation | TwoPixelSpecialTransformation::PortraitOrientation => vec![ |
|
|
|
(2, 2), (2, 3), (3, 2), (3, 3) |
|
], |
|
TwoPixelSpecialTransformation::MixedOrientation => vec![ |
|
|
|
(4, 2), (4, 3), (5, 2), (5, 3) |
|
], |
|
}; |
|
|
|
let (train_count, test_count) = *pair_count_values.choose(&mut rng).unwrap(); |
|
let pair_count: u8 = train_count + test_count; |
|
|
|
let mut values: Vec<u8> = (0..pair_count).collect(); |
|
values.shuffle(&mut rng); |
|
|
|
|
|
let zero_one_two_tree: Vec<u8> = match transformation { |
|
TwoPixelSpecialTransformation::LandscapeOrientation | TwoPixelSpecialTransformation::PortraitOrientation => { |
|
|
|
|
|
|
|
vec![0, 1] |
|
}, |
|
TwoPixelSpecialTransformation::MixedOrientation => { |
|
|
|
|
|
|
|
|
|
|
|
vec![0, 1, 2, 3] |
|
} |
|
}; |
|
let mut mode_vec = Vec::<u8>::new(); |
|
mode_vec.extend(Self::round_robin_shuffled(&mut rng, train_count as usize, &zero_one_two_tree)); |
|
mode_vec.extend(Self::round_robin_shuffled(&mut rng, test_count as usize, &zero_one_two_tree)); |
|
assert!(mode_vec.len() == pair_count as usize); |
|
|
|
|
|
let mut color_pairs: Vec<(u8, u8)> = Self::five_unique_color_pairs(&mut rng); |
|
color_pairs.truncate(pair_count as usize); |
|
while color_pairs.len() < pair_count as usize { |
|
let color0: u8 = rng.gen_range(0..=9); |
|
let color1: u8 = rng.gen_range(0..=9); |
|
if color0 == color1 { |
|
continue; |
|
} |
|
if color_pairs.contains(&(color0, color1)) { |
|
continue; |
|
} |
|
color_pairs.push((color0, color1)); |
|
} |
|
assert!(color_pairs.len() == pair_count as usize); |
|
|
|
let mut available_colors: Vec<u8> = (0..=9).collect(); |
|
available_colors.shuffle(&mut rng); |
|
|
|
|
|
for i in 0..pair_count { |
|
let value: u8 = mode_vec[i as usize]; |
|
let assign_same_color: bool = (value & 1) == 0; |
|
if !assign_same_color { |
|
continue; |
|
} |
|
let color: u8 = available_colors.remove(0); |
|
color_pairs[i as usize] = (color, color); |
|
|
|
} |
|
|
|
if print_to_htmllog { |
|
HtmlLog::text(format!("pair_count: {}", pair_count)); |
|
} |
|
let mut export = ExportARCTaskJson::new(); |
|
let mut color_pair_strings = Vec::<String>::new(); |
|
for i in 0..pair_count { |
|
let is_train: bool = i < train_count; |
|
|
|
|
|
|
|
|
|
let (color0, color1) = color_pairs.remove(0); |
|
|
|
let input_landscape: Image = Image::try_create(2, 1, vec![color0, color1])?; |
|
let input_portrait: Image = input_landscape.rotate_cw()?; |
|
|
|
|
|
|
|
|
|
let orientation: bool = mode_vec[i as usize] & 2 == 0; |
|
let input_mixed: Image = match orientation { |
|
false => input_landscape.clone(), |
|
true => input_portrait.clone(), |
|
}; |
|
|
|
let input: &Image = match transformation { |
|
TwoPixelSpecialTransformation::LandscapeOrientation => &input_landscape, |
|
TwoPixelSpecialTransformation::PortraitOrientation => &input_portrait, |
|
TwoPixelSpecialTransformation::MixedOrientation => &input_mixed, |
|
}; |
|
|
|
let output_reversed: Image = ReverseColorPopularity::apply_to_image(input)?; |
|
let output_rotate_ccw: Image = input.rotate_ccw()?; |
|
|
|
let output: &Image = if color0 == color1 { |
|
&output_rotate_ccw |
|
} else { |
|
&output_reversed |
|
}; |
|
|
|
if print_to_htmllog { |
|
HtmlLog::compare_images(vec![input.clone(), output.clone()]); |
|
} |
|
assert!(input != output, "input and output must be different"); |
|
if is_train { |
|
export.push_train(&input, &output); |
|
} else { |
|
export.push_test(&input, &output); |
|
} |
|
|
|
color_pair_strings.push(format!("{}{}", color0, color1)); |
|
} |
|
|
|
let transformation_name: &str = match transformation { |
|
TwoPixelSpecialTransformation::LandscapeOrientation => "land_rotrev", |
|
TwoPixelSpecialTransformation::PortraitOrientation => "port_rotrev", |
|
TwoPixelSpecialTransformation::MixedOrientation => "landport_rotrev", |
|
}; |
|
|
|
let color_pair_strings_joined: String = color_pair_strings.join("_"); |
|
let filename: String = format!("{}_{}.json", transformation_name, color_pair_strings_joined); |
|
|
|
let dataset_item: DatasetItem = DatasetItem { |
|
json: export.to_string()?, |
|
dirname: transformation_name.to_string(), |
|
filename, |
|
}; |
|
Ok(dataset_item) |
|
} |
|
|
|
} |
|
|
|
#[cfg(test)] |
|
mod tests { |
|
use super::*; |
|
|
|
#[test] |
|
fn test_10000_five_unique_color_pairs() { |
|
let actual: Vec<(u8, u8)> = GenerateDataset::five_unique_color_pairs(&mut StdRng::seed_from_u64(0)); |
|
assert_eq!(actual, vec![(5, 2), (9, 1), (6, 3), (4, 0), (7, 8)]); |
|
} |
|
|
|
#[test] |
|
fn test_20000_alternate() { |
|
assert_eq!(GenerateDataset::alternate(2, vec![0, 1]), vec![0, 1]); |
|
assert_eq!(GenerateDataset::alternate(3, vec![0, 1]), vec![0, 1, 0]); |
|
assert_eq!(GenerateDataset::alternate(4, vec![0, 1]), vec![0, 1, 0, 1]); |
|
assert_eq!(GenerateDataset::alternate(3, vec![4, 5]), vec![4, 5, 4]); |
|
assert_eq!(GenerateDataset::alternate(6, vec![1, 2, 3]), vec![1, 2, 3, 1, 2, 3]); |
|
} |
|
|
|
#[test] |
|
fn test_30000_round_robin_shuffled() { |
|
let a: Vec<u8> = vec![0, 1]; |
|
let b: Vec<u8> = vec![5, 6, 7]; |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(0), 5, &a), vec![1, 1, 0, 0, 0]); |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(0), 6, &a), vec![1, 1, 0, 0, 0, 1]); |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(0), 7, &a), vec![1, 0, 0, 0, 1, 0, 1]); |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(1), 5, &a), vec![0, 0, 1, 1, 0]); |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(1), 6, &a), vec![0, 0, 1, 1, 0, 1]); |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(1), 7, &a), vec![0, 1, 0, 1, 0, 0, 1]); |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(1), 8, &a), vec![0, 0, 1, 0, 1, 0, 1, 1]); |
|
assert_eq!(GenerateDataset::round_robin_shuffled(&mut StdRng::seed_from_u64(0), 3, &b), vec![7, 5, 6]); |
|
} |
|
|
|
#[allow(dead_code)] |
|
|
|
fn test_40000_generate() { |
|
|
|
let mut generate_dataset = GenerateDataset::new(); |
|
|
|
|
|
|
|
generate_dataset.populate(1200, false).expect("ok"); |
|
|
|
generate_dataset.save(&PathBuf::from("/Users/neoneye/Downloads/output")).expect("ok"); |
|
|
|
|
|
} |
|
} |
|
|