qubvel / efficientnet Goto Github PK
View Code? Open in Web Editor NEWImplementation of EfficientNet model. Keras and TensorFlow Keras.
Home Page: https://arxiv.org/abs/1905.11946
License: Apache License 2.0
Implementation of EfficientNet model. Keras and TensorFlow Keras.
Home Page: https://arxiv.org/abs/1905.11946
License: Apache License 2.0
hi,
Trying to train this. when running :
from efficientnet import EfficientNetB0
import keras
from keras.layers import Activation, Dense, Multiply, Input, Flatten, Dropout, Conv2D, BatchNormalization
inp_mask = Input(shape=(512,512,3))
bn_inp = inp_mask
pretrain_model_mask = EfficientNetB0(
input_shape=(512, 512, 3), #SWITCH
include_top=False,
weights='imagenet',
# dropout=0.5,
# pooling='max'
)(bn_inp)
I get the following
Traceback (most recent call last):
File "t.py", line 12, in <module>
weights='imagenet',
File "/home/m/.local/lib/python2.7/site-packages/efficientnet/model.py", line 380, in EfficientNetB0
weights=weights, classes=classes)
File "/home/m/.local/lib/python2.7/site-packages/efficientnet/model.py", line 358, in _get_model_by_name
model = EfficientNet(input_shape, block_agrs_list, global_params, include_top=include_top)
File "/home/m/.local/lib/python2.7/site-packages/efficientnet/model.py", line 282, in EfficientNet
x = MBConvBlock(block_args, global_params)(x)
File "/home/m/.local/lib/python2.7/site-packages/efficientnet/model.py", line 232, in block
x = DropConnect(global_params.drop_connect_rate)(x)
File "/home/m/.local/lib/python2.7/site-packages/efficientnet/layers.py", line 16, in __init__
super().__init__()
TypeError: super() takes at least 1 argument (0 given)
Could you provide some details about how you converted the weights?
The official checkpoints for EfficientNet-B4 and -B5 have recently been released and I tried to convert them, but my B4 model only achieves 79.7% top-1 accuracy instead of 82.6%.
K.set_image_data_format('channels_first')
Hello, I'm wondering how to correctly use this library in a channels first setup. I already have my generator outputting (batches, channels, x, y) and tried initializing efficientnet with input shape (channels, x, y) but it doesn't seem to work. Any help is appreciated!
Consistently getting errors, either No module named 'efficientnet.tfkeras'
or AttributeError: 'tuple' object has no attribute 'layer'
. Using with standalone Keras, model compiles ok, however, I want to use tf.data.
I'm using tensorflow-gpu==1.14
while running the code
from efficientnet import EfficientNetB0
I get error DLL load failed: The specified module could not be found.
tracebook
from efficientnet import EfficientNetB0
Traceback (most recent call last):
File "", line 1, in
from efficientnet import EfficientNetB0
File "C:\Users\Student\Anaconda2\lib\site-packages\efficientnet_init_.py", line 18, in
from .preprocessing import center_crop_and_resize, preprocess_input
File "C:\Users\Student\Anaconda2\lib\site-packages\efficientnet\preprocessing.py", line 16, in
from skimage.transform import resize
File "C:\Users\Student\Anaconda2\lib\site-packages\skimage_init_.py", line 135, in
from .data import data_dir
File "C:\Users\Student\Anaconda2\lib\site-packages\skimage\data_init_.py", line 16, in
from ._binary_blobs import binary_blobs
File "C:\Users\Student\Anaconda2\lib\site-packages\skimage\data_binary_blobs.py", line 2, in
from ..filters import gaussian
File "C:\Users\Student\Anaconda2\lib\site-packages\skimage\filters_init_.py", line 1, in
from .lpi_filter import inverse, wiener, LPIFilter2D
File "C:\Users\Student\Anaconda2\lib\site-packages\skimage\filters\lpi_filter.py", line 7, in
from scipy.fftpack import ifftshift
File "C:\Users\Student\Anaconda2\lib\site-packages\scipy\fftpack_init_.py", line 99, in
from .basic import *
File "C:\Users\Student\Anaconda2\lib\site-packages\scipy\fftpack\basic.py", line 12, in
**from . import _fftpack**
ImportError: DLL load failed: The specified module could not be found.
Hi,
I am using v1.0.0b3 with tf.keras and I get the following warning multiple times when creating an EfficientNet model:
WARNING:tensorflow:Entity <bound method Dropout.call of <efficientnet.model.get_dropout..FixedDropout object at 0x0000017000529710>> could not be transformed and will be executed as-is. Please
report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10
) and attach the full output. Cause: converting <bound method Dropout.call of
<efficientnet.model.get_dropout..FixedDropout object at 0x0000017000529710>>: AssertionError: Bad argument number for Name: 3, expecting 4
Is this a known issue that I can safely ignore, or do you know if it could affect whether the dropout layer works correctly?
Best Regards
John
Weights for B6 and B7 trained with auto-augment are available. Maybe worth adding?
https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet
I'm now getting the error message
AttributeError: 'NoneType' object has no attribute 'image_data_format'
on this line. It appears that a backend
argument needs to be provided when creating EfficientNet through any of the EfficientNetB*
functions, but this is an undocumented argument as far as I can see.
What value should be provided for it?
I have a few models that I am not that keen on retraining....
Thank you!
when I try to load the trained model, it give me "ValueError: Unknown activation function:swish", do you know how to solve that? thanks a lot!
Hi, qubvel. I piped install and trying to import 'load_model, but it got ImportError: cannot import name 'load_model' message.
I do recheck the whole*.py files but I still can't find any function named 'load_model'. Maybe I should pip installed fom git+https://github.com/qubvel/efficientnet instead of pip install efficientnet directly.
Really appreciate your help.
I cannot load models with the v1.0.0b1 version of your package. In model.py
of your package you are calling backend.backend()
: https://github.com/qubvel/efficientnet/blob/master/efficientnet/model.py#L246 .
I cannot see anywhere where this is supposed to be automatically initialized to the user's backend. The same file contains backend = None
: https://github.com/qubvel/efficientnet/blob/master/efficientnet/model.py#L43
When trying to load a model this leads to the following error message:
Found 3662 validated image filenames.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-3f84b7149375> in <module>
8 model_dir = "../",
9 folds = 1,
---> 10 batch_size = 16)
11 ("")
<ipython-input-12-eda66ccb6232> in pred_cv_test(df, model_dir, folds, nr_samples, batch_size)
31 batch_size = batch_size)
32
---> 33 model = get_model(model_dir = model_dir, fold_nr = fold)
34 preds = model.predict_generator(
35 generator = test_generator,
<ipython-input-12-eda66ccb6232> in get_model(model_dir, fold_nr)
9 best_epoch_model = load_model(
10 model_dir + "effnet_reg_fold" + str(fold_nr) + ".h5",
---> 11 custom_objects = {"root_mse": root_mse})
12 return(best_epoch_model)
13
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\saving\save.py in load_model(filepath, custom_objects, compile)
144 h5py is not None and (
145 isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 146 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
147
148 if isinstance(filepath, six.string_types):
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
210 model_config = json.loads(model_config.decode('utf-8'))
211 model = model_config_lib.model_from_config(model_config,
--> 212 custom_objects=custom_objects)
213
214 # set weights
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\saving\model_config.py in model_from_config(config, custom_objects)
53 '`Sequential.from_config(config)`?')
54 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top
---> 55 return deserialize(config, custom_objects=custom_objects)
56
57
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\layers\serialization.py in deserialize(config, custom_objects)
87 module_objects=globs,
88 custom_objects=custom_objects,
---> 89 printable_module_name='layer')
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
190 custom_objects=dict(
191 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 192 list(custom_objects.items())))
193 with CustomObjectScope(custom_objects):
194 return cls.from_config(cls_config)
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\engine\network.py in from_config(cls, config, custom_objects)
1129 if layer in unprocessed_nodes:
1130 for node_data in unprocessed_nodes.pop(layer):
-> 1131 process_node(layer, node_data)
1132
1133 name = config.get('name')
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\engine\network.py in process_node(layer, node_data)
1085 flat_input_tensors = nest.flatten(input_tensors)
1086 if len(flat_input_tensors) == 1:
-> 1087 layer(flat_input_tensors[0], **kwargs)
1088 else:
1089 layer(input_tensors, **kwargs)
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
632 outputs = base_layer_utils.mark_as_return(outputs, acd)
633 else:
--> 634 outputs = call_fn(inputs, *args, **kwargs)
635
636 except TypeError as e:
~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\layers\core.py in call(self, inputs)
358
359 def call(self, inputs):
--> 360 return self.activation(inputs)
361
362 def compute_output_shape(self, input_shape):
~\Anaconda3\envs\r-tensorflow\lib\site-packages\efficientnet\model.py in swish(x)
134 """
135
--> 136 if backend.backend() == 'tensorflow':
137 try:
138 # The native TF implementation has a more
AttributeError: 'NoneType' object has no attribute 'backend'
I can get it to work if I hardcode my backend by editing model.py.
In your inference notebook you are not loading a saved model from disk. Have you guys tested and successfully loaded in a model from disk with the new version?
Hi, I'm trying to train imagenet with EfficientnetB3. Would you please be kind enough to mention the correct combination of hyperparameters.
runners.py", line 80, in train
model = model_fn(inputs, problem.output_spec())
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/gin/config.py", line 1032, in wrapper
utils.augment_exception_message_and_reraise(e, err_str)
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/gin/utils.py", line 49, in augment_exception_message_and_reraise
six.raise_from(proxy.with_traceback(exception.__traceback__), None)
File "<string>", line 3, in raise_from
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/gin/config.py", line 1009, in wrapper
return fn(*new_args, **new_kwargs)
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/ml_glaucoma/models/efficientnet.py", line 45, in efficient_net
input_tensor=inputs, **kwargs).outputs
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/efficientnet/__init__.py", line 57, in wrapper
return func(*args, **kwargs)
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/efficientnet/model.py", line 548, in EfficientNetB4
**kwargs)
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/efficientnet/model.py", line 369, in EfficientNet
if not backend.is_keras_tensor(input_tensor):
File "/opt/venvs/tflow3.6/lib/python3.6/site-packages/tensorflow/python/util/deprecation_wrapper.py", line 106, in __getattr__
attr = getattr(self._dw_wrapped_module, name)
Sep 08 03:15:55 ip-172-31-25-230 python[3136]: AttributeError: module 'tensorflow.python.keras.api._v1.keras.backend' has no attribute 'is_keras_tensor'
In call to configurable 'efficient_net' (<function efficient_net at 0x7feef701ce18>)
[Environment] GeForce RTX 2080 11GB
The max batch size dramatically drop to 8 in training EfficientNetB4 with 1 GPU, while the bacth size is 64 in training InceptionV3.
Thanks for any help.
model_json = model.to_json()
with open("testing.json", "w") as json_file:
json_file.write(model_json)
NameError Traceback (most recent call last)
in ()
----> 1 model_json = model.to_json()
2
3
4 with open("testing.json", "w") as json_file:
5 json_file.write(model_json)
3 frames
/usr/local/lib/python3.6/dist-packages/efficientnet/layers.py in get_config(self)
35 def get_config(self):
36 config = super().get_config()
---> 37 config['drop_connect_rate'] = drop_connect_rate
38
39
NameError: name 'drop_connect_rate' is not defined
Hi,
I wanna use the pretrained weights to perform feature extraction. What would be the best way to go about this?
Currently, I am loading the model and than trying to convert it to tflite. I've used just EffNet from 0.0.4 and keras/tfkeras from pre version, but issues occurred
ValueError: Unknown activation function:swish
Hi, thanks for your Keras implementation! I'm currently looking through the model definition to solidify my understanding of the paper. I've noticed something odd in the parametrisation of the blocks: https://github.com/qubvel/efficientnet/blob/master/efficientnet/params.py#L170 -
's22' means a stride of [2,2] which means downsampling both image dimensions by half while 's11' means a stride of [1,1] which means no downsampling (this is obvious to you I guess). Your order of strides is: s11 s22 s22 s22 s11 s22 s11 but according to Table 1 in the paper, I think it should be s11 s22 s22 s1 s22 s22 s11. Do you agree or am I wrong somewhere?
@qubvel what should be the version of tensorflow and keras so that there is no version conflict. This is not working in tensorflow-gpu==1.14 as well tensorflow-gpu==2.0.0-b1.
Thanks in advance.
I trained an EfficientNetB3.
But trying to load the model with keras.load_model(modelfile)
I get this error:
File "...python3.6/site-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown initializer: conv_kernel_initializer
What do I miss?
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.7
tf.keras.backend.set_session(tf.Session(config=config))
model = efn.EfficientNetB7()
model.summary()
# create new output layer
output_layer = Dense(5, activation='sigmoid', name="retrain_output")(model.get_layer('top_dropout').output)
new_model = Model(model.input, output=output_layer)
new_model.summary()
# lock previous weights
for i, l in enumerate(new_model.layers):
if i < 228:
l.trainable = False
# lock probs weights
new_model.compile(loss='mean_squared_error', optimizer='adam')
batch_size = 5
samples_per_epoch = 30
epochs = 20
# generate train data
train_datagen = ImageDataGenerator(
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0)
train_generator = train_datagen.flow_from_directory(
train_data_input_folder,
target_size=(input_dim, input_dim),
batch_size=batch_size,
class_mode='categorical',
seed=2019,
subset='training')
validation_generator = train_datagen.flow_from_directory(
validation_data_input_folder,
target_size=(input_dim, input_dim),
batch_size=batch_size,
class_mode='categorical',
seed=2019,
subset='validation')
new_model.fit_generator(
train_generator,
samples_per_epoch=samples_per_epoch,
epochs=epochs,
validation_steps=20,
validation_data=validation_generator,
nb_worker=24)
new_model.save(model_output_path)
2019-11-17 08:52:52.903583: I tensorflow/stream_executor/dso_loader.cc:152] successfully opened CUDA library libcublas.so.10.0 locally
....
...
2019-11-17 08:53:24.713020: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 110 Chunks of size 27724800 totalling 2.84GiB
2019-11-17 08:53:24.713024: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 6 Chunks of size 38814720 totalling 222.10MiB
2019-11-17 08:53:24.713027: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 23 Chunks of size 54000128 totalling 1.16GiB
2019-11-17 08:53:24.713031: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 1 Chunks of size 73760000 totalling 70.34MiB
2019-11-17 08:53:24.713034: I tensorflow/core/common_runtime/bfc_allocator.cc:645] Sum Total of in-use chunks: 5.45GiB
2019-11-17 08:53:24.713040: I tensorflow/core/common_runtime/bfc_allocator.cc:647] Stats:
Limit: 5856749158
InUse: 5848048896
MaxInUse: 5848061440
NumAllocs: 6140
MaxAllocSize: 32591708162019-11-17 08:53:24.713214: W tensorflow/core/common_runtime/bfc_allocator.cc:271] ****************************************************************************************************
2019-11-17 08:53:24.713232: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at cwise_ops_common.cc:70 : Resource exhausted: OOM when allocating tensor with shape[5,1344,38,38] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
Traceback (most recent call last):
File "/home/naort/Desktop/deep-learning-data-preparation-tools/EfficientNet-Transfer-Learning-Boiler-Plate/model_retrain.py", line 76, in
nb_worker=24)
File "/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1732, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py", line 220, in fit_generator
reset_metrics=False)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1514, in train_on_batch
outputs = self.train_function(ins)
File "/home/naort/.local/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3076, in call
run_metadata=self.run_metadata)
File "/home/naort/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1439, in call
run_metadata_ptr)
File "/home/naort/.local/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in exit
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[5,1344,38,38] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[[{{node training/Adam/gradients/AddN_387-0-TransposeNHWCToNCHW-LayoutOptimizer}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.[[{{node Mean}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
Hi @qubvel,
thank you so much for sharing this awesome tool!
While I was running ./scripts/convert_from_tf_to_keras.sh
, attempting to convert the TF model into Keras, I encountered the following error:
==============================================
= .......................................... =
= Setting up the installation environment... =
= .......................................... =
==============================================
==========================================
= ...................................... =
= Converting the checkpoints to Keras... =
= ...................................... =
==========================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ ............................. ~
~ Converting efficientnet-b0... ~
~ ............................. ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Using TensorFlow backend.
Traceback (most recent call last):
File "/Users/yunseong/repos/ai/qubvel-efficientnet/scripts/load_efficientnet.py", line 155, in <module>
import eval_ckpt_main
File "tf_src/eval_ckpt_main.py", line 31, in <module>
import efficientnet_builder
File "tf_src/efficientnet_builder.py", line 28, in <module>
import efficientnet_model
File "tf_src/efficientnet_model.py", line 37, in <module>
from condconv import condconv_layers
ModuleNotFoundError: No module named 'condconv'
Do you have any clue how I can fix this issue?
In efficientnet/models.py :
efficientnet/efficientnet/model.py
Lines 359 to 388 in 035df1a
You fall back to the default if input_shape is None. However when we use the efficientnet backbone from segmentation models:
https://github.com/qubvel/segmentation_models/blob/ef6350388a0e774b8e602268c8f6a90ac0ed3a29/segmentation_models/unet/model.py#L14-L26
You can say that by default the input shape is (None, None, 3)
and therefore the if statement shown above evaluates to False and we don't fall back to the default input. I recommend replacing with
if input_shape is None or None in input_shape:...
Thank you so much for your great work on this and the segmentation_models!
Wenn I use the efficientnetB7 with tensorflow 2.0 (2.0.0-beta1) it prints:
Large dropout rate: 0.5625 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended
Is this already handled?
Could we choose tf.keras instead of keras? I tried saving the model and loading it via tf.keras load_model to convert it to tf.keras, but it gives me the same error as #2.
Looks like the structure of the model efficientnet is quite similar to mnasnet?
are the weights released with this repository also under APACHE-2.0 license?
Would it be possible to "easily" use this model as a backbone for object detection / instances segmentation as many other popular architectures like ResNet50? Thx
I was trying to convert my EfficientNet model to TFLite:
converter = tf.lite.TFLiteConverter.from_keras_model_file('effNetB3.h5',custom_objects={'swish': Swish(swish)})
tfmodel = converter.convert()
open ("effNetB3.tflite" , "wb") .write(tfmodel)
It gave me the following error:
ValueError: Unknown layer: FixedDropout
AttributeError Traceback (most recent call last)
in
1 import efficientnet
2
----> 3 model = efficientnet.EfficientNetB3(weights=None)
~/anaconda3/envs/py36/lib/python3.7/site-packages/efficientnet/model.py in EfficientNetB3(include_top, input_shape, input_tensor, weights, classes, pooling)
441 weights=weights,
442 classes=classes,
--> 443 pooling=pooling,
444 )
445
~/anaconda3/envs/py36/lib/python3.7/site-packages/efficientnet/model.py in _get_model_by_name(model_name, input_shape, input_tensor, include_top, weights, classes, pooling)
367 input_tensor=input_tensor,
368 include_top=include_top,
--> 369 pooling=pooling,
370 )
371
~/anaconda3/envs/py36/lib/python3.7/site-packages/efficientnet/model.py in EfficientNet(input_shape, block_args_list, global_params, input_tensor, include_top, pooling)
214 # Stem part
215 if input_tensor is None:
--> 216 inputs = KL.Input(shape=input_shape)
217 else:
218 if not K.is_keras_tensor(input_tensor):
~/anaconda3/envs/py36/lib/python3.7/site-packages/keras/engine/input_layer.py in Input(shape, batch_shape, name, dtype, sparse, tensor)
176 name=name, dtype=dtype,
177 sparse=sparse,
--> 178 input_tensor=tensor)
179 # Return tensor including _keras_shape and _keras_history.
180 # Note that in this case train_output and test_output are the same pointer.
~/anaconda3/envs/py36/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
~/anaconda3/envs/py36/lib/python3.7/site-packages/keras/engine/input_layer.py in init(self, input_shape, batch_size, batch_input_shape, dtype, input_tensor, sparse, name)
37 if not name:
38 prefix = 'input'
---> 39 name = prefix + '_' + str(K.get_uid(prefix))
40 super(InputLayer, self).init(dtype=dtype, name=name)
41
~/anaconda3/envs/py36/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
72 """
73 global _GRAPH_UID_DICTS
---> 74 graph = tf.get_default_graph()
75 if graph not in _GRAPH_UID_DICTS:
76 _GRAPH_UID_DICTS[graph] = defaultdict(int)
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
maybe lets make an update to requirements.txt?
Currently there's an assert on the input dimension to preprocess_input() that implies this function expects a single image; can you make this a bit more flexible, to allow for batches of images in the same way that the usual keras_applications.imagenet_utils.preprocess_input() function allows for a 3D or 4D input array? Thanks.
I'm trying to use efficientnet.model.preprocess_input
as ImageDataGenerator(preprocessing_function=preprocess_input, ...)
. However, getting following error:
~/.virtualenvs/venv1/lib/python3.7/site-packages/keras_applications/imagenet_utils.py in preprocess_input(x, data_format, mode, **kwargs)
184
185 if data_format is None:
--> 186 data_format = backend.image_data_format()
187 if data_format not in {'channels_first', 'channels_last'}:
188 raise ValueError('Unknown data_format ' + str(data_format))
AttributeError: 'NoneType' object has no attribute 'image_data_format'
Symptoms look very similar to keras-team/keras-applications#54 and it seems that solution can be also similar to keras-team/keras-applications#54 (comment)_ , i.e. replacing keras_applications
with keras.applications
here:
efficientnet/efficientnet/model.py
Line 39 in 91366f5
please give me a demo, thanks!
Hi @Callidior
I am going to adapt my segmentation-models
lib for tf.keras, so I also need efficientnet adapted for tf.keras.
I saw your implementation of Efficientnet for keras-applications. Very nice work, but it is too long to wait until keras will merge it and make a new release.
It would be nice if you make a PR with same code to this repo, or allow to copy it.
Or only inference?
I would like to know if there is a 'requirement.txt' for setting up efficient net using setup.py.
Hi
In the efficient-net model used dropout layer in the final prediction layer and also used in the Conv layers with rate 0.2 for training mode, Why in the inference mode didn't set rate of these layers to 1.0, and these layers don't operate in the inference mode?
With the included example/inference_example.ipynb, I am seeing lower prediction with version 1.0.0 as opposed to version 0.0.4.
Using the below code for testing both versions (with the exception of import efficientnet vs import efficientnet.keras/tfkeras) produce different prediction results:
!pip install efficientnet==0.0.4
!wget https://github.com/qubvel/efficientnet/raw/master/misc/panda.jpg
from keras.applications.imagenet_utils import decode_predictions
import numpy as np
import matplotlib.pyplot as plt
from efficientnet import EfficientNetB0
from efficientnet import center_crop_and_resize, preprocess_input
model = EfficientNetB0(weights='imagenet')
image = plt.imread('panda.jpg')
image_size = model.input_shape[1]
x = center_crop_and_resize(image, image_size=image_size)
x = preprocess_input(x)
x = np.expand_dims(x, 0)
y = model.predict(x)
decode_predictions(y)
Output from version 0.0.4:
[[('n02510455', 'giant_panda', 0.8347928),
('n02134084', 'ice_bear', 0.015602051),
('n02509815', 'lesser_panda', 0.0045535257),
('n02133161', 'American_black_bear', 0.002471913),
('n02132136', 'brown_bear', 0.0020707587)]]
Output from version 1.0.0:
[[('n02510455', 'giant_panda', 0.75878686),
('n02134084', 'ice_bear', 0.008354747),
('n02132136', 'brown_bear', 0.0072072297),
('n02509815', 'lesser_panda', 0.0041302275),
('n02120079', 'Arctic_fox', 0.0040210797)]]
At its current state (version 1.0.0), I am also seeing lower top-1 score training on CIFAR10 on EfficientNet B0 as opposed to the same on ResNet50, thinking that the issue may be caused by outdated/bad weights as shown above.
i had a problem when using your efficiententB3 as backbone for object detection task(previous ResNet50).
while i use ResNet50,batchsize per GPU is 16. But using EfficientNetB3 batchsize per GPU is no more than 12. I also print(model.summary()) to show num of parameters.ResNet50 is 23,000,000+, EfficientNetB3 is 10,000,000+.
So,obviously,EfficientNetB3's parameters is about half the size of the ResNet50.However,i can only set batchsize to 8 or 12.This make me confused.
My GPU is Tesla P100 -16G mem
I found that preprocess_input in official github contain random crop for training. and crop center for eval. However your code has just center crop for eval.
Is it necessary to do same as original?. or it is just an augmentation process. Now, i'm finetuned all layers but i just use only MEAN RGB and MEAN STD. Good val Acc and test Acc
HI, I tried to initialized the model with the following code:
import efficientnet.keras as efn
model = efn.EfficientNetB0(weights='imagenet')
but it returned E tensorflow/core/grappler/clusters/utils.cc:87] Failed to get device properties, error code: 30
Hi,
I ran pip install -U git+https://github.com/qubvel/efficientnet
and received the following:
Collecting git+https://github.com/qubvel/efficientnet
Cloning https://github.com/qubvel/efficientnet to /tmp/pip-k06rtpj3-build
Collecting keras_applications<=1.0.8,>=1.0.7 (from efficientnet==1.0.0b3)
Downloading https://files.pythonhosted.org/packages/71/e3/19762fdfc62877ae9102edf6342d71b28fbfd9dea3d2f96a882ce099b03f/Keras_Applications-1.0.8-py3-none-any.whl (50kB)
100% |████████████████████████████████| 51kB 11.2MB/s
Collecting scikit-image (from efficientnet==1.0.0b3)
Downloading https://files.pythonhosted.org/packages/00/ba/9688afba6ab3714875a8d22284ce0362086e47f3cda60243e5d4de5e5943/scikit_image-0.15.0-cp35-cp35m-manylinux1_x86_64.whl (26.2MB)
100% |████████████████████████████████| 26.2MB 57kB/s
Collecting numpy>=1.9.1 (from keras_applications<=1.0.8,>=1.0.7->efficientnet==1.0.0b3)
Downloading https://files.pythonhosted.org/packages/d4/64/7619774f0bd8ef364d46a5df8eb1bc78784cd787324b9624f6793e72f787/numpy-1.17.1-cp35-cp35m-manylinux1_x86_64.whl (20.1MB)
100% |████████████████████████████████| 20.2MB 77kB/s
Requirement already up-to-date: h5py in ./.local/lib/python3.5/site-packages (from keras_applications<=1.0.8,>=1.0.7->efficientnet==1.0.0b3)
Collecting PyWavelets>=0.4.0 (from scikit-image->efficientnet==1.0.0b3)
Downloading https://files.pythonhosted.org/packages/6a/3f/d92fbd9b65f879197620f404c1603bfeaa54560743bb9fb3a6b48b66a707/PyWavelets-1.0.3-cp35-cp35m-manylinux1_x86_64.whl (4.4MB)
100% |████████████████████████████████| 4.4MB 373kB/s
Collecting pillow>=4.3.0 (from scikit-image->efficientnet==1.0.0b3)
Downloading https://files.pythonhosted.org/packages/d6/98/0d360dbc087933679398d73187a503533ec0547ba4ffd2115365605559cc/Pillow-6.1.0-cp35-cp35m-manylinux1_x86_64.whl (2.1MB)
100% |████████████████████████████████| 2.1MB 802kB/s
Requirement already up-to-date: imageio>=2.0.1 in ./.local/lib/python3.5/site-packages (from scikit-image->efficientnet==1.0.0b3)
Collecting matplotlib!=3.0.0,>=2.0.0 (from scikit-image->efficientnet==1.0.0b3)
Downloading https://files.pythonhosted.org/packages/12/d1/7b12cd79c791348cb0c78ce6e7d16bd72992f13c9f1e8e43d2725a6d8adf/matplotlib-3.1.1.tar.gz (37.8MB)
100% |████████████████████████████████| 37.8MB 42kB/s
Complete output from command python setup.py egg_info:
Beginning with Matplotlib 3.1, Python 3.6 or above is required.
This may be due to an out of date pip.
Make sure you have pip >= 9.0.1.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-l3aisvf_/matplotlib/
You are using pip version 8.1.2, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Does anyone know the reason?
@qubvel I want to change the downsampling factor from 32 to 16 of last layer keeping everything else same? how can I change it? Thanks in advance! :)
ImportError: cannot import name '_validate_lengths'
This solves the problem, but it will be good to add requirements.txt
pip install -U scikit-image
python -c "import skimage; print(skimage.__version__)"
0.15.0
I trained the model on my custom dataset, and now when I am trying to import the .h5 file, it is throwing me this error
model = load_model('model/model_weights_efficient.h5')
Traceback (most recent call last):
File "", line 1, in
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/saving/save.py", line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 212, in load_model_from_hdf5
custom_objects=custom_objects)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/saving/model_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 89, in deserialize
printable_module_name='layer')
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 192, in deserialize_keras_object
list(custom_objects.items())))
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1121, in from_config
process_layer(layer_data)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1105, in process_layer
layer = deserialize_layer(layer_data, custom_objects=custom_objects)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 89, in deserialize
printable_module_name='layer')
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 194, in deserialize_keras_object
return cls.from_config(cls_config)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 446, in from_config
return cls(**config)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 477, in init
kernel_initializer=initializers.get(kernel_initializer),
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/initializers.py", line 194, in get
return deserialize(identifier)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/initializers.py", line 186, in deserialize
printable_module_name='initializer')
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 181, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "/home/eleven/ccd/tf12/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 166, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown initializer: EfficientConv2DKernelInitializer
Any way around?
What are recommendations for params for creating smaller then EfficientNetB0
models for 128x128 input and about 1kk params?
# Base EfficientNetB0()
# Total params: 5,330,564
# Trainable params: 5,288,548
# Non-trainable params: 42,016
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.