示例#1
0
def prepare(Params, data):
    '''
       resizing the images to fit the resNet50V2
       performig preprocessing and fit it into numpy array
       :param:data: image + labels dataframe
       :param: Params: pipe parameters
       :return: dataframe with resized images
       '''

    copy_data = data.copy()
    size = Params['Prepare']['size']
    # resizing the images
    copy_data['Data'] = copy_data['Data'].apply(
        lambda x: cv2.resize(x, (size, size)))

    # applying resNetV2 pre_processing to fit the model
    copy_data['Data'] = copy_data['Data'].apply(lambda x: preprocess_input(x))

    train_Set = []
    labels_set = []

    for img in copy_data['Data']:
        train_Set.append(img)

    for label in copy_data['Labels']:
        labels_set.append(label)

    train_Set = np.array(train_Set)
    labels_set = np.array(labels_set)

    return train_Set, labels_set
示例#2
0
    def eraser(input_img):
        input_img = preprocess_input(input_img)
        img_h, img_w, img_c = input_img.shape
        p_1 = np.random.rand()

        if p_1 > p:
            return input_img

        while True:
            s = np.random.uniform(s_l, s_h) * img_h * img_w
            r = np.random.uniform(r_1, r_2)
            w = int(np.sqrt(s / r))
            h = int(np.sqrt(s * r))
            left = np.random.randint(0, img_w)
            top = np.random.randint(0, img_h)

            if left + w <= img_w and top + h <= img_h:
                break

        if pixel_level:
            c = np.random.uniform(v_l, v_h, (h, w, img_c))
        else:
            c = np.random.uniform(v_l, v_h)

        input_img[top:top + h, left:left + w, :] = c

        return input_img
示例#3
0
def getData(params_data):
    """
    The function get the data from the data_path
    :param params_data: parameters for getting the data
    :return: a dictionary contains the data (dict as 'name: image') and the labels (list of 1/0)
    """
    print('\nStart getting the data')
    image_size = params_data['size']  # The desirable size of the images
    images_names = os.listdir(data_path)  # Get all the names of the images
    labels_file_name = params_data['labels_file_name']  # The name of the file containing the labels

    labels = sio.loadmat(data_path + '\\' + labels_file_name)['Labels'][0].tolist()  # Load the labels
    data = {}

    for name in images_names:

        # Skip on the mat file
        if '.mat' in name:
            continue

        img_path = data_path + '\\' + name  # Get the path of the current image
        name_key = name.split('.')[0]  # Get only the name of the image without the .jpeg

        image = cv2.imread(img_path)  # Get the current image
        image = cv2.resize(image, (image_size[0], image_size[1]))  # Resize the image
        # image = cv2.normalize(image, None, alpha=-1, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)  # Norm image
        image = resnet_v2.preprocess_input(image)

        data[name_key] = image

    print('End getting the data')
    return {'data': data, 'labels': labels}
def get_and_resize_test_data(width, height):
    """Gets the test data and resize it.
    
    Arguments:
        width {int} -- Width of the resized image
        height {int} -- Height of the resized image
    
    Returns:
        [np.ndarray] -- Matrix with all the images, resized.
    """
    test_data = []
    for file in sorted(os.listdir("../data/testing/"),
                       key=lambda x: int(x.split('.')[0])):
        image = load_img("../data/testing/" + file,
                         grayscale=False,
                         color_mode='rgb')
        image = img_to_array(image, dtype='float')

        width, height, _ = image.shape
        if width != max_width or height != max_height:
            image = cv2.resize(image, (max_width, max_height))

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

        test_data.append(np.array(image))

    test_data = np.asarray(test_data)

    return test_data
def preprocessData():
    """Get training images and resize each one of them to get the size of the bigger image in the data set
    
    Returns:
        X [np.ndarray] -- Matrix with all the images, resized.
        y [type] -- Matrix 1*N, with the class of each image.
    """
    list_of_classes = []
    index = 0

    #getting each class from data
    for _, dirs, files in os.walk("../data/training/", topdown=False):
        for name in dirs:
            list_of_classes.append(name)
            hash_map[name] = index
            index += 1
            number_files = len(
                fnmatch.filter(os.listdir("../data/training/" + name),
                               '*.jpg'))

    X = []
    y = []
    #get the size of the biggest image
    for current_class in list_of_classes:
        for index in range(100):
            image = load_img('../data/training/' + current_class + '/' +
                             str(index) + '.jpg',
                             grayscale=False,
                             color_mode='rgb')
            image = img_to_array(image, dtype='float')
            width, height, _ = image.shape
            max_width = max(width, max_width)
            max_height = max(height, max_height)

    #resizing every image
    for current_class in list_of_classes:
        for index in range(100):
            image = load_img('../data/training/' + current_class + '/' +
                             str(index) + '.jpg',
                             grayscale=False,
                             color_mode='rgb')
            image = img_to_array(image, dtype='float')
            width, height, _ = image.shape
            if width != max_width or height != max_height:
                image = cv2.resize(image, (max_width, max_height))

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

            X.append(image)
            y.append(hash_map[current_class])

    X = np.asarray(X)
    y = np.asarray(y)

    return X, y
 def extract_feat(self, img_path):
     img = image.load_img(img_path,
                          target_size=(self.input_shape[0],
                                       self.input_shape[1]))
     img = image.img_to_array(img)
     img = np.expand_dims(img, axis=0)
     img = preprocess_input(img)
     feat = self.model.predict(img)
     # print("------"+str(feat.shape[0])+"-----"+str(feat.shape[1]))
     norm_feat = feat[0] / LA.norm(feat[0])
     return norm_feat
def extract_features(filename, model):
    # load the models
    image = load_img(filename, target_size=(224,224))
    # convert image pixels to numpy array
    image = img_to_array(image)
    # reshape data
    image = np.expand_dims(image, axis=0)
    # preprocess image for model
    image = preprocess_input(image)
    # get features 
    feature = model.predict(image,verbose=0)
    return feature
    def extract_feat(self, img_path, regions):
        img = image.load_img(img_path,
                             target_size=(self.input_shape[0],
                                          self.input_shape[1]))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        img = preprocess_input(img)
        feat = self.model.predict(img)

        print(type(feat))
        print(feat.shape)
        roi_pools = roi.RoiPooling().get_pooled_rois(feat, regions)
        print(roi_pools.shape)

        # print("------"+str(feat.shape[0])+"-----"+str(feat.shape[1]))
        norm_feat = feat[0] / LA.norm(feat[0])
        for i in range(len(regions)):
            roi_pools[i] = roi_pools[i][0] / LA.norm(roi_pools[i][0])
        return roi_pools
示例#9
0
 def DefineLandmark(self):
     model = MobileNet()
     # load an image from file
     image = load_img('birthday-cake-600x600.jpg', target_size=(224, 224))
     # convert the image pixels to a numpy array
     image = img_to_array(image)
     # reshape data for the model
     image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
     # prepare the image for the VGG model
     image = preprocess_input(image)
     # predict the probability across all output classes
     yhat = model.predict(image)
     # convert the probabilities to class labels
     label = decode_predictions(yhat)
     # retrieve the most likely result, e.g. highest probability
     label2 = label[0]
     print(type(label2))
     for i in label2:
         print('%s (%.2f%%)' % (i[1], i[2] * 100))
     return label2
def extract_features(directory):
    # load model
    model = ResNet50V2(weights="imagenet")
    model.layers.pop()
    # re-structure the model by replacing the last output layer
    model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
    # sumarize
    print(model.summary())

    # save model
    model.save("ResNet50_feature_extraction.h5")
    # extract features from each photo
    features = dict()
    num_images = len(listdir(directory))

    for index, name in enumerate(listdir(directory)):
        # load an image from file
        filename = directory + "/" + name
        image = load_img(filename, target_size=(224, 224))

        # convert image pixels to numpy array
        image = img_to_array(image)

        # reshape data
        image = np.expand_dims(image, axis=0)
        # image = image[np.newaxis,:]

        # preprocess image for model
        image = preprocess_input(image)

        # get features
        feature = model.predict(image, verbose=0)

        # get image_id
        image_id = name.split(".")[0]

        # store feature
        features[image_id] = feature
        print(">(%i/%i) %s" % (index, num_images, name))

    return features
def getData(data_path, s):
    """
   function that read the photos from the computer
   :param data_path: data_path: string- address of the folder with the photos
   :param s:  int- the size of the images
   :return: data set and its following labels
   """
    print("Stage 1: Importing Data")
    x = scipy.io.loadmat(data_path + '/FlowerDataLabels.mat')
    labels = x['Labels'].T
    dataOriginal = []
    data = []
    for i in range(1, labels.shape[0] + 1):
        tmp = cv2.imread(data_path + '/' + str(i) + '.jpeg')
        tmp = cv2.cvtColor(tmp, cv2.COLOR_BGR2RGB)
        tmp = cv2.resize(tmp, (s, s))
        dataOriginal.append(tmp)
        data.append(preprocess_input(tmp))
    data = np.asarray(data)
    dataOriginal = np.asarray(dataOriginal)
    return data, labels, dataOriginal
示例#12
0
        def augmentation_data(image):
            image = np.array(image)
            augmentation = Compose([
                OneOf([
                    RGBShift(),
                    ChannelShuffle(),
                    RandomContrast(limit=0.2, p=0.5),
                    RandomGamma(gamma_limit=(80, 120), p=0.5),
                    RandomBrightness(limit=0.2, p=0.5),
                    HueSaturationValue(hue_shift_limit=5, sat_shift_limit=20,
                                    val_shift_limit=10, p=.9),
                ], p=0.1),
                
                # HorizontalFlip(p=0.5),
                
                # CLAHE(p=1.0, clip_limit=2.0),
                # ShiftScaleRotate(
                #     shift_limit=0.0625, scale_limit=0.1, 
                #     rotate_limit=15, border_mode=cv2.BORDER_REFLECT_101, p=0.8), 
                # ToFloat(max_value=255)    # normalization
            ])

            augmented_image = augmentation(image=image)['image']
            return preprocess_input(augmented_image)
示例#13
0

# Get training data
bboxes, polygon_labels = process()

# Define GCN models
epochs_per_image = 10
gcn_models = [None] * epochs_per_image

# Run stochastic training
for image in range(len(bboxes)):
    print("Training image {0} of {1}".format(image + 1, len(bboxes)))
    # Process bounding box
    bbox, polygon_points = bboxes[image], polygon_labels[image]
    resized_bb = cv2.resize(bbox, (224, 224), interpolation=cv2.INTER_AREA)
    resized_bb_exp = preprocess_input(np.expand_dims(resized_bb, axis=0))

    # Compute feature map
    resnet_model = ResNet50V2(weights='imagenet')
    embedding_model = Model(inputs=resnet_model.input,
                            outputs=resnet_model.get_layer('post_relu').output)
    feature_map = embedding_model.predict(resized_bb_exp)

    # Define graph
    N = 45
    G = nx.Graph()
    G.add_nodes_from(range(N))
    for i in range(N):
        if i - 2 < 0:
            G.add_edge(N + (i - 2), i)
        else:
示例#14
0
 def _extract_feature(model, path):
     im = Image.open(path)
     img = preprocess_input(np.expand_dims(im.copy(), axis=0))
     resnet_feature = model.predict(img)
     return np.array(resnet_feature).flatten()
示例#15
0
from keras.preprocessing import image
from keras.applications.resnet_v2 import preprocess_input, decode_predictions
from keras.layers import Input
import numpy as np
import tensorflow as tf

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.keras.backend.set_session(tf.Session(config=config))

dim_size = 224
input_tensor = Input(shape=(dim_size, dim_size, 3))

model = ResNet152V2(input_tensor=input_tensor, weights='imagenet')
#model = ResNet152V2(input_tensor=input_tensor, weights=None, classes=7)
#model.load_weights('resnet_weights.h5', by_name=False)

img_path = 'tiger.jpg'
img = image.load_img(img_path, target_size=(dim_size, dim_size))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=5))
# Predicted: 
# [(u'n02504013', u'Indian_elephant', 0.82658225), 
# (u'n01871265', u'tusker', 0.1122357), 
# (u'n02504458', u'African_elephant', 0.061040461)]