2024-03-16 01:41:27
+{
+ "cells": [
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"# Setupn",
+"%pip install git
+https://github.com/apple/ml
-stable
-diffusionn",
+"!git clone https://github.com/apple/ml
-stable
-diffusion.git"
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"repo_id = "apple/coreml
-stable
-diffusion
-v1
-4"n",
+"variant = "split_einsum/packages"n",
+"model_path = "models/coreml
-stable
-diffusion
-v1
-4_split_einsum_packages"n",
+"prompt = "A very happy cat smiling at the camera""
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"# Download the modeln",
+"# I am using the torch2coreml script to convert the model to CoreML. n",
+"# Normally I want to use this in the notebook directly, but I can't get it to work.n",
+"#n",
+"# You might need to "huggingface
-cli login" firstn",
+"!python
-m pip install
-r requirements.txtn",
+"!python
-m python_coreml_stable_diffusion.torch2coreml
-convert
-unet
-convert
-text
-encoder
-convert
-vae
-decoder
-convert
-safety
-checker
-model
-version {model_id}
-o {model_path}n",
+"n"
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"from diffusers import StableDiffusionPipelinen",
+"from python_coreml_stable_diffusion.pipeline import get_coreml_pipen",
+"import coremltoolsn",
+"import numpy as npn",
+"n",
+"model_id = "CompVis/stable
-diffusion
-v1
-4"n",
+"pytorch_pipe = StableDiffusionPipeline.from_pretrained(model_id)"
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"coreml_pipe = get_coreml_pipe(pytorch_pipe=pytorch_pipe,n",
+"mlpackages_dir=model_path,n",
+"model_version=model_id,n",
+"compute_unit="ALL")"
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"from tqdm import tqdmn",
+"n",
+"# Set the seed for reproducibility n",
+"np.random.seed(8675309)n",
+"n",
+"images = []n",
+"init_guidance_scale = 1.05n",
+"init_noise_scale = 0.01n",
+"for i in tqdm(range(64)):n",
+"# Disregard the safety checker for nown",
+"coreml_pipe.safety_checker = Nonen",
+"n",
+"# Turn off progress barsn",
+"coreml_pipe.progress = Falsen",
+"n",
+"n",
+"image = coreml_pipe(prompt=prompt,n",
+"num_inference_steps=25,n",
+"guidance_scale=init_guidance_scale,n",
+"noise_scale=init_noise_scale,)n",
+"n",
+"image_data = {n",
+""image": image,n",
+""guidance_scale": init_guidance_scale,n",
+""noise_scale": init_noise_scalen",
+"}n",
+"images.append(image_data)n",
+"n",
+"# Show the imagen",
+"image.images[0].show()n",
+"n",
+"init_guidance_scale *= 1.05n",
+"init_noise_scale *= 1.05n",
+"n",
+""
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"# Show images in a gridn",
+"import matplotlib.pyplot as pltn",
+"import numpy as npn",
+"n",
+"fig, ax = plt.subplots(8, 8, figsize=(64, 64))n",
+"n",
+"# Title the plot with the model name and the promptn",
+"fig.suptitle(f"Model: {model_id}nPrompt: {prompt}", fontsize=48)n",
+"n",
+"# Less space between imagesn",
+"# plt.subplots_adjust(wspace=0.1, hspace=0.1)n",
+"n",
+"# Other common plt tweaksn",
+"# plt.setp(ax, xticks=[], yticks=[], frame_on=False)n",
+"# plt.tight_layout()n",
+"# plt.margins(0, 0)n",
+"n",
+"for i in range(64):n",
+"row = i // 8n",
+"col = i % 8n",
+"ax[row, col].imshow(images[i]["image"][0][0])n",
+"ax[row, col].set_title(f"Guidance: {images[i]['guidance_scale']:.2f}nNoise: {images[i]['noise_scale']:.2f}", fontsize=24)n",
+"ax[row, col].axis("off")n",
+"n",
+"# Save the plotn",
+"plt.savefig("images.png")"
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+"images[0]["image"][0][0]"
+ ]
+},
+{
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+}
+ ],
+ "metadata": {
+"kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+},
+"language_info": {
+ "codemirror_mode": {
+"name": "ipython",
+"version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x
-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.11.5"
+}
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
Read more...