예제 #1
0
def predict_issues(prediction_length, new_input, checkpoint_path):
    # recall model
    model = build_model(period_size=PERIOD_SIZE,
                        output_size=OUTPUT_SIZE,
                        state_size=STATE_SIZE,
                        batch_size=BATCH_SIZE,
                        lstm_size=[STATE_SIZE, STATE_SIZE],
                        dropout_prob=DROPOUT_PROB)

    saver = model['saver']
    prediction = model['preds']
    # recall checkpoints and get predictions
    with tf.Session() as sess:
        saved_path = tf.train.latest_checkpoint(checkpoint_path)
        saver.restore(sess, saved_path)
        predictions = []
        for i in range(prediction_length):
            _prediction = sess.run(prediction,
                                   feed_dict={model['X']: new_input[:, i:]})
            # append prediction to predictions(array -> list)
            predictions.append(int(_prediction.tolist()[0][0]))
            # append array
            new_input = np.append(new_input,
                                  _prediction.astype(np.int16)).reshape(1, -1)
    # reshape predictions
    fn_predictions = np.array(predictions)
    return fn_predictions
예제 #2
0
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")

        self.nets, self.nets_ema = build_model(args)

        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + "_ema", module)

        if args.resume_ckpt is not None:
            print("Transfer learning from", args.resume_ckpt)
            CheckpointIO(
                args.resume_ckpt, **{
                    k: (n.module if isinstance(n, nn.DataParallel) else n)
                    for k, n in self.nets.items()
                }).load(args.resume_ckpt.split("_")[0], restore_D=False
                        )  # no discriminator included in EMA ckpts :(\

        if args.mode == "train":
            self.optims = Munch()
            for net in self.nets.keys():
                if net == "fan":
                    continue
                self.optims[net] = torch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.f_lr if net == "mapping_network" else args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay,
                )

            self.ckptios = [
                CheckpointIO(
                    ospj(args.checkpoint_dir, "{:06d}_nets.ckpt"), **{
                        k: (n.module if isinstance(n, nn.DataParallel) else n)
                        for k, n in self.nets.items()
                    }),
                CheckpointIO(ospj(args.checkpoint_dir, "{:06d}_nets_ema.ckpt"),
                             **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, "{:06d}_optims.ckpt"),
                             **self.optims),
            ]
        else:
            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, "{:06d}_nets_ema.ckpt"),
                             **self.nets_ema)
            ]

        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ("ema" not in name) and ("fan" not in name):
                print("Initializing %s..." % name)
                network.apply(utils.he_init)
예제 #3
0
파일: solver_2.py 프로젝트: paper7745/SOIP
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')

        self.nets, self.nets_ema = build_model(args)
        self.arcface, self.conf = load_arcface()
        self.writer = SummaryWriter('log/test11')
        # print(self.arcface)
        # assert False
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + '_ema', module)

        if args.mode == 'train':
            self.optims = Munch()
            for net in self.nets.keys():
                if net == 'fan':
                    continue
                self.optims[net] = torch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.f_lr if net == 'mapping_network' else args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay)

            # self.ckptios = [
            #     CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets.ckpt'), **self.nets),
            #     CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema),
            #     CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_optims.ckpt'), **self.optims)]
            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, '{}_nets.ckpt'),
                             **self.nets),
                CheckpointIO(ospj(args.checkpoint_dir, '{}_nets_ema.ckpt'),
                             **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, '{}_optims.ckpt'),
                             **self.optims)
            ]
        else:

            self.ckptios = [
                CheckpointIO(
                    ospj(args.checkpoint_dir,
                         '{:06d}_nets_ema.ckpt'.format(100000)),
                    **self.nets_ema)
            ]
            # self.ckptios = [CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema)]

        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ('ema' not in name) and ('fan' not in name):
                print('Initializing %s...' % name)
                network.apply(utils.he_init)
예제 #4
0
def load_model(args):
    _, nets_ema = build_model(args)

    ckptios = [
        CheckpointIO(ospj(args.checkpoint_dir, '{0:06d}_nets_ema.ckpt'),
                     **nets_ema)
    ]  # compatible with Windows
    for ckptio in ckptios:
        ckptio.load(args.resume_iter)

    return nets_ema
예제 #5
0
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')
        self.summary_writer = SummaryWriter('./runs/experiment_1')

        #self.nets, self.nets_ema = build_model(args)
        self.nets = build_model(args)
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)

        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        # for name, module in self.nets_ema.items():
        #     setattr(self, name + '_ema', module)

        if args.mode == 'train':
            self.optims = Munch()
            for net in self.nets.keys():
                if net == 'fan':
                    continue
                self.optims[net] = torch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay)

            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, '{0}_nets.ckpt'),
                             **self.nets),
                #CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, '{0}_optims.ckpt'),
                             **self.optims)
            ]

            #""" load the pretrained checkpoint """
            #self._load_checkpoint(step="", fname='./checkpoints/git_nets_ema.ckpt')

        if args.mode == 'eval':
            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, '{0}_nets.ckpt'),
                             **self.nets),
                #CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema),
                #CheckpointIO(ospj(args.checkpoint_dir, '{0}_optims.ckpt'), **self.optims)]
            ]
        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ('ema' not in name) and ('fan' not in name):
                print('Initializing %s...' % name)
                network.apply(utils.he_init)
예제 #6
0
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        self.nets, self.nets_ema = build_model(args)
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + '_ema', module)

        if args.mode == 'train':
            self.optims = Munch()
            for net in self.nets.keys():
                if net == 'fan':
                    continue
                self.optims[net] = torch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.f_lr if net == 'mapping_network' else args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay)

            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets.ckpt'), **self.nets),
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_optims.ckpt'), **self.optims)]
        else:
            self.ckptios = [CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema)]

        # Multi-gpu Training
        if self.args.gpus != "0" and torch.cuda.is_available():
            self.gpus = self.gpus.split(',')
            self.gpus = [int(i) for i in self.gpus]
            self = torch.nn.DataParallel(self,device_ids=self.gpus)
            """
            self.nets.generator = torch.nn.DataParallel(self.G, device_ids=self.gpus)
            self.nets.generator = torch.nn.DataParallel(self.D, device_ids=self.gpus)
            self.M = torch.nn.DataParallel(self.M, device_ids=self.gpus)
            self.S = torch.nn.DataParallel(self.S, device_ids=self.gpus)
            """

        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ('ema' not in name) and ('fan' not in name):
                print('Initializing %s...' % name)
                network.apply(utils.he_init)
예제 #7
0
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")

        self.nets, self.nets_ema = build_model(args)
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + "_ema", module)

        if args.mode == "train":
            self.optims = Munch()
            for net in self.nets.keys():
                if net == "fan":
                    continue
                self.optims[net] = torch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.f_lr if net == "mapping_network" else args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay,
                )

            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, "{:06d}_nets.ckpt"),
                             **self.nets),
                CheckpointIO(ospj(args.checkpoint_dir, "{:06d}_nets_ema.ckpt"),
                             **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, "{:06d}_optims.ckpt"),
                             **self.optims),
            ]
        else:
            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, "{:06d}_nets_ema.ckpt"),
                             **self.nets_ema)
            ]

        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ("ema" not in name) and ("fan" not in name):
                print("Initializing %s..." % name)
                network.apply(utils.he_init)
예제 #8
0
    def __init__(self, args):
        super().__init__()
        self.args = args

        self.nets, self.nets_ema = build_model(args)
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + '_ema', module)

        if args.mode == 'train':
            place = paddle.fluid.CUDAPlace(
                self.args.whichgpu) if paddle.fluid.is_compiled_with_cuda(
                ) else paddle.fluid.CPUPlace()
            with fluid.dygraph.guard(place):
                self.optims = Munch()
                self.ckptios = Munch()
                for net in self.nets.keys():
                    if net == 'fan':
                        continue
                    self.optims[net] = fluid.optimizer.AdamOptimizer(
                        learning_rate=args.f_lr
                        if net == 'mapping_network' else args.lr,
                        beta1=args.beta1,
                        beta2=args.beta2,
                        parameter_list=self.nets[net].parameters(),
                        regularization=fluid.regularizer.L2Decay(
                            regularization_coeff=args.weight_decay))
                    self.ckptios[net] = [
                        CheckpointIO(
                            ospj(args.checkpoint_dir,
                                 '{:06d}_nets_ema_' + net)),
                        CheckpointIO(
                            ospj(args.checkpoint_dir, '{:06d}_nets_' + net))
                    ]
        else:
            self.ckptios = Munch()
            for net in self.nets.keys():
                self.ckptios[net] = [
                    CheckpointIO(
                        ospj(args.checkpoint_dir, '{:06d}_nets_ema_' + net))
                ]
예제 #9
0
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.device = torch.device('cuda')

        self.nets, self.nets_ema = build_model(args)
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + '_ema', module)

        if args.mode == 'train':
            self.optims = Munch()
            for net in self.nets.keys():
                if net == 'fan':
                    continue
                self.optims[net] = torch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.f_lr if net == 'mapping_network' else args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay)

            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets.ckpt'), **self.nets),
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_optims.ckpt'), **self.optims)]
        else:
            self.ckptios = [CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema)]

        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ('ema' not in name) and ('fan' not in name):
                print('Initializing %s...' % name)
                network.apply(utils.he_init)

        ### modify def sample
        self._load_checkpoint(args.resume_iter)
예제 #10
0
    def __init__(self, args):

        super().__init__()
        self.args = args
        # self.device = porch.device('cuda' if porch.cuda.is_available() else 'cpu')
        print("Solver init....")
        self.nets, self.nets_ema = build_model(args)
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + '_ema', module)

        if args.mode == 'train':
            self.optims = Munch()
            for net in self.nets.keys():
                if net == 'fan':
                    continue
                self.optims[net] = porch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.f_lr if net == 'mapping_network' else args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay)

            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'),
                             **self.nets),
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'),
                             **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_optims.ckpt'),
                             **self.optims)
            ]
        else:
            self.ckptios = [
                CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'),
                             **self.nets_ema)
            ]

        self
예제 #11
0
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        self.nets, self.nets_ema, self.vgg, self.VggExtract = build_model(args)
        self.instancenorm = nn.InstanceNorm2d(512, affine=False)
        self.L1Loss = nn.L1Loss()
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + '_ema', module)

        if args.mode == 'train':
            self.optims = Munch()
            for net in self.nets.keys():
                if net == 'fan':
                    continue
                self.optims[net] = torch.optim.Adam(
                    params=self.nets[net].parameters(),
                    lr=args.f_lr if net == 'mapping_network' else args.lr,
                    betas=[args.beta1, args.beta2],
                    weight_decay=args.weight_decay)

            self.ckptios = [CheckpointIO(ospj(args.checkpoint_dir, '100000_nets.ckpt'), **self.nets),
                CheckpointIO(ospj(args.checkpoint_dir, '100000_nets_ema.ckpt'), **self.nets_ema),
                CheckpointIO(ospj(args.checkpoint_dir, '100000_optims.ckpt'), **self.optims)]
        else:
            self.ckptios = [CheckpointIO(ospj(args.checkpoint_dir, '100000_nets_ema.ckpt'), **self.nets_ema)]

        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ('ema' not in name) and ('fan' not in name):
                print('Initializing %s...' % name)
                network.apply(utils.he_init)
예제 #12
0
파일: solver.py 프로젝트: mrfwn/api-ts-pet
    def __init__(self):
        super().__init__()
        args = resolver_args()
        self.args = args
        
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        self.nets, self.nets_ema = build_model(args)
        # below setattrs are to make networks be children of Solver, e.g., for self.to(self.device)
        for name, module in self.nets.items():
            utils.print_network(module, name)
            setattr(self, name, module)
        for name, module in self.nets_ema.items():
            setattr(self, name + '_ema', module)

    
        self.ckptios = [CheckpointIO(ospj(args.checkpoint_dir, '{:06d}_nets_ema.ckpt'), **self.nets_ema)]

        self.to(self.device)
        for name, network in self.named_children():
            # Do not initialize the FAN parameters
            if ('ema' not in name) and ('fan' not in name):
                print('Initializing %s...' % name)
                network.apply(utils.he_init)
예제 #13
0
tracker = Tracker(domain_data, stories_content)
tracker.convert_stories_content_to_states()
tracker.stories_states_to_training_examples(tracker.stories_states)
training_examples = tracker.training_examples

X = np.array(training_examples["X"])
y = np.array(training_examples["y"])

for i in range(7):
    X = np.concatenate((X, X), axis=0)
    y = np.concatenate((y, y), axis=0)

X, y = shuffle_matrix(X, y)

model = build_model(X, y)

model.fit(X, y, batch_size=128, epochs=50, verbose=1)

model.save("../models/core/core.h5")

model = load_model("../models/core/core.h5")

score, acc = model.evaluate(X, y, batch_size=128)
print('Train score:', score)
print('Train accuracy:', acc)
print("-----")


# state = State(domain_data)
# for i in range(len(X)):