helper-code
Copyright By PowCoder代写 加微信 powcoder
import torch
import torch.nn as nn
from torchvision import transforms, datasets
from torch.utils.data import DataLoader, TensorDataset
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import requests
import tarfile
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.metrics import confusion_matrix, adjusted_rand_score, adjusted_mutual_info_score
from sklearn.pipeline import Pipeline
from sklearn.base import TransformerMixin
Flowers Dataset and VGG Features¶
filename = ‘./flowers_features_and_labels.npz’
if os.path.exists(filename):
file = np.load(filename)
f_all, y_all = file[‘f_all’], file[‘y_all’]
if not os.path.exists(‘./flower_photos’):
# download the flowers dataset and extract its images
url = ‘http://download.tensorflow.org/example_images/flower_photos.tgz’
with open(‘./flower_photos.tgz’, ‘wb’) as file:
file.write(requests.get(url).content)
with tarfile.open(‘./flower_photos.tgz’) as file:
file.extractall(‘./’)
os.remove(‘./flower_photos.tgz’)
class FeatureExtractor(nn.Module):
def __init__(self):
super().__init__()
vgg = torch.hub.load(‘pytorch/vision:v0.10.0’, ‘vgg16’, pretrained=True)
# Extract VGG-16 Feature Layers
self.features = list(vgg.features)
self.features = nn.Sequential(*self.features)
# Extract VGG-16 Average Pooling Layer
self.pooling = vgg.avgpool
# Convert the image into one-dimensional vector
self.flatten = nn.Flatten()
# Extract the first part of fully-connected layer from VGG16
self.fc = vgg.classifier[0]
def forward(self, x):
# It will take the input ‘x’ until it returns the feature vector called ‘out’
out = self.features(x)
out = self.pooling(out)
out = self.flatten(out)
out = self.fc(out)
return out
# Initialize the model
assert torch.cuda.is_available()
feature_extractor = FeatureExtractor().cuda().eval()
dataset = datasets.ImageFolder(root=’./flower_photos’,
transform=transforms.Compose([transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]))
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
# Extract features and store them on disk
f_all, y_all = np.zeros((0, 4096)), np.zeros((0,))
for x, y in tqdm(dataloader):
with torch.no_grad():
f_all = np.vstack([f_all, feature_extractor(x.cuda()).cpu()])
y_all = np.concatenate([y_all, y])
np.savez(filename, f_all=f_all, y_all=y_all)
print(f_all.shape, y_all.shape)
num_features = f_all.shape[1]
(3670, 4096) (3670,)
f_pca = PCA(n_components=2).fit_transform(f_all)
plt.scatter(*f_pca.T, c=y_all)
MLP Classifier¶
class MLP(torch.nn.Module):
def __init__(self, num_features):
super().__init__()
self.model = nn.Sequential(
nn.Linear(num_features, 1280),
nn.ReLU(True),
nn.Linear(1280, 640),
nn.ReLU(True),
nn.Linear(640, 5),
nn.LogSoftmax(dim=1)
self.cuda()
def forward(self, X):
return self.model(X)
def train(self, X, y):
X = torch.tensor(X, dtype=torch.float32, device=’cuda’)
y = torch.tensor(y, dtype=torch.int64, device=’cuda’)
self.model.train()
criterion = nn.NLLLoss()
optimizer = torch.optim.Adam(self.parameters(), lr=1e-3, weight_decay=1e-5)
dataset = TensorDataset(X, y)
dataloader = DataLoader(dataset, batch_size=128, shuffle=True)
for epoch in tqdm(range(100)):
for (X_, y_) in dataloader:
##################################
# you should implement this part #
##################################
raise NotImplementedError
return self
def eval(self, X_test, y_test):
##################################
# you should implement this part #
##################################
raise NotImplementedError
Autoencoder¶
class Autoencoder(torch.nn.Module, TransformerMixin):
def __init__(self, n_components):
super().__init__()
self.n_components = n_components
self.n_features = None # to be determined with data
self.encoder = None
self.decoder = None
def _create_encoder(self):
return nn.Sequential(
nn.Linear(4096, 1280),
nn.ReLU(True),
nn.Linear(1280, 640),
nn.ReLU(True), nn.Linear(640, 120), nn.ReLU(True), nn.Linear(120, self.n_components))
def _create_decoder(self):
return nn.Sequential(
nn.Linear(self.n_components, 120),
nn.ReLU(True),
nn.Linear(120, 640),
nn.ReLU(True),
nn.Linear(640, 1280),
nn.ReLU(True), nn.Linear(1280, 4096))
def forward(self, X):
encoded = self.encoder(X)
decoded = self.decoder(encoded)
return decoded
def fit(self, X):
X = torch.tensor(X, dtype=torch.float32, device=’cuda’)
self.n_features = X.shape[1]
self.encoder = self._create_encoder()
self.decoder = self._create_decoder()
self.cuda()
self.train()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(self.parameters(), lr=1e-3, weight_decay=1e-5)
dataset = TensorDataset(X)
dataloader = DataLoader(dataset, batch_size=128, shuffle=True)
for epoch in tqdm(range(100)):
for (X_,) in dataloader:
X_ = X_.cuda()
# ===================forward=====================
output = self(X_)
loss = criterion(output, X_)
# ===================backward====================
optimizer.zero_grad()
loss.backward()
optimizer.step()
return self
def transform(self, X):
X = torch.tensor(X, dtype=torch.float32, device=’cuda’)
self.eval()
with torch.no_grad():
return self.encoder(X).cpu().numpy()
X_em =Autoencoder(2).fit_transform(f_all)
plt.scatter(*X_em.T, c=y_all)
100%|██████████████████████████████████████████████████████████████████████████████████████| 100/100 [00:28<00:00, 3.56it/s]
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com