Пример #1
0
 def _basic_cnn(self):
     action, pose, img_in, angles_in = self._inputs()
     net = CNN(filters=[8, 16], fc_layers=[20, 20], kernel_sizes=[3, 3],
               strides=[2, 2], max_pool=False, norm=None, activation='relu')
     outputs = net.forward([img_in, angles_in], [action, pose],
                           training=None)
     return outputs, img_in, angles_in
Пример #2
0
 def _default_evaluation(self, dataset, q_s_size=2, disk_images=True):
     support_size = query_size = q_s_size
     net = CNN(filters=[4, 4, 4, 4],
               fc_layers=[20, 20],
               kernel_sizes=[3, 3, 3, 3],
               strides=[2, 2, 2, 2],
               max_pool=False,
               norm=None,
               activation='relu')
     sequencer = DataSequencer('first_last', dataset.time_horizon)
     eval_cons = EvalConsumer(dataset, sequencer, support_size, disk_images)
     task_emb = TaskEmbedding(network=net,
                              embedding_size=6,
                              support_size=support_size,
                              query_size=query_size,
                              include_state=False,
                              include_action=False)
     ml = MarginLoss(margin=0.1)
     ctr = Control(network=net,
                   action_size=dataset.action_size,
                   include_state=True)
     consumers = [eval_cons, task_emb, ml, ctr]
     p = Pipeline(consumers)
     outs = p.get_outputs()
     return outs, p.get_session()
Пример #3
0
def main(_):
    # preprocess
    conf.observation_dims = eval(conf.observation_dims)

    # start
    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=calc_gpu_fraction(conf.gpu_fraction))

    dataset = data_loader(conf.source_path, conf.target_path)

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        env = Curve()

        pred_network = CNN(sess=sess,
                           observation_dims=conf.observation_dims,
                           name='pred_network',
                           trainable=True)

        policy = Policy(sess=sess,
                        pred_network=pred_network,
                        env=env,
                        dataset=dataset,
                        conf=conf)

        if conf.is_train:
            policy.train()
        else:
            policy.test(conf.test_image_path)
Пример #4
0
 def testCNN(self):
     with tf.Session(config=tf.ConfigProto( )) as sess:
         pred_network = CNN(sess=sess,
                            data_format='NCHW',
                            history_length=4,
                            observation_dims=[80, 80],
                            output_size=18,
                            network_header_type='nature',
                            name='pred_network', trainable=True)
Пример #5
0
    def _norm_cnn(self, norm, scope):
        action, pose, img_in, angles_in = self._inputs()
        net = CNN(filters=[8, 16], fc_layers=[20, 20], kernel_sizes=[3, 3],
                  strides=[2, 2], max_pool=False, norm=norm,
                  activation='relu')

        with tf.variable_scope(scope):
            train = tf.placeholder(tf.bool)
            outputs = net.forward([img_in, angles_in], [action, pose],
                                  training=train)
            sess = tf.InteractiveSession()
            tf.global_variables_initializer().run()
            action, pose = sess.run(
                [outputs['action'], outputs['pose']], feed_dict={
                    img_in.tensor: np.ones((1, 8, 8, 3)),
                    angles_in.tensor: np.ones((1, 2)),
                    train: True
                })
        self.assertEqual(action.shape, (1, 3,))
        self.assertEqual(pose.shape, (1, 2,))
Пример #6
0
 def _check_loss(self, scope, state):
     net = CNN(filters=[8, 16],
               fc_layers=[20, 20],
               kernel_sizes=[3, 3],
               strides=[2, 2],
               max_pool=False,
               norm=None,
               activation='relu')
     c = Control(network=net, action_size=ACTION_SIZE, include_state=state)
     with tf.variable_scope(scope):
         outputs = c.consume(self._fake_tensors())
         sess = tf.InteractiveSession()
         tf.global_variables_initializer().run()
         actions = sess.run(outputs['output_actions'])
     self.assertEqual(actions.shape, (BATCH, 2, ACTION_SIZE))
Пример #7
0
def main(_):
    # preprocess
    conf.observation_dims = eval(conf.observation_dims)

    if conf.use_gpu:
        conf.data_format = 'NCHW'
    else:
        conf.data_format = 'NHWC'

    #  Allow soft placement to occupy as much GPU as needed
    sess_config = tf.ConfigProto(
        log_device_placement=False, allow_soft_placement=True)
    sess_config.gpu_options.allow_growth = True

    with tf.Session(config=sess_config) as sess:
        env = FetchEnvironment(conf.observation_dims)
        testEnv = FetchEnvironment(conf.observation_dims)

        pred_network = CNN(sess=sess,
                           data_format=conf.data_format,
                           observation_dims=conf.observation_dims,
                           name='pred_network', trainable=True,
                           successor=conf.successor)
        target_network = CNN(sess=sess,
                             data_format=conf.data_format,
                             observation_dims=conf.observation_dims,
                             name='target_network', trainable=False,
                             successor=conf.successor)

        if conf.successor:
            agent = DeepSuccessor(sess, pred_network, env, testEnv,
                                  conf, target_network=target_network)

            agent.train(conf.t_train_max)
        else:
            raise NotImplementedError
    def getNetwork(self):
        if self.architecture[0] == 'FNN':
            return FNN(self.input_size, self.hidden_layers, self.num_classes)

        if self.architecture[0] == 'CNN':
            kernels = int(self.architecture[1])
            flip = int(self.architecture[2])
            stride = int(self.architecture[3])
            return CNN(self.batch_size, self.input_size, self.hidden_layers,
                       self.num_classes, kernels, flip, stride,
                       self.context_window)

        if self.architecture[0] == 'LSTM':
            forget = float(self.architecture[1])
            return LSTM(self.input_size, self.hidden_layers, self.num_classes,
                        forget)
Пример #9
0
    def __init__(self,
                 in_channels=1,
                 out_channels=2,
                 feature_maps=64,
                 levels=4,
                 group_norm=False,
                 lambda_rec=0,
                 lambda_dom=0,
                 s=128):
        super(YNetAE, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.feature_maps = feature_maps
        self.levels = levels
        self.group_norm = group_norm
        self.lambda_rec = lambda_rec
        self.lambda_dom = lambda_dom

        # encoder
        self.encoder = UNetEncoder(in_channels=in_channels,
                                   feature_maps=feature_maps,
                                   levels=levels,
                                   group_norm=group_norm)

        # segmentation decoder
        self.segmentation_decoder = UNetDecoder(out_channels=out_channels,
                                                feature_maps=feature_maps,
                                                levels=levels,
                                                group_norm=group_norm)

        # reconstruction decoder
        self.reconstruction_decoder = UNetDecoder(out_channels=in_channels,
                                                  feature_maps=feature_maps,
                                                  levels=levels,
                                                  group_norm=group_norm,
                                                  skip_connections=False)

        # domain classifier on the latent encoding (fixed network architecture)
        n = s // (2**levels)
        fm = feature_maps * (2**levels)
        conv_channels = [48, 48, 48]
        fc_channels = [48, 24, 2]
        self.domain_classifier = CNN(input_size=(fm, n, n),
                                     conv_channels=conv_channels,
                                     fc_channels=fc_channels)
Пример #10
0
 def _check_loss_and_accuracy(self, support, query, scope, state, action):
     net = CNN(filters=[8, 16],
               fc_layers=[20, 20],
               kernel_sizes=[3, 3],
               strides=[2, 2],
               max_pool=False,
               norm=None,
               activation='relu')
     te = TaskEmbedding(network=net,
                        embedding_size=EMB_SIZE,
                        frame_collapse_method='concat',
                        include_state=state,
                        include_action=action)
     with tf.variable_scope(scope):
         outputs = te.consume(self._fake_tensors(support, query))
         sess = tf.InteractiveSession()
         tf.global_variables_initializer().run()
         sup_emb, que_emb = sess.run(
             [outputs['support_embedding'], outputs['query_embedding']])
     self.assertEqual(sup_emb.shape, (BATCH, support, EMB_SIZE))
     self.assertEqual(que_emb.shape, (BATCH, support, EMB_SIZE))
Пример #11
0
    def __init__(self,
                 in_channels=1,
                 out_channels=1,
                 feature_maps=64,
                 levels=4,
                 group_norm=False,
                 lambda_dom=0,
                 s=128):
        super(UNetAE, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.feature_maps = feature_maps
        self.levels = levels
        self.group_norm = group_norm
        self.lambda_dom = lambda_dom

        # contractive path
        self.encoder = UNetEncoder(in_channels,
                                   feature_maps=feature_maps,
                                   levels=levels,
                                   group_norm=group_norm)
        # expansive path
        self.decoder = UNetDecoder(in_channels,
                                   out_channels,
                                   feature_maps=feature_maps,
                                   levels=levels,
                                   skip_connections=False,
                                   group_norm=group_norm)

        # domain classifier on the latent encoding (fixed network architecture)
        n = s // (2**levels)
        fm = feature_maps * (2**levels)
        conv_channels = [48, 48, 48]
        fc_channels = [48, 24, 2]
        self.domain_classifier = CNN(input_size=(fm, n, n),
                                     conv_channels=conv_channels,
                                     fc_channels=fc_channels)
Пример #12
0
    def _default_pipeline(self, dataset, q_s_size=2):
        support_size = query_size = q_s_size
        net = CNN(filters=[4, 4, 4, 4],
                  fc_layers=[20, 20],
                  kernel_sizes=[3, 3, 3, 3],
                  strides=[2, 2, 2, 2],
                  max_pool=False,
                  norm=None,
                  activation='relu')

        sequencer = DataSequencer('first_last', dataset.time_horizon)
        gen = Generator(dataset=dataset,
                        batch_size=4,
                        support_size=support_size,
                        query_size=query_size,
                        data_sequencer=sequencer)

        gen_con = GeneratorConsumer(gen, dataset, support_size, query_size)
        task_emb = TaskEmbedding(network=net,
                                 embedding_size=6,
                                 support_size=support_size,
                                 query_size=query_size,
                                 include_state=False,
                                 include_action=False)
        ml = MarginLoss(margin=0.1)
        ctr = Control(network=net,
                      action_size=dataset.action_size,
                      include_state=True)
        il = ImitationLoss()
        consumers = [gen_con, task_emb, ml, ctr, il]
        p = Pipeline(consumers)
        outputs = p.get_outputs()
        trainer = ILTrainer(pipeline=p,
                            outputs=outputs,
                            generator=gen,
                            iterations=10)
        trainer.train()
Пример #13
0
                                   preprocess=args.preprocess)
test = WeaklyLabeledVolumeDataset(args.data_test,
                                  args.labels_test,
                                  input_shape,
                                  mode='image-level',
                                  transform=test_xtransform,
                                  target_transform=test_ytransform,
                                  preprocess=args.preprocess)
train_loader = DataLoader(train, batch_size=args.train_batch_size)
test_loader = DataLoader(test, batch_size=args.test_batch_size)
"""
    Setup optimization for supervised training
"""
print('[%s] Setting up optimization for supervised training' %
      (datetime.datetime.now()))
net = CNN()
"""
    Train the network supervised
"""
print('[%s] Training network supervised' % (datetime.datetime.now()))
net.train_net(train_loader=train_loader,
              test_loader=test_loader,
              loss_fn=loss_fn_seg,
              lr=args.lr,
              step_size=args.step_size,
              gamma=args.gamma,
              epochs=args.epochs,
              test_freq=args.test_freq,
              print_stats=args.print_stats,
              log_dir=args.log_dir)
"""
Пример #14
0
def main(_):
    # preprocess
    conf.observation_dims = eval(conf.observation_dims)

    for flag in [
            'memory_size', 't_target_q_update_freq', 't_test', 't_ep_end',
            't_train_max', 't_learn_start', 'learning_rate_decay_step'
    ]:
        setattr(conf, flag, getattr(conf, flag) * conf.scale)

    if conf.use_gpu:
        conf.data_format = 'NCHW'
    else:
        conf.data_format = 'NHWC'

    model_dir = get_model_dir(conf, [
        'use_gpu', 'max_random_start', 'n_worker', 'is_train', 'memory_size',
        't_save', 't_train', 'display', 'log_level', 'random_seed', 'tag',
        'scale'
    ])

    # start
    with tf.Session() as sess:
        if 'Corridor' in conf.env_name:
            env = ToyEnvironment(conf.env_name, conf.n_action_repeat,
                                 conf.max_random_start, conf.observation_dims,
                                 conf.data_format, conf.display)
        else:
            env = AtariEnvironment(conf.env_name, conf.n_action_repeat,
                                   conf.max_random_start,
                                   conf.observation_dims, conf.data_format,
                                   conf.display)

        if conf.network_header_type in ['nature', 'nips']:
            pred_network = CNN(sess=sess,
                               data_format=conf.data_format,
                               history_length=conf.history_length,
                               observation_dims=conf.observation_dims,
                               output_size=env.env.action_space.n,
                               network_header_type=conf.network_header_type,
                               name='pred_network',
                               trainable=True)
            target_network = CNN(sess=sess,
                                 data_format=conf.data_format,
                                 history_length=conf.history_length,
                                 observation_dims=conf.observation_dims,
                                 output_size=env.env.action_space.n,
                                 network_header_type=conf.network_header_type,
                                 name='target_network',
                                 trainable=False)
        elif conf.network_header_type == 'mlp':
            pred_network = MLPSmall(
                sess=sess,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.env.action_space.n,
                hidden_activation_fn=tf.sigmoid,
                network_output_type=conf.network_output_type,
                name='pred_network',
                trainable=True)
            target_network = MLPSmall(
                sess=sess,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.env.action_space.n,
                hidden_activation_fn=tf.sigmoid,
                network_output_type=conf.network_output_type,
                name='target_network',
                trainable=False)
        else:
            raise ValueError('Unkown network_header_type: %s' %
                             (conf.network_header_type))

        stat = Statistic(sess, conf.t_test, conf.t_learn_start, model_dir,
                         pred_network.var.values())
        agent = TrainAgent(sess,
                           pred_network,
                           env,
                           stat,
                           conf,
                           target_network=target_network)

        if conf.is_train:
            agent.train(conf.t_train_max)
        else:
            agent.play(conf.ep_end)
Пример #15
0
    data = MilSimReach()
elif FLAGS.dataset == 'sim_push':
    data = MilSimPush()
else:
    raise RuntimeError('Unrecognised dataset.')

loader = saver = None
if FLAGS.save:
    saver = Saver(savedir=FLAGS.logdir)
if FLAGS.load:
    loader = Loader(savedir=FLAGS.logdir, checkpoint=FLAGS.checkpoint_iter)

net = CNN(filters=filters,
          fc_layers=fc_layers,
          kernel_sizes=kernels,
          strides=strides,
          max_pool=FLAGS.max_pool,
          drop_rate=FLAGS.drop_rate,
          norm=FLAGS.norm,
          activation=FLAGS.activation)

sequencer = DataSequencer('first_last', data.time_horizon)
gen = Generator(dataset=data,
                batch_size=FLAGS.batch_size,
                support_size=FLAGS.support,
                query_size=FLAGS.query,
                data_sequencer=sequencer)

generator_consumer = GeneratorConsumer(gen, data, FLAGS.support, FLAGS.query)
task_emb = TaskEmbedding(network=net,
                         embedding_size=FLAGS.embedding,
                         include_state=False,
Пример #16
0
def main(_):
    # preprocess
    conf.observation_dims = eval(conf.observation_dims)

    for flag in [
            'memory_size', 't_target_q_update_freq', 't_test', 't_ep_end',
            't_train_max', 't_learn_start', 'learning_rate_decay_step'
    ]:
        setattr(conf, flag, getattr(conf, flag) * conf.scale)

    if conf.use_gpu:
        conf.data_format = 'NCHW'
    else:
        conf.data_format = 'NHWC'

    model_dir = get_model_dir(conf, [
        'use_gpu', 'max_random_start', 'n_worker', 'is_train', 'memory_size',
        'gpu_fraction', 't_save', 't_train', 'display', 'log_level',
        'random_seed', 'tag', 'scale'
    ])

    # start
    #gpu_options = tf.GPUOptions(
    #        per_process_gpu_memory_fraction=calc_gpu_fraction(conf.gpu_fraction))
    # TODO: just manually set for now
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        if any(name in conf.env_name for name in ['Corridor', 'FrozenLake']):
            env = ToyEnvironment(conf.env_name, conf.n_action_repeat,
                                 conf.max_random_start, conf.observation_dims,
                                 conf.data_format, conf.display)
        else:
            env = AtariEnvironment(conf.env_name, conf.n_action_repeat,
                                   conf.max_random_start,
                                   conf.observation_dims, conf.data_format,
                                   conf.display)

        if conf.network_header_type == 'rnn_cnn':
            pred_network = RNNCNN(sess=sess,
                                  data_format=conf.data_format,
                                  history_length=conf.history_length,
                                  num_steps=conf.num_steps,
                                  num_layers=conf.num_layers,
                                  attention=conf.attention,
                                  observation_dims=conf.observation_dims,
                                  output_size=env.env.action_space.n,
                                  network_header_type=conf.network_header_type,
                                  name='pred_network',
                                  trainable=True)
            target_network = RNNCNN(
                sess=sess,
                data_format=conf.data_format,
                history_length=conf.history_length,
                num_steps=conf.num_steps,
                num_layers=conf.num_layers,
                attention=conf.attention,
                observation_dims=conf.observation_dims,
                output_size=env.env.action_space.n,
                network_header_type=conf.network_header_type,
                name='target_network',
                trainable=False)
        elif conf.network_header_type in ['nature', 'nips']:
            pred_network = CNN(sess=sess,
                               data_format=conf.data_format,
                               history_length=conf.history_length,
                               observation_dims=conf.observation_dims,
                               output_size=env.env.action_space.n,
                               network_header_type=conf.network_header_type,
                               name='pred_network',
                               trainable=True)
            target_network = CNN(sess=sess,
                                 data_format=conf.data_format,
                                 history_length=conf.history_length,
                                 observation_dims=conf.observation_dims,
                                 output_size=env.env.action_space.n,
                                 network_header_type=conf.network_header_type,
                                 name='target_network',
                                 trainable=False)
        elif conf.network_header_type == 'mlp':
            pred_network = MLPSmall(
                sess=sess,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.env.action_space.n,
                hidden_activation_fn=tf.sigmoid,
                network_output_type=conf.network_output_type,
                name='pred_network',
                trainable=True)
            target_network = MLPSmall(
                sess=sess,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.env.action_space.n,
                hidden_activation_fn=tf.sigmoid,
                network_output_type=conf.network_output_type,
                name='target_network',
                trainable=False)
        else:
            raise ValueError('Unkown network_header_type: %s' %
                             (conf.network_header_type))

        stat = Statistic(sess, conf.t_test, conf.t_learn_start, model_dir,
                         pred_network.var.values())
        agent = TrainAgent(sess,
                           pred_network,
                           env,
                           stat,
                           conf,
                           target_network=target_network)

        if conf.is_train:
            agent.train(conf.t_train_max)
        else:
            agent.play(conf.ep_end)
Пример #17
0
    def __init__(self,
                 n,
                 lambdas,
                 in_channels=1,
                 out_channels=2,
                 feature_maps=64,
                 levels=4,
                 group_norm=False):
        super(UNet_DAT, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.feature_maps = feature_maps
        self.levels = levels
        self.lambdas = lambdas

        # encoder
        self.encoder = DATUNetEncoder(lambdas=lambdas,
                                      in_channels=in_channels,
                                      feature_maps=feature_maps,
                                      levels=levels,
                                      group_norm=group_norm)

        # segmentation decoder
        self.segmentation_decoder = DATUNetDecoder(lambdas=lambdas,
                                                   out_channels=out_channels,
                                                   feature_maps=feature_maps,
                                                   levels=levels,
                                                   group_norm=group_norm)

        # domain classifiers
        self.encoder_classifiers = []
        self.decoder_classifiers = []
        for l in range(levels):
            nl = n // 2**l  # size of the data at this level
            fml = feature_maps * 2**l  # number of feature maps at this level
            nb = 2 + (
                levels - l
            )  # number of convolution-pool blocks in the discriminator
            conv_channels = np.ones(nb, dtype=int) * 48
            fc_channels = [
                48, 24, 2
            ]  # number of feature maps in each fully connected layer
            # encoder classifiers
            if lambdas[l] > 0:
                self.encoder_classifiers.append(
                    CNN(input_size=(fml, nl, nl),
                        conv_channels=conv_channels,
                        fc_channels=fc_channels,
                        group_norm=group_norm).cuda())
            else:
                self.encoder_classifiers.append(None)
            # decoder classifiers
            if lambdas[levels + l + 1] > 0:
                self.decoder_classifiers.append(
                    CNN(input_size=(fml, nl, nl),
                        conv_channels=conv_channels,
                        fc_channels=fc_channels,
                        group_norm=group_norm).cuda())
            else:
                self.decoder_classifiers.append(None)
        # encoded classifier
        if lambdas[levels] > 0:
            nl = n // 2**levels  # size of the data at this level
            fml = feature_maps * 2**levels  # number of feature maps at this level
            nb = 2  # number of convolution-pool blocks in the discriminator
            conv_channels = np.ones(nb, dtype=int) * 48
            fc_channels = [
                48, 24, 2
            ]  # number of feature maps in each fully connected layer
            self.encoded_classifier = CNN(input_size=(fml, nl, nl),
                                          conv_channels=conv_channels,
                                          fc_channels=fc_channels,
                                          group_norm=group_norm).cuda()
        else:
            self.encoded_classifier = None
Пример #18
0
def main(_):
    # preprocess
    conf.observation_dims = eval(conf.observation_dims)

    for flag in [
            'memory_size', 't_target_q_update_freq', 't_test', 't_ep_end',
            't_train_max', 't_learn_start', 'learning_rate_decay_step'
    ]:
        setattr(conf, flag, getattr(conf, flag) * conf.scale)

    if conf.use_gpu:
        conf.data_format = 'NCHW'
    else:
        conf.data_format = 'NHWC'

    model_dir = get_model_dir(conf, [
        'use_gpu', 'max_random_start', 'n_worker', 'is_train', 'memory_size',
        'gpu_fraction', 't_save', 't_train', 'display', 'log_level',
        'random_seed', 'tag', 'scale'
    ])

    # start
    #gpu_options = tf.GPUOptions(
    #    per_process_gpu_memory_fraction=calc_gpu_fraction(conf.gpu_fraction))

    sess_config = tf.ConfigProto(
        log_device_placement=False,
        allow_soft_placement=conf.allow_soft_placement)
    sess_config.gpu_options.allow_growth = conf.allow_soft_placement

    with tf.Session(config=sess_config) as sess:

        if any(name in conf.env_name for name in ['Corridor', 'FrozenLake']):
            env = ToyEnvironment(conf.env_name, conf.n_action_repeat,
                                 conf.max_random_start, conf.observation_dims,
                                 conf.data_format, conf.display,
                                 conf.use_cumulated_reward)
        else:
            env = AtariEnvironment(conf.env_name, conf.n_action_repeat,
                                   conf.max_random_start,
                                   conf.observation_dims, conf.data_format,
                                   conf.display, conf.use_cumulated_reward)

        if conf.network_header_type in ['nature', 'nips']:
            pred_network = CNN(sess=sess,
                               data_format=conf.data_format,
                               history_length=conf.history_length,
                               observation_dims=conf.observation_dims,
                               output_size=env.env.action_space.n,
                               network_header_type=conf.network_header_type,
                               name='pred_network',
                               trainable=True)
            target_network = CNN(sess=sess,
                                 data_format=conf.data_format,
                                 history_length=conf.history_length,
                                 observation_dims=conf.observation_dims,
                                 output_size=env.env.action_space.n,
                                 network_header_type=conf.network_header_type,
                                 name='target_network',
                                 trainable=False)
        elif conf.network_header_type == 'mlp':
            pred_network = MLPSmall(
                sess=sess,
                data_format=conf.data_format,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.env.action_space.n,
                hidden_activation_fn=tf.sigmoid,
                network_output_type=conf.network_output_type,
                name='pred_network',
                trainable=True)
            target_network = MLPSmall(
                sess=sess,
                data_format=conf.data_format,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.env.action_space.n,
                hidden_activation_fn=tf.sigmoid,
                network_output_type=conf.network_output_type,
                name='target_network',
                trainable=False)
        else:
            raise ValueError('Unkown network_header_type: %s' %
                             (conf.network_header_type))

        stat = Statistic(sess, conf.t_test, conf.t_learn_start, model_dir,
                         pred_network.var.values())
        agent = TrainAgent(sess,
                           pred_network,
                           env,
                           stat,
                           conf,
                           target_network=target_network)

        if conf.is_train:
            agent.train(conf.t_train_max)
        else:
            agent.play(conf.ep_end)

        merged_summary = tf.summary.merge_all()
        file_writer = tf.summary.FileWriter("tensorboardLogs", sess.graph)
### main function ###
if __name__ == '__main__':
    args = parse().__dict__
    data_path = args['data_path']
    csv_path = args['csv_path']

    train_data = KAISD_data(data_path, csv_path)
    train_loader = DataLoader(train_data, batch_size=16, shuffle=True)

    if torch.cuda.is_available():
        device = torch.device('cuda:0')
    else:
        device = torch.device('cpu')

    ### load the CNN ###
    net = CNN(input_shape=(1, 128, 345))

    # ### Define loss function and optimizer ###
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

    # ### Training the Network ###
    losses = []
    pixel_accs = []

    for epoch in tqdm(range(20)):
        net.train()
        for batch_idx, (audio, label) in enumerate(train_loader):
            audio = audio.to(device, dtype=torch.float32)
            label = label.to(device, dtype=torch.long)
            output = net(audio)
Пример #20
0
def main(_):
    # preprocess
    conf.observation_dims = eval(conf.observation_dims)

    for flag in [
            'memory_size', 't_target_q_update_freq', 't_test', 't_ep_end',
            't_train_max', 't_learn_start', 'learning_rate_decay_step'
    ]:
        setattr(conf, flag, getattr(conf, flag) * conf.scale)

    if conf.use_gpu:
        conf.data_format = 'NCHW'
    else:
        conf.data_format = 'NHWC'

    model_dir = get_model_dir(conf, [
        'use_gpu', 'max_random_start', 'n_worker', 'is_train', 'memory_size',
        'gpu_fraction', 't_save', 't_train', 'display', 'log_level',
        'random_seed', 'tag', 'scale'
    ])

    # start
    gpu_options = tensor.GPUOptions(
        per_process_gpu_memory_fraction=calc_gpu_fraction(conf.gpu_fraction))

    with tensor.Session(config=tensor.ConfigProto(
            gpu_options=gpu_options)) as sess:
        env = StageEnvironment(conf.max_random_start, conf.observation_dims,
                               conf.data_format, conf.display,
                               conf.use_cumulated_reward)

        if conf.network_header_type in ['nature', 'nips']:
            pred_network = CNN(sess=sess,
                               data_format=conf.data_format,
                               history_length=conf.history_length,
                               observation_dims=conf.observation_dims,
                               output_size=env.action_size,
                               network_header_type=conf.network_header_type,
                               name='pred_network',
                               trainable=True)
            target_network = CNN(sess=sess,
                                 data_format=conf.data_format,
                                 history_length=conf.history_length,
                                 observation_dims=conf.observation_dims,
                                 output_size=env.action_size,
                                 network_header_type=conf.network_header_type,
                                 name='target_network',
                                 trainable=False)
        elif conf.network_header_type == 'mlp':
            pred_network = MLPSmall(
                sess=sess,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.action_size,
                hidden_activation_fn=tensor.sigmoid,
                network_output_type=conf.network_output_type,
                name='pred_network',
                trainable=True)
            target_network = MLPSmall(
                sess=sess,
                observation_dims=conf.observation_dims,
                history_length=conf.history_length,
                output_size=env.action_size,
                hidden_activation_fn=tensor.sigmoid,
                network_output_type=conf.network_output_type,
                name='target_network',
                trainable=False)
        else:
            raise ValueError('Unkown network_header_type: %s' %
                             (conf.network_header_type))

        stat = Statistic(sess, conf.t_test, conf.t_learn_start, model_dir,
                         pred_network.var.values())
        agent = TrainAgent(sess,
                           pred_network,
                           env,
                           stat,
                           conf,
                           target_network=target_network)

        if conf.is_train:
            agent.train(conf.t_train_max)
        else:
            agent.play(conf.ep_end)
Пример #21
0
        train = EMBLERPixelTrainDataset(input_shape=input_shape,
                                        transform=train_xtransform,
                                        target_transform=train_ytransform,
                                        n_samples=args.n_samples)
        test = EMBLERPixelTestDataset(input_shape=input_shape,
                                      transform=test_xtransform,
                                      target_transform=test_ytransform)
train_loader = DataLoader(train, batch_size=args.train_batch_size)
test_loader = DataLoader(test, batch_size=args.test_batch_size)
"""
    Setup optimization for finetuning
"""
print('[%s] Setting up optimization for finetuning' %
      (datetime.datetime.now()))
# load best checkpoint
net = CNN()
cam_net = ModelCAM(net.encoder, net.encoder[-1].out_channels)
optimizer = optim.Adam(net.parameters(), lr=args.lr)
scheduler = optim.lr_scheduler.StepLR(optimizer,
                                      step_size=args.step_size,
                                      gamma=args.gamma)
"""
    Train the network
"""
print('[%s] Training network' % (datetime.datetime.now()))
cam_net.train_net(train_loader=train_loader,
                  test_loader=test_loader,
                  loss_fn=loss_fn_seg,
                  optimizer=optimizer,
                  scheduler=scheduler,
                  epochs=args.epochs,