GithubHelp home page GithubHelp logo

tensorflow-image-pipeline's Introduction

TensorFlow Image Pipeline

StackOverflow Question: Why shape of 3D tensor (of an image) in filter() has None?

I am following this tutorial on tensorflow.org.

I have folder images with two folders cat and dog in it. Following above tutorial I am trying to convert .jpg and .png images to features (NumPy array) for modeling.

Problem

After processing the images to tensors I found that some images were converted to tensor with shape (28, 28, 4). So I added condition to filter out such tensors. This logic works while explicitly looping each tensor, using for loop, after converting it to numpy array, but same logic does not work when used with filter.

Please help me fix this filter() I went through filter() documentation and could not find any solution.

Source code

import tensorflow as tf
import os

print("TensorFlow version:", tf.__version__)

def process_image(file_path_tensor):
    parts = tf.strings.split(file_path_tensor, os.sep)
    label = parts[-2]

    image = tf.io.read_file(file_path_tensor)
    image = tf.image.decode_jpeg(image)
    image = tf.image.resize(image, [128, 128])
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = image / 255

    return image, label


def check_shape(x, y):
    print("\nShape received in filter():", x.shape)
    d1, d2, d3 = x.shape
    return d3 == 3


images_ds = tf.data.Dataset.list_files("./images/*/*", shuffle=True)

file_path = next(iter(images_ds))
image, label = process_image(file_path)

print("Shape:", image.shape)
print("Class label:", label.numpy().decode())

# ETL pipeline.
X_y_tensors = (
    images_ds
    .map(process_image)   # Extra and Transform
    .filter(check_shape)  # Filter
    .as_numpy_iterator()  # Load
)

print("\nTechnique 1:")
print("Final X count:", len(list(X_y_tensors)))


X_y_tensors = images_ds.map(process_image)

count = 0
for x, y in X_y_tensors:
    d1, d2, d3 = x.shape
    if d3 > 3:
        continue
    count += 1

print("\nTechnique 1:")
print("Final X count:", count)

Output

TensorFlow version: 2.6.0
Shape: (128, 128, 3)
Class label: cat

Shape received in filter(): (128, 128, None)

Technique 1:
Final X count: 0

Technique 1:
Final X count: 123

As it can be seen,

  1. Count is 0 when Technique 1 is used to filter tensors, since the shape of the tensor received is (128, 128, None).
  2. Count is 123 (image count after filtering) when Technique 2 is used.

I do not think this is an issue since I am not using batches.

Full code with dataset

tensorflow-image-pipeline's People

Contributors

dheemanthbhat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.