# Path: studio/fs_tracker.py
# STUDIOML_EXPERIMENT = 'STUDIOML_EXPERIMENT'
# STUDIOML_HOME = 'STUDIOML_HOME'
# STUDIOML_ARTIFACT_MAPPING = 'STUDIOML_ARTIFACT_MAPPING'
# def get_experiment_key():
# def get_studio_home():
# def setup_experiment(env, experiment, clean=True):
# def get_artifact(tag):
# def get_artifacts():
# def get_model_directory(experiment_name=None):
# def get_artifact_cache(tag, experiment_name=None):
# def get_blob_cache(blobkey):
# def get_model_directory(experiment_name=None):
# def _get_artifact_mapping_path(experiment_name=None):
# def _get_experiment_key(experiment):
# def _setup_model_directory(experiment_name, clean=False):
# def get_queue_directory():
# def get_tensorboard_dir(experiment_name=None):
# 
# Path: studio/model_util.py
# class BufferedPipe:
# class ModelPipe:
#     def __init__(self,
#                  func=lambda x: x,
#                  parent=None,
#                  q_in=None,
#                  q_out=None,
#                  num_workers=0,
#                  q_size=None,
#                  batch_size=1,
#                  filterf=lambda x: x is not None,
#                  batcher=lambda x: x,
#                  timeout=1):
#     def __call__(self, data_gen):
#     def add(self, func,
#             num_workers=None,
#             batch_size=None,
#             filterf=None,
#             batcher=None,
#             timeout=None):
#     def _wrapped_func(self, x):
#     def _wrapped_filter(self, x):
#     def __init__(self):
#     def add(self, func,
#             num_workers=None,
#             batch_size=None,
#             filterf=None,
#             batcher=None,
#             timeout=None):
#     def apply_unordered(self, data):
#     def apply_ordered(self, data):
#     def __call__(self, data):
# def resize_to_model_input(model, input_index=0):
#     def _run_resize(input_img):
# def _q2q_batch(
#         func,
#         queue_in,
#         queue_out,
#         filterf=lambda x: x is not None,
#         batch_size=32,
#         timeout=1):
# def _q2q_single(func, queue_in, queue_out, filterf=lambda x: True, timeout=1):
# def _gen2q(data, queue):
# def _q2gen(queue, timeout=1):
# def _prime_keras_model(model):


import sys
import urllib
import numpy as np
from keras.layers import Dense, Flatten
from keras.models import Sequential
from keras.datasets import fashion_mnist
from keras.utils import to_categorical
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras import optimizers
from PIL import Image
from io import BytesIO
from studio import fs_tracker, model_util
y_test = to_categorical(y_test, 10)


model = Sequential()

model.add(Flatten(input_shape=(28, 28, 1)))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='relu'))

model.add(Dense(10, activation='softmax'))
model.summary()


batch_size = 128
no_epochs = int(sys.argv[1]) if len(sys.argv) > 1 else 10
lr = 0.01

print('learning rate = {}'.format(lr))
print('batch size = {}'.format(batch_size))
print('no_epochs = {}'.format(no_epochs))

model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=lr),
              metrics=['accuracy'])


checkpointer = ModelCheckpoint(
    fs_tracker.get_model_directory() +
    '/checkpoint.{epoch:02d}-{val_loss:.2f}.hdf')



# Path: studio/fs_tracker.py
# STUDIOML_EXPERIMENT = 'STUDIOML_EXPERIMENT'
# STUDIOML_HOME = 'STUDIOML_HOME'
# STUDIOML_ARTIFACT_MAPPING = 'STUDIOML_ARTIFACT_MAPPING'
# def get_experiment_key():
# def get_studio_home():
# def setup_experiment(env, experiment, clean=True):
# def get_artifact(tag):
# def get_artifacts():
# def get_model_directory(experiment_name=None):
# def get_artifact_cache(tag, experiment_name=None):
# def get_blob_cache(blobkey):
# def get_model_directory(experiment_name=None):
# def _get_artifact_mapping_path(experiment_name=None):
# def _get_experiment_key(experiment):
# def _setup_model_directory(experiment_name, clean=False):
# def get_queue_directory():
# def get_tensorboard_dir(experiment_name=None):
# 
# Path: studio/model_util.py
# class BufferedPipe:
# class ModelPipe:
#     def __init__(self,
#                  func=lambda x: x,
#                  parent=None,
#                  q_in=None,
#                  q_out=None,
#                  num_workers=0,
#                  q_size=None,
#                  batch_size=1,
#                  filterf=lambda x: x is not None,
#                  batcher=lambda x: x,
#                  timeout=1):
#     def __call__(self, data_gen):
#     def add(self, func,
#             num_workers=None,
#             batch_size=None,
#             filterf=None,
#             batcher=None,
#             timeout=None):
#     def _wrapped_func(self, x):
#     def _wrapped_filter(self, x):
#     def __init__(self):
#     def add(self, func,
#             num_workers=None,
#             batch_size=None,
#             filterf=None,
#             batcher=None,
#             timeout=None):
#     def apply_unordered(self, data):
#     def apply_ordered(self, data):
#     def __call__(self, data):
# def resize_to_model_input(model, input_index=0):
#     def _run_resize(input_img):
# def _q2q_batch(
#         func,
#         queue_in,
#         queue_out,
#         filterf=lambda x: x is not None,
#         batch_size=32,
#         timeout=1):
# def _q2q_single(func, queue_in, queue_out, filterf=lambda x: True, timeout=1):
# def _gen2q(data, queue):
# def _q2gen(queue, timeout=1):
# def _prime_keras_model(model):


# Path: examples/keras/fashion_mnist.py
import sys
import urllib
import numpy as np
from keras.layers import Dense, Flatten
from keras.models import Sequential
from keras.datasets import fashion_mnist
from keras.utils import to_categorical
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras import optimizers
from PIL import Image
from io import BytesIO
from studio import fs_tracker, model_util

y_test = to_categorical(y_test, 10)


model = Sequential()

model.add(Flatten(input_shape=(28, 28, 1)))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='relu'))

model.add(Dense(10, activation='softmax'))
model.summary()


batch_size = 128
no_epochs = int(sys.argv[1]) if len(sys.argv) > 1 else 10
lr = 0.01

print('learning rate = {}'.format(lr))
print('batch size = {}'.format(batch_size))
print('no_epochs = {}'.format(no_epochs))

model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=lr),
              metrics=['accuracy'])


checkpointer = ModelCheckpoint(
    fs_tracker.get_model_directory() +
    '/checkpoint.{epoch:02d}-{val_loss:.2f}.hdf')



tbcallback = TensorBoard(log_dir=fs_tracker.get_tensorboard_dir(),