All technological notes.
digital image
Convolutional Neural Network
Image resolution
h * w * d:
Layers:


convolution layers along with pooling,fully connected layers,Soft-max function to classify an object with probabilistic values 0 and 1.Convolution layer
learnable filters and convolutional operations.learnable parameters/kernel, and the other matrix is the restricted portion of the receptive field.image matrix is h×w×d.filter is fh×fw×d.(h-fh+1)×(w-fw+1)×1.Features Map:

learnable parameters/kernel
RGB) channels, the kernel height and width will be spatially small, but the depth extends up to all three channels.used to collect neightbore region info
Stride
Convolutional layer slides.1, then we move the filters to 1 pixel at a time and similarly,
- Example: filter/kernel = 3*3; stride = 2.
Padding

NONLINEAR LAYER
Common activation functions used as nonlinear layers include:
Pooling layer
Purpose:
Spatial pooling / downsampling / subsampling

Max pooling


Average Pooling
Sum Pooling:
fully connected layer





https://www.geeksforgeeks.org/implementation-of-a-cnn-based-image-classifier-using-pytorch/
import torch
import matplotlib.pyplot as plt
import numpy as np
import torch.nn.functional as func
import PIL.ImageOps
from torch import nn
from torchvision import datasets, transforms
import requests
from PIL import Image
# Prepare Dataset
transform1 = transforms.Compose([transforms.Resize(
(28, 28)), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
training_dataset = datasets.MNIST(
root='./data', train=True, download=True, transform=transform1)
validation_dataset = datasets.MNIST(
root='./data', train=False, download=True, transform=transform1)
training_loader = torch.utils.data.DataLoader(
dataset=training_dataset, batch_size=100, shuffle=True)
validation_loader = torch.utils.data.DataLoader(
dataset=validation_dataset, batch_size=100, shuffle=False)
# def im_convert(tensor):
# image = tensor.clone().detach().numpy()
# image = image.transpose(1, 2, 0)
# print(image.shape)
# image = image*(np.array((0.5, 0.5, 0.5))+np.array((0.5, 0.5, 0.5)))
# image = image.clip(0, 1)
# return image
# dataiter = iter(training_loader)
# images, labels = dataiter.next()
# fig = plt.figure(figsize=(25, 4))
# for idx in np.arange(20):
# ax = fig.add_subplot(2, 10, idx+1)
# plt.imshow(im_convert(images[idx]))
# ax.set_title([labels[idx].item()])
# class classification1(nn.Module):
# def __init__(self, input_layer, hidden_layer1, hidden_layer2, output_layer):
# super().__init__()
# self.linear1 = nn.Linear(input_layer, hidden_layer1)
# self.linear2 = nn.Linear(hidden_layer1, hidden_layer2)
# self.linear3 = nn.Linear(hidden_layer2, output_layer)
# def forward(self, x):
# x = func.relu(self.linear1(x))
# x = func.relu(self.linear2(x))
# x = self.linear3(x)
# return x
# model = classification1(784, 125, 65, 10)
# criteron = nn.CrossEntropyLoss()
# optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
# epochs = 12
# loss_history = []
# correct_history = []
# val_loss_history = []
# val_correct_history = []
# for e in range(epochs):
# loss = 0.0
# correct = 0.0
# val_loss = 0.0
# val_correct = 0.0
# for input, labels in training_loader:
# inputs = input.view(input.shape[0], -1)
# outputs = model(inputs)
# loss1 = criteron(outputs, labels)
# optimizer.zero_grad()
# loss1.backward()
# optimizer.step()
# _, preds = torch.max(outputs, 1)
# loss += loss1.item()
# correct += torch.sum(preds == labels.data)
# else:
# with torch.no_grad():
# for val_input, val_labels in validation_loader:
# val_inputs = val_input.view(val_input.shape[0], -1)
# val_outputs = model(val_inputs)
# val_loss1 = criteron(val_outputs, val_labels)
# _, val_preds = torch.max(val_outputs, 1)
# val_loss += val_loss1.item()
# val_correct += torch.sum(val_preds == val_labels.data)
# epoch_loss = loss/len(training_loader.dataset)
# epoch_acc = correct.float()/len(training_dataset)
# loss_history.append(epoch_loss)
# correct_history.append(epoch_acc)
# val_epoch_loss = val_loss/len(validation_loader.dataset)
# val_epoch_acc = val_correct.float()/len(validation_dataset)
# val_loss_history.append(val_epoch_loss)
# val_correct_history.append(val_epoch_acc)
# print('training_loss:{:.4f},{:.4f}'.format(
# epoch_loss, epoch_acc.item()))
# print('validation_loss:{:.4f},{:.4f}'.format(
# val_epoch_loss, val_epoch_acc.item()))
# url = 'https://images.homedepot-static.com/productImages/007164ea-d47e-4f66-8d8c-fd9f621984a2/svn/architectural-mailboxes-house-letters-numbers-3585b-5-64_1000.jpg'
# response = requests.get(url, stream=True)
# img = Image.open(response.raw)
# img = PIL.ImageOps.invert(img)
# img = img.convert('1')
# img = transform1(img)
# plt.imshow(im_convert(img))
# img = img.view(img.shape[0], -1)
# output = model(img)
# _, pred = torch.max(output, 1)
# print(pred.item())
# dataiter = iter(validation_loader)
# images, labels = dataiter.next()
# images_ = images.view(images.shape[0], -1)
# output = model(images_)
# _, preds = torch.max(output, 1)
# fig = plt.figure(figsize=(25, 4))
# for idx in np.arange(20):
# ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[])
# plt.imshow(im_convert(images[idx]))
# ax.set_title("{}({})".format(str(preds[idx].item()), str(
# labels[idx].item())), color=("green" if preds[idx] == labels[idx] else "red"))
# plt.show()