File size: 9,046 Bytes
2ad255e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# Copyright 2024 Hui Lu, Fang Dai, Siqiong Yao.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# import os
# import torch
# import numpy as np
# import torchvision.transforms as transforms
# from torch.utils.data import DataLoader, Dataset
# from PIL import Image
# from gtda.images import Binarizer, HeightFiltration
# from gtda.homology import CubicalPersistence
# from gtda.diagrams import Amplitude
# from sklearn.metrics import pairwise_distances
# transform = transforms.Compose([
# transforms.Resize((256, 256)),
# transforms.ToTensor(),
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# ])
# class ImageFolderDataset(Dataset):
# def __init__(self, folder_path, transform=None):
# self.file_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.png')]
# self.transform = transform
# def __len__(self):
# return len(self.file_paths)
# def __getitem__(self, idx):
# img_path = self.file_paths[idx]
# image = Image.open(img_path).convert('RGB')
# if self.transform:
# image = self.transform(image)
# return image
# def load_data(folder_path):
# dataset = ImageFolderDataset(folder_path, transform=transform)
# loader = DataLoader(dataset, batch_size=10, shuffle=False)
# return loader
# # 计算Diversity Score
# def calculate_diversity_score(features):
# distances = pairwise_distances(features, metric='euclidean')
# diversity_score = np.mean(distances)
# return diversity_score
# # 计算Geometry Score
# def calculate_geometry_score(images):
# binarizer = Binarizer(threshold=0.5)
# height_filtration = HeightFiltration(direction=np.array([1, 1, 1]))
# cubical_persistence = CubicalPersistence(homology_dimensions=[0, 1], coeff=2)
# amplitude = Amplitude(metric='wasserstein', metric_params={'p': 2})
# # Preprocess images
# images = np.array([img.numpy() if isinstance(img, torch.Tensor) else img for img in images])
# images_binarized = binarizer.fit_transform(images)
# images_filtered = height_filtration.fit_transform(images_binarized)
# diagrams = cubical_persistence.fit_transform(images_filtered)
# gs_score = amplitude.fit_transform(diagrams)
# return gs_score.mean()
# generated_images_loader = load_data('../figure/1')
# real_images_loader = load_data('../figure/2')
# generated_features = []
# real_features = []
# for img_batch in generated_images_loader:
# generated_features.extend(img_batch.numpy())
# for img_batch in real_images_loader:
# real_features.extend(img_batch.numpy())
# generated_features = np.array(generated_features)
# real_features = np.array(real_features)
# # 计算Diversity Score
# generated_div_score = calculate_diversity_score(generated_features.reshape(len(generated_features), -1))
# real_div_score = calculate_diversity_score(real_features.reshape(len(real_features), -1))
# # 计算Geometry Score
# generated_gs_score = calculate_geometry_score(generated_features)
# real_gs_score = calculate_geometry_score(real_features)
# print(f"Generated Images Diversity Score: {generated_div_score}")
# print(f"Real Images Diversity Score: {real_div_score}")
# print(f"Generated Images Geometry Score: {generated_gs_score}")
# print(f"Real Images Geometry Score: {real_gs_score}")
# import torch
# import torch.nn.functional as F
# from torchvision import transforms
# from PIL import Image
# import numpy as np
# import os
# # Function to load and preprocess images
# def load_and_preprocess_image(img_path):
# img = Image.open(img_path).convert('RGB')
# preprocess = transforms.Compose([
# transforms.ToTensor(),
# ])
# img = preprocess(img).unsqueeze(0) # Add batch dimension
# return img
# # Function to compute image gradients
# def compute_gradients(img):
# grad_x = img[:, :, 1:, :] - img[:, :, :-1, :]
# grad_y = img[:, :, :, 1:] - img[:, :, :, :-1]
# return grad_x, grad_y
# # Function to calculate Gradient Similarity (GS)
# def gradient_similarity(real_img, gen_img):
# real_grad_x, real_grad_y = compute_gradients(real_img)
# gen_grad_x, gen_grad_y = compute_gradients(gen_img)
# grad_sim_x = F.cosine_similarity(real_grad_x, gen_grad_x, dim=1).mean()
# grad_sim_y = F.cosine_similarity(real_grad_y, gen_grad_y, dim=1).mean()
# gs = (grad_sim_x + grad_sim_y) / 2.0
# return gs.item()
# # Example usage
# real_img_dir = '../GS/real' # Replace with your real image directory
# gen_img_dir = '../GS/fake' # Replace with your generated image directory
# real_img_paths = [os.path.join(real_img_dir, fname) for fname in os.listdir(real_img_dir) if fname.endswith(('jpg', 'jpeg', 'png'))]
# gen_img_paths = [os.path.join(gen_img_dir, fname) for fname in os.listdir(gen_img_dir) if fname.endswith(('jpg', 'jpeg', 'png'))]
# # Ensure both directories have the same number of images
# assert len(real_img_paths) == len(gen_img_paths), "The number of images in both directories must be the same"
# gs_scores = []
# for real_img_path, gen_img_path in zip(real_img_paths, gen_img_paths):
# real_img = load_and_preprocess_image(real_img_path)
# gen_img = load_and_preprocess_image(gen_img_path)
# gs = gradient_similarity(real_img, gen_img)
# gs_scores.append(gs)
# print(f'Processed {real_img_path} and {gen_img_path}: GS = {gs:.3e}')
# mean_gs = np.mean(gs_scores)
# print(f'Mean Gradient Similarity (GS) score: {mean_gs:.3e}')
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
import numpy as np
import os
from prdc import compute_prdc
# Function to load and preprocess images
def load_and_preprocess_image(img_path):
img = Image.open(img_path).convert('RGB')
preprocess = transforms.Compose([
transforms.Resize((299, 299)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
img = preprocess(img).unsqueeze(0) # Add batch dimension
return img
# Function to extract features using InceptionV3
def extract_features(img_paths, model):
features = []
with torch.no_grad():
for img_path in img_paths:
img = load_and_preprocess_image(img_path)
feature = model(img).numpy().squeeze()
features.append(feature)
features = np.array(features)
return features
# Load the InceptionV3 model
model = models.resnet18(pretrained=False)
model.load_state_dict(torch.load('../modelsaved/Pretrained_InceptionV3.pth', map_location=lambda storage, loc: storage),strict=False)
model.fc = nn.Identity() # Remove the final classification layer
model.eval()
# Example usage
real_img_dir = '../dataset/1' # Replace with your real image directory
gen_img_dir = '../dataset/2' # Replace with your generated image directory
real_img_paths = [os.path.join(real_img_dir, fname) for fname in os.listdir(real_img_dir) if fname.endswith(('jpg', 'jpeg', 'png'))]
gen_img_paths = [os.path.join(gen_img_dir, fname) for fname in os.listdir(gen_img_dir) if fname.endswith(('jpg', 'jpeg', 'png'))]
# Extract features for real and generated images
real_features = extract_features(real_img_paths, model)
gen_features = extract_features(gen_img_paths, model)
# Calculate PRDC metrics
metrics = compute_prdc(real_features=real_features,
fake_features=gen_features,
nearest_k=2)
print(metrics)
# import torch
# from torch import nn
# from clip import clip
# import numpy as np
# clip_model, preprocess = clip.load("ViT-L/14@336px", device="cuda")
# def get_clip_embedding(images):
# with torch.no_grad():
# images = preprocess(images).unsqueeze(0).to("cuda")
# image_features = clip_model.encode_image(images)
# return image_features
# def compute_mmd(x, y, kernel):
# xx = kernel(x, x)
# yy = kernel(y, y)
# xy = kernel(x, y)
# mmd = torch.mean(xx) + torch.mean(yy) - 2 * torch.mean(xy)
# return mmd
# def gaussian_rbf_kernel(x, y, sigma=1.0):
# dist = torch.cdist(x, y, p=2.0)
# return torch.exp(-dist**2 / (2 * sigma**2))
# real_images = ...
# generated_images = ...
# real_features = get_clip_embedding(real_images)
# generated_features = get_clip_embedding(generated_images)
# sigma = 1.0
# mmd = compute_mmd(real_features, generated_features, lambda x, y: gaussian_rbf_kernel(x, y, sigma))
# cmmd = mmd * 1000
# print(f"CMMD: {cmmd.item()}")
|