mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

SoftmaxOutput same prediction for all samples

Open webrot9 opened this issue 9 years ago • 6 comments

Good morning, I am using mxnet 0.10.1 on Ubuntu 16.04, python (3) package.

I am currently experiencing a very weird situation. I am basically trying to learn the parameters for a cnn over a subset of imagenet. Specifically, I am targeting 11 classes + bounding boxes.

The network is structured as follows (top-down):


     graph = network(
                    n_hidden=[50, 30, 20,
                    hidden_activations=['relu', 'relu', None],
                    n_convolution_filters=[60, 90, 120, 90, 60],
                    convolution_activations=['relu', 'relu', 'relu', 'relu', 'relu'],
                    convolution_kernels=[(6, 6), (5, 5), (3, 3), (3, 3), (3, 3)],
                    normalize=[True, True, False, False, False],
                    pool_types=['max', 'max', None, None, 'max'],
                    pool_kernels=[(3, 3), (3, 3), (2, 2), (2, 2), (2, 2)],
                    obs_name='rgb_image')

    classes = mx.sym.slice_axis(graph, axis=1, begin=0, end=desired_classes)
    classes = mx.sym.SoftmaxOutput(data=classes, label=mx.sym.Variable('class_label'),
                                   preserve_shape=True, use_ignore=True, ignore_label=255)

    bounding_boxes = mx.sym.slice_axis(graph, axis=1, begin=desired_classes, end=20)
    bounding_boxes = network(bounding_boxes, n_hidden=[8, 4], hidden_activations=['sigmoid', 'sigmoid'])
    bounding_boxes = mx.sym.MAERegressionOutput(data=bounding_boxes, label=mx.sym.Variable('bbox_label'))

        output = mx.sym.Group([classes, bounding_boxes])

also, initialization and binding happen as follows:

    image_shape = (batch_size, 3, image_width, image_height)
    input_shapes = {'rgb_image': image_shape, 'class_label': (batch_size,),
                    'bbox_label': (batch_size, 4)}
    executor = output.simple_bind(ctx=context, grad_req='write', **input_shapes)

    param_initializer = mx.initializer.MSRAPrelu()

    data_args = ['rgb_image', 'class_label', 'bbox_label']
    param_names = executor.arg_dict.keys() - set(data_args)

    attrs = output.attr_dict()
    for key, array in executor.arg_dict.items():
        if key not in data_args:
            desc = mx.initializer.InitDesc(key, attrs.get(key, None))

            if 'bias' in key:
                array[:] = 0.01 * np.random.randn(*list(array.shape))
            else:
                param_initializer(desc, array)

Batches are prepared like this, with also normalization happening for the input:

   batch = {'rgb_image': np.zeros((batch_size, 3, image_width, image_height)),
                 'class_label': np.zeros(batch_size),
                 'bbox_label': np.zeros((batch_size, 4))}

    # data is a dict of image path, label and bbox
    for i, v in enumerate(data):
        try:
            image = imread(v['path'], flatten=False, mode='RGB')
            image = imresize(image, (image_width, image_height))
            image = np.array(image, dtype=np.float32)
            mean = np.array([123.68, 116.779, 103.939]).reshape(3, 1, 1)  # (R, G, B)

            batch['rgb_image'][i, :, :, :] = (image.reshape(3, image_width, image_height) - mean) / 128.
            batch['class_label'][i] = v['label']
            batch['bbox_label'][i, :] = np.array(v['bbox'])
        except OSError:
            batch['rgb_image'][i, :, :, :] = np.zeros((3, image_width, image_height))
            batch['class_label'][i] = 255
            batch['bbox_label'][i, :] = np.zeros(4)

Training happens as follows:

               optimizer = mx.optimizer.create('adam', learning_rate=0.01, rescale_grad=1. / batch_size, wd=0.0005)
               updater = mx.optimizer.get_updater(optimizer)

                for i in range(iterations):
                    print('Iteration {}'.format(i + 1))
                    model.forward(is_train=True, **train_inputs)
                    model.backward()

                    for key in param_names:
                        updater(key_index[key], model.grad_dict[key], model.arg_dict[key])

and the predict function is

    predict_shapes = {'rgb_image': inputs['rgb_image'].shape}
    predict_model = model.reshape(partial_shaping=True, allow_up_sizing=False, **predict_shapes)

    predictions = predict_model.forward(is_train=False, **inputs)

    return {'class_probabilities': predictions[0].asnumpy(), 'bounding_boxes': predictions[1].asnumpy()}

Also changing hyperparameters and layers, the network keeps converging to the same output

array([[ 0.27070844,  0.0657189 ,  0.00669123, ...,  0.00728226,
         0.01492491,  0.01282928],
       [ 0.27070844,  0.0657189 ,  0.00669123, ...,  0.00728226,
         0.01492491,  0.01282928],
       [ 0.27070844,  0.0657189 ,  0.00669123, ...,  0.00728226,
         0.01492491,  0.01282928],
       ..., 
       [ 0.27070844,  0.0657189 ,  0.00669123, ...,  0.00728226,
         0.01492491,  0.01282928],
       [ 0.27070844,  0.0657189 ,  0.00669123, ...,  0.00728226,
         0.01492491,  0.01282928],
       [ 0.27070844,  0.0657189 ,  0.00669123, ...,  0.00728226,
         0.01492491,  0.01282928]], dtype=float32)

I double checked the inputs and they are fine. I am hoping to find some help here.

Thanks

webrot9 avatar Jul 04 '17 10:07 webrot9

Just an update: I changed the learning rates and the wd in any possible combinations, without getting any improvement. I also changed the label to be provided as a one_hot vector (although this does not happen in any example I saw, and I am not sure on how that plays with the ignore_label).

Any ideas?

webrot9 avatar Jul 05 '17 18:07 webrot9

if possible, try to get the forward and back output of each layer, you can take example from here

hanpum avatar Jul 06 '17 02:07 hanpum

So, I simplified the network to understand what is going on. It looks like the slice_axis is doing something very weird:

Batch:     401 sem_labeling_flatten_0         0.411879	
Batch:     401 sem_labeling_fullyconnected_1_output 88.4677	
Batch:     401 sem_labeling_activation_1_output 70.1301	
Batch:     401 sem_labeling_fullyconnected_2_output 14538.9	
Batch:     401 slice_axis0_0                  14538.9	
Batch:     401 sem_labeling_class_out_output  0.281027	
Batch:     401 sem_labeling_class_out_backward_data 0.255512	
Batch:     401 sem_labeling_class_out_backward_label 0.0	
Batch:     401 slice_axis0_backward_0         0.255512	
Batch:     401 sem_labeling_fullyconnected_2_backward_data 0.0534117	
Batch:     401 sem_labeling_fullyconnected_2_backward_weight 73.1946	
Batch:     401 sem_labeling_fullyconnected_2_backward_bias 3.80981	
Batch:     401 sem_labeling_activation_1_backward_data 0.0341162	
Batch:     401 sem_labeling_fullyconnected_1_backward_data 0.388222	
Batch:     401 sem_labeling_fullyconnected_1_backward_weight 0.0897558	
Batch:     401 sem_labeling_fullyconnected_1_backward_bias 0.423309	
Batch:     401 sem_labeling_flatten_backward_0 0.388222	
Batch:     401 rgb_image                      0.411879	
Batch:     401 sem_labeling_fullyconnected_1_weight 0.111948	
Batch:     401 sem_labeling_fullyconnected_1_bias 0.701451	
Batch:     401 sem_labeling_fullyconnected_2_weight 0.38802	
Batch:     401 sem_labeling_fullyconnected_2_bias 0.657289	
Batch:     401 class_label                    0.288675	

The stat function I'm using is the default one:

def asum_stat(x):
   """returns |x|/size(x), async execution."""
   return ndarray.norm(x)/sqrt(x.size)

webrot9 avatar Jul 06 '17 12:07 webrot9

Actually, this seems to be independent from slice_axis. Here I removed it, but the top layer still is that big

Batch:     401 sem_labeling_fullyconnected_1_output 96.4277	
Batch:     401 sem_labeling_activation_1_output 76.1274	
Batch:     401 sem_labeling_fullyconnected_2_output 17178.2	
Batch:     401 sem_labeling_class_out_output  0.286579	
Batch:     401 sem_labeling_class_out_backward_data 0.367671	
Batch:     401 sem_labeling_class_out_backward_label 0.0	
Batch:     401 sem_labeling_fullyconnected_2_backward_data 0.0761676	
Batch:     401 sem_labeling_fullyconnected_2_backward_weight 1014.23	
Batch:     401 sem_labeling_fullyconnected_2_backward_bias 12.2497	
Batch:     401 sem_labeling_activation_1_backward_data 0.0466071	
Batch:     401 sem_labeling_fullyconnected_1_backward_data 0.494545	
Batch:     401 sem_labeling_fullyconnected_1_backward_weight 0.517794	
Batch:     401 sem_labeling_fullyconnected_1_backward_bias 1.10399	
Batch:     401 sem_labeling_flatten_backward_0 0.494545	
Batch:     401 rgb_image                      0.411878	
Batch:     401 sem_labeling_fullyconnected_1_weight 0.114631	
Batch:     401 sem_labeling_fullyconnected_1_bias 0.677352	
Batch:     401 sem_labeling_fullyconnected_2_weight 0.444761	
Batch:     401 sem_labeling_fullyconnected_2_bias 0.721972	
Batch:     401 class_label                    0.288675

EDIT: Just to clarify, at the beginning everything looks fine

Batch:       1 sem_labeling_fullyconnected_1_output 0.398348	
Batch:       1 sem_labeling_activation_1_output 0.281925	
Batch:       1 sem_labeling_fullyconnected_2_output 0.397358	
Batch:       1 sem_labeling_class_out_output  0.0912365	
Batch:       1 sem_labeling_class_out_backward_data 0.278354	
Batch:       1 sem_labeling_class_out_backward_label 0.0	
Batch:       1 sem_labeling_fullyconnected_2_backward_data 0.0393928	
Batch:       1 sem_labeling_fullyconnected_2_backward_weight 0.89977	
Batch:       1 sem_labeling_fullyconnected_2_backward_bias 4.07354	
Batch:       1 sem_labeling_activation_1_backward_data 0.027938	
Batch:       1 sem_labeling_fullyconnected_1_backward_data 0.0147067	
Batch:       1 sem_labeling_fullyconnected_1_backward_weight 0.0818928	
Batch:       1 sem_labeling_fullyconnected_1_backward_bias 0.346158	
Batch:       1 sem_labeling_flatten_backward_0 0.0147067	
Batch:       1 rgb_image                      0.301861	
Batch:       1 sem_labeling_fullyconnected_1_weight 0.0181442	
Batch:       1 sem_labeling_fullyconnected_1_bias 0.00999996	
Batch:       1 sem_labeling_fullyconnected_2_weight 0.0417773	
Batch:       1 sem_labeling_fullyconnected_2_bias 0.00999995	
Batch:       1 class_label                    0.288675

webrot9 avatar Jul 06 '17 13:07 webrot9

Any suggestions?

webrot9 avatar Jul 08 '17 08:07 webrot9

This issue is closed due to lack of activity in the last 90 days. Feel free to ping me to reopen if this is still an active issue. Thanks!

szha avatar Oct 08 '17 23:10 szha