{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport os\nprint(os.listdir(\"../input\"))\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.utils.data import SubsetRandomSampler, DataLoader\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms, utils,datasets\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-28T01:23:13.600483Z","iopub.execute_input":"2021-08-28T01:23:13.600876Z","iopub.status.idle":"2021-08-28T01:23:15.060996Z","shell.execute_reply.started":"2021-08-28T01:23:13.600798Z","shell.execute_reply":"2021-08-28T01:23:15.060083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ndf_test = pd.read_csv('../input/Kannada-MNIST/test.csv')\nprint('Train size: ', df_train.shape)\nprint('Test size: ', df_test.shape)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:15.062507Z","iopub.execute_input":"2021-08-28T01:23:15.063113Z","iopub.status.idle":"2021-08-28T01:23:20.269476Z","shell.execute_reply.started":"2021-08-28T01:23:15.063072Z","shell.execute_reply":"2021-08-28T01:23:20.268667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = df_train.drop('label', axis=1).values\ntrain_mean = train_data.mean()/255.\ntrain_std = train_data.std()/255.\n# train_std\nprint('Mean: ', train_mean)\nprint('Std: ', train_std)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:20.271275Z","iopub.execute_input":"2021-08-28T01:23:20.271785Z","iopub.status.idle":"2021-08-28T01:23:20.695217Z","shell.execute_reply.started":"2021-08-28T01:23:20.271746Z","shell.execute_reply":"2021-08-28T01:23:20.694265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = np.random.rand(len(df_train)) < 0.8\ndf_val = df_train[~mask]\ndf_train = df_train[mask]\nprint('Train size: ', df_train.shape)\nprint('Val size: ', df_val.shape)\nprint('Test size: ', df_test.shape)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:20.696838Z","iopub.execute_input":"2021-08-28T01:23:20.697362Z","iopub.status.idle":"2021-08-28T01:23:20.876244Z","shell.execute_reply.started":"2021-08-28T01:23:20.697324Z","shell.execute_reply":"2021-08-28T01:23:20.875241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nind = np.random.randint(0, df_train.shape[0]-1)\nplt.imshow(df_train.iloc[ind].values[1:].reshape((28,28)), cmap='gray')\nplt.title(str(df_train.iloc[ind][0]))","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:20.877689Z","iopub.execute_input":"2021-08-28T01:23:20.878050Z","iopub.status.idle":"2021-08-28T01:23:21.038625Z","shell.execute_reply.started":"2021-08-28T01:23:20.878012Z","shell.execute_reply":"2021-08-28T01:23:21.037676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class KannadaMNIST(Dataset):\n    def __init__(self, df, transform=None):\n        self.df = df\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, n):\n        data = self.df.iloc[n]\n        image = data[1:].values.reshape((28,28)).astype(np.uint8)\n        label = data[0]\n        if self.transform:\n            image = self.transform(image)\n        return (image, label)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:21.040012Z","iopub.execute_input":"2021-08-28T01:23:21.040355Z","iopub.status.idle":"2021-08-28T01:23:21.046448Z","shell.execute_reply.started":"2021-08-28T01:23:21.040318Z","shell.execute_reply":"2021-08-28T01:23:21.045389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.ToTensor()","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:21.047870Z","iopub.execute_input":"2021-08-28T01:23:21.048484Z","iopub.status.idle":"2021-08-28T01:23:21.056416Z","shell.execute_reply.started":"2021-08-28T01:23:21.048450Z","shell.execute_reply":"2021-08-28T01:23:21.055644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = KannadaMNIST(df_train, transform = transform)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:21.059728Z","iopub.execute_input":"2021-08-28T01:23:21.060002Z","iopub.status.idle":"2021-08-28T01:23:21.067911Z","shell.execute_reply.started":"2021-08-28T01:23:21.059978Z","shell.execute_reply":"2021-08-28T01:23:21.066961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 20\nnum_workers = 0\ntrain_loader = torch.utils.data.DataLoader(train_data, batch_size = batch_size, num_workers = num_workers)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:21.069771Z","iopub.execute_input":"2021-08-28T01:23:21.070099Z","iopub.status.idle":"2021-08-28T01:23:21.077530Z","shell.execute_reply.started":"2021-08-28T01:23:21.070067Z","shell.execute_reply":"2021-08-28T01:23:21.076784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataiter = iter(train_loader)\nimages, labels = dataiter.next()\nprint(images, images.shape, len(images), images[0].shape)\nprint()\nprint(labels, labels.shape, len(labels))","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:21.078731Z","iopub.execute_input":"2021-08-28T01:23:21.079072Z","iopub.status.idle":"2021-08-28T01:23:21.167769Z","shell.execute_reply.started":"2021-08-28T01:23:21.079038Z","shell.execute_reply":"2021-08-28T01:23:21.167005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig =  plt.figure(figsize = (30,10))\nfor i in range(len(labels)):\n  ax = fig.add_subplot(2,10,i+1,xticks=[], yticks = [])\n  plt.imshow(np.squeeze(images[i]))\n  ax.set_title(labels[i].item(), color = 'blue')","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:21.169616Z","iopub.execute_input":"2021-08-28T01:23:21.169938Z","iopub.status.idle":"2021-08-28T01:23:22.137707Z","shell.execute_reply.started":"2021-08-28T01:23:21.169906Z","shell.execute_reply":"2021-08-28T01:23:22.136885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Discriminator(nn.Module):\n  def __init__(self,  input_size, hidden_dim, output_size):\n    super(Discriminator, self).__init__()\n\n    self.fc1 = nn.Linear(input_size, hidden_dim*4)\n    self.fc2 = nn.Linear(hidden_dim * 4, hidden_dim*2)\n    self.fc3 = nn.Linear(hidden_dim * 2, hidden_dim)\n    self.fc4 = nn.Linear(hidden_dim, output_size)\n    self.dropout = nn.Dropout(0.3)\n\n  def forward(self, x):\n    x = x.view(-1, 28*28)\n    x = F.leaky_relu(self.fc1(x), 0.2) #negative_slope = 0.2\n    x = self.dropout(x)\n    x = F.leaky_relu(self.fc2(x), 0.2)\n    x = self.dropout(x)\n    x = F.leaky_relu(self.fc3(x), 0.2)\n    x = self.dropout(x)\n    out = self.fc4(x)\n\n    return out","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.138869Z","iopub.execute_input":"2021-08-28T01:23:22.139220Z","iopub.status.idle":"2021-08-28T01:23:22.148064Z","shell.execute_reply.started":"2021-08-28T01:23:22.139183Z","shell.execute_reply":"2021-08-28T01:23:22.147275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Generator(nn.Module):\n\n  def __init__(self, input_size, hidden_dim, output_size):\n    super(Generator, self).__init__()\n\n    self.fc1 = nn.Linear(input_size, hidden_dim)\n    self.fc2 = nn.Linear(hidden_dim, hidden_dim*2)\n    self.fc3 = nn.Linear(hidden_dim*2, hidden_dim*4)\n    self.fc4 = nn.Linear(hidden_dim*4, output_size)\n    self.dropout = nn.Dropout(0.3)\n\n\n  def forward(self, x):\n    x = F.leaky_relu(self.fc1(x), 0.2)\n    x = self.dropout(x)\n    x = F.leaky_relu(self.fc2(x), 0.2)\n    x = self.dropout(x)\n    x = F.leaky_relu(self.fc3(x), 0.2)\n    x = self.dropout(x)\n    out = F.tanh(self.fc4(x))\n\n    return out","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.149226Z","iopub.execute_input":"2021-08-28T01:23:22.149570Z","iopub.status.idle":"2021-08-28T01:23:22.159259Z","shell.execute_reply.started":"2021-08-28T01:23:22.149538Z","shell.execute_reply":"2021-08-28T01:23:22.158384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_size = 784\nd_output_size = 1\nd_hidden_size = 32","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.160714Z","iopub.execute_input":"2021-08-28T01:23:22.161279Z","iopub.status.idle":"2021-08-28T01:23:22.168545Z","shell.execute_reply.started":"2021-08-28T01:23:22.161240Z","shell.execute_reply":"2021-08-28T01:23:22.167773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z_size = 100\ng_output_size = 784\ng_hidden_size = 32","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.169736Z","iopub.execute_input":"2021-08-28T01:23:22.170084Z","iopub.status.idle":"2021-08-28T01:23:22.178439Z","shell.execute_reply.started":"2021-08-28T01:23:22.170047Z","shell.execute_reply":"2021-08-28T01:23:22.177621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = Discriminator(input_size, d_hidden_size, d_output_size)\nG = Generator(z_size, g_hidden_size, g_output_size)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.181348Z","iopub.execute_input":"2021-08-28T01:23:22.181675Z","iopub.status.idle":"2021-08-28T01:23:22.190930Z","shell.execute_reply.started":"2021-08-28T01:23:22.181648Z","shell.execute_reply":"2021-08-28T01:23:22.190111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(D)\nprint(G)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.193997Z","iopub.execute_input":"2021-08-28T01:23:22.194240Z","iopub.status.idle":"2021-08-28T01:23:22.201289Z","shell.execute_reply.started":"2021-08-28T01:23:22.194218Z","shell.execute_reply":"2021-08-28T01:23:22.200268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def real_loss(D_out, smooth = False):\n  batch_size = D_out.size(0)\n  if smooth:\n    labels = torch.ones(batch_size) * 0.9\n  \n  else:\n    labels = torch.ones(batch_size)\n\n  criterion = nn.BCEWithLogitsLoss()\n  loss = criterion(D_out.squeeze(1), labels)\n  return loss\n\ndef fake_loss(D_out):\n  batch_size = D_out.size(0)\n  labels = torch.zeros(batch_size)\n  criterion = nn.BCEWithLogitsLoss()\n\n  loss = criterion(D_out.squeeze(1), labels)\n  return loss","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.202546Z","iopub.execute_input":"2021-08-28T01:23:22.203061Z","iopub.status.idle":"2021-08-28T01:23:22.210505Z","shell.execute_reply.started":"2021-08-28T01:23:22.203019Z","shell.execute_reply":"2021-08-28T01:23:22.209341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\nlr = 0.002\n\nd_optimizer = optim.Adam(D.parameters(), lr)\ng_optimizer = optim.Adam(G.parameters(), lr)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.212196Z","iopub.execute_input":"2021-08-28T01:23:22.212947Z","iopub.status.idle":"2021-08-28T01:23:22.223061Z","shell.execute_reply.started":"2021-08-28T01:23:22.212907Z","shell.execute_reply":"2021-08-28T01:23:22.222259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle as pkl\n\nnum_epochs = 100\n\nsamples = []\nlosses = []\n\nprint_every = 400\n\nsample_size = 16\nfixed_z = np.random.uniform(-1, 1, size = (sample_size, z_size))\nfixed_z = torch.from_numpy(fixed_z).float()","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.224494Z","iopub.execute_input":"2021-08-28T01:23:22.224889Z","iopub.status.idle":"2021-08-28T01:23:22.232492Z","shell.execute_reply.started":"2021-08-28T01:23:22.224854Z","shell.execute_reply":"2021-08-28T01:23:22.231722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D.train()\nG.train()\nfor epoch in range(num_epochs):\n  for batch_i, (real_images, _) in enumerate(train_loader):\n\n    batch_size =  real_images.size(0)\n\n    real_images = real_images * 2 - 1\n\n\n\n    d_optimizer.zero_grad()\n\n\n    # entrenar en imagenes reales\n\n    D_real = D(real_images)\n    d_real_loss = real_loss(D_real, smooth =  True)\n\n\n    # entrenar en imagenes falsas\n    z = np.random.uniform(-1, 1, size = (batch_size, z_size))\n    z = torch.from_numpy(z).float()\n    fake_images = G(z)\n\n    #calcular la perdida del discriminador en iamgenes falsas\n    D_fake = D(fake_images)\n    d_fake_loss = fake_loss(D_fake)\n\n    #realizar backprop\n    d_loss = d_real_loss + d_fake_loss\n    d_loss.backward()\n    d_optimizer.step()\n\n\n\n    #entrenamiento del generador\n\n    g_optimizer.zero_grad()\n\n    z = np.random.uniform(-1, 1, size=(batch_size, z_size))\n    z = torch.from_numpy(z).float()\n    fake_images = G(z)\n\n    D_fake = D(fake_images)\n    g_loss = real_loss(D_fake)\n\n    g_loss.backward()\n    g_optimizer.step()\n\n    if batch_i % print_every == 0:\n      print('Epoch [{:5d}/{:5d}] | d_loss: {:6.4f} | g_loss: {:6.4f}'.format (epoch +1, num_epochs, d_loss.item(), g_loss.item()))\n\n  losses.append((d_loss.item(), g_loss.item()))\n\n  G.eval()\n  samples_z = G(fixed_z)\n  samples.append(samples_z)\n  G.train()\n\nwith open('train_samples.pkl', 'wb') as f:\n  pkl.dump(samples, f)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T01:23:22.233908Z","iopub.execute_input":"2021-08-28T01:23:22.234266Z","iopub.status.idle":"2021-08-28T02:16:03.255563Z","shell.execute_reply.started":"2021-08-28T01:23:22.234231Z","shell.execute_reply":"2021-08-28T02:16:03.254691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nlosses = np.array(losses)\nplt.plot(losses.T[0], label = 'Discriminator')\nplt.plot(losses.T[1], label = 'Generator')\nplt.title(\"Training Losses\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-28T02:16:03.256979Z","iopub.execute_input":"2021-08-28T02:16:03.257358Z","iopub.status.idle":"2021-08-28T02:16:03.404948Z","shell.execute_reply.started":"2021-08-28T02:16:03.257317Z","shell.execute_reply":"2021-08-28T02:16:03.403970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_samples(epoch, samples):\n    fig, axes = plt.subplots(figsize=(7,7), nrows=4, ncols=4, sharey=True, sharex=True)\n    for ax, img in zip(axes.flatten(), samples[epoch]):\n        img = img.detach()\n        ax.xaxis.set_visible(False)\n        ax.yaxis.set_visible(False)\n        im = ax.imshow(img.reshape((28,28)), cmap='Greys_r')\n        \n        \nwith open('train_samples.pkl', 'rb') as f:\n    samples = pkl.load(f)\n    \nview_samples(-1, samples)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T02:16:03.408175Z","iopub.execute_input":"2021-08-28T02:16:03.408537Z","iopub.status.idle":"2021-08-28T02:16:04.183450Z","shell.execute_reply.started":"2021-08-28T02:16:03.408501Z","shell.execute_reply":"2021-08-28T02:16:04.182608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nrows = 10 \ncols = 6\nfig, axes = plt.subplots(figsize=(7,12), nrows=rows, ncols=cols, sharex=True, sharey=True)\n\nfor sample, ax_row in zip(samples[::int(len(samples)/rows)], axes):\n    for img, ax in zip(sample[::int(len(sample)/cols)], ax_row):\n        img = img.detach()\n        ax.imshow(img.reshape((28,28)), cmap='Greys_r')\n        ax.xaxis.set_visible(False)\n        ax.yaxis.set_visible(False)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T02:16:04.184718Z","iopub.execute_input":"2021-08-28T02:16:04.185040Z","iopub.status.idle":"2021-08-28T02:16:08.055897Z","shell.execute_reply.started":"2021-08-28T02:16:04.185002Z","shell.execute_reply":"2021-08-28T02:16:08.055070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_size=16\nrand_z = np.random.uniform(-1, 1, size=(sample_size, z_size))\nrand_z = torch.from_numpy(rand_z).float()\n\nG.eval() \nrand_images = G(rand_z)\nview_samples(0, [rand_images])","metadata":{"execution":{"iopub.status.busy":"2021-08-28T02:16:08.057129Z","iopub.execute_input":"2021-08-28T02:16:08.057578Z","iopub.status.idle":"2021-08-28T02:16:08.843128Z","shell.execute_reply.started":"2021-08-28T02:16:08.057543Z","shell.execute_reply":"2021-08-28T02:16:08.842067Z"},"trusted":true},"execution_count":null,"outputs":[]}]}