ModuleNotFoundError: No module named 'classification_models.resnet' - python

from classification_models.resnet import ResNet18, preprocess_input
model = ResNet18((224, 224, 3), weights='imagenet')
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-43-ff8b34ae99fa> in <module>()
----> 1 from classification_models.resnet import ResNet18, preprocess_input
2
3 model = ResNet18((224, 224, 3), weights='imagenet')
ModuleNotFoundError: No module named 'classification_models.resnet'
I am not able to import pre-trained ResNet18 model on google colab. Please help

You can access all ResNet(ResNet50, ResNet101, ResNet152) models and its preprocess_input() easily from tf.keras.applications API as below:
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
Please check this link for more details on how to use and apply ResNet model.

Related

module 'tensorflow' has no attribute 'random_normal'

i was running this step and it had error
from efficientnet import EfficientNetB0 as Net
from efficientnet import center_crop_and_resize, preprocess_input
# loading pretrained conv base model
conv_base = Net(weights='imagenet', include_top=False,
input_shape=input_shape)
error:
AttributeError Traceback (most recent call last)
<ipython-input-13-34d286b24b60> in <module>()
2 from efficientnet import center_crop_and_resize, preprocess_input
3 # loading pretrained conv base model
----> 4 conv_base = Net(weights='imagenet', include_top=False, input_shape=input_shape)
4 frames
/content/efficientnet_keras_transfer_learning/efficientnet/model.py in __call__(***failed resolving arguments***)
65 kernel_height, kernel_width, _, out_filters = shape
66 fan_out = int(kernel_height * kernel_width * out_filters)
---> 67 return tf.random_normal(
68 shape, mean=0.0, stddev=np.sqrt(2.0 / fan_out), dtype=dtype)
69
AttributeError: module 'tensorflow' has no attribute 'random_normal'
code ref:
https://colab.research.google.com/github/Tony607/efficientnet_keras_transfer_learning/blob/master/Keras_efficientnet_transfer_learning.ipynb
Thank you so much
Google Colab by default uses Tensorflow 2.x, where this function is called tensorflow.random.normal. The repository you link to seems to be using Tensorflow 1.x, where it was called tensorflow.random_normal. You can either:
clone the repository and update its code to run correctly on the newer version, or
instruct Google Colab to use the old Tensorflow using this instruction before import tensorflow:
%tensorflow_version 1.x

ImportError: cannot import name "backend"

Below the written code,
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from keras.layers import backend as k
batch_size = 128
num_classes = 10
epochs = 12
and below the issue,
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-d1183f2cea73> in <module>
8 from tensorflow.keras.layers import Conv2D
9 from tensorflow.keras.layers import MaxPooling2D
---> 10 from keras.layers import backend as k
11
12
ImportError: cannot import name 'backend'
Need help to solve this issue
Note: I am using Python 3.6.0.
You can use the keras backend lib
import keras.backend as k
or directelly from tensorflow
from tensorflow.keras import backend as k
Try uninstalling Tensor Flow and keras and removing their directories in site-packages then reinstalling them.

Tensorflow/Keras Type Error about compile

I import
from tensorflow.keras.layers import Activation, Dense, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras import optimizers
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
these and at this part,
model.compile(loss='binary_crossentropy', optimizer='adam'(lr=0.001), metrics=['acc'])
like this Type error was occurred
TypeError Traceback (most recent call last)
<ipython-input-82-d2bcad9a50e3> in <module>
----> 1 model.compile(loss='binary_crossentropy', optimizer='adam'(lr=0.001), metrics=['acc'])
TypeError: 'str' object is not callable
I found it on google, but I could not find a solution...
Does anyone know why this error is occurred?
Thank you for reading.
change it to this:
opt = optimizers.Adam(learning_rate=0.001)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])

ImportError: cannot import name 'AutoModelWithLMHead' from 'transformers'

This is literally all the code that I am trying to run:
from transformers import AutoModelWithLMHead, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
model = AutoModelWithLMHead.from_pretrained("microsoft/DialoGPT-small")
I am getting this error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-14-aad2e7a08a74> in <module>
----> 1 from transformers import AutoModelWithLMHead, AutoTokenizer
2 import torch
3
4 tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
5 model = AutoModelWithLMHead.from_pretrained("microsoft/DialoGPT-small")
ImportError: cannot import name 'AutoModelWithLMHead' from 'transformers' (c:\python38\lib\site-packages\transformers\__init__.py)
What do I do about it?
I solved it! Apperantly AutoModelWithLMHead is removed on my version.
Now you need to use AutoModelForCausalLM for causal language models, AutoModelForMaskedLM for masked language models and AutoModelForSeq2SeqLM for encoder-decoder models.
So in my case code looks like this:
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")

Is there a shorter way to import classes of a submodule?

Currently, I do this:
import tensorflow as tf
keras = tf.contrib.keras
Sequential = keras.models.Sequential
Dense = keras.layers.Dense
Dropout = keras.layers.Dropout
Flatten = keras.layers.Flatten
Conv2D = keras.layers.Conv2D
MaxPooling2D = keras.layers.MaxPooling2D
I would like to do something like this:
import tensorflow as tf
keras = tf.contrib.keras
from tf.contrib.keras import (Sequential, Dense, Dropout, Flatten,
Conv2D, MaxPooling2D)
but when I try this I get
ImportError: No module named tf.contrib.keras
Is there a shorter way than the first code block to import those classes?
Other tries
>>> from tensorflow.contrib.keras import (Sequential, Dense, Dropout, Flatten)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name Sequential
Nr 2
>>> import tensorflow
>>> from tensorflow.contrib.keras.models import Sequential
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named models
>>> tensorflow.contrib.keras.models.Sequential
<class 'tensorflow.contrib.keras.python.keras.models.Sequential'>
First, you need to import using the actual name of the tensorflow module, not the tf alias you used for it:
Second, the package structure of tensorflow.contrib.keras is kind of weird. The names
tensorflow.contrib.keras.models
tensorflow.contrib.keras.layers
are actually aliases for the more deeply nested modules
tensorflow.contrib.keras.api.keras.models
tensorflow.contrib.keras.api.keras.layers
and the way these aliases are created doesn't allow you to use from imports to import their contents. You can use the "real" names directly:
from tensorflow.contrib.keras.api.keras.models import Sequential
from tensorflow.contrib.keras.api.keras.layers import (
Dense, Dropout, Flatten, Conv2D, MaxPooling2D)
(There's even more aliasing here - the tensorflow.contrib.keras.api.keras.* modules pull most of their contents from tensorflow.contrib.keras.python.keras - but we don't need to worry about that.)
Try this:
from tensorflow.contrib.keras.models import Sequential
from tensorflow.contrib.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D

Categories