import numpy as np from utils import CreateOutputDir, GetLogger from hypernetwork import Hypernetwork from cifar10_data_fetcher import Cifar10DataFetcher from params import GeneralParameters, HypernetworkHyperParameters, Cifar10Params, ResNetCifar10HyperParameters initialize_from_checkpoint = False output_dir = CreateOutputDir('output/', __file__) logger = GetLogger(log_file_mode='a' if initialize_from_checkpoint else 'w', log_file_path=os.path.join(output_dir, 'log.txt')) # create parameter objects general_params = GeneralParameters() hparams = HypernetworkHyperParameters() target_hparams = ResNetCifar10HyperParameters() image_params = Cifar10Params() # override hparams.initialization_std = 1e-3 hparams.learning_rate = 1e-6 hparams.learning_rate_rate = 0.9999 hparams.lamBda = 1e8 target_hparams.batch_type = 'BATCH_TYPE5' # set random seeds np.random.seed(general_params.seed) tf.set_random_seed(general_params.seed)
def __init__(self, x, y, mode: str = 'EVAL', general_params=GeneralParameters(), hnet_hparams=HypernetworkHyperParameters(), target_hparams: ResNetHyperParameters = ResNetCifar10HyperParameters(), image_params: DataParams = Cifar10Params(), devices=None, graph=None): """ Args: x: image batch y: labels (one hot) mode: either `'TRAIN'` or `'EVAL'`. determines whether to add optimization ops general_params: hnet_hparams: target_hparams: image_params: devices: (optional) a dictionary, with keys `'cpu'` and `'gpus'`, where the former is the cpu device and the latter is a list of gpu devices. graph: """ if mode.upper() in ['TRAIN', 'TRAINING']: mode = 'TRAIN' elif mode.upper() in [ 'VALIDATE', 'VALIDATION', 'VALIDATING', 'EVALUATE', 'EVAL', 'EVALUATION', 'EVALUATING', 'TEST', 'TESTING' ]: mode = 'EVAL' else: raise ValueError( "mode needs to be one of 'TRAIN','VALIDATE','INFER'") self.general_params = general_params self.hnet_hparams = hnet_hparams self.target_hparams = target_hparams self.image_params = image_params self.graph = graph if devices is None: devices = GetDevices() self.devices = devices self.cpu = devices['cpu'] # we will cycle over the GPU list, so that our code is agnostic to the number of GPUs self.gpus = cycle( devices['gpus']) if len(devices['gpus']) > 0 else cycle( [devices['cpu']]) self.x = x self.y = y if graph is None: graph = tf.get_default_graph() self.graph = graph with self.graph.as_default(): self.z = tf.placeholder(tf.float32, [None, hnet_hparams.input_noise_size]) self.is_training = tf.Variable(False, trainable=False, name='is_training') with tf.variable_scope('generator', reuse=tf.AUTO_REUSE): self.__AddGeneratorOps() self.__AddEvaluationOps() if mode == 'TRAIN': self.__AddOptimizationOps() self.initializer = tf.variables_initializer( self.graph.get_collection('variables'), name='initializer')
import numpy as np from utils import CreateOutputDir, GetLogger from hypernetwork import Hypernetwork from cifar10_data_fetcher import Cifar10DataFetcher from params import GeneralParameters, HypernetworkHyperParameters, Cifar10Params, ResNetCifar10HyperParameters initialize_from_checkpoint = False output_dir = CreateOutputDir('output/', __file__) logger = GetLogger(log_file_mode='a' if initialize_from_checkpoint else 'w', log_file_path=os.path.join(output_dir, 'log.txt')) # create parameter objects general_params = GeneralParameters() hparams = HypernetworkHyperParameters() target_hparams = ResNetCifar10HyperParameters() image_params = Cifar10Params() # override hparams.initialization_std = 1e-3 hparams.learning_rate = 4e-7 hparams.learning_rate_rate = 0.99999 hparams.create_optimizer = lambda learning_rate, hnet_hparams: tf.train.AdamOptimizer( learning_rate) hparams.with_residual_connections = True target_hparams.batch_type = 'BATCH_TYPE5' # set random seeds np.random.seed(general_params.seed)
import numpy as np from utils import CreateOutputDir, GetLogger from hypernetwork import Hypernetwork from cifar10_data_fetcher import Cifar10DataFetcher from params import GeneralParameters, HypernetworkHyperParameters initialize_from_checkpoint = False output_dir = CreateOutputDir('output', __file__) logger = GetLogger(log_file_mode='a' if initialize_from_checkpoint else 'w', log_file_path=os.path.join(output_dir, 'log.txt')) # create parameter objects general_params = GeneralParameters() hparams = HypernetworkHyperParameters() # set random seeds np.random.seed(general_params.seed) tf.set_random_seed(general_params.seed) # get training data pipeline. training_data = Cifar10DataFetcher('TRAIN', batch_size=hparams.batch_size) validation_data = Cifar10DataFetcher('VALIDATION', batch_size=hparams.batch_size) # create hypernet hnet = Hypernetwork(training_data.image, training_data.label, 'TRAIN', hnet_hparams=hparams)