enter code here
img = cv2.imread(f'resized_data/train/normal/IM-0115-0001.jpeg')
img2 = cv2.imread(f'resized_data/train/normal/IM-0117-0001.jpeg')
imgs = []
imgs.append(img)
imgs.append(img2)
imgs = np.array(imgs)
So I have two numpy.ndarray so far, with the shape of (256, 256, 3) each.
I append them to a list, which I will convert to a numpy ndarray later. When I call the imgs.shape function the shape is the following --> (2, ).
Why is the shape of the imgs array (2, ) and not (2, 256, 256, 3) ?
Thanks in advance.
Related
I'm decoding a base64 image with the following code:
def string_to_image(base64_string):
decoded = base64.b64decode(base64_string)
np_data = np.frombuffer(decoded, np.uint8)
img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED)
return img
The goal is to receive an image from the request body, decode it, resize it with tensorflow, predict it with a model, and return a response saying what is that image:
image_base64 = request.json['image']
decoded_image = string_to_image(image_base64)
image_resized = tf.image.resize(decoded_image, (256, 256))
model = load_model('src/models/mymodel.h5')
result = model.predict(np.expand_dims(image_resized/255, 0))
However, I'm getting the error ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 256, 256, 4).
I don't know how to change the Shape value from '4' to '3'.
I tried the following:
image_resized = tf.image.resize(decoded_image, (256, 256, 3))
But I get 'size' must be a 1-D Tensor of 2 elements: new_height, new_width.
I also tried:
image_resized = cv2.resize(decoded_image, (256,256,3))
But I get OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'resize'
Overload resolution failed:
- Can't parse 'dsize'. Expected sequence length 2, got 3
- Can't parse 'dsize'. Expected sequence length 2, got 3
Please help :(
You could reshape the array by using tf.squeeze after reshaping the tensor. According to documentation, tf.squeeze will remove axis with dimensions 1.
image_resized = tf.reshape(decoded_image, (-1, 256, 256, 3, 1))
image_resized = tf.squeeze(image_resized)
With vijayachandran mariappan comment and AndreaYolo answer I figured out a solution. First, change the channels of the image and then resize its dimensions:
decoded_image = string_to_image(image_base64)
decoded_image = decoded_image[:,:,:3]
image_resized = tf.image.resize(decoded_image, (256, 256))
My model then was able to predict perfectly!
I would like to combine a tensor of shape [3,1024,1024] and a tensor of shape [1,1024,1024] in order to form a single tensor of shape [4,1024,1024]
This is to combine the channels of an RGB image to a depth image in the format of [r,g,b,d] for each pixel
I am currently trying to do this like this:
tensor = tf.concat([imageTensor, depthTensor], axis=2)
But I receive the error
InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [3,1024,1024] vs. shape[1] = [1,1024,1024] [Op:ConcatV2]
I was just wondering how this would be done?
You want to concatenate on axis=0:
import tensorflow as tf
t1 = tf.random.uniform((3, 1024, 1024))
t2 = tf.random.uniform((1, 1024, 1024))
final_tensor = tf.concat((t1, t2), axis=0)
print(final_tensor.shape)
(4, 1024, 1024)
I want to predict from image url. In the past, I use ImageDatagenerator().flow_from_directory() methods, but now I have only one image. so I want to predict from this single image.
I have tried the below code, but failed. (Dimension error)
url = "http://3.36.149.28/uploads/WEBUPLOADprofile.png"
img = Image.open(requests.get(url, stream=True).raw)
img = img_to_array(img)
img = img/255.
#Predict
pred = model.predict(img)
so I tried reshape & retrying, but failed (cannot reshape array of size 1048576 into shape (28,28,1))
img = img.reshape(-1, 28, 28, 1)
img = img/255.
#Predict
pred = model.predict(img)
for getting reshape & get colored predict image, what can I do ? please help..
Additional : I trained srcnn model, and inputs :
inputs = Input((None, None, 3), dtype='float')
I resolved this problem.
First, my url image shape is (None, None, 4), but my trained shape is (None, None, 3).
So I tried another jpg image (None, None, 3) and expand dimension via np,
and result shape = (1, None, None, 3)
image = np.expand_dims(image, axis=0)
model.predict(image)
from link
and now I get predict image successfully.
I have 440 images with the same size 924 x 640 and three channels. I load them via
image_data = []
for filename in iglob(os.path.join(store, '*.jpg')):
image_data.append(plt.imread(filename))
Then I make a numpy ndarray from this list:
image_np_orig = np.array(image_data)
This array has a shape (440,) and it consists of elements with shape of (924, 640, 3). I want to make some t-SNE transformations on this array of images, so I want to reshape the array to make it's shape look like (440, 1):
image_np = image_np_orig.reshape(image_np_orig.shape[0], -1)
Expectation / Reality
I expect to see an array image_np of shape (440, 1) where each element of the first dimension (axis=0) is an array of shape (924, 640, 3). However I get an array image_np of shape (440, 1), where each element of the first dimension is an array of shape (1,) and in these arrays each element of their respective first dimensions is of shape (924, 640, 3).
What I've tried
I've tried
image_np = image_np_orig[:, np.newaxis]
with the same results.
I`ve also tried
image_np = np.stack(image_np_orig)
which lead to image_np with the shape of (440, 924, 640, 3) and then I got the mistake during the t-SNE transform:
from sklearn.manifold import TSNE
tsne = TSNE(n_components=2, init='pca')
X_tsne = tsne.fit_transform(image_np)
returns ValueError: Found array with dim 4. Estimator expected <=2.
Probably relevant
It may be relevant that image_np_orig has dtype object and image_np_orig[0] has dtype uint8. If this is relevant then how can I reshape arrays of different types?
From what I understand, you have an array of shape (440, 1, 924, 640, 3), but you actually need (440, 924, 640, 3)
Try:
image_np = image_np_orig.squeeze()
This will squeeze out the unnwanted dimension.
I'm not sure why the first approach doesn't work for you. But since image_np = np.stack(image_np_orig) returns the 4D data, you can go from there:
image_np = np.stack(image_np_orig).reshape(len(image_np_orig), -1)
I need to build a numpy array with images of different shapes to training a Fully convolutional network, the array need to have rank 4, the shape that I need is [64, None, None, 3], and the code that I´m using is like that:
batch_x = []
for i, j in enumerate(index_array):
image = cv2.imread(self.filenames[j], is_color)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
batch_x.append(image)
batch_x = np.array(batch_x)
np.reshape(batch_x, (64, None, None, 3))
But the numpy consider each image as a separated object.