Path: blob/master/notebooks/misc/clip_imagenette_make_dataset_pytorch.ipynb
1192 views
This notebook shows the pipeline to make clip extracted data from Imagenette dataset and stores the data in the form torch.Tensor in a .pt file
To check NVIDIA_GPU devices are available
Import and Installations
Required installations for CLIP:
To know about CLIP, you may refer to github repo CLIP from openai
Requirement already satisfied: ftfy in /usr/local/lib/python3.7/dist-packages (6.0.3)
Requirement already satisfied: regex in /usr/local/lib/python3.7/dist-packages (2019.12.20)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (4.41.1)
Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from ftfy) (0.2.5)
Collecting git+https://github.com/openai/CLIP.git
Cloning https://github.com/openai/CLIP.git to /tmp/pip-req-build-36z5k9c8
Running command git clone -q https://github.com/openai/CLIP.git /tmp/pip-req-build-36z5k9c8
Requirement already satisfied (use --upgrade to upgrade): clip==1.0 from git+https://github.com/openai/CLIP.git in /usr/local/lib/python3.7/dist-packages
Requirement already satisfied: ftfy in /usr/local/lib/python3.7/dist-packages (from clip==1.0) (6.0.3)
Requirement already satisfied: regex in /usr/local/lib/python3.7/dist-packages (from clip==1.0) (2019.12.20)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from clip==1.0) (4.41.1)
Requirement already satisfied: torch~=1.7.1 in /usr/local/lib/python3.7/dist-packages (from clip==1.0) (1.7.1)
Requirement already satisfied: torchvision~=0.8.2 in /usr/local/lib/python3.7/dist-packages (from clip==1.0) (0.8.2)
Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from ftfy->clip==1.0) (0.2.5)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch~=1.7.1->clip==1.0) (3.7.4.3)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from torch~=1.7.1->clip==1.0) (1.19.5)
Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.7/dist-packages (from torchvision~=0.8.2->clip==1.0) (7.1.2)
Building wheels for collected packages: clip
Building wheel for clip (setup.py) ... done
Created wheel for clip: filename=clip-1.0-cp37-none-any.whl size=1368722 sha256=caab03eea332fc55f11ab1751b2e1e061657a6cd221d80287205086d2cb38d24
Stored in directory: /tmp/pip-ephem-wheel-cache-8jlmnfyd/wheels/79/51/d7/69f91d37121befe21d9c52332e04f592e17d1cabc7319b3e09
Successfully built clip
Loading the CLIP model
This uses "ViT-B/32" and convertes the data into 512 sized tensors
The CLIP model is jitted and is run on GPU
Downloading Imagenette DS
(run this cell twice, if you are facing an issue while downloading..)
Actual class names for the imagenette v2 dataset
Visualizing first N images
Cardinality of the Datasets
Making the CLIP features
Dataset class
which transfroms the data into PIL images which then preprocessed by CLIP's image preprocessor "preprocess_clip"
Dataloaders
To make batches which are then encoded by the clip model.
Data should not be shuffled as we are just extracting features (making inference) but not for training.
Note: Multiple workers are not used in these dataloaders, as they seem to freeze sometimes
(but you are free to try by setting num_workers=no.of.cpus
in DataLoader
, if you are interested check this issue pytorch-#15808 )
Encoding the Image data
For Training data
Saving the clip_data
You can download the compressed files for future use
To see demo of the working of these extracted feautures, you can check out the MLP and logreg examples on clip extracted imagenette data using Sklearn, Flax(+Jax), Pytorch-lightning! in the pyprobml repo