Adding an additional channel to a 3 channel tensor - python
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)
Related
Reshaping numpy array of images results in extra dimension
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)
how to reduce the second dimension in pytorch python tensor operation
I have the below tensor with the following shape seq = dataset['features'][...] print(f'shape of seq before unsequeeze {seq.shape}') shape of seq before unsequeeze (461, 1024) I am trying to convert the shape in (461, 512) How should I achieve this in pytorch tensor operation. examples feature x as below, import torch device = torch.device("cuda" if torch.cuda.is_available() else "cpu") x = torch.tensor([[0.1,0.8,0.8,0.4,0.9,0.4,0.4,0.5,0.4,0.2,0.3,0.8,0.2,0.7,0.6,0.1,0.2,0.1,0.1,0.4,0.2,0.1,0.0,0.5,0.2,0.4,0.3,0.3,0.7,0.1,0.4,0.6,0.5,1.0,0.1,0.8,0.9,0.0,0.2,0.9,0.8,0.0,0.9,0.7,0.2,0.2,0.9,0.6,0.1,0.2,0.6,0.0,0.1,0.1,0.3,0.5,0.8,0.8,0.4,0.4,0.7,0.7,0.4,0.2,0.1,1.0,0.3,0.8,0.1,0.7,0.7,0.9,0.6,0.3,0.8,0.2,0.9,0.6,0.7,0.8,0.2,0.1,1.0,0.6,0.5,0.5,0.5,0.8,0.8,0.3,0.1,0.2,0.5,0.9,0.6,0.8,0.0,0.6,0.2,0.1,0.8,0.4,0.8,0.5,0.8,0.4,0.7,0.6,0.8,0.1,0.4,0.8,1.0,0.9,0.4,0.4,0.4,0.1,0.7,0.3,0.8,0.6,0.4,0.5,0.9,0.1,0.9,0.7,0.4,0.7,0.1,0.8,0.2,0.2,0.7,0.2,0.9,0.6,0.2,0.9,0.1,0.9,0.2,1.0,0.9,0.6,0.3,0.6,0.9,0.6,0.0,0.3,0.4,0.6,0.7,0.9,0.2,0.6,0.2,0.5,0.3,0.3,0.4,0.4,0.1,0.2,0.6,0.0,0.7,0.5,0.5,0.2,0.5,0.6,0.5,0.5,0.7,0.8,0.4,0.5,0.8,0.8,0.1,0.5,0.7,0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0.6,0.0,0.1,0.3,0.7,0.4,0.1,0.2,0.7,0.2,0.3,0.8,0.2,0.4,0.2,1.0,1.0,0.7,0.8,0.2,0.5,0.3,0.5,0.4,0.6,0.5,0.3,0.6,0.5,1.0,0.7,0.8,0.9,0.0,0.6,0.3,0.9,0.3,0.9,0.5,0.7,0.5,0.1,0.1,0.3,0.7,0.8,0.1,0.0,0.7,0.5,1.0,0.3,0.8,0.7,0.7,0.2,0.9,0.5,0.6,0.1,0.5,0.5,0.0,0.2,0.7,0.9,0.1,0.9,0.3,0.2]]).to( device, dtype=torch.int64 ) x.shape torch.Size([2, 1024]) I need to reduce the feature size to 512 keeping the first dim batch size in tact, x.shape torch.Size([2, 512]) Thanks
z = torch.narrow(x,1,0,512) z.shape torch.Size([2, 512]) if you use x.unsqueeze(0), then the dim 2 will require the change, z = torch.narrow(x,2,0,512)
How to append numpy ndarray (images) to get similar dataset like mnist
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.
Display extracted feature vector from trained layer of the model as an image
I am using Transfer learning for recognizing objects. I used trained VGG16 model as the base model and added my classifier on top of it using Keras. I then trained the model on my data, the model works well. I want to see the feature generated by the intermediate layers of the model for the given data. I used the following code for this purpose: def ModeloutputAtthisLayer(model, layernme, imgnme, width, height): layer_name = layernme intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) img = image.load_img(imgnme, target_size=(width, height)) imageArray = image.img_to_array(img) image_batch = np.expand_dims(imageArray, axis=0) processed_image = preprocess_input(image_batch.copy()) intermediate_output = intermediate_layer_model.predict(processed_image) print("outshape of ", layernme, "is ", intermediate_output.shape) In the code, I used np.expand_dims to add one extra dimension for the batch as the input matrix to the network should be of the form (batchsize, height, width, channels). This code works fine. The shape of the feature vector is 1, 224, 224, 64. Now I wish to display this as image, for this I understand there is an additional dimension added as batch so I should remove it. Following this I used the following lines of the code: imge = np.squeeze(intermediate_output, axis=0) plt.imshow(imge) However it throws an error: "Invalid dimensions for image data" I wonder how can I display the extracted feature vector as an image. Any suggestion please.
Your feature shape is (1,224,224,64), you cannot directly plot a 64 channel image. What you can do is plot the individual channels independently like following imge = np.squeeze(intermediate_output, axis=0) filters = imge.shape[2] plt.figure(1, figsize=(32, 32)) # plot image of size (32x32) n_columns = 8 n_rows = math.ceil(filters / n_columns) + 1 for i in range(filters): plt.subplot(n_rows, n_columns, i+1) plt.title('Filter ' + str(i)) plt.imshow(imge[:,:,i], interpolation="nearest", cmap="gray") This will plot 64 images in 8 rows and 8 columns.
A possible way to go consists in combining the 64 channels into a single-channel image through a weighted sum like this: weighted_imge = np.sum(imge*weights, axis=-1) where weights is an array with 64 weighting coefficients. If you wish to give all the channels the same weight you could simply compute the average: weighted_imge = np.mean(imge, axis=-1) Demo import numpy as np import matplotlib.pyplot as plt intermediate_output = np.random.randint(size=(1, 224, 224, 64), low=0, high=2**8, dtype=np.uint8) imge = np.squeeze(intermediate_output, axis=0) weights = np.random.random(size=(imge.shape[-1],)) weighted_imge = np.sum(imge*weights, axis=-1) plt.imshow(weighted_imge) plt.colorbar() In [33]: intermediate_output.shape Out[33]: (1, 224, 224, 64) In [34]: imge.shape Out[34]: (224, 224, 64) In [35]: weights.shape Out[35]: (64,) In [36]: weighted_imge.shape Out[36]: (224, 224)
Display output of vgg19 layer as image
I was reading this paper: Neural Style Transfer. In this paper author reconstructs image from output of layers of vgg19. I am using Keras. The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1 is number of images as input, 400 is number of rows, 533 number of columns and 64 number of channels. When I try to reconstruct it as an image, I get an error as size of image is 13644800 which is not a multiple of 3, so I can't display the image in three channels. How can I reconstruct this image? I want to reconstruct images from layers as shown below: Below is the code for the same: from keras.preprocessing.image import load_img, img_to_array from scipy.misc import imsave import numpy as np from keras.applications import vgg19 from keras import backend as K CONTENT_IMAGE_FN = store image as input here def preprocess_image(image_path): img = load_img(image_path, target_size=(img_nrows, img_ncols)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg19.preprocess_input(img) return img width, height = load_img(CONTENT_IMAGE_FN).size img_nrows = 400 img_ncols = int(width * img_nrows / height) base_image = K.variable(preprocess_image(CONTENT_IMAGE_FN)) RESULT_DIR = "generated/" RESULT_PREFIX = RESULT_DIR + "gen" if not os.path.exists(RESULT_DIR): os.makedirs(RESULT_DIR) result_prefix = RESULT_PREFIX # this will contain our generated image if K.image_data_format() == 'channels_first': combination_image = K.placeholder((1, 3, img_nrows, img_ncols)) else: combination_image = K.placeholder((1, img_nrows, img_ncols, 3)) x = preprocess_image(CONTENT_IMAGE_FN) outputs_dict = dict([(layer.name, layer.output) for layer in model.layers]) feature_layers = ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1'] outputs = [] for layer_name in feature_layers: outputs.append(outputs_dict[layer_name]) functor = K.function([combination_image], outputs ) # evaluation function # Testing test = x layer_outs = functor([test]) print(layer_outs) layer_outs[0].reshape(400, -1 , 3) //getting error here I am getting following error: ValueError: cannot reshape array of size 13644800 into shape (400,newaxis,3)
You wrote: "The size of output of block1_conv1 layer is (1, 400, 533, 64). Here 1 is number of images as input, 400 is number of rows, 533 number of columns and 64 number of channels" But this is not correct. The block1_conv1 output corresponds 1 channel dimension(channel first), 400 * 533 image dimension and 64 filters. The error occurs, as you try to reshape a vector of VGG19 output of an image input with a 1 channel (400 * 533 * 64 = 13644800) to a vector which correspond to a 3 channels output. Furthermore you have to pass 3 channel input: From the VGG19 code: input_shape: optional shape tuple, only to be specified if include_top is False (otherwise the input shape has to be (224, 224, 3) (with channels_last data format) or (3, 224, 224) (with channels_first data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. (200, 200, 3) would be one valid value. Thus your input images has to be 3 channels. If you even want to feed 1 channel(grayscale) images to VGG19 you should make the following, if channels first: X = np.repeat(X, 3 , axis=0) or X = np.repeat(X, 3 , axis=2) if channels last without batch dimension or X = np.repeat(X, 3 , axis=3) with batch dimension. If you provide more information regarding the actual dimensions of your input matrices of your images and type of it(grayscale,RGB), I can give you more help upon needing it.