def plot_prob_map(network, dataset, idx): idx = idx % len(dataset) image, mask = dataset[idx]['image'].unsqueeze(0), dataset[idx]['mask'] final_tensor, pred_mask = predict_image(network, image) # Apply softmax on the final tensor from the evaluation to get a probability map final_tensor = F.softmax(final_tensor, dim=1) # Labeling connected components for cell detection blobs_labels = measure.label(pred_mask.squeeze().squeeze().numpy(), background=0) plt.figure(figsize=(13, 2.1)) plt.subplot(151) plt.imshow(image.squeeze().squeeze(), cmap='gray') plt.axis('off') plt.subplot(152) plt.imshow(mask.squeeze(), cmap="hot") plt.axis('off') plt.subplot(153) plt.imshow(pred_mask.squeeze(), cmap="hot") plt.axis('off') plt.subplot(154) plt.imshow(blobs_labels, cmap="hot") plt.axis('off') plt.subplot(155) plt.imshow(final_tensor[:, 1, :, :].squeeze(), cmap="BrBG") plt.colorbar(fraction=0.046, pad=0.04) plt.axis('off') plt.show()
def evaluate(network, dataloader, criterion, use_gpu=True): device = torch.device( "cuda:0" if torch.cuda.is_available() and use_gpu else "cpu") loss_tot, dice_tot, i = 0.0, 0.0, len(dataloader) with torch.no_grad(): for index, sample in enumerate(dataloader): image, true_mask = sample['image'], sample['mask'] prob_tensor, pred_mask = predict_image(network, image, use_gpu=use_gpu, \ input_tile = (476, 476)) prob_tensor, pred_mask = prob_tensor.to(device), pred_mask.to( device) weight_map, new_mask = compute_weights(true_mask, v_bal=1.5) weight_map, new_mask = weight_map.to(device), new_mask.to(device) crit = criterion.apply loss = crit(prob_tensor, new_mask, weight_map) dice = dice_measure(pred_mask, new_mask) loss_tot, dice_tot = loss_tot + loss, dice_tot + dice return loss_tot / i, dice_tot / i
def main(): os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' ap = argparse.ArgumentParser() ap.add_argument("-n", "--name", required=True, type=str, help="Name of image file") ap.add_argument("-e", "--enet", required=True, type=int, help="Size of EfficientNet") animal_group = ap.add_mutually_exclusive_group(required=True) animal_group.add_argument("-i", "--insect", action=STORE_TRUE, help="Identify Insect") animal_group.add_argument("-b", "--bird", action=STORE_TRUE, help="Identify Bird") animal_group.add_argument("-r", "--reptile", action=STORE_TRUE, help="Identify Reptile or Amphibian") animal_level = ap.add_mutually_exclusive_group(required=True) animal_level.add_argument("-c", "--class", action=STORE_TRUE, help="Make a class level ID") animal_level.add_argument("-o", "--order", action=STORE_TRUE, help="Make an order level ID") animal_level.add_argument("-f", "--family", action=STORE_TRUE, help="Make a family level ID") animal_level.add_argument("-g", "--genus", action=STORE_TRUE, help="Make a genus level ID") animal_level.add_argument("-s", "--species", action=STORE_TRUE, help="Make a species level ID") args = vars(ap.parse_args()) animal, level, pred = predict_image(args) output(pred, animal, level)
def stream(self): stream = cv2.VideoCapture(0) while True: ret, frame = stream.read() width = frame.shape[0] height = frame.shape[1] frame = frame[int((width - 96*5)/2) : int(width - (width - 96*5)/2), int((height - 96*5)/2) : int(height - (height - 96*5)/2), :] frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) print(frame.shape) frame_to_predict = cv2.resize(frame, (96, 96)) frame_to_predict = convert_to_3_channels(frame_to_predict) frame_to_predict = frame_to_predict.astype(float) print(frame_to_predict.shape) output = predict_image(self.model, frame_to_predict) output = output[0] output = scale_coordinates(output, 96.0) output = np.array(output) zipped_coordinates = zip_to_coordinate_pairs(output) print('zipped', zipped_coordinates) # In case I want to use the drawing mechanism # output = scale_coordinates(output, 1.0/96.0) # draw(frame_to_predict, output) frame_to_predict = np.transpose(frame_to_predict, (2, 0, 1))[0] for coordinate_pair in zipped_coordinates: x = int(coordinate_pair[0]) y = int(coordinate_pair[1]) for i in range(5): for j in range(5): frame_to_predict[y + i][x + j] = 0 frame_to_predict = frame_to_predict / 255 cv2.namedWindow('frame', cv2.WINDOW_NORMAL) cv2.resizeWindow('frame', 600,600) cv2.imshow('frame', frame_to_predict) if cv2.waitKey(33) == ord('q'): break stream.release() cv2.destroyAllWindows()
def validate_scores(network, dataset): dataloader = DataLoader(dataset, batch_size=1, num_workers=1, shuffle=False) results = np.zeros(shape=(1, 4)) cols = ['pixel accuracy', 'mean_accuracy', 'dice score', 'jacard score'] for index, sample in enumerate(dataloader): ('Processing image {}'.format(index + 1)) image, image_mask = sample['image'], sample['mask'] > 0 _, pred_mask = predict_image(network, image) results[0, 0] += pixel_accuracy(pred_mask, image_mask) results[0, 1] += mean_accuracy(pred_mask, image_mask) results[0, 2] += (-1) * (dice_measure(pred_mask, image_mask) - 1) results[0, 3] += jacard_score(pred_mask, image_mask) results /= len(dataset) results_df = pd.DataFrame(data=results, index=['o'], columns=cols) results_df.plot(kind='bar') print(results_df.head())
def save_and_store_test_data(network, dataset, dataset_name, film=1): dataloader = DataLoader(dataset, batch_size=1, num_workers=1, shuffle=False) for index, sample in enumerate(dataloader): print("Saving image {} ... ".format(index + 1), end='') image, im_file = sample _, pred_mask = predict_image(network, image, input_tile=(716, 716)) labeled = measure.label(pred_mask.squeeze().squeeze().numpy(), background=0) pred_mask = torch.from_numpy(labeled).unsqueeze(0).short() save_image(pred_mask.short(), dataset_name, name=im_file[0], type='mask', film=film, d_type="Testing") print("OK")
def classify_image(): res = request.get_json(force=True) image = res['image'] user_email = res['user'] lat = res['lat'] lon = res['long'] print("Request received") writeImage(image) print("Calling tensorflow.....") classification = predict_image('./temp.jpg') img_name = classification[0].lower() img_name = img_name.replace(' ', '_') print('Conceito: ' + img_name) conceito = db.session.query(Conceito).get(img_name) score = float(classification[1]) if float(score) < 0.7: img_name = 'desconhecido' conc_lat = conceito.latitude conc_long = conceito.longitude if conc_lat is not None and conc_long is not None: if distance.distance((lat, lon), (conc_lat, conc_long)).km > 0.3: img_name = 'desconhecido' print(img_name, score) folder = os.path.join('./static/img', img_name) if not os.path.exists(folder): os.makedirs(folder) files = os.listdir(folder) print("Folder created") file_id = len(files)+1 filename = os.path.join(folder, str(file_id) + '.jpg') while True: if os.path.exists(filename): filename = os.path.join(folder, str(file_id) + '1' + '.jpg') else: break print("Filename: " + filename) os.rename('./temp.jpg', filename) print("Imagem gravada") while(True): print("Looping...") foto = addFotografia(None, img_name, user_email, lat, lon, filename, None, datetime.datetime.now(), None, 'pending', score, None) if foto is None: continue else: break print("Enviando resposta...") if img_name != 'desconhecido': return jsonify({ 'concept_id' : img_name, 'name': conceito.nome, 'description' : conceito.descricao, 'id' : foto.id }) return jsonify({ 'concept_id': 'desconhecido', 'name': 'Desconhecido', 'description': '', 'id': foto.id })