Beispiel #1
0
def hist_compare_best(game,name='dense',num_games=100):

    wins_list = []
    losses_list = []
    draws_list = []
    best_wins = 0
    best_net = 0
    for net_no in range(1,11):
        if best_net == 0:
            player2 = Nets.Dense()                                                             # dense or conv
        else:
            player2 = Nets.Dense()                                                             # dense or conv
            player2.load_state_dict(torch.load(name+'/'+name+str(int(best_net))+'.pth'))       # dense or conv
            player2.eval()
        player1 = Nets.Dense()                                                                 # dense or conv
        player1.load_state_dict(torch.load(name+'/'+name+str(int(net_no))+'.pth'))             # dense or conv
        player1.eval()
        print('Pitting',net_no-1,'and',net_no)
        wins,draws,losses = pit_against_network(player1,player2,game,num_games)
        print('wins:',wins)
        print('draws:',draws)
        print('losses:',losses)
        if(wins>=best_wins):
            best_wins = wins
            best_net = net_no
        print('Best Net:',best_net)
        wins_list.append(wins)
        draws_list.append(draws)
        losses_list.append(losses)
    
    plt.plot(wins_list,color='green')
    plt.plot(losses_list,color='red',alpha=0.3)
    plt.savefig('BestHistComp'+name+'.png')
Beispiel #2
0
def hist_compare(game,name='dense',num_games=100):      #currently string default is 'dense'

    wins_list = []
    losses_list = []
    draws_list = []
    for net_no in range(1,11):
        if net_no == 1:
            player2 = Nets.Conv()
            player2.eval()                                                              # dense or conv
        else:
            player2 = Nets.Conv()                                                              # dense or conv
            player2.load_state_dict(torch.load(name+'/'+name+str(int(net_no-1))+'.pth'))        # dense or conv
            player2.eval()
        player1 = Nets.Conv()                                                                  # dense or conv
        player1.load_state_dict(torch.load(name+'/'+name+str(int(net_no))+'.pth'))              # dense or conv
        player1.eval()
        print('Pitting',net_no-1,'and',net_no)
        wins,draws,losses = pit_against_network(player1,player2,game,num_games)
        print('wins:',wins)
        print('draws:',draws)
        print('losses:',losses)
        wins_list.append(wins)
        draws_list.append(draws)
        losses_list.append(losses)
    
    plt.plot(wins_list,color='green')
    plt.plot(losses_list,color='red',alpha=0.3)
    plt.savefig('HistComp'+name+'.png')
Beispiel #3
0
def main():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    net1 = Nets.Net1().to(device)
    net1.load_state_dict(torch.load('./model/Net1.pth'))
    net3 = Nets.Net3().to(device)
    net3.load_state_dict(torch.load('./model/Net3.pth'))
    net = [net1]
    train(net, lr=0.001, num_epochs=200, batch_size=512, s=2.0, w=0.06)
Beispiel #4
0
def build_net(x_train,
              y_train,
              num_train_steps=10000,
              x_test=None,
              y_test=None):
    # Number of stats currently used to predict outcome- 23 per team + variable for side
    inputs = 47
    outputs = 1
    if x_test is None:
        x_test = x_train
    if y_test is None:
        y_test = y_train
    # widths of fully-connected layers in NN

    # Input data goes here (via feed_dict or equiv)
    x = tf.placeholder(tf.float32, shape=[None, inputs])
    layer_widths = [16, 16, 16, 16, 16, 16]
    activations = [Nets.selu for _ in layer_widths] + [tf.identity]
    layer_widths += [outputs]
    net = Nets.SuperDenseNet(inputs, layer_widths, activations)
    # Construct all parameters of NN, set to independant gaussian priors
    params = [Nets.gauss_prior(shape) for shape in net.param_space()]

    out = ed.models.Bernoulli(logits=net.apply(x, params))

    # Variational 'posterior's for NN params
    qparams = [Nets.gauss_var_post(w.shape) for w in params]
    asd = tf.train.AdamOptimizer

    # Map from random variables to their variational posterior objects
    params_post = {params[i]: qparams[i] for i in range(len(params))}

    # evaluate accuracy and likelihood of model over the dataset before training
    print(
        'accuracy, log_likelihood, crossentropy',
        ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'],
                    data={
                        out: y_test,
                        x: x_test
                    }))
    # Run variational inference, minimizing KL(q, p) using stochastic gradient descent over variational params

    inference = ed.KLqp(params_post, data={out: y_train, x: x_train})
    #inference.initialize(optimizer=YFOptimizer())

    inference.run(n_samples=32, n_iter=num_train_steps)

    # Get output object dependant on variational posteriors rather than priors
    out_post = ed.copy(out, params_post)
    # Re-evaluate metrics
    print(
        'accuracy, log_likelihood, crossentropy',
        ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'],
                    data={
                        out_post: y_test,
                        x: x_test
                    }))
Beispiel #5
0
    def __init__(self, teams, predictor, features=6, prior=None):

        self.team_numbers = {}
        self.team_names = []
        self.features = features
        self.predictor = predictor
        self.param_space = list(predictor.param_space()) + [[teams, features]]
        self.var_post = [
            Nets.gauss_var_post(shape) for shape in self.param_space
        ]
        if prior is None:
            prior = [Nets.gauss_prior(shape) for shape in self.param_space]
        self.prior = prior
Beispiel #6
0
    def validation_drgan(self, batch_data, data_size, noise=None, pose=None):
        sample_outen = Nets.netG_encoder(batch_data, reuse=True)
        valid_add_z = tf.concat([sample_outen, noise], 3)
        pose = tf.expand_dims(pose, 1)
        pose = tf.expand_dims(pose, 1)
        valid_add_zp = tf.concat([valid_add_z, pose], 3)
        # ------------
        sample_valid, _ = Nets.netG_deconder(valid_add_zp,
                                             data_size,
                                             self.output_channel,
                                             reuse=True)

        return sample_valid
Beispiel #7
0
    def predict_drgan(self, batch_data, noise=None, pose=None):
        #        with tf.variable_scope('drgan'):
        with tf.name_scope('generator_encoder_decoder'):
            output_en = Nets.netG_encoder(batch_data)
            #----------noise
            # print 'noise:max{},min{}'.format(np.max(sampel_z),np.min(sampel_z))
            sample_add_z = tf.concat([output_en, noise], 3)
            pose = tf.expand_dims(pose, 1)
            pose = tf.expand_dims(pose, 1)
            sample_add_zp = tf.concat([sample_add_z, pose], 3)
            # print  sample_add_zp.shape
            #------------
            size_noise = noise.shape.as_list()[0]
            # print 'size_noise',pose.shape.as_list(),noise.shape.as_list()

            output_de, _ = Nets.netG_deconder(sample_add_zp,
                                              self.output_channel)

        tf.summary.histogram('generator/outputencoder', output_en)
        tf.summary.histogram('generator/inputdecoder', sample_add_zp)
        tf.summary.histogram('generator/outputdecoder', output_de)

        tf.summary.histogram('input_discrimintor/inputrealdata', batch_data)
        tf.summary.histogram('input_discrimintor/inputsyndata', output_de)
        with tf.name_scope('discriminator_total'):
            predict_r,predict_r_logits,\
            predict_r_label,predict_r_label_logits,\
            predict_r_pose,predict_r_pose_logits,\
            _ = \
            Nets.netD_discriminator(batch_data,class_nums=self.data_loader_train.class_nums,posenum=self.pose_c)

            predict_f,predict_f_logits,\
            predict_f_label,predict_f_label_logits,\
            predict_f_pose,predict_f_pose_logits,\
            _ = \
            Nets.netD_discriminator(output_de,class_nums=self.data_loader_train.class_nums,posenum=self.pose_c,reuse=True)

        tf.summary.histogram('discriminator/real_d', predict_r)
        tf.summary.histogram('discriminator/fake_d', predict_f)
        tf.summary.histogram('discriminator/real_id', predict_r_label)
        tf.summary.histogram('discriminator/fake_id', predict_f_label)
        tf.summary.histogram('discriminator/real_pose', predict_r_pose)
        tf.summary.histogram('discriminator/fake_pose', predict_f_pose)

        return predict_r,predict_r_logits,\
               predict_r_label,predict_r_label_logits,\
               predict_r_pose,predict_r_pose_logits,\
               predict_f,predict_f_logits,\
               predict_f_label,predict_f_label_logits,\
               predict_f_pose,predict_f_pose_logits,\
               output_de,output_en
Beispiel #8
0
def Single_test(Net, model_params, criterion, optimizer, train_input, train_target, test_input, test_target, \
                nb_iters = 100, batch_size = 316, use_tol = False, inter_states = False):
    """ 
        This function estimates the errors of the test and train data on the netowrk 'Net', trained by the train data.
    Arguments: 
        Net: the neural network structure to be used, type nn.Module
        model_params: the models / networks parameters, such as size, chn_conv, ker_conv, ker_pool, nb_hidden, len_IN_lin.
                    see the networks specification for more detail
        train_input: the input to train the network, type Variable() of size N*C*L
                    where N is the number of samples, C is the number of channels and L is the length of a sample
        train_target: the target to train the network
        test_input: the input to be tested
        test_target: the target of the test data (not used for training)
        nb_iters: (max) number of iterations during training
        batch_size: size of the batches during training
        use_tol = if True, use the tolerance on the loss as stopping criterion
        inter_states = if True, compute the train and test errors every 25 iterations
    Output:
        train_error: The error of the training data at the last iteration
        test_error: The error of the testing data at the last iteration
        err_array: If inter_states is True, returns an array containing a row with information at intervals of 25 iteration:
            first column: the iteration, second column: the train error, third column: the test error
    """

    #define the model and the criterion
    model = Net(*model_params)
    #criterion = nn.CrossEntropyLoss()

    #Use cuda if available
    #if torch.cuda.is_available():
    #    model.cuda()
    #    criterion.cuda()

    #standardize the data
    # mu, std = train_input.data.mean(), train_input.data.std()
    # train_input.data.sub_(mu).div_(std)
    # test_input.data.sub_(mu).div_(std)

    #apply the model (train the network)
    err_array = Nets.train_model(model, criterion, optimizer, train_input, train_target, \
                                 nb_iters, batch_size, use_tol, inter_states, test_input, test_target)
    #compute the errors
    train_error = Nets.compute_nb_errors(
        model, train_input, train_target,
        batch_size) / train_input.size(0) * 100
    test_error = Nets.compute_nb_errors(model, test_input, test_target,
                                        batch_size) / test_input.size(0) * 100
    print('train_error {:.02f}; test_error {:.02f}'.format(
        train_error, test_error))
    return train_error, test_error, err_array
Beispiel #9
0
    def _build_network(self):
        #network model
        with tf.variable_scope('model'):
            net_args = {}
            net_args['left_img'] = self._left_input
            net_args['right_img'] = self._right_input
            net_args['split_layers'] = [None]
            net_args['sequence'] = True
            net_args['train_portion'] = 'BEGIN'
            net_args['bulkhead'] = True if self._mode == 'MAD' else False
            self._net = Nets.get_stereo_net(self._model_name, net_args)
            self._predictions = self._net.get_disparities()
            self._full_res_disp = self._predictions[-1]

            self._inputs = {
                'left':
                self._left_input,
                'right':
                self._right_input,
                'target':
                tf.zeros([1, self._image_shape[0], self._image_shape[1], 1],
                         dtype=tf.float32)
            }

            #full resolution loss between warped right image and original left image
            self._loss = loss_factory.get_reprojection_loss(
                'mean_SSIM_l1', reduced=True)(self._predictions, self._inputs)
Beispiel #10
0
def test1(s=2.0):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    goal = 1
    index = random.randint(0, 9999)

    R = attack_net.ANGRI()
    victim = Nets.Net1()
    victim.load_state_dict(torch.load('./model/Net1.pth'))
    victim = victim.to(device)
    PATH = './model/ANGRI Net1 w=0.12/epoch_200.pth'
    R.load_state_dict(torch.load(PATH, map_location=device))
    R = R.to(device)

    _, test_iter = dl.load_MNIST(batch_size=512, cut=10)
    TFR, MR, FS, C = evaluate_accuracy(test_iter, victim, R, s)
    print("Targeted fooling rate:", TFR)
    print("Misclassification rate:", MR)
    print("Fidelity score:", FS)
    print("Confidence:", C)

    test_images = dl.load_test_images()
    test_labels = dl.load_test_labels()
    X = test_images[index]
    y = test_labels[index]
    X = torch.from_numpy(X.reshape(1, 1, 28, 28)).float().to(device)
    t = torch.zeros(1, 10)
    t[0, goal] = 1
    t = t.to(device)

    with torch.no_grad():
        y_hat = victim(X)
        perturb = s * R(t, X)
        X_adv = torch.clamp(X + perturb, min=-1.0, max=1.0)
        y_adv_hat = victim(X_adv)
        print("image index:", index)
        print('The label of number is', int(y))
        print('The evaluated number is', (y_hat.argmax(dim=1)).cpu().item())
        print('After attack, the evaluated number is',
              (y_adv_hat.argmax(dim=1)).cpu().item())

        test = X.cpu().numpy().reshape(28, 28)
        test = test * 127.5 + 127.5
        plt.figure(figsize=(3, 3))
        plt.imshow(test, cmap='gray')

        test_adv = X_adv.cpu().numpy().reshape(28, 28)
        test_adv = test_adv * 127.5 + 127.5
        plt.figure(figsize=(3, 3))
        plt.imshow(test_adv, cmap='gray')

        perturb = X_adv - X
        perturb = perturb.cpu().numpy().reshape(28, 28)
        perturb = perturb * 127.5 + 127.5
        plt.figure(figsize=(3, 3))
        plt.imshow(perturb, cmap='gray')

        plt.show()
Beispiel #11
0
    def predict_drgan(self, batch_data, noise=None, pose=None):
        predict_r,predict_r_logits,\
        predict_r_label,predict_r_label_logits,\
        predict_r_pose,predict_r_pose_logits,\
        var = \
            Nets.netD_discriminator(batch_data,class_nums=self.data_loader_train.class_nums,posenum=self.pose_c)
        # tf.summary.histogram('discriminator/fake_pose',predict_f_pose)

        return predict_r,predict_r_logits,\
               predict_r_label,predict_r_label_logits,\
               predict_r_pose,predict_r_pose_logits,var
Beispiel #12
0
    def __init__(self, pnet_param, rnet_param, onet_param, isCuda=True):
        self.isCuda = isCuda
        self.pnet = Nets.PNet()
        self.rnet = Nets.RNet()
        self.onet = Nets.ONet()

        if self.isCuda:
            self.pnet.cuda()
            self.rnet.cuda()
            self.onet.cuda()

        # 加载网络参数
        self.pnet.load_state_dict(torch.load(pnet_param))
        self.rnet.load_state_dict(torch.load(rnet_param))
        self.onet.load_state_dict(torch.load(onet_param))

        # 网络是测试
        self.pnet.eval()
        self.rnet.eval()
        self.onet.eval()
        # 定义transform为ToTensor
        self.__image_transform = transforms.Compose([transforms.ToTensor()])
Beispiel #13
0
    def __init__(self, pnet_param, rnet_param, onet_param, isCuda=False):

        self.isCuda = isCuda
        # 实例化网络
        self.pnet = Nets.PNet()
        self.rnet = Nets.RNet()
        self.onet = Nets.ONet()
        # CUDA加速网络
        if self.isCuda:
            self.pnet().cuda()
            self.rnet().cuda()
            self.onet().cuda()
        # 装载网络训练结果
        self.pnet.load_state_dict(torch.load(pnet_param))
        self.rnet.load_state_dict(torch.load(rnet_param))
        self.onet.load_state_dict(torch.load(onet_param))

        self.pnet.eval()
        self.rnet.eval()
        self.onet.eval()
        # 将图片数据转换成NCHW
        self.__image_transform = transforms.Compose(transforms.ToTensor())
Beispiel #14
0
def main():
    # net definition
    net = Nets.AlexNet().cuda()
    # net = Nets.ResNet(block = Nets.BasicBlock, layers = [2, 2, 2, 2], num_classes = 1).cuda()

    # load pretrained model
    load_model(torch.load('./models/alexnet.pth'), net)
    # load_model(torch.load('./models/resnet18.pth'), net)

    # evaluate
    net.eval()

    # loading data...
    root = '../data/faces'
    valdir = '../data/1/test_1.txt'
    transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    ])
    val_dataset = read_img(root, valdir, transform=transform)

    with torch.no_grad():
        label = []
        pred = []

        for i, (img, target) in enumerate(val_dataset):
            img = img.unsqueeze(0).cuda(non_blocking=True)
            target = target.cuda(non_blocking=True)
            output = net(img).squeeze(1)
            label.append(target.cpu()[0])
            pred.append(output.cpu()[0])
            print i

        # measurements
        label = np.array(label)
        pred = np.array(pred)
        correlation = np.corrcoef(label, pred)[0][1]
        mae = np.mean(np.abs(label - pred))
        rmse = np.sqrt(np.mean(np.square(label - pred)))

    print(
        'Correlation:{correlation:.4f}\t'
        'Mae:{mae:.4f}\t'
        'Rmse:{rmse:.4f}\t'.format(correlation=correlation, mae=mae,
                                   rmse=rmse))
Beispiel #15
0
def train_model(x_train, y_train):
    # x = tf.placeholder(tf.float32, shape=[None, 2])
    # widths of fully-connected layers in NN
    inputs = 2
    outputs = 1
    layer_widths = [8, 8, 8, 8, 8]
    activations = [tf.nn.elu for _ in layer_widths] + [tf.identity]
    layer_widths += [outputs]
    # Input data goes here (via feed_dict or equiv)
    x = tf.placeholder(tf.float32, shape=[len(x_train), inputs])
    net = Nets.SuperDenseNet(inputs, layer_widths, activations)
    # Construct all parameters of NN, set to independant gaussian priors
    weights = [gauss_prior(shape) for shape in net.weight_shapes()]
    biases = [gauss_prior(shape) for shape in net.bias_shapes()]
    out = ed.models.Bernoulli(logits=net.apply(x, weights, biases))

    # Variational 'posterior's for NN params
    qweights = [gauss_var_post(w.shape) for w in weights]
    qbiases = [gauss_var_post(b.shape) for b in biases]

    # Map from random variables to their variational posterior objects
    weights_post = {weights[i]: qweights[i] for i in range(len(weights))}
    biases_post = {biases[i]: qbiases[i] for i in range(len(weights))}
    var_post = {**weights_post, **biases_post}

    # evaluate 'accuracy' (what even is this??) and likelihood of model over the dataset before training
    print(
        'accuracy, log_likelihood, crossentropy',
        ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'],
                    data={
                        out: y_train,
                        x: x_train
                    }))
    # Run variational inference, minimizing KL(q, p) using stochastic gradient descent over variational params
    inference = ed.KLqp(var_post, data={out: y_train, x: x_train})
    inference.run(n_samples=16, n_iter=10000)

    # Get output object dependant on variational posteriors rather than priors
    out_post = ed.copy(out, var_post)
    # Re-evaluate metrics
    print(
        'accuracy, log_likelihood, crossentropy',
        ed.evaluate(['accuracy', 'log_likelihood', 'crossentropy'],
                    data={
                        out_post: y_train,
                        x: x_train
                    }))
Beispiel #16
0
def test3():
    s = 2.0
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    R = attack_net.ANGRI()
    victim = Nets.Net1()
    victim.load_state_dict(torch.load('./model/Net1.pth'))
    victim = victim.to(device)
    PATH = './model/ANGRI Net1 w=0.12/epoch_200.pth'
    R.load_state_dict(torch.load(PATH, map_location=device))
    R = R.to(device)

    t_one_hot = (torch.eye(10)).to(device)
    fig, ax = plt.subplots(nrows=10, ncols=10, sharex=True, sharey=True)
    #ax = ax.flatten()
    i = 0
    for src in range(0, 10):
        _, test_iter = dl.load_MNIST(batch_size=512, cut=src)
        with torch.no_grad():
            for X, _ in test_iter:
                # print(src)
                X = X[0]
                X = torch.cat((X, X, X, X, X, X, X, X, X, X), dim=0)
                X = X.reshape(10, -1, 28, 28)
                # print(X.shape)
                X = X.to(device)
                R.eval()  # 评估模式, 这会关闭dropout
                #t = goal*torch.ones(m, 1).long()
                #t_one_hot = torch.zeros(m, 10).scatter_(1,t,1)
                #t = t.to(device)
                #t_one_hot = t_one_hot.to(device)
                X_adv = torch.clamp(X + s * R(t_one_hot, X), min=-1.0, max=1.0)
                # print(X_adv.shape)
                X_adv = X_adv.cpu().numpy().reshape(-1, 28, 28)
                X_adv = X_adv * 127.5 + 127.5
                for j in range(0, 10):
                    if src != j:
                        ax[src, j].imshow(X_adv[j], cmap='gray')
                    else:
                        ax[src, j].imshow(255 * np.ones((28, 28)), cmap='gray')
                R.train()  # 改回训练模式
                break
    plt.show()
Beispiel #17
0
def test2(src, goal, s=2.0):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    R = attack_net.ANGRI()
    victim = Nets.Net1()
    victim.load_state_dict(torch.load('./model/Net1.pth'))
    victim = victim.to(device)
    PATH = './model/ANGRI Net1 w=0.12/epoch_200.pth'
    R.load_state_dict(torch.load(PATH, map_location=device))
    R = R.to(device)

    _, test_iter = dl.load_MNIST(batch_size=512, cut=src)
    TFR, total = 0.0, 0
    with torch.no_grad():
        for X, y in test_iter:
            m = X.shape[0]
            X = X.to(device)
            y = y.to(device)
            R.eval()  # 评估模式, 这会关闭dropout
            # t = (torch.floor(10*torch.rand(m, 1))).long()
            t = goal * torch.ones(m, 1).long()
            t_one_hot = torch.zeros(m, 10).scatter_(1, t, 1)
            t = t.to(device)
            t_one_hot = t_one_hot.to(device)

            X_adv = torch.clamp(X + s * R(t_one_hot, X), min=-1.0, max=1.0)
            y_adv_hat = victim(X_adv)
            TFR += (y_adv_hat.argmax(
                dim=1) == t.reshape(-1)).float().sum().cpu().item()

            index = (y_adv_hat.argmax(dim=1) == t.reshape(-1))
            y_adv_hat_tmp = (F.softmax(y_adv_hat, dim=1))[index]
            y_adv_hat_tmp = torch.max(y_adv_hat_tmp, dim=1)[0]

            R.train()  # 改回训练模式
            total += m
    return TFR / total
Beispiel #18
0
def train(
        batch_size,
        Epochs=2**32,
        dataset_name='lfw',
        lr=0.0001,
        noise_label_std=0.12,
        z_size=100,
        ztag_size=5,
        img_size=128,
        Dngf=128,
        Gngf=128,
        sym_conv=True,
        weights={},
        times={},
        save=True,
        fast_save=False,
        cont=False,
        plt_=True,
        hours=float("Inf"),
        show=False,
):
    torch.autograd.set_detect_anomaly(True)

    base = f"./models/trainings/{utils.get_local_time()}"
    params = locals()
    os.makedirs(base)
    with open(f"{base}/params.txt", 'wt') as f:
        f.write(str(params))
    with open(f"{base}/params_dict", 'wb') as f:
        pickle.dump(params, f)
    hours = hours * (60 * 60)  # sec in hour

    if cont:
        g = torch.load(f"{cont}/g_net")
        D = torch.load(f"{cont}/D_net")
    else:
        g = Nets.SymG(z_size=z_size,
                      ztag_size=ztag_size,
                      H=img_size,
                      ngf=Gngf,
                      device=device,
                      sym_conv=sym_conv)
        D = Nets.SymD(H=img_size, ngf=Dngf, sym_conv=sym_conv)
        utils.init_weights(g)
        utils.init_weights(D)
    g, D = g.to(device), D.to(device)
    opt_g = optim.Adam(g.parameters(), lr=lr, betas=(0.5, 0.999))
    opt_D = optim.Adam(D.parameters(), lr=lr, betas=(0.5, 0.999))
    lossfn = nn.BCELoss()
    mseloss = nn.MSELoss()
    opts = {'g': opt_g, 'D': opt_D}
    dl = utils.get_data(dataset_name, batch_size, img_size)

    def myplt(n=1, path=None):
        z = get_z(n, z_size).to(device)
        g.eval()
        with torch.no_grad():
            gz = g(z).cpu()
        gz = (gz + 1) * 0.5
        g.train()
        plt.figure(figsize=(15, 10))
        utils.plt_row_images(gz)
        if path: plt.savefig(f"{path}/imgs.jpg")
        if show: plt.show()

    def printl(l):
        mlosses = {n: sum(l[n]) / len(l[n]) for n in l if len(l[n]) > 0}
        print(mlosses)

    names = ['L_G', 'L_D', 'L_ALT', 'fake_probs', 'real_probs']
    weights = dict(weights)
    for n in names:
        weights.setdefault(n, 1)
    times = dict(times)
    for n in names:
        times.setdefault(n, 1)

    start_time = time.time()
    for e in range(Epochs):
        losses = {n: [] for n in names}
        norms = {n: [] for n in names + ['L_D_G']}

        for i, (x, y) in enumerate(dl):
            print(">", end="")
            # x : (bs, 3, 32, 32)
            x = x.to(device)

            ones = torch.ones((x.size(0), 1)).to(device)
            zeros = torch.zeros_like(ones).to(device)
            z = get_z(x.size(0), z_size).to(device)

            for j in range(times['L_D']):
                # D step
                # z = get_z(x.size(0), z_size).to(device)
                real_probs = D(x)
                loss = lossfn(D(g(z)), zeros +
                              get_rand_noise(zeros, noise_label_std)) + lossfn(
                                  real_probs,
                                  ones - get_rand_noise(ones, noise_label_std))
                loss *= weights['L_D']
                opts['D'].zero_grad()
                loss.backward()
                opts['D'].step()
                losses['L_D'].append(loss.cpu().item())
                losses['real_probs'].append(real_probs.mean().cpu().item())
                norms['L_D'].append(utils.get_total_grad_norms(D))
                norms['L_D_G'].append(utils.get_total_grad_norms(g))

            for j in range(times['L_G']):
                # G step
                # z = get_z(x.size(0), z_size).to(device)
                gz = g(z)
                fake_probs = D(gz)
                loss = lossfn(fake_probs, ones)
                loss *= weights['L_G']
                opts['g'].zero_grad()
                loss.backward()
                opts['g'].step()
                losses['L_G'].append(loss.cpu().item())
                losses['fake_probs'].append(fake_probs.mean().cpu().item())
                norms['L_G'].append(utils.get_total_grad_norms(g))

            for j in range(times['L_ALT']):
                # Alternative loss
                # z = get_z(x.size(0), z_size).to(device)
                loss = mseloss(g(z), mirror_img(g(get_zN(z))))
                loss *= weights['L_ALT']
                opts['g'].zero_grad()
                loss.backward()
                opts['g'].step()
                losses['L_ALT'].append(loss.cpu().item())
                norms['L_ALT'].append(utils.get_total_grad_norms(g))

            if i % 5 == 0:
                printl(losses)
                printl(norms)
                myplt(4)
                if fast_save:
                    dirpath = f"{base}"
                    if not os.path.isdir(dirpath): os.mkdir(dirpath)
                    for name, model in [("g_net", g), ("D_net", D)]:
                        torch.save(model.cpu(), f"{dirpath}/{name}")
                        model.to(device)
                    print(f"CP -- models saved to {dirpath}/{name}")

        if save and e % 1 == 0:
            dirpath = f"{base}/{e}"
            if not os.path.isdir(dirpath): os.mkdir(dirpath)
            myplt(4, path=f"{base}/{e}")
            for name, model in [("g_net", g), ("D_net", D)]:
                torch.save(model.cpu(), f"{base}/{e}/{name}")
                model.to(device)
            print(f"CP -- models saved to {base}/{e}/{name}")
        print()
        print(f">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> EPOCH {e} END")
        printl(losses)
        printl(norms)
        myplt(4)

        if e > Epochs: break
        if (time.time() - start_time) > hours: break

    if save:
        for name, model in [("g_net", g), ("D_net", D)]:
            torch.save(model.cpu(), f"{base}/{name}")

    utils.rmdir_if_empty(base)
Beispiel #19
0
__author__ = 'Rafael Lopes <*****@*****.**>'

from Nets import *
from pieces import networklist

net = Nets(networklist)

net.printnetworks()
net.checkoverlaps()
Beispiel #20
0
def main(args):
    #read input data
    with tf.name_scope('input_reader'):
        with tf.name_scope('training_set_reader'):
            data_set = data_reader.dataset(args.trainingSet,
                                           batch_size=args.batchSize,
                                           crop_shape=args.imageShape,
                                           num_epochs=args.numEpochs,
                                           augment=args.augment,
                                           is_training=True,
                                           shuffle=True)
            left_img_batch, right_img_batch, gt_image_batch = data_set.get_batch(
            )
            inputs = {
                'left': left_img_batch,
                'right': right_img_batch,
                'target': gt_image_batch
            }
        if args.validationSet is not None:
            with tf.name_scope('validation_set_reader'):
                validation_set = data_reader.dataset(args.validationSet,
                                                     batch_size=args.batchSize,
                                                     augment=False,
                                                     is_training=False,
                                                     shuffle=True)
                left_val_batch, right_val_batch, gt_val_batch = validation_set.get_batch(
                )
                print(left_val_batch.shape, right_val_batch.shape)

    #build network
    with tf.variable_scope('model') as scope:
        net_args = {}
        net_args['left_img'] = left_img_batch
        net_args['right_img'] = right_img_batch
        net_args['split_layers'] = [None]
        net_args['sequence'] = True
        net_args['train_portion'] = 'BEGIN'
        net_args['bulkhead'] = False
        stereo_net = Nets.get_stereo_net(args.modelName, net_args)
        print('Stereo Prediction Model:\n', stereo_net)
        predictions = stereo_net.get_disparities()
        full_res_disp = predictions[-1]

        if args.validationSet is not None:
            scope.reuse_variables()
            net_args['left_img'] = left_val_batch
            net_args['right_img'] = right_val_batch
            val_stereo_net = Nets.get_stereo_net(args.modelName, net_args)
            val_prediction = val_stereo_net.get_disparities()[-1]

    if args.validationSet is not None:
        #build validation ops
        with tf.variable_scope('validation_error'):
            # compute error against gt
            abs_err = tf.abs(val_prediction - gt_val_batch)
            valid_map = tf.where(tf.equal(gt_val_batch, 0),
                                 tf.zeros_like(gt_val_batch, dtype=tf.float32),
                                 tf.ones_like(gt_val_batch, dtype=tf.float32))
            filtered_error = abs_err * valid_map

            abs_err = tf.reduce_sum(filtered_error) / tf.reduce_sum(valid_map)
            bad_pixel_abs = tf.where(
                tf.greater(filtered_error, PIXEL_TH),
                tf.ones_like(filtered_error, dtype=tf.float32),
                tf.zeros_like(filtered_error, dtype=tf.float32))
            bad_pixel_perc = tf.reduce_sum(bad_pixel_abs) / tf.reduce_sum(
                valid_map)

            tf.summary.scalar('EPE', abs_err)
            tf.summary.scalar('bad3', bad_pixel_perc)
            tf.summary.image('val_prediction',
                             preprocessing.colorize_img(val_prediction,
                                                        cmap='jet'),
                             max_outputs=1)
            tf.summary.image('val_gt',
                             preprocessing.colorize_img(gt_val_batch,
                                                        cmap='jet'),
                             max_outputs=1)

    with tf.name_scope('training_error'):
        #build train ops
        global_step = tf.Variable(0, trainable=False)
        learning_rate = tf.train.exponential_decay(args.lr,
                                                   global_step,
                                                   args.decayStep,
                                                   0.5,
                                                   staircase=True)
        disparity_trainer = tf.train.AdamOptimizer(args.lr, 0.9)

        #l1 regression loss for each scale mutiplied by the corresponding weight
        if args.lossWeights is not None and len(
                args.lossWeights) == len(predictions):
            raise ValueError(
                'Wrong number of loss weights provide, should provide {}'.
                format(len(predictions)))
        full_reconstruction_loss = loss_factory.get_supervised_loss(
            args.lossType,
            multiScale=True,
            logs=False,
            weights=args.lossWeights,
            max_disp=MAX_DISP)(predictions, inputs)

        train_op = disparity_trainer.minimize(full_reconstruction_loss,
                                              global_step=global_step)

        #add summaries
        tf.summary.image('full_res_disp',
                         preprocessing.colorize_img(full_res_disp, cmap='jet'),
                         max_outputs=1)
        tf.summary.image('gt_disp',
                         preprocessing.colorize_img(gt_image_batch,
                                                    cmap='jet'),
                         max_outputs=1)
        tf.summary.scalar('full_reconstruction_loss', full_reconstruction_loss)

    #create summary logger
    summary_op = tf.summary.merge_all()
    logger = tf.summary.FileWriter(args.output)

    #create saver
    main_saver = tf.train.Saver(max_to_keep=2)

    #start session
    gpu_options = tf.GPUOptions(allow_growth=True)
    max_steps = data_set.get_max_steps()
    exec_time = 0
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        #init stuff
        sess.run([
            tf.global_variables_initializer(),
            tf.local_variables_initializer()
        ])

        #restore disparity inference weights
        restored, step_eval = weights_utils.check_for_weights_or_restore_them(
            args.output, sess, initial_weights=args.weights)
        print('Disparity Net Restored?: {} from step {}'.format(
            restored, step_eval))

        sess.run(global_step.assign(step_eval))
        try:
            start_time = time.time()
            while True:
                tf_fetches = [global_step, train_op, full_reconstruction_loss]

                if step_eval % 100 == 0:
                    #summaries
                    tf_fetches = tf_fetches + [summary_op]

                #run network
                run_options = tf.RunOptions(
                    report_tensor_allocations_upon_oom=True)
                fetches = sess.run(tf_fetches, options=run_options)

                if step_eval % 100 == 0:
                    #log on terminal
                    fbTime = (time.time() - start_time)
                    exec_time += fbTime
                    fbTime = fbTime / 100
                    logger.add_summary(fetches[-1], global_step=step_eval)
                    missing_time = (max_steps - step_eval) * fbTime
                    print(
                        'Step:{:4d}\tLoss:{:.2f}\tf/b time:{:3f}\tMissing time:{}'
                        .format(step_eval, fetches[2], fbTime,
                                datetime.timedelta(seconds=missing_time)))
                    start_time = time.time()

                if step_eval % 10000 == 0:
                    ckpt = os.path.join(args.output, 'weights.ckpt')
                    main_saver.save(sess, ckpt, global_step=step_eval)

                step_eval = fetches[0]
        except tf.errors.OutOfRangeError:
            pass
        finally:
            print('All Done, Bye Bye!')
Beispiel #21
0
import numpy as np
import torch

import Nets
from eval import pit_against_network, pit_human, hist_compare, pit_onelookahead
from Connect4Game import Connect4Game

if __name__ == '__main__':

    # initialize networks
    net = Nets.Conv()
    net.load_state_dict(torch.load('convbkup/conv10.pth'))
    net.eval()
    aux_net = Nets.Conv()
    #aux_net.load_state_dict(torch.load('convbkup/conv1.pth'))
    aux_net.eval()

    # initialize game
    game = Connect4Game()
    """ wins,draws,losses = pit_against_network(aux_net,net,game,100)

    print('Wins:',wins)
    print('Losses:',losses)
    print('Draws:',draws) """

    #pit_human(aux_net,game)
    hist_compare(game, name='conv', num_games=100)
    """ wins,draws,losses = pit_onelookahead(game,aux_net,1)

    print('Wins:',wins)
    print('Losses:',losses)
Beispiel #22
0
connected = True
from_same_vessel = False
bifurcations_allowed = False
save_vertices_indxs = False

# Setting other parameters
nEpochs = 2000  # Number of epochs for training
numHGScales = 4  # How many times to downsample inside each HourGlass
useTest = 1  # See evolution of the test set when training?
testBatch = 1  # Testing Batch
nTestInterval = 10  # Run on test set every nTestInterval iterations
gpu_id = int(os.environ['SGE_GPU'])  # Select which GPU, -1 if CPU
snapshot = 200  # Store a model every snapshot epochs

# Network definition
net = nt.Net_SHG(p['numHG'], numHGScales, p['Block'], 128, 1)
if gpu_id >= 0:
    torch.cuda.set_device(device=gpu_id)
    net.cuda()

# Loss function definition
criterion = nn.MSELoss(size_average=True)

# Use the following optimizer
optimizer = optim.RMSprop(net.parameters(),
                          lr=0.00005,
                          alpha=0.99,
                          momentum=0.0)

# Preparation of the data loaders
# Define augmentation transformations as a composition
Beispiel #23
0
import numpy as np
import torch

import Nets
from eval import pit_against_network, pit_human, hist_compare
from Connect4Game import Connect4Game

if __name__ == '__main__':

    # initialize networks
    net = Nets.Dense()
    net.load_state_dict(torch.load('dense/dense9.pth'))
    net.eval()
    aux_net = Nets.Dense()

    # initialize game
    game = Connect4Game()

    wins, draws, losses = pit_against_network(net, aux_net, game, 100)

    print('Wins:', wins)
    print('Losses:', losses)
    print('Draws:', draws)

    #pit_human(net,game)
    #hist_compare(game,name='dense',num_games=100)
Beispiel #24
0
import Nets
import train_nets

if __name__ == '__main__':
    net = Nets.RNet()
    trainer = train_nets.Trainer(net, './param/rnet1.pth', r'E:\data\data\24')
    trainer.train()
Beispiel #25
0
    parser.add_argument('-opc', dest='opc', help='Type of experiment')
    return parser.parse_args()


# run run.py -f ER -opc 0

args = parse_args()
name = args.f
opc = int(args.opc)

#-------------------------------------------------------------------------------------------------

if opc == 0:

    # Generating synthetic data
    data = Nets(name)
    data.autoencoder.train()

    # Visualizing graph embeddings
    print("Visualizing graph embeddings...")
    data.visualize_mssne()
    #data.visualize_tsne()

elif opc == 1:

    # Clustering graph embeddings in the embedding space
    print("Clustering graph embeddings...")
    nmi_list = []
    for i in range(0, 10):

        # Generate networks with different node permutations
Beispiel #26
0
    def predict_drgan(self,batch_data,pose=None):
        with tf.name_scope('generator_encoder_decoder'):
            output_en = Nets.Custom_netG_encoder(batch_data)
            shape = output_en.get_shape().as_list()
            print shape
        #----------noise
            noise = tf.random_uniform(shape=(self.batch_size, 6, 6, 50), minval=-1, maxval=1, dtype=tf.float32,name='input_noise')
            ps_noise_map=Nets.Custom_netG_pose_and_noise(output_en,shape,pose,noise)

            # pose_sp=tf.split(pose,2,axis=0)
            # pose=tf.concat(pose_sp,axis=0)
            # ps_noise_map_exchange = Nets.Custom_netG_pose_and_noise(output_en, shape, pose, noise,reuse=True)
            # pose_1=tf.zeros_like(pose)
            # ps_noise_map_1=Nets.Custom_netG_pose_and_noise(output_en,shape,pose_1,noise,reuse=True)
            # pose_2=tf.ones_like(pose)
            # ps_noise_map_2=Nets.Custom_netG_pose_and_noise(output_en,shape,pose_2,noise,reuse=True)
            # pose_3=tf.ones_like(pose)*0.5
            # ps_noise_map_3=Nets.Custom_netG_pose_and_noise(output_en,shape,pose_3,noise,reuse=True)
            # size_noise=noise.shape.as_list()[0]
            output_de = Nets.Custom_netG_decoder(output_en,ps_noise_map)
            # output_de_exchange = Nets.Custom_netG_decoder(output_en,ps_noise_map_exchange,reuse=True)
            # output_de_zeros=Nets.Custom_netG_decoder(output_en,ps_noise_map_1,reuse=True)
            # output_de_halves=Nets.Custom_netG_decoder(output_en,ps_noise_map_3,reuse=True)
            # output_de_ones=Nets.Custom_netG_decoder(output_en,ps_noise_map_2,reuse=True)
        #
        # tf.summary.histogram('input_discrimintor/inputrealdata',batch_data)
        # tf.summary.histogram('input_discrimintor/inputsyndata',output_de)
        with tf.name_scope('discriminatortotal'):
            # softmax_ad_real,adlogits_real=\
            # Nets.Custom_netD_discriminator_adloss(batch_data)

            softmax_id_real,idlogits_real= \
            Nets.Custom_netD_discriminator_idloss(batch_data,class_nums=self.data_loader_train.class_nums+1)

            pslogits_real,content_feature_real=\
            Nets.Custom_netD_discriminator_psloss(batch_data)

            # softmax_ad_fake, adlogits_fake = \
            # Nets.Custom_netD_discriminator_adloss(output_de,reuse=True)

            softmax_id_fake, idlogits_fake = \
            Nets.Custom_netD_discriminator_idloss(output_de, class_nums=self.data_loader_train.class_nums+1,reuse=True)

            pslogits_fake, content_feature_fake = \
            Nets.Custom_netD_discriminator_psloss(output_de,reuse=True)

            # softmax_ad_fake_ex, adlogits_fake_ex = \
            # Nets.Custom_netD_discriminator_adloss(output_de, reuse=True)

            # softmax_id_fake_ex, idlogits_fake_ex = \
            # Nets.Custom_netD_discriminator_idloss(output_de_exchange, class_nums=self.data_loader_train.class_nums+1, reuse=True)
            #
            # pslogits_fake_ex, content_feature_fake_ex = \
            # Nets.Custom_netD_discriminator_psloss(output_de_exchange, reuse=True)
        # tf.summary.histogram('discriminator/real_d',predict_r)
        # tf.summary.histogram('discriminator/fake_d',predict_f)
        # tf.summary.histogram('discriminator/real_id',predict_r_label)
        # tf.summary.histogram('discriminator/fake_id',predict_f_label)
        # tf.summary.histogram('discriminator/real_pose',predict_r_pose)
        # tf.summary.histogram('discriminator/fake_pose',predict_f_pose)

        return softmax_id_real, idlogits_real, \
               pslogits_real, content_feature_real, \
               softmax_id_fake, idlogits_fake, \
               pslogits_fake, content_feature_fake, \
                output_de
Beispiel #27
0
def main(args):
    #load json file config
    with open(args.blockConfig) as json_data:
        train_config = json.load(json_data)

    #read input data
    with tf.variable_scope('input_reader'):
        data_set = data_reader.dataset(args.list,
                                       batch_size=1,
                                       crop_shape=args.imageShape,
                                       num_epochs=1,
                                       augment=False,
                                       is_training=False,
                                       shuffle=False)
        left_img_batch, right_img_batch, gt_image_batch = data_set.get_batch()
        inputs = {
            'left': left_img_batch,
            'right': right_img_batch,
            'target': gt_image_batch
        }

    #build inference network
    with tf.variable_scope('model'):
        net_args = {}
        net_args['left_img'] = left_img_batch
        net_args['right_img'] = right_img_batch
        net_args['split_layers'] = [None]
        net_args['sequence'] = True
        net_args['train_portion'] = 'BEGIN'
        net_args['bulkhead'] = True if args.mode == 'MAD' else False
        stereo_net = Nets.get_stereo_net(args.modelName, net_args)
        print('Stereo Prediction Model:\n', stereo_net)
        predictions = stereo_net.get_disparities()
        full_res_disp = predictions[-1]

    #build real full resolution loss
    with tf.variable_scope('full_res_loss'):
        # reconstruction loss between warped right image and original left image
        full_reconstruction_loss = loss_factory.get_reprojection_loss(
            'mean_SSIM_l1', reduced=True)(predictions, inputs)

    #build validation ops
    with tf.variable_scope('validation_error'):
        # compute error against gt
        abs_err = tf.abs(full_res_disp - gt_image_batch)
        valid_map = tf.where(tf.equal(gt_image_batch, 0),
                             tf.zeros_like(gt_image_batch, dtype=tf.float32),
                             tf.ones_like(gt_image_batch, dtype=tf.float32))
        filtered_error = abs_err * valid_map

        abs_err = tf.reduce_sum(filtered_error) / tf.reduce_sum(valid_map)
        bad_pixel_abs = tf.where(
            tf.greater(filtered_error, PIXEL_TH),
            tf.ones_like(filtered_error, dtype=tf.float32),
            tf.zeros_like(filtered_error, dtype=tf.float32))
        bad_pixel_perc = tf.reduce_sum(bad_pixel_abs) / tf.reduce_sum(
            valid_map)

    #build train ops
    disparity_trainer = tf.train.MomentumOptimizer(args.lr, 0.9)
    train_ops = []
    if args.mode == 'MAD':
        #build train ops for separate portion of the network
        predictions = predictions[:-1]  #remove full res disp

        inputs_modules = {
            'left':
            scale_tensor(left_img_batch, args.reprojectionScale),
            'right':
            scale_tensor(right_img_batch, args.reprojectionScale),
            'target':
            scale_tensor(gt_image_batch, args.reprojectionScale) /
            args.reprojectionScale
        }

        assert (len(predictions) == len(train_config))
        for counter, p in enumerate(predictions):
            print('Build train ops for disparity {}'.format(counter))

            #rescale predictions to proper resolution
            multiplier = tf.cast(
                tf.shape(left_img_batch)[1] // tf.shape(p)[1], tf.float32)
            p = preprocessing.resize_to_prediction(
                p, inputs_modules['left']) * multiplier

            #compute reprojection error
            with tf.variable_scope('reprojection_' + str(counter)):
                reconstruction_loss = loss_factory.get_reprojection_loss(
                    'mean_SSIM_l1', reduced=True)([p], inputs_modules)

            #build train op
            layer_to_train = train_config[counter]
            print('Going to train on {}'.format(layer_to_train))
            var_accumulator = []
            for name in layer_to_train:
                var_accumulator += stereo_net.get_variables(name)
            print('Number of variable to train: {}'.format(
                len(var_accumulator)))

            #add new training op
            train_ops.append(
                disparity_trainer.minimize(reconstruction_loss,
                                           var_list=var_accumulator))

            print('Done')
            print('=' * 50)

        #create Sampler to fetch portions to train
        sampler = sampler_factory.get_sampler(args.sampleMode, args.numBlocks,
                                              args.fixedID)

    elif args.mode == 'FULL':
        #build single train op for the full network
        train_ops.append(disparity_trainer.minimize(full_reconstruction_loss))

    if args.summary:
        #add summaries
        tf.summary.scalar('EPE', abs_err)
        tf.summary.scalar('bad3', bad_pixel_perc)
        tf.summary.image('full_res_disp',
                         preprocessing.colorize_img(full_res_disp, cmap='jet'),
                         max_outputs=1)
        tf.summary.image('gt_disp',
                         preprocessing.colorize_img(gt_image_batch,
                                                    cmap='jet'),
                         max_outputs=1)

        #create summary logger
        summary_op = tf.summary.merge_all()
        logger = tf.summary.FileWriter(args.output)

    #start session
    gpu_options = tf.GPUOptions(allow_growth=True)
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        #init stuff
        sess.run([
            tf.global_variables_initializer(),
            tf.local_variables_initializer()
        ])

        #start queue runners
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(sess=sess, coord=coord)

        #restore disparity inference weights
        var_to_restore = weights_utils.get_var_to_restore_list(
            args.weights, [])
        assert (len(var_to_restore) > 0)
        restorer = tf.train.Saver(var_list=var_to_restore)
        restorer.restore(sess, args.weights)
        print('Disparity Net Restored?: {}, number of restored variables: {}'.
              format(True, len(var_to_restore)))

        num_actions = len(train_ops)
        if args.mode == 'FULL':
            selected_train_ops = train_ops
        else:
            selected_train_ops = [tf.no_op()]

        epe_accumulator = []
        bad3_accumulator = []
        time_accumulator = []
        exec_time = 0
        fetch_counter = [0] * num_actions
        sample_distribution = np.zeros(shape=[num_actions])
        temp_score = np.zeros(shape=[num_actions])
        loss_t_2 = 0
        loss_t_1 = 0
        expected_loss = 0
        last_trained_blocks = []
        reset_counter = 0
        step = 0
        max_steps = data_set.get_size()
        try:
            start_time = time.time()
            while True:
                #fetch new network portion to train
                if step % args.sampleFrequency == 0 and args.mode == 'MAD':
                    #Sample
                    distribution = softmax(sample_distribution)
                    blocks_to_train = sampler.sample(distribution)
                    selected_train_ops = [
                        train_ops[i] for i in blocks_to_train
                    ]

                    #accumulate sampling statistics
                    for l in blocks_to_train:
                        fetch_counter[l] += 1

                #build list of tensorflow operations that needs to be executed

                #errors and full resolution loss
                tf_fetches = [
                    abs_err, bad_pixel_perc, full_reconstruction_loss
                ]

                if args.summary and step % 100 == 0:
                    #summaries
                    tf_fetches = tf_fetches + [summary_op]

                #update ops
                tf_fetches = tf_fetches + selected_train_ops

                if args.logDispStep != -1 and step % args.logDispStep == 0:
                    #prediction for serialization to disk
                    tf_fetches = tf_fetches + [full_res_disp]

                #run network
                fetches = sess.run(tf_fetches)
                new_loss = fetches[2]

                if args.mode == 'MAD':
                    #update sampling probabilities
                    if step == 0:
                        loss_t_2 = new_loss
                        loss_t_1 = new_loss
                    expected_loss = 2 * loss_t_1 - loss_t_2
                    gain_loss = expected_loss - new_loss
                    sample_distribution = 0.99 * sample_distribution
                    for i in last_trained_blocks:
                        sample_distribution[i] += 0.01 * gain_loss

                    last_trained_blocks = blocks_to_train
                    loss_t_2 = loss_t_1
                    loss_t_1 = new_loss

                #accumulate performance metrics
                epe_accumulator.append(fetches[0])
                bad3_accumulator.append(fetches[1])

                if step % 100 == 0:
                    #log on terminal
                    fbTime = (time.time() - start_time)
                    exec_time += fbTime
                    fbTime = fbTime / 100
                    if args.summary:
                        logger.add_summary(fetches[3], global_step=step)
                    missing_time = (max_steps - step) * fbTime
                    print(
                        'Step:{:4d}\tbad3:{:.2f}\tEPE:{:.2f}\tSSIM:{:.2f}\tf/b time:{:3f}\tMissing time:{}'
                        .format(step, fetches[1], fetches[0], new_loss, fbTime,
                                datetime.timedelta(seconds=missing_time)))
                    start_time = time.time()

                #reset network if necessary
                if new_loss > args.SSIMTh:
                    restorer.restore(sess, args.weights)
                    reset_counter += 1

                #save disparity if requested
                if args.logDispStep != -1 and step % args.logDispStep == 0:
                    dispy = fetches[-1]
                    dispy_to_save = np.clip(dispy[0].astype(np.uint16), 0,
                                            MAX_DISP)
                    cv2.imwrite(
                        os.path.join(
                            args.output,
                            'disparities/disparity_{}.png'.format(step)),
                        dispy_to_save * 256)

                step += 1

        except Exception as e:
            print('Exception catched {}'.format(e))
            #raise(e)
        finally:
            epe_array = epe_accumulator
            bad3_array = bad3_accumulator
            epe_accumulator = np.sum(epe_accumulator)
            bad3_accumulator = np.sum(bad3_accumulator)
            with open(os.path.join(args.output, 'stats.csv'), 'w+') as f_out:
                # report series
                f_out.write('Metrics,cumulative,average\n')
                f_out.write('EPE,{},{}\n'.format(epe_accumulator,
                                                 epe_accumulator / step))
                f_out.write('bad3,{},{}\n'.format(bad3_accumulator,
                                                  bad3_accumulator / step))
                f_out.write('time,{},{}\n'.format(exec_time, exec_time / step))
                f_out.write('FPS,{}\n'.format(1 / (exec_time / step)))
                f_out.write('#resets,{}\n'.format(reset_counter))
                f_out.write('Blocks')
                for n in range(len(predictions)):
                    f_out.write(',{}'.format(n))
                f_out.write(',final\n')
                f_out.write('fetch_counter')
                for c in fetch_counter:
                    f_out.write(',{}'.format(c))
                f_out.write('\n')
                for c in sample_distribution:
                    f_out.write(',{}'.format(c))
                f_out.write('\n')

            step_time = exec_time / step
            time_array = [str(x * step_time) for x in range(len(epe_array))]

            with open(os.path.join(args.output, 'series.csv'), 'w+') as f_out:
                f_out.write('Iteration,Time,EPE,bad3\n')
                for i, (t, e,
                        b) in enumerate(zip(time_array, epe_array,
                                            bad3_array)):
                    f_out.write('{},{},{},{}\n'.format(i, t, e, b))

            print('All Done, Bye Bye!')
            coord.request_stop()
            coord.join()
Beispiel #28
0
                                  [-1, 2 * self.features])
        return self.predictor.apply(
            tf.concat(
                [team_vectors, tf.cast(x[:, -1:], tf.float32)], 1),
            self.prior[:-1])


def read_csv(league):
    return pd.read_csv(_constants.data_location +
                       'simple_game_data_leagueId={}.csv'.format(league))


if __name__ == '__main__':
    games, results, teams = read_data(read_csv(2))
    print(games[0])
    outputs = 32
    print(results)
    team_vector_length = 1
    layer_widths = []
    activations = [Nets.selu for _ in layer_widths] + [tf.identity]
    layer_widths += [outputs]

    net = Nets.SuperDenseNet(2 * team_vector_length + 1, layer_widths,
                             activations)
    predictor = FactoredPredictor(net, len(results[0]))
    myModel = PairModel(teams, predictor, team_vector_length)

    # tf.global_variables_initializer()

    myModel.train_model(games, results)
Beispiel #29
0
import Nets
import train_nets

if __name__ == '__main__':
    net = Nets.PNet()
    trainer = train_nets.Trainer(net, './param/pnet1.pth', r'E:\data\data\12')
    trainer.train()


Beispiel #30
0
def get_most_confident_outputs(img_id, patch_center_row, patch_center_col,
                               confident_th, gpu_id, connected_same_vessel):

    patch_size = 64
    center = (patch_center_col, patch_center_row)

    x_tmp = int(center[0] - patch_size / 2)
    y_tmp = int(center[1] - patch_size / 2)

    confident_connections = {}
    confident_connections['x_peak'] = []
    confident_connections['y_peak'] = []
    confident_connections['peak_value'] = []

    root_dir = './gt_dbs/DRIVE/'
    img = Image.open(
        os.path.join(root_dir, 'test', 'images', '%02d_test.tif' % img_id))
    img = np.array(img, dtype=np.float32)
    h, w = img.shape[:2]

    if x_tmp > 0 and y_tmp > 0 and x_tmp + patch_size < w and y_tmp + patch_size < h:

        img_crop = img[y_tmp:y_tmp + patch_size, x_tmp:x_tmp + patch_size, :]

        img_crop = img_crop.transpose((2, 0, 1))
        img_crop = torch.from_numpy(img_crop)
        img_crop = img_crop.unsqueeze(0)

        inputs = img_crop / 255 - 0.5

        # Forward pass of the mini-batch
        inputs = Variable(inputs)

        if gpu_id >= 0:
            inputs = inputs.cuda()

        p = {}
        p['useRandom'] = 1  # Shuffle Images
        p['useAug'] = 0  # Use Random rotations in [-30, 30] and scaling in [.75, 1.25]
        p['inputRes'] = (64, 64)  # Input Resolution
        p['outputRes'] = (64, 64)  # Output Resolution (same as input)
        p['g_size'] = 64  # Higher means narrower Gaussian
        p['trainBatch'] = 1  # Number of Images in each mini-batch
        p['numHG'] = 2  # Number of Stacked Hourglasses
        p['Block'] = 'ConvBlock'  # Select: 'ConvBlock', 'BasicBlock', 'BottleNeck'
        p['GTmasks'] = 0  # Use GT Vessel Segmentations as input instead of Retinal Images
        model_dir = './results_dir_vessels/'
        if connected_same_vessel:
            modelName = tb.construct_name(p, "HourGlass-connected-same-vessel")
        else:
            modelName = tb.construct_name(p, "HourGlass-connected")
        numHGScales = 4  # How many times to downsample inside each HourGlass
        net = nt.Net_SHG(p['numHG'], numHGScales, p['Block'], 128, 1)
        epoch = 1800
        net.load_state_dict(
            torch.load(os.path.join(
                model_dir,
                os.path.join(model_dir,
                             modelName + '_epoch-' + str(epoch) + '.pth')),
                       map_location=lambda storage, loc: storage))

        if gpu_id >= 0:
            net = net.cuda()

        output = net.forward(inputs)
        pred = np.squeeze(
            np.transpose(
                output[len(output) - 1].cpu().data.numpy()[0, :, :, :],
                (1, 2, 0)))

        mean, median, std = sigma_clipped_stats(pred, sigma=3.0)
        threshold = median + (10.0 * std)
        sources = find_peaks(pred, threshold, box_size=3)

        indxs = np.argsort(sources['peak_value'])
        for ii in range(0, len(indxs)):
            idx = indxs[len(indxs) - 1 - ii]
            if sources['peak_value'][idx] > confident_th:
                confident_connections['x_peak'].append(sources['x_peak'][idx])
                confident_connections['y_peak'].append(sources['y_peak'][idx])
                confident_connections['peak_value'].append(
                    sources['peak_value'][idx])
            else:
                break

        confident_connections = Table([
            confident_connections['x_peak'], confident_connections['y_peak'],
            confident_connections['peak_value']
        ],
                                      names=('x_peak', 'y_peak', 'peak_value'))

    return confident_connections
Beispiel #31
0
def main(args):
    #load json file config
    with open(args.blockConfig) as json_data:
        train_config = json.load(json_data)

    #read input data
    with tf.variable_scope('input_reader'):
        data_set = continual_data_reader.dataset(args.list,
                                                 batch_size=1,
                                                 crop_shape=args.imageShape,
                                                 num_epochs=1,
                                                 augment=False,
                                                 is_training=False,
                                                 shuffle=False)
        left_img_batch, right_img_batch, gt_image_batch, px_image_batch, real_width = data_set.get_batch(
        )
        inputs = {
            'left': left_img_batch,
            'right': right_img_batch,
            'target': gt_image_batch,
            'proxy': px_image_batch,
            'real_width': real_width
        }

    #build inference network
    with tf.variable_scope('model'):
        net_args = {}
        net_args['left_img'] = left_img_batch
        net_args['right_img'] = right_img_batch
        net_args['split_layers'] = [None]
        net_args['sequence'] = True
        net_args['train_portion'] = 'BEGIN'
        net_args['bulkhead'] = True if args.mode == 'MAD' else False
        stereo_net = Nets.get_stereo_net(args.modelName, net_args)
        print('Stereo Prediction Model:\n', stereo_net)
        predictions = stereo_net.get_disparities()
        full_res_disp = predictions[-1]

    #build real full resolution loss
    with tf.variable_scope('full_res_loss'):
        # loss with respect to proxy labels
        full_proxy_loss = loss_factory.get_proxy_loss('mean_l1',
                                                      max_disp=192,
                                                      weights=[0.01] * 10,
                                                      reduced=True)(
                                                          predictions, inputs)

    #build validation ops
    with tf.variable_scope('validation_error'):
        # compute error against gt
        abs_err = tf.abs(full_res_disp - gt_image_batch)
        valid_map = tf.where(tf.equal(gt_image_batch, 0),
                             tf.zeros_like(gt_image_batch, dtype=tf.float32),
                             tf.ones_like(gt_image_batch, dtype=tf.float32))
        filtered_error = abs_err * valid_map

        abs_err = tf.reduce_sum(filtered_error) / tf.reduce_sum(valid_map)
        bad_pixel_abs = tf.where(
            tf.greater(filtered_error, PIXEL_TH),
            tf.ones_like(filtered_error, dtype=tf.float32),
            tf.zeros_like(filtered_error, dtype=tf.float32))
        bad_pixel_perc = tf.reduce_sum(bad_pixel_abs) / tf.reduce_sum(
            valid_map)

    #build train ops
    disparity_trainer = tf.train.MomentumOptimizer(args.lr, 0.9)
    train_ops = []
    if args.mode == 'MAD':
        #build train ops for separate portion of the network
        predictions = predictions[:-1]  #remove full res disp

        inputs_modules = {
            'left':
            scale_tensor(left_img_batch, args.reprojectionScale),
            'right':
            scale_tensor(right_img_batch, args.reprojectionScale),
            'target':
            scale_tensor(gt_image_batch, args.reprojectionScale) /
            args.reprojectionScale,
            'proxy':
            scale_tensor(px_image_batch, args.reprojectionScale) /
            args.reprojectionScale,
        }

        assert (len(predictions) == len(train_config))
        for counter, p in enumerate(predictions):
            print('Build train ops for disparity {}'.format(counter))

            #rescale predictions to proper resolution
            multiplier = tf.cast(
                tf.shape(left_img_batch)[1] // tf.shape(p)[1], tf.float32)
            p = preprocessing.resize_to_prediction(
                p, inputs_modules['left']) * multiplier

            #compute proxy error
            with tf.variable_scope('proxy_' + str(counter)):
                proxy_loss = loss_factory.get_proxy_loss(
                    'mean_l1', max_disp=192, weights=[0.1] * 10,
                    reduced=True)([p], inputs_modules)

            #build train op
            layer_to_train = train_config[counter]
            print('Going to train on {}'.format(layer_to_train))
            var_accumulator = []
            for name in layer_to_train:
                var_accumulator += stereo_net.get_variables(name)
            print('Number of variable to train: {}'.format(
                len(var_accumulator)))

            #add new training op
            train_ops.append(
                disparity_trainer.minimize(proxy_loss,
                                           var_list=var_accumulator))

            print('Done')
            print('=' * 50)

        #create Sampler to fetch portions to train
        sampler = sampler_factory.get_sampler(args.sampleMode, args.numBlocks,
                                              args.fixedID)

    elif args.mode == 'FULL':
        #build single train op for the full network
        train_ops.append(disparity_trainer.minimize(full_proxy_loss))

    if args.summary:
        #add summaries
        tf.summary.scalar('EPE', abs_err)
        tf.summary.scalar('bad3', bad_pixel_perc)
        tf.summary.image('full_res_disp',
                         preprocessing.colorize_img(full_res_disp, cmap='jet'),
                         max_outputs=1)
        tf.summary.image('proxy_disp',
                         preprocessing.colorize_img(px_image_batch,
                                                    cmap='jet'),
                         max_outputs=1)
        tf.summary.image('gt_disp',
                         preprocessing.colorize_img(gt_image_batch,
                                                    cmap='jet'),
                         max_outputs=1)

        #create summary logger
        summary_op = tf.summary.merge_all()
        logger = tf.summary.FileWriter(args.output)

    #start session
    gpu_options = tf.GPUOptions(allow_growth=True)
    adaptation_saver = tf.train.Saver()

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        #init stuff
        sess.run([
            tf.global_variables_initializer(),
            tf.local_variables_initializer()
        ])

        #restore disparity inference weights
        var_to_restore = weights_utils.get_var_to_restore_list(
            args.weights, [])
        assert (len(var_to_restore) > 0)
        restorer = tf.train.Saver(var_list=var_to_restore)
        restorer.restore(sess, args.weights)
        print('Disparity Net Restored?: {}, number of restored variables: {}'.
              format(True, len(var_to_restore)))

        num_actions = len(train_ops)
        if args.mode == 'FULL':
            selected_train_ops = train_ops
        else:
            selected_train_ops = [tf.no_op()]

        # accumulators
        avg_accumulator = []
        d1_accumulator = []

        time_accumulator = []
        exec_time = 0
        fetch_counter = [0] * num_actions
        sample_distribution = np.zeros(shape=[num_actions])
        temp_score = np.zeros(shape=[num_actions])
        loss_t_2 = 0
        loss_t_1 = 0
        expected_loss = 0
        last_trained_blocks = []
        reset_counter = 0
        step = 0
        max_steps = data_set.get_max_steps()
        with open(os.path.join(args.output, 'histogram.csv'), 'w') as f_out:
            f_out.write('Histogram\n')

        try:
            start_time = time.time()
            while True:

                #fetch new network portion to train
                if step % args.sampleFrequency == 0 and args.mode == 'MAD':
                    #Sample
                    distribution = softmax(sample_distribution)
                    blocks_to_train = sampler.sample(distribution)
                    selected_train_ops = [
                        train_ops[i] for i in blocks_to_train
                    ]

                    #accumulate sampling statistics
                    for l in blocks_to_train:
                        fetch_counter[l] += 1

                #build list of tensorflow operations that needs to be executed

                tf_fetches = [
                    full_proxy_loss, full_res_disp, inputs['target'],
                    inputs['real_width']
                ]

                if args.summary and step % 100 == 0:
                    #summaries
                    tf_fetches = tf_fetches + [summary_op]

                #update ops
                if step % args.dilation == 0:
                    tf_fetches = tf_fetches + selected_train_ops

                tf_fetches = tf_fetches + [abs_err, bad_pixel_perc]

                if args.logDispStep != -1 and step % args.logDispStep == 0:
                    #prediction for serialization to disk
                    tf_fetches = tf_fetches + [inputs['left']] + [
                        inputs['proxy']
                    ] + [full_res_disp]

                #run network
                fetches = sess.run(tf_fetches)
                new_loss = fetches[0]

                if args.mode == 'MAD':
                    #update sampling probabilities
                    if step == 0:
                        loss_t_2 = new_loss
                        loss_t_1 = new_loss
                    expected_loss = 2 * loss_t_1 - loss_t_2
                    gain_loss = expected_loss - new_loss
                    sample_distribution = args.decay * sample_distribution
                    for i in last_trained_blocks:
                        sample_distribution[i] += args.uf * gain_loss

                    last_trained_blocks = blocks_to_train
                    loss_t_2 = loss_t_1
                    loss_t_1 = new_loss

                disp = fetches[1][-1]
                gt = fetches[2][-1]
                real_width = fetches[3][-1]

                # compute errors
                val = gt > 0
                disp_diff = np.abs(gt[val] - disp[val])
                outliers = np.logical_and(disp_diff > 3,
                                          (disp_diff / gt[val]) >= 0.05)
                d1 = np.mean(outliers) * 100.
                epe = np.mean(disp_diff)

                d1_accumulator.append(d1)
                avg_accumulator.append(epe)

                if step % 100 == 0:
                    #log on terminal
                    fbTime = (time.time() - start_time)
                    exec_time += fbTime
                    fbTime = fbTime / 100
                    if args.summary:
                        logger.add_summary(fetches[4], global_step=step)
                    missing_time = (max_steps - step) * fbTime

                    with open(os.path.join(args.output, 'histogram.csv'),
                              'a') as f_out:
                        f_out.write('%s\n' % fetch_counter)

                    print('Step: %04d \tEPE:%.3f\tD1:%.3f\t' % (step, epe, d1))
                    start_time = time.time()

                #reset network if necessary
                if new_loss > args.SSIMTh:
                    restorer.restore(sess, args.weights)
                    reset_counter += 1

                #save disparity if requested
                if args.logDispStep != -1 and step % args.logDispStep == 0:
                    dispy = fetches[-1]
                    prox = fetches[-2]
                    l = fetches[-3]

                    dispy_to_save = np.clip(dispy[0].astype(np.uint16), 0,
                                            MAX_DISP)
                    cv2.imwrite(
                        os.path.join(
                            args.output,
                            'disparities/disparity_{}.png'.format(step)),
                        dispy_to_save * 256)
                step += 1

        except tf.errors.InvalidArgumentError:  #OutOfRangeError:
            pass
        finally:

            with open(os.path.join(args.output, 'overall.csv'), 'w+') as f_out:
                print(fetch_counter)

                # report series
                f_out.write('EPE\tD1\n')
                f_out.write('%.3f\t%.3f\n' %
                            (np.asarray(avg_accumulator).mean(),
                             np.asarray(d1_accumulator).mean()))

            with open(os.path.join(args.output, 'series.csv'), 'w+') as f_out:
                f_out.write('step\tEPE\tD1\n')
                for i, (a, b) in enumerate(zip(avg_accumulator,
                                               d1_accumulator)):
                    f_out.write('%d & %.3f & %.3f\n' % (i, a, b))

            if args.saveWeights:
                adaptation_saver.save(sess,
                                      args.output + '/weights/model',
                                      global_step=step)
                print('Checkpoint saved in {}/weights'.format(args.output))
            print('Result saved in {}'.format(args.output))
            print('All Done, Bye Bye!')