def infer(args,cut_path,image_list,comb_path): data_shape = cityscape.test_data_shape() num_classes = cityscape.num_classes() # define network images = fluid.layers.data(name='image', shape=data_shape, dtype='float32') _, _, sub124_out = icnet(images, num_classes, np.array(data_shape[1:]).astype("float32")) predict = fluid.layers.resize_bilinear( sub124_out, out_shape=data_shape[1:3]) predict = fluid.layers.transpose(predict, perm=[0, 2, 3, 1]) predict = fluid.layers.reshape(predict, shape=[-1, num_classes]) _, predict = fluid.layers.topk(predict, k=1) predict = fluid.layers.reshape( predict, shape=[data_shape[1], data_shape[2], -1]) # batch_size should be 1 inference_program = fluid.default_main_program().clone(for_test=True) # prepare environment place = fluid.CPUPlace() if args.use_gpu: place = fluid.CUDAPlace(0) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) assert os.path.exists(args.model_path) fluid.io.load_params(exe, args.model_path) print("loaded model from: %s" % args.model_path) sys.stdout.flush() if not os.path.isdir(args.out_path): os.makedirs(args.out_path) for line in image_list: # image_file = args.images_path + "/" + line.strip() # filename = os.path.basename(image_file) # print(str(cut_path)+"/"+str(line)) # image = cv2.imread(cut_path+'/'+line) #print(11111111,line) image = paddle.dataset.image.load_image( cut_path+"/" +line, is_color=True).astype("float32") image -= IMG_MEAN img = paddle.dataset.image.to_chw(image)[np.newaxis, :] image_t = fluid.LoDTensor() image_t.set(img, place) result = exe.run(inference_program, feed={"image": image_t}, fetch_list=[predict]) cv2.imwrite(comb_path + "/" + line + "_result.png", color(result[0])) print("predicted images saved in :"+comb_path)
def eval(args): data_shape = cityscape.test_data_shape() num_classes = cityscape.num_classes() # define network images = fluid.layers.data(name='image', shape=data_shape, dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='int32') mask = fluid.layers.data(name='mask', shape=[-1], dtype='int32') _, _, sub124_out = icnet(images, num_classes, np.array(data_shape[1:]).astype("float32")) iou, out_w, out_r = create_iou(sub124_out, label, mask, num_classes, data_shape) inference_program = fluid.default_main_program().clone(for_test=True) # prepare environment place = fluid.CPUPlace() if args.use_gpu: place = fluid.CUDAPlace(0) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) assert os.path.exists(args.model_path) fluid.io.load_params(exe, args.model_path) print("loaded model from: %s" % args.model_path) sys.stdout.flush() fetch_vars = [iou, out_w, out_r] out_wrong = np.zeros([num_classes]).astype("int64") out_right = np.zeros([num_classes]).astype("int64") count = 0 test_reader = cityscape.test() for data in test_reader(): count += 1 result = exe.run(inference_program, feed=get_feeder_data( data, place, for_test=True), fetch_list=fetch_vars) out_wrong += result[1] out_right += result[2] sys.stdout.flush() iou = cal_mean_iou(out_wrong, out_right) print("\nmean iou: %.3f" % iou) print("kpis test_acc %f" % iou)