0

I have the following code:

import os
import numpy as np
from skimage.transform import resize

# Define the path to the folder containing the optical flow data
flow_path = "flow"

# Define the path to the folder to save the training and validation sets
data_path = "data"

# Define the number of training samples per class
num_train_samples = 3

clas = {"class1": 0,"class2":1, "class3":2, "class14":3, "class5":4, "class6":5}

# Define the empty list to hold the data and labels
data = []
labels = []
size = (120, 160)


# Iterate over each class folder
for class_folder in os.listdir(flow_path):
    class_path = os.path.join(flow_path, class_folder)
    class_label = int(clas[class_folder])

    # Iterate over each video folder in the class folder
    for video_folder in os.listdir(class_path):
        video_path = os.path.join(class_path, video_folder)

        # Load the optical flow data from the .npy file
        flow_data = np.load(os.path.join(video_path, "flow.npy"))
        # print(flow_data)
        # flow_resized = resize(flow_data, size, anti_aliasing=True)
        # Append the data and label to the list
        data.append(flow_data)
        labels.append(class_label)

# Convert the list to a numpy array
data = np.array(data, dtype=np.float32)
labels = np.array(labels, dtype=np.int32)

# Shuffle the data and labels
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]

# Split the data and labels into a training set and validation set
x_train = data[:num_train_samples]
y_train = labels[:num_train_samples]
x_val = data[num_train_samples:]
y_val = labels[num_train_samples:]

# Save the training set and validation set to disk as .npy files
np.save(os.path.join(data_path, "x_train.npy"), x_train)
np.save(os.path.join(data_path, "y_train.npy"), y_train)
np.save(os.path.join(data_path, "x_val.npy"), x_val)
np.save(os.path.join(data_path, "y_val.npy"), y_val)

I am trying to stack optical flow data from respective videos together before feeding it to a 3dCNN model. I am unable to do so because of the following error

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in <cell line: 40>() 38 39 # Convert the list to a numpy array ---> 40 data = np.array(data, dtype=np.int32) 41 labels = np.array(labels, dtype=np.int32) 42

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (28,) + inhomogeneous part.

I tried removing float 32 from the following line

data = np.array(data, dtype=np.float32)

But when I do this for my model trainig:

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import numpy as np

# Define the 3D CNN model
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv3d(2, 32, kernel_size=3)
        self.pool1 = nn.MaxPool3d(kernel_size=2, stride=2)
        self.conv2 = nn.Conv3d(32, 64, kernel_size=3)
        self.pool2 = nn.MaxPool3d(kernel_size=2, stride=2)
        self.conv3 = nn.Conv3d(64, 128, kernel_size=3)
        self.pool3 = nn.MaxPool3d(kernel_size=2, stride=2)
        self.fc1 = nn.Linear(128 * 7 * 9 * 6, 512)
        self.dropout1 = nn.Dropout(p=0.5)
        self.fc2 = nn.Linear(512, 6)

    def forward(self, x):
        x = self.pool1(torch.relu(self.conv1(x)))
        x = self.pool2(torch.relu(self.conv2(x)))
        x = self.pool3(torch.relu(self.conv3(x)))
        x = x.view(-1, 128 * 7 * 9 * 6)
        x = self.dropout1(torch.relu(self.fc1(x)))
        x = self.fc2(x)
        return x

# Define the path to the folder containing the training and validation sets
data_path = "data"

# Load the training and validation sets
x_train = np.load(os.path.join(data_path, "x_train.npy"), allow_pickle=True)
y_train = np.load(os.path.join(data_path, "y_train.npy"), allow_pickle=True)
x_val = np.load(os.path.join(data_path, "x_val.npy"), allow_pickle=True)
y_val = np.load(os.path.join(data_path, "y_val.npy"), allow_pickle=True)

# Convert the data to PyTorch tensors and create a DataLoader
train_data = TensorDataset(torch.Tensor(x_train), torch.Tensor(y_train))
train_loader = DataLoader(train_data, batch_size=16, shuffle=True)
val_data = TensorDataset(torch.Tensor(x_val), torch.Tensor(y_val))
val_loader = DataLoader(val_data, batch_size=16, shuffle=False)

# Define the device to use for training
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)

# Define the number of epochs to train for
num_epochs = 10

# Train the model
net = Net().to(device)
for epoch in range(num_epochs):
    net.train()
    train_loss = 0.0
    for i, (inputs, labels) in enumerate(train_loader):
        inputs = inputs.to(device)
        labels = labels.to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, labels.long())
        loss.backward()
        optimizer.step()
        train_loss += loss.item()
    train_loss /= len(train_loader)
    net.eval()
    val_loss = 0.0
    correct = 0
    total = 0
    with torch.no_grad():
        for inputs, labels in val_loader:
            inputs = inputs.to(device)
            labels = labels.to(device)
            outputs = net(inputs)
            loss = criterion(outputs, labels.long())
            val_loss += loss.item()
    print('Epoch %d validation loss: %.3f' % (epoch + 1, val_loss / len(val_loader)))

print('Finished training the 3D CNN model')

I get the following error

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in <cell line: 40>() 38 39 # Convert the data to PyTorch tensors and create a DataLoader ---> 40 train_data = TensorDataset(torch.Tensor(x_train), torch.Tensor(y_train)) 41 train_loader = DataLoader(train_data, batch_size=16, shuffle=True) 42 val_data = TensorDataset(torch.Tensor(x_val), torch.Tensor(y_val))

TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

I would really like to know how I can use optical flow data for input to a 3dcnn model training.

Mr.J
  • 181
  • 1
  • 10

0 Answers0