diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 41b5535e1..2f57eeb69 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ ci: repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v4.6.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace @@ -23,7 +23,7 @@ repos: - id: detect-private-key - repo: https://github.com/codespell-project/codespell - rev: v2.2.6 + rev: v2.3.0 hooks: - id: codespell additional_dependencies: [tomli] @@ -37,7 +37,7 @@ repos: args: ["--in-place"] - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.1.0 + rev: v4.0.0-alpha.8 hooks: - id: prettier files: \.(json|yml|yaml|toml) @@ -54,7 +54,7 @@ repos: - mdformat_frontmatter - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.3.5 + rev: v0.5.0 hooks: # try to fix what is possible - id: ruff diff --git a/course_UvA-DL/05-transformers-and-MH-attention/Transformers_MHAttention.py b/course_UvA-DL/05-transformers-and-MH-attention/Transformers_MHAttention.py index 02f233849..f3ba2d528 100644 --- a/course_UvA-DL/05-transformers-and-MH-attention/Transformers_MHAttention.py +++ b/course_UvA-DL/05-transformers-and-MH-attention/Transformers_MHAttention.py @@ -87,7 +87,7 @@ os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: @@ -796,7 +796,7 @@ def _create_model(self): num_heads=self.hparams.num_heads, dropout=self.hparams.dropout, ) - # Output classifier per sequence lement + # Output classifier per sequence element self.output_net = nn.Sequential( nn.Linear(self.hparams.model_dim, self.hparams.model_dim), nn.LayerNorm(self.hparams.model_dim), @@ -948,8 +948,8 @@ def _calculate_loss(self, batch, mode="train"): acc = (preds.argmax(dim=-1) == labels).float().mean() # Logging - self.log("%s_loss" % mode, loss) - self.log("%s_acc" % mode, acc) + self.log(f"{mode}_loss", loss) + self.log(f"{mode}_acc", acc) return loss, acc def training_step(self, batch, batch_idx): @@ -1419,8 +1419,8 @@ def _calculate_loss(self, batch, mode="train"): preds = preds.squeeze(dim=-1) # Shape: [Batch_size, set_size] loss = F.cross_entropy(preds, labels) # Softmax/CE over set dimension acc = (preds.argmax(dim=-1) == labels).float().mean() - self.log("%s_loss" % mode, loss) - self.log("%s_acc" % mode, acc, on_step=False, on_epoch=True) + self.log(f"{mode}_loss", loss) + self.log(f"{mode}_acc", acc, on_step=False, on_epoch=True) return loss, acc def training_step(self, batch, batch_idx): diff --git a/course_UvA-DL/06-graph-neural-networks/GNN_overview.py b/course_UvA-DL/06-graph-neural-networks/GNN_overview.py index 6767f0a61..5b53866f0 100644 --- a/course_UvA-DL/06-graph-neural-networks/GNN_overview.py +++ b/course_UvA-DL/06-graph-neural-networks/GNN_overview.py @@ -61,7 +61,7 @@ os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: @@ -616,7 +616,7 @@ def forward(self, data, mode="train"): elif mode == "test": mask = data.test_mask else: - assert False, "Unknown forward mode: %s" % mode + assert False, f"Unknown forward mode: {mode}" loss = self.loss_module(x[mask], data.y[mask]) acc = (x[mask].argmax(dim=-1) == data.y[mask]).sum().float() / mask.sum() @@ -671,7 +671,7 @@ def train_node_classifier(model_name, dataset, **model_kwargs): trainer.logger._default_hp_metric = None # Optional logging argument that we don't need # Check whether pretrained model exists. If yes, load it and skip training - pretrained_filename = os.path.join(CHECKPOINT_PATH, "NodeLevel%s.ckpt" % model_name) + pretrained_filename = os.path.join(CHECKPOINT_PATH, f"NodeLevel{model_name}.ckpt") if os.path.isfile(pretrained_filename): print("Found pretrained model, loading...") model = NodeLevelGNN.load_from_checkpoint(pretrained_filename) @@ -790,7 +790,7 @@ def print_results(result_dict): # %% print("Data object:", tu_dataset.data) print("Length:", len(tu_dataset)) -print("Average label: %4.2f" % (tu_dataset.data.y.float().mean().item())) +print(f"Average label: {tu_dataset.data.y.float().mean().item():4.2f}") # %% [markdown] # The first line shows how the dataset stores different graphs. @@ -957,7 +957,7 @@ def train_graph_classifier(model_name, **model_kwargs): trainer.logger._default_hp_metric = None # Check whether pretrained model exists. If yes, load it and skip training - pretrained_filename = os.path.join(CHECKPOINT_PATH, "GraphLevel%s.ckpt" % model_name) + pretrained_filename = os.path.join(CHECKPOINT_PATH, f"GraphLevel{model_name}.ckpt") if os.path.isfile(pretrained_filename): print("Found pretrained model, loading...") model = GraphLevelGNN.load_from_checkpoint(pretrained_filename) diff --git a/course_UvA-DL/07-deep-energy-based-generative-models/Deep_Energy_Models.py b/course_UvA-DL/07-deep-energy-based-generative-models/Deep_Energy_Models.py index f9f15ccb5..f4e6d8c77 100644 --- a/course_UvA-DL/07-deep-energy-based-generative-models/Deep_Energy_Models.py +++ b/course_UvA-DL/07-deep-energy-based-generative-models/Deep_Energy_Models.py @@ -68,7 +68,7 @@ os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: @@ -770,7 +770,7 @@ def train_model(**kwargs): rand_imgs = torch.rand((128,) + model.hparams.img_shape).to(model.device) rand_imgs = rand_imgs * 2 - 1.0 rand_out = model.cnn(rand_imgs).mean() - print("Average score for random images: %4.2f" % (rand_out.item())) + print(f"Average score for random images: {rand_out.item():4.2f}") # %% [markdown] # As we hoped, the model assigns very low probability to those noisy images. @@ -781,7 +781,7 @@ def train_model(**kwargs): train_imgs, _ = next(iter(train_loader)) train_imgs = train_imgs.to(model.device) train_out = model.cnn(train_imgs).mean() - print("Average score for training images: %4.2f" % (train_out.item())) + print(f"Average score for training images: {train_out.item():4.2f}") # %% [markdown] # The scores are close to 0 because of the regularization objective that was added to the training. @@ -803,8 +803,8 @@ def compare_images(img1, img2): plt.xticks([(img1.shape[2] + 2) * (0.5 + j) for j in range(2)], labels=["Original image", "Transformed image"]) plt.yticks([]) plt.show() - print("Score original image: %4.2f" % score1) - print("Score transformed image: %4.2f" % score2) + print(f"Score original image: {score1:4.2f}") + print(f"Score transformed image: {score2:4.2f}") # %% [markdown] diff --git a/course_UvA-DL/08-deep-autoencoders/Deep_Autoencoders.py b/course_UvA-DL/08-deep-autoencoders/Deep_Autoencoders.py index 015cdb15a..7819bd8cc 100644 --- a/course_UvA-DL/08-deep-autoencoders/Deep_Autoencoders.py +++ b/course_UvA-DL/08-deep-autoencoders/Deep_Autoencoders.py @@ -64,7 +64,7 @@ file_path = os.path.join(CHECKPOINT_PATH, file_name) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: diff --git a/course_UvA-DL/09-normalizing-flows/NF_image_modeling.py b/course_UvA-DL/09-normalizing-flows/NF_image_modeling.py index 00c53aa01..9b0c02e67 100644 --- a/course_UvA-DL/09-normalizing-flows/NF_image_modeling.py +++ b/course_UvA-DL/09-normalizing-flows/NF_image_modeling.py @@ -68,7 +68,7 @@ file_path = os.path.join(CHECKPOINT_PATH, file_name) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: @@ -518,7 +518,7 @@ def visualize_dequantization(quants, prior=None): plt.plot([inp[indices[0][-1]]] * 2, [0, prob[indices[0][-1]]], color=color) x_ticks.append(inp[indices[0][0]]) x_ticks.append(inp.max()) - plt.xticks(x_ticks, ["%.1f" % x for x in x_ticks]) + plt.xticks(x_ticks, [f"{x:.1f}" for x in x_ticks]) plt.plot(inp, prob, color=(0.0, 0.0, 0.0)) # Set final plot properties plt.ylim(0, prob.max() * 1.1) @@ -1199,8 +1199,8 @@ def print_num_params(model): table = [ [ key, - "%4.3f bpd" % flow_dict[key]["result"]["val"][0]["test_bpd"], - "%4.3f bpd" % flow_dict[key]["result"]["test"][0]["test_bpd"], + "{:4.3f} bpd".format(flow_dict[key]["result"]["val"][0]["test_bpd"]), + "{:4.3f} bpd".format(flow_dict[key]["result"]["test"][0]["test_bpd"]), "%2.0f ms" % (1000 * flow_dict[key]["result"]["time"]), "%2.0f ms" % (1000 * flow_dict[key]["result"].get("samp_time", 0)), "{:,}".format(sum(np.prod(p.shape) for p in flow_dict[key]["model"].parameters())), diff --git a/course_UvA-DL/10-autoregressive-image-modeling/Autoregressive_Image_Modeling.py b/course_UvA-DL/10-autoregressive-image-modeling/Autoregressive_Image_Modeling.py index 2fdf597ea..3367a1496 100644 --- a/course_UvA-DL/10-autoregressive-image-modeling/Autoregressive_Image_Modeling.py +++ b/course_UvA-DL/10-autoregressive-image-modeling/Autoregressive_Image_Modeling.py @@ -92,7 +92,7 @@ file_path = os.path.join(CHECKPOINT_PATH, file_name) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: diff --git a/course_UvA-DL/11-vision-transformer/Vision_Transformer.py b/course_UvA-DL/11-vision-transformer/Vision_Transformer.py index 73d231325..fd7a6d45a 100644 --- a/course_UvA-DL/11-vision-transformer/Vision_Transformer.py +++ b/course_UvA-DL/11-vision-transformer/Vision_Transformer.py @@ -69,7 +69,7 @@ os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: @@ -353,8 +353,8 @@ def _calculate_loss(self, batch, mode="train"): loss = F.cross_entropy(preds, labels) acc = (preds.argmax(dim=-1) == labels).float().mean() - self.log("%s_loss" % mode, loss) - self.log("%s_acc" % mode, acc) + self.log(f"{mode}_loss", loss) + self.log(f"{mode}_acc", acc) return loss def training_step(self, batch, batch_idx): @@ -396,7 +396,7 @@ def train_model(**kwargs): # Check whether pretrained model exists. If yes, load it and skip training pretrained_filename = os.path.join(CHECKPOINT_PATH, "ViT.ckpt") if os.path.isfile(pretrained_filename): - print("Found pretrained model at %s, loading..." % pretrained_filename) + print(f"Found pretrained model at {pretrained_filename}, loading...") # Automatically loads the model with the saved hyperparameters model = ViT.load_from_checkpoint(pretrained_filename) else: diff --git a/course_UvA-DL/12-meta-learning/Meta_Learning.py b/course_UvA-DL/12-meta-learning/Meta_Learning.py index 5e17b5692..6cb110823 100644 --- a/course_UvA-DL/12-meta-learning/Meta_Learning.py +++ b/course_UvA-DL/12-meta-learning/Meta_Learning.py @@ -92,7 +92,7 @@ os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True) if not os.path.isfile(file_path): file_url = base_url + file_name - print("Downloading %s..." % file_url) + print(f"Downloading {file_url}...") try: urllib.request.urlretrieve(file_url, file_path) except HTTPError as e: @@ -525,8 +525,8 @@ def calculate_loss(self, batch, mode): preds, labels, acc = self.classify_feats(prototypes, classes, query_feats, query_targets) loss = F.cross_entropy(preds, labels) - self.log("%s_loss" % mode, loss) - self.log("%s_acc" % mode, acc) + self.log(f"{mode}_loss", loss) + self.log(f"{mode}_acc", acc) return loss def training_step(self, batch, batch_idx): @@ -573,7 +573,7 @@ def train_model(model_class, train_loader, val_loader, **kwargs): # Check whether pretrained model exists. If yes, load it and skip training pretrained_filename = os.path.join(CHECKPOINT_PATH, model_class.__name__ + ".ckpt") if os.path.isfile(pretrained_filename): - print("Found pretrained model at %s, loading..." % pretrained_filename) + print(f"Found pretrained model at {pretrained_filename}, loading...") # Automatically loads the model with the saved hyperparameters model = model_class.load_from_checkpoint(pretrained_filename) else: @@ -947,8 +947,8 @@ def outer_loop(self, batch, mode="train"): opt.step() opt.zero_grad() - self.log("%s_loss" % mode, sum(losses) / len(losses)) - self.log("%s_acc" % mode, sum(accuracies) / len(accuracies)) + self.log(f"{mode}_loss", sum(losses) / len(losses)) + self.log(f"{mode}_acc", sum(accuracies) / len(accuracies)) def training_step(self, batch, batch_idx): self.outer_loop(batch, mode="train") diff --git a/course_UvA-DL/13-contrastive-learning/SimCLR.py b/course_UvA-DL/13-contrastive-learning/SimCLR.py index 40a71192d..cc8d027cb 100644 --- a/course_UvA-DL/13-contrastive-learning/SimCLR.py +++ b/course_UvA-DL/13-contrastive-learning/SimCLR.py @@ -760,7 +760,7 @@ def train_resnet(batch_size, max_epochs=100, **kwargs): # Check whether pretrained model exists. If yes, load it and skip training pretrained_filename = os.path.join(CHECKPOINT_PATH, "ResNet.ckpt") if os.path.isfile(pretrained_filename): - print("Found pretrained model at %s, loading..." % pretrained_filename) + print(f"Found pretrained model at {pretrained_filename}, loading...") model = ResNet.load_from_checkpoint(pretrained_filename) else: L.seed_everything(42) # To be reproducible diff --git a/flash_tutorials/image_classification/image_classification.py b/flash_tutorials/image_classification/image_classification.py index ba34a8b0e..f656f09dc 100644 --- a/flash_tutorials/image_classification/image_classification.py +++ b/flash_tutorials/image_classification/image_classification.py @@ -1,5 +1,5 @@ # %% [markdown] -# In this tutorial, we'll go over the basics of lightning Flash by finetuning/predictin with an ImageClassifier on [Hymenoptera Dataset](https://www.kaggle.com/ajayrana/hymenoptera-data) containing ants and bees images. +# In this tutorial, we'll go over the basics of lightning Flash by finetuning/prediction with an ImageClassifier on [Hymenoptera Dataset](https://www.kaggle.com/ajayrana/hymenoptera-data) containing ants and bees images. # # # Finetuning #