Make split_and_compare_prnus_of_subgroups.py only use 2 images of RAM

quantity
This commit is contained in:
Benjamin Loison 2024-04-27 19:24:12 +02:00
parent 4ac75448a6
commit 9d8e5ddbec
No known key found for this signature in database

View File

@ -21,35 +21,32 @@ IMAGES_FOLDER = 'flat-field/TIF'
imagesFileNames = os.listdir(IMAGES_FOLDER)
# To not have a bias (chronological for instance) when split to make subgroups.
shuffle(imagesFileNames)
numberOfImagesPerSubgroup = len(imagesFileNames) // NUMBER_OF_SUBGROUPS
numberOfImagesThresholds = range(numberOfImagesPerSubgroup, numberOfImagesPerSubgroup + 1)
subgroupsPrnuEstimatesNpArray = []
for subgroupIndex in tqdm(range(NUMBER_OF_SUBGROUPS), 'Subgroup'):
subgroupPrnuEstimateNpArray = []
subgroupIterativeMean = iterativeMean()
subgroupImagesFileNames = imagesFileNames[numberOfImagesPerSubgroup * subgroupIndex : numberOfImagesPerSubgroup * (subgroupIndex + 1)]
for imageFileName in tqdm(subgroupImagesFileNames, f'Image of subgroup {subgroupIndex}'):
imagePath = f'{IMAGES_FOLDER}/{imageFileName}'
imagePil = Image.open(imagePath)
numberOfImagesPerSubgroup = len(imagesFileNames) // NUMBER_OF_SUBGROUPS
subgroupsIterativeMean = [iterativeMean() for _ in range(NUMBER_OF_SUBGROUPS)]
rmss = []
for subgroupImageIndex in tqdm(range(numberOfImagesPerSubgroup), 'Subgroup image index'):
for subgroupIndex in tqdm(range(NUMBER_OF_SUBGROUPS), 'Subgroup'):
imageIndex = (subgroupIndex * NUMBER_OF_SUBGROUPS) + subgroupImageIndex
imageFileName = imagesFileNames[imageIndex]
imageFilePath = f'{IMAGES_FOLDER}/{imageFileName}'
imagePil = Image.open(imageFilePath)
imageNpArray = img_as_float(np.array(imagePil))
imagePrnuEstimateNpArray = imageNpArray - denoise(imageNpArray, DENOISER)
subgroupIterativeMean = subgroupsIterativeMean[subgroupIndex]
subgroupIterativeMean.add(imagePrnuEstimateNpArray)
subgroupPrnuEstimateNpArray += [subgroupIterativeMean.mean]
if subgroupIndex == NUMBER_OF_SUBGROUPS - 1:
assert NUMBER_OF_SUBGROUPS == 2
rms = rmsDiffNumpy(subgroupIterativeMean.mean, subgroupsIterativeMean[1 - subgroupIndex].mean)
rmss += [rms]
subgroupsPrnuEstimatesNpArray += [subgroupPrnuEstimateNpArray]
mostImagesSubgroupPrnuEstimatesNpArray = [subgroupPrnuEstimatesNpArray[-1] for subgroupPrnuEstimatesNpArray in subgroupsPrnuEstimateNpArray]
mostImagesSubgroupPrnuEstimatesNpArray = [subgroupIterativeMean.mean for subgroupIterativeMean in subgroupsIterativeMean]
minimum = np.min(mostImagesSubgroupPrnuEstimatesNpArray)
maximum = np.max(mostImagesSubgroupPrnuEstimatesNpArray)
for subgroupIndex in range(NUMBER_OF_SUBGROUPS):
plt.imsave(f'prnu_subgroup_{subgroupIndex}.png', (subgroupsPrnuEstimateNpArray[subgroupIndex][-1] - minimum) / (maximum - minimum))
rmss = []
for numberOfImagesIndex in range(len(numberOfImagesThresholds)):
rms = rmsDiffNumpy(subgroupsPrnuEstimatesNpArray[0][numberOfImagesIndex], subgroupsPrnuEstimatesNpArray[1][numberOfImagesIndex])
rmss += [rms]
plt.imsave(f'prnu_subgroup_{subgroupIndex}.png', (subgroupsIterativeMean[subgroupIndex].mean - minimum) / (maximum - minimum))
plt.title(f'RMS between both subgroups estimated PRNUs with {DENOISER} denoiser for a given number of images among them')
plt.xlabel('Number of images of each subgroup')