示例#1
0
def test(model, loader, attacker, criterion, device):
    model.eval()

    N = len(loader.dataset)
    pred_success, attack_success, loss = 0, 0, 0.
    with torch.no_grad():
        for itr, data in enumerate(loader):
            input, target = data[0], data[1]

            noise = torch.randn_like(input) * 0.05
            input = input + noise
            input = torch.clamp(input, 0., 1.)
            input = input.to(device)
            target = target.to(device)

            logit = model(input)

            Loss = criterion(logit, target)
            loss += Loss.item() * input.shape[0]

            pred = torch.argmax(logit, dim=1)
            pred_success += pred.eq(target).sum().item()

            with torch.enable_grad():
                input_attack = attacker.attack(inputs=input,
                                               model=model,
                                               labels=target,
                                               targeted=None)

            logit_attack = model(input_attack)
            pred_attack = torch.argmax(logit_attack, dim=1)
            attack_success += (~pred_attack.eq(target)).sum().item()

    # pred_succes should > 0
    return pred_success / N, attack_success / N, loss / N
示例#2
0
 def on_button_click1(self,df):
     dataset_location=self.entry1.get()
     loc = dataset_location.split("/")
     filename= loc[-1]
     dest_loc = "./"+filename
     shutil.copyfile(dataset_location,dest_loc)
     preprocessing.missing_value_filling(filename)
     df = feature_engineering.feature_engineering(df)
     todo = 'classfication' #nlp module will be sending this
     models.model(df, todo)
def predict_1batch(model, inputs, tta):
    inputs = inputs.cuda()
    outputs = model(inputs)
    if isinstance(outputs, tuple):
        outputs, logits_fc = outputs
        B, C, H, W = outputs.shape
    else:
        B, C, H, W = outputs.shape
        logits_fc = outputs.view(B, C, -1)
        logits_fc = torch.max(logits_fc, -1)[0]
    logits_fc = logits_fc.view(B, C)

    cls_pred = F.sigmoid(logits_fc).view(-1).data.cpu().numpy()
    mask_pred = F.sigmoid(outputs).view(-1, H, W).data.cpu().numpy()

    if tta >= 2:  # h flip
        # mask
        outputs = model(inputs.flip(3))
        if isinstance(outputs, tuple):
            outputs, logits_fc = outputs
        else:
            logits_fc = outputs.view(B, C, -1)
            logits_fc = torch.max(logits_fc, -1)[0]
        logits_fc = logits_fc.view(B, C)

        outputs = outputs.flip(3)
        mask_pred += F.sigmoid(outputs).view(-1, H, W).data.cpu().numpy()
        # cls
        cls_pred += F.sigmoid(logits_fc).view(-1).data.cpu().numpy()
    if tta == 3:  # v flip
        # mask
        outputs = model(inputs.flip(2))
        if isinstance(outputs, tuple):
            outputs, logits_fc = outputs
        else:
            logits_fc = outputs.view(B, C, -1)
            logits_fc = torch.max(logits_fc, -1)[0]
        logits_fc = logits_fc.view(B, C)
        outputs = outputs.flip(2)
        mask_pred += F.sigmoid(outputs).view(-1, H, W).data.cpu().numpy()
        # cls
        logits_fc = outputs.view(B, C, -1)
        logits_fc = torch.max(logits_fc, -1)[0]
        cls_pred += F.sigmoid(logits_fc).view(-1).data.cpu().numpy()

    if tta != 0:
        cls_pred /= tta
        mask_pred /= tta
    return mask_pred, cls_pred
示例#4
0
def test(test_loader, model, folds, all_train_loader=None):
    #     sample_submission_df = pd.read_csv("./dataset/test_visit.csv")
    #3.1 confirm the model converted to cuda
    filenames, labels_te, labels_tr, submissions = [], [], [], []
    labels_true = []
    model.cuda()
    model.eval()
    submit_results = []
    try:
        with tqdm(test_loader, ascii=True) as t:
            for img_data, visit_data, filepath in t:
                #3.2 change everything to cuda and get only basename
                filepath = [x for x in filepath]
                with torch.no_grad():
                    image_var = img_data.cuda(non_blocking=True)
                    visit_var = visit_data.cuda(non_blocking=True)
                    y_pred = model(image_var, visit_var)
                    label = y_pred.sigmoid().cpu().data.numpy()
                    #print(label > 0.5)

                    submit_results.append(np.argmax(label, 1))
                    labels_te.append(np.squeeze(label))

                    filenames.append(np.squeeze(filepath))

        if not isinstance(all_train_loader, type(None)):
            with tqdm(all_train_loader, ascii=True) as t:
                for img_data, visit_data, target in t:
                    #3.2 change everything to cuda and get only basename
                    target = np.squeeze(target).cpu().data.numpy()
                    with torch.no_grad():
                        image_var = img_data.cuda(non_blocking=True)
                        visit_var = visit_data.cuda(non_blocking=True)
                        y_pred = model(image_var, visit_var)
                        label = y_pred.sigmoid().cpu().data.numpy()
                        labels_tr.append(
                            np.hstack(
                                [np.squeeze(label),
                                 target.reshape(-1, 1)]))
            return np.concatenate(labels_te), np.concatenate(labels_tr)
    except:
        t.close()
        raise
    t.close()

    # 	sample_submission_df['Predicted'] = np.concatenate(submit_results)
    # 	sample_submission_df['IterId'] = np.concatenate(filenames)
    # 	sample_submission_df.to_csv('./submit/%s_bestloss_submission%s.csv'%(config.model_name, folds), index=None)
    return np.concatenate(labels_te), None
示例#5
0
def evaluate(split, verbose=False, n_batches=None):
    """
    Compute loss on val or test data.
    """
    model.eval()
    loss = 0
    acc = 0
    correct = 0
    n_examples = 0
    if split == "val":
        loader = val_loader
    elif split == "test":
        loader = test_loader
    for batch_i, batch in enumerate(loader):
        data, target = batch
        data, target = (
            Variable(data).to(device=device),
            Variable(target).to(device=device),
        )
        output = model(data)
        loss += criterion(output, target).item()
        acc += (np.sum(
            output.cpu().data.numpy()[target.cpu().data.numpy() != 0] > 0.5) +
                np.sum(output.cpu().data.numpy()[target.cpu().data.numpy(
                ) == 0] < 0.5)) / float(args.im_size[1] * args.im_size[2])
        n_examples += output.size(0)

        if n_batches and (batch_i == n_batches - 1):
            break

    loss /= n_examples
    acc /= n_examples
    return loss, acc
示例#6
0
def evaluate(val_loader, model, optimizer, criterion, epoch):
    # 2.1 为损失和准确率创建“标尺”(拥有计数、求和、求平均功能)
    losses = AverageMeter()
    top1 = AverageMeter()
    # 2.2 创建进度条
    val_progressor = ProgressBar(mode="Val  ", epoch=epoch, total_epoch=config.epochs, model_name=config.model_name,
                                 total=len(val_loader)*config.batch_size)
    # 2.3 验证过程
    model.cuda()
    model.eval()
    with torch.no_grad():
        for i, (input, target) in enumerate(val_loader):
            val_progressor.current = i*config.batch_size
            input = input.cuda()
            target = torch.from_numpy(np.array(target)).long().cuda()  # 范式
            output = model(input)  # 将input输入模型得到预测输出
            loss = criterion(output, target)  # 根据预测和真实标签求损失
            # 2.3.2 计算准确率并实时更新进度条的显示内容
            precision = accuracy(output, target)
            losses.update(loss.item(), input.size(0))
            top1.update(precision[0], input.size(0))
            val_progressor.current_loss = losses.avg
            val_progressor.current_top1 = top1.avg
            val_progressor.current_lr = get_learning_rate(optimizer)
            val_progressor()
        val_progressor.done()
    return [losses.avg, top1.avg]
示例#7
0
def evaluate(data_loader):
    model.eval()
    total_loss = 0
    accBF = 0.0
    accBC = 0.0
    with torch.no_grad():
        for idx, (cpu_images, cpu_texts) in enumerate(data_loader):
            batch_size = cpu_images.size(0)
            util.loadData(image, cpu_images)
            t, l = converter.encode(cpu_texts)
            util.loadData(text, t)
            util.loadData(length, l)
            output = model(image)
            output_size = Variable(
                torch.LongTensor([output.size(0)] * batch_size))
            loss = criterion(output, text, output_size, length) / batch_size
            total_loss += loss
            _, output = output.max(2)
            output = output.transpose(1, 0).contiguous().view(-1)
            sim_preds = converter.decode(output.data,
                                         output_size.data,
                                         raw=False)
            accBF += metric.by_field(sim_preds, cpu_texts)
            accBC += metric.by_char(sim_preds, cpu_texts)
        total_loss /= len(data_loader)
        return total_loss, accBF / len(data_loader), accBC / len(data_loader)
示例#8
0
def train(data_loader):
    total_loss = 0
    model.train()
    data_loader = tqdm(data_loader)
    for idx, (cpu_images, cpu_texts) in enumerate(data_loader):
        batch_size = cpu_images.size(0)
        util.loadData(image, cpu_images)
        t, l = converter.encode(cpu_texts)
        util.loadData(text, t)
        util.loadData(length, l)
        output = model(image)
        output_size = Variable(torch.LongTensor([output.size(0)] * batch_size))
        loss = criterion(output, text, output_size, length) / batch_size
        optimizer.zero_grad()
        loss.backward()
        clipping_value = 1.0
        torch.nn.utils.clip_grad_norm_(model.parameters(), clipping_value)
        if not (torch.isnan(loss) or torch.isinf(loss)):
            optimizer.step()
        total_loss += loss.item()
        if idx % 1000 == 0 and idx != 0:
            print('{} index: {}/{}(~{}%) loss: {}'.format(
                datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), idx,
                len(data_loader), round(idx * 100 / len(data_loader)),
                total_loss / idx))
    return total_loss / len(data_loader)
示例#9
0
def search_thresholds(all_loader, model):
    model.cuda()
    model.eval()
    all_target = []
    all_pred = []
    with torch.no_grad():
        for i, (images, target) in enumerate(tqdm(all_loader)):
            images_var = images.cuda(non_blocking=True)
            output = model(images_var)
            pred = output.sigmoid().cpu().data.numpy()
            all_target.extend(np.array(target).tolist())
            all_pred.extend(pred.tolist())

    print(np.array(all_target).shape)
    print(np.array(all_pred).shape)
    thresholds = np.linspace(0, 1, 100)
    test_threshold = 0.5 * np.ones(28)
    best_threshold = np.zeros(28)
    best_val = np.zeros(28)
    for i in range(28):
        for threshold in thresholds:
            test_threshold[i] = threshold
            score = f1_score(np.array(all_target),
                             np.array(all_pred) > test_threshold,
                             average='macro')
            if score > best_val[i]:
                best_threshold[i] = threshold
                best_val[i] = score
        print("Threshold[%d] %0.6f, F1: %0.6f" %
              (i, best_threshold[i], best_val[i]))
        test_threshold[i] = best_threshold[i]
    print("Best threshold: ")
    print(best_threshold)
    print("Best f1:")
    print(best_val)
示例#10
0
def test(test_loader, model, folds):
    correct = 0
    total = 0
    # confirm the model converted to cuda
    model.cuda()
    model.eval()
    for i, (inputs, labels) in enumerate(test_loader):
        # change everything to cuda
        with torch.no_grad():
            inputs = Variable(inputs).cuda()
            labels = Variable(labels).cuda()

            # print(inputs, inputs.shape)
            outputs = model(inputs)

            # Mapping outputs to (0,1), The sum of each row is 1
            smax = nn.Softmax(1)
            smax_out = smax(outputs)

            # Return the element with the largest value in each row and it's index
            _, pred_labels = torch.max(smax_out, 1)
            # print("pred_label:", pred_labels, "pred:", pred)

            total += labels.size(0)
            correct += (pred_labels == labels.long()).sum().item()

    accuracy = 100.0 * float(correct) / float(total)
    print("correct:", correct)
    print("total:", total)
    print('Accuracy of the network on the test set: %.3f%%' % (
        accuracy))
示例#11
0
def call_classifier(img):
    

    image = transforms.ToPILImage()(img)
    image=transforms.Resize((224,224))(image)    
    
    # initialize the computation device
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 
    image=transforms.ToTensor()(image).unsqueeze(0).to(device)
    
    
    #intialize the model
    model = models.model(pretrained=False, requires_grad=False).to(device)
    # load the model checkpoint
    checkpoint = torch.load('./output/model.pth')
    
    # load model weights state_dict
    model.load_state_dict(checkpoint['model_state_dict'])
    model.eval()
    
    
    outputs=model(image)
    outputs = torch.sigmoid(outputs)
    outputs = outputs.detach().cpu()
    
    if np.array(outputs[0][0])>0.5 and np.array(outputs[0][1])<0.5:
            print("DANGER cat")
    elif np.array(outputs[0][0])<0.5 and np.array(outputs[0][1])>0.5:
            print("bird")
    elif np.array(outputs[0][0])>0.5 and np.array(outputs[0][1])>0.5:
            print("DANGER cat and bird") 
示例#12
0
def make_model(img, results, mask):
    confs = results[:, :, :, :n_pos]
    pafs = results[:, :, :, n_pos:]
    m1 = tf_repeat(mask, [1, 1, 1, n_pos])
    m2 = tf_repeat(mask, [1, 1, 1, n_pos * 2])
    cnn, b1_list, b2_list, net = model(img, n_pos, m1, m2, True, False)
    # define loss
    losses = []
    last_losses_l1 = []
    last_losses_l2 = []
    stage_losses = []

    for idx, (l1, l2) in enumerate(zip(b1_list, b2_list)):
        loss_l1 = tf.nn.l2_loss((l1.outputs - confs) * m1)
        loss_l2 = tf.nn.l2_loss((l2.outputs - pafs) * m2)

        losses.append(tf.reduce_mean([loss_l1, loss_l2]))
        stage_losses.append(loss_l1 / batch_size)
        stage_losses.append(loss_l2 / batch_size)

    last_conf = b1_list[-1].outputs
    last_paf = b2_list[-1].outputs
    last_losses_l1.append(loss_l1)
    last_losses_l2.append(loss_l2)
    L2 = 0.0

    for p in tl.layers.get_variables_with_name('kernel', True, True):
        L2 += tf.contrib.layers.l2_regularizer(0.0005)(p)
    total_loss = tf.reduce_sum(losses) / batch_size + L2

    return total_loss, last_conf, stage_losses, L2, cnn, last_paf, img, confs, pafs, m1, net
示例#13
0
def evaluate(val_loader, model, criterion, epoch):
    # 2.1 define meters
    losses = AverageMeter()
    top1 = AverageMeter()
    # progress bar
    val_progressor = ProgressBar(mode="Val  ",
                                 epoch=epoch,
                                 total_epoch=config.epochs,
                                 model_name=config.model_name,
                                 total=len(val_loader))
    # 2.2 switch to evaluate mode and confirm model has been transfered to cuda
    model.cuda()
    # model.eval()
    with torch.no_grad():
        for i, (input, target) in enumerate(val_loader):
            val_progressor.current = i
            input = Variable(input).cuda()
            target = Variable(torch.from_numpy(np.array(target)).long()).cuda()
            # target = Variable(target).cuda()
            # 2.2.1 compute output
            output = model(input)
            loss = criterion(output, target)

            # 2.2.2 measure accuracy and record loss
            precision1, precision2 = accuracy(output, target, topk=(1, 2))
            losses.update(loss.item(), input.size(0))
            top1.update(precision1[0], input.size(0))
            val_progressor.current_loss = losses.avg
            val_progressor.current_top1 = top1.avg
            val_progressor()
        val_progressor.done()
    return [losses.avg, top1.avg]
示例#14
0
def train():
    for epoch in range(0, num_epochs):
        batch_num = 0
        for i, batch in enumerate(train_dataloader):
            if batch_num > max_batches_per_epoch:
                break
            # split batch into input, target and covariates. Target is simply the input shifted forward by one time step
            input, target, covariates = utils.split_batch(batch)
            # input in the conditioning window. For the prediction window, model output will be conditioned on the
            # model output for the previous step
            input_cond = input[:, 0:cond_win_len, :]
            # Scale the input and oovariates so they are 0 mean and range = 1. Note we apply scaling per batch, not for the
            # entire dataset. Contrast this with training imagenet models, where the scaling params are calculated over
            # the entire training dataset
            input_cond, covariates = utils.scale(input_cond, covariates)
            optimizer.zero_grad()
            # Run the forward pass
            out = model(input_cond, covariates, future=pred_win_len)
            # scale model output and target to the original data scale.
            out = utils.invert_scale(out)
            # calculate MSE loss, run backward pass, update parameters.
            loss = criterion(out[:], target[:])

            loss.backward()
            scheduler.step()
            optimizer.step()
            loss = np.sqrt(loss.item())
            losses.append(loss)
            print('epoch:{0}/{1}, step:{2}, loss:{3}'.format(
                epoch, num_epochs, batch_num, loss))
            batch_num = batch_num + 1

    return model, losses
示例#15
0
def train(train_loader, model, criterion, optimizer, epoch, valid_loss,
          best_results, start):
    losses = AverageMeter()
    f1 = AverageMeter()
    model.train()
    for i, (images, target) in enumerate(train_loader):
        images = images.cuda(non_blocking=True)
        target = torch.from_numpy(
            np.array(target)).float().cuda(non_blocking=True)
        # compute output
        output = model(images)
        loss = criterion(output, target)
        losses.update(loss.item(), images.size(0))

        f1_batch = f1_score(target,
                            output.sigmoid().cpu() > 0.15,
                            average='macro')
        f1.update(f1_batch, images.size(0))
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print('\r', end='', flush=True)
        message = '%s %5.1f %6.1f         |         %0.3f  %0.3f           |         %0.3f  %0.4f         |         %s  %s    | %s' % (\
                "train", i/len(train_loader) + epoch, epoch,
                losses.avg, f1.avg,
                valid_loss[0], valid_loss[1],
                str(best_results[0])[:8],str(best_results[1])[:8],
                time_to_str((timer() - start),'min'))
        print(message, end='', flush=True)
    log.write("\n")
    #log.write(message)
    #log.write("\n")
    return [losses.avg, f1.avg]
示例#16
0
def test(model: nn.Module, test_loader: DataLoader, criterion, last_layer=None, device=torch.device('cpu')):
    model.eval()
    # model.to(device)
    # device = model.device

    N = len(test_loader.dataset)

    loss = 0.
    acc = 0.
    n = 0
    with torch.no_grad():
        for itr, data in enumerate(test_loader):
            input, target = data[0].to(device), data[1].to(device)

            output = model(input)

            if last_layer is not None:
                if isinstance(output, list) or isinstance(output, tuple):
                    for i in range(len(output)):
                        output[i] = last_layer(output[i])


            Loss = criterion(output, target)

            loss += Loss.item() * input.shape[0]
            n += input.shape[0]

            pred = torch.argmax(output, dim=1)
            acc += pred.eq(target).sum().item()

    return loss / n, acc / n 
示例#17
0
def test(test_loader, model, folds):
    sample_submission_df = pd.read_csv("/mfs/human/sample_submission.csv")
    #3.1 confirm the model converted to cuda
    filenames, labels, submissions = [], [], []
    model.cuda()
    model.eval()
    submit_results = []
    for i, (input, filepath) in enumerate(tqdm(test_loader)):
        #3.2 change everything to cuda and get only basename
        filepath = [os.path.basename(x) for x in filepath]
        with torch.no_grad():
            image_var = input.cuda(non_blocking=True)
            y_pred = model(image_var)
            label = y_pred.sigmoid().cpu().data.numpy()
            #print(label > 0.5)

            labels.append(label > 0.15)
            filenames.append(filepath)

    for row in np.concatenate(labels):
        subrow = ' '.join(list([str(i) for i in np.nonzero(row)[0]]))
        submissions.append(subrow)
    sample_submission_df['Predicted'] = submissions
    sample_submission_df.to_csv('./submit/%s_bestloss_submission.csv' %
                                config.model_name,
                                index=None)
示例#18
0
文件: main.py 项目: tantan1379/mygit
def val(args, model, criterion, dataloader, epoch, k_fold):
    with torch.no_grad():
        model.eval()
        top1_m = u.AverageMeter()
        loss_m = u.AverageMeter()
        # precision_m = u.AverageMeter()
        # recall_m = u.AverageMeter()
        # f1_m = u.AverageMeter()
        eval_progressor = pb.Val_ProgressBar(mode='val',
                                             fold=k_fold,
                                             model_name=args.model_name,
                                             epoch=epoch + 1,
                                             total=len(dataloader))
        for i, (data, label) in enumerate(dataloader):
            eval_progressor.current = i
            if torch.cuda.is_available() and args.use_gpu:
                data = data.cuda()
                label = label.cuda()
            predict = model(data)
            loss = criterion(predict, label)
            top1 = u.accuracy(predict, label)
            top1_m.update(top1[0].item(), data.size(0))
            acc = top1_m.avg
            loss_m.update(loss.item())
            loss = loss_m.avg
            eval_progressor.val = [acc, loss, 0, 0]
            eval_progressor()
        eval_progressor.done()
    return acc, 0, 0, 0
示例#19
0
def test(test_loader, model, folds):
    sample_submission_df = pd.read_csv(config.sample_submission)
    #1.1 confirm the model converted to cuda
    filenames, labels, submissions = [], [], []
    model.cuda()
    model.eval()
    submit_results = []
    for i, (input, filepath) in enumerate(tqdm(test_loader)):
        #1.2 change everything to cuda and get only basename
        filepath = [os.path.basename(x) for x in filepath]
        probs = []
        for k in range(config.n_tta):
            with torch.no_grad():
                image_var = input.cuda(non_blocking=True)
                y_pred = model(image_var)
                prob = y_pred.sigmoid().cpu().data.numpy()
                probs.append(prob)
        probs_agg = np.vstack(probs).mean(axis=0)
        preds = probs_agg > config.thresold
        if len(preds) == 0: preds = [np.argmax(probs_agg)]

        subrow = ' '.join(list([str(i) for i in np.nonzero(preds)[0]]))
        if len(subrow) == 0:
            subrow = np.argmax(probs_agg)
        submissions.append(subrow)

    sample_submission_df['Predicted'] = submissions
    sample_submission_df.to_csv('./submit/{}_bestloss_submission.csv'.format(
        config.model_name),
                                index=None)
def evaluate(data, mode):
    if mode == 'val_mode':
        bsz = val_batch_size
    elif mode == 'test_mode':
        bsz = test_batch_size
    global loss_least
    global num_tokens
    model.eval()
    total_loss = 0
    hidden = model.init_hidden(bsz)
    for i in range(0, data.size(0) - 1, BPTT):
        X, Y = batching.get_batch(data, i, evaluation=True)
        output, hidden = model(X, hidden)
        predictions = output.view(-1, num_tokens)
        total_loss += len(X) * criterion(predictions, Y).data
        hidden = repackage_hidden(hidden)
    final_loss = total_loss[0] / len(data)
    print("Epoch: " + str(epoch) + " Val Loss: " + str(final_loss) +
          " Val Perplexity: " + str(math.exp(final_loss)))
    '''
    if final_loss < loss_least:
            with open(MODEL_SAVE_PATH, 'wb') as f:
                torch.save(model, f)
            loss_least = final_loss
    '''
    return final_loss
示例#21
0
文件: main.py 项目: CH-LIU/ML2018FALL
def evaluate(val_loader,model,criterion,epoch,train_loss,best_results,start):
    # only meter loss and f1 score
    losses = AverageMeter()
    f1 = AverageMeter()
    # switch mode for evaluation
    model.cuda()
    model.eval()
    with torch.no_grad():
        for i, (images,target) in enumerate(val_loader):
            images_var = images.cuda(non_blocking=True)
            target = torch.from_numpy(np.array(target)).float().cuda(non_blocking=True)
            #image_var = Variable(images).cuda()
            #target = Variable(torch.from_numpy(np.array(target)).long()).cuda()
            output = model(images_var)
            loss = criterion(output,target)
            losses.update(loss.item(),images_var.size(0))
            f1_batch = f1_score(target,output.sigmoid().cpu().data.numpy() > 0.15,average='macro')
            f1.update(f1_batch,images_var.size(0))
            print('\r',end='',flush=True)
            message = '%s   %5.1f %6.1f         |         %0.3f  %0.3f           |         %0.3f  %0.4f         |         %s  %s    | %s' % (\
                    "val", i/len(val_loader) + epoch, epoch,                    
                    train_loss[0], train_loss[1], 
                    losses.avg, f1.avg,
                    str(best_results[0])[:8],str(best_results[1])[:8],
                    time_to_str((timer() - start),'min'))

            print(message, end='',flush=True)
        log.write("\n")
        #log.write(message)
        #log.write("\n")
        
    return [losses.avg,f1.avg]
示例#22
0
def evaluate(split, verbose=False, n_batches=None):
    '''
    Compute loss on val or test data.
    '''
    model.eval()
    loss = 0
    acc = 0
    correct = 0
    n_examples = 0
    if split == 'val':
        loader = val_loader
    elif split == 'test':
        loader = test_loader
    for batch_i, batch in enumerate(loader):
        data, target = batch
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data)
        loss += criterion(output, target).item()
        acc += (np.sum(output.cpu().data.numpy()[target.cpu().data.numpy()!=0] > 0.5) \
            + np.sum(output.cpu().data.numpy()[target.cpu().data.numpy()==0] < 0.5)) / float(args.im_size[1]*args.im_size[2])
        n_examples += output.size(0)

        if n_batches and (batch_i == n_batches-1):
            break

    loss /= n_examples
    acc /= n_examples
    return loss, acc
示例#23
0
def test(test_loader, model, folds):
    # 3.1 confirm the model converted to cuda
    csv_map = OrderedDict({"filename": [], "probability": []})
    model.cuda()
    model.eval()
    for i, (input, filepath) in enumerate(tqdm(test_loader)):
        # 3.2 change everything to cuda and get only basename
        filepath = [os.path.basename(x) for x in filepath]
        with torch.no_grad():
            image_var = Variable(input).cuda()
            # 3.3.output
            # print(filepath)
            # print(input,input.shape)
            y_pred = model(image_var)
            print(y_pred.shape)
            smax = nn.Softmax(1)
            smax_out = smax(y_pred)
        # 3.4 save probability to csv files
        csv_map["filename"].extend(filepath)
        for output in smax_out:
            prob = ";".join([str(i) for i in output.data.tolist()])
            csv_map["probability"].append(prob)
    result = pd.DataFrame(csv_map)
    result["probability"] = result["probability"].map(
        lambda x: [float(i) for i in x.split(";")])
    result.to_csv("./submit/{}_submission.csv".format(config.model_name + "_" +
                                                      str(folds)),
                  index=False,
                  header=None)
示例#24
0
def eval(model, dataLoader_valid):
    with torch.no_grad():
        model.eval()
        model.mode = 'valid'
        valid_loss, index_valid= 0, 0
        top1, top3, map3 = 0,0,0
        for valid_data in dataLoader_valid:
            images, labels = valid_data
            images = images.cuda()
            labels = labels.cuda()
            results = model(images)
            top1_, top3_ = accuracy(results, labels, topk=(1,3))
            map3_ = mapk(labels, results, k=3)
            model.getLoss(results,labels)
            valid_loss += model.loss.data.cpu().numpy()
            top1 += top1_
            top3 += top3_
            map3 += map3_
            index_valid += 1


        valid_loss /= index_valid
        top1 /= index_valid
        top3 /= index_valid
        map3 /= index_valid


        return valid_loss, top1, top3, map3
示例#25
0
def valid(model, criterion,converter, device, datasets, dataset_names):
    print('start evaluate')

    accs = {}
    losses = {}
    dataloaders = {}
    batch_dict  = {'print_word': 48, 'hand_num': 52, 'print_num': 48, 'symbol': 88, 'hand_word': 64, 'seal': 64, 'catword': 48}
    for dataset_name in dataset_names:
        dataset = datasets.get(dataset_name)

        dataloader = DataLoader(dataset, batch_size=batch_dict.get(dataset_name),
                                 shuffle=True, num_workers=0, drop_last=False)
        dataloaders[dataset_name] = dataloader

    model.eval()
    with torch.no_grad():
        for dataset_name in dataset_names:
            all_cnt = 0
            n_correct = 0
            total_loss = 0
            total_cnt = 0
            dataloader = dataloaders.get(dataset_name)

            for img, label, img_names in dataloader:

                total_cnt += 1
                img = img.to(device)
                batch_size = img.size(0)
                all_cnt += batch_size
                text, length = converter.encode(label)
                preds = model(img)

                preds_size = torch.IntTensor([preds.size(0)]*batch_size)
                preds = preds.to('cpu')
                text, length = text.to('cpu'), length.to('cpu')
                loss = criterion(preds, text, preds_size, length)
                total_loss += loss.item()


                _, preds = preds.max(2)
                preds = preds.transpose(1, 0).contiguous().view(-1)
                sim_preds = converter.decode(preds.data, preds_size.data, raw=False)

                list1 = [x for x in label]
                for pred, target, img_name in zip(sim_preds, list1, img_names):
                    if pred == target:
                        n_correct += 1
                    #else:
                        #print(img_name + '  label:' + target + '  pred:' + pred)
                        #img_path = os.path.join('/home/chen-ubuntu/Desktop/checks_dataset/merge2/Image/', img_name)
                        #image = Image.open(img_path)
                        #image.save('test/' + img_name + "_" + target + "_" + pred + ".jpg")

            acc = n_correct/all_cnt
            loss = total_loss/total_cnt
            accs[dataset_name] = acc
            losses[dataset_name] = loss
            print("dataset: {},  acc: {}, loss: {}".format(dataset_name, acc, loss))
            print('imgs count: {},  correct: {}'.format(all_cnt, n_correct))
    return accs, losses
示例#26
0
def evaluate(split, verbose=False, n_batches=None):
    '''
    Compute loss on val or test data.
    '''
    model.eval()
    loss = 0
    acc = 0
    correct = 0
    n_examples = 0
    if split == 'val':
        loader = val_loader
        #loader = train_loader
    elif split == 'test':
        loader = test_loader
    for batch_i, batch in enumerate(loader):
        data, target = batch
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data)
        loss += criterion(output, target).item()
        acc += (np.sum(output.cpu().data.numpy()[target.cpu().data.numpy()!=0] > 0.5) \
            + np.sum(output.cpu().data.numpy()[target.cpu().data.numpy()==0] < 0.5)) / float(args.im_size[1]*args.im_size[2])
        n_examples += output.size(0)

        if n_batches and (batch_i == n_batches - 1):
            break

    loss /= n_examples
    acc /= n_examples
    return loss, acc
示例#27
0
def predict_tf(img, nb_filters, nb_conv, batch_size, wpath, spath):
    """
    the cnn model for image transformation


    Parameters
    ----------
    img : array
        The image need to be calculated


    Returns
    -------
    y_img
        Description.

      """
    img = nor_data(img)
    pn, iy, ix = img.shape
    mdl = model(iy, ix, nb_filters, nb_conv)
    mdl.load_weights(wpath)
    for i in range(pn):
        print('Processing the %s th projection' % i)
        tstart = time.time()
        x_img = img[i]
        x_img = np.reshape(x_img, (1, iy, ix, 1))
        y_img = mdl.predict(x_img, batch_size=batch_size)
        y_img = np.reshape(y_img, (iy, ix))
        fname = spath + 'prj-' + str(i)
        dxchange.write_tiff(y_img, fname, dtype='float32')
        print('The prediction runs for %s seconds' % (time.time() - tstart))
示例#28
0
def test(model, dataset=None, compute_metric=True):
    model.eval()
    if dataset:
        test_loader = dataset
    else:
        time.sleep(0.5)
        print("Loading test data...")
        time.sleep(0.5)
        test_loader = TwitterDataset(test_file,
                                     cache_size=20000000,
                                     use_user_info=use_user_info)
        time.sleep(0.5)
        print("Testing...")
        time.sleep(0.5)
    pred = []
    gt = []
    with torch.no_grad():
        for x, y in test_loader:
            if compute_metric:
                gt.extend(y.numpy())
            pred.extend(model(x).squeeze().cpu().numpy())
    if make_average:
        pred = [sum(pred) / len(pred)] * len(pred)
    if not compute_metric:
        return pred
    pred = np.array(pred, dtype=np.float64)
    ce = log_loss(gt, pred)
    prauc = compute_prauc(pred, gt)
    rce = compute_rce(pred, gt)
    return ce, prauc, rce
示例#29
0
def train_running():
    with tf.name_scope('input'):

        train_batch, train_label_batch, _ = input_data.get_batch(train_txt, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
        val_batch, val_label_batch, _ = input_data.get_batch(val_txt, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

    x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
    y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE])

    model = models.model(x, N_CLASSES)
    model.AlexNet()
    logits = model.fc3

    loss = tools.loss(logits, y_)
    acc = tools.accuracy(logits, y_)
    train_op = tools.optimize(loss, LEARNING_RATE)

    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        summary_op = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        val_writer = tf.summary.FileWriter(logs_val_dir, sess.graph)

        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                    break

                tra_images, tra_labels = sess.run([train_batch, train_label_batch])
                _, tra_loss, tra_acc = sess.run([train_op, loss, acc],
                                                feed_dict={x: tra_images, y_: tra_labels})

                if step % 50 == 0:
                    print('Step %d, train loss = %.4f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc))
                    summary_str = sess.run(summary_op, feed_dict={x: tra_images, y_: tra_labels})
                    train_writer.add_summary(summary_str, step)
                #
                if step % 200 == 0 or (step + 1) == MAX_STEP:
                    val_images, val_labels = sess.run([val_batch, val_label_batch])
                    val_loss, val_acc = sess.run([loss, acc],
                                                 feed_dict={x: val_images, y_: val_labels})

                    print('**  Step %d, val loss = %.4f, val accuracy = %.2f%%  **' % (step, val_loss, val_acc))
                    summary_str = sess.run(summary_op, feed_dict={x: val_images, y_: val_labels})
                    val_writer.add_summary(summary_str, step)
                    #
                if step % 2000 == 0 or (step + 1) == MAX_STEP:
                    checkpoint_path = os.path.join(model_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)

        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
示例#30
0
def train():
    # Turn on training mode which enables dropout.
    total_loss = 0
    begin_t = time.time()
    #ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(BATCH_SIZE)
    batch, i = 0, 0
    while i < train_data.size(0) - 1 - 1:
        bptt = BPTT if np.random.random() < 0.95 else BPTT / 2.
        # Prevent excessively small or negative sequence lengths
        seq_len = max(5, int(np.random.normal(bptt, 5)))
        # There's a very small chance that it could select a very long sequence length resulting in OOM
        # seq_len = min(seq_len, args.bptt + 10)

        lr2 = optimizer.param_groups[0]['lr']
        optimizer.param_groups[0]['lr'] = lr2 * seq_len / BPTT
        model.train()
        data, targets = batching.get_batch(train_data, i, seq_len=seq_len)

        # Starting each batch, we detach the hidden state from how it was previously produced.
        # If we didn't, the model would try backpropagating all the way to start of the dataset.
        hidden = repackage_hidden(hidden)
        optimizer.zero_grad()

        output, hidden, rnn_hs, dropped_rnn_hs = model(data,
                                                       hidden,
                                                       return_h=True)
        raw_loss = criterion(output.view(-1, num_tokens), targets)

        loss = raw_loss
        # Activiation Regularization
        loss = loss + sum(ALPHA * dropped_rnn_h.pow(2).mean()
                          for dropped_rnn_h in dropped_rnn_hs[-1:])
        # Temporal Activation Regularization (slowness)
        loss = loss + sum(BETA * (rnn_h[1:] - rnn_h[:-1]).pow(2).mean()
                          for rnn_h in rnn_hs[-1:])
        loss.backward()

        # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
        torch.nn.utils.clip_grad_norm(model.parameters(), CLIP_GRADIENTS)
        optimizer.step()

        total_loss += raw_loss.data
        optimizer.param_groups[0]['lr'] = lr2
        if batch % DEUBG_LOG_INTERVAL == 0 and batch > 0:
            cur_loss = total_loss[0] / DEUBG_LOG_INTERVAL
            elapsed = time.time() - start_time
            print(
                '| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
                'loss {:5.2f} | ppl {:8.2f}'.format(
                    epoch, batch,
                    len(train_data) // BPTT, optimizer.param_groups[0]['lr'],
                    elapsed * 1000 / DEUBG_LOG_INTERVAL, cur_loss,
                    math.exp(cur_loss)))
            total_loss = 0
            start_time = time.time()
        ###
        batch += 1
        i += seq_len
示例#31
0
def evaluate(model, dev_set):

    model.eval()

    total_loss = 0
    batch_x, batch_t, batch_y1, batch_y2 = [], [], [], []
    y1_true, y2_true = [], []

    label_predictions, gender_predictions = [], []

    with torch.no_grad():
        for i in range(len(dev_set)):

            batch_x.append(dev_set[i]['tokenized'])
            batch_t.append(dev_set[i]['DM'])

            batch_y1.append(dev_set[i]['y1'])
            batch_y2.append(dev_set[i]['y2'])

            y1_true.append(C.label2idx[dev_set[i]['label']])
            y2_true.append(C.gen2idx[dev_set[i]['gen']])

            if len(batch_x) == batch_size or i == len(dev_set) - 1:
                mask = masking(batch_x)
                padded = pad_features(batch_x)

                batch_emoj = np.array(batch_t, dtype='uint16')
                batch_emoj = torch.from_numpy(
                    batch_emoj.astype('int64')).long()

                # out = model(torch.tensor(padded).cuda(), batch_emoj.cuda(), torch.tensor(mask).cuda())
                out = model(
                    torch.tensor(padded).cuda(),
                    torch.tensor(mask).cuda())

                y_pred1 = out['y_pred1'].cpu()
                y_pred2 = out['y_pred2'].cpu()

                label_predictions.extend(
                    list(torch.argmax(y_pred1, -1).numpy()))
                gender_predictions.extend(
                    list(torch.argmax(y_pred2, -1).numpy()))

                loss = F.binary_cross_entropy(y_pred1, torch.Tensor(batch_y1), weight=class_weights1) + \
                       F.binary_cross_entropy(y_pred2, torch.Tensor(batch_y2), weight=class_weights2)

                total_loss += loss.item()

                batch_x, batch_t, batch_y1, batch_y2 = [], [], [], []

    macro_f1_1 = f1_score(y1_true, label_predictions, average='macro')
    macro_f1_2 = f1_score(y2_true, gender_predictions, average='macro')

    return label_predictions, \
           gender_predictions, \
           total_loss/len(dev_set), \
           macro_f1_1,\
           macro_f1_2
示例#32
0
 def __init__(self, options=None):
     self.ID = -1
     self.options = options
     if self.options is None:
         self.options = KTreeOptions()
     # self.model provides all metric related functions:
     self.model = models.model(self.options)
     # root node of tree:
     self.root = self.new_root()
示例#33
0
 def __init__(self, options=None, model=None, parent=None):
     global ID
     ID += 1
     self.ID = ID
     # TODO: look up options in parent node?
     self.options = options
     if self.options is None:
         self.options = KTreeOptions()
     self.model = model
     if self.model is None:
             self.model = models.model(self.options)
     self.parent = parent
示例#34
0
def train(epoch):
    iters = []
    lrs = []
    train_losses = []
    val_losses = []
    val_accuracies = []
    model.train()
    # train loop
    for batch_idx, batch in enumerate(train_loader):
        # prepare data
        images = Variable(batch[0])
        targets = Variable(batch[1])

        if args.cuda:
            images, targets = images.cuda(), targets.cuda()

        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()

        if args.vis and batch_idx % args.log_interval == 0 and images.shape[0] == 1:
            cv2.imshow('output: ', outputs.cpu().data.numpy()[0][0])
            cv2.imshow('target: ', targets.cpu().data.numpy()[0][0])
            cv2.waitKey(10)

        # Learning rate decay.
        if epoch % args.step_interval == 0 and epoch != 1 and batch_idx == 0:
            if args.lr_decay != 1:
                global lr, optimizer
                lr *= args.lr_decay
                for param_group in optimizer.param_groups:
                    param_group['lr'] = lr
                print('Learning rate decayed to %f.' % lr)

        if batch_idx % args.log_interval == 0:
            val_loss, val_acc = evaluate('val', n_batches=80)
            train_loss = loss.item()
            iters.append(len(train_loader.dataset)*(epoch-1)+batch_idx)
            lrs.append(lr)
            train_losses.append(train_loss)
            val_losses.append(val_loss)
            val_accuracies.append(val_acc)

            examples_this_epoch = batch_idx * len(images)
            epoch_progress = 100. * batch_idx / len(train_loader)
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\t'
                  'Train Loss: {:.6f}\tVal Loss: {:.6f}\tVal Acc: {}'.format(
                epoch, examples_this_epoch, len(train_loader.dataset),
                epoch_progress, train_loss, val_loss, val_acc))

    return iters, train_losses, val_losses, val_accuracies
示例#35
0
from models import gaussian as model
import numpy as np
import matplotlib.pyplot as plt
from palettable import colorbrewer

#plt.rcParams['image.cmap'] = colorbrewer.sequential.Blues_9.mpl_colormap
cmap = colorbrewer.sequential.Blues_9.mpl_colormap

if __name__ == "__main__":
    nx,dx = 32, 2.
    flux = 3.
    fwhm = 37.
    p = 0.052

    m = model(flux,fwhm,p,nx,dx)

    # plot model
    psi = np.arctan(m.U/m.Q)/2.
    plt.imshow(m.I,cmap=cmap)
    plt.quiver(np.cos(psi)*m.I,np.sin(m.I),headwidth=0)

    #plt.ion()
    plt.show()
示例#36
0
image_shape = (X_train.shape[2], X_train.shape[3], 1)

X = None
Y = None

## TODO: when finished, train with all dataset!!

sinkhorn_on = False
weight_decay = 0.005
dropout = 0.3
initial_lr = 0.0001
epochs = 30
batch_size = 64

model = model(tiles_per_dim, image_shape, sinkhorn_on, weight_decay, dropout)
sgd = keras.optimizers.SGD(lr=initial_lr, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=keras.optimizers.Adam(lr=initial_lr),
              metrics=['accuracy'])
print(model.summary())

datagen = data_generator(X_train, y_train, batch_size=batch_size)
vdatagen = data_generator(X_test, y_test, batch_size=batch_size)

history = model.fit_generator(generator=datagen,
                              steps_per_epoch=X_train.shape[0] // batch_size,
                              validation_data=vdatagen,
                              validation_steps=X_test.shape[0] // batch_size,
                              epochs=epochs,
                              callbacks=[LearningRateScheduler(schedule)])