UOMOP
Cifar10 Fourier 본문
import matplotlib.pyplot as plt
import numpy as np
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
from scipy.ndimage import sobel
# CIFAR-10 데이터셋 로드
transform = transforms.Compose([transforms.ToTensor()])
cifar10 = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
dataloader = DataLoader(cifar10, batch_size=1, shuffle=True)
# 엣지 검출량 계산 함수
def calculate_edge_amount(image):
image_gray = np.mean(image, axis=0) # RGB -> Grayscale 변환
dx = sobel(image_gray, axis=0)
dy = sobel(image_gray, axis=1)
edge_magnitude = np.hypot(dx, dy)
return edge_magnitude.mean()
# 엣지 검출량이 가장 많은 이미지와 가장 적은 이미지 찾기
max_edge_image = None
min_edge_image = None
max_edge_amount = -np.inf
min_edge_amount = np.inf
for images, _ in dataloader:
image = images[0].numpy()
edge_amount = calculate_edge_amount(image)
if edge_amount > max_edge_amount:
max_edge_amount = edge_amount
max_edge_image = image
if edge_amount < min_edge_amount:
min_edge_amount = edge_amount
min_edge_image = image
# 이미지 변환 함수
def compute_fft(image):
fft_image = np.fft.fft2(image, axes=(1, 2))
fft_image_shifted = np.fft.fftshift(fft_image, axes=(1, 2))
magnitude_spectrum = np.abs(fft_image_shifted)
return np.log(magnitude_spectrum + 1)
# 엣지 검출량이 가장 많은 이미지와 가장 적은 이미지의 푸리에 변환
fft_max_edge_image = compute_fft(max_edge_image)
fft_min_edge_image = compute_fft(min_edge_image)
# 원본 및 푸리에 변환 이미지 플롯
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# 엣지 검출량이 가장 많은 이미지와 그 푸리에 변환
axs[0, 0].imshow(max_edge_image.transpose(1, 2, 0))
axs[0, 0].set_title('Max Edge Original Image')
axs[0, 0].axis('off')
axs[0, 1].imshow(np.sum(fft_max_edge_image, axis=0), cmap='gray')
axs[0, 1].set_title('Max Edge Fourier Transform')
axs[0, 1].axis('off')
# 엣지 검출량이 가장 적은 이미지와 그 푸리에 변환
axs[1, 0].imshow(min_edge_image.transpose(1, 2, 0))
axs[1, 0].set_title('Min Edge Original Image')
axs[1, 0].axis('off')
axs[1, 1].imshow(np.sum(fft_min_edge_image, axis=0), cmap='gray')
axs[1, 1].set_title('Min Edge Fourier Transform')
axs[1, 1].axis('off')
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
# CIFAR-10 데이터셋 로드
transform = transforms.Compose([transforms.ToTensor()])
cifar10 = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
dataloader = DataLoader(cifar10, batch_size=2, shuffle=True)
# 이미지 2장 가져오기
data_iter = iter(dataloader)
images, labels = next(data_iter)
# 원본 이미지
image1 = images[0].numpy().transpose(1, 2, 0) # 채널 축을 마지막으로 이동
image2 = images[1].numpy().transpose(1, 2, 0)
# 푸리에 변환 이미지 계산
def compute_fft(image):
# RGB 채널별로 푸리에 변환을 계산하고, 각 채널의 크기를 결합하여 시각화
fft_image = np.fft.fft2(image, axes=(0, 1))
fft_image_shifted = np.fft.fftshift(fft_image, axes=(0, 1)) # 중심을 이동
magnitude_spectrum = np.abs(fft_image_shifted) # 크기 계산
return np.log(magnitude_spectrum + 1) # 로그 스케일 적용
fft_image1 = compute_fft(image1)
fft_image2 = compute_fft(image2)
# 원본 및 푸리에 변환 이미지 플롯
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# 첫 번째 이미지와 그 푸리에 변환
axs[0, 0].imshow(image1)
axs[0, 0].set_title('Original Image 1')
axs[0, 0].axis('off')
axs[0, 1].imshow(fft_image1, cmap='gray')
axs[0, 1].set_title('Fourier Transform Image 1')
axs[0, 1].axis('off')
# 두 번째 이미지와 그 푸리에 변환
axs[1, 0].imshow(image2)
axs[1, 0].set_title('Original Image 2')
axs[1, 0].axis('off')
axs[1, 1].imshow(fft_image2, cmap='gray')
axs[1, 1].set_title('Fourier Transform Image 2')
axs[1, 1].axis('off')
plt.tight_layout()
plt.show()
'DE > Code' 카테고리의 다른 글
FLOPs (Proposed encoder) (0) | 2024.08.28 |
---|---|
Level decision model's MSE training (x+x_f, x, x_f) (0) | 2024.08.27 |
DE : Selection (33%, 60%, 75%) (0) | 2024.08.15 |
Masked, Reshaped, index Gen (0) | 2024.08.10 |
DeepJSCC BenchMark (0) | 2024.08.10 |
Comments