NVIDIA-AI-IOT / redtail

Perception and AI components for autonomous mobile robotics.
BSD 3-Clause "New" or "Revised" License
1.01k stars 344 forks source link

Problem when predicting output using TrailNet_SResNet-18 #81

Closed nhatuan84 closed 6 years ago

nhatuan84 commented 6 years ago

Hi,

I have problem when predicting based on pretrained model. This is my code: `import numpy as np import caffe import glob import cv2

caffe.set_device(0) caffe.set_mode_gpu()

load the model

net = caffe.Net('TrailNet_SResNet-18.prototxt', 'TrailNet_SResNet-18.caffemodel', caffe.TEST)

Define image transformers

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_transpose('data', (2,0,1))

Reading image paths

test_img_paths = [img_path for img_path in glob.glob("./*jpg")]

Making predictions

test_ids = [] preds = [] for img_path in test_img_paths: print (img_path) img = cv2.imread(img_path, cv2.IMREAD_COLOR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) net.blobs['data'].data[...] = transformer.preprocess('data', img) out = net.forward() print(out)`

I used the images that you used when testing cpp program: rot_c.jpg rot_l.jpg rot_r.jpg tran_l.jpg tran_r.jpg

Here is the output:

./rot_l.jpg {'out': array([[0.83052695, 0.16267 , 0.00680302, 0.06352565, 0.5423914 , 0.39408293]], dtype=float32)} ./tran_r.jpg {'out': array([[0.00359121, 0.471454 , 0.52495474, 0.07992556, 0.16039667, 0.75967777]], dtype=float32)} ./rot_r.jpg {'out': array([[0.00299141, 0.04390362, 0.95310503, 0.06651229, 0.3616249 , 0.57186276]], dtype=float32)} ./tran_l.jpg {'out': array([[0.0055711 , 0.9919258 , 0.00250322, 0.9745238 , 0.01419899, 0.01127721]], dtype=float32)} ./rot_c.jpg {'out': array([[0.14533867, 0.8473169 , 0.00734437, 0.34082696, 0.50264335, 0.15652977]], dtype=float32)}

The output is not similar comparing to the code in tests.cpp auto images = std::vector{"rot_l.jpg", "rot_c.jpg", "rot_r.jpg", "tran_l.jpg", "tran_r.jpg"}; float predictions[][6] = {{0.932, 0.060, 0.006, 0.080, 0.848, 0.071}, {0.040, 0.958, 0.001, 0.488, 0.375, 0.135}, {0.000, 0.027, 0.971, 0.036, 0.407, 0.555}, {0.011, 0.988, 0.000, 0.981, 0.008, 0.009}, {0.000, 0.855, 0.144, 0.013, 0.031, 0.954}};

Thank you.

nhatuan84 commented 6 years ago

I solved it: img = cv2.resize(cv2.imread(img_path, cv2.IMREAD_COLOR),(320,180)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.transpose(img[:, :, ::-1], [2, 0, 1]).astype(np.float32) net.blobs['data'].data[...] = img out = net.forward()