Flip a Tensor #100
Comments
Hi @nh9k, thanks for reporting the need for this operation! It's correct, there is no implementation for In the meantime, if an operation is import torch
import torch.nn as nn
class ModelWrapper(nn.Module):
def __init__(self, model):
super().__init__()
self.model = model
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = x.flip([0, 1])
return self.model(x)
wrapped_model = ModelWrapper(model) And then export the model for the lite interpreter runtime as usual from torch.utils.mobile_optimizer import optimize_for_mobile
scripted_model = torch.jit.script(wrapped_model)
optimized_model = optimize_for_mobile(scripted_model)
optimized_model._save_for_lite_interpreter("wrapped_model.ptl") Hope that helps! |
@nh9k, we merged #101 and it is available through the Are you using the PlayTorch app and Expo Snack or are you developing in a local React Native app? Note: if you are using the PlayTorch app, then |
@nh9k, there is another option via an exported model to polyfill the The example Snack demonstrates the option: https://playtorch.dev/snack/@raedle/flip-polyfill Basically, it wraps the import torch
import torch.nn as nn
from typing import List
from torch.utils.mobile_optimizer import optimize_for_mobile
class Flip(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x: torch.Tensor, dims: List[int]) -> torch.Tensor:
return torch.flip(x, dims)
flip = Flip()
scripted_model = torch.jit.script(flip)
optimized_model = optimize_for_mobile(scripted_model)
optimized_model._save_for_lite_interpreter("flip.ptl") The exported const filePath = await MobileModel.download(require('./flip.ptl'));
const flipModule = await torch.jit._loadForMobile(filePath); In React Native, the flip operation is loaded via the const flip = usePolyfillFlip(); Then, let tensor = torch.arange(8).reshape([2, 2, 2]);
const flippedTensor = flip(tensor, [1, 0]); Hope that unblocks you until PlayTorch comes natively with |
Hi! @raedle, Thank you so much your kindness. I am developing in a local React Native app, and your idea which is building flip operation into the model is very good.
I hope that the selected method will be efficient. |
@nh9k, do I understand correctly that Option 1 and 2 sound reasonable if you have control over the model or you can upgrade You can add the latest nightly for your local React Native app with the following command: cd <YOUR_LOCAL_REACT_NATIVE_APP_DIRECTORY>
yarn add react-native-pytorch-core@nightly After executing the command, you should be able to use flip as follows: Example: let tensor = torch.arange(8).reshape([2, 2, 2]);
const flippedTensor = flip(tensor, [1, 0]); Depending on the image format and the flip axis, you may want to choose the width or height axis to flip. Please let us know if this is successful, and also please do share what you built with PlayTorch, if you can 😉! |
@raedle, Thank you so much 😄 |
Hi @raedle, I solved the problem!
Thank you :)
|
Fantastic to hear, @nh9k! I'll close the issue, then |
Area Select
react-native-pytorch-core (core package)
Description
Hi, thanks for contributions.
How can i flip a tensor like torch.flip?
There is no implementation of flip function in Playtorch docs
Another solution i thought is to make a new tensor and assign reversed values using
for loop
such as thetensor[::, ::-1]
.However, i got a result
Cannot assign to property ~
My code and the corresponding error are as follows.
CODE: e.g., (.js file)
ERROR: e.g.,
How can i solve this problem? Many thanks for your help :)
The text was updated successfully, but these errors were encountered: