Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/misc/celeba_viz.ipynb
1192 views
Kernel: Python 3

Open In Colab

Visualize CelebA

Here we download a zipfile of images and their attributes that have been preprocessed to 64x64 using the script at https://github.com/probml/pyprobml/blob/master/scripts/celeba_kaggle_preprocess.py

# Standard Python libraries from __future__ import absolute_import, division, print_function, unicode_literals import os import time import numpy as np import glob import matplotlib.pyplot as plt import PIL import imageio from IPython import display import sklearn # from time import time np.random.seed(0)
# N can be 200, 20000, or 40000 N = 20000 H = 64 W = 64 C = 3 input_shape = [H, W, 3] name = "celeba_small_H{}_W{}_N{}".format(H, W, N) csv_name = "{}.csv".format(name) zip_name = "{}.zip".format(name)
!rm {csv_name} !wget https://raw.githubusercontent.com/probml/pyprobml/master/data/CelebA/{csv_name}
rm: cannot remove 'celeba_small_H64_W64_N20000.csv': No such file or directory --2020-12-10 06:22:02-- https://raw.githubusercontent.com/probml/pyprobml/master/data/CelebA/celeba_small_H64_W64_N20000.csv Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 2440084 (2.3M) [text/plain] Saving to: ‘celeba_small_H64_W64_N20000.csv’ celeba_small_H64_W6 100%[===================>] 2.33M --.-KB/s in 0.1s 2020-12-10 06:22:03 (16.2 MB/s) - ‘celeba_small_H64_W64_N20000.csv’ saved [2440084/2440084]
import pandas as pd df = pd.read_csv(csv_name) df.head()
!rm {zip_name} !wget https://raw.githubusercontent.com/probml/pyprobml/master/data/CelebA/{zip_name}
rm: cannot remove 'celeba_small_H64_W64_N20000.zip': No such file or directory --2020-12-10 06:22:20-- https://raw.githubusercontent.com/probml/pyprobml/master/data/CelebA/celeba_small_H64_W64_N20000.zip Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 34148268 (33M) [application/zip] Saving to: ‘celeba_small_H64_W64_N20000.zip’ celeba_small_H64_W6 100%[===================>] 32.57M 58.6MB/s in 0.6s 2020-12-10 06:22:24 (58.6 MB/s) - ‘celeba_small_H64_W64_N20000.zip’ saved [34148268/34148268]
!rm *.jpg !ls
rm: cannot remove '*.jpg': No such file or directory celeba_small_H64_W64_N20000.csv celeba_small_H64_W64_N20000.zip sample_data
!unzip -qq {zip_name}
from glob import glob filenames = glob("*.jpg") # print(filenames) # should match df['image_id'] print(len(filenames))
20000
from matplotlib.image import imread images_celeba = np.zeros((N, H, W, C), dtype=np.float32) # pre-allocate memory for i in range(N): filename = df.iloc[i]["image_id"] img = imread(filename) # numpy array of uint8 images_celeba[i, :, :, :] = img / 255 # float in 0..1
fig, axs = plt.subplots(2, 4, figsize=(15, 10)) axs = np.reshape(axs, 8) for i in range(8): ax = axs[i] ax.imshow(images_celeba[i, :, :, :]) ax.axis("off") plt.tight_layout() plt.show()
Image in a Jupyter notebook