GithubHelp home page GithubHelp logo

Comments (4)

valeriosofi avatar valeriosofi commented on June 3, 2024

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?

from nebuly.

nemeziz69 avatar nemeziz69 commented on June 3, 2024

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)

from nebuly.

valeriosofi avatar valeriosofi commented on June 3, 2024

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")

from nebuly.

nemeziz69 avatar nemeziz69 commented on June 3, 2024

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.

from nebuly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.