Beispiel #1
0
    def paint(self, painter, option, widget):
        if self.footstep_direction == 0:
            image = load_image("footsteps_left.png")
        elif self.footstep_direction == 1:
            image = load_image("footsteps_right.png")

        transform = QTransform().rotate(self.direction)
        image = image.transformed(transform)

        # rotates images relative to the player's turns
        if self.footstep_direction == 1 and self.direction == 90:
            draw_x = 0
            draw_y = 25
        elif self.footstep_direction == 0 and self.direction == 180:
            draw_x = 25
            draw_y = 0
        elif self.footstep_direction == 1 and self.direction == 0:
            draw_x = 25
            draw_y = 25
        elif self.direction == 270:
            draw_x = 25
            if self.footstep_direction == 0:
                draw_y = 25
            else:
                draw_y = 0
        else:
            draw_x = 0
            draw_y = 0

        painter.drawImage(draw_x, draw_y, image)
Beispiel #2
0
def similarity(request):
    use_gpu = False
    spatial = False

    model = models.PerceptualLoss(model='net-lin',
                                  net='alex',
                                  use_gpu=use_gpu,
                                  spatial=spatial)

    img1_filename = request.POST['img1'].filename
    img1_input_file = request.POST['img1'].file
    img1_file_path = os.path.join('/tmp', img1_filename)

    with open(img1_file_path, 'wb') as img1_output_file:
        shutil.copyfileobj(img1_input_file, img1_output_file)

    img1tensor = util.im2tensor(util.load_image(img1_file_path))

    img2_filename = request.POST['img2'].filename
    img2_input_file = request.POST['img2'].file
    img2_file_path = os.path.join('/tmp', img2_filename)

    with open(img2_file_path, 'wb') as img2_output_file:
        shutil.copyfileobj(img2_input_file, img2_output_file)

    img2tensor = util.im2tensor(util.load_image(img2_file_path))

    d = model.forward(img1tensor, img2tensor)

    return Response('Distancia: %.3f' % d)
Beispiel #3
0
    def paint(self, painter, option, widget):
        if self.footstep_direction == 0:
            image = load_image("footsteps_left.png")
        elif self.footstep_direction == 1:
            image = load_image("footsteps_right.png")

        transform = QTransform().rotate(self.direction)
        image = image.transformed(transform)
        
        # rotates images relative to the player's turns
        if self.footstep_direction == 1 and self.direction == 90:
            draw_x = 0
            draw_y = 25
        elif self.footstep_direction == 0 and self.direction == 180:
            draw_x = 25
            draw_y = 0
        elif self.footstep_direction == 1 and self.direction == 0:
            draw_x = 25
            draw_y = 25
        elif self.direction == 270:
            draw_x = 25
            if self.footstep_direction == 0:
                draw_y = 25
            else:
                draw_y = 0
        else:
            draw_x = 0
            draw_y = 0

        painter.drawImage(draw_x,draw_y,image)
def compute_metric(dir1, dir2, mode):
    files1 = os.listdir(dir1)
    files2 = os.listdir(dir2)
    img_files1 = sorted([
        file for file in files1
        if file.endswith('.png') or file.endswith('.jpg')
    ])
    img_files2 = sorted([
        file for file in files2
        if file.endswith('.png') or file.endswith('.jpg')
    ])
    assert len(img_files1) == len(img_files2)

    vals = numpy.empty(len(img_files1))
    if mode == 'perceptual':
        global model

    for ind in range(len(img_files1)):
        if mode == 'ssim' or mode == 'l2':
            img1 = skimage.img_as_float(
                skimage.io.imread(os.path.join(dir1, img_files1[ind])))
            img2 = skimage.img_as_float(
                skimage.io.imread(os.path.join(dir2, img_files2[ind])))
            if mode == 'ssim':
                vals[ind] = skimage.measure.compare_ssim(img1,
                                                         img2,
                                                         datarange=img2.max() -
                                                         img2.min(),
                                                         multichannel=True)
            else:
                vals[ind] = numpy.mean((img1 - img2)**2) * 255.0 * 255.0
        elif mode == 'perceptual':
            img1 = util.im2tensor(
                util.load_image(os.path.join(dir1, img_files1[ind])))
            img2 = util.im2tensor(
                util.load_image(os.path.join(dir2, img_files2[ind])))
            vals[ind] = model.forward(img1, img2)[0]
        else:
            raise

    filename_all = mode + '_all.txt'
    filename_breakdown = mode + '_breakdown.txt'
    filename_single = mode + '.txt'
    numpy.savetxt(os.path.join(dir1, filename_all), vals, fmt="%f, ")
    target = open(os.path.join(dir1, filename_single), 'w')
    target.write("%f" % numpy.mean(vals))
    target.close()
    if len(img_files1) == 30:
        target = open(os.path.join(dir1, filename_breakdown), 'w')
        target.write("%f, %f, %f" % (numpy.mean(
            vals[:5]), numpy.mean(vals[5:10]), numpy.mean(vals[10:])))
        target.close()
    return vals
Beispiel #5
0
def compute(image_file1, image_file2, gpu_flag):

	## Initializing the model
	model = dm.DistModel()
	model.initialize(model='net-lin', net='squeeze', use_gpu=gpu_flag)

	# Load images
	img0 = util.im2tensor(util.load_image(image_file1)) # RGB image from [-1,1]
	img1 = util.im2tensor(util.load_image(image_file2))

	# Compute distance
	dist01 = model.forward(img0, img1)
	
	return float(dist01[0])
Beispiel #6
0
def compute_dists_pair(use_gpu, dir, out):
    ## Initializing the model
    model = models.PerceptualLoss(model='net-lin', net='alex', use_gpu=use_gpu)
    dists = []
    # crawl directories
    # f = open(out,'w')
    # category_files = os.listdir(opt.dir)
    # print('category_file', category_files)
    i = 0
    for dir_path_class, dir_name_class, file_names_class in os.walk(dir):
        # print('1',dir_path_class)
        # print('2',dir_name_class)
        # print('3',file_names_class)
        dists_category = []
        for (ff, file0) in enumerate(file_names_class[:-1]):
            if ff < 10:
                # print('here',file0)
                # print(os.path.exists(os.path.join(dir_path_class,file0)))
                img0 = util.im2tensor(
                    util.load_image(os.path.join(
                        dir_path_class, file0)))  # RGB image from [-1,1]
                if (use_gpu):
                    img0 = img0.cuda()

                for (gg, file1) in enumerate(file_names_class[ff + 1:]):
                    img1 = util.im2tensor(
                        util.load_image(os.path.join(dir_path_class, file1)))
                    if (use_gpu):
                        img1 = img1.cuda()
                    # Compute distance
                    dist01 = model.forward(img0, img1).item()
                    dists_category.append(dist01)

            else:
                # print('continue')
                dists_category_mean = np.mean(np.array(dists_category))
                print('{}_cateogory {}_samples lpips is {}'.format(
                    i, len(dists_category), dists_category_mean))
                dists.append(dists_category_mean)
                break
        i = i + 1
        # if i > 5:
        # 	break
        # print('(%s, %s): %.3f'%(file0,file1,dist01))
        # f.writelines('(%s, %s): %.3f'%(file0,file1,dist01))
    dist_mean = np.mean(np.array(dists))
    print('Mean: %.3f' % dist_mean)
    return dists, dist_mean
Beispiel #7
0
 def paint(self, painter, option, widget):
     # draws image with offset for aesthetics
     if self.image_name is not None:
         image = load_image(self.image_name)
         if self.vertorhor == None:
             painter.drawImage(0, 0, image)
         elif self.vertorhor == 0:
             painter.drawImage(20, 0, image)
         elif self.vertorhor == 1:
             painter.drawImage(0, 20, image)
Beispiel #8
0
 def paint(self, painter, option, widget):
     # draws image with offset for aesthetics
     if self.image_name is not None:
         image = load_image(self.image_name)
         if self.vertorhor == None:
             painter.drawImage(0, 0, image)
         elif self.vertorhor == 0:
             painter.drawImage(20, 0, image)
         elif self.vertorhor == 1:
             painter.drawImage(0, 20, image)
 def load_feature_style(self):
     image = load_image(os.path.join(self.style_dir, self.style_image_name),
                        size=self.image_size)
     image = transforms.Compose([
         transforms.CenterCrop(min(image.size[0], image.size[1])),
         transforms.Resize(self.image_size),
         transforms.ToTensor(),
         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
     ])(image)
     image = image.repeat(self.batch_size, 1, 1, 1)
     self.style_image = image.to(self.device)
Beispiel #10
0
    def data_load(self, label):
        data_path = os.path.join(self.opt.dataroot, self.opt.cap_scheme, label)
        self.num_train_samples = min(
            self.opt.train_size,
            len(os.listdir(os.path.join(data_path, 'train'))))
        # num_test_samples = len(os.listdir(os.path.join(data_path, 'test')))
        self.num_test_sample = min(
            2000, len(os.listdir(os.path.join(data_path, 'test'))))

        # load training set
        x_train = np.empty((self.num_train_samples, self.opt.loadHeight,
                            self.opt.loadWidth, 1),
                           dtype='uint8')
        y_train = np.empty(
            (self.num_train_samples, self.opt.cap_len * self.opt.char_set_len),
            dtype='uint8')
        train_labels = util.load_label(
            os.path.join(data_path, label + '_train.txt'))
        for i in range(self.num_train_samples):
            # print(i)
            img_name = os.path.join(data_path, 'train', str(i) + '.jpg')
            x_train[i, :, :, :] = util.load_image(img_name)
            y_train[i, :] = self.text2vec(train_labels[i])

        # load testing set
        x_test = np.empty(
            (self.num_test_sample, self.opt.loadHeight, self.opt.loadWidth, 1),
            dtype='uint8')
        y_test = np.empty(
            (self.num_test_sample, self.opt.cap_len * self.opt.char_set_len),
            dtype='uint8')
        test_labels = util.load_label(
            os.path.join(data_path, label + '_test.txt'))
        for i in range(self.num_test_sample):
            # print(i)
            img_name = os.path.join(data_path, 'test', str(i) + '.jpg')
            x_test[i, :, :, :] = util.load_image(img_name)
            y_test[i, :] = self.text2vec(test_labels[i])

        return (x_train, y_train), (x_test, y_test)
def compute_perceptual(dir1, dir2):
    files1 = os.listdir(dir1)
    files2 = os.listdir(dir2)
    img_files1 = sorted([
        file for file in files1
        if file.endswith('.png') or file.endswith('.jpg')
    ])
    img_files2 = sorted([
        file for file in files2
        if file.endswith('.png') or file.endswith('.jpg')
    ])
    #img_files1 = sorted([file for file in files1 if file.endswith('.png') or file.endswith('synthesized_image.jpg')])
    #img_files2 = sorted([file for file in files1 if file.endswith('.png') or file.endswith('real_image.jpg')])
    assert len(img_files1) == len(img_files2)

    vals = numpy.empty(len(img_files1))

    global model

    for ind in range(len(img_files1)):
        img1 = util.im2tensor(
            util.load_image(os.path.join(dir1, img_files1[ind])))
        img2 = util.im2tensor(
            util.load_image(os.path.join(dir2, img_files2[ind])))
        vals[ind] = model.forward(img1, img2)[0]

    numpy.savetxt(os.path.join(dir1, 'perceptual_all.txt'), vals, fmt="%f, ")
    target = open(os.path.join(dir1, 'perceptual.txt'), 'w')
    target.write("%f" % numpy.mean(vals))
    target.close()
    if len(img_files1) == 30:
        target = open(os.path.join(dir1, 'perceptual_breakdown.txt'), 'w')
        target.write("%f, %f, %f" % (numpy.mean(
            vals[:5]), numpy.mean(vals[5:10]), numpy.mean(vals[10:])))
        target.close()
    return vals
Beispiel #12
0
    def cal_lipis(self):
        for method in self.methods:
            img_path_GT = os.path.join(self.folder_root, 'HR', self.dataset)
            img_path_SR = os.path.join(self.folder_root, 'SR', self.dataset,
                                       method)
            lipis_List = []

            img_paths_GT = os.listdir(img_path_GT)
            img_paths_SR = os.listdir(img_path_SR)
            for path_GT, path_SR in zip(img_paths_GT, img_paths_SR):
                assert path_GT == path_SR, "Images with different name"
                img_GT = util.im2tensor(
                    util.load_image(os.path.join(img_path_GT, path_GT)))
                img_SR = util.im2tensor(
                    util.load_image(os.path.join(img_path_SR, path_SR)))
                name = path_SR[:-4]

                lipis = self.model.forward(img_GT, img_SR)[0]
                print("Image {} LIPIS result: {}".format(name, lipis))
                lipis_List.append(lipis)
            aver_lipis = np.mean(np.asarray(lipis_List))
            print("Average LIPIS result of {} in {} dataset: {}".format(
                method, self.dataset, aver_lipis))
            print("End")
Beispiel #13
0
    def load_feature_style(self):
        if not os.path.exists(self.style_dir):
            os.makedirs(self.style_dir)
        if not os.listdir(os.path.join(self.style_dir, self.style_image_name)):
            raise Exception(f"[!] No image for style transfer")

        image = load_image(os.path.join(self.style_dir, self.style_image_name),
                           size=self.image_size)
        image = transforms.Compose([
            transforms.CenterCrop(min(image.size[0], image.size[1])),
            transforms.Resize(self.image_size),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])(image)
        image = image.repeat(self.batch_size, 1, 1, 1)
        image = image.to(self.device)
        style_image = self.vgg(image)
        self.gram_style = [gram_matrix(y) for y in style_image]
Beispiel #14
0
    def train(self):
        total_step = len(self.data_loader)
        optimizer = Adam(self.transfer_net.parameters(), lr=self.lr)
        loss = nn.MSELoss()
        self.transfer_net.train()

        for epoch in range(self.epoch, self.num_epoch):
            for step, image in enumerate(self.data_loader):
                image = image.to(self.device)
                transformed_image = self.transfer_net(image)

                image_feature = self.vgg(image)
                transformed_image_feature = self.vgg(transformed_image)

                content_loss = self.content_weight * loss(
                    image_feature, transformed_image_feature)

                style_loss = 0
                for ft_y, gm_s in zip(transformed_image_feature,
                                      self.gram_style):
                    gm_y = gram_matrix(ft_y)
                    style_loss += load_image(gm_y,
                                             gm_s[:self.batch_size, :, :])
                style_loss *= self.style_weight

                total_loss = content_loss + style_loss

                optimizer.zero_grad()
                total_loss.backward()
                optimizer.step()

                if step % 10 == 0:
                    print(
                        f"[Epoch {epoch}/{self.num_epoch}] [Batch {step}/{total_step}] "
                        f"[Style loss: {style_loss.item()}] [Content loss loss: {content_loss.item()}]"
                    )
            torch.save(
                self.transfer_net.state_dict(),
                os.path.join(self.checkpoint_dir, f"TransferNet_{epoch}.pth"))
Beispiel #15
0
# category_files = os.listdir(opt.dir)
# print('category_file', category_files)
i = 0
for dir_path_class, dir_name_class, file_names_class in os.walk(opt.dir):
    # print('1',dir_path_class)
    # print('2',dir_name_class)
    # print('3',file_names_class)

    print('number of each category', len(file_names_class))
    print('{}_category'.format(i))
    for (ff, file0) in enumerate(file_names_class[:-1]):
        if ff < 10:
            # print('here',file0)
            # print(os.path.exists(os.path.join(dir_path_class,file0)))
            img0 = util.im2tensor(
                util.load_image(os.path.join(dir_path_class,
                                             file0)))  # RGB image from [-1,1]
            if (opt.use_gpu):
                img0 = img0.cuda()

            for (gg, file1) in enumerate(file_names_class[ff + 1:]):
                img1 = util.im2tensor(
                    util.load_image(os.path.join(dir_path_class, file1)))
                if (opt.use_gpu):
                    img1 = img1.cuda()
                # Compute distance
                dist01 = model.forward(img0, img1).item()
                dists.append(dist01)
        else:
            # print('continue')
            continue
    i = i + 1
Beispiel #16
0
                    action='store_true',
                    help='turn on flag to use GPU')
parser.add_argument('--test_list', type=str, default='')
opt = parser.parse_args()

## Initializing the model
model = models.PerceptualLoss(model='net-lin', net='alex', use_gpu=opt.use_gpu)
dists = []

# Load images
with open(opt.test_list, 'r') as f:
    for ll in f:
        path0, path1 = ll.strip().split('\t')
        print(path0, path1)
        img0 = util.im2tensor(
            util.load_image(os.path.join(opt.path, path0 + '.png')))
        img1 = util.im2tensor(
            util.load_image(os.path.join(opt.path, path1 + '.png')))

        if opt.use_gpu:
            img0 = img0.cuda()
            img1 = img1.cuda()

        # Compute distance
        dist01 = model.forward(img0, img1).data.cpu().squeeze().numpy()
        print('Distance: %.4f' % dist01)
        dists.append(dist01)

print('Average distance: %.4f' % (sum(dists) / len(dists)))
print('Standard deviation:', np.array(dists).std())
Beispiel #17
0
model.initialize(model='net-lin', net='alex', use_gpu=use_gpu, spatial=spatial)
#model.initialize(model='net-lin',net='vgg',use_gpu=use_gpu,spatial=spatial)

# Off-the-shelf uncalibrated networks
#model.initialize(model='net',net='squeeze',use_gpu=use_gpu)
#model.initialize(model='net',net='alex',use_gpu=use_gpu)
#model.initialize(model='net',net='vgg',use_gpu=use_gpu)

# Low-level metrics
# model.initialize(model='l2',colorspace='Lab')
# model.initialize(model='ssim',colorspace='RGB')
print('Model [%s] initialized' % model.name())

## Example usage with images
dist = []

for index, row in data.iterrows():

    img_1 = util.load_image('/mnt/wham/ImageSim/females64/%s' % row['img1'])
    img_2 = util.load_image('/mnt/wham/ImageSim/females64/%s' % row['img2'])

    ex_ref = util.im2tensor(img_1)
    ex_p1 = util.im2tensor(img_2)

    ex_d0 = model.forward(ex_ref, ex_p1)
    dist.append(ex_d0)
    print(ex_d0)

data.distance = dist
data.to_csv("/mnt/wham/ImageSim/output_females_data.csv")
Beispiel #18
0
# model.initialize(model='net',net='squeeze',use_gpu=True)
# model.initialize(model='net',net='alex',use_gpu=True)
# model.initialize(model='net',net='vgg',use_gpu=True)

# Low-level metrics
# model.initialize(model='l2',colorspace='Lab')
# model.initialize(model='ssim',colorspace='RGB')
# print('Model [%s] initialized'%model.name())

## Example usage with dummy tensors
# dummy_im0 = torch.Tensor(1,3,64,64) # image should be RGB, normalized to [-1,1]
# dummy_im1 = torch.Tensor(1,3,64,64)
# dist = model.forward(dummy_im0,dummy_im1)

## Example usage with images
ex_ref = util.im2tensor(util.load_image('./imgs/ex_ref.png'))
ex_p0 = util.im2tensor(util.load_image('./imgs/ex_p0.png'))
ex_p1 = util.im2tensor(util.load_image('./imgs/ex_p1.png'))
# ex_d0 = model.forward(ex_ref,ex_p0)[0]
# ex_d1 = model.forward(ex_ref,ex_ref)[0]

# ---------- new model -------
model.initialize(model='net-lin', net='alex', use_gpu=True)
print('Model [%s] initialized' % model.name())
same_image = model.forward(ex_ref, ex_ref)[0]
print("comparing Same Image : ", same_image)

my_image_load = util.im2tensor(util.load_image('./imgs/my_image.PNG'))
my_image = model.forward(my_image_load, my_image_load)[0]
print("comparing Same  My Image : ", same_image)
def compute_metric(dir1, dir2, mode, mask=None, thre=0):

    if mode == 'perceptual':
        from models import dist_model as dm
        import torch
        from util import util
        global model
        if model is None:
            cwd = os.getcwd()
            os.chdir('../../PerceptualSimilarity')
            model = dm.DistModel()
            #model.initialize(model='net-lin',net='alex',use_gpu=True, spatial=True)
            model.initialize(model='net-lin', net='alex', use_gpu=True)
            print('Model [%s] initialized' % model.name())
            os.chdir(cwd)

    if mode.startswith('perceptual_tf'):
        sys.path += ['../../lpips-tensorflow']
        import lpips_tf
        import tensorflow as tf
        image0_ph = tf.placeholder(tf.float32, [1, None, None, 3])
        image1_ph = tf.placeholder(tf.float32, [1, None, None, 3])
        if mode == 'perceptual_tf':
            distance_t = lpips_tf.lpips(image0_ph,
                                        image1_ph,
                                        model='net-lin',
                                        net='alex')
        elif mode == 'perceptual_tf_vgg':
            distance_t = lpips_tf.lpips(image0_ph,
                                        image1_ph,
                                        model='net-lin',
                                        net='vgg')
        else:
            raise
        sess = tf.Session()

    if mode == 'l2_with_gradient':
        import demo
        import tensorflow as tf
        output = tf.placeholder(tf.float32, shape=[1, None, None, None])
        gradient = demo.image_gradients(output)
        sess = tf.Session()

    files1 = os.listdir(dir1)
    files2 = os.listdir(dir2)
    img_files1 = sorted([
        file for file in files1
        if file.endswith('.png') or file.endswith('.jpg')
    ])
    img_files2 = sorted([
        file for file in files2
        if file.endswith('.png') or file.endswith('.jpg')
    ])

    if '--prefix' in sys.argv:
        prefix_idx = sys.argv.index('--prefix')
        prefix = sys.argv[prefix_idx + 1]
        img_files2 = [file for file in img_files2 if file.startswith(prefix)]

    if mask is not None:
        mask_files = []
        for dir in sorted(mask):
            files3 = os.listdir(dir)
            add_mask_files = sorted([
                os.path.join(dir, file) for file in files3
                if file.startswith('mask')
            ])
            if len(add_mask_files) == 0:
                files_to_dilate = sorted([
                    file for file in files3
                    if file.startswith('g_intermediates')
                ])
                for file in files_to_dilate:
                    mask_arr = numpy.load(os.path.join(dir, file))
                    mask_arr = numpy.squeeze(mask_arr)
                    mask_arr = mask_arr >= thre
                    dilated_mask = dilation(mask_arr, disk(10))
                    dilated_filename = 'mask_' + file
                    numpy.save(os.path.join(dir, dilated_filename),
                               dilated_mask.astype('f'))
                    add_mask_files.append(os.path.join(dir, dilated_filename))
            mask_files += add_mask_files
        print(mask_files)
        assert len(mask_files) == len(img_files1)

    skip_first_n = 0
    if '--skip_first_n' in sys.argv:
        try:
            skip_first_n = int(sys.argv[sys.argv.index('--skip_first_n') + 1])
        except:
            skip_first_n = 0

    skip_last_n = 0
    if '--skip_last_n' in sys.argv:
        try:
            skip_last_n = int(sys.argv[sys.argv.index('--skip_last_n') + 1])
        except:
            skip_last_n = 0

    img_files2 = img_files2[skip_first_n:]
    if skip_last_n > 0:
        img_files2 = img_files2[:-skip_last_n]
    assert len(img_files1) == len(img_files2)

    # locate GT gradient directory
    if mode == 'l2_with_gradient':
        head, tail = os.path.split(dir2)
        gradient_gt_dir = os.path.join(head, tail[:-3] + 'grad')
        if not os.path.exists(gradient_gt_dir):
            printf("dir not found,", gradient_gt_dir)
            raise
        gradient_gt_files = os.listdir(gradient_gt_dir)
        gradient_gt_files = sorted(
            [file for file in gradient_gt_files if file.endswith('.npy')])
        assert len(img_files1) == len(gradient_gt_files)

    vals = numpy.empty(len(img_files1))
    #if mode == 'perceptual':
    #    global model

    for ind in range(len(img_files1)):
        if mode == 'ssim' or mode == 'l2' or mode == 'l2_with_gradient':
            img1 = skimage.img_as_float(
                skimage.io.imread(os.path.join(dir1, img_files1[ind])))
            img2 = skimage.img_as_float(
                skimage.io.imread(os.path.join(dir2, img_files2[ind])))
            if mode == 'ssim':
                #vals[ind] = skimage.measure.compare_ssim(img1, img2, datarange=img2.max()-img2.min(), multichannel=True)
                metric_val = skimage.measure.compare_ssim(
                    img1,
                    img2,
                    datarange=img2.max() - img2.min(),
                    multichannel=True)
            else:
                #vals[ind] = numpy.mean((img1 - img2) ** 2) * 255.0 * 255.0
                metric_val = ((img1 - img2)**2) * 255.0 * 255.0
            if mode == 'l2_with_gradient':
                metric_val = numpy.mean(metric_val, axis=2)
                gradient_gt = numpy.load(
                    os.path.join(gradient_gt_dir, gradient_gt_files[ind]))
                dx, dy = sess.run(gradient,
                                  feed_dict={
                                      output:
                                      numpy.expand_dims(img1[..., ::-1],
                                                        axis=0)
                                  })
                #is_edge = skimage.feature.canny(skimage.color.rgb2gray(img1))
                dx_ground = gradient_gt[:, :, :, 1:4]
                dy_ground = gradient_gt[:, :, :, 4:]
                edge_ground = gradient_gt[:, :, :, 0]
                gradient_loss_term = numpy.mean(
                    (dx - dx_ground)**2.0 + (dy - dy_ground)**2.0, axis=3)
                metric_val += numpy.squeeze(
                    0.2 * 255.0 * 255.0 * gradient_loss_term * edge_ground *
                    edge_ground.size / numpy.sum(edge_ground))

            #if mode == 'l2' and mask is not None:
            #    img_diff = (img1 - img2) ** 2.0
            #    mask_img = numpy.load(mask_files[ind])
            #    img_diff *= numpy.expand_dims(mask_img, axis=2)
            #    vals[ind] = (numpy.sum(img_diff) / numpy.sum(mask_img * 3)) * 255.0 * 255.0
        elif mode == 'perceptual':
            img1 = util.im2tensor(
                util.load_image(os.path.join(dir1, img_files1[ind])))
            img2 = util.im2tensor(
                util.load_image(os.path.join(dir2, img_files2[ind])))
            #vals[ind] = numpy.mean(model.forward(img1, img2)[0])
            metric_val = numpy.expand_dims(model.forward(img1, img2), axis=2)
        elif mode.startswith('perceptual_tf'):
            img1 = np.expand_dims(skimage.img_as_float(
                skimage.io.imread(os.path.join(dir1, img_files1[ind]))),
                                  axis=0)
            img2 = np.expand_dims(skimage.img_as_float(
                skimage.io.imread(os.path.join(dir2, img_files2[ind]))),
                                  axis=0)
            metric_val = sess.run(distance_t,
                                  feed_dict={
                                      image0_ph: img1,
                                      image1_ph: img2
                                  })
        else:
            raise

        if mask is not None:
            assert mode in ['l2', 'perceptual']
            mask_img = numpy.load(mask_files[ind])
            metric_val *= numpy.expand_dims(mask_img, axis=2)
            vals[ind] = numpy.sum(metric_val) / (numpy.sum(mask_img) *
                                                 metric_val.shape[2])
        else:
            vals[ind] = numpy.mean(metric_val)

    mode = mode + ('_mask' if mask is not None else '')
    filename_all = mode + '_all.txt'
    filename_breakdown = mode + '_breakdown.txt'
    filename_single = mode + '.txt'
    numpy.savetxt(os.path.join(dir1, filename_all), vals, fmt="%f, ")
    target = open(os.path.join(dir1, filename_single), 'w')
    target.write("%f" % numpy.mean(vals))
    target.close()
    if len(img_files1) == 30:
        target = open(os.path.join(dir1, filename_breakdown), 'w')
        target.write("%f, %f, %f" % (numpy.mean(
            vals[:5]), numpy.mean(vals[5:10]), numpy.mean(vals[10:])))
        target.close()
    if mode in ['l2_with_gradient', 'perceptual_tf']:
        sess.close()
    return vals
Beispiel #20
0
def get_image_tensor(path):
    image = load_image(path)
    # batch = pipeline(image).unsqueeze(0)
    return pipeline(image)
Beispiel #21
0
# crawl directories
dirs = os.listdir(opt.dir)
dirs = [join(opt.dir, d) for d in dirs]

dists = []
for dir in dirs:
    files = os.listdir(dir)
    if (opt.N is not None):
        files = files[:opt.N]
    F = len(files)
    print(dir)
    print(files)

    for (ff, file) in enumerate(files[:-1]):
        img0 = util.im2tensor(util.load_image(os.path.join(
            dir, file)))  # RGB image from [-1,1]
        if (opt.use_gpu):
            img0 = img0.cuda()

        if (opt.all_pairs):
            files1 = files[ff + 1:]
        else:
            files1 = [
                files[ff + 1],
            ]

        for file1 in files1:
            print('(%s,%s)' % (file, file1))
            img1 = util.im2tensor(util.load_image(os.path.join(dir, file1)))

            if (opt.use_gpu):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dir0', type=str, default='./imgs/ex_dir0')
parser.add_argument('--dir1', type=str, default='./imgs/ex_dir1')
parser.add_argument('--out', type=str, default='./imgs/example_dists.txt')
parser.add_argument('--use_gpu', action='store_true', help='turn on flag to use GPU')
opt = parser.parse_args()

# embed()

## Initializing the model
model = dm.DistModel()
model.initialize(model='net-lin',net='alex',use_gpu=opt.use_gpu)

# crawl directories
f = open(opt.out,'w')
files = os.listdir(opt.dir0)
for file in files:
	if(os.path.exists(os.path.join(opt.dir1,file))):
		# Load images
		img0 = util.im2tensor(util.load_image(os.path.join(opt.dir0,file))) # RGB image from [-1,1]
		img1 = util.im2tensor(util.load_image(os.path.join(opt.dir1,file)))

		# Compute distance
		dist01 = model.forward(img0,img1)
		print('%s: %.3f'%(file,dist01))
		f.writelines('%s: %.6f\n'%(file,dist01))


f.close()
    content = file.readlines()
    # This for loop deletes the EOF (like \n)
    for i in range(len(content)):
        content[i] = content[i][:len(content[i])-1]
    file.close()
    return content
files = text_readlines(opt.input)
print(len(files))

# crawl directories
f = open(opt.out,'w')

sum_dist = 0
for file in files:
    # Load images
    img0 = util.load_image(opt.dir0 + file)
    img1 = util.load_image(opt.dir1 + file)
    img1 = cv2.resize(img1, (img0.shape[1], img0.shape[0]))
    img0 = util.im2tensor(img0)
    img1 = util.im2tensor(img1)

    if(opt.use_gpu):
        img0 = img0.cuda()
        img1 = img1.cuda()

    # Compute distance
    dist01 = model.forward(img0,img1)
    sum_dist += dist01
    print('%s: %.3f'%(file,dist01))
    f.writelines('%s: %.6f\n'%(file,dist01))
        
Beispiel #24
0
import argparse
from models import dist_model as dm
from util import util

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--path0', type=str, default='./imgs/ex_ref.png')
parser.add_argument('--path1', type=str, default='./imgs/ex_p0.png')
parser.add_argument('--use_gpu', action='store_true', help='turn on flag to use GPU')
opt = parser.parse_args()

## Initializing the model
print("Initializing the model")
model = dm.DistModel()
model.initialize(model='net-lin',net='squeeze',use_gpu=opt.use_gpu)

# Load images
print("Load images")
img0 = util.im2tensor(util.load_image(opt.path0)) # RGB image from [-1,1]
img1 = util.im2tensor(util.load_image(opt.path1))

# Compute distance
print("Compute distance")
dist01 = model.forward(img0,img1)
print('Distance: %.3f'%dist01)
Beispiel #25
0
from util import util
from models import dist_model as dm
from IPython import embed
import os, sys
import numpy as np

use_gpu = True  # Whether to use GPU
spatial = False  # Return a spatial map of perceptual distance

## Initializing the model
model = dm.DistModel()

# Linearly calibrated models
model.initialize(model='net-lin', net='alex', use_gpu=use_gpu, spatial=spatial)

# Low-level metrics
print('Model [%s] initialized' % model.name())

files = os.listdir(sys.argv[1])
index = np.load(sys.argv[2])
lpips = np.zeros(10)
for i in range(10):
    p = index[i]
    score = 0.
    for j in range(1900):
        img1 = util.im2tensor(util.load_image(files[j]))
        img2 = util.im2tensor(util.load_image(files[p[j]]))
        score += model.forward(ex_ref, ex_p0)
    lpips[i] = score / 1900.

print(mp.mean(lpips), np.std(lpips))
Beispiel #26
0
score_max = 0
score_min = 1

for ii in range(5):
    for subdir in os.listdir(opt.dir):
        subdir = opt.dir + '/' + subdir
        count = 0
        all_file = os.listdir(subdir)
        for i in range(10):
            randp = np.random.permutation(len(all_file))
            rand1 = randp[0]
            rand2 = randp[1]
            # Load images
            img0 = util.im2tensor(
                util.load_image(os.path.join(
                    subdir, all_file[rand1])))  # RGB image from [-1,1]
            img1 = util.im2tensor(
                util.load_image(os.path.join(subdir, all_file[rand2])))
            # Compute distance
            dist01 = model.forward(img0, img1)
            num += 1
            score += dist01
    print('%d::%.3f' % (ii, score / num))
    if score / num > score_max:
        score_max = score / num
    if score / num < score_min:
        score_min = score / num
print('score: %.3f +- %.3f' % ((score_max + score_min) / 2,
                               (score_max - score_min) / 2))
# Off-the-shelf uncalibrated networks
#model.initialize(model='net',net='squeeze',use_gpu=use_gpu)
#model.initialize(model='net',net='alex',use_gpu=use_gpu)
#model.initialize(model='net',net='vgg',use_gpu=use_gpu)

# Low-level metrics
# model.initialize(model='l2',colorspace='Lab')
# model.initialize(model='ssim',colorspace='RGB')
print('Model [%s] initialized'%model.name())

## Example usage with dummy tensors
dummy_im0 = torch.Tensor(1,3,64,64) # image should be RGB, normalized to [-1,1]
dummy_im1 = torch.Tensor(1,3,64,64)
dist = model.forward(dummy_im0,dummy_im1)

## Example usage with images
ex_ref = util.im2tensor(util.load_image('./imgs/ex_ref.png'))
ex_p0 = util.im2tensor(util.load_image('./imgs/ex_p0.png'))
ex_p1 = util.im2tensor(util.load_image('./imgs/ex_p1.png'))
ex_d0 = model.forward(ex_ref,ex_p0)
ex_d1 = model.forward(ex_ref,ex_p1)
if not spatial:
    print('Distances: (%.3f, %.3f)'%(ex_d0, ex_d1))
else:
    print('Distances: (%.3f, %.3f)'%(ex_d0.mean(),ex_d1.mean()))            # The mean distance is approximately the same as the non-spatial distance
    
    # Visualize a spatially-varying distance map between ex_p0 and ex_ref
    import pylab
    pylab.imshow(ex_d0)
    pylab.show()
# import sys; sys.path += ['models']
import argparse
from util import util
from models import dist_model as dm

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--path0', type=str, default='./imgs/ex_ref.png')
parser.add_argument('--path1', type=str, default='./imgs/ex_p0.png')
parser.add_argument('--use_gpu', action='store_true', help='turn on flag to use GPU')
opt = parser.parse_args()

## Initializing the model
model = dm.DistModel()
model.initialize(model='net-lin',net='alex',use_gpu=opt.use_gpu)

# Load images
img0 = util.im2tensor(util.load_image(opt.path0)) # RGB image from [-1,1]
img1 = util.im2tensor(util.load_image(opt.path1))

# Compute distance
dist01 = model.forward(img0,img1)
print('Distance: %.3f'%dist01)
num = 0

score_max = 0
score_min = 1

for ii in range(10):
	for subdir in os.listdir(opt.dir):
		subdir = opt.dir + '/'+ subdir
		count = 0
		all_file = os.listdir(subdir)
		for i in range(20):
			randp = np.random.permutation(len(all_file))
			rand1 = randp[0]
			rand2 = randp[1]
			# Load images
			img0 = util.im2tensor(util.load_image(os.path.join(subdir,all_file[rand1]))) # RGB image from [-1,1]
			img1 = util.im2tensor(util.load_image(os.path.join(subdir,all_file[rand2])))
			# mask = torch.zeros(img0.shape)
			# mask[:, :, round((224/192)* 60):round((224/192) * 140), round((224/192) * 32):round((224/192)* 168)] = 1
			# img0 = img0 * mask
			# img1 = img1 * mask
			# Compute distance
			# dist01 = model.forward(img0[:, :, 92 :144, 48 :172],
			#    					   img1[:, :, 92 :144, 48 :172]) # celebA

			#dist01 = model.forward(img0[:, :, 68:104, 48:154],
			#					   img1[:, :, 68:104, 48:154])  # lfw

			dist01 = model.forward(img0[:, :, 48:78, 24:96],
								   img1[:, :, 48:78, 24:96])  # meglass
			#dist01 = model.forward(img0,img1)
Beispiel #30
0
#model.initialize(model='net',net='squeeze',use_gpu=use_gpu)
#model.initialize(model='net',net='alex',use_gpu=use_gpu)
#model.initialize(model='net',net='vgg',use_gpu=use_gpu)

# Low-level metrics
# model.initialize(model='l2',colorspace='Lab')
# model.initialize(model='ssim',colorspace='RGB')
print('Model [%s] initialized' % model.name())

## Example usage with dummy tensors
# dummy_im0 = torch.Tensor(1,3,64,64) # image should be RGB, normalized to [-1,1]
# dummy_im1 = torch.Tensor(1,3,64,64)
# dist = model.forward(dummy_im0,dummy_im1)

## Example usage with images
ex_ref = util.im2tensor(util.load_image('./imgs/ex_ref.png'))
ex_p0 = util.im2tensor(util.load_image('./imgs/ex_p0.png'))
ex_p1 = util.im2tensor(util.load_image('./imgs/ex_p1.png'))
ex_d0 = model.forward(ex_ref, ex_p0)
ex_d1 = model.forward(ex_ref, ex_p1)
if not spatial:
    print('Distances: (%.3f, %.3f)' % (ex_d0, ex_d1))
else:
    print(
        'Distances: (%.3f, %.3f)' % (ex_d0.mean(), ex_d1.mean())
    )  # The mean distance is approximately the same as the non-spatial distance

    # Visualize a spatially-varying distance map between ex_p0 and ex_ref
    import pylab
    pylab.imshow(ex_d0)
    pylab.show()
            if filespath[-3:] == 'jpg':
                ret.append(os.path.join(root, filespath))
    return ret


imglist_dir0 = get_files(opt.dir0)
imglist_dir1 = get_files(opt.dir1)
assert len(imglist_dir0) == len(imglist_dir1)
totallen = len(imglist_dir0)
print(totallen)

# crawl directories
f = open(opt.out, 'w')

for file in range(totallen):
    # Load images
    img0 = util.im2tensor(util.load_image(
        imglist_dir0[file]))  # RGB image from [-1,1]
    img1 = util.im2tensor(util.load_image(imglist_dir1[file]))

    if (opt.use_gpu):
        img0 = img0.cuda()
        img1 = img1.cuda()

    # Compute distance
    dist01 = model.forward(img0, img1)
    print('%s-th image: %.3f' % (file, dist01))
    f.writelines('%s: %.6f\n' % (file, dist01))

f.close()
Beispiel #32
0
## Initializing the model
model = dm.DistModel()
model.initialize(model='net-lin', net='alex', use_gpu=opt.use_gpu)

# crawl directories
f = open(opt.out, 'w')
files = os.listdir(opt.dir0)

print(files)

all_dist01 = []
for file in files:
    if (os.path.exists(os.path.join(opt.dir1, file))):
        print("a")
        # Load images
        img0 = util.im2tensor(util.load_image(os.path.join(
            opt.dir0, file)))  # RGB image from [-1,1]
        img1 = util.im2tensor(util.load_image(os.path.join(opt.dir1, file)))

        # Compute distance
        dist01 = model.forward(img0, img1)
        all_dist01.append(dist01)
        print('%s: %.3f' % (file, dist01))
        f.writelines('%s: %.6f\n' % (file, dist01))

all_dist01 = np.asarray(all_dist01, dtype=np.float32)
print('mean: {} std: {}'.format(all_dist01.mean(), all_dist01.std()))
f.writelines('mean: {} std: {}'.format(all_dist01.mean(), all_dist01.std()))
f.close()
torch.set_grad_enabled(False)

## Initializing the model
model = models.PerceptualLoss(model='net-lin',net='alex', use_gpu=opt.gpu_index >= 0 and torch.cuda.is_available(), gpu_ids=[opt.gpu_index])

results_dir = os.path.expanduser('~/results/diverse_gan/bicyclegan/%s/test/images' % opt.cfg)
print(results_dir)

t_LPIPS = 0.0
for i in range(opt.ninput):
    imgs = []
    for j in range(opt.nsample):
        img_name = 'input_%03d_random_sample%02d.png' % (i, j + 1)
        img_path = os.path.join(results_dir, img_name)
        # print(img_path)
        img = util.im2tensor(util.load_image(img_path))
        imgs.append(img)

    LPIPS = 0.0
    n_pairs = 0
    for p in range(1, len(imgs)):
        for q in range(p):
            LPIPS += model.forward(imgs[q].to(device), imgs[p].to(device)).item()
            n_pairs += 1
    LPIPS /= n_pairs
    t_LPIPS += LPIPS
    print('%d %.4f' % (i, LPIPS))

t_LPIPS /= opt.ninput
print('Total LPIPS %.4f' % t_LPIPS)
Beispiel #34
0
    for i in range(len(content)):
        content[i] = content[i][:len(content[i]) - 1]
    file.close()
    return content


files = text_readlines(opt.input)
print(len(files))

# crawl directories
f = open(opt.out, 'w')

sum_dist = 0
for file in files:
    # Load images
    img0 = util.im2tensor(util.load_image(opt.dir0 +
                                          file))  # RGB image from [-1,1]
    img1 = util.im2tensor(util.load_image(opt.dir1 + file))

    if (opt.use_gpu):
        img0 = img0.cuda()
        img1 = img1.cuda()

    # Compute distance
    dist01 = model.forward(img0, img1)
    sum_dist += dist01
    print('%s: %.3f' % (file, dist01))
    f.writelines('%s: %.6f\n' % (file, dist01))

sum_dist = sum_dist / len(files)
print(sum_dist)
Beispiel #35
0
import argparse
from models import dist_model as dm
from util import util
import numpy as np
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--path0', type=str, default='./imgs/ex_ref.png')
parser.add_argument('--path1', type=str, default='./imgs/ex_p0.png')
parser.add_argument('--use_gpu', action='store_true', help='turn on flag to use GPU')
opt = parser.parse_args()

## Initializing the model
model = dm.DistModel()
model.initialize(model='net-lin',net='vgg',use_gpu=opt.use_gpu)

# Load images
img0 = util.im2tensor(util.load_image(opt.path0), factor=1500./2.) # RGB image from [-1,1]
img1 = util.im2tensor(util.load_image(opt.path1), factor=1500./2.)

# Compute distance
dist01 = model.forward(img0,img1)
print('Distance: %.4f'%dist01)