Load a pretrained caffe model to lasagne? - python

I am trying to reproduce the Long-term Recurrent Convolutional Networks paper.
I have a pretrained caffe model that I'd like to use in theano.
I have the .caffemodel for this file, and the prototxt.
I have used the lasagne example to load the caffe weights to the caffe model.
This is the code I used, but the data is not loaded to the lasagne model.
I check it by using the lasagne.layers.get_all_param_values(net) command, which throws this error.
Traceback (most recent call last):
File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 2411, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 1802, in run
launch(file, globals, locals) # execute the script
File "/media/anilil/Data/charm/mv_clean/Vgg_las.py", line 218, in <module>
x=lasagne.layers.get_all_param_values(net)
File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 439, in get_all_param_values
params = get_all_params(layer, **tags)
File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 353, in get_all_params
return utils.unique(params)
File "/usr/local/lib/python2.7/dist-packages/lasagne/utils.py", line 157, in unique
for el in l:
File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 352, in <genexpr>
params = chain.from_iterable(l.get_params(**tags) for l in layers)
AttributeError: 'str' object has no attribute 'get_params'
TRIAL/Test Code:-
# -*- coding: utf-8 -*-
import os
import sys
import lasagne
from lasagne.layers import InputLayer
from lasagne.layers import DenseLayer
from lasagne.layers import NonlinearityLayer
from lasagne.nonlinearities import rectify
from lasagne.layers import DropoutLayer
from lasagne.layers import Pool2DLayer as PoolLayer
from lasagne.layers.dnn import Conv2DDNNLayer as ConvLayer
from lasagne.nonlinearities import softmax
import theano as T
from lasagne.layers import LocalResponseNormalization2DLayer as LRN
sys.path.append('/home/anilil/projects/lstm/lisa-caffe-public/python/')
import caffe
from lasagne.utils import floatX
import numpy as np
def build_model():
net = {}
# Input layer
net['input'] = InputLayer((None, 3, 227, 227))
# First Conv Layer
net['conv1'] = ConvLayer(net['input'], num_filters=96,filter_size=7, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
net['pool1'] = PoolLayer(net['conv1'], pool_size=3,stride=2,mode='max')
net['norm1'] = LRN(net['pool1'],alpha=0.0001,beta=0.75,n=5)
# 2nd Conv Layer
net['conv2'] = ConvLayer(net['norm1'], num_filters=384,filter_size=5, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
net['pool2'] = PoolLayer(net['conv2'], pool_size=3,stride=2,mode='max')
net['norm2'] = LRN(net['pool2'],alpha=0.0001,beta=0.75,n=5)
# 3rd Conv Layer
net['conv3'] = ConvLayer(net['norm2'], num_filters=512,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
net['conv4'] = ConvLayer(net['conv3'], num_filters=512,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
net['conv5'] = ConvLayer(net['conv4'], num_filters=384,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
net['pool5'] = PoolLayer(net['conv5'], pool_size=3,stride=2,mode='max')
net['fc6'] = DenseLayer(net['pool5'], num_units=4096,nonlinearity=rectify)
net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=4096)
net['fc7_dropout'] = DropoutLayer(net['fc7'], p=0.5)
net['fc8-ucf'] = DenseLayer(net['fc7_dropout'], num_units=101, nonlinearity=None)
net['prob'] = NonlinearityLayer(net['fc8-ucf'], softmax)
return net
if __name__=="__main__":
net = build_model()
#net= load_caffe_weights(net,'/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/deploy_singleFrame.prototxt','/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/singleframe_flow/snaps/snapshots_singleFrame_flow_v2_iter_50000.caffemodel')
caffe.set_device(0)
caffe.set_mode_gpu()
net_caffe = caffe.Net('/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/deploy_singleFrame.prototxt', '/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/singleframe_flow/snaps/snapshots_singleFrame_flow_v2_iter_50000.caffemodel', caffe.TEST)
layers_caffe = dict(zip(list(net_caffe._layer_names), net_caffe.layers))
for name, layer in net.items():
try:
layer.W.set_value(layers_caffe[name].blobs[0].data,borrow=True)
layer.b.set_value(layers_caffe[name].blobs[1].data,borrow=True)
except AttributeError:
continue
print ("Loaded the files without issues !!!!!!!!!!")
x=lasagne.layers.get_all_param_values(net)
print ("Saved Weights to the file without issues !!!!!!!!!!")

try:
x=lasagne.layers.get_all_param_values(net['prob'])
or make your net in this way:
def build_model():
net = {}
# Input layer
net = InputLayer((None, 3, 227, 227))
# First Conv Layer
net = ConvLayer(net, num_filters=96,filter_size=7, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
net = PoolLayer(net, pool_size=3,stride=2,mode='max')
....
net= NonlinearityLayer(net, softmax)
return net

Related

Keras: How to load a model with pretrained ELMO Layer

I have trained a Deep Learning network that has a pretrained ELMO layer. I've saved the model and weights using the code below.
model.save("model.h5")
model.save_weights("weights.h5")
I now need to load the load but I'm not sure whats the right way. I've tried two techniques and both of them fail.
1: Tried just loading the model but fails with a get_config error
import numpy as np
import io
import re
from tensorflow import keras
elmo_BiDirectional_model = keras.models.load_model("model.h5")
x_data = np.zeros((1, 1), dtype='object')
x_data[0] = "test token"
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
print( elmo_BiDirectional_model.predict(x_data) )
File "C:\temp\Simon\perdict_elmo.py", line 36, in
elmo_BiDirectional_model = keras.models.load_model("model.h5")
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\save.py",
line 143, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py",
line 159, in load_model_from_hdf5
raise ValueError('No model found in config file.')
ValueError: No model found in config file.
2: Tried building the model and just setting the weights:
import tensorflow_hub as hub
import tensorflow as tf
elmo = hub.Module("https://tfhub.dev/google/elmo/3", trainable=False)
from tensorflow.keras.layers import Input, Lambda, Bidirectional, Dense, Dropout, Flatten, LSTM
from tensorflow.keras.models import Model
def ELMoEmbedding(input_text):
return elmo(tf.reshape(tf.cast(input_text, tf.string), [-1]), signature="default", as_dict=True)["elmo"]
def build_model():
input_layer = Input(shape=(1,), dtype="string", name="Input_layer")
embedding_layer = Lambda(ELMoEmbedding, output_shape=(1024, ), name="Elmo_Embedding")(input_layer)
BiLSTM = Bidirectional(LSTM(128, return_sequences= False, recurrent_dropout=0.2, dropout=0.2), name="BiLSTM")(embedding_layer)
Dense_layer_1 = Dense(64, activation='relu')(BiLSTM)
Dropout_layer_1 = Dropout(0.5)(Dense_layer_1)
Dense_layer_2 = Dense(32, activation='relu')(Dropout_layer_1)
Dropout_layer_2 = Dropout(0.5)(Dense_layer_2)
output_layer = Dense(3, activation='sigmoid')(Dropout_layer_2)
model = Model(inputs=[input_layer], outputs=output_layer, name="BiLSTM with ELMo Embeddings")
model.summary()
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
elmo_BiDirectional_model = build_model()
elmo_BiDirectional_model.load_weights('weights.h5')
import numpy as np
import io
import re
from tensorflow import keras
x_data = np.zeros((1, 1), dtype='object')
x_data[0] = "test token"
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
print( elmo_BiDirectional_model.predict(x_data) )
But this failed with error:
File "C:\temp\Simon\perdict_elmo.py", line 28, in
elmo_BiDirectional_model.load_weights('weights.h5')
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 182, in load_weights
return super(Model, self).load_weights(filepath, by_name)
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\network.py",
line 1373, in load_weights
saving.load_weights_from_hdf5_group(f, self.layers)
File
"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py",
line 645, in load_weights_from_hdf5_group
original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'
Versions:
keras.__version__
'2.2.4-tf'
tensorflow.__version__
'1.15.0'
Finally! I had to downgrade two dependencies, then I used strategy #2 to load the weights into the model.
pip install astroid==2.3.0 --force-reinstall --user
pip install h5py==2.10.0 --force-reinstall --user

tf.keras.models.clone_model unknown activation: ReLU

The environment is Python 3.7.6, below is my import:
import os, sys
import tensorflow as tf # v2.2.0
tf.compat.v1.enable_eager_execution()
import numpy as np
import matplotlib.pyplot as plt
from sys import platform
import time
import random
import pickle
from tensorflow.keras.layers import ReLU
I tried to clone a tf.keras.Model without success, because ReLU is an unknown activation. Nevertheless, the initiation was successful, so the system should know what ReLU is. I wonder how to fix this.
def init_model(D=8, W=256):
# This is a simple MLP neural network
# D: The number of layers
# H: The neurons in each layer
relu = ReLU()
dense = lambda W=W, act=relu: tf.keras.layers.Dense(W, activation=act, dtype='float32')
inputs = tf.keras.Input(shape=(3 + 3 * 2 * L_embed))
outputs = inputs
for i in range(D):
outputs = dense()(outputs)
if i % 4 == 0 and i > 0:
outputs = tf.concat([outputs, inputs], -1)
outputs = dense(4, act=None)(outputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
Then I called:
model_inner_copy = tf.keras.models.clone_model(model)
The error message is:
File "D:/Code Repository/Meta-NeRF/Code/NeRF/Tiny_MAML_NeRF.py", line 211, in train_maml_nerf
model_inner_copy = tf.keras.models.clone_model(model)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 427, in clone_model
model, input_tensors=input_tensors, layer_fn=clone_function)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 196, in _clone_functional_model
model, new_input_layers, layer_fn)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 246, in _clone_layers_and_model_config
config = network.get_network_config(model, serialize_layer_fn=_copy_layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\network.py", line 2119, in get_network_config
layer_config = serialize_layer_fn(layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 243, in _copy_layer
created_layers[layer.name] = layer_fn(layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 61, in _clone_layer
return layer.__class__.from_config(layer.get_config())
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\base_layer.py", line 655, in from_config
return cls(**config)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\layers\core.py", line 1135, in __init__
self.activation = activations.get(activation)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\activations.py", line 465, in get
identifier, printable_module_name='activation')
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 362, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 321, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown activation: ReLU
So the problem is that tf.keras.layers.ReLU is a layer that implements the ReLU activation, but it is not an activation function by itself. It is meant to be used as a layer inside your model, not as a parameter of your Dense layer.
To have a function that works as an activation to give as a parameter to Dense, you should use tf.keras.activations.relu.

how do I solve OSError: [Errno 22] Invalid argument:

I am trying to use the following code to train Keras-I3D model from the following link:
https://github.com/srijandas07/i3d
imported modules are
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="3"
from keras.layers import Dense, Flatten, Dropout, Reshape
from keras import regularizers
from keras.preprocessing import image
from keras.models import Model, load_model
from keras.applications.vgg16 import preprocess_input
from keras.utils import to_categorical
from keras.optimizers import SGD
from i3d_inception import Inception_Inflated3d, conv3d_bn
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, CSVLogger, Callback
from keras.utils import Sequence, multi_gpu_model
import random
import sys
from multiprocessing import cpu_count
import numpy as np
import glob
from skimage.io import imread
import cv2
some definitions
epochs = str(sys.argv[0])
#epochs = 17
model_name = sys.argv[0]
#model_name = model_name
version = sys.argv[0]
num_classes = 35
batch_size = 16
stack_size = 64
DataLoader_video_train = DataLoader_video_train
DataLoader_video_test = DataLoader_video_test
class CustomModelCheckpoint(Callback):
def __init__(self, model_parallel, path):
super(CustomModelCheckpoint, self).__init__()
self.save_model = model_parallel
self.path = path
self.nb_epoch = 0
def on_epoch_end(self, epoch, logs=None):
self.nb_epoch += 1
self.save_model.save(self.path + str(self.nb_epoch) + '.hdf5')
i3d = i3d_modified(weights = 'rgb_imagenet_and_kinetics')
model = i3d.i3d_flattened(num_classes = num_classes)
optim = SGD(lr = 0.01, momentum = 0.9)
there is an issue here with the csvlogger
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor = 0.1, patience = 10)
csvlogger = CSVLogger('i3d_'+model_name+'.csv')
model.compile(loss = 'categorical_crossentropy', optimizer = optim, metrics = ['accuracy'])
model_checkpoint = CustomModelCheckpoint(model, './weights_'+model_name+'/epoch_')
train_generator = DataLoader_video_train('/train_CS.txt',version, batch_size = batch_size)
test_generator = DataLoader_video_test('/test_CS.txt', version, batch_size = batch_size)
fit generator
model.fit_generator(
generator = train_generator,
#validation_data=val_generator,
epochs = epochs,
steps_per_epoch = 17,
callbacks = [csvlogger, reduce_lr, model_checkpoint],
max_queue_size = 48,
workers = cpu_count() - 2,
use_multiprocessing = True,
)
print(model.evaluate_generator(generator = test_generator))
I get the following error
runfile('D:/Clones/i3d-master/i3d_train.py', wdir='D:/Clones/i3d-master')
Reloaded modules: i3d_inception
C:\Users\sancy\Anaconda3\lib\site-packages\keras\engine\training_generator.py:47: UserWarning: Using a generator with `use_multiprocessing=True` and multiple workers may duplicate your data. Please consider using the`keras.utils.Sequence class.
UserWarning('Using a generator with `use_multiprocessing=True`'
Traceback (most recent call last):
File "<ipython-input-30-8f7b9cc152d8>", line 1, in <module>
runfile('D:/Clones/i3d-master/i3d_train.py', wdir='D:/Clones/i3d-master')
File "C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/Clones/i3d-master/i3d_train.py", line 109, in <module>
use_multiprocessing = True,
File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\engine\training_generator.py", line 102, in fit_generator
callbacks.on_train_begin()
File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\callbacks.py", line 132, in on_train_begin
callback.on_train_begin(logs)
File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\callbacks.py", line 1183, in on_train_begin
**self._open_args)
OSError: [Errno 22] Invalid argument: 'i3d_D:/Clones/i3d-master/i3d_train.py.csv'
What am I doing wrong? how do you correctly use str(sys.argv[0]) and CSVLogger?
It looks like the 'i3d_'+ in the filename is causing an invalid filename in csvlogger = CSVLogger('i3d_'+model_name+'.csv'). Try removing the i3d_ prefix.

TensorFlow graph error in Estimator (ValueError: Tensor (...) must be from the same graph as Tensor(...))

UPDATE: Testing the same code with tensorflow-gpu 1.13.1 works both on my PC and on Google Cloud.
Using TensorFlow Estimator and running train_and_evaluate gives me the following error message:
"ValueError: Tensor("Const:0", shape=(3,), dtype=float32) must be from the same graph as Tensor("ParallelMapDataset:0", shape=(), dtype=variant, device=/device:CPU:0)." (see the full error output near bottom)
This happens when training the CNN on my PC with a GPU (GeForge RTX 2070). I am using Python 3.7 with tensorflow-gpu/tensorflow 1.14.0, Keras 2.2.4, running in a Conda environment.
It happens after the following log message "... Saving checkpoints for 2716 into C:/EstimatorOutput/10/model.ckpt." and appear to be when the input function for the evaluation step is being processed.
The code, as it is now, has run previously with no issue, but this has suddenly changed for reasons that are unclear to me.
I ran similar code on Google Cloud (which also previously ran fine), and the same problem occur (see error output near bottom; Run on GPU (BASIC_GPU); TensorFlow 1.14; Keras 2.2.4)
The error seems to be related to the evaluation step when the graph is created for some reason the new graph is not compatible.
Here is my code - >
My task module:
import tensorflow as tf
from train_model import model #("train_model" is local folder)
from train_model.model import create_estimator
if __name__ == '__main__':
model_num = 10
# Throw properties into params dict to pass to other functions
params = {}
params['train csv'] = "train_set_local.csv"
params['eval csv'] = "eval_set_local.csv"
params['output path'] = "C:/EstimatorOutput/" + str(model_num) + "/"
params['data path'] = "C:/Databases/Birds_dB/Images"
params['image size'] = [244, 224]
params["batch size"] = 16*2
params['use random flip'] = True
params['learning rate'] = 0.000001
params['dropout rate'] = 0.50
params['num classes'] = 123
params['train steps'] = 65000
params['eval steps'] = 20
params['eval_throttle_secs'] = 600
params['num parallel calls'] = 4
# Run the training job
model.go_train(params) # (See "go_train" below in model script ->)
My model module
import tensorflow as tf
from tensorflow.python.keras import estimator as kes
from tensorflow.python.keras.applications.vgg16 import VGG16
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dropout, Flatten, Dense
from train_model.input_fn import make_input_fn
def create_estimator(params):
# Import VGG16 model for transfer learning
base_model = VGG16(weights='imagenet')
base_model.summary()
x = base_model.get_layer('fc2').output
x = Dropout(params['dropout rate'])(x)
predictions = Dense(params['num classes'], activation="sigmoid", name="sm_out")(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in model.layers:
layer.trainable = True
model.compile(
loss="binary_crossentropy",
optimizer=tf.train.AdamOptimizer(params['learning rate'],
beta1=0.9,
beta2=0.999),
metrics=["categorical_accuracy"]
)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.95
run_config = tf.estimator.RunConfig(
session_config=config,
model_dir=params['output path']
)
# Convert to Estimator
estimator_model = kes.model_to_estimator(
keras_model=model,
config=run_config
)
return estimator_model
def go_train(params):
# Create the estimator
Est = create_estimator(params)
# Set up Estimator train and evaluation specifications
train_spec = tf.estimator.TrainSpec(
input_fn=make_input_fn(params['train csv'], tf.estimator.ModeKeys.TRAIN, params, augment=True),
max_steps=params['train steps']
)
eval_spec = tf.estimator.EvalSpec(
input_fn=make_input_fn(params['eval csv'], tf.estimator.ModeKeys.EVAL, params, augment=True),
steps=params['eval steps'], # Evaluates on "eval steps" batches
throttle_secs=params['eval_throttle_secs']
)
# Run training and evaluation
tf.estimator.train_and_evaluate(Est, train_spec, eval_spec)
My input module:
import tensorflow as tf
from keras.applications.vgg16 import preprocess_input
tf.logging.set_verbosity(v=tf.logging.INFO)
HEIGHT = 224
WIDTH = 224
NUM_CHANNELS = 3
NCLASSES = 123
def read_and_preprocess_with_augment(image_bytes, label=None):
return read_and_preprocess(image_bytes, label, augment=True)
def read_and_preprocess(image_bytes, label=None, augment=False):
image = tf.image.decode_jpeg(contents=image_bytes, channels=NUM_CHANNELS)
image = tf.image.convert_image_dtype(image=image, dtype=tf.float32) # 0-1
image = tf.expand_dims(input=image, axis=0) # resize_bilinear needs batches
if augment:
# Resize to slightly larger than target size
image = tf.image.resize_bilinear(images=image, size=[HEIGHT + 50, WIDTH + 50], align_corners=False)
# Image random rotation
degree_angle = tf.random.uniform((), minval=-25, maxval=25, dtype=tf.dtypes.float32)
radian = degree_angle * 3.14 / 180
image = tf.contrib.image.rotate(image, radian, interpolation='NEAREST')
# remove batch dimension
image = tf.squeeze(input=image, axis=0)
# Random Crop
image = tf.random_crop(value=image, size=[HEIGHT, WIDTH, NUM_CHANNELS])
# Random L-R flip
image = tf.image.random_flip_left_right(image=image)
# Random brightness
image = tf.image.random_brightness(image=image, max_delta=63.0 / 255.0)
# Random contrast
image = tf.image.random_contrast(image=image, lower=0.2, upper=1.8)
else:
image = tf.image.resize_bilinear(images=image, size=[HEIGHT, WIDTH], align_corners=False)
image = tf.squeeze(input=image, axis=0) # remove batch dimension
image = tf.cast(tf.round(image * 255), tf.int32)
image = preprocess_input(image)
label = tf.one_hot(tf.strings.to_number(label, out_type=tf.int32), depth=NCLASSES)
return {"input_1": image}, label
def make_input_fn(csv_of_filenames, mode, params, augment=False):
def _input_fn():
def decode_csv(csv_row):
filename, label = tf.decode_csv(records=csv_row, record_defaults=[[""], [""]])
image_bytes = tf.read_file(filename=filename)
return image_bytes, label
# Create tf.data.dataset from filename
dataset = tf.data.TextLineDataset(filenames=csv_of_filenames).map(map_func=decode_csv, num_parallel_calls=params['num parallel calls'])
if augment:
dataset = dataset.map(map_func=read_and_preprocess_with_augment, num_parallel_calls=params['num parallel calls'])
else:
dataset = dataset.map(map_func=read_and_preprocess, num_parallel_calls=params['num parallel calls'])
if mode == tf.estimator.ModeKeys.TRAIN:
num_epochs = None
dataset = dataset.shuffle(buffer_size=10*params["batch size"])
else:
num_epochs = 1
dataset = dataset.repeat(count=num_epochs).batch(batch_size=params["batch size"]).prefetch(4)
images, labels = dataset.make_one_shot_iterator().get_next()
return images, labels
return _input_fn
Error output on PC
As mentioned, the above code when running locally on my GPU results is this series of error messages(abbreviated):
Saving checkpoints for 2716 into ....
...
...
File "C:...\estimator.py", line 501, in _evaluate
self._evaluate_build_graph(input_fn, hooks, checkpoint_path))
File "C:...\estimator.py", line 1501, in _evaluate_build_graph
self._call_model_fn_eval(input_fn, self.config))
File "C:...\estimator.py", line 1534, in _call_model_fn_eval
input_fn, ModeKeys.EVAL)
File "C:...\estimator.py", line 1022, in _get_features_and_labels_from_input_fn
self._call_input_fn(input_fn, mode))
File "C:...\estimator.py", line 1113, in _call_input_fn
return input_fn(**kwargs)
File "C:...\input_fn.py", line 71, in _input_fn
dataset = dataset.map(map_func=read_and_preprocess_with_augment, num_parallel_calls=params['num parallel calls'])
File "C:...dataset_ops.py", line 1776, in map
self, map_func, num_parallel_calls, preserve_cardinality=False))
File "C:...\dataset_ops.py", line 3239, in init
**flat_structure(self))
File "C:...\gen_dataset_ops.py", line 4179, in parallel_map_dataset
name=name)
File "C:...\op_def_library.py", line 366, in _apply_op_helper
g = ops._get_graph_from_inputs(_Flatten(keywords.values()))
File "C:...\ops.py", line 6135, in _get_graph_from_inputs
_assert_same_graph(original_graph_element, graph_element)
File "C:...ops.py", line 6071, in _assert_same_graph
(item, original_item))
ValueError: Tensor("Const:0", shape=(3,), dtype=float32) must be from the same graph as Tensor("ParallelMapDataset:0", shape=(), dtype=variant, device=/device:CPU:0).
Error output on Google Cloud
service
The replica master 0 exited with a non-zero status of 1.
Traceback (most recent call last): [...]
File "/usr/local/lib/python3.5/dist-packages/tensorflow_estimator/python/estimator/estimator.py", line 1534, in _call_model_fn_eval input_fn, ModeKeys.EVAL)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_estimator/python/estimator/estimator.py", line 1022, in _get_features_and_labels_from_input_fn self._call_input_fn(input_fn, mode))
File "/usr/local/lib/python3.5/dist-packages/tensorflow_estimator/python/estimator/estimator.py", line 1113, in _call_input_fn return input_fn(**kwargs)
File "/root/.local/lib/python3.5/site-packages/train_model/input_fn.py", line 87, in _input_fn dataset = dataset.map(map_func=read_and_preprocess_with_augment, num_parallel_calls=params['num parallel calls'])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 1776, in map self, map_func, num_parallel_calls, preserve_cardinality=False))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 3239, in init **flat_structure(self)) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_dataset_ops.py", line 4179, in parallel_map_dataset name=name) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 366, in _apply_op_helper g = ops._get_graph_from_inputs(_Flatten(keywords.values()))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 6135, in _get_graph_from_inputs _assert_same_graph(original_graph_element, graph_element)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 6071, in _assert_same_graph (item, original_item))
ValueError: Tensor("Const_1:0", shape=(3,), dtype=float32, device=/device:CPU:0) must be from the same graph as Tensor("ParallelMapDataset:0", shape=(), dtype=variant, device=/device:CPU:0).
Any help/hint is much appreciated. I am stuck at this point and do not know how to debug this one!
use this preprocess function:
from tensorflow.keras.applications.mobilenet import preprocess_input
It has same functionality to that of VGGs preprocess input.

Converting Pytorch model to Keras model with MMdnn IR interface Model

I have downloaded a pretrained Pytorch cifar model from this link that it has a custom architecture. I want to convert this model to a Keras model. I know that there are some tools to do that. For example I can convert the Pytorch model to a standard model like onnx or IR.
Now I have used the IR interface model and the following code to load and save the entire model:
import torch
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
import torch as th
from collections import OrderedDict
class CIFAR(nn.Module):
def __init__(self, features, n_channel, num_classes):
super(CIFAR, self).__init__()
assert isinstance(features, nn.Sequential), type(features)
self.features = features
self.classifier = nn.Sequential(
nn.Linear(n_channel, num_classes)
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
def make_layers(cfg, batch_norm=False):
layers = []
in_channels = 3
for i, v in enumerate(cfg):
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
padding = v[1] if isinstance(v, tuple) else 1
out_channels = v[0] if isinstance(v, tuple) else v
conv2d = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=padding)
if batch_norm:
layers += [conv2d, nn.BatchNorm2d(out_channels, affine=False), nn.ReLU()]
else:
layers += [conv2d, nn.ReLU()]
in_channels = out_channels
return nn.Sequential(*layers)
n_channel=128
cfg = [n_channel, n_channel, 'M', 2*n_channel, 2*n_channel, 'M', 4*n_channel, 4*n_channel, 'M', (8*n_channel, 0), 'M']
layers = make_layers(cfg, batch_norm=True)
model = CIFAR(layers, n_channel=8*n_channel, num_classes=10)
pretrained=True
if pretrained:
m = th.load('MY_PATH/cifar10-d875770b.pth')
state_dict = m.state_dict() if isinstance(m, nn.Module) else m
assert isinstance(state_dict, (dict, OrderedDict)), type(state_dict)
model.load_state_dict(state_dict)
torch.save(model, 'MY_PATH/pytorch.pth')
Now it's time to convert the above Pytorch model pytorch.pth to IR model.
In the cmd I enter this command from the example of here:
mmtoir -f pytorch -d IRModel --inputShape 3,32,32 -n pytorch.pth
But this error appears:
Traceback (most recent call last):
File "c:\users\***\anaconda3\lib\site-packages\mmdnn\conversion\pytorch\pytorch_parser.py", line 76, in __init__
model = torch.load(model_file_name)
File "c:\users\***\anaconda3\lib\site-packages\torch\serialization.py", line 387, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
File "c:\users\***\anaconda3\lib\site-packages\torch\serialization.py", line 574, in _load
result = unpickler.load()
AttributeError: Can't get attribute 'CIFAR' on <module '__main__' from 'C:\\Users\\***\\Anaconda3\\Scripts\\mmtoir.exe\\__main__.py'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\***\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\***\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\***\Anaconda3\Scripts\mmtoir.exe\__main__.py", line 9, in <module>
File "c:\users\***\anaconda3\lib\site-packages\mmdnn\conversion\_script\convertToIR.py", line 192, in _main
ret = _convert(args)
File "c:\users\***\anaconda3\lib\site-packages\mmdnn\conversion\_script\convertToIR.py", line 92, in _convert
parser = PytorchParser(model, inputshape[0])
File "c:\users\***\anaconda3\lib\site-packages\mmdnn\conversion\pytorch\pytorch_parser.py", line 78, in __init__
model = torch.load(model_file_name, map_location='cpu')
File "c:\users\***\anaconda3\lib\site-packages\torch\serialization.py", line 387, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
File "c:\users\***\anaconda3\lib\site-packages\torch\serialization.py", line 574, in _load
result = unpickler.load()
AttributeError: Can't get attribute 'CIFAR' on <module '__main__' from 'C:\\Users\\***\\Anaconda3\\Scripts\\mmtoir.exe\\__main__.py'>
How can I solve that?
Even though it's been long since this question was asked, I'll share the answer.
The solution in my case was fairly simple, I had installed mmdnn in an anaconda env. And the file mmtoir was stored in the following location:
'user_name/anaconda3/envs/env_name/bin/mmtoir'
And there needed to be an explicit reference of my Neural Network class in this file. Meaning in your case, you need to:
1) Locate this file
2) Copy the definition of CIFAR class
class CIFAR(nn.Module):
def __init__(self, features, n_channel, num_classes):
super(CIFAR, self).__init__()
assert isinstance(features, nn.Sequential), type(features)
self.features = features
self.classifier = nn.Sequential(
nn.Linear(n_channel, num_classes)
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
into mmtoir file. Save it and run the command again.
And above is one of the two methods to solve this issue and is relatively simple. You can read this discussion for further details.
Note: MMdnn currently supports PyTorch=0.4.0 only. So make sure your model was trained on the same version as well. With PyTorch>0.4.0 all steps will run fine but will throw an error at the end.

Categories

Resources