- X_train (98, 1, 40, 844)
- X_val (21, 1, 40, 844)
- X_test (21, 1, 40, 844)
Код: Выделить всё
# Create a DataLoader for the validation set
valid_dl = learn.dls.test_dl(X_val, y_val)
# Get predictions and interpret them on the validation set
interp = ClassificationInterpretation.from_learner(learn, dl=valid_dl)
Код: Выделить всё
RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x2110 and 67520x128)Код: Выделить всё
from fastai.vision.all import *
import librosa
import numpy as np
from sklearn.model_selection import train_test_split
import torch
import torch.nn as nn
from torchsummary import summary
[...] #labels in y can be [0,1,2,3]
# Split the data
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.3, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)
# Reshape data for CNN input (add channel dimension)
X_train = X_train[:, np.newaxis, :, :]
X_val = X_val[:, np.newaxis, :, :]
X_test = X_test[:, np.newaxis, :, :]
#X_train.shape, X_val.shape, X_test.shape
#((98, 1, 40, 844), (21, 1, 40, 844), (21, 1, 40, 844))
class DraftCNN(nn.Module):
def __init__(self):
super(DraftCNN, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
# Calculate flattened size based on input dimensions
with torch.no_grad():
dummy_input = torch.zeros(1, 1, 40, 844) # shape of one input sample
dummy_output = self.pool(self.conv2(self.pool(F.relu(self.conv1(dummy_input)))))
self.flattened_size = dummy_output.view(dummy_output.size(0), -1).size(1)
self.fc1 = nn.Linear(self.flattened_size, 128)
self.fc2 = nn.Linear(128, 4)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(x.size(0), -1) # Flatten the output of convolutions
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
# Initialize the model and the Learner
model = AudioCNN()
learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=[accuracy, Precision(average='macro'), Recall(average='macro'), F1Score(average='macro')])
# Train the model
learn.fit_one_cycle(8)
print(summary(model, (1, 40, 844)))
# Create a DataLoader for the validation set
valid_dl = learn.dls.test_dl(X_val, y_val)
# Get predictions and interpret them on the validation set
interp = ClassificationInterpretation.from_learner(learn, dl=valid_dl)
interp.plot_confusion_matrix()
interp.plot_top_losses(5)
Изменить. По запросу я добавил больше кода.
Подробнее здесь: https://stackoverflow.com/questions/786 ... multiplied