async def get_verifications(img1: UploadFile = File(...), img2: UploadFile = File(...)): img1 = process_image(img1) img2 = process_image(img2) result = skai.compare(img1, img2) return { "verified": result }
def train(): notcars = glob.glob('data/non-vehicles/*/*.png') cars = glob.glob('data/vehicles/*/*.png') print_stats(cars, notcars) features_car = [] for car in cars: img = read_image(car) img_processed = process_image(img) features_car.append(extract_features(img_processed, parameters)) features_notcar = [] for notcar in notcars: img = read_image(notcar) img_processed = process_image(img) # png features_notcar.append(extract_features(img_processed, parameters)) features = np.vstack((features_car, features_notcar)) # Fit a per-column scaler scaler = StandardScaler().fit(features) # Apply the scaler to X features_scaled = scaler.transform(features) # Define the labels vector labels = np.hstack( (np.ones(len(features_car)), np.zeros(len(features_notcar)))) # Split up data into randomized training and test sets rand_state = np.random.randint(0, 100) out = train_test_split(features_scaled, labels, test_size=0.2, random_state=rand_state) features_train, features_test, labels_train, labels_test = out # Initialize support vector machine object clf = SVC(kernel='linear', C=0.00001) # Check the training time for the SVC t = time.time() clf.fit(features_train, labels_train) print('{0:2.2f} seconds to train SVC...'.format(time.time() - t)) # Accuracy score accuracy = clf.score(features_test, labels_test) print('Test Accuracy of SVC = {0:2.4f}'.format(accuracy)) classifier = Classifier(clf, scaler) joblib.dump(classifier, 'classifier.pkl') return classifier
def predict(self): names = utils.cat_to_name(self.category_names) processed_image = utils.process_image(self.predict_path, self.re_size, self.norm_means, self.norm_stdv) self.model.to(self.device) self.model.eval() processed_image = processed_image.float() processed_image = processed_image.unsqueeze(0) processed_image = processed_image.to(self.device) output = self.model.forward(processed_image) ps = torch.exp(output) probs, index = ps.topk(self.top_k) index = index.tolist()[0] inv_dict = {v: k for k, v in self.model.class_to_idx.items()} classes = [] for i in index: classes.append(inv_dict[i]) probs = probs.tolist()[0] top_names = [] for c in classes: top_names.append(names[c].title()) probs_per = [] for p in probs: probs_per.append(p * 100) print('resulting [{}] predictions:'.format(self.top_k)) for name in top_names: print(name + '....' + ' {0:.2f} percent'.format(probs_per[top_names.index(name)]))
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = utils.process_image( cv2.cvtColor(np.asarray(image), cv2.COLOR_BGR2RGB)) steering_angle = float( model.predict(image_array[None, :, :, :], batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def process(infile, outdir): """Process an image file to its relevant text block, writing the same named file to `outdir`""" with open(infile, 'rb') as img: outfile = os.path.join(outdir, os.path.basename(infile)) image_array = np.asarray(bytearray(img.read()), dtype=np.uint8) cropped_image = process_image(image_array) cv2.imwrite(outfile, cropped_image)
def predict(image_path, model, topK, device): ''' Make prediction on 1 image, Parameters: -image_path(str): path to image -model(pytorch model): trained model -topK(int): int -device(str): cuda / cpu returns top k probabilities and class(es) ''' model = model.to(device) model.eval() #using image_process: image = process_image(image_path) image_tensor = torch.from_numpy(np.array([image])).float() image_tensor = image_tensor.to(device) output = model(image_tensor) ps = torch.exp(output) top_p, top_class = ps.topk(topK, dim=1) top_class = top_class.tolist() top_p = top_p.tolist() real_class = [] for class_value in top_class[0]: real_class.append( ([k for k, v in model.class_to_idx.items() if v == class_value])[0]) return top_p[0], real_class
def predict(image_path, model, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. arguments: image_path - path to the image being predicted model - the trained model to use for the prediction topk - the number of top probabilties that the input matched the most return: top_p - the list values for topk probalities of input matches classes - the clases to which the input has been matched ''' # open and process the given image image = Image.open(image_path) img = process_image(image) # peform the model classification top_p = None top_class = None with torch.no_grad(): model.eval() logs = model(img.unsqueeze_(0)) ps = torch.exp(logs) top_p, top_class = ps.topk(topk, dim=1) #get the top probalities classes_idx = [] # map the predicted classes to the class names using category_names top_class = top_class.squeeze() for i, x in model.class_to_idx.items(): if x in top_class: classes_idx.append(i) return top_p, classes_idx
def predict(image_path, model, topk, use_cuda): if use_cuda: model.cuda() model.eval() image = process_image(image_path) image = torch.from_numpy(image) image = image.type(torch.FloatTensor).unsqueeze(0) if use_cuda: image = image.cuda() image = image.unsqueeze(0) with torch.no_grad(): output = model.forward(image) ps = torch.exp(output) top_p, top_class = ps.topk(topk) top_p, top_class = top_p.cpu(), top_class.cpu() probable_classes = [] for label in top_class.numpy()[0]: probable_classes.append( list(model.class_to_idx.keys())[list( model.class_to_idx.values()).index(label)]) return top_p.numpy()[0], probable_classes
def predict(image_path, model, topk=5): ''' 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 # http://blog.outcome.io/pytorch-quick-start-classifying-an-image/ - Add dimension to simulate batch when # converting to tensor manually and then convert to Autograd variable model.eval() with torch.no_grad(): # Load image as PIL, process it and generate FloatTensor inputs = torch.from_numpy(process_image(Image.open(image_path))).type( torch.FloatTensor) # Simulate batch inputs = inputs.unsqueeze_(0) # Convert to autograd inputs = Variable(inputs) inputs = inputs.to(DEVICE) # Forward pass and get prob logps = model.forward(inputs) ps = torch.exp(logps) top_p, top_idx = ps.topk(topk, dim=1) # Convert to np array and then squeeze first dimension manually (to prevent squeezing top_k=1 case) top_prob = np.array(top_p)[0] top_class = np.array( [model.idx_to_class[idx] for idx in np.array(top_idx)[0]]) return top_prob, top_class
def predict(image_path, model, topk, gpu=False): if gpu and torch.cuda.is_available(): print('Using GPU to predict') device = torch.device("cuda:0") model.cuda() else: print('Using CPU for testing') device = torch.device("cpu") im = Image.open(image_path) processed_im = process_image(im).unsqueeze(0) model.to(device) model.eval() with torch.no_grad(): processed_im = processed_im.to(device).float() output = model(processed_im) 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
def predict(image_path, model, topk, gpu): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' device = torch.device( "cuda" if gpu and torch.cuda.is_available() else "cpu") model.eval() image = process_image(image_path, gpu) image = image.unsqueeze_(0) model = model.to(device) image = image.to(device) with torch.no_grad(): output = model.forward(image) output = output.to(device) probabilities = torch.exp(output).data prob = torch.topk(probabilities, topk)[0].tolist()[0] # probabilities index = torch.topk(probabilities, topk)[1].tolist()[0] # index idx_to_class = {v: k for k, v in model.class_to_idx.items()} label = [idx_to_class[idx] for idx in index] return prob, label
def predict(options, img_read_path, img_write_path): # Read image content = process_image(img_read_path, -1, -1, resize=False) ori_height = content.shape[1] ori_width = content.shape[2] # Pad image content = get_padding(content) height = content.shape[1] width = content.shape[2] # Get eval model eval_model = get_evaluate_model(width, height) eval_model.load_weights(options['weights_read_path']) # If flag is set, print model summary and generate model description if options["plot_model"]: eval_model.summary() plot_model(eval_model, to_file='model.png') # Generate output and save image res = eval_model.predict([content]) output = deprocess_image(res[0], width, height) output = remove_padding(output, ori_height, ori_width) imwrite(img_write_path, output)
def upload(request): if not request.method == 'POST': raise Http404 response_data = {} if request.is_ajax(): if request.FILES: image = request.FILES.values()[0] path = process_image(image, request.user.id) try: preview_size = request.POST['preview_size'] except KeyError: preview_size = '64' response_data['status'] = True response_data['imagePath'] = path response_data['thumbnail'] = render_to_string('files_widget/includes/thumbnail.html', {'MEDIA_URL': settings.MEDIA_URL, 'STATIC_URL': settings.STATIC_URL, 'preview_size': preview_size}) return HttpResponse(json.dumps(response_data), content_type="application/json") else: response_data['status'] = False response_data['message'] = "We're sorry, but something went wrong." return HttpResponse(json.dumps(response_data), content_type='application/json')
def predict(device=None, image_path=None, model=None, topx=1): ## Process image in the image_path np_img = process_image(image_path) ## Convert from np to tensor tensor_img = torch.from_numpy(np_img).type(torch.FloatTensor) ## Convert tensor to a batch of 1 tensor_img.unsqueeze_(0) ## Predict prob for top most prediction model.to(device) model.eval() with torch.no_grad(): log_ps = model(tensor_img) ps = torch.exp(log_ps) probs, class_idxs = ps.topk(topx, dim=1) ## Convert tensors to lists probs = probs.tolist()[0] class_idxs = class_idxs.tolist()[0] ## Reverse dictionary idx_to_class = dict([[v, k] for k, v in model.class_to_idx.items()]) ## Get Classes from Indices classes = [idx_to_class[i] for i in class_idxs] return probs, classes
def predict(image_path, model, topk, device): model.eval() model.to(device) np_im = process_image(image_path) # Converting it to Tensor im = torch.from_numpy(np_im).float() if device == 'cuda': im = im.cuda() im = im.unsqueeze(0) with torch.no_grad(): output = model.forward(im) ps = torch.exp(output) probs, indices = ps.topk(topk) #Cannot convert CUDA tensor to numpy. So, copying the tensor to host memory first. indices = indices.cpu() probs = probs.cpu() # inverting dictionary inv_map = {v: k for k, v in model.class_to_idx.items()} # mapping classes = list() for label in indices.numpy()[0]: classes.append(inv_map[label]) return probs.numpy()[0], classes
def analysis(img, model, preprocess_input, decode_predictions, layer_name, n_classes): # Preprocess data imgArray, originalImage = process_image(img, preprocess_input) preds = model.predict(imgArray) pred_class = np.argmax(preds) # Change here to get view of something else decoded_preds = decode_predictions(preds) class_data = decoded_preds[0][0] # Compute methods localization_grad = grad_cam(imgArray, model, pred_class, originalImage, relu_activation, linear, layer_name, n_classes) localization_squad = grad_cam(imgArray, model, pred_class, originalImage, relu_activation, squared_weights, layer_name, n_classes) bprop = guided_backprop(imgArray, model, pred_class, layer_name, n_classes) # Make it three dimensions to allow for multiplication localization_grad = np.array( [localization_grad, localization_grad, localization_grad]) localization_grad = np.swapaxes(localization_grad, 0, 2) localization_grad = np.swapaxes(localization_grad, 0, 1) localization_squad = np.array( [localization_squad, localization_squad, localization_squad]) localization_squad = np.swapaxes(localization_squad, 0, 2) localization_squad = np.swapaxes(localization_squad, 0, 1) # Combine gradcam and backprop to get guided gradcam guided_gradcam = np.multiply(localization_grad, bprop) guided_squadcam = np.multiply(localization_squad, bprop) guided_gradcam = rescale(guided_gradcam) guided_squadcam = rescale(guided_squadcam) return bprop, guided_gradcam, guided_squadcam, class_data
def eval(image_path, checkpoint_name, topk, gpu): model = model_utils.load_checkpoint(checkpoint_name, gpu) model.eval() if (gpu == False): model.cpu() image = utils.process_image(image_path) image = torch.from_numpy(image).unsqueeze(0) image = image.float() output = model.forward(image) top_prob, top_labels = torch.topk(output, topk) top_prob = top_prob.exp() top_prob_array = top_prob.data.numpy()[0] inv_class_to_idx = {v: k for k, v in model.class_to_idx.items()} top_labels_data = top_labels.data.numpy() top_labels_list = top_labels_data[0].tolist() top_classes = [inv_class_to_idx[x] for x in top_labels_list] return top_prob_array, top_classes
def predict(image_path, model, topk=5, device='cuda'): ''' 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 image img = process_image(image_path) # Need to convert to tensor to push through the model img_tensor = torch.from_numpy(img).type(torch.FloatTensor) # This is needed to mimic batch size as required by VGG img_model = img_tensor.unsqueeze(0) img_model = img_model.to(device).float() model.to(device) # Model return LogSoftmax, so converting back to probabilities probs = torch.exp(model.forward(img_model)) # Get Top K probabilities and labels results = probs.topk(topk) probs = results[0].to('cpu') labels = results[1].to('cpu') topk_probs = probs.detach().numpy().tolist()[0] topk_labels = labels.detach().numpy().tolist()[0] # Convert indices to classes idx_to_class = {val: key for key, val in model.class_to_idx.items()} top_labels = [idx_to_class[lab] for lab in topk_labels] top_flowers = [cat_to_name[idx_to_class[lab]] for lab in topk_labels] return topk_probs, top_flowers
def spectrumToArraysTIMIT(device, params, img, output_folder, file_name): g = tf.Graph() content_image = ut.process_image(img) with g.device(device), g.as_default(), tf.Session(graph=g, config=tf.ConfigProto(allow_soft_placement=True)) as sess: print "Load content values..." image = tf.constant(content_image) model = models.getModel(image, params) content_image_y_val = [sess.run(y_l) for y_l in model.y()] # sess.run(y_l) is a constant numpy array for i in range(len(content_image_y_val)): output_path = output_folder + layers_names[i] + "/" if not os.path.exists(output_path): os.makedirs(output_path) dirr = os.path.dirname(output_path + file_name) if not os.path.exists(dirr): os.makedirs(dirr) np.save(output_path + file_name, content_image_y_val[i]) #cleanUp del image del content_image del model del content_image_y_val del g
def predict(image_path, model, topk, gpu=False): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' if gpu: device = 'cuda' else: device = 'cpu' img = Image.open(image_path) image_tensor = process_image(img).unsqueeze(0) model.eval() model.to(device) model.idx_to_class = {v: k for k, v in model.class_to_idx.items()} with torch.no_grad(): image_tensor = image_tensor.to(device) outputs = model(image_tensor).squeeze(0) outputs = torch.nn.functional.softmax(outputs, dim=0) topK_indices = (-outputs).argsort() topK_probs = outputs[topK_indices][:topk] topK_classes = [ model.idx_to_class[i] for i in topK_indices.cpu().numpy() ][:topk] return list(topK_probs.cpu().numpy()), topK_classes
def predict(img_path, model, top_k): devices = args.devices if devices == 'gpu': device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') else: device = torch.device('cpu') model.to(device) img_torch = process_image(img_path) img_torch = torch.from_numpy(img_torch) img_torch = img_torch.unsqueeze_(0) img_torch = img_torch.float() if devices == 'gpu': with torch.no_grad(): output = model.forward(img_torch.cuda()) else: with torch.no_grad(): output = model.forward(img_torch) probs = F.softmax(output.data, dim=1) top_prob = np.array(probs.topk(top_k)[0][0]) class_to_idx = {v: k for k, v in model.class_to_idx.items()} top_classes = [class_to_idx[x] for x in np.array(probs.topk(top_k)[1][0])] return top_prob, top_classes
def predict(image_path, model, k=1, use_gpu=False): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' #Pre-process Image np_array = utils.process_image( image_path) # Load & Format PIL Image to Numpy Image Array tensor_input = torch.from_numpy( np_array) # Convert image_array to torch tensor input # Determine the device to use device = torch.device( 'cuda' if use_gpu and torch.cuda.is_available() else 'cpu') if device == torch.device('cuda'): print( "\n************************************\n\tPredicting with GPU\n************************************" ) else: print( "\n************************************\n\tPredicting with CPU\n************************************" ) # Turn off dropouts model.base_model.eval() # Pass model to the appropriate device model.base_model.to(device) with torch.no_grad(): # Autograd Tensor m_input = Variable(tensor_input) # Pass input object to device m_input = m_input.to(device) # Add one more dimension m_input = m_input.unsqueeze(0) # Feed input into model m_output = model.base_model.forward(m_input.float()) # Top K probabilities outcome = torch.exp(m_output).data.topk(k) # For to_numpy conversion if use_gpu: probs = outcome[0].cpu() classes = outcome[1].cpu() else: probs = outcome[0] classes = outcome[1] # Invert class_to_idx dictionary idx_to_class = {value: key for key, value in model.class_to_idx.items()} # Retrieve predicted class indices pred_classes = [idx_to_class[label] for label in classes.numpy()[0]] return probs.numpy()[0], pred_classes
def detect_image(image_np): target_dimension = int(model.meta["height"]) processed_img = utils.process_image(image_np, target_dimension) image_dimension = torch.FloatTensor([image_np.shape[1], image_np.shape[0]]) scaling_factor = torch.min(target_dimension / image_dimension) if CUDA: processed_img = processed_img.cuda() image_var = Variable(processed_img) # 416 * 416 * (1/(8*8) + 1/(16*16) + 1/(32*32) )*3 start = time.time() with torch.no_grad(): output = model(image_var, CUDA) end = time.time() print("Total time: {}".format(end - start)) # print("output", output.shape) thresholded_output = utils.object_thresholding(output[0]) # print("Thresholded", thresholded_output.shape) # print(output[0]) true_output = utils.non_max_suppression(thresholded_output) # print("True output", true_output.shape) original_image_np = np.copy(image_np) if true_output.size(0) > 0: # Offset for padded image vertical_offset = (target_dimension - scaling_factor * image_dimension[0].item()) / 2 horizontal_offset = (target_dimension - scaling_factor * image_dimension[1].item()) / 2 for output_box in true_output: rect_coords = utils.center_coord_to_diagonals(output_box[:4]) rect_coords = torch.FloatTensor(rect_coords) # transform box detection w.r.t. boundaries of the padded image rect_coords[[0, 2]] -= vertical_offset rect_coords[[1, 3]] -= horizontal_offset rect_coords /= scaling_factor # Clamp to actual image's boundaries rect_coords[[0, 2]] = torch.clamp(rect_coords[[0, 2]], 0.0, image_dimension[0]) rect_coords[[1, 3]] = torch.clamp(rect_coords[[1, 3]], 0.0, image_dimension[1]) # print(image_np.shape) class_label = coco_classes[output_box[5].int()] print("Output Box:", output_box, "Class Label:", class_label) print("Rect coords:", rect_coords) if constants.PERFORM_FACE_DETECTION and class_label == "person": rc = rect_coords.int() person_img_np = original_image_np[rc[1]:rc[3], rc[0]:rc[2]] # print("person_img_np: ", person_img_np, person_img_np.shape) # cv2.imshow("bounded_box_img", person_img_np) # cv2.waitKey(0) face_label = face_recognition_utils.recognize_face_in_patch( person_img_np) if face_label is not None: class_label = face_label image_np = utils.draw_box(rect_coords, image_np, class_label) return image_np
def predict(image_path, model_path, top_k, class_names_json): ''' Function takes inputs and prints top predicted class name, label and probability for flower image. It also prints the top k results for flower image. INPUT: image_path - (str) path to image. model_path - (str) path to tensorflow model (h5). top_k - (int) Top K results requested. class_names (json_file) Dict [class names : class ids] OUTPUT: NONE ''' #Getting mapping file for class index and class names class_names = get_classes(class_names_json) #Reads Tensorflow model. model = tf.keras.models.load_model( model_path, custom_objects={'KerasLayer': hub.KerasLayer}) #open image img = Image.open(image_path) #put image into array image_numpy = np.asarray(img) #resize image for processing processed_image = process_image(image_numpy) #Predict image using tensorflow model prob_preds = model.predict(np.expand_dims(processed_image, axis=0)) prob_preds = prob_preds[0] #Get top_k results as tensors. values, index = tf.math.top_k(prob_preds, k=top_k) #Conver tensors to numpy for use. probs = values.numpy().tolist() class_index = index.numpy().tolist() #Map class ids to class names. pred_label_names = [] for i in class_index: pred_label_names.append(class_names[str(i)]) #1 Result print( f"""\n\n Class most likely based on the following {image_path} with the highest probaility as listed: \n class_id: {class_index[0]} \n class_label: {pred_label_names[0]} \n probability: {str(round(float(probs[0]) *100, 2)) + '%'} \n\n\n """) if top_k > 1: print(f"\n Top {top_k} probs", probs) print(f"\n Top {top_k} class names", pred_label_names) print(f"\n Top {top_k} class ids", class_index) print("\n\n")
def analysis_localize(img, model, preprocess_input, decode_predictions, layer_name, n_classes): # Preprocess data imgArray, originalImage = process_image(img, preprocess_input) preds = model.predict(imgArray) # pred_class = np.argmax(preds) # Change here to get view of something else pred_classes = np.argsort(-preds[0])[:5] # top 5 classes decoded_preds = decode_predictions(preds) bboxes = [] bboxes_s = [] for i, pred_class in enumerate(pred_classes): # Compute methods localization_grad = grad_cam(imgArray, model, pred_class, originalImage, relu_activation, linear, layer_name, n_classes) # Threshold and pick largest max_value = np.amax(localization_grad) masked = np.where(localization_grad > 0.15 * max_value, 1, 0) labels = skimage.measure.label(masked) if labels.any() > 0: largest_segment = labels == np.argmax( np.bincount(labels.flat)[1:]) + 1 largest_segment = np.where(largest_segment, 1, 0) for region in regionprops(largest_segment): ymin, xmin, ymax, xmax = region.bbox bboxes.append([decoded_preds[0][i][0], xmin, ymin, xmax, ymax]) else: return False, False for i, pred_class in enumerate(pred_classes): # Compute methods localization_squad = grad_cam(imgArray, model, pred_class, originalImage, relu_activation, squared_weights, layer_name, n_classes) # Threshold and pick largest max_value = np.amax(localization_squad) masked = np.where(localization_squad > 0.15 * max_value, 1, 0) labels = skimage.measure.label(masked) if labels.any() > 0: largest_segment = labels == np.argmax( np.bincount(labels.flat)[1:]) + 1 largest_segment = np.where(largest_segment, 1, 0) for region in regionprops(largest_segment): ymin, xmin, ymax, xmax = region.bbox bboxes_s.append([decoded_preds[0][i][0], xmin, ymin, xmax, ymax]) else: return False, False del (imgArray) del (originalImage) del (localization_grad) del (localization_squad) return bboxes, bboxes_s
def __getitem__(self, idx): if idx < 0: idx = len(self) + idx if not (0 <= idx < len(self)): raise IndexError(idx) image, *_, image_name = process_image(self.images_paths[idx], check_target=False) if self.transform_fn is not None: image = self.transform_fn(image=image) return {'image': image, 'name': image_name}
def predict(image_path, model, gpu, cat_to_name=None, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. Prints a bar chart of the top 5 most likely labels for the given input image. ''' model.train(False) model.eval() np_image = utils.process_image(image_path) image = torch.from_numpy(np_image) image = image.type(torch.FloatTensor) image = image.unsqueeze(0) if gpu and not torch.cuda.is_available(): print("Sorry, no cuda enabled gpu is available.") print("Predicting on the cpu...") gpu = False if gpu: model.cuda() image.cuda() output = model(image) prob_output = utils.softmax(output) probs, classes = prob_output.topk(topk) probs = probs.cpu().detach().numpy()[0] fig, ax = plt.subplots() ax.barh(np.arange(topk), probs, align='center') ax.set_yticks(np.arange(topk)) if cat_to_name is not None: image_names = [] for image_name_idx in classes[0]: image_names.append(cat_to_name[str(int(image_name_idx) + 1)]) ax.set_yticklabels(image_names) ax.invert_yaxis() for i, prob in enumerate(probs): ax.text(prob + (np.max(probs) / 100), i + .1, round(prob, 3), color='black', fontweight='bold') ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.set_xlabel('SOFTMAX PROBABILITY') ax.set_title('PREDICTED PROBABILITIES') ax.set_xlim(0) plt.tight_layout() plt.show()
def predict(image_path, checkpoint, gpu, topk, msg=True): ''' Predict the class (or classes) of an image using a trained deep learning model. Params: image_path : dir where is the image model : trainned model topk : number of top classes to return Returns: top_p : highest topk probabilies top_class_idx : top classes index checkpoint. : Trainned model checkpoint msg : Bool to display feedback messages ''' model, optimizer = load_model(checkpoint, gpu, msg) #Load the saved model top_class_idx = [] image = process_image(image_path) image = image.unsqueeze(0) idx_to_class = {value: key for (key, value) in model.class_to_idx.items() } #ReMap idx to classes #Check device available if gpu == 'y': device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") else: device = torch.device("cpu") print('Running on: ', color(device)) #Move to the device model.to(device) model.eval() with torch.no_grad(): image = image.to(device) logps = model.forward(image) # Top k classifications ps = torch.exp(logps) top_p, top_class = ps.topk(topk, dim=1) for i in top_class[0]: idx = idx_to_class[i.item()] top_class_idx.append(idx) return top_p, top_class_idx
def predict(image_path, model, top_k): # Convert given image as an np array image_as_array = np.asarray(Image.open(image_path)) # pre-process the image preprocessed_image = process_image(image_as_array) # add extra dimension to match the model input size input_image = np.expand_dims(preprocessed_image, axis=0) # Get the Predictions for the processed image predictions = model.predict(input_image) # Get the Probabilities and Labels prob, labels = tf.math.top_k(predictions, int(top_k)) labels += 1 return prob.numpy()[0], labels.numpy()[0]
def checkImage(image_file): image = cv2.imread(image_file) smoke_count = 0 window_count = 0 for (x, y, window) in sliding_window( image, stepSize, (winW, winH)): window_count = window_count + 1 prediction = classifier2.predict_proba( process_image(window, feature.getFeature))[0] if(prediction[1] >= threshold): smoke_count = smoke_count + 1 logger.info("%d smoke in %d widnows of %s." % ( smoke_count, window_count, os.path.basename(image_file)))
print #print "CHUNK NUMBER: " + str(chunk_number) #i_img_path = images_folder + "/" + str(chunk_number) + ".png" #img = spectograms.ReadRGB(i_img_path) #TEST chunk_number = 549 test_image = images_folder + "/" + "549.png" img = spectograms.ReadRGB(test_image) #style_image_path = style_images_folder + "/" + "579.png" #style_img = spectograms.ReadRGB(style_image_path) #style_image = ut.process_image(style_img) g = tf.Graph() content_image = ut.process_image(img) wanted_style = np.array([[0,1.0]]) with g.device(device), g.as_default(), tf.Session(graph=g, config=tf.ConfigProto(allow_soft_placement=True)) as sess: print "Load content values and calculate style and content softmaxes" #content cW = tf.constant(content_weights) cB = tf.constant(content_bias) #style sW = tf.constant(style_weights) sB = tf.constant(style_bias) wanted_style = tf.constant(wanted_style, tf.float32) image = tf.constant(content_image) model = models.getModel(image, params) pool2_image_val = sess.run(model.y())
if __name__ == '__main__': if(len(sys.argv) != 2): logger.critical("请输入测试图片路径!") sys.exit() else: image_path = sys.argv[1] # 全局模型 overallModel_file = config["model"]["overallModel_file"] with open(overallModel_file, 'rb') as fid: classifier1 = cPickle.load(fid) # logger.info("overall model imported successfully.") # 局部模型 localModel_file = config["model"]["localModel_file"] with open(localModel_file, 'rb') as fid: classifier2 = cPickle.load(fid) # logger.info("overall model imported successfully.") image = cv2.imread(image_path) pred = classifier1.predict_proba(process_image( image, feature.getFeature))[0] # threshold_window = 1 - min(0.5, pred[1]) * 5 / 7.0 # 窗口上的阈值取决于整体上的概率 threshold_window = 0.6 result = list() for (x, y, window) in sliding_window(image, stepSize, (winW, winH)): prediction = classifier2.predict_proba( process_image(window, feature.getFeature))[0] if(prediction[1] >= threshold_window): result.append([x, y, x+winW, y+winH]) print result