Giter Site home page Giter Site logo

curiousily / getting-things-done-with-pytorch Goto Github PK

View Code? Open in Web Editor NEW
2.2K 2.2K 610.0 33.1 MB

Jupyter Notebook tutorials on solving real-world problems with Machine Learning & Deep Learning using PyTorch. Topics: Face detection with Detectron 2, Time Series anomaly detection with LSTM Autoencoders, Object Detection with YOLO v5, Build your first Neural Network, Time Series forecasting for Coronavirus daily cases, Sentiment Analysis with BERT.

Home Page: https://www.mlexpert.io/

License: Apache License 2.0

Jupyter Notebook 100.00%
anomaly-detection bert computer-vision coronavirus deep-learning face-detection face-recognition lstm machine-learning nlp object-detection pytorch sentiment-analysis time-series time-series-anomaly-detection time-series-forecasting transfer-learning transformer tutorial yolo

getting-things-done-with-pytorch's Introduction

Get SH*T Done with PyTorch

Learn how to solve real-world problems with Deep Learning models (NLP, Computer Vision, and Time Series). Go from prototyping to deployment with PyTorch and Python!

Open In Colab

Read the book here

๐Ÿ“– Read for FREE

The whole book can be read using the links below. Each part contains a notebook that you can find in this repository.

Consider buying the book if you want to support my work. Thanks for stopping by! ๐Ÿค—

getting-things-done-with-pytorch's People

Contributors

curiousily avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

getting-things-done-with-pytorch's Issues

How to Export 'ONNX' Model?

Hi. Thanks to this code, I was able to write a multi-label classification model well.
By the way, can you tell me how to export the model made like this using torch.onnx? An error occurred when I used the normal torch.onnx.export method.

My code :

test_comment="hello"  
encoding = tokenizer.encode_plus(
    test_comment,
    add_special_tokens=True,
    max_length=63,
    return_token_type_ids=False,
    padding="max_length",
    return_attention_mask=True,
    return_tensors='pt',
  )



torch.onnx.export(trained_model,
                    (encoding["input_ids"], encoding["attention_mask"]),
                    'model.onnx',
                    export_params=True,
                    do_constant_folding=True,
                    opset_version=11,
                    input_names=['input_ids', 'attention_mask'],
                    output_names=['output'],
)

error :


RuntimeError                              Traceback (most recent call last)
<ipython-input-49-9c2e1e064898> in <module>
----> 1 torch.onnx.export(trained_model,
      2                     (encoding["input_ids"], encoding["attention_mask"]),
      3                     'model.onnx',
      4                     export_params=True,
      5                     do_constant_folding=True,

~/anaconda3/envs/myenv1/lib/python3.8/site-packages/torch/onnx/__init__.py in export(model, args, f, export_params, verbose, training, input_names, output_names, aten, export_raw_ir, operator_export_type, opset_version, _retain_param_name, do_constant_folding, example_outputs, strip_doc_string, dynamic_axes, keep_initializers_as_inputs, custom_opsets, enable_onnx_checker, use_external_data_format)
    273 
    274     from torch.onnx import utils
--> 275     return utils.export(model, args, f, export_params, verbose, training,
    276                         input_names, output_names, aten, export_raw_ir,
    277                         operator_export_type, opset_version, _retain_param_name,

~/anaconda3/envs/myenv1/lib/python3.8/site-packages/torch/onnx/utils.py in export(model, args, f, export_params, verbose, training, input_names, output_names, aten, export_raw_ir, operator_export_type, opset_version, _retain_param_name, do_constant_folding, example_outputs, strip_doc_string, dynamic_axes, keep_initializers_as_inputs, custom_opsets, enable_onnx_checker, use_external_data_format)
     86         else:
     87             operator_export_type = OperatorExportTypes.ONNX
---> 88     _export(model, args, f, export_params, verbose, training, input_names, output_names,
     89             operator_export_type=operator_export_type, opset_version=opset_version,
     90             _retain_param_name=_retain_param_name, do_constant_folding=do_constant_folding,

~/anaconda3/envs/myenv1/lib/python3.8/site-packages/torch/onnx/utils.py in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, example_outputs, opset_version, _retain_param_name, do_constant_folding, strip_doc_string, dynamic_axes, keep_initializers_as_inputs, fixed_batch_size, custom_opsets, add_node_names, enable_onnx_checker, use_external_data_format, onnx_shape_inference)
    687 
...
    128             wrapper,
    129             in_vars + module_state,

RuntimeError: output 1 (0
[ CPULongType{} ]) of traced region did not have observable data dependence with trace inputs; this probably indicates your program cannot be understood by the tracer.

Thank you in advance for your reply.

How to handle imbalanced classes ?

Hi, I have a use case wherein there are two classes and are hugely imbalanced. How can i fix this issue.

I have used the source code.

Sentiment Analysis with BERT

How to display class name of detected object

Regarding the tutorial you made on face detection using Detectron2, i want to output the bounding box on detected objects with the class name, instead of displaying the scores only. I would be glad if you could explain how to achieve that.

04.first-neural-network.ipynb error

I am getting the following error for 04.first-neural-network.ipynb notebook:

--------------------------------------------------
RuntimeError     Traceback (most recent call last)
<ipython-input-28-8756d5fbee9e> in <module>
     10 
     11     if epoch % 100 == 0:
---> 12       train_acc = calculate_accuracy(y_train, y_pred)
     13 
     14       y_test_pred = net(X_test)

<ipython-input-24-51ba3ab94870> in calculate_accuracy(y_true, y_pred)
      1 def calculate_accuracy(y_true, y_pred):
      2   predicted = y_pred.ge(.5).view(-1)
----> 3   return (y_true == predicted).sum().float() / len(y_true)

RuntimeError: Expected object of scalar type Float but got scalar type Byte for argument #2 'other'

Problem printing the shape of last_hidden_state and pooled_output

Change this line -> bert_model = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME) to
bert_model = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME, return_dict=False)

as a result the dict will not return str rather will return tensor.

It will help you with other parts of code as well.

Multi class time series classification

Hello,

Thanks for the good examples in the repo.
I need to classify some time series (similar to what you did in script 06-ecg classification) but I have more than just 2 classes.
I can't seem to make it work. Do you have any input on how it should be done?

Thanks!

AttributeError: 'dict' object has no attribute 'dataset'

I am getting this error while writing the following class code. The error message is attached below the code.

class GPReviewDataset(data.dataset):

def init(self, review, target, tokenizer, max_len):
self.review = review
self.target = target
self.tokenizer = tokenizer
self.max_len = max_len

def len(self):
# return the number of reviews we have
return len(self.reviews)

def getitem(self, item): # takes the index and the reviews
review = str(self.reviews[item])

encoding = self.tokenizer.encode_plus(
  review,
  add_special_tokens=True,
  max_length=self.max_len,
  return_token_type_ids=False,
  pad_to_max_length=True,
  return_attention_mask=True,
  return_tensors='pt'
)

return {
  'review_text': review,
  'input_ids': encoding['input_ids'].flatten(),
  'attention_mask': encoding['attention_mask'].flatten(),
  'targets': torch.tensor(self.target, dtype=torch.long)
}

image

LSTM autoencoder vs ANN autoencoder

Hi,
This question is related to notebook 6 on ECG anomaly detection.

I would like to know if there is any study on how much is the accuracy improvement on using LSTM autoencoder versus ANN encoder. Let me know if you have any info.

Regards, Debapriya

Slightly different kind of labels for the input ( Multi-label Text Classification with BERT and PyTorch Lightning)

Great work you did in the Multi-label Text Classification! Thanks!!
I have a similar problem as you except that II only have one column for the labels (for example, you have [1,0,0,0,0,0] as label of the toxic classe but with my data I have only one value as the number of the class. For example with the first class I have 1 in the column and the number goes to 10). This causes a problem in the training (trainer.fit), the code keep telling me that the target size (torch.Size([16]) which is the epoch number) is different from the input size (which is torch.Size([16, 10]) and the 10 here is the number of the classes) Can you please tell me where can I make the changes so the code will run?

Error: ModelCheckpoint(monitor='val_loss') not found in the returned metrics

Hi,
When running the notebook 11.multi-label-text-classification-with-bert.ipynb
I encounter the following error...
pytorch_lightning.utilities.exceptions.MisconfigurationException: ModelCheckpoint(monitor='val_loss') not found in the returned metrics: ['train_loss']. HINT: Did you call self.log('val_loss', tensor) in the LightningModule?

Do you know how I could fix this?

8. sentiment-analysis-with-bert

Hi there,
I was following along with your video guide on this project but with my own dataset.
As I started training for my data I ran into an error.

RuntimeError : stack expects each tensor to be equal size

Our codes are basically identical and the structure of data seems to be identical as well. I'm not sure what's causing this issue or how to resolve it.

More in-depth error:

RuntimeError                              Traceback (most recent call last)
<timed exec> in <module>

<ipython-input-26-8ba1e19dd195> in train_epoch(model, data_loader, loss_fn, optimizer, device, scheduler, n_examples)
      4     correct_predictions = 0
      5 
----> 6     for i in data_loader:
      7         input_ids = i['input_ids'].to(device)
      8         attention_mask = i['attention_mask'].to(device)

~\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
    361 
    362     def __next__(self):
--> 363         data = self._next_data()
    364         self._num_yielded += 1
    365         if self._dataset_kind == _DatasetKind.Iterable and \

~\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
    401     def _next_data(self):
    402         index = self._next_index()  # may raise StopIteration
--> 403         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    404         if self._pin_memory:
    405             data = _utils.pin_memory.pin_memory(data)

~\Anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
     45         else:
     46             data = self.dataset[possibly_batched_index]
---> 47         return self.collate_fn(data)

~\Anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py in default_collate(batch)
     72         return batch
     73     elif isinstance(elem, container_abcs.Mapping):
---> 74         return {key: default_collate([d[key] for d in batch]) for key in elem}
     75     elif isinstance(elem, tuple) and hasattr(elem, '_fields'):  # namedtuple
     76         return elem_type(*(default_collate(samples) for samples in zip(*batch)))

~\Anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py in <dictcomp>(.0)
     72         return batch
     73     elif isinstance(elem, container_abcs.Mapping):
---> 74         return {key: default_collate([d[key] for d in batch]) for key in elem}
     75     elif isinstance(elem, tuple) and hasattr(elem, '_fields'):  # namedtuple
     76         return elem_type(*(default_collate(samples) for samples in zip(*batch)))

~\Anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py in default_collate(batch)
     53             storage = elem.storage()._new_shared(numel)
     54             out = elem.new(storage)
---> 55         return torch.stack(batch, 0, out=out)
     56     elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
     57             and elem_type.__name__ != 'string_':

RuntimeError: stack expects each tensor to be equal size, but got [160] at entry 0 and [161] at entry 5

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.