This error message indicates that there is an issue with the get_checkpoint() function in your code. The function is defined to not take any positional arguments, but it is being called with one argument.
To fix this issue, you need to ensure that the get_checkpoint() function is correctly defined and called without any arguments. Double-check the implementation of the get_checkpoint() function and make sure it does not expect any arguments.
If you are using a library or framework that defines the get_checkpoint() function, you may need to consult the documentation or example code to ensure you are using it correctly.
Once you have fixed the get_checkpoint() function, you can retry running your code and see if the error is resolved.
from IPython.display import clear_output
from pathlib import Path
from ray import tune, air
from ray.air import session, Checkpoint
from ray.tune.schedulers import PopulationBasedTraining
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import Dataset, DataLoader
from torchvision.models import resnet50
from tqdm import tqdm
import cv2
import json
import math
import numpy as np
import optuna
import os
import random
import ray
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
Set Up
#!device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = torch.device('cpu')
RAY_USE_XRAY=1
Set a random seed for torch
seed = 13
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # If using multiple GPUs
np.random.seed(seed)
random.seed(seed)
Additional configurations for deterministic behavior on CUDA devices
num_samples = 100 # None # Number of samples used for training. If None then all images
None # Number of samples used for validation. If None then all images
num_val_samples = 25
Tuple defining the input image shape (height, width)
input_shape = (224, 224)
num_classes = 4 # The possible rotation angles: 0, 90, 180, 270
Optuna Optimization Parameters
n_trials = 5 # Number of Optuna Trials
learning_rate_options = (1e-5, 1e-1) # Learning rate for the optimizer
batch_size_options = [8, 16] # Batch size for training
num_epochs_options = (5, 5) # Number of epochs to train the model for
weight_decay_options = (1e-6, 0.1)
dropout_rate_options = (0.001, 0.5)
This error message indicates that there is an issue with the get_checkpoint() function in your code. The function is defined to not take any positional arguments, but it is being called with one argument.
To fix this issue, you need to ensure that the get_checkpoint() function is correctly defined and called without any arguments. Double-check the implementation of the get_checkpoint() function and make sure it does not expect any arguments.
If you are using a library or framework that defines the get_checkpoint() function, you may need to consult the documentation or example code to ensure you are using it correctly.
Once you have fixed the get_checkpoint() function, you can retry running your code and see if the error is resolved.
User
how to fix it
Assistant
To fix the issue, you need to remove the argument from the get_checkpoint() function.
In the provided code, it seems that the get_checkpoint() function is not defined explicitly, but it is being called in the train_model() function using session.get_checkpoint(checkpoint_dir).
To fix the issue, you can try removing the checkpoint_dir argument from the get_checkpoint() function call in the train_model() function.
Update the following line of code in the train_model() function:
def angle_difference(x, y):
"""
Calculate minimum difference between two angles.
"""
return 180 - abs(abs(x - y) - 180)
def angle_error(y_true, y_pred):
"""
Calculate the mean difference between the true angles
and the predicted angles.
"""
diff = angle_difference(y_true, y_pred)
return torch.mean(torch.abs(diff.float()))
Rotation Related
def rotate(image, angle):
"""
Rotates an OpenCV 2 / NumPy image about its center by the given angle
(in degrees).
"""
center = (image.shape[1] / 2, image.shape[0] / 2)
rot_mat = cv2.getRotationMatrix2D(center, angle, 1.0)
return cv2.warpAffine(image, rot_mat, (image.shape[1], image.shape[0]), flags=cv2.INTER_LINEAR)
def largest_rotated_rect(w, h, angle):
"""
Given a rectangle of size wxh that has been rotated by 'angle' (in
radians), computes the width and height of the largest possible
axis-aligned rectangle within the rotated rectangle.
Original JS code by 'Andri' and Magnus Hoff from Stack Overflow
Converted to Python by Aaron Snoswell
Source: http://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders
"""
quadrant = int(math.floor(angle / (math.pi / 2))) & 3
sign_alpha = angle if ((quadrant & 1) == 0) else math.pi - angle
alpha = (sign_alpha % math.pi + math.pi) % math.pi
bb_w = w * math.cos(alpha) + h * math.sin(alpha)
bb_h = w * math.sin(alpha) + h * math.cos(alpha)
gamma = math.atan2(bb_w, bb_w) if (w < h) else math.atan2(bb_w, bb_w)
delta = math.pi - alpha - gamma
length = h if (w < h) else w
d = length * math.cos(alpha)
a = d * math.sin(alpha) / math.sin(delta)
y = a * math.cos(gamma)
x = y * math.tan(gamma)
return (
bb_w - 2 * x,
bb_h - 2 * y
)
def crop_around_center(image, width, height):
"""
Given a NumPy / OpenCV 2 image, crops it to the given width and height,
around it's centre point
def crop_largest_rectangle(image, angle, height, width):
"""
Crop around the center the largest possible rectangle
found with largest_rotated_rect.
"""
return crop_around_center(
image,
*largest_rotated_rect(
width,
height,
math.radians(angle)
)
)
def generate_rotated_image(image, angle, size=None, crop_center=False,
crop_largest_rect=False):
"""
Generate a valid rotated image for the RotNetDataGenerator. If the
image is rectangular, the crop_center option should be used to make
it square. To crop out the black borders after rotation, use the
crop_largest_rect option. To resize the final image, use the size
option.
"""
height, width = image.shape[:2]
if crop_center:
if width < height:
height = width
else:
width = height
image = rotate(image, angle)
if crop_largest_rect:
image = crop_largest_rectangle(image, angle, height, width)
if size:
image = cv2.resize(image, size)
return image
# Recursively traverse the directory structure
for root, _, filenames in os.walk(path):
for filename in filenames:
image_paths.append(os.path.join(root, filename))
# Use all available images if num_samples is not provided
if num_samples is None:
train_filenames = image_paths
else:
# Randomly select num_samples images
train_filenames = random.sample(
image_paths, min(num_samples, len(image_paths)))
# Shuffle the train_filenames list
random.shuffle(train_filenames)
return train_filenames