# -*- coding: utf-8 -*-
'''Inception-ResNet-v2 model for Keras.
'''
from __future__ import print_function

import numpy as np
import warnings

from keras.layers import Input
from keras import layers
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input

if __name__ == '__main__':
    model = InceptionResNetV2(include_top=True, weights='imagenet')

    img_path = 'data\\dogscats\\train\\cats\\cat.10013.jpg'
    img = image.load_img(img_path, target_size=(299, 299))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    print('Input image shape:', x.shape)

    preds = model.predict(x)
    print('Predicted:', decode_predictions(preds))  # ('n02123394', 'Persian_cat', 0.94211012)
Exemplo n.º 2
0
plt.plot(epochs, val_acc, 'blue', label='Validation acc')
plt.legend()

plt.figure()
plt.title('Training and validation loss')
plt.plot(epochs, loss, 'red', label='Training loss')
plt.plot(epochs, val_loss, 'blue', label='Validation loss')

plt.legend()
plt.show()

import time
import numpy as np
from keras.preprocessing import image
test_image = image.load_img(
    '/home/vanish/prgs/MLandDL/MITIndoor/Dataset/trainingset/bathroom/b1.jpg',
    target_size=(200, 200))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
test_image = preprocess_input(test_image)  # added to check same preds issue
start_time = time.time()
result = model.predict(test_image)
#decode_predictions(result)
print("--- %s seconds ---" % (time.time() - start_time))
for i in range(0, dict.__len__()):
    if result[0][i] >= 0.05:
        listOfKeys = [key for (key, value) in dict.items() if value == i]
        for key in listOfKeys:
            print(key)
            break
def extract_InceptionResV2(tensor):
    return pretrained_model('InceptionResNetV2').predict(preprocess_input(tensor))
Exemplo n.º 4
0
    def run(self):
        # set enviornment
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = str(self.gpuid)
        print("InferenceWorker init, GPU ID: {}".format(self.gpuid))

        from Base.model import build_model

        # load models
        model_weights_path = 'models/model.00-0.0296.hdf5'
        model = build_model()
        model.load_weights(model_weights_path)

        while True:
            try:
                try:
                    item = self.in_queue.get(block=False)
                except queue.Empty:
                    continue

                image_name_0, image_name_1, image_name_2 = item

                filename = os.path.join(image_folder, image_name_0)
                image_bgr = cv.imread(filename)
                image_bgr = cv.resize(image_bgr, (img_size, img_size),
                                      cv.INTER_CUBIC)
                image_rgb = cv.cvtColor(image_bgr, cv.COLOR_BGR2RGB)
                image_rgb_0 = preprocess_input(image_rgb)
                filename = os.path.join(image_folder, image_name_1)
                image_bgr = cv.imread(filename)
                image_bgr = cv.resize(image_bgr, (img_size, img_size),
                                      cv.INTER_CUBIC)
                image_rgb = cv.cvtColor(image_bgr, cv.COLOR_BGR2RGB)
                image_rgb_1 = preprocess_input(image_rgb)
                filename = os.path.join(image_folder, image_name_2)
                image_bgr = cv.imread(filename)
                image_bgr = cv.resize(image_bgr, (img_size, img_size),
                                      cv.INTER_CUBIC)
                image_rgb = cv.cvtColor(image_bgr, cv.COLOR_BGR2RGB)
                image_rgb_2 = preprocess_input(image_rgb)

                batch_inputs = np.empty((3, 1, img_size, img_size, 3),
                                        dtype=np.float32)
                batch_inputs[0] = image_rgb_0
                batch_inputs[1] = image_rgb_1
                batch_inputs[2] = image_rgb_2
                y_pred = model.predict(
                    [batch_inputs[0], batch_inputs[1], batch_inputs[2]])

                a = y_pred[0, 0:128]
                p = y_pred[0, 128:256]
                n = y_pred[0, 256:384]

                self.out_queue.put({
                    'image_name': image_name_0,
                    'embedding': a
                })
                self.out_queue.put({
                    'image_name': image_name_1,
                    'embedding': p
                })
                self.out_queue.put({
                    'image_name': image_name_2,
                    'embedding': n
                })
                if self.in_queue.qsize() == 0:
                    break
            except Exception as e:
                print(e)

        import keras.backend as K
        K.clear_session()
        print('InferenceWorker done, GPU ID {}'.format(self.gpuid))
Exemplo n.º 5
0
No_Forgery_train_Labels = No_Genuine_train_Labels


No_Genuine_test_Labels =  No_of_Users * No_testing_samples_per_User
No_Forgery_test_Labels = No_Genuine_test_Labels

print(No_Genuine_train_Labels)
print(No_Forgery_train_Labels)
print(No_Genuine_test_Labels)
print(No_Forgery_test_Labels)
#################################################################################################################################################
for photo in Final_train_List:
        #print(photo)
        img = image.load_img(photo, target_size=(img_width, img_height))
        tr_x = image.img_to_array(img)
        tr_x = preprocess_input(tr_x)
        Final_train_List3.append(tr_x)

for photo in Final_test_List:
        #print(photo)
        img = image.load_img(photo, target_size=(img_width, img_height))
        tr_x = image.img_to_array(img)
        tr_x = preprocess_input(tr_x)
        Final_test_List3.append(tr_x)
############################################################################################################
###############################################################################################################################
for i in range(0,No_Genuine_train_Labels):  
    Y_train.append(0)
for i in range(No_Forgery_train_Labels):
    Y_train.append(1)
Y_train = np.array(Y_train)
Exemplo n.º 6
0
                # load an image in PIL format
                try:
                    img = image.load_img(img_name,
                                         target_size=(img_width, img_height))
                except:
                    print("skips as it is broken")
                    print(f_idx, fname)
                    images_broken_idx[f_idx] = True
                    img = PIL.Image.new(mode="RGB",
                                        size=(img_width, img_height))

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

                # prepare the image (normalisation for channels)
                img_preprocessed = inception_resnet_v2.preprocess_input(
                    img.copy())
                images.append(img_preprocessed)

            # vstack for batch tagging
            images_vstack = np.vstack(images)

            # stack up images list to pass for prediction
            predictions = model_trained.predict(images_vstack,
                                                batch_size=bsize_tmp)

            # predictions.shape

            ## top selected classes
            top_classes_idx_arr = np.argsort(predictions)[:, ::-1][:, :top]

            top_classes_arr = classes_arr[top_classes_idx_arr]
Exemplo n.º 7
0
def GetDataGenerator(dataloader):
    while True:
        for images, labels in dataloader:
            images = preprocess_input(images)
            labels = keras.utils.to_categorical(labels, cfg.num_classes)
            yield (images, labels)
Exemplo n.º 8
0
# Reading the images
image_1 = cv2.resize(cv2.imread(image_1_path, 1), (299, 299))
image_2 = cv2.resize(cv2.imread(image_2_path, 1), (299, 299))
image_3 = cv2.resize(cv2.imread(image_3_path, 1), (299, 299))
# Creating a copy of the images for displaying purpose
image_1_copy = image_1
image_2_copy = image_2
image_3_copy = image_3

# Pre-processing the input
image_1 = np.expand_dims(image_1, axis=0)
image_2 = np.expand_dims(image_2, axis=0)
image_3 = np.expand_dims(image_3, axis=0)

image_1 = preprocess_input(image_1)
image_2 = preprocess_input(image_2)
image_3 = preprocess_input(image_3)

# Loading the model
my_model = incpt(weights='imagenet')

# Model Summary
# my_model.summary()

cv2.imshow("Cat", image_1_copy)
cv2.waitKey(0)
print("Predicted:", decode_predictions(my_model.predict(image_1), top=3)[0])

cv2.imshow("Moon", image_2_copy)
cv2.waitKey(0)
Exemplo n.º 9
0
def preproc_input_classifcation(img):
    from keras.applications.inception_resnet_v2 import preprocess_input
    return preprocess_input(img)
Exemplo n.º 10
0
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 29 16:40:26 2019

@author: Jianmu
"""

from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input, decode_predictions
from keras.preprocessing.image import load_img, img_to_array
import numpy as np
#%%
model = InceptionResNetV2()
print(model.summary())
#%%
target_size = (299, 299)
img_path = "C:/Users/14534/Desktop/4.jpg"
image = load_img(img_path, target_size=target_size)
image_data = img_to_array(image)
#image_data = image_data.reshape((1,) + image_data.shape)
image_data = np.expand_dims(image_data, axis=0)
print(image_data.shape)
image_data = preprocess_input(image_data)
#%%
prediction = model.predict(image_data)
results = decode_predictions(prediction, top=3)
print(results)
Exemplo n.º 11
0
def main():
    '''
    A script which can visualize picture's embedding
    '''
    # ================================================
    # Load pre-trained model and remove higher level layers
    # ================================================
    print("Loading inception_resnet_v2 pre-trained model...")
    model = InceptionResNetV2(weights='imagenet', include_top=False)

    # ================================================
    # Read images and convert them to feature vectors
    # ================================================
    img_list, filename_heads = [], []
    path = "db"
    print("Reading images from '{}' directory...\n".format(path))
    for index, file_path in enumerate(Path(path).iterdir()):
        if index == 50:
            break
        # Process filename
        head, ext = file_path.name, file_path.suffix
        if ext.lower() not in [".jpg", ".jpeg"]:
            continue

        # Read image file
        img = image.load_img(str(file_path), target_size=(224, 224))  # load
        filename_heads.append(head)  # filename head

        # Pre-process for model input
        # keras's numpy format is float32
        img = image.img_to_array(img)  # convert to array
        img = np.expand_dims(img, axis=0)
        if len(img_list) > 0:
            img_list = np.concatenate((img_list, img))
        else:
            img_list = img

    img_list = preprocess_input(img_list)
    predicts = model.predict(img_list).reshape(len(img_list), -1)
    img_list = img_list.astype('uint8')
    print("img_list.shape = {}".format(img_list.shape))
    print("X_features.shape = {}\n".format(predicts.shape))

    # ===========================
    # Find k-nearest images to each image
    # ===========================
    n_neighbours = 5 + 1  # +1 as itself is most similar
    knn = kNN()  # kNN model
    knn.compile(n_neighbors=n_neighbours, algorithm="brute", metric="cosine")
    knn.fit(predicts)

    # ==================================================
    # Plot recommendations for each image in database
    # ==================================================
    output_rec_dir = Path('output', 'rec')
    if not output_rec_dir.exists():
        output_rec_dir.mkdir()
    n_imgs = int(len(img_list) / 5)
    ypixels, xpixels = img_list[0].shape[0], img_list[0].shape[1]
    for ind_query in range(n_imgs):
        # Find top-k closest image feature vectors to each vector
        print("[{}/{}] Plotting similar image recommendations \
        for: {}".format(ind_query + 1, n_imgs, filename_heads[ind_query]))
        distances, indices = knn.predict(np.array([predicts[ind_query]]))
        distances = distances.flatten()
        indices = indices.flatten()
        indices, distances = find_topk_unique(indices, distances, n_neighbours)

        # Plot recommendations
        rec_filename = Path(output_rec_dir,
                            "{}_rec.png".format(filename_heads[ind_query]))
        x_query_plot = img_list[ind_query].reshape((-1, ypixels, xpixels, 3))
        x_answer_plot = img_list[indices].reshape((-1, ypixels, xpixels, 3))
        plot_query_answer(
            x_query=x_query_plot,
            x_answer=x_answer_plot[1:],  # remove itself
            filename=str(rec_filename))

    # ===========================
    # Plot tSNE
    # ===========================
    output_tsne_dir = Path("output")
    if not output_tsne_dir.exists():
        output_tsne_dir.mkdir()
    tsne_filename = Path(output_tsne_dir, "tsne.png")
    print("Plotting tSNE to {}...".format(tsne_filename))
    plot_tsne(img_list, predicts, str(tsne_filename))
Exemplo n.º 12
0
##  create unlabeled synthetic samples as Cohort I

FC_features = np.random.rand(261, 90, 90, 3)
SC_features = np.random.rand(261, 90, 90, 3)

##  load VGG pretrained network
base_model = VGG19(include_top=False,
                   weights='imagenet',
                   input_shape=(90, 90, 3))
TL_model = Model(inputs=base_model.input, outputs=base_model.output)
for layer in TL_model.layers:
    layer.trainable = False

##   dimension reduction with VGG to the same size as target cohort
FC_train = preprocess_input(FC_features)
SC_train = preprocess_input(SC_features)

FC_train = TL_model.predict(FC_train)
SC_train = TL_model.predict(SC_train)

##   flatten connectome
FC_train_flat = FC_train.reshape(
    (FC_train.shape[0],
     FC_train.shape[1] * FC_train.shape[2] * FC_train.shape[3]))
SC_train_flat = SC_train.reshape(
    (SC_train.shape[0],
     SC_train.shape[1] * SC_train.shape[2] * SC_train.shape[3]))

##   train SSAE using cohort II in an unsupervised learning
##   the number of nodes in first and second layer should match to deep multimodal learning
Exemplo n.º 13
0
    x = Dropout(0.75)(base_model.output)
    x = Dense(10, activation='softmax')(x)

    model = Model(base_model.input, x)
    model.load_weights('weights/inception_resnet_weights.h5')

    score_list = np.empty((len(imgs), 11), dtype=object)
    step = 20000
    i = 0
    while i < len(imgs):
        print(i)
        imgs_temp = imgs[i:i + step]
        img_array = np.empty((len(imgs_temp), 224, 224, 3))
        file_names = []
        for ind, j in tqdm.tqdm(enumerate(imgs_temp)):
            x = preprocess_input(np.expand_dims(img_to_array(load_img(j, target_size=target_size)), axis=0))
            img_array[ind, ] = x
            file_names.append(Path(j).name.lower())

        scores = model.predict(img_array, batch_size=100, verbose=0)

        #si = np.arange(1, 11, 1)

        #mean = np.sum(scores * si, axis=1)

        #std = np.sqrt(np.sum(((np.array([si, ] * len(imgs_temp)) - mean.reshape(len(imgs_temp), -1)) ** 2) * scores, axis=1))

        #result = np.stack([file_names, mean, std]).transpose()
        #result = np.stack([file_names, scores]).transpose()
        #score_list[i:i + step, ] = result
        score_list[i:i + step, 0] = file_names
Exemplo n.º 14
0
 def predict_scores(self, frames, model, start):
     # frames = tf.convert_to_tensor(frames, dtype=tf.float32)
     frames = preprocess_input(frames.astype(np.float32))
     scores = model.predict(frames, batch_size=1, verbose=1)
     means = self.mean_score(scores)
     return (np.max(means), np.argmax(means) + start)
Exemplo n.º 15
0
def calculate_mode_score(gen_images_path,
                         real_images_path,
                         batch_size=32,
                         splits=10):
    # Create an instance of InceptionV3
    model = InceptionResNetV2()

    # Load real images
    real_images = None
    for image_ in glob.glob(real_images_path):
        # Load image
        loaded_image = image.load_img(image_, target_size=(299, 299))

        # Convert PIL image to numpy ndarray
        loaded_image = image.img_to_array(loaded_image)

        # Another another dimension (Add batch dimension)
        loaded_image = np.expand_dims(loaded_image, axis=0)

        # Concatenate all images into one tensor
        if real_images is None:
            real_images = loaded_image
        else:
            real_images = np.concatenate([real_images, loaded_image], axis=0)

    # Load generated images
    gen_images = None
    for image_ in glob.glob(gen_images_path):
        # Load image
        loaded_image = image.load_img(image_, target_size=(299, 299))

        # Convert PIL image to numpy ndarray
        loaded_image = image.img_to_array(loaded_image)

        # Another another dimension (Add batch dimension)
        loaded_image = np.expand_dims(loaded_image, axis=0)

        # Concatenate all images into one tensor
        if gen_images is None:
            gen_images = loaded_image
        else:
            gen_images = np.concatenate([gen_images, loaded_image], axis=0)

    # Calculate number of batches for generated images
    gen_num_batches = (gen_images.shape[0] + batch_size - 1) // batch_size
    gen_images_probs = None
    # Use InceptionV3 to calculate probabilities of generated images
    for i in range(gen_num_batches):
        image_batch = gen_images[i * batch_size:(i + 1) * batch_size, :, :, :]
        prob = model.predict(preprocess_input(image_batch))

        if gen_images_probs is None:
            gen_images_probs = prob
        else:
            gen_images_probs = np.concatenate([prob, gen_images_probs], axis=0)

    # Calculate number of batches for real images
    real_num_batches = (real_images.shape[0] + batch_size - 1) // batch_size
    real_images_probs = None
    # Use InceptionV3 to calculate probabilities of real images
    for i in range(real_num_batches):
        image_batch = real_images[i * batch_size:(i + 1) * batch_size, :, :, :]
        prob = model.predict(preprocess_input(image_batch))

        if real_images_probs is None:
            real_images_probs = prob
        else:
            real_images_probs = np.concatenate([prob, real_images_probs],
                                               axis=0)

    # KL-Divergence: compute kl-divergence and mean of it
    num_gen_images = len(gen_images)
    split_scores = []

    for j in range(splits):
        gen_part = gen_images_probs[j * (num_gen_images // splits):(j + 1) *
                                    (num_gen_images // splits), :]
        real_part = real_images_probs[j * (num_gen_images // splits):(j + 1) *
                                      (num_gen_images // splits), :]
        gen_py = np.mean(gen_part, axis=0)
        real_py = np.mean(real_part, axis=0)
        scores = []
        for i in range(gen_part.shape[0]):
            scores.append(entropy(gen_part[i, :], gen_py))

        split_scores.append(np.exp(np.mean(scores) - entropy(gen_py, real_py)))

    final_mean = np.mean(split_scores)
    final_std = np.std(split_scores)

    return final_mean, final_std
Exemplo n.º 16
0
from time import time
import numpy as np
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.inception_resnet_v2 import preprocess_input
from keras.applications.inception_resnet_v2 import decode_predictions
from skimage.io import imread
from skimage.transform import resize

tic = time()
model = InceptionResNetV2(weights='imagenet')
print("Loaded model in {:.3}s".format(time() - tic))

image = imread('laptop.jpeg')
image_resized = resize(image, (299, 299), preserve_range=True, mode='reflect')
image_resized_batch = np.expand_dims(image_resized, axis=0)

tic = time()
preds = model.predict(preprocess_input(image_resized_batch))
print("Computed predictions in {:.3}s".format(time() - tic))

print('Predicted image labels:')
class_names, confidences = [], []
for class_id, class_name, confidence in decode_predictions(preds, top=5)[0]:
    print("    {} (synset: {}): {:0.3f}".format(class_name, class_id,
                                                confidence))
Exemplo n.º 17
0
Arquivo: test.py Projeto: dsys/pavlov
        for j, k in enumerate(prediction[0]):
            if k >= min_score:
                o[labels[j]] = float(k)
        output.append(o)
    return output


for kf in glob.glob('data/raw/*'):
    klass = os.path.basename(kf)
    total = 0
    correct = 0
    wrong_klasses = set()
    for image_paths in chunk(glob.glob('data/raw/' + klass + '/*'), 139):
        images = []
        for image_path in image_paths:
            try:
                img = preprocess_input(load_image(image_path))
                images.append(img)
                total += 1
            except OSError:
                print("error processing", image_path)

        for output in predict(images):
            if klass in output and output[klass] >= 0.5:
                correct += 1
            wrong_klasses |= set(output.keys())
    wrong_klasses.discard(klass)
    print(klass + ': ' + str(correct) + '/' + str(total))
    print('  wrong classes: ', ', '.join(wrong_klasses))
    print()
Exemplo n.º 18
0
def inception_resnet_v2_predict(images):
    images = images.astype(np.float32)
    predictions = model.predict(preprocess_input(images))
    return predictions
y = np.argmax(predictions, axis=-1)
print(y)

print('Classification Report')
cr = classification_report(y_true=validation_generator.classes, y_pred=y)
print(cr)

print('Confusion Matrix')
plt.clf()
cm = confusion_matrix(validation_generator.classes, y)
df = pd.DataFrame(cm, columns=validation_generator.class_indices)
plt.figure(figsize=(80, 80))
sn.heatmap(df, annot=True)
plt.savefig('./cm.png')

####################### Classify random img
imageno = np.random.random_integers(low=0, high=validation_generator.samples)

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

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

for i in probabilities[0].argsort()[-3:][::-1]:
    print(probabilities[0][i], "  :  ", breed_list[i])
def save_bottlebeck_features(model_name):

    if model_name == 'resnet50':
        model = resnet50.ResNet50(weights='imagenet',
                                  include_top=False,
                                  pooling='avg')
        feat_output = [
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/2)resnet50/',
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/2)resnet50/'
        ]

        # 2048 dimensional features
        # pooling: 1) None: output is 16x16x2048, 2) avg: 1x1x2048, 3) max: 1x1x2048
        #base_model=resnet50.ResNet50(input_shape=(img_height,img_width,3),weights='imagenet', include_top=False)
        #model = Model(inputs=base_model.input, outputs=base_model.get_layer('activation_25').output)
    elif model_name == 'nasnet_large':
        model = nasnet.NASNetLarge(input_shape=(img_height, img_width, 3),
                                   weights='imagenet',
                                   include_top=False,
                                   pooling='avg')
        #4032 dimensional features
    elif model_name == 'xception':
        model = xception.Xception(input_shape=(img_height, img_width, 3),
                                  weights='imagenet',
                                  include_top=False,
                                  pooling='avg')
        feat_output = [
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/4)xception/',
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/4)xception/'
        ]

        #2048 dimensional features
    elif model_name == 'inceptionv3':
        model = inception_v3.InceptionV3(input_shape=(img_height, img_width,
                                                      3),
                                         weights='imagenet',
                                         include_top=False,
                                         pooling='avg')
        feat_output = [
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/5)inceptionv3/',
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/5)inceptionv3/'
        ]
        #2048 dimensional features
    elif model_name == 'inceptionresnetv2':
        model = inception_resnet_v2.InceptionResNetV2(input_shape=(img_height,
                                                                   img_width,
                                                                   3),
                                                      weights='imagenet',
                                                      include_top=False,
                                                      pooling='avg')
        #1536 dimensional features
    elif model_name == 'densenet':
        model = densenet.DenseNet201(input_shape=(img_height, img_width, 3),
                                     weights='imagenet',
                                     include_top=False,
                                     pooling='avg')
        # 1920 dimensional features
    else:
        model = vgg19.VGG19(weights='imagenet',
                            include_top=False,
                            pooling='avg')
        # 512 dimensional features
        #base_model=vgg19.VGG19(input_shape=(img_height,img_width,3),weights='imagenet', include_top=False)
        #model=Model(inputs=base_model.input,outputs=base_model.get_layer('block4_pool').output)

    #for i,layer in enumerate(model.layers):
    #    print(i,layer.name)

    #print(model.summary())

    for ind in range(1, len(img_path)):
        path_ind = img_path[ind]
        #path_split=path_ind.split("/")
        images = os.listdir(path_ind)
        for image_name in images:
            if '.svs' in image_name:
                patient_id = image_name[0:23]
                image_features = []
                image_names = []

                ss = time.time()
                patient_id = 'TCGA-2F-A9KO'
                #patches=os.listdir(train_data_dir[ind]+patient_id+'*.png')
                patches = glob.glob(train_data_dir[0] + patient_id + '*.png')

                for patch_name in patches:
                    patch_split = patch_name.split("\\")
                    img = image.load_img(patch_name,
                                         target_size=(img_height, img_width))
                    # convert image to numpy array
                    x = image.img_to_array(img)

                    # the image is now in an array of shape (224, 224, 3)
                    # need to expand it to (1, 224, 224, 3) as it's expecting a list
                    x = np.expand_dims(x, axis=0)
                    #imshow(np.uint8(x[0,:,:,:]))

                    if model_name == 'resnet50':
                        x = resnet50.preprocess_input(x)
                    elif model_name == 'nasnet_large':
                        x = nasnet.preprocess_input(x)
                    elif model_name == 'xception':
                        x = xception.preprocess_input(x)
                    elif model_name == 'inceptionv3':
                        x = inception_v3.preprocess_input(x)
                    elif model_name == 'inceptionresnetv2':
                        x = inception_resnet_v2.preprocess_input(x)
                    elif model_name == 'densenet':
                        x = densenet.preprocess_input(x)
                    else:
                        x = vgg19.preprocess_input(x)

                    # extract the features
                    features = model.predict(x)[0]
                    #features=np.mean(features,axis=(0,1))

                    image_features.append(features)
                    image_names.append(patch_split[1])

                se = time.time() - ss
                print(se)
                if save_features == True:
                    scipy.io.savemat(feat_output[ind] + patient_id +
                                     '_feat.mat',
                                     mdict={
                                         'image_features': image_features,
                                         'image_names': image_names
                                     })
Exemplo n.º 21
0
    def my_TTA(img):

        # Set the indication if new whale predicted already for this picture to false.
        # Set the place to put the new_whale predication to the end of the list(ignored there).
        # Preprocess the image before insert it into the predic model.
        Is_new_whale = False
        new_whale_location = NUMBER_OF_PREDICATIONS
        x = image.img_to_array(img)

        # Augmentation for the picture as described.
        original_img = np.array(x)

        original_img = np.expand_dims(original_img, axis=0)
        original_img = preprocess_input(original_img)

        predictions_orig = model.predict(original_img, verbose=0)
        flip_img = np.array(Augmentation_Scale_Trans_Flip_Blur(np.array(x)))

        flip_img = np.expand_dims(flip_img, axis=0)
        flip_img = preprocess_input(flip_img)

        predictions_flip = model.predict(flip_img, verbose=0)
        fliplr_img = np.fliplr(np.array(x))

        fliplr_img = np.expand_dims(fliplr_img, axis=0)
        fliplr_img = preprocess_input(fliplr_img)

        predictions_fliplr = model.predict(fliplr_img, verbose=0)
        flipud_img = np.array(Augmentation_Scale_Trans_Flip_Blur(np.array(x)))

        flipud_img = np.expand_dims(flipud_img, axis=0)
        flipud_img = preprocess_input(flipud_img)

        predictions_flipud = model.predict(flipud_img, verbose=0)

        # The first 5 predictions for each augmentation
        predictions_orig_ind = predictions_orig.argsort(
        )[0][::-1][:NUMBER_OF_PREDICATIONS]
        predictions_flip_ind = predictions_flip.argsort(
        )[0][::-1][:NUMBER_OF_PREDICATIONS]
        predictions_fliplr_ind = predictions_fliplr.argsort(
        )[0][::-1][:NUMBER_OF_PREDICATIONS]
        predictions_flipud_ind = predictions_flipud.argsort(
        )[0][::-1][:NUMBER_OF_PREDICATIONS]
        llist = []
        Best_guess_list = []
        Index_list = [0, 0, 0, 0]

        # For each picture find the correct prediction according the described logic.
        for i in range(NUMBER_OF_PREDICATIONS):
            llist        = [predictions_orig_ind[Index_list[0]], predictions_flip_ind[Index_list[1]], \
                             predictions_fliplr_ind[Index_list[2]], predictions_flipud_ind[Index_list[3]]]
            max_pred_num = [predictions_orig[0][Index_list[0]], predictions_flip[0][Index_list[1]], \
                                predictions_fliplr[0][Index_list[2]], predictions_flipud[0][Index_list[3]]]

            max_pred_value = max_pred_num[max_pred_num.index(
                max(max_pred_num))]

            # If the prediction is not above the threshold than put a new_whale classification in that spot
            # and ignore futher classifiaction below the thresholf for that picture.
            if not Is_new_whale and max_pred_value < THRESHOLD:
                Is_new_whale = True
                new_whale_location = i

            # If there are more than one prediction agreement on the classification, choose this classification,
            # otherwise choose the highest prediction.
            if Most_common_count(llist) > 1:
                Best_guess_list.append(Most_common_element(llist))
                Index_list[Index_of_value(llist,
                                          Most_common_element(llist))] += 1
            else:
                Best_guess_list.append(max_pred_num.index(max_pred_value))
                Index_list[max_pred_num.index(max_pred_value)] += 1

        # Insert the 'new_whale' classification into the predictions.
        if Is_new_whale:
            Best_guess_list.insert(new_whale_location, NEW_WHALE_INDEX)
            del Best_guess_list[-1]

        return (Best_guess_list)
Exemplo n.º 22
0
    # convert image to 3D tensor with shape (299, 299, 3)
    x = image.img_to_array(img)
    # convert 3D tensor to 4D tensor with shape (1, 299, 299, 3)
    return np.expand_dims(x, axis=0)


def paths_to_tensor(img_paths):
    list_of_tensors = [
        path_to_tensor(img_path) for img_path in tqdm(img_paths)
    ]
    return np.vstack(list_of_tensors)


from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

# pre-process the data for Keras
train_tensors = paths_to_tensor(train_files)
test_tensors = paths_to_tensor(test_files)

# load InceptionResNet V2 model
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
model = InceptionResNetV2(include_top=False, weights='imagenet')

# preprocess the tensors and get bottleneck_features
# These codes take a long time. It's better to use GPU/cloud services
bottleneck_features_train = model.predict(preprocess_input(train_tensors))
np.save(open('bottleneck_features_train.npy', 'w'), bottleneck_features_train)
bottleneck_features_test = model.predict(preprocess_input(test_tensors))
np.save(open('bottleneck_features_test.npy', 'w'), bottleneck_features_test)
Exemplo n.º 23
0
        # @todo skip readily done files.

        # convert the PIL image to a numpy array
        # IN PIL - image is in (width, height, channel)
        # In Numpy - image is in (height, width, channel)
        numpy_image = img_to_array(original)

        # Convert the image / images into batch format
        # expand_dims will add an extra dimension to the data at a particular axis
        # We want the input matrix to the network to be of the form (batchsize, height, width, channels)
        # Thus we add the extra dimension to the axis 0.
        image_batch = np.expand_dims(numpy_image, axis=0)

        # prepare the image (normalisation for channels)
        processed_image = inception_resnet_v2.preprocess_input(
            image_batch.copy())

        # get the predicted probabilities for each class
        # @todo predict_on_batch
        predictions = model_trained.predict(processed_image)

        # print predictions
        dominant_feature_idx = np.argmax(predictions[0])

        # This is the dominant entry in the prediction vector
        dominant_output = model_trained.output[:, dominant_feature_idx]

        # convert the probabilities to class labels
        predicted_class = classes[dominant_feature_idx]
        print('Predicted:', predicted_class)
Exemplo n.º 24
0
def make_transfer_prediction(tensor):
    with K.get_session().graph.as_default() as g:
        return transfer_model.predict(preprocess_input(tensor))
Exemplo n.º 25
0
def preprocess(x):
    x = eraser(x)
    x = preprocess_input(x)
    return x
Exemplo n.º 26
0
def preproc_glaucoma(img):
    glauc_a = glaucoma(img)
    final = preprocess_input(glauc_a)

    return final
with tf.device('/GPU:0'):
    base_model = InceptionResNetV2(input_shape=(None, None, 3), include_top=False, pooling='avg', weights=None)
    x = Dropout(0.75)(base_model.output)
    x = Dense(10, activation='softmax')(x)

    model = Model(base_model.input, x)
    model.load_weights('weights/inception_resnet_weights.h5')

    score_list = []

    for img_path in imgs:
        img = load_img(img_path, target_size=target_size)
        x = img_to_array(img)
        x = np.expand_dims(x, axis=0)

        x = preprocess_input(x)

        scores = model.predict(x, batch_size=1, verbose=0)[0]

        mean = mean_score(scores)
        std = std_score(scores)

        file_name = Path(img_path).name.lower()
        score_list.append((file_name, mean))

        # print("Evaluating : ", img_path)
        # print("NIMA Score : %0.3f +- (%0.3f)" % (mean, std))
        # print()

    if rank_images:
        # print("*" * 40, "Ranking Images", "*" * 40)
Exemplo n.º 28
0
def preproc_amd(img):
    amd_a = amd(img)
    final = preprocess_input(amd_a)

    return final
from time import time
import numpy as np
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.inception_resnet_v2 import preprocess_input
from keras.applications.inception_resnet_v2 import decode_predictions
from skimage.io import imread
from skimage.transform import resize

tic = time()
model = InceptionResNetV2(weights='imagenet')
print("Loaded model in {:.3}s".format(time() - tic))

image = imread('laptop.jpeg')
image_resized = resize(image, (299, 299), preserve_range=True, mode='reflect')
image_resized_batch = np.expand_dims(image_resized, axis=0)

tic = time()
preds = model.predict(preprocess_input(image_resized_batch))
print("Computed predictions in {:.3}s".format(time() - tic))

print('Predicted image labels:')
class_names, confidences = [], []
for class_id, class_name, confidence in decode_predictions(preds, top=5)[0]:
    print("    {} (synset: {}): {:0.3f}".format(class_name, class_id, confidence))
Exemplo n.º 30
0
def preproc_diabeticRet(img):
    dr_a = diabeticRet(img)
    final = preprocess_input(dr_a)

    return final
Exemplo n.º 31
0
from keras.preprocessing.sequence import pad_sequences
from keras.models import Model
from keras.utils import to_categorical
from keras.layers import Embedding, TimeDistributed, RepeatVector, LSTM, concatenate , Input, Reshape, Dense, Flatten
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
import numpy as np

# Load the images and preprocess them for inception-resnet
images = []
all_filenames = listdir('images/')
all_filenames.sort()
for filename in all_filenames:
    images.append(img_to_array(load_img('images/'+filename, target_size=(299, 299))))
images = np.array(images, dtype=float)
images = preprocess_input(images)

# Run the images through inception-resnet and extract the features without the classification layer
IR2 = InceptionResNetV2(weights='imagenet', include_top=False)
features = IR2.predict(images)

# We will cap each input sequence to 100 tokens
max_caption_len = 100
# Initialize the function that will create our vocabulary 
tokenizer = Tokenizer(filters='', split=" ", lower=False)

# Read a document and return a string
def load_doc(filename):
    file = open(filename, 'r')
    text = file.read()
    file.close()
Exemplo n.º 32
0
def preprocess_image(img):
    Image = image.load_img("train/" + img + ".jpg", target_size=(299, 299))
    data = image.img_to_array(Image)
    data = preprocess_input(data)
    return data