Example #1
0
def predict(image_path, checkpoint, device, topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    # Reload our model
    rebuilt_model = load_checkpoint(checkpoint)

    # Mode the model to the default device
    rebuilt_model = rebuilt_model.to(device)

    # Pre-process the image to use
    processed_image = du.process_image(Image.open(image_path))

    # Convert it: Torch tensor via array
    processed_image = torch.from_numpy(np.array(processed_image))

    # Add dimension to the image
    processed_image = processed_image.unsqueeze_(0)

    # Put model to evaluation mode
    rebuilt_model.eval()

    # Move the image to the available device
    processed_image = processed_image.to(device)

    # Turn off gradients, send image through network
    with torch.no_grad():
        output = rebuilt_model.forward(processed_image)

    # Get the probabilities
    probabilities = torch.exp(output)

    # Extract the probabilities
    probs = probabilities.topk(topk)[0]
    index = probabilities.topk(topk)[1]

    # Convert them to list
    probs = np.array(probs)[0]
    index = np.array(index)[0]

    # Now get our index, class mapping from model
    class_to_idx = rebuilt_model.class_to_idx

    # Invert the dictionnary
    inv_class_to_idx = {x: y for y, x in class_to_idx.items()}

    # Convert index to class
    classes = []
    for element in index:
        classes += [inv_class_to_idx[element]]

    return probs, classes
Example #2
0
def predict():
        conf = Config()
        # 打印模型配置信息
        conf.dump()
        parser = argparse.ArgumentParser(description='图片分类模型训练')
        parser.add_argument(
                '--image_path', type=str,default='data/zhengjian/predict/test/3601216003722.jpg', help='指定要分类的路径')
        parser.add_argument(
                '--checkpoint', type=str, default='model/checkpoint.pth', help='指定checkpoint的模型的保存位置')
        parser.add_argument(
                '--top_k', type=int, default=2, help='选取topk概率的最大类别, dafault=2')
        args = parser.parse_args()

        # 加载转换,处理,转换图片到Tensor
        image_tensor = process_image(image_path=args.image_path)

        # 加载模型,是否使用gpu
        model, _, _, _ = load_checkpoint(
                checkpoint_path=args.checkpoint, load_optimizer=False, gpu=conf.cuda)

        #图片分类
        probabilities, predictions = classify_image(
                image_tensor=image_tensor, model=model, top_k=args.top_k, gpu=conf.cuda)

        #分类结果
        top_class = predictions[0]
        top_prob = probabilities[0]
        top_k = args.top_k
        print(f'\n预测概率最高的类别是 {top_class.capitalize()} '
              f' 概率是{top_prob:.4f}')
        print(f'\n预测的topk是 {top_k} 类别是 {predictions}'
              f'概率是 {probabilities}')

        # 绘图
        display_prediction(
                image_path=args.image_path,
                probabilities=probabilities,
                predictions=predictions)
Example #3
0
    def predict(self, image_path, checkpoint_file, topk=5):
        ''' Predict the class (or classes) of an image using a trained deep learning model.
        '''
        # Implement the code to predict the class from an image file
        self.model = self.load_checkpoint(checkpoint_file)

        pil_image = Image.open(image_path)
        np_image = process_image(pil_image)

        tensor_image = torch.from_numpy(np.expand_dims(np_image, axis=0)).type(
            torch.FloatTensor)
        tensor_image = tensor_image.to(self.device)
        #   model = load_checkpoint(model)
        self.model.idx_to_class = {
            v: k
            for k, v in self.model.class_to_idx.items()
        }

        with torch.no_grad():
            self.model.to(self.device)
            self.model.eval()

            output = self.model.forward(tensor_image)

            ps = torch.exp(output)
            probs, preds = ps.topk(topk, dim=1)
            list_probs = probs.cpu().numpy().squeeze().tolist()
            list_preds = preds.cpu().numpy().squeeze().tolist()
            if topk == 1:
                list_predsclass = self.model.idx_to_class[list_preds]
            else:
                list_predsclass = [
                    self.model.idx_to_class[index] for index in list_preds
                ]

        return list_probs, list_predsclass
def predict(image_path, model, device, topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''

    model.to(device)

    img = process_image(image_path)
    image_tensor = torch.from_numpy(np.expand_dims(img, axis=0)).type(
        torch.FloatTensor).to(device)
    model.eval()
    with torch.no_grad():
        logps = model.forward(image_tensor)
        ps = torch.exp(logps)
        top_p, top_class = ps.topk(topk, dim=1)
        idx_to_class = {
            value: key
            for key, value in model.class_to_idx.items()
        }
        probabilities = [p.item() for p in top_p[0]]
        classes = [idx_to_class[i.item()] for i in top_class[0]]

    model.train()

    return probabilities, classes
def predict(in_args,model,device):

    model.eval()
    model.to(device)

    img = Image.open(in_args.image)
    img_arr = dutils.process_image(img)
    inputs = torch.from_numpy(img_arr).type(torch.cuda.FloatTensor)
    inputs.unsqueeze_(0)
    inputs.to(device)
    with torch.no_grad():
        output = model.forward(inputs)

        prob,labels = torch.topk(output,in_args.top_k)
        top_prob = prob.exp()

    class_idx_dict = {model.class_to_idx[key]: key for key in model.class_to_idx}

    classes = list()
    cpu_labels = labels.cpu()
    for label in cpu_labels.detach().numpy()[0]:
        classes.append(class_idx_dict[label])

    return top_prob.cpu().numpy()[0],classes
def main():
    """
    Main Function for predictions using Image classifier on a 
    image path.
    Inputs are passed using command line.
    Type python predict.py -h for help on usage.
    """

    # Parse and validate the cmdline arguments passed for Training.
    args, proceed = parse_args_predict()

    if proceed:
        print('args in main:', args)


#         print('time for gpu')
#         return [], [], []
    else:
        print("\n Type python predict.py -h for help on setting training "
              "parameters. Thank you. \n")
        return [], [], []

    # Check if cuda is available
    if args.gpu and torch.cuda.is_available():
        device = torch.device("cuda")
    elif not args.gpu:
        device = torch.device("cpu")
    else:
        print("cuda is available? :", torch.cuda.is_available())
        raise Exception('Error ! device CUDA is not available')

    # Read Image data
    with active_session():
        image = get_image(args.image_path)
        # Pre process the image into numpy ndarray
        image_nparray = process_image(image)
    print("Pre Processing the Image complete.")
    # Convert numpy ndarray to torch tensor.
    image_torch_tensor = torch.from_numpy(image_nparray)

    # Assign first dimension of tensor for number of images = 1
    # as input to model.
    image_torch_tensor = image_torch_tensor.unsqueeze(0)
    image_torch_tensor = image_torch_tensor.float()

    probs, classes, class_labels = [], [], []
    # Load the model from checkpoint, Predict for input image.
    with active_session():
        model, checkpoint = load_checkpoint(args.checkpoint)
        print("Loading the checkpoint complete.")
        probs, classes = predict(image_torch_tensor,
                                 model,
                                 device,
                                 topk=args.top_k)

    print("Predict the Image class complete.")
    print('Top {} predictions Probabilities: '.format(args.top_k), probs,
          '\nTop {} predictions Classes: '.format(args.top_k), classes)

    if args.category_names:
        print("Mapping the Prediction class categories to class labels.")
        catg_to_name = get_catg_name(args.category_names)
        class_labels = [catg_to_name[x] for x in classes]
        print('Top {} predictions Class Labels: '.format(args.top_k),
              class_labels)

    return probs, classes, class_labels
Example #7
0
def telemetry(sid, data):
    global t, control_value, replay_memory
    try:
        if args.image_folder and not os.path.exists(args.image_folder):
            os.makedirs(args.image_folder)
            replay_memory.reset()
            t = 0
    except Exception as e:
        print(e.with_traceback())
    if data:
        # The current steering angle of the car
        steering_angle = float(data["steering_angle"])
        # The current throttle of the car
        throttle = float(data["throttle"])
        # The current speed of the car
        speed = float(data["speed"])
        if t % 3 == 0:
            try:
                img_orig = Image.open(BytesIO(base64.b64decode(data["image"])))
                img = process_image(img_orig)

                state_img = np.expand_dims(img, 0)
                state_info = np.array([[steering_angle, throttle, speed]])
                state = [state_img, state_info]

                pos_out = pdm.predict(state_img)
                # print(pos_out)
                pos_idx = np.argmax(pos_out, axis=1)[0]
                # print(pos_idx)
                reward = position.compute_reward(pos_idx)

                if args.mode == 'hardcoded':
                    msg = "{:6} {:5} Hardcoded Action: {}->{}"
                    action = get_hardcoded_action(pos_idx)
                elif args.mode == 'drive':
                    msg = "{:6} {:5} Predict   Action:   {}->{}"
                    action = np.argmax(qlm.predict(state_img), axis=1)[0]
                    if position.get_label(pos_idx) == 'LEFT' and action < 5:
                        msg = "{:6} {:5} Hardcoded Action:   {}->{}"
                        action = get_hardcoded_action(pos_idx)
                    elif position.get_label(pos_idx) == 'RIGHT' and action > 3:
                        msg = "{:6} {:5} Hardcoded Action:   {}->{}"
                        action = get_hardcoded_action(pos_idx)
                else:
                    if position.get_label(pos_idx) == 'ON':
                        if random.random() <= RANDOM_ACTION_PROB:
                            msg = "{:6} {:5} Random    Action:   {}->{}"
                            action = get_random_action()
                        else:
                            msg = "{:6} {:5} Predict   Action:   {}->{}"
                            action = np.argmax(qlm.predict(state_img),
                                               axis=1)[0]
                    else:
                        msg = "{:6} {:5} Hardcoded Action:   {}->{}"
                        action = get_hardcoded_action(pos_idx)

                if args.mode != 'drive':
                    replay_memory.memorize(reward, img_orig, state, pos_idx,
                                           action)
                    replay_memory.store_mini_batch()

                control_value = get_control_value(action, speed)
                print(
                    msg.format(int(t / 3), position.get_label(pos_idx), action,
                               control_value))
            except Exception as e:
                print(e.with_traceback())
        send_control(*control_value)
        t = t + 1
    else:
        # NOTE: DON'T EDIT THIS.
        sio.emit('manual', data={}, skip_sid=True)
parser.add_argument(
    '--category_names',
    action='store',
    default='',
    help='Path to file with mapping of categories to class names '
    'in .json format')
parser.add_argument('--gpu',
                    action='store_true',
                    help='Use GPU for training. default=True.')
args = parser.parse_args()

gpu = True if args.gpu else False

# Load, process and convert image to Tensor
############################################
image_tensor = process_image(image_path=args.image_path)

# load model
# model moved to device specified by gpu(bool) on load
############################################
model, _, _, _ = load_checkpoint(checkpoint_path=args.checkpoint,
                                 load_optimizer=False,
                                 gpu=gpu)

# Classify image
############################################
probabilities, predictions = classify_image(image_tensor=image_tensor,
                                            model=model,
                                            top_k=args.top_k,
                                            category_names=args.category_names,
                                            gpu=gpu)