Python optimizer.Optimizer() Examples

The following are 30 code examples of optimizer.Optimizer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module optimizer , or try the search function .
Example #1
Source File: optimizer_largetest.py    From compare-codecs with Apache License 2.0 6 votes vote down vote up
def test_OptimizeOverMultipleEncoders(self):
    """Run the optimizer for a few cycles with a real codec.

    This may turn out to be an over-heavy test for every-checkin testing."""
    my_fileset = test_tools.TestFileSet()
    my_codec = vp8.Vp8Codec()
    my_optimizer = optimizer.Optimizer(my_codec, my_fileset,
                                       cache_class=encoder.EncodingDiskCache)
    # Establish a baseline.
    for bitrate, videofile_name in my_fileset.AllFilesAndRates():
      videofile = encoder.Videofile(videofile_name)
      my_encoding = my_optimizer.BestEncoding(bitrate, videofile)
      my_encoding.Execute().Store()
    # Try to improve it.
    encoding_count = 0
    while encoding_count < 10:
      (bitrate, videofile_name) = random.choice(my_fileset.AllFilesAndRates())
      videofile = encoder.Videofile(videofile_name)
      next_encoding = my_optimizer.BestUntriedEncoding(bitrate, videofile)
      if not next_encoding:
        break
      encoding_count += 1
      next_encoding.Execute().Store() 
Example #2
Source File: load_opt_sched.py    From amortized-variational-filtering with MIT License 6 votes vote down vote up
def load_opt_sched(train_config, model):

    inf_params = model.inference_parameters()
    gen_params = model.generative_parameters()

    inf_opt = Optimizer(train_config['optimizer'], inf_params,
                        lr=train_config['inference_learning_rate'],
                        clip_grad_norm=train_config['clip_grad_norm'])
    inf_sched = ExponentialLR(inf_opt.opt, 0.999)

    gen_opt = Optimizer(train_config['optimizer'], gen_params,
                        lr=train_config['generation_learning_rate'],
                        clip_grad_norm=train_config['clip_grad_norm'])
    gen_sched = ExponentialLR(gen_opt.opt, 0.999)

    return (inf_opt, gen_opt), (inf_sched, gen_sched) 
Example #3
Source File: visual_metrics.py    From compare-codecs with Apache License 2.0 6 votes vote down vote up
def ListMpegSingleConfigResults(codecs, datatable, score_function=None):
  encoder_list = {}
  optimizer_list = {}
  for codec_name in codecs:
    codec = pick_codec.PickCodec(codec_name)
    my_optimizer = optimizer.Optimizer(codec,
        score_function=score_function, file_set=mpeg_settings.MpegFiles())
    optimizer_list[codec_name] = my_optimizer
    encoder_list[codec_name] = my_optimizer.BestOverallEncoder()
  for rate, filename in sorted(mpeg_settings.MpegFiles().AllFilesAndRates()):
    videofile = encoder.Videofile(filename)
    for codec_name in codecs:
      if encoder_list[codec_name]:
        my_encoding = encoder_list[codec_name].Encoding(rate, videofile)
        my_encoding.Recover()
        AddOneEncoding(codec_name, optimizer_list[codec_name],
                       my_encoding, videofile, datatable) 
Example #4
Source File: graph_metrics.py    From compare-codecs with Apache License 2.0 6 votes vote down vote up
def __init__(self, filename, codec, score_function):
    self.name = codec.name
    self.videofile = encoder.Videofile(filename)
    self.fileset = optimizer.FileAndRateSet()
    self.fileset.AddFilesAndRates([filename],
        fileset_picker.ChooseRates(self.videofile.width,
                                   self.videofile.framerate))
    self.my_optimizer = optimizer.Optimizer(codec,
                                            file_set=self.fileset,
                                            score_function=score_function)
    self.filename = filename
    self.encoder = self.my_optimizer.BestOverallEncoder()
    if not self.encoder:
      raise NotEnoughDataError('No overall encoder for %s on %s' %
                               (codec.name, filename))
    self.points = None 
Example #5
Source File: vp9_unittest.py    From compare-codecs with Apache License 2.0 6 votes vote down vote up
def test_Passes(self):
    """This test checks that both 1-pass and 2-pass encoding works."""
    codec = vp9.Vp9Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
      'one_black_frame_1024_768_30.yuv')
    start_encoder = codec.StartEncoder(my_optimizer.context)
    encoder1 = encoder.Encoder(my_optimizer.context,
        start_encoder.parameters.ChangeValue('passes', 1))
    encoding1 = encoder1.Encoding(1000, videofile)
    encoder2 = encoder.Encoder(my_optimizer.context,
        start_encoder.parameters.ChangeValue('passes', 2))
    encoding2 = encoder2.Encoding(1000, videofile)
    encoding1.Execute()
    encoding2.Execute()
    self.assertTrue(encoding1.result)
    self.assertTrue(encoding2.result) 
Example #6
Source File: optimizer_unittest.py    From compare-codecs with Apache License 2.0 6 votes vote down vote up
def test_MultipleOptimizers(self):
    # Make sure other score directories don't interfere with this test.
    encoder_configuration.conf.override_scorepath_for_test([])

    os.mkdir(os.path.join(encoder_configuration.conf.sysdir(), 'first_dir'))
    os.mkdir(os.path.join(encoder_configuration.conf.sysdir(), 'second_dir'))
    one_optimizer = optimizer.Optimizer(self.codec, scoredir='first_dir')
    another_optimizer = optimizer.Optimizer(self.codec, scoredir='second_dir')
    self.assertNotEqual(one_optimizer.context.cache.workdir,
                        another_optimizer.context.cache.workdir)
    # Storing one encoding's score should not affect the other's.
    one_encoding = one_optimizer.BestEncoding(100,
                                              self.videofile)
    one_encoding.Execute().Store()
    another_encoding = another_optimizer.BestEncoding(100, self.videofile)
    self.assertFalse(another_encoding.Result())
    another_encoding.Recover()
    self.assertFalse(another_encoding.Result()) 
Example #7
Source File: vp9_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_OneBlackFrame(self):
    codec = vp9.Vp9Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
      'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(50.0, my_optimizer.Score(encoding))
    self.assertEqual(1, len(encoding.result['frame']))
    # Check that expected results are present and "reasonable".
    print encoding.result
    self.assertTrue(0.02 < encoding.result['encode_cputime'] < 15.0)
    self.assertTrue(100 < encoding.result['bitrate'] < 500)
    self.assertTrue(500 < encoding.result['frame'][0]['size'] < 12000) 
Example #8
Source File: vp8_mpeg_1d_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_SuggestTweakDecreasesCq(self):
    codec = vp8_mpeg_1d.Vp8CodecMpeg1dMode()
    videofile = encoder.Videofile('foofile_640_480_30.yuv')
    my_optimizer = optimizer.Optimizer(codec)
    my_encoder = codec.StartEncoder(my_optimizer.context)
    encoding = encoder.Encoding(my_encoder, 500, videofile)
    encoding.result = {'bitrate': 200}
    # Since the bitrate is too high, the suggstion should be to increase it.
    new_encoding = codec.SuggestTweak(encoding)
    self.assertEqual('0', new_encoding.encoder.parameters.GetValue('key-q')) 
Example #9
Source File: vp8_mpeg_1d_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_OneBlackFrame(self):
    codec = vp8_mpeg_1d.Vp8CodecMpeg1dMode()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
      'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(50.0, my_optimizer.Score(encoding)) 
Example #10
Source File: x265_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_OneBlackFrame(self):
    codec = x265.X265Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(40.0, my_optimizer.Score(encoding)) 
Example #11
Source File: x265_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_TenBlackFrames(self):
    codec = x265.X265Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithBlankFrames(
        'ten_black_frames_1024_768_30.yuv', 10)
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(40.0, my_optimizer.Score(encoding)) 
Example #12
Source File: openh264_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_OneBlackFrame(self):
    codec = openh264.OpenH264Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(40.0, my_optimizer.Score(encoding)) 
Example #13
Source File: openh264_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_TenBlackFrames(self):
    codec = openh264.OpenH264Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithBlankFrames(
        'ten_black_frames_1024_768_30.yuv', 10)
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(40.0, my_optimizer.Score(encoding)) 
Example #14
Source File: vp8_mpeg_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_SuggestedTweakRefersToSameContext(self):
    codec = vp8_mpeg.Vp8CodecMpegMode()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
      'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    # Fake result.
    encoding.result = {'psnr': 42.0, 'bitrate':1000}
    next_encoding = codec.SuggestTweak(encoding)
    self.assertEqual(encoding.context, next_encoding.context) 
Example #15
Source File: x264_baseline_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_OneBlackFrame(self):
    codec = x264_baseline.X264BaselineCodec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(40.0, my_optimizer.Score(encoding)) 
Example #16
Source File: libavc_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_OneBlackFrame(self):
    codec = libavc.LibavcCodec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(40.0, my_optimizer.Score(encoding)) 
Example #17
Source File: libavc_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_TenBlackFrames(self):
    codec = libavc.LibavcCodec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithBlankFrames(
        'ten_black_frames_1024_768_30.yuv', 10)
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Most codecs should be good at this.
    self.assertLess(40.0, my_optimizer.Score(encoding)) 
Example #18
Source File: main.py    From neural-network-genetic-algorithm with MIT License 5 votes vote down vote up
def generate(generations, population, nn_param_choices, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" %
                     (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-'*80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5]) 
Example #19
Source File: optimizertester.py    From erays with MIT License 5 votes vote down vote up
def __init__(self, line, debug):
		reader = EffectReader(line)
		# reader = TraceReader(line)
		reader.parse_trace()
		self.code_size = len(reader.code)

		signal.signal(signal.SIGALRM, handler)
		signal.alarm(15)
		# print(reader.signature)
		optimizer = Optimizer(reader.code)

		InstructionExecutor(reader, optimizer, debug)
		signal.alarm(0) 
Example #20
Source File: dpg.py    From Python-Reinforcement-Learning-Projects with MIT License 5 votes vote down vote up
def _init_modules(self):
        
        # Replay memory
        self.replay_memory = ReplayMemory(history_len=self.history_len, 
                                          capacity=self.capacity)
        # Actor critic network
        self.ac_network = ActorCriticNet(input_dim=self.state_dim, 
                                         action_dim=self.action_dim, 
                                         critic_layers=self.critic_layers, 
                                         actor_layers=self.actor_layers, 
                                         actor_activation=self.actor_activation,
                                         scope='ac_network')
        # Target network
        self.target_network = ActorCriticNet(input_dim=self.state_dim, 
                                             action_dim=self.action_dim, 
                                             critic_layers=self.critic_layers, 
                                             actor_layers=self.actor_layers, 
                                             actor_activation=self.actor_activation,
                                             scope='target_network')
        # Optimizer
        self.optimizer = Optimizer(config=self.config, 
                                   ac_network=self.ac_network, 
                                   target_network=self.target_network, 
                                   replay_memory=self.replay_memory)
        # Ops for updating target network
        self.clone_op = self.target_network.get_clone_op(self.ac_network, tau=self.tau)
        # For tensorboard
        self.t_score = tf.placeholder(dtype=tf.float32, shape=[], name='new_score')
        tf.summary.scalar("score", self.t_score, collections=['dpg'])
        self.summary_op = tf.summary.merge_all('dpg') 
Example #21
Source File: q_learning.py    From Python-Reinforcement-Learning-Projects with MIT License 5 votes vote down vote up
def _init_modules(self):
        
        # Replay memory
        self.replay_memory = ReplayMemory(history_len=self.num_frames, 
                                          capacity=self.capacity, 
                                          batch_size=self.batch_size,
                                          input_scale=self.input_scale)
        
        input_shape = self.feedback_size + (self.num_frames,)
        # Q-network
        self.q_network = QNetwork(input_shape=input_shape, n_outputs=len(self.actions), 
                                  network_type=self.config['network_type'], scope='q_network')
        # Target network
        self.target_network = QNetwork(input_shape=input_shape, n_outputs=len(self.actions), 
                                       network_type=self.config['network_type'], scope='target_network')
        # Optimizer
        self.optimizer = Optimizer(config=self.config, 
                                   feedback_size=self.feedback_size, 
                                   q_network=self.q_network, 
                                   target_network=self.target_network, 
                                   replay_memory=self.replay_memory)
        # Ops for updating target network
        self.clone_op = self.target_network.get_clone_op(self.q_network)
        # For tensorboard
        self.t_score = tf.placeholder(dtype=tf.float32, shape=[], name='new_score')
        tf.summary.scalar("score", self.t_score, collections=['dqn'])
        self.summary_op = tf.summary.merge_all('dqn') 
Example #22
Source File: main.py    From mnist-multi-gpu with Apache License 2.0 5 votes vote down vote up
def generate(generations, population, nn_param_choices, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evole the population
        population (int): Number of networks in each generation
        nn_param_choices (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    optimizer = Optimizer(nn_param_choices)
    networks = optimizer.create_population(population)

    # Evolve the generation.
    for i in range(generations):
        logging.info("***Doing generation %d of %d***" %
                     (i + 1, generations))

        # Train and get accuracy for networks.
        train_networks(networks, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(networks)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-'*80)

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Do the evolution.
            networks = optimizer.evolve(networks)

    # Sort our final population.
    networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks.
    print_networks(networks[:5]) 
Example #23
Source File: optimizer_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def testInit(self):
    optimizer.Optimizer(self.codec, self.file_set,
                        cache_class=self.cache_class) 
Example #24
Source File: mjpeg_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_ParametersSet(self):
    codec = mjpeg.MotionJpegCodec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    my_encoder = encoder.Encoder(my_optimizer.context,
        encoder.OptionValueSet(codec.option_set, '-qmin 1 -qmax 2',
                               formatter=codec.option_formatter))
    encoding = my_encoder.Encoding(5000, videofile)
    encoding.Execute()
    self.assertLess(50.0, my_optimizer.Score(encoding)) 
Example #25
Source File: mjpeg_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_ParametersAdjusted(self):
    codec = mjpeg.MotionJpegCodec()
    my_optimizer = optimizer.Optimizer(codec)
    my_encoder = encoder.Encoder(my_optimizer.context,
        encoder.OptionValueSet(codec.option_set, '-qmin 2 -qmax 2',
                               formatter=codec.option_formatter))
    self.assertEquals('2', my_encoder.parameters.GetValue('qmin'))
    self.assertEquals('2', my_encoder.parameters.GetValue('qmax'))
    # qmax is less than qmin. Should be adjusted to be above.
    my_encoder = encoder.Encoder(my_optimizer.context,
        encoder.OptionValueSet(codec.option_set, '-qmin 3 -qmax 2',
                               formatter=codec.option_formatter))
    self.assertEquals('3', my_encoder.parameters.GetValue('qmin'))
    self.assertEquals('3', my_encoder.parameters.GetValue('qmax')) 
Example #26
Source File: file_codec_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_OneBlackFrame(self):
    codec = CopyingCodec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    self.assertTrue(encoding.Result())
    self.assertIn('encode_cputime', encoding.Result())
    self.assertIn('encode_clocktime', encoding.Result())
    self.assertIn('yuv_md5', encoding.Result()) 
Example #27
Source File: file_codec_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_VerifyOneBlackFrame(self):
    codec = CopyingCodec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
      'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    self.assertTrue(encoding.VerifyEncode()) 
Example #28
Source File: file_codec_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_VerifyMd5Varies(self):
    codec = CorruptingCodec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
      'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    first_md5 = encoding.Result()['yuv_md5']
    encoding.Execute()
    self.assertNotEqual(first_md5, encoding.Result()['yuv_md5']) 
Example #29
Source File: file_codec_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_VerifyMatroskaFile(self):
    codec = vp8.Vp8Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # Matroska files will be identical if generated within the same
    # clock second. So wait a bit.
    time.sleep(1)
    self.assertTrue(encoding.VerifyEncode()) 
Example #30
Source File: file_codec_unittest.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def test_MatroskaFrameInfo(self):
    codec = vp8.Vp8Codec()
    my_optimizer = optimizer.Optimizer(codec)
    videofile = test_tools.MakeYuvFileWithOneBlankFrame(
        'one_black_frame_1024_768_30.yuv')
    encoding = my_optimizer.BestEncoding(1000, videofile)
    encoding.Execute()
    # This line comes from file_codec.Execute()
    encodedfile = '%s/%s.%s' % (encoding.Workdir(), videofile.basename,
                                codec.extension)
    frameinfo = file_codec.MatroskaFrameInfo(encodedfile)
    self.assertEquals(len(frameinfo), 1)
    self.assertGreater(os.path.getsize(encodedfile) * 8, frameinfo[0]['size'])