Importing conv_block from resnet50 - keras

I cant seem to import identity_block and conv_block from the resnet architecture.
https://github.com/keras-team/keras-applications/blob/master/keras_applications/resnet50.py
from keras.applications.resnet50 import ResNet50,decode_predictions,identity_block, conv_block
It"s resulting in an import error.
from keras.applications.resnet50 import ResNet50,decode_predictions,identity_block, conv_block
ImportError: cannot import name 'identity_block'

You can use the following code, it is inside resnet50.
from keras.applications.resnet50 import ResNet50,decode_predictions,resnet50
identity_block, conv_block = resnet50.identity_block, resnet50.conv_block

I got the following error
ImportError: cannot import name 'resnet50' from 'keras.applications.resnet50' (/home/mike/miniconda3/lib/python3.7/site-packages/keras/applications/resnet50.py)

The official Keras library has a big change and can not be called directly by the client script in the case you mentioned. I have met the same issue and solved it as follows.
1. Download keras.applications and put keras_applications into the current directory
It is called as a library by the client script.
2. Make an independent script of resnet50_custom.py
change the original import statment
from keras.applications.imagenet_utils import _obtain_input_shape
to the new statements as follows
from keras_applications.imagenet_utils import _obtain_input_shape
3. Add the following import statement into the client script.
from resnet50_custom import identity_block, conv_block
4. Change "include_top=include_top" to "require_flatten=include_top"
input_shape = _obtain_input_shape(input_shape,default_size=224, \
min_size=197, data_format=None, require_flatten=include_top)
Cheers.

Related

Ordinal Logistic Regression in python and Google Colab

I have been trying to import statmodels (https://www.statsmodels.org/devel/examples/notebooks/generated/ordinal_regression.html#Probit-ordinal-regression:) to google colab without success.
I tried pip install statmodels which worked but then when I tried to import the ordinal model following the code from the above website:
import numpy as np
import pandas as pd
import scipy.stats as stats
from statsmodels.miscmodels.ordinal_model import OrderedModel
The following error message came up:
ModuleNotFoundError: No module named 'statsmodels.miscmodels.ordinal_model'
I have tried to follow the instruction from https://www.statsmodels.org/devel/install.html but I am not sure what went wrong, please help, thank you so much
The module location has changed to from statsmodels.discrete.discrete_model import OrderedModel

Yolo-v3 object detection with python

I'm trying to detect objects using Yolo-v3 referring this tutorial. I have already installed darknet.
When I tried to run the following code:
from darknet import Darknet
it says:
ImportError: cannot import name 'Darknet' from 'darknet' (unknown location)
so I tried:
import darknet as dn
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
then it says:
"AttributeError: module 'darknet' has no attribute 'load_net'"
How I can avoid these errors?
Use it like this.
import cv2
import matplotlib.pyplot as plt
from utils import *
from darknet import Darknet
net = Darknet("cfg/tiny-yolo.cfg")
net.load_weights("tiny-yolo.weights")

module 'tensorflow' has no attribute 'merge_summary' error

Long story short I have this problem where when I run the code it says that the module I use Tensorflow has no attribute 'merge_summary' The thing is that I don't even use merge_summary.
I have tried to uninstall tenserflow and it didn't work.
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import numpy
import tflearn
import tensorflow
import json
import random
with open("intents.json") as file:
data = json.load(file)
print(data)
This should put a lot of text in the console.
You can also try tf.compat.v1.summary.merge_all as merge_summary is deprecated.
merge_summary is deprecated.
use instead :
tf.summary.merge

TensorFlow Object Detection API - error = "This call to matplotlib.use() has no effect because the backend has already been[...]"

I'm trying to get the TensorFlow Object Detection API on Windows. I use Python 3.6.5 (64bits).
After running the following program:
Here is the part of the code which generates the warning:
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
import cv2
cap = cv2.VideoCapture("video.mp4")
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
I have this warning message :
Warning (from warnings module):
File "C:\Users\leahj\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\utils\visualization_utils.py", line 25
import matplotlib; matplotlib.use('Agg') # pylint: disable=multiple-statements
UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
Can anybody help me please ?
The line import matplotlib; matplotlib.use('Agg') is hardcoded in object_detection\utils\visualization_utils. I don't know the reason for this; usually a package should allow the user to choose the backend.
Unless there is any other problem arising from the use of object_detection the easiest is probably to live with this warning.

ImportError: cannot import name '_obtain_input_shape' from keras

In Keras,
I'm trying to import _obtain_input_shape as follows:
from keras.applications.imagenet_utils import _obtain_input_shape
However, I get the following error:
ImportError: cannot import name '_obtain_input_shape'
The reason I'm trying to import _obtain_input_shape is so that I can determine the input shape(so as to load VGG-Face as follows :
I'm using it to determine the correct input shape of the input tensor as follow:
input_shape = _obtain_input_shape(input_shape,
default_size=224,
min_size=48,
data_format=K.image_data_format(),
require_flatten=include_top)`
Please assist?
Thanks in advance.
You don't have to downgrade Keras 2.2.2.
In Keras 2.2.2 there is no _obtain_input_shape method in the keras.applications.imagenet_utils module. You can find it under keras-applications with the modul name keras_applications (underscore).
So you don't have to downgrade your Keras to 2.2.0 just change:
from keras.applications.imagenet_utils import _obtain_input_shape
to
from keras_applications.imagenet_utils import _obtain_input_shape
I have found a method that works well. You just use
from keras_applications.imagenet_utils import _obtain_input_shape
Notice: It is keras_applications instead of keras.application.
This issue occured because of the version of keras.
In my case, I was downgrade keras 2.2.2 to 2.2.0, and the problem was solved.
In Colab I solved it by importing Keras and installing :
import keras
!pip install keras_applications
from keras_applications.imagenet_utils import _obtain_input_shape
keras_applications.imagenet_utils is deprecated
Traceback (most recent call last):
File "inception_v3.py", line 36, in
from keras_applications.imagenet_utils import _obtain_input_shape
ModuleNotFoundError: No module named 'keras_application
from keras.applications.imagenet_utils import obtain_input_shape
Not _obtain_input_shape. This works fine with keras==2.5.0rc0 (pip install keras==2.5.0rc0)
for keras 2.2.4:
Change the line like below to make it work.
from keras_applications.imagenet_utils import _obtain_input_shape
Note: It is importing from keras_applications and does not from keras.applications as before.

Resources