Exemple #1
0
def plotKeypointsOverOutputModel(index, dataset, model, img_folder):
    """Forward a img to the model and display the output keypoints over the image.
       It enables us to see the loss evolution over the model visually over the image
       index is the index of the img in the dataset argument"""
    # Get an image
    imgId = dataset.img_ids[index]
    img, keypoints = dataset[index]

    # Transform into a pytorch model input and Forward pass
    y = model(Variable(img.unsqueeze(0)))

    # Get the coordinates of the keypoints
    keypoints = keypoints_from_heatmaps(y[0].data.numpy())

    # Plot the image
    img_anno = dataset.annotations.loadImgs(imgId)[0]
    img_path = os.path.join(img_folder, img_anno['file_name'])
    img_array = load_image(img_path)
    img_array_resized = resize(img_array, (512, 640))
    plt.figure()
    plt.title('Original image')
    plt.imshow(img_array_resized)
    xs, ys, vs = get_xs_ys_vs(keypoints)
    plt.plot(xs, ys, 'ro', color='c')
    plt.show()
Exemple #2
0
def test(images, threshold, img_s, context):
    print("Loading model...")
    model = load_model("model/global-wheat-yolo3-darknet53.params", ctx=context)
    for path in images:
        print(path)
        raw = load_image(path)
        rh, rw, _ = raw.shape
        classes_list = []
        scores_list = []
        bboxes_list = []
        for _ in range(5):
            img, flips = gcv.data.transforms.image.random_flip(raw, px=0.5, py=0.5)
            x, _ = gcv.data.transforms.presets.yolo.transform_test(img, short=img_s)
            _, _, xh, xw = x.shape
            rot = random.randint(0, 3)
            if rot > 0:
                x = np.rot90(x.asnumpy(), k=rot, axes=(2, 3))
            classes, scores, bboxes = model(mx.nd.array(x, ctx=context))
            if rot > 0:
                if rot == 1:
                    raw_bboxes = bboxes.copy()
                    bboxes[0, :, [0, 2]] = xh - raw_bboxes[0, :, [1, 3]]
                    bboxes[0, :, [1, 3]] = raw_bboxes[0, :, [2, 0]]
                elif rot == 2:
                    bboxes[0, :, [0, 1, 2, 3]] = mx.nd.array([[xw], [xh], [xw], [xh]], ctx=context) - bboxes[0, :, [2, 3, 0, 1]]
                elif rot == 3:
                    raw_bboxes = bboxes.copy()
                    bboxes[0, :, [0, 2]] = raw_bboxes[0, :, [1, 3]]
                    bboxes[0, :, [1, 3]] = xw - raw_bboxes[0, :, [2, 0]]
                raw_bboxes = bboxes.copy()
                bboxes[0, :, 0] = raw_bboxes[0, :, [0, 2]].min(axis=0)
                bboxes[0, :, 1] = raw_bboxes[0, :, [1, 3]].min(axis=0)
                bboxes[0, :, 2] = raw_bboxes[0, :, [0, 2]].max(axis=0)
                bboxes[0, :, 3] = raw_bboxes[0, :, [1, 3]].max(axis=0)
            bboxes[0, :, :] = gcv.data.transforms.bbox.flip(bboxes[0, :, :], (xw, xh), flip_x=flips[0], flip_y=flips[1])
            bboxes[0, :, 0::2] = (bboxes[0, :, 0::2] / (xw - 1)).clip(0.0, 1.0)
            bboxes[0, :, 1::2] = (bboxes[0, :, 1::2] / (xh - 1)).clip(0.0, 1.0)
            classes_list.append([
                int(classes[0, i].asscalar()) for i in range(classes.shape[1])
                    if classes[0, i].asscalar() >= 0.0

            ])
            scores_list.append([
                scores[0, i].asscalar() for i in range(classes.shape[1])
                    if classes[0, i].asscalar() >= 0.0

            ])
            bboxes_list.append([
                bboxes[0, i].asnumpy().tolist() for i in range(classes.shape[1])
                    if classes[0, i].asscalar() >= 0.0
            ])
        bboxes, scores, classes = weighted_boxes_fusion(bboxes_list, scores_list, classes_list)
        bboxes[:, 0::2] *= rw - 1
        bboxes[:, 1::2] *= rh - 1
        gcv.utils.viz.plot_bbox(raw, [
            bboxes[i] for i in range(classes.shape[0])
                if model.classes[int(classes[i])] == "wheat" and scores[i] > threshold
        ])
        plt.show()
Exemple #3
0
def evaluate_coco(model,
                  dataset,
                  coco,
                  eval_type="bbox",
                  limit=0,
                  image_ids=None):
    """Runs official COCO evaluation.
    dataset: A Dataset object with valiadtion data
    eval_type: "bbox" or "segm" for bounding box or segmentation evaluation
    limit: if not 0, it's the number of images to use for evaluation
    """
    # Pick COCO images from the dataset
    image_ids = image_ids or dataset.image_ids

    # Limit to a subset
    if limit:
        image_ids = image_ids[:limit]

    # Get corresponding COCO image IDs.
    coco_image_ids = [dataset.image_info[id]["id"] for id in image_ids]

    t_prediction = 0
    t_start = time.time()

    results = []
    for i, image_id in enumerate(image_ids):
        # Load image
        image = dataset.load_image(image_id)

        # Run detection
        t = time.time()
        r = model.detect([image], verbose=0)[0]
        t_prediction += (time.time() - t)

        # Convert results to COCO format
        # Cast masks to uint8 because COCO tools errors out on bool
        image_results = build_coco_results(dataset, coco_image_ids[i:i + 1],
                                           r["rois"], r["class_ids"],
                                           r["scores"],
                                           r["masks"].astype(np.uint8))
        results.extend(image_results)

    # Load results. This modifies results with additional attributes.
    coco_results = coco.loadRes(results)

    # Evaluate
    cocoEval = COCOeval(coco, coco_results, eval_type)
    cocoEval.params.imgIds = coco_image_ids
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()

    print("Prediction time: {}. Average {}/image".format(
        t_prediction, t_prediction / len(image_ids)))
    print("Total time: ", time.time() - t_start)
Exemple #4
0
    def load_and_process_image(self, path, imggen=None):
        """
        Load and preprocess a single image
        Args:
            path: path to image file.
            imggen: Image Generator for performing image perturbation

        Returns:
            image in ndarray
        """
        img = dataset.load_image(path, self.img_grayscale)
        if self.img_resized:
            img = dataset.resize_img(img, self.img_size)
        if imggen:
            img = imggen.random_transform(img)
        return img
Exemple #5
0
    def generator(self, n=0):
        i = 0
        name = None
        if n == 0:
            n = self.n

        while i < n:
            img_path = self.img_paths[i]

            if self.mode != 'test':
                label_val = int(self.label[i])
                label = np.array(onehot(label_val, self.num))
            else:
                label = self.label

            img = load_image(img_path)
            ori_img = resize_image(img, (self.width, self.height))
            img = np.float32(ori_img) / 255.0

            if self.mode == 'train':
                #flip image

                #rotate image in range of [-30, 30]
                if random.random() < cfg.p_rotate:
                    img_rotate = rotate(img, self.width, self.height)
                    yield img_rotate, label

                # # hsv image
                # if random.random() < cfg.p_hsv:
                #     img_hsv = random_hsv_transform(ori_img)
                #     img_hsv = np.float32(img_hsv) / 255.0
                #     yield img_hsv, label
                #
                #
                # #gamma
                # if random.random() < cfg.p_gamma:
                #     img_gamma = random_gamma_transform(ori_img)
                #     img_gamma = np.float32(img_gamma) / 255.0
                #     yield img_gamma, label

            if self.mode == 'test':
                yield img_path, img
            else:
                yield img, label

            i += 1
Exemple #6
0
def predict(myFile):
    # First, pass the path of the image
    dir_path = os.path.dirname(os.path.realpath(__file__))
    image_path = myFile
    filename = str(dir_path + '/' + image_path)
    image_size = 8
    num_channels = 6
    images = []
    # Reading the image using OpenCV
    image = dataset.load_image(filename)
    # Resizing the image to our desired size and preprocessing will be done exactly as done during training
    x_batch = [image]
    ## Let us restore the saved model
    sess = tf.Session()
    # Step-1: Recreate the network graph. At this step only graph is created.
    saver = tf.train.import_meta_graph('eval-model.meta')
    # Step-2: Now let's load the weights saved using the restore method.
    saver.restore(sess, tf.train.latest_checkpoint('./'))

    # Accessing the default graph which we have restored
    graph = tf.get_default_graph()

    # Now, let's get hold of the op that we can be processed to get the output.
    # In the original network y_pred is the tensor that is the prediction of the network
    y_pred = graph.get_tensor_by_name("y_pred:0")

    ## Let's feed the images to the input placeholders
    x = graph.get_tensor_by_name("x:0")
    y_true = graph.get_tensor_by_name("y_true:0")
    y_test_images = np.zeros((1, len(os.listdir('training_data'))))

    ### Creating the feed_dict that is required to be fed to calculate y_pred
    feed_dict_testing = {x: x_batch, y_true: y_test_images}
    result = sess.run(y_pred, feed_dict=feed_dict_testing)
    # result is of this format [probabiliy_of_rose probability_of_sunflower]
    #print(sess.run(tf.argmax(y_pred, 1), feed_dict={x: mnist.test.images}))
    """
    builder = tf.saved_model.builder.SavedModelBuilder("/Users/thomas/Personal-Projects/chess/evaluation/pos-model" )
    builder.add_meta_graph_and_variables(
        sess,
        [tf.saved_model.tag_constants.SERVING]
        )
    builder.save()
    """
    return result
Exemple #7
0
def test(images, model, is_reversed, size, context):
    print("Loading models...", flush=True)
    dis_a = PatchDiscriminator()
    dis_a.load_parameters("model/{}.dis_a.params".format(model), ctx=context)
    dis_b = PatchDiscriminator()
    dis_b.load_parameters("model/{}.dis_b.params".format(model), ctx=context)
    gen_ab = ResnetGenerator()
    gen_ab.load_parameters("model/{}.gen_ab.params".format(model), ctx=context)
    gen_ba = ResnetGenerator()
    gen_ba.load_parameters("model/{}.gen_ba.params".format(model), ctx=context)

    for path in images:
        print(path)
        raw = load_image(path)
        real = mx.image.resize_short(raw, size)
        real = mx.nd.image.normalize(mx.nd.image.to_tensor(real),
                                     mean=(0.5, 0.5, 0.5),
                                     std=(0.5, 0.5, 0.5))
        real = real.expand_dims(0).as_in_context(context)
        real_a_y, _ = dis_a(real)
        real_b_y, _ = dis_b(real)
        if is_reversed:
            fake, _ = gen_ba(real)
            rec, _ = gen_ab(fake)
        else:
            fake, _ = gen_ab(real)
            rec, _ = gen_ba(fake)
        fake_a_y, _ = dis_a(fake)
        fake_b_y, _ = dis_b(fake)
        print("Real score A:", mx.nd.sigmoid(real_a_y).mean().asscalar())
        print("Real score B:", mx.nd.sigmoid(real_b_y).mean().asscalar())
        print("Fake score A:", mx.nd.sigmoid(fake_a_y).mean().asscalar())
        print("Fake score B:", mx.nd.sigmoid(fake_b_y).mean().asscalar())
        plt.subplot(1, 3, 1)
        plt.imshow(raw.asnumpy())
        plt.axis("off")
        plt.subplot(1, 3, 2)
        plt.imshow(reconstruct_color(fake[0].transpose((1, 2, 0))).asnumpy())
        plt.axis("off")
        plt.subplot(1, 3, 3)
        plt.imshow(reconstruct_color(rec[0].transpose((1, 2, 0))).asnumpy())
        plt.axis("off")
        plt.show()
Exemple #8
0
def predict():

    # Equivalent to shuffling
    for test in test_input_names:

        # to get the right aspect ratio of the output
        loaded_image = dataset.load_image(test)
        height, width, channels = loaded_image.shape
        resize_height = int(height / (width / cfg.width))

        resized_image = cv2.resize(loaded_image, (cfg.width, resize_height))
        input_image = np.expand_dims(
            np.float32(resized_image[:cfg.height, :cfg.width]), axis=0) / 255.0

        st = time.time()
        print(input_image.shape)
        output_image = sess.run(network, feed_dict={net_input: input_image})

        run_time = time.time() - st

        output_image = np.array(output_image[0, :, :, :])
        output_image = helpers.reverse_one_hot(output_image)

        # this needs to get generalized
        # class_names_list, label_values = helpers.get_label_info(os.path.join(cfg.data_dir, "class_dict.csv"))

        out_vis_image = helpers.colour_code_segmentation(
            output_image, label_values)
        # print(out_vis_image.shape)
        # out_vis_image = cv2.resize(out_vis_image, (height, width))
        # out_vis_image[out_vis_image >= cfg.threshold*255] = 255
        # out_vis_image[out_vis_image < cfg.threshold*255] = 0
        #
        # save_img = cv2.cvtColor(np.uint8(loaded_image), cv2.COLOR_RGB2BGR)
        # transparent_image = np.append(np.array(save_img)[:, :, 0:3], out_vis_image[:, :, None], axis=-1)
        # transparent_image = Image.fromarray(transparent_image)

        file_name = utils.filepath_to_name(test)
        cv2.imwrite(
            cfg.base_dir + "%s/%s/%s_pred.png" % ("result", "test", file_name),
            cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR))
        # cv2.imwrite(cfg.base_dir + "%s/%s/%s_pred.png" % ("result", "Test", file_name), transparent_image)
    print("Finished!")
Exemple #9
0
def bounding_boxes():
    test_path = 'input/test_stg2/'
    model_x = load_model('fisheries-localization-x.h5')
    model_y = load_model('fisheries-localization-y.h5')

    files = os.listdir(test_path)
    for filename in files:
        file_path = test_path + filename
        img = io.imread(file_path)
        X = dataset.load_image(file_path)

        pred_x = model_x.predict(X)
        pred_y = model_y.predict(X)
        print(str(pred_x) + ',' + str(pred_y))

        plt.imshow(img)
        plt.gca().add_patch(pts.Circle((pred_x[0, 0], pred_y[0, 0]), 50))
        plt.gca().add_patch(
            pts.Rectangle((pred_x[0, 0], pred_y[0, 0]), 300, 300, fill=None))
        plt.show()
Exemple #10
0
def visualize():
    model = load_model('fisheries.h5')

    path = './input/train/ALB/img_00003.jpg'
    img = utils.load_img(path, target_size=(dataset.SIZE, dataset.SIZE))
    X = dataset.load_image(path)
    print(X.shape)
    # print(X[0])

    preds = model.predict(X)
    pred_class = preds.argmax()
    print('Predicted:' + str(preds))
    print('Predicted:' + dataset.TYPES[pred_class])

    plt.imshow(img)
    plt.show()

    idx = [2, 6, 8, 13]
    for i in idx:
        print(model.layers[i])
        heatmap = visualize_cam(model, i, [pred_class], img)
        plt.imshow(heatmap)
        plt.show()
Exemple #11
0
def get_seed_image(bpart, img_size, img_path, grayscale):
    """
    Utility method for getting a seeding image to visualize attention.
    Pick a random image from validation set, unless img_path is specified.
    :param bpart: Body part to pick.
    :param img_size: Image size to reshape to.
    :param img_path: path to image file.
    :param grayscale: Import image as grayscale or RGB
    :return: Keras model
    """
    _, valid_labeled, _, valid_path = dataset.load_dataframe()
    if not img_path:
        df_valid = dataset.build_dataframe(valid_labeled, valid_path)
        if bpart != "all":
            df_valid = df_valid[df_valid["body_part"] == bpart]
        rdm_row = df_valid.sample(1).iloc[0]
        img_path = rdm_row["path"]
        label = rdm_row["label"]
    else:
        label = valid_labeled[valid_labeled["path"] ==
                              img_path].iloc[0]["label"]
    img = dataset.load_image(img_path, grayscale)
    img = dataset.resize_img(img, img_size)
    return img, img_path, label
Exemple #12
0
def test(images, dims, threshold, plt_hw, seq_len, no_yolo, beam, beam_size,
         context):
    print("Loading model...")
    if not no_yolo:
        yolo = model_zoo.get_model('yolo3_darknet53_voc',
                                   pretrained=True,
                                   ctx=context)
    wpod = WpodNet()
    wpod.load_parameters("model/wpod_net.params", ctx=context)
    vocab = Vocabulary()
    vocab.load("model/vocabulary.json")
    ocr = OcrNet(plt_hw, vocab.size(), seq_len)
    ocr.load_parameters("model/ocr_net.params", ctx=context)
    for path in images:
        print(path)
        raw = load_image(path)
        if no_yolo:
            detect_plate(wpod, vocab, ocr, raw, dims, threshold, plt_hw, beam,
                         beam_size, context)
        else:
            ts = time.time()
            x, _ = data.transforms.presets.yolo.transform_test(raw, short=512)
            classes, scores, bboxes = yolo(x.as_in_context(context))
            bboxes[0, :, 0::2] = bboxes[0, :, 0::2] / x.shape[3] * raw.shape[1]
            bboxes[0, :, 1::2] = bboxes[0, :, 1::2] / x.shape[2] * raw.shape[0]
            vehicles = [
                fixed_crop(raw, bboxes[0, i]) for i in range(classes.shape[1])
                if (yolo.classes[int(classes[0, i].asscalar())] == 'car'
                    or yolo.classes[int(classes[0, i].asscalar())] == 'bus')
                and scores[0, i].asscalar() > 0.5
            ]
            print("yolo profiling: %f" % (time.time() - ts))
            for i, raw in enumerate(vehicles):
                print("vehicle[%d]:" % i)
                detect_plate(wpod, vocab, ocr, raw, dims, threshold, plt_hw,
                             beam, beam_size, context)
Exemple #13
0
    """
    mask = np.dstack((mask, mask, mask)) * np.array(color)
    mask = mask.astype(np.uint8)
    weighted_sum = cv2.addWeighted(mask, 0.5, image, 0.5, 0.)
    img = image.copy()
    ind = mask[:, :, 1] > 0    
    img[ind] = weighted_sum[ind]    
    return img

#%%
    
model_path = 'unet16_binary_20/model_0.pt'
model = get_model(model_path, model_type='UNet16', problem_type='binary')
#%% 
img_file_name = '3064_img.jpg'
img= load_image(img_file_name)
imshow(img)
#%%
img_transform  = Compose([
    ToTensor(),
    Normalize(mean=[0.1509,0.1509,0.1509], std=[0.0612,0.0612,0.0612])
])
    
#%%

   temp = variable(img_transform(img))

   input_img = torch.unsqueeze(temp, dim=0)

#%%
Exemple #14
0
def train():
    if cfg.class_balancing:
        print("Computing class weights for trainlabel ...")
        class_weights = utils.compute_class_weights(
            labels_dir=train_output_names, label_values=label_values)
        weights = tf.reduce_sum(class_weights * net_output, axis=-1)
        unweighted_loss = None
        unweighted_loss = tf.nn.softmax_cross_entropy_with_logits_v2(
            logits=network, labels=net_output)
        losses = unweighted_loss * class_weights
    else:
        losses = tf.nn.softmax_cross_entropy_with_logits_v2(logits=network,
                                                            labels=net_output)
    loss = tf.reduce_mean(losses)

    opt = tf.train.AdamOptimizer(cfg.lr).minimize(
        loss, var_list=[var for var in tf.trainable_variables()])

    sess.run(tf.global_variables_initializer())
    utils.count_params()

    # If a pre-trained ResNet is required, load the weights.
    # This must be done AFTER the variables are initialized with sess.run(tf.global_variables_initializer())
    if init_fn is not None:
        init_fn(sess)

    avg_scores_per_epoch = []
    avg_loss_per_epoch = []

    # Which validation images do we want
    val_indices = []
    num_vals = min(cfg.num_val_images, len(val_input_names))

    # Set random seed to make sure models are validated on the same validation images.
    # So you can compare the results of different models more intuitively.
    random.seed(16)
    val_indices = random.sample(range(0, len(val_input_names)), num_vals)

    # Do the training here
    for epoch in range(0, cfg.num_epochs):
        current_losses = []
        cnt = 0

        # Equivalent to shuffling
        id_list = np.random.permutation(len(train_input_names))

        num_iters = int(np.floor(len(id_list) / cfg.batch_size))
        st = time.time()
        epoch_st = time.time()

        for i in range(num_iters):
            # st=time.time()
            input_image_batch = []
            output_image_batch = []

            # Collect a batch of images
            for j in range(cfg.batch_size):
                index = i * cfg.batch_size + j
                id = id_list[index]
                input_image = dataset.load_image(train_input_names[id])
                output_image = dataset.load_image(train_output_names[id])

                h, w, _ = input_image.shape
                new_h, new_w = dataset.getTrainSize(h, w)

                with tf.device('/cpu:0'):
                    input_image, output_image = dataset.data_augmentation(
                        input_image, output_image, new_h, new_w)

                    # Prep the data. Make sure the labels are in one-hot format
                    input_image = np.float32(input_image) / 255.0
                    output_image = np.float32(
                        helpers.one_hot_it(label=output_image,
                                           label_values=label_values))

                    input_image_batch.append(
                        np.expand_dims(input_image, axis=0))
                    output_image_batch.append(
                        np.expand_dims(output_image, axis=0))

            # ***** THIS CAUSES A MEMORY LEAK AS NEW TENSORS KEEP GETTING CREATED *****
            # input_image = tf.image.crop_to_bounding_box(input_image, offset_height=0, offset_width=0,
            #                                               target_height=args.crop_height, target_width=args.crop_width).eval(session=sess)
            # output_image = tf.image.crop_to_bounding_box(output_image, offset_height=0, offset_width=0,
            #                                               target_height=args.crop_height, target_width=args.crop_width).eval(session=sess)
            # ***** THIS CAUSES A MEMORY LEAK AS NEW TENSORS KEEP GETTING CREATED *****

            # memory()
            # print(cfg.batch_size)
            if cfg.batch_size == 1:
                input_image_batch = input_image_batch[0]
                output_image_batch = output_image_batch[0]
            else:
                input_image_batch = np.squeeze(
                    np.stack(input_image_batch, axis=1))
                output_image_batch = np.squeeze(
                    np.stack(output_image_batch, axis=1))

            # print(input_image_batch.shape)
            # Do the training
            _, current = sess.run([opt, loss],
                                  feed_dict={
                                      net_input: input_image_batch,
                                      net_output: output_image_batch
                                  })
            current_losses.append(current)
            cnt = cnt + cfg.batch_size
            if cnt % 20 == 0:
                string_print = "Epoch = %d Count = %d Current_Loss = %.4f Time = %.2f" % (
                    epoch, cnt, current, time.time() - st)
                utils.LOG(string_print)
                st = time.time()

        mean_loss = np.mean(current_losses)
        avg_loss_per_epoch.append(mean_loss)

        # Create directories if needed
        if not os.path.isdir(cfg.base_dir + "%s/%s/%04d" %
                             ("checkpoints", cfg.model, epoch)):
            os.makedirs(cfg.base_dir + "%s/%s/%04d" %
                        ("checkpoints", cfg.model, epoch))

        # Save latest checkpoint to same file name
        print("Saving latest checkpoint")
        saver.save(sess, model_checkpoint_name)

        if val_indices != 0 and epoch % cfg.checkpoint_step == 0:
            print("Saving checkpoint for this epoch")
            saver.save(
                sess, cfg.base_dir + "%s/%s/%04d/model.ckpt" %
                ("checkpoints", cfg.model, epoch))

        if epoch % cfg.validation_step == 0:
            print("Performing validation")
            target = open(
                cfg.base_dir + "%s/%s/%04d/val_scores.csv" %
                ("checkpoints", cfg.model, epoch), 'w')
            target.write(
                "val_name, avg_accuracy, precision, recall, f1 score, mean iou, %s\n"
                % (class_names_string))

            scores_list = []
            class_scores_list = []
            precision_list = []
            recall_list = []
            f1_list = []
            iou_list = []

            # Do the validation on a small set of validation images
            for ind in val_indices:
                input_image = dataset.load_image(val_input_names[ind])
                output_image = dataset.load_image(val_output_names[ind])

                h, w, _ = input_image.shape
                new_h, new_w = dataset.getTrainSize(h, w)

                input_image, output_image = utils.random_crop(
                    input_image, output_image, new_h, new_w)

                input_image = np.expand_dims(np.float32(input_image),
                                             axis=0) / 255.0

                gt = helpers.reverse_one_hot(
                    helpers.one_hot_it(output_image, label_values))

                # st = time.time()

                output_image = sess.run(network,
                                        feed_dict={net_input: input_image})

                output_image = np.array(output_image[0, :, :, :])
                output_image = helpers.reverse_one_hot(output_image)
                out_vis_image = helpers.colour_code_segmentation(
                    output_image, label_values)

                accuracy, class_accuracies, prec, rec, f1, iou = utils.evaluate_segmentation(
                    pred=output_image, label=gt, num_classes=num_classes)

                file_name = utils.filepath_to_name(val_input_names[ind])
                target.write("%s, %f, %f, %f, %f, %f" %
                             (file_name, accuracy, prec, rec, f1, iou))
                for item in class_accuracies:
                    target.write(", %f" % (item))
                target.write("\n")

                scores_list.append(accuracy)
                class_scores_list.append(class_accuracies)
                precision_list.append(prec)
                recall_list.append(rec)
                f1_list.append(f1)
                iou_list.append(iou)

                gt = helpers.colour_code_segmentation(gt, label_values)

                file_name = os.path.basename(val_input_names[ind])
                file_name = os.path.splitext(file_name)[0]
                cv2.imwrite(
                    cfg.base_dir + "%s/%s/%04d/%s_pred.png" %
                    ("checkpoints", cfg.model, epoch, file_name),
                    cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR))
                cv2.imwrite(
                    cfg.base_dir + "%s/%s/%04d/%s_gt.png" %
                    ("checkpoints", cfg.model, epoch, file_name),
                    cv2.cvtColor(np.uint8(gt), cv2.COLOR_RGB2BGR))

            target.close()

            avg_score = np.mean(scores_list)
            class_avg_scores = np.mean(class_scores_list, axis=0)
            avg_scores_per_epoch.append(avg_score)
            avg_precision = np.mean(precision_list)
            avg_recall = np.mean(recall_list)
            avg_f1 = np.mean(f1_list)
            avg_iou = np.mean(iou_list)

            print("\nAverage validation accuracy for epoch # %04d = %f" %
                  (epoch, avg_score))
            print("Average per class validation accuracies for epoch # %04d:" %
                  (epoch))
            for index, item in enumerate(class_avg_scores):
                print("%s = %f" % (class_names_list[index], item))
            print("Validation precision = ", avg_precision)
            print("Validation recall = ", avg_recall)
            print("Validation F1 score = ", avg_f1)
            print("Validation IoU score = ", avg_iou)

        epoch_time = time.time() - epoch_st
        remain_time = epoch_time * (cfg.num_epochs - 1 - epoch)
        m, s = divmod(remain_time, 60)
        h, m = divmod(m, 60)
        if s != 0:
            train_time = "Remaining training time = %d hours %d minutes %d seconds\n" % (
                h, m, s)
        else:
            train_time = "Remaining training time : Training completed.\n"
        utils.LOG(train_time)
        scores_list = []

    utils.drawLine(range(cfg.num_epochs),
                   avg_scores_per_epoch,
                   cfg.base_dir + 'checkpoints/' + cfg.model +
                   '/accuracy_vs_epochs.png',
                   title='Average validation accuracy vs epochs',
                   xlabel='Epoch',
                   ylabel='Avg. val. accuracy')
    utils.drawLine(range(cfg.num_epochs),
                   avg_loss_per_epoch,
                   cfg.base_dir + 'checkpoints/' + cfg.model +
                   '/loss_vs_epochs.png',
                   title='Average loss vs epochs',
                   xlabel='Epoch',
                   ylabel='Current loss')
Exemple #15
0
import numpy as np
import matplotlib.pyplot as plt
from albumentations.pytorch.transforms import img_to_tensor

arrt = np.random.randn(500, 500)
tensor = img_to_tensor(arrt)
mean=(0.485, 0.456, 0.406)
std=(0.229, 0.224, 0.225)

#new_img = img - mean /std
#old img = 

model_path = 'pretrained/unet16_instruments_20/model_1.pt'
model = get_model(model_path, model_type='UNet16', problem_type='instruments')

img_l =  load_image('dataset/instrument_dataset_1/left_frames/frame000.png')
input_img_l = torch.unsqueeze(img_to_tensor(img_transform(p=1)(image=img_l)['image']), dim=0)
mask_l = model(input_img_l)

img_r =  load_image('dataset/instrument_dataset_1/right_frames/frame000.png')
input_img_r = torch.unsqueeze(img_to_tensor(img_transform(p=1)(image=img_r)['image']), dim=0)
mask_r = model(input_img_r)

mask = jaccard(mask_l, mask_r)


im_seg = mask.data[0].cpu().numpy()[0]

#mask_array = (im_seg * std) + mean

plt.imshow(im_seg > 0)
Exemple #16
0
def main(args):
	dataset_path = './BDCI2017-jiage-Semi'
	model_path='./model'
	model_name='FPN_ResNet_itr100000'
	model_file = os.path.join(model_path,'%s.ckpt'%model_name)
	period = 'test'
	csv_path = './CSV'
	class_num = 5
	sample_num = 3
	patch_size=256
	sample_size = 1024
	rate = sample_size/patch_size
	batch_size=1
	accuracy = 0
	radius = 8
	eps = 0.2*0.2
	os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu

	file_path = os.path.join(dataset_path,'test')
	print('NO.1 test sample is loading...')
	image = dataset.load_image(file_path,index=1,load_label=False)
	ori_row = image.shape[0]
	ori_col = image.shape[1]
	image = cv2.resize(image,(np.int32(image.shape[1]/rate),np.int32(image.shape[0]/rate)))
	print('loading finished')
	row = image.shape[0]
	col = image.shape[1]
	
	x = tf.placeholder(tf.float32,[batch_size,patch_size,patch_size,3])
	net = factory.FPN_ResNet(x,class_num)

	net_sub = tf.slice(net,[0,0,0,1],[1,256,256,4])
	#CRF
	net_softmax = tf.nn.softmax(net_sub)#########attention net not net_sub
	x_int = (1+x)*128
	x_int = tf.cast(x_int,dtype=tf.uint8)
	result = tf.py_func(utils.dense_crf, [net_softmax, x_int], tf.float32)
	# result = tf.argmax(result,axis=-1)
	sess = tf.Session()
	sess.run(tf.global_variables_initializer())
	saver = tf.train.Saver()
	print('start restore parameter...')
	saver.restore(sess,model_file)
	print('parameter restore finished!')
	offs=int(patch_size/4)
	offe=int(3*patch_size/4)
	for n in range(1,sample_num+1):
		if n!=1:
			print('NO.%d test sample is loading...'%n)
			image = dataset.load_image(file_path,index=n,load_label=False)
			ori_row = image.shape[0]
			ori_col = image.shape[1]
			image = cv2.resize(image,(np.int32(image.shape[1]/rate),np.int32(image.shape[0]/rate)))
			row = image.shape[0]
			col = image.shape[1]
			print('loading finished')
		print('float transforming...')
		image = np.float32(image)/128.0-1.0
		vote = np.zeros((row,col,class_num-1))#original class_num-1
		
		sub_image = image[0:patch_size,0:patch_size,:]
		sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
		cls_result = sess.run(result,feed_dict={x:sub_image})
		vote[0:offe,0:offe]\
			 = cls_result[0,0:offe,0:offe]
		for c in range(0,col-patch_size,int(patch_size/2)):
			sub_image = image[0:patch_size,c:c+patch_size,:]
			sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
			cls_result = sess.run(result,feed_dict={x:sub_image})
			vote[0:offe,c+offs:c+offe] \
				= cls_result[0,0:offe,offs:offe]
		sub_image = image[0:patch_size,col-patch_size:col,:]
		sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
		cls_result = sess.run(result,feed_dict={x:sub_image})
		vote[0:offe,col-patch_size+offs:col]\
			 = cls_result[0,0:offe,offs:]
		for r in range(0,row-patch_size,int(patch_size/2)):
			print('sample%d,row:%d patch is processing'%(n,r))
			sub_image = image[r:r+patch_size,0:patch_size,:]
			sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
			cls_result = sess.run(result,feed_dict={x:sub_image})
			vote[r+offs:r+offe,0:offe] \
				= cls_result[0,offs:offe,0:offe]
			for c in range(0,col-patch_size,int(patch_size/2)):
				sub_image = image[r:r+patch_size,c:c+patch_size,:]
				sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
				cls_result = sess.run(result,feed_dict={x:sub_image})
				vote[r+offs:r+offe,c+offs:c+offe]\
					 = cls_result[0,offs:offe,offs:offe]
			sub_image = image[r:r+patch_size,col-patch_size:col,:]
			sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
			cls_result = sess.run(result,feed_dict={x:sub_image})
			vote[r+offs:r+offe,col-patch_size+offs:col] \
				= cls_result[0,offs:offe,offs:patch_size]
			
		sub_image = image[row-patch_size:row,0:patch_size,:]
		sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
		cls_result = sess.run(result,feed_dict={x:sub_image})
		vote[row-patch_size+offs:row,0:offe]\
			 = cls_result[0,offs:,0:offe]
		for c in range(0,col-patch_size,int(patch_size/2)):
			sub_image = image[row-patch_size:row,c:c+patch_size,:]
			sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
			cls_result = sess.run(result,feed_dict={x:sub_image})
			vote[row-patch_size+offs:row,c+offs:c+offe] \
				= cls_result[0,offs:patch_size,offs:offe]
		sub_image = image[row-patch_size:row,col-patch_size:col,:]
		sub_image = np.reshape(sub_image,[1,patch_size,patch_size,3])
		cls_result = sess.run(result,feed_dict={x:sub_image})
		vote[row-patch_size+offs:row,col-patch_size+offs:col]\
			 = cls_result[0,offs:,offs:]

		# gray_img = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)		
		# for channel in range(0,class_num-1):
		# 	temp_vote = vote[:,:,channel]
		# 	vote[:,:,channel] = guidedfilter.guidedfilter(temp_vote,gray_img,radius,eps)
		# vote_softmax = vote.copy()
		# vote_softmax = vote_softmax[:,:,[1,3]]
		# vote_pb = np.argmax(vote_softmax,axis=-1)
		# vote_pb[vote_pb==1]=3
		# vote_pb[vote_pb==0]=1
		vote = np.argmax(vote,axis=-1)
		vote = np.uint8(vote)
		# vote[vote==0]=vote_pb[vote==0]
		# vote = cv2.medianBlur(vote,7)
		vote = vote+1
		copy_vote = vote.copy()
		copy_vote[vote==2] = 4
		copy_vote[vote==3] = 2
		copy_vote[vote==4] = 3
		vote = copy_vote.copy()
		vote = cv2.resize(vote,(ori_col,ori_row),cv2.INTER_NEAREST)
		
		print('%d test sample is writing into csv...'%n)
		write_csv(csv_path,index=n,label=vote)
		print('%d test result is writing into png...'%n)
		save_y(file_path,n,vote)
		print('writing finished')
	print('accuracy: %f'%accuracy)
        male_val_loader, EPOCHS)
    print('\nTRAINING FEMALE MODEL')
    female_model, _, female_optimizer, female_scheduler = train(
        'female', female_model, female_optimizer, female_scheduler,
        female_train_loader, female_val_loader, EPOCHS)

# # Generate Output

# In[ ]:

test_df = pd.read_csv('test.csv')
submission = pd.read_csv('sample_submission.csv')
progress = tqdm.tqdm(total=len(test_df), desc='Sample', position=0)
for key, row in test_df.iterrows():
    img_path = os.path.join('images', row['fileName'])
    img = load_image(img_path)
    img = img.unsqueeze(0)
    img = img.cuda()

    if not GENDER_SENSITIVE:
        boneage = mixed_model(img)
    elif row['male'] == True:
        boneage = male_model(img)
    else:
        boneage = female_model(img)

    boneage = float(boneage.view(-1).detach().cpu()[0])
    submission.loc[submission.fileName == row['fileName'],
                   'boneage'] = boneage if boneage > 0 else 0
    progress.update(1)
    def generator(self, n=0):
        i = 0

        if n == 0:
            n = self.n

        while i < n:
            img_path = self.img_paths[i]

            img = load_image(img_path)

            h, w, _ = img.shape
            size = [w, h]
            ori_img = cv2.resize(img, (self.width, self.height),
                                 interpolation=cv2.INTER_NEAREST)

            if self.mode != 'test':
                ann_path = self.label_paths[i]
                ann = load_image(ann_path)
                ann = cv2.resize(ann, (self.width, self.height),
                                 interpolation=cv2.INTER_NEAREST)

            # img = cv2.copyMakeBorder(img, 0, 512 - h, 0, 512 - w, cv2.BORDER_CONSTANT)
            # heatmap = cv2.resize(heatmap, (512, 512))

            img = np.float32(ori_img) / 255.0

            if self.mode == 'train':
                #flip image
                if random.random() < cfg.p_flip:
                    img_flip, ann_flip = self.flip(img, ann)
                    ann_flip = np.float32(
                        one_hot_it(label=ann_flip,
                                   label_values=self.label_value))
                    yield img_path, size, img_flip, ann_flip

                #rotate image in range of [-30, 30]
                if random.random() < cfg.p_rotate:
                    img_rotate, ann_rotate = self.rotate(img, ann)
                    ann_rotate = np.float32(
                        one_hot_it(label=ann_rotate,
                                   label_values=self.label_value))
                    yield img_path, size, img_rotate, ann_rotate

                # hsv image
                if random.random() < cfg.p_hsv:
                    img_hsv = self.random_hsv_transform(ori_img)
                    img_hsv = np.float32(img_hsv) / 255.0
                    ann_hsv = np.float32(
                        one_hot_it(label=ann, label_values=self.label_value))
                    yield img_path, size, img_hsv, ann_hsv

                #gamma
                if random.random() < cfg.p_gamma:
                    img_gamma = self.random_gamma_transform(ori_img)
                    img_gamma = np.float32(img_gamma) / 255.0
                    ann_gamma = np.float32(
                        one_hot_it(label=ann, label_values=self.label_value))
                    yield img_path, size, img_gamma, ann_gamma

            elif self.mode == 'val':
                ann_convert = np.float32(
                    one_hot_it(label=ann, label_values=self.label_value))
                yield img_path, size, img, ann_convert

            else:
                yield img_path, size, img

            i += 1
Exemple #19
0
def val():
    # Create directories if needed
    if not os.path.isdir(cfg.base_dir + "%s/%s" % ("result", "Val")):
        os.makedirs(cfg.base_dir + "%s/%s" % ("result", "Val"))

    target = open(cfg.base_dir + "%s/%s/val_scores.csv" % ("result", "Val"),
                  'w')
    target.write(
        "val_name, avg_accuracy, precision, recall, f1 score, mean iou, %s\n" %
        (class_names_string))
    scores_list = []
    class_scores_list = []
    precision_list = []
    recall_list = []
    f1_list = []
    iou_list = []
    run_times_list = []

    # Run testing on ALL test images
    for ind in range(len(val_input_names)):
        sys.stdout.write("\rRunning test image %d / %d" %
                         (ind + 1, len(val_input_names)))
        sys.stdout.flush()

        input_image = np.expand_dims(np.float32(
            dataset.load_image(val_input_names[ind])[:cfg.height, :cfg.width]),
                                     axis=0) / 255.0
        gt = dataset.load_image(val_output_names[ind])[:cfg.height, :cfg.width]
        gt = helpers.reverse_one_hot(helpers.one_hot_it(gt, label_values))

        st = time.time()
        output_image = sess.run(network, feed_dict={net_input: input_image})

        run_times_list.append(time.time() - st)

        output_image = np.array(output_image[0, :, :, :])
        output_image = helpers.reverse_one_hot(output_image)
        out_vis_image = helpers.colour_code_segmentation(
            output_image, label_values)

        accuracy, class_accuracies, prec, rec, f1, iou = utils.evaluate_segmentation(
            pred=output_image, label=gt, num_classes=num_classes)

        file_name = utils.filepath_to_name(val_input_names[ind])
        target.write("%s, %f, %f, %f, %f, %f" %
                     (file_name, accuracy, prec, rec, f1, iou))
        for item in class_accuracies:
            target.write(", %f" % (item))
        target.write("\n")

        scores_list.append(accuracy)
        class_scores_list.append(class_accuracies)
        precision_list.append(prec)
        recall_list.append(rec)
        f1_list.append(f1)
        iou_list.append(iou)

        gt = helpers.colour_code_segmentation(gt, label_values)

        cv2.imwrite(
            cfg.base_dir + "%s/%s/%s_pred.png" % ("result", "Val", file_name),
            cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR))
        cv2.imwrite(
            cfg.base_dir + "%s/%s/%s_gt.png" % ("result", "Val", file_name),
            cv2.cvtColor(np.uint8(gt), cv2.COLOR_RGB2BGR))

    target.close()

    avg_score = np.mean(scores_list)
    class_avg_scores = np.mean(class_scores_list, axis=0)
    avg_precision = np.mean(precision_list)
    avg_recall = np.mean(recall_list)
    avg_f1 = np.mean(f1_list)
    avg_iou = np.mean(iou_list)
    avg_time = np.mean(run_times_list)
    print("Average test accuracy = ", avg_score)
    print("Average per class test accuracies = \n")
    for index, item in enumerate(class_avg_scores):
        print("%s = %f" % (class_names_list[index], item))
    print("Average precision = ", avg_precision)
    print("Average recall = ", avg_recall)
    print("Average F1 score = ", avg_f1)
    print("Average mean IoU score = ", avg_iou)
    print("Average run time = ", avg_time)