What is the output of .pth after prediction - conv-neural-network

I am creating a multi class image classification network. My model has ben trained and saved under the notation model.pth. I have loaded it and then passed input to it which is an image tensors. However its giving me weird output.
The output is as follows. I have three classes in my dataset.
tensor([[-0.0997, -0.1605, -0.2177]], device='cuda:0',
grad_fn=)

Related

Extract output of an intermediate layer from a trained u-net model in keras

I have trained a U-Net model in keras with images in input. The model generates image outputs. Now I have to extract the output from an intermediate layer using the trained weights. I tried creating a new model with the desired intermediate layer as output and loading the saved weights. But then the trained weights can not be loaded as the layers don't match. How to generate the output from an intermediate layer using the saved trained weights?

Weight Initialization for Mask RCNN without using pretrained weights from Imagenet / COCO

# define the model
model = MaskRCNN(mode='training', model_dir='./', config=config)
# load weights (mscoco) and exclude the output layers
model.load_weights('mask_rcnn_coco.h5', by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])
# train weights (output layers or 'heads')
model.train(train_set, test_set, learning_rate=config.LEARNING_RATE, epochs=2, layers='heads')
I have certain medical images containing fibroids.
I wish to apply instance segmentation or object detection.
I may have to use mask Rcnn for instance segmentation and object detection. Is it possible to design the network from scratch instead of using transfer learning?
What I mean here is random initialization of weights for my data, instead of using weights derived from imagenet data or coco data.
From the command line,instead of training a model starting from pre-trained COCO weights like this
python my_model.py train --dataset=/path/dataset --weights=coco
execute the following line.
python my_model.py train --dataset=/path/dataset
And to start training from the first layer execute the following code.
model.train(dataset_train, dataset_val,learning_rate=config.LEARNING_RATE,epochs=10, layers='all')
Can't you just run the training without doing the model.load_weights() line? It seems to be running fine for me when I do that. I assume that runs it with randomized initial weights. It didn't result in quite as good results as starting with coco does, but I'm sure that's expected behavior for some datasets.

Tensorflow - building a CNN model as described in the tutorial

I just completed the implementation of A Guide to TF Layers: Building a Convolutional Neural Network for the MNIST data set. The training model successfully ran and gave accuracy of 97.3%.
However, the tutorial does not mention how to use this new trained model to supply own images and see the predictions. Does anyone know how to use the output of the training model to make predictions? I see in the tmp/mnist_convnet_model$ folder, there are some output files like .pbtxt , meta files and index files. But I can't find instructions to use them for making predictions on my own images.
y_pred = tf.nn.softmax(your_final_layer)
y_pred_cls = tf.argmax(y_pred, dimension=1)
and for prediction
feed_dict = {x: [your_image]}
classification = tf.run(y_pred_cls, feed_dict)
print classification
This applies to just about any model you create

Keras, siamese network, how to abstract feature?

I'm trying to modify Keras Siamese Network example to get image feature.
The problem is, how can I get image features? The output of last layer is only a number. What should I do to get the feature before euclidean_distance?
You can try to first train the model on the entire dataset and save it.
Load the model back again, now set the output layers to be processed_a and processed_b
Now call the model.predict() function on the entire dataset once again and you'll have the features for each image in the dataset.
Have a look at this
Hope this helps!
To get the embeddings from the Keras siamese network MNIST example after training:
model_a = Model(inputs=model.input, outputs=processed_a)
model_a.predict([tr_pairs[:, 0], tr_pairs[:, 1]])
I did it as follows (reference from my github post):
My trained siamese model looked like this:
siamese_model.summary()
Note that my newly redefined model is basically the same as the one highlighted in yellow
I then redefined my model which I wanted to use for extracting embeddings (It should be the same model you defined except now it will not have those multiple inputs like siamese) which looked like this:
siamese_embeddings_model = build_siamese_model(input_shape)
siamese_embeddings_model .summary()
Then I just extracted the weights from my trained siamese model and set them into my new model
embeddings_weights = siamese_model.layers[-3].get_weights()
siamese_embeddings_model.set_weights(embeddings_weights )
Then you can supply the new Image to extract the embeddings from the new model
vector = siamese.predict(image)
len(vector[0]) it will print 150 because of my fine dense layer (which are the output vector)

Feed an unseen example to a pre-trained model made in Keras

I've implemented a neural network using Keras. Once trained and tested for final test accuracy, using a matrix with a bunch of rows containing features (plus corresponding labels), I have a model which I should be able to use for prediction.
How can I feed a single unseen example, meaning a feature vector to the model, to obtain a class prediction?
I've looked at their documentation here but could not find a method for it.
What you want is the predict method, it takes a batch of input samples and produces predictions, which are the outputs computer by your network. To feed a single example you can just put it inside a numpy ndarray wrapper.

Resources