[VP] 缩小图像并计算Content Loss

缩小图像并计算Content Loss


代码

所用图像如下, s h a p e = ( 300 , 300 ) shape=(300, 300) shape=(300,300),请自行更换图像:
在这里插入图片描述
我主要使用了四种方法缩小图像:

  • 1、直接删去图像偶数行列

  • 2、使用高斯平滑,然后删去图像偶数行列

  • 3、使用最大池化

  • 4、使用均值池化

Content Loss由于需要相同的shape才能计算,所以我统一用opencv的resize默认方式将缩小的图像放大回去,便于计算Loss

import numpy as np
from scipy.ndimage import rotate
import os
from PIL import Image
import math
import cv2
import matplotlib.pyplot as plt
%matplotlib inline

im = cv2.imread('figures/exampleFig.jpg', 0)
im = cv2.resize(im,(300,300))
plt.imshow(im)

def content_loss(im1, im2):
    return np.mean(np.square((im1 - im2)))

def GaussianFilter(u, v, sigma):
    f = (1 / (2 * math.pi * sigma**2)) * math.e**(-(u**2+v**2) / 2*sigma**2)
    return f

def decrease1(im):
    
    imD = np.zeros((int(im.shape[0] / 2), int(im.shape[1] / 2))).astype(np.uint8)
    
    for i in range(int(im.shape[0] / 2)):
        for j in range(int(im.shape[1] / 2)):
            imD[i][j] = im[i*2][j*2]
    return imD

def decrease2(im):
    im = cv2.filter2D(im, kernel = GFKernel, ddepth = -1)
    
    imD = np.zeros((int(im.shape[0] / 2), int(im.shape[1] / 2))).astype(np.uint8)
    
    for i in range(int(im.shape[0] / 2)):
        for j in range(int(im.shape[1] / 2)):
            imD[i][j] = im[i*2][j*2]
    return imD

def decrease3(im):
    
    imD = np.zeros((int(im.shape[0] / 2), int(im.shape[1] / 2))).astype(np.uint8)
    
    for i in range(int(imD.shape[0])):
        for j in range(int(imD.shape[1])):
            imD[i][j] = np.max(im[i*2:i*2+2, j*2:j*2+2])
    return imD

def decrease4(im):
    
    imD = np.zeros((int(im.shape[0] / 2), int(im.shape[1] / 2))).astype(np.uint8)
    
    for i in range(int(imD.shape[0])):
        for j in range(int(imD.shape[1])):
            imD[i][j] = np.mean(im[i*2:i*2+2, j*2:j*2+2])
    return imD

def estimateLoss(im, way = 1):
    _, ax = plt.subplots(nrows=1, ncols=3, figsize=(20, 10))
    
    if way == 1:
        a = decrease1(decrease1(im))
    elif way == 2:
        a = decrease2(decrease1(im))
    elif way == 3:
        a = decrease3(decrease1(im))
    elif way == 4:
        a = decrease4(decrease1(im))
    else:
        print('Error: You should choose way from 1-4')
        
    print(a.shape)
    b = cv2.resize(a, (300, 300))
    print(b.shape)
    
    ax[0].imshow(im)
    ax[0].set_title("Original")
    
    ax[1].imshow(a)
    ax[1].set_title("DownSample")
    
    ax[2].imshow(b)
    ax[2].set_title("DownSample to UpSample")
    
    loss = content_loss(im, b)
    
    return loss


GFKernel = [[GaussianFilter(-1,  1, 1), GaussianFilter(0,  1, 1), GaussianFilter(1,  1, 1)], 
            [GaussianFilter(-1,  0, 1), GaussianFilter(0,  0, 1), GaussianFilter(1,  0, 1)], 
            [GaussianFilter(-1, -1, 1), GaussianFilter(0, -1, 1), GaussianFilter(1, -1, 1)]]
GFKernel = np.array(GFKernel)


print('Delete Even Rows and Columns')
print('loss: ', estimateLoss(im, 1))

print('Using Gassian Filter Then Delete Even Rows and Columns')
print('loss: ', estimateLoss(im, 2))

print('Max Pooling')
print('loss: ', estimateLoss(im, 3))

print('Mean Pooling')
print('loss: ', estimateLoss(im, 4))

结果对比

1、直接删除偶数行列,content loss 为 77.55943333333333

在这里插入图片描述

2、使用高斯平滑,然后删去图像偶数行列,content loss 为 91.84892222222223

在这里插入图片描述
3、使用最大池化,content loss 为 83.76383333333334
在这里插入图片描述

4、使用均值池化,content loss 为 73.81282222222222
在这里插入图片描述

Content Loss计算的是像素值差异,因此我们可以估计缩小前后变化像素的多少来预估。第四幅图像的损失最低,这是因为像素值是平均值,并且更接近之前周围的像素值。第二种算法的损失最高,因为它通过加权改变了所有的像素。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是土豆大叔啊!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值