Repository: pytorch
Issue: Incorrect lable read with ImageInput Op of Caffe2
Labels: caffe2,triaged

## 🐛 Bug
Incorrect label data on reading mnist lmdb with image_input operator. I see the same behavior with both C++ and python interface. mnist db read works as expected if I use TensorProtosDBInput operator.  
## To Reproduce

Steps to reproduce the behavior:

1. Please run the script below to reproduce. You will need to add full path to data_folder variable for mnist db 
<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

import os, sys
import shutil
from caffe2.python import core, model_helper, net_drawer, workspace, visualize, brew
from caffe2.python.helpers.tools import image_input


# This section preps your image and test set in a lmdb database
def DownloadResource(url, path):
  '''Downloads resources from s3 by url and unzips them to the provided path'''
  import requests, zipfile, StringIO
  print("Downloading... {} to {}".format(url, path))
  try:
    r = requests.get(url, stream=True)
  except Exception as ex:
    print("Failed to download dataset. Please download it manually from {}".format(url))
    print("Unzip it and place the two database folders here: {}".format(path))
    raise ex
  z = zipfile.ZipFile(StringIO.StringIO(r.content))
  z.extractall(path)
  print("Completed download and extraction.")



# If you would like to see some really detailed initializations,
# you can change --caffe2_log_level=0 to --caffe2_log_level=-1
core.GlobalInit(['caffe2', '--caffe2_log_level=0'])
print("Necessities imported!")
data_folder = "./"
root_folder = os.path.join("./",'tutorial_mnist')

if not os.path.exists(data_folder):
  os.makedirs(data_folder)   
  print("Your data folder was not found!! This was generated: {}".format(data_folder))
# Look for existing database: lmdb
if os.path.exists(os.path.join(data_folder,"mnist-train-nchw-lmdb")):
  print("lmdb train db found!")
else:
  db_missing = True

if os.path.exists(os.path.join(data_folder,"mnist-test-nchw-lmdb")):
  print("lmdb test db found!")
else:
  db_missing = True

if os.path.exists(root_folder):
  print("Looks like you ran this before, so we need to cleanup those old files...")
  shutil.rmtree(root_folder)

os.makedirs(root_folder)
workspace.ResetWorkspace(root_folder)

print("training data folder:" + data_folder)
print("workspace root folder:" + root_folder)

def AddInput(model, batch_size, db, db_type,useTensorProto=True):
    # load the data
  if useTensorProto:
    data_uint8, label = model.TensorProtosDBInput([], ["data_uint8", "label"], batch_size=batch_size,db=db, db_type=db_type)
  else:
    data_uint8, label = image_input(model,[],["data_uint8", "label"],batch_size=batch_size,
                        db=db, db_type=db_type, is_test=1,scale=28,color=0,crop=28,use_caffe_datum=0,label_type=0,
                        num_decode_threads=1)
  # cast the data to float
  data = model.Cast(data_uint8, "data", to=core.DataType.FLOAT)
  # scale data from [0,255] down to [0,1]
  data = model.Scale(data, data, scale=float(1./256))
  # don't need the gradient for the backward pass
  data = model.StopGradient(data, data)
  return data, label

def main(argv):
  arg_scope = {"order": "NCHW"}
  ## Load images using TensorProtoDBInput
  train_model = model_helper.ModelHelper(name="mnist_train-TensorProto", arg_scope=arg_scope)
  data, label = AddInput(
      train_model, batch_size=64,
      db=os.path.join(data_folder,"mnist-train-nchw-lmdb"),
      db_type='lmdb',useTensorProto=True)
  print(label)
  workspace.RunNetOnce(train_model.param_init_net)
  # creating the network
  workspace.CreateNet(train_model.net, overwrite=True)
  # Run Net
  workspace.RunNet(train_model.net)
  labelData=workspace.FetchBlob('label')
  print(labelData)

  ## Load images using ImageInput
  workspace.ResetWorkspace(root_folder)
  train_model = model_helper.ModelHelper(name="mnist_train-image_input", arg_scope=arg_scope)
  data, label = AddInput(
      train_model, batch_size=64,
      db=os.path.join(data_folder,"mnist-train-nchw-lmdb"),
      db_type='lmdb',useTensorProto=False)
  print(label)
  workspace.RunNetOnce(train_model.param_init_net)
  # creating the network
  workspace.CreateNet(train_model.net, overwrite=True)
  # Run Net
  workspace.RunNet(train_model.net)
  labelData=workspace.FetchBlob('label')
  print(labelData) 

  sys.exit(0)
  
if __name__ == '__main__':
  main(sys.argv[1:])

## Expected behavior 

Output on ruining the script. Bold label data is from Tensorprotodb op where as the italic label data is from image_input


E0909 14:56:37.372220 215429 init_intrinsics_check.cc:43] CPU feature avx is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
E0909 14:56:37.372248 215429 init_intrinsics_check.cc:43] CPU feature avx2 is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
E0909 14:56:37.372252 215429 init_intrinsics_check.cc:43] CPU feature fma is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
Necessities imported!
Looks like you ran this before, so we need to cleanup those old files...
training data folder:./
workspace root folder:./tutorial_mnist
**label
[5 0 4 1 9 2 1 3 1 4 3 5 3 6 1 7 2 8 6 9 4 0 9 1 1 2 4 3 2 7 3 8 6 9 0 5 6
 0 7 6 1 8 7 9 3 9 8 5 9 3 3 0 7 4 9 8 0 9 4 1 4 4 6 0]**
label
E0909 14:56:37.384630 215429 image_input_op.h:250] You are using an old ImageInputOp format that creates a local db reader. Consider moving to the new style that takes in a DBReader blob instead.
I0909 14:56:37.385756 215429 image_input_op.h:325] Creating an image input op with the following setting: 
I0909 14:56:37.385769 215429 image_input_op.h:326]     Using 4 CPU threads;
I0909 14:56:37.385772 215429 image_input_op.h:330]     Outputting in batches of 64 images;
I0909 14:56:37.385776 215429 image_input_op.h:331]     Treating input image as grayscale image;
I0909 14:56:37.385779 215429 image_input_op.h:345]     Scaling image to 28 without warping;
I0909 14:56:37.385782 215429 image_input_op.h:359]     Central cropping image to 28 without random mirroring;
I0909 14:56:37.385785 215429 image_input_op.h:362] Label Type: 0
I0909 14:56:37.385789 215429 image_input_op.h:363] Num Labels: 0
I0909 14:56:37.385792 215429 image_input_op.h:371]     Default [Channel 0] Subtract mean 0 and divide by std 1.
I0909 14:56:37.385809 215429 image_input_op.h:371]     Default [Channel 1] Subtract mean 0 and divide by std 1.
I0909 14:56:37.385813 215429 image_input_op.h:371]     Default [Channel 2] Subtract mean 0 and divide by std 1.
I0909 14:56:37.385821 215429 image_input_op.h:376]     Outputting images as unknown.
_[1953394531  543977330  543516788 1867525728 1869052268 1881170030
 1701867378 1701409906  168442483  538976288 1867524133 1866692972
 1667591276 1852795252  168457001 1970562386  175337074  757935405
  170732845 1867525728 1866692972 1667591276 1852795252  538970720
  541138976 1867525728 1866692972 1667591276 1852795252 1868767328
 1767994478 1735289198 1701344288 1869377568 1684370548 1819242528
 1852794745  168439411  543515987 1869835329  757935370  757935405
 1768294957 1650420844 1702327397  544763493 1766203450 1646292076
 1702327397 1948282469 1931505527  544437349 2015389295 1818326573
  779314549 1867385354  175334772  757935405  774769197 1869503264
  544433524 1952671091  544108393 1970365810]_


## Environment

Collecting environment information...
PyTorch version: 1.1.0a0+863818e
Is debug build: No
CUDA used to build PyTorch: 10.0.130

OS: Red Hat Enterprise Linux Server release 7.6 (Maipo)
GCC version: (GCC) 4.9.3
CMake version: version 2.8.12.2

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 10.0.130
GPU models and configuration: 
GPU 0: GeForce RTX 2080 Ti
GPU 1: GeForce RTX 2080 Ti
GPU 2: GeForce RTX 2080 Ti
GPU 3: GeForce RTX 2080 Ti

Nvidia driver version: 410.48
cuDNN version: Probably one of the following:
/appl/gpu-local.rhel.7.6/cuda-10.0/lib64/libcudnn.so.7
/usr/lib64/libcudnn.so.7.5.0

Versions of relevant libraries:
[pip3] numpy==1.16.0
[pip3] torch==1.1.0a0+863818e
[pip3] torchvision==0.2.1
[conda] Could not collect


## Additional context

<!-- Add any other context about the problem here. -->