Openvino xml file graph output shape not as expected - openvino

I've been trying to reshape to NCHW format using tf.keras.layers.Reshape function but the final xml file outputs this :
There's a Transpose layer after reshaping to (1,6,26,26) and final shape is (1,26,6,26)
I'm not sure why there's a Transpose layer , I want the shapes to be (1,6,26,26)
What's the reason ?

First and foremost, you need to understand the topology of your model's network. Then you can reshape and batch.
You can use the model optimizer to manipulate your input size.
This is how you can do it: Model Optimizer Advanced Reshape, Batching

Related

Can you control the outputs when converting pytorch to CoreML?

❓Question
I'm trying to convert a pytorch model to coreml. The model was based on yolov5.
Here is a netron view our our exported coreml model.
Currently, the architecture has 3 outputs. You can see one of the outputs in the screenshot, number '740'.
However, we want a different output from coreml. We need to get the output before the reshapeStatic and transpose layers. So in this image you can see that we need the last convolution layer instead of 740.
Those reshapeStatic and transpose layers were added by the process which convert the net to coreML, they are not organic layers of yolov5.
Is there any way we can do the conversion to coreml differently in order to have more control over which layers are output? For example, can we have control over the output layers in the sample code below:
model = ct.convert(
traced_model,
inputs=[ct.ImageType(name="input_1", shape=example_input.shape)],
classifier_config = ct.ClassifierConfig(class_labels) )
Alternatively, is there a way where we can choose at runtime which values to pull out of the coreml model? For example, is there a way to specify in the code below which layers we want to output?
img = load_image(img_path, resize_to=(img_size, img_size))
//can we specify here which layers to output?
coreml_out_dict = model.predict({'image': img})
Thanks!

Reshape Images from ImageDataGenerator

from tensorflow.keras.preprocessing.image import ImageDataGenerator
With image data generator's flow_from_directory method can we reshape images also.
e.g. we have color images in 10 classes in 10 folders and we are providing path of that directory let's say train:
gen = ImageDataGenerator(rescale=1./255, width_shift_range=0.05, height_shift_range=0.05)
train_imgs= gen .flow_from_directory(
'/content/data/train',
target_size=(10,10),
batch_size=1,
class_mode='categorical')
Now my model is taking input shape 300. And I want to define training data from this train_imgs that is images of 10X10X3.
Is there any library, method or option available to convert this data generator to matrix in which columns are each image vector?
Generally the best option in these cases is to add a Reshape layer to the start of your model: layers.Reshape((300), input_shape=(10,10,3)). You can also do layers.Reshape((-1), input_shape=(10,10,3)), and it will automatically figure out the correct output length.

How to change input type(image) to list or array when using PyTorch tutorial code

I have searched the code that uses list or array input data for training DQN code. But I have could not find any code.
Currently, I reference the reinforcement learning tutorial(DQN) of Pytorch.
However, this code uses image input data.
I want to know how to change the image input data to list or array input data.
(I need help to resolve my research that uses list input data. List input data shape is 1 by 9. )
In PyTorch, we deal with tensors. Images, text, even sounds can be transformed to tensors and then PyTorch models can learn on the data.
In PyTorch image classifier examples, you often see something like this, to transform images to tensors:
train_transform = transforms.Compose([
transforms.Resize(x),
...
transforms.ToTensor()
])
If your input is a numpy array x, you can convert it to a tensor like this:
torch.from_numpy(x)
You also have to pay attention to tensor dimensions, your input data needs to match what the model expects in the first layer.

add histogram summaries to tensorboard while using Keras train_on_batch

I am using model.train_on_batch in keras in order to be able to handle different batches of input data differently. Essentially I cannot use model.fit
But I need to store histogram and images of activations and weights in Tensorboard. Is there a way to do this?
You can do it manually by calling summary.histogram and passing the weights of each layer as below
with summary_writer.as_default():
for layer in self.model.layers:
for weight in layer.weights:
tf.summary.histogram('weights/{}'.format(layer.name), weight, step=your_step)

Keras LSTM error: Input from layer reshape is incompatible with layer lstm

Using RapidMiner I want to implement an LSTM to classify patternes in a time series. Input data is a flat table. My first layer in the Keras operator is a core reshape from exampleset_length x nr_of_attributes to batch x time-steps x features. In the reshape parameter I specifically enter three figures because I want a specific amount of features and time-steps. The only way to achieve this is to specify also batch size, so in total three figures. But when I add a RNN LSTM layer an error is returned: Input is incompatible with layer lstm expected ndim=n found ndim=n+1. What’s wrong?
When specifying 'input_shape' for the LSTM layer, you do not include the batch size.
So your 'input_shape' value should be (timesteps, input_dim).
Source: Keras RNN Layer, the parent layer for LSTM

Resources