{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import torch\n", "import sys\n", "sys.path.insert(1, '../')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from models.resnet_simclr import ResNetSimCLR\n", "import torchvision.transforms as transforms\n", "from torch.utils.data import DataLoader\n", "from torchvision import datasets\n", "import numpy as np\n", "from sklearn.neighbors import KNeighborsClassifier\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "batch_size = 512\n", "out_dim = 128" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def _load_stl10(prefix=\"train\"):\n", " X_train = np.fromfile('../data/stl10_binary/' + prefix + '_X.bin', dtype=np.uint8)\n", " y_train = np.fromfile('../data/stl10_binary/' + prefix + '_y.bin', dtype=np.uint8)\n", "\n", " X_train = np.reshape(X_train, (-1, 3, 96, 96))\n", " X_train = np.transpose(X_train, (0, 3, 2, 1))\n", " print(\"{} images\".format(prefix))\n", " print(X_train.shape)\n", " print(y_train.shape)\n", " return X_train, y_train" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "train images\n", "(5000, 96, 96, 3)\n", "(5000,)\n" ] } ], "source": [ "# load STL-10 train data\n", "X_train, y_train = _load_stl10(\"train\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "test images\n", "(8000, 96, 96, 3)\n", "(8000,)\n" ] } ], "source": [ "# load STL-10 test data\n", "X_test, y_test = _load_stl10(\"test\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test protocol #1 PCA features" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from sklearn.decomposition import PCA\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn import preprocessing" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PCA features\n", "(5000, 128)\n", "(8000, 128)\n" ] } ], "source": [ "scaler = preprocessing.StandardScaler()\n", "scaler.fit(X_train.reshape((X_train.shape[0],-1)))\n", "\n", "pca = PCA(n_components=out_dim)\n", "\n", "X_train_pca = pca.fit_transform(scaler.transform(X_train.reshape(X_train.shape[0], -1)))\n", "X_test_pca = pca.transform(scaler.transform(X_test.reshape(X_test.shape[0], -1)))\n", "\n", "print(\"PCA features\")\n", "print(X_train_pca.shape)\n", "print(X_test_pca.shape)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def linear_model_eval(X_train, y_train, X_test, y_test):\n", " \n", " clf = LogisticRegression(random_state=0, max_iter=1000, solver='lbfgs', C=1.0)\n", " clf.fit(X_train, y_train)\n", " print(\"PCA feature evaluation\")\n", " print(\"Train score:\", clf.score(X_train, y_train))\n", " print(\"Test score:\", clf.score(X_test, y_test))\n", " print(\"-------------------------------\")\n", " neigh = KNeighborsClassifier(n_neighbors=10)\n", " neigh.fit(X_train, y_train)\n", " print(\"KNN with PCA features\")\n", " print(\"Train score:\", neigh.score(X_train, y_train))\n", " print(\"Test score:\", neigh.score(X_test, y_test))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PCA feature evaluation\n", "Train score: 0.436\n", "Test score: 0.360875\n", "-------------------------------\n", "KNN with PCA features\n", "Train score: 0.4222\n", "Test score: 0.318\n" ] } ], "source": [ "linear_model_eval(X_train_pca, y_train, X_test_pca, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Protocol #2 Logisitc Regression" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = ResNetSimCLR(out_dim=out_dim)\n", "model.eval()\n", "\n", "state_dict = torch.load('../checkpoints/checkpoint.pth')\n", "model.load_state_dict(state_dict)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def next_batch(X, y, batch_size):\n", " for i in range(0, X.shape[0], batch_size):\n", " X_batch = torch.tensor(X[i: i+batch_size]) / 255.\n", " y_batch = torch.tensor(y[i: i+batch_size])\n", " yield X_batch.permute((0,3,1,2)), y_batch" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train features\n", "(5000, 512)\n" ] } ], "source": [ "X_train_feature = []\n", "\n", "for batch_x, batch_y in next_batch(X_train, y_train, batch_size):\n", " features, _ = model(batch_x)\n", " X_train_feature.extend(features.detach().numpy())\n", " \n", "X_train_feature = np.array(X_train_feature)\n", "\n", "print(\"Train features\")\n", "print(X_train_feature.shape)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test features\n", "(8000, 512)\n" ] } ], "source": [ "X_test_feature = []\n", "\n", "for batch_x, batch_y in next_batch(X_test, y_test, batch_size):\n", " features, _ = model(batch_x)\n", " X_test_feature.extend(features.detach().numpy())\n", " \n", "X_test_feature = np.array(X_test_feature)\n", "\n", "print(\"Test features\")\n", "print(X_test_feature.shape)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/thalles/anaconda3/envs/pytorch/lib/python3.6/site-packages/sklearn/linear_model/_logistic.py:940: ConvergenceWarning: lbfgs failed to converge (status=1):\n", "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n", "\n", "Increase the number of iterations (max_iter) or scale the data as shown in:\n", " https://scikit-learn.org/stable/modules/preprocessing.html\n", "Please also refer to the documentation for alternative solver options:\n", " https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n", " extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "PCA feature evaluation\n", "Train score: 0.937\n", "Test score: 0.71875\n", "-------------------------------\n", "KNN with PCA features\n", "Train score: 0.7378\n", "Test score: 0.6675\n" ] } ], "source": [ "scaler = preprocessing.StandardScaler()\n", "scaler.fit(X_train_feature)\n", "\n", "linear_model_eval(scaler.transform(X_train_feature), y_train, scaler.transform(X_test_feature), y_test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# SimCLR feature evaluation\n", "# Train score: 0.8966\n", "# Test score: 0.634125" ] } ], "metadata": { "kernelspec": { "display_name": "pytorch", "language": "python", "name": "pytorch" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }