Comments (4)
So it turns out the method I've put in place for predictions is far from optimal.
Reorganising the directory will take some time but meanwhile, I wrote some code that should help you.
First, download the .pkl
model. I've included the link in the get_data_model.sh
script. As mentioned here, this is the correct way to use a model for inference.
In my testing, this worked with arrays that were shaped (height, width, channels)
; the size of the array doesn't matter (images don't need to be (375, 666)
.
from fastai.vision import *
learn = load_learner('~/shot-type-classifier/models', file='shot-type-classifier.pkl')
## Predict from an image on disk
img = '~/test.jpg'
learn.predict(open_image(img))
## Predict from a numpy array
# arr.shape --> (height, width, 3)
img = PIL.Image.fromarray(arr).convert('RGB')
img = pil2tensor(arr, np.float32).div_(255) # Convert to torch.Tensor
img = Image(img) # Convert to fastai.vision.image.Image
learn.predict(img)[0] # --> Shot Type
learn.predict(img)[2] # --> Probabilities
Further optimising would be to convert it directly to a float.Tensor
from a numpy.ndarray
but in my brief testing, that gave some strange errors that I haven't gotten around yet.
from shot-type-classifier.
The data object needs to be created only if you want to generate heatmaps. It's a hacky way of doing it, but it's the only way I could get it to work, for now.
What's your objective? Do you want to
- Get the model's predictions?
- Generate heatmaps?
If it's just getting predictions, then you should be able to do that from a video stream without creating an ImageDataBunch
. Take a look at the save_preds
function in the get-preds.py
file here.
Does that help?
This is a useful feature to add to the repo. I'll work on the code and push it soon.
Thanks for the help, the model looks to be amazing!
Thanks! Happy to help :)
from shot-type-classifier.
Thanks for the response. I only need 1], the model's predictions. I used get-preds.py
as a template for loading my own images. It seems to call initialise.py
which in turns creates a data = ImageDataBunch.from_folder
which prompted my question. What I need is a way to load the "learner"
learn = cnn_learner(data, models.resnet50, metrics = [accuracy], pretrained=True)
learn = learn.to_fp16()
learn.load(path/'models'/'shot-type-classifier');
without having to either create a data
element, or an empty one that only has the transforms. Right now, I'm dumping each image to a temporary file and then reading it back in with open_file
! This applies the transforms, but it's a huge waste of IO as I've already got the image loaded as a numpy array.
While you're here, it might be nice to note in the documentation what image format you're using:
1] width by height or height by width?
2] RGB or BGR?
from shot-type-classifier.
Thanks, your example helped a lot! I found you didn't need the line img = PIL.Image.fromarray(arr).convert('RGB')
.
from shot-type-classifier.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shot-type-classifier.