What's wrong with my code? (Solved)

Provide details on the platforms you are using:
OS : Linux 16.04
GPU : GTX 1080ti
nvidia driver version : 384.130
CUDA version : 9.0
CUDNN version : 7.3.1
Python version : 3.5.2
Tensorflow version : 1.11.0
TensorRT version : 5.0.0.10

Describe the problem
I got poor accuracy when i use conv layer with keras. I thought it cause from data format.
so I set my network’s input and data format set to be CHW.
But still bad…

Blow is my code for data.
I have MNIST data(28x28 grayscale image)

def load_testdata(data_path):
images = dict()
for (path, dir, files) in os.walk(data_path):
for file in files:
ext = os.path.splitext(file)[-1]

        if ext == '.png':
            img_path = path+'/'+file
            img = Image.open(img_path)
            label = img_path.replace('/'+file, '').split('/')[-1]
            np_img = np.array(img, dtype=np.float32, order='C')
            np_img = np.expand_dims(np_img, axis=0)

            if not label in images:
                images[label] = list()
                images[label].append(np_img)
            else:
                images[label].append(np_img)

return images

def copy_image_to_host_mem(img, pagelock_memory):
img = img / 255.0
img = img.ravel()
np.copyto(pagelock_memory, img)

Engine

======================================================
def build_engine(model_file, input_names, output_names):
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser:
builder.max_workspace_size = GiB(3)
# builder.max_batch_size = 1
# only support CHW format
parser.register_input(input_names, (1, 28, 28))
parser.register_output(output_names)
parser.parse(model_file, network)
return builder.build_cuda_engine(network)

Network

======================================================
def create_vgg_style():
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation=‘relu’,
input_shape=(1, 28, 28), padding=‘same’, data_format=‘channels_first’))
model.add(keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation=‘relu’, padding=‘same’))
model.add(keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(keras.layers.Conv2D(filters=128, kernel_size=(3, 3), activation=‘relu’, padding=‘same’))
model.add(keras.layers.Conv2D(filters=128, kernel_size=(3, 3), activation=‘relu’, padding=‘same’))
model.add(keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(keras.layers.Conv2D(filters=256, kernel_size=(3, 3), activation=‘relu’, padding=‘same’))
model.add(keras.layers.Conv2D(filters=256, kernel_size=(3, 3), activation=‘relu’, padding=‘same’))
model.add(keras.layers.Conv2D(filters=256, kernel_size=(3, 3), activation=‘relu’, padding=‘same’))
model.add(keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(256, activation=‘relu’))
model.add(keras.layers.Dense(10, activation=‘softmax’))
model.compile(optimizer=‘adam’, loss=‘sparse_categorical_crossentropy’, metrics=[‘accuracy’])

return model

======================================================

Thanks!

Hello,

can you quantify what you mean by “poor accuracy” as compared to keras inference?

Also, it’d help us debug if you can share a small repro package containing the source, model, dataset, and inference source that demonstrate the symptoms you are seeing.

Regards,
NVIDIA Enterprise Support

“poor accuracy” means that the model can’t recognize the input well.
I got about 99% from keras model. but I got about 10% from TensorRT model.

I attached the files
thanks :)


I removed the attached files because I solved the problem.

I solved that problem.
I use the ‘reshape function’ instead of ‘flatten layer’.

thanks :)