{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('/opt/platform/code/dd_widgets/')\n",
    "from dd_widgets import Classification, CSV, Text, Segmentation, Detection, OCR, TSNE_CSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2b1e4bf2e30a439da770bd03b1e58fa6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>CSV task: forest</h2>'), HBox(children=(Button(description='Run …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# CSV training example with MLP neural network\n",
    "# See https://deepdetect.com/tutorials/csv-training/ for the full tutorial outside of the platform\n",
    "csv_train_job = CSV(\n",
    "    'forest',\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    training_repo=\"/opt/platform/examples/covert/train.csv\",\n",
    "    model_repo=\"/opt/platform/models/training/examples/covert\",\n",
    "    label_offset=-1,\n",
    "    csv_label='Cover_Type',\n",
    "    csv_id='Id',\n",
    "    tsplit=0.2,\n",
    "    template='mlp',\n",
    "    layers='[150,150,150]',\n",
    "    activation=\"prelu\",\n",
    "    nclasses=7,\n",
    "    scale= True,\n",
    "    iterations=10000,\n",
    "    base_lr=0.001,\n",
    "    solver_type=\"AMSGRAD\"\n",
    ")\n",
    "csv_train_job"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8ef421492d75417a817b2162445e1aba",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>Text task: news20</h2>'), HBox(children=(Button(description='Run…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Text training example with BoW (bag of words) with MLP neural network\n",
    "# See https://deepdetect.com/tutorials/txt-training/ for the full tutorial outside of the platform\n",
    "txt_mlp_train_job = Text(\n",
    "    'news20',\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    training_repo='/opt/platform/examples/news20/news20/',\n",
    "    model_repo='/opt/platform/models/training/examples/news20',\n",
    "    path='',\n",
    "    nclasses=20,\n",
    "    shuffle=True,\n",
    "    min_count=10,\n",
    "    min_word_length=5,\n",
    "    count=False,\n",
    "    template='mlp',\n",
    "    layers='[200,200]',\n",
    "    activation='relu',\n",
    "    tsplit=0.2,\n",
    "    base_lr=0.001,\n",
    "    solver_type='AMSGRAD',\n",
    "    iterations=10000,\n",
    "    test_interval=500,\n",
    "    batch_size=128\n",
    ")\n",
    "txt_mlp_train_job"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4693b95756774eb59072ce7f572e2df9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>Text task: news20</h2>'), HBox(children=(Button(description='Run…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Text training with a convolutional neural network\n",
    "txt_convnet_train_job = Text(\n",
    "    'news20',         \n",
    "    training_repo = '/opt/platform/examples/news20/news20/',\n",
    "    testing_repo=None,\n",
    "    host='examples',\n",
    "    port=8080,\n",
    "    model_repo=\"/opt/platform/models/training/examples/news20_convnet/\",\n",
    "    db=True,\n",
    "    nclasses=20,\n",
    "    characters=True,\n",
    "    read_forward=False,\n",
    "    sequence=512,\n",
    "    embedding=True,\n",
    "    template=\"vdcnn_9\",\n",
    "    iterations=25000,\n",
    "    test_interval=1000,\n",
    "    batch_size=128,\n",
    "    base_lr=0.0001,\n",
    "    solver_type=\"AMSGRAD\",\n",
    "    tsplit=0.2,\n",
    "    shuffle=True\n",
    ")\n",
    "txt_convnet_train_job"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "92de16857f524cd1a91f8fa9216fe345",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>Detection task: cars_mobilenet_v2</h2>'), HBox(children=(Button(…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Image object detector training of an SSD neural network (SSD-300)\n",
    "img_obj_detect = Detection(\n",
    "    \"cars_mobilenet_v2\",\n",
    "    training_repo= \"/opt/platform/examples/cars/train.txt\",\n",
    "    testing_repo= \"/opt/platform/examples/cars/test.txt\",\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    model_repo='/opt/platform/models/training/examples/cars_mobilenet/',\n",
    "    img_width=300,\n",
    "    img_height=300,\n",
    "    db_width=512,\n",
    "    db_height=512,\n",
    "    snapshot_interval=500,\n",
    "    test_interval=500,\n",
    "    iterations=25000,\n",
    "    template=\"mobilenet_v2_ssd\",\n",
    "    mirror=True,\n",
    "    rotate=False,\n",
    "    finetune=True,\n",
    "    weights=\"/opt/platform/models/pretrained/mobilenet_v2/mobilenet_v2.caffemodel\",\n",
    "    batch_size=8,\n",
    "    iter_size=4,\n",
    "    test_batch_size=4,\n",
    "    nclasses=2,\n",
    "    base_lr=0.0001,\n",
    "    solver_type=\"RMSPROP\",\n",
    "    gpuid=2,\n",
    "    resume=False\n",
    ")\n",
    "img_obj_detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b9600a0a21994f9a98a99a0df0e0c4af",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>Classification task: dogs_cats</h2>'), HBox(children=(Button(des…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# image classifier training with a convolutional neural network (squeeze-excitation ResNet-50)\n",
    "img_classif = Classification(\n",
    "    'dogs_cats', \n",
    "    training_repo='/opt/platform/examples/dogs_cats/train/',\n",
    "    tsplit=.2,\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    model_repo='/opt/platform/models/training/examples/dogs_cats',\n",
    "    template='se_resnet_50',\n",
    "    img_width=224,\n",
    "    img_height=224,\n",
    "    mirror=True,\n",
    "    rotate=False,\n",
    "    base_lr=0.001,\n",
    "    solver_type=\"SGD\",\n",
    "    finetune=True,\n",
    "    weights='/opt/platform/models/pretrained/se_resnet_50/SE-ResNet-50.caffemodel',\n",
    "    iterations=50000,\n",
    "    test_interval=1000,\n",
    "    snapshot_interval=1000,\n",
    "    batch_size=16,\n",
    "    iter_size=2,\n",
    "    nclasses=2,\n",
    "    test_batch_size=4,\n",
    "    noise_prob=0.001,\n",
    "    distort_prob=0.001,\n",
    "    gpuid=0,\n",
    "    resume=True\n",
    ")\n",
    "img_classif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "268fac9ef5b54ef9bafa8442ab0989ac",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>Classification task: snd_5_classes</h2>'), HBox(children=(Button…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# sound classifier training with a convolutional neural network\n",
    "snd_classif = Classification(\n",
    "    'snd_5_classes', \n",
    "    training_repo='/opt/platform/examples/bbc_sound/sounds_dataset_1.0_mini/train/',\n",
    "    testing_repo='/opt/platform/examples/bbc_sound/sounds_dataset_1.0_mini/test/',\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    model_repo='/opt/platform/models/training/examples/snd_5_classes',\n",
    "    layers='[\"1CR32\", \"1CR64\", \"1CR128\", \"1024\"]',\n",
    "    template='convnet',\n",
    "    img_width=257,\n",
    "    img_height=257,\n",
    "    base_lr=0.0001,\n",
    "    solver_type=\"AMSGRAD\",\n",
    "    iterations=10000,\n",
    "    test_interval=500,\n",
    "    snapshot_interval=1000,\n",
    "    batch_size=32,\n",
    "    test_batch_size=4,\n",
    "    unchanged_data=True,\n",
    "    bw=True,\n",
    "    gpuid=2\n",
    ")\n",
    "snd_classif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4aafa0c5ae7a4d2d8ca56ecf3e8508d9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>CSV task: auto_mnist</h2>'), HBox(children=(Button(description='…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# CSV training example with autoencoder MLP neural network\n",
    "# See https://deepdetect.com/tutorials/csv-training/ for the full tutorial outside of the platform\n",
    "autoenc_train_job = CSV(\n",
    "    'auto_mnist',\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    training_repo=\"/opt/platform/examples/mnist_csv/mnist_train.csv\",\n",
    "    testing_repo=\"/opt/platform/examples/mnist_csv/mnist_test.csv\",\n",
    "    model_repo=\"/opt/platform/models/training/examples/auto_mnist\",\n",
    "    #csv_ignore='[\"label\"]',\n",
    "    csv_label=\"label\",\n",
    "    db=True,\n",
    "    template='mlp',\n",
    "    layers='[500,250,30,250,500]',\n",
    "    activation=\"sigmoid\",\n",
    "    dropout=0.0,\n",
    "    autoencoder=True,\n",
    "    scale= True,\n",
    "    iterations=65000,\n",
    "    test_interval=5000,\n",
    "    base_lr=0.01,\n",
    "    batch_size=128,\n",
    "    solver_type=\"AMSGRAD\"\n",
    ")\n",
    "autoenc_train_job"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cfdb76c3e87d43f8a84a96f6993c90a2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>Segmentation task: city_psp</h2>'), HBox(children=(Button(descri…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Image segmentation\n",
    "city_seg = Segmentation(\n",
    "    'city_psp', \n",
    "    training_repo='/opt/platform/examples/cityscapes/train.txt',\n",
    "    testing_repo='/opt/platform/examples/cityscapes/test_shuf50.txt',\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    img_height=480,\n",
    "    img_width=480,\n",
    "    model_repo='/opt/platform/models/training/examples/cityscapes/',\n",
    "    nclasses=8,\n",
    "    template='pspnet_vgg16',\n",
    "    iterations=75000,\n",
    "    test_interval=1000,\n",
    "    snapshot_interval=1000,\n",
    "    batch_size=6,\n",
    "    test_batch_size=1,\n",
    "    noise_prob=0.001,\n",
    "    distort_prob=0.001,\n",
    "    gpuid=0,\n",
    "    base_lr=0.001,\n",
    "    weights='/opt/platform/models/pretrained/pspnet_vgg16/vgg16_init_deeplab.caffemodel',\n",
    "    solver_type=\"AMSGRAD\",\n",
    "    finetune=True,\n",
    "    rotate=False,\n",
    "    mirror=True,\n",
    "    resume=False,\n",
    "    loss='softmax'\n",
    ")#.run()\n",
    "city_seg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5f126c4d91814a69b9af3b4a00a342ae",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>OCR task: word_mnist</h2>'), HBox(children=(Button(description='…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ocr = OCR(\n",
    "    'word_mnist',\n",
    "    training_repo='/opt/platform/examples/word_mnist/train.txt',\n",
    "    testing_repo='/opt/platform/examples/word_mnist/test.txt',\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    img_height=80,\n",
    "    img_width=128,\n",
    "    model_repo='/opt/platform/models/training/examples/words_mnist',\n",
    "    nclasses=100,\n",
    "    template='crnn',\n",
    "    iterations=10000,\n",
    "    test_interval=1000,\n",
    "    snapshot_interval=1000,\n",
    "    batch_size=128,\n",
    "    test_batch_size=32,\n",
    "    noise_prob=0.001,\n",
    "    distort_prob=0.001,\n",
    "    gpuid=1,\n",
    "    base_lr=0.0001,\n",
    "    solver_type='ADAM',\n",
    "    mirror=False,\n",
    "    rotate=False,\n",
    "    resume=False\n",
    ")\n",
    "ocr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e802c4d0c5d74b678193ab414b35ed27",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>TSNE_CSV task: tsne_mnist</h2>'), HBox(children=(Button(descript…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tsne_mnist = TSNE_CSV(\n",
    "    'tsne_mnist',\n",
    "    training_repo = '/opt/platform/examples/mnist_csv/mnist_train.csv',\n",
    "    host='deepdetect',\n",
    "    port=8080,\n",
    "    model_repo='/opt/platform/models/training/examples/test_tsne/',\n",
    "    iterations = 5000,\n",
    "    perplexity = 30\n",
    ")\n",
    "tsne_mnist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "tsne_mnist.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df_orig = pd.read_csv(\"/opt/platform/examples/mnist_csv/mnist_train.csv\")\n",
    "\n",
    "# other parameters like 'scatter' can be used\n",
    "tsne_mnist.plot(s=10, marker='^', c=df_orig.label, cmap='jet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "91357cea0c5d464594849583ec7c1389",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HTML(value='<h2>Classification task: autoenc_cnn</h2>'), HBox(children=(Button(d…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cnn_autoenc_train_job = Classification(\n",
    "    'autoenc_cnn',\n",
    "    host='eris_deepdetect_training',\n",
    "    port=8080,\n",
    "    training_repo=\"/opt/platform/examples/cnn_autoenc/impressionism/\",\n",
    "    testing_repo=\"/opt/platform/examples/cnn_autoenc/test/\",\n",
    "    model_repo=\"/opt/platform/models/training/examples/cnn_autoenc/\",\n",
    "    template='convnet',\n",
    "    layers='[\"1CR32\",\"1CR32\",\"1CR32\",\"DR32\",\"1CR32\",\"DR32\",\"1CR32\"]',\n",
    "    activation=\"relu\",\n",
    "    dropout=0.0,\n",
    "    autoencoder=True,\n",
    "    scale=0.004,\n",
    "    bw=False,\n",
    "    img_width=224,\n",
    "    img_height=224,\n",
    "    iterations=56000,\n",
    "    test_interval=2000,\n",
    "    base_lr=0.0001,\n",
    "    batch_size=64,\n",
    "    test_batch_size=16,\n",
    "    solver_type=\"ADAM\",\n",
    "    gpuid=1,\n",
    "    noise_prob=0.001,\n",
    "    mirror=True\n",
    ")\n",
    "cnn_autoenc_train_job"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
