# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="EDgSCpeOzpR8" colab_type="text"
# # Tel-Aviv Deep Learning Boot-camp: 12 Applied Deep Learning Labs
#
#
# # Lab 3: ONNX, YOLO-3, Keras, Medical 3D visualization 
#
#
# <img src="https://github.com/bayesianio/applied-dl-2018/raw/master/assets/tumor_visdom.jpg" align="center">
#
# ## Instructor:
#
# - <NAME>: <EMAIL>
#
#
# ## Progress
#
# - [x] Compile Darknet Yolo v3
# - [x] Convert Darknet Yolo v3 to Keras v3
# - []  Convert Keras to ONNX
# - []  Convert Keras/ONNX to WebDNN
#
#
# ## Links:
# - https://www.meetup.com/Tel-Aviv-Deep-Learning-Bootcamp/ 
# - Git: https://github.com/bayesianio/applied-dl-2018
# - Full info: https://www.evernote.com/shard/s341/sh/3855640e-2b0b-42e5-b5b9-00216d02ac9a/b47968226e49a81ee813901cd41d3924
#
#
# ## Google Colab version:
#
#
# ## Colab links:
# - https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d
# - https://jovianlin.io/pytorch-with-gpu-in-google-colab/

# + id="rmr9jTsazpR-" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 581.0} outputId="fa7900a4-503d-455a-b725-06faec98cd21" executionInfo={"status": "ok", "timestamp": 1529166095586.0, "user_tz": -180.0, "elapsed": 42905.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}
# %reset -f 
# %matplotlib inline

# ! pip install torch torchvision keras 

import matplotlib.pyplot as plt
import time
from shutil import copyfile
from os.path import isfile, join, abspath, exists, isdir, expanduser
from os import listdir, makedirs, getcwd, remove
from PIL import Image
from mpl_toolkits.axes_grid1 import ImageGrid
import pandas as pd
import numpy as np
import torch
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as func
import torchvision
from torchvision import transforms, datasets, models
import random 


import sys
print('__Python VERSION:', sys.version)
print('__pyTorch VERSION:', torch.__version__)
print('__CUDA VERSION')
from subprocess import call
# call(["nvcc", "--version"]) does not work
# ! nvcc --version
print('__CUDNN VERSION:', torch.backends.cudnn.version())
print('__Number CUDA Devices:', torch.cuda.device_count())
print('__Devices')
# call(["nvidia-smi", "--format=csv", "--query-gpu=index,name,driver_version,memory.total,memory.used,memory.free"])
print('Active CUDA Device: GPU', torch.cuda.current_device())

print ('Available devices ', torch.cuda.device_count())
print ('Current cuda device ', torch.cuda.current_device())

use_cuda = torch.cuda.is_available()
# use_cuda = False

print("USE CUDA=" + str (use_cuda))
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
Tensor = FloatTensor

manualSeed = 2222
def fixSeed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if use_cuda:
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)


if manualSeed is None:
        manualSeed = 999
fixSeed(manualSeed)

# + id="0FfUc7TNzpSI" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}}
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir) 

# + [markdown] id="A_qfAA46YXiw" colab_type="text"
# # Build Darknet: Requires CUDA 9.0 + C compiler

# + id="6G5fhWG1YXiw" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 1176.0} outputId="4927830e-6383-4af5-dc58-dceaa5274cf7" executionInfo={"status": "ok", "timestamp": 1529166124357.0, "user_tz": -180.0, "elapsed": 21225.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}} language="bash"
# git clone https://github.com/pjreddie/darknet
# cd darknet
# make

# + id="F7PA2Y5rYXiy" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 34.0} outputId="3d227332-d6d1-475d-e544-350fc974f9a1" executionInfo={"status": "ok", "timestamp": 1529166146598.0, "user_tz": -180.0, "elapsed": 771.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}
# cd darknet


# + id="7dMG1YWKaLXD" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 527.0} outputId="dafad5f3-5e59-40b9-99cd-61136767d3ff" executionInfo={"status": "ok", "timestamp": 1529166264232.0, "user_tz": -180.0, "elapsed": 2930.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}
# ! pwd
# ! ls -la

# + [markdown] id="GzquZNklYXi0" colab_type="text"
# ## Get the weights

# + id="GBwNQyyqYXi1" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 204.0} outputId="210e5155-f660-4292-db2d-fa0386fbbf8c" executionInfo={"status": "ok", "timestamp": 1529166180254.0, "user_tz": -180.0, "elapsed": 9430.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}
# Linux / OSX
# !wget https://pjreddie.com/media/files/yolov3.weights
# Windows    
#! "c:/Program Files (x86)/GnuWin32/bin/wget.exe" --no-check-certificate https://pjreddie.com/media/files/yolov3.weights

# + [markdown] id="2DlTUOeIYXi2" colab_type="text"
# ## Detection on a single image

# + id="Axp_sPt6YXi3" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 2091.0} outputId="ec1aa029-676d-46e8-8835-028fe98bdab8" executionInfo={"status": "ok", "timestamp": 1529166637869.0, "user_tz": -180.0, "elapsed": 17511.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}
# ! wget http://vrf.wpengine.netdna-cdn.com/wp-content/uploads/2014/03/sonkey13.jpeg
# !./darknet detect cfg/yolov3.cfg yolov3.weights sonkey13.jpeg

# + id="NhJV7QhIYXi5" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 248.0} outputId="2bc4708e-3781-43b1-972f-51f784502c3a" executionInfo={"status": "ok", "timestamp": 1529166642726.0, "user_tz": -180.0, "elapsed": 987.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}

import os.path
import cv2
import matplotlib.pyplot as plt


fig,ax = plt.subplots()
ax.tick_params(labelbottom="off",bottom="off")
ax.tick_params(labelleft="off",left="off")
ax.set_xticklabels([]) 
ax.axis('off')

file = './predictions.png'
if os.path.exists(file):
  img = cv2.imread(file)
  show_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
  plt.imshow(show_img)

# + [markdown] id="cAJL-ZfJYXi7" colab_type="text"
# # Drakent to Keras yolo v3

# + id="pTDbdtV7YXi8" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 12770.0} outputId="f5d2bfcb-bb7b-48c1-817e-90c80fccbd15" executionInfo={"status": "ok", "timestamp": 1529167422506.0, "user_tz": -180.0, "elapsed": 80775.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}
# ! python keras-yolo3/convert.py keras-yolo3/yolov3.cfg yolov3.weights keras-yolo3/model_data/yolo.h5



# + id="xlwGiM8Sd3Je" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 394.0} outputId="628fee6a-9c42-4edd-be34-c1ede09f8ad6" executionInfo={"status": "ok", "timestamp": 1529167771703.0, "user_tz": -180.0, "elapsed": 83620.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}
# # ! cp -R keras-yolo3/model_data .

# ! python  keras-yolo3/yolo.py ../sonkey13.jpeg

# + id="zfn1uafXflAD" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}, "base_uri": "https://localhost:8080/", "height": 367.0} outputId="ee421d41-5486-4e8e-e7f8-f00717a4c6c5" executionInfo={"status": "error", "timestamp": 1529168124112.0, "user_tz": -180.0, "elapsed": 955.0, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "110613709458758208639"}}


# + id="n9G68RMSifeD" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}}


# + id="SnqhZzcEhAEE" colab_type="code" colab={"autoexec": {"startup": false, "wait_interval": 0.0}}


StarcoderdataJupyterScriptsDedupFiltered