Cloned github repository url lead to error in google colab - python

I'm working on a google colab available at this link: https://colab.research.google.com/github/lyricstopaintings/lyricstopaintings/blob/main/Lyrics%20inspired%20AI%20paintings.ipynb
On github: https://github.com/lyricstopaintings/lyricstopaintings
I would like to store all the files regarding my project on my own github repository, so I cloned a few public repositories for example:
https://github.com/openai/CLIP
https://github.com/kostarion/guided-diffusion
The following script works fine,
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)
but when I change the links
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
got the following error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-b4c7e2b3bdd9> in <module>()
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'
What is wrong with my approach? I'm new on this field, so sorry if this is some basic thing.

The Github repository should be referred to as follows
https://github.com/lyricstopaintings/lyricstopaintings
But this contains folders for the cloned repositories so the path needs to include the folder as follows.
sys.path.append(f'{PROJECT_DIR}/lyricstopaintings/CLIP')
I couldn't get from CLIP import clip working so I changed it to import clip.
I cut down the code to be a minimal example because I couldn't find some of the functions such as subprocess.run and gitclone so I replaced these with other functions.
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']

Related

Googel Colab: Can't import py or ipynb file from Google Drive, error msg: "Input ludb.py could not be resolved"

I am writing some code for a project in Google Colab which requires importing various functions and classes from other .py files. So I uploaded them into Google Drive, mounted my drive, changed the working directory and tried to import but I am unable to. When I try to import a file named "ludb_dat", I get a massage saying "Import "ludb_dat" could not be resolved(reportMissingImports)".
My Code:
from google.colab import drive
drive.mount('/content/drive/')
import os
root_dir = "/content/drive/My Drive/"
project_folder = "Colab Notebooks/Bio Signals Project/My Code"
os.chdir(root_dir + project_folder)
os.getcwd()
import numpy as np
from scipy.signal import medfilt
import scipy.stats
import pywt
import operator
from ludb_dat import * #this is the line that gives the error
I have tried some alternate methods such as:
!cp /content/drive/MyDrive/Colab Notebooks/Bio Signals Project/My Code/ludb_dat.py .
And
import sys
sys.path.append('/content/drive/MyDrive/Colab Notebooks/Bio Signals Project/My Code/')
from ludb_dat import *
But they all give the same result, a yellow wavy line under "ludb_dat" and the message: "Import "ludb_dat" could not be resolved(reportMissingImports)". I have tried saving ludb_dat, the file to be imported as both a .py file and as an .ipynb file, but both have the same problem.
I do not know how to fix this. Any help would be greatly appreciated.

get_file(fname, origin=_URL, extract=True) not extracting the file

My code:
import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
from tensorflow.keras.preprocessing import image_dataset_from_directory
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
dl_fname = '/Volumes/D/PythonCode/tf_transfer_learning/cats_and_dogs.zip'
path_to_zip = tf.keras.utils.get_file(dl_fname, origin=_URL, extract=True)
I can see cats_and_dogs.zip being downloaded, however, it is not extracted/unzipped. I am on MacOS, using PyCharm.
I am not sure why not. Anyone has a pointer? Thanks.
The URL is https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip but you saved it as cats_and_dogs.zip. When you extract it, you get a directory cats_and_dogs_filtered, no matter what the name of the zip file is.
Though it does seem that the extracted file is in ~/.keras/datasets/cats_and_dogs_filtered.zip even if one uses an absolute path.
import tensorflow as tf
url = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
dl_path = "/tmp/cats_and_dogs_filtered.zip"
path_to_zip = tf.keras.utils.get_file(dl_path, origin=url, extract=True)
The zip file is saved to the absolute path, but the extracted path goes to ~/.keras/datasets/cats_and_dogs_filtered/.
print(path_to_zip)
# /tmp/cats_and_dogs_filtered.zip

How to run the code using two OpenCV versions python at the same time?

Two versions are installed on my computer, OpenCV 3.3.1 is working with ROS library (kinetic) and OpenCV 4.2 has some advantage compared to 3.3.1. So, I'd like to use them both.
simple code:
import cv2
print (cv2.__version__)
import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if ros_path2 and ros_path3 in sys.path:
sys.path.remove(ros_path2)
sys.path.remove(ros_path3)
import cv2
print (cv2.__version__)
The output is:
3.3.1
3.3.1
if I comment out two first lines
# import cv2
# print (cv2.__version__)
import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if ros_path2 and ros_path3 in sys.path:
sys.path.remove(ros_path2)
sys.path.remove(ros_path3)
import cv2
print (cv2.__version__)
The output becames:
4.2.0
My goal from this action is to use some advantage OpenCV 4.2 for example: from cv2.xfeatures2d import matchGMS
More explanations:
if I added last line as:
import cv2
print (cv2.__version__)
import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if ros_path2 and ros_path3 in sys.path:
sys.path.remove(ros_path2)
sys.path.remove(ros_path3)
import cv2
from cv2.xfeatures2d import matchGMS
print (cv2.__version__)
The output is:
3.3.1
Traceback (most recent call last):
File "/home/redhwan/learn2.py", line 11, in <module>
from cv2.xfeatures2d import matchGMS
ImportError: cannot import name matchGMS
if I comment out two first lines again:
# import cv2
# print (cv2.__version__)
import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if ros_path2 and ros_path3 in sys.path:
sys.path.remove(ros_path2)
sys.path.remove(ros_path3)
import cv2
from cv2.xfeatures2d import matchGMS
print (cv2.__version__)
It is working fine, the output is:
4.2.0
Sorry, for long explanation
Any help,thank you in advance.
I don't think you can use them both because It was not designed it to be that way. I am sorry If I couldn't answer as you might have expected. I hope you work your way around this to avoid this kind of problem. :)

Nested import between ipynb files doesn't import properly

I am using import_ipynb module to import one jupyter notebook inside another.
txt2csv.ipynb has the following import statements
import pandas as pd
import os
import import_ipynb
import config as cfg
I am using another file to import txt2csv.ipynb which has following import statements
data_processing.ipynb
import glob
import numpy as np
import datetime
import import_ipynb
import txt2csv
My issue is that when I try to use a statement like
data_in = glob.glob(os.path.join(cfg.data_dir, "*.csv"))
inside data_processing.ipynb it throws the error
NameError Traceback (most recent call last)
<ipython-input-2-683b1d3df82f> in <module>
----> 1 data_in = glob.glob(os.path.join(cfg.data_dir, "*.csv"))
NameError: name 'os' is not defined
Are nested imports not possible in python, will I have to explicitly write import os again in data_processing.ipynb file?

importing the right module but getting an error in python

import sys
from subprocess import run, PIPE
import shlex
from src.detector_main import detect_main
def main():
# print command line arguments
for arg in sys.argv[1:]:
print(arg)
if __name__ == "__main__":
# main()
print(sys.argv)
This is my main module. If you see the from src.detector_main import detect_main, it is supposed to import detect_main from src/detector_main.py.
In my detector_main.py, I have a bunch of imports,
import ast
import os
import fpdf
import sys
from Detector.class_coupling_detector import detect_class_cohesion
from Detector.cyclomatic_complexity_detector import detect_cyclomatic_complexity
from Detector.long_lambda_detector import detect_long_lambda
from Detector.long_list_comp_detector import detect_long_list_comp
from Detector.pylint_output_detector import detect_pylint_output
from Detector.shotgun_surgery_detector import detect_shotgun_surgery
from Detector.useless_exception_detector import detect_useless_exception
# from tools.viz_generator import add_viz
def detect_main(directory):
# Get stats for files in directory
stats_dict = get_stats(directory)
....
Running my main module gives me this error:
File "pyscent.py", line 5, in <module>
from src.detector_main import detect_main
File "C:\Users\user\Desktop\proj\src\detector_main.py", line 5, in <module>
from Detector.class_coupling_detector import detect_class_cohesion
ModuleNotFoundError: No module named 'Detector'
I don't get this because I am following the exact path.
I don't get this because I am following the right path.
In your example you import the Detector.class_coupling_detector module in file that is in the same directory as Detector but your cwd is not the src directory.
Due to this you should either use absolute import from src.Detector... or relative import from .Detector...
Here is some information about difference between this two ways to import

Categories