目录
目录
1、训练集和测试集划分(仅图片)
将图像按照一定比例随机划分为训练集和测试集,并复制到相应的文件夹下。
import os
import random
import shutil
# 将图片按照一定比例划分为训练集和测试集
# 设置文件夹路径和划分比例
folder_path = '/data2/lxt/data/2020_ICIP_Align_all/All_visible/' # 要划分图像的所在的文件夹路径
# folder_path = '/data2/lxt/data/2020_ICIP_Align_all/All_IR/'
train_ratio = 0.9 # 训练集所占的比例
# 获取所有图片路径
img_paths = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.jpg') or file.endswith('.png'): # 选择出文件中以相应后缀结尾的图片
img_paths.append(os.path.join(root, file))
# 计算训练集和测试集的数量
num_imgs = len(img_paths)
num_train = int(num_imgs * train_ratio)
num_test = num_imgs - num_train
# 随机打乱图片列表
random.shuffle(img_paths)
# 复制训练集图片到指定目录
train_path = '/data2/lxt/code/generate_images/pytorch-CycleGAN-and-pix2pix-master-all_icip/datasets/All_ICIP/trainA'
for i in range(num_train):
img_name = os.path.basename(img_paths[i])
shutil.copy(img_paths[i], os.path.join(train_path, img_name))
# 复制测试集图片到指定目录
test_path = '/data2/lxt/code/generate_images/pytorch-CycleGAN-and-pix2pix-master-all_icip/datasets/All_ICIP/testA'
for i in range(num_train, num_imgs):
img_name = os.path.basename(img_paths[i])
shutil.copy(img_paths[i], os.path.join(test_path, img_name))
2、配对数据中对应训练集和测试集的划分(仅图片)
对于红外与可见光的配对图像,当将红外图像按照一定比例划分为训练集和测试集之后,需要将可见光图像中与红外图像配对的图像选择出来划分为对应的训练集和测试集,此时可以按照以下代码进行划分。
import shutil
import os
# 按照划分好训练集和测试集的红外数据名称进行可见光数据的划分
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
def copy_img():
'''
复制、重命名、粘贴文件
:return:
'''
# 原图文件夹地址
ori_img_name = r'/data/lxt/data/KAIST/kaist/testIR' # 原本划分好的测试集(红外图片)所在的文件夹路径
target =r'/data/lxt/data/KAIST/VISIBLE' # 所有可见光图片存放的路径
imgs = os.listdir(target)
a = []
n = 0
path = r'/data/lxt/data/KAIST/kaist/testVISIBLE/' # 存放可见光图片测试集的文件夹路径
depth_name = os.listdir(ori_img_name)
for i in imgs:
name1 = i.split('_')[0]+'_'+i.split('_')[1]
RGB_name = name1 + '_V_' + i.split('_')[3] # 可见光图像名称(名称是否拼接正确需要在运行代码之前进行调试确认)
IR_name = name1 + '_' + i.split('_')[3] # 红外图像名称(名称是否拼接正确需要在运行代码之前进行调试确认)
if IR_name in depth_name: # 如果当前红外图像在测试集中,则将该红外图像对应的可见光图像复制到相应的测试集文件夹中
shutil.copy(target + '/' + RGB_name, path + '/' + RGB_name)
else:
a.append(RGB_name)
n += 1
print('a :', a)
print('num:', n)
if __name__ == '__main__':
copy_img()
3、将可见光图像转换为灰度图
from PIL import Image
import os
# 定义输入和输出文件夹路径
input_folder = '/data/lxt/data/KAIST/kaist/testA' # 原始可见光图片所在文件夹路径
output_folder = '/data/lxt/data/KAIST/kaist_grey/testA_grey' # 生成灰度图像所要存放的路径(需要手动新建)
# 遍历输入文件夹中的所有文件
for filename in os.listdir(input_folder):
# 检查文件是否为图像文件
if filename.endswith('.jpg') or filename.endswith('.png'):
# 读取图像文件
img = Image.open(os.path.join(input_folder, filename))
# 将图像转换为灰度图
img_gray = img.convert('L')
# 保存灰度图到输出文件夹中,并保留原始图像命名
output_filename = os.path.join(output_folder, filename)
img_gray.save(output_filename)
Pix2pix客观结果评估
import os
import cv2
from PIL import Image
import numpy as np
import pytorch_ssim
import math
import torch
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
# 计算生成图像的质量
def psnr2(img1, img2):
mse = np.mean((img1 / 255.0 - img2 / 255.0) ** 2)
if mse < 1e-10:
return 100
psnr2 = 20 * math.log10(1 / math.sqrt(mse))
return psnr2
def calculate_metrics(gen_folder):
psnr_list = []
ssim_list = []
i = 0
for filename in os.listdir(gen_folder):
if 'fake' in filename:
i = i + 1
gen_img = np.array(Image.open(os.path.join(gen_folder, filename)))
target_img_name = filename.replace("fake", "real")
target_img = np.array(Image.open(os.path.join(gen_folder, target_img_name)))
# psnr = peak_signal_noise_ratio(target_img, gen_img, data_range=255)
psnr1=psnr2(target_img, gen_img)
# 将图像转换为张量
ssim1 = structural_similarity(target_img, gen_img, data_range=255, channel_axis=2)
psnr_list.append(psnr1)
ssim_list.append(ssim1)
print(i,':'+filename)
print(psnr1)
print(ssim1)
avg_psnr = np.mean(psnr_list)
avg_ssim = np.mean(ssim_list)
return avg_psnr, avg_ssim
gen_folder = '/data/lxt/code/generate_images/new_code/pytorch-CycleGAN-and-pix2pix-master-all_cvc14_unet_grey/cvc14_Align_PIX2PIX_200epoch_lossgan_1_lossl1_1_256_Grey_200pt/cvc_14_grey_PIX2PIX_200epoch_lossgan_1_lossl1_1_unet_scale_00001_50_150/test_latest/images/'
# target_folder = 'path/to/target/images'
avg_psnr, avg_ssim = calculate_metrics(gen_folder)
print('Average PSNR:', avg_psnr)
print('Average SSIM:', avg_ssim)
cyclegan客观结果评估
import os
import cv2
from PIL import Image
import numpy as np
import pytorch_ssim
import math
import torch
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
# 计算生成图像的质量
def psnr2(img1, img2):
mse = np.mean((img1 / 255.0 - img2 / 255.0) ** 2)
if mse < 1e-10:
return 100
psnr2 = 20 * math.log10(1 / math.sqrt(mse))
return psnr2
def calculate_metrics(gen_folder, target_fold):
psnr_list = []
ssim_list = []
for filename in os.listdir(gen_folder):
if 'fake' in filename:
gen_img = np.array(Image.open(os.path.join(gen_folder, filename)))
target_img_name = filename.split('_')[0]+'_'+filename.split('_')[1]+'_'+filename.split('_')[3]+'.jpg'
target_img = np.array(Image.open(os.path.join(target_fold, target_img_name)))
psnr1=psnr2(target_img, gen_img)
ssim1 = structural_similarity(target_img, gen_img, data_range=255,channel_axis=2)
psnr_list.append(psnr1)
ssim_list.append(ssim1)
print(filename)
print(psnr1)
print(ssim1)
avg_psnr = np.mean(psnr_list)
avg_ssim = np.mean(ssim_list)
return avg_psnr, avg_ssim
gen_folder = '/data/lxt/code/generate_images/new_code/pytorch-CycleGAN-and-pix2pix-master-all_kaist_unet_add_intensity_loss_cycleGan/KAIST_Align_cyclegan_200epoch_lossgan_1_lossl1_1_256_grey_test_set00-07_intensityloss_25epoch/kaist_grey_cyclegan_indentisyloss_200epoch_lossgan_1_lossl1_1_unet_scale_00002_50_150/test_latest/images/'
target_folder = '/data/lxt/data/KAIST/kaist_grey/testB/'
avg_psnr, avg_ssim = calculate_metrics(gen_folder, target_folder)
print('Average PSNR:', avg_psnr)
print('Average SSIM:', avg_ssim)
损失曲线绘制
import os
import matplotlib.pyplot as plt
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
import torch
torch.cuda.current_device()
torch.cuda._initialized = True
def draw(log_path: str = 'weights/log.txt') -> None:
try:
txt = open(log_path, 'r')
except:
print('Invalid path.')
exit(-1)
contents0 = txt.readlines()
txt.close()
contents = [content for content in contents0 if content.startswith('(epoch')]
jump_display = True if input('Need jumping display? [y/n]: ') == 'y' else False
iterations, loss_G_GAN, loss_G_L1, loss_G_D_real, loss_G_D_fake,total_loss = [], [], [], [], [],[]
for i, content in enumerate(contents):
if jump_display and i % 50 != 0:
continue
# iter0, loss_info0 = content.split('\t\t')[0].strip(' '), \
# content.split('\t')[2].strip(' ')
iter = 100 * i
ls_G_GAN = float(content.split(': ')[5].split(' ')[0])
ls_G_L1 = float(content.split(': ')[6].split(' ')[0])
ls_D_real = float(content.split(': ')[7].split(' ')[0])
ls_D_fake = float(content.split(': ')[8].split(' ')[0])
# total_ls = float(content.split(',')[5].strip(' ').split(' ')[-1])
iterations.append(iter)
loss_G_GAN.append(ls_G_GAN)
loss_G_L1.append(ls_G_L1)
loss_G_D_real.append(ls_D_real)
loss_G_D_fake.append(ls_D_fake)
# total_loss.append(total_ls)
fig = plt.figure(num=1, dpi=400)
fig.tight_layout()
plt.subplots_adjust(wspace=0.5, hspace=0.5)
# plt.subplot(511)
plt.figure(1)
plt.title('loss_G_GAN')
plt.xlabel('Iteration')
plt.ylabel('Loss')
plt.plot(iterations, loss_G_GAN)
# plt.subplot(512)
plt.figure(2)
plt.title('loss_G_L1')
plt.xlabel('Iteration')
plt.ylabel('Loss')
plt.plot(iterations, loss_G_L1)
# plt.subplot(513)
plt.figure(3)
plt.title('loss_G_D_real')
plt.xlabel('Iteration')
plt.ylabel('Loss')
plt.plot(iterations, loss_G_D_real)
# plt.subplot(514)
plt.figure(4)
plt.title('loss_G_D_fake')
plt.xlabel('Iteration')
plt.ylabel('Loss')
plt.plot(iterations, loss_G_D_fake)
# plt.subplot(515)
# plt.figure(5)
# plt.title('Total Loss')
# plt.xlabel('Iteration')
# plt.ylabel('Loss')
# plt.plot(iterations, total_loss)
if input('Want to save pic? [y/n]: ') == 'y':
plt.savefig('Loss_curves.png', dpi=400)
plt.show()
if __name__ == '__main__':
draw('/data/lxt/code/generate_images/new_code/pytorch-CycleGAN-and-pix2pix-master-all_cvc14_unet_grey/checkpoints/cvc_14_grey_PIX2PIX_200epoch_lossgan_1_lossl1_1_unet_scale_00001_50_150/loss_log.txt')
为数据名称加前缀
import os
import shutil
def change_name(source_folder='s',target_folder='d',prefix='pre'):
source_folder = source_folder
target_folder = target_folder
prefix = prefix
if not os.path.exists(target_folder):
os.mkdir(target_folder)
for filename in os.listdir(source_folder):
if filename.endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif')):
new_filename = prefix + filename
old_filepath = os.path.join(source_folder, filename)
new_filepath = os.path.join(target_folder, new_filename)
shutil.copy2(old_filepath, new_filepath)
change_name(source_folder=r'/data2/lxt/data/data3000/v1',target_folder=r'/data2/lxt/data/data3000/all_visible',prefix='set07_v002_V_')
/data2/lxt/code/generate_images/new_code_old_3090/pytorch-CycleGAN-and-pix2pix-master
/data2/lxt/code/generate_images/new_code_old_3090/real_pytorch-CycleGAN-and-pix2pix-master-all_kaist_resnet_add_intensity_loss_cycleGan_grey_two_direction/
/data2/lxt/code/generate_images/new_code_old_3090/real_pytorch-CycleGAN-and-pix2pix-master-all_kaist_resnet_add_intensity_loss_cycleGan_RGB_two_direction/
/data2/lxt/code/generate_images/new_code_old_3090/pytorch-CycleGAN-and-pix2pix-master-all_kaist_resnet_add_intensity_loss_cycleGan_grey_two_direction
/data/lxt/code/generate_images/new_code/Cyclegan/pytorch-CycleGAN-and-pix2pix-master-all_kaist_unet_add_single_intensity_loss_cycleGan_grey_unaligned_695_697/