Two circles demo¶
This demo is using the two circles datset from Scikit-learn. It is made for showing some SSL methods on some simple data with visual comprehension. Those experiments are made with SemiPy.
In [4]:
Copied!
from sklearn.datasets import make_circles, make_moons
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import matplotlib.colors as cl
import seaborn as sns
import numpy as np
import math
import torch
from torch.utils.data import TensorDataset, DataLoader
from torch import nn
import torch.nn.functional as F
import random
import semipy
from sklearn.datasets import make_circles, make_moons
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import matplotlib.colors as cl
import seaborn as sns
import numpy as np
import math
import torch
from torch.utils.data import TensorDataset, DataLoader
from torch import nn
import torch.nn.functional as F
import random
import semipy
Data acquisition¶
In [5]:
Copied!
X, y = make_circles(n_samples=2000, noise=0.07)
X, y = make_circles(n_samples=2000, noise=0.07)
In [6]:
Copied!
X_train, X_val, y_train, y_val = train_test_split(X, y)
X_train, X_val, y_train, y_val = train_test_split(X, y)
In [7]:
Copied!
indices = list(range(len(y_train)))
unlabelled_indices = random.sample(indices, int(0.98*len(y_train)))
y_train[unlabelled_indices] = -1
indices = list(range(len(y_train)))
unlabelled_indices = random.sample(indices, int(0.98*len(y_train)))
y_train[unlabelled_indices] = -1
Complete case¶
In [8]:
Copied!
# Getting only labelled items
labelled_indices = list(set(indices)-set(unlabelled_indices))
X_train_cc = X_train[labelled_indices]
y_train_cc = y_train[labelled_indices]
# Converting to tensors
X_train_cc = torch.from_numpy(X_train_cc).to(torch.float32)
y_train_cc = torch.from_numpy(y_train_cc).to(torch.int64)
X_val_cc = torch.from_numpy(X_val).to(torch.float32)
y_val_cc = torch.from_numpy(y_val).to(torch.int64)
# Getting only labelled items
labelled_indices = list(set(indices)-set(unlabelled_indices))
X_train_cc = X_train[labelled_indices]
y_train_cc = y_train[labelled_indices]
# Converting to tensors
X_train_cc = torch.from_numpy(X_train_cc).to(torch.float32)
y_train_cc = torch.from_numpy(y_train_cc).to(torch.int64)
X_val_cc = torch.from_numpy(X_val).to(torch.float32)
y_val_cc = torch.from_numpy(y_val).to(torch.int64)
In [9]:
Copied!
plt.figure(figsize=(10, 10))
plt.title("Dataset")
plt.scatter(X_train_cc[:, 0], X_train_cc[:, 1], c=y_train_cc, cmap=cl.ListedColormap(['orange', 'blue']))
plt.show()
plt.figure(figsize=(10, 10))
plt.title("Dataset")
plt.scatter(X_train_cc[:, 0], X_train_cc[:, 1], c=y_train_cc, cmap=cl.ListedColormap(['orange', 'blue']))
plt.show()
In [10]:
Copied!
# Creating datasets
train_dataset = TensorDataset(X_train_cc, y_train_cc)
val_dataset = TensorDataset(X_val_cc, y_val_cc)
# Creating dataloaders
train_dataloader = DataLoader(train_dataset, batch_size=16)
val_dataloader = [DataLoader(val_dataset, batch_size=16)]
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
# Using semipy's PseudoLabel class to use PseudoLabel method
# As we only have labelled items in the dataloaders, the complete will be computed instead
# of real PseudoLabel
method = semipy.methods.CompleteCase(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
# Creating datasets
train_dataset = TensorDataset(X_train_cc, y_train_cc)
val_dataset = TensorDataset(X_val_cc, y_val_cc)
# Creating dataloaders
train_dataloader = DataLoader(train_dataset, batch_size=16)
val_dataloader = [DataLoader(val_dataset, batch_size=16)]
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
# Using semipy's PseudoLabel class to use PseudoLabel method
# As we only have labelled items in the dataloaders, the complete will be computed instead
# of real PseudoLabel
method = semipy.methods.CompleteCase(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
0%| | 0/100 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
In [11]:
Copied!
def show_separation(model, save=False, name_to_save=""):
sns.set(style="white")
model = model.to('cpu')
xx, yy = np.mgrid[-1.5:1.5:.01, -1.5:1.5:.01]
grid = np.c_[xx.ravel(), yy.ravel()]
batch = torch.from_numpy(grid).type(torch.float32)
with torch.no_grad():
probs, indices = torch.max(torch.softmax(model(batch), dim=1), dim=1)
for i, (prob, index) in enumerate(zip(probs, indices)):
if index.item() == 0:
probs[i] = torch.tensor(1-prob.item())
probs = probs.numpy().reshape(xx.shape)
f, ax = plt.subplots(figsize=(12, 10))
ax.set_title("Decision boundary", fontsize=14)
contour = ax.contourf(xx, yy, probs, 15, cmap="RdBu",
vmin=0, vmax=1)
ax_c = f.colorbar(contour)
ax_c.set_label("$P(y = 1)$")
#ax_c.set_ticks([0, .25, .5, .75, 1])
ax.scatter(X[100:,0], X[100:, 1], c=y[100:], s=40,
cmap="RdBu", vmin=-.2, vmax=1.2,
edgecolor="white", linewidth=1)
ax.set(xlabel="$X_1$", ylabel="$X_2$")
if save:
plt.savefig(name_to_save)
else:
plt.show()
def show_separation(model, save=False, name_to_save=""):
sns.set(style="white")
model = model.to('cpu')
xx, yy = np.mgrid[-1.5:1.5:.01, -1.5:1.5:.01]
grid = np.c_[xx.ravel(), yy.ravel()]
batch = torch.from_numpy(grid).type(torch.float32)
with torch.no_grad():
probs, indices = torch.max(torch.softmax(model(batch), dim=1), dim=1)
for i, (prob, index) in enumerate(zip(probs, indices)):
if index.item() == 0:
probs[i] = torch.tensor(1-prob.item())
probs = probs.numpy().reshape(xx.shape)
f, ax = plt.subplots(figsize=(12, 10))
ax.set_title("Decision boundary", fontsize=14)
contour = ax.contourf(xx, yy, probs, 15, cmap="RdBu",
vmin=0, vmax=1)
ax_c = f.colorbar(contour)
ax_c.set_label("$P(y = 1)$")
#ax_c.set_ticks([0, .25, .5, .75, 1])
ax.scatter(X[100:,0], X[100:, 1], c=y[100:], s=40,
cmap="RdBu", vmin=-.2, vmax=1.2,
edgecolor="white", linewidth=1)
ax.set(xlabel="$X_1$", ylabel="$X_2$")
if save:
plt.savefig(name_to_save)
else:
plt.show()
In [12]:
Copied!
plt.plot(range(len(results)), results)
plt.plot(range(len(results)), results)
Out[12]:
[<matplotlib.lines.Line2D at 0x23817f74bb0>]
In [13]:
Copied!
show_separation(model)
show_separation(model)
PseudoLabel¶
In [14]:
Copied!
# Converting to tensors
X_train_t = torch.from_numpy(X_train).to(torch.float32)
y_train_t = torch.from_numpy(y_train).to(torch.int64)
X_val_t = torch.from_numpy(X_val).to(torch.float32)
y_val_t = torch.from_numpy(y_val).to(torch.int64)
# Converting to tensors
X_train_t = torch.from_numpy(X_train).to(torch.float32)
y_train_t = torch.from_numpy(y_train).to(torch.int64)
X_val_t = torch.from_numpy(X_val).to(torch.float32)
y_val_t = torch.from_numpy(y_val).to(torch.int64)
In [15]:
Copied!
plt.figure(figsize=(10, 10))
plt.title("Dataset")
plt.scatter(X_train_t[:, 0], X_train_t[:, 1], c=y_train, cmap=cl.ListedColormap(['darkgray', 'orange', 'blue']))
plt.show()
plt.figure(figsize=(10, 10))
plt.title("Dataset")
plt.scatter(X_train_t[:, 0], X_train_t[:, 1], c=y_train, cmap=cl.ListedColormap(['darkgray', 'orange', 'blue']))
plt.show()
In [16]:
Copied!
# Creating datasets
train_dataset = TensorDataset(X_train_t, y_train_t)
val_dataset = TensorDataset(X_val_t, y_val_t)
# Creating datasets
train_dataset = TensorDataset(X_train_t, y_train_t)
val_dataset = TensorDataset(X_val_t, y_val_t)
In [17]:
Copied!
# Using custom sampler from semipy
sampler = semipy.sampler.JointSampler(train_dataset, batch_size=32, proportion=0.5)
# Defining dataloaders using the created sampler
train_dataloader = DataLoader(train_dataset, batch_sampler=sampler)
val_dataloader = [DataLoader(val_dataset, batch_size=32)]
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
# Using semipy's PseudoLabel class
method = semipy.methods.PseudoLabel(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
# Using custom sampler from semipy
sampler = semipy.sampler.JointSampler(train_dataset, batch_size=32, proportion=0.5)
# Defining dataloaders using the created sampler
train_dataloader = DataLoader(train_dataset, batch_sampler=sampler)
val_dataloader = [DataLoader(val_dataset, batch_size=32)]
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
# Using semipy's PseudoLabel class
method = semipy.methods.PseudoLabel(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
0%| | 0/100 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
In [18]:
Copied!
plt.plot(range(len(results)), results)
plt.plot(range(len(results)), results)
Out[18]:
[<matplotlib.lines.Line2D at 0x2386a977cd0>]
In [19]:
Copied!
show_separation(model)
show_separation(model)
FixMatch¶
With FixMatch method, we need to define two types of data augmentations : a weak and a strong one. Here we define a rotation of random angle as the weak augmentation, and a gaussian noise as the strong augmentation.
In [20]:
Copied!
class Rotation:
""" Code for rotation transform on 2D points. """
def __call__(self, coord):
angle = random.randint(0, 180)
phi = torch.tensor(angle*math.pi/180)
s = torch.sin(phi)
c = torch.cos(phi)
rotation_matrix = torch.stack([torch.stack([c, -s]),
torch.stack([s, c])])
coord = coord @ rotation_matrix
return coord
class GaussianNoise:
""" Code for adding gaussian noise on 2D points. """
def __init__(self, mean=0., std=1.):
self.mean = mean
self.std = std
def __call__(self, coord):
return coord + torch.randn(coord.size()) * self.std + self.mean
class Rotation:
""" Code for rotation transform on 2D points. """
def __call__(self, coord):
angle = random.randint(0, 180)
phi = torch.tensor(angle*math.pi/180)
s = torch.sin(phi)
c = torch.cos(phi)
rotation_matrix = torch.stack([torch.stack([c, -s]),
torch.stack([s, c])])
coord = coord @ rotation_matrix
return coord
class GaussianNoise:
""" Code for adding gaussian noise on 2D points. """
def __init__(self, mean=0., std=1.):
self.mean = mean
self.std = std
def __call__(self, coord):
return coord + torch.randn(coord.size()) * self.std + self.mean
In [21]:
Copied!
# Using semipy's CustomDataset two apply two sorts of augmentations
aug_train_dataset = semipy.datasets.SSLDataset(train_dataset, weak_transform=Rotation(), strong_transform=GaussianNoise(std=0.01))
# Using semipy's CustomDataset two apply two sorts of augmentations
aug_train_dataset = semipy.datasets.SSLDataset(train_dataset, weak_transform=Rotation(), strong_transform=GaussianNoise(std=0.01))
In [22]:
Copied!
# Using custom sampler from semipy
sampler = semipy.sampler.JointSampler(train_dataset, batch_size=32, proportion=0.5)
# Defining dataloaders using the created sampler
train_dataloader = DataLoader(aug_train_dataset, batch_sampler=sampler)
val_dataloader = [DataLoader(val_dataset, batch_size=32)]
# Using custom sampler from semipy
sampler = semipy.sampler.JointSampler(train_dataset, batch_size=32, proportion=0.5)
# Defining dataloaders using the created sampler
train_dataloader = DataLoader(aug_train_dataset, batch_sampler=sampler)
val_dataloader = [DataLoader(val_dataset, batch_size=32)]
In [23]:
Copied!
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
In [24]:
Copied!
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
In [25]:
Copied!
# Using semipy's PseudoLabel class
method = semipy.methods.FixMatch(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
# Using semipy's PseudoLabel class
method = semipy.methods.FixMatch(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
0%| | 0/100 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
In [26]:
Copied!
show_separation(model)
show_separation(model)
In [27]:
Copied!
plt.plot(range(len(results)), results)
plt.plot(range(len(results)), results)
Out[27]:
[<matplotlib.lines.Line2D at 0x2386a99fcd0>]
Complete Case with data augmentation¶
In [28]:
Copied!
# Using custom sampler from semipy
sampler = semipy.sampler.JointSampler(aug_train_dataset, batch_size=16, proportion=1)
# Defining dataloaders using the created sampler
train_dataloader = DataLoader(aug_train_dataset, batch_sampler=sampler)
val_dataloader = [DataLoader(val_dataset, batch_size=16)]
# Using custom sampler from semipy
sampler = semipy.sampler.JointSampler(aug_train_dataset, batch_size=16, proportion=1)
# Defining dataloaders using the created sampler
train_dataloader = DataLoader(aug_train_dataset, batch_sampler=sampler)
val_dataloader = [DataLoader(val_dataset, batch_size=16)]
C:\Users\lboiteau\Documents\Demos\semipy\sampler\jointsampler.py:35: UserWarning: Warning : you are in the complete case. All items will be labelledand no unlabelled items will be seen by the model. warnings.warn(("Warning : you are in the complete case. All items will be labelled"
In [29]:
Copied!
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
# Using semipy's PseudoLabel class
method = semipy.methods.FixMatch(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
# Defining model
model = nn.Sequential(
nn.Linear(2, 30),
nn.ReLU(),
nn.Linear(30, 20),
nn.ReLU(),
nn.Linear(20, 2)
).to('cuda')
# Defining optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.04, momentum=0.9)
# Defining parameters
args = semipy.tools.get_config('config.yaml')
# Using semipy's PseudoLabel class
method = semipy.methods.FixMatch(args, model, train_dataloader, val_dataloader, optimizer, scheduler=None, num_classes=2)
results = method.training()
0%| | 0/100 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
Iterations: 0%| | 0/2 [00:00<?, ?it/s]
In [30]:
Copied!
plt.plot(range(len(results)), results)
plt.plot(range(len(results)), results)
Out[30]:
[<matplotlib.lines.Line2D at 0x23890c9c550>]
In [31]:
Copied!
show_separation(model)
show_separation(model)
In [ ]:
Copied!