def _x_aug(self, X):
     if self.aug_time == 0:
         X = [preprocess_input(x) for x in X]
         yield np.array(X)
     else: 
         for _ in range(self.aug_time):
             X = self.seq.augment_images(X)
             X = [preprocess_input(x) for x in X]
             yield np.array(X)
Пример #2
0
def predict_breast_cancer(image):
    path = str(os.getcwd()) + "/Breast_Cancer/"
    image = cv2.imread(str("/home/ubuntu/ml_apis/images/") + str(image))
    image = cv2.resize(image, (96, 96))
    with Image(filename=image) as img:
        img.format = 'tif'
        img.save(filename=path + 'temp.tif')
    test_files = glob(path + '*.tif')
    model = get_model_classif_nasnet()
    batch_size = 32
    h5_path = path + "model.h5"
    model.load_weights(h5_path)
    preds = []
    ids = []
    for batch in chunker(test_files, batch_size):
        X = [preprocess_input(cv2.imread(x)) for x in batch]
        ids_batch = [get_id_from_file_path(x) for x in batch]
        X = np.array(X)
        preds_batch = (
            (model.predict(X).ravel() *
             model.predict(X[:, ::-1, :, :]).ravel() *
             model.predict(X[:, ::-1, ::-1, :]).ravel() *
             model.predict(X[:, :, ::-1, :]).ravel())**0.25).tolist()
        preds += preds_batch
        ids += ids_batch
    os.remove(path + "temp.tif")
    if preds[0] >= 0.7:
        return True
    else:
        return False
Пример #3
0
def model_train_eval(model, X, y, e):
    X = [preprocess_input(x) for x in X]
    X = np.array(X)
    hr_preds = model.predict(X)
    hr_preds = np.exp(hr_preds)
    ci = concordance_index(y, -hr_preds, e)
    return ci
    def extract_batch(self, image_path_list, cnn_model_type='InceptionV3'):
        if cnn_model_type== 'InceptionV3':
            target_size = (299, 299,3)
            # feature_size = 2048

        elif  cnn_model_type== 'nasnet':
            target_size = (331, 331,3)
            # feature_size = 4032

        batch_size = len(image_path_list)

        X = np.zeros((batch_size,) + target_size )

        for img_idx, image_path in enumerate(image_path_list):
            img = image.load_img(image_path, target_size=target_size[0:2])
            array = image.img_to_array(img)
            X[img_idx] = array
        # x = np.expand_dims(x, axis=0)

        if cnn_model_type == 'InceptionV3':
            X = inception_v3.preprocess_input(X)
        elif cnn_model_type == 'nasnet':
            X = nasnet.preprocess_input(X)

        # Get the prediction.
        features_batch = self.model.predict(X)

        return features_batch
Пример #5
0
def main():
    file_name = args["i"]
    sample_x, sample_y = loadTrainSeq(file_name)
    model = get_classification_model(train_length)
    h5_path = "deepHGT.h5"
    model.load_weights(h5_path)
    tmp_y = []
    for batch in chunker(sample_x, batch_size):
        batch.sort()
        X = sample_x[batch]
        X = [x[:train_length] for x in X]
        X = [preprocess_input(x) for x in X]
        y_pred_keras = model.predict(np.array(X)).ravel()
        tmp_y.append(y_pred_keras)
    pred_y = []
    fo = open('prediction.txt', 'w')
    for batch in tmp_y:
        for i in range(batch.shape[0]):
            fo.write(str(batch[i]) + '\n')
            pred_y.append(batch[i])
    fo.close()
    fpr, tpr, thresholds = metrics.roc_curve(sample_y, np.array(pred_y))
    auc = round(metrics.auc(fpr, tpr), 3)
    #precision, recall, _ = precision_recall_curve(sample_y, np.array(pred_y))
    ap = round(average_precision_score(sample_y, np.array(pred_y)), 3)
    fo = open('auc_ap.txt', 'w')
    fo.write("AUC:" + str(auc) + '\n')
    fo.write("AP:" + str(ap) + '\n')
    print("AUC:" + str(auc) + '\n')
    print("AP:" + str(ap) + '\n')
    fo.close()
Пример #6
0
 def get_feature_by_net(self, net_name, img_file):
     img = image.load_img(img_file, target_size=model_size[net_name])
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = preprocess_input(x)
     features = models[net_name].predict(x)
     return features
Пример #7
0
def model_predict(img_path, model):
    img = image.load_img(img_path, target_size=(331, 331))
    x = image.img_to_array(img)  # Preprocessing the image
    x = np.expand_dims(x, axis=0)  # x = np.true_divide(x, 255)

    preds = model.predict(preprocess_input(x))
    return preds
def judge_area(dir_p, dst='c:/', logger=None):
    os.makedirs(dst, exist_ok=True)
    if logger is None:
        logger = gen_logger('judge_area')

    model = model_nas_clf()
    model.compile(optimizer=Adam(0.0001), loss=binary_crossentropy, metrics=['acc'])

    for case_name in os.listdir(dir_p):
        case_dir = os.path.join(dir_p, case_name)
        record = {}
        if not os.path.isdir(case_dir):
            continue
        
        try:
            for batch in chunk(case_dir, 32):
                X = [preprocess_input(cv2.imread(x)) for x in batch]
                X = np.array(X)
                preds_batch = predicting(model, X)
                record.update({key:value for key, value in zip(batch, preds_batch)})
            with open(os.path.join(dst, f'{case_name}.pkl'), 'wb') as temp_pkl:
                pickle.dump(record, temp_pkl, pickle.HIGHEST_PROTOCOL)
            logger.info(f'{case_name} is completed')
        except:
            logger.exception(f'{case_name} encounter mistakes')
Пример #9
0
    def predict(self, uploaded_image: TemporaryUploadedFile) -> (List, Image):
        """
        Read image and predict category
        """
        # convert the image pixels to a numpy array
        image = img_to_array(Image.open(uploaded_image))

        # resize (crop) to vgg16 size (331, 331, 3)
        image = smart_resize(image, (331, 331))

        # grab a RGB preview for frontend
        rgb_preview = Image.fromarray(np.uint8(image)).convert('RGB')

        # reshape data for the model, add new axis (1, 224, 224, 3)
        image = np.expand_dims(image, axis=0)

        # prepare the image for the VGG model (subtracts the mean RGB channels)
        image = preprocess_input(image)

        # predict the probability across all output classes
        what = self.model.predict(image)

        # convert the probabilities to class labels
        labels = decode_predictions(what, top=3)

        # make it readable
        labels = self.labels_to_percents(labels)

        return labels, rgb_preview
Пример #10
0
def preprocess_img(img_path):
    img_path = img_path
    img = image.load_img(img_path, target_size=(224, 224))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    return img
Пример #11
0
def data_gen(x, y, batch_size, aug_flag):
    while True:
        for batch in chunker(x, batch_size):
            batch.sort()
            X = x[batch]
            Y = y[batch]
            X = [x[:train_length] for x in X]
            if aug_flag is True:
                X = [random_snp(x) for x in X]
            X = [preprocess_input(x) for x in X]
            yield np.array(X), np.array(Y)
Пример #12
0
def model_predict(img_path, model):
    img = image.load_img(img_path, target_size=(331, 331))
    x = image.img_to_array(img)  # Preprocessing the image
    x = np.expand_dims(x, axis=0)  # x = np.true_divide(x, 255)

    # Be careful how your trained model deals with the input
    # otherwise, it won't make correct prediction!
    #x = preprocess_input(x, mode='caffe')

    preds = model.predict(preprocess_input(x))
    return preds
Пример #13
0
def data_gen(list_files, id_label_map, batch_size, augment=False):
    seq = get_seq()
    while True:
        shuffle(list_files)
        for batch in chunker(list_files, batch_size):
            X = [cv2.imread(x) for x in batch]
            Y = [id_label_map[get_id_from_file_path(x)] for x in batch]
            if augment:
                X = seq.augment_images(X)
            X = [preprocess_input(x) for x in X]
            yield np.array(X), np.array(Y)
Пример #14
0
def preprocess(image):
    """Load and preprocess image."""
    # Create the array of the right shape to feed into the keras model
    data = []
    size = (96, 96)
    image = ImageOps.fit(image, size, Image.ANTIALIAS)
    image = np.asarray(image)
    x = preprocess_input(image)
    data.append(x)
    data = np.array(data)
    return data
Пример #15
0
def model_val_eval(model, X_val, y, e):
    hr_preds = []
    for x_case in X_val:
        x_case = [preprocess_input(x) for x in x_case]
        x_case = np.array(x_case)
        hr_pred = model.predict(x_case)
        hr_pred = sorted(hr_pred)[-2]  # only the second most serious area
        hr_preds.append(hr_pred)
    hr_preds = np.exp(hr_preds)
    ci = concordance_index(y, -hr_preds, e)
    return ci
Пример #16
0
    def get_assessment_score(self, img_array):
        with self.graph.as_default():
            with self.session.as_default():
                target_size = (224, 224)
                img = NeuralImageAssessment.resize_image(img_array, target_size)
                x = img_to_array(img)
                x = np.expand_dims(x, axis=0)
                x = preprocess_input(x)

                scores = self.model.predict(x, batch_size=1, verbose=0)[0]
        mean = NeuralImageAssessment.mean_score(scores)

        return mean
def load_patches(patch_list, path):
    batch_patches = []
    for file in patch_list:
        patch = path + file + ".tif"

        patch = cv2.imread(patch, cv2.IMREAD_COLOR)
        cv2.imshow("a", patch)
        patch = reshape_patch(patch, np.random.randint(1, 10))
        cv2.imshow("b", patch)
        cv2.waitKey(0)
        patch = preprocess_input(patch)
        batch_patches.append(patch)

    return batch_patches
def preprocessing_image(image, target):
    # Make sure the image mode is RGB:
    if image.mode != "RGB":
        image = image.convert("RGB")

    # Resize the input image:
    image = image.resize(target)
    # Convert PIL format to numpy array:
    image = img_to_array(image)
    # Convert the image/images into batch format:
    image = np.expand_dims(image, axis=0)
    # Pre-process (prepare) the image using the specific architecture:
    image = nasnet.preprocess_input(image)
    # Return the image:
    return image
Пример #19
0
	def __getitem__(self, index):
		'Generate one batch of data'
		# selects indices of data for next batch
		indexes = self.indexes[index * self.batch_size : (index + 1) * self.batch_size]

		# select data and load images
		labels = np.array([self.labels[k] for k in indexes])
		images = [cv2.imread(self.images_paths[k]) for k in indexes]
        
		# preprocess and augment data
		if self.augment == True:
		    images = self.augmentor(images)
		
		images = np.array([preprocess_input(img) for img in images])
		return images, labels
Пример #20
0
def predict(image_response):
    image_response = base64.b64decode(image_response)
    t = time.time()
    img = image.load_img(BytesIO(image_response), target_size=(224, 224))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)

    with graph.as_default():
        preds = model.predict(img)
        print(time.time() - t)
        # decode the results into a list of tuples (class, description, probability)
        # (one such list for each sample in the batch)
        print(decode_predictions(preds))
        return decode_predictions(preds,top=3)[0]
Пример #21
0
def get_windows(padded_image, windows_x, windows_y, w_size, height, width):

    # This function performs a sliding window through the padded image and returns a list of all windows
    X = np.ndarray((windows_x * windows_y, 96, 96, 3))
    for x in range(windows_x):
        for y in range(windows_y):
            #x is start of window, x+3 is end (extra mirror window is counted), because window is 3*window size. same for y

            start_x = min(x * w_size, height - 96)
            end_x = min((x + 3) * w_size, height)
            start_y = min(y * w_size, width - 96)
            end_y = min((y + 3) * w_size, width)
            cur_im = padded_image[start_x:end_x, start_y:end_y]
            X[x * windows_y + y, :, :, :] = preprocess_input(cur_im)

    return X
Пример #22
0
def predict(model, validation_dir):
    sample_counts = [0, 0, 0, 0, 0, 0, 0]
    hit_counts = [0, 0, 0, 0, 0, 0, 0]

    truths = []
    predictions = []
    prediction_arrays = []

    index = 0
    for r, dirs, files in os.walk(validation_dir):
        for dr in dirs:
            print(index, dr)
            files_in_dir = glob.glob(os.path.join(r, dr + "/*"))
            for fid in files_in_dir:
                sample_counts[index] += 1
                print(fid)

                img = image.load_img(path=fid,
                                     target_size=(img_width, img_height))
                img = image.img_to_array(img).astype('float32')
                img = preprocess_input(img)
                img -= np.mean(img, keepdims=True)
                img /= (np.std(img, keepdims=True) + K.epsilon())

                img = np.expand_dims(img, axis=0)

                prediction_array = model.predict(img)[0]
                prediction_arrays.append(prediction_array)

                pred = np.argmax(prediction_array)

                if index == pred:
                    hit_counts[index] += 1

                print('Accuracy:', sample_counts, hit_counts,
                      np.sum(hit_counts) / np.sum(sample_counts))

                truths.append(index)
                predictions.append(pred)

                # cnt += len(glob.glob(os.path.join(r, dr + "/*")))

            index = index + 1

    return sample_counts, hit_counts, truths, predictions, prediction_arrays
Пример #23
0
def read_dir(dir_p, sel_num) -> list:
    pool = os.listdir(dir_p)
    pool_size = len(pool)
    cur, end = 0, sel_num
    fns, ori_carrier, pro_carrier = [], [], []
    while end < pool_size:
        while cur < end:
            fn = pool[cur]
            ori_img = cv2.imread(os.path.join(dir_p, fn))
            fns.append(fn)
            ori_carrier.append(ori_img)
            pro_carrier.append(preprocess_input(ori_img))
            cur += 1
        yield fns, ori_carrier, pro_carrier
        fns.clear()
        ori_carrier.clear()
        pro_carrier.clear()
        end += sel_num
    def classify_random_images(self):
        imageno = np.random.random_integers(
            low=0, high=self.validation_generator.samples)

        name = self.validation_generator.filepaths[imageno]
        print(name)
        plt.imshow(mpimg.imread(name))

        img = Image.open(self.validation_generator.filepaths[imageno]).resize(
            (self.img_width, self.img_height))
        probabilities = self.model.predict(
            preprocess_input(np.expand_dims(img, axis=0)))
        breed_list = tuple(
            zip(self.validation_generator.class_indices.values(),
                self.validation_generator.class_indices.keys()))

        for i in probabilities[0].argsort()[-3:][::-1]:
            print(probabilities[0][i], "  :  ", breed_list[i])
Пример #25
0
def extract_features(directory, ids, model):
    if int(model) == 1:
        print("1")
        # load ResNet50 model
        model = ResNet50()
        input_size = 224
    else:
        print("2")
        # load NASNetLarge model
        model = NASNetLarge(input_shape=(331, 331, 3),
                            include_top=True,
                            weights='imagenet',
                            input_tensor=None,
                            pooling=None)
        input_size = 331
    # pops the last layer to get the features
    model.layers.pop()
    model = Model(inputs=model.inputs, outputs=model.layers[-2].output)
    model.summary()
    print(len(model.layers))
    # model characteristics
    plot_model(model, to_file='model.png')
    imgs = load_list(ids)
    print('Dataset: %d' % len(imgs))
    N = len(imgs)
    print(N)
    results = []
    i = 0
    batch_size = 1  # this can be 8 for a GTX 1080 Ti and 32G of RAM
    while i < N:
        if i % 1024 == 0:
            print('{} from {} images.'.format(i, N))
        batch = imgs[i:i + batch_size]
        i += batch_size
        images = [
            load_img(os.path.join(directory, img + ".jpg"),
                     target_size=(input_size, input_size)) for img in batch
        ]
        images = [preprocess_input(img_to_array(img)) for img in images]
        images = np.stack(images)
        r = model.predict(images)
        for ind in range(batch_size):
            results.append(r[ind])
    return results
 def _model_eval(self, X_val, y, e):
     hr_preds = []
     if self.gene:
         if len(X_val) != 152:
             gene_array = self.train_gene.values
         else:
             gene_array = self.test_gene.values
     assert len(gene_array) == len(X_val), 'impossible match'
     for idx, x_case in enumerate(X_val):
         x_case = [preprocess_input(x) for x in x_case]
         x_case = np.array(x_case)
         if self.gene:
             x_case = [x_case, np.array([gene_array[idx]]*len(x_case))]
         hr_pred = self.model.predict(x_case)
         hr_pred = sorted(hr_pred)[-2] # only the second most serious area, i.e. the second shorest time
         hr_preds.append(hr_pred)
     hr_preds = np.exp(hr_preds)
     ci = concordance_index(y,-hr_preds,e)
     return ci
Пример #27
0
def preprocess(img_file, target_size, transform=True):
    img = Image.open(img_file)
    w, h = img.size

    if transform:
        if w > h:
            ratio = (min(target_size) * 1.2) / float(h)
        else:
            ratio = (min(target_size) * 1.2) / float(w)
        output = img.resize((int(w * ratio), int(h * ratio)))
        output = output.convert("RGB")
        output = image.img_to_array(output)

        # Random rotation
        output = image.random_rotation(output,
                                       30,
                                       row_axis=0,
                                       col_axis=1,
                                       channel_axis=2,
                                       fill_mode='nearest')
        # Random crop
        left = np.random.randint(0, output.shape[1] - target_size[0] + 1)
        upper = np.random.randint(0, output.shape[0] - target_size[1] + 1)
        output = output[upper:upper + target_size[1],
                        left:left + target_size[0]]

        # Random horizontal flip
        if np.random.rand() < 0.5:
            output = output[:, ::-1, :]
    else:
        if w > h:
            left = (w - h) // 2
            output = img.crop((left, 0, left + h, h))
        else:
            upper = (h - w) // 2
            output = img.crop((0, upper, w, upper + w))
        output = output.resize(target_size)
        output = output.convert("RGB")
        output = image.img_to_array(output)

    output = preprocess_input(output)

    return output
    def extract(self, image_path, cnn_model_type='nasnet'):
        if cnn_model_type== 'InceptionV3':
            target_size = (299, 299)
        elif  cnn_model_type== 'nasnet':
            target_size = (331, 331)

        img = image.load_img(image_path, target_size=target_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)

        if cnn_model_type == 'InceptionV3':
            x = inception_v3.preprocess_input(x)
        elif cnn_model_type == 'nasnet':
            x = nasnet.preprocess_input(x)

        # Get the prediction.
        features = self.model.predict(x)

        features = features[0]

        return features
Пример #29
0
def extract_feature_one_image(img_path, intermediate_layer_model,
                              feature_extraction_method, input_img):
    img = image.load_img(img_path, target_size=(input_img, input_img))
    img_data = image.img_to_array(img)
    img_data = np.expand_dims(img_data, axis=0)

    if (feature_extraction_method == 'pretrained_lenet'):
        img_data = img_data / 255
    elif (feature_extraction_method == 'pretrained_vgg16'):
        img_data = vgg16.preprocess_input(img_data)
    elif (feature_extraction_method == 'pretrained_vgg19'):
        img_data = vgg19.preprocess_input(img_data)
    elif (feature_extraction_method == 'pretrained_xception'):
        img_data = xception.preprocess_input(img_data)
    elif (feature_extraction_method == 'pretrained_resnet'):
        img_data = resnet.preprocess_input(img_data)
    elif (feature_extraction_method == 'pretrained_inception_resnet'):
        img_data = inception_resnet.preprocess_input(img_data)
    elif (feature_extraction_method == 'pretrained_nasnet'):
        img_data = nasnet.preprocess_input(img_data)

    features = intermediate_layer_model.predict(img_data)
    features = features.reshape((-1))
    return features
Пример #30
0
def generate_features(my_model, my_model_name, path_to_descriptions, path_to_data, output_file, output_features_dir):

    try:
        os.mkdir(output_features_dir)
    except OSError:
        a = 0
        a = a + 1
    else:
        a = 0
        a = a + 1

    csv_ok = pd.read_csv(path_to_descriptions)
    x = my_model.output
    x = GlobalAveragePooling2D()(x)

    model = Model(inputs=my_model.input, outputs=x)

    for a in range(csv_ok.shape[0]):
        if a % 100 == 0:
            print(my_model_name + " " + str(a) + " of " + str(csv_ok.shape[0]))
        my_file = str(csv_ok.iloc[a, 0])
        img_path = path_to_data + my_file + '.png'
        img = image.load_img(img_path, target_size=(60, 60))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        features = model.predict(x)

        file_object = open(output_file, 'a')
        features = features[0]
        for b in range(features.shape[0]):
            if b > 0:
                file_object.write(",")
            file_object.write(str(features[b]))
        file_object.write('\n')
        file_object.close()