コード例 #1
0
def downsample_zerofill(x,
                        down_ratio=None,
                        sparsity=None,
                        min_shape=(128, 128),
                        method='bicubic'):
    x = (x / np.iinfo(x.dtype).max).astype(np.float32)
    if x.shape[0] < min_shape[0]:
        x = resize(x, [min_shape[0], x.shape[1]], method, antialias=True)
    if x.shape[1] < min_shape[1]:
        x = resize(x, [x.shape[1], min_shape[1]], method, antialias=True)
    if down_ratio is not None and sparsity is None:
        mask = np.zeros(x.shape, dtype=np.float32)
        mask[::down_ratio[0], ::down_ratio[1], :] = 1
        down = x * mask
        #mask[mask == 0] = 0.5
        x = np.dstack((x, down, mask))
    elif down_ratio is None and sparsity is not None:
        mask = np.random.random(x.shape).astype(np.float32)
        mask[mask > sparsity] = 1
        mask[mask <= sparsity] = 0
        down = x * mask
        #mask[mask == 0] = 0.5
        x = np.dstack((x, down, mask))
    else:
        print('ERROR: Please, input either a downsampling ratio or sparsity.')
    return x
コード例 #2
0
def downsample(x, down_ratio=(2, 1), min_shape=(128, 128), method='bicubic'):
    x = (x / np.iinfo(x.dtype).max).astype(np.float32)
    if x.shape[0] < min_shape[0]:
        x = resize(x, [min_shape[0], x.shape[1]], method, antialias=True)
    if x.shape[1] < min_shape[1]:
        x = resize(x, [x.shape[1], min_shape[1]], method, antialias=True)
    down = x[::down_ratio[0], ::down_ratio[1], :]
    x = (x, down)
    return x
コード例 #3
0
ファイル: decode_utils.py プロジェクト: mobicom2021p438/imon
def decode_img_enhanced(leye_img, reye_img, region, label):
    precision_type = tf.float16
    region = tf.cast(region, tf.int32)

    leye_im = tf.io.decode_jpeg(leye_img)
    reye_im = tf.io.decode_jpeg(reye_img)
    '''Convert to float16/32 in the [0,1] range'''
    leye_im = convert_image_dtype(leye_im, precision_type)
    reye_im = convert_image_dtype(reye_im, precision_type)
    '''Resize'''
    leye_im = resize(leye_im, [config.eyeIm_size, config.eyeIm_size])
    reye_im = resize(reye_im, [config.eyeIm_size, config.eyeIm_size])
    '''Normalize'''
    # leye_im = tf.image.per_image_standardization(leye_im)
    # reye_im = tf.image.per_image_standardization(reye_im)

    orientation = tf.cast(tf.one_hot(region[24], depth=3), precision_type)

    eyelandmark = tf.cast(tf.concat([region[8:11], region[13:16]], 0),
                          tf.float32) / 640.0
    '''Create heatmap label'''
    if (config.heatmap):
        hmFocus_size = 17 if (config.mobile) else 9  # tablet focus_size=9

        HM_FOCUS_IM = np.zeros((5, hmFocus_size, hmFocus_size, 1))

        stdv_list = [0.2, 0.25, 0.3, 0.35, 0.4]
        for level in range(5):  # 5 levels of std to constuct heatmap
            stdv = stdv_list[level]  # 3/(12-level)
            for i in range(hmFocus_size):
                for j in range(hmFocus_size):
                    distanceFromCenter = 2 * \
                        np.linalg.norm(np.array([i-int(hmFocus_size/2),
                                                 j-int(hmFocus_size/2)]))/((hmFocus_size)/2)
                    gauss_prob = gauss(distanceFromCenter, stdv)
                    HM_FOCUS_IM[level, i, j, 0] = gauss_prob
        HM_FOCUS_IM[level, :, :, 0] /= np.sum(HM_FOCUS_IM[level, :, :, 0])

        heatmap_im = convert_image_dtype(HM_FOCUS_IM[0, :, :, :], tf.float32)
        heatmap_im = pad_to_bounding_box(
            heatmap_im,
            int(label[0] * config.scale + config.hm_size / 2 -
                hmFocus_size / 2),
            int(label[1] * config.scale + config.hm_size / 2 -
                hmFocus_size / 2), config.hm_size, config.hm_size)
        label = heatmap_im

    return (orientation, eyelandmark, leye_im, reye_im, label)
コード例 #4
0
ファイル: test_video.py プロジェクト: lc0/io
def test_video_predict():
    model = ResNet50(weights='imagenet')
    x = VideoDataset(video_path).batch(1).map(
        lambda x: preprocess_input(image.resize(x, (224, 224))))
    y = model.predict(x)
    p = decode_predictions(y, top=1)
    assert len(p) == 166
def path_to_image(path, image_size, num_channels, interpolation):
    img = io_ops.read_file(path)
    img = image_ops.decode_image(img,
                                 channels=num_channels,
                                 expand_animations=False)
    #img = image_ops.resize_images_v2(img, image_size, method=interpolation)

    #---------------------- This is the custom resizing

    size = image_size
    lo_dim = min(size)
    # Take width/height
    initial_width = shape(img)[0]
    initial_height = shape(img)[1]

    # Take the greater value, and use it for the ratio
    min_ = minimum(initial_width, initial_height)
    ratio = cast(min_, "float") / constant(lo_dim, dtype=float32)

    new_width = cast(cast(initial_width, "float") / ratio, "int32")
    new_height = cast(cast(initial_height, "float") / ratio, "int32")

    img = image.resize(img, [new_width, new_height])
    img = image.resize_with_crop_or_pad(img, size[0], size[1])
    #----------------------

    img.set_shape((image_size[0], image_size[1], num_channels))
    return img
コード例 #6
0
    def size(cls, uri, new_size=None, keep_aspect=False):
        """
        Return the current image dimensions or resize it.

        Parameters
        ----------
        uri: str
            Description of where the image are located

        new_size (optional): tuple
            The new intended dimension of the image

        keep_aspect (optional) (default=False): bool
            If the image proportions should be preserved
            or not.

        Returns
        -------
        tuple:
            Current image dimensions

        If new_size is True:
            numpy.ndarray:
                A resized image, if new_size parameter
                was passed through
        """
        if new_size:
            return image_ops.resize(
                cls.get_image(uri, as_tensor=True),
                new_size,
                method=image_ops.ResizeMethod.NEAREST_NEIGHBOR,
                preserve_aspect_ratio=keep_aspect)

        return cls.get_image(uri, as_tensor=True).shape[:2]
コード例 #7
0
def predict(img_bytes):

    # Decode
    x = image.decode_jpeg(img_bytes, channels=3)
    print('decoded')

    # Normalize
    x = image.convert_image_dtype(x, float32)
    print('normalized')

    # Resize
    np_x = image.resize(x, [224, 224]).numpy()
    x = np_x.tolist()
    print('resized')

    # Predict
    pred = model.predict(reshape(x, [-1, 224, 224, 3]))

    # Map
    top_k_values, top_k_indices = math.top_k(pred, k=3, sorted=True, name=None)

    top_k_values = top_k_values.numpy().tolist()[0]
    top_k_labels = [d[idx] for idx in top_k_indices.numpy()[0]]
    results = {'prob': top_k_values, 'prob_labels': top_k_labels}
    print(results)

    # Return
    return json.dumps(results)
コード例 #8
0
def run(input_data):
    data = json.loads(input_data)['data']

    base64_decoded = base64.b64decode(data.strip())
    image = Image.open(io.BytesIO(base64_decoded))
    image_np = np.array(image) / 255

    print("input shape")
    print(image_np.shape)

    image_np = resize(image_np, img_size)

    print("input shape")
    print(image_np.shape)

    image_np = reshape(
        image_np,
        (-1, image_np.shape[0], image_np.shape[1], image_np.shape[2]))

    print("input shape")
    print(image_np.shape)

    prediction = model.predict(image_np)

    return json.dumps(prediction.tolist())
コード例 #9
0
    def predict(self, image_array):
        if image_array.shape != (28, 28, 1):
            image_array = resize(image_array, (28, 28))
        image_array = image_array.reshape(1, 28, 28, 1)

        result = self.artifacts.classifier.predict_classes(image_array)[0]
        return class_names[result]
コード例 #10
0
ファイル: img.py プロジェクト: benoitkoenig/blobWar-image
def get_img(img_path):
    img = read_file(img_path)
    img = decode_jpeg(img, channels=3)
    img = resize(img, [image_size, image_size])
    img = 1 - img/255. # We would rather have the whole white void area be full of zeros than ones
    img = tf.convert_to_tensor([img])
    return img
コード例 #11
0
def get_discriminator(im_h, im_w, n_class, lrelu_alpha=0.2):

    disc_inp = Input((im_h, im_w, n_class))

    x = ZeroPadding2D(padding=(1,1))(disc_inp)
    x = Conv2D(64, kernel_size=(4,4), strides=2, padding='valid', activation=None)(x)
    x = LeakyReLU(alpha=0.2)(x)

    x = ZeroPadding2D(padding=(1,1))(x)
    x = Conv2D(128, kernel_size=(4,4), strides=2, padding='valid', activation=None)(x)
    x = LeakyReLU(alpha=0.2)(x)

    x = ZeroPadding2D(padding=(1,1))(x)
    x = Conv2D(256, kernel_size=(4,4), strides=2, padding='valid', activation=None)(x)
    x = LeakyReLU(alpha=0.2)(x)

    x = ZeroPadding2D(padding=(1,1))(x)
    x = Conv2D(512, kernel_size=(4,4), strides=2, padding='valid', activation=None)(x)
    x = LeakyReLU(alpha=0.2)(x)

    preds = Conv2D(1, kernel_size=(4,4), strides=2, padding='valid', activation=None)(x)

    upsample_layer = Lambda(lambda x: resize(x, size=(im_h, im_w), method='bilinear'))

    preds_upsample = upsample_layer(preds)
    preds_upsample = Activation('sigmoid')(preds_upsample)

    return Model(disc_inp, preds_upsample, name='discriminator')
コード例 #12
0
def process_path(file_path):
    print(file_path)
    file = read_file(file_path)
    file = image.decode_jpeg(file, channels=3)
    file = cast(file, float32)
    file = preprocess_input(file)
    file = image.resize(file, [ROW, COL])

    return file
コード例 #13
0
def resize_images_generator(X, y, batch_size, shape):
    size = len(X)
    epochs = len(X) // batch_size + 1

    while True:
        for e in range(epochs):
            images = X[e * batch_size:(e + 1) * batch_size]
            labels = y[e * batch_size:(e + 1) * batch_size]
            yield resize(images, shape), labels
コード例 #14
0
ファイル: app.py プロジェクト: holdmygithub/Data-Science
def predict():
    img = Image.open(file)
    st.image(img)
    img = np.asarray(img) / 255
    img = resize(img, (256, 256))
    img = expand_dims(img, axis=0)
    prediction = int(round(model.predict(x=img)).numpy()[0][0])
    prediction = class_indices[prediction]
    return prediction
コード例 #15
0
def resize_images_from(input_path, image_size):
    for class_dir in tqdm(os.listdir(input_path)):
        path = os.path.join(input_path, class_dir)
        files = os.listdir(path)
        for file in files:
            img = load_img(pathlib.Path(path, file))
            img = np.array(img)
            img = resize(img, image_size).numpy()
            save_img(pathlib.Path(path, file), img)
コード例 #16
0
ファイル: fid.py プロジェクト: wkuenne/album-gan
def fid_function(real_image_batch, generated_image_batch):
    """
	Given a batch of real images and a batch of generated images, this function pulls down a pre-trained inception 
	v3 network and then uses it to extract the activations for both the real and generated images. The distance of 
	these activations is then computed. The distance is a measure of how "realistic" the generated images are.

	:param real_image_batch: a batch of real images from the dataset, shape=[batch_size, height, width, channels]
	:param generated_image_batch: a batch of images generated by the generator network, shape=[batch_size, height, width, channels]

	:return: the inception distance between the real and generated images, scalar
	"""
    INCEPTION_IMAGE_SIZE = (299, 299)
    real_resized = image.resize(real_image_batch, INCEPTION_IMAGE_SIZE)
    fake_resized = image.resize(generated_image_batch, INCEPTION_IMAGE_SIZE)
    module.build([None, 299, 299, 3])
    real_features = module(real_resized)
    fake_features = module(fake_resized)
    return tfgan.eval.frechet_classifier_distance_from_activations(
        real_features, fake_features)
コード例 #17
0
ファイル: architecture.py プロジェクト: huynhngoc/deoxys
 def resize_by_axis(self, img, dim_1, dim_2, ax):
     resized_list = []
     # print(img.shape, ax, dim_1, dim_2)
     unstack_img_depth_list = tf.unstack(img, axis=ax)
     for j in unstack_img_depth_list:
         resized_list.append(
             image.resize(j, [dim_1, dim_2], method='bicubic'))
     stack_img = tf.stack(resized_list, axis=ax)
     # print(stack_img.shape)
     return stack_img
コード例 #18
0
def preprocess_input(data, labels):
    for i in tqdm(range(len(data))):
        try:
            data[i] = data[i].astype(np.float32)
            data[i] = resize(data[i], IMAGE_SIZE).numpy()
            #data[i] = rgb2gray(data[i])
            data[i] = 1. / 255. * data[i]
        except:
            del data[i]
            del labels[i]
    return data, labels
コード例 #19
0
def load_image(image_path):
    """
    Convert all image into array of shape (1, width, height, 3)
    """
    image = io.read_file(image_path)
    image = img.decode_image(image, channels=3)
    image = img.convert_image_dtype(image, float32)

    image = img.resize(image, (width, height))
    image = image[newaxis, :]
    return image
コード例 #20
0
    def __build_localizator_model(self):
        """
		original_shape = cuboid.shape[1:]
		split_cub_shape = (cuboid.shape[1],
							cuboid.shape[2]//self.__subwind_size[0],
							self.__subwind_size[0],
							cuboid.shape[3]//self.__subwind_size[1],
							self.__subwind_size[1], cuboid.shape[4])

		split_cub = cuboid.reshape(split_cub_shape)

		# Shift the cumuled axis to the first axises
		split_cub = np.rollaxis(split_cub, 1, 0)
		split_cub = np.rollaxis(split_cub, 3, 1)

		split_cub = split_cub.reshape((-1,) + split_cub.shape[2:])
		"""

        cub_length, width, height, channels = self._rec_model.input.shape[1:]
        split_cub_shape = (cub_length, width // self.__subwind_size[0],
                           self.__subwind_size[0],
                           height // self.__subwind_size[1],
                           self.__subwind_size[1], channels)

        # Make base model
        base_input_layer = Input(shape=(cub_length, self.__subwind_size[0],
                                        self.__subwind_size[1], channels))
        base_resize_layer = TimeDistributed(
            Lambda(lambda x: tf_image.resize(x, (width, height))))(
                base_input_layer)
        base_rec_model = self._rec_model(base_resize_layer)

        base_model = Model(inputs=base_input_layer, outputs=base_rec_model)

        # Add resizing layers at first of to the rec model to resize the tensors
        # to the input accepted by the model
        input_layer = Input(shape=(cub_length, width, height, channels))
        # Split cuboid in subcuboids
        split_layer = Reshape(split_cub_shape)(input_layer)
        permut_dim_layer = Lambda(
            lambda x: tf_transpose(x, [0, 2, 4, 1, 3, 5, 6]))(split_layer)
        acum_dim_layer = Reshape(
            (split_cub_shape[2] * split_cub_shape[4], cub_length,
             self.__subwind_size[0], self.__subwind_size[1],
             channels))(permut_dim_layer)
        # Resize each subcuboid at full cuboid size
        rec_model = TimeDistributed(base_model)(acum_dim_layer)
        # Predict each subcuboid
        #rec_model = self._rec_model(rec_model)

        model = Model(inputs=input_layer, outputs=rec_model)
        return model, base_model
        """
コード例 #21
0
def upsample(dst: KerasTensor, pre_name: str = '', idx: int = 0) -> layers.Layer:
    """
    A customized up-sampling layer using bi-linear Interpolation
    :param dst: the target tensor, need it's size for up-sample
    :param pre_name: the layer's prefix name
    :param idx: the index of layer
    :return: the up-sample layer
    """
    from tensorflow import image
    import numpy as np
    np.random.rand(30)
    return layers.Lambda(lambda x, w, h: image.resize(x, (w, h)),
                         arguments={'w': dst.shape[1], 'h': dst.shape[2]}, name=f"{pre_name}_upsample{idx}")
コード例 #22
0
def resize_display(x_train,y_train,
                   x_test,y_test,pixels):
    x_train=np.array(timage.resize(x_train,[pixels,pixels]))
    x_test=np.array(timage.resize(x_test,[pixels,pixels]))
    N=len(y_train); shuffle_ids=np.arange(N)
    np.random.RandomState(12).shuffle(shuffle_ids)
    x_train,y_train=x_train[shuffle_ids],y_train[shuffle_ids]
    N=len(y_test); shuffle_ids=np.arange(N)
    np.random.RandomState(23).shuffle(shuffle_ids)
    x_test,y_test=x_test[shuffle_ids],y_test[shuffle_ids]
    n=int(len(x_test)/2)
    x_valid,y_valid=x_test[:n],y_test[:n]
    x_test,y_test=x_test[n:],y_test[n:]
    df=pd.DataFrame([[x_train.shape,x_valid.shape,x_test.shape],
                     [x_train.dtype,x_valid.dtype,x_test.dtype],
                     [y_train.shape,y_valid.shape,y_test.shape],
                     [y_train.dtype,y_valid.dtype,y_test.dtype]],
                    columns=['train','valid','test'],
                    index=['image shape','image type',
                           'label shape','label type'])
    display(df)    
    return [[x_train,x_valid,x_test],
            [y_train,y_valid,y_test]]
コード例 #23
0
    def upload_frame():

        image = plt.imread(uploaded_file)
        image = cv2.resize(image,
                           dsize=(1280, 720),
                           interpolation=cv2.INTER_CUBIC)
        image_origin = image.copy()

        face_locations = detector.detect_faces(image)

        X1, X2, Y1, Y2 = [], [], [], []
        pred_values = {}

        for i in range(len(face_locations)):
            x1, y1, width, height = face_locations[i]["box"]
            x2, y2 = x1 + width, y1 + height

            cropped = image[y1:y2, x1:x2]
            pred = model.predict(
                np.expand_dims(resize(
                    (cropped), [224, 224]), axis=0) / 255.0)[0]
            top_values = pred.argsort()[-3:]
            prediction1 = classes[top_values[2]]
            pred1 = pred[top_values[2]]
            prediction2 = classes[top_values[1]]
            pred2 = pred[top_values[1]]
            prediction3 = classes[top_values[0]]
            pred3 = pred[top_values[0]]

            cv2.rectangle(image, (x1, y1), (x2, y2), (105, 60, 114), 5)
            cv2.putText(image, f"1: {prediction1}:{round(pred1*100)}%",
                        (x1, y2 + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9,
                        (244, 139, 41), 2)
            cv2.putText(image, f"2: {prediction2}:{round(pred2*100)}%",
                        (x1, y2 + 60), cv2.FONT_HERSHEY_SIMPLEX, 0.9,
                        (105, 60, 114), 2)
            cv2.putText(image, f"3: {prediction3}:{round(pred3*100)}%",
                        (x1, y2 + 90), cv2.FONT_HERSHEY_SIMPLEX, 0.9,
                        (244, 139, 41), 2)

            pred_values[i] = [[prediction1, round(pred1 * 100)],
                              [prediction2, round(pred2 * 100)],
                              [prediction3, round(pred3 * 100)]]

            X1.append(x1)
            X2.append(x2)
            Y1.append(y1)
            Y2.append(y2)

        return image, image_origin, X1, X2, Y1, Y2, pred_values, face_locations
コード例 #24
0
    def preprocess(self, image):
        image = resize(image,
                       (self.inp_shape[1],
                        self.inp_shape[2]))  # rescale to match the input size

        # change to grayscale if the model is in grayscale
        if self.inp_shape[3] == 1 and image.shape[2] != 1:
            if image.shape[2] == 3:
                image = rgb_to_grayscale(image)
            elif image.shape[2] == 4:
                image = rgb_to_grayscale(image[:, :, :3])

        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)
        return image
コード例 #25
0
def image_import(all_paths, img_size, ch=3):
    """Function to convert two images into tensors for putting it into the network
       Arguments: all_path - list of paths to images,
                  img_size - image size
                  ch - kind of image color channels
       Return: list of tensors"""

    images = [
        image.resize(image.decode_image(io.read_file(path)),
                     [img_size, img_size, ch]) for path in all_paths
    ]
    images /= 255.0
    print('Images are ready')

    return images
コード例 #26
0
def preprocess(x, data_format=None):
    """Preprocesses images for ResNet models.

  Order of preprocessing:
    - Resize to 256 by 256
    - Central crop to 224 by 224
    - Normalize values by scaling into the range [0, 1]
    - Normalize values by mean subtraction
    - Normalize values by dividing by the standard deviation
  """
    x = utils.resize(x, (256, 256))
    x = utils.central_crop(x, (224 / 256))
    return imagenet_utils.preprocess_input(x,
                                           data_format=data_format,
                                           mode='torch')
コード例 #27
0
def get_embedding(model, img):
    """
    Get embedding of input image
    Arguments:
        model: Embedding model
        img: Input image
    Return:
        Embedding of input image

    """
    img = resize(img, (160, 160))  # Resize
    img /= 255  # Normalize
    img = expand_dims(img, axis=0)  # Expand dimension
    embedding = model.predict(img)[0]  # Get embedding

    return embedding
コード例 #28
0
def load_img_by_classes(input_path, no_images=None, image_size=None):
    data = []
    labels = []
    for class_dir in tqdm(os.listdir(input_path)):
        path = os.path.join(input_path, class_dir)
        if no_images is None:
            files = os.listdir(path)
        else:
            files = os.listdir(path)[:no_images]
        for file in files:
            img = load_img(pathlib.Path(path, file))
            img = img_to_array(img)
            if image_size is not None:
                img = resize(img, image_size)
            data.append(img)
            labels.append(class_dir)
    return data, labels
コード例 #29
0
def ResizeConvReluMaxPool(X, weights, bias):
    resize = image.resize(X, [N + 2, N + 2])
    conv = nn.conv2d(resize,
                     weights,
                     strides=[1, 1, 1, 1],
                     padding="VALID",
                     data_format="NHWC")
    conv_bias = nn.bias_add(conv, bias, data_format="NHWC")

    relu = nn.relu(conv_bias)
    maxpool = nn.max_pool2d(relu,
                            ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1],
                            padding="VALID",
                            data_format="NHWC")

    return maxpool
コード例 #30
0
    def augmentation(self, image, label, th=.90):
        """  Basic Data augmentation  used in this pipeline
        in this step only use a basic transformation

        :param image: tensor sample
        :param label: one hot label
        :param th: random value which triggered a augmentation

        :return: 0-1 tensor and one-hot label

        """

        if random() > th:
            size = uniform(th, 1.0)
            image = central_crop(image, central_fraction=size)
            image = resize(image, self.shape, antialias=True)
        return image, label