0% found this document useful (0 votes)
17 views5 pages

BIA9

Model cod Python

Uploaded by

Daniel Gyorfi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

BIA9

Model cod Python

Uploaded by

Daniel Gyorfi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#Problema 1

#Perform the cross-correlation operation between an input sensor with 3 input


channels and the following kernel:
#[[0,1],[2,3]],[[1,2],[3,4]],[[7,2],[3,5]].

import numpy as np

# Define the input sensor and the kernel


input_sensor = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
[[19, 20, 21], [22, 23, 24], [25, 26, 27]]])
kernel = np.array([[[0, 1], [2, 3]],
[[1, 2], [3, 4]],
[[7, 2], [3, 5]]])

# Flip the kernel along both axes


kernel_flipped = np.flip(np.flip(kernel, axis=0), axis=1)

# Perform the cross-correlation operation


output = np.zeros((input_sensor.shape[0] - kernel.shape[0] + 1,
input_sensor.shape[1] - kernel.shape[1] + 1, 1))
for i in range(output.shape[0]):
for j in range(output.shape[1]):
input_patch = input_sensor[i:i+kernel.shape[0], j:j+kernel.shape[1], :]
output[i, j, 0] = np.sum(input_patch * kernel_flipped)

print(output)

#We perform a cross-correlation operation on a given input sensor and a kernel


using nested loops.
#First, the input sensor and kernel are defined as multi-dimensional numpy arrays.
The kernel is then flipped along both axes using the np.flip() function.
#Next, a new numpy array called output is initialized with zeros, with dimensions
calculated based on the input sensor and kernel shapes. The nested for loops
iterate through #each pixel in the output array and extract a patch of the input
sensor that matches the shape of the kernel. The dot product between the input
patch and the flipped kernel is then #computed using the np.sum() function, and the
resulting scalar value is assigned to the corresponding pixel in the output array.
#Finally, the output array is printed to the console, representing the result of
the cross-correlation operation.

#Problema 2

#Classify the SVHN(Street View House Numbers) dataset(32x32 images,10 classes,73257


training images and 26032 testing images) using the LeNet model.Divide the training
#dataset as follows:30000 validation images and 43257 training images.Set the
learning rate to 0.5.

import tensorflow as tf
from tensorflow.keras.datasets import svhn
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Preprocess the dataset


(x_train, y_train), (x_test, y_test) = svhn.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train, y_test = tf.keras.utils.to_categorical(y_train, num_classes=10),
tf.keras.utils.to_categorical(y_test, num_classes=10)

# Split the training dataset into validation and training sets


x_val, y_val = x_train[:30000], y_train[:30000]
x_train, y_train = x_train[30000:], y_train[30000:]

# Define the LeNet model architecture


model = Sequential([
Conv2D(6, kernel_size=(5, 5), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(16, kernel_size=(5, 5), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(120, activation='relu'),
Dense(84, activation='relu'),
Dense(10, activation='softmax')
])

# Compile the model


model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.5),
loss='categorical_crossentropy',
metrics=['accuracy'])

# Train the model


model.fit(x_train, y_train, epochs=10, batch_size=32, validation_data=(x_val,
y_val))

# Evaluate the model on the test dataset


test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('Test accuracy:', test_acc)

#We load the dataset using svhn.load_data() function from the TensorFlow Keras
library. The input images are normalized by dividing their pixel values by 255.0,
and the labels #are one-hot encoded using the tf.keras.utils.to_categorical()
function.
#The training dataset is then split into training and validation sets, with the
first 30,000 images used for validation and the remaining images used for training.
#The CNN model architecture is defined using the Sequential API of TensorFlow
Keras. It consists of two convolutional layers with max pooling, followed by two
fully connected #layers and an output softmax layer.
#The model is compiled using stochastic gradient descent (SGD) optimizer with a
learning rate of 0.5, and the categorical cross-entropy loss function. The model is
then trained #for 10 epochs on the training data with a batch size of 32, and the
validation data is used to monitor the training progress.
#Finally, the trained model is evaluated on the test dataset using the
model.evaluate() function, which returns the test loss and accuracy. The test
accuracy is printed to the console.
#Note that the LeNet model architecture consists of two convolutional layers
followed by max pooling, and three fully connected layers. The learning rate is set
to 0.5, and the #model is trained for 10 epochs with a batch size of 32. Finally,
the model is evaluated on the test dataset to obtain the accuracy.

#Problema 3
#Classify the SVHN dataset using the following convolutional neural network
architecture.Divide the training dataset as follows:30000 validation images and
43257 training #images.Train your model for 5 epochs using a learning rate of 0.05
and a batch size of 256.Define a class for implementing the Convolutional
Block.Conv2D,32,3x3 means 32 #output channels and 3x3 kernel size.Use padding,if
necessary.
#The convolutional block is described like that:
#-input->Conv2d,64,3x3 ReLU
->
#
Add->Flatten->Linear,10
# ->Conv2d,32,3x3->Conv2d,64,3x3,BatchNorm2d ReLU ->
#Generate a Python code for this request.

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import SVHN
from torchvision.transforms import ToTensor

# Define the Convolutional Block


class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3,
padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.relu2 = nn.ReLU(inplace=True)

def forward(self, x):


identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.bn2(out)
out += identity
out = self.relu2(out)
return out

# Define the Convolutional Neural Network


class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.block1 = ConvBlock(32, 64)
self.block2 = ConvBlock(64, 64)
self.block3 = ConvBlock(64, 64)
self.conv2 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.relu2 = nn.ReLU(inplace=True)
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(64 * 8 * 8, 10)
def forward(self, x):
out = self.conv1(x)
out = self.block1(out)
out = self.block2(out)
out = self.block3(out)
out = self.conv2(out)
out = self.bn2(out)
out += self.conv1(x) # skip connection
out = self.relu2(out)
out = self.flatten(out)
out = self.fc1(out)
return out

# Load the SVHN dataset


train_dataset = SVHN(root='./data', split='train', transform=ToTensor(),
download=True)
val_dataset = SVHN(root='./data', split='test', transform=ToTensor(),
download=True)

# Divide the training dataset into training and validation datasets


train_dataset, val_dataset = torch.utils.data.random_split(train_dataset, [43257,
30000])

# Create data loaders


train_loader = DataLoader(train_dataset, batch_size=256, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=256, shuffle=False)

# Define the device


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Initialize the model and move it to the device


model = CNN().to(device)

# Define the loss function and optimizer


criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.05)

# Train the model


for epoch in range(5):
running_loss = 0.0
for i, data in enumerate(train_loader):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)

optimizer.zero_grad()

outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()

running_loss += loss.item()

if i % 50 == 49: # print every 50 mini-batches


print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 50))
running_loss = 0.0

# Evaluate the model on the validation dataset


correct = 0
total = 0
with torch.no_grad():
for data in val_loader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print('Accuracy of the network on the validation images: %d %%' % (100 *


correct / total))

print('Finished Training')

#We train the convolutional neural network on the SVHN dataset for 5 epochs with a
learning rate of 0.05 and a batch size of 256. The training dataset is divided into
training and #validation datasets with 43,257 and 30,000 images, respectively. The
ConvBlock class implements a convolutional block with skip connections, and the CNN
class implements #the full convolutional neural network with the specified
architecture. The train_loader and val_loader data loaders are used to load the
training and validation datasets, #respectively. The criterion and optimizer are
used to define the loss function and optimizer, respectively. The for loop trains
the model for 5 epochs, and the with torch.no_grad() #block evaluates the model on
the validation dataset. The final line of the code indicates that the training has
finished.

You might also like