zheng-da commented on a change in pull request #8302: Refactor operators & 
MKLDNN
URL: https://github.com/apache/incubator-mxnet/pull/8302#discussion_r164921462
 
 

 ##########
 File path: tests/python/gpu/test_gluon_model_zoo.py
 ##########
 @@ -0,0 +1,163 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import print_function
+import mxnet as mx
+import numpy as np
+import copy
+from mxnet import autograd
+from mxnet.gluon.model_zoo.vision import get_model
+from mxnet.test_utils import assert_almost_equal
+import sys
+
+def eprint(*args, **kwargs):
+    print(*args, file=sys.stderr, **kwargs)
+
+VAL_DATA='data/val-5k-256.rec'
+def download_data():
+    return mx.test_utils.download(
+        'http://data.mxnet.io/data/val-5k-256.rec', VAL_DATA)
+
+def test_inference():
+    all_models = ['resnet50_v1', 'vgg19_bn', 'alexnet', #'inceptionv3',
+                  'densenet201', 'squeezenet1.0', 'mobilenet0.25']
+
+    batch_size = 10
+    download_data()
+    for model_name in all_models:
+        eprint('testing inference on %s'%model_name)
+
+        data_shape = (3, 224, 224) if 'inception' not in model_name else (3, 
299, 299)
+        dataIter = mx.io.ImageRecordIter(
+            path_imgrec        = VAL_DATA,
+            label_width        = 1,
+            preprocess_threads = 1,
+            batch_size         = batch_size,
+            data_shape         = data_shape,
+            label_name         = 'softmax_label',
+            rand_crop          = False,
+            rand_mirror        = False)
+        data_batch = dataIter.next()
+        data = data_batch.data[0]
+        label = data_batch.label[0]
+        gpu_data = data.as_in_context(mx.gpu())
+        gpu_label = label.as_in_context(mx.gpu())
+
+        # This is to create a model and run the model once to initialize
+        # all parameters.
+        cpu_model = get_model(model_name)
+        cpu_model.collect_params().initialize(ctx=mx.cpu())
+        cpu_model(mx.nd.array(data, ctx=mx.cpu()))
+        gpu_model = get_model(model_name)
+        gpu_model.collect_params().initialize(ctx=mx.gpu())
+        gpu_model(mx.nd.array(data, ctx=mx.gpu()))
+
+        # Force the two models have the same parameters.
+        cpu_params = cpu_model.collect_params()
+        gpu_params = gpu_model.collect_params()
+        for k in cpu_params.keys():
+            k = k.replace(cpu_params.prefix, '')
+            cpu_param = cpu_params.get(k)
+            gpu_param = gpu_params.get(k)
+            gpu_param.set_data(cpu_param.data().as_in_context(mx.gpu()))
+
+        # Run inference.
+        with autograd.record(train_mode=False):
+            cpu_out = cpu_model(mx.nd.array(data, ctx=mx.cpu()))
+            gpu_out = gpu_model(gpu_data)
+        out = cpu_out.asnumpy()
+        max_val = np.max(out)
+        assert_almost_equal(out / max_val, gpu_out.asnumpy() / max_val, 
rtol=1e-2, atol=1e-2)
+
+def get_nn_model(name):
+    if "densenet" in name:
+        return get_model(name, dropout=0)
+    else:
+        return get_model(name)
+
+def test_training():
+    # We use network models without dropout for testing.
+    # TODO(zhengda) mobilenet can't pass this test even without MKLDNN.
+    all_models = ['resnet18_v1', 'densenet121']
+
+    batch_size = 10
+    label = mx.nd.random.uniform(low=0, high=10, 
shape=(batch_size)).astype('int32')
+
+    download_data()
+    dataIter = mx.io.ImageRecordIter(
+        path_imgrec        = VAL_DATA,
+        label_width        = 1,
+        preprocess_threads = 1,
+        batch_size         = batch_size,
+        data_shape         = (3, 224, 224),
+        label_name         = 'softmax_label',
+        rand_crop          = False,
+        rand_mirror        = False)
+    data_batch = dataIter.next()
+    data = data_batch.data[0]
+    label = data_batch.label[0]
+    gpu_data = data.as_in_context(mx.gpu())
+    gpu_label = label.as_in_context(mx.gpu())
+    softmax_cross_entropy = mx.gluon.loss.SoftmaxCrossEntropyLoss()
+
+    for model_name in all_models:
+        eprint('testing %s'%model_name)
+        #data = mx.nd.random.uniform(shape=(100, 3, 224, 224))
+
+        # This is to create a model and run the model once to initialize
+        # all parameters.
+        cpu_model = get_nn_model(model_name)
+        cpu_model.collect_params().initialize(ctx=mx.cpu())
+        cpu_model(mx.nd.array(data, ctx=mx.cpu()))
+        gpu_model = get_nn_model(model_name)
+        gpu_model.collect_params().initialize(ctx=mx.gpu())
+        gpu_model(mx.nd.array(data, ctx=mx.gpu()))
+
+        # Force the two models have the same parameters.
+        cpu_params = cpu_model.collect_params()
+        gpu_params = gpu_model.collect_params()
+        for k in cpu_params.keys():
+            k = k.replace(cpu_params.prefix, '')
+            cpu_param = cpu_params.get(k)
+            gpu_param = gpu_params.get(k)
+            gpu_param.set_data(cpu_param.data().as_in_context(mx.gpu()))
+
+        cpu_trainer = mx.gluon.Trainer(cpu_params, 'sgd', {'learning_rate': 
0.1})
+        gpu_trainer = mx.gluon.Trainer(gpu_params, 'sgd', {'learning_rate': 
0.1})
+
+        # Run forward and backward once.
+        with autograd.record():
+            cpu_out = cpu_model(mx.nd.array(data, ctx=mx.cpu()))
+            gpu_out = gpu_model(gpu_data)
+            cpu_loss = softmax_cross_entropy(cpu_out, label)
+            gpu_loss = softmax_cross_entropy(gpu_out, gpu_label)
+        assert_almost_equal(cpu_out.asnumpy(), gpu_out.asnumpy(), rtol=1e-2, 
atol=1e-2)
 
 Review comment:
   It seems there is a bug in CuDNNBatchNorm. Let's skip this for now.
   ```
   terminate called after throwing an instance of 'dmlc::Error'
     what():  [00:16:06] src/engine/./threaded_engine.h:359: [00:16:06] 
src/operator/nn/./cudnn/cudnn_batch_norm-inl.h:173: Check failed: ctx.is_train 
&& !param_.use_global_stats use global statistics is not yet supported in 
CuDNNBatchNorm```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to