Exemple #1
0
def get_image_from_array(img_array, denormalize=True, dtype='float32' ,is_cv2_image = False):
    """Converts numpy image array to a PIL Image instance.
    Parameters
    ----------
        img: Input Numpy image array.
        denormalize: Revert back normalized image to unnormalized form
            Default: True.
        dtype: Dtype to use.
            Default: "float32".
        is_cv2_image: Set to True if image is loaded using cv2
            Default: False
    Returns
    -------
        A PIL Image.
    Raises
    ------
        Raises TypeError if image_array is not an numpy ndarray
    """
    if not hasattr(img_array, 'ndim'):
        raise TypeError(f'Required image_array to be of type numpy.ndarray but got {type(img_array)} instead')

    if img_array.ndim != 3:
        if img_array.ndim == 2:
            """expand image dimensions only if image is 2D grayscale
            manually adding channel dimension `1` to image (only for 2D grayscale image)"""
            img_array = expand_dims(img_array,axis=2)
        else:
            raise ValueError(f'Expected array with 3 dimensions Got array with shape {img_array.shape}\n'
                'Incase you have used expand_dims for preprocessing, use nv.reduce_dims() for reducing expanded dimensions\n'
                'make sure to check the axis position while expanding or reducing dimensions.')

    if is_cv2_image: #If numpy array is cv2 image
        img_array = img_array[...,::-1] #Convert BGR to RGB

    img_array = toarray(img_array,dtype=dtype)
    # Original Numpy array x has format (height, width, channel)
    # or (channel, height, width)
    # but target PIL image has format (width, height, channel)

    if denormalize:
        img_array = img_array - npmin(img_array)
        img_max = npmax(img_array)
        if img_max != 0:
            img_array /= img_max
        img_array *= 255
    if img_array.shape[2] == 4: #RGBA Image
        return pilimage.fromarray(img_array.astype('uint8'), 'RGBA')
    elif img_array.shape[2] == 3: #RGB image
        return pilimage.fromarray(img_array.astype('uint8'), 'RGB')
    elif img_array.shape[2] == 1: # grayscale image
        if npmax(img_array) > 255:
            # 32-bit signed integer grayscale image. PIL mode "I"
            return pilimage.fromarray(img_array[:, :, 0].astype('int32'), 'I')
        return pilimage.fromarray(img_array[:, :, 0].astype('uint8'), 'L')
    else:
        raise ValueError(f'Channel {img_array.shape[2]} not supported')
Exemple #2
0
    def fit(self, X, y):
        assert type(y) in [np.ndarray, list], 'expecting array or numpy array'
        assert type(X) in [np.ndarray, list,
                           bsr_matrix], 'expecting array or numpy array'

        if type(X) == bsr_matrix:
            y = np.asarray(y)
            X = X.toarray()

        elif type(X) == list:
            y = np.asarray(y)
            X = np.toarray(X)

        self.labels = np.unique(y)
        self.numclasses = self.labels.shape[0]
        self.numattributes = X.shape[1]
        self.priors = np.zeros(self.numclasses, dtype=float)
        self.numsamples = y.shape[0]
        self.likelihood = np.zeros((self.numclasses, X.shape[1]), dtype=float)
        updcount = float(self.smoothing * self.numattributes)

        self.priors = np.log(
            np.asarray([
                np.where(y == cl)[0].shape[0] / float(self.numsamples)
                for cl in self.labels
            ]))

        # compute tfidf weights instead of the count
        if self.tfidf:
            alldoc = X.shape[0]
            # freqperdoc = np.sum(np.where(X > 0, 1, 0), axis=0)
            idf = np.log((alldoc + 1.0) /
                         (np.sum(np.where(X > 0, 1, 0), axis=0) + 1.0)) + 1.0
            X = X.astype(float) / X.sum(axis=1)[:, None]
            X = X * idf

        def attlikelihood(X):
            attsum = np.sum(X)
            return np.log((X + self.smoothing) / (updcount + attsum))

        labelwordsum = np.squeeze(np.asarray(
            [np.sum(X[np.where(y == cl), :], axis=1) for cl in self.labels]),
                                  axis=1)

        self.likelihood = np.apply_along_axis(attlikelihood, 1, labelwordsum)

        return self.likelihood, self.priors
Exemple #3
0
def all_locs_numpy_total(hash):
    for hash in range(len(dbr)):
        a = np.toarray(dbr[hash])
        raise NotImplemented()
Exemple #4
0
def all_locs_numpy(hash):
    a = np.toarray(dbr[hash])
    return a
def show(img, res=1080, kernel=NEAREST_NEIGHBOR):
    todisplay = img
    if img.shape[0] != res:
        todisplay = upscale(np.toarray(img), (res, res), kernel)
    todisplay.show()
Exemple #6
0
def imread(image_path,resize=None,color_mode = None,interpolation='nearest',dtype='float32',return_original = False,normalize=False):
    """Converts a PIL Image instance to a Ndarray optimised for model.
    Parameters
    ----------
        image_path: Image Path or bytes.
        resize: (width,height) tuple
        color_mode: default is None
            you can also use color_mode as `rgb` or `rgba` or `grayscale`
        interpolation:
            Interpolation method used to resample the image if the
            target size is different from that of the loaded image.
            Supported methods are "nearest", "bilinear", and "bicubic".
            If PIL version 1.1.3 or newer is installed, "lanczos" is also
            supported. If PIL version 3.4.0 or newer is installed, "box" and
            "hamming" are also supported.
            Default: "nearest".
        dtype: Dtype to use for the returned array.
            Default: float32

        return_original: Returns original image array along with resized image array.
            Default: False
            Note: This parameter only works with resize parameter

        normalize: Returns normalized image if set to True
            Default: False
    # Returns
        A 3D Numpy array.
    # Raises
        ValueError: if invalid `image_path` or `resize` or `color_mode` or `interpolation` or `dtype` is passed.
        ValueError: if return_original is True and resize is None
    """
    image = pilimage.open(image_path)
    if color_mode is not None:
        if color_mode == 'grayscale':
            if image.mode not in ('L', 'I;16', 'I'):
                image = image.convert('L')
        elif color_mode == 'rgba':
            if image.mode != 'RGBA':
                image = image.convert('RGBA')
        elif color_mode == 'rgb':
            if image.mode != 'RGB':
                image = image.convert('RGB')
        else:
            raise ValueError('color_mode must be "grayscale", "rgb", or "rgba"')
        
    if resize is not None:
        if not isinstance(resize,tuple):
            raise TypeError(f'resize must be tuple of (width,height) but got {resize} of type {type(resize)} instead')

        if len(resize) != 2:
            raise ValueError(f'Tuple with (width,height) required but got {resize} instead.')

        original_image_array = toarray(image,dtype=dtype)
        if image.size != resize:
            if interpolation not in interpolation_methods:
                raise ValueError(f'Invalid interpolation, currently supported interpolations:{interpolation_methods.keys()}')
            resample = interpolation_methods.get(interpolation)
            image = image.resize(resize, resample)
    
    image_array = toarray(image,dtype=dtype)

    if normalize:
        image_array /= 255.
    
    if return_original:
        if resize is None:
            raise ValueError("return_original parameter only works with resize parameter")
        return original_image_array , image_array
    
    return image_array
Exemple #7
0
text = [sent_tokenize(d) for d in data_list]
text = [[clean_str(s).split(" ") for s in j] for j in text]

x, vocabulary, vocabulary_inv = build_input(text)
word2vec = vocab_to_word2vec('/Users/johnbeieler/GoogleNews-vectors-negative300.bin', vocabulary)
embedding_mat = build_word_embedding_mat(word2vec, vocabulary_inv)

sentences = LabeledLineSentence(text, names)
model = Doc2Vec.load_word2vec_format('/Users/johnbeieler/GoogleNews-vectors-negative300.bin',
                                     binary=True)
#model = Doc2Vec(min_count=1, window=10, size=100, sample=1e-4, negative=5,
#                workers=8)
model.build_vocab(sentences.to_array())
for epoch in range(10):
    model.train(sentences.sentences_perm())

model.save('HR_docs.d2v')

data = np.zeros((len(data_list), NUM_SENTS, 100))
for ix, x in enumerate(mapping):
    for w in xrange(NUM_SENTS):
        if x:
            try:
                data[ix, w, :] = model.docvecs['{}_{}_{}'.format(x[1], x[0],
                                                                 w)]
            except KeyError:
                pass

cPickle.dump([data, np.toarray(y)], open('HR_monarchy_data.p', 'wb'))