<jupyter_start><jupyter_text>PyTorch & Plant Seedling Classification Tutorial<jupyter_code>import numpy as np
import pandas as pd

import torch
import torch.nn as nn
import torch.nn.functional as F

from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Data Extraction & Preprocessing<jupyter_code>df = pd.read_csv('csv/data.csv', sep=';', index_col=0)
df.head()<jupyter_output><empty_output><jupyter_text>---<jupyter_code>x = df.drop('label', axis=1)
y = df.label
x.head()
y.head()<jupyter_output><empty_output><jupyter_text>---<jupyter_code>l_encoder = LabelEncoder()
y = l_encoder.fit_transform(y)
x = np.array(x).reshape(-1, 32, 32, 3)
x = np.transpose(x, (0, 3, 1, 2))<jupyter_output><empty_output><jupyter_text>---<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(x, y,
                                                    random_state=42,
                                                    test_size=0.2)<jupyter_output><empty_output><jupyter_text>---<jupyter_code>x_train = torch.FloatTensor(x_train)
x_test = torch.FloatTensor(x_test)
y_train = torch.LongTensor(y_train)
y_test = torch.LongTensor(y_test)<jupyter_output><empty_output><jupyter_text>Net parameters<jupyter_code>EPOCHES = 50
BATCH_SIZE = 32
NUM_CLASSES = 12<jupyter_output><empty_output><jupyter_text>Batch Retrieval<jupyter_code>def get_batch(x, y=None, batch_size=None):
    if batch_size is None:
        batch_size = BATCH_SIZE
    
    selected = np.random.randint(0, x.shape[0], batch_size)
    
    if y is None:
        return x[selected]
    else:
        return x[selected], y[selected]<jupyter_output><empty_output><jupyter_text>Net Architecture<jupyter_code>class MainNet(nn.Module):
    def __init__(self):
        super(MainNet, self).__init__()
        self.conv_1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3)
        self.conv_2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3)
        self.conv_3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3)
        self.pool_1 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.conv_4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3)
        self.pool_2 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.dropout_1 = nn.Dropout2d(p=0.3)
        self.conv_5 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3)
        self.dense_1 = nn.Linear(3 * 3 * 512, 1024)
        self.dense_2 = nn.Linear(1024, 256)
        self.dense_3 = nn.Linear(256, NUM_CLASSES)

    def forward(self, x):
        x = F.relu(self.conv_1(x))
        x = F.relu(self.conv_2(x))
        x = self.pool_1(F.relu(self.conv_3(x)))
        x = self.pool_2(F.relu(self.conv_4(x)))
        x = F.relu(self.dropout_1(x))
        x = F.relu(self.conv_5(x))
        x = x.view(-1, 3 * 3 * 512)
        x = F.relu(self.dense_1(x))
        x = F.relu(self.dense_2(x))
        x = F.dropout(x, training=self.training)
        x = self.dense_3(x)
        return x<jupyter_output><empty_output><jupyter_text>---<jupyter_code>net = MainNet()

loss_fn = torch.nn.CrossEntropyLoss()

learning_rate = 1e-4
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)<jupyter_output><empty_output><jupyter_text>Net Training<jupyter_code>for epoch in range(EPOCHES):
    total_loss = .0
    for i in range(x_train.shape[0] // BATCH_SIZE):
        x_batch, y_batch = get_batch(x_train, y_train, BATCH_SIZE)
        optimizer.zero_grad()
        y_pred = net(x_batch)
        loss = loss_fn(y_pred, y_batch)
        loss.backward()
        optimizer.step()
        total_loss += loss.item()
    if epoch % 5 == 4 or epoch == 0:
        print('EPOCH\t {0:3d}\t LOSS: {1:.3f}'.format(epoch + 1, total_loss / 100))<jupyter_output>EPOCH	   1	 LOSS: 3.062
EPOCH	   5	 LOSS: 1.353
EPOCH	  10	 LOSS: 0.714
EPOCH	  15	 LOSS: 0.443
EPOCH	  20	 LOSS: 0.273
EPOCH	  25	 LOSS: 0.196
EPOCH	  30	 LOSS: 0.116
EPOCH	  35	 LOSS: 0.098
EPOCH	  40	 LOSS: 0.066
EPOCH	  45	 LOSS: 0.052
EPOCH	  50	 LOSS: 0.034<jupyter_text>Net Accuracy<jupyter_code>def total_accuracy(net, x_test, y_test):
    correct = 0
    
    with torch.no_grad():
        for i in range(len(x_test)):
            logits = net(x_test[i: i + 1])
            _, pred = torch.max(logits, 1)
            if pred == y_test[i]:
                correct += 1

    print('TOTAL ACCURACY OF THE NET\n\t{0:.3f}'.format(correct / x_test.shape[0]))
def class_accuracy(net, x_test, y_test):
    correct = list(0 for i in range(NUM_CLASSES))
    total = list(0 for i in range(NUM_CLASSES))

    with torch.no_grad():
        for i in range(len(x_test)):
            logits = net(x_test[i: i + 1])
            _, pred = torch.max(logits, 1)
            mask = (pred == y_test[i])
            correct[y_test[i]] += mask.item()
            total[y_test[i]] += 1

    print('CLASSWISE ACCURACY\n')
    for i in range(NUM_CLASSES):
        print('\tACCURACY OF CLASS {0:2d}: {1:.3f}%'.format(i, 100 * correct[i] / total[i]))<jupyter_output><empty_output><jupyter_text>---<jupyter_code>total_accuracy(net, x_test, y_test)
class_accuracy(net, x_test, y_test)<jupyter_output>CLASSWISE ACCURACY

	ACCURACY OF CLASS  0: 26.027%
	ACCURACY OF CLASS  1: 91.000%
	ACCURACY OF CLASS  2: 78.462%
	ACCURACY OF CLASS  3: 91.391%
	ACCURACY OF CLASS  4: 56.410%
	ACCURACY OF CLASS  5: 82.524%
	ACCURACY OF CLASS  6: 89.437%
	ACCURACY OF CLASS  7: 67.442%
	ACCURACY OF CLASS  8: 85.586%
	ACCURACY OF CLASS  9: 74.242%
	ACCURACY OF CLASS 10: 90.517%
	ACCURACY OF CLASS 11: 74.737%<jupyter_text>Saving the Net<jupyter_code>torch.save(net.state_dict(), 'MainNet')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13268551236749115, 'fraction_numerical': 0.05053003533568905, 'mean_word_length': 3.298405466970387, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '90868', 'license': 'MIT', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2254, 'n_words': 416, 'path': 'Seminars/PCR.ipynb', 'repo_name': 'svinkapeppa/Sirius_2018', 'size': '5660'}