keras-team / keras-applications Goto Github PK
View Code? Open in Web Editor NEWReference implementations of popular deep learning models.
License: Other
Reference implementations of popular deep learning models.
License: Other
In the API of MobileNetV2, there's a parameter depth_multiplier
but it seems useless, which is confusing.
After this commit 5f99f52 I think pretrained weights have to be changed too.
Version 1.0.5 was uploaded to PyPI on August 22, but that release never got tagged here.
Shape of Each Extracted Feature (1, 7, 7, 2048)
preprocessing input for rnn
Traceback (most recent call last):
File "main.py", line 88, in
for i in extracted_features_train]
File "main.py", line 88, in
for i in extracted_features_train]
File "/home/anu/Desktop/btp/implementation/rnn_backend.py", line 42, in preprocess_input_rnn
tensor = tensor.reshape((n_time_steps, n_inputs))
ValueError: cannot reshape array of size 100352 into shape (32,64)
I am using pretrained ResNet50 Model for Feature Extraction.
Earlier the Shape of the Feature Vector which I was getting from the Last -1 layer was of shape (1,1,1,2048).
But Now It seems that its shape is changed to (1,7,7,2048) .
Hi, i got a question . I can not find solution. What is the difference between DepthwiseConv2D and Separable Conv2D ?
The model download speed is not optimized if just download from github, is there some way to make the download from my own mirror entrypoint?
Is it possible to share the code used to generate the data on the README? Was each model trained using the suggested hyperparameters in each paper or all training used the same set of hyperparameters (e.g. batch size, optimizer, lr)?
Thanks in advance
In the tables of the original paper (and in other implementations official tensorflow, torchvision, keras-resnet, tensorpack) the output shape after the initial 3x3 max pooling is 56x56
but in this ResNet50 implementation the output shape is 55x55
.
I see that the first convolutional layer in densenet is followed by a BN and RELU layers but the original paper says all convolutional layers follow BN-RELU-CONV order. @taehoonlee's original implementation also follows BN-RELU-CONV.
I am using the NASNetLarge/NASNetMobile and the MobileNetV2 on Keras 2.2.2. I am testing with all three backends and I see the differences between TensorFlow and Theano/CNTK.
I reported that here
At first, I thought it was because of the Depthwise and Separately Conv2D that was implemented for CNTK and Theano in their backend files. But later I found out that it seems to #be because of padding SAME inconsistency between TensorFlow and the other two backends.
I have seen fixes in the Mobile and ResNet50 that simply add ZeroPadding and use padding VALID. #9473?
Can someone confirm that we can do the same for these networks? Or should we actually implement the padding scheme SAME of TensorFlow on Theano and CNTK to be consistent?
I have been trying to test the pre-trained weight for InceptionResNetV2 on CNTK and Tensorflow backends on the ImageNet validation images with a single crop. And Tensorflow and CNTK do not agree with each other while Tensorflow and Theano do produce exactly the same output (It is mentioned on the application website that this model is compatible with all three backends). I attached the demo code that will recreate the problem on some selected images from the dataset (This is on Keras 2.1.6, Tensorflow 1.8.0, CNTK 2.5.1, Theano 1.0.1):
I have also tested DenseNet (121, 169, 201), all variations output different prediction (on some images) between CNTK and Tensorflow/Theano. One interesting thing to note is that VGG16 and VGG19 are claimed to be compatible with Tensorflow and Theano only actually work with CNTK also (produced the same outputs as the other two).
There is another thing to note is that the overall accuracy of the model on the single cropped validation ImageNet 2012 dataset is only around 80-82% top-5. The Tensorflow pre-trained weight report accuracy in the 95% region.
Attached are the differences in predicted labels (Using pre-trained InceptionResNetV2 and between Tensorflow and CNTK):
I'm doing image classification using inceptionv3 model. When i try to import pretrained model weights i get error as below. Can anyone please help me fix it
x = GlobalAveragePooling2D()(x)
File "/home/isemes/anaconda3/envs/tensorflow-venv/lib/python3.6/site-packages/keras/engine/base_layer.py", line 414, in __call__
self.assert_input_compatibility(inputs)
File "/home/isemes/anaconda3/envs/tensorflow-venv/lib/python3.6/site-packages/keras/engine/base_layer.py", line 311, in assert_input_compatibility
str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer global_average_pooling2d_1: expected ndim=4, found ndim=2
Hi guys,
OS: Windows and Linux
Tensorflow version: 1.12.0
Keras version: 2.1.6-tf
Devices: CPU and GPU
I am using keras.applications.inception_v3.InceptionV3 as a feature extractor and I am getting different feature vectors in each session. Seems related to random variable initialization, because setting tf.random_seed(1234) solves the problem. I still cannot pinpoint the exact variables that are initialized randomly instead of being assigned the saved weights.
Using inception v3 from tensorflow hub works as expected (same value in every session).
I have illustrated the problem in this notebook.
Update:
I'm trying to build a VGG16 model (without using the pre-trained one), but 25 epochs aren't enough because I get overfitting. How many epochs are enough? I have to classify two object
sgd = optimizers.SGD(lr=0.0001, decay=1e-6, momentum=0.9)
model.compile(loss='categorical_crossentropy', # Better loss function for neural networks
optimizer=sgd, # Adam optimizer with 1.0e-4 learning rate
metrics = ['accuracy']) # Metrics to be evaluated by the model
model_details=model.fit_generator(train_batches,steps_per_epoch=3,validation_data=valid_batches,validation_steps=3, epochs=25,verbose=2)
I have to preprocess images that have multi label.
In particular I have to project a neural network that not only classifies two different shape, triangle or circle, but also get their dimensions: the side, the radius, the position etc.. It's seems with no sense, but for my application it has. Where can I find a similar example?
In the official DenseNet implementations (caffe, torch, pytorch) and others (mxnet, torchvision) a Relu activation is applied after the last batch norm and before the global pooling, however this activation does not exist in this keras implementation.
Keras version: 2.2.4
Backend: Tensorflow
I am loading Resnet50 model pre-trained on imagenet and getting a prediction for the 'nematode' class for different images. Using the same methods with InceptionV3 return different (correct) results for different images.
Resnet50 weights are loaded from resnet50_weights_tf_dim_ordering_tf_kernels.h5
Helper functions I defined and used:
import os
import requests
import random
import string
from IPython.display import Image, display
from IPython.core.display import HTML
import keras
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions
## Helper methods for downloading an image from a url
def download_file(url, filename):
response = requests.get(url)
response.raise_for_status()
with open(filename, 'wb') as f:
f.write(response.content)
def gen_random_str(k=16):
rand_array = [random.choice(string.ascii_letters + string.digits) for i in range(k)]
return ''.join(rand_array)
def download_url_to_random_filename(url):
tmp_filename = os.path.join("/tmp", "%s.jpg" % gen_random_str(k=16))
download_file(url, tmp_filename)
return tmp_filename
# Prediction methods for local/remote files
def pred_image(model, image_path):
img = image.img_to_array(image.load_img(image_path, target_size=(224, 224))) / 255.
res = model.predict(img.reshape((1,) + img.shape))
return decode_predictions(res)
def pred_image_from_url(model, url):
tmp_filename = download_url_to_random_filename(url)
res = pred_image(model, tmp_filename)
os.remove(tmp_filename)
return res
# Load resnet50 and InceptionV3 models
resnet_model = keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet')
inception_model = keras.applications.inception_v3.InceptionV3(weights='imagenet')
Chose two random images of a cat and a dog:
from IPython.display import Image, display
from IPython.core.display import HTML
cat_url = "https://upload.wikimedia.org/wikipedia/commons/c/c0/Ragdoll_Blue_Colourpoint.jpg"
dog_url = "https://www.purina.co.nz/wp-content/uploads/2013/10/ChineseSharPei_body.jpeg"
display(Image(url=cat_url, width=200, height=200))
display(Image(url=dog_url, width=200, height=200))
And queried them against the model:
pred_image_from_url(resnet_model, cat_url)
pred_image_from_url(resnet_model, dog_url)
pred_image_from_url(inception_model, cat_url)
pred_image_from_url(inception_model, dog_url)
I tried with other images, the Resnet50 model predicts the 'nematode' class for them as well...
In file inception_v3.py, the dimension of layer mixed2
is supposed to be 35 x 35 x 256.
# mixed 2: 35 x 35 x 256
branch1x1 = conv2d_bn(x, 64, 1, 1)
branch5x5 = conv2d_bn(x, 48, 1, 1)
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)
branch3x3dbl = conv2d_bn(x, 64, 1, 1)
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
branch_pool = layers.AveragePooling2D((3, 3),
strides=(1, 1),
padding='same')(x)
branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
x = layers.concatenate(
[branch1x1, branch5x5, branch3x3dbl, branch_pool],
axis=channel_axis,
name='mixed2')
However, when I load a pre-trained Inception v3 model, the dimension becomes (?, ?, ?, 288)
.
from keras.applications.inception_v3 import InceptionV3
base_model = InceptionV3(weights='imagenet', include_top=True)
base_model.get_layer("mixed2").output
# <tf.Tensor 'mixed2/concat:0' shape=(?, ?, ?, 288) dtype=float32>
All other concatenation layers match the dimensions noted in inception_v3.py except mixed2
and mixed1
. I am not sure what I am missing here?
Steps to replicate: (tried on MacOS Mojave and Windows 10)
$conda install tensorflow-gpu keras
The following packages will be UPDATED:
keras: 2.2.2-0 --> 2.2.4-0
keras-applications: 1.0.4-py36_1 --> 1.0.6-py36_0
keras-base: 2.2.2-py36_0 --> 2.2.4-py36_0
keras-preprocessing: 1.0.2-py36_1 --> 1.0.5-py36_0
tensorboard: 1.10.0-py36he025d50_0 --> 1.12.2-py36h33f27b4_0
tensorflow: 1.10.0-gpu_py36h3514669_0 --> 1.12.0-gpu_py36ha5f9131_0
tensorflow-base: 1.10.0-gpu_py36h6e53903_0 --> 1.12.0-gpu_py36h6e53903_0
tensorflow-gpu: 1.10.0-hf154084_0 --> 1.12.0-h0d30ee6_0
Proceed ([y]/n)? y
conda create -n testenv tensorflow-gpu keras
works too
$python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>> from keras_applications.xception import Xception
>>> Xception()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\USER\AppData\Local\conda\conda\envs\yitao\lib\site-packages\keras_applications\xception.py", line 107, in Xception
if backend.image_data_format() != 'channels_last':
AttributeError: 'NoneType' object has no attribute 'image_data_format'
Creating a ResNeXt (keras applications 1.0.7) and Inception v3 model also gives the same error. I also tried installing keras and keras-applications using pip but same error again. Downgrading to keras 2.2.2 and everything will work properly.
Update: I found that this works:
import keras
from keras import applications
model = applications.xception.Xception()
However I'm not sure if this is the intended usage as the docs seem to be using the previous style of from keras.applications.inception_v3 import InceptionV3
Since Keras 2.2.0, integration tests have been failed (see test codes and CI results). The tf.keras.engine
does not exist even in the latest tensorflow==1.9.0rc0
. @fchollet, Don't we have no choice but to wait for tensorflow==1.9.0
?
Instantiated VGG16/19 model with the argument 'include_top'=False
gives constant output shape (1, 1, 512) Although I provided 'classes'=10
along with them.
import keras
from keras.applications import vgg16
# Use (-1, 32, 32, 3) CIFAR-10 images as input dataset, for example.
(train_x, train_y), (test_x, test_y) =\
keras.datasets.cifar10.load_data()
vgg = vgg16.VGG16(include_top=False,
weights=None,
input_shape=(32, 32, 3),
classes=10)
vgg.summary()
I Expected (None, 1, 1, 10) as an output shape, but the model gives: (None, 1, 1, 512)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 32, 32, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 32, 32, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 32, 32, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 16, 16, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 16, 16, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 16, 16, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 8, 8, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 8, 8, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 8, 8, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 8, 8, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 4, 4, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 4, 4, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 4, 4, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 4, 4, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 2, 2, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 1, 1, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________
I'm not sure if this is an expected behaviour, but seems like the argument classes
cannot affects the model when the include_top
flag is off.
keras-applications/keras_applications/vgg19.py
Lines 187 to 198 in 4cef245
If it is okay, I'd like to make a PR with some corresponding test code.
Thanks for the wonderful work by the way, will look forward to your comment. :)
Wondering if the mode needs to be explicitly set to the backend I am using. For example in VGG16, preprocess_input () seems to pick the 'caffe' mode if I leave it unset , even if though my backend is Tensorflow. How would choosing one over the other would impact inference of the pre-trained VGG16 model ?
Some model definitions are not precise:
he_normal
.The version recorded in the setup.py file for the 1.0.4 tag is 1.0.2, which is incorrect. The sdist on PyPI has the correct version in the setup.py file.
This can causes issues if a checkout of the git tag is used as the mean to install keras-applications as the incorrect version will be recorded in the metadata.
I do not think anything should be done to fix this as changing a tag is a bad procedure. I think it is useful to have this in the issue tracker in case anyone else runs into the issue.
I am trying to replace Pooling layers in Densenet with a custom pooling layer I implemented. I am able to use the model without loading imagenet weights but when I try with by_name=False
which is default, it raises an error due to difference in number of layers. I tried changing by_name=True
which stops the error but does not actually load the weights(I assume it does not load since the predictions it makes on a given image is different on each load and performance on training is worse compared to pre-trained).
Is there some way to use the pre-trained weights along with additional layers?
Is it possible to add a U-Net architecture to this list (https://arxiv.org/abs/1505.04597)? This is very popular in current medical imaging research and would be very helpful to have an officially supported version of the model that is pretrained on medical images. Even an untrained, but official version of the model would be a great starting spot for the medical research community.
There is an official version of the model with weights maintained by the authors, however, it uses Caffe (https://lmb.informatik.uni-freiburg.de/people/ronneber/u-net/). A Keras version would be helpful as many in the medical research industry are already using Keras/TensorFlow.
According Google's research : https://arxiv.org/abs/1805.08974
Inception-V4 shows best performance on fine-tune, while keras-application still not include inception-v4 pretrained model. Add an official implementation will be very helpful, thank u.
Nowhere in the documentation the release of ImageNet used to get the weights for the pretrained models is indicated.
Hello,
I am trying to fine tune InceptionResNetV2 model and I want to add few more classes in it like Watermelon, tomato etc so I tried many ways and every time I am getting an error "a 2D array of shape (samples, 1000)). Found array with shape: 5", so Is it possible to add new classes in existing model or how can I merge my new trained model with InceptionResNetV2 ?
Hi, I have been trying to figure out how width and height of an image is handled by Keras applications. For example if I go through the VGG16 code in https://keras.io/applications/ (copied below) , I see that there is flipping of height and width.
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
model = VGG16(weights='imagenet', include_top=False)
img_path = 'elephant.jpg'
img = image.load_img(img_path, )
x = image.img_to_array(img)
x1 = np.expand_dims(x, axis=0)
x2 = preprocess_input(x1)
features = model.predict(x2)
The image object uses PIL, and I see the following.
In [117]: img.size
Out[117]: (500, 375)
In [118]: x.shape
Out[118]: (375, 500, 3)
In [119]: x1.shape
Out[119]: (1, 375, 500, 3)
In [120]: x2.shape
Out[120]: (1, 375, 500, 3)
In the above img shows 500 as width, which is the how the image supposed to be displayed. However, after converting to numpy array, width and height seems to be flipped. What is the input_shape convention for VGG16 ? Which dimension is considered width and height respectively ? The example shape was for a square image 224x224x3, hence ambivalent. Also, a related question is how does that impact usage ( as it is a pre-trained network).
x=Input(shape=(224,224,3))
from keras_applications import densenet
model=densenet.DenseNet(blocks=[2,2,2,2],weights=None,input_tensor=x)
or
from keras.applications import densenet
model=densenet.densenet.DenseNet(blocks=[2,2,2,2],weights=None,input_tensor=x)
Error:
Traceback (most recent call last):
File "", line 1, in
File "D:\Py36\Python36\lib\site-packages\keras_applications\densenet.py", line 193, in DenseNet
data_format=backend.image_data_format(),
AttributeError: 'NoneType' object has no attribute 'image_data_format'
I have been checked all the models here with TensorFlow backend (see the commit). For ImageNet validation set, all except ResNet50 and MobileNet showed the same top-k errors with TensorNets.
We need to investigate where the differences come from ๐ฅ.
Hello, I get
Exception: URL fetch failure on https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)
When trying to import InceptionV3 on Python3.6, using
from keras.applications.inception_v3 import InceptionV3
basemodel = InceptionV3(weights='imagenet', include_top=False)
If you go to the link, it's a 404 error.
Using TensorFlow backend.
Traceback (most recent call last):
File ".\train.py", line 8, in
from keras.applications.mobilenet import MobileNet, _depthwise_conv_block
ImportError: cannot import name '_depthwise_conv_block'
I get
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-6-ab6c5b1b7181> in <module>()
3 import numpy as np
4
----> 5 basemodel = InceptionV3(weights='imagenet', include_top=False)
6 x = base_model.output
7 x = Dense(1024, activation='relu')(x)
/usr/local/lib/python3.7/site-packages/keras/applications/__init__.py in wrapper(*args, **kwargs)
26 kwargs['models'] = models
27 kwargs['utils'] = utils
---> 28 return base_fun(*args, **kwargs)
29
30 return wrapper
/usr/local/lib/python3.7/site-packages/keras/applications/inception_v3.py in InceptionV3(*args, **kwargs)
9 @keras_modules_injection
10 def InceptionV3(*args, **kwargs):
---> 11 return inception_v3.InceptionV3(*args, **kwargs)
12
13
/usr/local/lib/python3.7/site-packages/keras_applications/inception_v3.py in InceptionV3(include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
167 channel_axis = 3
168
--> 169 x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid')
170 x = conv2d_bn(x, 32, 3, 3, padding='valid')
171 x = conv2d_bn(x, 64, 3, 3)
/usr/local/lib/python3.7/site-packages/keras_applications/inception_v3.py in conv2d_bn(x, filters, num_row, num_col, padding, strides, name)
76 padding=padding,
77 use_bias=False,
---> 78 name=conv_name)(x)
79 x = layers.BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
80 x = layers.Activation('relu', name=name)(x)
/usr/local/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
429 'You can build it manually via: '
430 '`layer.build(batch_input_shape)`')
--> 431 self.build(unpack_singleton(input_shapes))
432 self.built = True
433
/usr/local/lib/python3.7/site-packages/keras/layers/convolutional.py in build(self, input_shape)
139 name='kernel',
140 regularizer=self.kernel_regularizer,
--> 141 constraint=self.kernel_constraint)
142 if self.use_bias:
143 self.bias = self.add_weight(shape=(self.filters,),
/usr/local/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.7/site-packages/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
247 if dtype is None:
248 dtype = K.floatx()
--> 249 weight = K.variable(initializer(shape),
250 dtype=dtype,
251 name=name,
/usr/local/lib/python3.7/site-packages/keras/initializers.py in __call__(self, shape, dtype)
216 limit = np.sqrt(3. * scale)
217 return K.random_uniform(shape, -limit, limit,
--> 218 dtype=dtype, seed=self.seed)
219
220 def get_config(self):
/usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in random_uniform(shape, minval, maxval, dtype, seed)
4137 seed = np.random.randint(10e6)
4138 return tf.random_uniform(shape, minval=minval, maxval=maxval,
-> 4139 dtype=dtype, seed=seed)
4140
4141
/usr/local/lib/python3.7/site-packages/tensorflow/python/ops/random_ops.py in random_uniform(shape, minval, maxval, dtype, seed, name)
233 with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) as name:
234 shape = _ShapeTensor(shape)
--> 235 minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
236 maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")
237 seed1, seed2 = random_seed.get_seed(seed)
/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
996 name=name,
997 preferred_dtype=preferred_dtype,
--> 998 as_ref=False)
999
1000
/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1092
1093 if ret is None:
-> 1094 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1095
1096 if ret is NotImplemented:
/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
215 as_ref=False):
216 _ = as_ref
--> 217 return constant(v, dtype=dtype, name=name)
218
219
/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
194 tensor_value.tensor.CopyFrom(
195 tensor_util.make_tensor_proto(
--> 196 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
197 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
198 const_tensor = g.create_op(
/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
534 raise TypeError(
535 "Element type not supported in TensorProto: %s" % numpy_dtype.name)
--> 536 append_fn(tensor_proto, proto_values)
537
538 return tensor_proto
tensorflow/python/framework/fast_tensor_util.pyx in tensorflow.python.framework.fast_tensor_util.AppendFloat32ArrayToTensorProto()
/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/google/protobuf/internal/containers.py in append(***failed resolving arguments***)
249 def append(self, value):
250 """Appends an item to the list. Similar to list.append()."""
--> 251 self._values.append(self._type_checker.CheckValue(value))
252 if not self._message_listener.dirty:
253 self._message_listener.Modified()
UnboundLocalError: local variable 'self' referenced before assignment
when instantiating an InceptionV3 model on Python 3.7 using
from keras.applications.inception_v3 import InceptionV3
basemodel = InceptionV3(weights='imagenet', include_top=False)
...
This seems to propagate to other models in keras.applications
(see https://stackoverflow.com/questions/51964883/keras-and-tensorflow-unboundlocalerror-local-variable-self-referenced-before )
The nightly build of TF has switched to a version where we use keras_applications
as the module tf.keras.applications
. Unfortunately, this has created the following issue: when using Keras (not tf.keras) with the TF backend and when importing TF, the underlying Keras backend being used by keras.applications is actually tf.keras, causing this code to fail:
import tensorflow as tf # Importing tf creates the issue
import keras
xception = keras.application.Xception(weights=None)
d = keras.layers.Dense(1)(xception.outputs[0])
In general the fact that modules imports represent a shared state across the entire python runtime represents an impassable issue (e.g. we could use reload
to fix the above, but that would introduce the reverse issue). We need to de-share our two imports of keras_applications
somehow.
There are two possible fixes here:
keras_applications
as a local copy both in Keras and tf.keras. I haven't been able to get this to work (using the existing literature). If it can be made to work, it would be a good option.keras_applications
receive the modules they need as keyword arguments. In this setup the applications make no imports from keras (instead they get everything they need to run from their keyword arguments, which are set to correct values in the redefined signatures in the parent packages).I will go with the second solution unless someone has a better idea.
I am using TensorFlow Serving(1.10.0-dev) and have deployed InceptionV3 model using REST API on port 9000 on my Ubuntu 18.04.1. Inorder to test the API, I have written a sample_request.py
script:
import requests, argparse, json
import numpy as np
from keras.preprocessing import image
from keras_applications import inception_v3
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path of the image")
args = vars(ap.parse_args())
image_path = args['image']
img = image.img_to_array(image.load_img(image_path, target_size=(224, 224))) / 255.
# this line is added because of a bug in tf_serving < 1.11
img = img.astype('float16')
payload = {
"instances": [{'input_image': img.tolist()}]
}
# sending post request
r = requests.post('http://localhost:9000/v1/models/ImageClassifier:predict', json=payload)
pred = json.loads(r.content.decode('utf-8'))
print(json.dumps(inception_v3.decode_predictions(np.array(pred['predictions']))[0]))
Just a month ago this script was working perfectly and now suddenly I get this error:
Using TensorFlow backend.
Traceback (most recent call last):
File "sample_request.py", line 49, in <module>
print(inception_v3.decode_predictions(np.array(pred['predictions']))[0])
File "/home/ubuntu/tensorflow/lib/python3.6/site-packages/keras_applications/imagenet_utils.py", line 224, in decode_predictions
fpath = keras_utils.get_file(
AttributeError: 'NoneType' object has no attribute 'get_file'
I did find a work around by replacing the import statement:
from keras_applications import inception_v3
with
from keras.applications import inception_v3
But the response format has change and it gives response like this:
$ python sample_request.py -i ./test_images/car.png
Using TensorFlow backend.
[('n04285008', 'sports_car', 0.99841400000000002), ('n04037443', 'racer', 0.00140099), ('n03459775', 'grille', 0.00016079399999999999), ('n02974003', 'car_wheel', 9.5786199999999998e-06), ('n03100240', 'convertible', 6.0158099999999999e-06)]
Those imagenet (starting with 'n0....') labels were not present earlier. Please can anyone tell me where is the issue?
Loading a VGG19 layer with include_top=False and pooling=None should return an output that is the 4D tensor of the last convolutional layer according to the docstring.
pooling: Optional pooling mode for feature extraction
when `include_top` is `False`.
- `None` means that the output of the model will be
the 4D tensor output of the
last convolutional layer.
However, we get a MaxPooling layer at the very end rather than a Conv2D layer.
>>> vgg19 = keras.applications.VGG19(weights='imagenet', include_top=False, pooling=None)
>>> vgg19.layers
[<keras.engine.input_layer.InputLayer at 0x7ef7b7f5e710>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb67ddd8>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb2d72b0>,
<keras.layers.pooling.MaxPooling2D at 0x7ef7bb2ef2b0>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb2ef908>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb28f7f0>,
<keras.layers.pooling.MaxPooling2D at 0x7ef7bb2ad160>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb2adc50>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb2631d0>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb202160>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb2205c0>,
<keras.layers.pooling.MaxPooling2D at 0x7ef7bb23b208>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb23bd68>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb1e7208>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb182048>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb19c128>,
<keras.layers.pooling.MaxPooling2D at 0x7ef7bb1ba4a8>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb1bad30>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb1762e8>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb114390>,
<keras.layers.convolutional.Conv2D at 0x7ef7bb12d208>,
<keras.layers.pooling.MaxPooling2D at 0x7ef7bb0cb588>]
I've traced it back via this git blame to this commit which was a part of farizrahman4u/keras#9
Depending on what is the intended behaviour, we'll need to either change the docstring to clarify that it is the MaxPooling2D layer that is the output, or modify the VGG scripts so that the output is a Conv2D layer.
Some steps involved for the latter (more involved) fix:
if include_top
statement, see this codeblock section.I can create a pull request to help out with the first two once we clarify the intended behaviour. Changing the weight files will require a bit of help.
keras ver:2.2.0
When use Mobilenet V2, alpha is wrong(example:0.25).The error message is as following:
ValueError: If imagenet weights are being loaded, alpha can be one of0.25
, 0.50
, 0.75
or 1.0
only.
But the right alpha is:0.35, 0.50, 0.75, 1.0, 1.3, 1.4.
Please check the mobilenet_v2.py 310th row.
I can see that the source code still points to https://github.com/fchollet/deep-learning-models/releases. Will these end up pointing to: https://github.com/keras-team/keras-applications/releases? The deep-learning-models repo README says its now deprecated.
Is their a fundamental problem with allowing input images of different size? I had to comment this line to allow training images of size 64x64. My goal is to experiment with progressive training where you train on smaller images then train on larger ones (idea advocated in the fast.ai course).
Right now it complains about the image size, even though it seem it shouldn't once include_top
is False
.
Currently, for all models, we can do weights='imagenet'
or weights=None
. We would like to add weights='openimages'
. From @taehoonlee in #26 :
I think that it is worth and significant differentiation to add reference models trained with Open Images. Contributions welcome :) In my humble opinion,
Using different datasets without hyper-parameter changes: it is easy and worth to add because we just need to replace weights='imagenet' with weights='openimages', classes=5000 and add information on weight files.
Using different datasets with minor hyper-parameter changes (e.g., changing the numbers of blocks in ResNets): if factorization over imagenet and openimages models is easy, it is still worth to add keras-applications.
Using different datasets with significantly different hyper-parameters (e.g., changing the block configuration or adding some layers): it is better to consider them as the community contribution and exist outside of keras-applications.
from keras_applications.nasnet import NASNet
IMG_HEIGHT = 224 * 3
IMG_WIDTH = 224
DIMS = (IMG_HEIGHT, IMG_WIDTH, 3)
...
NASNet(include_top=False, input_shape=DIMS, num_blocks=1, penultimate_filters=24, stem_block_filters=32, skip_reduction=False)
or
NASNet(include_top=False, input_shape=DIMS, num_blocks=1, penultimate_filters=48, stem_block_filters=32, skip_reduction=False)
cause error:
Traceback (most recent call last):
File "train_classes_v3.py", line 369, in <module>
_main(parser.parse_args())
File "train_classes_v3.py", line 322, in _main
use_multiprocessing=True,
File "/home/xxx/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/xxx/.local/lib/python3.5/site-packages/keras/engine/training.py", line 1415, in fit_generator
initial_epoch=initial_epoch)
File "/home/xxx/.local/lib/python3.5/site-packages/keras/engine/training_generator.py", line 39, in fit_generator
model._make_train_function()
File "/home/xxx/.local/lib/python3.5/site-packages/keras/engine/training.py", line 498, in _make_train_function
loss=self.total_loss)
File "/home/xxx/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/xxx/.local/lib/python3.5/site-packages/keras/optimizers.py", line 500, in get_updates
self.updates.append(K.update(m, m_t))
File "/home/xxx/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 971, in update
return tf.assign(x, new_x)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/state_ops.py", line 284, in assign
return ref.assign(value, name=name)
AttributeError: 'Tensor' object has no attribute 'assign'
Second error:
from keras_applications.nasnet import NASNet
IMG_HEIGHT = 224 * 3
IMG_WIDTH = 224
DIMS = (IMG_HEIGHT, IMG_WIDTH, 3)
...
NASNet(include_top=False, input_shape=DIMS, num_blocks=1, penultimate_filters=72,
stem_block_filters=32, skip_reduction=False)
and
NASNet(include_top=False, input_shape=DIMS, num_blocks=1, penultimate_filters=120, stem_block_filters=32, skip_reduction=False)
File "train_classes_v3.py", line 369, in <module>
_main(parser.parse_args())
File "train_classes_v3.py", line 256, in _main
base_model = NASNet(include_top=False, input_shape=DIMS, num_blocks=1, penultimate_filters=120, stem_block_filters=32, skip_reduction=False)
File "/home/xxx/.local/lib/python3.5/site-packages/keras_applications/nasnet.py", line 216, in NASNet
x, p = _normal_a_cell(x, p, filters, block_id='%d' % (i))
File "/home/xxx/.local/lib/python3.5/site-packages/keras_applications/nasnet.py", line 614, in _normal_a_cell
x3 = layers.add([x3, p], name='normal_add_3_%s' % block_id)
File "/home/xxx/.local/lib/python3.5/site-packages/keras/layers/merge.py", line 547, in add
return Add(**kwargs)(inputs)
File "/home/xxx/.local/lib/python3.5/site-packages/keras/engine/base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "/home/xxx/.local/lib/python3.5/site-packages/keras/layers/merge.py", line 90, in build
output_shape = self._compute_elemwise_op_output_shape(output_shape, shape)
File "/home/xxx/.local/lib/python3.5/site-packages/keras/layers/merge.py", line 61, in _compute_elemwise_op_output_shape
str(shape1) + ' ' + str(shape2))
ValueError: Operands could not be broadcast together with shapes (84, 28, 5) (84, 28, 4)
Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f0781bc3eb8>>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 707, in __del__
TypeError: 'NoneType' object is not callable
Hey,
I am trying to import the ResNet50 network on an image classification problem
The input shape I am feeding it is (64, 64, 3), and the documentation mentions that the minimum input width / height is (32, 32).
This is the the code I am using to load the
model = ResNet50(include_top=False, input_shape=(64,64,3), classes=2, weights=None)
This is the error code I am getting:
model = ResNet50(include_top=False, input_shape=(64,64,3), classes=2, weights=None)
File "/usr/local/lib/python3.5/dist-packages/keras_applications/resnet50.py", line 198, in ResNet50
weights=weights)
File "/usr/local/lib/python3.5/dist-packages/keras_applications/imagenet_utils.py", line 316, in _obtain_input_shape
str(input_shape) + '`')
ValueError: Input size must be at least 197x197; got `input_shape=(64, 64, 3)`
In theory ( 64, 64), should be accepted. Is there any error in my code, is the documentation mismatched with the code?
How can I solve this?
Thank you!!
from . import get_submodules_from_kwargs
ValueError: Error when checking target: expected out_relu to have 4 dimensions, but got array with shape (32, 7)
(I have 7 classes in my label.)
Anybody can help?
When instantiating MobileNetV2
without special kwargs
I get the error:
Using TensorFlow backend.
Traceback (most recent call last):
[...]
File "/usr/local/lib/python2.7/dist-packages/keras_applications/mobilenet_v2.py", line 267, in MobileNetV2
if backend.image_data_format() == 'channels_first':
AttributeError: 'NoneType' object has no attribute 'image_data_format'
because backend
is undefined (None
).
Explicitly defining the backend via kwargs MobileNetV2(..., backend=keras.backend)
solves the issue.
It is not obvious to me, why get_submodules_from_kwargs
is not using the keras defaults (in this case keras.backend
) since there is no reason to mix backends in one instance of keras.
Why does one have to set the backend
, layers
and models
explicitly when using MobileNetV2
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.