apache / mxnet

Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more
https://mxnet.apache.org
Apache License 2.0
20.76k stars 6.8k forks source link

AssertionError / mod.forward(Batch(data = [mx.nd.array(img)])) #12372

Closed J4k3117 closed 6 years ago

J4k3117 commented 6 years ago

Description

I'm following this guide https://aws.amazon.com/de/blogs/machine-learning/building-an-autonomous-vehicle-part-4-using-behavioral-cloning-with-apache-mxnet-for-your-self-driving-car/ to train a model with the provided trainingsdata and the valid.rec. When I add the code from "Evaluation and simulator" part of this guide. Following Error occurs.

Error Message

AssertionError                            Traceback (most recent call last)
<ipython-input-7-fabdcf182ee5> in <module>()
     22     img = np.swapaxes(img, 1, 2)
     23     img = img[np.newaxis, :]
---> 24     mod.forward(Batch(data = [mx.nd.array(img)]))
     25     exp = mod.get_outputs()[0].asnumpy()[0]
     26     angle = 180*exp

/usr/local/lib/python2.7/dist-packages/mxnet/module/module.pyc in forward(self, data_batch, is_train)
    588             Default is ``None``, which means ``is_train`` takes the value of ``self.for_training``.
    589         """
--> 590         assert self.binded and self.params_initialized
    591 
    592         curr_data_shapes = tuple(i.shape for i in self._data_shapes)

AssertionError: 

Can somebody help me or has some advice? I'm not really expirenced with mxnet and don't know where to go from here now. I had more Problems with the code from this Guide and made some changes.

Code I used

import mxnet as mx
import numpy as np

data = mx.symbol.Variable(name="data")

body = mx.sym.Convolution(data=data, num_filter=24,  kernel=(5, 5), stride=(2,2)) 
body = mx.sym.Activation(data=body, act_type='relu', name='relu1')
body = mx.symbol.Pooling(data=body, kernel=(2, 2), stride=(2,2), pool_type='max')

body = mx.sym.Convolution(data=body, num_filter=32,  kernel=(5, 5), stride=(2,2))
body = mx.sym.Activation(data=body, act_type='relu')
body = mx.symbol.Pooling(data=body, kernel=(2, 2), stride=(2,2), pool_type='max')

flatten = mx.symbol.Flatten(data=body)

body = mx.symbol.FullyConnected(data=flatten, name='fc0', num_hidden=32)
body = mx.sym.Activation(data=body, act_type='relu', name='relu6')
body = mx.sym.Dropout(data=body, p=0.1)

body = mx.symbol.FullyConnected(data=body, name='fc1', num_hidden=16)
body = mx.sym.Activation(data=body, act_type='relu', name='relu7')

out = mx.symbol.FullyConnected(data=body, name='fc2', num_hidden=1)
out = mx.symbol.LinearRegressionOutput(data=out, name="softmax")

# Get Iterators

def get_iterators(batch_size, data_shape=(3, 120, 160)):
    train = mx.io.ImageRecordIter(
        path_imgrec         = 'train.rec', 
        data_name           = 'data',
        label_name          = 'softmax_label',
        batch_size          = batch_size,
        data_shape          = data_shape,
        shuffle             = True,
        rand_crop           = True,
        rand_mirror         = True)
    val = mx.io.ImageRecordIter(
        path_imgrec         = 'valid.rec',
        data_name           = 'data',
        label_name          = 'softmax_label',
        batch_size          = batch_size,
        data_shape          = data_shape,
        rand_crop           = False,
        rand_mirror         = False)
    return (train, val)

batch_size = 16
train_iter, val_iter = get_iterators(batch_size)

#Training

batch_size = 8
num_cpus = 2
num_epoch = 10
model_prefix = 'my-car2'
checkpoint = mx.callback.do_checkpoint(model_prefix)
mod = mx.mod.Module(out, context=[mx.cpu(i) for i in range(num_cpus)])
mod.fit(train_data=train_iter, 
        eval_data=val_iter, 
        eval_metric='mae', 
        optimizer='adam',
        optimizer_params={'learning_rate': 0.0001},
        num_epoch=num_epoch,
        epoch_end_callback = checkpoint,        
       )

import os   
import time
import mxnet as mx
import numpy as np
%matplotlib inline
from PIL import Image
from IPython import display
from collections import namedtuple
import matplotlib.patches as patches
import matplotlib.pyplot as plt

PATH = 'trainingdata/'
all_files = sorted(os.listdir(PATH))
sym, arg_params, aux_params = mx.model.load_checkpoint('my-car2', num_epoch)
mod = mx.mod.Module(symbol=sym) 
fig, ax = plt.subplots(1)
Batch = collections.namedtuple('Batch', ['data'])
for fname in all_files:
    org_img = Image.open(PATH + fname)    
    img = np.array(org_img)
    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 1, 2) 
    img = img[np.newaxis, :]
    mod.forward(Batch(data = [mx.nd.array(img)]))
    exp = mod.get_outputs()[0].asnumpy()[0]
    angle = 180*exp
    left = 80 * exp + 80
    rect = patches.Rectangle((left, 85),20,30, angle=angle,
                             linewidth=2,edgecolor='r',facecolor='none')
    patch = ax.add_patch(rect)
    display.clear_output(wait=True)
    display.display(plt.gcf())
    plt.imshow(org_img)
    time.sleep(0.1)
    patch.remove()

Package used (Python/R/Scala/Julia):

I'm using:

  1. Ubuntu 16.04 in OracleVM
  2. Python2.7
  3. Jupyter qtconsole
  4. Mxnet v1.2.1 with cpu and pip

Steps to reproduce

  1. Downloaded trainingdata and valid.rec in one folder.
  2. Created from trainingdata train.lst and train.rec.
    python im2rec.py /home/summer/summerws/train /home/summer/summerws/trainingdata/ --recursive --list --num-thread 8
    python im2rec.py /train /home/summer/summerws/trainingdata --recursive --pass-through --pack-label --num-thread 8
  3. Start Jupyter QtConsole from terminal inside of the folder with train.rec, valid.rec and trainingdata/
    jupyter qtconsole
  4. Copied my code inside the jupyter qtconsole

Thanks in advance for your help.

ankkhedia commented 6 years ago

@J4k3117 Thanks for trying it out. For how-to and usability questions, please use MXNet discuss forum - https://discuss.mxnet.io/ to involve wider community. We want to use Github issues mainly for bugs and feature request. Feel free to reopen if closed in error.

@sandeep-krishnamurthy Can you please close this issue? @mxnet-label-bot [Python, Question]

ankkhedia commented 6 years ago

@ThomasDelteil Could you please help with the question?

J4k3117 commented 6 years ago

@ankkhedia Thank you, I will post it on the forum.