<jupyter_start><jupyter_text>This notebook converts a specific number of images from the `img_align_celeba` dataset to greyscale and saves them in a numpy array.  This then could be used for a DCGAN that takes 1x32x32 images.  Note that the images do have to be transformed within `numpy_dataset()` in the DCGAN notebook:     transform=transforms.Compose([                               transforms.Resize(image_size),                               transforms.CenterCrop(image_size),                               transforms.ToTensor(),                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),                           ])<jupyter_code>import numpy as np
import glob as glob
import matplotlib.pyplot as plt

from PIL import Image
from tqdm import tqdm

%matplotlib inline
%config InlineBackend.print_figure_kwargs={'facecolor' : "w"} # Make sure the axis background of plots is white, this is usefull for the black theme in JupyterLab<jupyter_output><empty_output><jupyter_text>Load images<jupyter_code>paths = glob.glob('F:\Jupyterlab\DCGAN\data\img_align_celeba - complete\*.jpg')
print('Detected %s images' % len(paths))
"""
Loads images as PIL images from paths.
Saves the PIL images as a numpy array containing these PIL images.
"""

paths = paths[:10**2] # selected the first 100k images

images = []

for _ , path in enumerate(tqdm(paths)):
    image = Image.open(path) # opens PIL images
    #image = np.expand_dims(image, axis=0) # add channel dimension (N_images, C, H, W)
    images.append(image)<jupyter_output>100%|██████████████████████████████████████████████████████████████████████████████| 100/100 [00:00<00:00, 4917.87it/s]<jupyter_text>Convert the list of images to an array and save the array<jupyter_code>%%time
images = np.array(images)
images.shape
plt.imshow(images[0, 0, :, :], cmap='gray')
%%time
save_path = 'F:\Jupyterlab\SRON-DCGAN\data\\'
np.save(save_path+'celeb_faces.npy', images)
print('DONE')<jupyter_output>DONE
{'fraction_non_alphanumeric': 0.14423076923076922, 'fraction_numerical': 0.02479757085020243, 'mean_word_length': 3.7869249394673123, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '233133', 'license': 'MIT', 'n_tokens_mistral': 733, 'n_tokens_neox': 659, 'n_words': 181, 'path': 'notebooks/old notebooks/faces_to_npy.ipynb', 'repo_name': 'deKeijzer/SRON-DCGAN', 'size': '1976'}