Ejemplo n.º 1
0
def get_cancer_prediction(image_bytes):
    with torch.no_grad():
        img = get_image(image_bytes)
        imwidth, imheight = img.size
        plist = crop(img, 256, 256, 200)
        outlist = []
        for rlist in plist:
            out = []
            for i in range(len(rlist)):
                rlist[i] = ToTensor()(rlist[i])
                rlist[i] = TF.normalize(rlist[i], normMean, normStd)
                if is_cuda:
                    rlist[i] = rlist[i].unsqueeze(0).cuda()
                image = rlist[i]
                output = net(image)
                output = ToPILImage()(output.cpu().squeeze(0))
                out.append(output)
            outlist.append(out)
        outimg = attach(outlist, 200, imwidth, imheight)
        outimg = np.array(outimg)
        outimg = cv2.resize(outimg, (imwidth, imheight))

        low_conf_pixels = outimg[outimg > 65]
        low_conf_pixels = low_conf_pixels[low_conf_pixels < 190]
        low_conf_pixels_count = len(low_conf_pixels)
        confidence = 1 - (low_conf_pixels_count / imwidth * imheight)
        _, outimg = cv2.threshold(outimg, 127, 255, cv2.THRESH_BINARY)

    flag = 1
    # Criterion for presence of lesion
    if len(outimg == 255) < 100:
        flag == 0
    superimposed = superimpose(img, outimg)
    superimposed = Image.fromarray(superimposed.astype('uint8'))
    return superimposed, confidence, flag
Ejemplo n.º 2
0
def JPeg(image, quality):
    img = torch.clamp(image, 0, 1)
    img = ToPILImage()(img.cpu())
    path = '/tmp/def_jpeg.jpeg'
    img.save(path, 'JPEG', quality=quality)
    img = Image.open(path)
    img = ToTensor()(img)
    return img
Ejemplo n.º 3
0
    def evaluate_model(config, model_folder, image_folder, output_path, save_json=True):
        """
        Evaluates a model by comparing input images with output images
        :param config: the model configuration
        :param model_folder: folder of the saved model
        :param image_folder: path images used to evaluate the model
        :param output_path: path where anonymized images should be stored
        :return: list of distances
        """

        image_folder = Path(image_folder)
        output_path = Path(output_path)
        model = config.model(**config.model_params)
        model.load_model(Path(model_folder))
        extractor = FaceExtractor(margin=0.05, mask_factor=10)

        print("The authors of the package recommend 0.6 as max distance for the same person.")
        scores = {}
        for image_file in image_folder.iterdir():
            if image_file.is_dir():
                continue

            print('#' * 10)
            print('Processing image:', image_file.name)

            input_image = Image.open(image_file)
            extracted_face, extracted_info = extractor(input_image)
            if extracted_face is None:
                print('Face could not be extracted')
                continue

            face_out = model.anonymize(extracted_face, extracted_info).squeeze(0)
            face_out = ToPILImage()(face_out.cpu().detach())
            face_out = face_out.resize(extracted_face.size, resample=BICUBIC)

            try:
                face_out.save(output_path / ('anonymized_' + image_file.name.__str__()))
                score, sim, emo = Evaluator.evaluate_image_pair(extracted_face, face_out)
                scores[image_file.name] = {'score': score, 'sim': sim, 'emo': emo}
            except Exception as ex:
                print(ex)
                continue

            print('Current image score:', scores[image_file.name])

        if save_json:
            with open(output_path / 'scores.json', 'w') as f:
                json.dump(scores, f)

        return scores
Ejemplo n.º 4
0
    def __getitem__(self, index):
        hr_image = Image.open(self.image_filenames[index])
        w, h = hr_image.size
        crop_size = calculate_valid_crop_size_2(min(w, h), 32)

        if crop_size > self.crop_size:
            hr_image_ = CenterCrop(crop_size)(hr_image)
            hr_image = ToTensor()(hr_image_)
            hr_image_gray = gray(hr_image)
            hr_image = Variable(hr_image_gray)
            hr_image = hr_image.unsqueeze(0)
            # if torch.cuda.is_available():
            #     hr_image = hr_image.cuda()

            output = self.model(hr_image)
            output = output.squeeze(0)
            output = output.data
            output = output.cpu()
            output = ToPILImage(output)

            x = random.randint(0, crop_size - self.crop_size)
            y = random.randint(0, crop_size - self.crop_size)
            Crop_Image = Crop(x, y, self.crop_size)
            hr_image = Crop_Image(hr_image_gray)
            lr_image = Crop_Image(output)
            return ToTensor()(lr_image).unsqueeze(0).unsqueeze(1), ToTensor()(hr_image).unsqueeze(0).unsqueeze(1)
        else:
            hr_image = Image.open(self.image_filenames[1])
            w, h = hr_image.size
            crop_size = calculate_valid_crop_size_2(min(w, h), 32)

            hr_image_ = CenterCrop(crop_size)(hr_image)
            hr_image = ToTensor()(hr_image_)
            hr_image = Variable(hr_image)
            # if torch.cuda.is_available():
            #     hr_image = hr_image.cuda()

            output = self.model(hr_image)
            output = output.data
            output = output.cpu()
            output = ToPILImage(output)

            x = random.randint(0, crop_size - self.crop_size)
            y = random.randint(0, crop_size - self.crop_size)
            Crop_Image = Crop(x, y, self.crop_size)
            hr_image = Crop_Image(hr_image_)
            lr_image = Crop_Image(output)
            return ToTensor()(lr_image), ToTensor()(hr_image)
def augment_images(images, targets, preds):
    augmented_images = []
    for img, target, pred in zip(images, targets, preds):
        img = ToPILImage()(img.cpu())
        img1 = ImageDraw.Draw(img)
        for bb in target['boxes']:
            bb = bb.cpu().numpy()
            img1.rectangle([bb[0], bb[1], bb[2], bb[3]],
                           fill=None,
                           outline="green",
                           width=5)
        for bb in pred['boxes']:
            img1.rectangle([bb[0], bb[1], bb[2], bb[3]],
                           fill=None,
                           outline="blue",
                           width=5)
        #img1.rectangle([10, 10, 40, 40], fill =None, outline ="green", width=5)
        augmented_images.append(ToTensor()(img.resize((256, 256))))
    return augmented_images
Ejemplo n.º 6
0
    def __call__(self, image):
        """
        Merges an anonymized face on the scene
        :param image: PIL image
        :return: PIL image
        """
        constructed_image = None
        # Extract face
        extracted_face, extracted_information = self.extractor(image)
        if extracted_face is not None:
            face_out = self.model.anonymize(extracted_face,
                                            extracted_information).squeeze(0)
            # get it back to the cpu and get the data
            face_out = ToPILImage()(face_out.cpu().detach())
            # scale to original resolution
            face_out = face_out.resize(extracted_face.size, resample=BICUBIC)
            # Constructed scene with new face
            constructed_image = self.reconstructor(face_out,
                                                   extracted_information)

        return constructed_image
Ejemplo n.º 7
0
    def validate(args, epoch, net, validLoader, optimizer, criterion):
        net.eval()
        tqdm.write("VALIDATING...")
        valid_loss = 0
        incorrect = 0
        logs['epoch_tags']['image'] = []
        pbar = tqdm(validLoader)
        with torch.no_grad():
            for data, targets in pbar:
                for img_, target_ in zip(data, targets):
                    img = ToPILImage()(img_)
                    imwidth, imheight = img.size
                    plist = crop(img, imwidth // args.patch_ratio,
                                 imheight // args.patch_ratio)
                    outlist = []
                    for rlist in plist:
                        out = []
                        for i in range(len(rlist)):
                            rlist[i] = ToTensor()(rlist[i])
                            if args.cuda:
                                rlist[i] = rlist[i].unsqueeze(0).cuda()
                            image = rlist[i]
                            output = net(image)
                            output = ToPILImage()(output.cpu().squeeze(0))
                            out.append(output)
                            print(count)
                        outlist.append(out)

                    newimg = attach(outlist, imwidth // args.patch_ratio,
                                    imheight // args.patch_ratio, imwidth,
                                    imheight)
                    logs['epoch_tags']['image'].append(
                        (unorm(img_.cpu()), unorm(target_.cpu()),
                         unorm(ToTensor()(newimg))))
                    valid_loss += criterion(target_, ToTensor()(newimg))
            valid_loss /= len(
                validLoader)  # loss function already averages over batch size
            logs['epoch_tags']['score'] = (1.0 - round(valid_loss.item(), 3))
            for callback in callbacks:
                callback.on_val_end(logs)
Ejemplo n.º 8
0
def create_image(net, loader, name, upscaling=2, multiscale=False):
    patch_size = 30
    patches = np.array(
        [[85, 90, 85 + patch_size, 90 + patch_size],
         [160, 140, 160 + patch_size, 140 + patch_size],
         [350, 80, 350 + patch_size, 80 + patch_size]])

    fig = plt.figure(figsize=(15, 18), dpi=100)
    gs = fig.add_gridspec(4, 3, wspace=0.01, hspace=0.3, left=0.05,
                          top=0.95, bottom=0.02, right=0.95)

    highres, lowres = loader[0]
    lowres = lowres.to(torch.device("cuda:0"))
    lowres = lowres.view([1] + list(lowres.size()))
    net.eval()
    with torch.no_grad():
        if (multiscale):
            superres = net(lowres, upscaling)
        else:
            superres = net(lowres)
    lowres = ToImage()(lowres.cpu().view(lowres.size()[1:]))
    superres = ToImage()(superres.cpu().view(superres.size()[1:]))
    highres = ToImage()(highres.cpu().view(highres.size()[1:]))

    lowres_draw = lowres.copy()
    draw = ImageDraw.Draw(lowres_draw)
    draw.rectangle(list(patches[0]), outline='white')
    draw.rectangle(list(patches[1]), outline='white')
    draw.rectangle(list(patches[2]), outline='white')

    lowres = lowres.resize((lowres.size[0] * upscaling,
                            lowres.size[1] * upscaling),
                           Image.BICUBIC)

    psnr_low, psnr_super, ssim_low, ssim_super = compute_metrics(
        net, DataLoader(loader), upscaling, multiscale)
    lowres_title = "Low-resolution image" +\
    "\nAverage PSNR over the dataset: {:.2f}\n".format(psnr_low) +\
    "Average SSIM over the dataset: {:.4f}".format(ssim_low)
    superres_title = "Reconstructed image\n" +\
    "Average PSNR over the dataset: {:.2f}\n".format(psnr_super) +\
    "Average SSIM over the dataset: {:.4f}".format(ssim_super)
    fig.add_subplot(gs[0, 0], xticks=[], yticks=[],
                    ylabel=f"Image", title=lowres_title)
    plt.imshow(np.array(lowres_draw))
    fig.add_subplot(gs[0, 1], xticks=[], yticks=[],
                    title=superres_title)
    plt.imshow(np.array(superres))
    fig.add_subplot(gs[0, 2], xticks=[], yticks=[],
                    title="High-resolution image")
    plt.imshow(np.array(highres))

    ylabels = ["Patch 1", "Patch 2", "Patch 3"]
    for i in range(3):
        print(lowres.size, highres.size, superres.size)
        lowres_patch = lowres.crop(patches[i] * upscaling)
        highres_patch = highres.crop(patches[i] * upscaling)
        superres_patch = superres.crop(patches[i] * upscaling)

        fig.add_subplot(gs[1 + i, 0], xticks=[], yticks=[],
                        ylabel=ylabels[i])
        plt.imshow(np.array(lowres_patch))
        fig.add_subplot(gs[1 + i, 1], xticks=[], yticks=[])
        plt.imshow(np.array(superres_patch))
        fig.add_subplot(gs[1 + i, 2], xticks=[], yticks=[])
        plt.imshow(np.array(highres_patch))

    plt.savefig(name)
Ejemplo n.º 9
0
def resize(m, target_shape):
    m = ToPILImage()(m.cpu().to(torch.float32))
    m = Resize(target_shape)(m)
    m = ToTensor()(m)
    return m.cuda()
Ejemplo n.º 10
0
def resize(x, target_shape):
    x = ToPILImage()(x.cpu().to(torch.float32))
    x = Resize(target_shape)(x)
    x = ToTensor()(x)
    return x.cuda()
Ejemplo n.º 11
0
            print 'cover:  ', valing_results['psnr'], valing_results['ssim']            

            # print meas, cover_o, meas_o
            g_loss = mse_loss(cover_o, cover)
            meas_real = Variable(torch.zeros(meas.shape).cuda()+meas.data)
            meas_loss = mse_loss(meas_o, meas_real)

            running_results['g_loss'] += g_loss.data[0] * batch_size
            running_results['meas_loss'] += meas_loss.data[0] * batch_size

            train_bar.set_description(desc='[%d/%d] Loss_G: %.5f Loss_Meas: %.5f' % (
                epoch, NUM_EPOCHS, running_results['g_loss'] / running_results['batch_sizes'], running_results['meas_loss'] / running_results['batch_sizes']))

            cover_o_ = ToPILImage()(cover_o.cpu()[0,:,:,:].data)
            cover_o_.save(save_dir+'/'+str(img_id)+'_container'+'.png')
            cover_o = ToPILImage()(torch.abs(cover_o.cpu()[0,:,:,:].data - data[0,:,:,:])*10)
            cover_o.save(save_dir+'/'+str(img_id)+'_diff_cover_container'+'.png')

        # elif stage == 2:

            secret_o = netG(cover, secret, 2)

            batch_mse = ((secret_o - secret) ** 2).data.mean()
            valing_results2['mse'] += batch_mse * batch_size
            batch_ssim = pytorch_ssim.ssim(secret_o, secret).data[0]
            valing_results2['ssims'] += batch_ssim * batch_size
            valing_results2['psnr'] = 10 * log10(1 / (valing_results2['mse'] / valing_results2['batch_sizes']))
            valing_results2['ssim'] = valing_results2['ssims'] / valing_results2['batch_sizes']
            print 'secret:  ', valing_results2['psnr'], valing_results2['ssim']

            g_loss = mse_loss(secret_o, secret)