Ejemplo n.º 1
0
    def predict(arg):
        img_meta, img = arg
        h, w = img.shape[:2]
        s = patch_size

        def make_batch(xy_batch_):
            return (xy_batch_, torch.stack([utils.img_transform(img)]))

        pred_img = np.zeros((2, s, s), dtype=np.float32)
        #          np.zeros((utils.N_CLASSES + 1, h, w), dtype=np.float32)
        #         pred_count = np.zeros((s, s), dtype=np.int32)
        inputs = torch.stack([utils.img_transform(img)])
        outputs = model(utils.variable(inputs, volatile=True))
        #         print("outputs", outputs.shape)
        outputs_data = np.exp(outputs.data.cpu().numpy())
        #         print("o_data", outputs_data.shape)
        for pred in outputs_data:
            pred_img += pred
#             print("pred", pred)
#         print("pred_img", pred_img)
#         for idx, i in enumerate(pred_img):
#             utils.save_image('_runs/pred-{}-{}.png'.format(img_meta[0].stem, idx), (i > 0.25+idx*0.5).astype(np.float32))
        utils.save_image('_runs/pred-{}.png'.format(img_meta[0].stem),
                         colored_prediction(outputs_data[0]))
        utils.save_image(
            '_runs/{}-input.png'.format(prefix),
            skimage.exposure.rescale_intensity(img, out_range=(0, 1)))
        #         utils.save_image(
        #             '_runs/{}-target.png'.format(prefix),
        #             colored_prediction(target_one_hot.astype(np.float32)))

        return img_meta, pred_img
Ejemplo n.º 2
0
    def __getitem__(self, idx):
        img = load_image(self.image_paths[idx])
        mask = load_mask(self.mask_paths[idx])

        if self.to_augment:
            img, mask = augment(img, mask)

        return utils.img_transform(img), torch.from_numpy(np.expand_dims(mask, 0))
 def make_batch(xy_batch_):
     indices, patches = [], []
     for cls, i, x, y in xy_batch_:
         patch = img[max(0, y - s):y + s, max(0, x - s):x + s]
         if patch.shape[:2] == (patch_size, patch_size):
             patches.append(utils.img_transform(patch))
             indices.append((cls, i))
     patches = torch.stack(patches) if patches else None
     return indices, patches
Ejemplo n.º 4
0
    def get_diffs(self, img, bk):
        h, w = img.shape[0:2]

        diff_rgb = self.get_rgb_diff_scale_invariant(img, bk, self.rgb_downscale_factor)

        img_trans = img_transform(img)
        bk_trans = img_transform(bk)

        batch = np.stack((img_trans, bk_trans), axis=0)
        batch = torch.tensor(batch, device=self.device)

        batch_features = self.vgg16(batch)
        diffs = []

        for batch_feature in batch_features:
            img_feature = batch_feature[0, :, :, :]
            bk_feature = batch_feature[1, :, :, :]
            
            diff = torch.sum((img_feature - bk_feature)**2, dim=0)**0.5
            
            diff = diff.detach().cpu().numpy()
            diff = cv2.resize(diff, (w, h))

            diffs.append(diff)
            plt.imshow(diff)
            plt.show()

        plt.imshow(diff_rgb)
        plt.show()

        diffs.append(diff_rgb)
        diffs = np.stack(diffs, axis=2)
        # whiten
        means = np.mean(diffs, axis=(0, 1), keepdims=True)
        stds = np.std(diffs, axis=(0, 1), keepdims=True)
        diffs = (diffs - means) / stds
        return diffs
Ejemplo n.º 5
0
 def __getitem__(self, idx):
     img = load_image(self.image_paths[idx], size=self.size)
     mask = load_mask(self.mask_paths[idx], size=self.out_size)
     return utils.img_transform(img), torch.from_numpy(mask)
Ejemplo n.º 6
0
 def __getitem__(self, idx):
     path = self.paths[idx % len(self.paths)]
     image, size = load_image(path, self.size, with_size=True)
     return utils.img_transform(image), (path.stem, list(size))
Ejemplo n.º 7
0
            num_exp = 0
            tstart = time.clock()

            batchnum_train = math.ceil(idx_finetune.shape[0] // batchsize)
            for bi in range(batchnum_train):
                if bi == batchnum_train - 1:
                    idx = idx_finetune[bi * batchsize:]
                else:
                    idx = idx_finetune[bi * batchsize:(bi + 1) * batchsize]
                img = images_combined_train[idx]
                lab = labels_combined_train[idx]
                lab_onehot = utils.one_hot(lab, num_class)

                # transform
                if flag_augmentation_e2e == False:
                    img = utils.img_transform(img, 'train')
                img = torch.from_numpy(img).float()
                img = img.cuda()
                lab_onehot = torch.from_numpy(lab_onehot)
                lab_onehot = lab_onehot.float()
                lab_onehot = lab_onehot.cuda()

                # print("Outside: input size", img.size(), "output_size", lab.size())
                output = net.forward(img)

                # classification loss
                indices = torch.LongTensor(
                    np.concatenate((class_old, class_novel), axis=0))
                indices = indices.cuda()
                prob_cls = torch.index_select(output, 1, indices)
                prob_cls = softmax(prob_cls)
Ejemplo n.º 8
0
 def __getitem__(self, idx):
     path = self.paths[idx % len(self.paths)]
     image = train.load_image(path)
     return utils.img_transform(image), path.stem
Ejemplo n.º 9
0
 def make_batch(xy_batch_):
     return (xy_batch_, torch.stack([utils.img_transform(img)]))
Ejemplo n.º 10
0
 def make_batch(xy_batch_):
     return (xy_batch_, torch.stack([
         utils.img_transform(img[y: y + s, x: x + s]) for x, y in xy_batch_]))