예제 #1
0
def train_test():
    for k in model_select:
        
        table = BeautifulTable()
        avgtable = BeautifulTable()
        fieldnames1 = [model_lst[k],'Avg','Std_dev'] #column names report GLOBAL CSV
        folder = os.path.join(cwd,'Report_'+str(model_lst[k]))
        if not os.path.exists(folder):
            os.mkdir(folder)

        logfilepath = os.path.join(folder,'log.txt')
        logfile = open(logfilepath,"w") 

        with open(os.path.join(folder,'Report_folds.csv'),'w') as f_fold, open(os.path.join(folder,'Report_global.csv'),'w') as f_global:
            writer = csv.DictWriter(f_fold, fieldnames = fieldnames)
            writer1  = csv.DictWriter(f_global, fieldnames = fieldnames1)
            writer.writeheader()
            writer1.writeheader()
            t0 = 0
            t1 = 0
            for i in range(1,nfold+1):
                
                t0 = time.time()
                setSeeds(0)
                
                class Traindataset(Dataset):
                    def __init__(self):
                        self.data=trainfolds[i-1]
                        self.x_data=torch.from_numpy(np.asarray(self.data.iloc[:, 0:-1])) 
                        self.len=self.data.shape[0]
                        self.y_data = torch.from_numpy(np.asarray(self.data.iloc[:, [-1]]))
                        if (use_cuda):
                            self.x_data = self.x_data.cuda()
                            self.y_data = self.y_data.cuda()
                    def __getitem__(self, index):
                        return self.x_data[index], self.y_data[index]
                    def __len__(self):
                        return self.len
                class Testdataset(Dataset):
                    def __init__(self):
                        self.data=testfolds[i-1]
                        self.x_data=torch.from_numpy(np.asarray(self.data.iloc[:, 0:-1]))
                        self.len=self.data.shape[0]
                        self.y_data = torch.from_numpy(np.asarray(self.data.iloc[:, [-1]]))
                        if (use_cuda):
                            self.x_data = self.x_data.cuda()
                            self.y_data = self.y_data.cuda()
                    def __getitem__(self, index):
                        return self.x_data[index], self.y_data[index]
                    def __len__(self):
                        return self.len

                traindataset = Traindataset()
                testdataset = Testdataset()
                testdataset_U = Testdataset()    # Aggiunto, relativo agli UNLEARNED

                header(model_lst,k,i,traindataset,testdataset)

                #train_sampler,dev_sampler,test_sampler=dev_shuffle(shuffle_train,shuffle_test,val_split,traindataset,testdataset)
                #train_sampler,dev_sampler,test_val_sampler,test_sampler=data_split(shuffle_train,shuffle_test,val_split,test_val_split,traindataset,testdataset)
                
                #loaders
                train_loader = torch.utils.data.DataLoader(traindataset, batch_size=batch_size, 
                                                           sampler=train_sampler,drop_last=True)
                test_val_loader = torch.utils.data.DataLoader(testdataset, batch_size=batch_size,
                                                                sampler=test_val_sampler,drop_last=True)
                dev_loader = torch.utils.data.DataLoader(traindataset, batch_size=batch_size, 
                                                           sampler=dev_sampler,drop_last=True)
                
                # Questo l'ho cambiato da 'testdataset' a 'testdataset_U'
                test_loader = torch.utils.data.DataLoader(testdataset_U, batch_size=batch_size,
                                                                sampler=test_sampler,drop_last=True)
                modelClass = "Model" + str(k)
                model = eval(modelClass)()
                
                if (use_cuda):
                    model = model.cuda()

                if doTrain:
                    
                    criterion = nn.BCELoss(size_average=True)
                    optimizer = torch.optim.SGD(model.parameters(), lr)    
                    msg = 'Accuracy on test set before training: '+str(accuracy(test_loader, model))+'\n'
                    print(msg)
                    logfile.write(msg + "\n")
                    #EARLY STOP
                    epoch = 0
                    patience = 0
                    best_acc_dev=0
                    while (epoch<maxepoch and patience < maxpatience):
                        running_loss = 0.0
                        for l, data in enumerate(train_loader, 0):
                            inputs, labels = data
                            if use_cuda:
                                inputs, labels = inputs.cuda(), labels.cuda()
                            inputs, labels = Variable(inputs), Variable(labels)
                            y_pred = model(inputs)
                            if use_cuda:
                                y_pred = y_pred.cuda()
                            loss = criterion(y_pred, labels)
                            optimizer.zero_grad()
                            loss.backward()
                            optimizer.step()
                            running_loss += loss.item()
                            #print accuracy ever l mini-batches
                            if l % 2000 == 1999:
                                msg = '[%d, %5d] loss: %.3f' %(epoch + 1, l + 1, running_loss / 999)
                                print(msg)
                                logfile.write(msg + "\n")
                                running_loss = 0.0
                                #msg = 'Accuracy on dev set:' + str(accuracy(dev_loader))
                                #print(msg)
                                #logfile.write(msg + "\n")        
                        accdev = (accuracy(dev_loader, model))
                        msg = 'Accuracy on dev set:' + str(accdev)
                        print(msg)
                        logfile.write(msg + "\n")        
                        is_best = bool(accdev > best_acc_dev)
                        best_acc_dev = (max(accdev, best_acc_dev))
                        save_checkpoint({
                            'epoch': epoch + 1,
                            'state_dict': model.state_dict(),
                            'best_acc_dev': best_acc_dev
                        }, is_best,os.path.join(folder,'F'+str(i)+'best.pth.tar'), logfile)
                        if is_best:
                            patience=0
                        else:
                            patience = patience+1
                        epoch = epoch+1
                        logfile.flush()
                        
                if doEval:
                    if use_cuda:                        
                        state = torch.load(os.path.join(folder,'F'+str(i)+'best.pth.tar'))
                    else:
                        state = torch.load(os.path.join(folder,'F'+str(i)+'best.pth.tar'), map_location=lambda storage, loc: storage)
                    stop_epoch = state['epoch']
                    model.load_state_dict(state['state_dict'])
                    if not use_cuda:
                        model.cpu()
                    accuracy_dev = state['best_acc_dev']
                    model.eval()
                    acctest = (accuracy(test_loader, model))
                    acctest_val = (accuracy(test_val_loader, model))
                    accs[i-1] = acctest
                    accs_test_val[i-1] = acctest_val
                    
                    precision_0_U,recall_0_U,f1_score_0_U = pre_rec(test_loader, model, 0.0)
                    precisions_0_U[i-1] = precision_0_U
                    recalls_0_U[i-1] = recall_0_U
                    f1_scores_0_U[i-1] = f1_score_0_U
                    
                    precision_1_U,recall_1_U,f1_score_1_U = pre_rec(test_loader, model, 1.0)
                    precisions_1_U[i-1] = precision_1_U
                    recalls_1_U[i-1] = recall_1_U
                    f1_scores_1_U[i-1] = f1_score_1_U
                    
                    precision_0_L,recall_0_L,f1_score_0_L = pre_rec(test_val_loader, model, 0.0)
                    precisions_0_L[i-1] = precision_0_L
                    recalls_0_L[i-1] = recall_0_L
                    f1_scores_0_L[i-1] = f1_score_0_L
                    
                    precision_1_L,recall_1_L,f1_score_1_L = pre_rec(test_val_loader, model, 1.0)
                    precisions_1_L[i-1] = precision_1_L
                    recalls_1_L[i-1] = recall_1_L
                    f1_scores_1_L[i-1] = f1_score_1_L
                    
                    accs_dev[i-1] = accuracy_dev
                    
                    writer.writerow({'Fold': i,'Acc_L': acctest_val, 'Acc_U': acctest,
                                     #'P_0_U': precision_0_U,'R_0_U': recall_0_U,'F1_0_U': f1_score_0_U,
                                     'R_0_U': recall_0_U,
                                     #'P_1_U': precision_1_U,'R_1_U': recall_1_U,'F1_1_U': f1_score_1_U,
                                     'R_1_U': recall_1_U,
                                     #'P_0_L': precision_0_L,'R_0_L': recall_0_L,'F1_0_L': f1_score_0_L,
                                     'R_0_L': recall_0_L,
                                     #'P_1_L': precision_1_L,'R_1_L': recall_1_L,'F1_1_L': f1_score_1_L,
                                     'R_1_L': recall_1_L,
                                     'Stop_epoch': stop_epoch,'Accuracy_dev': accuracy_dev})
                    table.column_headers = fieldnames
                    table.append_row([i,acctest_val,acctest,
                                      #precision_0_U,recall_0_U,f1_score_0_U,
                                      recall_0_U,
                                      #precision_1_U,recall_1_U,f1_score_1_U,
                                      recall_1_U,
                                      #precision_0_L,recall_0_L,f1_score_0_L,
                                      recall_0_L,
                                      #precision_1_L,recall_1_L,f1_score_1_L,
                                      recall_1_L,
                                      stop_epoch,accuracy_dev])
                    print(table)
                    print('----------------------------------------------------------------------')
                    logfile.write(str(table) + "\n----------------------------------------------------------------------\n")
                    t1 = time.time()
                    times[i-1] = int(t1-t0)
            
            duration = str(datetime.timedelta(seconds=np.sum(times)))
            writer.writerow({})
            writer.writerow({'Fold': 'Elapsed time: '+duration})
            avg_acc_test_val = round(np.average(accs_test_val),3)
            std_acc_test_val = round(np.std(accs_test_val),3)
            
            avg_acc_test_val,avg_a,avg_p_0_U,avg_r_0_U,avg_f_0_U,avg_p_1_U,avg_r_1_U,avg_f_1_U,avg_p_0_L,avg_r_0_L,avg_f_0_L,avg_p_1_L,avg_r_1_L,avg_f_1_L,avg_a_d=averages([accs_test_val,accs,precisions_0_U,recalls_0_U,f1_scores_0_U,precisions_1_U,recalls_1_U,f1_scores_1_U,precisions_0_L,recalls_0_L,f1_scores_0_L,precisions_1_L,recalls_1_L,f1_scores_1_L,accs_dev])
            std_acc_test_val,std_a,std_p_0_U,std_r_0_U,std_f_0_U,std_p_1_U,std_r_1_U,std_f_1_U,std_p_0_L,std_r_0_L,std_f_0_L,std_p_1_L,std_r_1_L,std_f_1_L,std_a_d=stds([accs_test_val,accs,precisions_0_U,recalls_0_U,f1_scores_0_U,precisions_1_U,recalls_1_U,f1_scores_1_U,precisions_0_L,recalls_0_L,f1_scores_0_L,precisions_1_L,recalls_1_L,f1_scores_1_L,accs_dev])
            
            writer1.writerow({model_lst[k]: 'Acc_U','Avg': avg_a,'Std_dev': std_acc_test_val})
            writer1.writerow({model_lst[k]: 'Acc_L','Avg': avg_acc_test_val,'Std_dev': std_a})
            writer1.writerow({model_lst[k]: 'P_0_U','Avg': avg_p_0_U ,'Std_dev': std_p_0_U})
            writer1.writerow({model_lst[k]: 'R_0_U','Avg': avg_r_0_U,'Std_dev': std_r_0_U})
            writer1.writerow({model_lst[k]: 'F1_0_U','Avg': avg_f_0_U,'Std_dev': std_f_0_U})
            writer1.writerow({model_lst[k]: 'P_1_U','Avg': avg_p_1_U,'Std_dev': std_p_1_U})
            writer1.writerow({model_lst[k]: 'R_1_U','Avg': avg_r_1_U,'Std_dev': std_r_1_U})
            writer1.writerow({model_lst[k]: 'F1_1_U','Avg': avg_f_1_U,'Std_dev': std_f_1_U})            
            writer1.writerow({model_lst[k]: 'P_0_L','Avg': avg_p_0_L,'Std_dev': std_p_0_L})
            writer1.writerow({model_lst[k]: 'R_0_L','Avg': avg_r_0_L,'Std_dev': std_r_0_L})
            writer1.writerow({model_lst[k]: 'F1_0_L','Avg': avg_f_0_L,'Std_dev': std_f_0_L})
            writer1.writerow({model_lst[k]: 'P_1_L','Avg': avg_p_1_L,'Std_dev': std_p_1_L})
            writer1.writerow({model_lst[k]: 'R_1_L','Avg': avg_r_1_L,'Std_dev': std_r_1_L})
            writer1.writerow({model_lst[k]: 'F1_1_L','Avg': avg_f_1_L,'Std_dev': std_f_1_L})                        
            writer1.writerow({model_lst[k]: 'Acc_dev','Avg': avg_a_d,'Std_dev': std_a_d})
            writer1.writerow({})
            writer1.writerow({model_lst[k]: 'Elapsed time: '+duration})
            avgtable.column_headers = fieldnames1
            avgtable.append_row(['Acc_U',avg_a,std_a])
            avgtable.append_row(['Acc_L',avg_acc_test_val,std_acc_test_val])
            avgtable.append_row(['P_0_U',avg_p_0_U,std_p_0_U])
            avgtable.append_row(['R_0_U',avg_r_0_U,std_r_0_U])
            avgtable.append_row(['F1_0_U',avg_f_0_U,std_f_0_U])
            avgtable.append_row(['P_1_U',avg_p_1_U,std_p_1_U])
            avgtable.append_row(['R_1_U',avg_r_1_U,std_r_1_U])
            avgtable.append_row(['F1_1_U',avg_f_1_U,std_f_1_U])                        
            avgtable.append_row(['P_0_L',avg_p_0_L,std_p_0_L])
            avgtable.append_row(['R_0_L',avg_r_0_L,std_r_0_L])
            avgtable.append_row(['F1_0_L',avg_f_0_L,std_f_0_L])
            avgtable.append_row(['P_1_L',avg_p_1_L,std_p_1_L])
            avgtable.append_row(['R_1_L',avg_r_1_L,std_r_1_L])
            avgtable.append_row(['F1_1_L',avg_f_1_L,std_f_1_L])            
            avgtable.append_row(['Accuracy_dev',avg_a_d,std_a_d])
            print(avgtable)
            logfile.write(str(avgtable) + "\n")
            msg = 'Elapsed time: '+ duration + '\n\n'
            print(msg)
            logfile.write(msg )

        logfile.close()
예제 #2
0
def delete_person(file_path):
    parser = argparse.ArgumentParser()
    parser.add_argument('--image_folder', type=str, default='data/samples', help='path to dataset')
    parser.add_argument('--config_path', type=str, default='config/yolov3.cfg', help='path to model config file')
    parser.add_argument('--weights_path', type=str, default='weights/yolov3.weights', help='path to weights file')
    parser.add_argument('--class_path', type=str, default='data/coco.names', help='path to class label file')
    parser.add_argument('--conf_thres', type=float, default=0.8, help='object confidence threshold')
    parser.add_argument('--nms_thres', type=float, default=0.4, help='iou thresshold for non-maximum suppression')
    parser.add_argument('--batch_size', type=int, default=1, help='size of the batches')
    parser.add_argument('--n_cpu', type=int, default=8, help='number of cpu threads to use during batch generation')
    parser.add_argument('--img_size', type=int, default=416, help='size of each image dimension')
    parser.add_argument('--use_cuda', type=bool, default=True, help='whether to use cuda if available')
    opt = parser.parse_args()
    print(opt)

    cuda = torch.cuda.is_available() and opt.use_cuda

    os.makedirs('output', exist_ok=True)

    # Set up model
    model = Darknet(opt.config_path, img_size=opt.img_size)
    model.load_weights(opt.weights_path)

    if cuda:
        model.cuda()

    model.eval()  # Set in evaluation mode

    dataloader = DataLoader(ImageFolder(file_path, img_size=opt.img_size),
                            batch_size=opt.batch_size, shuffle=False, num_workers=opt.n_cpu)

    classes = load_classes(opt.class_path)  # Extracts class labels from file

    Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor

    imgs = []  # Stores image paths
    img_detections = []  # Stores detections for each image index

    print ('\nPerforming object detection:')
    prev_time = time.time()
    for batch_i, (img_paths, input_imgs) in enumerate(dataloader):
        # Configure input
        input_imgs = Variable(input_imgs.type(Tensor))

        # Get detections
        with torch.no_grad():
            detections = model(input_imgs)
            detections = non_max_suppression(detections, 80, opt.conf_thres, opt.nms_thres)


        # Log progress
        current_time = time.time()
        inference_time = datetime.timedelta(seconds=current_time - prev_time)
        prev_time = current_time
        print('\t Thread %d Batch %d, Inference Time: %s' % (threading.get_ident(), batch_i, inference_time))

        # Save image and detections
        imgs.extend(img_paths[0])
        img_detections.extend(detections)
        if detections[0] is not None:
            for detection in detections:
                x1, y1, x2, y2, conf, cls_conf, cls_pred = detection.numpy()[0,:]
                print ('\t+ Label: %s, Conf: %.5f' % (classes[int(cls_pred)], cls_conf.item()))
                if classes[int(cls_pred)] == 'person' and cls_conf.item() > 0.7 and abs(y2-y1) >= 128:
                    os.remove(img_paths[0])
                    print('delete')
                    break



# delete_person(file_path="/Users/rl/Desktop/data 2/wild/desert/sand")
# delete_person(file_path="data/samples")
예제 #3
0
from torch.autograd import Variable


class TwoLayerNet(torch.nn.Module):
    def __init__(self, D_in, H, D_out):
        super(TwoLayerNet, self).__init__()
        self.linear1 = torch.nn.Linear(D_in, H)
        self.linear2 = torch.nn.Linear(H, D_out)

    def forward(self, x):
        h_relu = self.linear1(x).clamp(min=0)
        y_pred = self.linear2(h_relu)
        return y_pred


N, D_in, H, D_out = 64, 1000, 100, 10

x = Variable(torch.randn(N, D_in))
y = Variable(torch.randn(N, D_out), requires_grad=False)
model = TwoLayerNet(D_in, H, D_out)
loss_fn = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=le - 4)

for i in range(500):
    y_pred = model(x)
    loss = loss_fn(y_pred, y)
    print(i, loss.data[0])

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
예제 #4
0
    def linearize_dynamics(self, x, u, dynamics, diff):
        # TODO: Cleanup variable usage.
        n_batch = x[0].shape[0]

        if self.grad_method == GradMethods.ANALYTIC:
            _u = u[:-1].reshape(-1, self.n_ctrl)
            _x = x[:-1].reshape(-1, self.n_state)

            # This inefficiently calls dynamics again, but is worth it because
            # we can efficiently compute grad_input for every time step at once.
            _new_x = dynamics(_x, _u)
            # This check is a little expensive and should only be done if
            # modifying this code.
            # assert torch.abs(_new_x.data - x[1:].view(-1, self.n_state)).max() <= 1e-6

            # if not diff:
            #     _new_x = _new_x.data
            #     _x = _x.data
            #     _u = _u.data
            R, S = dynamics.grad_input(_x, _u)

            f = _new_x - util.bmv(R, _x) - util.bmv(S, _u)
            f = f.reshape(self.T-1, n_batch, self.n_state)

            R = R.reshape(self.T-1, n_batch, self.n_state, self.n_state)
            S = S.reshape(self.T-1, n_batch, self.n_state, self.n_ctrl)
            F = np.concatenate((R, S), 3)

            return F, f
        else:
            # TODO: This is inefficient and confusing.
            x_init = x[0]
            x = [x_init]
            F, f = [], []
            for t in range(self.T):
                if t < self.T-1:
                    xt = Variable(x[t], requires_grad=True)
                    ut = Variable(u[t], requires_grad=True)
                    xut = torch.cat((xt, ut), 1)
                    new_x = dynamics(xt, ut)

                    # Linear dynamics approximation.
                    if self.grad_method in [GradMethods.AUTO_DIFF,
                                             GradMethods.ANALYTIC_CHECK]:
                        Rt, St = [], []
                        for j in range(self.n_state):
                            Rj, Sj = torch.autograd.grad(
                                new_x[:,j].sum(), [xt, ut],
                                retain_graph=True)
                            if not diff:
                                Rj, Sj = Rj.data, Sj.data
                            Rt.append(Rj)
                            St.append(Sj)
                        Rt = torch.stack(Rt, dim=1)
                        St = torch.stack(St, dim=1)

                        if self.grad_method == GradMethods.ANALYTIC_CHECK:
                            assert False # Not updated
                            Rt_autograd, St_autograd = Rt, St
                            Rt, St = dynamics.grad_input(xt, ut)
                            eps = 1e-8
                            if torch.max(torch.abs(Rt-Rt_autograd)).data[0] > eps or \
                            torch.max(torch.abs(St-St_autograd)).data[0] > eps:
                                print('''
        nmpc.ANALYTIC_CHECK error: The analytic derivative of the dynamics function may be off.
                                ''')
                            else:
                                print('''
        nmpc.ANALYTIC_CHECK: The analytic derivative of the dynamics function seems correct.
        Re-run with GradMethods.ANALYTIC to continue.
                                ''')
                            sys.exit(0)
                    elif self.grad_method == GradMethods.FINITE_DIFF:
                        Rt, St = [], []
                        for i in range(n_batch):
                            Ri = util.jacobian(
                                lambda s: dynamics(s, ut[i]), xt[i], 1e-3
                            )
                            Si = util.jacobian(
                                lambda a : dynamics(xt[i], a), ut[i], 1e-3
                            )
                            if not diff:
                                Ri, Si = Ri.data, Si.data
                            Rt.append(Ri)
                            St.append(Si)
                        Rt = torch.stack(Rt)
                        St = torch.stack(St)
                        Rt = Rt.squeeze(0)
                        St = St.squeeze(0)
                    else:
                        assert False

                    Ft = torch.cat((Rt, St), 2)
                    F.append(Ft)

                    if not diff:
                        xt, ut, new_x = xt.data, ut.data, new_x.data
                    ft = new_x - util.bmv(Rt, xt) - util.bmv(St, ut)
                    f.append(ft)

                if t < self.T-1:
                    x.append(util.detach_maybe(new_x))

            F = torch.stack(F, 0)
            f = torch.stack(f, 0)
            if not diff:
                F, f = list(map(Variable, [F, f]))
            return F, f
예제 #5
0
numExamples = instances.shape[1]

dtype = torch.FloatTensor
ltype = torch.ByteTensor

##======================================================================
## STEP 2: Initialize parameters, set them up as Variables for autograd.
#

#  Instructions: Initialize W with a standard Gaussian * 0.01
#                Compute the groundTruth matrix, set as a variable.

# Randomly initialize parameters W and b.
groundTruth = coo_matrix((np.ones(numExamples, dtype = np.uint8),
                            (labels, np.arange(numExamples)))).toarray()
groundTruth = Variable(torch.Tensor(groundTruth))
W = torch.Tensor(numClasses, inputSize).normal_() * 0.01
W = Variable(W, requires_grad = True)
b = torch.Tensor(numClasses, 1).fill_(0)
b = Variable(b, requires_grad = True)

# Load training data into Variables that do not require gradients.
instances = Variable(torch.Tensor(instances))
labels = Variable(torch.Tensor(labels).type(ltype))

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

# Set hyper-parameters.
learning_rate = 1.0
decay = 0.001
num_epochs = 200
예제 #6
0
 def make_std_mask(tgt, pad):
     "Create a mask to hide padding and future words"
     tgt_mask = (tgt != pad).unsqueeze(-2)
     tgt_mask = tgt_mask & Variable(
         subsequent_mask(tgt.size(-1)).type_as(tgt_mask.data))
     return tgt_mask
예제 #7
0
    def forward(self, x_init, cost, dx):
        # QuadCost.C: [T, n_batch, n_tau, n_tau]
        # QuadCost.c: [T, n_batch, n_tau]
        assert isinstance(cost, QuadCost) or \
            isinstance(cost, Module) or isinstance(cost, Function)
        assert isinstance(dx, LinDx) or \
            isinstance(dx, Module) or isinstance(dx, Function)

        # TODO: Clean up inferences, expansions, and assumptions made here.
        if self.n_batch is not None:
            n_batch = self.n_batch
        elif isinstance(cost, QuadCost) and cost.C.ndim == 4:
            n_batch = cost.C.shape[1]
        else:
            print('MPC Error: Could not infer batch size, pass in as n_batch')
            sys.exit(-1)


        # if c.ndimension() == 2:
        #     c = c.unsqueeze(1).expand(self.T, n_batch, -1)

        if isinstance(cost, QuadCost):
            C, c = cost
            if C.ndim == 2:
                # Add the time and batch dimensions.
                C = np.tile(C, (self.T, n_batch, 1, 1))
            elif C.ndim == 3:
                # Add the batch dimension.
                C = np.tile(np.expand_dims(C, 1), (1, n_batch, 1, 1))

            if c.ndim == 1:
                # Add the time and batch dimensions.
                c = np.tile(c, (self.T, n_batch, 1))
            elif c.ndim == 2:
                # Add the batch dimension.
                c = np.tile(np.expand_dims(c, 1), (1, n_batch, 1))

            if C.ndim != 4 or c.ndim != 3:
                print('MPC Error: Unexpected QuadCost shape.')
                sys.exit(-1)
            cost = QuadCost(C, c)

        assert x_init.ndim == 2 and x_init.shape[0] == n_batch

        if self.u_init is None:
            u = np.zeros((self.T, n_batch, self.n_ctrl), dtype='single')
        else:
            u = self.u_init
            if u.ndim == 2:
                u = np.tile(np.expand_dims(u, 1), (1, n_batch, 1))

        if self.verbose > 0:
            print('Initial mean(cost): {:.4e}'.format(
                np.mean(util.get_cost(
                    self.T, u, cost, dx, x_init=x_init
                ))
            ))

        best = None

        n_not_improved = 0
        for i in range(self.lqr_iter):
            u = u
            # Linearize the dynamics around the current trajectory.
            # time3 = time.time()
            # print('begin get traj')
            x = util.get_traj(self.T, u, x_init=x_init, dynamics=dx)
            # print('end get traj')
            # time4 = time.time()
            # print('get trajectory time:', time4 - time3)
            if isinstance(dx, LinDx):
                F, f = dx.F, dx.f
            else:
                # start = time.time()
                F, f = self.linearize_dynamics(
                    x, u, dx, diff=False)
                # end = time.time()
                # print('dynamics linearize:',end-start)
            if isinstance(cost, QuadCost):
                C, c = cost.C, cost.c
            else:
                C, c, _ = self.approximate_cost(
                    x, u, cost, diff=False)

            x, u, _lqr = self.solve_lqr_subproblem(
                x_init, C, c, F, f, cost, dx, x, u, self.verbose)
            # print(u)
            back_out, for_out = _lqr.back_out, _lqr.for_out
            n_not_improved += 1
            assert x.ndim == 3
            assert u.ndim == 3

            if best is None:
                best = {
                    'x': list(np.split(x, indices_or_sections=1, axis=1)),
                    'u': list(np.split(u, indices_or_sections=1, axis=1)),
                    'costs': for_out.costs,
                    # 'costsxx': for_out.costsxx,
                    # 'costsuu': for_out.costsuu,
                    # 'costsx': for_out.costsx,
                    # 'costsu': for_out.costsu,
                    # 'objsxx': for_out.objsxx,
                    # 'objsuu': for_out.objsuu,
                    # 'objsx': for_out.objsx,
                    # 'objsu': for_out.objsu,
                    'full_du_norm': for_out.full_du_norm,
                }
            else:
                for j in range(n_batch):
                    if for_out.costs[j] <= best['costs'][j] - self.best_cost_eps:
                        n_not_improved = 0
                        best['x'][j] = np.expand_dims(x[:,j], 1)
                        best['u'][j] = np.expand_dims(u[:,j], 1)
                        best['costs'][j] = for_out.costs[j]
                        # best['costsxx'][j] = for_out.costsxx[j]
                        # best['costsuu'][j] = for_out.costsuu[j]
                        # best['costsx'][j] = for_out.costsx[j]
                        # best['costsu'][j] = for_out.costsu[j]
                        # best['objsxx'][:,j] = for_out.objsxx[:,j]
                        # best['objsuu'][:,j] = for_out.objsuu[:,j]
                        # best['objsx'][:,j] = for_out.objsx[:,j]
                        # best['objsu'][:,j] = for_out.objsu[:,j]
                        best['full_du_norm'][j] = for_out.full_du_norm[j]

            if self.verbose > 0:
                util.table_log('lqr', (
                    ('iter', i),
                    ('mean(cost)', np.mean(best['costs']).item(), '{:.4e}'),
                    ('mean(costxx)', np.mean(best['costsxx']).item(), '{:.4e}'),
                    ('mean(costuu)', np.mean(best['costsuu']).item(), '{:.4e}'),
                    # ('mean(costx)', np.mean(best['costsx']).item(), '{:.4e}'),
                    # ('mean(costu)', np.mean(best['costsu']).item(), '{:.4e}'),
                    ('mean(objsxx[0])', np.mean(best['objsxx'][0], ).item(), '{:.4e}'),
                    ('mean(objsuu[0])', np.mean(best['objsuu'][0], ).item(), '{:.4e}'),
                    ('mean(objsxx[1])', np.mean(best['objsxx'][1], ).item(), '{:.4e}'),
                    ('mean(objsuu[1])', np.mean(best['objsuu'][1], ).item(), '{:.4e}'),
                    ('mean(objsxx[2])', np.mean(best['objsxx'][2], ).item(), '{:.4e}'),
                    ('mean(objsuu[2])', np.mean(best['objsuu'][2], ).item(), '{:.4e}'),
                    ('mean(objsxx[3])', np.mean(best['objsxx'][3], ).item(), '{:.4e}'),
                    ('mean(objsuu[3])', np.mean(best['objsuu'][3], ).item(), '{:.4e}'),
                    # ('mean(objsxx[4])', np.mean(best['objsxx'][4], ).item(), '{:.4e}'),
                    # ('mean(objsuu[4])', np.mean(best['objsuu'][4], ).item(), '{:.4e}'),
                    # ('||full_du||_max', max(for_out.full_du_norm).item(), '{:.2e}'),
                    # ('||alpha_du||_max', max(for_out.alpha_du_norm), '{:.2e}'),
                    # TODO: alphas, total_qp_iters here is for the current
                    # iterate, not the best
                    # ('mean(alphas)', for_out.mean_alphas.item(), '{:.2e}'),
                    # ('total_qp_iters', back_out.n_total_qp_iter),
                ))

            if max(for_out.full_du_norm) < self.eps or \
               n_not_improved > self.not_improved_lim:
                break


        x = np.concatenate(best['x'], axis=1)
        u = np.concatenate(best['u'], axis=1)
        full_du_norm = best['full_du_norm']

        # if isinstance(dx, LinDx):
        #     F, f = dx.F, dx.f
        # else:
        #     time1 = time.time()
        #     F, f = self.linearize_dynamics(x, u, dx, diff=True)
        #     time2 = time.time()
        #     print('dynamics linearize2:', time2 - time1)
        #
        # if isinstance(cost, QuadCost):
        #     C, c = cost.C, cost.c
        # else:
        #     C, c, _ = self.approximate_cost(x, u, cost, diff=True)
        #
        # x, u, _ = self.solve_lqr_subproblem(
        #     x_init, C, c, F, f, cost, dx, x, u, no_op_forward=True)

        if self.detach_unconverged:
            if max(best['full_du_norm']) > self.eps:
                if self.exit_unconverged:
                    assert False

                if self.verbose >= 0:
                    print("LQR Warning: All examples did not converge to a fixed point.")
                    print("Detaching and *not* backpropping through the bad examples.")

                I = for_out.full_du_norm < self.eps
                Ix = Variable(I.unsqueeze(0).unsqueeze(2).expand_as(x)).type_as(x.data)
                Iu = Variable(I.unsqueeze(0).unsqueeze(2).expand_as(u)).type_as(u.data)
                x = x*Ix + x.clone().detach()*(1.-Ix)
                u = u*Iu + u.clone().detach()*(1.-Iu)

        costs = best['costs']
        return (x, u, costs)
예제 #8
0
 def init_hidden(self, bsz):
     weight = next(self.parameters()).data
     return (Variable(weight.new(self.layers_num, bsz, self.hidden_dim).zero_()),
             Variable(weight.new(self.layers_num, bsz, self.hidden_dim).zero_()))
예제 #9
0
    testloader = DataLoader(testset,
                            batch_size=batch_size,
                            shuffle=shuffle,
                            num_workers=num_workers)
    print('[INFO] testloader generated')

    probs_list = []
    labels_list = []
    rsids_list = []
    query_snp_rsids_list = []

    for data in tqdm(testloader):
        inputs1, inputs2, inputs3, inputs4, labels, rsids, query_snp_rsids, metadata = data

        if torch.cuda.is_available():
            inputs1, inputs2, inputs3, inputs4, labels, metadata = Variable(
                inputs1.cuda()), Variable(inputs2.cuda()), Variable(
                    inputs3.cuda()), Variable(inputs4.cuda()), Variable(
                        labels.cuda()), Variable(metadata.cuda())
        else:
            inputs1, inputs2, inputs3, inputs4, labels, metadata = Variable(
                inputs1), Variable(inputs2), Variable(inputs3), Variable(
                    inputs4), Variable(labels), Variable(metadata)

        outputs = model(inputs1, inputs2, inputs3, inputs4, metadata)
        _, preds = torch.max(outputs.data, 1)
        probs = F.softmax(outputs, 1)
        correct += torch.sum(preds == labels.data)
        tp += torch.sum((preds + labels.data) == 2)
        size += len(labels)

        probs_list.extend(probs.data[:, 1])
 def init_hidden(self):
     """Initialize hidden state"""
     return (Variable(torch.zeros(1, 128, self.hidden_size)),
             Variable(torch.zeros(1, 128, self.hidden_size)))
예제 #11
0
def _runnetwork(epoch, loader, train=True, scaler=None, pbar=None):
    global nb_update_network
    # net
    if train:
        net.train()
    else:
        net.eval()

    if train:
        optimizer.zero_grad()
    for batch_idx, targets in enumerate(loader):

        data = Variable(targets['image'].cuda())
        
        with amp.autocast():
            output_belief, output_affinities = net(data)

            target_belief = Variable(targets['beliefs'].cuda())        
            target_affinity = Variable(targets['affinities'].cuda())

            loss = None
            
            # Belief maps loss
            for l in output_belief: #output, each belief map layers. 
                if loss is None:
                    loss = ((l - target_belief) * (l-target_belief)).mean()
                else:
                    loss_tmp = ((l - target_belief) * (l-target_belief)).mean()
                    loss += loss_tmp
            
            # Affinities loss
            for l in output_affinities: #output, each belief map layers. 
                loss_tmp = ((l - target_affinity) * (l-target_affinity)).mean()
                loss += loss_tmp 

        if train:
            scaler.scale(loss).backward()
            if batch_idx % (opt.batchsize // opt.subbatchsize) == 0:
                if train:
                    scaler.step(optimizer)
                    scaler.update()
                    nb_update_network+=1
                    optimizer.zero_grad()

        if train:
            namefile = '/loss_train.csv'
        else:
            namefile = '/loss_test.csv'

        with open (opt.outf+namefile,'a') as file:
            s = '{}, {},{:.15f}\n'.format(
                epoch,batch_idx,loss.data.item()) 
            # print (s)
            file.write(s)

        # break
        if not opt.nbupdates is None and nb_update_network > int(opt.nbupdates):
            torch.save(net.state_dict(), '{}/net_{}.pth'.format(opt.outf, opt.namefile))
            break

        if train:
            if pbar is not None:
                pbar.set_description("Training loss: %0.4f (%d/%d)" % (loss.data.item(), batch_idx, len(loader)))
        else:
            if pbar is not None:
                pbar.set_description("Testing loss: %0.4f (%d/%d)" % (loss.data.item(), batch_idx, len(loader)))
    if train:
        optimizer.zero_grad()
def to_var(x, volatile=False):
    if torch.cuda.is_available():
        x = x.cuda()
    return Variable(x, volatile=volatile)
예제 #13
0
def test(args, shared_model):
    log = {}
    logger = setup_logger("test_log", "./logs/test_log")

    torch.manual_seed(args.seed)
    env = Tetris(50)

    model = agentNET()
    model.eval()

    test_time = 0
    reward_num = 0
    clean_sum = 0
    max_reward = -1

    while (1):
        model.load_state_dict(shared_model.state_dict())
        if args.gpu:
            model = model.cuda()
            cx = Variable(torch.zeros(1, 78).cuda())
            hx = Variable(torch.zeros(1, 78).cuda())
        else:
            cx = Variable(torch.zeros(1, 78))
            hx = Variable(torch.zeros(1, 78))

        state = env.reset()  #50 100 3
        state = torch.from_numpy(state).float()

        while (1):
            if args.gpu:
                value, logit, (hx, cx) = model(
                    (Variable(state.unsqueeze(0)).cuda(), (hx, cx)))
            else:
                value, logit, (hx, cx) = model(
                    (Variable(state.unsqueeze(0)), (hx, cx)))

            prob = F.softmax(logit)
            if args.gpu:
                action = prob.max(1)[1].data.cpu()
            else:
                action = prob.max(1)[1].data

            state, done, reward, clean = env.step(action.numpy()[0])
            state = torch.from_numpy(state).float()
            reward_num += reward
            clean_sum += clean.get('1', -1000)

            if done:
                #print('dead', test_time)
                test_time += 1
                break

        if test_time % 50 == 0:
            if reward_num > max_reward:
                if args.gpu:
                    model = model.cpu()
                state_to_save = model.state_dict()
                torch.save(state_to_save, "./tetris.dat")
                logger.info('save')
                max_reward = reward_num
                if args.gpu:
                    model = model.cuda()
            logger.info('reward = ' + str(reward_num / test_time))
            logger.info('cleaned = ' + str(clean_sum / test_time))
            test_time = 0
            reward_num = 0
            clean_sum = 0
예제 #14
0
    def forward(self, left, right):

        refimg_fea = self.feature_extraction(left)
        targetimg_fea = self.feature_extraction(right)

        #matching
        cost = Variable(
            torch.FloatTensor(refimg_fea.size()[0],
                              refimg_fea.size()[1] * 2, self.maxdisp / 4,
                              refimg_fea.size()[2],
                              refimg_fea.size()[3]).zero_()).cuda()

        for i in range(int(self.maxdisp / 4)):
            if i > 0:
                cost[:, :refimg_fea.size()[1], i, :, i:] = refimg_fea[:, :, :,
                                                                      i:]
                cost[:, refimg_fea.size()[1]:, i, :,
                     i:] = targetimg_fea[:, :, :, :-i]
            else:
                cost[:, :refimg_fea.size()[1], i, :, :] = refimg_fea
                cost[:, refimg_fea.size()[1]:, i, :, :] = targetimg_fea
        cost = cost.contiguous()

        cost0 = self.dres0(cost)
        cost0 = self.dres1(cost0) + cost0

        out1, pre1, post1 = self.dres2(cost0, None, None)
        out1 = out1 + cost0

        out2, pre2, post2 = self.dres3(out1, pre1, post1)
        out2 = out2 + cost0

        out3, pre3, post3 = self.dres4(out2, pre1, post2)
        out3 = out3 + cost0

        cost1 = self.classif1(out1)
        cost2 = self.classif2(out2) + cost1
        cost3 = self.classif3(out3) + cost2

        if self.training:
            cost1 = F.upsample(
                cost1,
                [self.maxdisp, left.size()[2],
                 left.size()[3]],
                mode='trilinear')
            cost2 = F.upsample(
                cost2,
                [self.maxdisp, left.size()[2],
                 left.size()[3]],
                mode='trilinear')

            cost1 = torch.squeeze(cost1, 1)
            pred1 = F.softmax(cost1, dim=1)
            pred1 = disparityregression(self.maxdisp)(pred1)

            cost2 = torch.squeeze(cost2, 1)
            pred2 = F.softmax(cost2, dim=1)
            pred2 = disparityregression(self.maxdisp)(pred2)

        cost3 = F.upsample(
            cost3, [self.maxdisp, left.size()[2],
                    left.size()[3]],
            mode='trilinear')
        cost3 = torch.squeeze(cost3, 1)
        pred3 = F.softmax(cost3, dim=1)
        pred3 = disparityregression(self.maxdisp)(pred3)

        if self.training:
            return pred1, pred2, pred3
        else:
            return pred3
예제 #15
0
def siamese_track(state,
                  im,
                  mask_enable=False,
                  refine_enable=False,
                  device='cpu',
                  debug=False):
    p = state['p']
    net = state['net']
    avg_chans = state['avg_chans']
    window = state['window']
    target_pos = state['target_pos']
    target_sz = state['target_sz']

    wc_x = target_sz[1] + p.context_amount * sum(target_sz)
    hc_x = target_sz[0] + p.context_amount * sum(target_sz)
    s_x = np.sqrt(wc_x * hc_x)
    scale_x = p.exemplar_size / s_x
    d_search = (p.instance_size - p.exemplar_size) / 2
    pad = d_search / scale_x
    s_x = s_x + 2 * pad
    crop_box = [
        target_pos[0] - round(s_x) / 2, target_pos[1] - round(s_x) / 2,
        round(s_x),
        round(s_x)
    ]

    if debug:
        im_debug = im.copy()
        crop_box_int = np.int0(crop_box)
        cv2.rectangle(im_debug, (crop_box_int[0], crop_box_int[1]),
                      (crop_box_int[0] + crop_box_int[2],
                       crop_box_int[1] + crop_box_int[3]), (255, 0, 0), 2)
        cv2.imshow('search area', im_debug)
        cv2.waitKey(0)

    # extract scaled crops for search region x at previous target position
    x_crop = Variable(
        get_subwindow_tracking(im, target_pos, p.instance_size, round(s_x),
                               avg_chans).unsqueeze(0))

    if mask_enable:
        score, delta, mask = net.track_mask(x_crop.to(device))
    else:
        score, delta = net.track(x_crop.to(device))

    delta = delta.permute(1, 2, 3, 0).contiguous().view(4,
                                                        -1).data.cpu().numpy()
    score = F.softmax(score.permute(1, 2, 3,
                                    0).contiguous().view(2, -1).permute(1, 0),
                      dim=1).data[:, 1].cpu().numpy()

    delta[0, :] = delta[0, :] * p.anchor[:, 2] + p.anchor[:, 0]
    delta[1, :] = delta[1, :] * p.anchor[:, 3] + p.anchor[:, 1]
    delta[2, :] = np.exp(delta[2, :]) * p.anchor[:, 2]
    delta[3, :] = np.exp(delta[3, :]) * p.anchor[:, 3]

    def change(r):
        return np.maximum(r, 1. / r)

    def sz(w, h):
        pad = (w + h) * 0.5
        sz2 = (w + pad) * (h + pad)
        return np.sqrt(sz2)

    def sz_wh(wh):
        pad = (wh[0] + wh[1]) * 0.5
        sz2 = (wh[0] + pad) * (wh[1] + pad)
        return np.sqrt(sz2)

    # size penalty
    target_sz_in_crop = target_sz * scale_x
    s_c = change(sz(delta[2, :], delta[3, :]) /
                 (sz_wh(target_sz_in_crop)))  # scale penalty
    r_c = change((target_sz_in_crop[0] / target_sz_in_crop[1]) /
                 (delta[2, :] / delta[3, :]))  # ratio penalty

    penalty = np.exp(-(r_c * s_c - 1) * p.penalty_k)
    pscore = penalty * score

    # cos window (motion model)
    pscore = pscore * (1 - p.window_influence) + window * p.window_influence
    best_pscore_id = np.argmax(pscore)

    pred_in_crop = delta[:, best_pscore_id] / scale_x
    lr = penalty[best_pscore_id] * score[best_pscore_id] * p.lr  # lr for OTB

    res_x = pred_in_crop[0] + target_pos[0]
    res_y = pred_in_crop[1] + target_pos[1]

    res_w = target_sz[0] * (1 - lr) + pred_in_crop[2] * lr
    res_h = target_sz[1] * (1 - lr) + pred_in_crop[3] * lr

    target_pos = np.array([res_x, res_y])
    target_sz = np.array([res_w, res_h])

    # for Mask Branch
    if mask_enable:
        best_pscore_id_mask = np.unravel_index(best_pscore_id,
                                               (5, p.score_size, p.score_size))
        delta_x, delta_y = best_pscore_id_mask[2], best_pscore_id_mask[1]

        if refine_enable:
            mask = net.track_refine(
                (delta_y, delta_x)).to(device).sigmoid().squeeze().view(
                    p.out_size, p.out_size).cpu().data.numpy()
        else:
            mask = mask[0, :, delta_y, delta_x].sigmoid(). \
                squeeze().view(p.out_size, p.out_size).cpu().data.numpy()

        def crop_back(image, bbox, out_sz, padding=-1):
            a = (out_sz[0] - 1) / bbox[2]
            b = (out_sz[1] - 1) / bbox[3]
            c = -a * bbox[0]
            d = -b * bbox[1]
            mapping = np.array([[a, 0, c], [0, b, d]]).astype(np.float)
            crop = cv2.warpAffine(image,
                                  mapping, (out_sz[0], out_sz[1]),
                                  flags=cv2.INTER_LINEAR,
                                  borderMode=cv2.BORDER_CONSTANT,
                                  borderValue=padding)
            return crop

        s = crop_box[2] / p.instance_size
        sub_box = [
            crop_box[0] + (delta_x - p.base_size / 2) * p.total_stride * s,
            crop_box[1] + (delta_y - p.base_size / 2) * p.total_stride * s,
            s * p.exemplar_size, s * p.exemplar_size
        ]
        s = p.out_size / sub_box[2]
        back_box = [
            -sub_box[0] * s, -sub_box[1] * s, state['im_w'] * s,
            state['im_h'] * s
        ]
        mask_in_img = crop_back(mask, back_box, (state['im_w'], state['im_h']))

        target_mask = (mask_in_img > p.seg_thr).astype(np.uint8)
        if cv2.__version__[-5] == '4':
            contours, _ = cv2.findContours(target_mask, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_NONE)
        else:
            _, contours, _ = cv2.findContours(target_mask, cv2.RETR_EXTERNAL,
                                              cv2.CHAIN_APPROX_NONE)
        cnt_area = [cv2.contourArea(cnt) for cnt in contours]
        if len(contours) != 0 and np.max(cnt_area) > 100:
            contour = contours[np.argmax(cnt_area)]  # use max area polygon
            polygon = contour.reshape(-1, 2)
            # pbox = cv2.boundingRect(polygon)  # Min Max Rectangle
            prbox = cv2.boxPoints(
                cv2.minAreaRect(polygon))  # Rotated Rectangle

            # box_in_img = pbox
            rbox_in_img = prbox
        else:  # empty mask
            location = cxy_wh_2_rect(target_pos, target_sz)
            rbox_in_img = np.array(
                [[location[0], location[1]],
                 [location[0] + location[2], location[1]],
                 [location[0] + location[2], location[1] + location[3]],
                 [location[0], location[1] + location[3]]])

    target_pos[0] = max(0, min(state['im_w'], target_pos[0]))
    target_pos[1] = max(0, min(state['im_h'], target_pos[1]))
    target_sz[0] = max(10, min(state['im_w'], target_sz[0]))
    target_sz[1] = max(10, min(state['im_h'], target_sz[1]))

    state['target_pos'] = target_pos
    state['target_sz'] = target_sz
    state['score'] = score[best_pscore_id]
    state['mask'] = mask_in_img if mask_enable else []
    state['ploygon'] = rbox_in_img if mask_enable else []
    return state
예제 #16
0
파일: demo.py 프로젝트: hjyai94/CRF
def do_crf_inference(image, unary, args):

    if args.pyinn or not hasattr(torch.nn.functional, 'unfold'):
        # pytorch 0.3 or older requires pyinn.
        args.pyinn = True
        # Cheap and easy trick to make sure that pyinn is loadable.
        import pyinn

    # get basic hyperparameters
    num_classes = unary.shape[2]
    shape = image.shape[0:2]
    config = convcrf.default_conf
    config['filter_size'] = 7
    config['pyinn'] = args.pyinn

    if args.normalize:
        # Warning, applying image normalization affects CRF computation.
        # The parameter 'col_feats::schan' needs to be adapted.

        # Normalize image range
        #     This changes the image features and influences CRF output
        image = image / 255
        # mean substraction
        #    CRF is invariant to mean subtraction, output is NOT affected
        image = image - 0.5
        # std normalization
        #       Affect CRF computation
        image = image / 0.3

        # schan = 0.1 is a good starting value for normalized images.
        # The relation is f_i = image * schan
        config['col_feats']['schan'] = 0.1

    # make input pytorch compatible
    image = image.transpose(2, 0, 1)  # shape: [3, hight, width]
    # Add batch dimension to image: [1, 3, height, width]
    image = image.reshape([1, 3, shape[0], shape[1]])
    img_var = Variable(torch.Tensor(image)).cuda()

    unary = unary.transpose(2, 0, 1)  # shape: [3, hight, width]
    # Add batch dimension to unary: [1, 21, height, width]
    unary = unary.reshape([1, num_classes, shape[0], shape[1]])
    unary_var = Variable(torch.Tensor(unary)).cuda()

    logging.info("Build ConvCRF.")
    ##
    # Create CRF module
    gausscrf = convcrf.GaussCRF(conf=config, shape=shape, nclasses=num_classes)
    # Cuda computation is required.
    # A CPU implementation of our message passing is not provided.
    gausscrf.cuda()

    logging.info("Start Computation.")
    # Perform CRF inference
    prediction = gausscrf.forward(unary=unary_var, img=img_var)

    if args.nospeed:
        # Evaluate inference speed
        logging.info("Doing speed evaluation.")
        start_time = time.time()
        for i in range(10):
            # Running ConvCRF 10 times and average total time
            pred = gausscrf.forward(unary=unary_var, img=img_var)

        pred.cpu()  # wait for all GPU computations to finish

        duration = (time.time() - start_time) * 1000 / 10

        logging.info("Finished running 10 predictions.")
        logging.info("Avg. Computation time: {} ms".format(duration))

    return prediction.data.cpu().numpy()
예제 #17
0
파일: evaluator.py 프로젝트: yxsu/RRGen
def evaluate(src_sents, tgt_sents, src_seqs, tgt_seqs, src_lens, tgt_lens, rate_sents, cate_sents, senti_sents, encoder, decoder):
    # -------------------------------------
    # Prepare input and output placeholders
    # -------------------------------------
    # Last batch might not have the same size as we set to the `batch_size`
    batch_size = src_seqs.size(1)
    assert (batch_size == tgt_seqs.size(1))

    # Pack tensors to variables for neural network inputs (in order to autograd)
    src_seqs = Variable(src_seqs, volatile=True)
    tgt_seqs = Variable(tgt_seqs, volatile=True)
    # Categorize seq lengths
    if opts.use_sent_len:
        bins = range(0, opts.max_seq_len + 10, opts.use_sent_len)
        src_len_cates = list(pd.cut(src_lens, bins, labels=range(len(bins) - 1)))
        src_len_cates = Variable(torch.LongTensor(src_len_cates), volatile=True)
    else:
        src_len_cates = None

    src_lens = Variable(torch.LongTensor(src_lens), volatile=True)
    tgt_lens = Variable(torch.LongTensor(tgt_lens), volatile=True)
    rate_sents = Variable(torch.LongTensor(rate_sents), volatile=True)
    cate_sents = Variable(torch.LongTensor(cate_sents), volatile=True)
    senti_sents = Variable(torch.LongTensor(senti_sents), volatile=True)

    # Decoder's input
    input_seq = Variable(torch.LongTensor([BOS] * batch_size), volatile=True)

    # Decoder's output sequence length = max target sequence length of current batch.
    max_tgt_len = tgt_lens.data.max()

    # Store all decoder's outputs.
    # **CRUTIAL**
    # Don't set:
    # >> decoder_outputs = Variable(torch.zeros(max_tgt_len, batch_size, decoder.vocab_size))
    # Varying tensor size could cause GPU allocate a new memory causing OOM,
    # so we intialize tensor with fixed size instead:
    # `opts.max_seq_len` is a fixed number, unlike `max_tgt_len` always varys.
    decoder_outputs = Variable(torch.zeros(opts.max_seq_len, batch_size, decoder.vocab_size), volatile=True)

    # Move variables from CPU to GPU.
    if USE_CUDA:
        src_seqs = src_seqs.cuda()
        tgt_seqs = tgt_seqs.cuda()
        src_lens = src_lens.cuda()
        tgt_lens = tgt_lens.cuda()
        input_seq = input_seq.cuda()
        rate_sents = rate_sents.cuda()
        cate_sents = cate_sents.cuda()
        if opts.use_sent_len:
            src_len_cates = src_len_cates.cuda()
        senti_sents = senti_sents.cuda()
        decoder_outputs = decoder_outputs.cuda()

    # -------------------------------------
    # Evaluation mode (disable dropout)
    # -------------------------------------
    encoder.eval()
    decoder.eval()

    # -------------------------------------
    # Forward encoder
    # -------------------------------------
    encoder_outputs, encoder_hidden = encoder(src_seqs, src_lens.data.tolist())

    # -------------------------------------
    # Forward decoder
    # -------------------------------------
    # Initialize decoder's hidden state as encoder's last hidden state.
    decoder_hidden = encoder_hidden

    # Run through decoder one time step at a time.
    for t in range(max_tgt_len):
        # decoder returns:
        # - decoder_output   : (batch_size, vocab_size)
        # - decoder_hidden   : (num_layers, batch_size, hidden_size)
        # - attention_weights: (batch_size, max_src_len)
        decoder_output, decoder_hidden, attention_weights = decoder(input_seq, decoder_hidden, encoder_outputs, src_lens, rate_sents, cate_sents, src_len_cates, senti_sents)

        # Store decoder outputs.
        decoder_outputs[t] = decoder_output

        # Next input is current target
        input_seq = tgt_seqs[t]

        # Detach hidden state (may not need this, since no BPTT)
        detach_hidden(decoder_hidden)

    # -------------------------------------
    # Compute loss
    # -------------------------------------
    loss, pred_seqs, num_corrects, num_words = masked_cross_entropy(
        decoder_outputs[:max_tgt_len].transpose(0, 1).contiguous(),
        tgt_seqs.transpose(0, 1).contiguous(),
        tgt_lens
    )

    pred_seqs = pred_seqs[:max_tgt_len]

    return loss.data.item(), pred_seqs, attention_weights, num_corrects, num_words
예제 #18
0
    valset = MiniImageNet2('trainvaltest')
    val_loader = DataLoader(dataset=valset, batch_size = 128,
                            num_workers=8, pin_memory=True)
    valset2 = MiniImageNet2('trainval')
    val_loader2 = DataLoader(dataset=valset2, batch_size = 128,
                            num_workers=8, pin_memory=True)
    valset3 = MiniImageNet2('test')
    val_loader3 = DataLoader(dataset=valset3, batch_size = 128,
                            num_workers=8, pin_memory=True)

    model_cnn = Convnet().cuda()
    model_cnn.load_state_dict(torch.load('./100way_pn_basenovel.pth'))
    global_proto = torch.load('./global_proto_basenovel_PN_5shot_500.pth')
    global_base =global_proto[:args.n_base_class,:]
    global_novel = global_proto[args.n_base_class:,:]
    global_base = [Variable(global_base.cuda(),requires_grad=True)]
    global_novel = [Variable(global_novel.cuda(),requires_grad=True)]

    def log(out_str):
        print(out_str)
        logfile.write(out_str+'\n')
        logfile.flush()

    model_cnn.eval()
    for epoch in range(1, args.max_epoch + 1):

        for i, batch in enumerate(val_loader, 1):
            data, lab = [_.cuda() for _ in batch]

            data_shot = data[:, 3:, :]
            proto = model_cnn(data_shot)
예제 #19
0
        state_dict['rnn.1.embedding.bias'] = torch.randn(class_count)
        state_dict['rnn.1.embedding.weight'] = torch.randn(class_count, 512)
    crnn.load_state_dict(state_dict)
print(crnn)

image = torch.FloatTensor(opt.batchSize, 3, opt.imgH, opt.imgH)
text = torch.IntTensor(opt.batchSize * 5)
length = torch.IntTensor(opt.batchSize)

if opt.cuda:
    crnn.cuda()
    crnn = torch.nn.DataParallel(crnn, device_ids=range(opt.ngpu))
    image = image.cuda()
    criterion = criterion.cuda()

image = Variable(image)
text = Variable(text)
length = Variable(length)

# loss averager
loss_avg = utils.averager()

# setup optimizer
if opt.adam:
    optimizer = optim.Adam(crnn.parameters(),
                           lr=opt.lr,
                           betas=(opt.beta1, 0.999))
elif opt.adadelta:
    optimizer = optim.Adadelta(crnn.parameters(), lr=opt.lr)
else:
    optimizer = optim.RMSprop(crnn.parameters(), lr=opt.lr)
    def __len__(self):
        return len(self.data)

model = Net().cuda()
ceLoss = t.nn.CrossEntropyLoss()
opt = t.optim.Adam(model.parameters(), lr=0.001)
#trainDataset    = tv.datasets.MNIST(root=".",train=True,transform=tv.transforms.ToTensor(),download=True)
#testDataset     = tv.datasets.MNIST(root=".",train=False,transform=tv.transforms.ToTensor(),download=True)
trainDataset = MyData(isTrain=True, nTrain=600)
testDataset = MyData(isTrain=False, nTest=100)
trainLoader = t.utils.data.DataLoader(dataset=trainDataset, batch_size=nTrainBatchSize, shuffle=True)
testLoader = t.utils.data.DataLoader(dataset=testDataset, batch_size=nTrainBatchSize, shuffle=True)

for epoch in range(40):
    for i, (xTrain, yTrain) in enumerate(trainLoader):
        xTrain = Variable(xTrain).cuda()
        yTrain = Variable(yTrain).cuda()
        opt.zero_grad()
        y_, z = model(xTrain)
        loss = ceLoss(y_, yTrain)
        loss.backward()
        opt.step()
    if not (epoch + 1) % 10:
        print("%s, epoch %d, loss = %f" % (dt.now(), epoch + 1, loss.data))

acc = 0
model.eval()
for xTest, yTest in testLoader:
    xTest = Variable(xTest).cuda()
    yTest = Variable(yTest).cuda()
    y_, z = model(xTest)
예제 #21
0
    def solve_lqr_subproblem(self, x_init, C, c, F, f, cost, dynamics, x, u, verbose,
                             no_op_forward=False):
        if self.slew_rate_penalty is None or isinstance(cost, Module):
            _lqr = LQRStep(
                n_state=self.n_state,
                n_ctrl=self.n_ctrl,
                T=self.T,
                verbose=verbose,
                u_lower=self.u_lower,
                u_upper=self.u_upper,
                u_zero_I=self.u_zero_I,
                true_cost=cost,
                true_dynamics=dynamics,
                delta_u=self.delta_u,
                linesearch_decay=self.linesearch_decay,
                max_linesearch_iter=self.max_linesearch_iter,
                delta_space=True,
                current_x=x,
                current_u=u,
                back_eps=self.back_eps,
                no_op_forward=no_op_forward,
            )
            e = np.array([])
            x, u = _lqr(x_init, C, c, F, f if f is not None else e)
            return x, u, _lqr
        else:
            nsc = self.n_state + self.n_ctrl
            _n_state = nsc
            _nsc = _n_state + self.n_ctrl
            n_batch = C.shape[1]
            _C = np.zeros((self.T, n_batch, _nsc, _nsc), dtype='single')
            half_gamI = np.expand_dims(np.expand_dims(self.slew_rate_penalty * np.eye(
                self.n_ctrl), 0), 0).repeat(self.T, 0).repeat(n_batch, 1)
            _C[:,:,:self.n_ctrl,:self.n_ctrl] = half_gamI
            _C[:,:,-self.n_ctrl:,:self.n_ctrl] = -half_gamI
            _C[:,:,:self.n_ctrl,-self.n_ctrl:] = -half_gamI
            _C[:,:,-self.n_ctrl:,-self.n_ctrl:] = half_gamI
            slew_C = _C.copy()
            _C = _C + torch.nn.ZeroPad2d((self.n_ctrl, 0, self.n_ctrl, 0))(C)

            _c = torch.cat((
                torch.zeros(self.T, n_batch, self.n_ctrl).type_as(c),c), 2)

            _F0 = torch.cat((
                torch.zeros(self.n_ctrl, self.n_state+self.n_ctrl),
                torch.eye(self.n_ctrl),
            ), 1).type_as(F).unsqueeze(0).unsqueeze(0).repeat(
                self.T-1, n_batch, 1, 1
            )
            _F1 = torch.cat((
                torch.zeros(
                    self.T-1, n_batch, self.n_state, self.n_ctrl
                ).type_as(F),F), 3)
            _F = torch.cat((_F0, _F1), 2)

            if f is not None:
                _f = torch.cat((
                    torch.zeros(self.T-1, n_batch, self.n_ctrl).type_as(f),f), 2)
            else:
                _f = Variable(torch.Tensor())

            u_data = util.detach_maybe(u)
            if self.prev_ctrl is not None:
                prev_u = self.prev_ctrl
                if prev_u.ndimension() == 1:
                    prev_u = prev_u.unsqueeze(0)
                if prev_u.ndimension() == 2:
                    prev_u = prev_u.unsqueeze(0)
                prev_u = prev_u.data
            else:
                prev_u = torch.zeros(1, n_batch, self.n_ctrl).type_as(u)
            utm1s = torch.cat((prev_u, u_data[:-1])).clone()
            _x = torch.cat((utm1s, x), 2)

            _x_init = torch.cat((Variable(prev_u[0]), x_init), 1)

            if not isinstance(dynamics, LinDx):
                _dynamics = CtrlPassthroughDynamics(dynamics)
            else:
                _dynamics = None

            if isinstance(cost, QuadCost):
                _true_cost = QuadCost(_C, _c)
            else:
                _true_cost = SlewRateCost(
                    cost, slew_C, self.n_state, self.n_ctrl
                )

            _lqr = LQRStep(
                n_state=_n_state,
                n_ctrl=self.n_ctrl,
                T=self.T,
                u_lower=self.u_lower,
                u_upper=self.u_upper,
                u_zero_I=self.u_zero_I,
                true_cost=_true_cost,
                true_dynamics=_dynamics,
                delta_u=self.delta_u,
                linesearch_decay=self.linesearch_decay,
                max_linesearch_iter=self.max_linesearch_iter,
                delta_space=True,
                current_x=_x,
                current_u=u,
                back_eps=self.back_eps,
                no_op_forward=no_op_forward,
            )
            x, u = _lqr(_x_init, _C, _c, _F, _f)
            x = x[:,:,self.n_ctrl:]

            return x, u, _lqr
예제 #22
0
    for i in range(0, len(train[0])-batch_size,batch_size):

        augment = True
        video_list = [(train[0][k],augment)
                       for k in random_indices[i:(batch_size+i)]]
        data = pool_threads.map(loadFrame,video_list)

        next_batch = 0
        for video in data:
            if video.size==0: # there was an exception, skip this
                next_batch = 1
        if(next_batch==1):
            continue

        x = np.asarray(data,dtype=np.float32)
        x = Variable(torch.FloatTensor(x)).cuda().contiguous()

        y = train[1][random_indices[i:(batch_size+i)]]
        y = torch.from_numpy(y).cuda()

        output = model(x)

        loss = criterion(output, y)
        optimizer.zero_grad()

        loss.backward()
        optimizer.step()
        
        prediction = output.data.max(1)[1]
        accuracy = ( float( prediction.eq(y.data).sum() ) /float(batch_size))*100.0
        if(epoch==0):
예제 #23
0
        for i in range(future):  # if we should predict the future
            h_t, c_t = self.lstm1(output, (h_t, c_t))
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))
            output = self.linear(h_t2)
            outputs += [output]
        outputs = torch.stack(outputs, 1).squeeze(2)
        return outputs


if __name__ == '__main__':
    # set random seed to 0
    np.random.seed(0)
    torch.manual_seed(0)
    # load data and make training set
    data = torch.load('traindata.pt')
    input = Variable(torch.from_numpy(data[3:, :-1]), requires_grad=False)
    target = Variable(torch.from_numpy(data[3:, 1:]), requires_grad=False)
    test_input = Variable(torch.from_numpy(data[:3, :-1]), requires_grad=False)
    test_target = Variable(torch.from_numpy(data[:3, 1:]), requires_grad=False)
    # build the model
    seq = Sequence()
    seq.double()
    criterion = nn.MSELoss()
    # use LBFGS as optimizer since we can load the whole data to train
    optimizer = optim.LBFGS(seq.parameters(), lr=0.8)
    #begin to train
    for i in range(15):
        print('STEP: ', i)

        def closure():
            optimizer.zero_grad()
예제 #24
0
파일: detect.py 프로젝트: zhyever/yolov3
        # 一共有的batch数
        num_batches = len(imlist) // batch_size + leftover
        # im_batches构建
        im_batches = [torch.cat((im_batches[i * batch_size: min((i + 1) * batch_size,
                                                                len(im_batches))])) for i in range(num_batches)]

    #开始探测
    write = 0
    start_det_loop = time.time()
    for i, batch in enumerate(im_batches):
        # load the image
        start = time.time()
        if CUDA:
            batch = batch.cuda()

        prediction = model(Variable(batch, volatile=True), CUDA)

        prediction = write_results(prediction, confidence, num_classes, nms_conf=nms_thesh)

        end = time.time()

        # 说明不存在探测对象,跳过 只返回了个 0
        if type(prediction) == int:

            for im_num, image in enumerate(imlist[i * batch_size: min((i + 1) * batch_size, len(imlist))]):
                im_id = i * batch_size + im_num
                print("{0:20s} predicted in {1:6.3f} seconds".format(image.split("/")[-1], (end - start) / batch_size))
                print("{0:20s} {1:s}".format("Objects Detected:", ""))
                print("----------------------------------------------------------")
            continue
예제 #25
0
파일: models.py 프로젝트: phymhan/mocogan
 def initHidden(self, batch_size):
     self.hidden = Variable(torch.zeros(batch_size, self.hidden_size))
     if self._gpu == True:
         self.hidden = self.hidden.cuda()
예제 #26
0
    def train(self, dataset):
        # Optimizers
        self.optimizer_G = torch.optim.Adam(
            itertools.chain(
                self.encoder.parameters(),
                self.decoder.parameters()
            ),
            lr=self.config.train.lr,
            betas=(self.config.train.b1, self.config.train.b2)
        )
        self.optimizer_D = torch.optim.Adam(
            self.discriminator.parameters(),
            lr=self.config.train.lr,
            betas=(self.config.train.b1, self.config.train.b2))

        # tensorboard callback
        self.writer = SummaryWriter(os.path.join(self.output, 'log'))

        self.running_loss_g = 0
        self.running_loss_d = 0

        dataloader = dataset.dataloader(tensor=self.Tensor)
        for epoch in tqdm(range(self.config.train.n_epochs), total=self.config.train.n_epochs, desc='Epoch',
                          leave=True):
            for batch in tqdm(dataloader, total=len(dataloader), desc='Bath'):
                imgs = batch.reshape(-1, self.img_shape[-2], self.img_shape[-1])
                # Adversarial ground truths
                valid = Variable(self.Tensor(imgs.shape[0], 1).fill_(1.0), requires_grad=False)
                fake = Variable(self.Tensor(imgs.shape[0], 1).fill_(0.0), requires_grad=False)

                # Configure input
                real_imgs = Variable(imgs.type(self.Tensor))
                self.optimizer_G.zero_grad()

                encoded_imgs = self.encoder(real_imgs)
                decoded_imgs = self.decoder(encoded_imgs)

                # Loss measures generator's ability to fool the discriminator
                g_loss = \
                    0.001 * self.adversarial_loss(self.discriminator(encoded_imgs), valid) + \
                    0.999 * self.pixelwise_loss(decoded_imgs, real_imgs)
                g_loss.backward()
                self.optimizer_G.step()
                self.running_loss_g += g_loss.item()

                self.optimizer_D.zero_grad()

                # Sample noise as discriminator ground truth
                z = Variable(self.Tensor(np.random.normal(0, 1, (imgs.shape[0], self.config.struct.latent_dim))))

                # Measure discriminator's ability to classify real from generated samples
                real_loss = self.adversarial_loss(self.discriminator(z), valid)
                fake_loss = self.adversarial_loss(self.discriminator(encoded_imgs.detach()), fake)
                d_loss = 0.5 * (real_loss + fake_loss)

                d_loss.backward()
                self.optimizer_D.step()
                self.running_loss_d += d_loss.item()
            if epoch % 10 == 0:
                save_path = '/root/weights'
                torch.save(self.encoder.state_dict(), os.path.join(save_path, f'encoder_{self.name}_{epoch}'))
                torch.save(self.decoder.state_dict(), os.path.join(save_path, f'decoder_{self.name}_{epoch}'))
                torch.save(self.discriminator.state_dict(),
                           os.path.join(save_path, f'discriminator_{self.name}_{epoch}'))
            self.tensorboard_callback(epoch, len(dataloader))
        self.writer.close()
예제 #27
0
        net = torch.nn.DataParallel(net,
                                    device_ids=range(
                                        torch.cuda.device_count()))
        cudnn.benchmark = True

    net.eval()
    net.training = False
    test_loss = 0
    correct = 0
    total = 0

    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(testloader):
            if use_cuda:
                inputs, targets = inputs.cuda(), targets.cuda()
            inputs, targets = Variable(inputs), Variable(targets)
            outputs = net(inputs)

            _, predicted = torch.max(outputs.data, 1)
            total += targets.size(0)
            correct += predicted.eq(targets.data).cpu().sum()

        acc = 100. * correct / total
        print("| Test Result\tAcc@1: %.2f%%" % (acc))

    sys.exit(0)

# Model
print('\n[Phase 2] : Model setup')
if args.resume:
    # Load checkpoint
예제 #28
0
criterion = nn.MSELoss()  # 定义损失函数为MSE(最小平方误差函数)
optimizer = optim.SGD(model.parameters(), lr=learning_rate)  # 定义迭代优化算法为随机梯度下降算法

plt.ion()  # 使图像变为交互模式
plt.figure()  # 创建画布

loss_train = []  # 存储训练损失的数组
loss_test = []

if not LOAD_MODEL:  # 训练
    plt.scatter(avg_dBZ, real_RI, c='b', s=3)  # 绘制散点图

    eval_loss = 0

    if cuda_available:  # 将训练转移到GPU上进行
        inputs = Variable(dBZ).cuda()
        factors = Variable(factor).cuda()
        values = Variable(RI).cuda()
    else:
        inputs = Variable(dBZ)
        factors = Variable(factor)
        values = Variable(RI)
    inputs = inputs.view(inputs.size(0), 1, inputs.size(1), inputs.size(2))

    if cuda_available:
        tinputs = Variable(tdBZ).cuda()
        tfactors = Variable(tfactor).cuda()
        tvalues = Variable(tRI).cuda()
    else:
        tinputs = Variable(tdBZ)
        tfactors = Variable(tfactor)
예제 #29
0
def train_vae(logger=None):

  out_dir, listdir, featslistdir = get_dirpaths(args)
  batchsize = args.batchsize 
  hiddensize = args.hiddensize 
  nmix = args.nmix 
  nepochs = args.epochs
 
  data = colordata(\
    os.path.join(out_dir, 'images'), \
    listdir=listdir,\
    featslistdir=featslistdir,
    split='train')

  nbatches = np.int_(np.floor(data.img_num/batchsize))

  data_loader = DataLoader(dataset=data, num_workers=args.nthreads,\
    batch_size=batchsize, shuffle=True, drop_last=True)

  model = VAE()
  model.cuda()
  model.train(True)

  optimizer = optim.Adam(model.parameters(), lr=5e-5)

  itr_idx = 0
  for epochs in range(nepochs):
    train_loss = 0.

    for batch_idx, (batch, batch_recon_const, batch_weights, batch_recon_const_outres, _) in \
      tqdm(enumerate(data_loader), total=nbatches):

      input_color = Variable(batch).cuda()
      lossweights = Variable(batch_weights).cuda()
      lossweights = lossweights.view(batchsize, -1)
      input_greylevel = Variable(batch_recon_const).cuda()
      z = Variable(torch.randn(batchsize, hiddensize))
 
      optimizer.zero_grad()
      mu, logvar, color_out = model(input_color, input_greylevel, z)
      kl_loss, recon_loss, recon_loss_l2 = \
        vae_loss(mu, logvar, color_out, input_color, lossweights, batchsize)
      loss = kl_loss.mul(1e-2)+recon_loss
      recon_loss_l2.detach()
      loss.backward()
      optimizer.step()

      train_loss = train_loss + recon_loss_l2.data[0]

      if(logger): 
        logger.update_plot(itr_idx, \
          [kl_loss.data[0], recon_loss.data[0], recon_loss_l2.data[0]], \
          plot_type='vae')
        itr_idx += 1

      if(batch_idx % args.logstep == 0):
        data.saveoutput_gt(color_out.cpu().data.numpy(), \
          batch.numpy(), \
          'train_%05d_%05d' % (epochs, batch_idx), \
          batchsize, \
          net_recon_const=batch_recon_const_outres.numpy())
 
    train_loss = (train_loss*1.)/(nbatches)
    print('[DEBUG] VAE Train Loss, epoch %d has loss %f' % (epochs, train_loss)) 

    test_loss = test_vae(model) 
    if(logger):
      logger.update_test_plot(epochs, test_loss)
    print('[DEBUG] VAE Test Loss, epoch %d has loss %f' % (epochs, test_loss)) 

    torch.save(model.state_dict(), '%s/models/model_vae.pth' % (out_dir))
예제 #30
0
def mask2d(B, D, keep_prob, cuda=True):
    m = torch.floor(torch.rand(B, D) + keep_prob) / keep_prob
    m = Variable(m, requires_grad=False)
    if cuda:
        m = m.cuda()
    return m