You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torchvision.datasets import CIFAR10
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
from torch.optim import Adam
from torch.autograd import Variable
import torch.nn.functional as F
data = np.load('E:\Bawar Research\FaultNet-main\FaultNet-main\CWRU files\featurized_data.npy')
labels = np.load('E:\Bawar Research\FaultNet-main\FaultNet-main\CWRU files\featurized_data_labels.npy')
x=data[:,0:1600]
def mean(data,no_elements):
X=np.zeros((data.shape[0],data.shape[1]))
for i in range(data.shape[1]-no_elements+1):
X[:,i]=np.mean(data[:,i:i+no_elements],axis=1)
return X.astype(np.float16)
def median(data,no_elements):
X=np.zeros((data.shape[0],data.shape[1]))
for i in range(data.shape[1]-no_elements+1):
X[:,i]=np.median(data[:,i:i+no_elements],axis=1)
return X.astype(np.float16)
def sig_image(data,size):
X=np.zeros((data.shape[0],size,size))
for i in range(data.shape[0]):
X[i]=(data[i,:].reshape(size,size))
return X.astype(np.float16)
channel_mean=(mean(x,10)).astype(np.float16)
x_m=sig_image(channel_mean,40)
channel_median=(median(x,10)).astype(np.float16)
x_md=sig_image(x,40)
x_n=sig_image(x,40)
x_n.shape
x_m.shape
X=np.stack((x_n,x_m,x_md),axis=1).astype(np.float16)
X.shape
from sklearn.model_selection import train_test_split
trainx, testx, trainlabel, testlabel = train_test_split(X, labels, test_size=0.2, random_state=20)
sig_train, sig_test = trainx,testx
lab_train, lab_test = trainlabel,testlabel
sig_train = torch.from_numpy(sig_train)
sig_test = torch.from_numpy(sig_test)
lab_train= torch.from_numpy(lab_train)
lab_test = torch.from_numpy(lab_test)
import torch.utils.data as data_utils
batch_size = 128
train_tensor = data_utils.TensorDataset(sig_train, lab_train)
train_loader = data_utils.DataLoader(dataset = train_tensor, batch_size = batch_size, shuffle = True)
batch_size = 1024
test_tensor = data_utils.TensorDataset(sig_test, lab_test)
test_loader = data_utils.DataLoader(dataset = test_tensor, batch_size = batch_size, shuffle = False)
sig_train.size()
sig_test.size()
class CNN(nn.Module):
def init(self):
super(CNN, self).init()
self.conv1 = nn.Conv2d(3, 32, kernel_size=4,stride=1,padding = 1)
self.mp1 = nn.MaxPool2d(kernel_size=4,stride=2)
self.conv2 = nn.Conv2d(32,64, kernel_size=4,stride =1)
self.mp2 = nn.MaxPool2d(kernel_size=4,stride=2)
self.fc1= nn.Linear(2304,256)
self.dp1 = nn.Dropout(p=0.2)
self.fc2 = nn.Linear(256,10)
def forward(self, x):
in_size = x.size(0)
x = F.relu(self.mp1(self.conv1(x)))
x = F.relu(self.mp2(self.conv2(x)))
x = x.view(in_size,-1)
x = F.relu(self.fc1(x))
x = self.dp1(x)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
cnn = CNN().double()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=0.001)
num_epochs = 100
total_step = len(train_loader)
loss_list = []
acc_list = []
for epoch in range(num_epochs):
for i, (signals, labels) in enumerate(train_loader):
optimizer.zero_grad()
# Run the forward pass
signals=signals
labels=labels
outputs = cnn(signals.double())
loss = criterion(outputs, labels.long())
loss_list.append(loss.item())
# Backprop and perform Adam optimisation
loss.backward()
optimizer.step()
# Track the accuracy
total = labels.size(0)
_, predicted = torch.max(outputs.data, 1)
correct = (predicted == labels.long()).sum().item()
acc_list.append(correct / total)
if (epoch+1) % 5 == 0 or epoch==0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Train Accuracy: {:.2f}%'
.format(epoch + 1, num_epochs, i + 1, total_step, loss.item(),
(correct / total) * 100))
total_step = len(test_loader)
print(total_step)
loss_list_test = []
acc_list_test = []
with torch.no_grad():
for i, (signals, labels) in enumerate(test_loader):
# Run the forward pass
signals=signals
labels=labels
outputs = cnn(signals.double())
loss = criterion(outputs, labels.long())
loss_list_test.append(loss.item())
if epoch%10 ==0:
print(loss)
total = labels.size(0)
_, predicted = torch.max(outputs.data, 1)
correct = (predicted == labels.long()).sum().item()
acc_list_test.append(correct / total)
if (epoch) % 1 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'
.format(epoch + 1, num_epochs, i + 1, total_step, loss.item(),
(correct / total) * 100))
if you need to save
torch.save(cnn,'cnnTC3_fold3_45.pth')
This code has serious error about the reshape array of size 20 into shape of (40,40)
Here is the error
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torchvision.datasets import CIFAR10
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
from torch.optim import Adam
from torch.autograd import Variable
import torch.nn.functional as F
data = np.load('E:\Bawar Research\FaultNet-main\FaultNet-main\CWRU files\featurized_data.npy')
labels = np.load('E:\Bawar Research\FaultNet-main\FaultNet-main\CWRU files\featurized_data_labels.npy')
x=data[:,0:1600]
def mean(data,no_elements):
X=np.zeros((data.shape[0],data.shape[1]))
for i in range(data.shape[1]-no_elements+1):
X[:,i]=np.mean(data[:,i:i+no_elements],axis=1)
return X.astype(np.float16)
def median(data,no_elements):
X=np.zeros((data.shape[0],data.shape[1]))
for i in range(data.shape[1]-no_elements+1):
X[:,i]=np.median(data[:,i:i+no_elements],axis=1)
return X.astype(np.float16)
def sig_image(data,size):
X=np.zeros((data.shape[0],size,size))
for i in range(data.shape[0]):
X[i]=(data[i,:].reshape(size,size))
return X.astype(np.float16)
channel_mean=(mean(x,10)).astype(np.float16)
x_m=sig_image(channel_mean,40)
channel_median=(median(x,10)).astype(np.float16)
x_md=sig_image(x,40)
x_n=sig_image(x,40)
x_n.shape
x_m.shape
X=np.stack((x_n,x_m,x_md),axis=1).astype(np.float16)
X.shape
from sklearn.model_selection import train_test_split
trainx, testx, trainlabel, testlabel = train_test_split(X, labels, test_size=0.2, random_state=20)
sig_train, sig_test = trainx,testx
lab_train, lab_test = trainlabel,testlabel
sig_train = torch.from_numpy(sig_train)
sig_test = torch.from_numpy(sig_test)
lab_train= torch.from_numpy(lab_train)
lab_test = torch.from_numpy(lab_test)
import torch.utils.data as data_utils
batch_size = 128
train_tensor = data_utils.TensorDataset(sig_train, lab_train)
train_loader = data_utils.DataLoader(dataset = train_tensor, batch_size = batch_size, shuffle = True)
batch_size = 1024
test_tensor = data_utils.TensorDataset(sig_test, lab_test)
test_loader = data_utils.DataLoader(dataset = test_tensor, batch_size = batch_size, shuffle = False)
sig_train.size()
sig_test.size()
class CNN(nn.Module):
def init(self):
super(CNN, self).init()
self.conv1 = nn.Conv2d(3, 32, kernel_size=4,stride=1,padding = 1)
self.mp1 = nn.MaxPool2d(kernel_size=4,stride=2)
self.conv2 = nn.Conv2d(32,64, kernel_size=4,stride =1)
self.mp2 = nn.MaxPool2d(kernel_size=4,stride=2)
self.fc1= nn.Linear(2304,256)
self.dp1 = nn.Dropout(p=0.2)
self.fc2 = nn.Linear(256,10)
cnn = CNN().double()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=0.001)
num_epochs = 100
total_step = len(train_loader)
loss_list = []
acc_list = []
for epoch in range(num_epochs):
for i, (signals, labels) in enumerate(train_loader):
optimizer.zero_grad()
# Run the forward pass
signals=signals
labels=labels
outputs = cnn(signals.double())
loss = criterion(outputs, labels.long())
total_step = len(test_loader)
print(total_step)
loss_list_test = []
acc_list_test = []
with torch.no_grad():
for i, (signals, labels) in enumerate(test_loader):
# Run the forward pass
signals=signals
labels=labels
outputs = cnn(signals.double())
loss = criterion(outputs, labels.long())
loss_list_test.append(loss.item())
if epoch%10 ==0:
print(loss)
total = labels.size(0)
_, predicted = torch.max(outputs.data, 1)
correct = (predicted == labels.long()).sum().item()
acc_list_test.append(correct / total)
if (epoch) % 1 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'
.format(epoch + 1, num_epochs, i + 1, total_step, loss.item(),
(correct / total) * 100))
if you need to save
torch.save(cnn,'cnnTC3_fold3_45.pth')
This code has serious error about the reshape array of size 20 into shape of (40,40)
Here is the error
ValueError Traceback (most recent call last)
Cell In[52], line 2
1 channel_mean=(mean(x,10)).astype(np.float16)
----> 2 x_m=sig_image(channel_mean,40)
3 channel_median=(median(x,10)).astype(np.float16)
4 x_md = sig_image(channel_median, 40)
Cell In[51], line 21
19 for i in range(data.shape[0]):
20 if data[i, :].size != expected_size:
---> 21 raise ValueError(f"Cannot reshape array of size {data[i, :].size} into shape ({size},{size})")
22 X[i] = (data[i, :].reshape(size, size))
23 return X.astype(np.float16)
ValueError: Cannot reshape array of size 20 into shape (40,40)
The text was updated successfully, but these errors were encountered: