def test(generator): filenames = ["1.jpg", "2.jpg", "3.jpg"] # orignal image for predict without mask for filename in filenames: img = process_image('test/' + filename) ## image for dreawing temp_img = process_image('test/' + filename) print("Testing ...") mask = erase_img(temp_img) img = np.expand_dims(img, 0) mask = np.expand_dims(mask, 0) completion_image = generator.predict([img, mask]) # # Delete Batch dimension completion_image = np.squeeze(completion_image, 0) img = np.squeeze(img, 0) #cv2 show #completion_image = cv2.cvtColor(completion_image, cv2.COLOR_BGR2RGB) #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.figure(figsize=(6, 3)) plot_image(temp_img, 'Input', 1) plot_image(completion_image, 'Output', 2) plot_image(img, 'Ground Truth', 3) plt.savefig("result/" + filename.split('.')[0] + "_test") plt.show() # cv2.imshow("result",completion_image) # cv2.waitKey() print("Done.....")
def predict(self, image_path, topk, cat_to_name): """ Predict the class (or classes) of an image using a trained deep learning model. """ # --- Load image to get prediction --------- img_np = util.process_image(image_path) print("Getting prediction ... ", end="") # --- Convert image to tensor for prediction --------- img_tensor = torch.from_numpy(img_np).type(torch.FloatTensor) img_tensor.unsqueeze_(0) # --- Get probabilities --------- self.model.eval() with torch.no_grad(): img_variable = Variable(img_tensor) log_ps = self.model(img_variable) ps = torch.exp(log_ps) top_ps, top_class = ps.topk(topk) top_ps = top_ps.detach().numpy().tolist()[0] top_class = top_class.detach().numpy().tolist()[0] # --- Convert indices to classes and invert --------- idx_to_class = {val: key for key, val in self.class_to_idx.items()} top_labels = [idx_to_class[label] for label in top_class] top_flowers = [cat_to_name[idx_to_class[label]] for label in top_class] print("done!") return top_ps, top_labels, top_flowers
def predict(image_path, model, topk=5, device='cpu'): '''Predict the class (or classes) of an image using a trained deep learning model. ''' print(f"Inferring using '{device}'...") model.to(device) # Retrieve file and covert to tensor input_im = torch.tensor(process_image(image_path)).type(torch.FloatTensor) # Retrieve label from input image path label_class = Path(image_path).parts[2] # Switch class mapping for convenience idx_to_class = {d:k for k,d in model.class_to_idx.items()} model.eval() with active_session(): with torch.no_grad(): input_im = input_im.to(device) logps = model(input_im.unsqueeze_(0)) ps = torch.exp(logps) # Get top probabilities, indices top_p, top_idx = ps.topk(topk, dim=1) # Convert top indices to classes top_class = [idx_to_class[k] for k in top_idx.tolist()[0]] model.train() input_im = input_im.squeeze().cpu() # Return top probabilities, corresponding classes, input image tensor, and correct label class return top_p.tolist()[0], top_class, input_im, label_class
def predict(self, image_path, topk=5, gpu=False): """ Predict the class (or classes) of an image using a trained deep learning model. :param image_path: :param topk: the number of classes to return :param gpu: flag to use a GPU when predicting :return: tuple containing [0]the list of probabilities and [1]the list of classes """ # Setup Cuda device = torch.device( "cuda:0" if gpu and torch.cuda.is_available() else "cpu") self.model.to(device) # Make sure model is in eval mode self.model.eval() # Process image into numpy image, then convert to torch tensor np_image = process_image(image_path) torch_image = torch.from_numpy(np_image) torch_image = torch_image.to(device) with torch.no_grad(): output = self.model(torch_image.unsqueeze_(0)) probabilities = torch.exp(output) kprobs, kindex = probabilities.topk(topk) kprobs_list = kprobs[0].cpu().numpy().tolist() kindex_list = kindex[0].cpu().numpy().tolist() # For every kindex value, look up the class and return it instead of the index idx_to_class = {v: k for k, v in self.class_to_idx.items()} class_list = [idx_to_class[idx] for idx in kindex_list] return kprobs_list, class_list
def predict(self, image_path, top_k, cat_to_name_path): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' self.model.to(self.device) im = Image.open(image_path) im_tensor = util.process_image(im) # print(im_tensor.shape) im_tensor.unsqueeze_(0) # print(im_tensor.shape) # log_ps = self.model.forward(im_tensor.float()) im_tensor = im_tensor.to(self.device) # get probabilities without gradients with torch.no_grad(): log_ps = self.model.forward(im_tensor.float()) ps = torch.exp(log_ps) # get top probabilities top_probs, top_indexes = ps.topk(top_k) # print(f'top_probs: {top_probs}') # print(f'top_indexes: {top_indexes}') # flatten top_probs = top_probs.cpu().numpy().reshape(top_k,) # print(f'top_probs: {top_probs}') top_indexes = top_indexes.cpu().numpy().reshape(top_k,) # print(f'top_indexes: {top_indexes}') #print(f'model.class_to_idx: {model.class_to_idx}') idx_to_class = {v: str(k) for k, v in self.class_to_idx.items()} #print(f'idx_to_class: {idx_to_class}') top_classes = [] for tc in top_indexes: top_classes.append(idx_to_class[tc]) #[idx_to_class[tc] for tc in top_indexes.cpu().numpy()] # get class names class_to_name = util.read_json_file(cat_to_name_path) return top_probs, [class_to_name[tc] for tc in top_classes]
def predict(image_path, model, k=5): image = Image.open(image_path) image = np.asarray(image) processed_image = process_image(image) processed_image = processed_image[np.newaxis] predictions_array = model.predict(processed_image) predictions_array = np.squeeze(predictions_array) top_k = np.argpartition(-predictions_array, k)[:k] classes = [] for i in top_k: classes.append(i) return predictions_array[top_k], classes
def predict(): args = cli() device = torch.device("cuda" if args.gpu else "cpu") print(f'Device: {device}') image = process_image(args.image_path) model = load_model() with open(args.category_names, 'r') as f: cat_to_name = json.load(f) top_ps, top_class = _predict(model, image, args.top_k, device) for i, c in enumerate(top_class): print(f"Prediction {i + 1}: " f"{cat_to_name[c]} .. " f"({100.0 * top_ps[i]:.3f}%)")
def predict(image_path, model, topk=5, device='cuda'): ''' 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 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
# Use GPU for inference: gpu_enabled = results.gpu with open('correction.json', 'r') as c: correct_index = json.load(c) with open(category_names, 'r') as f: cat_to_name = json.load(f) # Prediction net = util.load_model(checkpoint) device = torch.device( "cuda" if torch.cuda.is_available() and gpu_enabled else "cpu") im = Image.open(image) im = util.process_image(im) net.model = net.model.double() im = im.to(device) logps = net.model(im) ps = torch.exp(logps) top_p, top_class = ps.topk(top_k) index = [] probabilities = [] for i in range(len(top_class[0])): ind = str(top_class[0][i].item()) ind = correct_index[ind] index.append(str(ind)) for j in range(len(top_p[0])):
parser.add_argument('--gpu', action='store_true', help='use gpu to infer classes') parser.add_argument('--topk', action = 'store', dest = 'topk', type=int, default = 5, required = False, help = 'Return top K most likely classes') parser.add_argument('--category_names', action='store', help='Label mapping file') arguments = parser.parse_args() try: # Use GPU if it's available #device = util.choose_device(arguments.gpu) #loads a checkpoint and rebuilds the model model = util.load_checkpoint(arguments.checkpoint_file) model.eval() #Image Preprocessing img_file = random.choice(os.listdir(arguments.img_path)) image_path = arguments.img_path+img_file img = util.process_image(image_path) # Class Prediction probs, classes = util.predict(image_path, model, arguments.gpu, arguments.topk) # Sanity Checking cat_to_name = util.cat_to_name(classes, model, arguments.category_names) for i in range(len(cat_to_name)): print(f"class = {cat_to_name[i]} prob = {probs.data[0][i]:.3f}") #util.view_classify(image_path, probs, classes, cat_to_name) except Exception as e: logging.exception("Exception occurred")
from PIL import ImageGrab, Image import deep_q_agent agent = deep_q_agent.Agent() HOST = 'localhost' PORT = 8080 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen() conn, addr = s.accept() print('Connected by', addr) frame_count = 0 start_image = util.process_image(Image.open("images/start_image.png")) start = ([start_image for _ in range(4)], 0 ) # feed a list of blank images of a dummy checkpoint to start with max_reward = 0 curr_reward = 0 eps_since_max = 0 ep = 0 images = [] images, agent.curr_checkpoint = start last_act = np.array([[0, 0, 0, 0, 0]]) while True: frame_count = (frame_count + 1) % 1000000 # Receive relevant data data = conn.recv(1024)
'--cat_to_name_json', type=str, help='Json file to load for class values to name conversion', default='cat_to_name.json') 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) # : Load Checkpoint model, checkpoint = load_checkpoint(args.checkpoint) # : Process a PIL image for use in a PyTorch model im = Image.open(image_path) processed_im = process_image(im) def predict(image_path, model, topk=5, device='cuda'): ''' 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 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)
#Load the checkpoint and rebuild the model checkpoint = torch.load(args.checkpoint) model = classifier_util.construct_model(checkpoint['arch'], checkpoint['hidden_units']) #Load the trained weights model.load_state_dict(checkpoint['model_state']) model.class_to_idx = checkpoint['classtoidx'] #Select the device used for inference (either CPU or GPU/cuda) device = 'cuda' if args.gpu else 'cpu' model.to(device) #Convert a PIL image into an object that can be used as input to a trained model image = Image.open(args.input) image_processed = torch.from_numpy(util.process_image(image)).float().to(device) #Feed the input image to the model and calculate the output probabilities model.eval() logprob = model.forward(image_processed.resize_(1, 3, 224, 224)) prob = torch.exp(logprob) #Categories with top k probabilities top_p, top_index = prob.topk(args.top_k) #Translate indices to categories mapping = model.class_to_idx inverse_mapping = {ind: cls for cls, ind in mapping.items()} top_prob = top_p.detach().cpu().numpy().squeeze() top_index_list = top_index.cpu().numpy().squeeze().tolist()
def build_model(): optimizer = Adadelta() # build Completion Network model org_img = Input(shape=input_shape, dtype='float32') mask = Input(shape=(input_shape[0], input_shape[1], 1)) generator, completion_out = model_generator(org_img, mask) completion_model = generator.compile(loss='mse', optimizer=optimizer) # build Discriminator model in_pts = Input(shape=(4, ), dtype='int32') # [y1,x1,y2,x2] discriminator = model_discriminator(input_shape, local_shape) d_container = Network(inputs=[org_img, in_pts], outputs=discriminator([org_img, in_pts])) d_out = d_container([org_img, in_pts]) d_model = Model([org_img, in_pts], d_out) d_model.compile(loss='binary_crossentropy', optimizer=optimizer) d_container.trainable = False # build Discriminator & Completion Network models all_model = Model([org_img, mask, in_pts], [completion_out, d_out]) all_model.compile(loss=['mse', 'binary_crossentropy'], loss_weights=[1.0, alpha], optimizer=optimizer) X_train = filenames[:5000] valid = np.ones((batch_size, 1)) ## label fake = np.zeros((batch_size, 1)) ## label for n in range(n_epoch): progbar = generic_utils.Progbar(len(X_train)) for i in range(int(len(X_train) // batch_size)): X_batch = X_train[i * batch_size:(i + 1) * batch_size] inputs = np.array([ process_image(filename, input_shape[:2]) for filename in X_batch ]) points, masks = get_points(batch_size) completion_image = generator.predict([inputs, masks]) g_loss = 0.0 d_loss = 0.0 if n < tc: g_loss = generator.train_on_batch([inputs, masks], inputs) else: d_loss_real = d_model.train_on_batch([inputs, points], valid) d_loss_fake = d_model.train_on_batch( [completion_image, points], fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) if n >= tc + td: g_loss = all_model.train_on_batch([inputs, masks, points], [inputs, valid]) g_loss = g_loss[0] + alpha * g_loss[1] progbar.add(inputs.shape[0], values=[("Epoch", int(n + 1)), ("D loss", d_loss), ("G mse", g_loss)]) # show_image show_image(batch_size, n, inputs, masks, completion_image) # save model generator.save("model/generator.h5") discriminator.save("model/discriminator.h5")