예제 #1
0
def do_train(config):
    paddle.set_device("gpu" if config.n_gpu else "cpu")
    if paddle.distributed.get_world_size() > 1:
        paddle.distributed.init_parallel_env()
    set_seed(config)

    base_graph, term_ids = load_data(config.graph_work_path)
    collate_fn = partial(
        batch_fn,
        samples=config.samples,
        base_graph=base_graph,
        term_ids=term_ids)

    mode = 'train'
    train_ds = TrainData(config.graph_work_path)
    model = ErnieSageForLinkPrediction.from_pretrained(
        config.model_name_or_path, config=config)
    model = paddle.DataParallel(model)

    train_loader = GraphDataLoader(
        train_ds,
        batch_size=config.batch_size,
        shuffle=True,
        num_workers=config.sample_workers,
        collate_fn=collate_fn)

    optimizer = paddle.optimizer.Adam(
        learning_rate=config.lr, parameters=model.parameters())

    global_step = 0
    tic_train = time.time()
    for epoch in range(config.epoch):
        for step, (graphs, datas) in enumerate(train_loader):
            global_step += 1
            loss, outputs = model(graphs, datas)
            if global_step % config.log_per_step == 0:
                logger.info(
                    "global step %d, epoch: %d, batch: %d, loss: %f, speed: %.2f step/s"
                    % (global_step, epoch, step, loss,
                       config.log_per_step / (time.time() - tic_train)))
                tic_train = time.time()
            loss.backward()
            optimizer.step()
            optimizer.clear_grad()
            if global_step % config.save_per_step == 0:
                if (not config.n_gpu > 1) or paddle.distributed.get_rank() == 0:
                    output_dir = os.path.join(config.output_path,
                                              "model_%d" % global_step)
                    if not os.path.exists(output_dir):
                        os.makedirs(output_dir)
                    model._layers.save_pretrained(output_dir)
    if (not config.n_gpu > 1) or paddle.distributed.get_rank() == 0:
        output_dir = os.path.join(config.output_path, "last")
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        model._layers.save_pretrained(output_dir)
예제 #2
0
def train(**kwargs):

    # attributes
    for k, v in kwargs.items():
        setattr(opt, k, v)

    device = torch.device(
        'cuda') if torch.cuda.is_available() else torch.device('cpu')
    torch.backends.cudnn.enabled = False

    # dataset
    if opt.chinese:
        mydata = TrainData(opt.chinese_data_path, opt.conversation_path,
                           opt.chinese_results_path, opt.chinese, opt.fb,
                           opt.prev_sent, True)
    else:
        mydata = TrainData(opt.data_path, opt.conversation_path,
                           opt.results_path, opt.chinese, opt.fb,
                           opt.prev_sent, True)

    # models
    if opt.attn:
        seq2seq = NewSeq2seqAttention(num_tokens=mydata.data.num_tokens,
                                      opt=opt,
                                      sos_id=mydata.data.word2id["<START>"])

        if opt.model_attention_path:
            seq2seq.load_state_dict(
                torch.load(opt.model_attention_path, map_location="cpu"))
            print("Pretrained model has been loaded.\n")
    else:
        seq2seq = NewSeq2seq(num_tokens=mydata.data.num_tokens,
                             opt=opt,
                             sos_id=mydata.data.word2id["<START>"])

        if opt.chinese:
            if opt.chinese_model_path:
                seq2seq.load_state_dict(
                    torch.load(opt.chinese_model_path, map_location="cpu"))
                print("Pretrained model has been loaded.\n")
        else:
            if opt.model_path:
                seq2seq.load_state_dict(
                    torch.load(opt.model_path, map_location="cpu"))
                print("Pretrained model has been loaded.\n")

    seq2seq = seq2seq.to(device)

    #=============================================================#

    optimizer = RMSprop(seq2seq.parameters(), lr=opt.learning_rate)
    criterion = nn.CrossEntropyLoss().to(device)

    for epoch in range(opt.epochs):
        print("epoch %d:" % epoch)
        mini_batches = mydata._mini_batches(opt.batch_size)

        for ii, (ib, tb) in enumerate(mini_batches):

            ib = ib.to(device)
            tb = tb.to(device)

            optimizer.zero_grad()
            decoder_outputs, decoder_hidden1, decoder_hidden2 = seq2seq(ib, tb)

            # Its own last output
            a = []
            b = []
            for t in range(opt.mxlen):
                _, indices = torch.topk(decoder_outputs[t][0], 1)
                a.append(mydata.data.id2word[ib[t][0].item()])
                b.append(mydata.data.id2word[indices[0].item()])
            print(a)
            print(b)

            # Reshape the outputs
            b = decoder_outputs.size(1)
            t = decoder_outputs.size(0)
            targets = Variable(torch.zeros(t, b)).to(
                device)  # (time_steps,batch_size)
            targets[:-1, :] = tb[1:, :]

            targets = targets.contiguous().view(-1)  # (time_steps*batch_size)
            decoder_outputs = decoder_outputs.view(
                b * t, -1)  # S = (time_steps*batch_size) x V
            loss = criterion(decoder_outputs, targets.long())

            if ii % 1 == 0:
                print("Current Loss:", loss.data.item())

            loss.backward()
            optimizer.step()
        if opt.chinese:
            save_path = "checkpoints/chinese-epoch-%s.pth" % epoch
        else:
            save_path = "checkpoints/epoch-%s.pth" % epoch

        torch.save(seq2seq.state_dict(), save_path)
예제 #3
0
def test(**kwargs):

    # attributes
    for k, v in kwargs.items():
        setattr(opt, k, v)

    device = torch.device(
        'cuda') if torch.cuda.is_available() else torch.device('cpu')
    torch.backends.cudnn.enabled = False

    prev_sentence = ""

    while True:

        data = input('You say: ')
        if data == "exit":
            break
        if opt.prev_sent == 2:
            data = (prev_sentence + data) if not opt.chinese else data

        # Dataset
        if opt.chinese:
            data = list(convert(data, 't2s'))
            mydata = TrainData(opt.chinese_data_path, opt.conversation_path,
                               opt.chinese_results_path, opt.chinese, opt.fb,
                               opt.prev_sent, True)
        else:
            data = ' '.join(data.split(' '))
            mydata = TrainData(opt.data_path, opt.conversation_path,
                               opt.results_path, opt.chinese, opt.fb,
                               opt.prev_sent, True)

        # models
        if opt.attn:
            seq2seq = NewSeq2seqAttention(
                num_tokens=mydata.data.num_tokens,
                opt=opt,
                sos_id=mydata.data.word2id["<START>"])
            if opt.model_attention_path:
                seq2seq.load_state_dict(
                    torch.load(opt.model_attention_path, map_location="cpu"))

        elif opt.rl:
            seq2seq = NewSeq2seqRL(num_tokens=mydata.data.num_tokens,
                                   opt=opt,
                                   sos_id=mydata.data.word2id["<START>"])
            if opt.model_rl_path:
                seq2seq.load_state_dict(
                    torch.load(opt.model_rl_path, map_location="cpu"))
            seq2seq = seq2seq.to(opt.device)

        else:
            seq2seq = NewSeq2seq(num_tokens=mydata.data.num_tokens,
                                 opt=opt,
                                 sos_id=mydata.data.word2id["<START>"])

            if opt.chinese:
                if opt.chinese_model_path:
                    seq2seq.load_state_dict(
                        torch.load(opt.chinese_model_path, map_location="cpu"))
            else:
                if opt.model_path:
                    seq2seq.load_state_dict(
                        torch.load(opt.model_path, map_location="cpu"))

        seq2seq = seq2seq.to(device)

        # Predict
        encoder_data = mydata._test_batch(
            data, 2 * opt.mxlen if not opt.chinese else opt.mxlen).to(device)
        # encoder_data = torch.LongTensor([[  1,  30, 112,  10,   3,   2,   1, 645, 131,   7,  25,   7, 146, 584,
        # 871, 207,  16,   3,   2,   0,   0,   0,   0,   0,   0,   0,   0,   0,
        #   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]]).transpose(1,0)
        decoded_indices, decoder_hidden1, decoder_hidden2 = seq2seq.evaluation(
            encoder_data)

        toks_to_replace = {
            "i": "I",
            "im": "I'm",
            "id": "I'd",
            "ill": "I'll",
            "iv": "I'v",
            "hes": "he's",
            "shes": "she's",
            "youre": "you're",
            "its": "it's",
            "dont": "don't",
            "youd": "you'd",
            "cant": "can't",
            "thats": "that's",
            "isnt": "isn't",
            "didnt": "didn't",
            "hows": "how's",
            "ive": "I've"
        }

        decoded_sequence = ""
        for idx in decoded_indices:
            idx = idx.item() if opt.rl else idx
            sampled_tok = mydata.data.id2word[idx]
            if sampled_tok == "<START>":
                continue
            elif sampled_tok == "<EOS>":
                break
            else:
                if not opt.chinese:
                    if sampled_tok in toks_to_replace:
                        sampled_tok = toks_to_replace[sampled_tok]
                    decoded_sequence += sampled_tok + ' '
                else:
                    decoded_sequence += sampled_tok

        print("WayneBot:",decoded_sequence if not opt.chinese \
            else convert(decoded_sequence,'s2t').replace("雞仔","我").replace("主人","陛下").replace("主子","陛下"))
        prev_sentence = decoded_sequence
예제 #4
0
파일: train.py 프로젝트: Rancho86/deepgmr
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--n_epochs', type=int, default=100)
    parser.add_argument('--log_freq', type=int, default=30)
    parser.add_argument('--plot_freq', type=int, default=250)
    parser.add_argument('--save_freq', type=int, default=10)
    # eval setting
    parser.add_argument('--val_fraction', type=float, default=0.1)
    parser.add_argument('--eval_batch_size', type=int, default=32)
    parser.add_argument('--eval_plot_freq', type=int, default=10)
    args = parser.parse_args()

    model = DeepGMR(args)
    if torch.cuda.is_available():
        model.cuda()

    data = TrainData(args.data_file, args)
    ids = np.random.permutation(len(data))
    n_val = int(args.val_fraction * len(data))
    train_data = Subset(data, ids[n_val:])
    valid_data = Subset(data, ids[:n_val])

    train_loader = DataLoader(train_data,
                              args.batch_size,
                              drop_last=True,
                              shuffle=True)
    valid_loader = DataLoader(valid_data, args.eval_batch_size, drop_last=True)

    optimizer = torch.optim.Adam(model.parameters(), args.lr)
    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                           factor=0.5,
                                                           min_lr=1e-6)
예제 #5
0
def train_RL():

    torch.backends.cudnn.enabled = False

    dull_set = [
        "I don't know what you're talking about.", "I don't know.",
        "You don't know.", "You know what I mean.", "I know what you mean.",
        "You know what I'm saying.", "You don't know anything."
    ]
    ones_reward = torch.ones(opt.mxlen).to(opt.device)

    # dataset
    mydata = TrainData(opt.data_path,
                       opt.conversation_path,
                       opt.results_path,
                       chinese=False,
                       fb=False,
                       prev_sent=opt.prev_sent)

    # Ease of answering data
    dull_target_set = make_batch(mydata.data, dull_set, opt.mxlen)
    dull_target_set = dull_target_set.permute(1, 0)
    dull_target_data = Variable(torch.LongTensor(opt.batch_size,
                                                 opt.mxlen)).to(opt.device)
    for i in range(opt.batch_size):
        dull_target_data[i] = dull_target_set[np.random.randint(len(dull_set))]
    dull_target_data = dull_target_data.permute(1, 0)

    # models
    # 1. RL model
    seq2seq_rl = NewSeq2seqRL(num_tokens=mydata.data.num_tokens,
                              opt=opt,
                              sos_id=mydata.data.word2id["<START>"])
    if opt.model_rl_path:
        seq2seq_rl.load_state_dict(
            torch.load(opt.model_rl_path, map_location="cpu"))
        print("Pretrained RL model has been loaded.")
    seq2seq_rl = seq2seq_rl.to(opt.device)

    # 2. Normal model
    seq2seq_normal = NewSeq2seq(num_tokens=mydata.data.num_tokens,
                                opt=opt,
                                sos_id=mydata.data.word2id["<START>"])
    if opt.model_path:
        seq2seq_normal.load_state_dict(
            torch.load(opt.model_path, map_location="cpu"))
        print("Pretrained Normal model has been loaded.")
    seq2seq_normal = seq2seq_normal.to(opt.device)

    #=============================================================#

    optimizer = RMSprop(seq2seq_rl.parameters(), lr=opt.learning_rate)
    criterion = nn.CrossEntropyLoss()

    for epoch in range(opt.epochs):
        print("epoch %d:" % epoch)
        mini_batches = mydata._mini_batches(opt.batch_size)

        for ii, (ib, tb) in enumerate(mini_batches):

            # Skip the last batch to avoid error
            if ib.size(1) != opt.batch_size:
                continue

            ib = ib.to(opt.device)
            tb = tb.to(opt.device)

            optimizer.zero_grad()

            # First evaluate an output
            action_words, _, _ = seq2seq_rl.evaluation(ib)

            # Ease of answering data
            dull_outputs, dull_loss, dull_entropies = seq2seq_rl(
                action_words, dull_target_data, ones_reward)

            # Semantic Coherence: Forward
            forward_outputs, forward_loss, forward_entropies = seq2seq_rl(
                ib, action_words, ones_reward)

            # Semantic Coherence: Backward
            backward_outputs, _, _ = seq2seq_normal(action_words,
                                                    ib[:opt.mxlen])
            backward_targets = ib[:opt.mxlen]  # (time_steps, batch_size)
            backward_entropies = []
            for t in range(opt.mxlen):
                backward_loss = criterion(backward_outputs[t],
                                          backward_targets[t].long())
                backward_entropies.append(backward_loss)

            rewards = count_rewards(dull_entropies, forward_entropies,
                                    backward_entropies)

            # Add rewards to train the data
            decoder_outputs, pg_loss, entropies = seq2seq_rl(ib, tb, rewards)

            if ii % 1 == 0:
                print("Current Loss:", pg_loss.data.item())

            pg_loss.backward()
            optimizer.step()

            save_path = "checkpoints/rl-epoch-%s.pth" % epoch
            if (epoch + 1) % 10 == 0:
                torch.save(seq2seq_rl.state_dict(), save_path)
예제 #6
0
 def __init__(self, confPath=""):
     Network.__init__(self, confPath)
     self.data = TrainData(
         self.conf, self.conf["epochWalked"] / self.conf["updateEpoch"])
     self.data.check_data()
     self.GPU0 = '0'
예제 #7
0
class GANArtery(Network):
    def __init__(self, confPath=""):
        Network.__init__(self, confPath)
        self.data = TrainData(
            self.conf, self.conf["epochWalked"] / self.conf["updateEpoch"])
        self.data.check_data()
        self.GPU0 = '0'

    def ae_u(self, X, training, batch_size, threshold):
        original = self.conf["network"]["generatorOriginSize"]
        growth = self.conf["network"]["denseBlockGrowth"]
        dense_layer_num = self.conf["network"]["denseBlockDepth"]
        X_input = self.Input(X, "input", batch_size, original, training)
        down_1 = self.Down_Sample(X_input, "down_sample_1", 2, training,
                                  original * 1)
        dense_1 = self.Dense_Block(down_1, "dense_block_1", dense_layer_num,
                                   growth, training)
        down_2 = self.Down_Sample(dense_1, "down_sample_2", 2, training,
                                  original * 2)
        dense_2 = self.Dense_Block(down_2, "dense_block_2", dense_layer_num,
                                   growth, training)
        down_3 = self.Down_Sample(dense_2, "down_sample_3", 2, training,
                                  original * 4)

        dense_3 = self.Dense_Block(down_3, "dense_block_3", dense_layer_num,
                                   growth, training)
        mid_input = self.Concat([
            dense_3,
            self.Down_Sample(dense_2, "cross_1", 2, training, original),
            self.Down_Sample(dense_1, "cross_2", 4, training, original),
            self.Down_Sample(X_input, "cross_3", 8, training, original),
        ],
                                axis=4,
                                size=original * 6,
                                name="concat_up_mid")
        dense_4 = self.Dense_Block(mid_input, "dense_block_4", dense_layer_num,
                                   growth, training)

        up_input_1 = self.Concat([down_3, dense_4],
                                 axis=4,
                                 size=original * 8,
                                 name="up_input_1")
        up_1 = self.Up_Sample(up_input_1, "up_sample_1", 2, training,
                              original * 4)

        dense_input_5 = self.Concat([up_1, dense_2],
                                    axis=4,
                                    size=original * 4,
                                    name="dense_input_5")
        dense_5 = self.Dense_Block(dense_input_5, "dense_block_5",
                                   dense_layer_num, growth, training)

        up_input_2 = self.Concat([dense_5, down_2],
                                 axis=4,
                                 size=original * 6,
                                 name="up_input_2")
        up_2 = self.Up_Sample(up_input_2, "up_sample_2", 2, training,
                              original * 2)

        dense_input_6 = self.Concat([up_2, dense_1],
                                    axis=4,
                                    size=original * 2,
                                    name="dense_input_6")
        dense_6 = self.Dense_Block(dense_input_6, "dense_block_6",
                                   dense_layer_num, growth, training)

        up_input_3 = self.Concat([dense_6, down_1],
                                 axis=4,
                                 size=original * 6,
                                 name="up_input_3")
        up_3 = self.Up_Sample(up_input_3, "up_sample_3", 2, training,
                              original * 1)

        predict_input = self.Concat([
            up_3,
            self.Up_Sample(dense_6, "cross_4", 2, training, original),
            self.Up_Sample(up_2, "cross_5", 2, training, original),
            self.Up_Sample(dense_5, "cross_6", 4, training, original),
            self.Up_Sample(up_1, "cross_7", 4, training, original),
            self.Up_Sample(dense_4, "cross_8", 8, training, original),
            self.Up_Sample(mid_input, "cross_9", 8, training, original),
            self.Up_Sample(dense_3, "cross_10", 8, training, original)
        ],
                                    axis=4,
                                    size=original * 4,
                                    name="predict_input")
        vox_sig, vox_sig_modified, vox_no_sig = self.Predict(
            predict_input, "predict", training, threshold)

        return vox_sig, vox_sig_modified, vox_no_sig

    def dis(self, X, Y, training):
        with tf.variable_scope("input"):
            X = tf.reshape(X, [
                self.batch_size, self.blockShape[0], self.blockShape[1],
                self.blockShape[2], 1
            ])
            Y = tf.reshape(Y, [
                self.batch_size, self.blockShape[0], self.blockShape[1],
                self.blockShape[2], 1
            ])
            # layer = tf.concat([X, Y], axis=4)
            layer = X * Y
            c_d = [1, 2, 64, 128, 256, 512]
            s_d = [0, 2, 2, 2, 2, 2]
            layers_d = []
            layers_d.append(layer)
        with tf.variable_scope("down_sample"):
            for i in range(1, 6, 1):
                layer = tools.Ops.conv3d(layers_d[-1],
                                         k=4,
                                         out_c=c_d[i],
                                         str=s_d[i],
                                         name='d_1' + str(i))
                if i != 5:
                    layer = tools.Ops.xxlu(layer, name='lrelu')
                    # batch normal layer
                    layer = tools.Ops.batch_norm(layer,
                                                 'bn_up' + str(i),
                                                 training=training)
                layers_d.append(layer)
        with tf.variable_scope("flating"):
            y = tf.reshape(layers_d[-1], [self.batch_size, -1])
        return tf.nn.sigmoid(y)


# net = GAN("./conf.json")
# print(type(net))
예제 #8
0
class GAN(Network):
    def __init__(self, confPath=""):
        Network.__init__(self, confPath)
        self.data = TrainData(
            self.conf, self.conf["epochWalked"] / self.conf["updateEpoch"])
        self.data.check_data()

    def ae_u(self, X, training, batch_size, threshold):
        original = self.conf["network"]["generatorOriginSize"]
        growth = self.conf["network"]["denseBlockGrowth"]
        dense_layer_num = self.conf["network"]["denseBlockDepth"]
        # input layer
        X = tf.reshape(X, [
            batch_size, self.blockShape[0], self.blockShape[1],
            self.blockShape[2], 1
        ])
        # image reduce layer
        conv_input_1 = tools.Ops.conv3d(X,
                                        k=3,
                                        out_c=original,
                                        str=2,
                                        name='conv_input_down')
        conv_input_normed = tools.Ops.batch_norm(conv_input_1,
                                                 'bn_dense_0_0',
                                                 training=training)
        conv_input_relu = tools.Ops.xxlu(conv_input_normed)
        conv_input_conv1 = tools.Ops.conv3d(conv_input_relu,
                                            k=2,
                                            out_c=original,
                                            str=1,
                                            name='conv_input_conv1')
        conv_input_conv2 = tools.Ops.conv3d(conv_input_conv1,
                                            k=2,
                                            out_c=original,
                                            str=1,
                                            name='conv_input_conv2')
        conv_input_conv3 = tools.Ops.conv3d(conv_input_conv2,
                                            k=2,
                                            out_c=original,
                                            str=1,
                                            name='conv_input_conv3')
        # network start
        conv_input = tools.Ops.conv3d(conv_input_conv3,
                                      k=2,
                                      out_c=original * 2,
                                      str=2,
                                      name='conv_input')
        conv_input = tf.reshape(conv_input, [
            self.batch_size, self.blockShape[0] / 4, self.blockShape[1] / 4,
            original * 2
        ],
                                name="reshape_1")
        with tf.variable_scope("dense_part_1"):
            ##### dense block 1
            c_e = []
            s_e = []
            layers_1 = []
            layers_1.append(conv_input)
            for i in range(dense_layer_num):
                c_e.append(original + growth * (i + 1))
                s_e.append(1)
            for j in range(dense_layer_num):
                layer = tools.Ops.batch_norm(layers_1[-1],
                                             'bn_dense_1_' + str(j),
                                             training=training)
                layer = tools.Ops.xxlu(layer, name="relu_" + str(j))
                layer = tools.Ops.conv2d(layer,
                                         k=3,
                                         out_c=growth,
                                         str=s_e[j],
                                         name='dense_1_' + str(j))
                next_input = tf.concat([layer, layers_1[-1]], axis=3)
                layers_1.append(next_input)

        with tf.variable_scope("middle_down_sample"):
            mid_layer = tools.Ops.batch_norm(layers_1[-1],
                                             'bn_mid',
                                             training=training)
            mid_layer = tools.Ops.xxlu(mid_layer, name='lrelu')
            mid_layer = tools.Ops.conv2d(mid_layer,
                                         k=3,
                                         out_c=original +
                                         growth * dense_layer_num,
                                         str=1,
                                         name='mid_conv')
            mid_layer_down = tools.Ops.conv2d(mid_layer,
                                              k=2,
                                              out_c=original +
                                              growth * dense_layer_num,
                                              str=2,
                                              name='mid_down')
            # mid_layer_down = tools.Ops.maxpool3d(mid_layer,k=2,s=2,pad='SAME')

        ##### dense block
        with tf.variable_scope("dense_part_2"):
            # lfc = tools.Ops.xxlu(tools.Ops.fc(lfc, out_d=d1 * d2 * d3 * cc, name='fc2'),name='relu')
            # lfc = tf.reshape(lfc, [bat, d1, d2, d3, cc])

            c_d = []
            s_d = []
            layers_2 = []
            layers_2.append(mid_layer_down)
            for i in range(dense_layer_num):
                c_d.append(original + growth * (dense_layer_num + i + 1))
                s_d.append(1)
            for j in range(dense_layer_num):
                layer = tools.Ops.batch_norm(layers_2[-1],
                                             'bn_dense_2_' + str(j),
                                             training=training)
                layer = tools.Ops.xxlu(layer, name="relu_" + str(j))
                layer = tools.Ops.conv2d(layer,
                                         k=3,
                                         out_c=growth,
                                         str=s_d[j],
                                         name='dense_2_' + str(j))
                next_input = tf.concat([layer, layers_2[-1]], axis=3)
                layers_2.append(next_input)

        with tf.variable_scope("up_sampling1"):
            bn_1 = tools.Ops.batch_norm(layers_2[-1],
                                        'bn_after_dense',
                                        training=training)
            relu_1 = tools.Ops.xxlu(bn_1, name='relu_after_dense')
            deconv_1 = tools.Ops.deconv2d(relu_1,
                                          k=2,
                                          out_c=128,
                                          str=2,
                                          name='deconv_up_sample_1')

            bn_middle = tools.Ops.batch_norm(deconv_1,
                                             'bn_middle',
                                             training=training)
            relu_middle = tools.Ops.xxlu(bn_middle, name='relu_middle')
            conv_middle1 = tools.Ops.conv2d(relu_middle,
                                            k=3,
                                            out_c=original +
                                            growth * dense_layer_num * 2,
                                            str=1,
                                            name='conv_middle1')
            conv_middle2 = tools.Ops.conv2d(conv_middle1,
                                            k=3,
                                            out_c=original +
                                            growth * dense_layer_num * 2,
                                            str=1,
                                            name='conv_up_sample_2')
            conv_middle3 = tools.Ops.conv2d(conv_middle2,
                                            k=3,
                                            out_c=original +
                                            growth * dense_layer_num,
                                            str=1,
                                            name='conv_up_sample_3')

        with tf.variable_scope("up_sampling2"):
            # concat_up_1 = tf.concat([conv_middle3,mid_layer],axis=3)
            concat_up_1 = conv_middle3 + mid_layer
            concat_up_1 = tf.reshape(concat_up_1, [
                self.batch_size, self.blockShape[0] / 4,
                self.blockShape[1] / 4, 1,
                concat_up_1.get_shape()[3]
            ],
                                     name="reshape_2")
            deconv_2 = tools.Ops.deconv3d(concat_up_1,
                                          k=3,
                                          out_c=64,
                                          str=2,
                                          name='deconv_up_sample_2')

            bn_middle2 = tools.Ops.batch_norm(deconv_2,
                                              'bn_middle2',
                                              training=training)
            relu_middle2 = tools.Ops.xxlu(bn_middle2, "relu_middle2")
            conv_middle4 = tools.Ops.conv3d(relu_middle2,
                                            k=3,
                                            out_c=64,
                                            str=1,
                                            name='conv_middle4')
            conv_middle5 = tools.Ops.conv3d(conv_middle4,
                                            k=3,
                                            out_c=64,
                                            str=1,
                                            name='conv_middle5')
            conv_middle6 = tools.Ops.conv3d(conv_middle5,
                                            k=3,
                                            out_c=64,
                                            str=1,
                                            name='conv_middle6')
            concat_up_2 = tf.concat([conv_middle6, conv_input_1], axis=4)

        with tf.variable_scope("predict"):
            predict_map_normed = tools.Ops.batch_norm(concat_up_2,
                                                      'bn_after_dense_1',
                                                      training=training)
            predict_map_relued = tools.Ops.xxlu(predict_map_normed,
                                                "relu_predict_map")
            predict_map_zoomed = tools.Ops.deconv3d(predict_map_relued,
                                                    k=3,
                                                    out_c=original,
                                                    str=2,
                                                    name='deconv_zoom_3')
            predict_map = tools.Ops.conv3d(predict_map_zoomed,
                                           k=1,
                                           out_c=1,
                                           str=1,
                                           name="predict_map")

            vox_no_sig = predict_map
            # vox_no_sig = tools.Ops.xxlu(vox_no_sig,name='relu')
            vox_sig = tf.sigmoid(predict_map)
            vox_sig_modified = tf.maximum(vox_sig - threshold, 0.01)
        return vox_sig, vox_sig_modified, vox_no_sig

    def dis(self, X, Y, training):
        with tf.variable_scope("input"):
            X = tf.reshape(X, [
                self.batch_size, self.blockShape[0], self.blockShape[1],
                self.blockShape[2], 1
            ])
            Y = tf.reshape(Y, [
                self.batch_size, self.blockShape[0], self.blockShape[1],
                self.blockShape[2], 1
            ])
            # layer = tf.concat([X, Y], axis=4)
            layer = X * Y
            c_d = [1, 32, 64, 128, 256, 512]
            s_d = [0, 2, 2, 2, 2, 2]
            layers_d = []
            layers_d.append(layer)
        with tf.variable_scope("down_sample"):
            for i in range(1, 6, 1):
                if i <= 2:
                    layer = tools.Ops.conv3d(layers_d[-1],
                                             k=4,
                                             out_c=c_d[i],
                                             str=s_d[i],
                                             name='d_1' + str(i))
                else:
                    shape = layer.get_shape()
                    if len(shape) > 4:
                        layer = tf.reshape(
                            layers_d[-1],
                            [shape[0], shape[1], shape[2], shape[4]],
                            name="reshape1")
                    else:
                        layer = layers_d[-1]
                    layer = tools.Ops.conv2d(layer,
                                             k=4,
                                             out_c=c_d[i],
                                             str=s_d[i],
                                             name='d_1' + str(i))
                if i != 5:
                    # batch normal layer
                    layer = tools.Ops.batch_norm(layer,
                                                 'bn_down' + str(i),
                                                 training=training)
                    layer = tools.Ops.xxlu(layer, name='lrelu')
                layers_d.append(layer)
        with tf.variable_scope("flating"):
            y = tf.reshape(layers_d[-1], [self.batch_size, -1])
        return tf.nn.sigmoid(y)