def next_test_batch(self):
        img, annotations = self.next_test_sample()
        predictions_per_cell = Configuration.ANCHORS.shape[0] * (
            len(Configuration.CLASS_LABELS.keys()) + 5)
        #annotations = annotations[['label','xc','yc','w','h']]
        y_true = pd.DataFrame(
            np.zeros(shape=(Configuration.GRID_SIZE * Configuration.GRID_SIZE,
                            predictions_per_cell)))
        #Get the size of the cell
        cell_size = 1.0 / Configuration.GRID_SIZE
        for i in range(annotations.shape[0]):
            l = 0
            m = 0
            cx = 0.0
            cy = 0.0
            gnd_truth_box = annotations.iloc[i]
            while cx <= gnd_truth_box[5]:
                cx += cell_size
                l += 1
            cx -= cell_size
            l -= 1
            while cy <= gnd_truth_box[6]:
                cy += cell_size
                m += 1
            cy -= cell_size
            m -= 1

            xc = gnd_truth_box[5]
            yc = gnd_truth_box[6]
            #            xc = gnd_truth_box[5] - cx
            #            yc = gnd_truth_box[6] - cy
            w = gnd_truth_box[7]
            h = gnd_truth_box[8]
            labels = []
            for label in Configuration.CLASS_LABELS.keys():
                if label == gnd_truth_box[0]:
                    labels.append(1)
                else:
                    labels.append(0)
            data = []
            for j in range(Configuration.ANCHORS.shape[0]):
                anchor_box = Configuration.ANCHORS[j]
                anchor_box = np.append(gnd_truth_box[5:7], anchor_box, axis=0)
                anchor_box = cvt_coord_to_diagonal(anchor_box)
                c = intersection_over_union(anchor_box, gnd_truth_box[1:5])
                #                c = 1.0
                data = data + [xc, yc, w, h, c] + labels
            y_true.iloc[Configuration.GRID_SIZE * m + l] = pd.Series(data)

        #y true contains [xc,yc,w,h,c]+one_hot_labels
        return img, np.reshape(np.array(y_true),
                               newshape=(Configuration.GRID_SIZE,
                                         Configuration.GRID_SIZE,
                                         predictions_per_cell))
    def predict_disease(self, path):
        model = get_pre_trained_model()
        model.load_weights('trained_weights/model_weights_16.h5')
        # model.summary()

        IMAGE_H, IMAGE_W = Configuration.IMAGE_HEIGHT, Configuration.IMAGE_WIDTH
        N_CLASSES = len(Configuration.CLASS_LABELS.keys())
        N_ANCHORS = Configuration.ANCHORS.shape[0]
        GRID_SIZE = Configuration.GRID_SIZE
        # Loading Single Image
        x = cv2.imread(path)
        original_h, original_w, _ = x.shape
        # By Default cv2 Loads in BGR format so rotate to RGB
        x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)
        # Resize Image
        x = cv2.resize(x, (IMAGE_H, IMAGE_W))
        # Normalize
        x = x / 255.0
        # Reshape into [BATCH_SIZE,H,W,C]
        x = np.reshape(x, (-1, IMAGE_H, IMAGE_W, 3))
        y_pred = model.predict(x)
        K.clear_session()
        y_pred_1 = y_pred[0]
        x_1 = x[0]
        y_pred_1 = non_max_suppression(
            np.reshape(y_pred_1,
                       (GRID_SIZE * GRID_SIZE * N_ANCHORS, 5 + N_CLASSES)),
            0.4, 0.2)
        result = []
        for i in range(y_pred_1.shape[0]):
            box = y_pred_1[i, 0:4]
            box = np.array(util.cvt_coord_to_diagonal(box)).reshape(-1, 2)
            box[:, 0] = original_w * box[:, 0]
            box[:, 1] = original_h * box[:, 1]
            box = box.reshape(-1)
            box = np.ceil(box).astype(int)
            obj = {
                'box': box.tolist(),
                'infected': float(y_pred_1[i, 5]),
                'not_infected': float(y_pred_1[i, 6]),
                'objectness': float(y_pred_1[i, 4])
            }
            result.append(obj)
        return result
示例#3
0
def predict():
    # initialize the data dictionary that will be returned from the
    # view
    y_pred_1 = {"success": False}

    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))
            original_w, original_h = image.size

            # preprocess the image and prepare it for classification
            image = prepare_image(image, target=(IMAGE_H, IMAGE_W))

            # classify the input image and then initialize the list
            # of predictions to return to the client
            y_pred = model.predict(image)
            y_pred_1 = y_pred[0]
            y_pred_1 = np.reshape(
                y_pred_1, (GRID_SIZE * GRID_SIZE * N_ANCHORS, 5 + N_CLASSES))
            for i in range(y_pred_1.shape[0]):
                y_pred_1[i, 0:4] = util.cvt_coord_to_diagonal(y_pred_1[i, 0:4])

            y_pred_1 = non_max_suppression(y_pred_1, 0.5, 0.3)
            result = []
            for i in range(y_pred_1.shape[0]):
                box = y_pred_1[i, 0:4]
                box = box.reshape(-1, 2)
                box = np.clip(box, a_min=0, a_max=1.0)
                box[:, 0] = original_w * box[:, 0]
                box[:, 1] = original_h * box[:, 1]
                box = box.reshape(-1)
                box = np.ceil(box).astype(int)
                obj = {
                    'box': box.tolist(),
                    'infected': float(y_pred_1[i, 5]),
                    'not_infected': float(y_pred_1[i, 6]),
                    'objectness': float(y_pred_1[i, 4])
                }
                result.append(obj)
    # return the data dictionary as a JSON response
    return flask.jsonify(result)
    def train(self, data):
        #Initialization
        iteration = 1
        avg_error = 10000000000

        #Initialize cluster vectors to random points from data
        idx = np.random.randint(low=0, high=data.shape[0], size=self.k)
        self.cluster_vectors = data[idx, :]

        #Cluster Labels for data
        cluster_labels = np.zeros(shape=(data.shape[0]), dtype=np.int32)

        while True:
            #divide the set of training vectors into K clusters using
            #minimum error criteria
            print('Iteration:', iteration)
            for i in range(data.shape[0]):
                bnd_box = data[i]
                bnd_box = np.concatenate(([0, 0], bnd_box))
                bnd_box = cvt_coord_to_diagonal(bnd_box)
                error_iou = 0
                #assign bnd_box a cluster based on iou
                for j in range(self.k):
                    training_vector = np.concatenate(
                        ([0, 0], self.cluster_vectors[j]))

                    training_vector = cvt_coord_to_diagonal(training_vector)

                    e_iou = 1.0 - intersection_over_union(
                        bnd_box, training_vector)
                    if j == 0:
                        error_iou = e_iou
                        cluster_labels[i] = j
                    elif e_iou < error_iou:
                        error_iou = e_iou
                        cluster_labels[i] = j

            #compute average distortion
            error = 0.0
            xc = [0 for _ in range(self.k)]
            yc = [0 for _ in range(self.k)]
            coordinate_counts = [0 for _ in range(self.k)]
            average_iou = 0
            for i in range(data.shape[0]):
                bnd_box = data[i]

                xc[cluster_labels[i]] += bnd_box[0]
                yc[cluster_labels[i]] += bnd_box[1]
                coordinate_counts[cluster_labels[i]] += 1

                bnd_box = np.concatenate(([0, 0], bnd_box))
                bnd_box = cvt_coord_to_diagonal(bnd_box)

                training_vector = self.cluster_vectors[cluster_labels[i]]
                training_vector = np.concatenate(([0, 0], training_vector))
                training_vector = cvt_coord_to_diagonal(training_vector)
                iou = intersection_over_union(bnd_box, training_vector)
                average_iou += iou
                error += (1.0 - iou)

            error = error / data.shape[0]
            average_iou = average_iou / data.shape[0]

            #Make new cluster vectors ie find new centroids
            new_clusters = []
            for i in range(self.k):
                if coordinate_counts[i] == 0:
                    new_clusters.append([
                        self.cluster_vectors[i][0], self.cluster_vectors[i][1]
                    ])
                else:
                    new_clusters.append([
                        xc[i] / coordinate_counts[i],
                        yc[i] / coordinate_counts[i]
                    ])
            self.cluster_vectors = np.array(new_clusters)

            if np.abs(avg_error - error
                      ) / error < self.e or iteration >= self.max_iteration:
                break
            else:
                avg_error = error
                iteration += 1

        print("K: ", self.k, ' AvgError: ',
              np.abs(avg_error - error) / error, 'Iterations: ', iteration,
              'AvgIou: ', average_iou)
        return self.cluster_vectors, cluster_labels, average_iou
示例#5
0
    def test_cvt_coord_to_diagonal(self):
        x = [3, 8, 5, 1]
        y_true = [0.5, 7.5, 5.5, 8.5]
        y = cvt_coord_to_diagonal(x).tolist()

        self.assertEqual(y_true, y)
                       'data/leaf_data_v2/test',
                       shuffle=True,
                       histogram_equ=False,
                       brightness_range=None)
#%%
x, y = dataset.next_test_batch()
#%%
y_pred = model.predict(x)
#%%
y_pred_1 = y_pred[0]
x_1 = x[0]
#%%
y_pred_1 = np.reshape(y_pred_1,
                      (GRID_SIZE * GRID_SIZE * N_ANCHORS, N_CLASSES + 5))
for i in range(y_pred_1.shape[0]):
    y_pred_1[i, 0:4] = util.cvt_coord_to_diagonal(y_pred_1[i, 0:4])
y_pred_1[:, 0:4] = np.clip(y_pred_1[:, 0:4], a_min=0.0, a_max=1.0)
#%%
y_pred_1 = non_max_suppression(y_pred_1, 0.5, 0.3)
#%%
img = x_1
for i in range(y_pred_1.shape[0]):
    box = y_pred_1[i, 0:4]
    box = np.ceil(IMAGE_H * box).astype(int)
    pt1 = (box[0], box[1])
    pt2 = (box[2], box[3])
    cv2.rectangle(img, pt1, pt2, (
        255,
        0,
        0,
    ), 3)