DeepSense Forum

Notifications
Clear all

[Solved] Training Radar Aided Beamforming


avn
 avn
Active Member
Joined: 1 year ago
Posts: 7
Topic starter  

Hi DeepSense Team,

I modified scenario9_radar_beam-prediction_inference.py

and tried to train the model using the modified script. This is the modified script

import torch

from radar_network_models import LeNet_RadarCube, LeNet_RangeAngle, LeNet_RangeVelocity
from radar_preprocessing_torch import range_velocity_map, range_angle_map, radar_cube
from network_functions import test_loop, evaluate_predictions, train_loop
from dataset import load_radar_data

# Model Name
model_folder = 'type0_split100_batchsize32_rng0_epoch40' #  Radar Cube solution
# model_folder = 'type1_split100_batchsize32_rng0_epoch40' # Range-Velocity solution
# model_folder = 'type2_split100_batchsize32_rng0_epoch40' # Range-Angle solution

model_path = './saved_models/' + model_folder + '/'

# Dataset Files
dataset_dir = r'./Scenario9/development_dataset' # Development dataset location
csv_file = 'scenario9_dev_train.csv' # Test CSV file

# Solution Type Selection / Automatically extracted from folder name
data_type = int(model_folder.split('_')[0][-1]) # 0: Radar Cube / 1: Range Velocity / 2: Range Angle

# Define set of preprocessing and neural networks for different solutions
preprocessing_functions = [radar_cube, range_velocity_map, range_angle_map]
neural_nets = [LeNet_RadarCube, LeNet_RangeVelocity, LeNet_RangeAngle]

# Load Data
X_train, y_train = load_radar_data(dataset_dir, csv_file, radar_column='unit1_radar_1', label_column='beam_index_1')

# Radar Preprocessing
preprocessing_function = preprocessing_functions[data_type]
X_train = preprocessing_function(X_train)

# PyTorch Tensors
X_train = torch.from_numpy(X_train)
y_train = torch.from_numpy(y_train)

# Neural Network Settings
if torch.cuda.is_available():
    dev_name = 'cuda'
elif torch.backends.mps.is_available():
    dev_name = 'mps'
else:
    dev_name = 'cpu'
device = torch.device(dev_name)
net = neural_nets[data_type]()
net.to(device)

print('Training..')
topk = 5

criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3, weight_decay=1e-4)
train_loop(X_train, 
           y_train, 
           net, 
           optimizer, 
           criterion, 
           device, 
           batch_size=32, 
           model_path=model_path+'modelcustom.pth')

 

and the modified train_loop() function is below:

def train_loop(X_train, 
               y_train, 
               net, 
               optimizer, 
               criterion, 
               device, 
               batch_size=64,
               model_path='./model_custom.pth'):
    net.train()
    
    running_acc = 0.0
    running_loss = 0.0
    with tqdm(iterate_minibatches(X_train, y_train, batch_size, shuffle=True), unit=' batch', 
              total=int(np.ceil(X_train.shape[0]/batch_size)), file=sys.stdout, leave=True) as tepoch:
        for batch_x, batch_y in tepoch:
            
            batch_x, batch_y = batch_x.to(device), batch_y.to(device)
            
            optimizer.zero_grad() # Make the gradients zero
            batch_y_hat = net(batch_x) # Prediction
            loss = criterion(batch_y_hat, batch_y) # Loss computation
            loss.backward() # Backward step
            optimizer.step() # Update coefficients
            
            predictions = batch_y_hat.argmax(dim=1, keepdim=True).squeeze()
            
            batch_correct = (predictions == batch_y).sum().item()
            running_acc += batch_correct
            batch_loss = loss.item()
            running_loss += batch_loss
            
            tepoch.set_postfix(loss=batch_loss, accuracy=100. * batch_correct/batch_size)
            
        curr_acc = 100. * running_acc/len(X_train)
        curr_loss = running_loss/np.ceil(X_train.shape[0]/batch_size)
    torch.save(net.state_dict(), model_path)
    print('saved in ' + model_path)
    print('Training: [accuracy=%.2f, loss=%.4f]' % (curr_acc, curr_loss), flush=True)
    return curr_loss, curr_acc

I print the accuracy and loss and get this result :

[accuracy=11.26, loss=3.7072]

 

I suppose that I didn't set some parameters correctly. From your this line code, I suppose that I have to set

split=100, rng=0, and epoch=40

. How to set this params and any other suggestion? 


Quote
udemirhan
Member Admin
Joined: 2 years ago
Posts: 4
 

Hi @avn,

 

I've added a training example (train.py) to the GitHub repo. You can use it as a reference.

 

-Umut


ReplyQuote
avn
 avn
Active Member
Joined: 1 year ago
Posts: 7
Topic starter  

Thank you for your response....


ReplyQuote

Leave a reply

Author Name

Author Email

Title *

 
Preview 0 Revisions Saved