harbor run -t openthoughts/tasktrove-mix-h11-single-skill-only-v2-mix-h11-single-skill-2133harbor download openthoughts/tasktrove-mix-h11-single-skill-only-v2-mix-h11-single-skill-2133solution file
import argparse
import logging
import os
import numpy as np
import imageio
from torchvision.utils import make_grid
import torch
from dpsda.logging import setup_logging
from dpsda.data_loader import load_data
from dpsda.feature_extractor import extract_features
from dpsda.metrics import make_fid_stats
from dpsda.metrics import compute_fid
from dpsda.dp_counter import dp_nn_histogram
from dpsda.arg_utils import str2bool
from apis import get_api_class_from_name
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--api',
type=str,
required=True,
choices=['DALLE', 'stable_diffusion', 'improved_diffusion'],
help='Which foundation model API to use')
parser.add_argument(
'--plot_images',
type=str2bool,
default=True,
help='Whether to save generated images in PNG files')
parser.add_argument(
'--data_checkpoint_path',
type=str,
default="",
help='Path to the data checkpoint')
parser.add_argument(
'--data_checkpoint_step',
type=int,
default=-1,
help='Iteration of the data checkpoint')
parser.add_argument(
'--num_samples_schedule',
type=str,
default='50000,'*9 + '50000',
help='Number of samples to generate at each iteration')
parser.add_argument(
'--variation_degree_schedule',
type=str,
default='0,'*9 + '0',
help='Variation degree at each iteration')
parser.add_argument(
'--num_fid_samples',
type=int,
default=50000,
help='Number of generated samples to compute FID')
parser.add_argument(
'--num_private_samples',
type=int,
default=50000,
help='Number of private samples to load')
parser.add_argument(
'--noise_multiplier',
type=float,
default=0.0,
help='Noise multiplier for DP NN histogram')
parser.add_argument(
'--lookahead_degree',
type=int,
default=0,
help=('Lookahead degree for computing distances between private and '
'generated images'))
parser.add_argument(
'--feature_extractor',
type=str,
default='clip_vit_b_32',
choices=['inception_v3', 'clip_vit_b_32', 'original'],
help='Which image feature extractor to use')
parser.add_argument(
'--num_nearest_neighbor',
type=int,
default=1,
help='Number of nearest neighbors to find in DP NN histogram')
parser.add_argument(
'--nn_mode',
type=str,
default='L2',
choices=['L2', 'IP'],
help='Which distance metric to use in DP NN histogram')
parser.add_argument(
'--private_image_size',
type=int,
default=1024,
help='Size of private images')
parser.add_argument(
'--tmp_folder',
type=str,
default='result/tmp',
help='Temporary folder for storing intermediate results')
parser.add_argument(
'--result_folder',
type=str,
default='result',
help='Folder for storing results')
parser.add_argument(
'--data_folder',
type=str,
required=True,
help='Folder that contains the private images')
parser.add_argument(
'--count_threshold',
type=float,
default=0.0,
help='Threshold for DP NN histogram')
parser.add_argument(
'--compute_fid',
type=str2bool,
default=True,
help='Whether to compute FID')
parser.add_argument(
'--fid_dataset_name',
type=str,
default='customized_dataset',
help=('Name of the dataset for computing FID against. If '
'fid_dataset_name and fid_dataset_split in combination are one '
'of the precomputed datasets in '
'https://github.com/GaParmar/clean-fid and make_fid_stats=False,'
' then the precomputed statistics will be used. Otherwise, the '
'statistics will be computed using the private samples and saved'
' with fid_dataset_name and fid_dataset_split for future use.'))
parser.add_argument(
'--fid_dataset_split',
type=str,
default='train',
help=('Split of the dataset for computing FID against. If '
'fid_dataset_name and fid_dataset_split in combination are one '
'of the precomputed datasets in '
'https://github.com/GaParmar/clean-fid and make_fid_stats=False,'
' then the precomputed statistics will be used. Otherwise, the '
'statistics will be computed using the private samples and saved'
' with fid_dataset_name and fid_dataset_split for future use.'))
parser.add_argument(
'--fid_model_name',
type=str,
default='inception_v3',
choices=['inception_v3', 'clip_vit_b_32'],
help='Which embedding network to use for computing FID')
parser.add_argument(
'--make_fid_stats',
type=str2bool,
default=True,
help='Whether to compute FID stats for the private samples')
parser.add_argument(
'--data_loading_batch_size',
type=int,
default=100,
help='Batch size for loading private samples')
parser.add_argument(
'--feature_extractor_batch_size',
type=int,
default=500,
help='Batch size for feature extraction')
parser.add_argument(
'--fid_batch_size',
type=int,
default=500,
help='Batch size for computing FID')
parser.add_argument(
'--gen_class_cond',
type=str2bool,
default=False,
help='Whether to generate class labels')
parser.add_argument(
'--initial_prompt',
action='append',
type=str,
help='Initial prompt for image generation. It can be specified '
'multiple times to provide a list of prompts. If the API accepts '
'prompts, the initial samples will be generated with these '
'prompts')
parser.add_argument(
'--image_size',
type=str,
default='1024x1024',
help='Size of generated images in the format of HxW')
args, api_args = parser.parse_known_args()
args.num_samples_schedule = list(map(
int, args.num_samples_schedule.split(',')))
variation_degree_type = (float if '.' in args.variation_degree_schedule
else int)
args.variation_degree_schedule = list(map(
variation_degree_type, args.variation_degree_schedule.split(',')))
if len(args.num_samples_schedule) != len(args.variation_degree_schedule):
raise ValueError('The length of num_samples_schedule and '
'variation_degree_schedule should be the same')
api_class = get_api_class_from_name(args.api)
api = api_class.
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py