Error converting pytorch model to coreml.
How do I convert a model with two inputs?
This model is style transfer.
var' was not implemented in the model, so it was searched and added. And an error occurred in 'var'.
var
#register_torch_op()
def var(context, node):
inputs = _get_inputs(context, node, expected=4)
x = inputs[0]
axes = inputs[1].val
print(inputs)
# Assert we can have biased divisor (N).
# An unbiased divisor (N - 1) would be much more complex,
# since we can't use reduce_mean. We therefore would need
# to otherwise do manual computation of the divisor for each axis.
assert (inputs[2].val == False)
keepdim = inputs[3].val
x_mean = mb.reduce_mean(x = x, axes = axes, keep_dims=keepdim)
x_sub_mean = mb.sub(x = x, y = x_mean)
x_sub_mean_square = mb.square(x = x_sub_mean)
x_var = mb.reduce_mean(x = x_sub_mean_square, axes = axes, keep_dims=keepdim)
context.add(x_var, torch_name=node.name)
code
import coremltools as ct
import urllib
import torch
import torch.nn as nn
import torchvision
from PIL import Image
from torchvision import transforms
from model import Model, AdaIN
class ConvertModel(nn.Module):
def __init__(self):
super(ConvertModel, self).__init__()
self.model = Model()
self.model.decoder.state_dict(torch.load('./models/decoder_3.pth', map_location='cpu'))
self.model.encoder
def forward(self, content, style, alpha=0.6):
content_f = self.model.encoder(content)
style_f = self.model.encoder(style)
feat = AdaIN(content_f, style_f)
feat = feat * alpha + content_f * (1 - alpha)
return self.model.decoder(feat)
model = ConvertModel()
dummy_input = torch.rand(1, 3, 512, 512)
traced_model = torch.jit.trace(model, (dummy_input, dummy_input))
input_content = ct.ImageType(name='content', shape=(1, 3, 512, 512),
bias=[-0.485/0.229, -0.456/0.224, -0.406/0.225], scale=1./(0.226*255.0))
input_style = ct.ImageType(name='style', shape=(1, 3, 512, 512),
bias=[-0.485/0.229, -0.456/0.224, -0.406/0.225], scale=1./(0.226*255.0))
coreml_model = ct.convert(traced_model, inputs=[input_content, input_style])
spec = coreml_model.get_spec()
ct.utils.rename_feature(spec, "current", "output")
coreml_model_updated = ct.models.MLModel(spec)
coreml_model_updated.save('style_transfer.mlmodel')
log
WARNING:root:Torch version 1.11.0 has not been tested with coremltools. You may run into unexpected errors. Torch 1.10.2 is the most recent version that has been tested.
Converting Frontend ==> MIL Ops: 54%|█████▎ | 197/367 [00:00<00:00, 2564.34 ops/s]
Traceback (most recent call last):
File "/Users/sinmugyeol/deeplearning/generator_model/style_transfer/convert2coreml.py", line 36, in <module>
coreml_model = ct.convert(traced_model, inputs=[input_content, input_style])
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/_converters_entry.py", line 352, in convert
mlmodel = mil_convert(
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 183, in mil_convert
return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 210, in _mil_convert
proto, mil_program = mil_convert_to_proto(
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 273, in mil_convert_to_proto
prog = frontend_converter(model, **kwargs)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 105, in __call__
return load(*args, **kwargs)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 47, in load
return _perform_torch_convert(converter, debug)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 84, in _perform_torch_convert
prog = converter.convert()
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 250, in convert
convert_nodes(self.context, self.graph)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 89, in convert_nodes
add_op(context, node)
File "/Users/sinmugyeol/opt/anaconda3/envs/core/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 256, in var
assert (inputs[2].val == False)
AssertionError
macOS Monterey 12.3.1 M1.
python 3.10.4.
pytorch 1.11.0.
coremltools 5.2.0.
numpy 1.21.5.
Related
I'm trying to load a saved Tensorflow ELMO model in a different function than I trained it, because I want to do multiple predictions with the model without having to train it every time. My (simplified) code is as follows:
(builder.py)
from word_classifier import train_word_classifier, predict_labels
def builder(lines):
train_word_classifier()
for lst in lines:
print('PRED_LABELS: ', predict_labels(lst))
(word_classifier.py)
import pandas as pd
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.python.keras import backend as K
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Model, Sequential, model_from_json
from tensorflow.keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional, Lambda, add, Input
def train_word_classifier():
"""
Input data preparation excluded for readability
"""
sess = tf.compat.v1.Session()
K.set_session(sess)
elmo_model = hub.Module("https://tfhub.dev/google/elmo/3", trainable=True)
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
input_text = Input(shape=(MAX_LEN,), dtype=tf.string)
def elmo_embedding(inData):
return \
elmo_model(inputs={"tokens": tf.squeeze(tf.cast(inData, tf.string)),
"sequence_len": tf.constant(BATCH_SIZE * [MAX_LEN])},
signature="tokens", as_dict=True)["elmo"]
embedding = Lambda(lambda text, : elmo_embedding(text), output_shape=(MAX_LEN, 1024))(input_text, )
x = Bidirectional(LSTM(units=LSTM_UNITS, return_sequences=LSTM_RETURN_SEQ,
recurrent_dropout=LSTM_RO_DROPOUT, dropout=LSTM_DROPOUT))(embedding)
x_rnn = Bidirectional(LSTM(units=LSTM_UNITS, return_sequences=LSTM_RETURN_SEQ,
recurrent_dropout=LSTM_RO_DROPOUT, dropout=LSTM_DROPOUT))(x)
x = add([x, x_rnn]) # residual connection to the first biLSTM
out = TimeDistributed(Dense(n_tags, activation="softmax"))(x)
model = Model(input_text, out)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
line_count_training_data = count_lines_in_file(CLASSIFIER_SENTENCE_FILE, 10)
size_train, size_test = get_count_for_batch_train_test_data(line_count_training_data)
print(size_train)
mode_dict = {
"train": size_train,
"test": size_test,
}
x_tr, x_val = x_tr[:mode_dict["train"] * BATCH_SIZE], x_tr[-mode_dict["test"] * BATCH_SIZE:]
y_tr, y_val = y_tr[:mode_dict["train"] * BATCH_SIZE], y_tr[-mode_dict["test"] * BATCH_SIZE:]
y_tr = y_tr.reshape(y_tr.shape[0], y_tr.shape[1], 1)
y_val = y_val.reshape(y_val.shape[0], y_val.shape[1], 1)
history = model.fit(np.array(x_tr),
y_tr,
validation_data=(np.array(x_val), y_val),
batch_size=BATCH_SIZE,
epochs=NUM_EPOCHS,
verbose=VERBOSE_VALUE)
model_json = model.to_json()
with open("resources/SavedModel/word_classifier/model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("resources/SavedModel/word_classifier/model.h5")
def predict_labels(input_data_list):
with open('resources/SavedModel/word_classifier/model.json', 'r') as json_file:
loaded_model_json = json_file.read()
def elmo_embedding(inData):
return \
elmo_model(inputs={"tokens": tf.squeeze(tf.cast(inData, tf.string)),
"sequence_len": tf.constant(BATCH_SIZE * [MAX_LEN])},
signature="tokens", as_dict=True)["elmo"]
loaded_model = tf.keras.models.model_from_json(loaded_model_json, custom_objects={'elmo_embedding': elmo_embedding})
# load weights into new model
loaded_model.load_weights("resources/SavedModel/word_classifier/model.h5")
print("Loaded model from disk")
In the end, after training the model, this gives me the error "TypeError: 'str' object is not callable" with the following traceback:
Traceback (most recent call last):
File "usc_coordinator.py", line 62, in <module>
run_usc_coordinator(fIn, fOut, mode)
File "usc_coordinator.py", line 32, in run_usc_coordinator
user_story_builder(fast_mode, file_in)
File "/home/ubuntu/PA/PA_AI4US/PythonVersion/src/builder.py", line 45, in builder
print('PRED_LABELS: ', predict_labels(lst))
File "/home/ubuntu/PA/PA_AI4US/PythonVersion/src/word_classifier.py", line 161, in predict_labels
loaded_model = tf.keras.models.model_from_json(loaded_model_json, custom_objects={'elmo_embedding': elmo_embedding})
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py", line 122, in model_from_json
return deserialize(config, custom_objects=custom_objects)
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py", line 171, in deserialize
return generic_utils.deserialize_keras_object(
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 354, in deserialize_keras_object
return cls.from_config(
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 616, in from_config
input_tensors, output_tensors, created_layers = reconstruct_from_config(
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1214, in reconstruct_from_config
process_node(layer, node_data)
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1162, in process_node
output_tensors = layer(input_tensors, **kwargs)
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 776, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py", line 903, in call
result = self.function(inputs, **kwargs)
File "/home/ubuntu/PA/PA_AI4US/PythonVersion/src/word_classifier.py", line 101, in <lambda>
embedding = Lambda(lambda text, : elmo_embedding(text), output_shape=(MAX_LEN, 1024))(input_text, )
TypeError: 'str' object is not callable
My versions are:
Python 3.8.10
Keras 2.3.0
Tensorflow 2.3.1
Tensorflow-hub 0.10.0
My guess is that the error is caused by the variable input_text that is set as a dtype tf.string. However, I don't how what to do about that without breaking the training sequence.
I hope that somebody can help!
It is a bug in tensorflow v2.3.1:
Loading a model with a Lambda layer causes a 'str' object is not callable exception #46659
https://github.com/tensorflow/tensorflow/issues/46659
You should change the string to something else like if I have to calculate 2+2, this would be the wrong code:
x = "2"
print(x + 2)
This shows:
Traceback (most recent call):
File "main.py", line 2, in <module>
print(x + 2)
TypeError: can only concatenate str (not "int") to str
So what do we do?
Convert it to something else
x = "2"
y = int(x)
print(y + 2)
Output: 4
int = integer,
float = float.
Edit: You can not convert some things to something else, like
x = "This is an example"
y = int(x)
This would show:
Traceback (most recent call last):
File "main.py", line 2, in <module>
y = int(x)
ValueError: invalid literal for int() with base10: 'This is an example'
#For a string to array
x = "1-2"
y = x.split("-")
print(y)
y would be ["1", "2"]
Or if you do not know short forms you can use full forms
Line #32,
'str' is not callable
fast_mode and file_In would be strings
And same for other lines
First of all, I'm running on a crappy laptop (Intel(R) Core(TM) i5-8300H CPU # 2.30GHz, 24 GB ram). I have a raspberry pi * 18 cluster that gives me the same response. I've spent the past 3-4 days just tinkering with the variables trying to figure out how to get this thing to work, but nothing I've done seems to have any affect.
First time poster, so if I made any mistakes, I apologize.
The script itself is
import csv
import gc
import os
import math
import numpy as np
import pandas as pd
from numba import njit
from numpy import sqrt, sin, cos, pi, zeros
from numpy.random import randn, rand, uniform, normal
from scipy.linalg import hadamard
import tensorflow as tf
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input, Dense, Activation, LSTM, Dropout, RepeatVector, TimeDistributed, Embedding, Reshape, Dot, Concatenate
from tensorflow.keras.layers import GRU, SpatialDropout1D, Conv1D, GlobalMaxPooling1D,Multiply, Lambda, Softmax, Flatten, BatchNormalization, Bidirectional, dot, concatenate
from tensorflow.keras.layers import AdditiveAttention, Attention
from tensorflow.keras.activations import relu
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import callbacks
from tensorflow.keras import backend
from tensorflow.keras.utils import plot_model
from tensorflow.keras.metrics import MeanSquaredError
from tensorflow.python.ops.tensor_array_ops import _EagerTensorArray
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
from tensorflow import keras
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.
print('Running on TPU ', tpu.master())
except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.experimental.TPUStrategy(tpu)
else:
strategy = tf.distribute.get_strategy()
inputfile = ('D:\\users\\rober\\downloads\\DGR.csv')
dgr = pd.read_csv(inputfile, index_col="DRAW DATE")
inputfile = open(inputfile)
csvreader = csv.DictReader(inputfile)
header = next(csvreader)
rows = []
inputai = []
outputai = []
for row in csvreader:
rows.append(row)
drawnumber = row.get("DRAW NUMBER")
year = (row.get("DRAW DATE")[0:4])
month = (row.get("DRAW DATE")[5:7])
day = (row.get("DRAW DATE")[8:10])
date = (year, month, day, drawnumber)
inputstuff = tf.TensorArray(np.array(date), size = 16, tensor_array_name = "Slibs")
inputai.append(inputstuff)
outputstuff = tf.TensorArray(np.array((row.get("NUMBER DRAWN 1"), row.get("NUMBER DRAWN 2"), row.get("NUMBER DRAWN 3"), row.get("NUMBER DRAWN 4"), row.get("NUMBER DRAWN 5"),row.get("NUMBER DRAWN 6"), row.get("BONUS NUMBER"))), size = 16)
outputai.append(outputstuff)
data = dgr.values
train = data[:-50]
test = data[-50:]
w = 10
X_train = []
y_train = []
print("X and Y trains created")
for i in range(w, len(train)):
X_train.append(train[i - w: i, :])
y_train.append(train[i])
X_train, y_train = np.array(X_train), np.array(y_train)
inputs = data[data.shape[0] - test.shape[0] - w:]
X_test = []
for i in range(w, inputs.shape[0]):
X_test.append(inputs[i - w: i, :])
X_test = np.array(X_test)
y_test = test
print(f"Data Shape = {data.shape}")
print(f"X_train.shape = {X_train.shape}")
print(f"y_train.shape = {y_train.shape}")
print(f"X_test.shape = {X_test.shape}")
print(f"y_test.shape = {y_test.shape}")
embed_dim = (59 // 2) + 1
dropout_rate = 0.5
spatial_dropout_rate = 0.5
steps_before = w
steps_after = 5
feature_count = embed_dim * 5
hidden_neurons = [64, 32]
bidirectional = True
attention_style = 'Bahdanau'
with strategy.scope():
inp0 = Input(shape = (w, X_train.shape[2]))
inp1 = Lambda(lambda x: x[:, :, 0])(inp0)
inp1 = Embedding(59, embed_dim)(inp1)
inp1 = SpatialDropout1D(spatial_dropout_rate)(inp1)
inp2 = Lambda(lambda x: x[:, :, 1])(inp0)
inp2 = Embedding(59, embed_dim)(inp2)
inp2 = SpatialDropout1D(spatial_dropout_rate)(inp2)
inp3 = Lambda(lambda x: x[:, :, 2])(inp0)
inp3 = Embedding(59, embed_dim)(inp3)
inp3 = SpatialDropout1D(spatial_dropout_rate)(inp3)
inp4 = Lambda(lambda x: x[:, :, 3])(inp0)
inp4 = Embedding(59, embed_dim)(inp4)
inp4 = SpatialDropout1D(spatial_dropout_rate)(inp4)
inp5 = Lambda(lambda x: x[:, :, 4])(inp0)
inp5 = Embedding(59, embed_dim)(inp5)
inp5 = SpatialDropout1D(spatial_dropout_rate)(inp5)
inp6 = Lambda(lambda x: x[:, :, 5])(inp0)
inp6 = Embedding(59, embed_dim)(inp6)
inp6 = SpatialDropout1D(spatial_dropout_rate)(inp6)
inp7 = Lambda(lambda x: x[:, :, 6])(inp0)
inp7 = Embedding(59, embed_dim)(inp7)
inp7 = SpatialDropout1D(spatial_dropout_rate)(inp7)
inp = Concatenate()([inp1, inp2, inp3, inp4, inp5, inp6, inp7])
num_layers = len(hidden_neurons)
sh_list, h_list, c_list = [inp], [], []
if bidirectional:
for i in range(num_layers):
sh, fh, fc, bh, bc = Bidirectional(LSTM(hidden_neurons[i],
dropout = dropout_rate,
return_state = True,
return_sequences = True))(sh_list[-1])
h = Concatenate()([fh, bh])
c = Concatenate()([fc, bc])
sh_list.append(sh)
h_list.append(h)
c_list.append(c)
else:
for i in range(num_layers):
sh, h, c = LSTM(hidden_neurons[i],
dropout = dropout_rate,
return_state = True,
return_sequences = True)(sh_list[-1])
sh_list.append(sh)
h_list.append(h)
c_list.append(c)
decoder = RepeatVector(steps_after)(h_list[-1])
if bidirectional:
decoder_hidden_neurons = [hn * 2 for hn in hidden_neurons]
else:
decoder_hidden_neurons = hidden_neurons
for i in range(num_layers):
decoder = LSTM(decoder_hidden_neurons[i],
dropout = dropout_rate,
return_sequences = True)(decoder, initial_state = [h_list[i], c_list[i]])
if attention_style == 'Bahdanau':
context = AdditiveAttention(dropout = dropout_rate)([decoder, sh_list[-1]])
decoder = concatenate([context, decoder])
elif attention_style == 'Luong':
context = Attention(dropout = dropout_rate)([decoder, sh_list[-1]])
decoder = concatenate([context, decoder])
out = Dense(59, activation = 'softmax')(decoder)
model = Model(inputs = inp0, outputs = out)
sparse_top_k = tf.keras.metrics.SparseTopKCategoricalAccuracy(k = 5, name = 'sparse_top_k')
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = [sparse_top_k])
model.load_weights('best_model.hdf5')
X_test=np.asarray(X_test).astype(int)
pred = model.predict(X_test)
pred = np.argmax(pred, axis = 2)
for i in range(y_test.shape[0]):
print('Prediction:\t', pred[i] + 1)
print('GoundTruth:\t', y_test[i] + 1)
print('-' * 40)
plot_model(model, show_shapes = True, show_layer_names = True, rankdir = 'TB', dpi = 60)
The error along with the shapes of the various relevant variables is as follows:
X and Y trains created
Data Shape = (661, 9)
X_train.shape = (601, 10, 9)
y_train.shape = (601, 9)
X_test.shape = (50, 10, 9)
y_test.shape = (50, 9)
Traceback (most recent call last):
File "C:\Users\rober\OneDrive\Documents\Python Stuff\csvpractice.py", line 210, in <module>
pred = model.predict(X_test)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'model/embedding/embedding_lookup' defined at (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\idlelib\run.py", line 163, in main
ret = method(*args, **kwargs)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\idlelib\run.py", line 566, in runcode
exec(code, self.locals)
File "C:\Users\rober\OneDrive\Documents\Python Stuff\csvpractice.py", line 210, in <module>
pred = model.predict(X_test)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1982, in predict
tmp_batch_outputs = self.predict_function(iterator)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1801, in predict_function
return step_function(self, iterator)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1790, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1783, in run_step
outputs = model.predict_step(data)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py", line 1751, in predict_step
return self(x, training=False)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\functional.py", line 451, in call
return self._run_internal_graph(
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\functional.py", line 589, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\rober\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\layers\embeddings.py", line 197, in call
out = tf.nn.embedding_lookup(self.embeddings, inputs)
Node: 'model/embedding/embedding_lookup'
indices[0,0] = 490 is not in [0, 59)
[[{{node model/embedding/embedding_lookup}}]] [Op:__inference_predict_function_7250]```
My thought is that this entire problem is probably caused by trying to squeeze too much data into the package that I'm sending through the prediction function, but I'm not entirely sure if that's the case or how to remedy it.
If anybody could offer me a solution, I would greatly appreciate it.
I am trying to use Pytorch grouped Conv2d operator on very large images (10k x 10k pixels). I am getting an RuntimeError: offset is too big error when trying to apply a grouped convolution in the network. Anyone knows how to circumvent this?
Code for reproducibility:
import torch
import torch.nn as nn
import pdb
def create_img(size, batch_size=1, channels=3):
return torch.FloatTensor(batch_size, channels, size, size).uniform_(-1, 1)
class TestModel(nn.Module):
def __init__(self):
super(TestModel, self).__init__()
self.conv1 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(3,3), stride=(1,1), groups=64, bias=False)
def forward(self, x):
out = self.conv1(x)
return out
if __name__ == '__main__':
model = TestModel()
data = create_img(5002, channels=64)
out = model(data)
pdb.set_trace()
and the error:
Traceback (most recent call last):
File "test.py", line 26, in <module>
out = model(data)
File ".../pipenv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "test.py", line 17, in forward
out = self.conv1(x)
File ".../pipenv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File ".../pipenv/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 320, in forward
self.padding, self.dilation, self.groups)
RuntimeError: offset is too big
I am using Python 3.6 and Pytorch 1.0.0. Strange thing is, this works with smaller images. Change the images size from 5002 to 502, for example.
Solved by updating Pytorch to 1.3.0
I am trying to train a neural network for it to find lines in images, the images are in bmp file format and grey-scale. After the network training phase has begun the program stops and outputs the corresponding error message.
This code is based entirely on 'Image Segmentation with tf.keras' by Raymond Yuan (https://ej.uz/hk9s) with changes in input pipeline, yet the model itself is exactly the same. I have tried redefining the shape of input to something else, yet every time I change something, the error message changes, but always happens at the same place.
#%%
import numpy as np
import matplotlib as mpl
import tensorflow as tf
from tensorflow.python.keras import layers
from tensorflow.python.keras import losses
from tensorflow.python.keras import models
#%%
#Defining paths to all the images
x_train_filenames = []
y_train_filenames = []
x_eval_filenames = []
y_eval_filenames = []
for x in range(250):
x_train_filenames.append(r'Train/Images/gen_{}_.bmp'.format(x))
y_train_filenames.append(r'Train/Labels/gen_{}_seg_.bmp'.format(x))
x_eval_filenames.append(r'Evaluate/Images/gen_{}_.bmp'.format(x))
y_eval_filenames.append(r'Evaluate/Labels/gen_{}_seg_.bmp'.format(x))
num_train_examples = len(x_train_filenames)
num_eval_examples = len(x_eval_filenames)
#%%
#Creating dataset from all of the pathnames.
img_shape = (3296, 3008, 1)
batch_size = 3
epochs = 5
threads = 5
def _process_pathnames(img_name, lbl_name):
img_str = tf.read_file(img_name)
img = tf.image.decode_bmp(img_str)
lbl_str = tf.read_file(lbl_name)
lbl = tf.image.decode_bmp(lbl_str)
return img, lbl
training_dataset = tf.data.Dataset.from_tensor_slices((x_train_filenames, y_train_filenames))
training_dataset = training_dataset.map(_process_pathnames, num_parallel_calls=threads)
training_dataset = training_dataset.shuffle(num_train_examples)
training_dataset = training_dataset.repeat().batch(batch_size)
evaluation_dataset = tf.data.Dataset.from_tensor_slices(((x_eval_filenames, y_eval_filenames)))
evaluation_dataset = evaluation_dataset.map(_process_pathnames, num_parallel_calls=threads)
evaluation_dataset = evaluation_dataset.shuffle(num_eval_examples)
evaluation_dataset = evaluation_dataset.repeat().batch(batch_size)
#%%
#Deining model
def conv_block(input_tensor, num_filters):
encoder = layers.Conv2D(num_filters, (3, 3), padding='same')(input_tensor)
encoder = layers.BatchNormalization()(encoder)
encoder = layers.Activation('relu')(encoder)
encoder = layers.Conv2D(num_filters, (3, 3), padding='same')(encoder)
encoder = layers.BatchNormalization()(encoder)
encoder = layers.Activation('relu')(encoder)
return encoder
def encoder_block(input_tensor, num_filters):
encoder = conv_block(input_tensor, num_filters)
encoder_pool = layers.MaxPooling2D((2, 2), strides=(2, 2))(encoder)
return encoder_pool, encoder
def decoder_block(input_tensor, concat_tensor, num_filters):
decoder = layers.Conv2DTranspose(num_filters, (2, 2), strides=(2, 2), padding='same')(input_tensor)
decoder = layers.concatenate([concat_tensor, decoder], axis=-1)
decoder = layers.BatchNormalization()(decoder)
decoder = layers.Activation('relu')(decoder)
decoder = layers.Conv2D(num_filters, (3, 3), padding='same')(decoder)
decoder = layers.BatchNormalization()(decoder)
decoder = layers.Activation('relu')(decoder)
decoder = layers.Conv2D(num_filters, (3, 3), padding='same')(decoder)
decoder = layers.BatchNormalization()(decoder)
decoder = layers.Activation('relu')(decoder)
return decoder
inputs = layers.Input(shape=img_shape)
encoder0_pool, encoder0 = encoder_block(inputs, 32)
encoder1_pool, encoder1 = encoder_block(encoder0_pool, 64)
encoder2_pool, encoder2 = encoder_block(encoder1_pool, 128)
encoder3_pool, encoder3 = encoder_block(encoder2_pool, 256)
encoder4_pool, encoder4 = encoder_block(encoder3_pool, 512)
center = conv_block(encoder4_pool, 1024)
decoder4 = decoder_block(center, encoder4, 512)
decoder3 = decoder_block(decoder4, encoder3, 256)
decoder2 = decoder_block(decoder3, encoder2, 128)
decoder1 = decoder_block(decoder2, encoder1, 64)
decoder0 = decoder_block(decoder1, encoder0, 32)
outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(decoder0)
model = models.Model(inputs=[inputs], outputs=[outputs])
#%%
#Defining custom loss functions
def dice_coeff(y_true, y_pred):
smooth = 1.
# Flatten
y_true_f = tf.reshape(y_true, [-1])
y_pred_f = tf.reshape(y_pred, [-1])
intersection = tf.reduce_sum(y_true_f * y_pred_f)
score = (2. * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)
return score
def dice_loss(y_true, y_pred):
loss = 1 - dice_coeff(y_true, y_pred)
return loss
def bce_dice_loss(y_true, y_pred):
loss = losses.binary_crossentropy(y_true, y_pred) + dice_loss(y_true, y_pred)
return loss
model.compile(optimizer='adam', loss=bce_dice_loss, metrics=[dice_loss])
save_model_path = '/tmp/weights.hdf5'
cp = tf.keras.callbacks.ModelCheckpoint(filepath=save_model_path, monitor='val_dice_loss', save_best_only=True, verbose=1)
#%%
#Training the model
history = model.fit(training_dataset,
steps_per_epoch=int(np.ceil(num_train_examples / float(batch_size))),
epochs=epochs,
validation_data=evaluation_dataset,
validation_steps=int(np.ceil(num_eval_examples / float(batch_size))),
callbacks=[cp])
Complete error message:
Traceback (most recent call last):
File "<ipython-input-19-f1dcac0996cd>", line 1, in <module>
runfile('//upb.lv/usr/profiles/Peteris.Zvejnieks/Desktop/Tests/Train data/A thing_retry.py', wdir='//upb.lv/usr/profiles/Peteris.Zvejnieks/Desktop/Tests/Train data')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "//upb.lv/usr/profiles/Peteris.Zvejnieks/Desktop/Tests/Train data/A thing_retry.py", line 159, in <module>
callbacks=[cp])
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 880, in fit
validation_steps=validation_steps)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 266, in model_iteration
batch_outs = f(actual_inputs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\backend.py", line 3076, in __call__
run_metadata=self.run_metadata)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
run_metadata_ptr)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
InvalidArgumentError: Number of channels must be 1, 3 or 4, was 0
[[{{node DecodeBmp_1}}]]
[[{{node IteratorGetNext_13}}]]
It appears that tensorflow.image.decode_bmp(...) is faulty, so I switched my project entirely to png, and that seems to work just fine.
As an added bonus I reduced the size of my data significantly, yet did`t loose any precious detail. However, now I understand, that what I am asking from my computer is extremely resource demanding.
I had the same exact situation but with the following error message:
InvalidArgumentError: `channels` must be 0, 3 or 4 for BMP, but got 1
[[{{node decode_image/DecodeImage}}]] [Op:IteratorGetNext]
Had to change 14k images from bmp to jpg.
But then it worked
I am trying to implement a Faster-RCNN model for object detection written by Yinghan Xu. After I have trained and saved the model with model_all.save('filename.h5'), I am trying to freeze the Keras model as TensorFlow graph (as .pb) for inference using keras_to_tensorflow.py written by Amir Abdi. But when I try to convert it, I get a ValueError: Unknown layer: roipoolingconv due to a custom RoiPoolingConv layer:
class RoiPoolingConv(Layer):
'''ROI pooling layer for 2D inputs.
See Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition,
K. He, X. Zhang, S. Ren, J. Sun
# Arguments
pool_size: int
Size of pooling region to use. pool_size = 7 will result in a 7x7 region.
num_rois: number of regions of interest to be used
# Input shape
list of two 4D tensors [X_img,X_roi] with shape:
X_img:
`(1, rows, cols, channels)`
X_roi:
`(1,num_rois,4)` list of rois, with ordering (x,y,w,h)
# Output shape
3D tensor with shape:
`(1, num_rois, channels, pool_size, pool_size)`
'''
def __init__(self, pool_size, num_rois, **kwargs):
self.dim_ordering = K.image_dim_ordering()
self.pool_size = pool_size
self.num_rois = num_rois
super(RoiPoolingConv, self).__init__(**kwargs)
def build(self, input_shape):
self.nb_channels = input_shape[0][3]
def compute_output_shape(self, input_shape):
return None, self.num_rois, self.pool_size, self.pool_size, self.nb_channels
def call(self, x, mask=None):
assert(len(x) == 2)
# x[0] is image with shape (rows, cols, channels)
img = x[0]
# x[1] is roi with shape (num_rois,4) with ordering (x,y,w,h)
rois = x[1]
input_shape = K.shape(img)
outputs = []
for roi_idx in range(self.num_rois):
x = rois[0, roi_idx, 0]
y = rois[0, roi_idx, 1]
w = rois[0, roi_idx, 2]
h = rois[0, roi_idx, 3]
x = K.cast(x, 'int32')
y = K.cast(y, 'int32')
w = K.cast(w, 'int32')
h = K.cast(h, 'int32')
# Resized roi of the image to pooling size (7x7)
rs = tf.image.resize_images(img[:, y:y+h, x:x+w, :], (self.pool_size, self.pool_size))
outputs.append(rs)
final_output = K.concatenate(outputs, axis=0)
# Reshape to (1, num_rois, pool_size, pool_size, nb_channels)
# Might be (1, 4, 7, 7, 3)
final_output = K.reshape(final_output, (1, self.num_rois, self.pool_size, self.pool_size, self.nb_channels))
# permute_dimensions is similar to transpose
final_output = K.permute_dimensions(final_output, (0, 1, 2, 3, 4))
return final_output
def get_config(self):
config = {'pool_size': self.pool_size,
'num_rois': self.num_rois}
base_config = super(RoiPoolingConv, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
I have looked at most of the resources out there and almost all of them suggest to comment out this layer. But since this layer is important for object detection, I was wondering if a workaround is possible or not.
The complete traceback of error (note: I've saved filename as freezekeras.py, contents are same as keras_to_tensorflow.py):
Using TensorFlow backend.
Traceback (most recent call last):
File "freezekeras.py", line 181, in <module>
app.run(main)
File "/usr/local/lib/python3.5/dist-packages/absl/app.py", line 300, in run
_run_main(main, args)
File "/usr/local/lib/python3.5/dist-packages/absl/app.py", line 251, in _run_main
sys.exit(main(argv))
File "freezekeras.py", line 127, in main
model = load_model(FLAGS.input_model, FLAGS.input_model_json, FLAGS.input_model_yaml)
File "freezekeras.py", line 105, in load_model
raise wrong_file_err
File "freezekeras.py", line 62, in load_model
model = keras.models.load_model(input_model_path)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1022, in from_config
process_layer(layer_data)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown layer: RoiPoolingConv
Try to specify the custom layer explicitly:
model = load_model('my_model.h5', custom_objects={'RoiPoolingConv': RoiPoolingConv})
Obviously, you have to re-write the keras_to_tensorflow.py script. See Handling custom layers (or other custom objects) in saved models section under Keras FAQ.
Solution
specify custom layer while loading model in keras_to_tensorflow.py
model = keras.models.load_model(input_model_path, custom_objects={'RoiPoolingConv':RoiPoolingConv})
import RoiPoolingConv.py to keras_to_tensorflow project
specify default pool_size, num_rois for RoiPoolingConv
def __init__(self, pool_size = 7, num_rois = 32, **kwargs):