def predict_topk(image_path, model, mapping_json, gpu, topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    #image showing
    print('read in cata-to-name files')
    with open(mapping_json, 'r') as f:
        cat_to_name = json.load(f)
    # gpu or
    print('reading in file finished')
    if gpu:
        model.cuda()
    else:
        model.cpu()
    model.eval()
    #image prediction
    image = process_image(image_path)
    model = model.double()
    image = image.transpose((2, 1, 0))
    image = torch.from_numpy(image)
    image = image.unsqueeze(0)
    with torch.no_grad():
        log_ps = model(image)
        ps = torch.exp(log_ps)
        probs, top_class = ps.topk(5, dim=1)
        probs, top_class = probs.numpy(), top_class.numpy()
    #show the top5 flowers name
    flower_names = [cat_to_name[class_] for class_ in classes[0].astype(str)]

    for prob, flower_name in zip(probs, flower_name):
        print('the probablity of ', flower_name, prob, ' is ', prob * 100, '%')
    return probs, top_class
Пример #2
0
def predict_image(image_path):
    #load the model
    model = load_checkpoint(args.checkpoint)

    if args.gpu:
        device = torch.device('cuda')
    else:
        device = torch.device('cpu')

    #Implement the code to predict the class from an image file

    image = helper.process_image(image_path)
    image = torch.from_numpy(image).float()
    image = Variable(torch.FloatTensor(image), requires_grad=True)
    image = image.unsqueeze(0)

    model = model.to(device)
    model.eval()
    image = image.to(device)

    output = model.forward(image)
    prob = torch.exp(output)
    prob, classes = prob.topk(arg.top_k, dim=1)

    top_prob = prob.data[0].tolist()
    top_classes = []
    for e in classes.data[0].cpu().numpy():
        for key, value in model.class_to_idx.items():
            if e == value:
                top_classes.append(int(key))

    return top_prob, top_classes
Пример #3
0
def process_video(file_path):

    if not os.path.isfile(file_path):
        raise ValueError('The file {} does not exist'.format(file_path))

    videos_folder = os.path.join(FLAGS.runs_dir, 'videos')

    if not os.path.isdir(videos_folder):
        os.makedirs(videos_folder)

    video_output = os.path.join(videos_folder, os.path.basename(file_path))

    with tf.Session(config=_get_config()) as sess:

        image_input, logits, keep_prob = _load_model(sess, _model_folder())

        reader = imageio.get_reader(file_path)
        fps = reader.get_meta_data()['fps']
        writer = imageio.get_writer(video_output, fps=fps)

        for frame in tqdm(reader, desc='Processing Video', unit='frames'):
            frame_processed = helper.process_image(frame, sess, logits,
                                                   keep_prob, image_input,
                                                   IMAGE_SHAPE)
            writer.append_data(frame_processed)

        writer.close()
Пример #4
0
def main():
    """
    Executing relevant functions
    """
    
    # Get Keyword Args for Prediction
    args = arg_parser()
    
    # Load categories to names json file
    with open(args.category_names, 'r') as f:
        	cat_to_name = json.load(f)

    # Load model trained with train.py
    model = load_checkpoint(args.checkpoint)
    print(model)
    # Process Image
    image = process_image(args.image_path)
    
    # Check for GPU
    device = check_gpu(gpu_arg=args.gpu);
    
    # Use `processed_image` to predict the top K most likely classes
    top_ps, top_class = predict(image, model, args.top_k)
    
    # Print out probabilities
    print_probability(top_class,cat_to_name, top_ps)
Пример #5
0
def predict(image_path, model, topk=5, category_names='cat_to_name.json', device="cpu"):
    """ Predict the class (or classes) of a flower image using a trained deep learning model.
    """
    image = Image.open(image_path)
    image = process_image(image).unsqueeze(dim=0)
    image = image.type(torch.FloatTensor)

    image = image.to(device)
    model = model.to(device)

    logps = model(image)
    ps = torch.exp(logps)
    top_ps, top_class = ps.topk(topk, dim=1)

    with open(category_names, 'r') as f:
        cat_to_name = json.load(f)

    idx_to_class = {v: k for k, v in model.class_to_idx.items()}

    if topk > 1:
        top_ps = [top_p.item() for top_p in top_ps.squeeze()]
        top_class = [idx_to_class[top_class.item()] for top_class in top_class.squeeze()]
    else:
        top_ps = [top_p.item() for top_p in top_ps]
        top_class = [idx_to_class[top_class.item()] for top_class in top_class]

    top_class_name = [cat_to_name[top_class] for top_class in top_class]

    return top_class_name, top_ps
Пример #6
0
def predict(image_path, model, cat_to_name, topk=5):
    ''' 
    Predict the class (or classes) of an image using a trained deep learning model.
    '''

    # Process image with helper method
    img = process_image(image_path)

    # Reshape image tensor to match model input 
    img = img.unsqueeze(0)
    img = img.float()

    # Retrieve label key matching the index in the dataset at large (Complete unsegmented dataset)
    # of the image to it's label
    class_to_idx = model.class_to_idx
    
    #Turning off Gradient because we are not training
    with torch.no_grad():
        # Passing model through model to get prediction log probabilities
        output = model.forward(img.to('cuda'))
        # Conversion from log to standard probabilities
        ps = torch.exp(output)
    

    # Taking top k probabilities and the corresponding labels to those predictions and printing them for 
    # review by data scientist
    probs, classes = ps.topk(topk, dim=1)
    prob_list, flow_list = [], []
    for prob in probs[0]:
        prob_list.append(prob.item())
    for cls in classes[0]:
        flow_list.append(class_to_idx[str(cls.item()+1)])    
    lab_list = [cat_to_name[str(cls+1)] for cls in flow_list]
    
    return lab_list, prob_list
Пример #7
0
def predict(image_path, model, topk=5, cuda = False):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    # TODO: Implement the code to predict the class from an image file
    image = Image.open(image_path)
    image = helper.process_image(image)
    image = torch.from_numpy(image).type(torch.FloatTensor)
    inputs = Variable(image, requires_grad=False)
    inputs = inputs.unsqueeze(0)
    if cuda:
        inputs = inputs.cuda()
        model.cuda()
    ps = torch.exp(model.forward(inputs))
    if cuda:
        ps.cuda()
    top_probs, top_labels = ps.topk(top_k)
    top_probs, top_labels = top_probs.data.cpu().numpy().squeeze(), top_labels.data.cpu().numpy().squeeze()
    idx_to_class = {v: key for key, v in model.class_to_idx.items()}
    if top_k == 1:
        top_classes = [idx_to_class[int(top_labels)]]
        top_probs = [float(top_probs)]
    else:
        top_classes = [idx_to_class[each] for each in top_labels]

    return top_probs, top_classes
Пример #8
0
def predict(image_path, model, topk, device):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
        '''
    # TODO: Implement the code to predict the class from an image file
    #process the image and show the image
    model.to(device)
    model.eval()
    tensor_img = process_image(image_path)
    #the model requires the batch size at the start of the input tensor, use unsqueeze to add a dimension of 1 (batch size=1)
    #to dimension 0 of image tensor
    tensor_img.unsqueeze_(0)
    tensor_img.to(device)
    log_results = model.forward(tensor_img)
    probs = torch.exp(log_results)
    top_p, top_class_idx = probs.topk(topk, dim=1)
    #convert top_p, top_class tensor to numpy and then to list.
    #Because the variable require Gradient, use var.detach().numpy() instead
    top_probs = top_p.detach().numpy().tolist()[0]
    top_class_idx = top_class_idx.detach().numpy().tolist()[0]

    #map the top_class to flower name using model.class_to_idx.items()
    idx_to_class = {val: key for key, val in model.class_to_idx.items()}
    top_class_label = [idx_to_class[label] for label in top_class_idx]

    return top_probs, top_class_label
Пример #9
0
def predict(image_path, checkpoint, device, categories_to_names, topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    with open(categories_to_names, 'r') as f:
        cat_to_name = json.load(f)
    # TODO: Implement the code to predict the class from an image file
    img = helper.process_image(image_path)
    model, class_to_idx = helper.load_checkpoint(
        checkpoint)  #,optimizer=load_checkpoint(model)

    img = torch.Tensor(img)

    #unsqueezing recommended on torch discussion forums for error i was having at:
    #https://discuss.pytorch.org/t/expected-stride-to-be-a-single-integer-value-or-a-list-of-1-values-to-match-the-convolution-dimensions-but-got-stride-1-1/17140
    img = img.unsqueeze(0)

    img = img.to(device)
    model = model.to(device)
    model.eval()
    with torch.no_grad():
        log_prob = model(img)
        probabilities = torch.exp(log_prob)
        probs, classes = probabilities.topk(topk, dim=1)
    classes = classes.cpu()
    classes = classes.numpy()
    classification = {
        number: string
        for string, number in class_to_idx.items()
    }
    class_names = [cat_to_name[classification[item]] for item in classes[0]]
    probs = probs.cpu()
    probs = probs.numpy()
    return probs[0], class_names
Пример #10
0
def main(image_path, model_URL, top_k, class_names_file=None):
    # Load classnames or assign classnames to none
    if class_names_file:
        with open(class_names_file, 'r') as f:
            class_names = json.load(f)
    else:
        class_names = None


# Load and compile model from file
    model = tf.keras.models.load_model(
        model_URL, custom_objects={'KerasLayer': hub.KerasLayer})
    # Load and process image to be classified
    image = Image.open(image_path)
    image = np.asarray(image)
    processed_image = h.process_image(image)
    processed_image = np.expand_dims(processed_image, axis=0)
    # Predict and return results
    probabilities = model.predict(processed_image).squeeze()

    prob_name_list = h.i_toName(class_names, probabilities)

    probs, classes = h.get_topk(prob_name_list, top_k)
    print(probs)
    print(classes)
    return probs, classes
Пример #11
0
 def get_classification(self, image):
     """Determines the color of the traffic light in the image
     Args:
         image (cv::Mat): image containing the traffic light
     Returns:
         int: ID of traffic light color (specified in styx_msgs/TrafficLight)
     """
     image = process_image(image)
     light_class = self.predict(image)
     return light_class
Пример #12
0
def Predict(model, image_path, topk, device):
    
    image = helper.process_image(image_path)
    
    image = image.to(device)
    
    probabilities = torch.exp(model(image))
    
    probability, classes = probabilities.topk(topk, dim=1)
    
    return probability, classes
Пример #13
0
def predict(image_path, checkpoint, topk, labels, gpu):
    ''' Predict the class (or classes) of an image using aa trained deep learning model.
    '''
    #model = helper.get_model(checkpoint)
    #new_net = NeuralNetClassifier(
    #    module=models.Inception_v3,
    #    criterion=torch.nn.CrossEntropyLoss,
    #    optimizer= optim.SGD,
    #    )
    #new_net.initialize()

    #new_net.load_params(f_params= checkpoint)
    model = models.Inception_v3.load_state_dict(
        torch.load(checkpoint)['valid_acc_best'])
    print(model.eval())
    #torch.load(path)

    #if gpu:
    #    model = model.cuda()
    #    torch.cuda.set_device(3)
    #loadvalid_acc_best
    image = Image.open(image_path)
    #process Image into a tensor
    image_tensor = helper.process_image(image)
    #if torch.cuda.is_available() and gpu:
    #    image = image_tensor.to('cuda')

    with torch.no_grad():
        output = model.forward(image)
    #else:
    output = model.forward(image_tensor)
    #get the probability output
    probs = torch.exp(output)
    #print(np.isnan(probs.data.numpy()).sum())
    top_probs, indices = probs.topk(topk, sorted=True)
    print("Probabilities: " + str(top_probs))
    print("Indices: " + str(indices))

    if torch.cuda.is_available() and gpu:
        # Added softmax here as per described here:
        # https://github.com/pytorch/vision/issues/432#issuecomment-368330817
        probs = torch.nn.functional.softmax(top_probs.data,
                                            dim=1).cpu().numpy()[0]
        classes = indices.data.cpu().numpy()[0]
    else:
        probs = torch.nn.functional.softmax(top_probs.data, dim=1).numpy()[0]
        classes = indices.data.numpy()[0]

    print(classes)
    #print(probs)

    print(probs)

    return probs, classes
Пример #14
0
def predict(image_path, model, topk):

    model.to(device)
    model.eval()

    imaget = h.process_image(image_path)
    imaget.to(device)

    imaget = imaget.unsqueeze(0)
    image = imaget.type(torch.cuda.FloatTensor)
    output = model.forward(image)
    ps = torch.exp(output)
    model.train()

    return ps.topk(topk)
Пример #15
0
def predict(image_path, model, topk=5, device='cuda'):
    im = Image.open(image_path)
    processed_image = process_image(im).unsqueeze(0)
    model.to(device)
    model.eval()
    with torch.no_grad():
        processed_image = processed_image.to(device).float()
        output = model(processed_image)
        ps = torch.exp(output)
    pred = ps.topk(topk)
    flower_ids = pred[1][0].to('cpu')
    flower_ids = torch.Tensor.numpy(flower_ids)
    probs = pred[0][0].to('cpu')
    idx_to_class = {k: v for v, k in checkpoint['class_to_idx'].items()}
    flower_names = np.array([cat_to_name[idx_to_class[x]] for x in flower_ids])

    return probs, flower_names
Пример #16
0
def sanity_check(image_path):

    probs, labels = predict(image_path, model, args.top_k)

    ps = [x for x in probs.cpu().detach().numpy()[0]]
    npar = [x for x in labels.cpu().numpy()[0]]
    names = list()

    inv_mapping = {v: k for k, v in model.class_to_idx.items()}

    for i in npar:
        names.append(cat_to_name[str(inv_mapping[i])])

    h.imshow(h.process_image(image_path), ax=plt.subplot(2, 1, 1))
    plt.title(names[0])

    plt.subplot(2, 1, 2)
    sb.barplot(y=names, x=ps, color=sb.color_palette()[0])
    plt.show()
Пример #17
0
def predict(model):

    if (model == None):
        model = helper.load_checkpoint()

    image_path = helper.get_file_path(
        '\nPlease enter the path of the image you want to analyse\n')
    topk = helper.get_int(
        '\nPlease enter how many to the top predictions you want to see (topk)\n'
    )

    device = helper.get_device()
    model = helper.load_device(model)

    image_tensor = helper.process_image(image_path).to(device)
    idx_to_class = helper.get_idx_to_class()
    print('\nPredicting\n')

    with torch.no_grad():
        output = model.forward(image_tensor)

    ps = torch.exp(output)

    topK_ps = torch.topk(ps, topk)

    probs = topK_ps[0].cpu().numpy().squeeze()
    sorted_ps_label_keys = topK_ps[1].cpu().numpy().squeeze()
    get_label = lambda x: idx_to_class[str(x)]

    classes = []

    for i in sorted_ps_label_keys[0:topk]:
        classes.append(get_label(i))

    print('\nFinished predicting\n')
    return probs, classes
Пример #18
0
parser.add_argument('--image_path',
                    type=str,
                    help='Path to file',
                    default='flowers/test/28/image_05230.jpg')
parser.add_argument('--gpu', type=bool, default=True, help='GPU or CPU')
parser.add_argument('--topk', type=int, help='K prediction', default=0)
args = parser.parse_args()

image_path = args.image_path
with open(args.cat_to_name_json, 'r') as f:
    cat_to_name = json.load(f)

model, checkpoint = load_checkpoint(args.checkpoint)

im = Image.open(image_path)
processed_image = process_image(im)


def predict(image_path, model, topk=5, device='cuda'):
    im = Image.open(image_path)
    processed_image = process_image(im).unsqueeze(0)
    model.to(device)
    model.eval()
    with torch.no_grad():
        processed_image = processed_image.to(device).float()
        output = model(processed_image)
        ps = torch.exp(output)
    pred = ps.topk(topk)
    flower_ids = pred[1][0].to('cpu')
    flower_ids = torch.Tensor.numpy(flower_ids)
    probs = pred[0][0].to('cpu')
import helper

parser = argparse.ArgumentParser(description='Image Classifier')
parser.add_argument('--inp_image',type = str, default = 'flowers/valid/1/image_06755.jpg', help = 'Path to dataset directory')
parser.add_argument('--checkpoint',type=str,default='trained1.pth',help='Checkpoint')
parser.add_argument('--gpu',type=str,default='cpu',help='GPU')
parser.add_argument('--json_class',type=str,default='cat_to_name.json',help='JSON of key value')
parser.add_argument('--top_k',type=int,default=5,help='Top k classes and probabilities')
args=parser.parse_args()


class_to_name= helper.load_class(args.json_class)

model=helper.load(args.checkpoint)
print(model)

vals=torch.load(args.checkpoint)

image = helper.process_image(args.inp_image)


helper.imshow(image)

probs, classes = helper.predict(args.inp_image, model, args.top_k, args.gpu)  

print(probs)
print(classes)

helper.display_image(args.inp_image, class_to_name, classes,probs)

Пример #20
0
import argparse
from helper import load_checkpoint, process_image, predict, convertJSON

# Get data from command line
parser = argparse.ArgumentParser( description ='Script to predict the probability of type of flower in supplied image' )
parser.add_argument("image_path", help="Path to image for processing" )
parser.add_argument("checkpoint", help="Name and path for checkpoint file containing the trained network" )
parser.add_argument("--category_name", help="The path and name of *.json file containing the mapping of flower categories")
parser.add_argument("--top_k", help="Top probabilities to return. Between 1 to 102", type=int, default=3 )
parser.add_argument("--gpu", help="Train model via GPU", action="store_true", default=False)

args = parser.parse_args()

# Load the checkpoint and rebuild the model
model = load_checkpoint(args.checkpoint)

# Process image
tmp = process_image(args.image_path)

# Inference
top_k_probs, classes = predict(args.image_path, model, args.top_k, args.gpu )
print("Top K probabilities: {}".format( top_k_probs ))
if args.category_name == None:
    print("classes: {}".format( classes ))
else:
    cat_to_name = convertJSON( args.category_name )
    index_to_class = {val: key for key, val in model.class_to_idx.items()}
    top_classes = [index_to_class[each] for each in classes]
    names = [cat_to_name[x] for x in top_classes]

    print("Names of top K flowers: {}".format( names ))
Пример #21
0
print('top_k           = {}'.format(top_k))
print('category_names  = {}'.format(category_names))
print('gpu             = {}'.format(gpu))
print('--------------------------------')

# Load Categories
with open('cat_to_name.json', 'r') as f:
    cat_to_name = json.load(f)

# Load checkpoint
device = 'cuda' if gpu else 'cpu'
model, criterion, optimizer, class_labels = helper.load_checkpoint(
    device, checkpoint_path)

# Process image
image = helper.process_image(image_path)
image_tensor = torch.from_numpy(image).type(torch.FloatTensor)
image_tensor.resize_([1, 3, 224, 224])

model.eval()
model.to(device)
image_tensor = image_tensor.to(device)

result = model(image_tensor)
result = torch.exp(result)

probs, idx = result.topk(top_k)
probs.detach_()
probs.resize_([top_k])
probs = probs.tolist()
import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Image Classification application')
    parser.add_argument(dest="image_path", default ="flowers/test/28/image_05230.jpg" ,action="store",help = "provide path to image, the path should look similar to this 'flowers/test/28/image_05230.jpg'")
    parser.add_argument(dest = "checkpoint",default = "ImageClassifier/final_checkpoint.pth", action="store",help = "paste in the checkpoint saved from training, default: ImageClassifier/final_checkpoint.pth")
    parser.add_argument("--top_k",dest = "top_k",default = 5, type=int, action="store",help = "how many most likely classes to display, default = 5")
    parser.add_argument("--device",dest = 'device', action="store", default='cpu',help = "device for prediction,default cpu")
    parser.add_argument("-c""--category_names",dest = "category_names",default = "cat_to_name.json", action="store",help = "the file to map categories to real names, default: 'cat_to_name.json' located on the working directory")
                        
    args = parser.parse_args()
    loaded_model = load_checkpoint(args.checkpoint)
    top_probs, top_class_label = predict(args.image_path, loaded_model, args.top_k, args.device)
    max_probs = max(top_probs)
    
    im_tensor = process_image(args.image_path)
    with open(args.category_names, 'r') as f:
         cat_to_name = json.load(f)
    img_label = cat_to_name[args.image_path.split('/')[-2]]
    print('***Prediction Results***')
    print('*top_probs:', top_probs)
    print('*top_class_label"', top_class_label)
    top_flowers = [cat_to_name[label] for label in top_class_label]
    print('*top_flowers:', top_flowers)
    print("*max class probability:", top_probs[0],
          "*predicted class label:", top_class_label[0],
          "*predicted flower:", top_flowers[0])
    print("*True Image class label:", args.image_path.split('/')[-2], "-"
        "*True Image label:", img_label)

      
Пример #23
0
 def process_image(image):
     return helper.process_image(sess, logits, keep_prob,
                                 image_input, image, IMAGE_SHAPE)
Пример #24
0
# ---

# %%
# %load_ext autoreload
# %autoreload 2

# %%
import numpy as np
import matplotlib.pyplot as plt
from skimage.draw import disk
import helper

# %%
shape = (100, 100)
image = np.zeros(shape)
_, _ = helper.process_image(image)

# %%
image = np.ones(shape)
recon_image, recon_image_sart = helper.process_image(image, with_fragment=True)

# %%
helper.plot_two_images(recon_image[:60, :20], recon_image_sart[:60, :20])

# %%
helper.plot_two_graphs(recon_image[71, :], recon_image_sart[71, :])

# %%
image = np.ones(shape)
image = np.pad(image, 20)
recon_image, recon_image_sart = helper.process_image(image)