I am trying to save and load the architecture and params file, but facing this below error. Please, can anyone help me solve this? I train on linux, inference on Linux
Error:
Traceback (most recent call last):
File "CNN.py", line 137, in <module>
deserialized_net.load_parameters('modelConvLSTM-0010.params', ctx=ctx)
File "/home/test/anaconda3/lib/python3.7/site-packages/mxnet/gluon/block.py", line 386, in load_parameters
filename, ctx, allow_missing, ignore_extra, self.prefix)
File "/home/test/anaconda3/lib/python3.7/site-packages/mxnet/gluon/parameter.py", line 911, in load
name[lprefix:], filename, _brief_print_list(arg_dict.keys()))
**AssertionError: Parameter 'data0' is missing in file 'modelConvLSTM-0010.params', which contains parameters: 'net0_conv_lstm0_i2h_weight', 'net0_conv_lstm0_h2h_weight', 'net0_conv_lstm0_i2h_bias', ..., 'net0_dense0_weight', 'net0_dense0_bias', 'net0_dense1_weight', 'net0_dense1_bias'. Please make sure source and target networks have the same prefix.**
Code:
class Net(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Net, self).__init__(**kwargs)
with self.name_scope():
self.encoder = gluon.rnn.HybridSequentialRNNCell()
self.encoder.add(mx.gluon.contrib.rnn.Conv3DLSTMCell(input_shape=(1,q, rows, columns),hidden_channels=64, activation='relu',i2h_kernel=(5,5,5),i2h_pad=(2,2,2), h2h_kernel=(5,5,5),conv_layout='NCDHW'))
self.encoder.add(mx.gluon.contrib.rnn.Conv3DLSTMCell(input_shape=(64,q, rows, columns),hidden_channels=32, activation='relu',i2h_kernel=(3,3,3),i2h_pad=(1,1,1), h2h_kernel=(3,3,3),conv_layout='NCDHW'))
self.middle = gluon.nn.HybridSequential()
self.middle.add(mx.gluon.nn.BatchNorm())
self.middle.add(mx.gluon.nn.Dropout(dropout))
self.decoder = gluon.nn.HybridSequential()
self.decoder.add(mx.gluon.nn.Dense(10,flatten=True))
self.decoder.add(mx.gluon.nn.Dense(1))
def hybrid_forward(self, F, x,states=None):
x,states= self.encoder(x, states)
x= self.middle(x)
x = self.decoder(x)
return x,states
def evaluate_accuracy(model, dataloader):
eval_metrics_1 = mx.metric.MAE()
eval_metrics_2 = mx.metric.MSE()
eval_metrics_3 = mx.metric.RMSE()
eval_metrics = mx.metric.CompositeEvalMetric()
for child_metric in [eval_metrics_1, eval_metrics_2, eval_metrics_3]:
eval_metrics.add(child_metric)
for i, (data, label) in enumerate(dataloader):
data = data.as_in_context(ctx)
label = label.as_in_context(ctx)
states =model.encoder.begin_state( batch_size=data.shape[0],ctx=ctx)
preds,states=model(data,states)
eval_metrics.update(labels=label, preds=preds)
data.as_in_context(mx.cpu(0))
label.as_in_context(mx.cpu(0))
return eval_metrics.get()
def fit(model):
train_loss = []
val_loss = []
for e in range(epochs):
tick = time.time()
for i, (data, label) in enumerate(train_iter):
data = data.as_in_context(ctx)
label = label.as_in_context(ctx)
states =model.encoder.begin_state( batch_size=data.shape[0],ctx=ctx)
with autograd.record():
Y_pred,states= model(data,states)
loss =loss1(Y_pred, label)
loss.backward()
trainer.step(batch_size)
if i == 0:
moving_loss = nd.mean(loss).asscalar()
else:
moving_loss = .99 * moving_loss + .01 * nd.mean(loss).asscalar()
train_loss.append(moving_loss)
val_err = evaluate_accuracy(model,val_iter)
val_loss.append(val_err[1])
print("Epoch %s. Loss: %.10f Test MAE: %s time:%.2f s" % (e, moving_loss, val_err, time.time()-tick))
return train_loss, val_loss, model
ctx = mx.gpu()
net=Net()
net.collect_params().initialize(mx.init.Xavier(), ctx=ctx)
loss1 = mx.gluon.loss.L1Loss()
trainer = mx.gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': lr})
print(" Loss before training: ", evaluate_accuracy(net, test_iter))
t_loss,v_loss, net=fit(net)
net.hybridize()
test=evaluate_accuracy(net, test_iter)
print("Test : ", test)
print net params give:
net0_ (
Parameter net0_conv_lstm0_i2h_weight (shape=(256, 1, 5, 5, 5), dtype=<class 'numpy.float32'>)
Parameter net0_conv_lstm0_h2h_weight (shape=(256, 64, 5, 5, 5), dtype=<class 'numpy.float32'>)
Parameter net0_conv_lstm0_i2h_bias (shape=(256,), dtype=<class 'numpy.float32'>)
Parameter net0_conv_lstm0_h2h_bias (shape=(256,), dtype=<class 'numpy.float32'>)
Parameter net0_conv_lstm1_i2h_weight (shape=(128, 64, 3, 3, 3), dtype=<class 'numpy.float32'>)
Parameter net0_conv_lstm1_h2h_weight (shape=(128, 32, 3, 3, 3), dtype=<class 'numpy.float32'>)
Parameter net0_conv_lstm1_i2h_bias (shape=(128,), dtype=<class 'numpy.float32'>)
Parameter net0_conv_lstm1_h2h_bias (shape=(128,), dtype=<class 'numpy.float32'>)
Parameter net0_batchnorm0_gamma (shape=(32,), dtype=<class 'numpy.float32'>)
Parameter net0_batchnorm0_beta (shape=(32,), dtype=<class 'numpy.float32'>)
Parameter net0_batchnorm0_running_mean (shape=(32,), dtype=<class 'numpy.float32'>)
Parameter net0_batchnorm0_running_var (shape=(32,), dtype=<class 'numpy.float32'>)
Parameter net0_dense0_weight (shape=(10, 812544), dtype=float32)
Parameter net0_dense0_bias (shape=(10,), dtype=float32)
Parameter net0_dense1_weight (shape=(1, 10), dtype=float32)
Parameter net0_dense1_bias (shape=(1,), dtype=float32)
)
to load the JSON and params file I use:
sym = mx.sym.load_json(open('modelConvLSTM-symbol.json', 'r').read())
deserialized_net = gluon.nn.SymbolBlock(outputs=sym, inputs=mx.sym.var('data'))
deserialized_net.load_parameters('modelConvLSTM-0010.params', ctx=ctx)
test=evaluate_accuracy(deserialized_net, test_iter)
print("Test : ", test)
Please help me to find out where am I going wrong here?
Thank you.