From 74e536411c4c4d94ba30b93f7072196437b2c3b0 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Wed, 6 Oct 2021 20:25:11 +0530 Subject: [PATCH 01/53] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 7dd5272956ae1..0b0cb7c5e2550 100644 --- a/README.md +++ b/README.md @@ -391,6 +391,10 @@ The lightning community is maintained by - [10+ core contributors](https://pytorch-lightning.readthedocs.io/en/latest/governance.html) who are all a mix of professional engineers, Research Scientists, and Ph.D. students from top AI labs. - 480+ active community contributors. +(You can create new features in PyTorch lightning.)[https://github.com/PyTorchLightning/pytorch-lightning/projects] + +(You can solve issues in our github issues tab.)[https://github.com/PyTorchLightning/pytorch-lightning/issues] + Want to help us build Lightning and reduce boilerplate for thousands of researchers? [Learn how to make your first contribution here](https://devblog.pytorchlightning.ai/quick-contribution-guide-86d977171b3a) Lightning is also part of the [PyTorch ecosystem](https://pytorch.org/ecosystem/) which requires projects to have solid testing, documentation and support. From e0af7c26380f8a08eb7f1b01c8ebad8704803632 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Wed, 6 Oct 2021 20:26:42 +0530 Subject: [PATCH 02/53] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0b0cb7c5e2550..83b126a9e626f 100644 --- a/README.md +++ b/README.md @@ -391,9 +391,9 @@ The lightning community is maintained by - [10+ core contributors](https://pytorch-lightning.readthedocs.io/en/latest/governance.html) who are all a mix of professional engineers, Research Scientists, and Ph.D. students from top AI labs. - 480+ active community contributors. -(You can create new features in PyTorch lightning.)[https://github.com/PyTorchLightning/pytorch-lightning/projects] +[You can create new features in PyTorch lightning.](https://github.com/PyTorchLightning/pytorch-lightning/projects) -(You can solve issues in our github issues tab.)[https://github.com/PyTorchLightning/pytorch-lightning/issues] +[You can solve issues in our github issues tab.](https://github.com/PyTorchLightning/pytorch-lightning/issues) Want to help us build Lightning and reduce boilerplate for thousands of researchers? [Learn how to make your first contribution here](https://devblog.pytorchlightning.ai/quick-contribution-guide-86d977171b3a) From b2a137254c7d601573fd12b9e1bac84873ebadf5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Oct 2021 15:00:02 +0000 Subject: [PATCH 03/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 83b126a9e626f..a07b81480d624 100644 --- a/README.md +++ b/README.md @@ -392,9 +392,9 @@ The lightning community is maintained by - 480+ active community contributors. [You can create new features in PyTorch lightning.](https://github.com/PyTorchLightning/pytorch-lightning/projects) - + [You can solve issues in our github issues tab.](https://github.com/PyTorchLightning/pytorch-lightning/issues) - + Want to help us build Lightning and reduce boilerplate for thousands of researchers? [Learn how to make your first contribution here](https://devblog.pytorchlightning.ai/quick-contribution-guide-86d977171b3a) Lightning is also part of the [PyTorch ecosystem](https://pytorch.org/ecosystem/) which requires projects to have solid testing, documentation and support. From 864b4005204626b32d4d6fa4b70317fb274fda6f Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Wed, 6 Oct 2021 23:23:14 +0530 Subject: [PATCH 04/53] Create evaluation.py --- pytorch_lightning/utilities/evaluation.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 pytorch_lightning/utilities/evaluation.py diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py new file mode 100644 index 0000000000000..e387f4b7a81d4 --- /dev/null +++ b/pytorch_lightning/utilities/evaluation.py @@ -0,0 +1,20 @@ +import torch +import torchvision + +def get_loss(model,X,y,criterion): + preds = model(X) + loss = criterion(preds,y) + return loss.item() + +def get_accuracy(model,X,y): + preds = model(X) + correct = 0 + total = 0 + for pred,yb in zip(preds,y): + pred = int(torch.argmax(pred)) + yb = int(torch.argmax(yb)) + if pred == yb: + correct += 1 + total += 1 + acc = round(correct/total,3)*100 + return acc From 9e74636534843463f7dd22b36a100bbd8cbb6767 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Oct 2021 17:54:26 +0000 Subject: [PATCH 05/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/evaluation.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index e387f4b7a81d4..09c84faf6e453 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,20 +1,22 @@ import torch import torchvision -def get_loss(model,X,y,criterion): + +def get_loss(model, X, y, criterion): preds = model(X) - loss = criterion(preds,y) + loss = criterion(preds, y) return loss.item() - -def get_accuracy(model,X,y): + + +def get_accuracy(model, X, y): preds = model(X) correct = 0 total = 0 - for pred,yb in zip(preds,y): + for pred, yb in zip(preds, y): pred = int(torch.argmax(pred)) yb = int(torch.argmax(yb)) if pred == yb: correct += 1 total += 1 - acc = round(correct/total,3)*100 + acc = round(correct / total, 3) * 100 return acc From 818e5fee81c671fa64b305c8d4bf90f0f66319d7 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Wed, 6 Oct 2021 23:26:45 +0530 Subject: [PATCH 06/53] Update README.md --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index a07b81480d624..7dd5272956ae1 100644 --- a/README.md +++ b/README.md @@ -391,10 +391,6 @@ The lightning community is maintained by - [10+ core contributors](https://pytorch-lightning.readthedocs.io/en/latest/governance.html) who are all a mix of professional engineers, Research Scientists, and Ph.D. students from top AI labs. - 480+ active community contributors. -[You can create new features in PyTorch lightning.](https://github.com/PyTorchLightning/pytorch-lightning/projects) - -[You can solve issues in our github issues tab.](https://github.com/PyTorchLightning/pytorch-lightning/issues) - Want to help us build Lightning and reduce boilerplate for thousands of researchers? [Learn how to make your first contribution here](https://devblog.pytorchlightning.ai/quick-contribution-guide-86d977171b3a) Lightning is also part of the [PyTorch ecosystem](https://pytorch.org/ecosystem/) which requires projects to have solid testing, documentation and support. From 5f86d08188baabac2f3cae6aed70146b4954fbd4 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Wed, 6 Oct 2021 23:34:21 +0530 Subject: [PATCH 07/53] Update evaluation.py --- pytorch_lightning/utilities/evaluation.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index 09c84faf6e453..30fbad51c3bae 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,6 +1,4 @@ import torch -import torchvision - def get_loss(model, X, y, criterion): preds = model(X) From 24136291205134012d82bf55fc6e4750d52ff5cf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Oct 2021 18:05:33 +0000 Subject: [PATCH 08/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/evaluation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index 30fbad51c3bae..cb029a82f2681 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,5 +1,6 @@ import torch + def get_loss(model, X, y, criterion): preds = model(X) loss = criterion(preds, y) From d004722d07655ea69304eec771695159975c8a1d Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Wed, 6 Oct 2021 23:44:34 +0530 Subject: [PATCH 09/53] Create evaluation.py --- pytorch_lightning/utilities/evaluation.py | 39 +++++++++++++++++------ 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index cb029a82f2681..9b35ff2f41fda 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,19 +1,40 @@ import torch - -def get_loss(model, X, y, criterion): - preds = model(X) - loss = criterion(preds, y) +def get_loss(model, X, y, criterion,device='cpu',model_eval=False) -> float: + """ + model: + """ + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + else: + model.train() # If model_eval is false this funtion will turn the model's state to train + model.to(device) + X = X.to(device) + y = y.to(device) + preds = model(X) # Predicting X + loss = criterion(preds, y) # Calculating loss return loss.item() -def get_accuracy(model, X, y): - preds = model(X) +def get_accuracy(model, X, y,device:str='cpu',model_eval:bool=False,argmax:bool=False) -> float: + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + else: + model.train() # If model_eval is false this funtion will turn the model's state to train + model.to(device) + X = X.to(device) + y = y.to(device) + preds = model(X) # Predicting X + preds = preds.to(device) # Convert predictions to the device correct = 0 total = 0 - for pred, yb in zip(preds, y): - pred = int(torch.argmax(pred)) - yb = int(torch.argmax(yb)) + for pred, yb in zip(preds, y): # iterating over the predictions and the truth + if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] + pred = int(torch.argmax(pred)) + yb = int(torch.argmax(yb)) + else: # checking if the pred and the truth is like 1 5 + pred = int(torch.round(pred)) + yb = int(torch.round(yb)) if pred == yb: correct += 1 total += 1 From 741bec7636397141dc581c10e04f712d26010178 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Oct 2021 18:15:43 +0000 Subject: [PATCH 10/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/evaluation.py | 33 ++++++++++++----------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index 9b35ff2f41fda..dbf59681c16f0 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,38 +1,39 @@ import torch -def get_loss(model, X, y, criterion,device='cpu',model_eval=False) -> float: + +def get_loss(model, X, y, criterion, device="cpu", model_eval=False) -> float: """ model: """ - if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. - else: - model.train() # If model_eval is false this funtion will turn the model's state to train + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + else: + model.train() # If model_eval is false this funtion will turn the model's state to train model.to(device) X = X.to(device) y = y.to(device) - preds = model(X) # Predicting X - loss = criterion(preds, y) # Calculating loss + preds = model(X) # Predicting X + loss = criterion(preds, y) # Calculating loss return loss.item() -def get_accuracy(model, X, y,device:str='cpu',model_eval:bool=False,argmax:bool=False) -> float: - if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. +def get_accuracy(model, X, y, device: str = "cpu", model_eval: bool = False, argmax: bool = False) -> float: + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. else: - model.train() # If model_eval is false this funtion will turn the model's state to train + model.train() # If model_eval is false this funtion will turn the model's state to train model.to(device) X = X.to(device) y = y.to(device) - preds = model(X) # Predicting X - preds = preds.to(device) # Convert predictions to the device + preds = model(X) # Predicting X + preds = preds.to(device) # Convert predictions to the device correct = 0 total = 0 - for pred, yb in zip(preds, y): # iterating over the predictions and the truth - if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] + for pred, yb in zip(preds, y): # iterating over the predictions and the truth + if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] pred = int(torch.argmax(pred)) yb = int(torch.argmax(yb)) - else: # checking if the pred and the truth is like 1 5 + else: # checking if the pred and the truth is like 1 5 pred = int(torch.round(pred)) yb = int(torch.round(yb)) if pred == yb: From 725b1ccf9d3f63c7ff237e6f52041186f93b93cd Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Wed, 6 Oct 2021 23:47:48 +0530 Subject: [PATCH 11/53] Create evaluation.py --- pytorch_lightning/utilities/evaluation.py | 47 +++++++++++++++-------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index dbf59681c16f0..f39f6f0b78521 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,39 +1,52 @@ import torch - -def get_loss(model, X, y, criterion, device="cpu", model_eval=False) -> float: +def get_loss(model, X, y, criterion,device='cpu',model_eval=False) -> float: """ model: + X:Inputs of the Model + y:Ground Truths + criterion: + device:the device that the model and all of the operations are run on + model_eval:should this funtion convert the model to a train state or eval state """ - if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. - else: - model.train() # If model_eval is false this funtion will turn the model's state to train + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + else: + model.train() # If model_eval is false this funtion will turn the model's state to train model.to(device) X = X.to(device) y = y.to(device) - preds = model(X) # Predicting X - loss = criterion(preds, y) # Calculating loss + preds = model(X) # Predicting X + loss = criterion(preds, y) # Calculating loss return loss.item() -def get_accuracy(model, X, y, device: str = "cpu", model_eval: bool = False, argmax: bool = False) -> float: - if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. +def get_accuracy(model, X, y,device:str='cpu',model_eval:bool=False,argmax:bool=False) -> float: + """ + model: + X:Inputs of the Model + y:Ground Truths + criterion: + device:the device that the model and all of the operations are run on + model_eval:should this funtion convert the model to a train state or eval state + argmax:True - check if the pred and the truth is like [0,1,0] [1,0,0] False - checking if the pred and the truth is like 1 5 + """ + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. else: - model.train() # If model_eval is false this funtion will turn the model's state to train + model.train() # If model_eval is false this funtion will turn the model's state to train model.to(device) X = X.to(device) y = y.to(device) - preds = model(X) # Predicting X - preds = preds.to(device) # Convert predictions to the device + preds = model(X) # Predicting X + preds = preds.to(device) # Convert predictions to the device correct = 0 total = 0 - for pred, yb in zip(preds, y): # iterating over the predictions and the truth - if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] + for pred, yb in zip(preds, y): # iterating over the predictions and the truth + if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] pred = int(torch.argmax(pred)) yb = int(torch.argmax(yb)) - else: # checking if the pred and the truth is like 1 5 + else: # checking if the pred and the truth is like 1 5 pred = int(torch.round(pred)) yb = int(torch.round(yb)) if pred == yb: From 646c929201c85e398a4707a247b68618025658b9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Oct 2021 18:18:57 +0000 Subject: [PATCH 12/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/evaluation.py | 33 ++++++++++++----------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index f39f6f0b78521..25b1a65cf7679 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,6 +1,7 @@ import torch -def get_loss(model, X, y, criterion,device='cpu',model_eval=False) -> float: + +def get_loss(model, X, y, criterion, device="cpu", model_eval=False) -> float: """ model: X:Inputs of the Model @@ -9,19 +10,19 @@ def get_loss(model, X, y, criterion,device='cpu',model_eval=False) -> float: device:the device that the model and all of the operations are run on model_eval:should this funtion convert the model to a train state or eval state """ - if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. - else: - model.train() # If model_eval is false this funtion will turn the model's state to train + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + else: + model.train() # If model_eval is false this funtion will turn the model's state to train model.to(device) X = X.to(device) y = y.to(device) - preds = model(X) # Predicting X - loss = criterion(preds, y) # Calculating loss + preds = model(X) # Predicting X + loss = criterion(preds, y) # Calculating loss return loss.item() -def get_accuracy(model, X, y,device:str='cpu',model_eval:bool=False,argmax:bool=False) -> float: +def get_accuracy(model, X, y, device: str = "cpu", model_eval: bool = False, argmax: bool = False) -> float: """ model: X:Inputs of the Model @@ -31,22 +32,22 @@ def get_accuracy(model, X, y,device:str='cpu',model_eval:bool=False,argmax:bool= model_eval:should this funtion convert the model to a train state or eval state argmax:True - check if the pred and the truth is like [0,1,0] [1,0,0] False - checking if the pred and the truth is like 1 5 """ - if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + if model_eval is True: # Check is model_eval is True + model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. else: - model.train() # If model_eval is false this funtion will turn the model's state to train + model.train() # If model_eval is false this funtion will turn the model's state to train model.to(device) X = X.to(device) y = y.to(device) - preds = model(X) # Predicting X - preds = preds.to(device) # Convert predictions to the device + preds = model(X) # Predicting X + preds = preds.to(device) # Convert predictions to the device correct = 0 total = 0 - for pred, yb in zip(preds, y): # iterating over the predictions and the truth - if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] + for pred, yb in zip(preds, y): # iterating over the predictions and the truth + if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] pred = int(torch.argmax(pred)) yb = int(torch.argmax(yb)) - else: # checking if the pred and the truth is like 1 5 + else: # checking if the pred and the truth is like 1 5 pred = int(torch.round(pred)) yb = int(torch.round(yb)) if pred == yb: From 8f4916c166531ef7ba765dbe5e4e74ea76cd8848 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 08:22:56 +0530 Subject: [PATCH 13/53] Update evaluation.py --- pytorch_lightning/utilities/evaluation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index 25b1a65cf7679..47b4cae29006a 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -30,7 +30,7 @@ def get_accuracy(model, X, y, device: str = "cpu", model_eval: bool = False, arg criterion: device:the device that the model and all of the operations are run on model_eval:should this funtion convert the model to a train state or eval state - argmax:True - check if the pred and the truth is like [0,1,0] [1,0,0] False - checking if the pred and the truth is like 1 5 + argmax:True - [0,1,0] [1,0,0] False -1 5 """ if model_eval is True: # Check is model_eval is True model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. From 543195c9e1cc1c3a6b7885444ffef10dfa68621e Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 08:58:11 +0530 Subject: [PATCH 14/53] Create nlp.py --- pytorch_lightning/utilities/nlp.py | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pytorch_lightning/utilities/nlp.py diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py new file mode 100644 index 0000000000000..95d5bd303dfcb --- /dev/null +++ b/pytorch_lightning/utilities/nlp.py @@ -0,0 +1,62 @@ +import numpy as np +import nltk +from nltk.stem.porter import PorterStemmer +from sklearn.model_selection import train_test_split +import torch +from tqdm import tqdm +stemmer = PorterStemmer() + +def tokenize(sentence): + return nltk.word_tokenize(sentence) + +def stem(word): + return stemmmer.stem(word.lower()) + +def bag_of_words(tokenized_words,all_words): + tokenized_words = [stem(w) for w in tokenized_words] + bag = np.zeros(len(all_words)) + for idx,w in enumerate(all_words): + if w in tokenized_words: + bag[idx] = 1.0 + return bag + +def create_nlp_data(X,y,matrix_y:bool=False,device:str='cpu',test_size:float=0.25): + data = [] + labels = {} + labels_r = {} + idx = 0 + words = [] + for label in y: + if label not in list(labels.keys()): + idx += 1 + labels[label] = 1 + for X_batch,y_batch in tqdm(zip(X,y)): + X_batch = tokenize(X_batch) + new_X = [] + for Xb in X_batch: + new_X.append(stem(Xb)) + words.extend(new_X) + if matrix_y is True: + data.append([ + new_X, + np.eye(labels[y_batch],len(labels))[labels[y_batch]-1] + ]) + else: + data.append([ + new_X, + labels[y_batch] + ]) + words = sorted(set(words)) + np.random.shuffle(words) + np.random.shuffle(data) + X = [] + y = [] + for sentence,tag in tqdm(data): + X.append(bag_of_words(sentence,words)) + y.append(tag) + X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=test_size,shuffle=False) + X_train = torch.from_numpy(np.array(X_train)).to(device).float() + y_train = torch.from_numpy(np.array(y_train)).to(device).float() + X_test = torch.from_numpy(np.array(X_test)).to(device).float() + y_test = torch.from_numpy(np.array(y_test)).to(device).float() + return X_train,X_test,y_train,y_test,X,y,data,words,labels,labels_r From c08ed599a334407fd1707404005e7420b806e359 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 03:29:20 +0000 Subject: [PATCH 15/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/nlp.py | 105 ++++++++++++++--------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 95d5bd303dfcb..a4e1510c9b7de 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -1,62 +1,61 @@ -import numpy as np import nltk +import numpy as np +import torch from nltk.stem.porter import PorterStemmer from sklearn.model_selection import train_test_split -import torch from tqdm import tqdm + stemmer = PorterStemmer() + def tokenize(sentence): - return nltk.word_tokenize(sentence) + return nltk.word_tokenize(sentence) + def stem(word): - return stemmmer.stem(word.lower()) - -def bag_of_words(tokenized_words,all_words): - tokenized_words = [stem(w) for w in tokenized_words] - bag = np.zeros(len(all_words)) - for idx,w in enumerate(all_words): - if w in tokenized_words: - bag[idx] = 1.0 - return bag - -def create_nlp_data(X,y,matrix_y:bool=False,device:str='cpu',test_size:float=0.25): - data = [] - labels = {} - labels_r = {} - idx = 0 - words = [] - for label in y: - if label not in list(labels.keys()): - idx += 1 - labels[label] = 1 - for X_batch,y_batch in tqdm(zip(X,y)): - X_batch = tokenize(X_batch) - new_X = [] - for Xb in X_batch: - new_X.append(stem(Xb)) - words.extend(new_X) - if matrix_y is True: - data.append([ - new_X, - np.eye(labels[y_batch],len(labels))[labels[y_batch]-1] - ]) - else: - data.append([ - new_X, - labels[y_batch] - ]) - words = sorted(set(words)) - np.random.shuffle(words) - np.random.shuffle(data) - X = [] - y = [] - for sentence,tag in tqdm(data): - X.append(bag_of_words(sentence,words)) - y.append(tag) - X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=test_size,shuffle=False) - X_train = torch.from_numpy(np.array(X_train)).to(device).float() - y_train = torch.from_numpy(np.array(y_train)).to(device).float() - X_test = torch.from_numpy(np.array(X_test)).to(device).float() - y_test = torch.from_numpy(np.array(y_test)).to(device).float() - return X_train,X_test,y_train,y_test,X,y,data,words,labels,labels_r + return stemmmer.stem(word.lower()) + + +def bag_of_words(tokenized_words, all_words): + tokenized_words = [stem(w) for w in tokenized_words] + bag = np.zeros(len(all_words)) + for idx, w in enumerate(all_words): + if w in tokenized_words: + bag[idx] = 1.0 + return bag + + +def create_nlp_data(X, y, matrix_y: bool = False, device: str = "cpu", test_size: float = 0.25): + data = [] + labels = {} + labels_r = {} + idx = 0 + words = [] + for label in y: + if label not in list(labels.keys()): + idx += 1 + labels[label] = 1 + for X_batch, y_batch in tqdm(zip(X, y)): + X_batch = tokenize(X_batch) + new_X = [] + for Xb in X_batch: + new_X.append(stem(Xb)) + words.extend(new_X) + if matrix_y is True: + data.append([new_X, np.eye(labels[y_batch], len(labels))[labels[y_batch] - 1]]) + else: + data.append([new_X, labels[y_batch]]) + words = sorted(set(words)) + np.random.shuffle(words) + np.random.shuffle(data) + X = [] + y = [] + for sentence, tag in tqdm(data): + X.append(bag_of_words(sentence, words)) + y.append(tag) + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, shuffle=False) + X_train = torch.from_numpy(np.array(X_train)).to(device).float() + y_train = torch.from_numpy(np.array(y_train)).to(device).float() + X_test = torch.from_numpy(np.array(X_test)).to(device).float() + y_test = torch.from_numpy(np.array(y_test)).to(device).float() + return X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r From d22b3a48cbe35c5e58bf9a47f358f8fa789c8b15 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 09:04:16 +0530 Subject: [PATCH 16/53] Update evaluation.py --- pytorch_lightning/utilities/evaluation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index 47b4cae29006a..ab39fd6239582 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -29,7 +29,7 @@ def get_accuracy(model, X, y, device: str = "cpu", model_eval: bool = False, arg y:Ground Truths criterion: device:the device that the model and all of the operations are run on - model_eval:should this funtion convert the model to a train state or eval state + model_eval:convert the model to a train state or eval state argmax:True - [0,1,0] [1,0,0] False -1 5 """ if model_eval is True: # Check is model_eval is True From 992023d883520c105e5b788f5c423e4789d3d499 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 09:07:07 +0530 Subject: [PATCH 17/53] Create evaluation.py --- pytorch_lightning/utilities/evaluation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index ab39fd6239582..cc844e9e5ec33 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -11,9 +11,9 @@ def get_loss(model, X, y, criterion, device="cpu", model_eval=False) -> float: model_eval:should this funtion convert the model to a train state or eval state """ if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + model.eval() else: - model.train() # If model_eval is false this funtion will turn the model's state to train + model.train() model.to(device) X = X.to(device) y = y.to(device) @@ -28,14 +28,14 @@ def get_accuracy(model, X, y, device: str = "cpu", model_eval: bool = False, arg X:Inputs of the Model y:Ground Truths criterion: - device:the device that the model and all of the operations are run on + device:the device that all of the operations are run on model_eval:convert the model to a train state or eval state argmax:True - [0,1,0] [1,0,0] False -1 5 """ if model_eval is True: # Check is model_eval is True - model.eval() # If model_eval is true this funtion will turn the model's state to evaluation. + model.eval() else: - model.train() # If model_eval is false this funtion will turn the model's state to train + model.train() model.to(device) X = X.to(device) y = y.to(device) From 28a767044f2d7b11eb248d8fde89f11995fed9c9 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 09:09:33 +0530 Subject: [PATCH 18/53] Update nlp.py --- pytorch_lightning/utilities/nlp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index a4e1510c9b7de..3e8e91a8399da 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -13,7 +13,7 @@ def tokenize(sentence): def stem(word): - return stemmmer.stem(word.lower()) + return stemmer.stem(word.lower()) def bag_of_words(tokenized_words, all_words): From 4751e2774f09a6c26012a67da0551a822e4af0d5 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 09:42:15 +0530 Subject: [PATCH 19/53] Update nlp.py --- pytorch_lightning/utilities/nlp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 3e8e91a8399da..23feceda08b07 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -25,7 +25,7 @@ def bag_of_words(tokenized_words, all_words): return bag -def create_nlp_data(X, y, matrix_y: bool = False, device: str = "cpu", test_size: float = 0.25): +def create_nlp_data(X, y, matrix_y: bool = False, device: str, test_size: float = 0.25): data = [] labels = {} labels_r = {} From f7272074eb8b164553197fe791c8e2716e2a4000 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 09:42:17 +0530 Subject: [PATCH 20/53] Update evaluation.py --- pytorch_lightning/utilities/evaluation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index cc844e9e5ec33..e152daf6f0b2a 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,7 +1,7 @@ import torch -def get_loss(model, X, y, criterion, device="cpu", model_eval=False) -> float: +def get_loss(model, X, y, criterion, device, model_eval=False) -> float: """ model: X:Inputs of the Model @@ -22,7 +22,7 @@ def get_loss(model, X, y, criterion, device="cpu", model_eval=False) -> float: return loss.item() -def get_accuracy(model, X, y, device: str = "cpu", model_eval: bool = False, argmax: bool = False) -> float: +def get_accuracy(model, X, y, device: str, model_eval: bool = False, argmax: bool = False) -> float: """ model: X:Inputs of the Model From e3a3acc56fe32967ff432e1914ee7ed4795cfa0b Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 09:42:23 +0530 Subject: [PATCH 21/53] Create evaluation.py From c410db0a2ec07e3e889068e4d9fbf46c0f99f78f Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 10:29:06 +0530 Subject: [PATCH 22/53] Update nlp.py --- pytorch_lightning/utilities/nlp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 23feceda08b07..8122c7c1510fd 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -25,7 +25,7 @@ def bag_of_words(tokenized_words, all_words): return bag -def create_nlp_data(X, y, matrix_y: bool = False, device: str, test_size: float = 0.25): +def create_nlp_data(X, y, device: str, matrix_y: bool = False, test_size: float = 0.25): data = [] labels = {} labels_r = {} From 03a5dcc1d2f456ae7f1c7a69a7baaf6ae320409f Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 10:31:49 +0530 Subject: [PATCH 23/53] Update nlp.py --- pytorch_lightning/utilities/nlp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 8122c7c1510fd..4c42ada8fa393 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -8,11 +8,11 @@ stemmer = PorterStemmer() -def tokenize(sentence): +def tokenize(sentence) -> list: return nltk.word_tokenize(sentence) -def stem(word): +def stem(word) -> str: return stemmer.stem(word.lower()) @@ -25,7 +25,7 @@ def bag_of_words(tokenized_words, all_words): return bag -def create_nlp_data(X, y, device: str, matrix_y: bool = False, test_size: float = 0.25): +def create_nlp_data(X, y, device: str, matrix_y: bool = False, test_size: float = 0.25) -> 'X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r': data = [] labels = {} labels_r = {} From e053747cb9600fdecf304d32e05da453220a7a92 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 10:32:57 +0530 Subject: [PATCH 24/53] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 2b0f7c04f19f8..d485c23094603 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ PyYAML>=5.1 fsspec[http]>=2021.05.0, !=2021.06.0 tensorboard>=2.2.0 torchmetrics>=0.4.1 +nltk>=3.6.3 pyDeprecate==0.3.1 packaging>=17.0 typing-extensions # TypedDict support for python<3.8 From 267085a68f5e3acb713f14618822f8511ae38661 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 05:02:58 +0000 Subject: [PATCH 25/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/nlp.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 4c42ada8fa393..9c3bf838942af 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -25,7 +25,9 @@ def bag_of_words(tokenized_words, all_words): return bag -def create_nlp_data(X, y, device: str, matrix_y: bool = False, test_size: float = 0.25) -> 'X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r': +def create_nlp_data( + X, y, device: str, matrix_y: bool = False, test_size: float = 0.25 +) -> "X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r": data = [] labels = {} labels_r = {} From 78528a0f5b75fbfb0d2c95921f7867bdc6af72ee Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 10:39:36 +0530 Subject: [PATCH 26/53] Update evaluation.py --- pytorch_lightning/utilities/evaluation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index e152daf6f0b2a..5ffce1e15cf5e 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -30,6 +30,7 @@ def get_accuracy(model, X, y, device: str, model_eval: bool = False, argmax: boo criterion: device:the device that all of the operations are run on model_eval:convert the model to a train state or eval state + argmax:True - [0,1,0] [1,0,0] False -1 5 """ if model_eval is True: # Check is model_eval is True From 851009700f62a5b3169813ef4e73c73861beea1d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 05:10:45 +0000 Subject: [PATCH 27/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/evaluation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index 5ffce1e15cf5e..c30fc05c6dca2 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -30,7 +30,7 @@ def get_accuracy(model, X, y, device: str, model_eval: bool = False, argmax: boo criterion: device:the device that all of the operations are run on model_eval:convert the model to a train state or eval state - + argmax:True - [0,1,0] [1,0,0] False -1 5 """ if model_eval is True: # Check is model_eval is True From 3981c0fabf0f1dcbd4952375626b710e92eb710b Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 17:01:01 +0530 Subject: [PATCH 28/53] Create data_loader.py --- pytorch_lightning/utilities/data_loader.py | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 pytorch_lightning/utilities/data_loader.py diff --git a/pytorch_lightning/utilities/data_loader.py b/pytorch_lightning/utilities/data_loader.py new file mode 100644 index 0000000000000..db9e6f9f7279e --- /dev/null +++ b/pytorch_lightning/utilities/data_loader.py @@ -0,0 +1,51 @@ +import os,cv2 +import numpy as np +import torch,torchvision +from tqdm import tqdm + +def load_data(data_dir:str,matrix:bool=False,idx_clf:bool=False): + """ + data_dir : data_dir is the directory that you want to load the data from + matrix : if you want y as [0,1,0] + idx_clf : if you want y as 1 or 2 or 3 + """ + data = [] + labels = {} + labels_r = {} + idx = 0 + if matrix: + for label in os.listdir(data_dir): + idx += 1 + labels[label] = idx + labels_r[idx] = label + for folder in os.listdir(data_dir): + for file in os.listdir(f'{data_dir}{folder}/'): + img = cv2.imread(f'{data_dir}{folder}/{file}') + img = cv2.resize(img,(56,56)) + img = img / 255.0 + data.append([ + img, + np.eye(labels[folder]+1,len(labels))[labels[folder]-1] + ]) + X = [] + y = [] + for d in data: + X.append(d[0]) + y.append(d[1]) + if idx_clf: + for folder in os.listdir(data_dir): + idx += 1 + for file in os.listdir(f'{data_dir}{folder}/'): + img = cv2.imread(f'{data_dir}{folder}/{file}') + img = cv2.resize(img,(56,56)) + img = img / 255.0 + data.append([ + img, + idx + ]) + X = [] + y = [] + for d in data: + X.append(d[0]) + y.append(d[1]) + return X,y,labels,labels_r,idx,data From 577e7465c60fdeadd11c85892fca0cc001ee84d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 11:32:35 +0000 Subject: [PATCH 29/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/data_loader.py | 72 +++++++++++----------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/pytorch_lightning/utilities/data_loader.py b/pytorch_lightning/utilities/data_loader.py index db9e6f9f7279e..40af4e32a4058 100644 --- a/pytorch_lightning/utilities/data_loader.py +++ b/pytorch_lightning/utilities/data_loader.py @@ -1,9 +1,13 @@ -import os,cv2 +import os + +import cv2 import numpy as np -import torch,torchvision +import torch +import torchvision from tqdm import tqdm -def load_data(data_dir:str,matrix:bool=False,idx_clf:bool=False): + +def load_data(data_dir: str, matrix: bool = False, idx_clf: bool = False): """ data_dir : data_dir is the directory that you want to load the data from matrix : if you want y as [0,1,0] @@ -14,38 +18,32 @@ def load_data(data_dir:str,matrix:bool=False,idx_clf:bool=False): labels_r = {} idx = 0 if matrix: - for label in os.listdir(data_dir): - idx += 1 - labels[label] = idx - labels_r[idx] = label - for folder in os.listdir(data_dir): - for file in os.listdir(f'{data_dir}{folder}/'): - img = cv2.imread(f'{data_dir}{folder}/{file}') - img = cv2.resize(img,(56,56)) - img = img / 255.0 - data.append([ - img, - np.eye(labels[folder]+1,len(labels))[labels[folder]-1] - ]) - X = [] - y = [] - for d in data: - X.append(d[0]) - y.append(d[1]) + for label in os.listdir(data_dir): + idx += 1 + labels[label] = idx + labels_r[idx] = label + for folder in os.listdir(data_dir): + for file in os.listdir(f"{data_dir}{folder}/"): + img = cv2.imread(f"{data_dir}{folder}/{file}") + img = cv2.resize(img, (56, 56)) + img = img / 255.0 + data.append([img, np.eye(labels[folder] + 1, len(labels))[labels[folder] - 1]]) + X = [] + y = [] + for d in data: + X.append(d[0]) + y.append(d[1]) if idx_clf: - for folder in os.listdir(data_dir): - idx += 1 - for file in os.listdir(f'{data_dir}{folder}/'): - img = cv2.imread(f'{data_dir}{folder}/{file}') - img = cv2.resize(img,(56,56)) - img = img / 255.0 - data.append([ - img, - idx - ]) - X = [] - y = [] - for d in data: - X.append(d[0]) - y.append(d[1]) - return X,y,labels,labels_r,idx,data + for folder in os.listdir(data_dir): + idx += 1 + for file in os.listdir(f"{data_dir}{folder}/"): + img = cv2.imread(f"{data_dir}{folder}/{file}") + img = cv2.resize(img, (56, 56)) + img = img / 255.0 + data.append([img, idx]) + X = [] + y = [] + for d in data: + X.append(d[0]) + y.append(d[1]) + return X, y, labels, labels_r, idx, data From bf7315c47195b01d1cf0aa9b230ced66663e2312 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 17:17:34 +0530 Subject: [PATCH 30/53] Update nlp.py --- pytorch_lightning/utilities/nlp.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 9c3bf838942af..868fde6615fda 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -26,7 +26,7 @@ def bag_of_words(tokenized_words, all_words): def create_nlp_data( - X, y, device: str, matrix_y: bool = False, test_size: float = 0.25 + X, y, matrix_y: bool = False, test_size: float = 0.25 ) -> "X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r": data = [] labels = {} @@ -56,8 +56,8 @@ def create_nlp_data( X.append(bag_of_words(sentence, words)) y.append(tag) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, shuffle=False) - X_train = torch.from_numpy(np.array(X_train)).to(device).float() - y_train = torch.from_numpy(np.array(y_train)).to(device).float() - X_test = torch.from_numpy(np.array(X_test)).to(device).float() - y_test = torch.from_numpy(np.array(y_test)).to(device).float() + X_train = torch.from_numpy(np.array(X_train)).float() + y_train = torch.from_numpy(np.array(y_train)).float() + X_test = torch.from_numpy(np.array(X_test)).float() + y_test = torch.from_numpy(np.array(y_test)).float() return X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r From 24fda2b53cc0e74a80f448c76369e59af2cfce22 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 17:18:10 +0530 Subject: [PATCH 31/53] Update evaluation.py --- pytorch_lightning/utilities/evaluation.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py index c30fc05c6dca2..ea3ed69134adf 100644 --- a/pytorch_lightning/utilities/evaluation.py +++ b/pytorch_lightning/utilities/evaluation.py @@ -1,34 +1,29 @@ import torch -def get_loss(model, X, y, criterion, device, model_eval=False) -> float: +def get_loss(model, X, y, criterion, model_eval=False) -> float: """ model: X:Inputs of the Model y:Ground Truths criterion: - device:the device that the model and all of the operations are run on model_eval:should this funtion convert the model to a train state or eval state """ if model_eval is True: # Check is model_eval is True model.eval() else: model.train() - model.to(device) - X = X.to(device) - y = y.to(device) preds = model(X) # Predicting X loss = criterion(preds, y) # Calculating loss return loss.item() -def get_accuracy(model, X, y, device: str, model_eval: bool = False, argmax: bool = False) -> float: +def get_accuracy(model, X, y, model_eval: bool = False, argmax: bool = False) -> float: """ model: X:Inputs of the Model y:Ground Truths criterion: - device:the device that all of the operations are run on model_eval:convert the model to a train state or eval state argmax:True - [0,1,0] [1,0,0] False -1 5 @@ -37,11 +32,7 @@ def get_accuracy(model, X, y, device: str, model_eval: bool = False, argmax: boo model.eval() else: model.train() - model.to(device) - X = X.to(device) - y = y.to(device) preds = model(X) # Predicting X - preds = preds.to(device) # Convert predictions to the device correct = 0 total = 0 for pred, yb in zip(preds, y): # iterating over the predictions and the truth From e8323afa4739929b41006b6f18037445107737f0 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 18:21:26 +0530 Subject: [PATCH 32/53] Update data_loader.py --- pytorch_lightning/utilities/data_loader.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pytorch_lightning/utilities/data_loader.py b/pytorch_lightning/utilities/data_loader.py index 40af4e32a4058..2a0354efe5f3f 100644 --- a/pytorch_lightning/utilities/data_loader.py +++ b/pytorch_lightning/utilities/data_loader.py @@ -1,9 +1,6 @@ import os - import cv2 import numpy as np -import torch -import torchvision from tqdm import tqdm @@ -22,15 +19,15 @@ def load_data(data_dir: str, matrix: bool = False, idx_clf: bool = False): idx += 1 labels[label] = idx labels_r[idx] = label - for folder in os.listdir(data_dir): - for file in os.listdir(f"{data_dir}{folder}/"): + for folder in tqdm(os.listdir(data_dir)): + for file in tqdm(os.listdir(f"{data_dir}{folder}/")): img = cv2.imread(f"{data_dir}{folder}/{file}") img = cv2.resize(img, (56, 56)) img = img / 255.0 data.append([img, np.eye(labels[folder] + 1, len(labels))[labels[folder] - 1]]) X = [] y = [] - for d in data: + for d in tqdm(data): X.append(d[0]) y.append(d[1]) if idx_clf: From 713d6fa42e91002dbef634be40fccf3a2d47d4f2 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 18:21:28 +0530 Subject: [PATCH 33/53] Update nlp.py --- pytorch_lightning/utilities/nlp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 868fde6615fda..3ab8e5a9681bd 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -27,7 +27,7 @@ def bag_of_words(tokenized_words, all_words): def create_nlp_data( X, y, matrix_y: bool = False, test_size: float = 0.25 -) -> "X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r": +): data = [] labels = {} labels_r = {} From 5662aff801f509716a8546ddf285c77e6037f8bf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 12:52:36 +0000 Subject: [PATCH 34/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/data_loader.py | 1 + pytorch_lightning/utilities/nlp.py | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pytorch_lightning/utilities/data_loader.py b/pytorch_lightning/utilities/data_loader.py index 2a0354efe5f3f..b53f011c76e7e 100644 --- a/pytorch_lightning/utilities/data_loader.py +++ b/pytorch_lightning/utilities/data_loader.py @@ -1,4 +1,5 @@ import os + import cv2 import numpy as np from tqdm import tqdm diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py index 3ab8e5a9681bd..9ac33a429eb59 100644 --- a/pytorch_lightning/utilities/nlp.py +++ b/pytorch_lightning/utilities/nlp.py @@ -25,9 +25,7 @@ def bag_of_words(tokenized_words, all_words): return bag -def create_nlp_data( - X, y, matrix_y: bool = False, test_size: float = 0.25 -): +def create_nlp_data(X, y, matrix_y: bool = False, test_size: float = 0.25): data = [] labels = {} labels_r = {} From 7cf29781ef926a914387a9a21fecf6c536dacaa3 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Thu, 7 Oct 2021 18:25:13 +0530 Subject: [PATCH 35/53] Update data_loader.py --- pytorch_lightning/utilities/data_loader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pytorch_lightning/utilities/data_loader.py b/pytorch_lightning/utilities/data_loader.py index b53f011c76e7e..2a0354efe5f3f 100644 --- a/pytorch_lightning/utilities/data_loader.py +++ b/pytorch_lightning/utilities/data_loader.py @@ -1,5 +1,4 @@ import os - import cv2 import numpy as np from tqdm import tqdm From 618ac570739ba505ee515883cebaacd809df0c0d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 12:57:33 +0000 Subject: [PATCH 36/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/data_loader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pytorch_lightning/utilities/data_loader.py b/pytorch_lightning/utilities/data_loader.py index 2a0354efe5f3f..b53f011c76e7e 100644 --- a/pytorch_lightning/utilities/data_loader.py +++ b/pytorch_lightning/utilities/data_loader.py @@ -1,4 +1,5 @@ import os + import cv2 import numpy as np from tqdm import tqdm From c9563f9a11912ae61236df36c121946505b0e508 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Fri, 8 Oct 2021 15:21:13 +0530 Subject: [PATCH 37/53] Update requirements.txt --- requirements.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index d485c23094603..339847b13010a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,16 @@ # the default package dependencies numpy>=1.17.2 -torch>=1.6 +torch>=1.8 future>=0.17.1 # required for builtins in setup.py tqdm>=4.41.0 PyYAML>=5.1 fsspec[http]>=2021.05.0, !=2021.06.0 tensorboard>=2.2.0 torchmetrics>=0.4.1 +scikit-learn>=0.18.1 nltk>=3.6.3 pyDeprecate==0.3.1 packaging>=17.0 typing-extensions # TypedDict support for python<3.8 +opencv-python>=4.5.3.56 From 20a97055aec8d1191bc9b6622269a231f8050931 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Fri, 8 Oct 2021 16:20:43 +0530 Subject: [PATCH 38/53] Update model_checkpoint.py --- pytorch_lightning/callbacks/model_checkpoint.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py index cff2116446b92..4ad9c0d70e2d7 100644 --- a/pytorch_lightning/callbacks/model_checkpoint.py +++ b/pytorch_lightning/callbacks/model_checkpoint.py @@ -222,6 +222,9 @@ def __init__( self.verbose = verbose self.save_last = save_last self.save_top_k = save_top_k + # TODO : The error is from #9868 do a better fix for the bug this is a solution for now + if self.save_top_k is None: + self.save_top_k = 0 self.save_weights_only = save_weights_only self.auto_insert_metric_name = auto_insert_metric_name self._save_on_train_epoch_end = save_on_train_epoch_end From 45f1373db89bbb0e69d23d57912248f3c140e903 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Fri, 8 Oct 2021 17:08:15 +0530 Subject: [PATCH 39/53] Delete evaluation.py --- pytorch_lightning/utilities/evaluation.py | 49 ----------------------- 1 file changed, 49 deletions(-) delete mode 100644 pytorch_lightning/utilities/evaluation.py diff --git a/pytorch_lightning/utilities/evaluation.py b/pytorch_lightning/utilities/evaluation.py deleted file mode 100644 index ea3ed69134adf..0000000000000 --- a/pytorch_lightning/utilities/evaluation.py +++ /dev/null @@ -1,49 +0,0 @@ -import torch - - -def get_loss(model, X, y, criterion, model_eval=False) -> float: - """ - model: - X:Inputs of the Model - y:Ground Truths - criterion: - model_eval:should this funtion convert the model to a train state or eval state - """ - if model_eval is True: # Check is model_eval is True - model.eval() - else: - model.train() - preds = model(X) # Predicting X - loss = criterion(preds, y) # Calculating loss - return loss.item() - - -def get_accuracy(model, X, y, model_eval: bool = False, argmax: bool = False) -> float: - """ - model: - X:Inputs of the Model - y:Ground Truths - criterion: - model_eval:convert the model to a train state or eval state - - argmax:True - [0,1,0] [1,0,0] False -1 5 - """ - if model_eval is True: # Check is model_eval is True - model.eval() - else: - model.train() - preds = model(X) # Predicting X - correct = 0 - total = 0 - for pred, yb in zip(preds, y): # iterating over the predictions and the truth - if argmax: # check if the pred and the truth is like [0,1,0] [1,0,0] - pred = int(torch.argmax(pred)) - yb = int(torch.argmax(yb)) - else: # checking if the pred and the truth is like 1 5 - pred = int(torch.round(pred)) - yb = int(torch.round(yb)) - if pred == yb: - correct += 1 - total += 1 - acc = round(correct / total, 3) * 100 - return acc From 7faf2ac7d169629933e6f82726f79840e5e72477 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Fri, 8 Oct 2021 17:08:17 +0530 Subject: [PATCH 40/53] Delete data_loader.py --- pytorch_lightning/utilities/data_loader.py | 47 ---------------------- 1 file changed, 47 deletions(-) delete mode 100644 pytorch_lightning/utilities/data_loader.py diff --git a/pytorch_lightning/utilities/data_loader.py b/pytorch_lightning/utilities/data_loader.py deleted file mode 100644 index b53f011c76e7e..0000000000000 --- a/pytorch_lightning/utilities/data_loader.py +++ /dev/null @@ -1,47 +0,0 @@ -import os - -import cv2 -import numpy as np -from tqdm import tqdm - - -def load_data(data_dir: str, matrix: bool = False, idx_clf: bool = False): - """ - data_dir : data_dir is the directory that you want to load the data from - matrix : if you want y as [0,1,0] - idx_clf : if you want y as 1 or 2 or 3 - """ - data = [] - labels = {} - labels_r = {} - idx = 0 - if matrix: - for label in os.listdir(data_dir): - idx += 1 - labels[label] = idx - labels_r[idx] = label - for folder in tqdm(os.listdir(data_dir)): - for file in tqdm(os.listdir(f"{data_dir}{folder}/")): - img = cv2.imread(f"{data_dir}{folder}/{file}") - img = cv2.resize(img, (56, 56)) - img = img / 255.0 - data.append([img, np.eye(labels[folder] + 1, len(labels))[labels[folder] - 1]]) - X = [] - y = [] - for d in tqdm(data): - X.append(d[0]) - y.append(d[1]) - if idx_clf: - for folder in os.listdir(data_dir): - idx += 1 - for file in os.listdir(f"{data_dir}{folder}/"): - img = cv2.imread(f"{data_dir}{folder}/{file}") - img = cv2.resize(img, (56, 56)) - img = img / 255.0 - data.append([img, idx]) - X = [] - y = [] - for d in data: - X.append(d[0]) - y.append(d[1]) - return X, y, labels, labels_r, idx, data From fe9c5ccf2b020841f32d92498939006f668e1f92 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Sat, 9 Oct 2021 11:58:45 +0530 Subject: [PATCH 41/53] Delete nlp.py --- pytorch_lightning/utilities/nlp.py | 61 ------------------------------ 1 file changed, 61 deletions(-) delete mode 100644 pytorch_lightning/utilities/nlp.py diff --git a/pytorch_lightning/utilities/nlp.py b/pytorch_lightning/utilities/nlp.py deleted file mode 100644 index 9ac33a429eb59..0000000000000 --- a/pytorch_lightning/utilities/nlp.py +++ /dev/null @@ -1,61 +0,0 @@ -import nltk -import numpy as np -import torch -from nltk.stem.porter import PorterStemmer -from sklearn.model_selection import train_test_split -from tqdm import tqdm - -stemmer = PorterStemmer() - - -def tokenize(sentence) -> list: - return nltk.word_tokenize(sentence) - - -def stem(word) -> str: - return stemmer.stem(word.lower()) - - -def bag_of_words(tokenized_words, all_words): - tokenized_words = [stem(w) for w in tokenized_words] - bag = np.zeros(len(all_words)) - for idx, w in enumerate(all_words): - if w in tokenized_words: - bag[idx] = 1.0 - return bag - - -def create_nlp_data(X, y, matrix_y: bool = False, test_size: float = 0.25): - data = [] - labels = {} - labels_r = {} - idx = 0 - words = [] - for label in y: - if label not in list(labels.keys()): - idx += 1 - labels[label] = 1 - for X_batch, y_batch in tqdm(zip(X, y)): - X_batch = tokenize(X_batch) - new_X = [] - for Xb in X_batch: - new_X.append(stem(Xb)) - words.extend(new_X) - if matrix_y is True: - data.append([new_X, np.eye(labels[y_batch], len(labels))[labels[y_batch] - 1]]) - else: - data.append([new_X, labels[y_batch]]) - words = sorted(set(words)) - np.random.shuffle(words) - np.random.shuffle(data) - X = [] - y = [] - for sentence, tag in tqdm(data): - X.append(bag_of_words(sentence, words)) - y.append(tag) - X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, shuffle=False) - X_train = torch.from_numpy(np.array(X_train)).float() - y_train = torch.from_numpy(np.array(y_train)).float() - X_test = torch.from_numpy(np.array(X_test)).float() - y_test = torch.from_numpy(np.array(y_test)).float() - return X_train, X_test, y_train, y_test, X, y, data, words, labels, labels_r From 500b1f086a67e14259830de701a236261fa314bf Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Sat, 9 Oct 2021 11:59:02 +0530 Subject: [PATCH 42/53] Update requirements.txt --- requirements.txt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 339847b13010a..2b0f7c04f19f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,16 +1,13 @@ # the default package dependencies numpy>=1.17.2 -torch>=1.8 +torch>=1.6 future>=0.17.1 # required for builtins in setup.py tqdm>=4.41.0 PyYAML>=5.1 fsspec[http]>=2021.05.0, !=2021.06.0 tensorboard>=2.2.0 torchmetrics>=0.4.1 -scikit-learn>=0.18.1 -nltk>=3.6.3 pyDeprecate==0.3.1 packaging>=17.0 typing-extensions # TypedDict support for python<3.8 -opencv-python>=4.5.3.56 From dfaeb641086f513d8f217d78de6261e69f92f039 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Sat, 9 Oct 2021 12:19:44 +0530 Subject: [PATCH 43/53] Update model_checkpoint.py --- pytorch_lightning/callbacks/model_checkpoint.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py index 188cc9052aab5..78a05a8f5f608 100644 --- a/pytorch_lightning/callbacks/model_checkpoint.py +++ b/pytorch_lightning/callbacks/model_checkpoint.py @@ -147,7 +147,7 @@ class ModelCheckpoint(Callback): Raises: MisconfigurationException: - If ``save_top_k`` is neither ``None`` nor more than or equal to ``-1``, + If ``save_top_k`` is neither ``0`` nor more than or equal to ``-1``, if ``monitor`` is ``None`` and ``save_top_k`` is none of ``None``, ``-1``, and ``0``, or if ``mode`` is none of ``"min"`` or ``"max"``. ValueError: @@ -222,9 +222,6 @@ def __init__( self.verbose = verbose self.save_last = save_last self.save_top_k = save_top_k - # TODO : The error is from #9868 do a better fix for the bug this is a solution for now - if self.save_top_k is None: - self.save_top_k = 0 self.save_weights_only = save_weights_only self.auto_insert_metric_name = auto_insert_metric_name self._save_on_train_epoch_end = save_on_train_epoch_end @@ -432,7 +429,7 @@ def __validate_init_configuration(self) -> None: ) if self.save_last: rank_zero_warn( - "ModelCheckpoint(save_last=True, save_top_k=None, monitor=None) is a redundant configuration." + "ModelCheckpoint(save_last=True, save_top_k=0, monitor=None) is a redundant configuration." " You can save the last checkpoint with ModelCheckpoint(save_top_k=None, monitor=None)." ) if self.save_top_k == -1 and self.save_last: From 2162f62ace33cc35660a6966b05a7aefbbd7ccca Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Sun, 10 Oct 2021 14:54:47 +0530 Subject: [PATCH 44/53] Update README.md From 83f379eb4f3309d1b4a11e42994306fc22997f7e Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Mon, 11 Oct 2021 07:28:13 +0530 Subject: [PATCH 45/53] Update pytorch_lightning/callbacks/model_checkpoint.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adrian Wälchli --- pytorch_lightning/callbacks/model_checkpoint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py index 78a05a8f5f608..1339d3ee98ca1 100644 --- a/pytorch_lightning/callbacks/model_checkpoint.py +++ b/pytorch_lightning/callbacks/model_checkpoint.py @@ -147,7 +147,7 @@ class ModelCheckpoint(Callback): Raises: MisconfigurationException: - If ``save_top_k`` is neither ``0`` nor more than or equal to ``-1``, + If ``save_top_k`` is smaller than ``-1``, if ``monitor`` is ``None`` and ``save_top_k`` is none of ``None``, ``-1``, and ``0``, or if ``mode`` is none of ``"min"`` or ``"max"``. ValueError: From fecfbc86f6c498cadae55fa6afb5f7367b23a96f Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Mon, 11 Oct 2021 07:56:40 +0530 Subject: [PATCH 46/53] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00bd7c84b0fb9..a9772978d681b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -435,6 +435,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Fixed bug where the training step output needed to be `deepcopy`-ed ([#9349](https://github.com/PyTorchLightning/pytorch-lightning/pull/9349)) +- Fix save_top_k warning in ModelCheckpoint callback ([#9875](https://github.com/PyTorchLightning/pytorch-lightning/pull/9875)) - Fixed freeing data iterators in loop `on_run_end` ([#9386](https://github.com/PyTorchLightning/pytorch-lightning/pull/9386)) From 3dc3337347b67a44e04e94aa556f8ac0b9cc896c Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Mon, 11 Oct 2021 15:59:23 +0530 Subject: [PATCH 47/53] Update test_model_checkpoint.py --- tests/checkpointing/test_model_checkpoint.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/checkpointing/test_model_checkpoint.py b/tests/checkpointing/test_model_checkpoint.py index ed2da81dbdeda..20a58cd948102 100644 --- a/tests/checkpointing/test_model_checkpoint.py +++ b/tests/checkpointing/test_model_checkpoint.py @@ -515,16 +515,6 @@ def test_none_monitor_top_k(tmpdir): ModelCheckpoint(dirpath=tmpdir, save_top_k=0) ModelCheckpoint(dirpath=tmpdir, save_top_k=1) - -def test_none_monitor_save_last(tmpdir): - """Test that a warning appears for save_last=True with monitor=None.""" - with pytest.warns(UserWarning, match=r"ModelCheckpoint.*is a redundant.*"): - ModelCheckpoint(dirpath=tmpdir, save_last=True) - # These should not fail - ModelCheckpoint(dirpath=tmpdir, save_last=None) - ModelCheckpoint(dirpath=tmpdir, save_last=False) - - def test_invalid_every_n_epochs(tmpdir): """Make sure that a MisconfigurationException is raised for a negative every_n_epochs argument.""" with pytest.raises(MisconfigurationException, match=r".*Must be >= 0"): From a3e7581063c515fa11358a17ae0abac3dfd5089e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Oct 2021 10:30:47 +0000 Subject: [PATCH 48/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/checkpointing/test_model_checkpoint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/checkpointing/test_model_checkpoint.py b/tests/checkpointing/test_model_checkpoint.py index 20a58cd948102..5664458564993 100644 --- a/tests/checkpointing/test_model_checkpoint.py +++ b/tests/checkpointing/test_model_checkpoint.py @@ -515,6 +515,7 @@ def test_none_monitor_top_k(tmpdir): ModelCheckpoint(dirpath=tmpdir, save_top_k=0) ModelCheckpoint(dirpath=tmpdir, save_top_k=1) + def test_invalid_every_n_epochs(tmpdir): """Make sure that a MisconfigurationException is raised for a negative every_n_epochs argument.""" with pytest.raises(MisconfigurationException, match=r".*Must be >= 0"): From acf6e2830b120c7c5e28f19df826735fc9dae357 Mon Sep 17 00:00:00 2001 From: Ranuga-Disansa <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Mon, 11 Oct 2021 16:05:40 +0530 Subject: [PATCH 49/53] Update model_checkpoint.py --- pytorch_lightning/callbacks/model_checkpoint.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py index 1339d3ee98ca1..9ea6d0edd471c 100644 --- a/pytorch_lightning/callbacks/model_checkpoint.py +++ b/pytorch_lightning/callbacks/model_checkpoint.py @@ -432,11 +432,11 @@ def __validate_init_configuration(self) -> None: "ModelCheckpoint(save_last=True, save_top_k=0, monitor=None) is a redundant configuration." " You can save the last checkpoint with ModelCheckpoint(save_top_k=None, monitor=None)." ) - if self.save_top_k == -1 and self.save_last: - rank_zero_info( - "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" - " will duplicate the last checkpoint saved." - ) +# if self.save_top_k == -1 and self.save_last: +# rank_zero_info( +# "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" +# " will duplicate the last checkpoint saved." +# ) def __init_ckpt_dir(self, dirpath: Optional[_PATH], filename: Optional[str]) -> None: self._fs = get_filesystem(dirpath if dirpath else "") From 73c0ae9ae21eef328dbaaeb6b042d1244a0bc708 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Oct 2021 10:36:46 +0000 Subject: [PATCH 50/53] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/callbacks/model_checkpoint.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py index 9ea6d0edd471c..3cb7e94ef9d68 100644 --- a/pytorch_lightning/callbacks/model_checkpoint.py +++ b/pytorch_lightning/callbacks/model_checkpoint.py @@ -432,11 +432,12 @@ def __validate_init_configuration(self) -> None: "ModelCheckpoint(save_last=True, save_top_k=0, monitor=None) is a redundant configuration." " You can save the last checkpoint with ModelCheckpoint(save_top_k=None, monitor=None)." ) -# if self.save_top_k == -1 and self.save_last: -# rank_zero_info( -# "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" -# " will duplicate the last checkpoint saved." -# ) + + # if self.save_top_k == -1 and self.save_last: + # rank_zero_info( + # "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" + # " will duplicate the last checkpoint saved." + # ) def __init_ckpt_dir(self, dirpath: Optional[_PATH], filename: Optional[str]) -> None: self._fs = get_filesystem(dirpath if dirpath else "") From c56bcb87d1f527a1e47aa1045c5cf9a578fe36cf Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Mon, 11 Oct 2021 17:27:54 +0530 Subject: [PATCH 51/53] update --- pytorch_lightning/callbacks/model_checkpoint.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py index 3cb7e94ef9d68..4101b82826da4 100644 --- a/pytorch_lightning/callbacks/model_checkpoint.py +++ b/pytorch_lightning/callbacks/model_checkpoint.py @@ -427,17 +427,12 @@ def __validate_init_configuration(self) -> None: f"ModelCheckpoint(save_top_k={self.save_top_k}, monitor=None) is not a valid" " configuration. No quantity for top_k to track." ) - if self.save_last: - rank_zero_warn( - "ModelCheckpoint(save_last=True, save_top_k=0, monitor=None) is a redundant configuration." - " You can save the last checkpoint with ModelCheckpoint(save_top_k=None, monitor=None)." - ) - # if self.save_top_k == -1 and self.save_last: - # rank_zero_info( - # "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" - # " will duplicate the last checkpoint saved." - # ) + if self.save_top_k == -1 and self.save_last: + rank_zero_info( + "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" + " will duplicate the last checkpoint saved." + ) def __init_ckpt_dir(self, dirpath: Optional[_PATH], filename: Optional[str]) -> None: self._fs = get_filesystem(dirpath if dirpath else "") From ad6259dac0081dc3a8637734752c24359f40c6c3 Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Mon, 11 Oct 2021 17:28:41 +0530 Subject: [PATCH 52/53] update --- pytorch_lightning/callbacks/model_checkpoint.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py index 4101b82826da4..8d8dca9db5b24 100644 --- a/pytorch_lightning/callbacks/model_checkpoint.py +++ b/pytorch_lightning/callbacks/model_checkpoint.py @@ -429,10 +429,10 @@ def __validate_init_configuration(self) -> None: ) if self.save_top_k == -1 and self.save_last: - rank_zero_info( - "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" - " will duplicate the last checkpoint saved." - ) + rank_zero_info( + "ModelCheckpoint(save_last=True, save_top_k=-1, monitor=None)" + " will duplicate the last checkpoint saved." + ) def __init_ckpt_dir(self, dirpath: Optional[_PATH], filename: Optional[str]) -> None: self._fs = get_filesystem(dirpath if dirpath else "") From 4a51af587817bf4effbf14d093099641249f5096 Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Mon, 11 Oct 2021 17:30:46 +0530 Subject: [PATCH 53/53] chlog update --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9772978d681b..bf426c3fa0371 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -418,6 +418,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed `TrainerProperties` mixin and moved property definitions directly into `Trainer` ([#9495](https://github.com/PyTorchLightning/pytorch-lightning/pull/9495)) +- Removed a redundant warning with `ModelCheckpoint(monitor=None)` callback ([#9875](https://github.com/PyTorchLightning/pytorch-lightning/pull/9875)) + + ### Fixed @@ -435,7 +438,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Fixed bug where the training step output needed to be `deepcopy`-ed ([#9349](https://github.com/PyTorchLightning/pytorch-lightning/pull/9349)) -- Fix save_top_k warning in ModelCheckpoint callback ([#9875](https://github.com/PyTorchLightning/pytorch-lightning/pull/9875)) - Fixed freeing data iterators in loop `on_run_end` ([#9386](https://github.com/PyTorchLightning/pytorch-lightning/pull/9386))