I am trying to reshape the image so as to run gan models but I get this error.
AttributeError: 'BatchDataset' object has no attribute 'reshape
I'm assuming you're trying to utilize a Tensorflow Dataset with an existing Keras example. In this case, you'll need to use a map function to achieve the image resize and conversion to grayscale which would match the MNIST input shape I believe you're going for. This example below should show you how to accomplish this in your code.
a = tf.ones([180,180,3])
dataset = tf.data.Dataset.from_tensors(a)
# Note, how we're first using tf.image.resize to reduce the image size to 28x28 and then using rgb_to_grayscale to convert from 3 channels into 1.
dataset = dataset.map(lambda x: tf.image.rgb_to_grayscale(tf.image.resize(x, (28,28))))
Related
I'm trying to create a Keras model to train with a group of images, taken from a list of paths.
I know that the method tf.keras.utils.image_dataset_from_directory exists but it doesn't meet my needs because I want to learn the correct way to handle images and because I need to make a regression, not a classification.
Every approach I tried failed one way or another, mostly because the type of the x_train variable is wrong.
The most promising function I used to load a single image is:
def encode_image(img_path):
img = tf.keras.preprocessing.image.load_img(img_path)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
return img_array
x_train = df['filename'].apply(lambda i: encode_image(i))
This doesn't work because, when I call the .fit() method this way:
history = model.fit(x_train, y_train, epochs=1)
I receive the following error:
Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
This makes me understand that I'm passing the data in a wrong format.
Can someone provide me a basic example of creating a (x_train, y_train) pair to feed a model for training using a set of images?
Thank you very much
I am following the notebook on TF-Hub: Fast Style Transfer for Arbitrary Style and am having difficulties importing my own images. Some background, this notebook basically does image augmentation using 2 input images.
Here is the code: https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_arbitrary_image_stylization.ipynb
I am importing images through:
img = cv.imread('path_to_directory')
img = np.float32(img)
which gives me ndarray with shape (256, 256, 3).
However, the images being used in the original notebook are of EagerTensor with shape (1, 256, 256, 3).
When I run the image generator:
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]
InvalidArgumentError: The first dimension of paddings must be the rank of inputs[4,2], [256,256,3]
[[node transformer/contract/MirrorPad (defined at /usr/local/lib/python3.7/dist-packages/tensorflow_hub/module_v2.py:106) ]] [Op:__inference_pruned_12110]
and if I understood correctly, it is expecting a 4 dimensional image which mine is only of 3 dimensions. I am not sure what the 2 means. As such, I believe it is due to the differing input shapes.
Could someone explain what exactly is the difference between the 2 images and how exactly do I convert the first to the second format? If I have misunderstood the error and the problem was not because of the image dimensions, please correct me. Thank you.
Edit:
After running the code:
tf.expand_dims(img,axis=0)
Output image
The image appears to become discoloured as shown in the link.
I am trying to train my model for image segmentation task and for that i am using a generator to yield the dataset. i have trained it multiple times before but recently I am facing this error.
ValueError:'generator' yielded an element of shape (128,192,3) where an element of shape (128,192,1) was expected.
when i printed out the shapes of my image and mask that comes out of generator it shows.
image:(128,192,1)
mask:(128,192,3)
The generator element gets both the image and mask data loaded from the tensorflow dataset. The question is how does the shape of the mask of an grayscale image changes to 3 when even the input image is grayscale of 1?
How to possibly convert the mask back to channel of 1?
Unfortunately I cannot post the complete code to reproduce as its under privacy
Without knowing more of the library you're using for reading the image it's hard to know. I am assuming you're using PIL and I'll do
from PIL import Image
img = Image.open('im1.jpg','r')
img = img.convert('L')
'L' is for the grayscale, you can check more mode -> https://pillow.readthedocs.io/en/stable/handbook/concepts.html
While you're updating your code, you should check the preprocessing module of keras, then the code will be
# Returns a PIL image
image = tf.keras.preprocessing.image.load_img(image_path, color_mode="grayscale")
input_arr = keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr]) # Convert single image to a batch.
predictions = model.predict(input_arr)
More information -> https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/load_img
I recently started studying CNNs with tensorflow and found tfrecords very helpful in speeding up the training, however I'm struggling with data API.
After parsing, my dataset is composed of (image, label) tuples, this is fine for training, however I'm trying to extract the image in another dataset to call keras.predict() on.
I've tried this solution:
test_set = get_set_tfrecord(test_path, _parse_function, num_parallel_calls = 4)
lab = []
f = True
for image, label in test_set.take(600):
if f:
img = tf.data.Dataset.from_tensors(image)
f = False
else:
img = img.concatenate(tf.data.Dataset.from_tensors(image))
lab.append(label.numpy())
naive, not great code, but it works EXCEPT in order to perform concatenation (i.e. stacking) it loads every image into RAM.
What's the proper way of doing this?
You can use the map API from tf.data.Dataset. You can write the following code.
result = test_set.map(lambda image, label: image)
# You can iterate and check what you have received at the end.
# I expect only the images.
for image in result.take(1):
print(image)
I hope that using the above code you resolve your issue and that this answer serves you well.
I encountered a quite weird problem. After running some images through my neural network and trying to display the segmentation as follows:
print(label.shape)
torch.Size([1, 3, 321, 321])
Now displaying the image with matplotlib shows everything correctly:
plt.imshow(fake_seg_tb[0].permute(1,2,0))
When I throw the image into tensorboard (pytorch API), I get a red version of the image, which is not what I have as pixel values.
writer_semisuper = SummaryWriter()
writer_semisuper.add_image('My Label: ', torchvision.utils.make_grid(label), some_step)
The library I am importing is (PyTorch 1.5.0):
from torch.utils.tensorboard import SummaryWriter
With the tensorboard --version beeing 2.1.0.
I have no clue why in matplotlib and in the tensor the RGB values are correct but when i display it, it is just not right.
I was facing this problem because, in the dataloader I was modifying my output labels to have a particular standard deviation and mean. But tensorboard is unaware of this modification to your labels.
Solution
Before passing your image to writer_semisuper.add_image, you need to apply an inverse transform to your grid. For example, if your transform in your dataloader was
data_transforms = transforms.Compose([transforms.ToTensor(),transforms.Normalize([0.485, 0.485, 0.485], [0.229, 0.229, 0.229])])
You need to create a inverse transform like
inv_normalize = transforms.Normalize(
mean=[-0.485/0.229, -0.485/0.229, -0.485/0.229],
std=[1/0.229, 1/0.229, 1/0.229]
)
Now write your grid to tensorboard using
writer_semisuper = SummaryWriter()
writer_semisuper.add_image('My Label: ', inv_normalize(torchvision.utils.make_grid(label)), some_step)