Пример #1
0
def add_label():
    train_path = './dataset/bottle/train_resize/'
    train_dataset = pretrain_vgg.MvtecLoader(train_path)
    train_loader = DataLoader(train_dataset, batch_size=1, shuffle=False)

    # label_name = 'label/bottle_trainK_16.pth'
    label_name = 'label/vgg19/bottle/train/16_100.pth'
    index_label = torch.load(label_name)

    for idx, img in tqdm(train_loader):
        idx = idx[0].item()
        ironman_grid = plt.GridSpec(1, 1)
        fig = plt.figure(figsize=(5,5), dpi=100)
        ax = fig.add_subplot(ironman_grid[:,:])

        im = ax.imshow(img.squeeze().cpu().detach().numpy().transpose(1,2,0), cmap="Blues")
        for i in range(32):
            for j in range(32):
                label = index_label[idx][i*32+j]
                label = label[0]
                ax.text(j*8, (i+0.5)*8, label, fontsize=5)
        
        
        pathToSave = './corn_ground_truth/label10K_' + str(idx) + '.png'
        plt.savefig(pathToSave, dpi=100)
        plt.close(fig)
Пример #2
0
def compute_var_black():

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    split_size = 8

    train_path = './dataset/bottle/train_resize/'
    train_dataset = pretrain_vgg.MvtecLoader(train_path)
    train_loader = DataLoader(train_dataset, batch_size=1, shuffle=False)

    model = pretrain_vgg.model
    model = model.to(device)
    white = []

    for idx, img in tqdm(train_loader):
        idx = idx[0].item()

        for y in range(8):
            for x in range(8):
                    patch = img[:, :, 96+x*split_size : 96+x*split_size+split_size, 96+y*split_size : 96+y*split_size+split_size].to(device)
                    crop_output = model.forward(patch)
                    crop_output = crop_output.squeeze()
                    # print(crop_output.size())
                    white.append(crop_output.cpu().detach().numpy())
                    
        
    
    print(f'size of white patch: {len(white)}')

    # print(white[0].shape)
    
    # print(f'variance of white: {np.var(white, axis=0)}')

    draw_hist(np.var(white, axis=0), './var_black.png')
Пример #3
0
def compute_dis_black():

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    split_size = 8

    train_path = './dataset/bottle/train_resize/'
    train_dataset = pretrain_vgg.MvtecLoader(train_path)
    train_loader = DataLoader(train_dataset, batch_size=1, shuffle=False)

    kmeans_path = 'kmeans_bottle_16.pickle'
    kmeans = pickle.load(open(kmeans_path, "rb"))

    model = pretrain_vgg.model
    model = model.to(device)
    white = []
    white_l1 = []

    output_center = kmeans.cluster_centers_[13]
    output_center = np.reshape(output_center, (1, -1))
    output_center = pil_to_tensor(output_center).to(device)
    output_center = torch.squeeze(output_center)

    
    for idx, img in tqdm(train_loader):
        idx = idx[0].item()
        for y in range(8):
            for x in range(8):
                patch = img[:, :, 96+x*split_size : 96+x*split_size+split_size, 96+y*split_size : 96+y*split_size+split_size].to(device)
                crop_output = model.forward(patch)
                crop_output = crop_output.squeeze()
                diff = nn.MSELoss()(output_center, crop_output)
                white.append(diff.item() ** 0.5)
                diff2 = nn.L1Loss()(output_center, crop_output)
                white_l1.append(abs(diff2.item()))
                # print(crop_output.size())


    print(f'size of white patch: {len(white)}')

    # print(white[0].shape)
    
    # print(f'mean of white: {np.mean(white)}, variance of white: {np.var(white)}')
    # print(f'mean of whiteL1: {np.mean(white_l1)}, variance of whiteL1: {np.var(white_l1)}')

    plt.hist(white)
    plt.savefig('diff_black.png')
Пример #4
0
def compute_dis_total():

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    split_size = 8

    train_path = './dataset/bottle/train_resize/'
    train_dataset = pretrain_vgg.MvtecLoader(train_path)
    train_loader = DataLoader(train_dataset, batch_size=1, shuffle=False)
   
    label_name = 'label/bottle_trainK_16.pth'
    index_label = torch.tensor(torch.load(label_name))

    kmeans_path = 'kmeans/kmeans_bottle_16.pickle'
    kmeans = pickle.load(open(kmeans_path, "rb"))

    model = pretrain_vgg.model
    model = model.to(device)
    white = []
    white_l1 = []

    
    for idx, img in tqdm(train_loader):
        idx = idx[0].item()
        for y in range(32):
            for x in range(32):

                label = index_label[idx][y*32+x].to(device)
                label = label.item()
                output_center = kmeans.cluster_centers_[label]
                output_center = np.reshape(output_center, (1, -1))
                output_center = pil_to_tensor(output_center).to(device)
                output_center = torch.squeeze(output_center)

                patch = img[:, :, x*split_size : x*split_size+split_size, y*split_size : y*split_size+split_size].to(device)
                crop_output = model.forward(patch)
                crop_output = crop_output.squeeze()
                diff = nn.MSELoss()(output_center, crop_output)
                white.append(diff.item() ** 0.5)
                diff2 = nn.L1Loss()(output_center, crop_output)
                white_l1.append(abs(diff2.item()))
                # print(crop_output.size())

    print(f'size of white patch: {len(white)}')
    plt.hist(white)
    plt.savefig('diff_total_16_K.png')
Пример #5
0
def check_training_label():

    dataset = pretrain_vgg.MvtecLoader('./dataset/bottle/train_resize/')
    loader = DataLoader(dataset, batch_size=1, shuffle=False)
    label_name = 'label/vgg19/bottle/train/16_100.pth'
    label = torch.load(label_name)
    # print(len(label))
    # print(len(label[0]))
    for idx, img in tqdm(loader):
        idx = idx[0].item()
        img = np.squeeze(img.detach().cpu().numpy()).transpose((1,2,0)) 
        ironman_grid = plt.GridSpec(1, 1)
        fig = plt.figure(figsize=(6,6), dpi=100)
        ax1 = fig.add_subplot(ironman_grid[0,0])
        im1 = ax1.imshow(img, cmap="Blues")

        """ add label text to each patch """ 
        for i in range(32):
            for j in range(32):
                # print(label[idx][i*32+j])
                ax1.text((j+0.2)*8, (i+0.6)*8, label[idx][i*32+j].item(), fontsize=5)
        pathToSave = './trainLabelMap/' + str(idx) + '.png'
        plt.savefig(pathToSave, dpi=100)
        plt.close(fig)