Problems setting up FastAi working on Colab - pytorch

I'm trying to work through a tutorial on CycleGANs using the Colab platform but I'm struggling to find a way through the 'simple' part of just importing libraries.
I'm just trying to import the following:
from fastai.conv_learner import *
from fastai.dataset import *
from cgan.options.train_options import *
from sklearn.model_selection import train_test_split
from cgan.options.train_options import TrainOptions
from cgan.data.data_loader import CreateDataLoader
from cgan.models.models import create_model
from cgan.util.visualizer import Visualizer
from google_images_download import google_images_download
I'm currently stuck with an error on the first line:
----> 7 class IntermediateLayerGetter(nn.ModuleDict):
8 """
9 Module wrapper that returns intermediate layers from a model
AttributeError: module 'torch.nn' has no attribute 'ModuleDict'
The code can be found online: https://colab.research.google.com/drive/1dMQWStzLfAHDTGuKaUNQn1aOBWeJw4FN

Did you check this issue it sorts partially the problem.
Afterthat I received and this error (Detected that PyTorch and torchvision were compiled with different CUDA versions. PyTorch has CUDA Version=9.0 and torchvision has CUDA Version=10.0. Please reinstall the torchvision that matches your PyTorch install.) and solve it with
pip install torch==1.0.1 -f https://download.pytorch.org/whl/cu100/stable
I hope it helps :)

Related

module 'tensorflow_core.compat.v2' has no attribute '__internal__'

I am using python 3.6.9 , keras 2.3.1, and (tf 2.0.0 or tf 2.1.0).
Got the compatibility versions from this table.
When I import keras I get this error.
Error:
AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'
Solution:
Install Libraries
!pip install tensorflow==2.1
!pip install keras==2.3.1
Import
from tensorflow import keras
I was able to replicate your issue with the below combination as shown below
!pip install tensorflow==2.1.0
!pip install keras==2.3.1
import keras
Output:
Using TensorFlow backend.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-88d96843a926> in <module>()
----> 1 import keras
8 frames
/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
47
48 LOCAL.ALL_OBJECTS = {}
---> 49 LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
50
51 # Compatibility aliases (need to exist in both V1 and V2).
AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'
Fixed code:
Here issue is due to incompatibility between TF2.1 and Keras 2.3.1. Your issue can be resolved in two ways
Upgrade to Keras2.5.0rc0 and import keras
import keras from tensorflow as from tensorflow import keras
I faced a similar error while trying to import Tokenizer from keras.preprocessing
This caused error -
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
This fixed it-
from tensorflow.keras import preprocessing
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

tensorflow.lite.python.convert.ConverterError:could not find toco_from_protos binary

in tensorflow 2.2,i train a mobileNetv1 model,and get a .h5 file, now i want to convert it to a .tflite file for a android app,but i get a error like title . here is my code:
model = load_model(h5path)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
litemodel = converter.convert()
open(litepath,"wb").write(litemodel)
here is my error:
tensorflow.lite.python.convert.ConverterError: Could not find toco_from_protos binary, make sure your virtualenv bin directory or pip local bin directory is in your path.
In particular, if you have installed TensorFlow with --user, make sure you add the install directory to your path.
For example:
Linux: export PATH=$PATH:~/.local/bin/
Mac: export PATH=$PATH:~/Library/Python/<version#>/bin
Alternative, use virtualenv.
please help me with my puzzle.
The server of Linux system.
It seems that your environment has not finished installing all requirements.
I would suggest if problem persists or at similar issues to check it also at Google Colab In this environment you can import Tensorflow and Keras libraries like:
import tensorflow as tf
import tensorflow.keras as K
import os
import numpy as np
import sys
import matplotlib.pyplot as plt
np.set_printoptions(threshold=sys.maxsize)
print(tf.__version__)
print(K.__version__)
And you are good to make your transformations. For your problem I would use code snippet below:
# WHOLE MODEL
tflite_model = tf.keras.models.load_model('my_model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(tflite_model)
tflite_save = converter.convert()
open("my_model.tflite", "wb").write(tflite_save)
Happy coding!

Pytorch/Ktrain import name BartForConditionalGeneration from transformers error?

I am trying to use the package ktrain's text function
text.TransformerSummarizer()
However, when I try to use it I get the error that
TransformerSummarizer() requires PyTorch to be installed.
I installed PyTorch using different variations of conda and pip, as well as the command given on the pytorch website. After it installs and I try to use it I get:
from ktrain import text
import torch
from transformers import BartForConditionalGeneration
ts = text.TransformerSummarizer()
ImportError: cannot import name 'BartForConditionalGeneration' from 'transformers' (C:\Users\user\AppData\Roaming\Python\Python37\site-packages\transformers\__init__.py)
Any suggestions of how to fix this? Thank you!

python3 keras import error with both tensorflow and theano

With python3 (version 3.6.8) and keras
the simple script:
import keras
gives an error:
Using TensorFlow backend.
Ungültiger Maschinenbefehl (Speicherabzug geschrieben)
(in english it would be something like: "invalid machine command (memory image written)")
So I tried to use theano instead:
import os
os.environ['KERAS_BACKEND'] = 'theano'
from keras import backend as K
With python3 it shows this output:
Using Theano backend.
Ungültiger Maschinenbefehl (Speicherabzug geschrieben)
How could I get further information about the problem?
Try
from tensorflow import keras
If the problem persists, try going through the documentation on how to install and how to use it.
Keras - Tensorflow
Keras Overview - Tensorflow
Keras.io

How to import WordEmbeddingSimilarityIndex function from gensim module?

When i try to import WordEmbeddingSimilarityIndex, it's giving me the following error:
>> from gensim.models import WordEmbeddingSimilarityIndex
ImportError: cannot import name 'WordEmbeddingSimilarityIndex
The same issue occurs for SparseTermSimilarityMatrix function:
>> from gensim.similarities import SparseTermSimilarityMatrix
ImportError: cannot import name 'SparseTermSimilarityMatrix
Note: I have installed and imported gensim, gensim.models and gensim.similarities. But still it's giving me the ImportError while importing the above mentioned functions.
Can you tell me what I am doing wrong, please?
Fix is change "models" to "similarities"
from gensim.similarities import WordEmbeddingSimilarityIndex
it works in gensim 4.0.1
Try to check the version of gensim that you are using. Usually, the older versions of gensim cause this issue.
from gensim.models import WordEmbeddingSimilarityIndex
print(gensim.__version__)
if the gensim version is 3.6.x or older update it to 3.7.x or latest version by running the below command. Once you update gensim version should get rid of this issue.
pip install --upgrade gensim

Resources