Chapter 21
Fetching & Loading CelebA
NotebookPython 3 (ipykernel)19 cells
Fetching & Loading CelebA
In [41]python · cell 2
python
# ! pip install torchvisionIn [42]python · cell 3
python
import torchvisionFetching CelebA dataset
-
Downloading the image files manually
- You can try setting
download=Truebelow. If this results in aBadZipfileerror, we recommend downloading theimg_align_celeba.zipfile manually from http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html. In the Google Drive folder, you can find it under theImgfolder as shown below:
- You can try setting
In [43]python · cell 6
python
IPythonImage(filename='figures/gdrive-download-location-1.png', width=500)Output
<IPython.core.display.Image object>
- You can also try this direct link: https://drive.google.com/file/d/1m8-EBPgi5MRubrm6iQjafK2QMHDBMSfJ/view?usp=sharing
- After downloading, please put this file into the
./celebasubolder and unzip it.
- Next, you need to download the annotation files and put them into the same
./celebasubfolder. The annotation files can be found underAnno:
In [44]python · cell 9
python
IPythonImage(filename='figures/gdrive-download-location-2.png', width=300)Output
<IPython.core.display.Image object>
- direct links are provided below:
In [45]python · cell 11
python
IPythonImage(filename='figures/gdrive-download-location-3.png', width=300)Output
<IPython.core.display.Image object>
- Lastly, you need to download the file
list_eval_partition.txtand place it under./celeba:
After completing steps 1-3 above, please ensure you have the following files in your ./celeba subfolder, and the files are non-empty (that is, they have similar file sizes as shown below):
In [46]python · cell 15
python
IPythonImage(filename='figures/celeba-files.png', width=400)Output
<IPython.core.display.Image object>
In [47]python · cell 17
python
image_path = './'
celeba_dataset = torchvision.datasets.CelebA(image_path, split='train', target_type='attr', download=False)
assert isinstance(celeba_dataset, torch.utils.data.Dataset)In [48]python · cell 18
python
example = next(iter(celeba_dataset))
print(example)Output
(<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=178x218 at 0x1332FD850>, tensor([0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1]))
In [49]python · cell 19
python
from itertools import islice
fig = plt.figure(figsize=(12, 8))
for i, (image, attributes) in islice(enumerate(celeba_dataset), 18):
ax = fig.add_subplot(3, 6, i+1)
ax.set_xticks([]); ax.set_yticks([])
ax.imshow(image)
ax.set_title(f'{attributes[31]}', size=15)
#plt.savefig('figures/12_05.pdf')
plt.show()Output
<Figure size 864x576 with 18 Axes>
[省略较大 image/png 输出]
