I have convert PyTorch SuperResouloution (.pth) Model to ONNX model (.onnx) with code below:
import torch
import onnx
from onnx_tf.backend import prepare
from basicsr.archs.rrdbnet_arch import RRDBNet
pth_path = "RESRGAN_x4plus_Main.pth"
Model = RRDBNet(num_in_ch=3, num_out_ch=3, scale=4, num_feat=64, num_block=23, num_grow_ch=32)
Model.load_state_dict(torch.load(pth_path)\["params_ema"\])
Model.eval()
X = torch.randn((1, 3, 64, 64))
torch.onnx.export(Model, X,
"Model.onnx",
export_params=True,
opset_version=11,
do_constant_folding=True,
input_names=\["input"\],
output_names=\["output"\])
and now I want to load this model into C++ and run it...
I have seen many examples of onnxruntime for loading models but I am very confused. sample codes are very complex and not for Super Resolution models.
I emphasize
I want to load the model directly in the C++ program (with onnx-runtime package)
I don't want to convert onnx model into another model format...
I have to load the .onnx model into c++ and pass the image into it and I expect to receive an Image from the model output. (model is a GAN)
Related
I am trying to store a trained Keras model within a Django model FileField. Since Keras is not pickleable, I used the .h5 file format (https://www.tensorflow.org/guide/keras/save_and_serialize) to store the keras model. So basically just:
kerasmodel.save('temp.h5')
dat = open('temp.h5', mode='r')
myFile = File(dat)
mymodel.content.save('test', myFile)
mymodel.content is the Django model FileField
This causes the error:
UnicodeDecodeError at /model/
'charmap' codec can't decode byte 0x90 in position 1928: character maps to "undefined"
I really don't care if Django can read this file or not, I just want to save it for later purposes. Since I can easily upload the file manually via the Django admin page to the model, it should be feasible.
Thanks in advance
I'm using django-imagekit to provide thumbnail versions of uploaded images on my Django models.
I would like to define various thumbnail ImageSpecFields in a mixin that my models can then inherit. However, each model currently has a different name for the ImageField on which its ImageSpecFields would be based.
I've tried this:
from django.db import models
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFit
class ThumbnailModelMixin(models.Model):
IMAGE_SPEC_SOURCE_FIELD = None
list_thumbnail = ImageSpecField(
source=IMAGE_SPEC_SOURCE_FIELD,
processors=[ResizeToFit((80, 160)],
format="JPEG",
options={"quality": 80}
)
class Meta:
abstract = True
class Book(ThumbnailModelMixin):
IMAGE_SPEC_SOURCE_FIELD = "cover"
cover = models.ImageField(upload_to="books/")
class Event(ThumbnailModelMixin):
IMAGE_SPEC_SOURCE_FIELD = "ticket"
ticket = models.ImageField(upload_to="events/")
But this fails on page load with:
AttributeError: 'Book' object has no attribute 'list_thumbnail'
Is there a way to get inheritance like this to work?
There are at least two other solutions:
Don't use a mixin/parent, and include the ImageSpecFields on each child class - a lot of duplicate code.
Change the Book.cover and Event.ticket fields to have the same name, like image and use "image" for the ImageSpecField source parameter.
The latter sounds best, but I'm still curious as to whether there's a way to get the inheritance working?
I am using a pre-trained model such as Alexnet, In this case also, ending up with the same error.
I was downloaded the alexnet_weights from here-->https://github.com/heuritech/convnets-keras
then I tried like this
from keras.models import load_model
base_model=load_model('alexnet_weights.h5')
I ended up with
ValueError: No model found in config file.
please help me to get rid out of it.
AlexNet is not a supported default model in Keras. Maybe you could try with VGG16 first:
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
model = VGG16(weights='imagenet', include_top=False)
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
Then you could move to using AlexNet, but you will need to build the model structure first and store that as "base_model" in your case. You only have the weight file I believe. Then you can load the weight file you have.
I have two apps:
trains provides the Train model, and
railroad the Rail model.
Since I totally love trains, I want to ensure that Train can only drive over the compatible Rail, therefore I implement a TrainRailCompatibility. However, I cannot modify neither Train nor Rail model to add a field ManyToMany(through=TrainRailCompatibility) (I am not a maintainer of those apps). I would still like to be able to do in my project something like my_train.compatible_rails.all() or filter(foo__trains__compatible_rails__in=[...]).
Is there a way / what would be the best way to achieve it?
You couls simply add this class to your desired App
class TrailRailCompatibility(models.Model):
train = models.ForeignKey('app1.Train')
rail = models.ForeignKey('app2.Rail')
and now the TrailRailCompatibility - class will be available on both of the referenced models via
from app1.models import Train
from app2.models import Rail
Trail.trailrailcompatibility_set.all()
Rail.trailrailcompatibility_set.all()
Looks like it is management issue because you can't do something in a proper way. Workaround is ugly and slower because it uses subquery instead of join.
class Compatibility(models.Model):
train = models.ForeignKey('trains.Train', related_name='compatibilities')
rail = models.ForeignKey('railroad.Rail', related_name='compatibilities')
class Meta:
unique_together = ('train', 'rail')
def get_trains(rails):
return Train.objects.filter(compatibilities__rail__in=rails)
def get_rails(train):
return Rail.objects.filter(compatibilities__train_id=train.id)
I don't think this can be done without adding the ManyToMany fields on the Train and Rail models. You could create a regular TrainRailCompatibility model with foreign keys to Train and Rail and set unique_together = ('train', 'rail') and then add entries manually. You just won't be able to access them from the Train and Rail models.
I'm using ImageMagick and the binding wand to generate thumbnails for uploaded images in Django. I can generate the thumbnail fine, but I'm uncertain about how to go about passing the image object from ImageMagick back into the Django model. So I have a simplified model as below:
from wand import Image
class Attachment(models.Model):
attachment = models.FileField(upload_to="some_path")
thumbnail = models.ImageField(upload_to="other_path")
def generate_thumb(self):
with Image(file=self.attachment) as wand:
thumb = wand.resize(width=50, height=50)
thumb.save(file=self.thumbnail)
This generates an error at the last line of ValueError: The 'thumbnail' attribute has no file associated with it. Is there a simple way to get a file object out of wand and into django without too much silliness?
Thanks.
Disclaimer: I am the creator of Wand you are trying to use.
First of all, ImageField requires PIL. It means you don’t need Wand, because you probably already installed an another image library. However I’ll answer to your question without any big changes.
It seems that self.thumbnail was not initialized yet at the moment, so you have to create a new File or ImageFile first:
import io
def generate_thumb(self):
buffer = io.BytesIO()
with Image(file=self.attachment) as wand:
wand.resize(width=50, height=50)
wand.save(file=buffer)
buffer.seek(0)
self.thumbnail = ImageFile(buffer)
Plus, from wand import Image will raise ImportError. It should be changed:
from wand.image import Image
If the goal is easy thumbnails in your django app try: https://github.com/sorl/sorl-thumbnail
Its pretty popular and active.