克隆的 github 存储库 url 导致 google colab 中的错误

分享于2022年07月17日 github google-colaboratory jupyter-notebook python 问答
【问题标题】:克隆的 github 存储库 url 导致 google colab 中的错误(Cloned github repository url lead to error in google colab)
【发布时间】:2022-07-13 03:42:05
【问题描述】:

我正在开发一个 google colab,可通过此链接获得: https://colab.research.google.com/github/lyricstopaintings/lyricstopaintings/blob/main/Lyrics%20inspired%20AI%20paintings.ipynb

在github上: https://github.com/lyricstopaintings/lyricstopaintings

我想将有关我项目的所有文件存储在我自己的 github 存储库中,因此我克隆了一些公共存储库,例如:

https://github.com/openai/CLIP

https://github.com/kostarion/guided-diffusion

以下脚本运行良好,

import pathlib, shutil, os, sys
useCPU = False #@param {type:"boolean"}

if not is_colab:
    # If running locally, there's a good chance your env will need this in order to not crash upon np.matmul() or similar operations.
    os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'

PROJECT_DIR = os.path.abspath(os.getcwd())
USE_ADABINS = True

if is_colab:
    if not google_drive:
        root_path = f'/content'
        model_path = '/content/models' 
else:
    root_path = os.getcwd()
    model_path = f'{root_path}/models'

multipip_res = subprocess.run(['pip', 'install', 'lpips', 'datetime', 'timm', 'ftfy', 'einops', 'pytorch-lightning', 'omegaconf'], stdout=subprocess.PIPE).stdout.decode('utf-8')
print(multipip_res)

if is_colab:
    subprocess.run(['apt', 'install', 'imagemagick'], stdout=subprocess.PIPE).stdout.decode('utf-8')

try:
    from CLIP import clip
except:
    if not os.path.exists("CLIP"):
        gitclone("https://github.com/openai/CLIP")
    sys.path.append(f'{PROJECT_DIR}/CLIP')

try:
    from guided_diffusion.script_util import create_model_and_diffusion
except:
    if not os.path.exists("guided-diffusion"):
        gitclone("https://github.com/kostarion/guided-diffusion")
    sys.path.append(f'{PROJECT_DIR}/guided-diffusion')

**... other modules and packages in the same structure**

import torch
from dataclasses import dataclass
from functools import partial
import cv2
import pandas as pd
import gc
import io
import math
import timm
from IPython import display
import lpips
from PIL import Image, ImageOps
import requests
from glob import glob
import json
from types import SimpleNamespace
from torch import nn
from torch.nn import functional as F
import torchvision.transforms as T
import torchvision.transforms.functional as TF
from tqdm.notebook import tqdm
from CLIP import clip
from resize_right import resize
from guided_diffusion.script_util import create_model_and_diffusion, model_and_diffusion_defaults
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import random
from ipywidgets import Output
import hashlib
from functools import partial
if is_colab:
    os.chdir('/content')
    from google.colab import files
else:
    os.chdir(f'{PROJECT_DIR}')
from IPython.display import Image as ipyimg
from numpy import asarray
from einops import rearrange, repeat
import torch, torchvision
import time
from omegaconf import OmegaConf
import warnings
warnings.filterwarnings("ignore", category=UserWarning)

但是当我更改链接时

https://github.com/openai/CLIP --> https://github.com/lyricstopaintings/lyricstopaintings/tree/main/CLIP

https://github.com/kostarion/guided-diffusion --> https://github.com/lyricstopaintings/lyricstopaintings/tree/main/guided-diffusion

出现以下错误:

ModuleNotFoundError                       Traceback (most recent call last)

 in ()
    107 import torchvision.transforms.functional as TF
    108 from tqdm.notebook import tqdm
--> 109 from CLIP import clip
    110 from resize_right import resize
    111 from guided_diffusion.script_util import create_model_and_diffusion, model_and_diffusion_defaults

ModuleNotFoundError: No module named 'CLIP'

我的方法有什么问题?我是这个领域的新手,如果这是一些基本的事情,很抱歉。


【解决方案1】:

Github 仓库应该参考如下

https://github.com/lyricstopaintings/lyricstopaintings

但这包含克隆存储库的文件夹,因此 path 需要包含该文件夹,如下所示。

sys.path.append(f'{PROJECT_DIR}/lyricstopaintings/CLIP')

我无法让 from CLIP import clip 工作,所以我将其更改为 import clip

我将代码缩减为一个最小的示例,因为我找不到一些函数,例如 subprocess.run gitclone ,所以我将它们替换为其他函数。

import pathlib, shutil, os, sys
PROJECT_DIR = os.path.abspath(os.getcwd())

!pip install -q ftfy

try:
    import clip
except:
    if not os.path.exists("CLIP"):
        !git clone -q "https://github.com/lyricstopaintings/lyricstopaintings"
        sys.path.append(f'{PROJECT_DIR}/lyricstopaintings/CLIP')

import clip
clip.available_models()

#
['RN50',
 'RN101',
 'RN50x4',
 'RN50x16',
 'RN50x64',
 'ViT-B/32',
 'ViT-B/16',
 'ViT-L/14',
 'ViT-L/14@336px']