def test_valid_job(self): job = example_job() with job: with Task(): # distributed_ctx_init_* ignored by analyzer ops.Add(['distributed_ctx_init_a', 'distributed_ctx_init_b']) # net_printer.analyze(example_job()) print(net_printer.to_string(example_job()))
def caffe2_predictor_v2(init_net_path, predict_net_path, image_dir, labels_filename): ''' https://github.com/caffe2/tutorials/blob/master/Loading_Pretrained_Models.ipynb :param init_net_path: :param predict_net_path: :param image_dir: :param labels_filename: :return: ''' resize_height = 224 resize_width = 224 labels = np.loadtxt(labels_filename, str, delimiter='\t') test_transform = transforms.Compose([ transforms.Resize(size=(resize_height, resize_width)), transforms.ToTensor(), # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) # Read the contents of the input protobufs into local variables with open(init_net_path, "rb") as f: init_net = f.read() with open(predict_net_path, "rb") as f: predict_net = f.read() predict_def = caffe2_pb2.NetDef() predict_def.ParseFromString(predict_net) print(net_printer.to_string(predict_def)) # 加载图像 # workspace.RunNetOnce(init_net) # workspace.CreateNet(predict_net) p = workspace.Predictor(init_net, predict_net) images_list = glob.glob(os.path.join(image_dir, '*.jpg')) for image_path in images_list: print("--------------------------------------") image = Image.open(image_path).convert('RGB') image_tensor = test_transform(image).float() # Add an extra batch dimension since pytorch treats all images as batches image_tensor = image_tensor.unsqueeze_(0) input = image_tensor.numpy() print("input.shape:{}".format(input.shape)) # output = p.run({'0': input}) output = p.run([input]) # output = np.asarray(output) output = np.squeeze(output, axis=(0, )) print(output) # print("output shape: ", output.shape) pre_score = fun.softmax(output, axis=1) pre_index = np.argmax(pre_score, axis=1) max_score = pre_score[:, pre_index] pre_label = labels[pre_index] print("{} is: pre labels:{},name:{} score: {}".format( image_path, pre_index, pre_label, max_score))
def caffe2_predictor_v1(init_net_path, predict_net_path, image_dir, labels_filename): ''' https://discuss.pytorch.org/t/caffe2-mobilenetv2-quantized-using-caffe2-blobistensortype-blob-cpu-blob-is-not-a-cpu-tensor-325/29065 :param init_net_path: :param predict_net_path: :param image_dir: :param labels_filename: :return: ''' resize_height = 224 resize_width = 224 labels = np.loadtxt(labels_filename, str, delimiter='\t') test_transform = transforms.Compose([ transforms.Resize(size=(resize_height, resize_width)), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) init_def = caffe2_pb2.NetDef() with open(init_net_path, "rb") as f: init_def.ParseFromString(f.read()) workspace.RunNetOnce(init_def.SerializeToString()) predict_def = caffe2_pb2.NetDef() with open(predict_net_path, "rb") as f: predict_def.ParseFromString(f.read()) workspace.CreateNet(predict_def.SerializeToString()) print(net_printer.to_string(predict_def)) print('------------------------Running net-------------------------') images_list = glob.glob(os.path.join(image_dir, '*.jpg')) for image_path in images_list: image = Image.open(image_path).convert('RGB') image_tensor = test_transform(image).float() image_tensor = image_tensor.unsqueeze_(0) input = image_tensor.numpy() # print("input.shape:{}".format(input.shape)) workspace.FeedBlob( '0', input ) # Feed the inputArray into the input blob (index) of the network workspace.RunNetOnce(predict_def) output = workspace.FetchBlob( "125" ) # Fetch the result from the output blob (index) of the network # output = np.asarray(output) # print("output shape: ", output.shape) pre_score = fun.softmax(output, axis=1) pre_index = np.argmax(pre_score, axis=1) max_score = pre_score[:, pre_index] pre_label = labels[pre_index] print("{} is: pre labels:{},name:{} score: {}".format( image_path, pre_index, pre_label, max_score))
# implementation of super-resolution model # `here <https://github.com/pytorch/examples/blob/master/super_resolution/super_resolve.py>`__ # # load the resized image and convert it to Ybr format img = Image.open("./_static/img/cat_224x224.jpg") img_ycbcr = img.convert('YCbCr') img_y, img_cb, img_cr = img_ycbcr.split() # Let's run the mobile nets that we generated above so that caffe2 workspace is properly initialized workspace.RunNetOnce(init_net) workspace.RunNetOnce(predict_net) # Caffe2 has a nice net_printer to be able to inspect what the net looks like and identify # what our input and output blob names are. print(net_printer.to_string(predict_net)) ###################################################################### # From the above output, we can see that input is named "9" and output is # named "27"(it is a little bit weird that we will have numbers as blob # names but this is because the tracing JIT produces numbered entries for # the models) # # Now, let's also pass in the resized cat image for processing by the model. workspace.FeedBlob("9", np.array(img_y)[np.newaxis, np.newaxis, :, :].astype(np.float32)) # run the predict_net to get the model output workspace.RunNetOnce(predict_net)
# implementation of super-resolution model # `here <https://github.com/pytorch/examples/blob/master/super_resolution/super_resolve.py>`__ # # load the resized image and convert it to Ybr format img = Image.open("./_static/img/cat_224x224.jpg") img_ycbcr = img.convert('YCbCr') img_y, img_cb, img_cr = img_ycbcr.split() # Let's run the mobile nets that we generated above so that caffe2 workspace is properly initialized workspace.RunNetOnce(init_net) workspace.RunNetOnce(predict_net) # Caffe2 has a nice net_printer to be able to inspect what the net looks like and identify # what our input and output blob names are. print(net_printer.to_string(predict_net)) ###################################################################### # From the above output, we can see that input is named "9" and output is # named "27"(it is a little bit weird that we will have numbers as blob # names but this is because the tracing JIT produces numbered entries for # the models) # # Now, let's also pass in the resized cat image for processing by the model. workspace.FeedBlob( "9", np.array(img_y)[np.newaxis, np.newaxis, :, :].astype(np.float32)) # run the predict_net to get the model output workspace.RunNetOnce(predict_net)
img = img * 255 - mean # add batch size axis which completes the formation of the NCHW shaped input that we want img = img[np.newaxis, :, :, :].astype(np.float32) print("NCHW image (ready to be used as input): ", img.shape) # Read the contents of the input protobufs into local variables with open(INIT_NET, "rb") as f: init_net = f.read() with open(PREDICT_NET, "rb") as f: predict_net = f.read() predict_def = caffe2_pb2.NetDef() predict_def.ParseFromString(predict_net) print(net_printer.to_string(predict_def)) # Initialize the predictor from the input protobufs p = workspace.Predictor(init_net, predict_net) # Run the net and return prediction results = p.run({'data': img}) # Turn it into something we can play with and examine which is in a multi-dimensional array results = np.asarray(results) print("results shape: ", results.shape) # Quick way to get the top-1 prediction result # Squeeze out the unnecessary axis. This returns a 1-D array of length 1000 preds = np.squeeze(results) # Get the prediction and the confidence by finding the maximum value and index of maximum value in preds array
def test_print(self): self.assertTrue(len(net_printer.to_string(example_job())) > 0)
#img_pil.size[0] + horizontal_padding, #img_pil.size[1] + vertical_padding #) #) #img_pil = img_pil.resize((1024,1024)) #img_tensor = preprocess(img_pil) #img_y = img_tensor.unsqueeze(0).cpu() # Let's run the mobile nets that we generated above so that caffe2 workspace is properly initialized workspace.RunNetOnce(init_net) workspace.RunNetOnce(predict_net) # Caffe2 has a nice net_printer to be able to inspect what the net looks like and identify # what our input and output blob names are. print(net_printer.to_string(core.Net(predict_net))) #graph = net_drawer.GetPydotGraph(predict_net, rankdir="LR") #display.Image(graph.create_png(), width=800) # Now, let's also pass in the resized cat image for processing by the model. #workspace.FeedBlob("1", np.array(img_y)[np.newaxis, np.newaxis, :, :].astype(np.float32)) workspace.FeedBlob("1", np.array(img_y).astype(np.float32)) # run the predict_net to get the model output workspace.RunNetOnce(predict_net) # Now let's get the model output classifier vector img_class = workspace.FetchBlob("1277") # Average pool layer for DenseNet 121