I am searching for a way to use Keras Model.predict() function in a sub-process.
I am using Keras 2.3.1 and TensorFlow 2.0.0. (I tried Keras 2.25 and TensorFlow 1.14)
The following code throws that error.
import itertools
import random
from abc import ABC
from multiprocessing import Pool as Pool
import numpy as np
from keras.engine.saving import load_model
from keras.models import Sequential
from keras.layers import Dense, Activation
class Pre(ABC):
pass
class Prediction(Pre):
def __init__(self):
model = Sequential([
Dense(32, input_shape=(2,)),
Activation('relu'),
Dense(2),
Activation('softmax'),
])
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0, 1], [1, 0], [1, 0], [0, 1]])
model.fit(x, y, epochs=20)
model.save("temp")
self.model = load_model('temp')
self.modifier = 2
def predict(self, input_array):
prediction = self.model.predict(np.array([input_array]))[0]
prediction += self.modifier
return prediction[0]
class B:
def __init__(self):
self.pred = Prediction()
def calculate_something(pred_inner: B, modifier: int):
pred_inner.modifier = modifier
sum_all = sum(pred_inner.pred.predict(np.array([random.choice([0, 1]), random.choice([0, 1])])) for _ in range(100))
# do some modifi
return (pred_inner,
sum_all)
if __name__ == '__main__':
probe_size = 100
pred = B()
for i in range(1000):
with Pool() as pool:
results = pool.starmap(calculate_something, zip(itertools.repeat(pred),
[probe_size for _ in range(i)]))
for r in results:
print(r[1])
Since I call the predict function in a sub-process it runs into a conflict with its own sub-process.
My Networks are very small so i think the multiprocessing is not strictly necessary is there any way to deactivate multiprocessing in Keras and TensorFlow?
Or is there another API i could use instead of Keras/TensorFlow?
Exception in thread Thread-24:
Traceback (most recent call last):
File "C:\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Python37\lib\multiprocessing\pool.py", line 470, in _handle_results
task = get()
File "C:\Python37\lib\multiprocessing\connection.py", line 251, in recv
return _ForkingPickler.loads(buf.getbuffer())
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\network.py", line 1334, in __setstate__
model = saving.unpickle_model(state)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\saving.py", line 604, in unpickle_model
return _deserialize_model(h5dict)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\saving.py", line 274, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\saving.py", line 627, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\layers\__init__.py", line 168, in deserialize
printable_module_name='layer')
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\sequential.py", line 302, in from_config
model.add(layer)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\sequential.py", line 162, in add
name=layer.name + '_input')
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\input_layer.py", line 178, in Input
input_tensor=tensor)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\engine\input_layer.py", line 87, in __init__
name=self.name)
File "C:\Users\phhor\PycharmProjects\py_doku\venv37\lib\site-packages\keras\backend\tensorflow_backend.py", line 73, in symbolic_fn_wrapper
if _SYMBOLIC_SCOPE.value:
Attrib
uteError: '_thread._local' object has no attribute 'value'
I found the problem!
If you create an object creating a Keras model and return it out of an subprocess you get this error.
If you delete the model before returning the object everything works fine.
def calculate_something(pred_inner: B, modifier: int):
pred_inner.modifier = modifier
sum_all = sum(pred_inner.pred.predict(np.array([random.choice([0, 1]), random.choice([0, 1])])) for _ in range(100))
del pred_inner.pred
# do some modifi
return (pred_inner,
sum_all)
Related
I am working with gpt2, python 3.9 and tensorflow 2.5 and when connecting to flask (flask run in terminal) I get a following message:
TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe'
Here is the code in generator.py
#!/usr/bin/env python3
import fire
import json
import os
import numpy as np
import tensorflow.compat.v1 as tf
# import model, sample, encoder
from text_generator import model
from text_generator import sample
from text_generator import encoder
class AI:
def generate_text(self, input_text):
model_name = '117M_Trained'
seed = None,
nsamples = 1
batch_size = 1
length = 150
temperature = 1
top_k = 40
top_p = 1
models_dir = 'models'
self.response = ''
models_dir = os.path.expanduser(os.path.expandvars(models_dir))
if batch_size is None:
batch_size = 1
assert nsamples % batch_size == 0
enc = encoder.get_encoder(model_name, models_dir)
hparams = model.default_hparams()
cur_path = os.path.dirname(__file__) + '/models' + '/' + model_name
with open(cur_path + '/hparams.json') as f:
hparams.override_from_dict(json.load(f))
if length is None:
length = hparams.n_ctx // 2
elif length > hparams.n_ctx:
raise ValueError("Can't get samples longer than window size: %s" % hparams.n_ctx)
with tf.Session(graph=tf.Graph()) as sess:
context = tf.placeholder(tf.int32, [batch_size, None])
np.random.seed(seed)
tf.set_random_seed(seed)
output = sample.sample_sequence(
hparams=hparams, length=length,
context=context,
batch_size=batch_size,
temperature=temperature, top_k=top_k, top_p=top_p
)
saver = tf.train.Saver()
ckpt = tf.train.latest_checkpoint(cur_path)
saver.restore(sess, ckpt)
context_tokens = enc.encode(input_text)
generated = 0
for _ in range(nsamples // batch_size):
out = sess.run(output, feed_dict={
context: [context_tokens for _ in range(batch_size)]
})[:, len(context_tokens):]
for i in range(batch_size):
generated += 1
text = enc.decode(out[i])
self.response = text
return self.response
ai = AI()
text = ai.generate_text('How are you?')
print(text)
Any help is appreciated 🙏 ps I have also added below the entire traceback
* Serving Flask app 'text_generator' (lazy loading)
* Environment: development
* Debug mode: on
2021-09-14 19:58:08.687907: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File "_mt19937.pyx", line 178, in numpy.random._mt19937.MT19937._legacy_seeding
TypeError: 'tuple' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/dusandev/miniconda3/bin/flask", line 8, in <module>
sys.exit(main())
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/flask/cli.py", line 990, in main
cli.main(args=sys.argv[1:])
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/flask/cli.py", line 596, in main
return super().main(*args, **kwargs)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/click/core.py", line 1062, in main
rv = self.invoke(ctx)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/click/core.py", line 1668, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/click/core.py", line 763, in invoke
return __callback(*args, **kwargs)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/click/decorators.py", line 84, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/click/core.py", line 763, in invoke
return __callback(*args, **kwargs)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/flask/cli.py", line 845, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/flask/cli.py", line 321, in __init__
self._load_unlocked()
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/flask/cli.py", line 346, in _load_unlocked
self._app = rv = self.loader()
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/flask/cli.py", line 402, in load_app
app = locate_app(self, import_name, name)
File "/Users/dusandev/miniconda3/lib/python3.9/site-packages/flask/cli.py", line 256, in locate_app
__import__(module_name)
File "/Users/dusandev/Desktop/AI/text_generator/__init__.py", line 2, in <module>
from .routes import generator
File "/Users/dusandev/Desktop/AI/text_generator/routes.py", line 2, in <module>
from .generator import ai
File "/Users/dusandev/Desktop/AI/text_generator/generator.py", line 74, in <module>
text = ai.generate_text('How are you?')
File "/Users/dusandev/Desktop/AI/text_generator/generator.py", line 46, in generate_text
np.random.seed(seed)
File "mtrand.pyx", line 244, in numpy.random.mtrand.RandomState.seed
File "_mt19937.pyx", line 166, in numpy.random._mt19937.MT19937._legacy_seeding
File "_mt19937.pyx", line 186, in numpy.random._mt19937.MT19937._legacy_seeding
TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe'
The problem is the line None, in your code. This is causing the tuple (None,) as the input to the np.random.seed(seed). It accepts integer, but you are sending the tuple.
I am trying to optimize self.parameters() with torch.optim.adam.Here is the original code:
def learning_rate_adjust(self, args):
self.learning_rate *= 0.8
self.train_op = torch.optim.Adam(self.parameters(), lr=self.learning_rate, weight_decay=args.l2_reg)
but when I run this code,here is the bug:
Traceback (most recent call last):
File "main.py", line 37, in <module>
train(args, loader, train_set_length, test_seq, item_attr_set, [], [])
File "E:\ACAM\0324ACAM-model-master\ACAM-model\code\train.py", line 62, in train
net.learning_rate_adjust(args)
File "E:\ACAM\0324ACAM-model-master\ACAM-model\code\model.py", line 160, in learning_rate_adjust
self.train_op = torch.optim.Adam(self.parameters(), lr=self.learning_rate, weight_decay=args.l2_reg)
File "D:\Anaconda3\envs\bio\lib\site-packages\torch\optim\adam.py", line 42, in __init__
super(Adam, self).__init__(params, defaults)
File "D:\Anaconda3\envs\bio\lib\site-packages\torch\optim\optimizer.py", line 51, in __init__
self.add_param_group(param_group)
File "D:\Anaconda3\envs\bio\lib\site-packages\torch\optim\optimizer.py", line 202, in
add_param_group
raise ValueError("can't optimize a non-leaf Tensor")
ValueError: can't optimize a non-leaf Tensor
so I try to see the list of self.parameters(),and i found list(self.parameters())[9] is a a non-leaf Tensor,then like some similar others' questions,i print the code like:
print('9',list(self.parameters())[9].requires_grad)
>>>true
print('9',list(self.parameters())[9].is_leaf)
>>>false
so i dont know how to change my code,please help me.T T
here is code about the model first:
class KAAM(torch.nn.Module):
def __init__(self, args, ent_embed, attr_embed, item_attr_set, loader, use_user_attn=True,use_item_attn=True, use_pretrain=True):
super(KAAM,self).__init__()
self.Super_parameter_construct(args, loader, use_user_attn, use_item_attn, use_pretrain)
self.variable_construct(args, ent_embed, item_attr_set, attr_embed)
self.optimizer(args)
for name, p in self.named_parameters():
if 'set' not in name:
p.requires_grad = True
then about opotimizer:
def optimizer(self,args):
self.loss_func = torch.nn.CrossEntropyLoss()
self.learning_rate, l2_reg = args.learning_rate, args.l2_reg
self.train_op = torch.optim.Adam(self.parameters(), lr=self.learning_rate, weight_decay=l2_reg)
when I train,for some k in my epoches,i will adjust learning rate(learning_rate_adjust) which is The problem code I asked first.
I got an error while loading my trained model. My model is about stereo vision, so I used both from tensorflow and keras( not tf.keras).
import numpy as np
import keras
import keras.layers as L
import keras.backend as B
import keras.backend.tensorflow_backend as B_
import keras.activations as A
import tensorflow as tf
Among the code lines I have this :
f_disp = 200
f_d = L.Conv2D(filters=f_disp, kernel_size=(3, 3), strides=1, padding='same')(f_uuu)
f_d = L.BatchNormalization()(f_d)
f_d = L.ReLU()(f_d)
f_d = L.Conv2D(filters=f_disp, kernel_size=(3, 3), strides=1, padding='same')(f_d)
f_d = L.BatchNormalization()(f_d)
f_d = L.ReLU()(f_d)
def disp_filter(shape, dtype=None):
kernel = (np.array(range(f_disp),dtype=np.float32))
kernel = np.tile(kernel,(shape[0]*shape[1]))
kernel = np.reshape(kernel,(shape[0],shape[1],f_disp,1))
print(shape,kernel.shape)
assert kernel.shape == shape
return tf.Variable(kernel,dtype=np.float32)
def sq(x):
sig = A.softmax(x,axis=-1)
f_f = L.Conv2D(filters = 1, kernel_size=(1,1),kernel_initializer=disp_filter,strides=1,padding='same',trainable=False)(sig)
sqx = B.squeeze(f_f,-1)
return sqx
f_sq = L.Lambda(sq)(f_d)
print(f_sq.shape)
return f_sq
Since my layer has more than 300 lines I took only this part.
However When I tried to load model, it outputs error message.
srst = tf.keras.models.load_model(route_weight, compile=True, custom_objects={'disp_filter':disp_filter,'A':keras.activations,'L':keras.layers,'B': keras.backend,'B_':keras.backend.tensorflow_backend,'tf':tf,'maxdsp':192})#,'A':keras.activations})
srst.summary()
This is my load_model code and :
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/eps/venv/test_srstereo.py", line 96, in <module>
srst = tf.keras.models.load_model(route_weight, compile=True, custom_objects={'disp_filter':disp_filter,'A':keras.activations,'L':keras.layers,'B': keras.backend,'B_':keras.backend.tensorflow_backend,'tf':tf,'maxdsp':192})#,'A':keras.activations})
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 168, in load_model_from_hdf5
custom_objects=custom_objects)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\saving\model_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py", line 102, in deserialize
printable_module_name='layer')
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 191, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 906, in from_config
config, custom_objects)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1852, in reconstruct_from_config
process_node(layer, node_data)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1799, in process_node
output_tensors = layer(input_tensors, **kwargs)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 842, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\tensorflow_core\python\keras\layers\core.py", line 795, in call
return self.function(inputs, **arguments)
File "C:\Users\Administrator\PycharmProjects\eps\venv\stereo_net.py", line 268, in sq
x2_u = L.Concatenate(axis=-1)([x2,x2_u])
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\keras\layers\convolutional.py", line 484, in __init__
**kwargs)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\keras\layers\convolutional.py", line 117, in __init__
self.kernel_initializer = initializers.get(kernel_initializer)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\keras\initializers.py", line 518, in get
return deserialize(config)
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\keras\initializers.py", line 510, in deserialize
printable_module_name='initializer')
File "C:\Users\Administrator\PycharmProjects\eps\venv\lib\site-packages\keras\utils\generic_utils.py", line 140, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown initializer: disp_filter
I cannot understand why the error message ValueError: Unknown initializer: disp_filter
occurs since I have never experienced this type of error when I made my network using CNN and backend only. Please tell me what I missed
I want to load a checkpoint from TensorFlow1 that consists of .index, .meta, .data-00000-of-00001 files into tensorflow2.0.0 and convert it to a keras model so to be able to use it natively in eager mode without need for tf.Session. Here is the code I ran:
import tensorflow as tf
import numpy as np
from tensorflow.python.keras.backend import set_session
from tensorflow.python.training.saver import _import_meta_graph_with_return_elements
def save_ckpt(ckpt_path='test'):
'''save TensorFlow-1 Checkpoint '''
with tf.Graph().as_default() as g:
in_op = tf.constant(np.random.rand(1,2,2,2),name='input',dtype=tf.float32)
out_op = tf.keras.layers.Conv2D(3,(3,3),padding='same',name='MY_LAYER')(in_op)
saver = tf.compat.v1.train.Saver()
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.variables_initializer(tf.compat.v1.global_variables()))
saver.save(sess,ckpt_path)
def load_ckpt():
'''KerasModel from meta & ckpt'''
in_op = tf.keras.Input([2,2,2])
_m = tf.keras.models.Model(inputs=in_op,outputs=in_op)
with _m.input.graph.as_default() as g:
saver, out_op = _import_meta_graph_with_return_elements('test.meta',
input_map={'input':_m.output},
return_elements=[
# 'input:0',
'MY_LAYER/Conv2D:0'
])
with tf.compat.v1.Session() as sess:
saver.restore(sess,'test')
set_session(sess)
out_mdl = tf.keras.models.Model(inputs=_m.input, outputs=out_op[0])
return out_mdl
# main
save_ckpt() # save name based checkpoint
meta_model = load_ckpt() # restore in keras model
oo = meta_model(np.random.rand(1,2,2,2)) # run the model
print(oo)
but I get this error:
Traceback (most recent call last):
File "question2.py", line 38, in <module>
meta_model = load_ckpt() # restore in keras model
File "question2.py", line 32, in load_ckpt
out_mdl = tf.keras.models.Model(inputs=_m.input, outputs=out_op[0])
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 146, in __init__
super(Model, self).__init__(*args, **kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 167, in __init__
self._init_graph_network(*args, **kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 270, in _init_graph_network
base_layer_utils.create_keras_history(self._nested_outputs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py", line 184, in create_keras_history
_, created_layers = _create_keras_history_helper(tensors, set(), [])
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py", line 229, in _create_keras_history_helper
constants[i] = backend.function([], op_input)([])
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py", line 3740, in __call__
outputs = self._graph_fn(*converted_inputs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1081, in __call__
return self._call_impl(args, kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1121, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1224, in _call_flat
ctx, args, cancellation_manager=cancellation_manager)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 511, in call
ctx=ctx)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable MY_LAYER/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/MY_LAYER/kernel)
[[node MY_LAYER/Conv2D/ReadVariableOp (defined at /home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_keras_scratch_graph_72]
Function call stack:
keras_scratch_graph
What I tried so far
Replacing MY_LAYER/Conv2D:0 with input:0 in _import_meta_graph_with_return_elements() makes the code run with no problem.
I am trying to load my saved keras model
model= tf.keras.models.load_model("my_model.h5",
custom_objects=None,
compile=True)
model.summary()
and getting the following error
Traceback (most recent call last):
File "C:\Users\admin\Desktop\phd python
projects\tensorflow_img_class\src\tensorflow ui.py", line 45, in <module>
compile=True
File "C:\Python37\lib\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 "C:\Python37\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 212, in load_model_from_hdf5
custom_objects=custom_objects)
File "C:\Python37\lib\site-packages\tensorflow\python\keras\saving\model_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Python37\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 89, in deserialize
printable_module_name='layer')
File "C:\Python37\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 192, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Python37\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 353, in from_config
model.add(layer)
File "C:\Python37\lib\site-packages\tensorflow\python\training\tracking\base.py", line 460, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Python37\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 174, in add
layer(x)
File "C:\Python37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 632, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Python37\lib\site-packages\tensorflow\python\keras\layers\core.py", line 782, in call
return self.function(inputs, **arguments)
File "C:/Users/admin/Desktop/phd python projects/tensorflow_img_class/src/tensorflow_img_class.py", line 35, in feature_extractor
feature_extractor_module = hub.Module(feature_extractor_url)
NameError: name 'feature_extractor_url' is not defined
More details are on this question. I have opened this post by following a suggestion from my previous linked post.
Code for this model is
image_generator = tf.compat.v1.keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
data_root = tf.compat.v1.keras.utils.get_file('Annotated_Image_Classes', 'https://github.com/PawanKaur/Viz-Image-Classification/tree/master/Annotated%20Image%20Classes.tqz',
untar=True)
feature_extractor_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/2"
def feature_extractor(x):
feature_extractor_module = hub.Module(feature_extractor_url)
return feature_extractor_module(x)
IMAGE_SIZE = hub.get_expected_image_size(hub.Module(feature_extractor_url))
image_data = image_generator.flow_from_directory(str(data_root), target_size=IMAGE_SIZE)
for image_batch,label_batch in image_data:
print("Image batch shape: ", image_batch.shape)
print("Label batch shape: ", label_batch.shape)
break
features_extractor_layer = layers.Lambda(feature_extractor, input_shape=IMAGE_SIZE+[3])
features_extractor_layer.trainable = False
model = tf.keras.Sequential([
features_extractor_layer,
layers.Dense(image_data.num_classes, activation='softmax')
])
model.summary()
sess = tf.compat.v1.keras.backend.get_session()
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
result = model.predict(image_batch)
result.shape
model.compile(
optimizer=tf.train.AdamOptimizer(),
loss='categorical_crossentropy',
metrics=['accuracy'])
class CollectBatchStats(tf.keras.callbacks.Callback):
def __init__(self):
self.batch_losses = []
self.batch_acc = []
def on_batch_end(self, batch, logs=None):
self.batch_losses.append(logs['loss'])
self.batch_acc.append(logs['acc'])
steps_per_epoch = image_data.samples//image_data.batch_size
batch_stats = CollectBatchStats()
model.fit((item for item in image_data), epochs=18,
steps_per_epoch=steps_per_epoch,
callbacks = [batch_stats])
model.save('my_model.h5')
Basically I have created this model by following transfer learning instructions from here. I am modelling this to run on my image data. After that I just need to open and view this pre-trained and saved model in another program but so far I am unable to do so. Any help will be appreciable.
just add
feature_extractor_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/2"
in your loading model script after import statments