Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Speedster] Optimization failed with PytorchBackendCompiler #341

Open
nemeziz69 opened this issue May 12, 2023 · 4 comments
Open

[Speedster] Optimization failed with PytorchBackendCompiler #341

nemeziz69 opened this issue May 12, 2023 · 4 comments

Comments

@nemeziz69
Copy link

May I know what is the reason behind the warning message?

FYI, my input data is Pytorch Dataloader, which was constructed in tensor.

2023-05-12 16:16:31 | INFO | [1/2] Running PyTorch Optimization Pipeline
2023-05-12 16:16:31 | INFO | Optimizing with PytorchBackendCompiler and q_type: None.
2023-05-12 16:16:33 | WARNING | Optimization failed with DeepLearningFramework.PYTORCH interface of ModelCompiler.TORCHSCRIPT. Got error 'list' object has no attribute 'to'. If possible the compilation will be re-scheduled with another interface. Please consult the documentation for further info or open an issue on GitHub for receiving assistance.
2023-05-12 16:16:33 | INFO | Optimizing with PytorchBackendCompiler and q_type: QuantizationType.HALF.
2023-05-12 16:16:35 | WARNING | Optimization failed with DeepLearningFramework.PYTORCH interface of ModelCompiler.TORCHSCRIPT. Got error 'list' object has no attribute 'to'. If possible the compilation will be re-scheduled with another interface. Please consult the documentation for further info or open an issue on GitHub for receiving assistance.

@valeriosofi
Copy link
Collaborator

Hello @nemeziz69, thank you for your interest in speedster! Which data loader are you using? If it's a custom one, can you provide the implementation of the __getitem__() method so that I can check?

@nemeziz69
Copy link
Author

Hello @nemeziz69, thank you for your interest in speedster! Which data loader are you using? If it's a custom one, can you provide the implementation of the __getitem__() method so that I can check?

Hi @valeriosofi ,

DataLoader is not custom, it's directly from torch.

from torch.utils.data import DataLoader, Dataset

input_data = DataLoader(dataset, batch_size=1, num_workers=1, pin_memory=True)

However, my dataset is custom. Here the full implementation:

import cv2
from tqdm import tqdm
from torch.utils.data import DataLoader, Dataset

class SampleDataset(Dataset):
    def __init__(self, image_tensor_array):
        self.data = torch.as_tensor(image_tensor_array)

    def __getitem__(self, index):
        x = self.data[index]
        return x

    def __len__(self):
        return len(self.data)


def convert_dataset_to_crop_and_tensor(path, crop_x=None, crop_y=None, crop_h=None):
    # Unpack parameter
    if crop_x is not None and crop_y is not None and crop_h is not None:
        crop_image = True
    else:
        crop_image = False

    image_files = glob(f"{path}/*/*.jpg")
    img_tensor_plist = []
    for img in tqdm(image_files, desc="Converting"):
        each_img = cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB)

        if crop_image:
            crop1 = each_img[crop_y:crop_y + crop_h, crop_x:crop_x + crop_h]
            crop2 = each_img[crop_y:crop_y + crop_h, crop_x + crop_h:crop_x + crop_h * 2]
            crop3 = each_img[crop_y:crop_y + crop_h, crop_x + crop_h * 2:crop_x + crop_h * 3]
            img_list = [crop1, crop2, crop3]
        else:
            img_list = [each_img]

        for imgcc in img_list:
            convert_tensor = transforms.ToTensor()
            image_tensor = convert_tensor(imgcc)
            rescale_img = transforms.Resize(size=(608, 608))
            resized_img = rescale_img(image_tensor)
            img_tensor_plist.append(resized_img.numpy())

    img_tensor_arr = torch.as_tensor(img_tensor_plist)

    return img_tensor_arr


if __name__ == "__main__":
    img_tensor_array = convert_dataset_to_crop_and_tensor(SAMPLE_DATASET_PATH, crop_x=0, crop_y=100, crop_h=1365)
    dataset = SampleDataset(img_tensor_array)
    input_data = DataLoader(dataset, batch_size=1, num_workers=1, pin_memory=True)

@nemeziz69 nemeziz69 changed the title Optimization failed with PytorchBackendCompiler [Speedster] Optimization failed with PytorchBackendCompiler May 15, 2023
@valeriosofi
Copy link
Collaborator

Hi @nemeziz69, I tried with your code and unfortunately I can't reproduce your error. I've performed a few minor changes to run the code on my local env, at the end the dataloader outputs tensors of shape torch.Size([1, 3, 608, 608])and speedster correctly processes the dataloader, can you check that you are passeng the dataloader correctly to speedster? Please also check that you are using the latest version, I'm using speedster==0.3.0.

from glob import glob

import cv2
import torchvision.models as models
from speedster import optimize_model, save_model
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
from tqdm import tqdm


class SampleDataset(Dataset):
    def __init__(self, image_tensor_array):
        self.data = image_tensor_array

    def __getitem__(self, index):
        x = self.data[index]
        return x

    def __len__(self):
        return len(self.data)


def convert_dataset_to_crop_and_tensor(path, crop_x=None, crop_y=None, crop_h=None):
    # Unpack parameter
    if crop_x is not None and crop_y is not None and crop_h is not None:
        crop_image = True
    else:
        crop_image = False

    image_files = glob(f"{path}/*.png")
    img_tensor_plist = []
    for img in tqdm(image_files, desc="Converting"):
        each_img = cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB)

        # Resize image to 4000x4000 to make sure that my images are not too small for the crop part
        rescale_img = cv2.resize(each_img, (4000, 4000))

        if crop_image:
            crop1 = rescale_img[crop_y:crop_y + crop_h, crop_x:crop_x + crop_h]
            crop2 = rescale_img[crop_y:crop_y + crop_h, crop_x + crop_h:crop_x + crop_h * 2]
            crop3 = rescale_img[crop_y:crop_y + crop_h, crop_x + crop_h * 2:crop_x + crop_h * 3]
            img_list = [crop1, crop2, crop3]
        else:
            img_list = [rescale_img]

        for imgcc in img_list:
            convert_tensor = transforms.ToTensor()
            image_tensor = convert_tensor(imgcc)
            rescale_img = transforms.Resize(size=(608, 608))
            image_tensor = rescale_img(image_tensor)
            img_tensor_plist.append(image_tensor)

    return img_tensor_plist


if __name__ == "__main__":
    img_tensor_array = convert_dataset_to_crop_and_tensor("images", crop_x=0, crop_y=100, crop_h=1365)
    dataset = SampleDataset(img_tensor_array)
    input_data = DataLoader(dataset, batch_size=1, num_workers=1, pin_memory=True)

    # 1 Provide input model and data (we support PyTorch Dataloaders and custom input, see the docs to learn more)
    model = models.resnet50()

    # 2 Run Speedster optimization
    optimized_model = optimize_model(
        model,
        input_data=input_data,
        optimization_time="constrained",
        metric_drop_ths=0.05
    )

    # 3 Save the optimized model
    save_model(optimized_model, "model_save_path")

@nemeziz69
Copy link
Author

Hi @valeriosofi, I'm confirm my speedster version is speedster==0.3.0, and my dataloaders output shape is torch.Size([1, 3, 608, 608]).

However, when I used the same model like yours which is models.resnet50(), the PytorchBackendCompiler run perfectly without any WARNING message.

So, I assume there's something wrong in my model input. FYI, I used custom pretrained model which is training output from this blog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants