alankbi / detecto

Build fully-functioning computer vision models with PyTorch
https://detecto.readthedocs.io/
MIT License
612 stars 107 forks source link

Can't do model.fit(dataset) #35

Closed karthick965938 closed 4 years ago

karthick965938 commented 4 years ago

Actually I got the following errors when I was trying this code on Google Colab.

from detecto import core, utils, visualize

dataset = core.Dataset('images/')
model = core.Model(['dog', 'cat'])

model.fit(dataset)

This is the error I got in the Google Colab terminal. Please help me, anyone

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. 
  warnings.warn("The default behavior for interpolate/upsample with float scale_factor will change "
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-9-11e9a74e8844> in <module>()
      4 model = core.Model(['dog', 'cat'])
      5 
----> 6 model.fit(dataset)

/usr/local/lib/python3.6/dist-packages/detecto/core.py in fit(self, dataset, val_dataset, epochs, learning_rate, momentum, weight_decay, gamma, lr_step_size, verbose)
    467             # Training step
    468             self._model.train()
--> 469             for images, targets in dataset:
    470                 self._convert_to_int_labels(targets)
    471                 images, targets = self._to_device(images, targets)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    343 
    344     def __next__(self):
--> 345         data = self._next_data()
    346         self._num_yielded += 1
    347         if self._dataset_kind == _DatasetKind.Iterable and \

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
    383     def _next_data(self):
    384         index = self._next_index()  # may raise StopIteration
--> 385         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    386         if self._pin_memory:
    387             data = _utils.pin_memory.pin_memory(data)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.6/dist-packages/detecto/core.py in __getitem__(self, idx)
    196                         box[0, 0], box[0, 2] = box[0, (2, 0)]
    197                 else:
--> 198                     image = t(image)
    199 
    200             # Scale down box if necessary

/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py in __call__(self, tensor)
    164             Tensor: Normalized Tensor image.
    165         """
--> 166         return F.normalize(tensor, self.mean, self.std, self.inplace)
    167 
    168     def __repr__(self):

/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)
    206     if std.ndim == 1:
    207         std = std[:, None, None]
--> 208     tensor.sub_(mean).div_(std)
    209     return tensor
    210 

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0
alankbi commented 4 years ago

The error seems to be that the PNG images have 4 channels instead of 3, i.e. RGBA vs. RGB. You’ll want to convert your images from RGBA to RGB, which you can do by applying a custom transform when creating your Dataset. For example:

from detecto import core, utils
from torchvision import transforms

augmentations = transforms.Compose([
    transforms.Lambda(lambda x: x[:,:,:3]),
    transforms.ToTensor(),
    utils.normalize_transform(),
])

dataset = core.Dataset('images/', transform=augmentations)

Here, the transforms.Lambda(lambda x: x[:,:,:3]) takes in an image and returns only the first three channels (RGB), which should allow you to train your model as normal:

model = core.Model(['dog', 'cat'])
model.fit(dataset)
karthick965938 commented 4 years ago

Thanks for your response @alankbi After adding your suggestion I am getting the following errors. Can you clearly explain?

My code:

from detecto import core, utils
from torchvision import transforms

augmentations = transforms.Compose([
    transforms.Lambda(lambda x: x[:,:,:3]),
    transforms.ToTensor(),
    utils.normalize_transform(),
])

dataset = core.Dataset('images/', transform=augmentations)
model = core.Model(['dog', 'cat'])

model.fit(dataset)

Error:


/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. 
  warnings.warn("The default behavior for interpolate/upsample with float scale_factor will change "
/pytorch/torch/csrc/utils/python_arg_parser.cpp:756: UserWarning: This overload of nonzero is deprecated:
    nonzero(Tensor input, *, Tensor out)
Consider using one of the following signatures instead:
    nonzero(Tensor input, *, bool as_tuple)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6a1e0406f5cc> in <module>()
     11 model = core.Model(['dog', 'cat'])
     12 
---> 13 model.fit(dataset)

/usr/local/lib/python3.6/dist-packages/detecto/core.py in fit(self, dataset, val_dataset, epochs, learning_rate, momentum, weight_decay, gamma, lr_step_size, verbose)
    467             # Training step
    468             self._model.train()
--> 469             for images, targets in dataset:
    470                 self._convert_to_int_labels(targets)
    471                 images, targets = self._to_device(images, targets)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    343 
    344     def __next__(self):
--> 345         data = self._next_data()
    346         self._num_yielded += 1
    347         if self._dataset_kind == _DatasetKind.Iterable and \

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
    383     def _next_data(self):
    384         index = self._next_index()  # may raise StopIteration
--> 385         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    386         if self._pin_memory:
    387             data = _utils.pin_memory.pin_memory(data)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.6/dist-packages/detecto/core.py in __getitem__(self, idx)
    148         # Read in the image from the file name in the 0th column
    149         img_name = os.path.join(self._root_dir, self._csv.iloc[idx, 0])
--> 150         image = io.imread(img_name)
    151 
    152         # Read in xmin, ymin, xmax, and ymax

/usr/local/lib/python3.6/dist-packages/skimage/io/_io.py in imread(fname, as_gray, plugin, **plugin_args)
     46 
     47     with file_or_url_context(fname) as fname:
---> 48         img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
     49 
     50     if not hasattr(img, 'ndim'):

/usr/local/lib/python3.6/dist-packages/skimage/io/manage_plugins.py in call_plugin(kind, *args, **kwargs)
    208                                (plugin, kind))
    209 
--> 210     return func(*args, **kwargs)
    211 
    212 

/usr/local/lib/python3.6/dist-packages/skimage/io/_plugins/imageio_plugin.py in imread(*args, **kwargs)
      8 @wraps(imageio_imread)
      9 def imread(*args, **kwargs):
---> 10     return np.asarray(imageio_imread(*args, **kwargs))

/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py in imread(uri, format, **kwargs)
    219 
    220     # Get reader and read first
--> 221     reader = read(uri, format, "i", **kwargs)
    222     with reader:
    223         return reader.get_data(0)

/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py in get_reader(uri, format, mode, **kwargs)
    137     if format is None:
    138         raise ValueError(
--> 139             "Could not find a format to read the specified file " "in mode %r" % mode
    140         )
    141 

ValueError: Could not find a format to read the specified file in mode 'i'
alankbi commented 4 years ago

Looks like some kind of issue with reading in your images. Try manually opening or reading your images using utils.read_image to see if the error is with your images. If not, could you share with me what your directory structure and XML files look like?

karthick965938 commented 4 years ago

Hi Alan

I don't know how to validate my images using utils.read_image. Can you explain the little details? I am a beginner one so I can't understand some basic things. And I have attached my images folder. Please check and let me know. new.zip https://drive.google.com/file/d/1FIZ-hTuby-0lGrkvqKO88ChmKU-J0REZ/view?usp=drive_web

On Fri, May 8, 2020 at 8:35 AM Alan Bi notifications@github.com wrote:

Looks like some kind of issue with reading in your images. Try manually opening or reading your images using utils.read_image to see if the error is with your images. If not, could you share with me what your directory structure and XML files look like?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/alankbi/detecto/issues/35#issuecomment-625604567, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD2U5D5TIPNNLX2U5O2O3QDRQNZGNANCNFSM4M2WGK4A .

-- Regards, Karthick N Ruby on Rails Developer & Web Designer. Skype : karthick_rc7 Contact : +91 9659383045 Email : karthick965938@gmail.com

alankbi commented 4 years ago

Basically, just try to run the following code to see if any similar errors come up when reading in and plotting your images:

from detecto import core, utils
import matplotlib.pyplot as plt

image = utils.read_image('images/cat0.jpg')
plt.imshow(image)
plt.show()

If that doesn't have any errors, you can also try seeing if the issue is with the dataset:

from torchvision import transforms

augmentations = transforms.Compose([
    transforms.Lambda(lambda x: x[:,:,:3]),
    transforms.ToTensor(),
    utils.normalize_transform(),
])

dataset = core.Dataset('images/', transform=augmentations)

print(dataset[0])
karthick965938 commented 4 years ago

Thanks for the help, Alan. Please check the attachment.

1) How I can know the dataset created successfully? 2) We can skip the user warning, right? because I can able to detect the cat images 3) Can we able to do the live detection? I mean can we use a webcam or a camera? can you share the code related to connect the camera? [image: resistor_ipynb_Colaboratory (1).png] [image: cat_dog_ipynb_Colaboratory.png]

On Sat, May 9, 2020 at 6:30 AM Alan Bi notifications@github.com wrote:

Basically, just try to run the following code to see if any similar errors come up when reading in and plotting your images:

from detecto import core, utilsimport matplotlib.pyplot as plt

image = utils.read_image('images/cat0.jpg') plt.imshow(image) plt.show()

If that doesn't have any errors, you can also try seeing if the issue is with the dataset:

from torchvision import transforms

augmentations = transforms.Compose([ transforms.Lambda(lambda x: x[:,:,:3]), transforms.ToTensor(), utils.normalize_transform(), ])

dataset = core.Dataset('images/', transform=augmentations) print(dataset[0])

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/alankbi/detecto/issues/35#issuecomment-626079394, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD2U5D3LHBFSORU2M4KXWILRQSTJVANCNFSM4M2WGK4A .

-- Regards, Karthick N Ruby on Rails Developer & Web Designer. Skype : karthick_rc7 Contact : +91 9659383045 Email : karthick965938@gmail.com

karthick965938 commented 4 years ago

Hi Alan,

Please check my last email and let me know ASAP.

On Sat, May 9, 2020 at 3:37 PM Karthick Nagarajan karthick965938@gmail.com wrote:

Thanks for the help, Alan. Please check the attachment.

1) How I can know the dataset created successfully? 2) We can skip the user warning, right? because I can able to detect the cat images 3) Can we able to do the live detection? I mean can we use a webcam or a camera? can you share the code related to connect the camera? [image: resistor_ipynb_Colaboratory (1).png] [image: cat_dog_ipynb_Colaboratory.png]

On Sat, May 9, 2020 at 6:30 AM Alan Bi notifications@github.com wrote:

Basically, just try to run the following code to see if any similar errors come up when reading in and plotting your images:

from detecto import core, utilsimport matplotlib.pyplot as plt

image = utils.read_image('images/cat0.jpg') plt.imshow(image) plt.show()

If that doesn't have any errors, you can also try seeing if the issue is with the dataset:

from torchvision import transforms

augmentations = transforms.Compose([ transforms.Lambda(lambda x: x[:,:,:3]), transforms.ToTensor(), utils.normalize_transform(), ])

dataset = core.Dataset('images/', transform=augmentations) print(dataset[0])

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/alankbi/detecto/issues/35#issuecomment-626079394, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD2U5D3LHBFSORU2M4KXWILRQSTJVANCNFSM4M2WGK4A .

-- Regards, Karthick N Ruby on Rails Developer & Web Designer. Skype : karthick_rc7 Contact : +91 9659383045 Email : karthick965938@gmail.com

-- Regards, Karthick N Ruby on Rails Developer & Web Designer. Skype : karthick_rc7 Contact : +91 9659383045 Email : karthick965938@gmail.com

alankbi commented 4 years ago

I can't seem to open your attachments, but here are the answers to your questions:

  1. If you run the code I shared above and it works without any errors (and you can successfully print dataset[0] then your dataset should be valid.
  2. Yes, that shouldn't be an issue.
  3. Yes, there is the visualize.detect_live function you can use. However, note that it doesn't work on VMs like Google Colaboratory.
karthick965938 commented 4 years ago

@alankbi I closed this issue. You have clarified over our email conversation and here also. Thanks

HuynID commented 3 years ago

I got error too

KeyError Traceback (most recent call last)

in () ----> 1 losses = model.fit(dataset, epochs=30, learning_rate=0.001) 2 frames /usr/local/lib/python3.7/dist-packages/detecto/core.py in fit(self, dataset, val_dataset, epochs, learning_rate, momentum, weight_decay, gamma, lr_step_size, verbose) 498 iterable = tqdm(dataset, position=0, leave=True) if verbose else dataset 499 for images, targets in iterable: --> 500 self._convert_to_int_labels(targets) 501 images, targets = self._to_device(images, targets) 502 /usr/local/lib/python3.7/dist-packages/detecto/core.py in _convert_to_int_labels(self, targets) 608 labels_array = target['labels'] 609 # convert string labels into one hot encoding --> 610 labels_int_array = [self._int_mapping[class_name] for class_name in labels_array] 611 target['labels'] = torch.tensor(labels_int_array) 612 /usr/local/lib/python3.7/dist-packages/detecto/core.py in (.0) 608 labels_array = target['labels'] 609 # convert string labels into one hot encoding --> 610 labels_int_array = [self._int_mapping[class_name] for class_name in labels_array] 611 target['labels'] = torch.tensor(labels_int_array) 612 KeyError: 'cavity'
HuynID commented 3 years ago

Can you help me? pls

alankbi commented 3 years ago

I don't think your error is related to this issue? It seems like you're missing the 'cavity' label when initializing your Model object. Could you double-check that the list of classes you pass in when calling model = Model(...) contains every label found in the XML files?

HuynID commented 3 years ago

I don't think your error is related to this issue? It seems like you're missing the 'cavity' label when initializing your Model object. Could you double-check that the list of classes you pass in when calling model = Model(...) contains every label found in the XML files?

Thank you so much, i found out problem, Can I contact you? you have facebook or twitter?

alankbi commented 3 years ago

Awesome! As for contact info, you should be able to find it in the README - that has a link to my email and Twitter.

HuynID commented 3 years ago

Awesome! As for contact info, you should be able to find it in the README - that has a link to my email and Twitter.

Okay!! Hi, I have a question, I have a model training OCR python, how can I get it to the app built in Android studio?

alankbi commented 3 years ago

Unfortunately that's not supported via Detecto. I'd recommend looking into the PyTorch docs or something similar to maybe get some insight into how to export a PyTorch model to be used on various platforms such as Android.